NBodySimulator.jl

Simulating systems of N interacting bodies.

This project is under development at the moment. The implementation of potential calculations is fairly experimental and has not been extensively verified yet. You can test simulation of different systems now, but be aware of possible changes in the future.

Basic Components

There are three basic components required for any simulation of systems of N-bodies: bodies, system, and simulation.

Bodies or Particles are the objects which will interact with each other and for which the equations of Newton's 2nd law are solved during the simulation process. Three parameters of a body are necessary, namely: initial location, initial velocity, and mass. MassBody structure represents such particles:

using StaticArrays
r = SVector(0.0, 0.0, 0.0)
v = SVector(0.1, 0.2, 0.5)
mass = 1.25
body = MassBody(r, v, mass)

For simulation speed, it is advised to use static arrays.

A System covers bodies and necessary parameters for correct simulation of interaction between particles. For example, to create an entity for a system of gravitationally interacting particles, one needs to use GravitationalSystem constructor:

const G = 6.67e-11 # m^3/kg/s^2
system = GravitationalSystem(bodies, G)

Simulation is an entity determining parameters of the experiment: time span of simulation, global physical constants, borders of the simulation cell, external magnetic or electric fields, etc. The required arguments for NBodySImulation constructor are the system to be tested and the time span of the simulation.

tspan = (0.0, 10.0)
simulation = NBodySimulation(system, tspan)

There are different types of bodies, but they are just containers of particle parameters. The interaction and acceleration of particles are defined by the potentials or force fields.

Generating bodies

The package exports quite a useful function for placing similar particles in the nodes of a cubic cell, with their velocities distributed in accordance with the Maxwell–Boltzmann law:

N = 100 # number of bodies/particles
m = 1.0 # mass of each of them
v = 10.0 # mean velocity
L = 21.0 # size of the cell side

bodies = generate_bodies_in_cell_nodes(N, m, v, L)

Molecules for the SPC/Fw water model can be imported from a PDB file:

molecules = load_water_molecules_from_pdb("path_to_pdb_file.pdb")

Potentials

The potentials or force field determines the interaction of particles and, therefore, their acceleration.

There are several structures for basic physical interactions:

g_parameters = GravitationalParameters(G)
m_parameters = MagnetostaticParameters(μ_4π)
el_potential = ElectrostaticParameters(k, cutoff_radius)
jl_parameters = LennardJonesParameters(ϵ, σ, cutoff_radius)
spc_water_paramters = SPCFwParameters(rOH, ∠HOH, k_bond, k_angle)

The Lennard-Jones potential is used in molecular dynamics simulations for approximating interactions between neutral atoms or molecules. The SPC/Fw water model is used in water simulations. The meaning of arguments for SPCFwParameters constructor will be clarified further in this documentation.

PotentialNBodySystem structure represents systems with a custom set of potentials. In other words, the user determines the ways in which the particles are allowed to interact. One can pass the bodies and parameters of interaction potentials into that system. In the case the potential parameters are not set, the particles will move at constant velocities without acceleration during the simulation.

system = PotentialNBodySystem(bodies,
                              Dict(:gravitational => g_parameters,
                                   :electrostatic => el_potential))

Custom Potential

There exists an example of a simulation of an N-body system at absolutely custom potential.

Here is shown how to create custom acceleration functions using tools of NBodySimulator.

First, it is necessary to create a structure for parameters for the custom potential.

struct CustomPotentialParameters <: PotentialParameters
    a::AbstractFloat
end

Next, the acceleration function for the potential is required. The custom potential defined here creates a force acting on all the particles, proportionate to their masses. The first argument of the function determines the potential for which the acceleration should be calculated in this method.

import NBodySimulator.get_accelerating_function
function get_accelerating_function(p::CustomPotentialParameters,
                                   simulation::NBodySimulation)
    ms = get_masses(simulation.system)
    (dv, u, v, t, i) -> begin
        custom_accel = SVector(0.0, 0.0, p.a)
        dv .= custom_accel * ms[i]
    end
end

After the parameters and acceleration function have been created, one can instantiate a system of particles interacting with a set of potentials which includes the just-created custom potential:

parameters = CustomPotentialParameters(-9.8)
system = PotentialNBodySystem(bodies, Dict(:custom_potential_params => parameters))

Gravitational Interaction

Using NBodySimulator, it is possible to simulate gravitational interaction of celestial bodies. In fact, any structure for bodies can be used for simulation of gravitational interaction since all those structures are required to have mass as one of their parameters:

body1 = MassBody(SVector(0.0, 1.0, 0.0), SVector(5.775e-6, 0.0, 0.0), 2.0)
body2 = MassBody(SVector(0.0, -1.0, 0.0), SVector(-5.775e-6, 0.0, 0.0), 2.0)

When solving a gravitational problem, one needs to specify the gravitational constant G.

G = 6.673e-11

Now we have enough parameters to create a GravitationalSystem object:

system = GravitationalSystem([body1, body2], G)

Usually, we solve an N-body problem for a certain period of time:

tspan = (0.0, 1111150.0)

The created objects determine the simulation we want to run:

simulation = NBodySimulation(system, tspan)
sim_result = run_simulation(simulation)

And, finally, we can animate our solution showing two equal bodies rotating on the same orbit:

using Plots
animate(sim_result, "path_to_animated_particles.gif")

<img src="https://user-images.githubusercontent.com/16945627/39958539-d2cf779c-561d-11e8-96a8-ffc3a595be8b.gif" alt="Here should appear a gif of rotating bodies" width="350"/>

Electrostatic Interaction

Interaction between charged particles obeys Coulomb's law. The movement of such bodies can be simulated using ChargedParticle and ChargedParticles structures. The following example shows how to model two oppositely charged particles. If one body is more massive than another, it will be possible to observe rotation of the light body around the heavy one without adjusting their positions in space. The constructor for the ChargedParticles system requires bodies and Coulomb's constant k to be passed as arguments.

r = 100.0 # m
q1 = 1e-3 # C
q2 = -1e-3 # C
m1 = 100.0 # kg
m2 = 0.1 # kg
v2 = sqrt(abs(k * q1 * q2 / m2 / r)) # m/s - using the centrifugal acceleration
t = 2 * pi * r / v2 # s  - for one rotation
p1 = ChargedParticle(SVector(0.0, 0.0, 0.0), SVector(0.0, 0, 0.0), m1, q1)
p2 = ChargedParticle(SVector(100.0, 0.0, 0.0), SVector(0.0, v2, 0.0), m2, q2)
system = ChargedParticles([p1, p2], k)
simulation = NBodySimulation(system, (0.0, t))
sim_result = run_simulation(simulation)

Magnetic Interaction

An N-body system consisting of MagneticParticles can be used for simulation of interacting magnetic dipoles, though such dipoles cannot rotate in space. Such a model can represent single domain particles interacting under the influence of a strong external magnetic field. To create a magnetic particle, one specifies its location in space, velocity, and the vector of its magnetic moment. The following code shows how we can construct an iron particle:

iron_dencity = 7800 # kg/m^3
magnetization_saturation = 1.2e6 # A/m
mass = 5e-6 # kg
r = SVector(-0.005, 0.0, 0.0) # m
v = SVector(0.0, 0.0, 0.0) # m/s
magnetic_moment = SVector(0.0, 0.0, magnetization_saturation * mass / iron_dencity) # A*m^2
p1 = MagneticParticle(r, v, mass, magnetic_moment)

For the second particle, we will use a shorter form:

p2 = MagneticParticle(SVector(0.005, 0.0, 0.0), SVector(0.0, 0.0, 0.0), 5e-6,
                      SVector(0.0, 0.0, 0.00077))

To calculate magnetic interactions properly, one should also specify the value for the constant μ<sub>0</sub>/4π or its substitute. Having created parameters for the magnetostatic potential, one can now instantiate a system of particles which should interact magnetically. For that purpose, we use PotentialNBodySystem and pass particles and potential parameters as arguments.

parameters = MagnetostaticParameters(μ_4π)
system = PotentialNBodySystem([p1, p2], Dict(:magnetic => parameters))
simulation = NBodySimulation(system, (t1, t2))
sim_result = run_simulation(simulation, VelocityVerlet(), dt = τ)

Molecular Dynamics (MD)

NBodySimulator allows one to conduct molecular dynamic simulations for the Lennard-Jones liquids, SPC/Fw model of water, and other molecular systems thanks to implementations of basic interaction potentials between atoms and molecules:

  • Lennard-Jones
  • electrostatic and magnetostatic
  • harmonic bonds
  • harmonic valence angle generated by pairs of bonds

The comprehensive examples of liquid argon and water simulations can be found in the examples folder. Here only the basic principles of the molecular dynamics simulations using NBodySimulator are presented using liquid argon as a classical MD system for beginners. First, one needs to define parameters of the simulation:

T = 120.0 # °K
T0 = 90.0 # °K
kb = 8.3144598e-3 # kJ/(K*mol)
ϵ = T * kb
σ = 0.34 # nm
ρ = 1374 / 1.6747# Da/nm^3
m = 39.95# Da
N = 216
L = (m * N / ρ)^(1 / 3)#10.229σ
R = 0.5 * L
v_dev = sqrt(kb * T / m)
bodies = generate_bodies_in_cell_nodes(N, m, v_dev, L)
τ = 0.5e-3 # ps or 1e-12 s
t1 = 0.0
t2 = 2000τ

Liquid argon consists of neutral molecules, so the Lennard-Jones potential runs their interaction:

parameters = LennardJonesParameters(ϵ, σ, R)
lj_system = PotentialNBodySystem(bodies, Dict(:lennard_jones => parameters));

Then, a thermostat and boundary conditions should be selected and instantiated:

thermostat = NoseHooverThermostat(T0, 200τ)
pbc = CubicPeriodicBoundaryConditions(L)
simulation = NBodySimulation(lj_system, (t1, t2), pbc, thermostat, kb);
result = run_simulation(simulation, VelocityVerlet(), dt = τ)

It is recommended to use CubicPeriodicBoundaryConditions since cubic boxes are among the most popular boundary conditions in MD. There are different variants of the NBodySimulation constructor for MD:

simulation = NBodySimulation(lj_system, (t1, t2));
simulation = NBodySimulation(lj_system, (t1, t2), pbc);
simulation = NBodySimulation(lj_system, (t1, t2), pbc, thermostat);
simulation = NBodySimulation(lj_system, (t1, t2), pbc, thermostat, kb);

The default boundary conditions are InfiniteBox without any limits, the default thermostat is NullThermostat (which does no thermostating), and the default Boltzmann constant kb equals its value in SI, i.e., 1.38e-23 J/K.

Water Simulations

In NBodySImulator the SPC/Fw water model is implemented. For using this model, one has to specify parameters of the Lennard-Jones potential between the oxygen atoms of water molecules, parameters of the electrostatic potential for the corresponding interactions between atoms of different molecules and parameters for harmonic potentials representing bonds between atoms and the valence angle made from bonds between hydrogen atoms and the oxygen atom.

bodies = generate_bodies_in_cell_nodes(N, mH2O, v, L)
jl_parameters = LennardJonesParameters(ϵOO, σOO, R)
e_parameters = ElectrostaticParameters(k, Rel)
spc_paramters = SPCFwParameters(rOH, ∠HOH, k_bond, k_angle)
water = WaterSPCFw(bodies, mH, mO, qH, qO, jl_parameters, e_parameters, spc_paramters);

For each water molecule here, rOH is the equilibrium distance between a hydrogen atom and the oxygen atom, ∠HOH denotes the equilibrium angle made of those two bonds, k_bond and k_angle are the elastic coefficients for the corresponding harmonic potentials. Further, one can pass the water system into the NBodySimulation constructor as a usual system of N-bodies.

simulation = NBodySimulation(water, (t1, t2), pbc, kb);

Thermostats

Usually, during the simulation, a system is required to be at a particular temperature. NBodySimulator contains several thermostats for that purpose. Here the thermostating of liquid argon is presented, for thermostating of water one can refer to this post.

Andersen Thermostat

τ = 0.5e-3 # timestep of integration and simulation
T0 = 90
ν = 0.05 / τ
thermostat = AndersenThermostat(90, ν)

andersen thermostating

Berendsen Thermostat

τB = 2000τ
thermostat = BerendsenThermostat(90, τB)

berendsen thermostating

Nosé–Hoover Thermostat

τNH = 200τ
thermostat = NoseHooverThermostat(T0, 200τ)

nose-hoover thermostating

Langevin Thermostat

γ = 10.0
thermostat = LangevinThermostat(90, γ)

langevin thermostating

Analyzing the Result of Simulation

Once the simulation is completed, one can analyze the result and obtain some useful characteristics of the system. The function run_simulation returns a structure containing the initial parameters of the simulation and the solution of the differential equation (DE) required for the description of the corresponding system of particles. There are different functions which help to interpret the solution of DEs into physical quantities. One of the main characteristics of a system during molecular dynamics simulations is its thermodynamic temperature. The value of the temperature at a particular time t can be obtained via calling this function:

T = temperature(result, t)

Radial distribution functions

The RDF is another popular and essential characteristic of molecules or similar systems of particles. It shows the reciprocal location of particles averaged by the time of simulation.

(rs, grf) = rdf(result)

The dependence of grf on rs shows radial distribution of particles at different distances from an average particle in a system. Here, the radial distribution function for the classic system of liquid argon is presented: rdf for liquid argon

Mean Squared Displacement (MSD)

The MSD characteristic can be used to estimate the shift of particles from their initial positions.

(ts, dr2) = msd(result)

For a standard liquid argon system, the displacement grows with time: rdf for liquid argon

Energy Functions

Energy is a highly important physical characteristic of a system. The module provides four functions to obtain it, though the total_energy function just sums potential and kinetic energy:

e_init = initial_energy(simulation)
e_kin = kinetic_energy(result, t)
e_pot = potential_energy(result, t)
e_tot = total_energy(result, t)

Plotting Images

Using tools of NBodySimulator, one can export the results of a simulation into a Protein Database File. VMD is a well-known tool for visualizing molecular dynamics, which can read data from PDB files.

save_to_pdb(result, "path_to_a_new_pdb_file.pdb")

In the future, it will be possible to export results via FileIO interface and its save function. Using Plots.jl, one can draw positions of particles at any time of simulation or create an animation of moving particles, molecules of water:

using Plots
plot(result)
animate(result, "path_to_file.gif")

Makie.jl also has a recipe for plotting the results of N-body simulations. The example is presented in the documentation.

Reproducibility

The documentation of this SciML package was built using these direct dependencies,
Status `~/work/NBodySimulator.jl/NBodySimulator.jl/docs/Project.toml`
  [e30172f5] Documenter v0.27.24
  [0e6f8da7] NBodySimulator v1.10.0 `~/work/NBodySimulator.jl/NBodySimulator.jl`
and using this machine and Julia version.
Julia Version 1.8.5
Commit 17cfb8e65ea (2023-01-08 06:45 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 2 × Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, skylake-avx512)
  Threads: 1 on 2 virtual cores
A more complete overview of all dependencies and their versions is also provided.
Status `~/work/NBodySimulator.jl/NBodySimulator.jl/docs/Manifest.toml`
  [a4c015fc] ANSIColoredPrinters v0.0.1
  [79e6a3ab] Adapt v3.6.1
  [ec485272] ArnoldiMethod v0.2.0
  [4fba245c] ArrayInterface v7.2.1
  [30b0a656] ArrayInterfaceCore v0.1.29
  [62783981] BitTwiddlingConvenienceFunctions v0.1.5
  [2a0fbf3d] CPUSummary v0.2.2
  [49dc2e85] Calculus v0.5.1
  [d360d2e6] ChainRulesCore v1.15.7
  [9e997f8a] ChangesOfVariables v0.1.6
  [fb6a15b2] CloseOpenIntervals v0.1.12
  [38540f10] CommonSolve v0.2.3
  [bbf7d656] CommonSubexpressions v0.3.0
  [34da2185] Compat v4.6.1
  [187b0558] ConstructionBase v1.5.1
  [adafc99b] CpuId v0.3.1
  [9a962f9c] DataAPI v1.14.0
  [864edb3b] DataStructures v0.18.13
  [e2d170a0] DataValueInterfaces v1.0.0
  [b429d917] DensityInterface v0.4.0
  [2b5f629d] DiffEqBase v6.122.1
  [459566f4] DiffEqCallbacks v2.26.0
  [163ba53b] DiffResults v1.1.0
  [b552c78f] DiffRules v1.13.0
  [b4f34e82] Distances v0.10.8
  [31c24e10] Distributions v0.25.86
  [ffbed154] DocStringExtensions v0.9.3
  [e30172f5] Documenter v0.27.24
  [fa6b7ba4] DualNumbers v0.6.8
  [4e289a0a] EnumX v1.0.4
  [d4d017d3] ExponentialUtilities v1.24.0
  [e2ba6199] ExprTools v0.1.9
  [7034ab61] FastBroadcast v0.2.5
  [9aa1b823] FastClosures v0.3.2
  [29a986be] FastLapackInterface v1.2.9
  [5789e2e9] FileIO v1.16.0
  [1a297f60] FillArrays v0.13.8
  [6a86dc24] FiniteDiff v2.18.0
  [f6369f11] ForwardDiff v0.10.35
  [069b7b12] FunctionWrappers v1.1.3
  [77dc65aa] FunctionWrappersWrappers v0.1.3
  [46192b85] GPUArraysCore v0.1.4
  [c145ed77] GenericSchur v0.5.3
  [86223c79] Graphs v1.8.0
  [3e5b6fbb] HostCPUFeatures v0.1.14
  [34004b35] HypergeometricFunctions v0.3.11
  [b5f81e59] IOCapture v0.2.2
  [615f187c] IfElse v0.1.1
  [d25df0c9] Inflate v0.1.3
  [3587e190] InverseFunctions v0.1.8
  [92d709cd] IrrationalConstants v0.2.2
  [42fd0dbc] IterativeSolvers v0.9.2
  [82899510] IteratorInterfaceExtensions v1.0.0
  [692b3bcd] JLLWrappers v1.4.1
  [682c06a0] JSON v0.21.3
  [ef3ab10e] KLU v0.4.0
  [ba0b0d4f] Krylov v0.9.0
  [0b1a1467] KrylovKit v0.6.0
  [10f19ff3] LayoutPointers v0.1.14
  [50d2b5c4] Lazy v0.15.1
  [d3d80556] LineSearches v7.2.0
  [7ed4a6bd] LinearSolve v1.38.0
  [2ab3a3ac] LogExpFunctions v0.3.23
  [bdcacae8] LoopVectorization v0.12.152
  [1914dd2f] MacroTools v0.5.10
  [d125e4d3] ManualMemory v0.1.8
  [e1d29d7a] Missings v1.1.0
  [46d2c3a1] MuladdMacro v0.2.4
  [0e6f8da7] NBodySimulator v1.10.0 `~/work/NBodySimulator.jl/NBodySimulator.jl`
  [d41bc354] NLSolversBase v7.8.3
  [2774e3e8] NLsolve v4.5.1
  [77ba4419] NaNMath v1.0.2
  [8913a72c] NonlinearSolve v1.5.0
  [6fe1bfb0] OffsetArrays v1.12.9
  [bac558e1] OrderedCollections v1.4.1
  [1dea7af3] OrdinaryDiffEq v6.49.2
  [90014a1f] PDMats v0.11.17
  [d96e819e] Parameters v0.12.3
  [69de0a69] Parsers v2.5.8
  [f517fe37] Polyester v0.7.3
  [1d0040c9] PolyesterWeave v0.2.1
  [d236fae5] PreallocationTools v0.4.12
  [21216c6a] Preferences v1.3.0
  [1fd47b50] QuadGK v2.8.2
  [3cdcf5f2] RecipesBase v1.3.3
  [731186ca] RecursiveArrayTools v2.38.0
  [f2c3362d] RecursiveFactorization v0.2.18
  [189a3867] Reexport v1.2.2
  [ae029012] Requires v1.3.0
  [79098fc4] Rmath v0.7.1
  [7e49a35a] RuntimeGeneratedFunctions v0.5.5
  [94e857df] SIMDTypes v0.1.0
  [476501e8] SLEEFPirates v0.6.38
  [0bca4576] SciMLBase v1.91.1
  [e9a6253c] SciMLNLSolve v0.1.3
  [c0aeaf25] SciMLOperators v0.2.0
  [efcf1570] Setfield v1.1.1
  [727e6d20] SimpleNonlinearSolve v0.1.14
  [699a6c99] SimpleTraits v0.9.4
  [ce78b400] SimpleUnPack v1.1.0
  [66db9d55] SnoopPrecompile v1.0.3
  [a2af1166] SortingAlgorithms v1.1.0
  [47a9eef4] SparseDiffTools v1.31.0
  [e56a9233] Sparspak v0.3.9
  [276daf66] SpecialFunctions v2.2.0
  [aedffcd0] Static v0.8.6
  [0d7ed370] StaticArrayInterface v1.3.0
  [90137ffa] StaticArrays v1.5.17
  [1e83bf80] StaticArraysCore v1.4.0
  [82ae8749] StatsAPI v1.5.0
  [2913bbd2] StatsBase v0.33.21
  [4c63d2b9] StatsFuns v1.3.0
  [7792a7ef] StrideArraysCore v0.4.9
  [2efcf032] SymbolicIndexingInterface v0.2.2
  [3783bdb8] TableTraits v1.0.1
  [bd369af6] Tables v1.10.1
  [8290d209] ThreadingUtilities v0.5.1
  [d5829a12] TriangularSolve v0.1.19
  [410a4b4d] Tricks v0.1.6
  [781d530d] TruncatedStacktraces v1.2.0
  [3a884ed6] UnPack v1.0.2
  [3d5dd08c] VectorizationBase v0.21.61
  [19fa3120] VertexSafeGraphs v0.2.0
  [700de1a5] ZygoteRules v0.2.2
  [efe28fd5] OpenSpecFun_jll v0.5.5+0
  [f50d1b31] Rmath_jll v0.4.0+0
  [0dad84c5] ArgTools v1.1.1
  [56f22d72] Artifacts
  [2a0f44e3] Base64
  [ade2ca70] Dates
  [8ba89e20] Distributed
  [f43a241f] Downloads v1.6.0
  [7b1f6079] FileWatching
  [9fa8497b] Future
  [b77e0a4c] InteractiveUtils
  [b27032c2] LibCURL v0.6.3
  [76f85450] LibGit2
  [8f399da3] Libdl
  [37e2e46d] LinearAlgebra
  [56ddb016] Logging
  [d6f4376e] Markdown
  [a63ad114] Mmap
  [ca575930] NetworkOptions v1.2.0
  [44cfe95a] Pkg v1.8.0
  [de0858da] Printf
  [3fa0cd96] REPL
  [9a3f8284] Random
  [ea8e919c] SHA v0.7.0
  [9e88b42a] Serialization
  [1a1011a3] SharedArrays
  [6462fe0b] Sockets
  [2f01184e] SparseArrays
  [10745b16] Statistics
  [4607b0f0] SuiteSparse
  [fa267f1f] TOML v1.0.0
  [a4e569a6] Tar v1.10.1
  [8dfed614] Test
  [cf7118a7] UUIDs
  [4ec0a83e] Unicode
  [e66e0078] CompilerSupportLibraries_jll v1.0.1+0
  [deac9b47] LibCURL_jll v7.84.0+0
  [29816b5a] LibSSH2_jll v1.10.2+0
  [c8ffd9c3] MbedTLS_jll v2.28.0+0
  [14a3606d] MozillaCACerts_jll v2022.2.1
  [4536629a] OpenBLAS_jll v0.3.20+0
  [05823500] OpenLibm_jll v0.8.1+0
  [bea87d4a] SuiteSparse_jll v5.10.1+0
  [83775a58] Zlib_jll v1.2.12+3
  [8e850b90] libblastrampoline_jll v5.1.1+0
  [8e850ede] nghttp2_jll v1.48.0+0
  [3f19e933] p7zip_jll v17.4.0+0
You can also download the manifest file and the project file.