Damping of a spring doesn't match analytical solution

Discuss any Chipmunk bugs here.
pwagner
Posts: 21
Joined: Wed Mar 23, 2011 1:59 am
Contact:

Damping of a spring doesn't match analytical solution

Post by pwagner »

Hey

I tried to keep the topic as small as possible but nothing else could describe my problem better. For a project I'm validating the accuracy of chipmunk (exactly I'm using the Python bindings: pymunk) to simulate real physics. I thought a small and simple experiment to get a first glance should be two bodies linked by a spring and watch what will happen. I think it takes longer to explain everything than it took to implement in pymunk :D . The bodies/shapes are two circles with mass=1, distance=2, radius=0.2 and no gravity in the space. The spring is linked directly to the centers of the circles with k=500, rest_length=3 and damping=0.5 . This should be everything to do the simulation.
The analytical solution should look like:
y(time) = y_0 * exp(-damping / (2 * mass) * time) * sin(sqrt(2*k/m - (d / (2 * mass))^2) * time + phi_0) (watch out for 2*k because of the symmetry)
y(time) = 0.5 * exp(-0.5 / 2 * time) * sin(sqrt(1000 - (0.5/2)^2) * time + 0)

I had to find a numerical solution to fit the simulated data which leads to the following parameters:
y(time) = 0.5 * exp(-9.7 / 2 * time) * sin(sqrt(1000 - (9.7/2)^2) * time - pi/1.85)

Does anybody have a satisfying answer for my problem. Do I miss anything?

Best regards
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Damping of a spring doesn't match analytical solution

Post by slembcke »

Yup, there was a thread about this a while back:
http://www.slembcke.net/forums/viewtopi ... 228&p=5712

I fixed the behavior in Chipmunk 6.x to match the real solution.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
pwagner
Posts: 21
Joined: Wed Mar 23, 2011 1:59 am
Contact:

Re: Damping of a spring doesn't match analytical solution

Post by pwagner »

Thanks for the link. I couldn't find it because of the misspelling ;) (And I'm not sure if the searching works the way it should because I neither can find the word 'spring' nor 'damping')
Also I'm in doubt that the factor of 2/e is realy the factor I am encountering but I will give it a try and test both, the factor and the newer version.

After a bit of searching i found the following in your code:

Code: Select all

spring->v_coef = 1.0f - cpfexp(-spring->damping*dt*k*0.5f/(cpFloat)M_E);
So against your posting you are using 1/(2e) and not 2/e?

Thanks again
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Damping of a spring doesn't match analytical solution

Post by slembcke »

Ack. The code I posted in that thread was correct, but not the explanation. (Yay for doing basic algebra in my head when posting) So yes, the factor is 1/2e and not 2/e.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
pwagner
Posts: 21
Joined: Wed Mar 23, 2011 1:59 am
Contact:

Re: Damping of a spring doesn't match analytical solution

Post by pwagner »

Ok that explains the one thing. But I'm still confused because as I tried to raise accuracy by increasing the number of normal and elastic iterations the damping seems to increase linear with the number of iterations. That can't be correct :shock:

Last solution which might work is the new version but I fear that behavior won't change ...
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Damping of a spring doesn't match analytical solution

Post by slembcke »

Well crap. You seem to be correct on the iterations thing. I'd like to say that I broke that recently somehow, but it looks like it's just been broken that way forever. I have a fix for that coming shortly. I'm checking if that explains the magic 1/2e coefficient that I came up with.

Also, don't use elastic iterations anymore. They've been deprecated for quite some time. They aren't required anymore and actually will make the quality of the solution worse.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Damping of a spring doesn't match analytical solution

Post by slembcke »

Ok! So yeah, that stupid 1/2e coefficient was simply because I tested it with 10 iterations. It doesn't work for any other number of iterations, and original code was actually correct for <= 2 iterations. I never could explain the math for why that coefficient was needed. Apparently I just needed to look a little harder at the code.

I committed the fixes to Chipmunk 6, but not to Chipmunk 5.x for the same reason as before. I don't want to break people's hand tweaked spring settings with a minor version upgrade.

I do appreciate the sanity check you are doing by the way.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
pwagner
Posts: 21
Joined: Wed Mar 23, 2011 1:59 am
Contact:

Re: Damping of a spring doesn't match analytical solution

Post by pwagner »

Well then thanks for the answers. As I explained before I'm trying to use a physics engine like chipmunk to simulate real physics. Therefore I have to compare some simple but meaningful physical experiments and the analytical solutions.

As for the coefficient: The decay time of a damped oszillation is the time the amplitude of the oszillator decreased to 1/e. I think this should explain your factor if you have somehow normed the decay time to half the starting amplitude and not to 1/e. But that was just my first thought as I read your answers.

As the only logical solution my next step is the replacement of the out-dated chipmunk version and rerun my experiments there. If you are interested in the outcomming I could open a new thread and post there some updates.

Have a nice day
pwagner
Posts: 21
Joined: Wed Mar 23, 2011 1:59 am
Contact:

Re: Damping of a spring doesn't match analytical solution

Post by pwagner »

So after one day of reviewing your code ;) I think the springs should do. As reference material I used the book Game Physics by David H. Eberly (who uses Jacobians and was therefore just a hint to the solution) and a nice presentation of Erin Catto 'Soft Constraints, Reinventing the Spring'

The experiments will follow as soon as possible.

Best regards
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Damping of a spring doesn't match analytical solution

Post by slembcke »

I skimmed Erin's paper a couple months ago. I didn't look very closely at his math, but I think my implementation is pretty different. It was definitely a cool idea to adjust the spring based on the natural frequency and the critical damping ratio instead straight stiffness and damping.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests