SPH Matlab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Mon. Not. R. Astron. Soc. 000, 1–9 (2011) Printed 13 December 2011 (MN LATEX style file v2.

2)

Smoothed Particle Hydrodynamics: Theory,


Implementation, and Application to Toy Stars

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

1 INTRODUCTION bust. Lucy (1977) (who developed SPH independently) used


SPH to study a rotating polytropic fluid undergoing fission
Smoothed particle hydrodynamics (SPH) is a particle-based to produce a binary system. SPH since has, among other
method for computational fluid dynamics. It was originally applications, been used to study criteria for fragmentation
invented to simulate polytropic stellar models under non- in collapsing molecular clouds (Miyama, Hayashi & Narita
axisymmetric conditions (the original papers are Gingold 1984), collision problems (e.g. impact with the Moon (Benz,
& Monaghan (1977) and Lucy (1977)). The SPH method Slattery & Cameron 1986)), and the structure of dark mat-
can easily be extended to deal with a large variety of com- ter via large scale parallelized cosmological simulations com-
plex physical models. SPH works independently of any grid, bined with N -body (Springel 2005). Other notable applica-
unlike finite-difference methods, and interactions between tions of SPH include its usage to simulate Gollum falling
volume elements such as the pressure gradient are repre- into the lava in ‘The Lord of the Rings: The Return of the
sented as a force between particles. The method is purely King’ (Monaghan 2005).
Lagrangian, meaning that interactions and derivatives are
all evaluated in a coordinate system attached to a moving In the present paper, we discuss the theory of SPH, its
fluid element. The two main ideas of SPH are (1) to evolve implementation in Matlab, and applications to toy models
the positions and velocities of particles according to the cal- of polytropic stars. The mathematical theory is outlined in
culation of the forces on each particle at each time-step, 2. In 3 we discuss the physics of the simple toy star,
and (2) to use an interpolating/smoothing kernel to calcu- which is based on the paper Monaghan & Price (2004) (see
late forces and spatial derivatives. also the later work Monaghan & Price (2006)). Our choices
Since its inception, SPH has found a number of appli- for initial conditions and parameters are influenced by the
cations in astrophysics, engineering, and computer graphics work by Braune & Lewiner (2009). We discuss the Matlab
(see reviews Monaghan (1992, 2005); Rosswog (2009)). In code we wrote in 4. In 5 we describe the initial conditions
astrophysics, in particular, SPH is very useful to model flu- and parameters of our simulations. In 6 we present results
ids where boundary conditions are absent. It also has the of our simulations of polytropic stars in 2 and 3 dimensions
advantage of simultaneous conservation of linear and angu- and compare the equilibrium states to the analytic solutions.
lar momentum, regardless of the geometry of the physical We test two different smoothing kernels and the robustness
system (Price 2011). In addition, particles may be divided of SPH under variations in some of the parameters we are
into types of particles which interact differently with differ- free to choose (e.g. number of particles, kernel smoothing
ent types, representing a mixture of various fluids. In the length). In addition, we also apply SPH to study the collision
original paper, Gingold & Monaghan (1977) developed and of two toy stars. In 7 we discuss the results and ways in
used SPH to simulate the equilibrium structure of uniformly which we could expand the work, and also compare SPH to
rotating polytropic stars with magnetic fields and compared other numerical methods for fluid dynamics. Our conclusions
it to analytic calculations to show that the method is ro- are stated in 8.

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

fluid (e.g. gravity). In general P will be some function of ρ


−1
and thermal energy. In this work we will assume the fluid
is polytropic, meaning we have the simple relationship be-
−1.5
tween pressure and density:
P = kρ1+1/n (2) −2
−3 −2 −1 0 1 2 3
r
where k some constant and n is the polytropic index.
The main goal of the SPH method will be to evaluate Figure 1. Plot of the Gaussian and spline smoothing kernels, the
the acceleration dv (the equation of motion) of each particle magnitude of their gradients, and their Laplacians (h = 1, d = 2).
dt
(which will involve looking at pairwise interactions between
particles) and update the velocities and positions of the par- means the interaction is simply 0 if two particles are suffi-
ticles with each time step using an integrator scheme (more ciently far apart. The two kernels, the magnitude of their
on this later). The largest challenge is developing a method gradients, and their Laplacians are plotted in Figure 1.
to evaluate spatial derivatives. This challenge is overcome Having replaced the Delta function in equation (3) to
by first considering the identity: estimate A(r), now have an estimate A1 (r) of A(r):
 
A(r) = A(r )δ(r − r ) dr (3) A(r)  A1 (r) := A(r )W (r − r ; h) dr (8)
where A(r) is some arbitrary function that maps into R and We then apply a second approximation, namely discretiza-
δ(r) is the Dirac Delta function. In the SPH scheme, δ is re- tion (i.e., we can only work with a finite number of par-
placed with an approximation: a smoothing kernel W (r; h), ticles j = 1, ..., N ). Our new approximation A2 (r) of A(r)
where h is the smoothing length scale. The smoothing kernel becomes:
must have the properties
 A(r)  A2 (r) := A(xj )W (r − rj ; h)ΔVj (9)
W (r) dr = 1 (4) j

where ΔVj mj /ρj , rj is the position of particle j, mj is its


and mass, and ρj is its density. This is quite useful because now
lim W (r; h) → δ(r). (5) we can approximate derivatives:
h→0
The kernel should also be non-negative and invariant under ∇A2 (r) = A(xj )∇W (r − rj ; h)ΔVj (10)
j
parity.
Two common choices of the smoothing kernel are the and
Gaussian kernel:
 d ∇2 A2 (r) = A(xj )∇2 W (r − rj ; h)ΔVj (11)
1 j
W (r; h) = √ exp(−r2 /h2 ) (6)
h π simply by knowing the gradient and Laplacian of the
and the cubic spline approximation: smoothing kernel.
⎧ We can write Euler’s equation (1) as:
3 3

⎨(2 − q) − 4(1 − q) 0≤q<1
dv 1 1
W (r; h) = Ch (2 − q)3 1≤q<2 (7) = − ∇P + f (12)

⎩ dt ρ ρ
0 q≥2
and we can calculate:
where d is the dimension of the problem (d = 1, 2 or  
1 P P
3), q = r/h, and the normalization constant Ch = ∇P = ∇ + ∇ρ (13)
1
ρ ρ ρ2
, 5 , 1 for one, two, three dimensions respectively.
6h 14πh2 4πh3
The Gaussian is the best choice for reproducing physical an- which, applying equation (10), yields the SPH approxima-
swers (Gingold & Monaghan 1977) but the spline kernel has tion:
 
the advantage of being non-zero only on a compact support, dvi Pi Pj
=− mj + ∇W (ri − rj ; h) + bi (14)
which reduces computation time since we will see that SPH dt ρ2i ρ2j
j,j=i
involves calculating pairwise particle interactions using the
smoothing kernel and using a kernel with compact support where bi is the acceleration of particle i due to external

2011 RAS, MNRAS 000, 1–9


SPH Toy Stars 3
forces (to be calculated). In the equation above, we may also our physical simulations of toy stars) is to choose h to have
add any other physical forces (damping, viscous, Coriolis) the same order of magnitude as:
that are present in the physical system. The density can be
estimated as: ( r2
− r
2 )1/2 (20)

ρi = mj W (ri − rj ; h). (15) where ,
denotes averaging over the particles (Gingold &
j Monaghan 1977). A more sophisticated approach may be to
use an adaptive scheme where a different hi is chosen for
The SPH approximation equation (14) is the main equation each particle such that a ball of radius hi around particle i
needed to be solved to obtain the time evolution of the fluid. contains k neighbors (Monaghan 1992).

2.1 Leap Frog time integration


In SPH, we must provide initial conditions – the positions 3 TOY STARS
and velocities of the N particles. Then equation (14) may The toy star model of Monaghan & Price (2004) is described
be solved with a time integration method, such as Runge- by:
Kutta or Leap Frog. The Leap Frog method is often pre-
 
ferred because it is explicit, second order (making it better dvi Pi Pj
= −νvi − mj 2
+ 2 ∇W (ri − rj ; h) − λxi
than Euler’s method), and symplectic (meaning it conserves dt j,j=i
ρi ρj
energy). The Leap Frog scheme is as follows: (21)
v(t + Δt/2) = v(t − Δt/2) + a(t)Δt (16) in the CM frame, where the pressure obeys the polytropic
equation (2), ν is a damping coefficient added so that the
simulation reaches an equilibrium state, and the λ term is a
x(t + Δt/2) = x(t) + v(t + Δt/2)Δt (17)
simplified gravitational term. In the static case (v = 0) we
that is, we calculate positions and velocities at interleaved can solve the differential equation
time points. There are two important caveats. First, at the
1 k(1 + 1/n) 1/n
start we only know the initial conditions x(0) and v(0), and 0 = − ∇P − λx = − ρ ∇ρ − λx (22)
ρ ρ
must use Euler’s method to step back half a time step and
find v(−Δt/2). Second, in equation (16) we see that a(t) analytically and the density is given by a simple profile:
may actually depend on v(t), which would make the equa-  n
λ
tion implicit. However, a common approximation in SPH ρ(r) = (R2 − r 2 ) (23)
is to assume that the velocity at the current time plays a 2k(1 + n)
minor role in the computation of the acceleration (i.e. ve- where r is radial position and R is the radius of the toy star,
locity changes are small with each time step) and then the beyond which the density is 0. For example, in the case that
following approximation can be made: n = 1, we have the simple density profile:
v(t − Δt/2) + v(t + Δt/2) λ
v(t + Δt) = , (18) ρ(r) = (R2 − r 2 ). (24)
2 4k
which keeps the method explicit and hence more computa- If we give the star a radius R and a mass M , then the density
tionally efficient. must integrate up to the mass, i.e.:

M = dd rρ(r) (25)
2.2 Choosing time steps and scale lengths
In SPH, we need to choose a value for the time step Δt and where d is the dimension of the problem, which allows us to
the kernel smoothing length h and here we discuss some solve for λ:
strategies for setting their values. ⎧
⎨2kπ −1/n M (1 + n)/R2 1+1/n /M d=2
The time step needs to be satisfy λ= 5 +n) 1/n (26)

⎩2k(1 + n)π −3/(2n) M Γ( 2
/R2 d = 3
h h R3 Γ(1+n)
Δt ≤ min{ , } (19)
vmax Fmax In summary, we expect SPH to yield these static solutions
to meet the CFL condition for stability (Gingold & Mon- when the particle motions are damped. Comparison with
aghan 1977), where vmax is the maximum velocity and Fmax analytic solutions provides a means to test the accuracy of
is the maximum force on a particle at a given time. In the SPH.
present work, we choose a fixed small enough value for Δt
that yielded accurate physical results. In larger scale prob-
lems, an adaptive time stepping scheme is often employed
4 IMPLEMENTATION OF SPH
(Monaghan 1992).
The value of h needs to be chosen carefully in SPH, es- We developed a SPH code in Matlab and ran all simula-
pecially if one is working with a kernel with compact support tions on a 2.50 GHz processor dual-core laptop. Each sim-
(such as the cubic spline kernel). If the kernel has compact ulation completed in under half an hour. In this section we
support and the smoothing length is too small, then the par- present some pseudo-code highlighting the main aspects of
ticle may not interact with other particles. One strategy for the SPH algorithm.
choosing a kernel length (which proved to be sufficient for Densities are calculated as follows:

2011 RAS, MNRAS 000, 1–9


4 P. Mocz
Calculate Density(x, m, h){ Table 1. Parameters for the toy star simulations
for i = 1 : N
% initialize density with i = j contribution Simulation 1 typical star collapse into equilibrium
rho(i) = m ∗ kernel(0, h);
Parameter Value
for j = i + 1 : N
number of particles N = 100
% calculate vector between two particles dimension d=2
uij = x(i, :) − x(j, :); star mass M =2
rho ij = m ∗ kernel(uij, h); star radius R = 0.75

% add contribution to density smoothing length h = 0.04/ N/1000
rho(i)+ = rho ij; time step Δt = 0.04
rho(j)+ = rho ij; damping ν=1
end pressure constant k = 0.1
end polytropic index n=1
} max time steps 400
kernel spline
where x are the particle positions, m is the mass of each initial config. random inside circle radius R
particle, and h is the smoothing length. Simulation 2 reduced number of particles
The accelerations due to pressure, damping, and gravity number of particles N = 50
are calculated as follows:
Simulation 3 increased number of particles
Calculate Acceleration(x, v, m, rho, P, nu, lambda, h){ number of particles N = 400
% initialize accelerations Simulation 4 Gaussian kernel
a = zeros(N, dim);
kernel Gaussian
% add damping and gravity
for i = 1 : N Simulation 5 3-D simulation
a(i, :)+ = −nu ∗ v(i, :) − lambda ∗ x(i, :); number of particles N = 300
end dimension d=3
% add pressure Simulation 6 altered polytropic index
for i = 1 : N polytropic index n=2
for j = i + 1 : N Simulation 7 more damping
% calculate vector between two particles
damping ν=4
uij = x(i, :) − x(j, :);
% calculate acceleration due to pressure Simulation 8 increased smoothing length

p a = −m ∗ ( P(i) 2 + P(j) 2 ) ∗ gradkernel(uij, h); smoothing length h = 0.06/ N/1000
rho(i) rho(j)
a(i, :)+ = p a; Simulation 9 separated material init. condition
a(j, :)+ = −p a; number of particles N = 500
end initial config. 1/5 of particles offset by distance 2.4
end
} interactions only between the k nearest neighbors and us-
ing a hierarchical tree algorithm (see, e.g. Hernquist & Katz
where v are the particle velocities and P are the calculated (1989)). In this project, we will only deal with < 1000 par-
pressures from the density. ticles and therefore can calculate all pairwise interactions in
The main loop in the SPH algorithm (the Leap Frog a reasonable time.
scheme) looks like:
Main Loop
for i = 1 : max time step 5 SIMULATIONS
v phalf = v mhalf + a ∗ dt;
We performed 11 simulations of toy stars in dimensionless
x+ = v phalf ∗ dt;
units. In most of the simulations, we wished to find the equi-
v = 0.5 ∗ (v mhalf + v phalf);
librium state of a collapsing polytropic star from random
v mhalf = v phalf;
initial conditions. We tested the effect of altering the num-
% update densities, pressures, accelerations
ber of particles N in the simulation, the type of kernel, the
rho = Calculate Density(x, m, h);
dimension of the problem, the polytropic index, the effect of
P = k ∗ rho. ∧ (1 + 1/npoly);
the size of damping, the size of the smoothing length h, and
a = Calculate Acceleration(x, v, m, rho, P, nu, lambda, h);
the effect of initial conditions. We also simulated collisions of
end
two toy stars to see where the material from the initial stars
With the simple algorithm we have prescribed above, end up in the final merged star and what are the physical
we see that given N particles the algorithm is order N 2 conditions during the evolution. The simulation parameters
because we have to look at pairwise interactions between and setup are described in Tables 1 and 2. If a parameter is
particles. Due to the symmetric nature of the pairwise in- not listed under a simulation, it takes the same value as the
teraction, the amount of computations is reduced by a factor first simulation in the table. All simulations are performed
of 2. Alternative methods have been developed in literature in the center of mass frame. Included with the report are
to make the computations more efficient, such as computing videos (*.wmv files) of some of the simulations.

2011 RAS, MNRAS 000, 1–9


SPH Toy Stars 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

2011 RAS, MNRAS 000, 1–9


6 P. Mocz
SPH simulation: 1, t = 0.040 SPH simulation: 1, t = 0.360
1 1
1.8
2.8
0.8 0.8

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

0.8 2.4 0.8

2
0.6 0.6
2.2

0.4 0.4 1.8


2

0.2 0.2
1.8
1.6
density

density
0 0
y

1.6

−0.2 −0.2 1.4


1.4

−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

2011 RAS, MNRAS 000, 1–9


SPH Toy Stars 7
SPH simulation: 3, t = 0.040 SPH simulation: 3, t = 0.360
1 1
2.2
0.8 2.6 0.8

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

0.8 2.4 0.8

2
0.6 2.2 0.6

0.4 0.4 1.8


2

0.2 1.8 0.2


1.6
density

density
0 0
y

1.6
1.4

−0.2 1.4 −0.2

1.2
−0.4 1.2 −0.4

−0.6 −0.6 1
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

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

where Πij has the form



⎨ −αcμij +βμ2ij 7.3 Comparison of SPH to grid-based methods
− ρij
vij · rij < 0
Πij = (29) The SPH method, unlike finite difference grid-based meth-
⎩0 vij · rij > 0
ods, is inherently non-dissipative. This means that in the
where absence of explicitly added dissipative terms, the numerical
hvij · rij solution will neither dissipate nor diffuse energy artificially
μij = (30) (Price 2011). This is one of the strengths of SPH in mod-
r2ij + η 2
eling certain fluid flows. This can also be a disadvantage in
where rij = ri − rj , vij = vi − vj , ρij = ρi − ρj (see some cases. Dissipative terms need to be added explicitly
Monaghan (1992)). to capture physical processes such as shocks (for example,
It is also possible to add a boundary to the SPH simu- the viscosity term equation (28) which contains non-physical
lation which serves as a wall. This was added into our SPH terms) (Price 2011).
code, although was not required for the toy star simulations. Unlike grid-based methods, the Hamiltonian that de-
A boundary wall can be simulated as a spring. It gives any scribes the equations of motion of particles in SPH guaran-
particle inside the wall a force proportional to the depth tees that there is a particle arrangement that minimizes the
of penetration and directed perpendicular to the wall. An Lagrangian (Price 2011). This makes SPH very useful for
additional damping factor may also be added. studying the evolution of a system reaching a stable equilib-
With an increased number of particles and lots of com- rium state as we have done. A stable and noise-free particle
puting power, very complicated hydrodynamic processes, arrangement is a usually a good indicator of the accuracy

2011 RAS, MNRAS 000, 1–9


8 P. Mocz
SPH simulation: 5, t = 0.040 SPH simulation: 5, t = 0.360

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.

of SPH solutions (Price 2011). We see in our simulations 8 CONCLUSION


(Figures 2, 3, 4, 5, 6, and 7) that this is the case.
In conclusion, we have demonstrated that SPH can reli-
ably reproduce expected physical results in fluid dynamics.
The resolution of SPH is determined by the masses of We have used SPH to determine the equilibrium density
the particles (Price 2011) instead of grid size in finite dif- profile of polytropic toy stars, which agreed well with an-
ference methods. This is very useful in astrophysics, making alytic profiles. We also explored the collision sequence of
SPH a great choice to study structure formation and grav- two polytropic stars to demonstrate how SPH can be used
itational collapse. However, in cases where the number of to track the evolution of physical quantities. We also dis-
SPH particles required to resolve a phenomena needs to be cussed SPH compared to finite difference methods. Both
the same order of magnitude as the number of cells in a approaches have their own strengths.
grid-based version of the problem, the grid-based method is
more computationally efficient, by a factor of around 10 in
practice (Price 2011). REFERENCES
Benz W., Slattery W. L., Cameron A. G. W., 1986, Icarus,
Much research and progress has been done with both 66, 515
particle and grid-based algorithms. One recent develop- Braune L., Lewiner T., 2009, Dept. Math, PUC-Rio, 11, 1
ment, which is sort of a mix of the two, has been to use Duffell P. C., MacFadyen A. I., 2011, ApJS, 197, 15
a mesh which distorts with time with the local fluid veloc- Gingold R. A., Monaghan J. J., 1977, MNRAS, 181, 375
ity, resulting in an effectively Lagrangian method (Duffell & Hernquist L., Katz N., 1989, ApJS, 70, 419
MacFadyen 2011). The algorithm by Duffell & MacFadyen Lucy L. B., 1977, AJ, 82, 1013
(2011) uses Voronoi tessellations to generate a numerical Miyama S. M., Hayashi C., Narita S., 1984, ApJ, 279, 621
mesh which moves its mesh-generating points in the tessel- Monaghan J. J., 1992, ARA&A, 30, 543
lation. —, 2005, Reports on Progress in Physics, 68, 1703

2011 RAS, MNRAS 000, 1–9


SPH Toy Stars 9
SPH simulation: 9, t = 0.040 SPH simulation: 9, t = 1.640
3 1.4 3

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.

Monaghan J. J., Price D. J., 2004, MNRAS, 350, 1449


—, 2006, MNRAS, 365, 991
Price D. J., 2011, ArXiv e-prints
Rosswog S., 2009, NAR, 53, 78
Springel V., 2005, MNRAS, 364, 1105

This paper has been typeset from a TEX/ LATEX file prepared
by the author.

2011 RAS, MNRAS 000, 1–9


10 P. Mocz
SPH simulation: collision, t = 0.040 SPH simulation: collision, t = 1.640
2 2

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

Figure 6. A few steps of the collision in Simulation 10.

2011 RAS, MNRAS 000, 1–9


SPH Toy Stars 11
SPH simulation: collision2, t = 0.600 SPH simulation: collision2, t = 1.640
2 2

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

Figure 7. A few steps of the collision in Simulation 11.

2011 RAS, MNRAS 000, 1–9


12 P. Mocz

Evolution of central density as a function of time


3.5
sim 1
sim 2
sim 3
sim 7
3

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.

2011 RAS, MNRAS 000, 1–9


SPH Toy Stars 13

Density profile of polytrope in equilibrium


2.5
sim 1
sim 2
sim 3
theory

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.

2011 RAS, MNRAS 000, 1–9


14 P. Mocz

Density profile of polytrope in equilibrium


2.5
sim 1
sim 4
sim 7
sim 8
sim 9
2 theory

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.

2011 RAS, MNRAS 000, 1–9


SPH Toy Stars 15

Density profile of polytrope in equilibrium


3
sim 5
theory

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.

2011 RAS, MNRAS 000, 1–9


16 P. Mocz

Density profile of polytrope in equilibrium


3.5
sim 6
theory

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.

2011 RAS, MNRAS 000, 1–9


SPH Toy Stars 17

Maximum densities as a function of time in collision sequences


5
sim 10
sim 11

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.

2011 RAS, MNRAS 000, 1–9

You might also like