-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
yampa
: improve accuracy of integral
#263
Comments
Which specific issue were you trying to point to? Is this a problem only in dunai or also in Yampa? |
@idontgetoutmuch Please let us know the details. I'll close this issue in a couple of days otherwise. Thanks! |
With
I get But with
I get what I expect Full code is here: https://github.com/idontgetoutmuch/phd-jonathan-thaler/commits/literate-haskell-for-review. In particular, it should be possible to run this: https://github.com/idontgetoutmuch/phd-jonathan-thaler/blob/literate-haskell-for-review/SIRABM.lhs#L262. |
Thanks! Based on past simulations, I think this is a problem with Yampa. If we can identify the problem there, we should fix it in Yampa and then just make bearriver adopt the same solution as Yampa (or one that is extensionally equivalent). |
This (lifted from https://github.com/miguel-negrao/test-integral/blob/main/app/Main.hs) works but I don't know how you could apply this to Yampa (thanks @miguel-negrao).
|
Maybe this?
|
Isn't this a duplicate of ivanperez-keera/dunai#268 ? |
Probably |
I made the change and with a test program it works. Here's my test
Here's the problem and after the fix But if I run the yampa tests then they just hang :-( I have put a PR here so you can try it yourself: #262 |
I don't know what to do about tests that just hang. I don't understand yampa sufficiently to debug it. |
This issue pertains to Yampa. I'm transferring it there. |
There are some small errors (the height going over 15 or under 0). What happens if you run this for a really long time? Do those always cancel out or do they accumulate? |
integral
is numerically unstableyampa
: integral
is numerically unstable
yampa
: integral
is numerically unstableyampa
: improve accuracy of integral
@idontgetoutmuch Sorry, this may be a bit off-topic. Could you please tell me how you generate these signal diagrams with Yampa. Very thanks. |
@chansey97 Full code is here: https://github.com/idontgetoutmuch/phd-jonathan-thaler/commits/literate-haskell-for-review. In particular, it should be possible to run this: https://github.com/idontgetoutmuch/phd-jonathan-thaler/blob/literate-haskell-for-review/SIRABM.lhs#L262. |
I'd like to move this issue forward. Would someone be willing to help by putting together a test that measures the difference between the old integral and the new integral for several input functions with known integrals, and with varying sampling rates? I'm thinking of something that will measure the distance between each implementation and the known integral, for example, measure |
I wrote a quick naive version of it. Was this more or less what you meant?
As far as I can tell the new version is better for most if not all cases. |
I don't have time to think about it further at the moment but why not make the integration method a function that can be passed in (with a default)? I am thinking here about symplectic methods for differential equations. Also we ought to be able to estimate the error for given methods anyway (although a test is also nice). There will be a performance / accuracy pay-off of course. |
The current implementation of integral has a tendency to diverge. This commonly manifests in games as bouncing elements that bounce more and more over time, or springs that bounce harder. In the general case, the energy of the simulated system increases over time. This commit re-implements the integral function to use the midpoint rule. The new integral is more accurate and does not increase over time in the same way that the rectangle rule does. Co-authored-by: Dominic Steinitz <dominic@steinitz.org> Co-authored-by: Miguel Negrao <miguel.negrao@friendlyvirus.org>
Surprisingly, I get a |
This is because integral now depends on the value at the current time, when before it only depended on the value in the past. There may be other games that break in the same way due to circular dependencies in arrow blocks involving |
The current implementation of integral has a tendency to diverge. This commonly manifests in games as bouncing elements that bounce more and more over time, or springs that bounce harder. In the general case, the energy of the simulated system increases over time. This commit introduces a new integral function that uses the trapezoid rule. The new integral can be more accurate and does not increase over time in the same way that the rectangle rule does. Co-authored-by: Dominic Steinitz <dominic@steinitz.org> Co-authored-by: Miguel Negrao <miguel.negrao@friendlyvirus.org>
This will likely impact users, who did not expect integral to change behavior in a way that can create loops. For now, and given the fact that the module defines already an immediate integral It's unclear to me how much this "belongs" inside Yampa: the idea of having different integrals, as opposed to just one integral, crosses the denotational barrier (it makes users think about sampling, which we should avoid). |
A general problem with this and probably other FRP approaches is that there is no one true integrator. You may want a symplectic integrator for certain applications e.g. simulating planetary motion otherwise the total energy does not remain constant. Could we have something like I will see if I can find some time to give an example of what I mean. |
This commit adds a test for the new trapezoid integral. The test is based on the existing integral.
Are you talking about |
Well I wasn't but then one can write
Is there a reason this is not done? Also I think a rename and moving the function from the section marked differentiation to the section marked integration would help folks find it more easily. Caveat: this did not seem to give the same bouncing ball. |
Although both are wrong amusingly. |
I just noticed you already implemented this: ec98d47#diff-c6bfd4c004c917ec92d44679b2ae331cc843a5e9fab57eddb39e9bae37fc7d68R79 |
What do you think of a move to the integration section and a rename? I could submit a PR. So |
I suspect that's because, in
Perhaps it needs to be somewhere else completely. |
Luckily I found this https://github.com/ivanperez-keera/dunai/issues?q=is%3Aissue+is%3Aclosed. Without it the bouncing ball does not bounce for long.
EDIT (by Ivan Perez): This issue has been transferred from the dunai repo to the Yampa repo.
The text was updated successfully, but these errors were encountered: