-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-validation.py
More file actions
251 lines (204 loc) · 7.81 KB
/
run-validation.py
File metadata and controls
251 lines (204 loc) · 7.81 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "matplotlib>=3.10.8",
# "numpy>=2.4.1",
# "pandas>=2.3.3",
# "seaborn>=0.13.2",
# ]
# ///
# Comes from Monte Carlo Simulations of Binary Lennard–Jones Mixtures: A Test of the van der Waals One-Fluid Model, https://doi.org/10.1023/A:1022614200488
import itertools
import json
import math
import os
import subprocess
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
# Different from publication, because we start from rectangular lattice instead of fcc
# It shouldn't affect results, as we're not looking at crystallisation
N = 1000
path_to_exe = "particlesmc"
epsilon_1 = 1
epsilon_12 = 1.1523
epsilon_2 = 1.3702
sigma_1 = 1
sigma_12 = 1.0339
sigma_2 = 1.0640
def create_config(n1: int, n2: int, box_length: float, filepath: str) -> None:
# We do a square lattice
# Species are put randomly amongst the sites
with open(filepath, "w") as f:
f.write(f"{N:d}\n")
f.write(
f'Lattice="{box_length:.4f} 0.0 0.0 0.0 {box_length:.4f} 0.0 0.0 0.0 {box_length:.4f}" Properties=:species:S:1:pos:R:3\n'
)
species_indices = [1] * n1 + [2] * n2
np.random.shuffle(species_indices)
number_of_particle_in_each_direction = round(N ** (1 / 3))
if number_of_particle_in_each_direction**3 != N:
raise RuntimeError("N is not x^3")
dxdydz = box_length / number_of_particle_in_each_direction
counter = 0
for i, j, k in itertools.product(
range(number_of_particle_in_each_direction),
range(number_of_particle_in_each_direction),
range(number_of_particle_in_each_direction),
):
x = (i + 0.5) * dxdydz - box_length / 2
y = (j + 0.5) * dxdydz - box_length / 2
z = (k + 0.5) * dxdydz - box_length / 2
f.write(f"{species_indices[counter]} {x} {y} {z}\n")
counter += 1
def create_params(parameters: dict, path_to_params: str) -> None:
with open(path_to_params, "w") as f:
f.write(f"""
[system]
config = "{parameters["config"]}"
temperature = {parameters["temperature"]}
density = {parameters["density"]}
list_type = "LinkedList"
[model]
[model."1-1"]
name = "LennardJones"
epsilon = {parameters["epsilon_1"]:.2f}
sigma = {parameters["sigma_1"]:.2f}
rcut = {parameters["rcut"]:.2f}
shift_potential = false
[model."1-2"]
name = "LennardJones"
epsilon = {parameters["epsilon_12"]}
sigma = {parameters["sigma_12"]}
rcut = {parameters["rcut"]:.2f}
shift_potential = false
[model."2-2"]
name = "LennardJones"
epsilon = {parameters["epsilon_2"]}
sigma = {parameters["sigma_2"]}
rcut = {parameters["rcut"]:.2f}
shift_potential = false
[simulation]
type = "Metropolis"
steps = {int(parameters["steps"]):d}
seed = 42
parallel = false
output_path = "./"
[[simulation.move]]
action = "Displacement"
probability = 0.9
policy = "SimpleGaussian"
parameters = {{sigma = 0.05}}
[[simulation.move]]
action = "DiscreteSwap"
probability = 0.1
policy = "DoubleUniform"
parameters = {{species = [1, 2]}}
[[simulation.output]]
algorithm = "StoreCallbacks"
callbacks = ["energy"]
scheduler_params = {{linear_interval = 100}}
[[simulation.output]]
algorithm = "StoreAcceptance"
dependencies = ["Metropolis"]
scheduler_params = {{linear_interval = 100}}
[[simulation.output]]
algorithm = "StoreLastFrames"
scheduler_params = {{linear_interval = 1000}}
fmt = "EXYZ"
""")
def run_simulations(output_path: str) -> None:
df_ref = pd.read_csv("reference-data.csv")
path_to_config = "config.exyz"
path_to_params = "params.toml"
data = []
for i, row in df_ref.iterrows():
workdir = f"./tmp/{i}"
os.makedirs(workdir, exist_ok=True)
print(row["t"], row["x"], row["density"])
# density = N / box_length**3
box_length = (N / row["density"]) ** (1 / 3)
n2 = round(N * row["x"])
n1 = N - n2
create_config(n1, n2, box_length, f"{workdir}/{path_to_config}")
# Generate input, and run
rc = 4 * sigma_1
parameters = {
"config": path_to_config,
"temperature": row["t"],
"epsilon_1": epsilon_1,
"epsilon_12": epsilon_12,
"epsilon_2": epsilon_2,
"sigma_1": sigma_1,
"sigma_12": sigma_12,
"sigma_2": sigma_2,
"steps": 1e3, # 1e4 equilibration in paper
"rcut": rc,
"density": N / box_length**3,
}
create_params(parameters, f"{workdir}/{path_to_params}")
subprocess.run(
[path_to_exe, path_to_params], cwd=workdir, stdout=subprocess.DEVNULL
)
# Post-process the energies
energies = pd.read_csv(f"{workdir}/chains/1/energy.dat", sep="\\s+", names=["i", "e"])[
"e"
]
# Remove the first half as equilibration, just to be sure
energies = energies[int(len(energies) / 2) :]
moves = {
1: "displacement",
2: "swap",
}
dfs = []
for move_id, move_name in moves.items():
path = f"{workdir}/moves/{move_id}/acceptance.dat"
df = pd.read_csv(path, sep=r"\s+", names=["i", move_name])
dfs.append(df)
# Merge all on column "i"
df_acceptance_rates = dfs[0]
for df in dfs[1:]:
df_acceptance_rates = df_acceptance_rates.merge(df, on="i")
displacement_acceptance = float(df_acceptance_rates["displacement"].iloc[-1])
swap_acceptance = float(df_acceptance_rates["swap"].iloc[-1])
# Compute long-range corrections from the cutoff
# Formula from Gromacs https://manual.gromacs.org/current/reference-manual/functions/long-range-vdw.html
lr_correction = 0
c6_11 = 4 * epsilon_1 * sigma_1**6
rho_11 = n1 / box_length**3
lr_correction += -2 / 3 * math.pi * n1 * rho_11 * c6_11 / rc**3
c6_22 = 4 * epsilon_2 * sigma_2**6
rho_22 = n2 / box_length**3
lr_correction += -2 / 3 * math.pi * n2 * rho_22 * c6_22 / rc**3
c6_12 = 4 * epsilon_12 * sigma_12**6
lr_correction += -2 / 3 * math.pi * (n1 * rho_22 + n2 * rho_11) * c6_12 / rc**3
data.append(
{
"t": row["t"],
"x": row["x"],
"density": row["density"],
"energy": np.mean(energies),
"energy_err": np.std(energies) / np.sqrt(len(energies)),
"lr_correction": lr_correction,
"acceptance_rate_displacement": displacement_acceptance,
"acceptance_rate_swap": swap_acceptance,
}
)
df_ref.merge(pd.DataFrame(data)).to_csv(output_path)
def plot_results(path_to_energies: str) -> None:
df = pd.read_csv(path_to_energies)
# u (from the paper) is actually total energy / epsilon_11 N
# epsilon_11 = 1, so it's the energy per particule
# Add long range corrections to the MC results, which is also in energy / particle
df["energy_lr"] = df["energy"] + df["lr_correction"] / N
sns.scatterplot(data=df, x="u", y="energy_lr", hue="density", style="x")
plt.axline((-5, -5), slope=1)
plt.xlabel("Published energy")
plt.ylabel("Re-computed from ParticleMC")
plt.savefig("correlation-plot.jpeg")
plt.show()
if __name__ == "__main__":
path_to_energies = "calculated-energies.csv"
run_simulations(path_to_energies)
plot_results(path_to_energies)