Skip to content

Commit

Permalink
Rewrite LJcos interaction interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jhossbach authored and jngrad committed Aug 24, 2022
1 parent 437cea0 commit 29ef411
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 79 deletions.
42 changes: 20 additions & 22 deletions src/core/nonbonded_interactions/ljcos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,32 @@
#include "ljcos.hpp"

#ifdef LJCOS
#include "interactions.hpp"
#include "nonbonded_interaction_data.hpp"

#include <utils/constants.hpp>
#include <utils/math/sqr.hpp>

int ljcos_set_params(int part_type_a, int part_type_b, double eps, double sig,
double cut, double offset) {
IA_parameters *data = get_ia_param_safe(part_type_a, part_type_b);

if (!data)
return ES_ERROR;

data->ljcos.eps = eps;
data->ljcos.sig = sig;
data->ljcos.cut = cut;
data->ljcos.offset = offset;

/* Calculate dependent parameters */
LJcos_Parameters::LJcos_Parameters(double eps, double sig, double cut,
double offset)
: LJcos_Parameters(eps, sig, cut, offset, 0., 0., 0.) {
auto const facsq = Utils::cbrt_2() * Utils::sqr(sig);
data->ljcos.rmin = sqrt(Utils::cbrt_2()) * sig;
data->ljcos.alfa = Utils::pi() / (Utils::sqr(data->ljcos.cut) - facsq);
data->ljcos.beta =
Utils::pi() * (1. - (1. / (Utils::sqr(data->ljcos.cut) / facsq - 1.)));

/* broadcast interaction parameters */
mpi_bcast_ia_params(part_type_a, part_type_b);
rmin = sqrt(Utils::cbrt_2()) * sig;
alfa = Utils::pi() / (Utils::sqr(cut) - facsq);
beta = Utils::pi() * (1. - (1. / (Utils::sqr(cut) / facsq - 1.)));
}

return ES_OK;
LJcos_Parameters::LJcos_Parameters(double eps, double sig, double cut,
double offset, double alfa, double beta,
double rmin)
: eps{eps}, sig{sig}, cut{cut}, offset{offset}, alfa{alfa}, beta{beta},
rmin{rmin} {
if (eps < 0.) {
std::domain_error("Lennard-Jones parameter 'epsilon' has to be >= 0");
}
if (sig < 0.) {
std::domain_error("Lennard-Jones parameter 'sigma' has to be >= 0");
}
}
#endif

#endif // LJCOS
3 changes: 0 additions & 3 deletions src/core/nonbonded_interactions/ljcos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@

#include <cmath>

int ljcos_set_params(int part_type_a, int part_type_b, double eps, double sig,
double cut, double offset);

/** Calculate Lennard-Jones cosine force factor */
inline double ljcos_pair_force_factor(IA_parameters const &ia_params,
double dist) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ struct LJcos_Parameters {
double alfa = 0.0;
double beta = 0.0;
double rmin = 0.0;
LJcos_Parameters() = default;
LJcos_Parameters(double eps, double sig, double cut, double offset);
LJcos_Parameters(double eps, double sig, double cut, double offset,
double alfa, double beta, double rmin);
};

/** Lennard-Jones with a different Cos potential */
Expand Down
14 changes: 0 additions & 14 deletions src/python/espressomd/interactions.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@ cdef extern from "nonbonded_interactions/nonbonded_interaction_data.hpp":
double Fmax
double r

cdef struct LJcos_Parameters:
double eps
double sig
double cut
double offset

cdef struct LJcos2_Parameters:
double eps
double sig
Expand Down Expand Up @@ -140,8 +134,6 @@ cdef extern from "nonbonded_interactions/nonbonded_interaction_data.hpp":

cdef struct IA_parameters:

LJcos_Parameters ljcos

LJcos2_Parameters ljcos2

LJGen_Parameters ljgen
Expand Down Expand Up @@ -176,12 +168,6 @@ cdef extern from "nonbonded_interactions/nonbonded_interaction_data.hpp":
cdef void ia_params_set_state(string)
cdef void reset_ia_params()

IF LJCOS:
cdef extern from "nonbonded_interactions/ljcos.hpp":
cdef int ljcos_set_params(int part_type_a, int part_type_b,
double eps, double sig,
double cut, double offset)

IF LJCOS2:
cdef extern from "nonbonded_interactions/ljcos2.hpp":
cdef int ljcos2_set_params(int part_type_a, int part_type_b,
Expand Down
61 changes: 21 additions & 40 deletions src/python/espressomd/interactions.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -559,37 +559,22 @@ IF LENNARD_JONES_GENERIC == 1:
return {"epsilon", "sigma", "cutoff",
"shift", "offset", "e1", "e2", "b1", "b2"}

IF LJCOS:
IF LJCOS == 1:

cdef class LennardJonesCosInteraction(NonBondedInteraction):

def validate_params(self):
if self._params["epsilon"] < 0:
raise ValueError("Lennard-Jones epsilon has to be >=0")
if self._params["sigma"] < 0:
raise ValueError("Lennard-Jones sigma has to be >=0")

def _get_params_from_es_core(self):
cdef IA_parameters * ia_params
ia_params = get_ia_param_safe(
self._part_types[0],
self._part_types[1])
return {
"epsilon": ia_params.ljcos.eps,
"sigma": ia_params.ljcos.sig,
"cutoff": ia_params.ljcos.cut,
"offset": ia_params.ljcos.offset,
}

def is_active(self):
return(self._params["epsilon"] > 0)
@script_interface_register
class LennardJonesCosInteraction(NewNonBondedInteraction):
"""Lennard-Jones cosine interaction.
def set_params(self, **kwargs):
"""Set parameters for the Lennard-Jones cosine interaction.
Methods
-------
set_params()
Set or update parameters for the interaction.
Parameters marked as required become optional once the
interaction has been activated for the first time;
subsequent calls to this method update the existing values.
Parameters
----------
epsilon : :obj:`float`
Magnitude of the interaction.
sigma : :obj:`float`
Expand All @@ -598,18 +583,15 @@ IF LJCOS:
Cutoff distance of the interaction.
offset : :obj:`float`, optional
Offset distance of the interaction.
"""
super().set_params(**kwargs)
"""

def _set_params_in_es_core(self):
if ljcos_set_params(self._part_types[0],
self._part_types[1],
self._params["epsilon"],
self._params["sigma"],
self._params["cutoff"],
self._params["offset"]):
raise Exception(
"Could not set Lennard-Jones Cosine parameters")
_so_name = "Interactions::InteractionLJcos"

def is_active(self):
"""Check if interactions is active.
"""
return self.epsilon > 0.

def default_params(self):
return {"offset": 0.}
Expand All @@ -624,7 +606,8 @@ IF LJCOS:
"""All parameters that can be set.
"""
return {"epsilon", "sigma", "cutoff", "offset"}
return {"epsilon", "sigma", "cutoff",
"offset", "alfa", "beta", "rmin"}

def required_keys(self):
"""Parameters that have to be set.
Expand Down Expand Up @@ -1578,8 +1561,6 @@ class NonBondedInteractionHandle(ScriptInterfaceHelper):
IF LENNARD_JONES_GENERIC:
self.generic_lennard_jones = GenericLennardJonesInteraction(
_type1, _type2)
IF LJCOS:
self.lennard_jones_cos = LennardJonesCosInteraction(_type1, _type2)
IF LJCOS2:
self.lennard_jones_cos2 = LennardJonesCos2Interaction(
_type1, _type2)
Expand Down
46 changes: 46 additions & 0 deletions src/script_interface/interactions/NonBondedInteraction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,42 @@ class InteractionLJ : public InteractionPotentialInterface<::LJ_Parameters> {
};
#endif // LENNARD_JONES

#ifdef LJCOS
class InteractionLJcos
: public InteractionPotentialInterface<::LJcos_Parameters> {
protected:
CoreInteraction IA_parameters::*get_ptr_offset() const override {
return &::IA_parameters::ljcos;
}

public:
InteractionLJcos() {
add_parameters({
make_autoparameter(&CoreInteraction::eps, "epsilon"),
make_autoparameter(&CoreInteraction::sig, "sigma"),
make_autoparameter(&CoreInteraction::cut, "cutoff"),
make_autoparameter(&CoreInteraction::offset, "offset"),
make_autoparameter(&CoreInteraction::alfa, "alfa"),
make_autoparameter(&CoreInteraction::beta, "beta"),
make_autoparameter(&CoreInteraction::rmin, "rmin"),
});
}

void make_new_instance(VariantMap const &params) override {
if (boost::get<std::string>(&params.at("alfa"))) {
m_ia_si = make_shared_from_args<CoreInteraction, double, double, double,
double, double, double, double>(
params, "epsilon", "sigma", "cutoff", "offset", "alfa", "beta",
"rmin");
} else {
m_ia_si = make_shared_from_args<CoreInteraction, double, double, double,
double>(params, "epsilon", "sigma",
"cutoff", "offset");
}
}
};
#endif // LJCOS

class NonBondedInteractionHandle
: public AutoParameters<NonBondedInteractionHandle> {
std::array<int, 2> m_types = {-1, -1};
Expand All @@ -201,6 +237,9 @@ class NonBondedInteractionHandle
#ifdef LENNARD_JONES
std::shared_ptr<InteractionLJ> m_lj;
#endif
#ifdef LJCOS
std::shared_ptr<InteractionLJcos> m_ljcos;
#endif

template <class T>
auto make_autoparameter(std::shared_ptr<T> &member, const char *key) const {
Expand All @@ -224,6 +263,9 @@ class NonBondedInteractionHandle
#endif
#ifdef LENNARD_JONES
make_autoparameter(m_lj, "lennard_jones"),
#endif
#ifdef LJCOS
make_autoparameter(m_ljcos, "lennard_jones_cos"),
#endif
});
}
Expand Down Expand Up @@ -270,6 +312,10 @@ class NonBondedInteractionHandle
#ifdef LENNARD_JONES
set_member<InteractionLJ>(m_lj, "lennard_jones",
"Interactions::InteractionLJ", params);
#endif
#ifdef LJCOS
set_member<InteractionLJcos>(m_ljcos, "lennard_jones_cos",
"Interactions::InteractionLJcos", params);
#endif
}

Expand Down
3 changes: 3 additions & 0 deletions src/script_interface/interactions/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ void initialize(Utils::Factory<ObjectHandle> *om) {
#ifdef LENNARD_JONES
om->register_new<InteractionLJ>("Interactions::InteractionLJ");
#endif
#ifdef LJCOS
om->register_new<InteractionLJcos>("Interactions::InteractionLJcos");
#endif
#ifdef WCA
om->register_new<InteractionWCA>("Interactions::InteractionWCA");
#endif
Expand Down

0 comments on commit 29ef411

Please sign in to comment.