Thanks for lots of help on previous Molly.jl questions.
I’m trying to understand the " Simulating diatomic molecules example. I have re-structured the example to attempt to figure out the key ideas. See below (I skipped the cut-off of forces).
Questions:
- Does my re-structuring make sense (see listing below)?
- In spite of using
Unitful.jl, it seems like “Data” must be specified in specific units. As an example, I get an error message if I providesigmainpminstead ofnm. Is this correct, i.e., specific units must be used? - Even though diatomic molecules are simulated, it seems like Lennard-Jones parameters (
sigma,epsilon) for the atom is carried over and used for the molecule. Is this ok? Can I infer that if I, e.g., want to simulate \mathrm{N}_2, then I can use published LJ data for the molecule in theAtomspecification? - Bonds between two atoms in a molecule seem to be specified via a potential energy for an ideal spring. Where can I find
k_springandr_springfor, say, \mathrm{N_2} and other molecules? - In the
Molly.jldiatomic example, all atoms are initiated with a random velocity. Is this realistic when considering that half of the atoms are tightly bonded with the other atom in the molecule? Or this just done for simplification?
Import
#=
using Unitful
import Plots as plt
using Latexify
using LaTeXStrings
using Statistics
using Molly
=#
Data
# Atom data
# - Molar mass
M_atom = 10.0u"g/mol"
# - Lennard-Jones parameters
sigma = 0.3u"nm"
epsilon = 0.2u"kJ*mol^-1"
#
# Molecule data
# - Spring energy parameters between atoms
k_spring = 300_000.0u"kJ*mol^-1*nm^-2"
r_spring = 0.1u"nm"
#
# Box for MD simulation
# - side of cubic box
r_box = 2.0u"nm"
#
# Thermostat
# - temperature
T_ts = 100u"K"
# - atom/thermostat collision probability
Pr_ts = 2e-3
#
# Number of molecules
n_mols = 50
Setting up atoms & molecules
# Involved atoms
atoms = [Atom(mass=M_atom, σ=sigma, ϵ=epsilon) for i in 1:2*n_mols]
#
# Bonds (spring potential energy) between atoms in molecules: k, r0
bonds = InteractionList2Atoms(
collect(1:n_mols),
collect(n_mols+1:2*n_mols),
[HarmonicBond(k=k_spring, r0=r_spring) for i in 1:n_mols]
)
#
# Interaction list
specific_inter_lists = (bonds,)
Setting particle box + placing molecules + molecule interactions
box = CubicBoundary(r_box)
# Coordinates for base atom in molecules
coords = place_atoms(n_mols, box; min_dist=sigma)
# Adding coordinates for skewed atom in molecules
for i in 1:n_mols
push!(coords, coords[i] .+ [ustrip(r_spring),0.0,0.0]u"nm")
end
# Specifying velocity to atoms
velocities = [random_velocity(M_atom, T_ts) for i in 1:2*n_mols]
# Pairwise interactions between molecules
pairwise_inters = (LennardJones(),)
Setting up system
# Subsampling of loggers
n_log = 10
sys = System(
atoms=atoms,
coords=coords,
boundary=box,
velocities=velocities,
specific_inter_lists=specific_inter_lists,
#pairwise_inters=pairwise_inters,
#neighbor_finder=neighbor_finder,
loggers=(
temp=TemperatureLogger(n_log),
pressure=PressureLogger(n_log),
coords=CoordinatesLogger(n_log),
),
)
Simulating system
# Simulation times
# - Fixed timestep in DE solver
dt = 0.002u"ps"
# - Number of timesteps
n_dt = 1_000
# - Thermostat coupling time dt_ts; Pr_ts = dt/dt_ts
dt_ts = dt/Pr_ts
#
# Specifying simulator
simulator = VelocityVerlet(
dt=dt,
coupling=AndersenThermostat(T_ts, dt_ts),
)
#
# Simulating System
# - Initial short simulation to compile simulation
simulate!(deepcopy(sys),simulator,20)
# - Full simulation
simulate!(sys,simulator,n_dt)
Plotting result
tvec = 0.0u"ps":dt*n_log:dt*n_dt
plt.plot(tvec,values(sys.loggers.temp); label="Molly logger")
plt.plot!(tvec,mean(values(sys.loggers.temp))*ones(size(tvec)); label="mean logger")
plt.plot!(tvec,temperature(sys)*ones(size(tvec)); label="Molly temperature")
plt.plot!(;xlabel="\\mathrm{time}", ylabel="T",unitformat=latexify,
title="Diatomic Molly.jl example", frame=:box)









