29#ifdef ESPRESSO_WALBERLA
44template <
typename CoreClass>
73 system.on_thermostat_param_change();
79 system.on_thermostat_param_change();
85 throw std::domain_error(
"Parameter '" +
name +
"' cannot be negative");
90 std::string
const &
name)
const {
92 throw std::domain_error(
"Parameter '" +
name +
"' cannot be negative");
102 virtual std::shared_ptr<CoreThermostat> &
105 void set_new_parameters(
VariantMap const ¶ms) {
108 throw std::invalid_argument(
"Parameter 'seed' is needed on first "
109 "activation of the thermostat");
111 check_required_parameters(params);
114 if (params.contains(
key)) {
115 auto const &v = params.at(
key);
116 if (
key ==
"is_active") {
127 void check_required_parameters(
VariantMap const ¶ms)
const {
130 if (
not params.contains(
name)) {
131 throw std::runtime_error(
"Parameter '" +
name +
"' is missing");
137 template <
typename T>
151 template <
typename T>
160#ifdef ESPRESSO_PARTICLE_ANISOTROPY
161 static_assert(std::is_same_v<T, Utils::Vector3d>);
185 for (
auto key : {std::string(
"seed"), std::string(
"philox_counter")}) {
186 if (
not params.contains(
key)) {
196 if (
name ==
"override_philox_counter") {
233 if (params.contains(
"kT")) {
238 return {std::nullopt};
242 auto lock = std::make_shared<bool>(
false);
252 auto const seed =
m_handle->rng_seed();
253 assert(seed <=
static_cast<uint32_t>(std::numeric_limits<int>::max()));
254 return static_cast<int>(seed);
258 auto const counter =
m_handle->rng_counter();
259 assert(counter <=
static_cast<uint64_t>(std::numeric_limits<int>::max()));
260 return static_cast<int>(counter);
266 throw std::domain_error(
"Parameter 'seed' must be a positive integer");
269 static_cast<uint64_t>(std::numeric_limits<uint32_t>::max()));
277 std::numeric_limits<uint64_t>::max());
282 m_handle = std::make_shared<CoreThermostat>();
283 if (
not params.empty()) {
285 set_new_parameters(params);
290 std::shared_ptr<::System::System>
system) {
296 m_handle = std::make_shared<CoreThermostat>();
313 std::shared_ptr<CoreThermostat> &
319 static constexpr std::array
names{std::string_view(
"gamma")};
327#ifdef ESPRESSO_ROTATION
339#ifdef ESPRESSO_ROTATION
341 if (
not params.contains(
"gamma_rotation")
and params.contains(
"gamma")) {
342 params[
"gamma_rotation"] = params.at(
"gamma");
350 std::shared_ptr<CoreThermostat> &
356 static constexpr std::array
names{std::string_view(
"gamma")};
364#ifdef ESPRESSO_ROTATION
376#ifdef ESPRESSO_ROTATION
378 if (
not params.contains(
"gamma_rotation")
and params.contains(
"gamma")) {
379 params[
"gamma_rotation"] = params.at(
"gamma");
388 std::shared_ptr<CoreThermostat> &
394 static constexpr std::array
names{std::string_view(
"gamma0"),
395 std::string_view(
"gammav")};
411#ifdef ESPRESSO_WALBERLA
413 std::shared_ptr<CoreThermostat> &
415 return thermostat.
lb;
457 static constexpr std::array
names{std::string_view(
"gamma")};
465 std::shared_ptr<CoreThermostat> &
467 return thermostat.
dpd;
479#ifdef ESPRESSO_STOKESIAN_DYNAMICS
481 std::shared_ptr<CoreThermostat> &
496 std::shared_ptr<CoreThermostat> &
510 std::shared_ptr<Langevin> langevin;
511 std::shared_ptr<Brownian> brownian;
513 std::shared_ptr<IsotropicNpt> npt_iso;
515#ifdef ESPRESSO_WALBERLA
516 std::shared_ptr<LBThermostat> lb;
519 std::shared_ptr<DPDThermostat> dpd;
521#ifdef ESPRESSO_STOKESIAN_DYNAMICS
522 std::shared_ptr<Stokesian> stokesian;
524 std::shared_ptr<ThermalizedBond> thermalized_bond;
525 std::shared_ptr<::Thermostat::Thermostat> m_handle;
526 std::unique_ptr<VariantMap> m_params;
528 template <
typename Fun>
void apply(
Fun fun) {
534#ifdef ESPRESSO_WALBERLA
540#ifdef ESPRESSO_STOKESIAN_DYNAMICS
543 fun(*thermalized_bond);
547 template <
typename T>
552 auto &thermostat = this->*
member;
554 throw WriteError{
name};
561 template <
typename T>
565 std::optional<double>
new_kT;
567 [&]() {
new_kT = thermostat->extract_kT(params); });
568 auto const thermo_flag = thermostat->get_thermo_flag();
573 auto const was_active = thermostat->is_activated();
589 thermostat->bind_system(
m_system.lock());
595 "An exception occurred when setting up the thermostat. "
596 "An exception also occurred when attempting to restore the "
597 "original thermostat. The system is now in an invalid state.");
605 auto const thermo_flag = thermostat.get_thermo_flag();
607 thermostat.detach_system();
609 if (m_handle->thermo_switch == 0) {
618 auto const thermo_switch = m_handle->thermo_switch;
621 throw std::runtime_error(
622 "Cannot set parameter 'kT' to " + std::to_string(
new_kT) +
623 ": there are currently active thermostats with kT=" +
639 return (m_handle->kT >= 0.) ?
Variant{m_handle->kT}
647#ifdef ESPRESSO_WALBERLA
653#ifdef ESPRESSO_STOKESIAN_DYNAMICS
662 if (params.contains(
"act_on_virtual")) {
664 throw std::runtime_error(
665 name +
"() got an unexpected keyword argument 'act_on_virtual'");
668 if (
name ==
"set_langevin") {
672 if (
name ==
"set_brownian") {
677 if (
name ==
"set_npt") {
682#ifdef ESPRESSO_WALBERLA
683 if (
name ==
"set_lb") {
689 if (
name ==
"set_dpd") {
694#ifdef ESPRESSO_STOKESIAN_DYNAMICS
695 if (
name ==
"set_stokesian") {
700 if (
name ==
"set_thermalized_bond") {
704 if (
name ==
"turn_off") {
710 if (
context()->is_head_node()) {
711 throw std::runtime_error(
712 "Unknown method " +
name +
713 "(). Hint: a feature is probably not compiled in.");
719 m_params = std::make_unique<VariantMap>(params);
723 assert(m_params !=
nullptr);
724 m_handle =
system.thermostat;
725 auto const ¶ms = *m_params;
726 if (
not params.empty()) {
727 reload_checkpointed_thermostats(params);
735 make_default_constructed_thermostats();
742 void reload_checkpointed_thermostats(
VariantMap const ¶ms) {
745 auto const &v = params.at(
key);
752 apply([
this](
auto &thermostat) {
754 thermostat.bind_system(m_system.lock());
755 m_handle->thermo_switch |= thermostat.get_thermo_flag();
765 void make_default_constructed_thermostats() {
775#ifdef ESPRESSO_WALBERLA
781#ifdef ESPRESSO_STOKESIAN_DYNAMICS
ScriptInterface::Context decorates ScriptInterface::ObjectHandle objects with a context: a creation p...
ThermostatFlags
Thermostat flags.
Data structures for bonded interactions.
Bind parameters in the script interface.
void do_set_parameter(const std::string &name, const Variant &value) final
Variant get_parameter(const std::string &name) const final
void add_parameters(std::vector< AutoParameter > &¶ms)
auto const & get_parameter_insertion_order() const
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
virtual std::shared_ptr< ObjectHandle > make_shared(std::string const &name, const VariantMap ¶meters)=0
Get a new reference counted instance of a script interface by name.
Type to indicate no value in Variant.
Context * context() const
Responsible context.
void set_parameter(const std::string &name, const Variant &value)
Set single parameter.
std::string_view name() const
Script interface wrapper for a component of the system class.
auto const & get_system() const
std::weak_ptr<::System::System > m_system
void bind_system(std::shared_ptr<::System::System > const &system)
VariantMap extend_parameters(VariantMap const ¶meters) const override
::ThermostatFlags get_thermo_flag() const final
std::shared_ptr< CoreThermostat > & get_member_handle(::Thermostat::Thermostat &thermostat) override
std::span< std::string_view const > get_required_parameters() const override
std::shared_ptr< CoreThermostat > & get_member_handle(::Thermostat::Thermostat &thermostat) override
std::span< std::string_view const > get_required_parameters() const override
::ThermostatFlags get_thermo_flag() const final
void sanity_checks_positive(double value, std::string const &name) const
void update_and_bind(VariantMap const ¶ms, bool was_active, std::shared_ptr<::System::System > system)
void on_bind_system(::System::System &system) override
void set_rng_counter(Variant const &value)
void set_rng_seed(Variant const &value)
virtual ::ThermostatFlags get_thermo_flag() const =0
auto get_rng_counter() const
auto get_rng_seed() const
virtual std::shared_ptr< CoreThermostat > & get_member_handle(::Thermostat::Thermostat &thermostat)=0
void on_detach_system(::System::System &system) override
virtual bool invalid_rng_state(VariantMap const ¶ms) const
auto make_autoparameter(T CoreThermostat::*member, char const *name)
Variant do_call_method(std::string const &name, VariantMap const ¶ms) override
virtual std::span< std::string_view const > get_required_parameters() const =0
virtual std::optional< double > extract_kT(VariantMap const ¶ms) const
auto is_activated() const
auto make_autogamma(T CoreThermostat::*member, char const *name)
virtual VariantMap extend_parameters(VariantMap const ¶meters) const
std::weak_ptr< bool > m_edit_lock
Basic lock mechanism that follows RAII.
std::shared_ptr< CoreThermostat > m_handle
virtual bool is_seed_required() const
void sanity_checks_positive(Utils::Vector3d const &value, std::string const &name) const
void do_construct(VariantMap const ¶ms) override
std::span< std::string_view const > get_required_parameters() const override
std::shared_ptr< CoreThermostat > & get_member_handle(::Thermostat::Thermostat &thermostat) override
::ThermostatFlags get_thermo_flag() const final
bool invalid_rng_state(VariantMap const ¶ms) const override
::ThermostatFlags get_thermo_flag() const final
std::shared_ptr< CoreThermostat > & get_member_handle(::Thermostat::Thermostat &thermostat) override
std::optional< double > extract_kT(VariantMap const ¶ms) const override
std::span< std::string_view const > get_required_parameters() const override
std::shared_ptr< CoreThermostat > & get_member_handle(::Thermostat::Thermostat &thermostat) override
std::span< std::string_view const > get_required_parameters() const override
VariantMap extend_parameters(VariantMap const ¶meters) const override
::ThermostatFlags get_thermo_flag() const final
std::span< std::string_view const > get_required_parameters() const override
std::shared_ptr< CoreThermostat > & get_member_handle(::Thermostat::Thermostat &thermostat) override
::ThermostatFlags get_thermo_flag() const final
std::span< std::string_view const > get_required_parameters() const override
::ThermostatFlags get_thermo_flag() const final
std::shared_ptr< CoreThermostat > & get_member_handle(::Thermostat::Thermostat &thermostat) override
void update_global_kT(double old_kT, double new_kT, int thermo_flag)
void on_bind_system(::System::System &system) override
auto make_autoparameter(T Thermostat::*member, char const *name)
void turn_thermostat_off(T &thermostat)
void do_construct(VariantMap const ¶ms) override
void setup_thermostat(std::shared_ptr< T > &thermostat, VariantMap const ¶ms)
Variant do_call_method(std::string const &name, VariantMap const ¶ms) override
std::shared_ptr< BrownianThermostat > brownian
std::shared_ptr< ThermalizedBondThermostat > thermalized_bond
std::shared_ptr< LangevinThermostat > langevin
std::shared_ptr< DPDThermostat > dpd
std::shared_ptr< LBThermostat > lb
std::shared_ptr< IsotropicNptThermostat > npt_iso
std::shared_ptr< StokesianThermostat > stokesian
static DEVICE_QUALIFIER constexpr Vector< T, N > broadcast(typename Base::value_type const &value) noexcept
Create a vector that has all entries set to the same value.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
Implementation in thermostat.cpp.
constexpr bool is_none(Variant const &v)
std::unordered_map< std::string, Variant > VariantMap
make_recursive_variant< ObjectRef > Variant
Possible types for parameters.
bool are_kT_equal(double old_kT, double new_kT)
Check that two kT values are close up to a small tolerance.
constexpr GammaType gamma_null
Value for a null friction coefficient.
GammaType gamma
Translational friction coefficient .
GammaType gamma_rotation
Rotational friction coefficient .
double gamma0
Friction coefficient of the particles .
double gammav
Friction coefficient for the box .
GammaType gamma_rotation
Rotational friction coefficient .
GammaType gamma
Translational friction coefficient .
Description and getter/setter for a parameter.
static constexpr const ReadOnly read_only
Recursive variant implementation.