Skip to content

[WIP] Snapshot/System converters #2

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

Open
wants to merge 1 commit into
base: master
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
1 change: 1 addition & 0 deletions gutis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.coveragerc
73 changes: 69 additions & 4 deletions gutis/converters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
"""
Functions to convert between OPS object and PyRETIS objects.
"""
import pyretis
import openpathsampling as paths
import simtk.unit as unit
import numpy as np


def ops_box_vectors_to_pyretis(box_vectors):
"""Convert OPS box_vectors (square numpy array) to PyRETIS ``Box``

Parameters
----------
box_vectors: :class:`numpy.array` shape (n_dim, n_dim)
input box vectors from OpenPathSampling

Returns
-------
:class:`pyretis.core.Box`
PyRETIS ``Box`` object for the box vectors
"""
if isinstance(box_vectors, unit.Quantity):
box_vectors = box_vectors._value
for i in range(len(box_vectors)):
for j in range(len(box_vectors[i])):
if i != j and box_vectors[i][j] != 0:
raise ValueError("PyRETIS requires rectangular boxes.")
box = pyretis.core.Box(size=[[0.0, box_vectors[i, i]]
for i in range(len(box_vectors))])
return box


def pyretis_box_to_ops(box):
"""Convert PyRETIS ``Box`` to OPS box_vectors (numpy array)
"""
box_vectors = np.zeros(shape=(box.dim, box.dim))
for i in range(box.dim):
box_vectors[i, i] = box.length[i]

# TODO: may need to add units for OpenMM
return box_vectors


def ops_snapshot_to_pyretis(snapshot):
"""Convert an OPS ``Snapshot`` to a PyRETIS ``System``
Expand All @@ -12,12 +52,37 @@ def ops_snapshot_to_pyretis(snapshot):

Returns
-------
:class:`pyretis.System`
:class:`pyretis.core.System`
output system
"""
pass
box = ops_box_vectors_to_pyretis(snapshot.box_vectors)
system = pyretis.core.System(box=box)
load_system_with_snapshot(system, snapshot, update_box=False)
return system

def pyretis_system_to_ops(system):
pass

def load_system_with_snapshot(system, snapshot, update_box=True):
"""Load a PyRETIS ``System`` object with data from an OPS ``Snapshot``
"""
if update_box:
box = ops_box_vectors_to_pyretis(snapshot.box_vectors)
system.box = box

pos = snapshot.coordinates
vel = snapshot.velocities

# strip units if present
if isinstance(pos, unit.Quantity):
pos = pos._value
if isinstance(vel, unit.Quantity):
vel = vel._value

particles = pyretis.core.Particles(dim=system.box.dim)
particles.pos = pos
particles.vel = vel

system.particles = particles


def pyretis_system_to_ops(system):
pass
1 change: 1 addition & 0 deletions gutis/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.coveragerc