├── README.md ├── _config.yml ├── LICENSE ├── index.md └── examples ├── Newton_solver.html ├── sdf_sphere_plot.html ├── sdf_box_plot.html ├── sph_approximation_user_fct.html ├── heat_equation.html ├── particle_system.html ├── sph_approximation.html ├── particle_system_rb.html ├── sph_kernel.html ├── deformation_gradient_strain.html ├── time_integration.html ├── sph_gradient_approximation.html ├── neighborhood_search.html ├── spring_plot.html ├── mass_spring_system.html └── pbd.html /README.md: -------------------------------------------------------------------------------- 1 | # Physics Simulation 2 | 3 | [Web page](https://interactivecomputergraphics.github.io/physics-simulation) 4 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-slate 2 | title: Physics Simulation in Visual Computing 3 | deacription: Tutorial on Physics Simulation in Visual Computing 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Interactive Computer Graphics 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.md: -------------------------------------------------------------------------------- 1 | # About 2 | 3 | Physics simulation is an important research topic in visual computing. It has many applications ranging from such as virtual prototyping, training simulators, robotics, animation software for digital production including visual effects in film and animation movies, and computer games – just to mention a few. 4 | 5 | At [RWTH Aachen university](https://animation.rwth-aachen.de) I offer [lectures](https://animation.rwth-aachen.de/courses/) which give an introduction to state-of-the-art simulation methods for rigid bodies, deformable solids and fluids in the area of visual computing (e.g, the Finite Element Method (FEM) and the [Smoothed Particle Hydrodynamics (SPH)](https://interactivecomputergraphics.github.io/SPH-Tutorial/) approach). For the lectures I programmed several simulation examples using JavaScript. 6 | 7 | Why JavaScript? Because everybody can directly run the examples in the browser. Moreover, I was able to add a short web page for each example which explains the method. 8 | 9 | I hope the examples and their documentation help you to learn something about the simulation methods. Feel free to use the examples in your own courses. 10 | 11 | # General 12 | 13 | * [Newton's method](examples/Newton_solver.html) 14 | * [Heat equation](examples/heat_equation.html) 15 | 16 | # Particles 17 | 18 | * [A simple particle system](examples/particle_system.html) 19 | * [A simple particle system where each particle is represented by a rigid body with a rotation and an angular velocity](examples/particle_system_rb.html) 20 | 21 | # Time Integration 22 | 23 | * [Comparison of different explicit time integration methods for a constant force](examples/time_integration.html) 24 | * [Comparison of different explicit and implicit time integration methods for a spring model](examples/spring_plot.html) 25 | * [Energy conservation of a spring-damper model](examples/spring_damper.html) 26 | 27 | # Deformable Solids 28 | 29 | ### Mass Spring System 30 | 31 | * [Mass Spring System](examples/mass_spring_system.html) 32 | 33 | ### Continuum Mechanics 34 | 35 | * [Computation of deformation gradient and strain tensor](examples/deformation_gradient_strain.html) 36 | * [Finite Element Method (FEM) - Corotated Linear Elasticity Model](examples/FEM_Corot.html) 37 | * [Finite Element Method (FEM) - St. Venant-Kirchhoff Model](examples/FEM_StVK.html) 38 | * [Finite Element Method (FEM) - Neohookean Elasticity Model](examples/FEM_Neohookean.html) 39 | 40 | ### Impulse-Based Dynamic Simulation 41 | 42 | * [Impulse-Based Dynamic Simulation (IBDS)](examples/ibds.html) 43 | * [Impulse-based collision handling for rigid bodies](examples/rigid_body_collision.html) 44 | 45 | ### Position-Based Dynamics 46 | 47 | * [Position-Based Dynamics (PBD)](examples/pbd.html) 48 | * [eXtended Position-Based Dynamics (XPBD)](examples/xpbd.html) 49 | * [Shape Matching](examples/shape_matching.html) 50 | * [Region-Based Shape Matching](examples/region_based_shape_matching.html) 51 | 52 | # Fluids 53 | 54 | ### SPH Simulation Methods 55 | 56 | * [Weakly Compressible SPH (WCSPH)](examples/wcsph.html) 57 | * [Position Based Fluids (PBF)](examples/pbf.html) 58 | * [Implicit Incompressible SPH (IISPH)](examples/iisph.html) 59 | * [Divergence-Free SPH (DFSPH)](examples/dfsph.html) 60 | 61 | ### Neighborhood Search 62 | 63 | * [Neighborhood search](examples/neighborhood_search.html) 64 | 65 | ### SPH Kernel 66 | 67 | * [Different kernel functions and their derivatives](examples/sph_kernel.html) 68 | 69 | ### SPH Approximation 70 | 71 | * [The SPH approximation of different functions and the benefit of a Shepard filter](examples/sph_approximation.html) 72 | * [The SPH approximation of a user-defined function and the benefit of a Shepard filter](examples/sph_approximation_user_fct.html) 73 | * [SPH approximation of the gradient of a quadratic function and the benefit of a kernel gradient correction](examples/sph_gradient_approximation.html) 74 | 75 | # Collision Detection 76 | 77 | ### Signed Distance Fields (SDFs) 78 | 79 | * [Signed distance function of a sphere](examples/sdf_sphere_plot.html) 80 | * [Signed distance function of a box](examples/sdf_box_plot.html) -------------------------------------------------------------------------------- /examples/Newton_solver.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | 23 | Newton solver 24 | 25 | 26 |
27 |

Newton solver

28 | 29 | 30 | 31 | 34 | 35 | 36 | 57 | 58 | 59 | 70 |
32 |
33 |
37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 55 | 56 |
54 |
60 |

Newton's method

61 | 62 |

Newton's method is an iterative approach to find the roots of a function. The method requires the function $f(x)$, its derivative $f'(x)$ and an initial guess $x_0$.

63 | 64 |

In each iteration the approximation of the solution is improved by:

65 | $$\begin{equation*} 66 | x_{n+1} = x_{n} - \frac{f(x_n)}{f'(x_n)} 67 | \end{equation*}$$ 68 |

If the initial guess $x_0$ is close enough to the solution and $f'(x_n) \neq 0$, the method usually converges.

69 |
71 | 72 |
73 | 74 | 227 | 228 | 229 | 230 | -------------------------------------------------------------------------------- /examples/sdf_sphere_plot.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | Signed Distance Function - Sphere 23 | 24 | 25 |
26 |

Signed Distance Function - Sphere

27 | 28 | 29 | 30 | 31 | 34 | 48 | 49 | 64 |
32 | Your browser does not support the HTML5 canvas tag. 33 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 |
0
47 |
50 |

Signed distance function of a sphere:

51 | The signed distance function of a sphere with radius $r$ is defined as: 52 | $$\Phi(\mathbf{x}) = \| \mathbf{x} - \mathbf{x}_{\text{sphere}} \| - r.$$ 53 | In the example $\mathbf x$ is the red point, $\mathbf{x}_{\text{sphere}}$ the blue point and the closest point to $\mathbf x$ on the surface of the sphere is rendered in yellow. 54 | 55 |

Surface normal vector

56 | 57 | The normal vector is defined by the gradient of the signed distance function: 58 | $$\mathbf n = \frac{\partial \Phi(\mathbf{x})}{\partial \mathbf x} = \frac{\mathbf{x} - \mathbf{x}_{\text{sphere}}}{\| \mathbf{x} - \mathbf{x}_{\text{sphere}}\|}. $$ 59 | 60 |

Closest point on the surface

61 | The closest point $\mathbf s$ on the surface of the sphere (yellow) can be determined by starting at the point $\mathbf x$ (red) and going by the signed distance in the direction of the negative normal vector: 62 | $$\mathbf s = \mathbf x - \Phi(\mathbf{x}) \mathbf n.$$ 63 |
65 | 66 |
67 | 68 | 317 | 318 | 319 | 320 | -------------------------------------------------------------------------------- /examples/sdf_box_plot.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | Signed Distance Function - Box 23 | 24 | 25 |
26 |

Signed Distance Function - Box

27 | 28 | 29 | 30 | 31 | 34 | 48 | 49 | 76 |
32 | Your browser does not support the HTML5 canvas tag. 33 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 |
0
47 |
50 |

Signed distance function of a box:

51 | The signed distance function of a box with width $w$ and height $h$ is defined as: 52 | $$\Phi(\mathbf{x}) = \min(\max(d_x,d_y),0.0) + \left \|\begin{pmatrix} \max(d_x,0.0) \\ \max(d_y,0.0) \end{pmatrix} \right \| - \text{tolerance},$$ 53 | where 54 | $$d = \begin{pmatrix} |x - x_{\text{box}}| - 0.5 w \\ |y - y_{\text{box}}| - 0.5 h \end{pmatrix}$$. 55 | In the example $\mathbf x$ is the red point, $\mathbf{x}_{\text{box}}$ the blue point and the closest point to $\mathbf x$ on the surface of the sphere is rendered in yellow. 56 | 57 |

Surface normal vector

58 | 59 |

The normal vector is approximated using central differences, where the derivative of a function $f(x)$ is approximated by 60 | $$ 61 | \frac{\partial f(x)}{\partial x} \approx \frac{f(x+\varepsilon) - f(x-\varepsilon)}{2\varepsilon}, 62 | $$ 63 | where $\varepsilon$ is a small constant (in our example $\varepsilon = 10^{-6}).

64 | 65 |

Since the normal vector is defined as 66 | $$ 67 | \mathbf n = \frac{\partial \Phi(\mathbf{x})}{\mathbf x}, 68 | $$ 69 | the normal can be simply approximated by applying central differences on the x- and the y-component of the function $\Phi(\mathbf{x})$. 70 |

71 | 72 |

Closest point on the surface

73 | The closest point $\mathbf s$ on the surface of the box (yellow) can be determined by starting at the point $\mathbf x$ (red) and going by the signed distance in the direction of the negative normal vector: 74 | $$\mathbf s = \mathbf x - \Phi(\mathbf{x}) \mathbf n.$$ 75 |
77 | 78 |
79 | 80 | 357 | 358 | 359 | 360 | -------------------------------------------------------------------------------- /examples/sph_approximation_user_fct.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | 23 | SPH Approximation 24 | 25 | 26 |
27 |

SPH Approximation

28 | 29 | 30 | 31 | 32 | 35 | 38 | 39 | 40 | 64 | 65 | 68 | 69 | 84 |
33 |
34 |
36 |
37 |
41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
0.025
66 | (Note you can use expressions like Math.sin or Math.sqrt in the function field.) 67 |
70 |

SPH Approximation of different functions:

71 | In this example a user-defined function is discretized using SPH with the cubic spline kernel [KBST19]. The left plot shows the particle sampling pattern while the right plot shows the exact functions and their SPH approximations. To compute the SPH approximations the function values are sampled along the red line in the left plot. That means that for each point on the line the neighbors are determined and the SPH approximation formula is applied: 72 | $$\langle f(x,y) \rangle = \sum_j \frac{m_j}{\rho_j} f(x_j,y_j) W_{ij}$$ 73 | 74 |

Shepard filter

75 | The SPH approximation of a fluid quantity is error-prone if the particle neighborhood is only partially filled. To consider this in the computation the smoothing kernel can be corrected by a factor $s_i$: 76 | $$\tilde {W}_{ij} = s_i W_{ij} = \frac{1}{\sum_k \frac{m_k}{\rho_k} W_{ik}} W_{ij}.$$ 77 | 78 | 79 |

References

80 |
    81 |
  • [KBST19] Dan Koschier, Jan Bender, Barbara Solenthaler, Matthias Teschner. Smoothed Particle Hydrodynamics for Physically-Based Simulation of Fluids and Solids. Eurographics Tutorial, 2019
  • 82 |
83 |
85 | 86 |
87 | 88 | 410 | 411 | 412 | 413 | -------------------------------------------------------------------------------- /examples/heat_equation.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | Heat Equation — 2D Diffusion Example 23 | 24 | 25 |
26 |

Heat Equation — 2D Diffusion Example

27 | 28 | 29 | 30 | 31 | 34 | 64 | 65 | 93 |
32 | Your browser does not support the HTML5 canvas tag. 33 | 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 |
0.00 s
0.00 ms
63 |
66 |

Heat diffusion example

67 |

68 | This example demonstrates a simple explicit finite-difference simulation of the 2D heat equation (diffusion). 69 | The simulation stores a temperature field on a lower-resolution grid for performance; the canvas displays the field upscaled. 70 |

71 | 72 |

Initial condition

73 |

The temperature is initially zero everywhere except a centered hot box (set to 100). You can restart the simulation with the Restart button.

74 | 75 |

Model (continuous)

76 |

The heat equation solved is 77 | \[ \frac{\partial T}{\partial t} = D \nabla^2 T, \] 78 | where \(D\) is the diffusivity.

79 | 80 |

Numerical method

81 |

Space is discretized on a uniform Cartesian grid with spacing \(\Delta x\) (we assume \(\Delta x = \Delta y\) here). Let \(T_{i,j}^n\) denote the temperature at grid cell \((i,j)\) at time step \(n\). The continuous PDE is 82 | \[ \frac{\partial T}{\partial t} = D \nabla^2 T, \] 83 | which we discretize using a 5-point central difference for the Laplacian and forward-Euler in time (explicit): 84 | 85 | \[ \nabla^2 T\big|_{i,j} \approx \frac{T_{i+1,j} + T_{i-1,j} + T_{i,j+1} + T_{i,j-1} - 4T_{i,j}}{\Delta x^2}. \] 86 | The time-stepping update is then 87 | \[ T_{i,j}^{n+1} = T_{i,j}^n + \Delta t \; D \; \frac{T_{i+1,j}^n + T_{i-1,j}^n + T_{i,j+1}^n + T_{i,j-1}^n - 4T_{i,j}^n}{\Delta x^2}. \] 88 | 89 |

Note on stability

90 |

Because the integrator is explicit, the time step must be small enough to satisfy the CFL-like condition for stability roughly: \(\Delta t \le \frac{\Delta x^2}{4D}\). If you increase \(D\) or \(\Delta t\) you may see the simulation blow up or become noisy.

91 | 92 |
94 | 95 |
96 | 97 | 350 | 351 | 352 | 353 | -------------------------------------------------------------------------------- /examples/particle_system.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | Particle System 23 | 24 | 25 |
26 |

Particle System

27 | 28 | 29 | 30 | 31 | 34 | 76 | 77 | 110 |
32 | Your browser does not support the HTML5 canvas tag. 33 | 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 |
0.00 s
0.00 ms
0
75 |
78 |

Particle system algorithm:

79 | This example shows a simple particle system: 80 |
    81 |
  1. remove particles with age > max. life time
  2. 82 |
  3. generate new particles by emitters
  4. 83 |
  5. compute forces for all particles
  6. 84 |
  7. time integration to get new particle positions and velocities
  8. 85 |
86 | 87 |

1. Remove particles with age > max. life time

88 | 89 |

The life time of each particle is set to 0 when a particle is generated. In each simulation step the life time is increased by the time step size $\Delta t$. When a particle's life time is greater than a predefined maximum life time, the particle is removed from the simulation.

90 | 91 |

Note that the life time can be used for rendering effects. In our case the transparency is increased depending on the life time to fade out the particles.

92 | 93 |

2. Generate new particles by emitters

94 |

In each simulation step new particles are generated at the position of the emitter. Typically they are initialized with some predefined values and sometimes some random values are added to obtain more natural results.

95 | 96 |

In our simulation the particles are generated with a velocity pointing upwards. Moreover, the velocity in x- and y-direction is modified by some random values.

97 | 98 |

3. Compute forces for all particles

99 |

In each step forces are determined for the particles, e.g. gravity, wind etc. In our simple example only gravity is applied.

100 | 101 |

4. Time integration

102 | Finally, the particles are advected by numerical time integration. In our case we use a symplectic Euler method: 103 | $$\begin{align*} 104 | \mathbf v(t + \Delta t) &= \mathbf v(t) + \frac{\Delta t}{m} \mathbf f^{\text{ext}} \\ 105 | \mathbf x(t + \Delta t) &= \mathbf x(t) + \Delta t \mathbf v(t + \Delta t), 106 | \end{align*}$$ 107 | where $\mathbf f^{\text{ext}}$ are the external forces. 108 | 109 |
111 | 112 |
113 | 114 | 379 | 380 | 381 | 382 | -------------------------------------------------------------------------------- /examples/sph_approximation.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | 23 | SPH Approximation 24 | 25 | 26 |
27 |

SPH Approximation

28 | 29 | 30 | 31 | 32 | 35 | 38 | 39 | 40 | 60 | 61 | 62 | 88 |
33 |
34 |
36 |
37 |
41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |
0.025
63 |

SPH Approximation of different functions:

64 | In this example we discretized a linear and a quadratic polynomial as well as a trigonometric function using SPH with the cubic spline kernel [KBST19, KBST22]. The left plot shows the particle sampling pattern while the right plot shows the exact functions and their SPH approximations. To compute the SPH approximations the function values are sampled along the red line in the left plot. That means that for each point on the line the neighbors are determined and the SPH approximation formula is applied: 65 | $$\langle f(x,y) \rangle = \sum_j \frac{m_j}{\rho_j} f(x_j,y_j) W_{ij}$$ 66 | 67 | Linear function: 68 | $$f(x,y) = \frac12 (x+y)$$ 69 | 70 | Quadratic_function: 71 | $$f(x,y) = \frac12 (x^2+y^2)-1$$ 72 | 73 | Trigonometric function: 74 | $$f(x,y) = \sin(5 x) \cos(3y)$$ 75 | 76 |

Shepard filter

77 | The SPH approximation of a fluid quantity is error-prone if the particle neighborhood is only partially filled. To consider this in the computation the smoothing kernel can be corrected by a factor $s_i$: 78 | $$\tilde {W}_{ij} = s_i W_{ij} = \frac{1}{\sum_k \frac{m_k}{\rho_k} W_{ik}} W_{ij}.$$ 79 | 80 | 81 |

References

82 |
    83 |
  • [KBST19] Dan Koschier, Jan Bender, Barbara Solenthaler, Matthias Teschner. Smoothed Particle Hydrodynamics for Physically-Based Simulation of Fluids and Solids. Eurographics Tutorial, 2019 84 |
  • [KBST22] Dan Koschier, Jan Bender, Barbara Solenthaler, Matthias Teschner. A Survey on SPH Methods in Computer Graphics. Computer Graphics Forum, 2022 85 |
  • 86 |
87 |
89 | 90 |
91 | 92 | 457 | 458 | 459 | 460 | -------------------------------------------------------------------------------- /examples/particle_system_rb.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | Particle System - Rigid Bodies 23 | 24 | 25 |
26 |

Particle System - Rigid Bodies

27 | 28 | 29 | 30 | 31 | 34 | 76 | 77 | 111 |
32 | Your browser does not support the HTML5 canvas tag. 33 | 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 |
0.00 s
0.00 ms
0
75 |
78 |

Particle system algorithm:

79 | This example shows a simple particle system where each particle is represented by a rigid body with a rotation and an angular velocity: 80 |
    81 |
  1. remove rigid bodies with age > max. life time
  2. 82 |
  3. generate new rigid bodies by emitters
  4. 83 |
  5. compute forces for all rigid bodies
  6. 84 |
  7. time integration to get new rigid body positions, rotations and velocities
  8. 85 |
86 | 87 |

1. Remove rigid bodies with age > max. life time

88 | 89 |

The life time of each rigid body is set to 0 when a rigid body is generated. In each simulation step the life time is increased by the time step size $\Delta t$. When a rigid body's life time is greater than a predefined maximum life time, the rigid body is removed from the simulation.

90 | 91 |

Note that the life time can be used for rendering effects. In our case the transparency is increased depending on the life time to fade out the rigid bodies.

92 | 93 |

2. Generate new rigid bodies by emitters

94 |

In each simulation step new rigid bodies are generated at the position of the emitter. Typically they are initialized with some predefined values and sometimes some random values are added to obtain more natural results.

95 | 96 |

In our simulation the rigid bodies are generated with a velocity pointing upwards. Moreover, the velocity in x- and y-direction is modified by some random values. The starting position of the body is at the emitter and it has a random rotation and angular velocity.

97 | 98 |

3. Compute forces for all rigid bodies

99 |

In each step forces are determined for the rigid bodies, e.g. gravity, wind etc. In our simple example only gravity is applied.

100 | 101 |

4. Time integration

102 | Finally, the rigid bodies are advected by numerical time integration. In our case we use a symplectic Euler method: 103 | $$\begin{align*} 104 | \mathbf v(t + \Delta t) &= \mathbf v(t) + \frac{\Delta t}{m} \mathbf f^{\text{ext}} \\ 105 | \mathbf x(t + \Delta t) &= \mathbf x(t) + \Delta t \mathbf v(t + \Delta t) \\ 106 | \alpha(t + \Delta t) &= \alpha(t) + \Delta t \omega, 107 | \end{align*}$$ 108 | where $\mathbf f^{\text{ext}}$ are the external forces, $\omega$ is the (in our example constant) angular velocity and $\alpha$ is the rotation angle. 109 | 110 |
112 | 113 |
114 | 115 | 408 | 409 | 410 | 411 | -------------------------------------------------------------------------------- /examples/sph_kernel.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | 23 | SPH Kernel 24 | 25 | 26 |
27 |

SPH Kernel

28 | 29 | 30 | 31 | 34 | 35 | 36 | 58 | 59 | 60 | 115 |
32 |
33 |
37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 55 | 56 | 57 |
54 |
61 |

SPH kernel functions:

62 | 63 |

In the following we investigate different SPH kernel functions and their derivatives. A kernel function $W(\mathbf{r}, h)$ with the smoothing length $h$ must have the following properties:

64 |
    65 |
  • Normalization condition: $\int_{\mathbb{R}^d} W(\mathbf x - \mathbf x^*, h) d \mathbf x^* = 1 $
  • 66 |
  • Symmetry condition: $W(\mathbf x - \mathbf x^*, h) = W(\mathbf x^* - \mathbf x, h)$
  • 67 |
  • Dirac-$\delta$ condition: $\lim_{h \rightarrow 0} W(\mathbf x - \mathbf x^*, h) = \delta(\mathbf x - \mathbf x^*)$
  • 68 |
  • Non-negative condition: $W(\mathbf x - \mathbf x^*, h) \geq 0$
  • 69 |
  • Compact support condition with support radius $r$: $W(\mathbf x - \mathbf x^*, h) = 0 \quad \text{if} \quad \|\mathbf x - \mathbf x^*\| > r$
  • 70 |
71 |

Furthermore, a kernel function should be at least twice continuously differentiable to enable a consistent discretization of 2nd-order partial 72 | differential equations (PDEs). 73 | The smoothing length controls defines how strongly a value at position $\mathbf x$ is influenced by the values in its neighborhood. Hence, a larger value $h$ yields a larger smoothing effect.

74 |

Popular kernel functions

75 |

Cubic spline kernel [Mon92]

76 | $$W(q) = \alpha_d \begin{cases} 77 | \frac23 - q^2 + \frac12 q^3 & 0 \leq q < 1 \\ 78 | \frac16 (2-q)^3 & 1 \leq q < 2 \\ 79 | 0 & q \geq 2, 80 | \end{cases}$$ 81 | where $q = \frac{\|r\|}{h}$ the kernel normalization factors $\alpha_d$ for the respective 82 | dimensions $d$ are $\alpha_1 = \frac1h$, $\alpha_2 = \frac{15}{7\pi h^2}$, and $\alpha_3 = \frac{3}{2\pi h^3}$. 83 | 84 |

Wendland C2 kernel for 1D [Wen95]

85 | $$W(q) = \alpha_d \begin{cases} 86 | \left (1-\frac{q}{2} \right )^3 \left (\frac32 q+1 \right ) & 0 \leq q \leq 2 \\ 87 | 0 & q > 2, 88 | \end{cases}$$ 89 | where the kernel normalization factor is $\alpha_1 = \frac{5}{8h}$. 90 | 91 |

Quintic spline kernel [LL10]

92 | $$W(q) = \alpha_d \begin{cases} 93 | (3-q)^5 - 6(2-q)^5 + 15 (1-q)^5 & 0 \leq q \leq 1 \\ 94 | (3-q)^5 - 6(2-q)^5 & 1 < q \leq 2 \\ 95 | (3-q)^5 & 2 < q \leq 3 \\ 96 | 0 & q > 3, 97 | \end{cases}$$ 98 | where the kernel normalization factors are $\alpha_1 = \frac{1}{120h}$, $\alpha_2 = \frac{7}{478 \pi h^2}$, and $\alpha_3 = \frac{1}{120 \pi h^3}$. 99 | 100 | 101 |

Derivatives

102 |

The gradient of the kernel function is 103 | $$\nabla W = \frac{\partial W}{\partial \mathbf r} = \frac{\partial W(q)}{\partial q} \cdot \frac{\partial q}{\partial \mathbf r} = \frac{\partial W(q)}{\partial q} \frac{\mathbf r} {h \| \mathbf r \|}$$ 104 | and the Laplacian 105 | $$\nabla^2 W = \frac{\partial^2 W(q)}{\partial q^2} \frac{1}{h^2} + \frac{\partial W(q)}{\partial q} \nabla^2 q$$ 106 |

107 | 108 |

References

109 |
    110 |
  • [Mon92] J. Monaghan. Smoothed Particle Hydrodynamics. Annual Review of Astronomy and Astrophysics, 1992, 30, 543-574
  • 111 |
  • [Wen95] H. Wendland. Piecewise polynomial, positive definite and compactly supported radial functions of minimal degree. Advances in computational Mathematics 4.1, 389-396, 1995
  • 112 |
  • [LL10] M. Liu, G. Liu. Smoothed Particle Hydrodynamics (SPH): an Overview and Recent Developments. Archives of Computational Methods in Engineering, 2010, 17, 25-76
  • 113 |
114 |
116 | 117 |
118 | 119 | 398 | 399 | 400 | 401 | -------------------------------------------------------------------------------- /examples/deformation_gradient_strain.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | Deformation Gradient - Strain 23 | 24 | 25 |
26 |

Deformation Gradient - Strain

27 | 28 | 29 | 30 | 31 | 34 | 88 | 89 | 114 |
32 | Your browser does not support the HTML5 canvas tag. 33 | 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 | 85 | 86 |
68 |

Transformation matrix:

69 |

70 | $$\mathbf{T} = \mathbf{R} \mathbf{S} \mathbf{U} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}$$ 71 |

72 |

Deformation gradient:

73 |

74 | $$\mathbf{F} = \mathbf{D}_s \mathbf{D}_m^{-1} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}$$ 75 |

76 |

Green strain tensor:

77 |

78 | $$\mathbf{E}_\text{Green} = \frac12 (\mathbf{F}^T \mathbf{F} - \mathbf{I}) = \begin{pmatrix} 0 & 0 \\ 0 & 0 \end{pmatrix}$$ 79 |

80 |

Cauchy strain tensor:

81 |

82 | $$\mathbf{E}_\text{Cauchy} = \frac12 (\mathbf{F} + \mathbf{F}^T) - \mathbf{I} = \begin{pmatrix} 0 & 0 \\ 0 & 0 \end{pmatrix}$$ 83 |

84 |
87 |
90 |

Computation of deformation gradient and strain tensor

91 |

This example shows a single linear finite element (triangle) which is deformed by a transformation matrix. This matrix is computed as 92 | $$\mathbf{T} = \mathbf{R} \mathbf{S} \mathbf{U},$$ 93 | with the rotation matrix 94 | $$\mathbf{R} = \begin{pmatrix} \cos{\alpha} & -\sin{\alpha} \\ \sin{\alpha} & \cos{\alpha} \end{pmatrix},$$ 95 | the scaling matrix 96 | $$\mathbf{S} = \begin{pmatrix} s_x & 0 \\ 0 & s_y \end{pmatrix},$$ 97 | the shearing matrix 98 | $$\mathbf{U} = \begin{pmatrix} 1 & u_x \\ 0 & 1 \end{pmatrix} \begin{pmatrix} 1 & 0 \\ u_y & 1 \end{pmatrix},$$ 99 | the scaling factors $s_x$, $s_y$, and the shear values $u_x$, $u_y$. 100 |

101 | 102 |

The deformation gradient for such an element can be determined as 103 | $$\mathbf{F} = \mathbf{D}_s \mathbf{D}_m^{-1},$$ 104 | where 105 | $$\mathbf{D}_s = \begin{pmatrix} \mathbf{x}_1 - \mathbf{x}_0 & \mathbf{x}_2 - \mathbf{x}_0 \end{pmatrix},$$ 106 | $$\mathbf{D}_m = \begin{pmatrix} \mathbf{X}_1 - \mathbf{X}_0 & \mathbf{X}_2 - \mathbf{X}_0 \end{pmatrix}.$$ 107 | Here $\mathbf{X}_i$ denote the reference positions of the triangle and $\mathbf{x}_i$ the deformed configuration. 108 |

109 | 110 |

This example shows that the deformation gradient extracts exactly the original transformation from the deformed configuration of the triangle vertices. Moreover, we can see that the linear Cauchy strain tensor is not zero for a pure rotation. This means that a rotation, which is a rigid body transformation, leads to a non-zero strain which can cause artifacts in the simulation. 111 |

112 | 113 |
115 | 116 |
117 | 118 | 399 | 400 | 401 | 402 | -------------------------------------------------------------------------------- /examples/time_integration.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | Time integration 23 | 24 | 25 |
26 |

Time integration

27 | 28 | 29 | 30 | 31 | 34 | 72 | 73 | 114 |
32 | Your browser does not support the HTML5 canvas tag. 33 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 |
0.00 s
53 |
71 |
74 |

Time integration methods:

75 | This example shows the motion of a particle with an initial velocity. Moreover, a constant gravitational acceleration is acting on the particle. In this special case an analytic solution can be computed as: 76 | $$\begin{align*} 77 | \mathbf x(t + \Delta t) &= \mathbf x(t) + \Delta t \mathbf v(t) + \frac12 \Delta t^2 \mathbf a^\text{grav} \\ 78 | \mathbf v(t + \Delta t) &= \mathbf v(t) + \Delta t \mathbf a^\text{grav} . 79 | \end{align*}$$ 80 | The motion determined using this analytic solution is plotted in green and can be compared with different numerical solutions that are introduced in the following. 81 | 82 |

Explicit Euler

83 | 84 | The explicit Euler method is a first-order accurate method for solving ordinary differential equations (ODEs): 85 | $$\begin{align*} 86 | \mathbf x(t + \Delta t) &= \mathbf x(t) + \Delta t \mathbf v(t) \\ 87 | \mathbf v(t + \Delta t) &= \mathbf v(t) + \Delta t \mathbf a(t). 88 | \end{align*}$$ 89 | 90 |

Symplectic Euler

91 | 92 | The symplectic Euler method is also known as semi-implicit Euler since first the new velocity is determined and then the new position is computed using the new velocity value: 93 | $$\begin{align*} 94 | \mathbf v(t + \Delta t) &= \mathbf v(t) + \Delta t \mathbf a(t) \\ 95 | \mathbf x(t + \Delta t) &= \mathbf x(t) + \Delta t \mathbf v(t + \Delta t). 96 | \end{align*}$$ 97 | The method is also first-order accurate but yields better results than the explicit Euler since it is a symplectic method. 98 | 99 |

Runge-Kutta 2

100 | 101 | Typically numerical integration methods are defined for a differential equation determined by a function $\mathbf f(t, \mathbf s(t))$, where $\mathbf s$ defines the state of the system at time $t$. In our example the state is defined by the current position and velocity of the particle and the function is defined as: 102 | $$\begin{equation*} 103 | \mathbf f(t, \mathbf s(t)) = \begin{pmatrix} \dot{\mathbf x} \\ \dot{\mathbf v} \end{pmatrix}, \quad\quad \mathbf s(t) = \begin{pmatrix} \mathbf x(t) \\ \mathbf v(t) \end{pmatrix} 104 | \end{equation*}$$ 105 | 106 | Using this function the second-order Runge-Kutta integration is defined as 107 | $$\begin{align*} 108 | \mathbf k_1 &= \Delta t \mathbf f(t, \mathbf s(t)) \\ 109 | \mathbf k_2 &= \Delta t \mathbf f(t + \frac12 \Delta t, \mathbf s(t) + \frac12 \mathbf k_1) \\ 110 | \mathbf s(t + \Delta t) &= \mathbf s(t) + \mathbf k_2. 111 | \end{align*}$$ 112 | Note that the Runge-Kutta method has multiple stages (in our case 2) to achieve a higher-order accuracy. However, this approach is more expensive than the Euler methods since the function has to be evaluated once per stage. 113 |
115 | 116 |
117 | 118 | 441 | 442 | 443 | 444 | -------------------------------------------------------------------------------- /examples/sph_gradient_approximation.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | 23 | SPH Gradient Approximation 24 | 25 | 26 |
27 |

SPH Gradient Approximation

28 | 29 | 30 | 31 | 32 | 35 | 38 | 39 | 40 | 68 | 69 | 70 | 97 |
33 |
34 |
36 |
37 |
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 |
0.025
71 |

SPH approximation of the gradient of a quadratic function:

72 | In this example we approximate the gradient of a quadratic polynomial using the SPH discretization with the cubic spline kernel [KBST19, KBST22]. The left plot shows the particle sampling pattern while the right plot shows the quadratic function, the exact gradient, the SPH gradient approximation, and the error. To compute the SPH approximation the function values are sampled along the red line in the left plot. That means that for each point on the line the neighbors are determined and the SPH difference formula to approximate the gradient is applied [KBST19]: 73 | $$\langle \nabla f(x,y) \rangle = \sum_j \frac{m_j}{\rho_j} (f(x_j,y_j) - f(x,y)) \nabla W_{ij}$$ 74 | 75 | Quadratic_function: 76 | $$f(x,y) = x^2 + \frac14 y^2 - 1$$ 77 | 78 | Gradient: 79 | $$\nabla f(x,y) = 2x + \frac12 y$$ 80 | 81 |

Kernel gradient correction

82 | The SPH gradient approximation of a fluid quantity is error-prone if the particle neighborhood is only partially filled. To consider this in the computation and to make it first-order consistent, the kernel gradient can be corrected by a matrix $\mathbf{L}_i$: 83 | $$\nabla \tilde {W}_{ij} = L_i \nabla W_{ij},$$ 84 | where 85 | $$\mathbf{L}_i = \left ( \sum_j \frac{m_j}{\rho_j} \nabla W_{ij} \otimes (\mathbf{x}_j - \mathbf{x}_i) \right )^{-1}. 86 | $$ 87 | Note that $\otimes$ denotes the dyadic product of two vectors. 88 | 89 | 90 |

References

91 |
    92 |
  • [KBST19] Dan Koschier, Jan Bender, Barbara Solenthaler, Matthias Teschner. Smoothed Particle Hydrodynamics for Physically-Based Simulation of Fluids and Solids. Eurographics Tutorial, 2019
  • 93 |
  • [KBST22] Dan Koschier, Jan Bender, Barbara Solenthaler, Matthias Teschner. A Survey on SPH Methods in Computer Graphics. Computer Graphics Forum, 2022 94 |
  • 95 |
96 |
98 | 99 |
100 | 101 | 493 | 494 | 495 | 496 | -------------------------------------------------------------------------------- /examples/neighborhood_search.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | Neighborhood Search 23 | 24 | 25 |
26 |

Neighborhood Search

27 | 28 | 29 | 30 | 31 | 34 | 72 | 73 | 87 |
32 | Your browser does not support the HTML5 canvas tag. 33 | 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 |
0.00 ms
0
0.025
71 |
74 |

Neighborhood search using spatial hashing:

75 |

This example shows how the neighborhood of a particle is determined using spatial hashing [THM+03]. For each particle we want to find its neighbors within a given radius (the support radius of the kernel function).

76 | 77 |

In each step all particles are added to a spatial grid using a cell size $s$ that equals the support radius of the SPH kernel function. The index $(i, j)$ of the grid cell of a particle is determined as 78 | $$i = \lfloor x/s \rfloor,\quad\quad j = \lfloor y/s \rfloor.$$ 79 | 80 | Since each particle is in a grid cell of size $s \times s$, the neighbors in a radius of $s$ must lie in one of the 9 neighboring cells (in 3D: 27 cells). Therefore, to get the neighbors for a particle $p$ in cell $(i,j)$, we test for all particles in the 9 neighboring cells $(i\pm1, j\pm1)$ if their distance to $p$ is less than $s$.

81 | 82 |

References

83 |
    84 |
  • [THM+03] Matthias Teschner, Bruno Heidelberger, Matthias Müller, Danat Pomeranets, Markus Gross. Optimized Spatial Hashing for Collision Detection of Deformable Objects. In Proceedings of VMV, 2003.
  • 85 |
86 |
88 | 89 |
90 | 91 | 470 | 471 | 472 | 473 | -------------------------------------------------------------------------------- /examples/spring_plot.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | 23 | Mass-Spring Model 24 | 25 | 26 |
27 |

Mass-Spring Model

28 | 29 | 30 | 31 | 32 | 35 | 38 | 39 | 40 | 87 | 88 | 170 |
33 | Your browser does not support the HTML5 canvas tag. 34 | 36 |
37 |
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 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 |
0.00 s
0.00 ms
76 |
86 |
89 |

Mass spring algorithm:

90 | This 1D example shows the motion of a mass spring system that consists of a static and a dynamic particle linked by a spring. In each simulation step the following steps are performed: 91 |
    92 |
  1. compute spring forces
  2. 93 |
  3. time integration to get new particle positions and velocities
  4. 94 |
95 | On the left the simulation is shown while on the right we see the plot of the motion in x-direction compared to the analytic solution. 96 | 97 |

1. Compute spring forces

98 | 99 |

In this simple 1D example we have a spring with a stiffness $k$ and a rest length of 0. Therefore, the spring force is detertmined as 100 | $$f(x) = -k x.$$ 101 |

102 | 103 |

2. Time integration

104 |

Analytic solution

105 | 106 | The analytic solution for the position $x$ of a particle which is linked by a spring to the origin is 107 | $$ 108 | x(t) = x(0) \cos\left(\sqrt{\frac{k}{m}} t \right ), 109 | $$ 110 | where $m$ is the mass of the particle and $x(0)$ the initial position. 111 | 112 |

Explicit Euler

113 | 114 | The explicit Euler method is a first-order accurate method for solving ordinary differential equations (ODEs): 115 | $$\begin{align*} 116 | x(t + \Delta t) &= x(t) + \Delta t v(t) \\ 117 | v(t + \Delta t) &= v(t) + \Delta t \frac{f(t)}{m}. 118 | \end{align*}$$ 119 | 120 |

Symplectic Euler

121 | 122 | The symplectic Euler method is also known as semi-implicit Euler since first the new velocity is determined and then the new position is computed using the new velocity value: 123 | $$\begin{align*} 124 | v(t + \Delta t) &= v(t) + \Delta t \frac{f(t)}{m} \\ 125 | x(t + \Delta t) &= x(t) + \Delta t v(t + \Delta t). 126 | \end{align*}$$ 127 | The method is also first-order accurate but yields better results than the explicit Euler since it is a symplectic method. 128 | 129 |

Runge-Kutta 2

130 | 131 | Typically numerical integration methods are defined for a differential equation determined by a function $f(t, s(t))$, where $s$ defines the state of the system at time $t$. In our 1D example the state is defined by the current position and velocity of the particle and the function is defined as: 132 | $$\begin{equation*} 133 | f(t, s(t)) = \begin{pmatrix} \dot{x} \\ \dot{v} \end{pmatrix}, \quad\quad s(t) = \begin{pmatrix} x(t) \\ v(t) \end{pmatrix} 134 | \end{equation*}$$ 135 | 136 | Using this function the second-order Runge-Kutta integration is defined as 137 | $$\begin{align*} 138 | k_1 &= \Delta t f(t, s(t)) \\ 139 | k_2 &= \Delta t f(t + \frac12 \Delta t, s(t) + \frac12 k_1) \\ 140 | s(t + \Delta t) &= s(t) + k_2. 141 | \end{align*}$$ 142 | Note that the Runge-Kutta method has multiple stages (in our case 2) to achieve a higher-order accuracy. However, this approach is more expensive than the Euler methods since the function has to be evaluated once per stage. 143 | 144 |

Implicit Euler

145 | 146 | The implicit Euler method is a first-order accurate method for solving ordinary differential equations (ODEs): 147 | $$\begin{align*} 148 | x(t + \Delta t) &= x(t) + \Delta t v(t + \Delta t) \\ 149 | v(t + \Delta t) &= v(t) + \Delta t \frac{f(x(t + \Delta t))}{m}. 150 | \end{align*}$$ 151 | The method is unconditionally stable but in general a non-linear system has to be solved. 152 | 153 | In our simple 1D example we linearize the spring force $f(x)$ using a first-order Taylor approximation: 154 | $$ 155 | f(x(t + \Delta t)) = f(x + \Delta x) = f(x) + \frac{\partial f(x)}{\partial x} \Delta x, 156 | $$ 157 | where $\frac{\partial f(x)}{\partial x} = -k$ and we write $x=x(t)$ and $x + \Delta x = x(t + \Delta t)$ for simplicity. Using this linearization and transforming the above equations yields the linear equation 158 | $$ 159 | \left (1 + \frac{\Delta t^2}{m}k \right ) \Delta v = \frac{\Delta t}{m} (f(x) - \Delta t k v), 160 | $$ 161 | which can be simply solved for the velocity change $\Delta v$. Finally, the position and velocity are updated as 162 | $$\begin{align*} 163 | v(t+\Delta t) &= v(t) + \Delta v \\ 164 | x(t+\Delta t) &= x(t) + \Delta t v(t+\Delta t). 165 | \end{align*} 166 | $$ 167 | 168 | 169 |
171 | 172 |
173 | 174 | 549 | 550 | 551 | 552 | -------------------------------------------------------------------------------- /examples/mass_spring_system.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | Mass Spring System 23 | 24 | 25 |
26 |

Mass Spring System

27 | 28 | 29 | 30 | 31 | 34 | 100 | 101 | 162 |
32 | Your browser does not support the HTML5 canvas tag. 33 | 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 | 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 |
0.00 s
0.00 ms
0
0
99 |
102 |

Mass spring algorithm:

103 | This example shows a mass spring system that consists of particles linked by damped springs: 104 |
    105 |
  1. compute spring forces
  2. 106 |
  3. time integration to get new particle positions and velocities
  4. 107 |
108 | 109 | Note that the simulation is only conditionally stable since a conditionally stable explicit time integration method is used. 110 | 111 |

1. Compute spring forces

112 | 113 |

In general a spring force can be obtained for any holonomic constraint. 114 | In this example we use distance constraints 115 | $$C_i(\mathbf{x}_{i_1}, \mathbf{x}_{i_2}) = \| \mathbf x_{i_1} -\mathbf x_{i_2} \|-d,$$ 116 | where $d$ is the rest length between particles $\mathbf{x}_{i_1}$ and $\mathbf{x}_{i_2}$.

117 | 118 |

Potential energy

119 |

For a scalar constraint we can define a potential energy as: 120 | $$E(\mathbf x) = \frac k 2 C(\mathbf x)^2,$$ 121 | where $k$ is the stiffness of the spring. 122 | 123 |

General spring force

124 | The spring force for a particle $j$ is then determined by the negative gradient of the potential energy function: 125 | $$\mathbf F_j = - \frac{\partial E(\mathbf x)}{\partial \mathbf x_j} = -k \frac{\partial C(\mathbf x)}{\partial \mathbf x_j} C(\mathbf x).$$ 126 | 127 |

General damping force

128 | The corresponding damping force is obtained by using the time derivative of the constraint function: 129 | $$\mathbf F^D_j = -\mu \frac{\partial C(\mathbf x)}{\partial \mathbf x_j} \dot{C}(\mathbf x).$$ 130 | 131 | 132 |

Constraint gradients:

133 |

To compute the spring and damping forces, the constraint gradients are required which are computed as:

134 | $$\begin{align*} 135 | \frac{\partial C_i}{\partial \mathbf x_{i_1}} &= \frac{\mathbf x_{i_1} -\mathbf x_{i_2}}{\| \mathbf x_{i_1} -\mathbf x_{i_2} \|} \\ 136 | \frac{\partial C_i}{\partial \mathbf x_{i_2}} &= - \frac{\mathbf x_{i_1} -\mathbf x_{i_2}}{\| \mathbf x_{i_1} -\mathbf x_{i_2} \|} 137 | \end{align*}$$ 138 | 139 |

Spring force

140 | So finally we get the following spring forces for a distance constraint $C_i(\mathbf{x}_{i_1}, \mathbf{x}_{i_2})$: 141 | $$\begin{align*} 142 | \mathbf F_{i_1} = -k (\| \mathbf x_{i_1} -\mathbf x_{i_2} \|-d) \frac{\mathbf x_{i_1} -\mathbf x_{i_2}}{\| \mathbf x_{i_1} -\mathbf x_{i_2} \|} \\ 143 | \mathbf F_{i_2} = +k (\| \mathbf x_{i_1} -\mathbf x_{i_2} \|-d) \frac{\mathbf x_{i_1} -\mathbf x_{i_2}}{\| \mathbf x_{i_1} -\mathbf x_{i_2} \|}. 144 | \end{align*}$$ 145 | 146 |

Damping force

147 | The damping forces for a distance constraint $C_i(\mathbf{x}_{i_1}, \mathbf{x}_{i_2})$ are determined as: 148 | $$\begin{align*} 149 | \mathbf F^D_{i_1} = -k \left ((\mathbf v_{i_1}- \mathbf v_{i_2}) \cdot \frac{\mathbf x_{i_1} -\mathbf x_{i_2}}{\| \mathbf x_{i_1} -\mathbf x_{i_2} \|} \right ) \frac{\mathbf x_{i_1} -\mathbf x_{i_2}}{\| \mathbf x_{i_1} -\mathbf x_{i_2} \|} \\ 150 | \mathbf F^D_{i_2} = +k \left ((\mathbf v_{i_1}- \mathbf v_{i_2}) \cdot \frac{\mathbf x_{i_1} -\mathbf x_{i_2}}{\| \mathbf x_{i_1} -\mathbf x_{i_2} \|} \right ) \frac{\mathbf x_{i_1} -\mathbf x_{i_2}}{\| \mathbf x_{i_1} -\mathbf x_{i_2} \|}. 151 | \end{align*}$$ 152 | 153 |

2. Time integration

154 | Finally, the particles are advected by numerical time integration. In our case we use a symplectic Euler method: 155 | $$\begin{align*} 156 | \mathbf v(t + \Delta t) &= \mathbf v(t) + \frac{\Delta t}{m} \left (\mathbf F(t) + \mathbf F^D + \mathbf F^{\text{ext}} \right ) \\ 157 | \mathbf x(t + \Delta t) &= \mathbf x(t) + \Delta t \mathbf v(t + \Delta t), 158 | \end{align*}$$ 159 | where $\mathbf F^{\text{ext}}$ are the external forces. 160 | 161 |
163 | 164 |
165 | 166 | 541 | 542 | 543 | 544 | -------------------------------------------------------------------------------- /examples/pbd.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 21 | 22 | Position-Based Dynamics 23 | 24 | 25 |
26 |

Position-Based Dynamics

27 | 28 | 29 | 30 | 31 | 34 | 96 | 97 | 164 |
32 | Your browser does not support the HTML5 canvas tag. 33 | 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 | 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 |
0.00 s
0.00 ms
0
0
95 |
98 |

PBD algorithm:

99 | This example shows the position based dynamics method introduced by Müller et al. [MHHR06,BMM17]. 100 |
    101 |
  1. time integration to predict particle positions
  2. 102 |
  3. loop
  4. 103 |
  5. compute Lagrange multipliers
  6. 104 |
  7. determine position correction
  8. 105 |
  9. update velocities
  10. 106 |
107 | In this example we use distance constraints 108 | $$C_i(\mathbf{x}_{i_1}, \mathbf{x}_{i_2}) = \| \mathbf x_{i_1} -\mathbf x_{i_2} \|-d,$$ 109 | where $d$ is the rest length between particles $\mathbf{x}_{i_1}$ and $\mathbf{x}_{i_2}$. 110 | 111 |

1. Time integration

112 | In the first step the particle positions are advected using a symplectic Euler method to obtain predicted positions $\mathbf x^*$: 113 | $$\begin{align*} 114 | \mathbf v^* &= \mathbf v(t) + \Delta t \mathbf a_\text{ext}(t) \\ 115 | \mathbf x^* &= \mathbf x(t) + \Delta t \mathbf v^*, 116 | \end{align*}$$ 117 | where $\mathbf a_\text{ext}$ are accelerations due to external forces. 118 | 119 |

2. Solver loop

120 | Position corrections are computed and applied in a loop using a fixed number of iterations. 121 | 122 |

3. Compute Lagrange multipliers

123 | 124 |

The general form to compute the Lagrange multipliers in position-based dynamics is 125 | $$\mathbf J \mathbf M^{-1} \mathbf J^T \boldsymbol \lambda = -\mathbf C(\mathbf p),$$ 126 | where $\mathbf J = (\partial C / \partial \mathbf x)^T$ is the Jacobian of the constraint function and $\mathbf M$ denotes the mass matrix.

127 | 128 |

Since the distance constraint is a scalar function the Lagrange multiplier is determined as 129 | $$\lambda_i = - \frac{C_i(\mathbf x)}{\sum_j \frac{1}{m_j} \| \partial C_i(\mathbf x) / \partial \mathbf x_j\|^2},$$ 130 | where $j$ denotes the indices of the particles which are influenced by the constraint. 131 |

132 | 133 |

To compute the Lagrange multipliers, the constraint gradients are required which are computed as:

134 |

Constraint gradients:

135 | $$\begin{align*} 136 | \frac{\partial C_i}{\partial \mathbf x_{i_1}} &= \frac{\mathbf x_{i_1} -\mathbf x_{i_2}}{\| \mathbf x_{i_1} -\mathbf x_{i_2} \|} \\ 137 | \frac{\partial C_i}{\partial \mathbf x_{i_2}} &= - \frac{\mathbf x_{i_1} -\mathbf x_{i_2}}{\| \mathbf x_{i_1} -\mathbf x_{i_2} \|} 138 | \end{align*}$$ 139 | 140 |

4. Position correction:

141 | The position correction for a particle is determined using the Lagrange multiplier: 142 | $$\begin{align*} 143 | \Delta \mathbf x_{i_1} &= \frac{1}{m_{i_1}} \frac{\partial C_i}{\partial \mathbf x_{i_1}} \lambda_i = -\frac{\frac{1}{m_{i_1}}}{\frac{1}{m_{i_1}} + \frac{1}{m_{i_2}}}(\|\mathbf{x}_{i_1} - \mathbf{x}_{i_2}\|-d)\frac{\mathbf{x}_{i_1}- \mathbf{x}_{i_2}}{\|\mathbf{x}_{i_1} - \mathbf{x}_{i_2}\|} \\ 144 | \Delta \mathbf x_{i_2} &= \frac{1}{m_{i_2}} \frac{\partial C_i}{\partial \mathbf x_{i_2}} \lambda_i = +\frac{\frac{1}{m_{i_1}}}{\frac{1}{m_{i_1}} + \frac{1}{m_{i_2}}}(\|\mathbf{x}_{i_1} - \mathbf{x}_{i_2}\|-d)\frac{\mathbf{x}_{i_1}- \mathbf{x}_{i_2}}{\|\mathbf{x}_{i_1} - \mathbf{x}_{i_2}\|} 145 | \end{align*}$$ 146 | and the correction is applied to update $\mathbf x^*$ as 147 | $$\mathbf x^* := \mathbf x^* + \Delta \mathbf x.$$ 148 | 149 |

5. Velocity update:

150 | The final positions and velocities are computed as: 151 | $$\begin{align*} 152 | \mathbf x_i(t+\Delta t) &= \mathbf x_i^* \\ 153 | \mathbf v_i(t+\Delta t) &= \frac{1}{\Delta t} (\mathbf x_i(t+\Delta t) - \mathbf x_i(t)) 154 | \end{align*} 155 | $$ 156 | 157 |

References

158 |
    159 |
  • [MHHR06] Matthias Müller, Bruno Heidelberger, Marcus Hennix, John Ratcliff. Position Based Dynamics. In Proceedings of Virtual Realitiy, Interactions, and Physical Simulation, 2006. Eurographics Association.
  • 160 |
  • [BMM17] Jan Bender, Matthias Müller, Miles Macklin. A Survey on Position Based Dynamics, 2017. Eurographics Tutorials, 2017
  • 161 |
162 | 163 |
165 | 166 |
167 | 168 | 543 | 544 | 545 | 546 | --------------------------------------------------------------------------------