Skip to content

Commit

Permalink
update analysis unit tests so that they check results against some ge…
Browse files Browse the repository at this point in the history
…nerated reference values
  • Loading branch information
ChiCheng45 committed Jan 28, 2025
1 parent df2fb58 commit 7aa2f46
Show file tree
Hide file tree
Showing 70 changed files with 465 additions and 93 deletions.
2 changes: 1 addition & 1 deletion MDANSE/Tests/UnitTests/Analysis/test_average_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def trajectory():


@pytest.mark.parametrize("output_unit,output_format", inputs)
def test_temperature(trajectory, output_unit, output_format):
def test_avg_structure(trajectory, output_unit, output_format):
temp_name = tempfile.mktemp()
parameters = {}
parameters["frames"] = (0, 10, 1)
Expand Down
164 changes: 151 additions & 13 deletions MDANSE/Tests/UnitTests/Analysis/test_dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import tempfile
import os
from os import path

import h5py
import numpy as np
import pytest

from MDANSE.Framework.InputData.HDFTrajectoryInputData import HDFTrajectoryInputData
Expand All @@ -15,13 +18,17 @@
"Data",
"short_trajectory_after_changes.mdt",
)

mdmc_traj = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Data",
"Ar_mdmc_h5md.h5",
)
result_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Results",
)


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -54,6 +61,20 @@ def test_vacf(trajectory, interp_order, normalise):
vacf.run(parameters, status=True)
assert path.exists(temp_name + ".mda")
assert path.isfile(temp_name + ".mda")

if normalise:
fname = f"vacf_{interp_order}_normalised.mda"
else:
fname = f"vacf_{interp_order}.mda"

result_file = os.path.join(result_dir, fname)

with h5py.File(temp_name + ".mda") as actual, h5py.File(result_file) as desired:
np.testing.assert_array_almost_equal(actual["/vacf_Cu"], desired["/vacf_Cu"])
np.testing.assert_array_almost_equal(actual["/vacf_S"], desired["/vacf_S"])
np.testing.assert_array_almost_equal(actual["/vacf_Sb"], desired["/vacf_Sb"])
np.testing.assert_array_almost_equal(actual["/vacf_total"], desired["/vacf_total"])

os.remove(temp_name + ".mda")
assert path.exists(temp_name + ".log")
assert path.isfile(temp_name + ".log")
Expand All @@ -71,6 +92,19 @@ def test_pps(trajectory):
pps.run(parameters, status=True)
assert path.exists(temp_name + ".mda")
assert path.isfile(temp_name + ".mda")

result_file = os.path.join(result_dir, "pps.mda")

with h5py.File(temp_name + ".mda") as actual, h5py.File(result_file) as desired:
np.testing.assert_array_almost_equal(actual["/pacf_Cu"], desired["/pacf_Cu"])
np.testing.assert_array_almost_equal(actual["/pacf_S"], desired["/pacf_S"])
np.testing.assert_array_almost_equal(actual["/pacf_Sb"], desired["/pacf_Sb"])
np.testing.assert_array_almost_equal(actual["/pacf_total"], desired["/pacf_total"])
np.testing.assert_array_almost_equal(actual["/pps_Cu"], desired["/pps_Cu"])
np.testing.assert_array_almost_equal(actual["/pps_S"], desired["/pps_S"])
np.testing.assert_array_almost_equal(actual["/pps_Sb"], desired["/pps_Sb"])
np.testing.assert_array_almost_equal(actual["/pps_total"], desired["/pps_total"])

os.remove(temp_name + ".mda")
assert path.exists(temp_name + ".log")
assert path.isfile(temp_name + ".log")
Expand Down Expand Up @@ -112,37 +146,141 @@ def parameters():

total_list = []

for tp in [short_traj, mdmc_traj]:
for tp in [("short_traj", short_traj), ("mdmc_traj", mdmc_traj)]:
for jt in [
# "AngularCorrelation",
# "GeneralAutoCorrelationFunction",
"CurrentCorrelationFunction",
"DensityOfStates",
"MeanSquareDisplacement",
"VelocityAutoCorrelationFunction",
"VanHoveFunctionDistinct",
"VanHoveFunctionSelf",
("DensityOfStates", {
"mdmc_traj": [
"dos_Ar",
"dos_total",
"vacf_Ar",
"vacf_total"
],
"short_traj": [
"dos_Cu",
"dos_S",
"dos_Sb",
"dos_total",
"vacf_Cu",
"vacf_S",
"vacf_Sb",
"vacf_total",
]
}),
("MeanSquareDisplacement", {
"mdmc_traj": [
"msd_Ar",
"msd_total",
],
"short_traj": [
"msd_Cu",
"msd_S",
"msd_Sb",
"msd_total",
]
}),
("VelocityAutoCorrelationFunction", {
"mdmc_traj": [
"vacf_Ar",
"vacf_total"
],
"short_traj": [
"vacf_Cu",
"vacf_S",
"vacf_Sb",
"vacf_total",
]
}),
("VanHoveFunctionDistinct", {
"mdmc_traj": [
"g(r,t)_inter_ArAr",
"g(r,t)_inter_total",
"g(r,t)_intra_ArAr",
"g(r,t)_intra_total",
"g(r,t)_total",
"g(r,t)_total_ArAr",
],
"short_traj": [
"g(r,t)_inter_CuCu",
"g(r,t)_inter_CuS",
"g(r,t)_inter_CuSb",
"g(r,t)_inter_SS",
"g(r,t)_inter_SSb",
"g(r,t)_inter_SbSb",
"g(r,t)_inter_total",
"g(r,t)_intra_CuCu",
"g(r,t)_intra_CuS",
"g(r,t)_intra_CuSb",
"g(r,t)_intra_SS",
"g(r,t)_intra_SSb",
"g(r,t)_intra_SbSb",
"g(r,t)_intra_total",
"g(r,t)_total_CuCu",
"g(r,t)_total_CuS",
"g(r,t)_total_CuSb",
"g(r,t)_total_SS",
"g(r,t)_total_SSb",
"g(r,t)_total_SbSb",
"g(r,t)_total",
]
}),
("VanHoveFunctionSelf", {
"mdmc_traj": [
"4_pi_r2_g(r,t)_Ar",
"4_pi_r2_g(r,t)_total",
"g(r,t)_Ar",
"g(r,t)_total",
],
"short_traj": [
"4_pi_r2_g(r,t)_Cu",
"4_pi_r2_g(r,t)_S",
"4_pi_r2_g(r,t)_Sb",
"4_pi_r2_g(r,t)_total",
"g(r,t)_Cu",
"g(r,t)_S",
"g(r,t)_Sb",
"g(r,t)_total",
]
}),
# "OrderParameter",
"PositionAutoCorrelationFunction",
("PositionAutoCorrelationFunction", {
"mdmc_traj": [
"pacf_Ar",
"pacf_total"
],
"short_traj": [
"pacf_Cu",
"pacf_S",
"pacf_Sb",
"pacf_total",
]
}),
]:
for rm in [("single-core", 1), ("multicore", -4)]:
for of in ["MDAFormat", "TextFormat"]:
total_list.append((tp, jt, rm, of))


@pytest.mark.parametrize("traj_path,job_type,running_mode,output_format", total_list)
@pytest.mark.parametrize("traj_info,job_info,running_mode,output_format", total_list)
def test_dynamics_analysis(
parameters, traj_path, job_type, running_mode, output_format
parameters, traj_info, job_info, running_mode, output_format
):
temp_name = tempfile.mktemp()
parameters["trajectory"] = traj_path
parameters["trajectory"] = traj_info[1]
parameters["running_mode"] = running_mode
parameters["output_files"] = (temp_name, (output_format,), "INFO")
job = IJob.create(job_type)
job = IJob.create(job_info[0])
job.run(parameters, status=True)
if output_format == "MDAFormat":
assert path.exists(temp_name + ".mda")
assert path.isfile(temp_name + ".mda")
result_file = os.path.join(result_dir, f"dynamics_analysis_{traj_info[0]}_{job_info[0]}.mda")

with h5py.File(temp_name + ".mda") as actual, h5py.File(result_file) as desired:
for key in job_info[1][traj_info[0]]:
np.testing.assert_array_almost_equal(actual[f"/{key}"], desired[f"/{key}"])

os.remove(temp_name + ".mda")
elif output_format == "TextFormat":
assert path.exists(temp_name + "_text.tar")
Expand Down
51 changes: 49 additions & 2 deletions MDANSE/Tests/UnitTests/Analysis/test_infrared.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import os
from os import path

import h5py
import numpy as np

from MDANSE.Framework.Jobs.IJob import IJob


Expand All @@ -14,6 +17,11 @@
"Data",
"named_molecules.mdt",
)
result_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Results",
)


def test_dacf_analysis():
Expand All @@ -23,13 +31,32 @@ def test_dacf_analysis():
parameters["output_files"] = (temp_name, ("MDAFormat",), "INFO")
parameters["running_mode"] = ("single-core", 1)
parameters["trajectory"] = short_traj
parameters["atom_charges"] = "{}"
parameters["atom_charges"] = (
'{"0": 0.72, "1": -0.36, "2": -0.36, "3": 0.72, "4": -0.36, '
'"5": -0.36, "6": 0.72, "7": -0.36, "8": -0.36, "9": 0.72, '
'"10": -0.36, "11": -0.36, "12": 0.72, "13": -0.36, "14": -0.36, '
'"15": 0.72, "16": -0.36, "17": -0.36, "18": 0.72, "19": -0.36, '
'"20": -0.36, "21": 0.72, "22": -0.36, "23": -0.36, "24": 0.72, '
'"25": -0.36, "26": -0.36, "27": 0.72, "28": -0.36, "29": -0.36, '
'"30": 0.72, "31": -0.36, "32": -0.36, "33": 0.72, "34": -0.36, '
'"35": -0.36, "36": 0.72, "37": -0.36, "38": -0.36, "39": 0.72, '
'"40": -0.36, "41": -0.36, "42": 0.72, "43": -0.36, "44": -0.36, '
'"45": 0.72, "46": -0.36, "47": -0.36, "48": 0.72, "49": -0.36, '
'"50": -0.36, "51": 0.72, "52": -0.36, "53": -0.36, "54": 0.72, '
'"55": -0.36, "56": -0.36, "57": 0.72, "58": -0.36, "59": -0.36}'
)
parameters["molecule_name"] = "InChI=1S/CO2/c2-1-3"
job = IJob.create("DipoleAutoCorrelationFunction")
job.run(parameters, status=True)
assert path.exists(temp_name + ".mda")
assert path.isfile(temp_name + ".mda")
result_file = os.path.join(result_dir, "dacf_analysis.mda")

with h5py.File(temp_name + ".mda") as actual, h5py.File(result_file) as desired:
np.testing.assert_array_almost_equal(actual["/dacf"], desired["/dacf"])

os.remove(temp_name + ".mda")

assert path.exists(temp_name + ".log")
assert path.isfile(temp_name + ".log")
os.remove(temp_name + ".log")
Expand All @@ -44,13 +71,33 @@ def test_ir_analysis():
parameters["derivative_order"] = 3
parameters["running_mode"] = ("single-core", 1)
parameters["trajectory"] = short_traj
parameters["atom_charges"] = "{}"
parameters["atom_charges"] = (
'{"0": 0.72, "1": -0.36, "2": -0.36, "3": 0.72, "4": -0.36, '
'"5": -0.36, "6": 0.72, "7": -0.36, "8": -0.36, "9": 0.72, '
'"10": -0.36, "11": -0.36, "12": 0.72, "13": -0.36, "14": -0.36, '
'"15": 0.72, "16": -0.36, "17": -0.36, "18": 0.72, "19": -0.36, '
'"20": -0.36, "21": 0.72, "22": -0.36, "23": -0.36, "24": 0.72, '
'"25": -0.36, "26": -0.36, "27": 0.72, "28": -0.36, "29": -0.36, '
'"30": 0.72, "31": -0.36, "32": -0.36, "33": 0.72, "34": -0.36, '
'"35": -0.36, "36": 0.72, "37": -0.36, "38": -0.36, "39": 0.72, '
'"40": -0.36, "41": -0.36, "42": 0.72, "43": -0.36, "44": -0.36, '
'"45": 0.72, "46": -0.36, "47": -0.36, "48": 0.72, "49": -0.36, '
'"50": -0.36, "51": 0.72, "52": -0.36, "53": -0.36, "54": 0.72, '
'"55": -0.36, "56": -0.36, "57": 0.72, "58": -0.36, "59": -0.36}'
)
parameters["molecule_name"] = "InChI=1S/CO2/c2-1-3"
job = IJob.create("Infrared")
job.run(parameters, status=True)
assert path.exists(temp_name + ".mda")
assert path.isfile(temp_name + ".mda")
result_file = os.path.join(result_dir, "ir_analysis.mda")

with h5py.File(temp_name + ".mda") as actual, h5py.File(result_file) as desired:
np.testing.assert_array_almost_equal(actual["/ddacf"], desired["/ddacf"])
np.testing.assert_array_almost_equal(actual["/ir"], desired["/ir"])

os.remove(temp_name + ".mda")

assert path.exists(temp_name + ".log")
assert path.isfile(temp_name + ".log")
os.remove(temp_name + ".log")
22 changes: 16 additions & 6 deletions MDANSE/Tests/UnitTests/Analysis/test_mdmc_h5md.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"Data",
"Ar_mdmc_h5md.h5",
)
result_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Results",
)


@pytest.fixture(scope="module")
Expand All @@ -25,22 +30,27 @@ def trajectory():


@pytest.mark.parametrize("interp_order", [1, 3])
def test_temperature_nonzero(trajectory, interp_order):
def test_h5md_temperature(trajectory, interp_order):
pos = trajectory._data.coordinates(0)
print(f"Coordinates span: {pos.min()}, {pos.max()}")
print(f"Trajectory length: {len(trajectory.trajectory)}")
print(f"Positions array shape: {trajectory.trajectory.variable('position').shape}")
temp_name = tempfile.mktemp()
parameters = {}
parameters["frames"] = (0, 10, 1)
parameters["frames"] = (0, 39, 1)
parameters["interpolation_order"] = interp_order
parameters["output_files"] = (temp_name, ("MDAFormat",), "INFO")
parameters["running_mode"] = ("single-core",)
parameters["trajectory"] = short_traj
temp = IJob.create("Temperature")
temp.run(parameters, status=True)
with h5py.File(temp_name + ".mda") as results:
print(results.keys())
temperature = np.array(results["/temperature"])
result_file = os.path.join(
result_dir, f"h5md_temperature_{interp_order}.mda")

with h5py.File(temp_name + ".mda") as actual, h5py.File(result_file) as desired:
np.testing.assert_array_almost_equal(actual["/kinetic_energy"], desired["/kinetic_energy"])
np.testing.assert_array_almost_equal(actual["/temperature"], desired["/temperature"])
np.testing.assert_array_almost_equal(actual["/avg_kinetic_energy"], desired["/avg_kinetic_energy"])
np.testing.assert_array_almost_equal(actual["/avg_temperature"], desired["/avg_temperature"])

os.remove(temp_name + ".mda")
assert np.all(temperature > 0.0)
Loading

0 comments on commit 7aa2f46

Please sign in to comment.