ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
script_interface/accumulators/AccumulatorBase.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
25
28
29#include <memory>
30#include <stdexcept>
31#include <string>
32#include <vector>
33
34namespace ScriptInterface {
35namespace Accumulators {
36
37class AccumulatorBase // LCOV_EXCL_LINE
38 : public AutoParameters<AccumulatorBase> {
39public:
41 add_parameters({{"delta_N",
42 [this](const Variant &v) {
43 auto const delta_N = get_value<int>(v);
45 if (delta_N < 1) {
46 throw std::runtime_error("delta_N must be >= 1");
47 }
48 });
49 accumulator()->delta_N() = delta_N;
50 },
51 [this]() { return accumulator()->delta_N(); }}});
52 }
53 Variant do_call_method(std::string const &method,
54 VariantMap const &parameters) override {
55 if (method == "shape") {
56 auto const shape = accumulator()->shape();
57 return std::vector<int>{shape.begin(), shape.end()};
58 }
59 if (method == "reload_from_checkpoint") {
60 accumulator()->set_internal_state(
61 get_value<std::string>(parameters, "state"));
62 return {};
63 }
64 return {};
65 }
66 virtual std::shared_ptr<const ::Accumulators::AccumulatorBase>
67 accumulator() const = 0;
68 virtual std::shared_ptr<::Accumulators::AccumulatorBase> accumulator() = 0;
69
70protected:
71 auto get_core_system_pointer(VariantMap const &params) const {
72 auto const *system = &::System::get_system();
73 if (params.contains("system")) {
74 system =
76 ->get_system());
77 }
78 return system;
79 }
80
81private:
82 std::string get_internal_state() const override {
83 return accumulator()->get_internal_state();
84 }
85
86 void set_internal_state(std::string const &state) override {
87 call_method("reload_from_checkpoint", {{"state", state}});
88 }
89};
90
91} // namespace Accumulators
92} // namespace ScriptInterface
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
virtual std::shared_ptr< const ::Accumulators::AccumulatorBase > accumulator() const =0
virtual std::shared_ptr<::Accumulators::AccumulatorBase > accumulator()=0
void add_parameters(std::vector< AutoParameter > &&params)
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
Variant call_method(const std::string &name, const VariantMap &params)
Call a method on the object.
Context * context() const
Responsible context.
make_recursive_variant< ObjectRef > Variant
Possible types for parameters.
Definition Variant.hpp:131
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
System & get_system()