Skip to content

Commit

Permalink
Merge pull request #668 from ISISNeutronMuon/chi/cp2k-optional-vel-file
Browse files Browse the repository at this point in the history
Update the CP2K configurator and widget so that the velocity file is optional
  • Loading branch information
ChiCheng45 authored Feb 24, 2025
2 parents 128b78b + 9a5c057 commit bfbb71d
Show file tree
Hide file tree
Showing 10 changed files with 4,212 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This file is part of MDANSE.
#
# MDANSE is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from .XYZFileConfigurator import XYZFileConfigurator


class OptionalXYZFileConfigurator(XYZFileConfigurator):
def configure(self, filepath: str) -> None:
"""Configure the XYZ file if the filepath is not empty.
Parameters
----------
filepath : str
THe filepath of the xyz file.
"""
if not filepath:
self._original_input = filepath
self["value"] = filepath
self["filename"] = filepath
self.error_status = "OK"
return

super().configure(filepath)
42 changes: 35 additions & 7 deletions MDANSE/Src/MDANSE/Framework/Converters/CP2K.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,21 @@ class CP2K(Converter):
},
)
settings["vel_file"] = (
"XYZFileConfigurator",
"OptionalXYZFileConfigurator",
{
"wildcard": "XYZ files (*.xyz);;All files (*)",
"default": "INPUT_FILENAME.xyz",
"optional": True,
"default": "",
"label": "Velocity file (XYZ, optional)",
},
)
settings["force_file"] = (
"OptionalXYZFileConfigurator",
{
"wildcard": "XYZ files (*.xyz);;All files (*)",
"default": "",
"label": "Force file (XYZ, optional)",
},
)
settings["cell_file"] = (
"InputFileConfigurator",
{
Expand Down Expand Up @@ -169,7 +176,7 @@ def initialize(self):
self._atomicAliases = self.configuration["atom_aliases"]["value"]
self._xyzFile = self.configuration["pos_file"]

if self.configuration["vel_file"]:
if self.configuration["vel_file"]["value"]:
self._velFile = self.configuration["vel_file"]
if abs(self._xyzFile["time_step"] - self._velFile["time_step"]) > 1.0e-09:
raise CP2KConverterError(
Expand All @@ -181,6 +188,18 @@ def initialize(self):
"Inconsistent number of frames between pos and vel files"
)

if self.configuration["force_file"]["value"]:
self._forceFile = self.configuration["force_file"]
if abs(self._xyzFile["time_step"] - self._forceFile["time_step"]) > 1.0e-09:
raise CP2KConverterError(
"Inconsistent time step between pos and force files"
)

if self._xyzFile["n_frames"] != self._forceFile["n_frames"]:
raise CP2KConverterError(
"Inconsistent number of frames between pos and force files"
)

self._cellFile = CellFile(self.configuration["cell_file"]["filename"])

if abs(self._cellFile["time_step"] - self._xyzFile["time_step"]) > 1.0e-09:
Expand Down Expand Up @@ -216,8 +235,10 @@ def initialize(self):
)

data_to_be_written = ["configuration", "time"]
if self.configuration["vel_file"]:
if self.configuration["vel_file"]["value"]:
data_to_be_written.append("velocities")
if self.configuration["force_file"]["value"]:
data_to_be_written.append("forces")

def run_step(self, index):
"""Runs a single step of the job.
Expand All @@ -236,10 +257,14 @@ def run_step(self, index):
)

variables = {}
if self.configuration["vel_file"]:
if self.configuration["vel_file"]["value"]:
variables["velocities"] = self._velFile.read_step(index) * measure(
1.0, iunit="ang/fs"
).toval("nm/ps")
if self.configuration["force_file"]["value"]:
variables["forces"] = self._forceFile.read_step(index) * measure(
1.0, iunit="uma ang / fs2"
).toval("uma nm / ps2")

real_conf = PeriodicRealConfiguration(
self._trajectory.chemical_system, coords, unitcell, **variables
Expand Down Expand Up @@ -284,9 +309,12 @@ def finalize(self):

self._xyzFile.close()

if self.configuration["vel_file"]:
if self.configuration["vel_file"]["value"]:
self._velFile.close()

if self.configuration["force_file"]["value"]:
self._forceFile.close()

self._cellFile.close()

# Close the output trajectory.
Expand Down
1 change: 0 additions & 1 deletion MDANSE/Src/MDANSE/Mathematics/Arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def assign_weights(
dim = key.count("%s")

for k in values.keys() & matches:

if symmetric:
permutations = set(itertools.permutations(matches[k], r=dim))
w = sum(weights[p] for p in permutations)
Expand Down
Binary file added MDANSE/Tests/UnitTests/Converted/cp2k_srtio3.mdt
Binary file not shown.
11 changes: 11 additions & 0 deletions MDANSE/Tests/UnitTests/Data/SrTiO3_MD-cell-1.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Step Time [fs] Ax [Angstrom] Ay [Angstrom] Az [Angstrom] Bx [Angstrom] By [Angstrom] Bz [Angstrom] Cx [Angstrom] Cy [Angstrom] Cz [Angstrom] Volume [Angstrom^3]
2 0.6 11.715 0.0 0.0 0.0 11.715 0.0 0.0 0.0 11.715 1607.780950875
3 1.2 11.715 0.0 0.0 0.0 11.715 0.0 0.0 0.0 11.715 1607.780950875
4 1.8 11.715 0.0 0.0 0.0 11.715 0.0 0.0 0.0 11.715 1607.780950875
5 2.4 11.715 0.0 0.0 0.0 11.715 0.0 0.0 0.0 11.715 1607.780950875
6 3.0 11.715 0.0 0.0 0.0 11.715 0.0 0.0 0.0 11.715 1607.780950875
7 3.6 11.715 0.0 0.0 0.0 11.715 0.0 0.0 0.0 11.715 1607.780950875
8 4.2 11.715 0.0 0.0 0.0 11.715 0.0 0.0 0.0 11.715 1607.780950875
9 4.8 11.715 0.0 0.0 0.0 11.715 0.0 0.0 0.0 11.715 1607.780950875
10 5.4 11.715 0.0 0.0 0.0 11.715 0.0 0.0 0.0 11.715 1607.780950875
11 6.0 11.715 0.0 0.0 0.0 11.715 0.0 0.0 0.0 11.715 1607.780950875
Loading

0 comments on commit bfbb71d

Please sign in to comment.