diff --git a/examples/advanced/ex10_convert_numpy_openpmd.py b/examples/advanced/ex10_convert_numpy_openpmd.py index 98128b63..45369ff8 100644 --- a/examples/advanced/ex10_convert_numpy_openpmd.py +++ b/examples/advanced/ex10_convert_numpy_openpmd.py @@ -29,7 +29,7 @@ descriptor_save_path="./", target_save_path="./", additional_info_save_path="./", - naming_scheme="converted_from_numpy_*.bp4", + naming_scheme="converted_from_numpy_*.bp5", descriptor_calculation_kwargs={"working_directory": "./"}, ) @@ -40,11 +40,11 @@ for snapshot in range(2): data_converter.add_snapshot( descriptor_input_type="openpmd", - descriptor_input_path="converted_from_numpy_{}.in.bp4".format( + descriptor_input_path="converted_from_numpy_{}.in.bp5".format( snapshot ), target_input_type="openpmd", - target_input_path="converted_from_numpy_{}.out.bp4".format(snapshot), + target_input_path="converted_from_numpy_{}.out.bp5".format(snapshot), additional_info_input_type=None, additional_info_input_path=None, target_units=None, diff --git a/test/complete_interfaces_test.py b/test/complete_interfaces_test.py index 8aa7da85..4ceb691d 100644 --- a/test/complete_interfaces_test.py +++ b/test/complete_interfaces_test.py @@ -89,6 +89,77 @@ def test_openpmd_io(self): rtol=accuracy_fine, ) + @pytest.mark.skipif( + importlib.util.find_spec("openpmd_api") is None, + reason="No OpenPMD found on this machine, skipping " "test.", + ) + def test_convert_numpy_openpmd(self): + parameters = mala.Parameters() + parameters.descriptors.descriptors_contain_xyz = False + + data_converter = mala.DataConverter(parameters) + for snapshot in range(2): + data_converter.add_snapshot( + descriptor_input_type="numpy", + descriptor_input_path=os.path.join( + data_path, "Be_snapshot{}.in.npy".format(snapshot) + ), + target_input_type="numpy", + target_input_path=os.path.join( + data_path, "Be_snapshot{}.out.npy".format(snapshot) + ), + additional_info_input_type=None, + additional_info_input_path=None, + target_units=None, + ) + + data_converter.convert_snapshots( + descriptor_save_path="./", + target_save_path="./", + additional_info_save_path="./", + naming_scheme="converted_from_numpy_*.bp5", + descriptor_calculation_kwargs={"working_directory": "./"}, + ) + + # Convert those files back to Numpy to verify the data stays the same. + + data_converter = mala.DataConverter(parameters) + + for snapshot in range(2): + data_converter.add_snapshot( + descriptor_input_type="openpmd", + descriptor_input_path="converted_from_numpy_{}.in.bp5".format( + snapshot + ), + target_input_type="openpmd", + target_input_path="converted_from_numpy_{}.out.bp5".format(snapshot), + additional_info_input_type=None, + additional_info_input_path=None, + target_units=None, + ) + + data_converter.convert_snapshots( + descriptor_save_path="./", + target_save_path="./", + additional_info_save_path="./", + naming_scheme="verify_against_original_numpy_data_*.npy", + descriptor_calculation_kwargs={"working_directory": "./"}, + ) + + for snapshot in range(2): + for i_o in ["in", "out"]: + original = os.path.join( + data_path, "Be_snapshot{}.{}.npy".format(snapshot, i_o) + ) + roundtrip = "verify_against_original_numpy_data_{}.{}.npy".format( + snapshot, i_o + ) + import numpy as np + + original_a = np.load(original) + roundtrip_a = np.load(roundtrip) + np.testing.assert_allclose(original_a, roundtrip_a) + @pytest.mark.skipif( importlib.util.find_spec("total_energy") is None or importlib.util.find_spec("lammps") is None,