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 <stdexcept>
34#include <string>
35#include <unordered_map>
36#include <utility>
37#include <vector>
38
39namespace ScriptInterface {
40namespace Interactions {
41
45
48 using container_type =
49 std::unordered_map<int, std::shared_ptr<BondedInteraction>>;
50
51public:
52 using key_type = typename container_type::key_type;
53 using mapped_type = typename container_type::mapped_type;
54
55private:
56 container_type m_bonds;
57 std::shared_ptr<::BondedInteractionsMap> m_handle;
58 std::unique_ptr<VariantMap> m_params;
59
60 void do_construct(VariantMap const &params) override {
61 m_handle = std::make_shared<::BondedInteractionsMap>();
62 m_params = std::make_unique<VariantMap>(params);
63 }
64
65 void on_bind_system(::System::System &system) override {
66 system.bonded_ias = m_handle;
67 m_handle->bind_system(m_system.lock());
68 m_handle->on_ia_change();
69 if (m_params and not m_params->empty()) {
70 restore_from_checkpoint(*m_params);
71 }
72 m_params.reset();
73 }
74
75 key_type insert_in_core(mapped_type const &obj_ptr) override {
76 key_type key{};
77 context()->parallel_try_catch(
78 [&]() { key = m_handle->insert(obj_ptr->bonded_ia()); });
79 m_bonds[key] = std::move(obj_ptr);
80 return key;
81 }
82
83 void insert_in_core(key_type const &key,
84 mapped_type const &obj_ptr) override {
85 context()->parallel_try_catch(
86 [&]() { m_handle->insert(key, obj_ptr->bonded_ia()); });
87 m_bonds[key] = std::move(obj_ptr);
88 }
89
90 void erase_in_core(key_type const &key) override {
91 m_handle->erase(key);
92 m_bonds.erase(key);
93 }
94
95public:
96 Variant do_call_method(std::string const &name,
97 VariantMap const &params) override {
98 if (name == "get_size") {
99 return {static_cast<int>(m_handle->size())};
100 }
101
102 if (name == "get_bond_ids") {
103 std::vector<int> bond_ids;
104 for (auto const &kv : *m_handle)
105 bond_ids.push_back(kv.first);
106 return bond_ids;
107 }
108
109 if (name == "has_bond") {
110 auto const bond_id = get_key(params.at("bond_id"));
111 return {m_bonds.contains(bond_id)};
112 }
113
114 if (name == "get_bond") {
115 auto const bond_id = get_key(params.at("bond_id"));
116 // core and script interface must agree
117 assert(m_bonds.count(bond_id) == m_handle->count(bond_id));
118 if (not context()->is_head_node())
119 return {};
120 // bond must exist
121 if (not m_bonds.contains(bond_id)) {
122 throw std::out_of_range("The bond with id " + std::to_string(bond_id) +
123 " is not yet defined.");
124 }
125 return {m_bonds.at(bond_id)};
126 }
127
128 if (name == "get_zero_based_type") {
129 auto const bond_id = get_key(params.at("bond_id"));
130 return m_handle->get_zero_based_type(bond_id);
131 }
132
133 return Base::do_call_method(name, params);
134 }
135};
136
137} // namespace Interactions
138} // 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.
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
typename container_type::mapped_type mapped_type
Owning map of ObjectHandles.
Definition ObjectMap.hpp:43
KeyType get_key(Variant const &key) const
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.
std::shared_ptr< BondedInteractionsMap > bonded_ias
ObjectMap< BondedInteraction, AutoParameters< ObjectMap< BondedInteraction, System::Leaf >, System::Leaf > > BondedInteractionsBase_t
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:69
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.
Definition Variant.hpp:67
static SteepestDescentParameters params
Currently active steepest descent instance.