Skip to content

Commit

Permalink
Merge pull request #118 from chrisjonesBSU/add-errors
Browse files Browse the repository at this point in the history
Add type check for forcefield parameter in `Simulation`
  • Loading branch information
chrisjonesBSU authored Mar 8, 2024
2 parents e4d8328 + 5df13f7 commit 62f8363
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
13 changes: 13 additions & 0 deletions flowermd/base/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inspect
import pickle
import warnings
from collections.abc import Iterable

import gsd.hoomd
import hoomd
Expand Down Expand Up @@ -67,6 +68,18 @@ def __init__(
log_file_name="sim_data.txt",
thermostat=HOOMDThermostats.MTTK,
):
if not isinstance(forcefield, Iterable) or isinstance(forcefield, str):
raise ValueError(
"forcefield must be a sequence of "
"hoomd.md.force.Force objects."
)
else:
for obj in forcefield:
if not isinstance(obj, hoomd.md.force.Force):
raise ValueError(
"forcefield must be a sequence of "
"hoomd.md.force.Force objects."
)
super(Simulation, self).__init__(device, seed)
self.initial_state = initial_state
self._forcefield = forcefield
Expand Down
17 changes: 17 additions & 0 deletions flowermd/tests/base/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import unyt as u

from flowermd import Simulation
from flowermd.library import OPLS_AA_PPS
from flowermd.tests import BaseTest
from flowermd.utils import ( # get_target_box_number_density,
get_target_box_mass_density,
Expand Down Expand Up @@ -358,6 +359,22 @@ def test_gsd_logger(self, benzene_system):
os.remove("trajectory.gsd")
os.remove("sim_data.txt")

def test_bad_ff(self, benzene_system):
with pytest.raises(ValueError):
Simulation(
initial_state=benzene_system.hoomd_snapshot, forcefield="gaff"
)
with pytest.raises(ValueError):
Simulation(
initial_state=benzene_system.hoomd_snapshot,
forcefield=OPLS_AA_PPS,
)
with pytest.raises(ValueError):
Simulation(
initial_state=benzene_system.hoomd_snapshot,
forcefield=[1, 2, 3],
)

def test_flush(self, benzene_system):
sim = Simulation.from_system(benzene_system, gsd_write_freq=100)
sim.run_NVT(kT=1.0, tau_kt=0.01, n_steps=500, write_at_start=False)
Expand Down
2 changes: 1 addition & 1 deletion flowermd/tests/base/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ def test_scale_charges(self, pps):
auto_scale=True,
)
assert abs(no_scale.net_charge.value) > abs(with_scale.net_charge.value)
assert np.allclose(0, with_scale.net_charge.value, atol=1e-30)
assert np.allclose(0, with_scale.net_charge.value, atol=1e-13)

def test_to_gsd(self, polyethylene):
polyethylene = polyethylene(lengths=5, num_mols=1)
Expand Down
2 changes: 1 addition & 1 deletion flowermd/tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_target_box_mass_density(self):
density = u.unyt_quantity(0.5, u.g / u.cm**3)
target_box = get_target_box_mass_density(density=density, mass=mass)
assert target_box[0] == target_box[1] == target_box[2]
assert np.array_equal(target_box, np.array([2 * u.cm] * 3))
assert np.array_equal(target_box, np.array([2] * 3) * u.cm)

def test_target_box_one_constraint_mass(self):
mass = u.unyt_quantity(4.0, u.g)
Expand Down

0 comments on commit 62f8363

Please sign in to comment.