Skip to content

Commit 7eade54

Browse files
committed
start adapting the code to 3D
1 parent 4c05a3b commit 7eade54

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

docs/source/chapters/chapter3.rst

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ Let us improve the previously created *InitializeSimulation* class:
3535
):
3636
super().__init__(*args, **kwargs)
3737
self.box_dimensions = box_dimensions
38-
self.dimensions = len(box_dimensions)
38+
# If a box dimension was entered as 0, dimensions will be 2
39+
self.dimensions = len(list(filter(lambda x: x > 0, box_dimensions)))
3940
self.seed = seed
4041
self.initial_positions = initial_positions
4142
self.thermo_period = thermo_period
@@ -137,11 +138,16 @@ method to the *InitializeSimulation* class:
137138
.. code-block:: python
138139
139140
def define_box(self):
140-
box_boundaries = np.zeros((self.dimensions, 2))
141-
for dim, L in zip(range(self.dimensions), self.box_dimensions):
141+
"""Define the simulation box.
142+
For 2D simulations, the third dimensions only contains 0.
143+
"""
144+
box_boundaries = np.zeros((3, 2))
145+
dim = 0
146+
for L in self.box_dimensions:
142147
box_boundaries[dim] = -L/2, L/2
148+
dim += 1
143149
self.box_boundaries = box_boundaries
144-
box_size = np.diff(self.box_boundaries).reshape(3)
150+
box_size = np.diff(self.box_boundaries).reshape(self.dimension)
145151
box_geometry = np.array([90, 90, 90])
146152
self.box_size = np.array(box_size.tolist()+box_geometry.tolist())
147153
@@ -183,9 +189,8 @@ case, the array must be of size 'number of atoms' times 'number of dimensions'.
183189
184190
def populate_box(self):
185191
if self.initial_positions is None:
186-
atoms_positions = np.zeros((self.total_number_atoms,
187-
self.dimensions))
188-
for dim in np.arange(self.dimensions):
192+
atoms_positions = np.zeros((self.total_number_atoms, 3))
193+
for dim in np.arange(3):
189194
diff_box = np.diff(self.box_boundaries[dim])
190195
random_pos = np.random.random(self.total_number_atoms)
191196
atoms_positions[:, dim] = random_pos*diff_box-diff_box/2

docs/source/chapters/chapter4.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,10 @@ class.
291291
sigma_ij = self.sigma_ij_list[Ni]
292292
epsilon_ij = self.epsilon_ij_list[Ni]
293293
# Measure distances
294-
rij_xyz = (np.remainder(position_i - positions_j + half_box_size, box_size) - half_box_size)
294+
# Measure distances
295+
# The nan_to_num is crutial in 2D to avoid nan value along third dimension
296+
rij_xyz = np.nan_to_num(np.remainder(position_i - positions_j
297+
+ half_box_size, box_size) - half_box_size)
295298
rij = np.linalg.norm(rij_xyz, axis=1)
296299
# Measure potential
297300
if output == "potential":

docs/source/chapters/chapter6.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ Let us add a method named *monte_carlo_move* to the *MonteCarlo* class:
5757
atom_id = np.random.randint(self.total_number_atoms)
5858
# Move the chosen atom in a random direction
5959
# The maximum displacement is set by self.displace_mc
60-
move = (np.random.random(self.dimensions)-0.5)*self.displace_mc
60+
if self.dimensions == 3:
61+
move = (np.random.random(3)-0.5)*self.displace_mc
62+
elif self.dimensions == 2: # the third value will be 0
63+
move = np.append((np.random.random(2) - 0.5) * self.displace_mc, 0)
6164
self.atoms_positions[atom_id] += move
6265
# Measure the optential energy of the new configuration
6366
trial_Epot = self.compute_potential(output="potential")

0 commit comments

Comments
 (0)