Loading [MathJax]/extensions/TeX/AMSmath.js
ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
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
49public:
50 using key_type = typename Base::key_type;
52
53private:
54 std::shared_ptr<::BondedInteractionsMap> m_handle;
55 std::unique_ptr<VariantMap> m_params;
56
57public:
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
65private:
67 system.bonded_ias = m_handle;
68 m_handle->bind_system(m_system.lock());
69 m_handle->on_ia_change();
70 if (m_params and not m_params->empty()) {
71 restore_from_checkpoint(*m_params);
72 }
73 m_params.reset();
74 }
75
77 key_type key{};
78 context()->parallel_try_catch(
79 [&]() { key = m_handle->insert(obj_ptr->bonded_ia()); });
80 return key;
81 }
82
84 mapped_type const &obj_ptr) override {
85 context()->parallel_try_catch(
86 [&]() { m_handle->insert(key, obj_ptr->bonded_ia()); });
87 }
88
89 void erase_in_core(key_type const &key) final { m_handle->erase(key); }
90
91public:
92 Variant do_call_method(std::string const &name,
93 VariantMap const &params) override {
94 if (name == "get_size") {
95 return {static_cast<int>(m_handle->size())};
96 }
97
98 if (name == "get_bond_ids") {
99 std::vector<int> bond_ids;
100 for (auto const &kv : *m_handle)
101 bond_ids.push_back(kv.first);
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().count(bond_id) == m_handle->count(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.
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:52
std::shared_ptr< ManagedType > mapped_type
Definition ObjectMap.hpp:57
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.
ObjectMap< BondedInteraction, AutoParameters< ObjectMap< BondedInteraction, System::Leaf >, System::Leaf > > BondedInteractionsBase_t
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: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.