Skip to content

Commit

Permalink
Merge pull request #1685 from glotzerlab/feature/vector-variant
Browse files Browse the repository at this point in the history
Add vector-valued variants
  • Loading branch information
joaander authored Feb 28, 2024
2 parents 3ccba25 + 7f123a5 commit d4349bf
Show file tree
Hide file tree
Showing 14 changed files with 834 additions and 13 deletions.
4 changes: 3 additions & 1 deletion hoomd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ set(_hoomd_sources Action.cc
Tuner.cc
Updater.cc
Variant.cc
VectorVariant.cc
extern/BVLSSolver.cc
extern/gsd.c
extern/kiss_fft.cc
Expand Down Expand Up @@ -187,6 +188,7 @@ set(_hoomd_headers
TextureTools.h
Updater.h
Variant.h
VectorVariant.h
VectorMath.h
WarpTools.cuh
)
Expand Down Expand Up @@ -352,7 +354,6 @@ set(files box.py
operations.py
pytest_plugin_validate.py
util.py
variant.py
simulation.py
state.py
trigger.py
Expand Down Expand Up @@ -384,6 +385,7 @@ add_subdirectory(write)
add_subdirectory(pytest)
add_subdirectory(tune)
add_subdirectory(update)
add_subdirectory(variant)

if (BUILD_TESTING)
# add_subdirectory(test-py)
Expand Down
6 changes: 3 additions & 3 deletions hoomd/Variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace hoomd
{
/** Defines quantities that vary with time steps.
/** Defines scalar quantities that vary with time steps.
Variant provides an interface to define quanties (such as kT) that vary over time. The base
class provides a callable interface. Derived classes implement specific kinds of varying
Variant provides an interface to define scalar quanties (such as kT) that vary over time. The
base class provides a callable interface. Derived classes implement specific kinds of varying
quantities.
*/
class PYBIND11_EXPORT Variant
Expand Down
94 changes: 94 additions & 0 deletions hoomd/VectorVariant.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) 2009-2024 The Regents of the University of Michigan.
// Part of HOOMD-blue, released under the BSD 3-Clause License.

#include "VectorVariant.h"
#include <pybind11/stl.h>

namespace hoomd
{
//* Trampoline for classes inherited in python
class VectorVariantBoxPy : public VectorVariantBox
{
public:
// Inherit the constructors
using VectorVariantBox::VectorVariantBox;

// trampoline method
array_type operator()(uint64_t timestep) override
{
PYBIND11_OVERLOAD_NAME(array_type, // Return type
VectorVariantBox, // Parent class
"__call__", // name of function in python
operator(), // Name of function in C++
timestep // Argument(s)
);
}
};

namespace detail
{

// This testVariantCall function allows us to test that Python custom vector
// variants work properly in C++. This ensures we can test that the function
// itself can be called in C++ when defined in Python.

/// Method to enable unit testing of C++ variant calls from pytest
std::array<Scalar, 6> testVectorVariantBoxCall(std::shared_ptr<VectorVariantBox> t, uint64_t step)
{
return (*t)(step);
}

void export_VectorVariantBoxClasses(pybind11::module& m)
{
pybind11::class_<VectorVariantBox, VectorVariantBoxPy, std::shared_ptr<VectorVariantBox>>(
m,
"VectorVariantBox")
.def(pybind11::init<>())
.def("__call__", &VectorVariantBox::operator());

pybind11::class_<VectorVariantBoxConstant,
VectorVariantBox,
std::shared_ptr<VectorVariantBoxConstant>>(m, "VectorVariantBoxConstant")
.def(pybind11::init<std::shared_ptr<BoxDim>>())
.def_property("_box", &VectorVariantBoxConstant::getBox, &VectorVariantBoxConstant::setBox);

pybind11::class_<VectorVariantBoxInterpolate,
VectorVariantBox,
std::shared_ptr<VectorVariantBoxInterpolate>>(m, "VectorVariantBoxInterpolate")
.def(pybind11::
init<std::shared_ptr<BoxDim>, std::shared_ptr<BoxDim>, std::shared_ptr<Variant>>())
.def_property("_initial_box",
&VectorVariantBoxInterpolate::getInitialBox,
&VectorVariantBoxInterpolate::setInitialBox)
.def_property("_final_box",
&VectorVariantBoxInterpolate::getFinalBox,
&VectorVariantBoxInterpolate::setFinalBox)
.def_property("variant",
&VectorVariantBoxInterpolate::getVariant,
&VectorVariantBoxInterpolate::setVariant);

pybind11::class_<VectorVariantBoxInverseVolumeRamp,
VectorVariantBox,
std::shared_ptr<VectorVariantBoxInverseVolumeRamp>>(
m,
"VectorVariantBoxInverseVolumeRamp")
.def(pybind11::init<std::shared_ptr<BoxDim>, Scalar, uint64_t, uint64_t>())
.def_property("_initial_box",
&VectorVariantBoxInverseVolumeRamp::getInitialBox,
&VectorVariantBoxInverseVolumeRamp::setInitialBox)
.def_property("t_start",
&VectorVariantBoxInverseVolumeRamp::getTStart,
&VectorVariantBoxInverseVolumeRamp::setTStart)
.def_property("t_ramp",
&VectorVariantBoxInverseVolumeRamp::getTRamp,
&VectorVariantBoxInverseVolumeRamp::setTRamp)
.def_property("final_volume",
&VectorVariantBoxInverseVolumeRamp::getFinalVolume,
&VectorVariantBoxInverseVolumeRamp::setFinalVolume);

m.def("_test_vector_variant_box_call", &testVectorVariantBoxCall);
}

} // end namespace detail

} // end namespace hoomd
Loading

0 comments on commit d4349bf

Please sign in to comment.