N-Body Simulation in Cosmology: Thesis To Be Submitted in Partial Fulfillment of The Requirements For The Degree
N-Body Simulation in Cosmology: Thesis To Be Submitted in Partial Fulfillment of The Requirements For The Degree
of
Masters in Science
by
Aurobinda Majhi
23PH40014
PHYSICS
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
Department of Physics
Indian Institute of Technology,
Kharagpur
India - 721302
CERTIFICATE
This is to certify that we have examined the thesis entitled N-body Simulation
in Cosmology, submitted by Aurobinda Majhi(Roll Number: 23PH40014 ) a
postgraduate student of Department of Physics in partial fulfillment for the award
of degree of Masters in Science. We hereby accord our approval of it as a study carried
out and presented in a manner required for its acceptance in partial fulfillment for
the Post Graduate Degree for which it has been submitted. The thesis has fulfilled all
the requirements as per the regulations of the Institute and has reached the standard
needed for submission.
Supervisor
Department of Physics
Indian Institute of Technology,
Kharagpur
Place: Kharagpur
Date:
ii
ABSTRACT
iii
Contents
1 N-body simulation 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Equations of motion . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Equations of motion in simulation space . . . . . . . . . . . . . . . . 6
1.4 Vlasov equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4.1 Eulerian Vlasov Equation . . . . . . . . . . . . . . . . . . . . 7
1.4.2 Lagrangian Vlasov Equation . . . . . . . . . . . . . . . . . . . 7
1.4.3 0th moment of Vlasov equation . . . . . . . . . . . . . . . . . 8
1.4.4 1st moment of Vlasov equation . . . . . . . . . . . . . . . . . 8
2 Simulation 11
2.1 Begin of Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 Initial perturbation from grid position . . . . . . . . . . . . . . . . . 11
2.2.1 Fourier representation of density contrast from Power spectrum 12
2.2.2 Initial Power Spectrum . . . . . . . . . . . . . . . . . . . . . . 12
2.2.3 Zel’dovich Approximation . . . . . . . . . . . . . . . . . . . . 14
2.3 Mass asignment to grid position . . . . . . . . . . . . . . . . . . . . . 15
2.3.1 NGP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.2 CIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.3 TSC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4 Potential Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.1 Poisson equation . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4.2 Telegraph equation . . . . . . . . . . . . . . . . . . . . . . . . 20
2.5 Calculate Force . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.6 Position and Velocity update . . . . . . . . . . . . . . . . . . . . . . . 22
2.7 flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
iv
3 Tests for the simulation program 25
3.1 Tests for Mass asignment . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.1.1 Test for uniform distribution . . . . . . . . . . . . . . . . . . . 25
3.1.2 Fourier space Validation . . . . . . . . . . . . . . . . . . . . . 25
3.2 Force accuracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.2.1 Force accuracy test - I . . . . . . . . . . . . . . . . . . . . . . 27
3.2.2 Force accuracy test - II . . . . . . . . . . . . . . . . . . . . . . 27
3.3 Energy Conservation . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.3.1 Energy conservation Test - I . . . . . . . . . . . . . . . . . . . 28
3.3.2 Energy conservation Test - II . . . . . . . . . . . . . . . . . . 29
3.4 Effect of Force Resolution . . . . . . . . . . . . . . . . . . . . . . . . 30
3.4.1 One dimensional perturbation test . . . . . . . . . . . . . . . 30
3.4.2 3 dimensional perturbation test . . . . . . . . . . . . . . . . . 32
3.5 Telegraph equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.5.1 Static density tests . . . . . . . . . . . . . . . . . . . . . . . . 35
3.5.2 Gaussian density test . . . . . . . . . . . . . . . . . . . . . . . 36
3.5.3 Compairing Total Force with Poisson Solver . . . . . . . . . . 37
3.5.4 Iteration vs Error . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.5.5 kappa vs Error . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4 Program 42
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.2 Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.2.1 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.2.2 Initial Conditions . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.2.3 Cosmology Calculations . . . . . . . . . . . . . . . . . . . . . 43
4.2.4 Mass Assignment . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.2.5 Poisson Solvers . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.2.6 Force Calculations . . . . . . . . . . . . . . . . . . . . . . . . 44
4.2.7 Time Integration . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.2.8 Energy Calculations . . . . . . . . . . . . . . . . . . . . . . . 44
4.2.9 Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.2.10 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
5 Conclusion 45
v
Chapter 1
N-body simulation
1.1 Introduction
N-body simulations in cosmology are computational techniques used to model the
evolution of particle systems under mutual gravitational interactions. These simula-
tions are crucial for understanding the formation of cosmic structures like galaxies,
galaxy clusters, and the cosmic web. Each particle represents a macro object, such
as a galaxy or a dark matter halo, allowing large-scale matter behavior to be studied
over cosmic timescales.
Simulations begin with a nearly homogeneous particle distribution, where each
particle approximates a galaxy by representing a large number of stars. Over time,
density fluctuations grow due to gravitational instability, forming cosmic structures.
The evolution of density contrast in an expanding universe is computed by solving
the gravitational field generated by all particles. The resulting field updates particle
velocities and positions iteratively, generating a new gravitational potential at each
time step. This iterative process continues until the final cosmic structure emerges,
providing insights into large-scale universe evolution.
1
L = m⃗r˙ 2 − mψ(⃗r, t) (1.1)
2
1
Here ⃗r is the physical position vector of the particle of mass m from the origin.
The scalar potential ψ(⃗r, t) satisfies the equation:
⃗r = a(t)⃗x
Here a(t) is the Hubble flow constant. Putting this in to the lagrangian we would
get,
1
L = m(ȧ⃗x + a⃗x˙ )2 − mψ(⃗x, t) (1.3)
2
*we are going to write ⃗x as x for simplicity.
The Poisson equation becomes,
or, Z t2
1
S= ( m(ȧx + aẋ)2 − mψ(x, t))dt (1.5)
t1 2
Take ȧ = da
dt
⇒ dt = da
ȧ
, dx
dt
= dx
da
ȧ = x′ ȧ and m = 1. So the action integral is now
becomes,
Z a2
1 ψ
S= ( (ȧa2 x′2 + ȧx2 + 2ȧax′ x) − )da (1.6)
a1 2 ȧ
2
we add and subtract the term aäx2ȧ
,
Z a2
1 aäx2 aäx2 ψ
S= ( (ȧa2 x′2 + ȧx2 + 2ȧax′ x + )− − )da (1.7)
a1 2 ȧ 2ȧ ȧ
2
By adding this term we have can write the 3 terms as,
d aäx2
(ȧax2 ) = + ȧx2 + 2ȧax′ x
da ȧ
Now the action integral is rewritten as,
Z a2
1 a
( ȧa2 x′2 − ϕ)da + ȧax2 a21
S= (1.8)
a1 2
Where,
äax2 ψ
ϕ= + (1.9)
2ȧ ȧ
Let Z a2
′ 1
S = ( ȧa2 x′2 − ϕ)da
a1 2
Then both S and S’ will result in same equation of motion. So Final action integral
becomes, Z a2
1
S= ( ȧa2 x′2 − ϕ)da (1.10)
a1 2
Our new scalar potential Now follows the equation,
¨
3a 4πGa2 ρ
∇2 ϕ = + (1.11)
H ȧ
*∇2 x2 = 6 as x is a 3d vector.
Where H(a) is called the Hubble parameter whose value is,
ȧ
H(a) =
a
Friedman equation is given by,
Λc2
ä 4πG 3p
=− ρ̄ + 2 +
a 3 c 3
Here p is the the pressure exerted by the contents of the universe (matter, radia-
tion, dark energy, etc.) and Λ is a constant energy density filling space homogeneously,
representing dark energy and ρ̄ is the mean density.
we set p = 0 for matter dominated era and Λ = 0 matter or radiation dominated
epochs. Then Friedmann’s equation can be written as,
ä 4πG
=− ρ̄
a 3
3
putting this in equation for ϕ we get,
4πGaρ̄
∇2 ϕ = δ (1.12)
H
We defined another parameter δ(x, t) which is called density contrast, that defined
as:
ρ − ρ̄
δ(x, t) = (1.13)
ρ̄
Lagrangian form for the action integral equation (1.10) is,
1
L = ȧa2 x′2 − ϕ (1.14)
2
Momentum of the particle from the lagrangian is obtained from the relation,
∂L
P =
∂x′
⇒ P = ȧa2 x′ = H(a)a3 x′
dx P v
⇒ = 3
= (1.15)
da H(a)a H(a)a3
and From Euler-Lagrange’s equation we obtain,
dv
= −∇ϕ (1.16)
da
Equations 1.14 and 1.15, together with Equation 1.12 are the essential equations
required to fully understand the motion of the particle in this framework.
We further modify Equation 1.12 by simplifying it, specifically by altering the
potential term ϕ , to achieve a more reduced form,
4πGa ′
ϕ= ρ̄ϕ
H(a)
Friedmann’s equation is given as,
3H02
ρc =
8πG
Where ρc is the critical density and H0 is the Hubble parameter at a time t = 0.
The density parameter Ω compares the mean density to the critical density:
ρ̄
Ω = Ω m + Ωr + ΩΛ =
ρc
4
Ω = 1, The universe is flat.(Curvature parameter k = 0)
Ω > 1, The universe is closed.(Curvature parameter k = +1)
Ω < 1, The universe is Open.(Curvature parameter k = −1)
For our purposes we take Ω = 1.
ρ̄ = ρc
In an expanding universe, the mean matter density scales inversely with the cube
of the scale factor because matter dilutes as the volume increases:
ρ̄ = a−3 ρ¯o
or,
3H02
ρ̄ = a−3
8πG
So the potential becomes,
4πGa −3 3H02 ′
ϕ= a ϕ
H(a) 8πG
or,
3 H0
ϕ= ϕ′ (1.17)
2 a2 E(a)
Where,
H(a) = H0 E(a)
dv 3 H0
=− 2 ∇ϕ′ (1.19)
da 2 a E(a)
∇2 ϕ′ = δ (1.20)
5
1.3 Equations of motion in simulation space
In the simulation space, the position and velocity of a particle can be expressed in
terms of scaled variables as follows:
x = LX
v = LH0 V
Define,
ϕ
ϕ̃ =
L2
˜ = L∇
∇
So putting all these into our equation of motion we get,
dX V
= 3 (1.21)
da a E(a)
dV 3 1 ˜ ϕ̃
=− 2 ∇ (1.22)
da 2 a E(a)
˜ 2 ϕ̃ = δ
∇ (1.23)
Equations 1.21, 1.22, and 1.23 collectively represent the equations of motion es-
sential for conducting the simulation.
Our code generates a three-dimensional grid of evenly spaced cubes, each with
side length L. The grid follows periodic boundary conditions, meaning that if any
coordinate reaches N, it wraps around to zero. This ensures seamless continuity in
simulations and maintains a consistent spatial structure across all dimensions.
6
1.4.1 Eulerian Vlasov Equation
To derive the Eulerian Vlsov eqution we assueme that the particles move under grav-
itational potential ϕ(r, t) and there is no collison between them. The distribution
function f(r, v, t) gives the number density of particles in phase space.
The continuity equation in phase space (Liouville’s theorem) states that the total
time derivative of f is zero:
df
=0
dt
expanding using the chain rule:
∂f dr dv
+ .∇r f + .∇v f = 0
∂t dt dt
since,
dr
=v
dt
and,
dv
= −∇r ϕ
dt
∂f
+ v.∇r f − ∇r ϕ.∇v f = 0 (1.24)
∂t
This is the Eulerian Vlasov equation. The term ∂f
∂t
represents the change in distri-
bution function f at a fixed point in phase space. The term ⃗v · ∇r f describes the
transport of particles through space due to their velocity, while −∇r Φ · ∇v f captures
changes in velocity from forces like gravity or potentials.
∂L
ṗ = v̇ = = −∇x ϕ
∂x
Define the co-moving distribution function f(x,p,t). Then equation (1.24) becomes,
∂fc p
+ .∇x fc − a∇x ϕ.∇v fc = 0 (1.25)
∂t a
This is the Lagrangian Vlasov equation.
7
1.4.3 0th moment of Vlasov equation
integrate equation (1.25) with respect to v,
Z
∂fc p
+ .∇x fc − a∇x ϕ.∇v fc d3 v = 0
∂t a
define, Z
ρ = f d3 v
Z
vf d3 v = ρv
The zeroth moment of the Vlasov equation gives the continuity equation, repre-
senting mass or number density conservation in phase space. The first moment gives
the momentum equation, describing how the bulk velocity of particles evolves under
external forces like gravity or electromagnetic fields, linking microscopic dynamics to
macroscopic fluid behavior.
8
define, Z
πij = pi pj f d3 v
v
V =
a
Then the integral becomes,
∂Vi ȧ 1 ∂Vi 1 ∂πij 1 ∂ϕ
+ Vj + Vj + + =0
∂t a a ∂x ρ̄(1 + δ)a3 ∂xi a ∂xi
The first moment of the Vlasov equation gives the momentum equation, which
describes how momentum flows and changes within a fluid over time. Velocity disper-
sion simply measures how much individual particle velocities differ from the average
velocity of the system. In smooth, streamline flow — where particle paths don’t cross
— the stress or pressure from velocity dispersion becomes zero.
So our equation becomes,
∂Vi ȧ 1 ∂Vi 1 ∂ϕ
+ Vj + Vj =− (1.27)
∂t a a ∂x a ∂xi
taking only linear terms, taking divergence of (27) and putting equation (26) in
it and also using (20), we get
∂ 2δ ȧ ∂δ
2
+2 =δ (1.28)
∂t a ∂t
Lets take a trial solution for the equation (1.28),
δ = D(t)δ(x)
Then we got,
ȧ
D̈ + 2 Ḋ = D
a
This is the Linear growth equation, the growing mode refers to the solution of
the density perturbation equation that describes how small initial fluctuations in the
matter density of the universe grow over time due to gravitational instability. In
the early universe, regions with slightly higher density attract more matter, causing
these overdense regions to become even denser. This positive feedback leads to the
formation of large-scale structures like galaxies and galaxy clusters. The growing
mode dominates in the matter-dominated era of the universe, while the decaying
mode, which decreases over time, becomes negligible. The rate of growth of these
9
perturbations depends on factors like the expansion rate of the universe and the
background matter content.
Solution for the growing mode is,
D = E(a)
and,
a
da′
Z
D = E(a) (1.29)
0 a′3 E 3 (a′ )
10
Chapter 2
Simulation
11
accuracy of the simulation by allowing the particles to interact more naturally, as
their positions are no longer constrained to the grid intersections.
Where ∆(k) is the Fourier representation of density contrast δ and P(K) is the
power Spectrum. where a(k) and b(k) are two real valued, independent Gaussian
random variables of unit variance, and the power spectrum is given by,
Where A, T(K) and ns are Amplitude, Transfer function and the Spectral index.
ns = 1, Scale invariant(Harrison-Zel’dovich spectrum).
ns > 1, ”Blue” spectrum (more power at small scales).
ns < 1, ”Red” spectrum (more power at large scales).
From the Latest Planck(2018) results gives the value of spectral index as,
ns = 0.9649 ± 0.0042
The transfer function T(k) relates the initial power spectrum Pinit (from inflation,
for example) to the matter power spectrum P(k) observed at a later time:
12
T (k) → 1 on large scales (small k), where the initial conditions remain mostly un-
changed.
T (k) < 1 on small scales (large k), where processes like Silk damping and horizon
crossing reduce power.
The Transfer function has the form,
Where Tb (k) and Tm (k) are the baryon transfer function and CDM+Neutrino
transfer function and fb is the baryon fraction given by,
Ωb
fb =
Ωm
The baryon transfer function is given by,
sin(ks) −( k k )2
Tb = e silk
ks
Where the quantity s and ksilk are given by,
44.5ln( Ω9,83
mh
2)
s= p
1 + 10(Ωh2 )3/4
Γ = Ωm h
and,
ke q = 0.0764Ωm h2
Define the quantity σ8 , as the variance of matter fluctuation smoothed at R =
−1
8h M pc, where R is the Radius. Then σ8 is given by,
Z ∞ 3
2 k P (k) 2 dk
σ8 = |W (k)|
0 2π 2 k
13
Putting the equation for Power spectrum P(k) we get,
Z ∞
2 dk
σ8 = AT 2 (k)k ns |W (k)|2
0 k
or,
σ82
A=
I
Where, Z ∞
dk
I= T 2 (k)k ns |W (k)|2
0 k
Here W(kR) is the fourier transform of the top-hat window function which repre-
sents averaging over a spherical volume and is given by,
sin(kR) − kRcos(kR)
W (kR) = 3
(kR)3
From plancks(2018) results we get the value of σ as,
r
Ωm
σ8 = 0.81
0.3
By Calculating the value of I and σ8 we can find the value of A and by calculating
T(k) we can find the power spectrum and Thus fourier representation of the density
contrast.
x(q, t) = q + ψ(q, t)
14
so,
x(q, t) = q + D(t)ψ0 (q)
and since the velocity is the time derivative of the position we get,
Ḋ = Hf D
dlnD
where f = dlna
is the growth rate.
The growth rate f describes how fast perturbations grow relative to the expansion:
f = Ωγm
2.3.1 NGP
In this particle scheme we assign mass of the particle to single nearest grid point.
X
ρ= WN GP (xi − n)
15
Where δxi = xi − n. For three dimension we multiply the three weight function for
total function.
W = Wx Wy Wz
2.3.2 CIC
After the displacement, the particles are no longer confined to the grid points, but
we still define the density at the grid points. In this method, we distribute the mass
of each particle across several nearest neighboring grid points. First, we identify the
8 surrounding grid points in the 3D simulation space. Then, we assign the particle’s
mass to these 8 grid points, weighted by their distance from the particle’s position.
2.3.3 TSC
The Triangular Shaped Cloud (TSC) method is a commonly used mass assignment
scheme in N-body cosmological simulations. It is an improvement over simpler meth-
ods like Nearest Grid Point (NGP) and Cloud-in-Cell (CIC), providing smoother and
more accurate density estimates. In TSC, a particle’s mass is distributed not only
to its nearest grid point but also to neighboring grid points within a certain range,
following a triangular weight function. The weight decreases linearly with distance
from the particle’s position, ensuring continuity and reducing noise in the density
field. This helps to minimize artifacts in force calculations and improves the accuracy
of the simulation, especially on small scales.
(
3
− δxi , δxi ≤ 1/2
WT SC (xi − n) = 41 3 2
2 2
− δxi , 21 ≤ δxi ≤ 3
2
16
changes in potential, adding a time derivative term. This allows the potential to
evolve dynamically over time rather than responding instantly, making it useful for
studying systems where delayed gravitational response or wave-like behavior of po-
tential is important. Comparing both approaches helps in understanding their effects
on structure formation and the evolution of density perturbations in the universe.
−k 2 ϕ̃ = ∆ (2.4)
with,
X 2πni
k2 = ( )2
i
Ni
17
Where ϕ̃ is the fourier transform of ϕ and ∆ is the fourier transform of δ.
∆
ϕ̃ = − (2.5)
k2
This is much easier and faster to solve. After finding ϕ̃ in Fourier space, we apply
the inverse Fourier Transform to get the potential ϕ in real space. This method,
combined with Fast Fourier Transform (FFT) algorithms, makes solving the Poisson
equation on large grids practical and accurate, especially in cosmological simulations
where periodic boundary conditions are used.
The Fourier Transform method naturally handles periodic boundary conditions,
which is why it is widely used in cosmological N-body simulations. In this approach,
the simulation box is treated as if it repeats infinitely in all directions, creating a
periodic universe. This assumption fits well with the Fast Fourier Transform (FFT)
algorithm, which inherently assumes that the input data is periodic. When we solve
the Poisson equation in Fourier space, the periodicity ensures that any gravitational
effect due to particles near the boundary of the box is smoothly connected to the
particles on the opposite side. This avoids artificial edge effects and ensures that the
gravitational potential and forces are consistent throughout the simulation domain.
Thus, the Fourier method not only simplifies solving the Poisson equation but also
elegantly incorporates periodic boundary conditions essential for modeling an infinite,
homogeneous, and isotropic universe.
The solution of poisson equation can be realized in terms of green’s function
G(x,x’) as
X
ϕl,m,n = Gp,q,r δp,q,r e2iπ(lp+mq+nr)/M (2.6)
p,q,r
where M is the grid size in one axis.For poisson equation there are two green’s function
can be used. The usual one is,
(
0, p=q=r=0
Gp,q,r = 1
(2.7)
− k2 else,
18
This can be obtained by using central difference method then transforming it into
fourier space.
We can solve the poisson equation by reducing it in matrix form. Using central
difference poison’s equation for 1 dimension can be approximated as,
Aϕ = δ (2.10)
where Aϕ is equivalent to the equation 2.10. So the matrix A would be in the
form,
−6 1 0 ... ...
1 −6 0 ... ...
0 1 −6 ... ...
. . . .... ..
ϕ0,0,0 δ0,0,0
ϕ1,0,0
, δ = δ1,0,0
Also ϕ and δ in this representation would be, ϕ =
ϕ2,0,0 δ2,0,0
... ...
So to calculate ϕ given δ we would need to solve the equation,
ϕ = A−1 δ (2.11)
Using the matrix form of the Poisson equation offers several advantages, especially
when dealing with non-periodic boundary conditions or more complex geometries
where Fourier methods may not be directly applicable.
The key advantages are:
19
• Locality: It allows for a more localized treatment of gravity without assuming
the whole universe repeats itself, which is useful for isolated systems.
• Better Control Over Accuracy: Directly solving the matrix system can provide
better accuracy in small-scale structures where Fourier methods might introduce
smoothing errors due to grid resolution limits.
However, the main drawback is that matrix methods are computationally more ex-
pensive for large 3D grids compared to Fourier methods, unless optimized solvers (like
multigrid or iterative methods) are used.
20
The added term effectively mimics a finite signal propagation speed for gravitational
interactions, ensuring smoother and more stable evolution of the potential in iterative
solvers commonly used in N-body or particle-mesh simulations.
The damped equation is,
∂ 2ϕ ∂ϕ
2
+ 2κ − c2g ∇2 ϕ = −c2g δ
∂t ∂t
*we take cg = 1 for entire simulation.
Since in our simulation we have transformed the variable t to a, the telegrpah
equation becomes,
∂ 2ϕ dH ∂ϕ
a2 H 2 2
+ aH[H + a + 2κ] − ∇2 ϕ = −δ
∂a da ∂a
or
∂ 2ϕ 1 1 dH 2κ ∂ϕ ∇2 ϕ δ
+ [ + + ] − = − (2.12)
∂a2 a H da aH ∂a a2 H 2 a2 H 2
In numerical simulations, it is often advantageous to split a second-order differential
equation into two coupled first-order equations.The main reason for this is that most
standard time integration schemes, like Euler or Runge-Kutta methods, are naturally
designed to handle first-order equations. By converting a second-order derivative
into two first-order derivatives, we can treat the problem as a system of coupled
ordinary differential equations (ODEs), which are easier to solve numerically with
better control over accuracy and stability.
∂ϕ
=ψ (2.13)
∂a
∂ϕ ∇2 ϕ δ 1 1 dH 2κ
= 2 2 − 2 2 −[ + + ]ψ (2.14)
∂a aH aH a H da aH
The telegraph equation provides a faster alternative to the Poisson equation. In
the standard Poisson equation, the gravitational potential is solved by inverting the
Laplacian operator, which often requires computationally expensive global operations
like Fourier transforms or iterative solvers that take time to converge, especially for
large grids or rapidly changing density fields. In contrast, the telegraph equation in-
troduces a time-dependent term that allows the potential to evolve gradually towards
its correct value through local updates at each time step. This effectively converts
the problem from a global elliptic equation to a local hyperbolic-type equation, where
information propagates at a finite speed. As a result, the telegraph equation can often
21
reach an accurate solution in fewer iterations, with simpler and faster computations
at each step, making it highly suitable for dynamic simulations with evolving density
distributions.
F = −∇ϕ
ϕi+1,j,k − ϕi−1,j,k
FX ≈
2h
ϕi,j+1,k − ϕi,j−1,k
FY ≈
2h
ϕi,j,k+1 − ϕi,j,k+1
FZ ≈
2h
box length 32
Here h = 1 is the grid spacing.h = grid size
= 32
= 1.
Once the force is calculated on grid points we will calculate the force on the
particle using some interpolation method like CIC.
22
After calculating the force on the particle we displace it from its position using the
Leapfrog method. The steps for calculating the displaced position and final velocity
are,
F0 ∆t
1. v1/2 = v0 + 2
2. x1 = x0 + v1/2 ∆t
F1 ∆t
3. v1 = v1/2 + 2
Repeat this method at each time step. Calculating third step is not required, and
it introduces additional compute power so we remove the third step in our simulation.
23
2.7 flowchart
24
Chapter 3
25
In the NGP scheme, each particle contributes its entire mass to its nearest grid
point, resulting in a discontinuous and sharply localized distribution in real space.
The corresponding window function in Fourier space takes the form of a sinc function.
Specifically, the Fourier transform of the NGP window function is given by:
WN GP = sinc(k/2)
The discontinuous nature of the NGP (Nearest Grid Point) mass assignment scheme
causes significant aliasing effects in Fourier space (fig 1). Since NGP assigns the entire
mass of a particle to the nearest grid point without any smoothing, the resulting
density field exhibits sharp discontinuities. In Fourier space, such abrupt features
translate to a slowly decaying window function, allowing considerable power to remain
at high-frequency modes. However, due to the finite resolution of the grid, these high-
frequency modes cannot be properly resolved and get aliased, or folded back, into
lower frequencies. This aliasing effect artificially enhances the power at low k-values,
distorting the true large-scale structure of the density field.
Figure 3.1: Left: Figure between k vs P(k). While CIC followed the theoretical curve NGP
did not due to aliasing effect. Right: figure between k vs the ratio of power spectrum between
CIC and NGP
On the other hand, the CIC scheme distributes the mass of each particle over
its nearest neighboring grid points using linear interpolation, resulting in a smoother
density distribution in real space. The corresponding Fourier-space window function
is the square of the NGP window function, given by:
26
This window function leads to less severe aliasing compared to NGP, owing to the
smoother transition between grid cells in real space. Nonetheless, the suppression of
power at large k-values still persists, though it is less sharp than in the NGP case.
27
Figure 3.2: figure between ln r vs ln(Fr )
While all methods deviate from the theoretical curve near the center, TSC per-
forms best, followed by CIC and NGP, indicating reduced numerical error. But as
TSC is computationally expensive we prefer CIC over TSC.
The Layzer-Irvine equation has been establishing itself as one of the most renowned
equations of modern cosmology with its many applications including the determina-
tion of the matter density, cluster mass and size, and the galaxy peculiar velocity field.
In simulations, C and C’ is approximately constant if energy is properly conserved.
28
Figure 3.3: fig between a vs C(layzer irvine constant) for a simulation from a = 1 to a =
2. grid size = 64, ∆a = 0.004
Fig 3 shows the variation of the Layzer-Irvine constant (C) with the scale factor (a)
for CIC and NGP schemes. Ideally, C should remain conserved over time. However,
the NGP scheme shows larger deviations and oscillations due to its poorer force
resolution and particle noise, especially at smaller scales. In contrast, the CIC scheme
provides smoother and more accurate force calculations, leading to better energy
conservation. The difference arises because CIC distributes mass over multiple grid
points, reducing discreteness noise, while NGP assigns mass to a single grid point,
causing higher errors in force and energy estimation.
29
plectic integrators by reducing the sensitivity of equations to the fast-changing scale
factor, especially during early times when a is very small.
Our equation of motion becomes,
dX V
= α+2
dp αa E(a)
dV 3 1
=− α+1
∇ϕ
dp 2α a E(a)
Figure 3.4: in figure α values are 0.1, 0.5, 1, 1.5 in order of upper left, bottom left, upper
right and bottom right
from fig 4. alpha value of 0.5 leads to lower error compared to others. Choosing
α = 2/3 as the integration variable provides the best time-stepping stability for white
noise initial conditions in cosmological simulations. This choice balances the expan-
sion rate and the growth of structures, leading to smoother evolution and reducing
numerical errors during integration, especially in the early stages of the universe.
A
xzi = qi + cos(2πkqz ) (3.1)
2πk
30
viz = x˙zi
a NGP CIC
xrms vrms xrms vrms
k=1
1.6 0.05929 7.07171 0.01927 2.11174
2.2 0.05227 13.02026 0.06647 9.63665
2.8 0.00351 16.10387 0.06832 14.61592
3.4 0.01376 17.09654 0.02235 17.35748
4.0 0.01951 16.45971 0.01702 18.92648
k=2
1.6 0.04625 7.07535 0.00631 1.01552
2.2 0.05147 13.01162 0.03058 6.57157
2.8 0.02131 16.11662 0.04301 12.44312
3.4 0.04444 17.06609 0.02348 16.04670
4.0 0.04049 16.60244 0.02788 18.43541
k=4
1.6 0.03664 7.11097 0.00316 0.50776
2.2 0.04513 12.97750 0.01486 3.71452
2.8 0.03762 16.12799 0.01639 9.87354
3.4 0.02144 17.11787 0.01600 14.08616
4.0 0.01178 16.62001 0.03119 17.25121
Table 3.1: Comparison of xrms and vrms for NGP and CIC at different values of k
and a.
To test our A-body codes against this solution, we have run a series of simulations
using a 163 mesh with one particle per cell. The unperturbed coordinates, qi , were
taken to be the cell centers, and the perturbation was applied in the z-direction. The
initial value of A was chosen to be 0.25, so equation (3.1) should remain valid until
a = 4.0. The results, for three values of kz, are given in Table, where we list the rms
deviations of the particle positions and velocities from the exact solution,
qX
∆xrms = (xi − xzi )2
31
qX
∆vrms = (vi − viz )2
For all values of kz, the NGP scheme leads to much larger deviations from the exact
solution than the CIC scheme. This is because the forces on the particles in the NGP
scheme vary discontinuously with time. For example, for kz = 2, there are no forces
on the particles initially because the particle dis- placements are smaller than half a
mesh cell. For kz = 4, the forces remain zero for the entire duration of the run. The
CIC scheme, in which the forces vary continuously with time, provides a much more
accurate de- scription. Since it does not take much more computing time than the
NGP scheme, it is to be preferred in most practical applications.
This comparison shows that even with a high degree of force resolution, one re-
quires several particles (≥ 4) per wave- length to reduce the errors in the positions,
∆xrms , down to a few percent. To get results of comparable accuracy using the CIC
scheme, one also requires several mesh cells (≥ 8) per wavelength.
kX
max
A ⃗k
xi = q i + cos(2π⃗k.⃗q + ϕk ) (3.2)
kp ,kq ,kr =−kmax
2π |k 2 |
where the ϕk are randomly assigned phases. In these experi- ments we took kmax
D E 21
= 2 and set the initial amplitude of the fluctuations so that δρ
ρ
= 0.22
Figure 5 and 6 shows a two-dimensional projection of the particle positions at a =
3 and at a = 10.0. This figure shows that large-scale structures (sheets and filaments)
are quite well reproduced in the low resolution pancake models. As the resolution is
increased, the dense knots at the intersections of sheets and filaments become more
concentrated and change shape significantly.
The two-point correlation function, ξ(r), is a fundamental statistical tool used to
quantify the clustering properties of particles in cosmological simulations. It measures
32
Figure 3.5: Figure shows the final evolution of particles after a = 3.
33
Figure 3.7: figure is between lnζ vs ln r the blue dots are for 323 and the red one is for 163
34
Figure 3.8: Particle position at center. this figure shows a slice of the potential grid.
35
Figure 3.9: fig for particle at center position.
Where ρ0 is the peak density, x0 is the Center position and σ is the standard
deviation. The analytical potential field is given by,
3
ρ(2π) 2 σ 3
|x − x0 |
ϕ(x) = − erf √
|x − x0 | 2σ
The analytical solution assumes an infinite space, whereas our simulation is con-
fined within periodic boundaries. This mismatch can lead to errors near the bound-
aries. To minimize such effects, we choose ρ0 = 1 and σ = 1, ensuring that the
Gaussian density peak remains narrow and localized away from the boundaries.
We have run the simulation with poisson solver and telegraph equation solver
function and compare it with analytical solution, the result gives,
Both methods give comparable results in terms of L2 error and maximum error,
indicating similar accuracy. However, the Poisson solver is slightly faster in terms of
computation time compared to the Telegraph solver when 1 core is used.
36
Poisson Solver Telegraph Solver(100 iterations)
L2 Error 2.103 2.128
Max Error 0.845 0.893
Time(s) 0.0028 0.0034
The Telegraph solver is slightly slower than the Poisson solver because it is an
iterative method and requires multiple iterations to converge to the solution, whereas
the Poisson solver directly solves the equation using Fourier methods. However, the
Telegraph solver is preferred in scenarios where a time-dependent evolution of the
potential field is required or when solving in non-periodic or more complex boundary
conditions where direct Fourier-based Poisson solvers are not applicable. It also
provides more flexibility in handling dynamic systems and localized interactions.
37
Figure 3.10: Fig shows plot between Total Force against scale factor
38
Figure 3.12: Fig shows Error vs Kappa value
39
Case 1: κ = 0
If κ is chosen to be very large, the system becomes heavily damped. While this
suppresses oscillations, it also slows down the evolution of the potential field, requiring
more iterations for convergence. Moreover, it might not properly capture the transient
behavior of the system.
Optimal Choice of κ
κ ≈ cg × kmax
where cg is the propagation speed used in the telegraph equation, and kmax is the
highest wavenumber that fits in the grid, given by:
π
kmax =
∆x
where ∆x is the grid spacing.
In Our Case
kmax ≈ π
κ∼1−3
The choice of κ in the telegraph solver plays an important role in determining the
accuracy and physical behavior of the solution. Although κ = 0 gives the lowest error,
40
it is not physically realistic as it allows undamped wave propagation, which may lead
to oscillations in a dynamic system. A small non-zero κ introduces necessary damping
that suppresses unphysical oscillations while allowing reasonable convergence speed.
An optimal choice of κ is often taken as κ ∼ cg kmax , where cg is the propagation
speed and kmax is the maximum wavenumber of the system. In our case, with cg = 1
and grid spacing ∆x = 1, we get kmax ≈ π, suggesting κ in the range of 1 to 3
is suitable. This range provides a good balance between accuracy, stability, and
physical relevance, ensuring fast enough convergence without introducing significant
unphysical behavior. Higher κ values improve stability but slow down convergence,
while lower values reduce error but risk instability.
41
Chapter 4
Program
4.1 Introduction
This document describes the methods of the ParticleMeshSimulation class, which
implements a particle-mesh simulation for cosmological structure formation. The
class combines particle dynamics with mesh-based field calculations to simulate the
evolution of matter under gravity.
42
4.2.3 Cosmology Calculations
• hubble(self, a=None): Computes the Hubble parameter at scale factor a
using H(a) = H0 E(a).
√
• E(self, a): Computes the dimensionless Hubble parameter E(a) = Ωm a−3 + Ωk a−2 + ΩΛ .
• power spectrum(self, ks): Computes the matter power spectrum P (k) using
Eisenstein & Hu transfer functions, normalized to σ8 .
43
4.2.6 Force Calculations
• compute forces(self): Computes forces from the potential grid using finite
differences with periodic boundary conditions.
• potential(self): Computes total potential energy from the density and po-
tential grids.
4.2.9 Visualization
• visualize(self, title extra=""): Creates a 2D scatter plot of particle po-
sitions.
4.2.10 Analysis
• compute power spectrum(self, delta x, n bins=20): Computes the power
spectrum of a density field δx by binning Fourier modes.
44
Chapter 5
Conclusion
45
Bibliography
46
[10] Masahiro Hirai, Shin Mineshige, and Kazunari Shibata. “An Improved Poisson
Solver for Self-Gravity Using the Telegraph Equation”. In: Publications of the
Astronomical Society of Japan (2002).
[11] R. W. Hockney and J. W. Eastwood. Computer Simulation Using Particles.
New York: Taylor & Francis, 1981. isbn: 9780852743928.
[12] Roger W. Hockney and James W. Eastwood. Computer Simulation Using Par-
ticles. CRC Press, 1988. isbn: 9780852743928.
[13] R. A. James. “Correcting for the alias effect when measuring the power spec-
trum”. In: Monthly Notices of the Royal Astronomical Society 179 (1977), pp. 15–
18.
[14] Aurobinda Majhi. N-body. https://github.com/aurobinda824/pm. 2025.
[15] Rajesh Mondal. N-body. https : / / github . com / rajeshmondal18 / N - body.
2021.
[16] P.J.E. Peebles. The Large-Scale Structure of the Universe. Princeton University
Press, 1980. isbn: 9780691082400.
[17] S. Prunet et al. “Starting Cosmological Simulations from the Big Bang: Phase-
Space Initial Conditions for N-Body Simulations”. In: The Astrophysical Jour-
nal Supplement Series 178.1 (2008), pp. 179–188.
[18] Hannes R Rüter, Volker Springel, and Rüdiger Pakmor. “An improved Poisson
solver for self-gravity in astrophysical fluid simulations”. In: Monthly Notices of
the Royal Astronomical Society 527.1 (2018), pp. 471–486.
[19] Volker Springel. “The cosmological simulation code GADGET-2”. In: Monthly
Notices of the Royal Astronomical Society 364.4 (2005). https://arxiv.org/
abs/astro-ph/0411105, pp. 1105–1134.
[20] Volker Springel et al. “Galaxy Formation in a ΛCDM Cosmology”. In: Nature
435 (2005), pp. 629–636.
[21] Gustavo Yepes. Introduction to N-body simulations. https://workshops.ift.
uam-csic.es/uploads/charla/275/Yepes_nbody.pdf. 2015.
47