Skip to content

Commit aee308a

Browse files
Merge pull request #93 from ChrisRackauckas/fix-formatting
Apply JuliaFormatter to fix code formatting
2 parents 2aed11f + 34563bd commit aee308a

17 files changed

+310
-201
lines changed

docs/make.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ cp("./docs/Project.toml", "./docs/src/assets/Project.toml", force = true)
66
include("pages.jl")
77

88
makedocs(sitename = "NbodySimulator.jl",
9-
authors = "Chris Rackauckas",
10-
modules = [NBodySimulator],
11-
clean = true, doctest = false, linkcheck = true,
12-
warnonly = [:docs_block, :missing_docs],
13-
format = Documenter.HTML(assets = ["assets/favicon.ico"],
14-
canonical = "https://docs.sciml.ai/NBodySimulator/stable/"),
15-
pages = pages)
9+
authors = "Chris Rackauckas",
10+
modules = [NBodySimulator],
11+
clean = true, doctest = false, linkcheck = true,
12+
warnonly = [:docs_block, :missing_docs],
13+
format = Documenter.HTML(assets = ["assets/favicon.ico"],
14+
canonical = "https://docs.sciml.ai/NBodySimulator/stable/"),
15+
pages = pages)
1616

1717
deploydocs(repo = "github.com/SciML/NBodySimulator.jl.git";
18-
push_preview = true)
18+
push_preview = true)

docs/pages.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
pages = [
44
"Home" => "index.md",
55
"examples.md",
6-
"nbodysimulator.md",
6+
"nbodysimulator.md"
77
]

docs/src/index.md

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ The Lennard-Jones potential is used in molecular dynamics simulations to approxi
8585

8686
```julia
8787
system = PotentialNBodySystem(bodies,
88-
Dict(:gravitational => g_parameters,
89-
:electrostatic => el_potential))
88+
Dict(:gravitational => g_parameters,
89+
:electrostatic => el_potential))
9090
```
9191

9292
### Custom Potential
@@ -108,7 +108,7 @@ Next, the acceleration function for the potential is required. The custom potent
108108
```julia
109109
import NBodySimulator.get_accelerating_function
110110
function get_accelerating_function(p::CustomPotentialParameters,
111-
simulation::NBodySimulation)
111+
simulation::NBodySimulation)
112112
ms = get_masses(simulation.system)
113113
(dv, u, v, t, i) -> begin
114114
custom_accel = SVector(0.0, 0.0, p.a)
@@ -165,10 +165,14 @@ And, finally, we can animate our solution, showing two equal bodies rotating on
165165
using Plots
166166
animate(sim_result, "path_to_animated_particles.gif")
167167
```
168+
168169
![Chaotic behavior of the Lorenz system](https://user-images.githubusercontent.com/16945627/39958539-d2cf779c-561d-11e8-96a8-ffc3a595be8b.gif)
170+
169171
### Electrostatic Interaction
172+
170173
Interaction between charged particles obeys Coulomb's law. The movement of such bodies can be simulated using `ChargedParticle` and `ChargedParticles` structures.
171174
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 the 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.
175+
172176
```julia
173177
r = 100.0 # m
174178
q1 = 1e-3 # C
@@ -183,9 +187,12 @@ system = ChargedParticles([p1, p2], k)
183187
simulation = NBodySimulation(system, (0.0, t))
184188
sim_result = run_simulation(simulation)
185189
```
190+
186191
### Magnetic Interaction
192+
187193
An N-body system consisting of `MagneticParticle`s can be used for the 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.
188194
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:
195+
189196
```julia
190197
iron_dencity = 7800 # kg/m^3
191198
magnetization_saturation = 1.2e6 # A/m
@@ -195,27 +202,35 @@ v = SVector(0.0, 0.0, 0.0) # m/s
195202
magnetic_moment = SVector(0.0, 0.0, magnetization_saturation * mass / iron_dencity) # A*m^2
196203
p1 = MagneticParticle(r, v, mass, magnetic_moment)
197204
```
205+
198206
For the second particle, we will use a shorter form:
207+
199208
```julia
200209
p2 = MagneticParticle(SVector(0.005, 0.0, 0.0), SVector(0.0, 0.0, 0.0), 5e-6,
201-
SVector(0.0, 0.0, 0.00077))
210+
SVector(0.0, 0.0, 0.00077))
202211
```
212+
203213
To calculate magnetic interactions properly, one should also specify the value for the constant μ0/4π or its substitute. Having created parameters for the magnetostatic potential, one can now instantiate a system of particles that should interact magnetically. For that purpose, we use `PotentialNBodySystem` and pass particles and potential parameters as arguments.
214+
204215
```julia
205216
parameters = MagnetostaticParameters(μ_4π)
206217
system = PotentialNBodySystem([p1, p2], Dict(:magnetic => parameters))
207218
simulation = NBodySimulation(system, (t1, t2))
208219
sim_result = run_simulation(simulation, VelocityVerlet(), dt = τ)
209220
```
221+
210222
## Molecular Dynamics (MD)
223+
211224
NBodySimulator allows one to conduct molecular dynamic simulations for the Lennard-Jones liquids, the SPC/Fw model of water, and other molecular systems thanks to implementations of basic interaction potentials between atoms and molecules:
225+
212226
- Lennard-Jones
213227
- electrostatic and magnetostatic
214228
- harmonic bonds
215229
- harmonic valence angle generated by pairs of bonds
216-
The comprehensive examples of liquid argon and water simulations can be found in the `examples` folder.
217-
Here only the basic principles of the molecular dynamics simulations using NBodySimulator are presented using liquid argon as a classical MD system for beginners.
218-
First, one needs to define the parameters of the simulation:
230+
The comprehensive examples of liquid argon and water simulations can be found in the `examples` folder.
231+
Here only the basic principles of the molecular dynamics simulations using NBodySimulator are presented using liquid argon as a classical MD system for beginners.
232+
First, one needs to define the parameters of the simulation:
233+
219234
```julia
220235
T = 120.0 # °K
221236
T0 = 90.0 # °K
@@ -233,110 +248,156 @@ bodies = generate_bodies_in_cell_nodes(N, m, v_dev, L)
233248
t1 = 0.0
234249
t2 = 2000τ
235250
```
251+
236252
Liquid argon consists of neutral molecules, so the Lennard-Jones potential runs their interaction:
253+
237254
```julia
238255
parameters = LennardJonesParameters(ϵ, σ, R)
239256
lj_system = PotentialNBodySystem(bodies, Dict(:lennard_jones => parameters));
240257
```
258+
241259
Then, a thermostat and boundary conditions should be selected and instantiated:
260+
242261
```julia
243262
thermostat = NoseHooverThermostat(T0, 200τ)
244263
pbc = CubicPeriodicBoundaryConditions(L)
245264
simulation = NBodySimulation(lj_system, (t1, t2), pbc, thermostat, kb);
246265
result = run_simulation(simulation, VelocityVerlet(), dt = τ)
247266
```
267+
248268
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:
269+
249270
```julia
250271
simulation = NBodySimulation(lj_system, (t1, t2));
251272
simulation = NBodySimulation(lj_system, (t1, t2), pbc);
252273
simulation = NBodySimulation(lj_system, (t1, t2), pbc, thermostat);
253274
simulation = NBodySimulation(lj_system, (t1, t2), pbc, thermostat, kb);
254275
```
276+
255277
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.
278+
256279
## Water Simulations
280+
257281
In NBodySImulator the [SPC/Fw water model](http://www.sklogwiki.org/SklogWiki/index.php/SPC/Fw_model_of_water) 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.
282+
258283
```julia
259284
bodies = generate_bodies_in_cell_nodes(N, mH2O, v, L)
260285
jl_parameters = LennardJonesParameters(ϵOO, σOO, R)
261286
e_parameters = ElectrostaticParameters(k, Rel)
262287
spc_parameters = SPCFwParameters(rOH, ∠HOH, k_bond, k_angle)
263288
water = WaterSPCFw(bodies, mH, mO, qH, qO, jl_parameters, e_parameters, spc_parameters);
264289
```
290+
265291
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.
266292
Further, one can pass the water system into the `NBodySimulation` constructor as a usual system of N-bodies.
293+
267294
```julia
268295
simulation = NBodySimulation(water, (t1, t2), pbc, kb);
269296
```
297+
270298
## Thermostats
299+
271300
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](https://mikhail-vaganov.github.io/gsoc-2018-blog/2018/08/06/thermostating.html).
301+
272302
### [Andersen Thermostat](http://www.sklogwiki.org/SklogWiki/index.php/Andersen_thermostat)
303+
273304
```julia
274305
τ = 0.5e-3 # timestep of integration and simulation
275306
T0 = 90
276307
ν = 0.05 / τ
277308
thermostat = AndersenThermostat(90, ν)
278309
```
310+
279311
![andersen thermostating](https://user-images.githubusercontent.com/16945627/44002487-cd99653a-9e5c-11e8-8481-78945a930d94.png)
280-
### [Berendsen Thermostat](https://www2.mpip-mainz.mpg.de/~andrienk/journal_club/thermostats.pdf)
312+
313+
### [Berendsen Thermostat](https://www2.mpip-mainz.mpg.de/%7Eandrienk/journal_club/thermostats.pdf)
314+
281315
```julia
282316
τB = 2000τ
283317
thermostat = BerendsenThermostat(90, τB)
284318
```
319+
285320
![berendsen thermostating](https://user-images.githubusercontent.com/16945627/44002495-f07e164a-9e5c-11e8-8db7-16c09a7631cd.png)
321+
286322
### [Nosé–Hoover Thermostat](http://www.sklogwiki.org/SklogWiki/index.php/Nos%C3%A9-Hoover_thermostat)
323+
287324
```julia
288325
τNH = 200τ
289326
thermostat = NoseHooverThermostat(T0, 200τ)
290327
```
328+
291329
![nose-hoover thermostating](https://user-images.githubusercontent.com/16945627/44002501-ffc1aea0-9e5c-11e8-857b-9e3c83197336.png)
330+
292331
### Langevin Thermostat
332+
293333
```julia
294334
γ = 10.0
295335
thermostat = LangevinThermostat(90, γ)
296336
```
337+
297338
![langevin thermostating](https://user-images.githubusercontent.com/16945627/44002505-0683c6b0-9e5d-11e8-8647-5b15b98eb0fa.png)
339+
298340
## Analyzing the Results of the Simulation
341+
299342
Once the simulation is completed, one can analyze the result and obtain some useful characteristics of the system.
300343
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 that help to interpret the solution of DEs into physical quantities.
301344
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 by calling this function:
345+
302346
```julia
303347
T = temperature(result, t)
304348
```
349+
305350
### [Radial distribution functions](https://en.wikipedia.org/wiki/Radial_distribution_function)
351+
306352
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.
353+
307354
```julia
308355
(rs, grf) = rdf(result)
309356
```
357+
310358
The dependence of `grf` on `rs` shows the radial distribution of particles at different distances from an average particle in a system.
311359
Here, the radial distribution function for the classic system of liquid argon is presented:
312360
![rdf for liquid argon](https://user-images.githubusercontent.com/16945627/43990348-843b164c-9d74-11e8-8d9e-daaff142c0b7.png)
361+
313362
### Mean Squared Displacement (MSD)
363+
314364
The MSD characteristic can be used to estimate the shift of particles from their initial positions.
365+
315366
```julia
316367
(ts, dr2) = msd(result)
317368
```
369+
318370
For a standard liquid argon system, the displacement grows with time:
319371
![rdf for liquid argon](https://user-images.githubusercontent.com/16945627/43990362-9a67c0aa-9d74-11e8-9512-08840294d411.png)
372+
320373
### Energy Functions
374+
321375
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:
376+
322377
```julia
323378
e_init = initial_energy(simulation)
324379
e_kin = kinetic_energy(result, t)
325380
e_pot = potential_energy(result, t)
326381
e_tot = total_energy(result, t)
327382
```
383+
328384
## Plotting Images
385+
329386
Using the tools of NBodySimulator, one can export the results of a simulation into a [Protein Database File](https://en.wikipedia.org/wiki/Protein_Data_Bank_(file_format)). [VMD](http://www.ks.uiuc.edu/Research/vmd/) is a well-known tool for visualizing molecular dynamics, which can read data from PDB files.
387+
330388
```julia
331389
save_to_pdb(result, "path_to_a_new_pdb_file.pdb")
332390
```
391+
333392
In the future, it will be possible to export results via the FileIO interface and its `save` function.
334393
Using Plots.jl, one can draw the positions of particles at any time of simulation or create an animation of moving particles, molecules of water:
394+
335395
```julia
336396
using Plots
337397
plot(result)
338398
animate(result, "path_to_file.gif")
339399
```
400+
340401
Makie.jl also has a recipe for plotting the results of N-body simulations. The [example](https://github.com/MakieOrg/Makie.jl/blob/master/ReferenceTests/src/tests/recipes.jl) is presented in the documentation.
341402

342403
## Contributing
@@ -356,40 +417,51 @@ Makie.jl also has a recipe for plotting the results of N-body simulations. The [
356417
+ See also [SciML Community page](https://sciml.ai/community/)
357418

358419
## Reproducibility
420+
359421
```@raw html
360422
<details><summary>The documentation of this SciML package was built using these direct dependencies,</summary>
361423
```
424+
362425
```@example
363426
using Pkg # hide
364427
Pkg.status() # hide
365428
```
429+
366430
```@raw html
367431
</details>
368432
```
433+
369434
```@raw html
370435
<details><summary>and using this machine and Julia version.</summary>
371436
```
437+
372438
```@example
373439
using InteractiveUtils # hide
374440
versioninfo() # hide
375441
```
442+
376443
```@raw html
377444
</details>
378445
```
446+
379447
```@raw html
380448
<details><summary>A more complete overview of all dependencies and their versions is also provided.</summary>
381449
```
450+
382451
```@example
383452
using Pkg # hide
384453
Pkg.status(; mode = PKGMODE_MANIFEST) # hide
385454
```
455+
386456
```@raw html
387457
</details>
388458
```
459+
389460
```@raw html
390461
You can also download the
391462
<a href="
392463
```
464+
393465
```@eval
394466
using TOML
395467
using Markdown

examples/liquid_argon.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function generate_random_directions(n::Int)
2222
directions = [@SVector [
2323
sin(theta[i]) .* cos(phi[i]),
2424
sin(theta[i]) .* sin(phi[i]),
25-
cos(theta[i]),
25+
cos(theta[i])
2626
] for i in 1:n]
2727
end
2828

examples/liquid_argon_reduced.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ bodies = generate_bodies_in_cell_nodes(N, _m, _v, _L)
2727
parameters = LennardJonesParameters(_ϵ, _σ, _R)
2828
lj_system = PotentialNBodySystem(bodies, Dict(:lennard_jones => parameters));
2929
simulation = NBodySimulation(lj_system, (_t1, _t2), CubicPeriodicBoundaryConditions(_L),
30-
/ T);
30+
/ T);
3131
#result = run_simulation(simulation, Tsit5())
3232
result = @time run_simulation(simulation, VelocityVerlet(), dt = _τ)
3333

0 commit comments

Comments
 (0)