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

Pair hpmc angular step potential #1728

Merged
merged 29 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e32532e
add files for hpmc angular step (patch) potential
melodyyzh Feb 19, 2024
9fe43f1
modified AngularStep code
melodyyzh Feb 23, 2024
92855ca
modified angularStep code
melodyyzh Feb 23, 2024
656c39b
add more code
melodyyzh Feb 24, 2024
ead7cdf
continue edits
melodyyzh Feb 26, 2024
a4deb38
Merge remote-tracking branch 'remotes/origin/hpmc-union-potential' in…
melodyyzh Feb 26, 2024
35842cc
added more changes
melodyyzh Mar 1, 2024
df2530c
code review with Josh
melodyyzh Mar 1, 2024
681045b
complete unit test
melodyyzh Mar 2, 2024
8ffcf4a
changed python file and CMakeLists
melodyyzh Mar 2, 2024
dfd9498
fix errors during compiling
melodyyzh Mar 2, 2024
c5923cb
add AngularStep to files for the unit test
melodyyzh Mar 3, 2024
ca8ce0a
code review with Josh
melodyyzh Mar 6, 2024
a8daca8
Merge branch 'trunk-minor' into pair-hpmc-angularStep
melodyyzh Mar 6, 2024
bb39c66
add the equation for angular step potential
melodyyzh Mar 6, 2024
853ba7c
Use _make_cpp_obj.
joaander Mar 6, 2024
0d60d94
Merge branch 'trunk-minor' into pair-hpmc-angularStep
joaander Mar 7, 2024
f1107d8
fix documentation and ran pre-commit
melodyyzh Mar 7, 2024
372d4e2
Merge branch 'pair-hpmc-angularStep' of github.com:glotzerlab/hoomd-b…
melodyyzh Mar 7, 2024
4399637
Revise documentation.
joaander Mar 7, 2024
838f828
modified code based on the suggestions
melodyyzh Mar 8, 2024
4dc0bba
Update hoomd/hpmc/PairPotentialAngularStep.h
melodyyzh Mar 8, 2024
a075f75
manually commit outdated suggestions
melodyyzh Mar 8, 2024
c9adecf
pre-commit fixes
melodyyzh Mar 8, 2024
8392229
Update hoomd/hpmc/PairPotentialAngularStep.h
melodyyzh Mar 12, 2024
0025336
add array and list as valid dict in unit test
melodyyzh Mar 12, 2024
8d86ae9
Merge branch 'pair-hpmc-angularStep' of github.com:glotzerlab/hoomd-b…
melodyyzh Mar 12, 2024
7865e61
small fix on wording and magnitude calculation
melodyyzh Mar 18, 2024
b6c4458
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 18, 2024
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
2 changes: 2 additions & 0 deletions hoomd/hpmc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ set(_hpmc_sources module.cc
PairPotentialLennardJones.cc
PairPotentialStep.cc
PairPotentialUnion.cc
PairPotentialAngularStep.cc
ShapeUtils.cc
UpdaterBoxMC.cc
UpdaterQuickCompress.cc
Expand Down Expand Up @@ -82,6 +83,7 @@ set(_hpmc_headers
PairPotentialLennardJones.h
PairPotentialStep.h
PairPotentialUnion.h
PairPotentialAngularStep.h
ShapeConvexPolygon.h
ShapeConvexPolyhedron.h
ShapeEllipsoid.h
Expand Down
130 changes: 130 additions & 0 deletions hoomd/hpmc/PairPotentialAngularStep.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright (c) 2009-2024 The Regents of the University of Michigan.
// Part of HOOMD-blue, released under the BSD 3-Clause License.

#include "PairPotentialAngularStep.h"

namespace hoomd
{
namespace hpmc
{

PairPotentialAngularStep::PairPotentialAngularStep(
std::shared_ptr<SystemDefinition> sysdef,
std::shared_ptr<PairPotential> isotropic_potential)
: PairPotential(sysdef), m_isotropic_potential(isotropic_potential)
{
unsigned int ntypes = m_sysdef->getParticleData()->getNTypes();
m_directors.resize(ntypes);
m_cos_deltas.resize(ntypes);

if (!m_isotropic_potential)
{
throw std::runtime_error("no isotropic potential given.");
}
}

void PairPotentialAngularStep::setMask(std::string particle_type, pybind11::object v)
{
unsigned int particle_type_id = m_sysdef->getParticleData()->getTypeByName(particle_type);

if (v.is_none())
{
m_directors[particle_type_id].clear();
m_cos_deltas[particle_type_id].clear();
return;
}
pybind11::list directors = v["directors"];
pybind11::list deltas = v["deltas"];

auto N = pybind11::len(directors);

if (pybind11::len(deltas) != N)
{
throw std::runtime_error("the length of the delta list should match the length"
"of the director list.");
}

m_directors[particle_type_id].resize(N);
m_cos_deltas[particle_type_id].resize(N);

for (unsigned int i = 0; i < N; i++)
{
pybind11::tuple director_python = directors[i];
if (pybind11::len(director_python) != 3)
{
throw std::length_error("director must be a list of 3-tuples.");
}

m_directors[particle_type_id][i] = vec3<LongReal>(director_python[0].cast<LongReal>(),
director_python[1].cast<LongReal>(),
director_python[2].cast<LongReal>());

// normalize the directional vector
m_directors[particle_type_id][i] /= fast::sqrt(dot(m_directors[particle_type_id][i],
m_directors[particle_type_id][i]));

pybind11::handle delta_python = deltas[i];
m_cos_deltas[particle_type_id][i] = cos(delta_python.cast<LongReal>());
}
}

pybind11::object PairPotentialAngularStep::getMask(std::string particle_type)
{
unsigned int particle_type_id = m_sysdef->getParticleData()->getTypeByName(particle_type);
size_t N = m_directors[particle_type_id].size();

if (N == 0)
{
return pybind11::none();
}

pybind11::list directors;
pybind11::list deltas;

for (unsigned int i = 0; i < N; i++)
{
directors.append(pybind11::make_tuple(m_directors[particle_type_id][i].x,
m_directors[particle_type_id][i].y,
m_directors[particle_type_id][i].z));
deltas.append(acos(m_cos_deltas[particle_type_id][i]));
}

pybind11::dict v;
v["directors"] = directors;
v["deltas"] = deltas;
return v;
}

// protected
LongReal PairPotentialAngularStep::energy(const LongReal r_squared,
const vec3<LongReal>& r_ij,
const unsigned int type_i,
const quat<LongReal>& q_i,
const LongReal charge_i,
const unsigned int type_j,
const quat<LongReal>& q_j,
const LongReal charge_j) const
{
if (maskingFunction(r_squared, r_ij, type_i, q_i, type_j, q_j))
{
return m_isotropic_potential
->energy(r_squared, r_ij, type_i, q_i, charge_i, type_j, q_j, charge_j);
}
return 0;
}

namespace detail
{
void exportPairPotentialAngularStep(pybind11::module& m)
{
pybind11::class_<PairPotentialAngularStep,
PairPotential,
std::shared_ptr<PairPotentialAngularStep>>(m, "PairPotentialAngularStep")
.def(pybind11::init<std::shared_ptr<SystemDefinition>, std::shared_ptr<PairPotential>>())
.def("setMask", &PairPotentialAngularStep::setMask)
.def("getMask", &PairPotentialAngularStep::getMask);
}
} // end namespace detail

} // end namespace hpmc
} // end namespace hoomd
100 changes: 100 additions & 0 deletions hoomd/hpmc/PairPotentialAngularStep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) 2009-2024 The Regents of the University of Michigan.
// Part of HOOMD-blue, released under the BSD 3-Clause License.

#pragma once

#include "PairPotential.h"

namespace hoomd
{
namespace hpmc
{

/*** Compute angular step potential between two particles.

For use with HPMC simulations.
*/
class PairPotentialAngularStep : public hpmc::PairPotential
{
public:
PairPotentialAngularStep(std::shared_ptr<SystemDefinition> sysdef,
std::shared_ptr<PairPotential> isotropic_potential);
virtual ~PairPotentialAngularStep() { }

/// Set the patch parameters for a given particle type.
void setMask(std::string particle_type, pybind11::object v);

/// Get the patch parameters for a given particle type.
pybind11::object getMask(std::string particle_type);

/*** Evaluate the energy of the pair interaction
@param r_squared Pre-computed dot(r_ij, r_ij).
@param r_ij Vector pointing from particle i to j.
@param type_i Integer type index of particle i.
@param charge_i Charge of particle i.
@param q_i Orientation quaternion of particle i.
@param type_j Integer type index of particle j.
@param q_j Orientation quaternion of particle j.
@param charge_j Charge of particle j.
@returns Energy of the pair interaction.
*/
virtual LongReal energy(const LongReal r_squared,
const vec3<LongReal>& r_ij,
const unsigned int type_i,
const quat<LongReal>& q_i,
const LongReal charge_i,
const unsigned int type_j,
const quat<LongReal>& q_j,
const LongReal charge_j) const;

virtual LongReal computeRCutNonAdditive(unsigned int type_i, unsigned int type_j) const
{
// Pass on the non-additive r_cut from the isotropic (parent) potential.
return m_isotropic_potential->computeRCutNonAdditive(type_i, type_j);
}

virtual LongReal computeRCutAdditive(unsigned int type) const
{
// Returns the additive part of the cutoff distance for a given type.
return m_isotropic_potential->computeRCutAdditive(type);
}

protected:
bool maskingFunction(LongReal r_squared,
joaander marked this conversation as resolved.
Show resolved Hide resolved
const vec3<LongReal>& r_ij,
const unsigned int type_i,
const quat<LongReal>& q_i,
const unsigned int type_j,
const quat<LongReal>& q_j) const
{
vec3<LongReal> rhat_ij = r_ij / fast::sqrt(r_squared);

for (size_t m = 0; m < m_directors[type_i].size(); m++)
{
LongReal cos_delta_m = m_cos_deltas[type_i][m];
vec3<LongReal> ehat_m = rotate(q_i, m_directors[type_i][m]);

for (size_t n = 0; n < m_directors[type_j].size(); n++)
{
LongReal cos_delta_n = m_cos_deltas[type_j][n];
vec3<LongReal> ehat_n = rotate(q_j, m_directors[type_j][n]);

if (dot(ehat_m, rhat_ij) >= cos_delta_m && dot(ehat_n, -rhat_ij) >= cos_delta_n)
{
return true;
}
}
}
return false;
}

/// Create a pointer to get the isotropic pair potential
std::shared_ptr<PairPotential> m_isotropic_potential;

/// Type pair parameters of potential
std::vector<std::vector<vec3<LongReal>>> m_directors;
std::vector<std::vector<LongReal>> m_cos_deltas;
};

} // end namespace hpmc
} // end namespace hoomd
3 changes: 3 additions & 0 deletions hoomd/hpmc/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ void exportPairPotential(pybind11::module& m);

void exportPairPotentialLennardJones(pybind11::module& m);

void exportPairPotentialAngularStep(pybind11::module& m);

void exportPairPotentialStep(pybind11::module& m);

void exportPairPotentialUnion(pybind11::module& m);
Expand Down Expand Up @@ -146,6 +148,7 @@ PYBIND11_MODULE(_hpmc, m)

exportPairPotential(m);
exportPairPotentialLennardJones(m);
exportPairPotentialAngularStep(m);
exportPairPotentialStep(m);
exportPairPotentialUnion(m);
}
Expand Down
1 change: 1 addition & 0 deletions hoomd/hpmc/pair/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(files __init__.py
pair.py
step.py
union.py
angular_step.py
user.py
)

Expand Down
1 change: 1 addition & 0 deletions hoomd/hpmc/pair/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@
from .pair import Pair
from .lennard_jones import LennardJones
from .union import Union
from .angular_step import AngularStep
from .step import Step
Loading
Loading