-
-
Notifications
You must be signed in to change notification settings - Fork 45
Add smooth
sources in Blocks
+ Add Square
and Triangular
sources
#59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
equation = if smooth == false | ||
offset + ifelse(t < start_time, 0, amplitude* sin(2*pi*frequency*(t - start_time) + phase)) | ||
else | ||
δ = 1e-5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this parameter be exposed and documented?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps yes. Although having δ = 1e-6 or such makes a miniscule change to the result, it can upset high precision tests
equation = if smooth == false | ||
offset + ifelse(t < start_time, 0, amplitude* sin(2*pi*frequency*(t - start_time) + phase)) | ||
else | ||
δ = 1e-5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should
src/Blocks/sources.jl
Outdated
end | ||
|
||
eqs = [ | ||
output.u ~ smooth_square(t, δ, frequency, amplitude, offset, start_time) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there no exact version?
src/Blocks/sources.jl
Outdated
eqs = [ | ||
output.u ~ offset + ifelse(t < start_time, 0, amplitude * exp(-damping * (t - start_time)) * sin(2*pi*frequency*(t - start_time) + phase)) | ||
output.u ~ smooth_triangular(t, δ, frequency, amplitude, offset, start_time) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there no exact version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I had tried the solvers became unstable. Let me give another shot at it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave it in. It is an upstream problem than and not of the library. Also add tests but mark it as broken.
Is this an aliasing problem or is the implementation incorrect? |
One option that can be provided alongside the smoothing option is to use callbacks to make sure the solver steps exactly at the discontinuities (in value or derivative etc.) of the signal. The symbolic callbacks should be able to handle these signals, but discrete callbacks may be more efficient given that we know the time points of the events beforehand. |
Cool! |
Yeah I had noted that. The last commit, I was working on, addresses exactly that. (I've updated the comment above with latest waveform) |
I will add this with a follow up PR |
The thermal.jl tests are no longer broken. See the logs here So can I go ahead & change |
If you do, thx. But in a separate PR. 😄 |
Ok. |
It looks like tests fail. Change test broken to test. |
- For `Sine`, `Cosine`, `ExpSine`, `Step`, `Ramp` Add Square and Triangular sources Add a non-zero start_time to smooth Sine, Ramp, ExpSine tests & non-zero `offset` to smooth ExpSine Add "almost" exact versions of Triangular and Square waves The formulae from https://en.wikipedia.org/wiki/Square_wave & https://en.wikipedia.org/wiki/Triangle_wave are modified to accomodate any amplitude, frequency, offset and start_time translate t on x-axis with start_time for square and triangular waves trigger workflow Merge branch 'vk/smooth_blocks' of https://github.com/ven-k/ModelingToolkitStandardLibrary.jl into vk/smooth_blocks thermal tests are no longer broken; so `@test_broken` -> `@test`
Codecov Report
@@ Coverage Diff @@
## main #59 +/- ##
==========================================
+ Coverage 70.12% 71.77% +1.65%
==========================================
Files 23 23
Lines 964 1024 +60
==========================================
+ Hits 676 735 +59
- Misses 288 289 +1
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
src/Blocks/sources.jl
Outdated
offset=0.0, start_time=0.0, smooth=false) | ||
|
||
if smooth | ||
frequency > 25 && @warn "`frequency > 25` can lead to non-triangular wave pattern" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's up with this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps the smoothing constants need to be frequency dependent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For f>25, the waveform is totally erratic
Perhaps the smoothing constants need to be frequency dependent?
I tried these:
amplitude * (1-2acos((1 - f* δ)sin(2π*(x - start_time)*f))/π)
amplitude * (1-2acos((1 - δ/f)sin(2π*(x - start_time)*f))/π)
amplitude * (1-2acos((1 - δ)sin(2π*(x - start_time)*f*δ))/π)
amplitude * (1-2acos((1 - δ)sin(2π*(x - start_time)*f/δ))/π)
amplitude * (1-2acos(sin(2π*(x - start_time)*f)/π)
None of these relations gave a better result.
I think it comes down to trigonometric functions used. Is there any source I can look at for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would imagine that the delta parameter influences how high frequencies can be tolerated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing that didn't make any difference (formula 5 above)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above image is output of just smooth_triangular
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still open which is why I didn't merge. What's the consensus here? I didn't look into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adaptivity should handle it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow it didn't. For offset!=0
similar issue crops up. saveat=0.01
saves. (Here)
src/Blocks/sources.jl
Outdated
@register_symbolic smooth_cos(x, δ, f, amplitude, ϕ, offset, start_time) | ||
@register_symbolic smooth_damped_sin(x, δ, f, amplitude, damping, ϕ, offset, start_time) | ||
@register_symbolic smooth_ramp(x, δ, height, duration, offset, start_time) | ||
@register_symbolic smooth_sin(x, δ, f, amplitude, ϕ, offset, start_time) | ||
@register_symbolic smooth_square(x, δ, f, amplitude, offset, start_time) | ||
@register_symbolic smooth_step(x, δ, height, offset, start_time) | ||
@register_symbolic smooth_triangular(x, δ, f, amplitude, offset, start_time) | ||
@register_symbolic triangular(x, f, amplitude, offset, start_time) | ||
@register_symbolic square(x, f, amplitude, offset, start_time) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why register these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Earlier we didn't assign parameters while declaring them inside components. So @register
was necessary.
But yeah, now its no longer required. Removed
Adds
smooth
option for sources in Blocks. ForSine
,Cosine
,ExpSine
,Step
,Ramp
Adds Square and Triangular sources