def test_offset(self): test_in = 'OUTCAR.21' ref_pos = np.array([2.23782, 2.37638, 0.06997]) ref_force = np.array([-0.130492, -0.307476, -1.951215]) ref_energy = -306.56723878 parser = Parser(test_in) for energy, pos, force in parser.build_configurations(10000,1): self.assertEqual(energy, ref_energy) self.assertTrue(np.array_equal(ref_pos, pos[0])) self.assertTrue(np.array_equal(ref_force, force[0]))
def test_read_lattice_vecors(self): test_in = 'OUTCAR.21' parser = Parser(test_in) test_lattice = np.array([ [10.546640000, 0.000000000, 0.000000000], [0.000000000, 10.546640000, 0.000000000], [0.000000000, 0.000000000, 10.546640000] ]) self.assertTrue( np.array_equal(parser.find_lattice_vectors(), test_lattice), 'lattice vectors do not match' )
def test_warn_not_cubic(self): import sys from io import StringIO warn_message = '*************WARNING*************\nThe given lattice vectors\n' \ '[[10. 0. 0. ]\n [ 0. 10.54664 0. ]\n' \ ' [ 0. 0. 10.54664]]\n' \ 'do not constitute a simple basic lattice.\n' \ 'The programm wont work correctly' wrong_parser = Parser('test_data/wrong_data_outcar.21') saved_stdout = sys.stdout try: out = StringIO() sys.stdout = out wrong_parser.find_lattice_vectors() output = out.getvalue().strip() self.assertEqual(output, warn_message, 'Warning message is not correct') finally: sys.stdout = saved_stdout
def load_data(u_conf: dict, stepsize=0) -> (int, int, np.array, list): ''' Loads the data from the file specified in u_conf and returns the parameters of the simulation as (N_conf, N_ion, lattice vectors, list of configurations) ''' if stepsize == 0: stepsize = int(input("Stepsize for prediction? (integer): -> ")) # load parser and save nr of ions and lattice vectors parser = Parser(u_conf['file_in']) lattice_vectors = parser.find_lattice_vectors() lat_consts = np.diag(lattice_vectors.dot(lattice_vectors.T)) # check if lattice constant is bigger than 2 rcut if any(np.greater(2 * u_conf["cutoff"], lat_consts)): raise ValueError('Cutoff cannot be bigger than half the lattice constants') # build the configurations from the parser configurations = [ Configuration(position, energy, forces) for (energy, position, forces) in parser .build_configurations(stepsize) ] return (len(configurations), parser.find_ion_nr(), lattice_vectors, configurations)
def test_read_pos_force_energy(self): test_in = 'OUTCAR.21' g_pos = np.array([2.26725, 2.36995, 0.06367]) g_force = np.array([-0.171492, -0.290427, -1.773642]) g_energy = -306.41169589 parser = Parser(test_in) self.assertIsNotNone(parser.build_configurations(1000), 'does not build iterator') nr_of_configs = len(list(parser.build_configurations(1000))) self.assertEqual(nr_of_configs, 1, 'does not return at least one config') nr_of_configs = len(list(parser.build_configurations(1))) self.assertEqual(nr_of_configs, 1000, 'does not return the correct number of configs') for energy, position, force in parser.build_configurations(1000): self.assertTrue(np.array_equal(position[0], g_pos), 'returns wrong position') self.assertTrue(np.array_equal(force[0], g_force), 'returns wrong force') self.assertEqual(energy, g_energy, 'returns wrong energy')
def test_file_opening(self): correct_path = 'OUTCAR.21' correct_parser = Parser(correct_path) self.assertEqual(correct_parser.filepath, correct_path) self.assertIsNotNone(correct_parser.outcar_content)
def test_ion_nrs(self): test_in = 'OUTCAR.21' parser = Parser(test_in) nr_ions = parser.find_ion_nr() self.assertIsInstance(nr_ions, int, 'ions should have type int') self.assertEqual(nr_ions, 64, f'nr of ions should be 64, is {nr_ions}')
def test_file_not_opening(self): wrong_path = 'mydata.21' with self.assertRaises(ValueError): Parser(wrong_path)