Skip to content

Commit 438dd8c

Browse files
committed
fixed swap
1 parent b7dfcaa commit 438dd8c

File tree

3 files changed

+54
-35
lines changed

3 files changed

+54
-35
lines changed

Diff for: docs/source/chapters/chapter3.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ neighboring atoms, the simulation becomes more efficient. Add the following
183183

184184
.. code-block:: python
185185
186-
def update_neighbor_lists(self):
187-
if (self.step % self.neighbor == 0):
186+
def update_neighbor_lists(self, force_update=False):
187+
if (self.step % self.neighbor == 0) | force_update:
188188
matrix = distances.contact_matrix(self.atoms_positions,
189189
cutoff=self.cut_off, #+2,
190190
returntype="numpy",
@@ -218,8 +218,8 @@ below).
218218

219219
.. code-block:: python
220220
221-
def update_cross_coefficients(self):
222-
if (self.step % self.neighbor == 0):
221+
def update_cross_coefficients(self, force_update=False):
222+
if (self.step % self.neighbor == 0) | force_update:
223223
# Precalculte LJ cross-coefficients
224224
sigma_ij_list = []
225225
epsilon_ij_list = []

Diff for: docs/source/chapters/chapter8.rst

+31-30
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,40 @@ Let us add the following method called *monte_carlo_exchange* to the *MonteCarlo
2929
.. code-block:: python
3030
3131
def monte_carlo_exchange(self):
32-
# The first step is to make a copy of the previous state
33-
# Since atoms numbers are evolving, its also important to store the
34-
# neighbor, sigma, and epsilon lists
35-
self.Epot = self.compute_potential() # TOFIX: not necessary every time
36-
initial_positions = copy.deepcopy(self.atoms_positions)
37-
initial_number_atoms = copy.deepcopy(self.number_atoms)
38-
initial_neighbor_lists = copy.deepcopy(self.neighbor_lists)
39-
initial_sigma_lists = copy.deepcopy(self.sigma_ij_list)
40-
initial_epsilon_lists = copy.deepcopy(self.epsilon_ij_list)
41-
# Apply a 50-50 probability to insert or delete
42-
insert_or_delete = np.random.random()
43-
if np.random.random() < insert_or_delete:
44-
self.monte_carlo_insert()
45-
else:
46-
self.monte_carlo_delete()
47-
if np.random.random() < self.acceptation_probability: # accepted move
48-
# Update the success counters
32+
if self.desired_mu is not None:
33+
# The first step is to make a copy of the previous state
34+
# Since atoms numbers are evolving, its also important to store the
35+
# neighbor, sigma, and epsilon lists
36+
self.Epot = self.compute_potential() # TOFIX: not necessary every time
37+
initial_positions = copy.deepcopy(self.atoms_positions)
38+
initial_number_atoms = copy.deepcopy(self.number_atoms)
39+
initial_neighbor_lists = copy.deepcopy(self.neighbor_lists)
40+
initial_sigma_lists = copy.deepcopy(self.sigma_ij_list)
41+
initial_epsilon_lists = copy.deepcopy(self.epsilon_ij_list)
42+
# Apply a 50-50 probability to insert or delete
43+
insert_or_delete = np.random.random()
4944
if np.random.random() < insert_or_delete:
50-
self.successful_insert += 1
45+
self.monte_carlo_insert()
5146
else:
52-
self.successful_delete += 1
53-
else:
54-
# Reject the new position, revert to inital position
55-
self.neighbor_lists = initial_neighbor_lists
56-
self.sigma_ij_list = initial_sigma_lists
57-
self.epsilon_ij_list = initial_epsilon_lists
58-
self.atoms_positions = initial_positions
59-
self.number_atoms = initial_number_atoms
60-
# Update the failed counters
61-
if np.random.random() < insert_or_delete:
62-
self.failed_insert += 1
47+
self.monte_carlo_delete()
48+
if np.random.random() < self.acceptation_probability: # accepted move
49+
# Update the success counters
50+
if np.random.random() < insert_or_delete:
51+
self.successful_insert += 1
52+
else:
53+
self.successful_delete += 1
6354
else:
64-
self.failed_delete += 1
55+
# Reject the new position, revert to inital position
56+
self.neighbor_lists = initial_neighbor_lists
57+
self.sigma_ij_list = initial_sigma_lists
58+
self.epsilon_ij_list = initial_epsilon_lists
59+
self.atoms_positions = initial_positions
60+
self.number_atoms = initial_number_atoms
61+
# Update the failed counters
62+
if np.random.random() < insert_or_delete:
63+
self.failed_insert += 1
64+
else:
65+
self.failed_delete += 1
6566
6667
.. label:: end_MonteCarlo_class
6768

Diff for: docs/source/chapters/chapter9.rst

+19-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ Carlo swap move can considerably speed up equilibration.
3636
position2 = copy.deepcopy(self.atoms_positions[shift_2+atom_id_2])
3737
self.atoms_positions[shift_2+atom_id_2] = position1
3838
self.atoms_positions[shift_1+atom_id_1] = position2
39+
# force the recalculation of neighbor list
40+
initial_atoms_sigma = self.atoms_sigma
41+
initial_atoms_epsilon = self.atoms_epsilon
42+
initial_atoms_mass = self.atoms_mass
43+
initial_atoms_type = self.atoms_type
44+
initial_sigma_ij_list = self.sigma_ij_list
45+
initial_epsilon_ij_list = self.epsilon_ij_list
46+
initial_neighbor_lists = self.neighbor_lists
47+
self.update_neighbor_lists(force_update=True)
48+
self.identify_atom_properties()
49+
self.update_cross_coefficients(force_update=True)
3950
# Measure the potential energy of the new configuration
4051
trial_Epot = self.compute_potential()
4152
# Evaluate whether the new configuration should be kept or not
@@ -49,7 +60,14 @@ Carlo swap move can considerably speed up equilibration.
4960
else: # Reject new position
5061
self.atoms_positions = initial_positions # Revert to initial positions
5162
self.failed_swap += 1
52-
63+
self.atoms_sigma = initial_atoms_sigma
64+
self.atoms_epsilon = initial_atoms_epsilon
65+
self.atoms_mass = initial_atoms_mass
66+
self.atoms_type = initial_atoms_type
67+
self.sigma_ij_list = initial_sigma_ij_list
68+
self.epsilon_ij_list = initial_epsilon_ij_list
69+
self.neighbor_lists = initial_neighbor_lists
70+
5371
.. label:: end_MonteCarlo_class
5472

5573
Let us initialise swap counter:

0 commit comments

Comments
 (0)