-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_md.py
217 lines (191 loc) · 6.27 KB
/
test_md.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""Tests for geometry optimisation calculation."""
from pathlib import Path
import subprocess
from ase.build import bulk
from ase.io import read, write
import pytest
from aiida.common import datastructures
from aiida.engine import run_get_node
from aiida.orm import Dict, Str, StructureData
from aiida.plugins import CalculationFactory
from aiida_mlip.data.config import JanusConfigfile
from aiida_mlip.data.model import ModelData
def test_MD(fixture_sandbox, generate_calc_job, janus_code, model_folder):
"""Test generating MD calculation job."""
entry_point_name = "mlip.md"
model_file = model_folder / "mace_mp_small.model"
inputs = {
"metadata": {"options": {"resources": {"num_machines": 1}}},
"code": janus_code,
"arch": Str("mace"),
"precision": Str("float64"),
"struct": StructureData(ase=bulk("NaCl", "rocksalt", 5.63)),
"model": ModelData.from_local(model_file, architecture="mace"),
"device": Str("cpu"),
"ensemble": Str("nve"),
"md_kwargs": Dict(
{
"temp": 300.0,
"steps": 4,
"traj-every": 1,
"restart-every": 3,
"stats-every": 1,
}
),
}
calc_info = generate_calc_job(fixture_sandbox, entry_point_name, inputs)
cmdline_params = [
"md",
"--arch",
"mace",
"--struct",
"aiida.xyz",
"--device",
"cpu",
"--log",
"aiida.log",
"--summary",
"md_summary.yml",
"--calc-kwargs",
"{'default_dtype': 'float64', 'model': 'mlff.model'}",
"--ensemble",
"nve",
"--temp",
300.0,
"--steps",
4,
"--traj-every",
1,
"--stats-every",
1,
"--restart-every",
3,
"--traj-file",
"aiida-traj.xyz",
"--stats-file",
"aiida-stats.dat",
]
retrieve_list = [
calc_info.uuid,
"aiida.log",
"aiida-stdout.txt",
"aiida-traj.xyz",
"aiida-stats.dat",
"md_summary.yml",
]
# Check the attributes of the returned `CalcInfo`
assert sorted(fixture_sandbox.get_content_list()) == sorted(
["aiida.xyz", "mlff.model"]
)
assert isinstance(calc_info, datastructures.CalcInfo)
assert isinstance(calc_info.codes_info[0], datastructures.CodeInfo)
assert len(calc_info.codes_info[0].cmdline_params) == len(cmdline_params)
assert sorted(map(str, calc_info.codes_info[0].cmdline_params)) == sorted(
map(str, cmdline_params)
)
assert sorted(calc_info.retrieve_list) == sorted(retrieve_list)
def test_MD_with_config(
fixture_sandbox, generate_calc_job, janus_code, model_folder, config_folder
):
"""Test generating MD calculation job."""
# Create a temporary cif file to use as input
nacl = bulk("NaCl", "rocksalt", a=5.63)
write("NaCl.cif", nacl)
entry_point_name = "mlip.md"
model_file = model_folder / "mace_mp_small.model"
inputs = {
"code": janus_code,
"model": ModelData.from_local(file=model_file, architecture="mace"),
"metadata": {"options": {"resources": {"num_machines": 1}}},
"config": JanusConfigfile(config_folder / "config_janus_md.yaml"),
}
calc_info = generate_calc_job(fixture_sandbox, entry_point_name, inputs)
cmdline_params = [
"md",
"--struct",
"aiida.xyz",
"--log",
"aiida.log",
"--arch",
"mace",
"--calc-kwargs",
"{'model': 'mlff.model'}",
"--config",
"config.yaml",
"--ensemble",
"nvt",
"--summary",
"md_summary.yml",
"--traj-file",
"aiida-traj.xyz",
"--stats-file",
"aiida-stats.dat",
]
retrieve_list = [
calc_info.uuid,
"aiida.log",
"aiida-stdout.txt",
"aiida-traj.xyz",
"aiida-stats.dat",
"md_summary.yml",
]
# Check the attributes of the returned `CalcInfo`
assert sorted(fixture_sandbox.get_content_list()) == sorted(
[
"aiida.xyz",
"config.yaml",
"mlff.model",
]
)
assert isinstance(calc_info, datastructures.CalcInfo)
assert isinstance(calc_info.codes_info[0], datastructures.CodeInfo)
assert len(calc_info.codes_info[0].cmdline_params) == len(cmdline_params)
assert sorted(map(str, calc_info.codes_info[0].cmdline_params)) == sorted(
map(str, cmdline_params)
)
assert sorted(calc_info.retrieve_list) == sorted(retrieve_list)
Path("NaCl.cif").unlink()
def test_run_md(model_folder, structure_folder, janus_code):
"""Test running molecular dynamics calculation"""
model_file = model_folder / "mace_mp_small.model"
structure_file = structure_folder / "NaCl.cif"
inputs = {
"metadata": {"options": {"resources": {"num_machines": 1}}},
"code": janus_code,
"arch": Str("mace"),
"precision": Str("float64"),
"struct": StructureData(ase=read(structure_file)),
"model": ModelData.from_local(model_file, architecture="mace"),
"device": Str("cpu"),
"ensemble": Str("nve"),
"md_kwargs": Dict(
{
"temp": 300.0,
"steps": 3,
"traj-every": 1,
"restart-every": 3,
"stats-every": 1,
}
),
}
MDCalculation = CalculationFactory("mlip.md")
result, node = run_get_node(MDCalculation, **inputs)
assert "final_structure" in result
assert "traj_output" in result
assert "traj_file" in result
assert "results_dict" in result
# obtained_res = result["results_dict"].get_dict()
assert result["traj_output"].numsteps == 4
assert node.outputs.final_structure.get_cell_volume() == pytest.approx(
179.406
) # check
def test_example_md(example_path):
"""
Test function to run md calculation through the use of the example file provided.
"""
example_file_path = example_path / "submit_md.py"
command = ["verdi", "run", example_file_path, "janus@localhost"]
# Execute the command
result = subprocess.run(command, capture_output=True, text=True, check=False)
assert result.stderr == ""
assert result.returncode == 0