Skip to content

Commit 476926f

Browse files
committed
merged matrix-vector and matrix
1 parent 23b110e commit 476926f

File tree

1 file changed

+20
-34
lines changed

1 file changed

+20
-34
lines changed

docs/source/chapters/chapter4.rst

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ following *run()* method to the *MinimizeEnergy* class:
148148
self.update_cross_coefficients() # Recalculate the cross coefficients, if necessary
149149
if self.step == 0: # At the first step, Epot/MaxF do not exists yet, calculate them both
150150
init_Epot = self.compute_potential()
151-
forces, init_MaxF = self.compute_vector_force()
151+
forces, init_MaxF = self.compute_force()
152152
# Save the current atom positions
153153
init_positions = copy.deepcopy(self.atoms_positions)
154154
# Move the atoms in the opposite direction of the maximum force
@@ -160,7 +160,7 @@ following *run()* method to the *MinimizeEnergy* class:
160160
if trial_Epot < init_Epot: # accept new position
161161
self.Epot = trial_Epot
162162
# calculate the new max force and save it
163-
forces, init_MaxF = self.compute_vector_force()
163+
forces, init_MaxF = self.compute_force()
164164
self.MaxF = np.max(np.abs(forces))
165165
self.wrap_in_box() # Wrap atoms in the box, if necessary
166166
self.displacement *= 1.2 # Multiply the displacement by a factor 1.2
@@ -312,8 +312,12 @@ class.
312312

313313
.. code-block:: python
314314
315-
def compute_vector_force(self):
316-
force_vector = np.zeros((self.total_number_atoms,3))
315+
def compute_force(self, return_vector = True):
316+
if return_vector: # return a N-size vector
317+
force_vector = np.zeros((self.total_number_atoms,3))
318+
else: # return a N x N matrix
319+
force_matrix = np.zeros((self.total_number_atoms,
320+
self.total_number_atoms,3))
317321
for Ni in np.arange(self.total_number_atoms-1):
318322
# Read neighbor list
319323
neighbor_of_i = self.neighbor_lists[Ni]
@@ -326,39 +330,21 @@ class.
326330
epsilon_ij = self.epsilon_ij_list[Ni]
327331
fij_xyz = potentials(self.potential_type, epsilon_ij,
328332
sigma_ij, rij, derivative = True)
329-
# Add the contribution to both Ni and its neighbors
330-
force_vector[Ni] += np.sum((fij_xyz*rij_xyz.T/rij).T, axis=0)
331-
force_vector[neighbor_of_i] -= (fij_xyz*rij_xyz.T/rij).T
332-
max_force = np.max(np.abs(force_vector))
333-
return force_vector, max_force
333+
if return_vector:
334+
# Add the contribution to both Ni and its neighbors
335+
force_vector[Ni] += np.sum((fij_xyz*rij_xyz.T/rij).T, axis=0)
336+
force_vector[neighbor_of_i] -= (fij_xyz*rij_xyz.T/rij).T
337+
else:
338+
# Add the contribution to the matrix
339+
force_matrix[Ni][neighbor_of_i] += (fij_xyz*rij_xyz.T/rij).T
340+
if return_vector:
341+
max_force = np.max(np.abs(force_vector))
342+
return force_vector, max_force
343+
else:
344+
return force_matrix
334345
335346
.. label:: end_Utilities_class
336347

337-
.. label:: start_Utilities_class
338-
339-
.. code-block:: python
340-
341-
def compute_vector_matrix(self):
342-
force_matrix = np.zeros((self.total_number_atoms,
343-
self.total_number_atoms,3))
344-
for Ni in np.arange(self.total_number_atoms-1):
345-
# Read neighbor list
346-
neighbor_of_i = self.neighbor_lists[Ni]
347-
# Measure distance
348-
rij, rij_xyz = self.compute_distance(self.atoms_positions[Ni],
349-
self.atoms_positions[neighbor_of_i],
350-
self.box_size[:3], only_norm = False)
351-
# Measure force using information about cross coefficients
352-
sigma_ij = self.sigma_ij_list[Ni]
353-
epsilon_ij = self.epsilon_ij_list[Ni]
354-
fij_xyz = potentials(self.potential_type, epsilon_ij,
355-
sigma_ij, rij, derivative = True)
356-
# Add the contribution to the force matrix
357-
force_matrix[Ni][neighbor_of_i] += (fij_xyz*rij_xyz.T/rij).T
358-
return force_matrix
359-
360-
.. label:: end_Utilities_class
361-
362348
Here, the method is a little bit complicated, because three types of outputs can
363349
be requested by the user: *force-vector*, *force-matrix*, and *potential*. The last
364350
one, *potential*, simply returns the value of the potential energy for the entire system.

0 commit comments

Comments
 (0)