Skip to content

Commit 98ffac5

Browse files
committed
added support for 1D/2D sims
1 parent 36f61a0 commit 98ffac5

File tree

2 files changed

+45
-46
lines changed

2 files changed

+45
-46
lines changed

MolecularDynamics/dump.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,49 +30,49 @@
3030

3131
import numpy as np
3232

33-
def writeOutput(filename, natoms, timestep, box, **args):
33+
def writeOutput(filename, natoms, timestep, box, **data):
3434
""" Writes the output (in dump format) """
3535

3636
axis = ('x', 'y', 'z')
37-
37+
3838
with open(filename, 'a') as fp:
39-
40-
fp.write('ITEM: TIMESTEP\n')
41-
fp.write('{}\n'.format(timestep))
42-
fp.write('ITEM: NUMBER OF ATOMS\n')
43-
fp.write('{}\n'.format(natoms))
44-
fp.write('ITEM: BOX BOUNDS ff ff ff\n')
45-
46-
for box_low, box_high in box:
47-
fp.write( '{} {}\n'.format(box_low,box_high) )
48-
49-
keys = args.keys()
50-
51-
for key in keys:
52-
dims = len(args[key].shape)
39+
40+
fp.write('ITEM: TIMESTEP\n')
41+
fp.write('{}\n'.format(timestep))
42+
43+
fp.write('ITEM: NUMBER OF ATOMS\n')
44+
fp.write('{}\n'.format(natoms))
45+
46+
fp.write('ITEM: BOX BOUNDS' + ' f' * len(box) + '\n')
47+
for box_bounds in box:
48+
fp.write('{} {}\n'.format(*box_bounds))
5349

54-
if dims > 1:
55-
for i in range(args[key].shape[1]):
50+
for i in range(len(axis) - len(box)):
51+
fp.write('0 0\n')
52+
53+
keys = list(data.keys())
54+
55+
for key in keys:
56+
isMatrix = len(data[key].shape) > 1
57+
58+
if isMatrix:
59+
_, nCols = data[key].shape
60+
61+
for i in range(nCols):
5662
if key == 'pos':
57-
args[axis[i]] = args[key][:,i]
63+
data['{}'.format(axis[i])] = data[key][:,i]
5864
else:
59-
args['{}_{}'.format(key, axis[i])] = args[key][:,i]
60-
61-
del args[key]
62-
63-
keys = args.keys()
64-
fp.write('ITEM: ATOMS' + (' {}' * len(keys)).format(*keys) + '\n')
65-
66-
output = []
67-
68-
if args:
69-
for key in keys:
70-
data = args[key]
71-
72-
if len(output):
73-
output = np.vstack((output, data.T))
74-
else:
75-
output = data
76-
77-
if len(output):
78-
np.savetxt(fp, output.T)
65+
data['{}_{}'.format(key,axis[i])] = data[key][:,i]
66+
67+
del data[key]
68+
69+
keys = data.keys()
70+
71+
fp.write('ITEM: ATOMS' + (' {}' * len(data)).format(*data) + '\n')
72+
73+
output = []
74+
for key in keys:
75+
output = np.hstack((output, data[key]))
76+
77+
if len(output):
78+
np.savetxt(fp, output.reshape((natoms, len(data)), order='F'))

MolecularDynamics/langevin.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ def run(**args):
109109

110110
natoms, box, dt, temp = args['natoms'], args['box'], args['dt'], args['temp']
111111
mass, relax, nsteps = args['mass'], args['relax'], args['steps']
112-
ofname, radius, freq = args['ofname'], args['radius'], args['freq']
113-
112+
ofname, freq, radius = args['ofname'], args['freq'], args['radius']
113+
114114
dim = len(box)
115115
pos = np.random.rand(natoms,dim)
116116

@@ -137,12 +137,12 @@ def run(**args):
137137
# Check if any particle has collided with the wall
138138
wallHitCheck(pos,vels,box)
139139

140-
# Compute output (avg kinetic energy)
141-
ins_temp = np.sum(np.dot(mass, (vels - vels.mean(axis=0))**2)) / (Boltzmann * 3 * natoms)
140+
# Compute output (temperature)
141+
ins_temp = np.sum(np.dot(mass, (vels - vels.mean(axis=0))**2)) / (Boltzmann * dim * natoms)
142142
output.append([step * dt, ins_temp])
143143

144144
if not step%freq:
145-
dump.writeOutput(ofname, natoms, step, box, mass=mass, radius=radius, pos=pos, v=vels)
145+
dump.writeOutput(ofname, natoms, step, box, radius=radius, pos=pos, v=vels)
146146

147147
return np.array(output)
148148

@@ -158,13 +158,12 @@ def run(**args):
158158
'steps': 10000,
159159
'freq': 100,
160160
'box': ((0, 1e-8), (0, 1e-8), (0, 1e-8)),
161-
'ofname': 'traj.dump'
161+
'ofname': 'traj-hydrogen-3D.dump'
162162
}
163163

164164
output = run(**params)
165165

166166
plt.plot(output[:,0] * 1e12, output[:,1])
167167
plt.xlabel('Time (ps)')
168168
plt.ylabel('Temp (K)')
169-
plt.grid(linestyle=':')
170169
plt.show()

0 commit comments

Comments
 (0)