Skip to content

Commit

Permalink
core: Remove redundant thermostat free functions
Browse files Browse the repository at this point in the history
Call the thermostat methods directly. We only need free functions
for MPI callbacks.
  • Loading branch information
jngrad committed Sep 8, 2020
1 parent c475c38 commit 5428fca
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 89 deletions.
5 changes: 3 additions & 2 deletions src/core/constraints/ShapeBasedConstraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "errorhandling.hpp"
#include "forces_inline.hpp"
#include "nonbonded_interactions/nonbonded_interaction_data.hpp"
#include "thermostat.hpp"

namespace Constraints {
Utils::Vector3d ShapeBasedConstraint::total_force() const {
Expand Down Expand Up @@ -77,7 +78,7 @@ ParticleForce ShapeBasedConstraint::force(Particle const &p,
force1 +=
dpd_pair_force(p, part_rep, ia_params, dist_vec, dist, dist * dist);
// Additional use of DPD here requires counter increase
dpd_rng_counter_increment();
dpd.rng_increment();
}
#endif
} else if (m_penetrable && (dist <= 0)) {
Expand All @@ -89,7 +90,7 @@ ParticleForce ShapeBasedConstraint::force(Particle const &p,
force1 += dpd_pair_force(p, part_rep, ia_params, dist_vec, dist,
dist * dist);
// Additional use of DPD here requires counter increase
dpd_rng_counter_increment();
dpd.rng_increment();
}
#endif
}
Expand Down
2 changes: 0 additions & 2 deletions src/core/stokesian_dynamics/sd_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@

#include <vector>

extern StokesianThermostat stokesian;

namespace {
/* type for particle data transfer between nodes */
struct SD_particle_data {
Expand Down
34 changes: 13 additions & 21 deletions src/core/thermostat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
/** \file
* Implementation of \ref thermostat.hpp.
*/
#include <boost/mpi.hpp>

#include "config.hpp"

#include "bonded_interactions/thermalized_bond.hpp"
#include "communication.hpp"
Expand All @@ -30,6 +31,8 @@
#include "npt.hpp"
#include "thermostat.hpp"

#include <boost/mpi.hpp>

#include <cstdint>

int thermo_switch = THERMO_OFF;
Expand All @@ -39,9 +42,9 @@ bool thermo_virtual = true;
using Thermostat::GammaType;

/**
* @brief Register a thermostat's MPI callbacks
* @brief Create MPI callbacks of thermostat objects
*
* @param thermostat The thermostat global variable
* @param thermostat The thermostat object name
*/
#define REGISTER_THERMOSTAT_CALLBACKS(thermostat) \
void mpi_##thermostat##_set_rng_seed(uint32_t const seed) { \
Expand All @@ -54,15 +57,6 @@ using Thermostat::GammaType;
mpi_call_all(mpi_##thermostat##_set_rng_seed, seed); \
} \
\
uint32_t thermostat##_get_rng_seed() { return (thermostat).rng_seed(); } \
\
void thermostat##_rng_counter_increment() { (thermostat).rng_increment(); } \
\
bool thermostat##_is_seed_required() { \
/* Seed is required if rng is not initialized */ \
return !(thermostat).rng_is_initialized(); \
} \
\
void mpi_##thermostat##_set_rng_counter(uint64_t const value) { \
(thermostat).set_rng_counter(value); \
} \
Expand All @@ -71,9 +65,7 @@ using Thermostat::GammaType;
\
void thermostat##_set_rng_counter(uint64_t const value) { \
mpi_call_all(mpi_##thermostat##_set_rng_counter, value); \
} \
\
uint64_t thermostat##_get_rng_counter() { return (thermostat).rng_counter(); }
}

LangevinThermostat langevin = {};
BrownianThermostat brownian = {};
Expand Down Expand Up @@ -126,27 +118,27 @@ void thermo_init() {

void philox_counter_increment() {
if (thermo_switch & THERMO_LANGEVIN) {
langevin_rng_counter_increment();
langevin.rng_increment();
}
if (thermo_switch & THERMO_BROWNIAN) {
brownian_rng_counter_increment();
brownian.rng_increment();
}
#ifdef NPT
if (thermo_switch & THERMO_NPT_ISO) {
npt_iso_rng_counter_increment();
npt_iso.rng_increment();
}
#endif
#ifdef DPD
if (thermo_switch & THERMO_DPD) {
dpd_rng_counter_increment();
dpd.rng_increment();
}
#endif
#if defined(STOKESIAN_DYNAMICS) || defined(STOKESIAN_DYNAMICS_GPU)
if (thermo_switch & THERMO_SD) {
stokesian_rng_counter_increment();
stokesian.rng_increment();
}
#endif
if (n_thermalized_bonds) {
thermalized_bond_rng_counter_increment();
thermalized_bond.rng_increment();
}
}
20 changes: 10 additions & 10 deletions src/core/thermostat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ struct BaseThermostat {
void set_rng_counter(uint64_t value) {
m_rng_counter = Utils::Counter<uint64_t>(0u, value);
}
/** Is the RNG counter initialized */
bool rng_is_initialized() const { return !!m_rng_seed; }
/** Is the RNG seed required */
bool is_seed_required() const { return !m_rng_seed; }
uint32_t rng_seed() const { return m_rng_seed.value(); }

private:
Expand Down Expand Up @@ -328,17 +328,13 @@ struct StokesianThermostat : public BaseThermostat {
************************************************/

/**
* @brief Register a thermostat public interface
* @brief Register MPI callbacks of thermostat objects
*
* @param thermostat The thermostat name
* @param thermostat The thermostat object name
*/
#define NEW_THERMOSTAT(thermostat) \
bool thermostat##_is_seed_required(); \
void thermostat##_rng_counter_increment(); \
void thermostat##_set_rng_seed(uint32_t const seed); \
void thermostat##_set_rng_counter(uint64_t const seed); \
uint32_t thermostat##_get_rng_seed(); \
uint64_t thermostat##_get_rng_counter();
void thermostat##_set_rng_seed(uint32_t seed); \
void thermostat##_set_rng_counter(uint64_t seed);

NEW_THERMOSTAT(langevin)
NEW_THERMOSTAT(brownian)
Expand All @@ -355,9 +351,13 @@ NEW_THERMOSTAT(stokesian)
extern LangevinThermostat langevin;
extern BrownianThermostat brownian;
extern IsotropicNptThermostat npt_iso;
extern ThermalizedBondThermostat thermalized_bond;
#ifdef DPD
extern DPDThermostat dpd;
#endif
#if defined(STOKESIAN_DYNAMICS) || defined(STOKESIAN_DYNAMICS_GPU)
extern StokesianThermostat stokesian;
#endif

/** Initialize constants of the thermostat at the start of integration */
void thermo_init();
Expand Down
6 changes: 3 additions & 3 deletions src/python/espressomd/interactions.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ from libcpp.vector cimport vector
from libcpp cimport bool as cbool
from libc cimport stdint

from .thermostat cimport thermalized_bond

include "myconfig.pxi"

# force include of config.hpp
Expand Down Expand Up @@ -526,10 +528,8 @@ cdef extern from "object-in-fluid/oif_local_forces.hpp":

cdef extern from "bonded_interactions/thermalized_bond.hpp":
int thermalized_bond_set_params(int bond_type, double temp_com, double gamma_com, double temp_distance, double gamma_distance, double r_cut)

cdef extern from "thermostat.hpp":
cbool thermalized_bond_is_seed_required()
stdint.uint32_t thermalized_bond_get_rng_seed()
stdint.uint64_t thermalized_bond_get_rng_counter()
void thermalized_bond_set_rng_seed(stdint.uint32_t seed)
void thermalized_bond_set_rng_counter(stdint.uint64_t counter)

Expand Down
6 changes: 3 additions & 3 deletions src/python/espressomd/interactions.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2119,12 +2119,12 @@ class ThermalizedBond(BondedInteraction):
bonded_ia_params[
self._bond_id].p.thermalized_bond.gamma_distance,
"r_cut": bonded_ia_params[self._bond_id].p.thermalized_bond.r_cut,
"_counter": thermalized_bond_get_rng_counter(),
"seed": thermalized_bond_get_rng_seed()
"_counter": thermalized_bond.rng_counter(),
"seed": thermalized_bond.rng_seed()
}

def _set_params_in_es_core(self):
if self.params["seed"] is None and thermalized_bond_is_seed_required():
if self.params["seed"] is None and thermalized_bond.is_seed_required():
raise ValueError(
"A seed has to be given as keyword argument on first activation of the thermalized bond")
if self.params["seed"] is not None:
Expand Down
58 changes: 26 additions & 32 deletions src/python/espressomd/thermostat.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,45 @@ cdef extern from "thermostat.hpp":
int THERMO_BROWNIAN
int THERMO_SD

cdef cppclass BaseThermostat:
stdint.uint32_t rng_seed()
stdint.uint64_t rng_counter()
cbool is_seed_required()

IF PARTICLE_ANISOTROPY:
ctypedef struct langevin_thermostat_struct "LangevinThermostat":
cdef cppclass LangevinThermostat(BaseThermostat):
Vector3d gamma_rotation
Vector3d gamma
ctypedef struct brownian_thermostat_struct "BrownianThermostat":
cdef cppclass BrownianThermostat(BaseThermostat):
Vector3d gamma_rotation
Vector3d gamma
ELSE:
ctypedef struct langevin_thermostat_struct "LangevinThermostat":
cdef cppclass LangevinThermostat(BaseThermostat):
double gamma_rotation
double gamma
ctypedef struct brownian_thermostat_struct "BrownianThermostat":
cdef cppclass BrownianThermostat(BaseThermostat):
double gamma_rotation
double gamma
ctypedef struct npt_iso_thermostat_struct "IsotropicNptThermostat":
cdef cppclass IsotropicNptThermostat(BaseThermostat):
double gamma0
double gammav
cdef cppclass ThermalizedBondThermostat(BaseThermostat):
pass
IF DPD:
cdef cppclass DPDThermostat(BaseThermostat):
pass
IF(STOKESIAN_DYNAMICS or STOKESIAN_DYNAMICS_GPU):
cdef cppclass StokesianThermostat(BaseThermostat):
pass

langevin_thermostat_struct langevin
brownian_thermostat_struct brownian
npt_iso_thermostat_struct npt_iso
LangevinThermostat langevin
BrownianThermostat brownian
IsotropicNptThermostat npt_iso
ThermalizedBondThermostat thermalized_bond
IF DPD:
DPDThermostat dpd
IF(STOKESIAN_DYNAMICS or STOKESIAN_DYNAMICS_GPU):
StokesianThermostat stokesian

void langevin_set_rng_seed(stdint.uint32_t seed)
void brownian_set_rng_seed(stdint.uint32_t seed)
Expand All @@ -72,30 +90,6 @@ cdef extern from "thermostat.hpp":
IF(STOKESIAN_DYNAMICS or STOKESIAN_DYNAMICS_GPU):
void stokesian_set_rng_counter(stdint.uint64_t counter)

cbool langevin_is_seed_required()
cbool brownian_is_seed_required()
cbool npt_iso_is_seed_required()
IF DPD:
cbool dpd_is_seed_required()
IF(STOKESIAN_DYNAMICS or STOKESIAN_DYNAMICS_GPU):
cbool stokesian_is_seed_required()

stdint.uint32_t langevin_get_rng_seed()
stdint.uint32_t brownian_get_rng_seed()
stdint.uint32_t npt_iso_get_rng_seed()
IF DPD:
stdint.uint32_t dpd_get_rng_seed()
IF(STOKESIAN_DYNAMICS or STOKESIAN_DYNAMICS_GPU):
stdint.uint32_t stokesian_get_rng_seed()

stdint.uint64_t langevin_get_rng_counter()
stdint.uint64_t brownian_get_rng_counter()
stdint.uint64_t npt_iso_get_rng_counter()
IF DPD:
stdint.uint64_t dpd_get_rng_counter()
IF(STOKESIAN_DYNAMICS or STOKESIAN_DYNAMICS_GPU):
stdint.uint64_t stokesian_get_rng_counter()

cdef extern from "stokesian_dynamics/sd_interface.hpp":
IF(STOKESIAN_DYNAMICS or STOKESIAN_DYNAMICS_GPU):
void set_sd_kT(double kT)
Expand Down
Loading

0 comments on commit 5428fca

Please sign in to comment.