ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
collision_detection/Protocol.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-2024 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 <config/config.hpp>
23
24#ifdef COLLISION_DETECTION
25
27
30
33
34#include <cassert>
35#include <memory>
36#include <optional>
37#include <stdexcept>
38#include <utility>
39
41
42class Protocol : public AutoParameters<Protocol> {
43 std::weak_ptr<BondedInteractionsMap> m_bonded_ias;
44 std::weak_ptr<Interactions::BondedInteractions> m_so_bonded_ias;
45 std::unique_ptr<VariantMap> m_params;
46
47public:
48 virtual std::shared_ptr<::CollisionDetection::ActiveProtocol> protocol() = 0;
50 std::weak_ptr<BondedInteractionsMap> bonded_ias,
51 std::weak_ptr<Interactions::BondedInteractions> so_bonded_ias) {
52 m_bonded_ias = std::move(bonded_ias);
53 m_so_bonded_ias = std::move(so_bonded_ias);
54 if (m_params) {
55 do_initialize(*m_params);
56 m_params.reset();
57 }
58 }
59
60 void do_construct(VariantMap const &params) override {
61 m_params = std::make_unique<VariantMap>(params);
62 }
63
64protected:
65 auto find_bond_id(Variant const &v) const {
66 auto bonded_ias = m_bonded_ias.lock();
67 assert(bonded_ias && "This protocol is not bound to a system");
68 std::optional<int> retval = std::nullopt;
69 if (is_type<int>(v)) {
70 auto const bond_id = get_value<int>(v);
71 if (bonded_ias->contains(bond_id)) {
72 retval = bond_id;
73 }
74 } else {
75 auto obj = get_value<std::shared_ptr<Interactions::BondedInteraction>>(v);
76 retval = bonded_ias->find_bond_id(obj->bonded_ia());
77 }
78 if (not retval) {
79 throw std::invalid_argument("Bond in parameter list was "
80 "not added to the system");
81 }
82 return *retval;
83 }
84 auto get_bond_variant_by_id(int bond_id) {
85 auto so_bonded_ias = m_so_bonded_ias.lock();
86 assert(so_bonded_ias != nullptr);
87 return so_bonded_ias->do_call_method("get", {{"key", bond_id}});
88 }
89 virtual void do_initialize(VariantMap const &params) = 0;
90};
91
92} // namespace ScriptInterface::CollisionDetection
93
94#endif // COLLISION_DETECTION
Data structures for bonded interactions.
Bind parameters in the script interface.
virtual std::shared_ptr<::CollisionDetection::ActiveProtocol > protocol()=0
void do_construct(VariantMap const &params) override
void bind_bonded_ias(std::weak_ptr< BondedInteractionsMap > bonded_ias, std::weak_ptr< Interactions::BondedInteractions > so_bonded_ias)
virtual void do_initialize(VariantMap const &params)=0
This file contains the defaults for ESPResSo.
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.