Skip to content

Commit

Permalink
Merge pull request #366 from ISISNeutronMuon/fix-ase-converter-box
Browse files Browse the repository at this point in the history
Fix ase converter box
  • Loading branch information
ChiCheng45 authored Mar 19, 2024
2 parents a6ebde2 + 10754af commit aa4c152
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 48 deletions.
34 changes: 22 additions & 12 deletions MDANSE/Src/MDANSE/Framework/Converters/ASE.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from MDANSE.MolecularDynamics.Configuration import (
PeriodicBoxConfiguration,
PeriodicRealConfiguration,
RealConfiguration,
)
from MDANSE.MolecularDynamics.Trajectory import TrajectoryWriter
from MDANSE.MolecularDynamics.UnitCell import UnitCell
Expand Down Expand Up @@ -91,6 +92,7 @@ def initialize(self):
"""
Initialize the job.
"""
self._isPeriodic = None
self._atomicAliases = self.configuration["atom_aliases"]["value"]

# The number of steps of the analysis.
Expand All @@ -101,6 +103,7 @@ def initialize(self):
).toval("ps")

self.parse_first_step(self._atomicAliases)
print(f"isPeriodic after parse_first_step: {self._isPeriodic}")
self._start = 0

if self.numberOfSteps < 1:
Expand Down Expand Up @@ -133,25 +136,30 @@ def run_step(self, index):
try:
frame = self._input[index]
except TypeError:
frame = next(self._input)
else:
print("ASE using the slower way")
frame = read(self.configuration["trajectory_file"]["value"], index=index)
time = self._timeaxis[index]

unitCell = frame.cell.array
if self._isPeriodic:
unitCell = frame.cell.array
print(f"Unit cell from frame: {unitCell}")

unitCell *= measure(1.0, "ang").toval("nm")
unitCell = UnitCell(unitCell)
unitCell *= measure(1.0, "ang").toval("nm")
unitCell = UnitCell(unitCell)

coords = frame.get_positions()
coords *= measure(1.0, "ang").toval("nm")

if self._fractionalCoordinates:
conf = PeriodicBoxConfiguration(
if self._isPeriodic:
realConf = PeriodicRealConfiguration(
self._trajectory.chemical_system, coords, unitCell
)
realConf = conf.to_real_configuration()
else:
realConf = PeriodicRealConfiguration(
self._trajectory.chemical_system, coords, unitCell
realConf = RealConfiguration(
self._trajectory.chemical_system,
coords,
)

self._trajectory.chemical_system.configuration = realConf
Expand Down Expand Up @@ -190,23 +198,25 @@ def parse_first_step(self, mapping):
try:
self._input = ASETrajectory(self.configuration["trajectory_file"]["value"])
except:
self._input = iread(
self.configuration["trajectory_file"]["value"], index="[:]"
)
first_frame = read(self.configuration["trajectory_file"]["value"], index=0)
last_iterator = 0
generator = iread(self.configuration["trajectory_file"]["value"])
for _ in generator:
last_iterator += 1
generator.close()
self._input = iread(
self.configuration["trajectory_file"]["value"] # , index="[:]"
)
self._total_number_of_steps = last_iterator
else:
first_frame = self._input[0]
self._total_number_of_steps = len(self._input)

self._timeaxis = self._timestep * np.arange(self._total_number_of_steps)

self._fractionalCoordinates = np.all(first_frame.get_pbc())
if self._isPeriodic is None:
self._isPeriodic = np.all(first_frame.get_pbc())
print(f"PBC in first frame = {first_frame.get_pbc()}")

g = Graph()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ def info(self):
val.append("Number of steps:")
val.append("%s\n" % len(self._data))
val.append("Configuration:")
val.append(
"\tIs periodic: {}\n".format(
"unit_cell" in self._data.file["/configuration"]
)
)
val.append("\tIs periodic: {}\n".format("unit_cell" in self._data.file))
val.append("Variables:")
for k, v in self._data.file["/configuration"].items():
val.append("\t- {}: {}".format(k, v.shape))
Expand Down
66 changes: 36 additions & 30 deletions MDANSE_GUI/Src/MDANSE_GUI/MolecularViewer/MolecularViewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,36 +646,42 @@ def set_coordinates(self, frame: int, tolerance=0.04):
if self._cell_visible:
# update the unit cell
uc = self._reader.read_pbc(self._current_frame)
a = uc.a_vector
b = uc.b_vector
c = uc.c_vector
uc_points = vtk.vtkPoints()
uc_points.SetNumberOfPoints(8)
for i, v in enumerate([[0, 0, 0], a, b, c, a + b, a + c, b + c, a + b + c]):
x, y, z = v
uc_points.SetPoint(i, x, y, z)
self._uc_polydata.SetPoints(uc_points)

uc_lines = vtk.vtkCellArray()
for i, j in [
(0, 1),
(0, 2),
(0, 3),
(1, 4),
(1, 5),
(4, 7),
(2, 4),
(2, 6),
(5, 7),
(3, 5),
(3, 6),
(6, 7),
]:
line = vtk.vtkLine()
line.GetPointIds().SetId(0, i)
line.GetPointIds().SetId(1, j)
uc_lines.InsertNextCell(line)
self._uc_polydata.SetLines(uc_lines)
if uc is not None:
a = uc.a_vector
b = uc.b_vector
c = uc.c_vector
uc_points = vtk.vtkPoints()
uc_points.SetNumberOfPoints(8)
for i, v in enumerate(
[[0, 0, 0], a, b, c, a + b, a + c, b + c, a + b + c]
):
x, y, z = v
uc_points.SetPoint(i, x, y, z)
self._uc_polydata.SetPoints(uc_points)

uc_lines = vtk.vtkCellArray()
for i, j in [
(0, 1),
(0, 2),
(0, 3),
(1, 4),
(1, 5),
(4, 7),
(2, 4),
(2, 6),
(5, 7),
(3, 5),
(3, 6),
(6, 7),
]:
line = vtk.vtkLine()
line.GetPointIds().SetId(0, i)
line.GetPointIds().SetId(1, j)
uc_lines.InsertNextCell(line)
self._uc_polydata.SetLines(uc_lines)
else:
uc_lines = vtk.vtkCellArray()
self._uc_polydata.SetLines(uc_lines)
else:
uc_lines = vtk.vtkCellArray()
self._uc_polydata.SetLines(uc_lines)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ def read_frame(self, frame: int) -> "np.array":
return np.array(coords)

def read_pbc(self, frame: int) -> "np.array":
unit_cell = self._chemical_system.configuration.unit_cell
try:
unit_cell = self._chemical_system.configuration.unit_cell
except AttributeError:
unit_cell = None
return unit_cell

0 comments on commit aa4c152

Please sign in to comment.