import os
import tempfile
import unittest
from ensembler import potentials as pot, system
from ensembler.samplers import _basicSamplers
from ensembler.samplers import stochastic, newtonian, optimizers
"""
STOCHASTIC INTEGRATORS
"""
[docs]class standard_IntegratorTests(unittest.TestCase):
integrator_class = _basicSamplers._samplerCls
tmp_test_dir: str = None
[docs] def setUp(self) -> None:
test_dir = os.getcwd()+"/tests_out"
if(not os.path.exists(test_dir)):
os.mkdir(test_dir)
if(__class__.tmp_test_dir is None):
__class__.tmp_test_dir = tempfile.mkdtemp(dir=test_dir, prefix="tmp_test_sampler")
_, self.tmp_out_path = tempfile.mkstemp(prefix="test_" + self.integrator_class.name, suffix=".obj", dir=__class__.tmp_test_dir)
[docs] def test_constructor(self):
integrator = self.integrator_class()
[docs] def test_save_integrator(self):
integrator = self.integrator_class()
integrator.save(self.tmp_out_path)
[docs] def test_load_integrator(self):
integrator = self.integrator_class()
integrator.save(self.tmp_out_path)
del integrator
integrator = self.integrator_class.load(self.tmp_out_path)
#print(integrator)
[docs]class test_MonteCarlo_Integrator(standard_IntegratorTests):
integrator_class = stochastic.metropolisMonteCarloIntegrator
[docs] def test_step(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
newPos, _, posShift = integrator.step(system=sys)
self.assertNotEqual(old_pos, newPos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, posShift, msg="Nothing happened here!")
[docs] def test_integrate(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
steps = 42
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
integrator.integrate(system=sys, steps=steps)
new_pos, new_Force = sys._currentPosition, sys._currentForce
self.assertEqual(steps + 1, len(sys.trajectory), msg="The simulation did not run or was too short!")
self.assertNotEqual(old_pos, new_pos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, new_Force, msg="Nothing happened here!")
pass
[docs]class test_MetropolisMonteCarlo_Integrator(standard_IntegratorTests):
integrator_class = stochastic.metropolisMonteCarloIntegrator
[docs] def test_step(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
newPos, _, posShift = integrator.step(system=sys)
self.assertNotEqual(old_pos, newPos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, posShift, msg="Nothing happened here!")
[docs] def test_integrate(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
steps = 42
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
integrator.integrate(system=sys, steps=steps)
new_pos, new_Force = sys._currentPosition, sys._currentForce
self.assertEqual(steps + 1, len(sys.trajectory), msg="The simulation did not run or was too short!")
self.assertNotEqual(old_pos, new_pos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, new_Force, msg="Nothing happened here!")
[docs]class test_Langevin_Integrator(standard_IntegratorTests):
integrator_class = stochastic.langevinIntegrator
[docs] def test_step(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
newPos, _, posShift = integrator.step(system=sys)
self.assertNotEqual(old_pos, newPos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, posShift, msg="Nothing happened here!")
[docs] def test_integrate(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
steps = 42
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
integrator.integrate(system=sys, steps=steps)
new_pos, new_Force = sys._currentPosition, sys._currentForce
self.assertEqual(steps + 1, len(sys.trajectory), msg="The simulation did not run or was too short!")
self.assertNotEqual(old_pos, new_pos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, new_Force, msg="Nothing happened here!")
[docs]class test_LangevinVelocity_Integrator(standard_IntegratorTests):
integrator_class = stochastic.langevinVelocityIntegrator
[docs] def test_step(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
newPos, _, posShift = integrator.step(system=sys)
self.assertNotEqual(old_pos, newPos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, posShift, msg="Nothing happened here!")
[docs] def test_integrate(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
steps = 42
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
integrator.integrate(system=sys, steps=steps)
new_pos, new_Force = sys._currentPosition, sys._currentForce
#print(sys.trajectory)
self.assertEqual(steps + 1, len(sys.trajectory), msg="The simulation did not run or was too short!")
self.assertNotEqual(old_pos, new_pos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, new_Force, msg="Nothing happened here!")
"""
NETOWNIAN
"""
[docs]class test_verlocityVerletIntegrator_Integrator(standard_IntegratorTests):
integrator_class = newtonian.velocityVerletIntegrator
[docs] def test_step(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
newPos, _, posShift = integrator.step(system=sys)
self.assertNotEqual(old_pos, newPos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, posShift, msg="Nothing happened here!")
[docs] def test_integrate(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
steps = 42
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
integrator.integrate(system=sys, steps=steps)
new_pos, new_Force = sys._currentPosition, sys._currentForce
self.assertEqual(steps + 1, len(sys.trajectory), msg="The simulation did not run or was too short!")
self.assertNotEqual(old_pos, new_pos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, new_Force, msg="Nothing happened here!")
[docs]class test_positionVerletIntegrator_Integrator(standard_IntegratorTests):
integrator_class = newtonian.positionVerletIntegrator
[docs] def test_step(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
newPos, _, posShift = integrator.step(system=sys)
self.assertNotEqual(old_pos, newPos, msg="Nothing happened here!")
[docs] def test_integrate(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
steps = 42
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
integrator.integrate(system=sys, steps=steps)
new_pos, new_Force = sys._currentPosition, sys._currentForce
self.assertEqual(steps + 1, len(sys.trajectory), msg="The simulation did not run or was too short!")
self.assertNotEqual(old_pos, new_pos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, new_Force, msg="Nothing happened here!")
[docs]class test_leapFrogIntegrator_Integrator(standard_IntegratorTests):
integrator_class = newtonian.leapFrogIntegrator
[docs] def test_step(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
newPos, _, posShift = integrator.step(system=sys)
self.assertNotEqual(old_pos, newPos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, posShift, msg="Nothing happened here!")
[docs] def test_integrate(self):
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
steps = 42
sys = system.system(potential=potent, sampler=integrator)
old_pos, oldForce = sys._currentPosition, sys._currentForce
integrator.integrate(system=sys, steps=steps)
new_pos, new_Force = sys._currentPosition, sys._currentForce
self.assertEqual(steps + 1, len(sys.trajectory), msg="The simulation did not run or was too short!")
self.assertNotEqual(old_pos, new_pos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, new_Force, msg="Nothing happened here!")
"""
Optimizer
"""
[docs]class test_cg_Integrator(standard_IntegratorTests):
integrator_class = optimizers.conjugate_gradient
[docs] def test_step(self):
position = 1
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
sys = system.system(potential=potent, sampler=integrator, start_position=position)
old_pos, oldForce = sys._currentPosition, sys._currentForce
newPos, _, posShift = integrator.step(system=sys)
self.assertNotEqual(old_pos, newPos, msg="Nothing happened here!")
[docs] def test_integrate(self):
position = 1
potent = pot.OneD.harmonicOscillatorPotential()
integrator = self.integrator_class()
steps = 42
sys = system.system(potential=potent, sampler=integrator, start_position=position)
old_pos, oldForce = sys._currentPosition, sys._currentForce
integrator.integrate(system=sys, steps=steps)
new_pos, new_Force = sys._currentPosition, sys._currentForce
self.assertEqual(steps + 1, len(sys.trajectory), msg="The simulation did not run or was too short!")
self.assertNotEqual(old_pos, new_pos, msg="Nothing happened here!")
self.assertNotEqual(oldForce, new_Force, msg="Nothing happened here!")
if __name__ == '__main__':
unittest.main()