-
Notifications
You must be signed in to change notification settings - Fork 11
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
Time dependent modifiers #522
Conversation
Not sure I understood this. Can you elaborate and/or give an example of what you mean? |
If the functions only receive step sizes, any function that depends on absolute time would have to maintain an accumulated sum of time steps. This makes it a bit impractical to use lambdas for such cases. But let's keep it as-is for now and see how it works out. It's just a matter of adding an additional argument anyway. |
Example:
|
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.
Very nice and clean solution!
Ok, I see now. Then I agree with you. This is something users will simply have to understand and accept if they use modifiers. |
OK, done. :) |
test/cpp/ramp_modifier_test.cpp
Outdated
double slope = 1.0; | ||
double* accumulator = &acc_; | ||
f_ = std::function<double(double, cse::duration)>( | ||
[=](double original, cse::duration deltaT) { |
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.
You don't need the accumulator
pointer, you can just use acc_
directly inside the lambda, because this
is implicitly captured when you use [=]
.
Alternatively, if you want to avoid having to refer to an outside variable, you can add it to the lambda directly:
[=, acc = 0.0](double original, cse::duration deltaT) mutable { /* use and modify acc here */ }
If you keep your solution or use the former of mine, I suggest that you make the class noncopyable and nonmovable. Otherwise, the lambda will have a pointer to the class instance, and if the instance inadvertently gets moved to a different memory location, you're in trouble. (For a simple test this is no big risk, but it's a good habit to always do this when you leak this
pointers to lambdas.)
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.
Great stuff, thanks! I learnt a lot from that. According to the fountain of wisdom making the class noncopyable also makes it nonmovable.
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.
Lots of good advice in that SO answer that I wasn't aware of, thanks! Time to go undelete all of my deleted move constructors, brb. 😄
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.
Clean solution and well documented with the test 👍
… in capture list, make lambda mutable.
This PR is a different take on PR #463, aiming to solve #203. The difference this time is that the API now supports modifier functions which depend on time information, and that these only need to be set once.
Premise:
In order to achieve this, two changes are necessary:
Discussion needed: The last point breaks the "simulation correctness" principle discussed earlier when connecting a slow simulator to a faster one (but only if setting a modifier function). Then again, if you are modifying a variable, simulation correctness is probably not your top priority.
The modifier functions now take the form of
where
cse::duration
represents the step size for "this step". I tried finding a reason/use case for also including the current simulation time, but was unsuccessful, so I left it out.