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

Update the CP2K configurator and widget so that the velocity file is optional #668

Open
wants to merge 4 commits into
base: protos
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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,7 +309,10 @@ 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._velFile.close()

self._cellFile.close()
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