32#include <boost/algorithm/string/join.hpp>
40#include <unordered_map>
50 std::string m_identifier;
51 std::string m_base_folder;
53 std::shared_ptr<::VTKHandle> m_vtk_handle;
54 std::weak_ptr<Field> m_field;
56 std::vector<Variant> m_pending_arguments;
58 [[nodiscard]]
auto get_vtk_uid()
const {
59 return m_base_folder +
'/' + m_identifier;
62 [[nodiscard]] std::shared_ptr<Field> get_field_instance()
const {
63 if (
auto const field = m_field.lock()) {
66 auto const has_lattice_expired = m_pending_arguments.empty();
67 auto const err_expired =
"Attempted access to an expired lattice object";
68 auto const err_detached =
"This VTK object isn't attached to a lattice";
69 throw std::runtime_error(has_lattice_expired ? err_expired : err_detached);
72 virtual std::unordered_map<std::string, int>
const &
get_obs_map()
const = 0;
74 [[nodiscard]]
auto get_valid_observable_names()
const {
75 std::vector<std::string> names{};
77 names.emplace_back(kv.first);
79 std::sort(names.begin(), names.end());
84 deserialize_obs_flag(std::vector<std::string>
const &names)
const {
87 for (
auto const &
name : names) {
88 if (obs_map.count(
name) == 0) {
89 auto const valid_names = get_valid_observable_names();
90 std::stringstream message;
91 message <<
"Only the following VTK observables are supported: ['"
92 << boost::algorithm::join(valid_names,
"', '") <<
"'], got '"
94 throw std::invalid_argument(message.str());
96 flag |= obs_map.at(
name);
101 [[nodiscard]]
Variant serialize_obs_flag(
int flag)
const {
102 std::vector<Variant> observables{};
104 if (flag & kv.second) {
105 observables.emplace_back(kv.first);
115 {
"enabled", read_only, [
this]() {
return m_vtk_handle->enabled; }},
116 {
"delta_N", read_only, [
this]() {
return m_delta_N; }},
117 {
"vtk_uid", read_only, [
this]() {
return get_vtk_uid(); }},
118 {
"identifier", read_only, [
this]() {
return m_identifier; }},
119 {
"base_folder", read_only, [
this]() {
return m_base_folder; }},
120 {
"prefix", read_only, [
this]() {
return m_prefix; }},
121 {
"observables", read_only,
122 [
this]() {
return serialize_obs_flag(m_flag_obs); }},
123 {
"execution_count", read_only,
124 [
this]() {
return m_vtk_handle->execution_count; }},
132 m_delta_N = get_value<int>(
params,
"delta_N");
133 m_identifier = get_value<std::string>(
params,
"identifier");
134 m_base_folder = get_value<std::string>(
params,
"base_folder");
135 m_prefix = get_value<std::string>(
params,
"prefix");
136 auto const is_enabled = get_value<bool>(
params,
"enabled");
137 auto const execution_count = get_value<int>(
params,
"execution_count");
139 m_flag_obs = deserialize_obs_flag(
142 throw std::domain_error(
"Parameter 'delta_N' must be >= 0");
144 if (m_identifier.empty()) {
145 throw std::domain_error(
"Parameter 'identifier' cannot be empty");
147 if (m_identifier.find(std::filesystem::path::preferred_separator) !=
149 throw std::invalid_argument(
150 "Parameter 'identifier' cannot be a filepath");
153 m_pending_arguments.emplace_back(is_enabled);
154 m_pending_arguments.emplace_back(execution_count);
160 if (
name ==
"enable") {
162 if (m_delta_N == 0) {
163 throw std::runtime_error(
"Manual VTK callbacks cannot be enabled");
165 get_field_instance()->switch_vtk(get_vtk_uid(),
true);
169 if (
name ==
"disable") {
171 if (m_delta_N == 0) {
172 throw std::runtime_error(
"Manual VTK callbacks cannot be disabled");
174 get_field_instance()->switch_vtk(get_vtk_uid(),
false);
178 if (
name ==
"write") {
181 throw std::runtime_error(
"Automatic VTK callbacks cannot be "
182 "triggered manually");
184 get_field_instance()->write_vtk(get_vtk_uid());
188 if (
name ==
"get_valid_observable_names") {
200 auto const was_attached_once = m_pending_arguments.empty();
201 if (not m_field.expired()) {
202 throw std::runtime_error(
"Cannot attach VTK object to multiple lattices");
204 if (was_attached_once) {
205 throw std::runtime_error(
"Detached VTK objects cannot be attached again");
207 assert(m_pending_arguments.size() == 2u);
208 auto const is_enabled = get_value<bool>(m_pending_arguments[0]);
209 auto const execution_count = get_value<int>(m_pending_arguments[1]);
212 auto instance = get_field_instance();
214 instance->create_vtk(m_delta_N, execution_count, m_flag_obs, m_units,
215 m_identifier, m_base_folder, m_prefix);
216 if (m_delta_N and not is_enabled) {
217 instance->switch_vtk(get_vtk_uid(),
false);
219 m_pending_arguments.clear();
std::unordered_map< std::string, double > units_map
Bind parameters in the script interface.
void add_parameters(std::vector< AutoParameter > &¶ms)
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
boost::string_ref name() const
Context * context() const
Responsible context.
void detach_from_lattice()
void do_construct(VariantMap const ¶ms) override
void attach_to_lattice(std::weak_ptr< Field > field, ::LatticeModel::units_map const &units)
virtual std::unordered_map< std::string, int > const & get_obs_map() const =0
Variant do_call_method(std::string const &name, VariantMap const ¶ms) override
This file contains the defaults for ESPResSo.
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
std::unordered_map< std::string, Variant > VariantMap
auto make_unordered_map_of_variants(std::unordered_map< K, V > const &v)
auto make_vector_of_variants(std::vector< T > const &v)
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.
static SteepestDescentParameters params
Currently active steepest descent instance.
static constexpr const ReadOnly read_only