SPH Matlab
SPH Matlab
SPH Matlab
2)
Philip Mocz
Applied Math 205 Final Project, Harvard University, Fall 2011, Prof. Knezevic
ABSTRACT
In this final project, we discuss the theory of Smoothed Particle Hydrodynamics (SPH)
and its implementation in Matlab. SPH is a particle-based method for computational
fluid dynamics. We apply SPH to study the time evolution of a toy star model and
find its equilibrium state. The goal of this paper is to demonstrate the SPH works
successfully, therefore we apply it to problems where analytic solutions are known to
exist and compare numerical and theoretical answers. We also consider a toy collision
problem of two polytropic bodies. SPH is found to give reliable answers and is a useful
method for computational fluid dynamics.
Key words: computational fluid dynamics – numerical analysis – smoothed particle
hydrodynamics – stars: general
2011 RAS
2 P. Mocz
2 THEORY OF THE SPH METHOD 2
Kernels used for SPH simulations
spline W(r)
In SPH, a fluid is modeled as a collection of fluid elements spline ∇ W(r)
2
1.5 spline ∇ W(r)
(particles) each of which have associated to them relevant gaussian W(r)
gaussian ∇ W(r)
physical quantities (position, velocity, mass, density, etc.). 2
gaussian ∇ W(r)
1
The Euler equation for an ideal fluid without dissipation
tells us:
0.5
dv
ρ = −∇P + f (1)
dt 0
y
where v is the velocity, ρ is density, P is pressure, and f
can be some external force per unit volume applied to the −0.5
Table 2. Parameters for the collision simulations Table 3. Errors of SPH simulations compared to analytic solu-
tions
Simulation 10 soft collision of 2 stars – head on
simulation error
Parameter Value
number of particles N = 500 1 0.002302
dimension d=2 2 0.004032
total mass M =2 3 0.003423
final star radius R = 0.75 4 0.003429
particles per star 400, 100 5 0.005435
smoothing length h = 0.04/ N/1000 6 0.005856
time step Δt = 0.04 7 0.002393
damping ν=6 8 0.005080
pressure constant k = 0.1 9 0.002497
polytropic index n=1
max time steps 400
kernel spline 7 DISCUSSION
initial config. two stars in equilibrium 7.1 Discussion of results
center distance 1.2 apart
relative velocity 0.4 First we explore what the different simulations 1–9 of col-
Simulation 11 soft collision of 2 stars – low damping lapsing toy stars reaching equilibrium tell us about the SPH
method. Simulation 1 is our original setup, which we take
Parameter Value
and vary one parameter at a time to create simulations 2–
damping ν=2
initial config. two stars in equilibrium
9. In Figure 9 we plot the equilibrium density profile of a
center distance 1.2 apart polytropic toy star simulated with 100, 50, and 400 parti-
relative velocity 0.8 cles (simulations 1, 2, 3). We notice a few key features in the
impact param. 0.6 SPH profile versus the analytic profile. First of all, we repro-
duce the analytic solution quite well. At r = 0, the central
The parameter value for λ (gravitational strength) is density ρ(0) is best estimated using larger number of par-
given by equation (26) and the mass per particle is m = ticles, N . We see that using only N = 50 particles gives a
M/N . density that is slightly small due to the approximations we
are making. Gingold & Monaghan (1977) encountered the
same phenomenon, performing simulations with N = 50 and
N = 80 particles, and to account for this error normalized
6 RESULTS
all densities by the central densities. We see however, that
We carry out the simulations with the setups mentioned in we can expect to obtain the correct actual densities by using
the previous section. Snapshots of simulations 1, 3, 5, 9, a larger number of particles, as expected by the theory of
10, and 11 are shown in Figures 2, 3, 4, 5, 6, and 7. I do SPH. There is another effect we notice in Figure 9. Notably,
not show all the simulations because a number of them are the density profiles deviate at r = R, where the density goes
visually similar. The simulations were carried out until equi- to zero in the analytic model. Here the shape of the kernel
librium was reached (up to 400 time steps may be required), affects the density profile and this is an unavoidable artifact
which was determined by monitoring the central density of of SPH. The smoothing length h may be altered to give a
the star (see Figure 8). The central density was found to better result. Generally, h can be reduced to provide bet-
oscillate, but dampen with time until the equilibrium value ter answers as long as there are enough particles so that
was reached. The plot is based on Figure 1 of Gingold & the neighborhood of radius h around each particle contains
Monaghan (1977). other particles. Reducing h to obtain better answers again
In addition we find the radial density profile along the means that we need a larger number of particles.
x-axis (sampled at 100 points xi between 0 and R) of the There does not seem to be a significant difference be-
equilibrium states of simulations 1–9 and compare it with tween the Gaussian kernel (simulation 4) and the spline ker-
what is expected from analytic theory. Plots of the density nel in the determination of the equilibrium density profile,
profiles of simulations 1–9 are shown in Figures 9, 10, 11, as seen from Figure 10. Gingold & Monaghan (1977) recom-
and 12. We calculate an error associated with each SPH mends using the Gaussian kernel to obtain the best physical
simulation, namely: answers, but we see that a spline kernel can be effectively
100 2
used, and makes calculations more efficient since it has com-
i=1 ρSPH (xi ) − ρtheory (xi )2 pact support.
error = (27)
100 The final density profile is also independent of damping,
The errors in reported in Table 3 and the simulations closely as one would hope, as seen by comparing simulations 1 and
match the theoretical profile. The results will be more care- 7 in Figure 10. Increased damping can significantly reduce
fully discussed in 7. the time it takes for the star to equilibrize (Figure 8). With
For the collision sequences (simulations 10 and 11), we no damping, the star would continue to oscillate and differ-
plot the maximum pressure in the system as a function of ent oscillatory modes could be excited by initial conditions
time in Figure 13 to give an example of how SPH can be used (c.f. Monaghan & Price (2004)).
to track the time dependent evolution of physical quantities. An increased smoothing length slightly smooths out the
Its significance will be discussed in 7. density profile, especially near r = R (compare simulations
2.6
0.6 0.6 1.6
2.4
0.4 0.4
2.2
1.4
0.2 2 0.2
density
density
0 1.8 0
y
y
1.2
1.6
−0.2 −0.2
1.4
−0.4 −0.4 1
1.2
−0.6 −0.6
1
0.8
−0.8 −0.8
0.8
−1 −1
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
x x
SPH simulation: 1, t = 1.640 SPH simulation: 1, t = 16.000
1 2.6 1
2.2
2
0.6 0.6
2.2
0.2 0.2
1.8
1.6
density
density
0 0
y
1.6
−0.4 −0.4
1.2 1.2
−0.6 −0.6
1
1
−0.8 −0.8
0.8
−1 −1
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
x x
Figure 2. A few steps of the progressive ordering of particles towards equilibrium in Simulation 1 of the toy star.
1 and 8 in Figure 10), although one can obtain reasonable or impossible to compute analytically. SPH also gives us
answers for a number of smoothing length scales. The best insight into the structures that may form given initial con-
value to use for h is still an open problem (Monaghan 1992, ditions and the time scales certain changes happen. An as-
2005). trophysicist, for example, may test a hypothesis about how
We can reach the equilibrium state from any initial the Moon was formed with soft-body collision using SPH,
state. In simulation 9 we separate the fluid into two dis- and see what initial conditions may have been required to
joint random distributions and show that we recover the complete the process in a certain amount of time or create
same single star density profile (see Figure 10). This is sim- necessary physical conditions that explain the composition
ilar to a collision problem, except that here each disjoint set of the Moon.
of particles is not in equilibrium initially.
In addition, we demonstrate that our code can easily
be extended to 3D. In simulation 5 we recover the analytic
density profile with SPH (Figure 11). We also tested that 7.2 Possible extensions of the work
the SPH simulation gives the correct density profile (both The SPH algorithm can be extended in a variety of ways
shape and magnitude) for a different polytropic index (see to study fluid dynamics. Notably, the equations of motion
simulation 6 in Figure 12). (21) can be changed to reflect the physics of a certain setup.
We have also simulated the collision of two toy stars and For example, the gravitational force may be replaced by a
recorded the evolution of their maximum pressures (Fig- downwards constant force to model uniform gravity on the
ure 13). The purpose was not to draw astrophysical con- Earth’s surface (this is built into our code, although we do
clusions (since we are working with a simplified model and not use it for modeling toy stars). We could then use the
arbitrary units) but to demonstrate how SPH can easily be SPH code to simulate, for example, drops of water, waves,
used to find the evolution of physical quantities (pressure, or a breaking dam.
energy, temperature, etc.) which may otherwise be difficult Often, a term is added into the equations of motion to
2
2.4
0.6 0.6
2.2 1.8
0.4 0.4
2
0.2 0.2 1.6
1.8
density
density
0 0
y
y
1.4
1.6
−0.2 −0.2
1.4 1.2
−0.4 −0.4
1.2
1
−0.6 −0.6
1
0.8
−0.8 0.8 −0.8
0.6
−1 0.6 −1
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
x x
SPH simulation: 3, t = 1.640 SPH simulation: 3, t = 16.000
1 2.6 1
2.2
2
0.6 2.2 0.6
density
0 0
y
1.6
1.4
1.2
−0.4 1.2 −0.4
−0.6 −0.6 1
1
−1 −1
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
x x
Figure 3. A few steps of the progressive ordering of particles towards equilibrium in Simulation 3 of the toy star.
represent viscous forces. Most commonly used is: from galaxy dynamics to planetary disks, may be studied
using SPH, which is one of the primary scientific applica-
− mj (Πij ) ∇W (ri − rj ; h) (28) tions of the method.
j,j=i
2.6
1 1
3.5
2.4
0.5 0.5
2.2
3
0 0 2
density
density
z
z
2.5
1.8
−0.5 −0.5
2 1.6
−1 −1
1 1
1.4
0.5 1 0.5 1
1.5
0.5 0.5
0 0
1.2
0 0
−0.5 −0.5
−0.5 −0.5
y −1 −1 x y −1 −1 x
SPH simulation: 5, t = 1.640 SPH simulation: 5, t = 16.000
3
2.6
1 2.8 1
2.4
2.6
0.5 0.5
2.4 2.2
0 0
density
density
2.2
z
2
−0.5 −0.5
1.8
1.8
−1 −1
1 1.6 1 1.6
0.5 1 0.5 1
1.4
0.5 0.5
0 0
1.4
0 0
−0.5 1.2 −0.5
−0.5 −0.5
y −1 −1 x y −1 −1 x
Figure 4. A few steps of the progressive ordering of particles towards equilibrium in Simulation 5 of the toy star.
2 1.2 2
1.8
1.6
1 1 1
1.4
density
density
0.8
0 0
y
y
1.2
0.6 1
−1 −1
0.8
0.4
−2 −2 0.6
0.2 0.4
−3 −3
−3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 3
x x
SPH simulation: 9, t = 3.240 SPH simulation: 9, t = 16.000
3 3 2.2
2
2
2 2
1.8
1.8
1 1.6 1
1.6
1.4
density
density
0 0 1.4
y
1.2
1.2
−1 1 −1
1
0.8
−2 −2
0.8
0.6
0.6
−3 0.4 −3
−3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 3
x x
Figure 5. A few steps of the progressive ordering of particles towards equilibrium in Simulation 9 of the toy star.
This paper has been typeset from a TEX/ LATEX file prepared
by the author.
1.5 1.5
1 1
0.5 0.5
0 0
y
y
−0.5 −0.5
−1 −1
−1.5 −1.5
−2 −2
−2 −1.5 −1 −0.5 0 0.5 1 1.5 2 −2 −1.5 −1 −0.5 0 0.5 1 1.5 2
x x
SPH simulation: collision, t = 3.240 SPH simulation: collision, t = 14.440
2 2
1.5 1.5
1 1
0.5 0.5
0 0
y
−0.5 −0.5
−1 −1
−1.5 −1.5
−2 −2
−2 −1.5 −1 −0.5 0 0.5 1 1.5 2 −2 −1.5 −1 −0.5 0 0.5 1 1.5 2
x x
1.5 1.5
1 1
0.5 0.5
0 0
y
y
−0.5 −0.5
−1 −1
−1.5 −1.5
−2 −2
−2 −1.5 −1 −0.5 0 0.5 1 1.5 2 −2 −1.5 −1 −0.5 0 0.5 1 1.5 2
x x
SPH simulation: collision2, t = 3.240 SPH simulation: collision2, t = 14.440
2 2
1.5 1.5
1 1
0.5 0.5
0 0
y
−0.5 −0.5
−1 −1
−1.5 −1.5
−2 −2
−2 −1.5 −1 −0.5 0 0.5 1 1.5 2 −2 −1.5 −1 −0.5 0 0.5 1 1.5 2
x x
2.5
ρ(0)
1.5
0.5
0 2 4 6 8 10 12 14 16
t
Figure 8. Evolution of central densities as a function of time for simulations 1, 3, 5, 7. We see that the simulations converge to the same
final density (with small deviation in simulation 2 due to the low number (N = 50) of particles). In simulation 7, the increased damping
allows for the star to reach its equilibrium state more quickly.
1.5
ρ(r)
0.5
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
r
Figure 9. Density profiles of simulations 1, 2, 3 compared to analytic model.
1.5
ρ(r)
0.5
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
r
Figure 10. Density profiles of simulations 4, 7, 8, 9 compared to analytic model.
2.5
2
ρ(r)
1.5
0.5
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
r
Figure 11. Density profiles of simulation 5 compared to analytic model.
2.5
2
ρ(r)
1.5
0.5
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
r
Figure 12. Density profiles of simulation 6 compared to analytic model.
4.5
4
ρmax
3.5
2.5
2
0 2 4 6 8 10 12 14 16
t
Figure 13. Maximum densities in the collision sequences of simulation 10 and 11.