Skip to content

Commit

Permalink
Merge pull request #430 from ISISNeutronMuon/lammps-converter-fix
Browse files Browse the repository at this point in the history
Lammps converter fix
  • Loading branch information
ChiCheng45 authored May 3, 2024
2 parents df7c1ad + a83659c commit 37eac8e
Show file tree
Hide file tree
Showing 31 changed files with 23,542 additions and 260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AseInputFileConfigurator(InputFileConfigurator):
_default = ""
_allowed_formats = ["guess"] + [str(x) for x in all_formats.keys()]

def __init__(self, name, wildcard="All files|*", **kwargs):
def __init__(self, name, wildcard="All files (*)", **kwargs):
"""
Initializes the configurator object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ class LAMMPSConfigFileError(Error):
pass


def parse_unit_cell(inputs):

unit_cell = np.zeros(9)

xlo, xhi, xy = inputs[0], inputs[1], inputs[2]
ylo, yhi, xz = inputs[3], inputs[4], inputs[5]
zlo, zhi, yz = inputs[6], inputs[7], inputs[8]
# The ax component.
unit_cell[0] = xhi - xlo

# The bx and by components.
unit_cell[3] = xy
unit_cell[4] = yhi - ylo

# The cx, cy and cz components.
unit_cell[6] = xz
unit_cell[7] = yz
unit_cell[8] = zhi - zlo

unit_cell = np.reshape(unit_cell, (3, 3))

return unit_cell


class ConfigFileConfigurator(FileWithAtomDataConfigurator):

def parse(self):
Expand All @@ -41,15 +65,39 @@ def parse(self):

self["elements"] = []

unit = open(self._filename, "r")
lines = []
for l in unit.readlines():
l = l.strip()
if l:
lines.append(l)
unit.close()
self["atom_types"] = []

self["unit_cell"] = np.zeros((3, 3))

with open(self._filename, "r") as source_file:
lines = []
for l in source_file.readlines():
l = l.strip()
if l:
lines.append(l)

for i, line in enumerate(lines):
toks = line.split()

if "xlo" in line and "xhi" in line:
try:
x_inputs = [float(x) for x in toks[0:3]]
except:
xspan = float(toks[1]) - float(toks[0])
self["unit_cell"][0, 0] = xspan
elif "ylo" in line and "yhi" in line:
try:
y_inputs = [float(x) for x in toks[0:3]]
except:
yspan = float(toks[1]) - float(toks[0])
self["unit_cell"][1, 1] = yspan
elif "zlo" in line and "zhi" in line:
try:
z_inputs = [float(x) for x in toks[0:3]]
except:
zspan = float(toks[1]) - float(toks[0])
self["unit_cell"][2, 2] = zspan

if self["n_atoms"] is None:
m = re.match("^\s*(\d+)\s*atoms\s*$", line, re.I)
if m:
Expand All @@ -74,7 +122,7 @@ def parse(self):
for j in range(1, self["n_atom_types"] + 1):
data_line = (
lines[i + j].strip().split("#")[0]
) # Remove commentary if any
) # Remove comments, if present
idx, mass = data_line.split()[0:2]
idx = int(idx)
mass = float(mass)
Expand All @@ -89,6 +137,22 @@ def parse(self):
self["bonds"].append([at1, at2])
self["bonds"] = np.array(self["bonds"], dtype=np.int32)

if re.match("^\s*Atoms\s*$", line):
if self["n_atoms"] is not None:
self["atom_types"] = self["n_atoms"] * [0]
for j in range(self["n_atoms"]):
atoks = lines[i + j + 1].split()
self["atom_types"][j] = int(atoks[2])

if np.trace(np.abs(self["unit_cell"])) < 1e-8:
# print(f"Concatenated: {np.concatenate([x_inputs, y_inputs, z_inputs])}")
try:
self["unit_cell"] = parse_unit_cell(
np.concatenate([x_inputs, y_inputs, z_inputs])
)
except:
print(f"LAMMPS ConfigFileConfigurator failed to find a unit cell")

def get_atom_labels(self) -> list[AtomLabel]:
"""
Returns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def configure(self, filepath: str) -> None:
return
try:
self.parse()
except Exception:
except Exception as e:
self.error_status = "File parsing error"

@abstractmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(
self,
name,
variables=None,
wildcard="MDA analysis results (*.mda)|*.mda|HDF5 files (*.h5)|*.h5|All files|*",
wildcard="MDA analysis results (*.mda);;HDF5 files (*.h5);;All files (*)",
**kwargs,
):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class InputFileConfigurator(IConfigurator):

_default = ""

def __init__(self, name, wildcard="All files|*", **kwargs):
def __init__(self, name, wildcard="All files (*)", **kwargs):
"""
Initializes the configurator object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MDMCTrajectoryConfigurator(IConfigurator):

_default = None

def __init__(self, name, wildcard="All files|*", **kwargs):
def __init__(self, name, wildcard="All files (*)", **kwargs):
"""
Initializes the configurator object.
Expand Down
2 changes: 1 addition & 1 deletion MDANSE/Src/MDANSE/Framework/Converters/CASTEP.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CASTEP(Converter):
settings["castep_file"] = (
"MDFileConfigurator",
{
"wildcard": "MD files (*.md)|*.md|All files|*",
"wildcard": "MD files (*.md);;All files (*)",
"default": "INPUT_FILENAME.md",
"label": "Input MD file",
},
Expand Down
6 changes: 3 additions & 3 deletions MDANSE/Src/MDANSE/Framework/Converters/CP2K.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ class CP2K(Converter):
settings["pos_file"] = (
"XYZFileConfigurator",
{
"wildcard": "XYZ files (*.xyz)|*.xyz|All files|*",
"wildcard": "XYZ files (*.xyz);;All files (*)",
"default": "INPUT_FILENAME.xyz",
"label": "Positions file (XYZ)",
},
)
settings["vel_file"] = (
"XYZFileConfigurator",
{
"wildcard": "XYZ files (*.xyz)|*.xyz|All files|*",
"wildcard": "XYZ files (*.xyz);;All files (*)",
"default": "INPUT_FILENAME.xyz",
"optional": True,
"label": "Velocity file (XYZ, optional)",
Expand All @@ -128,7 +128,7 @@ class CP2K(Converter):
settings["cell_file"] = (
"InputFileConfigurator",
{
"wildcard": "Cell files (*.cell)|*.cell|All files|*",
"wildcard": "Cell files (*.cell);;All files (*)",
"default": "INPUT_FILENAME.cell",
"label": "CP2K unit cell file (.cell)",
},
Expand Down
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Framework/Converters/DCD.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,15 @@ class DCD(Converter):
settings["pdb_file"] = (
"InputFileConfigurator",
{
"wildcard": "PDB files (*.pdb)|*.pdb|All files|*",
"wildcard": "PDB files (*.pdb);;All files (*)",
"default": "INPUT_FILENAME.pdb",
"label": "Input PDB file",
},
)
settings["dcd_file"] = (
"InputFileConfigurator",
{
"wildcard": "DCD files (*.dcd)|*.dcd|All files|*",
"wildcard": "DCD files (*.dcd);;All files (*)",
"default": "INPUT_FILENAME.dcd",
"label": "Input DCD file",
},
Expand Down
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Framework/Converters/DFTB.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ class DFTB(Forcite):
settings["xtd_file"] = (
"XTDFileConfigurator",
{
"wildcard": "XTD files (*.xtd)|*.xtd|All files|*",
"wildcard": "XTD files (*.xtd);;All files (*)",
"default": "INPUT_FILENAME.xtd",
"label": "The XTD file",
},
)
settings["trj_file"] = (
"InputFileConfigurator",
{
"wildcard": "TRJ files (*.trj)|*.trj|All files|*",
"wildcard": "TRJ files (*.trj);;All files (*)",
"default": "INPUT_FILENAME.trj",
"label": "The TRJ file",
},
Expand Down
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Framework/Converters/DL_POLY.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,15 @@ class DL_POLY(Converter):
settings["field_file"] = (
"FieldFileConfigurator",
{
"wildcard": "FIELD files (FIELD*)|FIELD*|All files|*",
"wildcard": "FIELD files (FIELD*);;All files (*)",
"default": "INPUT_FILENAME",
"label": "Input FIELD file",
},
)
settings["history_file"] = (
"InputFileConfigurator",
{
"wildcard": "HISTORY files (HISTORY*)|HISTORY*|All files|*",
"wildcard": "HISTORY files (HISTORY*);;All files (*)",
"default": "INPUT_FILENAME",
"label": "Input HISTORY file",
},
Expand Down
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Framework/Converters/Discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,15 @@ class Discover(Converter):
settings["xtd_file"] = (
"XTDFileConfigurator",
{
"wildcard": "XTD files (*.xtd)|*.xtd|All files|*",
"wildcard": "XTD files (*.xtd);;All files (*)",
"default": "INPUT_FILENAME.xtd",
"label": "Input XTD file",
},
)
settings["his_file"] = (
"InputFileConfigurator",
{
"wildcard": "HIS files (*.his)|*.his|All files|*",
"wildcard": "HIS files (*.his);;All files (*)",
"default": "INPUT_FILENAME.his",
"label": "Input HIS file",
},
Expand Down
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Framework/Converters/Forcite.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,15 @@ class Forcite(Converter):
settings["xtd_file"] = (
"XTDFileConfigurator",
{
"wildcard": "XTD files (*.xtd)|*.xtd|All files|*",
"wildcard": "XTD files (*.xtd);;All files (*)",
"default": "INPUT_FILENAME.xtd",
"label": "Input XTD file",
},
)
settings["trj_file"] = (
"InputFileConfigurator",
{
"wildcard": "TRJ files (*.trj)|*.trj|All files|*",
"wildcard": "TRJ files (*.trj);;All files (*)",
"default": "INPUT_FILENAME.trj",
"label": "Input TRJ file",
},
Expand Down
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Framework/Converters/Gromacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ class Gromacs(Converter):
settings["pdb_file"] = (
"InputFileConfigurator",
{
"wildcard": "PDB files (*.pdb)|*.pdb|All files|*",
"wildcard": "PDB files (*.pdb);;All files (*)",
"default": "INPUT_FILENAME.pdb",
"label": "Input PDB file",
},
)
settings["xtc_file"] = (
"InputFileConfigurator",
{
"wildcard": "XTC files (*.xtc)|*.xtc|TRR files (*.trr)|*.trr|All files|*",
"wildcard": "XTC files (*.xtc);;TRR files (*.trr);;All files (*)",
"default": "INPUT_FILENAME.xtc",
"label": "xtc or trr file",
},
Expand Down
Loading

0 comments on commit 37eac8e

Please sign in to comment.