ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
BondedInteractions.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-2022 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
22#include "BondedInteraction.hpp"
23
25
30
31#include <cassert>
32#include <memory>
33#include <ranges>
34#include <stdexcept>
35#include <string>
36#include <unordered_map>
37#include <utility>
38#include <vector>
39
40namespace ScriptInterface {
41namespace Interactions {
42
46
49
50public:
51 using Base::key_type;
53
54private:
55 std::shared_ptr<::BondedInteractionsMap> m_handle;
56 std::unique_ptr<VariantMap> m_params;
57
58public:
60
61 void do_construct(VariantMap const &params) override {
62 m_handle = std::make_shared<::BondedInteractionsMap>();
63 m_params = std::make_unique<VariantMap>(params);
64 }
65
66private:
68 system.bonded_ias = m_handle;
69 m_handle->bind_system(m_system.lock());
70 m_handle->on_ia_change();
71 if (m_params and not m_params->empty()) {
72 restore_from_checkpoint(*m_params);
73 }
74 m_params.reset();
75 }
76
78 key_type key{};
79 context()->parallel_try_catch(
80 [&]() { key = m_handle->insert(obj_ptr->bonded_ia()); });
81 return key;
82 }
83
85 mapped_type const &obj_ptr) override {
86 context()->parallel_try_catch(
87 [&]() { m_handle->insert(key, obj_ptr->bonded_ia()); });
88 }
89
90 void erase_in_core(key_type const &key) final { m_handle->erase(key); }
91
92public:
93 Variant do_call_method(std::string const &name,
94 VariantMap const &params) override {
95 if (name == "get_size") {
96 return {static_cast<int>(m_handle->size())};
97 }
98
99 if (name == "get_bond_ids") {
100 auto const view = std::views::elements<0>(*m_handle);
101 std::vector<int> bond_ids(view.begin(), view.end());
102 return bond_ids;
103 }
104
105 if (name == "has_bond") {
106 auto const bond_id = get_key(params.at("bond_id"));
107 return {elements().contains(bond_id)};
108 }
109
110 if (name == "get_bond") {
111 auto const bond_id = get_key(params.at("bond_id"));
112 // core and script interface must agree
113 assert(elements().contains(bond_id) == m_handle->contains(bond_id));
114 if (not context()->is_head_node())
115 return {};
116 // bond must exist
117 if (not elements().contains(bond_id)) {
118 throw std::out_of_range("The bond with id " + std::to_string(bond_id) +
119 " is not yet defined.");
120 }
121 return {elements().at(bond_id)};
122 }
123
124 if (name == "get_zero_based_type") {
125 auto const bond_id = get_key(params.at("bond_id"));
126 return m_handle->get_zero_based_type(bond_id);
127 }
128
129 return Base::do_call_method(name, params);
130 }
131};
132
133} // namespace Interactions
134} // namespace ScriptInterface
The ScriptInterface counterparts of the bonded interactions parameters structs from the core are defi...
Data structures for bonded interactions.
Bind parameters in the script interface.
std::shared_ptr< ManagedType > mapped_type
Definition ObjectMap.hpp:58
void insert_in_core(key_type const &key, mapped_type const &obj_ptr) override
Variant do_call_method(std::string const &name, VariantMap const &params) override
void do_construct(VariantMap const &params) override
void on_bind_system(::System::System &system) override
key_type insert_in_core(mapped_type const &obj_ptr) override
Owning map of object handles.
Definition ObjectMap.hpp:53
std::shared_ptr< ManagedType > mapped_type
Definition ObjectMap.hpp:58
key_type get_key(Variant const &key) const
auto const & elements() const
Map elements.
void restore_from_checkpoint(VariantMap const &params)
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
Script interface wrapper for a component of the system class.
Main system class.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
ObjectMap< BondedInteraction, AutoParameters< ObjectMap< BondedInteraction, System::Leaf >, System::Leaf > > BondedInteractionsBase_t
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
static SteepestDescentParameters params
Currently active steepest descent instance.
Recursive variant implementation.
Definition Variant.hpp:84