forked from jax-md/jax-md
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvt_simulation.py
More file actions
392 lines (297 loc) · 10 KB
/
nvt_simulation.py
File metadata and controls
392 lines (297 loc) · 10 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# ---
# jupyter:
# jupytext:
# formats: py:percent,ipynb
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.18.1
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %% [markdown]
# [](https://jax-md.readthedocs.io/en/main/notebooks/nvt_simulation.ipynb)
# [](https://raw.githubusercontent.com/google/jax-md/main/examples/nvt_simulation.py)
# %% [markdown]
# # Canonical Ensemble (NVT) - Nose-Hoover
# %% [markdown]
# Here we demonstrate some code to run a simulation at in the NVT ensemble. We start off by setting up some parameters of the simulation. This will include a temperature schedule that will start off at a high temperature and then instantaneously quench to a lower temperature.
# %% [markdown]
# ## Imports & Utils
# %%
import os
IN_COLAB = 'COLAB_RELEASE_TAG' in os.environ
if IN_COLAB:
import subprocess, sys
subprocess.run(
[
sys.executable,
'-m',
'pip',
'install',
'-q',
'git+https://github.com/jax-md/jax-md.git',
]
)
import numpy as onp
from jax import config
config.update('jax_enable_x64', True)
import jax.numpy as np
from jax import random
from jax import jit
from jax import lax
from jax import ops
import time
from jax_md import space, smap, energy, minimize, quantity, simulate
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
SMOKE_TEST = os.environ.get('READTHEDOCS', False)
sns.set_style(style='white')
def format_plot(x, y):
plt.xlabel(x, fontsize=20)
plt.ylabel(y, fontsize=20)
def finalize_plot(shape=(1, 1)):
plt.gcf().set_size_inches(
shape[0] * 1.5 * plt.gcf().get_size_inches()[1],
shape[1] * 1.5 * plt.gcf().get_size_inches()[1],
)
plt.tight_layout()
# %% [markdown]
# ## Setup Simulation Parameters
# %%
N = 500
dimension = 2
box_size = quantity.box_size_at_number_density(N, 0.8, 2)
dt = 5e-3
displacement, shift = space.periodic(box_size)
steps = 4000 if SMOKE_TEST else 10000
max_time = steps * dt
kT = lambda t: np.where(t < max_time / 2, 0.1, 0.01)
# %% [markdown]
# ## Generate Random Positions and Particle Sizes
#
# Next we need to generate some random positions as well as particle sizes.
# %%
key = random.PRNGKey(0)
# %%
key, split = random.split(key)
R = box_size * random.uniform(split, (N, dimension), dtype=np.float64)
# The system ought to be a 50:50 mixture of two types of particles, one
# large and one small.
sigma = np.array([[1.0, 1.2], [1.2, 1.4]])
N_2 = int(N / 2)
species = np.where(np.arange(N) < N_2, 0, 1)
# %% [markdown]
# ## Construct Simulation Operators
#
# Then we need to construct our simulation operators.
# %%
energy_fn = energy.soft_sphere_pair(displacement, species=species, sigma=sigma)
init, apply = simulate.nvt_nose_hoover(energy_fn, shift, dt, kT(0.0))
state = init(key, R)
# %% [markdown]
# ## Define Step Function with Logging
#
# Now let's actually do the simulation. To do this we'll write a small function that performs a single step of the simulation. This function will keep track of the temperature, the extended Hamiltonian of the Nose-Hoover dynamics, and the current particle positions.
# %%
write_every = 100
def step_fn(i, state_and_log):
state, log = state_and_log
t = i * dt
# Log information about the simulation.
T = quantity.temperature(momentum=state.momentum)
log['kT'] = log['kT'].at[i].set(T)
H = simulate.nvt_nose_hoover_invariant(energy_fn, state, kT(t))
log['H'] = log['H'].at[i].set(H)
# Record positions every `write_every` steps.
log['position'] = lax.cond(
i % write_every == 0,
lambda p: p.at[i // write_every].set(state.position),
lambda p: p,
log['position'],
)
# Take a simulation step.
state = apply(state, kT=kT(t))
return state, log
# %% [markdown]
# ## Run the Simulation
#
# To run our simulation we'll use `lax.fori_loop` which will execute the simulation a single call from python.
# %%
log = {
'kT': np.zeros((steps,)),
'H': np.zeros((steps,)),
'position': np.zeros((steps // write_every,) + R.shape),
}
state, log = lax.fori_loop(0, steps, step_fn, (state, log))
R = state.position
# %% [markdown]
# ## Plot Temperature Evolution
#
# Now, let's plot the temperature as a function of time. We see that the temperature tracks the goal temperature with some fluctuations.
# %%
t = onp.arange(0, steps) * dt
plt.plot(t, log['kT'], linewidth=3)
plt.plot(t, kT(t), linewidth=3)
format_plot('$t$', '$T$')
finalize_plot()
# %% [markdown]
# ## Plot NVT Hamiltonian
#
# Now let's plot the Hamiltonian of the system. We see that it is invariant apart from changes to the temperature, as expected.
# %%
plt.plot(t, log['H'], linewidth=3)
format_plot('$t$', '$H$')
finalize_plot()
# %% [markdown]
# ## Visualize the System
#
# Now let's plot a snapshot of the system.
# %%
ms = 65
R_plt = onp.array(state.position)
plt.plot(R_plt[:N_2, 0], R_plt[:N_2, 1], 'o', markersize=ms * 0.5)
plt.plot(R_plt[N_2:, 0], R_plt[N_2:, 1], 'o', markersize=ms * 0.7)
plt.xlim([0, np.max(R[:, 0])])
plt.ylim([0, np.max(R[:, 1])])
plt.axis('off')
finalize_plot((2, 2))
# %% [markdown]
# ## Animation (Optional)
#
# If we want, we can also draw an animation of the simulation using JAX MD's renderer. This only works in Google Colab.
# %%
if IN_COLAB:
from jax_md.colab_tools import renderer
diameters = sigma[species, species]
colors = np.where(
species[:, None],
np.array([[1.0, 0.5, 0.01]]),
np.array([[0.35, 0.65, 0.85]]),
)
renderer.render(
box_size,
{'particles': renderer.Disk(log['position'], diameters, colors)},
resolution=(700, 700),
)
else:
print('Renderer only available in Google Colab. Skipping.')
# %% [markdown]
# ## Larger Simulation with Neighbor Lists
# %% [markdown]
# We can use neighbor lists to run a much larger version of this simulation. As their name suggests, neighbor lists are lists of particles nearby a central particle. By keeping track of neighbors, we can compute the energy of the system much more efficiently. This becomes increasingly true as the simulation gets larger.
# %%
N = 4800 if SMOKE_TEST else 128000
box_size = quantity.box_size_at_number_density(N, 0.8, 2)
displacement, shift = space.periodic(box_size)
# %% [markdown]
# ## Initialize Large System
#
# As before we randomly initialize the system.
# %%
key, split = random.split(key)
R = box_size * random.uniform(split, (N, dimension), dtype=np.float64)
sigma = np.array([[1.0, 1.2], [1.2, 1.4]])
N_2 = int(N / 2)
species = np.where(np.arange(N) < N_2, 0, 1)
# %% [markdown]
# ## Construct Neighbor List and Energy Function
#
# Then we need to construct our simulation operators. This time we use the `energy.soft_sphere_neighbor_fn` to create two functions: one that constructs lists of neighbors and one that computes the energy.
# %%
neighbor_fn, energy_fn = energy.soft_sphere_neighbor_list(
displacement, box_size, species=species, sigma=sigma
)
init, apply = simulate.nvt_nose_hoover(
energy_fn, shift, dt, kT(0.0), tau=200 * dt
)
nbrs = neighbor_fn.allocate(R)
state = init(key, R, neighbor=nbrs)
# %% [markdown]
# ## Run Large Simulation
#
# Now let's actually do the simulation. This time our simulation step function will also update the neighbors. As above, we will also only record position data every hundred steps.
# %%
write_every = 100
def step_fn(i, state_nbrs_log):
state, nbrs, log = state_nbrs_log
t = i * dt
# Log information about the simulation.
T = quantity.temperature(momentum=state.momentum)
log['kT'] = log['kT'].at[i].set(T)
H = simulate.nvt_nose_hoover_invariant(energy_fn, state, kT(t), neighbor=nbrs)
log['H'] = log['H'].at[i].set(H)
# Record positions every `write_every` steps.
log['position'] = lax.cond(
i % write_every == 0,
lambda p: p.at[i // write_every].set(state.position),
lambda p: p,
log['position'],
)
# Take a simulation step.
state = apply(state, kT=kT(t), neighbor=nbrs)
nbrs = nbrs.update(state.position)
return state, nbrs, log
# %% [markdown]
# To run our simulation we'll use `lax.fori_loop` which will execute the simulation a single call from python.
# %%
steps = 4000 if SMOKE_TEST else 20000
max_time = steps * dt
kT = lambda t: np.where(t < max_time / 2, 0.1, 0.01)
log = {
'kT': np.zeros((steps,)),
'H': np.zeros((steps,)),
'position': np.zeros((steps // write_every,) + R.shape),
}
state, nbrs, log = lax.fori_loop(0, steps, step_fn, (state, nbrs, log))
R = state.position
# %% [markdown]
# ## Plot Results for Large Simulation
#
# Now, let's plot the temperature as a function of time. We see that the temperature tracks the goal temperature with some fluctuations.
# %%
t = onp.arange(0, steps) * dt
plt.plot(t, log['kT'], linewidth=3)
plt.plot(t, kT(t), linewidth=3)
format_plot('$t$', '$T$')
finalize_plot()
# %% [markdown]
# Now let's plot the Hamiltonian of the system. We see that it is invariant apart from changes to the temperature, as expected.
# %%
plt.plot(t, log['H'], linewidth=3)
format_plot('$t$', '$H$')
finalize_plot()
# %% [markdown]
# ## Visualize Large System
#
# Now let's plot a snapshot of the system.
# %%
ms = 10 if SMOKE_TEST else 1
R_plt = onp.array(state.position)
plt.plot(R_plt[:N_2, 0], R_plt[:N_2, 1], 'o', markersize=ms * 0.5)
plt.plot(R_plt[N_2:, 0], R_plt[N_2:, 1], 'o', markersize=ms * 0.7)
plt.xlim([0, np.max(R[:, 0])])
plt.ylim([0, np.max(R[:, 1])])
plt.axis('off')
finalize_plot((2, 2))
# %% [markdown]
# ## Velocity Distribution
#
# Finally, let's plot the velocity distribution compared with its theoretical prediction.
# %%
V_flat = onp.reshape(onp.array(state.velocity), (-1,))
occ, bins = onp.histogram(V_flat, bins=100, density=True)
# %%
T_cur = kT(steps * dt)
plt.semilogy(bins[:-1], occ, 'o')
plt.semilogy(
bins[:-1],
1.0 / np.sqrt(2 * np.pi * T_cur) * onp.exp(-1 / (2 * T_cur) * bins[:-1] ** 2),
linewidth=3,
)
format_plot('t', 'T')
finalize_plot()