ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
NonBondedInteractions.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
23
26
29
30#include <cassert>
31#include <memory>
32#include <stdexcept>
33#include <string>
34#include <unordered_map>
35#include <utility>
36#include <vector>
37
38namespace ScriptInterface {
39namespace Interactions {
40
42 using container_type =
43 std::unordered_map<unsigned int,
44 std::shared_ptr<NonBondedInteractionHandle>>;
45
46public:
47 using key_type = typename container_type::key_type;
48 using mapped_type = typename container_type::mapped_type;
49
50private:
51 container_type m_nonbonded_ia_params;
52 decltype(System::System::nonbonded_ias) m_nonbonded_ias;
53
54 auto make_interaction(int i, int j) {
55 assert(j <= i);
56 auto const types = std::vector<int>{{i, j}};
57 return std::dynamic_pointer_cast<NonBondedInteractionHandle>(
58 context()->make_shared_local("Interactions::NonBondedInteractionHandle",
59 {{"_types", Variant{types}}}));
60 }
61
62 void reset() {
63 auto const max_type = m_nonbonded_ias->get_max_seen_particle_type();
64 for (int i = 0; i <= max_type; i++) {
65 for (int j = 0; j <= i; j++) {
66 auto const key = m_nonbonded_ias->get_ia_param_key(i, j);
67 m_nonbonded_ias->set_ia_param(i, j,
68 std::make_shared<::IA_parameters>());
69 m_nonbonded_ia_params[key] = make_interaction(i, j);
70 }
71 }
73 }
74
75 void do_construct(VariantMap const &) override {
76 m_nonbonded_ias = System::get_system().nonbonded_ias;
77 auto const max_type = m_nonbonded_ias->get_max_seen_particle_type();
78 for (int i = 0; i <= max_type; i++) {
79 for (int j = 0; j <= i; j++) {
80 auto const key = m_nonbonded_ias->get_ia_param_key(i, j);
81 m_nonbonded_ia_params[key] = make_interaction(i, j);
82 }
83 }
84 }
85
86 std::pair<int, int> get_key(Variant const &key) const {
87 try {
88 auto const types = get_value<std::vector<int>>(key);
89 if (types.size() != 2ul or types[0] < 0 or types[1] < 0) {
90 throw Exception("need two particle types");
91 }
92 return {std::max(types[0], types[1]), std::min(types[0], types[1])};
93 } catch (...) {
94 if (context()->is_head_node()) {
95 throw std::invalid_argument(
96 "NonBondedInteractions[] expects two particle types as indices");
97 }
98 throw;
99 }
100 }
101
102protected:
103 Variant do_call_method(std::string const &method,
104 VariantMap const &params) override {
105 if (method == "get_n_types") {
106 return Variant{m_nonbonded_ias->get_max_seen_particle_type() + 1};
107 }
108 if (method == "reset") {
109 reset();
110 return {};
111 }
112 if (method == "insert") {
113 auto const [type_min, type_max] = get_key(params.at("key"));
114 m_nonbonded_ias->make_particle_type_exist(type_max);
115 auto const key = m_nonbonded_ias->get_ia_param_key(type_min, type_max);
116 auto obj_ptr = get_value<std::shared_ptr<NonBondedInteractionHandle>>(
117 params.at("object"));
118 m_nonbonded_ias->set_ia_param(type_min, type_max, obj_ptr->get_ia());
119 m_nonbonded_ia_params[key] = obj_ptr;
121 return {};
122 }
123 if (method == "check_key") {
124 static_cast<void>(get_key(params.at("key")));
125 return {};
126 }
127
128 return {};
129 }
130};
131
132} // namespace Interactions
133} // namespace ScriptInterface
The ScriptInterface counterparts of the non-bonded interactions parameters structs from the core are ...
Variant do_call_method(std::string const &method, VariantMap const &params) override
Base class for interface handles.
Context * context() const
Responsible context.
std::shared_ptr< InteractionsNonBonded > nonbonded_ias
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:82
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
System & get_system()
Various procedures concerning interactions between particles.
static SteepestDescentParameters params
Currently active steepest descent instance.