-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBlogEulerCromerTimeStep.html
More file actions
459 lines (387 loc) · 14 KB
/
Copy pathBlogEulerCromerTimeStep.html
File metadata and controls
459 lines (387 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<!DOCTYPE html>
<html>
<head>
<title>Encino Sim Class : The Euler-Cromer Time Step</title>
<style type="text/css" title="currentStyle">
@import "css/SimClass.css";
</style>
<script src='http://processingjs.org/js/processing.min.js' type='text/javascript'/></script>
<!Install the MathJax stuff so we can show LaTeX>
<script type="text/javascript"
src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</head>
<body>
<h2>Forward Euler Problems</h2>
In the previous class, we observed that a Forward Euler time step quickly
produced an unusable solution to our simple spring system. This illustration
from the <a href="http://en.wikipedia.org/wiki/Euler_method">
Wikipedia entry on Forward Euler</a> illustrates how the method works,
and also why its solutions drift away from the correct result.
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/a/ae/Euler_method.png/613px-Euler_method.png">
<p>
In this illustration, you can see that at each point, the slope of the function
is used to construct a line segment that estimates the position of the next point.
The error in each estimate is accumulated with each subsequent estimate, and the solution
veers off course. In order to study these systems better, we first need some debug tools.
<h2>Adding Debug Tools to our Spring Simulation</h2>
The Forward Euler Time Step that we added to the Spring Simulation in the
previous class produced an oscillatory motion, but did so in a way that
produced an unstable result, with the mass oscillating wildly off screen
in a short period of time.
<p>
The instability shown by our simple spring simulation is a constant problem
for most simulations. It's easy to see the problem in this simple example
(which is why we're using it!), but the problem affects large-scale fluid
and cloth sims, rigid body sims, and so on. Strangely, despite the general
knowledge that Forward Euler is not a good time integration tool, it is still
used in many commercial tools, including Maya and Houdini.
<p>
In order to analyze our improvements, we first need a measuring stick. To quote
my good friend Adam Shand, "You can't improve what you can't measure.". We
chose the simple spring because it did have an exact answer. Therefore, we'll
add some code to our drawing routine that draws the right answer just above
the simulated spring. Here's that code - note that this uses the expression
for the explicit position of the spring that we established in the previous
class. We'll also add some text to the top of the drawn image to remind
us which time integration we're using.
<pre><code>
void DrawState()
{
// Compute end of arm.
float SpringEndX = PixelsPerMeter * State[StatePositionX];
// Compute the CORRECT position.
float sqrtKoverM = sqrt( Stiffness / BobMass );
float x0 = InitState[StatePositionX];
float v0 = InitState[StateVelocityX];
float t = State[StateCurrentTime];
float CorrectPositionX = ( x0 * cos( sqrtKoverM * t ) ) +
( ( v0 / sqrtKoverM ) * sin( sqrtKoverM + t ) );
// Compute draw pos for "correct"
float CorrectEndX = PixelsPerMeter * State[CorrectPositionX];
// Draw the spring.
strokeWeight( 1.0 );
line( 0.0, 0.0, SpringEndX, 0.0 );
// Draw the spring pivot
fill( 0.0 );
ellipse( 0.0, 0.0,
PixelsPerMeter * 0.03,
PixelsPerMeter * 0.03 );
// Draw the spring bob
fill( 1.0, 0.0, 0.0 );
ellipse( SpringEndX, 0.0,
PixelsPerMeter * 0.1,
PixelsPerMeter * 0.1 );
// Draw the correct bob in blue
fill( 0.0, 0.0, 1.0 );
ellips( CorrectEndX, -PixelsPerMeter * 0.25,
PixelsPerMeter * 0.1,
PixelsPerMeter * 0.1 );
}
</code></pre>
<p>
And here that is:
<p>
<script type="application/processing" data-processing-target="pjsSpringSimp3">
float Stiffness = 5.0;
float BobMass = 0.5;
int StateSize = 3;
float[] InitState = new float[StateSize];
float[] State = new float[StateSize];
int StateCurrentTime = 0;
int StatePositionX = 1;
int StateVelocityX = 2;
int WindowWidthHeight = 300;
float WorldSize = 2.0;
float PixelsPerMeter;
float OriginPixelsX;
float OriginPixelsY;
void setup()
{
// Create initial state.
InitState[StateCurrentTime] = 0.0;
InitState[StatePositionX] = 0.65;
InitState[StateVelocityX] = 0.0;
// Copy initial state to current state.
// notice that this does not need to know what the meaning of the
// state elements is, and would work regardless of the state's size.
for ( int i = 0; i < StateSize; ++i )
{
State[i] = InitState[i];
}
// Set up normalized colors.
colorMode( RGB, 1.0 );
// Set up the stroke color and width.
stroke( 0.0 );
//strokeWeight( 0.01 );
// Create the window size, set up the transformation variables.
size( WindowWidthHeight, WindowWidthHeight );
PixelsPerMeter = (( float )WindowWidthHeight ) / WorldSize;
OriginPixelsX = 0.5 * ( float )WindowWidthHeight;
OriginPixelsY = 0.5 * ( float )WindowWidthHeight;
textSize( 24 );
}
// Draw our State, with the unfortunate units conversion.
void DrawState()
{
// Compute end of arm.
float SpringEndX = PixelsPerMeter * State[StatePositionX];
// Compute the CORRECT position.
float sqrtKoverM = sqrt( Stiffness / BobMass );
float x0 = InitState[StatePositionX];
float v0 = InitState[StateVelocityX];
float t = State[StateCurrentTime];
float CorrectPositionX = ( x0 * cos( sqrtKoverM * t ) ) +
( ( v0 / sqrtKoverM ) * sin( sqrtKoverM + t ) );
// Compute draw pos for "correct"
float CorrectEndX = PixelsPerMeter * CorrectPositionX;
// Draw the spring.
strokeWeight( 1.0 );
line( 0.0, 0.0, SpringEndX, 0.0 );
// Draw the spring pivot
fill( 0.0 );
ellipse( 0.0, 0.0,
PixelsPerMeter * 0.03,
PixelsPerMeter * 0.03 );
// Draw the spring bob
fill( 1.0, 0.0, 0.0 );
ellipse( SpringEndX, 0.0,
PixelsPerMeter * 0.1,
PixelsPerMeter * 0.1 );
// Draw the correct bob in blue
fill( 0.0, 0.0, 1.0 );
ellipse( CorrectEndX, -PixelsPerMeter * 0.25,
PixelsPerMeter * 0.1,
PixelsPerMeter * 0.1 );
}
// Time Step function.
void TimeStep( float i_dt )
{
// Compute acceleration from current position.
float A = ( -Stiffness / BobMass ) * State[StatePositionX];
// Update position based on current velocity.
State[StatePositionX] += i_dt * State[StateVelocityX];
// Update velocity based on acceleration.
State[StateVelocityX] += i_dt * A;
// Update current time.
State[StateCurrentTime] += i_dt;
}
// Processing Draw function, called every time the screen refreshes.
void draw()
{
// Time Step.
TimeStep( 1.0/24.0 );
// Clear the display to a constant color.
background( 0.75 );
// Label.
fill( 1.0 );
text( "Forward Euler", 10, 30 );
pushMatrix();
// Translate to the origin.
translate( OriginPixelsX, OriginPixelsY );
// Draw the simulation
DrawState();
}
// Reset function. If the key 'r' is released in the display,
// copy the initial state to the state.
void keyReleased()
{
if ( key == 114 )
{
for ( int i = 0; i < StateSize; ++i )
{
State[i] = InitState[i];
}
}
}
</script>
<canvas id="pjsSpringSimp3"> </canvas>
<p>
I also added a "reset" capability to the simple spring code by using
Processing's keyRelease function. In response to the key 'r' being released
in the view (which has ascii-code 114), we copy the initial state onto the
state. (See how handy state vectors are turning out to be!) Because we
included the Current Time in our state vector, this is easy:
<pre><code>
// Reset function. If the key 'r' is released in the display,
// copy the initial state to the state.
void keyReleased()
{
if ( key == 114 )
{
for ( int i = 0; i < StateSize; ++i )
{
State[i] = InitState[i];
}
}
}
</code></pre>
<h2>The Euler-Cromer Time Step</h2>
When we created the forward finite difference approximation to our
<b>ODE</b> equation of motion in the previous class, we had to create two
forward difference equations - one for velocity from acceleration, and then
one for position from velocity. As you remember, we computed acceleration
from the current position, then updated position based on the current
velocity, and then finally updated velocity based on the acceleration.
What if we updated velocity first, and then updated the position from the
current velocity? That would look like this:
<div class="LaTexEquation">
\(
a( t ) = f( t, x( t ) ) = \frac{-k}{m} x( t ) \\
v( t + \Delta t ) \approx v( t ) + \Delta t a( t ) \\
x( t + \Delta t ) \approx x( t ) + \Delta t v( t + \Delta t )
\)
</div>
This seems almost trivially different from our Forward-Euler time step. This
method is called many different things:
<a href="http://en.wikipedia.org/wiki/Semi-implicit_Euler_method">
Semi-Implicit Euler Method</a>, Symplectic Euler, Semi-Explicit Euler,
Euler-Cromer, and Newton–Størmer–Verlet (NSV) (at least). One of the things
I've found in years of writing simulations is that just learning the names
of all these different techniques, and learning that the different names
often mean exactly the same thing, is a huge part of simplifying the giant
mess of understanding.
<p>
The code changes to implement Euler Cromer are trivial, we just reorder the
time step - literally just swap the velocity update line and the position
update line:
<p>
<pre><code>
// Euler-Cromer Time Step function.
void TimeStep( float i_dt )
{
// Compute acceleration from current position.
float A = ( -Stiffness / BobMass ) * State[StatePositionX];
// Update velocity based on acceleration.
State[StateVelocityX] += i_dt * A;
// Update position based on current velocity.
State[StatePositionX] += i_dt * State[StateVelocityX];
// Update current time.
State[StateCurrentTime] += i_dt;
}
</code></pre>
<p>
And let's see how this ONE LINE (okay, TWO-LINE) change affects the sim:
<p>
<script type="application/processing" data-processing-target="pjsSpringSimp4">
float Stiffness = 5.0;
float BobMass = 0.5;
int StateSize = 3;
float[] InitState = new float[StateSize];
float[] State = new float[StateSize];
int StateCurrentTime = 0;
int StatePositionX = 1;
int StateVelocityX = 2;
int WindowWidthHeight = 300;
float WorldSize = 2.0;
float PixelsPerMeter;
float OriginPixelsX;
float OriginPixelsY;
void setup()
{
// Create initial state.
InitState[StateCurrentTime] = 0.0;
InitState[StatePositionX] = 0.65;
InitState[StateVelocityX] = 0.0;
// Copy initial state to current state.
// notice that this does not need to know what the meaning of the
// state elements is, and would work regardless of the state's size.
for ( int i = 0; i < StateSize; ++i )
{
State[i] = InitState[i];
}
// Set up normalized colors.
colorMode( RGB, 1.0 );
// Set up the stroke color and width.
stroke( 0.0 );
//strokeWeight( 0.01 );
// Create the window size, set up the transformation variables.
size( WindowWidthHeight, WindowWidthHeight );
PixelsPerMeter = (( float )WindowWidthHeight ) / WorldSize;
OriginPixelsX = 0.5 * ( float )WindowWidthHeight;
OriginPixelsY = 0.5 * ( float )WindowWidthHeight;
textSize( 24 );
}
// Draw our State, with the unfortunate units conversion.
void DrawState()
{
// Compute end of arm.
float SpringEndX = PixelsPerMeter * State[StatePositionX];
// Compute the CORRECT position.
float sqrtKoverM = sqrt( Stiffness / BobMass );
float x0 = InitState[StatePositionX];
float v0 = InitState[StateVelocityX];
float t = State[StateCurrentTime];
float CorrectPositionX = ( x0 * cos( sqrtKoverM * t ) ) +
( ( v0 / sqrtKoverM ) * sin( sqrtKoverM + t ) );
// Compute draw pos for "correct"
float CorrectEndX = PixelsPerMeter * CorrectPositionX;
// Draw the spring.
strokeWeight( 1.0 );
line( 0.0, 0.0, SpringEndX, 0.0 );
// Draw the spring pivot
fill( 0.0 );
ellipse( 0.0, 0.0,
PixelsPerMeter * 0.03,
PixelsPerMeter * 0.03 );
// Draw the spring bob
fill( 1.0, 0.0, 0.0 );
ellipse( SpringEndX, 0.0,
PixelsPerMeter * 0.1,
PixelsPerMeter * 0.1 );
// Draw the correct bob in blue
fill( 0.0, 0.0, 1.0 );
ellipse( CorrectEndX, -PixelsPerMeter * 0.25,
PixelsPerMeter * 0.1,
PixelsPerMeter * 0.1 );
}
// Euler-Cromer Time Step function.
void TimeStep( float i_dt )
{
// Compute acceleration from current position.
float A = ( -Stiffness / BobMass ) * State[StatePositionX];
// Update velocity based on acceleration.
State[StateVelocityX] += i_dt * A;
// Update position based on current velocity.
State[StatePositionX] += i_dt * State[StateVelocityX];
// Update current time.
State[StateCurrentTime] += i_dt;
}
// Processing Draw function, called every time the screen refreshes.
void draw()
{
// Time Step.
TimeStep( 1.0/24.0 );
// Clear the display to a constant color.
background( 0.75 );
// Label.
fill( 1.0 );
text( "Euler-Cromer", 10, 30 );
pushMatrix();
// Translate to the origin.
translate( OriginPixelsX, OriginPixelsY );
// Draw the simulation
DrawState();
}
// Reset function. If the key 'r' is released in the display,
// copy the initial state to the state.
void keyReleased()
{
if ( key == 114 )
{
for ( int i = 0; i < StateSize; ++i )
{
State[i] = InitState[i];
}
}
}
</script>
<canvas id="pjsSpringSimp4"> </canvas>
<p>
This is not bad! Our simulation seems to have become stable, which is good
news. However, if we watch the simulation for a while, it becomes clear that
the simulated positions diverge more and more over time from the correct
positions. In order to fix this, we're going to need to explore even
higher-order time integration techniques, which is the topic of the next
class!
</body>