Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement calculating and dumping deviation between DeepMD models #6

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions emle/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ def __init__(
plugin_path=".",
mm_charges=None,
deepmd_model=None,
deepmd_deviation=None,
rascal_model=None,
parm7=None,
qm_indices=None,
Expand Down Expand Up @@ -437,6 +438,10 @@ def __init__(
Path to the DeePMD model file to use for in vacuo calculations. This
must be specified if "deepmd" is the selected backend.

deepmd_deviation: str
Path to the file to dump max deviation between forces predicted
with the deepmd models.

rascal_model: str
Path to the Rascal model file used to apply delta-learning corrections
to the in vacuo energies and gradients computed by the backed.
Expand Down Expand Up @@ -745,6 +750,11 @@ def __init__(

# Store the list of model files, removing any duplicates.
self._deepmd_model = list(set(deepmd_model))
if len(self._deepmd_model == 1 and deepmd_deviation):
msg = "More that one deepmd model needed to calculate deviation"
_logger.error(msg)
raise IOError(msg)
self.deepmd_deviation = deepmd_deviation

# Initialise DeePMD backend attributes.
try:
Expand Down Expand Up @@ -1139,6 +1149,7 @@ def __init__(
"external_backend": None if external_backend is None else external_backend,
"mm_charges": None if mm_charges is None else self._mm_charges.tolist(),
"deepmd_model": deepmd_model,
"deepmd_deviation": deepmd_deviation,
"rascal_model": rascal_model,
"parm7": parm7,
"qm_indices": None if qm_indices is None else self._qm_indices,
Expand Down Expand Up @@ -2666,6 +2677,8 @@ def _run_deepmd(self, xyz, elements):
# Reshape to a frames x (natoms x 3) array.
xyz = xyz.reshape([1, -1])

f_list = []

# Run a calculation for each model and take the average.
for x, dp in enumerate(self._deepmd_potential):
# Work out the mapping between the elements and the type indices
Expand All @@ -2682,10 +2695,17 @@ def _run_deepmd(self, xyz, elements):

if x == 0:
energy, force, _ = dp.eval(xyz, cells=None, atom_types=atom_types)
f_list.append(force[0])
else:
e, f, _ = dp.eval(xyz, cells=None, atom_types=atom_types)
energy += e
force += f
f_list.append(f[0])

if self.deepmd_deviation:
max_f_std = _np.max(_np.std(_np.array(f_list), axis=0))
with open(self.deepmd_deviation, 'a') as deepmd_dev_file:
deepmd_dev_file.write(f'{max_f_std:12.5f}\n')

# Take averages and return. (Gradient equals minus the force.)
return (
Expand Down
Loading