Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# first do a couple of calculations
np.random.seed(0)
atoms.rattle()
cp_calc_1 = CheckpointCalculator(calc, logger=screen)
atoms.set_calculator(cp_calc_1)
e11 = atoms.get_potential_energy()
f11 = atoms.get_forces()
atoms.rattle()
e12 = atoms.get_potential_energy()
f12 = atoms.get_forces()
# then re-read them from checkpoint file
atoms = orig_atoms
np.random.seed(0)
atoms.rattle()
cp_calc_2 = CheckpointCalculator(calc, logger=screen)
atoms.set_calculator(cp_calc_2)
e21 = atoms.get_potential_energy()
f21 = atoms.get_forces()
atoms.rattle()
e22 = atoms.get_potential_energy()
f22 = atoms.get_forces()
self.assertAlmostEqual(e11, e21)
self.assertAlmostEqual(e12, e22)
self.assertArrayAlmostEqual(f11, f21)
self.assertArrayAlmostEqual(f12, f22)
def rattle_calc(self, atoms, calc):
try:
os.remove('checkpoints.db')
except OSError:
pass
orig_atoms = atoms.copy()
# first do a couple of calculations
np.random.seed(0)
atoms.rattle()
cp_calc_1 = CheckpointCalculator(calc, logger=screen)
atoms.set_calculator(cp_calc_1)
e11 = atoms.get_potential_energy()
f11 = atoms.get_forces()
atoms.rattle()
e12 = atoms.get_potential_energy()
f12 = atoms.get_forces()
# then re-read them from checkpoint file
atoms = orig_atoms
np.random.seed(0)
atoms.rattle()
cp_calc_2 = CheckpointCalculator(calc, logger=screen)
atoms.set_calculator(cp_calc_2)
e21 = atoms.get_potential_energy()
f21 = atoms.get_forces()
atoms.rattle()
self.logger.pr('retrieved results for {0} from checkpoint'.format(properties))
# save results in calculator for next time
if isinstance(self.calculator, Calculator):
if not hasattr(self.calculator, 'results'):
self.calculator.results = {}
self.calculator.results.update(dict(zip(properties, results)))
except NoCheckpoint:
if isinstance(self.calculator, Calculator):
self.logger.pr('doing calculation of {0} with new-style calculator interface'.format(properties))
self.calculator.calculate(atoms, properties, system_changes)
results = [self.calculator.results[prop] for prop in properties]
else:
self.logger.pr('doing calculation of {0} with old-style calculator interface'.format(properties))
results = []
for prop in properties:
method_name = CheckpointCalculator.property_to_method_name[prop]
method = getattr(self.calculator, method_name)
results.append(method(atoms))
_calculator = atoms.get_calculator()
try:
atoms.set_calculator(self.calculator)
self.checkpoint.save(atoms, *results)
finally:
atoms.set_calculator(_calculator)
self.results = dict(zip(properties, results))