def test_cell_init( self ): cell_matrix = np.array( [ [ 1.0, 1.0, 0.0 ], [ 1.0, 0.0, 1.0 ], [ 0.0, 1.0, 1.0 ] ] ) with patch( 'numpy.linalg.inv' ) as mock_invert: mock_invert.return_value = np.array( [ [ 0.0, 0.0, 1.0 ], [ 0.0, 1.0, 0.0 ], [ 1.0, 0.0, 0.0 ] ] ) cell = Cell( cell_matrix ) mock_invert.assert_called_with( cell_matrix ) np.testing.assert_array_equal( cell.matrix, cell_matrix ) np.testing.assert_array_equal( cell.inv_matrix, mock_invert.return_value )
def poscar_from_pimaim_restart(filename, atom_numbers, atom_labels): number_of_atoms = sum(atom_numbers) coordinates, velocities, dipoles, full_cell_matrix = read_restart_file( filename, number_of_atoms) poscar = Poscar() poscar.cell = Cell( full_cell_matrix) # TODO: possibly this needs transposing poscar.atoms = atom_labels poscar.atom_numbers = atom_numbers poscar.coordinate_type = 'Direct' poscar.coordinates = poscar.cell.cartesian_to_fractional_coordinates( coordinates) return poscar
def main(): filename = 'testout.rst' restart_file = True args = parse_command_line_arguments() coordinates, velocities, dipoles, full_cell_matrix, cell_lengths = read_pimaim_restart( filename ) assert( sum( args.atom_numbers ) == len( coordinates ) ) poscar = Poscar() full_cell_matrix = full_cell_matrix.transpose() coordinates = get_cart_coords_from_pimaim_restart(coordinates, full_cell_matrix,cell_lengths) poscar.cell = Cell( full_cell_matrix) poscar.atoms = args.labels poscar.atom_numbers = args.atom_numbers poscar.coordinate_type = 'Cartesian' poscar.coordinates = coordinates poscar.output()
def main(): filename = 'testout.rst' restart_file = True args = parse_command_line_arguments() coordinates, velocities, dipoles, full_cell_matrix = read_pimaim_restart( filename) assert (sum(args.atom_numbers) == len(coordinates)) poscar = Poscar() poscar.cell = Cell( full_cell_matrix) # TODO: possibly this needs transposing? poscar.atoms = args.labels poscar.atom_numbers = args.atom_numbers poscar.coordinate_type = 'Cartesian' poscar.coordinates = coordinates poscar.output()
def poscar_from_pimaim_restart(filename, atom_numbers, atom_labels): number_of_atoms = sum(atom_numbers) coordinates, velocities, dipoles, full_cell_matrix, cell_lengths = read_restart_file( filename, number_of_atoms, cell_lengths) poscar = Poscar() full_cell_matrix = full_cell_matrix.transpose() coordinates = get_cart_coords_from_pimaim_restart(coordinates, full_cell_matrix, cell_lengths) poscar.cell = Cell(full_cell_matrix) poscar.atoms = atom_labels poscar.atom_numbers = atom_numbers poscar.coordinate_type = 'Direct' poscar.coordinates = poscar.cell.cartesian_to_fractional_coordinates( coordinates) return poscar
class Test_Cell( unittest.TestCase ): def test_cell_init( self ): cell_matrix = np.array( [ [ 1.0, 1.0, 0.0 ], [ 1.0, 0.0, 1.0 ], [ 0.0, 1.0, 1.0 ] ] ) with patch( 'numpy.linalg.inv' ) as mock_invert: mock_invert.return_value = np.array( [ [ 0.0, 0.0, 1.0 ], [ 0.0, 1.0, 0.0 ], [ 1.0, 0.0, 0.0 ] ] ) cell = Cell( cell_matrix ) mock_invert.assert_called_with( cell_matrix ) np.testing.assert_array_equal( cell.matrix, cell_matrix ) np.testing.assert_array_equal( cell.inv_matrix, mock_invert.return_value ) def setUp( self ): cell_matrix = np.array( [ [ 10.0, 0.0, 0.0 ], [ 0.0, 10.0, 0.0 ], [ 0.0, 0.0, 10.0 ] ] ) self.cell = Cell( cell_matrix ) def test_dr( self ): r1 = np.array( [ 0.5, 0.1, 0.1 ] ) r2 = np.array( [ 0.1, 0.4, 0.1 ] ) self.assertEqual( self.cell.dr( r1, r2 ), 5.0 ) def test_dr_cutoff( self ): r1 = np.array( [ 0.5, 0.1, 0.1 ] ) r2 = np.array( [ 0.1, 0.4, 0.1 ] ) self.assertEqual( self.cell.dr( r1, r2, cutoff=1.0 ), None ) def test_nearest_image( self ): r1 = np.array( [ 0.5, 0.1, 0.1 ] ) r2 = np.array( [ 0.1, 0.4, 0.1 ] ) self.cell.minimum_image = Mock( return_value=np.array( [ -0.4, 0.3, 0.0 ] ) ) np.testing.assert_array_almost_equal( self.cell.nearest_image( r1, r2 ), r2 ) def test_minimum_image( self ): r1 = np.array( [ 0.5, 0.1, 0.1 ] ) r2 = np.array( [ 0.1, 0.4, 0.3 ] ) np.testing.assert_array_almost_equal( self.cell.minimum_image( r1, r2 ), np.array( [ -0.4, 0.3, 0.2 ] ) ) def test_minimum_image_2( self ): r1 = np.array( [ 0.5, 0.1, 0.1 ] ) r2 = np.array( [ 0.6, 0.8, 0.8 ] ) np.testing.assert_array_almost_equal( self.cell.minimum_image( r1, r2 ), np.array( [ 0.1, -0.3, -0.3 ] ) ) def test_minimum_image_dr( self ): r1 = np.array( [ 0.5, 0.1, 0.1 ] ) r2 = np.array( [ 0.8, 0.7, 0.3 ] ) self.cell.minimum_image = Mock( return_value=np.array( [ 0.3, -0.4, 0.2 ] ) ) self.assertAlmostEqual( self.cell.minimum_image_dr( r1, r2 ), 5.385164807 ) def test_lengths( self ): np.testing.assert_array_equal( self.cell.lengths(), np.array( [ 10.0, 10.0, 10.0 ] ) ) def test_angles( self ): self.assertEqual( self.cell.angles(), [ 90.0, 90.0, 90.0 ] ) def test_cartesian_to_fractional_coordinates( self ): coordinates = np.array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 6.0, 2.0 ] ] ) expected_fractional_coordinates = np.array( [ [ 0.1, 0.2, 0.3 ], [ 0.4, 0.6, 0.2 ] ] ) np.testing.assert_array_almost_equal( self.cell.cartesian_to_fractional_coordinates( coordinates ), expected_fractional_coordinates ) def test_fractional_to_cartesian_coordinates( self ): coordinates = np.array( [ [ 0.1, 0.2, 0.3 ], [ 0.4, 0.6, 0.2 ] ] ) expected_cartesian_coordinates = np.array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 6.0, 2.0 ] ] ) np.testing.assert_array_almost_equal( self.cell.fractional_to_cartesian_coordinates( coordinates ), expected_cartesian_coordinates ) def test_inside_cell( self ): c = np.array( [ [ [ 0.1, 0.2, 0.3 ], [ 0.1, 0.2, 0.3 ] ], [ [ 0.3, 1.2, 1.3 ], [ 0.3, 0.2, 0.3 ] ] ] ) for r1, r2 in c: np.testing.assert_array_almost_equal( self.cell.inside_cell( r1 ), r2 ) def test_volume( self ): self.assertEqual( self.cell.volume(), 1000.0 ) def test_unit_vectors( self ): np.testing.assert_array_equal( self.cell.unit_vectors(), np.array( [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] ) )
def setUp( self ): cell_matrix = np.array( [ [ 10.0, 0.0, 0.0 ], [ 0.0, 10.0, 0.0 ], [ 0.0, 0.0, 10.0 ] ] ) self.cell = Cell( cell_matrix )
number_of_atomic_data_lines = next( index for index, d in enumerate( data_per_line[4:] ) if d == 1 ) number_of_atomic_data_types = sum( [ cr_dump_log, vel_dump_log, chg_dump_log ] ) number_of_atoms = int( number_of_atomic_data_lines / number_of_atomic_data_types ) # this assumes coordinates, velocities, and dipoles are all present. # not sure what happens if atoms have qudrupoles, etc. coordinates = lines_to_numpy_array( file_data[ 4 : 4 + number_of_atoms ] ) velocities = lines_to_numpy_array( file_data[ 4 + number_of_atoms : 4 + number_of_atoms * 2 ] ) dipoles = lines_to_numpy_array( file_data[ 4 + number_of_atoms * 2 : 4 + number_of_atoms * 3 ] ) cell_matrix = lines_to_numpy_array( file_data[ -6: -3 ] ) cell_lengths = lines_to_numpy_array( file_data[ -3: ] ) full_cell_matrix = cell_matrix * cell_lengths # TODO! need to check this with a non-orthorhombic cell return( coordinates, velocities, dipoles, full_cell_matrix ) if __name__ == '__main__': filename = 'testout.rst' restart_file = True args = parse_command_line_arguments() coordinates, velocities, dipoles, full_cell_matrix = read_pimaim_restart( filename ) assert( sum( args.atom_numbers ) == len( coordinates ) ) poscar = Poscar() poscar.cell = Cell( full_cell_matrix ) # TODO: possibly this needs transposing? poscar.atoms = args.labels poscar.atom_numbers = args.atom_numbers poscar.coordinate_type = 'Cartesian' poscar.coordinates = coordinates poscar.output()