43template <
typename CoreClass>
84 throw std::domain_error(
"Parameter '" +
name +
"' cannot be negative");
89 std::string
const &
name)
const {
91 throw std::domain_error(
"Parameter '" +
name +
"' cannot be negative");
101 virtual std::shared_ptr<CoreThermostat> &
107 throw std::invalid_argument(
"Parameter 'seed' is needed on first "
108 "activation of the thermostat");
110 check_required_parameters(
params);
113 if (
params.contains(key)) {
114 auto const &v =
params.at(key);
115 if (key ==
"is_active") {
128 auto name = std::string(required);
130 throw std::runtime_error(
"Parameter '" +
name +
"' is missing");
136 template <
typename T>
142 auto const value = get_value<T>(v);
145 m_handle.get()->*member = std::move(value);
147 [
this, member]() {
return m_handle.get()->*member; }};
150 template <
typename T>
159#ifdef PARTICLE_ANISOTROPY
160 static_assert(std::is_same_v<T, Utils::Vector3d>);
162 if (is_type<int>(v) or is_type<double>(v)) {
163 gamma = T::broadcast(get_value<double>(v));
165 gamma = get_value<T>(v);
168 auto const gamma = get_value<T>(v);
176 auto const gamma =
m_handle.get()->*member;
184 for (
auto key : {std::string(
"seed"), std::string(
"philox_counter")}) {
185 if (not
params.contains(key)) {
195 if (
name ==
"override_philox_counter") {
232 if (
params.contains(
"kT")) {
233 auto const value = get_value<double>(
params,
"kT");
237 return {std::nullopt};
241 auto lock = std::make_shared<bool>(
false);
251 auto const seed =
m_handle->rng_seed();
252 assert(seed <=
static_cast<uint32_t
>(std::numeric_limits<int>::max()));
253 return static_cast<int>(seed);
257 auto const counter =
m_handle->rng_counter();
258 assert(counter <=
static_cast<uint64_t
>(std::numeric_limits<int>::max()));
259 return static_cast<int>(counter);
263 auto const seed = get_value<int>(value);
265 throw std::domain_error(
"Parameter 'seed' must be a positive integer");
267 assert(
static_cast<uint64_t
>(seed) <=
268 static_cast<uint64_t
>(std::numeric_limits<uint32_t>::max()));
269 m_handle->rng_initialize(
static_cast<uint32_t
>(seed));
273 auto const counter = get_value<int>(value);
274 assert(counter >= 0);
275 assert(
static_cast<uint64_t
>(counter) <=
276 std::numeric_limits<uint64_t>::max());
277 m_handle->set_rng_counter(
static_cast<uint64_t
>(counter));
281 m_handle = std::make_shared<CoreThermostat>();
284 set_new_parameters(
params);
289 std::shared_ptr<::System::System> system) {
292 new_params[
"__global_kT"] = system->thermostat->kT;
293 new_params[
"__check_rng_state"] =
true;
295 m_handle = std::make_shared<CoreThermostat>();
296 set_new_parameters(new_params);
312 std::shared_ptr<CoreThermostat> &
318 static constexpr std::array names{std::string_view(
"gamma")};
340 if (not
params.contains(
"gamma_rotation") and
params.contains(
"gamma")) {
349 std::shared_ptr<CoreThermostat> &
355 static constexpr std::array names{std::string_view(
"gamma")};
377 if (not
params.contains(
"gamma_rotation") and
params.contains(
"gamma")) {
387 std::shared_ptr<CoreThermostat> &
393 static constexpr std::array names{std::string_view(
"gamma0"),
394 std::string_view(
"gammav")};
412 std::shared_ptr<CoreThermostat> &
414 return thermostat.
lb;
426 auto const gamma = get_value<double>(v);
442 get_value<std::shared_ptr<walberla::LBFluid>>(
params,
"LB_fluid");
443 auto const value = get_value<double>(obj->get_parameter(
"kT"));
452 get_value<double>(
params,
"__global_kT") != 0.;
456 static constexpr std::array names{std::string_view(
"gamma")};
464 std::shared_ptr<CoreThermostat> &
466 return thermostat.
dpd;
478#ifdef STOKESIAN_DYNAMICS
480 std::shared_ptr<CoreThermostat> &
495 std::shared_ptr<CoreThermostat> &
509 std::shared_ptr<Langevin> langevin;
510 std::shared_ptr<Brownian> brownian;
512 std::shared_ptr<IsotropicNpt> npt_iso;
515 std::shared_ptr<LBThermostat> lb;
518 std::shared_ptr<DPDThermostat> dpd;
520#ifdef STOKESIAN_DYNAMICS
521 std::shared_ptr<Stokesian> stokesian;
523 std::shared_ptr<ThermalizedBond> thermalized_bond;
524 std::shared_ptr<::Thermostat::Thermostat> m_handle;
525 std::unique_ptr<VariantMap> m_params;
527 template <
typename Fun>
void apply(Fun fun) {
539#ifdef STOKESIAN_DYNAMICS
542 fun(*thermalized_bond);
546 template <
typename T>
551 auto &thermostat = this->*member;
553 throw WriteError{
name};
555 thermostat = get_value<T>(v);
557 [
this, member]() {
return this->*member; }};
560 template <
typename T>
563 auto const original_kT = m_handle->kT;
564 std::optional<double> new_kT;
566 [&]() { new_kT = thermostat->extract_kT(
params); });
567 auto const thermo_flag = thermostat->get_thermo_flag();
572 auto const was_active = thermostat->is_activated();
574 auto read_write_lock = thermostat->release_lock();
578 m_handle->kT = *new_kT;
581 m_handle->thermo_switch |= thermo_flag;
583 auto success =
false;
585 m_handle->kT = original_kT;
587 m_handle->thermo_switch |= thermo_flag;
588 thermostat->bind_system(
m_system.lock());
594 "An exception occurred when setting up the thermostat. "
595 "An exception also occurred when attempting to restore the "
596 "original thermostat. The system is now in an invalid state.");
604 auto const thermo_flag = thermostat.get_thermo_flag();
605 if (m_handle->thermo_switch & thermo_flag) {
606 thermostat.detach_system();
607 m_handle->thermo_switch &= ~thermo_flag;
608 if (m_handle->thermo_switch == 0) {
617 auto const thermo_switch = m_handle->thermo_switch;
618 if (thermo_switch !=
THERMO_OFF and thermo_switch != thermo_flag and
619 thermo_switch !=
THERMO_BOND and not same_kT and old_kT >= 0.) {
620 throw std::runtime_error(
621 "Cannot set parameter 'kT' to " + std::to_string(new_kT) +
622 ": there are currently active thermostats with kT=" +
623 std::to_string(old_kT));
627 m_handle->kT = new_kT;
638 return (m_handle->kT >= 0.) ?
Variant{m_handle->kT}
652#ifdef STOKESIAN_DYNAMICS
661 if (
params.contains(
"act_on_virtual")) {
663 throw std::runtime_error(
664 name +
"() got an unexpected keyword argument 'act_on_virtual'");
667 if (
name ==
"set_langevin") {
671 if (
name ==
"set_brownian") {
676 if (
name ==
"set_npt") {
682 if (
name ==
"set_lb") {
688 if (
name ==
"set_dpd") {
693#ifdef STOKESIAN_DYNAMICS
694 if (
name ==
"set_stokesian") {
699 if (
name ==
"set_thermalized_bond") {
703 if (
name ==
"turn_off") {
705 assert(m_handle->thermo_switch ==
THERMO_OFF);
713 m_params = std::make_unique<VariantMap>(
params);
717 assert(m_params !=
nullptr);
719 auto const &
params = *m_params;
721 reload_checkpointed_thermostats(
params);
726 if (not
context()->is_head_node()) {
729 make_default_constructed_thermostats();
739 auto const &v =
params.at(key);
744 m_handle->kT = get_value<double>(
params,
"kT");
746 apply([
this](
auto &thermostat) {
747 if (get_value<bool>(thermostat.get_parameter(
"is_active"))) {
748 thermostat.bind_system(m_system.lock());
749 m_handle->thermo_switch |= thermostat.get_thermo_flag();
759 void make_default_constructed_thermostats() {
760 assert(
context()->is_head_node());
761 auto const make_thermostat = [
this](
char const *
name,
char const *so_name) {
764 make_thermostat(
"langevin",
"Thermostat::Langevin");
765 make_thermostat(
"brownian",
"Thermostat::Brownian");
767 make_thermostat(
"npt_iso",
"Thermostat::IsotropicNpt");
770 make_thermostat(
"lb",
"Thermostat::LB");
773 make_thermostat(
"dpd",
"Thermostat::DPD");
775#ifdef STOKESIAN_DYNAMICS
776 make_thermostat(
"stokesian",
"Thermostat::Stokesian");
778 make_thermostat(
"thermalized_bond",
"Thermostat::ThermalizedBond");
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.
boost::string_ref name() const
Context * context() const
Responsible context.
void set_parameter(const std::string &name, const Variant &value)
Set single parameter.
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
void on_thermostat_param_change()
std::shared_ptr< Thermostat::Thermostat > thermostat
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)
Create a vector that has all entries set to the same value.
Implementation in thermostat.cpp.
std::unordered_map< std::string, Variant > VariantMap
boost::make_recursive_variant< None, bool, int, std::size_t, double, std::string, ObjectRef, Utils::Vector3b, Utils::Vector3i, Utils::Vector2d, Utils::Vector3d, Utils::Vector4d, std::vector< int >, std::vector< double >, std::vector< boost::recursive_variant_ >, std::unordered_map< int, boost::recursive_variant_ >, std::unordered_map< std::string, boost::recursive_variant_ > >::type Variant
Possible types for parameters.
bool is_none(Variant const &v)
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.
static SteepestDescentParameters params
Currently active steepest descent instance.
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