Skip to content

Commit a88b569

Browse files
committed
added some performance testing
1 parent 68bc865 commit a88b569

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

integration_tests/test_monte_carlo_move.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import sys
22
import os
33
import time
4+
import pstats
5+
import cProfile
46
import unittest
57
import numpy as np
68
from pint import UnitRegistry
@@ -31,7 +33,7 @@ def setUp(self):
3133
# Initialize the MonteCarlo object
3234
self.mc = MonteCarlo(
3335
ureg = ureg,
34-
maximum_steps = 10000,
36+
maximum_steps = 100000,
3537
thermo_period = 1000,
3638
dumping_period = 1000,
3739
number_atoms = [nmb_1],
@@ -46,19 +48,54 @@ def setUp(self):
4648
displace_mc = displace_mc,
4749
)
4850

51+
"""
4952
def test_monte_carlo_run(self):
50-
"""Test if the Monte Carlo simulation runs without errors."""
53+
# Test if the Monte Carlo simulation runs without errors.
5154
try:
5255
# Run the Monte Carlo simulation (this should not raise an exception)
5356
ti = time.time()
57+
58+
# Profile the run() method
5459
self.mc.run()
60+
# self.mc.run()
5561
# If it runs successfully, assert True
5662
tf = time.time()
63+
5764
print("Duration:", np.round(tf-ti, 2), "s")
5865
self.assertTrue(True)
5966
except Exception as e:
6067
# If any exception occurs, fail the test and print the error
6168
self.fail(f"Monte Carlo simulation failed with error: {e}")
69+
"""
70+
71+
def test_monte_carlo_run(self):
72+
"""Test if the Monte Carlo simulation runs without errors."""
73+
profiler = cProfile.Profile()
74+
try:
75+
# Start profiling before running the Monte Carlo simulation
76+
profiler.enable()
77+
78+
# Run the Monte Carlo simulation
79+
ti = time.time()
80+
self.mc.run() # Assuming self.mc is your Monte Carlo simulation object
81+
82+
# Stop profiling after the run
83+
profiler.disable()
84+
85+
tf = time.time()
86+
print("Duration:", np.round(tf - ti, 2), "s")
87+
88+
# Convert the profiler stats into a readable format
89+
stats = pstats.Stats(profiler)
90+
stats.strip_dirs() # Remove extraneous directory information
91+
stats.sort_stats('time') # Sort by time spent in function
92+
stats.print_stats(10) # Print top 10 slowest functions
93+
94+
self.assertTrue(True)
95+
except Exception as e:
96+
# If any exception occurs, fail the test and print the error
97+
self.fail(f"Monte Carlo simulation failed with error: {e}")
98+
6299

63100
if __name__ == "__main__":
64101
unittest.main()

molecular_simulation_code/monte_carlo.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from initialize_simulation import InitializeSimulation
77
from measurements_utilities import compute_epot
88
from monte_carlo_utilities import calculate_Lambda
9+
# from numba_test import numba_copy
910

1011
import warnings
1112
warnings.filterwarnings('ignore')
@@ -52,7 +53,9 @@ def monte_carlo_move(self):
5253
self.Epot = compute_epot(self.neighbor_lists, self.atoms_positions, self.box_mda, self.cross_coefficients)
5354
# Make a copy of the initial atom positions and initial energy
5455
initial_Epot = self.Epot
55-
initial_positions = copy.deepcopy(self.atoms_positions)
56+
# initial_positions = copy.deepcopy(self.atoms_positions)
57+
# initial_positions = np.copy(self.atoms_positions)
58+
# initial_positions = numba_copy(self.atoms_positions)
5659
# Pick an atom id randomly
5760
atom_id = np.random.randint(np.sum(self.number_atoms))
5861
# Move the chosen atom in a random direction
@@ -62,9 +65,12 @@ def monte_carlo_move(self):
6265
move = np.append(move, 0.0) # Pad with zero for the z-component
6366
else: # 3D case
6467
move = (np.random.random(3) - 0.5) * self.displace_mc
68+
initial_position_atom = np.copy(self.atoms_positions[atom_id])
6569
self.atoms_positions[atom_id] += move
70+
6671
# Measure the potential energy of the new configuration
6772
trial_Epot = compute_epot(self.neighbor_lists, self.atoms_positions, self.box_mda, self.cross_coefficients)
73+
6874
# Evaluate whether the new configuration should be kept or not
6975
beta = 1/self.desired_temperature
7076
delta_E = trial_Epot-initial_Epot
@@ -81,7 +87,8 @@ def monte_carlo_move(self):
8187
self.Epot = trial_Epot
8288
self.successful_move += 1
8389
else: # Reject new position
84-
self.atoms_positions = initial_positions # Revert to initial positions
90+
self.atoms_positions[atom_id] = initial_position_atom
91+
# self.atoms_positions = initial_positions # Revert to initial positions
8592
self.failed_move += 1
8693

8794
def monte_carlo_swap(self):
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import numpy as np
2+
from numba import njit
3+
4+
@njit
5+
def numba_copy(array):
6+
# Create an empty array of the same shape and type
7+
new_array = np.empty_like(array)
8+
# Copy elements manually
9+
for i in range(array.shape[0]):
10+
for j in range(array.shape[1]):
11+
new_array[i, j] = array[i, j]
12+
return new_array

0 commit comments

Comments
 (0)