11import sys
22import os
3+ import time
4+ import pstats
5+ import cProfile
36import unittest
47import numpy as np
58from pint import UnitRegistry
@@ -30,7 +33,7 @@ def setUp(self):
3033 # Initialize the MonteCarlo object
3134 self .mc = MonteCarlo (
3235 ureg = ureg ,
33- maximum_steps = 10000 ,
36+ maximum_steps = 100000 ,
3437 thermo_period = 1000 ,
3538 dumping_period = 1000 ,
3639 number_atoms = [nmb_1 ],
@@ -41,20 +44,58 @@ def setUp(self):
4144 cut_off = rc ,
4245 thermo_outputs = "Epot-press" ,
4346 desired_temperature = T ,
44- neighbor = 20 ,
47+ neighbor = 50 ,
4548 displace_mc = displace_mc ,
4649 )
4750
51+ """
4852 def test_monte_carlo_run(self):
49- """ Test if the Monte Carlo simulation runs without errors."""
53+ # Test if the Monte Carlo simulation runs without errors.
5054 try:
5155 # Run the Monte Carlo simulation (this should not raise an exception)
56+ ti = time.time()
57+
58+ # Profile the run() method
5259 self.mc.run()
60+ # self.mc.run()
5361 # If it runs successfully, assert True
62+ tf = time.time()
63+
64+ print("Duration:", np.round(tf-ti, 2), "s")
5465 self.assertTrue(True)
5566 except Exception as e:
5667 # If any exception occurs, fail the test and print the error
5768 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+
5899
59100if __name__ == "__main__" :
60101 unittest .main ()
0 commit comments