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
28
29#include <cassert>
30#include <memory>
31#include <stdexcept>
32#include <string>
33#include <unordered_map>
34#include <utility>
35#include <vector>
36
37namespace ScriptInterface {
38namespace Interactions {
39class BondedInteractions : public ObjectMap<BondedInteraction> {
40 using container_type =
41 std::unordered_map<int, std::shared_ptr<BondedInteraction>>;
42
43public:
44 using key_type = typename container_type::key_type;
45 using mapped_type = typename container_type::mapped_type;
46
48 add_parameters({
49 {"_objects", AutoParameter::read_only,
50 []() {
51 // deactivate serialization (done at the Python level)
52 return make_unordered_map_of_variants(container_type{});
53 }},
54 });
55 }
56
57private:
58 container_type m_bonds;
59
60 void before_do_construct() override {}
61
62 key_type insert_in_core(mapped_type const &obj_ptr) override {
63 auto const key = ::bonded_ia_params.insert(obj_ptr->bonded_ia());
64 m_bonds[key] = std::move(obj_ptr);
65 return key;
66 }
67
68 void insert_in_core(key_type const &key,
69 mapped_type const &obj_ptr) override {
70 ::bonded_ia_params.insert(key, obj_ptr->bonded_ia());
71 m_bonds[key] = std::move(obj_ptr);
72 }
73
74 void erase_in_core(key_type const &key) override {
76 m_bonds.erase(key);
77 }
78
79protected:
80 Variant do_call_method(std::string const &name,
81 VariantMap const &params) override {
82 if (name == "get_size") {
83 return {static_cast<int>(::bonded_ia_params.size())};
84 }
85
86 if (name == "get_bond_ids") {
87 std::vector<int> bond_ids;
88 for (auto const &kv : ::bonded_ia_params)
89 bond_ids.push_back(kv.first);
90 return bond_ids;
91 }
92
93 if (name == "has_bond") {
94 auto const bond_id = get_key(params.at("bond_id"));
95 return {m_bonds.count(bond_id) != 0};
96 }
97
98 if (name == "get_bond") {
99 auto const bond_id = get_key(params.at("bond_id"));
100 // core and script interface must agree
101 assert(m_bonds.count(bond_id) == ::bonded_ia_params.count(bond_id));
102 if (not context()->is_head_node())
103 return {};
104 // bond must exist
105 if (m_bonds.count(bond_id) == 0) {
106 throw std::out_of_range("The bond with id " + std::to_string(bond_id) +
107 " is not yet defined.");
108 }
109 return {m_bonds.at(bond_id)};
110 }
111
112 if (name == "get_zero_based_type") {
113 auto const bond_id = get_key(params.at("bond_id"));
114 return ::bonded_ia_params.get_zero_based_type(bond_id);
115 }
116
118 }
119};
120} // namespace Interactions
121} // namespace ScriptInterface
The ScriptInterface counterparts of the bonded interactions parameters structs from the core are defi...
BondedInteractionsMap bonded_ia_params
Field containing the parameters of the bonded ia types.
Data structures for bonded interactions.
auto count(key_type const &key) const
void insert(key_type const &key, mapped_type const &ptr)
auto erase(key_type const &key)
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
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
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:82
auto make_unordered_map_of_variants(std::unordered_map< K, V > const &v)
Definition Variant.hpp:93
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:80
static SteepestDescentParameters params
Currently active steepest descent instance.
static constexpr const ReadOnly read_only