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

Deprecate the box1 box2 arguments to BoxResize. #1740

Merged
merged 7 commits into from
Mar 13, 2024
Merged
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
80 changes: 11 additions & 69 deletions hoomd/BoxResizeUpdater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,18 @@ using namespace std;

namespace hoomd
{
/*! \param sysdef System definition containing the particle data to set the box size on
\param Lx length of the x dimension over time
\param Ly length of the y dimension over time
\param Lz length of the z dimension over time

The default setting is to scale particle positions along with the box.
/** @param sysdef System definition containing the particle data to set the box size on
@param trigger Steps on which to execute.
@param box Box as a function of time.
@param group Particles to scale.
*/

BoxResizeUpdater::BoxResizeUpdater(std::shared_ptr<SystemDefinition> sysdef,
std::shared_ptr<Trigger> trigger,
std::shared_ptr<BoxDim> box1,
std::shared_ptr<BoxDim> box2,
std::shared_ptr<Variant> variant,
std::shared_ptr<VectorVariantBox> box,
std::shared_ptr<ParticleGroup> group)
: Updater(sysdef, trigger), m_box1(box1), m_box2(box2), m_variant(variant), m_group(group)
: Updater(sysdef, trigger), m_box(box), m_group(group)
{
assert(m_pdata);
assert(m_variant);
m_exec_conf->msg->notice(5) << "Constructing BoxResizeUpdater" << endl;
}

Expand All @@ -41,60 +35,13 @@ BoxResizeUpdater::~BoxResizeUpdater()
m_exec_conf->msg->notice(5) << "Destroying BoxResizeUpdater" << endl;
}

/// Get box1
std::shared_ptr<BoxDim> BoxResizeUpdater::getBox1()
{
return m_box1;
}

/// Set a new box1
void BoxResizeUpdater::setBox1(std::shared_ptr<BoxDim> box1)
{
m_box1 = box1;
}

/// Get box2
std::shared_ptr<BoxDim> BoxResizeUpdater::getBox2()
{
return m_box2;
}

void BoxResizeUpdater::setBox2(std::shared_ptr<BoxDim> box2)
{
m_box2 = box2;
}

/// Get the current box based on the timestep
BoxDim BoxResizeUpdater::getCurrentBox(uint64_t timestep)
{
Scalar min = m_variant->min();
Scalar max = m_variant->max();
Scalar cur_value = (*m_variant)(timestep);
Scalar scale = 0;
if (cur_value == max)
{
scale = 1;
}
else if (cur_value > min)
{
scale = (cur_value - min) / (max - min);
}

const auto& box1 = *m_box1;
const auto& box2 = *m_box2;
Scalar3 new_L = box2.getL() * scale + box1.getL() * (1.0 - scale);
Scalar xy = box2.getTiltFactorXY() * scale + (1.0 - scale) * box1.getTiltFactorXY();
Scalar xz = box2.getTiltFactorXZ() * scale + (1.0 - scale) * box1.getTiltFactorXZ();
Scalar yz = box2.getTiltFactorYZ() * scale + (1.0 - scale) * box1.getTiltFactorYZ();

BoxDim new_box = BoxDim(new_L);
new_box.setTiltFactors(xy, xz, yz);
return new_box;
return BoxDim((*m_box)(timestep));
}

/** Perform the needed calculations to scale the box size
\param timestep Current time step of the simulation
*/
/** @param timestep Current time step of the simulation
*/
void BoxResizeUpdater::update(uint64_t timestep)
{
Updater::update(timestep);
Expand Down Expand Up @@ -124,7 +71,6 @@ void BoxResizeUpdater::update(uint64_t timestep)
}
}

/// Scale particles to the new box and wrap any others back into the box
void BoxResizeUpdater::scaleAndWrapParticles(const BoxDim& cur_box, const BoxDim& new_box)
{
ArrayHandle<Scalar4> h_pos(m_pdata->getPositions(),
Expand Down Expand Up @@ -171,13 +117,9 @@ void export_BoxResizeUpdater(pybind11::module& m)
"BoxResizeUpdater")
.def(pybind11::init<std::shared_ptr<SystemDefinition>,
std::shared_ptr<Trigger>,
std::shared_ptr<BoxDim>,
std::shared_ptr<BoxDim>,
std::shared_ptr<Variant>,
std::shared_ptr<VectorVariantBox>,
std::shared_ptr<ParticleGroup>>())
.def_property("box1", &BoxResizeUpdater::getBox1, &BoxResizeUpdater::setBox1)
.def_property("box2", &BoxResizeUpdater::getBox2, &BoxResizeUpdater::setBox2)
.def_property("variant", &BoxResizeUpdater::getVariant, &BoxResizeUpdater::setVariant)
.def_property("box", &BoxResizeUpdater::getBox, &BoxResizeUpdater::setBox)
.def_property_readonly("filter",
[](const std::shared_ptr<BoxResizeUpdater> method)
{ return method->getGroup()->getFilter(); })
Expand Down
48 changes: 17 additions & 31 deletions hoomd/BoxResizeUpdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
#include "ParticleGroup.h"
#include "Updater.h"
#include "Variant.h"
#include "VectorVariant.h"

#include <memory>
#include <pybind11/pybind11.h>
#include <stdexcept>
#include <string>

#ifndef __BOXRESIZEUPDATER_H__
#define __BOXRESIZEUPDATER_H__
#pragma once

namespace hoomd
{
Expand All @@ -37,42 +37,28 @@ class PYBIND11_EXPORT BoxResizeUpdater : public Updater
/// Constructor
BoxResizeUpdater(std::shared_ptr<SystemDefinition> sysdef,
std::shared_ptr<Trigger> trigger,
std::shared_ptr<BoxDim> box1,
std::shared_ptr<BoxDim> box2,
std::shared_ptr<Variant> variant,
std::shared_ptr<VectorVariantBox> box,
std::shared_ptr<ParticleGroup> m_group);

/// Destructor
virtual ~BoxResizeUpdater();

/// Get the current m_box2
std::shared_ptr<BoxDim> getBox1();

/// Set a new m_box_1
void setBox1(std::shared_ptr<BoxDim> box1);

/// Get the current m_box2
std::shared_ptr<BoxDim> getBox2();

/// Set a new m_box_2
void setBox2(std::shared_ptr<BoxDim> box2);

/// Gets particle scaling filter
std::shared_ptr<ParticleGroup> getGroup()
/// Set the box variant
void setBox(std::shared_ptr<VectorVariantBox> box)
{
return m_group;
m_box = box;
}

/// Set the variant for interpolation
void setVariant(std::shared_ptr<Variant> variant)
/// Get the box variant
std::shared_ptr<VectorVariantBox> getBox()
{
m_variant = variant;
return m_box;
}

/// Get the variant for interpolation
std::shared_ptr<Variant> getVariant()
/// Gets particle scaling filter
std::shared_ptr<ParticleGroup> getGroup()
{
return m_variant;
return m_group;
}

/// Get the current box for the given timestep
Expand All @@ -85,10 +71,11 @@ class PYBIND11_EXPORT BoxResizeUpdater : public Updater
virtual void scaleAndWrapParticles(const BoxDim& cur_box, const BoxDim& new_box);

protected:
std::shared_ptr<BoxDim> m_box1; ///< C++ box assoc with min
std::shared_ptr<BoxDim> m_box2; ///< C++ box assoc with max
std::shared_ptr<Variant> m_variant; //!< Variant that interpolates between boxes
std::shared_ptr<ParticleGroup> m_group; //!< Selected particles to scale when resizing the box.
/// Box as a function of time.
std::shared_ptr<VectorVariantBox> m_box;

/// Selected particles to scale when resizing the box.
std::shared_ptr<ParticleGroup> m_group;
};

namespace detail
Expand All @@ -97,4 +84,3 @@ namespace detail
void export_BoxResizeUpdater(pybind11::module& m);
} // end namespace detail
} // end namespace hoomd
#endif
18 changes: 3 additions & 15 deletions hoomd/BoxResizeUpdaterGPU.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,11 @@

namespace hoomd
{
/*! \param sysdef System definition containing the particle data to set the box size on
\param Lx length of the x dimension over time
\param Ly length of the y dimension over time
\param Lz length of the z dimension over time

The default setting is to scale particle positions along with the box.
*/

BoxResizeUpdaterGPU::BoxResizeUpdaterGPU(std::shared_ptr<SystemDefinition> sysdef,
std::shared_ptr<Trigger> trigger,
std::shared_ptr<BoxDim> box1,
std::shared_ptr<BoxDim> box2,
std::shared_ptr<Variant> variant,
std::shared_ptr<VectorVariantBox> box,
std::shared_ptr<ParticleGroup> group)
: BoxResizeUpdater(sysdef, trigger, box1, box2, variant, group)
: BoxResizeUpdater(sysdef, trigger, box, group)
{
// only one GPU is supported
if (!m_exec_conf->isCUDAEnabled())
Expand Down Expand Up @@ -87,9 +77,7 @@ void export_BoxResizeUpdaterGPU(pybind11::module& m)
"BoxResizeUpdaterGPU")
.def(pybind11::init<std::shared_ptr<SystemDefinition>,
std::shared_ptr<Trigger>,
std::shared_ptr<BoxDim>,
std::shared_ptr<BoxDim>,
std::shared_ptr<Variant>,
std::shared_ptr<VectorVariantBox>,
std::shared_ptr<ParticleGroup>>());
}

Expand Down
4 changes: 1 addition & 3 deletions hoomd/BoxResizeUpdaterGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ class PYBIND11_EXPORT BoxResizeUpdaterGPU : public BoxResizeUpdater
/// Constructor
BoxResizeUpdaterGPU(std::shared_ptr<SystemDefinition> sysdef,
std::shared_ptr<Trigger> trigger,
std::shared_ptr<BoxDim> box1,
std::shared_ptr<BoxDim> box2,
std::shared_ptr<Variant> variant,
std::shared_ptr<VectorVariantBox> box,
std::shared_ptr<ParticleGroup> m_group);

/// Destructor
Expand Down
72 changes: 66 additions & 6 deletions hoomd/pytest/test_box_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
from hoomd.conftest import operation_pickling_check
from hoomd.error import MutabilityError


@pytest.fixture(scope="function")
def rng():
return np.random.default_rng(42)


_n_points = 10


Expand Down Expand Up @@ -256,3 +250,69 @@ def test_mutability_error(simulation_factory, two_particle_snapshot_factory):

with pytest.raises(MutabilityError):
box_op.filter = filt


def test_new_api(simulation_factory, two_particle_snapshot_factory):
sim = simulation_factory(two_particle_snapshot_factory())
inverse_volume_ramp = hoomd.variant.box.InverseVolumeRamp(
initial_box=hoomd.Box.cube(6),
final_volume=100,
t_start=1_000,
t_ramp=21_000)

box_resize = hoomd.update.BoxResize(trigger=hoomd.trigger.Periodic(1),
filter=hoomd.filter.All(),
box=inverse_volume_ramp)
sim.operations.updaters.append(box_resize)

sim.run(0)

assert box_resize.box is inverse_volume_ramp


def test_invalid_api(variant, trigger):
box_variant = hoomd.variant.box.InverseVolumeRamp(
initial_box=hoomd.Box.cube(6),
final_volume=100,
t_start=1_000,
t_ramp=21_000)

box1 = hoomd.Box.cube(10)
box2 = hoomd.Box.cube(20)

with pytest.raises(ValueError):
hoomd.update.BoxResize(trigger=trigger)
with pytest.raises(ValueError):
hoomd.update.BoxResize(trigger=trigger, box1=box1)
with pytest.raises(ValueError):
hoomd.update.BoxResize(trigger=trigger, box1=box1, box2=box2)
with pytest.raises(ValueError):
hoomd.update.BoxResize(trigger=trigger, box2=box2, variant=variant)
with pytest.raises(ValueError):
hoomd.update.BoxResize(trigger=trigger,
box1=box1,
box2=box2,
variant=variant,
box=box_variant)
with pytest.raises(ValueError):
hoomd.update.BoxResize(trigger=trigger, box1=box1, box=box_variant)
with pytest.raises(ValueError):
hoomd.update.BoxResize(trigger=trigger, box2=box2, box=box_variant)
with pytest.raises(ValueError):
hoomd.update.BoxResize(trigger=trigger,
variant=variant,
box=box_variant)

box_resize = hoomd.update.BoxResize(trigger=trigger, box=box_variant)
with pytest.raises(RuntimeError):
box_resize.box1
with pytest.raises(RuntimeError):
box_resize.box2
with pytest.raises(RuntimeError):
box_resize.variant
with pytest.raises(RuntimeError):
box_resize.box1 = box1
with pytest.raises(RuntimeError):
box_resize.box2 = box2
with pytest.raises(RuntimeError):
box_resize.variant = variant
Loading
Loading