Computational Physics

[ICP]Introduction to Computational Physics


Professor: Andreas Adelmann


1. Random Number Generator

Congruential RNG

xi=(cxi1)mod px_i = (cx_{i-1})mod~p

maximal period is p1p-1, maximal period reach if cp1mod p=1c^{p-1}mod~p = 1

Lagged Fibonacci RNG

xb+1=(jJxb+1j)mod 2j{1,,b}x_{b+1} = (\sum_{j\in\mathcal J}x_{b+1-j}) mod ~ 2\\ j\subset\{1,\cdots, b\}

  • initial sequence at least cc bits
  • usually use congruential RNG to obtain seed sequence

When j=2|j| = 2

xi+1=(xic+xid)mod 2c,d{1,,i1}x_{i+1} = (x_{i-c}+x_{i-d}) mod ~ 2 \\ c,d\in\{1,\cdots, i-1\}

max period: 2c12^c - 1

Zierler-Trinomial condition

1+zc+zd1+z^c+z^d cannot be factorized by in subpolynomials, smallest (c,d)=(250,103)(c,d) = (250,103)

Square/Cubic Test

  • square test:(si,si+1)(s_i, s_{i+1})
  • cubic test: (si,si+1,si+2)(s_i, s_{i+1}, s_{i+2})

χ2\chi^2 test

fluctuation of mean value, mean value should be gaussian

χ2=i=1kNinknk\chi^2 = \sum_{i=1}^k\frac{N_i-\frac{n}{k}}{\frac{n}{k}}

nn : number of samples

NiN_i : count number for each bin

kk : number of bins

Monte Carlo

expected error for MC sampling is O(1N)\mathcal O(\frac{1}{\sqrt N})

error bound in quasi-MC is O((log N)dN)\mathcal O(\frac{(log~N)^d}{N})

D-star Discrepency

DN=max0vj11Ni=1Nj=1d10xjivjj=1dvjD^*_N = \underset{0\le v_j \le 1}{max}\left|\frac{1}{N}\sum_{i=1}^N\prod_{j=1}^d1_{0\le x_j^i \le v_j} - \prod_{j=1}^dv_j\right|

it measure how dense the points distributed inside a given volume

NN : number of dataset points {x1,,xn}\{x^1,\cdots,x^n\}

dd : dimension of the points

low-discrepancy : DNc(d)log(N)dND^*_N \le c(d)\frac{log(N)^d}{N}

Uniform Sphere Shell

given uniform distribution X,YUnif(0,1)X,Y\sim Unif(0,1), get 3d sphere uniform distribution

sphere coordinate

0X0Ydxdy=0Φ0Θ14πsinθdϕdθXY=14π(1cosΘ)Φ\int_0^X\int_0^Y dxdy = \int_0^\Phi\int_0^\Theta \frac{1}{4\pi} sin\theta d\phi d\theta \\ \Rightarrow XY = \frac{1}{4\pi}(1-cos\Theta)\Phi

let Φ=2πX\Phi = 2\pi X then Θ=acos(12Y)\Theta = acos(1-2Y)

S[RsinΘcosΦ,RsinΘsinΦ,RcosΘ]\therefore S\sim [Rsin\Theta cos\Phi, Rsin\Theta sin\Phi, Rcos\Theta]

Uniform Ellipse

  1. rejection sampling, sample XUnif(a,a),YUnif(b,b)X\sim Unif(-a, a), Y\sim Unif(-b,b), accept the points inside ellipse
  2. draw X,YUnif(0,1)X,Y\sim Unif(0,1), ψ=atan(batan(2πX))\psi = atan(\frac{b}{a}tan(2\pi X)), r=Yab(bcosψ)2+(asinψ)2r = \sqrt Y \frac{ab}{\sqrt{(b cos\psi)^2 + (a\sin\psi)^2}}, E[rcosψ,rsinψ]E\sim [rcos\psi, rsin\psi]

Uniform to Gaussian

y1=σ ln(1z2)sin(2πz1)y2=σ ln(1z2)cos(2πz1)y_1 = \sqrt{-\sigma~ln(1-z_2)}sin(2\pi z_1)\\ y_2 = \sqrt{-\sigma~ln(1-z_2)}cos(2\pi z_1)

using uniform z1z_1 and z2z_2 could get two gaussian y1y_1 and y2y_2

2. Percolation

  • cirtical point pcp_c: occupation probabilty pp, a phase transition from non-percolated system to percolated system containing an infinitely-sized cluster

  • percolation strength β\beta: P(ppc)ppcβP(p\gtrsim p_c) \sim |p-p_c|^\beta, how fast the transition

  • wrapping probability : W(p)={00p<pc1pc<p1W(p) = \begin{cases}0 & 0\leq p<p_c \\ 1 & p_c< p\le 1\end{cases}

  • cluster-size distribution: ns(p)=limLNs(p,L)Ln_s(p)=\underset{L\rightarrow \infin}{lim}\frac{N_s(p,L)}{L}, Ns(p,L)N_s(p,L) is the number of cluster of size ss given occupation probability pp and system’s side length LL

Burning Method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def burning_method(lattice):
"""
lattice: np.ndarray[L, L]
0 means empty, 1 means occupied
"""
t = 2
lattice[0, lattice[0, :] == 1] == t
while True:
t += 1
has_changed = False
for node in np.where(lattice == t):
for neighbor in get_neighbors(node):
if lattice[neighbor] == 1:
lattice[neighbor] = t
has_changed = True
at_bottom = (node[0] == lattice.shape[0] - 1).any()
if not has_changed or at_bottom:
break

Hoshen-Kopelman Algorithm

compute how many clusters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def hoshen_kopelman_algorithm(lattice):
"""
lattice: np.ndarray[L, L]
0 means empty, 1 means occupied
"""
cluster_sizes = [None, None] # starts with 2
k = 2
for i in range(lattice.shape[0]):
for j in range(lattice.shape[1]):
if is_top_and_left_empty(lattice[i, j]): # new cluster
lattice[i, j] = k
cluster_sizes.append(1)
k += 1
else is_top_left_same_cluster(lattice[i, j]): # one neighbor in cluster or neighbors in same cluster
lattice[i, j] = get_top_left_cluster(lattice[i,j])
cluster_sizes[lattice[i, j]] += 1
else: # neighbors in different cluster
k1, k2 = get_top_left_cluster(lattice[i, j])
lattice[i, j] = k1
cluster_sizes[k1] += cluster_sizes[k2]
mark_cluster_size_as_transferred(cluster_sizes, k2)

3. Fractals

  • fractal dimension dfd_f: stretch the object by aa, the volume grows adfa^{d_f}

    Vεεd=(Lε)df\frac{V_\varepsilon^*}{\varepsilon^d} = \left(\frac{L}{\varepsilon}\right)^{d_f}

  • correlation function c(r)c(r) : number of filled sites with in sphere rr shell Δr\Delta r normalized by surface

    c(r){C+exp(rξ)p<pcr(d2+η)ppcc(r) \propto \begin{cases} C + exp(-\frac{r}{\xi}) & p<p_c\\ r^{-(d-2 + \eta)} & p\approx p_c \end{cases}

    ξ\xi is the correlation length

    ξppcν where ν={432d0.883d\xi \propto |p-p_c|^\nu~where~\nu=\begin{cases}\frac{4}{3}&2d \\ 0.88 & 3d\end{cases}

    η={5242d0.053d\eta = \begin{cases} \frac{5}{24} & 2d \\ -0.05 & 3d\end{cases}

    df=dβνd_f = d - \frac{\beta}{\nu}

    β\beta : percolation strength

    dd : dimension

Sandbox Method

1
2
3
4
5
6
7
8
9
10
11
12
def sandbox_method(lattice):
"""
lattice: np.ndarray[L, L]
0 means empty, 1 means occupied
"""
R = []
N_R = []
c = lattice.shape[0]/2 - 1 # as python starts from 0
for r in range(1, int(L//2)): # increase the sanbox size over iteration
R.append(r)
N_R.append(sum(lattice[c-r:c+r, c-r:c+r]==1))
plot_log(R, N_R) # the fractal dimension d_f is the slope

Box Counting Method

1
2
3
4
5
6
7
8
9
10
11
12
13
def box_counting_method(lattice):
"""
lattice: np.ndarray[L, L]
0 means empty, 1 means occupied
"""
epsilon_inv = []
N_epsilon = []
for epsilon in range(1, lattice.shape[0]):
boxes = maxpool2d(lattice, kernel=np.ones([epsilon, epsilon]), padding=0) # use the pooling to compute box
N_epsilon.append(sum(boxes > 0))
epsilon_inv.append(1 / epsilon)
plot_log(epsilon_inv, N_epsilon) # the fractal dimension d_f is the slope

4. Cellular Automata

cellular automata:(L,ψ,R,N)(\mathcal L, \psi, R,\mathcal N)

L\mathcal L : dd dimension lattice of cells

ψ\psi : mm dimension boolean state for each site at time tt

RR : mm rules to update the ψ\psi

neighborhoods

Von Neumann and Moore neighborhood

  • left:Von Neumann neighborhood : 4 (north-east-south-west)
  • right:Moore neighborhood : 8 (3x3 region)

boundary conditions

periodic,fixed,adiabtic,reflection boundary

assume x[1:] is the actual space

  • periodic : x[0] = x[-1]
  • fixed : x[0] = C
  • adiabtic : x[0] = x[1]
  • reflection: x[0] = x[2]

Game of Life

Game of Life

moore neighborhood

  • n<2n < 2 : 0 dead because of isolation
  • n=2n = 2 : stay as before
  • n=3n = 3 : 1 birth
  • n>3n>3 : 0 dead because of over population

Langton Ant

Langton Ant strategy

  • enter white cell, turn left and paint cell gray
  • enter gray cell, turn right and paint cell white

Langton Ant

observation

  • chaotic phase of about 10000 steps
  • formation highway
  • walking on highway

Traffic Models

Traffic Models

(ψi1,ψi,ψi+1)t(ψi)t+1(\psi_{i-1},\psi_i,\psi_{i+1})_t \rightarrow (\psi_i)_{t+1}

rule \(ψi1ψiψi+1)t(\psi_{i-1}\psi_{i}\psi_{i+1})_t 111 110 101 100 011 010 001 000
184 (ψi)t(\psi_{i})_t 1 0 1 1 1 0 0 0

Gas of Particles ( HPP model )

HPP model

HPP model strategy

ψ(r,t)=(1011)\psi(r, t) = (1011)

  • collision
  • propagation

5. Monte Carlo Method

Error

Δ1N\Delta \propto \frac{1}{\sqrt N}

π\boldsymbol \pi Buffon’s Needle Experiment

π(N)=4Nc(N)N\pi(N) = 4\frac{N_c(N)}{N}\\

NcN_c : points in the quarter circle

Monte Carlo Integral

abg(x)dxbaNi=1Ng(xi)Q\int_a^b g(x)dx \approx \frac{b-a}{N}\sum_{i=1}^N g(x_i) \equiv Q

xix_i is uniform sampled in [a,b][a, b]

NN is the number of samples

error (Δx)2N2d\propto (\Delta x)^2 \propto N^{-\frac{2}{d}}

Center Limit Theorem

δQ=(ba)σN=VσNσ2=1N1i=1N(g(xi)QN)\delta Q = (b-a) \frac{\sigma}{\sqrt{N}} = V\frac{\sigma}{\sqrt{N}}\\ \sigma^2 = \frac{1}{N-1}\sum_{i=1}^N \left(g(x_i) - \frac{Q}{N}\right)

VV : is the volume of hypercube for integration in high dimension

the error independent of dimension dd

cirtical point

N2d=crit1NN^{-\frac{2}{d}}\overset{crit}{=}\frac{1}{\sqrt{N}}

for d>4d>4, MC more efficient

high dimension integration

hard-sphere, overlap \rightarrow rejective

Importance Sampling

abf(x)dx1Ni=1Nf(xiG)g(xiG)\int_a^b f(x)dx \approx \frac{1}{N}\sum_{i=1}^N \frac{f(x_i^G)}{g(x_i^G)}

xiGx_i^G is sampled according to distribution g(x)g(x)

G(x)G(x) is the cdf of g(x)g(x), G(x)=axg(x)dxG(x) = \int_a^x g(x)dx

ff and gg need to be positively correlated

Control Variates

abf(x)dx=ab(f(x)g(x))dx+abg(x)dx\int_a^b f(x)dx = \int_a^b (f(x)-g(x))dx + \int_a^b g(x)dx

ff and gg need to be positively coorelated

  • Var(fg)<Var(f)Var(f-g) < Var(f)
  • abg(x)dx\int_a^b g(x)dx is known

Quasi Monte Carlo

DN=O((logN)dN)D^*_N = \mathcal O\left(\frac{(log N)^d}{N}\right)

O((logN)dN)<O(1N)N>2d\mathcal O(\frac{(logN)^d}{N}) < \mathcal O(\frac{1}{\sqrt{ N}}) \Rightarrow N > 2^d

Markov Chain

dp(X,τ)dτ=YXp(Y)W(YX)YXp(X)W(XY)W(XY)=T(XY)A(XY)\frac{dp(X,\tau)}{d\tau} = \sum_{Y\neq X} p(Y)W(Y\rightarrow X) - \sum_{Y\neq X} p(X)W(X\rightarrow Y)\\ W(X\rightarrow Y) = T(X\rightarrow Y) A(X\rightarrow Y)

A(XY)A(X\rightarrow Y) means the accpet probability from XX to YY

T(XY)T(X\rightarrow Y) means the transition probablity from XX to YY

  • ergodicity: $\forall X,Y: W(X\rightarrow Y) > 0 $
  • normalization: YW(XY)=1\sum_Y W(X\rightarrow Y) = 1
  • homogeneity: Yp(Y)W(YX)=p(X)\sum_Y p(Y)W(Y\rightarrow X) = p(X)

Detailed Balance : d p(X,τ)dτ=0\frac{d~p(X,\tau)}{d\tau} = 0

M(RT)^2^ Algorithm

  1. random choose a configuration XX
  2. compute ΔE=E(Y)E(X)\Delta E = E(Y) - E(X)
  3. spinflip if ΔE<0\Delta E < 0 else accept iwth probability exp(ΔEkBT)exp(-\frac{\Delta E}{k_BT})

Ising Model

simulate the magnetic properties of a material

H=Ji,jSiSjHiSi\mathcal H = - J \sum_{i,j}S_iS_j - H\sum_i S_i

SiS_i means the spin at position ii

jj : is the neighbor off ii

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def ising_model(lattice, M, E, J, beta, steps):
"""
lattice: np.ndarray[L, L]
1 means spin up, -1 means spin down
M: float
magnetic field
E: float
energy
J: float
coupling constant
beta: float
inverse of temperature `beta = 1 / T / kB`
steps: int
"""
L = lattice.shape[0]
for i in range(steps):
x, y = np.random.randint(0, L, [2])
sigma_j = lattice[get_neighbors(lattice, x, y)].sum()
sigma_i = lattice[x, y]
delta_E = 2 * J * sigma_i * sigma_j
accept = min(1., exp(-beta * delta_E)) > rand()
if accpet:
M -= 2*lattice[x, y]
E += delta_E
lattice[x, y] *= -1
return M, E
  1. random choose a configuration XX
  2. compute ΔE=E(Y)E(X)=2Jσiσj\Delta E = E(Y) - E(X) = 2J\sigma_i\sigma_j
  3. spinflip if ΔE<0\Delta E < 0 else accept with probability exp(ΔEkBT)exp(-\frac{\Delta E}{k_BT})

Multilevel Monte Carlo(MLMC)

E[PL]=E[P0]+l=1LE[PlPl1]\mathbb E[P_L ] = \mathbb E[P_0] + \sum_{l=1}^L \mathbb E [P_l - P_{l-1}]

Nl=μVlClC=l=1LClNlVar=l=1LVlNl1N_l = \mu \sqrt{\frac{V_l}{C_l}}\\ C = \sum_{l=1}^L C_l N_l \\ Var = \sum_{l=1}^L V_l N^{-1}_l

ClC_l : cost at level ll

VlV_l : variance at level ll

NlN_l : sample number at level ll

6. Finite Difference

Error

  • input data error
  • rounding error
  • truncation error
  • simplification in mathematical model
  • human & machine error

propogation

εin(fxi)2εi2\varepsilon \approx \sqrt{\sum_{i}^n \left(\frac{\partial f}{\partial x_i}\right)^2\varepsilon_i^2}

Partial Differential Equation (PDE)

  • parabolic : D2ϕ2xϕt=0D\frac{\partial ^2 \phi}{\partial ^ 2 x} - \frac{\partial \phi}{\partial t} = 0

  • hyperbolic : 2ϕ2x1c2ϕ2t=0\frac{\partial^2 \phi}{\partial ^2 x} - \frac{1}{c}\frac{\partial^2 \phi}{\partial^2 t} = 0

    generate solution is ϕ(x,t)=αf0(xct)+βg0(x+ct)\phi(x,t)=\alpha f_0(x-ct)+\beta g_0(x+ct)

  • elliptic : 2ϕ=0\nabla^2\phi = 0

Lagrange Derivative :

DϕDt=ϕt+uϕ\frac{D\phi}{Dt} = \frac{\partial \phi}{\partial t} + \overrightarrow u \cdot \nabla \phi

Forward in Time, Backward in Space (FTBS)

ϕjn+1t=ϕjn+1ϕjnΔt+O(Δt)ϕjnx=ϕjnϕj1nΔx+O(Δx)\begin{aligned} \frac{\partial \phi^{n+1}_j}{\partial t} &= \frac{\phi_j^{n+1} - \phi_j^{n}}{\Delta t } + \mathcal O(\Delta t ) \\ \frac{\partial \phi^n_j}{\partial x} &= \frac{\phi_j^{n} - \phi_{j-1}^{n}}{\Delta x} + \mathcal O(\Delta x) \end{aligned}

first order accurate

explicit

Centred in Time, Centred in Space (CTCS)

ϕjnt=ϕj(n+1)ϕj(n1)2Δt+O(Δt2)ϕjnx=ϕj+1nϕj1n2Δx+O(Δx2)\begin{aligned} \frac{\partial \phi_j^n}{\partial t } &= \frac{\phi_j^{(n+1)}-\phi_j^{(n-1)}}{2\Delta t} + \mathcal O(\Delta t^2)\\ \frac{\partial \phi_j^n}{\partial x} &= \frac{\phi_{j+1}^n - \phi_{j-1}^n}{2\Delta x} + \mathcal O(\Delta x^2) \end{aligned}

second order accurate

implicit

Backward in Time, Centred in Space (BTCS)

ϕjnt=ϕjn+1ϕjnΔt+O(Δt)ϕjnx=ϕj+1nϕj1nΔx+O(Δx)\frac{\partial \phi_j^n}{\partial t} = \frac{\phi_j^{n+1}-\phi_j^n}{\Delta t}+ \mathcal O(\Delta t)\\ \frac{\partial \phi_j^n}{\partial x} = \frac{\phi_{j+1}^n - \phi_{j-1}^n}{\Delta x } + \mathcal O(\Delta x)\\

first order accurate

implicit

Stability

Dissipation and Dispersion

  • Dissipation : smooth out sharp corners, gradients, discontinuities
  • Dispersion : dependence of wave speed on wavelength

Lax-Equivalence Theorem

consistency + stability \Leftrightarrow convergence

Courant-Friedrichs-Lewy(CFL) cirterion

C=uΔtΔxCmaxC = \frac{u\Delta t}{\Delta x} \le C_{max}

for explicit, Cmax=1C_{max} = 1, much larger for implicit, as it’s much more stable

Domain of Dependence(DoD)

  • DoD for FTBS 0c10\le c \le 1
  • DoD for CTCS 1c1-1 \le c \le 1

Von-Neumann Stability Analysis

ϕn+1=Aϕn\phi^{n+1} = A\phi^n

  • A2<1|A|^2 < 1 stable and damping
  • A2=1|A|^2 = 1 neutral stable
  • A2>1|A|^2 > 1 unstable and amplyfying

for FTBS

ϕjn+1=ϕjnc(ϕjnϕj1n)An+1eikjΔx=AneekjΔxcAn(eikjΔxeik(j1)Δx)A=1c(1eikΔx)A2=12c(1c)(1coskΔx)\begin{aligned} \phi^{n+1}_j &= \phi^n_j - c(\phi^n_j -\phi^n_{j-1}) \\ A^{n+1} e^{ikj\Delta x} &= A^n e^{ekj\Delta x} - cA^n\left(e^{ikj\Delta x}-e^{ik(j-1)\Delta x}\right) \\ A &= 1 - c(1-e^{-ik\Delta x}) \\ |A|^2 &= 1 - 2c(1-c)(1-cos k \Delta x) \end{aligned}

c=uΔtΔxc = \frac{u\Delta t}{\Delta x}

if $u < 0 $ or uΔtΔx>1\frac{u\Delta t}{\Delta x} > 1 the FTBS is unstable , which is 0c10\le c\le 1

for FTCS

A2=1+4c2sin2(kΔx)|A|^2 = 1 + 4c^2 sin^2(k\Delta x)

for CTCS

ϕjn+1=ϕjn1c(ϕj+1nϕj1n)A=icsin(kΔx)±1c2sin2kΔxA2=2c2sin2(kΔx)1sin(kΔx)c2sin2(kΔx)1\begin{aligned} \phi_j^{n+1} &= \phi_j^{n-1}- c(\phi_{j+1}^n - \phi_{j-1}^n) \\ A & = -ic sin(k\Delta x)\pm \sqrt{1 - c^2 sin^2 k\Delta x}\\ |A|^2 &= 2c^2sin^2(k\Delta x) - 1 \mp sin(k\Delta x)\sqrt{c^2sin^2(k\Delta x) - 1} \end{aligned}

  • c>1|c| > 1 unstable
  • c1|c| \le 1 stable

there are two solutions ,should ignore the spurious solution

Conservation

Mn+1=01ϕxn+1dx=Mn=01ϕxndx\begin{aligned} M^{n+1} &= \int_0^1 \phi^{n+1}_x dx \\ = M^{n} &= \int_0^1 \phi^n_xdx \end{aligned}

Phase Velocity

ϕ(x,t)=ϕ(xut,0)\phi(x,t )= \phi(x-ut,0)

uu: phase speed

for CTCS, small kk and Δx\Delta x is correct

Shallow Water Equation

H=H0+ηH = H_0 + \eta

where HH is the water height, η\eta is the fluctuation

ut+(u)u=1ρp+gu=0\frac{\partial u}{\partial t} + (u \cdot \nabla)u = -\frac{1}{\rho}\nabla p + g\\ \nabla \cdot u = 0

uit+ujuixj=1ρpxi+giuixi=0\frac{\partial u_i}{\partial t} + u_j\frac{\partial u_i}{\partial x_j} = -\frac{1}{\rho} \frac{\partial p}{\partial x_i} + g_i \\ \frac{\partial u_i}{\partial x_i} = 0

where g=[0,0,gz]g = [0, 0, g_z] is the velocity

A-Grid(unstaggered)

ηjnηjn1Δt=H0uj+1nuj1nΔxujn+1ujnΔt=gηj+1nηj1nΔx\frac{\eta^n_j - \eta^{n-1}_j}{\Delta t} = - H_0 \frac{u^n_{j+1} - u^n_{j-1}}{\Delta x} \\ \frac{u^{n+1}_j - u^n_j}{\Delta t} = -g \frac{\eta^n_{j+1} - \eta^n_{j-1}}{\Delta x}

courant number c=gH0ΔtΔxc = \sqrt{gH_0}\frac{\Delta t}{\Delta x}

stable for c2c\le 2

**C-Grid(Staggered) **

ηjnηjn1Δt=H0uj+12nuj1nΔxuj+12n+1uj+12nΔt=gηj+1nηj1nΔx\frac{\eta_j^n- \eta^{n-1}_j}{\Delta t} = - H_0\frac{u^n_{j+\frac{1}{2}}-u^n_{j-1}}{\Delta x} \\ \frac{u^{n+1}_{j+\frac{1}{2}} - u^n_{j+\frac{1}{2}}}{\Delta t} = - g \frac{\eta^n_{j+1} - \eta^n_{j-1}}{\Delta x}

stable for c1c\le 1

7. Time Integration

error

  • truncation error : taylor expansion in euler method, O(Δt2)\mathcal O(\Delta t^2) for each step, O(Δt)\mathcal O(\Delta t) in total
  • round-off error : charcteristic number η\eta , the smallest incremental, in euler method O(η)\mathcal O(\eta) for each step, O(ηΔt)\mathcal O(\frac{\eta}{\Delta t}) in total
  • total error : for euler method, εηΔt+Δt\varepsilon \sim \frac{\eta}{\Delta t} + \Delta t

one step method

for ordinary differential equatiion (ODE)

ϕt=f(t,ϕ(t))ϕ(t0)=ϕ0\frac{\partial \phi}{\partial t} = f(t, \phi(t))\quad \phi(t_0) = \phi^0

ϕn+1ϕnΔt=γf(t+Δt,ϕn+1)+(1γ)f(t,ϕn)\frac{\phi^{n+1}-\phi ^n}{\Delta t} = \gamma f(t+\Delta t, \phi^{n+1}) + (1-\gamma)f(t,\phi^n)

multi step method

(1+β)ϕn+1(1+2β)ϕn+βϕn1Δt=γf(t+Δt,ϕn+1)+(1γ+α)f(t,ϕn)αf(tΔt,ϕn1)\frac{(1+\beta)\phi^{n+1}-(1+2\beta)\phi^n + \beta\phi^{n-1}}{\Delta t} = \gamma f(t+\Delta t, \phi^{n+1}) + (1-\gamma + \alpha) f(t, \phi^n) - \alpha f(t-\Delta t, \phi^{n-1})

name α\alpha β\beta γ\gamma order
explicit euler 0 0 0 1
implicit euer 0 0 1 1
leapfrog 0 12-\frac{1}{2} 0 2

Runge-Kutta method

k1=Δtf(tn,ϕn)k2=Δtf(tn+12,ϕn+k12)k3=Δtf(tn+12,ϕn+k22)k4=Δtf(tn+1,ϕn+k3)ϕn+1=ϕn+k16+k23+k33+k46+O(Δt5)\begin{aligned} k_1 &= \Delta tf(t_n, \phi^n) \\ k_2 &= \Delta t f(t_{n+\frac{1}{2}}, \phi^n+\frac{k_1}{2})\\ k_3 &= \Delta t f(t_{n+\frac{1}{2}}, \phi^n+\frac{k_2}{2})\\ k_4 &= \Delta t f(t_{n+1}, \phi^n+k_3)\\ \phi^{n+1} &= \phi^n + \frac{k_1}{6} + \frac{k_2}{3} + \frac{k_3}{3} + \frac{k_4}{6} + \mathcal O(\Delta t^5) \end{aligned}

truncation error O(Δt4)\mathcal O(\Delta t^4)

round-off error O(ηΔt)\mathcal O(\frac{\eta}{\Delta t})

minimum error is smaller with bigger Δt\Delta t compared to euler

truncation and round-off error

Conservation

phase space conservation

volume \leftrightarrow energy

  • left: conserve
  • middle: loss energy
  • right: gain energy

Symplectic

for Hamiltonian [p,q][p, q] and p=q˙p = \dot q

[q(τ)p(τ)]=A[q(0)p(0)]\left[\begin{matrix} q(\tau)\\ p(\tau) \end{matrix}\right] = A \left[\begin{matrix} q(0)\\ p(0) \end{matrix}\right]

for energy conservation A=1|A| = 1

8. Maxwell Equation

Valsov-Maxwell-Bolzmann equation

computational plasma

fst+x(vfs)+v((E+v×B)qsfsms)=(fst)cE=ρε0H=0×E+Ht=0×Hμ0ε0Et=μ0sqsvfsdv3\begin{aligned} \frac{\partial f_s}{\partial t} + \nabla_x \cdot(\boldsymbol vf_s) + \nabla_{\boldsymbol v}\cdot ((\boldsymbol E + \boldsymbol v \times \boldsymbol B)\frac{q_sf_s}{m_s}) &= (\frac{\partial f_s}{\partial t})_c \\ \nabla \cdot \boldsymbol E &= \frac{\rho}{\varepsilon_0}\\ \nabla \cdot \boldsymbol H &= 0 \\ \nabla \times \boldsymbol E + \frac{\partial \boldsymbol H}{\partial t} &= 0\\ \nabla \times \boldsymbol H - \mu_0 \varepsilon_0 \frac{\partial \boldsymbol E}{\partial t} &= \mu_0\sum_s q_s \int_{-\infin}^{\infin} \boldsymbol vf_s d\boldsymbol v^3 \end{aligned}

where f(x,v,t)R3×3×f(x, \boldsymbol v, t)\in \mathbb R^{3\times 3\times} is the distribution

D=ε0εrEB=μ0μrH\begin{aligned} \boldsymbol D &= \varepsilon_0\varepsilon_r \boldsymbol E\\ \boldsymbol B &= \mu_0 \mu_r \boldsymbol H \end{aligned}

Particle In Cell Method (PIC)

dxdt=vdvdt=qm(E(x,t),v×B(x,t))\begin{aligned} \frac{d\boldsymbol x}{dt} &= \boldsymbol v\\ \frac{d\boldsymbol v}{dt} &= \frac{q}{m}(\boldsymbol E(\boldsymbol x,t), \boldsymbol v\times \boldsymbol B(\boldsymbol x, t)) \end{aligned}

PIC method

Boris algorithm

v=vn12+qmEnΔt2v+vΔt=q2m(v++v)×Bnvn+12=v++qmEnΔt2\begin{aligned} \boldsymbol v^- &= \boldsymbol v^{n-\frac{1}{2}} + \frac{q}{m} \boldsymbol E^n\frac{\Delta t}{2} \\ \frac{\boldsymbol v^+ - \boldsymbol v^-}{\Delta t} &= \frac{q}{2m}(\boldsymbol v^+ + \boldsymbol v^-) \times \boldsymbol B^n\\ \boldsymbol v^{n+\frac{1}{2}} &= \boldsymbol v^+ + \frac{q}{m}\boldsymbol E^n\frac{\Delta t}{2} \end{aligned}

without E\boldsymbol E

tqBΔt2ms=2t1+t2v=v+v×tv+=v+v×s\begin{aligned} \boldsymbol t &\approx \frac{q\boldsymbol B \Delta t}{2m}\\ \boldsymbol s &= \frac{2\boldsymbol t}{1 + |\boldsymbol t|^2}\\ \boldsymbol v' &= \boldsymbol v^- + \boldsymbol v^- \times \boldsymbol t\\ \boldsymbol v^+ &= \boldsymbol v^- + \boldsymbol v' \times \boldsymbol s \end{aligned}

Yee Cell

Yee-Cell method

Ht=1μ0×EEt=1ε0×H\begin{aligned} \frac{\partial \boldsymbol H}{\partial t} & = -\frac{1}{\mu_0} \nabla \times \boldsymbol E\\ \frac{\partial \boldsymbol E}{\partial t} &= \frac{1}{\varepsilon_0} \nabla \times \boldsymbol H \end{aligned}

Hxt=1μ0(EyzEzy)Ext=1ε0(EyzEzy)Hyt=1μ0(EzxExz)Eyt=1ε0(EzxExz)Hzt=1μ0(ExyEyx)Ezt=1ε0(ExyEyx)\begin{aligned} \frac{\partial \boldsymbol H_x}{\partial t} &= \frac{-1}{\mu_0} \left(\frac{\partial \boldsymbol E_y}{\partial z} - \frac{\partial \boldsymbol E_z}{\partial y}\right) & \frac{\partial \boldsymbol E_x}{\partial t} &= \frac{1}{\varepsilon_0}\left(\frac{\partial \boldsymbol E_y}{\partial z} - \frac{\partial \boldsymbol E_z}{\partial y}\right) \\ \frac{\partial \boldsymbol H_y}{\partial t} &= \frac{-1}{\mu_0}\left(\frac{\partial \boldsymbol E_z}{\partial x} -\frac{\partial \boldsymbol E_x}{\partial z}\right) & \frac{\partial \boldsymbol E_y}{\partial t} &=\frac{1}{\varepsilon_0}\left(\frac{\partial \boldsymbol E_z}{\partial x} - \frac{\partial \boldsymbol E_x}{\partial z}\right) \\ \frac{\partial \boldsymbol H_z}{\partial t} &=\frac{-1}{\mu_0}\left(\frac{\partial \boldsymbol E_x}{\partial y} - \frac{\partial \boldsymbol E_y}{\partial x}\right) & \frac{\partial \boldsymbol E_z}{\partial t} & = \frac{1}{\varepsilon_0}\left(\frac{\partial \boldsymbol E_x}{\partial y}-\frac{\partial \boldsymbol E_y}{\partial x}\right) \end{aligned}

Exkn+12Exkn12Δt=1ε0Hyk+12nHyk12nΔzHyk+12n+1Hyk+12nΔt=1μ0Exk+1n+12Exkn+12Δz\frac{E_{x_k}^{n+\frac{1}{2}}-E_{x_k}^{n-\frac{1}{2}}}{\Delta t} = -\frac{1}{\varepsilon_0}\frac{H_{y_{k+\frac{1}{2}}}^n - H_{y_{k-\frac{1}{2}}}^n}{\Delta z} \\ \frac{H_{y_{k+\frac{1}{2}}}^{n+1}-H_{y_{k+\frac{1}{2}}}^n}{\Delta t} = -\frac{1}{\mu_0}\frac{E_{x_{k+1}}^{n+\frac{1}{2}}-E_{x_k}^{n+\frac{1}{2}}}{\Delta z}

error minimized by E~x=ε0μ0Ex\tilde E_{x} = \sqrt{\frac{\varepsilon_0}{\mu_0}}E_x

stability: ΔtΔx=1dc\frac{\Delta t}{\Delta x} = \frac{1}{\sqrt d c}

9. N-Body Problems

Particle in Cell Method (PIC)

PIC method

Δϕ=ρF=ϕ-\Delta \phi = \rho\\ F = -\nabla \phi

ρ\rho is the mass density, ϕ\phi is the potential field

ϕ(x)=G(x,x)ρ(x)\phi(x) = G(x,x') * \rho(x')

  1. generate initial condition f0,F0f_0, F^0

  2. for loop

    1. force field interpolate from grid to particles

    2. push particles (x,v)kn(x,v)kn+1(x,v)^n_k\rightarrow (x,v)^{n+1}_k

    3. density field interpolation to grid (x,v)k(ρ,J)j(x,v)_k \rightarrow (\rho, J)_j

    4. force field calculation FknFkn+1F_k^n\rightarrow F_k^{n+1}, using FFT

      ϕ(x)=ρ(x)G(x,x)dx=F1(F(ρ)F(G))\phi(x) = \int \rho(x') G(x,x')dx' = \mathcal F^{-1}(\mathcal F(\rho) \cdot \mathcal F(G))

Particle particle Particle Mesh(P3M)

G(r)=1erf(αr)rGpp+erf(αr)rGpmG(r) = \underbrace{\frac{1-erf(\alpha r)}{r}}_{G_{pp}} + \underbrace{\frac{erf(\alpha r)}{r}}_{G_{pm}}

GppG_{pp} : use the n-body solver for short-range

GpmG_{pm} : use particle-mesh solver for long-range