From 5428fcadfbad559fe4a7a18b13f7a178404435c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-No=C3=ABl=20Grad?= Date: Tue, 8 Sep 2020 20:22:29 +0200 Subject: [PATCH] core: Remove redundant thermostat free functions Call the thermostat methods directly. We only need free functions for MPI callbacks. --- src/core/constraints/ShapeBasedConstraint.cpp | 5 +- src/core/stokesian_dynamics/sd_interface.cpp | 2 - src/core/thermostat.cpp | 34 +++++------ src/core/thermostat.hpp | 20 +++---- src/python/espressomd/interactions.pxd | 6 +- src/python/espressomd/interactions.pyx | 6 +- src/python/espressomd/thermostat.pxd | 58 +++++++++---------- src/python/espressomd/thermostat.pyx | 32 +++++----- 8 files changed, 74 insertions(+), 89 deletions(-) diff --git a/src/core/constraints/ShapeBasedConstraint.cpp b/src/core/constraints/ShapeBasedConstraint.cpp index bac04384a36..cd2bbe9d835 100644 --- a/src/core/constraints/ShapeBasedConstraint.cpp +++ b/src/core/constraints/ShapeBasedConstraint.cpp @@ -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 { @@ -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)) { @@ -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 } diff --git a/src/core/stokesian_dynamics/sd_interface.cpp b/src/core/stokesian_dynamics/sd_interface.cpp index 73d657dcb45..28859312beb 100644 --- a/src/core/stokesian_dynamics/sd_interface.cpp +++ b/src/core/stokesian_dynamics/sd_interface.cpp @@ -39,8 +39,6 @@ #include -extern StokesianThermostat stokesian; - namespace { /* type for particle data transfer between nodes */ struct SD_particle_data { diff --git a/src/core/thermostat.cpp b/src/core/thermostat.cpp index e6693e255e2..d6e67d7905c 100644 --- a/src/core/thermostat.cpp +++ b/src/core/thermostat.cpp @@ -21,7 +21,8 @@ /** \file * Implementation of \ref thermostat.hpp. */ -#include + +#include "config.hpp" #include "bonded_interactions/thermalized_bond.hpp" #include "communication.hpp" @@ -30,6 +31,8 @@ #include "npt.hpp" #include "thermostat.hpp" +#include + #include int thermo_switch = THERMO_OFF; @@ -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) { \ @@ -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); \ } \ @@ -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 = {}; @@ -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(); } } diff --git a/src/core/thermostat.hpp b/src/core/thermostat.hpp index 579e2e05a1e..e36fed17224 100644 --- a/src/core/thermostat.hpp +++ b/src/core/thermostat.hpp @@ -100,8 +100,8 @@ struct BaseThermostat { void set_rng_counter(uint64_t value) { m_rng_counter = Utils::Counter(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: @@ -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) @@ -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(); diff --git a/src/python/espressomd/interactions.pxd b/src/python/espressomd/interactions.pxd index d535c94c8f8..d7cc06ec3ea 100644 --- a/src/python/espressomd/interactions.pxd +++ b/src/python/espressomd/interactions.pxd @@ -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 @@ -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) diff --git a/src/python/espressomd/interactions.pyx b/src/python/espressomd/interactions.pyx index e134f820cb3..e6204a5495a 100644 --- a/src/python/espressomd/interactions.pyx +++ b/src/python/espressomd/interactions.pyx @@ -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: diff --git a/src/python/espressomd/thermostat.pxd b/src/python/espressomd/thermostat.pxd index a167e96555e..fb083f621ff 100644 --- a/src/python/espressomd/thermostat.pxd +++ b/src/python/espressomd/thermostat.pxd @@ -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) @@ -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) diff --git a/src/python/espressomd/thermostat.pyx b/src/python/espressomd/thermostat.pyx index 7d995ae8869..33bcd22c786 100644 --- a/src/python/espressomd/thermostat.pyx +++ b/src/python/espressomd/thermostat.pyx @@ -149,8 +149,8 @@ cdef class Thermostat: lang_dict["type"] = "LANGEVIN" lang_dict["kT"] = temperature lang_dict["act_on_virtual"] = thermo_virtual - lang_dict["seed"] = langevin_get_rng_seed() - lang_dict["counter"] = langevin_get_rng_counter() + lang_dict["seed"] = langevin.rng_seed() + lang_dict["counter"] = langevin.rng_counter() IF PARTICLE_ANISOTROPY: lang_dict["gamma"] = [langevin.gamma[0], langevin.gamma[1], @@ -173,8 +173,8 @@ cdef class Thermostat: lang_dict["type"] = "BROWNIAN" lang_dict["kT"] = temperature lang_dict["act_on_virtual"] = thermo_virtual - lang_dict["seed"] = brownian_get_rng_seed() - lang_dict["counter"] = brownian_get_rng_counter() + lang_dict["seed"] = brownian.rng_seed() + lang_dict["counter"] = brownian.rng_counter() IF PARTICLE_ANISOTROPY: lang_dict["gamma"] = [brownian.gamma[0], brownian.gamma[1], @@ -204,8 +204,8 @@ cdef class Thermostat: npt_dict = {} npt_dict["type"] = "NPT_ISO" npt_dict["kT"] = temperature - npt_dict["seed"] = npt_iso_get_rng_seed() - npt_dict["counter"] = npt_iso_get_rng_counter() + npt_dict["seed"] = npt_iso.rng_seed() + npt_dict["counter"] = npt_iso.rng_counter() npt_dict["gamma0"] = npt_iso.gamma0 npt_dict["gammav"] = npt_iso.gammav npt_dict.update(nptiso) @@ -215,16 +215,16 @@ cdef class Thermostat: dpd_dict = {} dpd_dict["type"] = "DPD" dpd_dict["kT"] = temperature - dpd_dict["seed"] = dpd_get_rng_seed() - dpd_dict["counter"] = dpd_get_rng_counter() + dpd_dict["seed"] = dpd.rng_seed() + dpd_dict["counter"] = dpd.rng_counter() thermo_list.append(dpd_dict) if thermo_switch & THERMO_SD: IF STOKESIAN_DYNAMICS: sd_dict = {} sd_dict["type"] = "SD" sd_dict["kT"] = get_sd_kT() - sd_dict["seed"] = stokesian_get_rng_seed() - sd_dict["counter"] = stokesian_get_rng_counter() + sd_dict["seed"] = stokesian.rng_seed() + sd_dict["counter"] = stokesian.rng_counter() thermo_list.append(sd_dict) return thermo_list @@ -357,7 +357,7 @@ cdef class Thermostat: "diagonal elements of the gamma_rotation tensor must be positive numbers") # Seed is required if the RNG is not initialized - if seed is None and langevin_is_seed_required(): + if seed is None and langevin.is_seed_required(): raise ValueError( "A seed has to be given as keyword argument on first activation of the thermostat") @@ -507,7 +507,7 @@ cdef class Thermostat: "diagonal elements of the gamma_rotation tensor must be positive numbers") # Seed is required if the RNG is not initialized - if seed is None and brownian_is_seed_required(): + if seed is None and brownian.is_seed_required(): raise ValueError( "A seed has to be given as keyword argument on first activation of the thermostat") @@ -656,7 +656,7 @@ cdef class Thermostat: raise ValueError("temperature must be a positive number") # Seed is required if the RNG is not initialized - if seed is None and npt_iso_is_seed_required(): + if seed is None and npt_iso.is_seed_required(): raise ValueError( "A seed has to be given as keyword argument on first activation of the thermostat") @@ -701,7 +701,7 @@ cdef class Thermostat: raise ValueError("temperature must be a positive number") # Seed is required if the RNG is not initialized - if seed is None and dpd_is_seed_required(): + if seed is None and dpd.is_seed_required(): raise ValueError( "A seed has to be given as keyword argument on first activation of the thermostat") @@ -739,7 +739,7 @@ cdef class Thermostat: if (kT is None) or (kT == 0): set_sd_kT(0.0) - if stokesian_is_seed_required(): + if stokesian.is_seed_required(): stokesian_set_rng_seed(0) else: utils.check_type_or_throw_except( @@ -747,7 +747,7 @@ cdef class Thermostat: set_sd_kT(kT) # Seed is required if the RNG is not initialized - if seed is None and stokesian_is_seed_required(): + if seed is None and stokesian.is_seed_required(): raise ValueError( "A seed has to be given as keyword argument on first activation of the thermostat") if seed is not None: