42template <
typename CoreClass>
83 throw std::domain_error(
"Parameter '" +
name +
"' cannot be negative");
88 std::string
const &
name)
const {
90 throw std::domain_error(
"Parameter '" +
name +
"' cannot be negative");
100 virtual std::shared_ptr<CoreThermostat> &
106 throw std::invalid_argument(
"Parameter 'seed' is needed on first "
107 "activation of the thermostat");
112 auto const &v =
params.at(key);
113 if (key ==
"is_active") {
123 template <
typename T>
129 auto const value = get_value<T>(v);
132 m_handle.get()->*member = std::move(value);
134 [
this, member]() {
return m_handle.get()->*member; }};
137 template <
typename T>
146#ifdef PARTICLE_ANISOTROPY
147 static_assert(std::is_same_v<T, Utils::Vector3d>);
149 if (is_type<int>(v) or is_type<double>(v)) {
150 gamma = T::broadcast(get_value<double>(v));
152 gamma = get_value<T>(v);
155 auto const gamma = get_value<T>(v);
163 auto const gamma =
m_handle.get()->*member;
171 for (
auto key : {std::string(
"seed"), std::string(
"philox_counter")}) {
172 if (
params.count(key) == 0ul) {
182 if (
name ==
"override_philox_counter") {
220 auto const value = get_value<double>(
params,
"kT");
224 return {std::nullopt};
228 auto lock = std::make_shared<bool>(
false);
238 auto const seed =
m_handle->rng_seed();
239 assert(seed <=
static_cast<uint32_t
>(std::numeric_limits<int>::max()));
240 return static_cast<int>(seed);
244 auto const counter =
m_handle->rng_counter();
245 assert(counter <=
static_cast<uint64_t
>(std::numeric_limits<int>::max()));
246 return static_cast<int>(counter);
250 auto const seed = get_value<int>(value);
252 throw std::domain_error(
"Parameter 'seed' must be a positive integer");
254 assert(
static_cast<uint64_t
>(seed) <=
255 static_cast<uint64_t
>(std::numeric_limits<uint32_t>::max()));
256 m_handle->rng_initialize(
static_cast<uint32_t
>(seed));
260 auto const counter = get_value<int>(value);
261 assert(counter >= 0);
262 assert(
static_cast<uint64_t
>(counter) <=
263 std::numeric_limits<uint64_t>::max());
264 m_handle->set_rng_counter(
static_cast<uint64_t
>(counter));
268 m_handle = std::make_shared<CoreThermostat>();
271 set_new_parameters(
params);
276 std::shared_ptr<::System::System> system) {
279 new_params[
"__global_kT"] = system->thermostat->kT;
280 new_params[
"__check_rng_state"] =
true;
282 m_handle = std::make_shared<CoreThermostat>();
283 set_new_parameters(new_params);
299 std::shared_ptr<CoreThermostat> &
322 if (
params.count(
"gamma_rotation") == 0ul and
params.count(
"gamma")) {
331 std::shared_ptr<CoreThermostat> &
354 if (
params.count(
"gamma_rotation") == 0ul and
params.count(
"gamma")) {
364 std::shared_ptr<CoreThermostat> &
383 std::shared_ptr<CoreThermostat> &
385 return thermostat.
lb;
397 auto const gamma = get_value<double>(v);
413 get_value<std::shared_ptr<walberla::LBFluid>>(
params,
"LB_fluid");
414 auto const value = get_value<double>(obj->get_parameter(
"kT"));
423 get_value<double>(
params,
"__global_kT") != 0.;
430 std::shared_ptr<CoreThermostat> &
432 return thermostat.
dpd;
440#ifdef STOKESIAN_DYNAMICS
442 std::shared_ptr<CoreThermostat> &
453 std::shared_ptr<CoreThermostat> &
463 std::shared_ptr<Langevin> langevin;
464 std::shared_ptr<Brownian> brownian;
466 std::shared_ptr<IsotropicNpt> npt_iso;
469 std::shared_ptr<LBThermostat> lb;
472 std::shared_ptr<DPDThermostat> dpd;
474#ifdef STOKESIAN_DYNAMICS
475 std::shared_ptr<Stokesian> stokesian;
477 std::shared_ptr<ThermalizedBond> thermalized_bond;
478 std::shared_ptr<::Thermostat::Thermostat> m_handle;
479 std::unique_ptr<VariantMap> m_params;
481 template <
typename Fun>
void apply(Fun fun) {
493#ifdef STOKESIAN_DYNAMICS
496 fun(*thermalized_bond);
500 template <
typename T>
505 auto &thermostat = this->*member;
507 throw WriteError{
name};
509 thermostat = get_value<T>(v);
511 [
this, member]() {
return this->*member; }};
514 template <
typename T>
517 auto const original_kT = m_handle->kT;
518 std::optional<double> new_kT;
520 [&]() { new_kT = thermostat->extract_kT(
params); });
521 auto const thermo_flag = thermostat->get_thermo_flag();
526 auto const was_active = thermostat->is_activated();
528 auto read_write_lock = thermostat->release_lock();
532 m_handle->kT = *new_kT;
535 m_handle->thermo_switch |= thermo_flag;
537 auto success =
false;
539 m_handle->kT = original_kT;
541 m_handle->thermo_switch |= thermo_flag;
542 thermostat->bind_system(
m_system.lock());
548 "An exception occurred when setting up the thermostat. "
549 "An exception also occurred when attempting to restore the "
550 "original thermostat. The system is now in an invalid state.");
558 auto const thermo_flag = thermostat.get_thermo_flag();
559 if (m_handle->thermo_switch & thermo_flag) {
560 thermostat.detach_system();
561 m_handle->thermo_switch &= ~thermo_flag;
562 if (m_handle->thermo_switch == 0) {
571 auto const thermo_switch = m_handle->thermo_switch;
572 if (thermo_switch !=
THERMO_OFF and thermo_switch != thermo_flag and
573 thermo_switch !=
THERMO_BOND and not same_kT and old_kT >= 0.) {
574 throw std::runtime_error(
575 "Cannot set parameter 'kT' to " + std::to_string(new_kT) +
576 ": there are currently active thermostats with kT=" +
577 std::to_string(old_kT));
581 m_handle->kT = new_kT;
592 return (m_handle->kT >= 0.) ?
Variant{m_handle->kT}
606#ifdef STOKESIAN_DYNAMICS
615 if (
name ==
"set_langevin") {
619 if (
name ==
"set_brownian") {
624 if (
name ==
"set_npt") {
630 if (
name ==
"set_lb") {
636 if (
name ==
"set_dpd") {
641#ifdef STOKESIAN_DYNAMICS
642 if (
name ==
"set_stokesian") {
647 if (
name ==
"set_thermalized_bond") {
651 if (
name ==
"turn_off") {
653 assert(m_handle->thermo_switch ==
THERMO_OFF);
661 m_params = std::make_unique<VariantMap>(
params);
665 assert(m_params !=
nullptr);
667 auto const &
params = *m_params;
669 reload_checkpointed_thermostats(
params);
674 if (not
context()->is_head_node()) {
677 make_default_constructed_thermostats();
687 auto const &v =
params.at(key);
692 m_handle->kT = get_value<double>(
params,
"kT");
694 apply([
this](
auto &thermostat) {
695 if (get_value<bool>(thermostat.get_parameter(
"is_active"))) {
696 thermostat.bind_system(m_system.lock());
697 m_handle->thermo_switch |= thermostat.get_thermo_flag();
707 void make_default_constructed_thermostats() {
708 assert(
context()->is_head_node());
709 auto const make_thermostat = [
this](
char const *
name,
char const *so_name) {
712 make_thermostat(
"langevin",
"Thermostat::Langevin");
713 make_thermostat(
"brownian",
"Thermostat::Brownian");
715 make_thermostat(
"npt_iso",
"Thermostat::IsotropicNpt");
718 make_thermostat(
"lb",
"Thermostat::LB");
721 make_thermostat(
"dpd",
"Thermostat::DPD");
723#ifdef STOKESIAN_DYNAMICS
724 make_thermostat(
"stokesian",
"Thermostat::Stokesian");
726 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::shared_ptr< CoreThermostat > & get_member_handle(::Thermostat::Thermostat &thermostat) 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::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::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::shared_ptr< CoreThermostat > & get_member_handle(::Thermostat::Thermostat &thermostat) override
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
::ThermostatFlags get_thermo_flag() const final
::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 Vector< T, N > broadcast(T const &s)
Create a vector that has all entries set to one 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