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 : public AutoParameters<AccumulatorBase> {
38public:
40 add_parameters({{"delta_N",
41 [this](const Variant &v) {
42 auto const delta_N = get_value<int>(v);
44 if (delta_N < 1) {
45 throw std::runtime_error("delta_N must be >= 1");
46 }
47 });
48 accumulator()->delta_N() = delta_N;
49 },
50 [this]() { return accumulator()->delta_N(); }}});
51 }
52 Variant do_call_method(std::string const &method,
53 VariantMap const &parameters) override {
54 if (method == "shape") {
55 auto const shape = accumulator()->shape();
56 return std::vector<int>{shape.begin(), shape.end()};
57 }
58 if (method == "reload_from_checkpoint") {
59 accumulator()->set_internal_state(
61 return {};
62 }
63 return {};
64 }
65 virtual std::shared_ptr<const ::Accumulators::AccumulatorBase>
66 accumulator() const = 0;
67 virtual std::shared_ptr<::Accumulators::AccumulatorBase> accumulator() = 0;
68
69protected:
70 auto get_core_system_pointer(VariantMap const &params) const {
71 auto const *system = &::System::get_system();
72 if (params.contains("system")) {
73 system =
75 ->get_system());
76 }
77 return system;
78 }
79
80private:
81 std::string get_internal_state() const override {
82 return accumulator()->get_internal_state();
83 }
84
85 void set_internal_state(std::string const &state) override {
86 call_method("reload_from_checkpoint", {{"state", state}});
87 }
88};
89
90} // namespace Accumulators
91} // 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
Bind parameters in the script interface.
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.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
System & get_system()
Recursive variant implementation.
Definition Variant.hpp:84