ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
CollisionDetection.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#ifndef SCRIPT_INTERFACE_COLLISION_DETECTION_COLLISION_DETECTION_HPP
23#define SCRIPT_INTERFACE_COLLISION_DETECTION_COLLISION_DETECTION_HPP
24
25#include "config/config.hpp"
26
27#ifdef COLLISION_DETECTION
28
30
31#include "core/collision.hpp"
32
33#include <cassert>
34#include <set>
35#include <stdexcept>
36#include <string>
37#include <tuple>
38#include <unordered_map>
39#include <vector>
40
41namespace ScriptInterface {
43
44class CollisionDetection : public AutoParameters<CollisionDetection> {
45 std::unordered_map<CollisionModeType, std::string> const cd_mode_to_name = {
47 {CollisionModeType::BIND_CENTERS, "bind_centers"},
48 {CollisionModeType::BIND_VS, "bind_at_point_of_collision"},
49 {CollisionModeType::GLUE_TO_SURF, "glue_to_surface"},
50 {CollisionModeType::BIND_THREE_PARTICLES, "bind_three_particles"},
51 };
52 std::unordered_map<std::string, CollisionModeType> cd_name_to_mode;
53 std::unordered_map<CollisionModeType,
54 std::vector<std::string>> const cd_mode_to_parameters = {
55 {CollisionModeType::OFF, {"mode"}},
56 {CollisionModeType::BIND_CENTERS, {"mode", "bond_centers", "distance"}},
58 {"mode", "bond_centers", "bond_vs", "part_type_vs", "distance",
59 "vs_placement"}},
61 {"mode", "bond_centers", "bond_vs", "part_type_vs",
62 "part_type_to_be_glued", "part_type_to_attach_vs_to",
63 "part_type_after_glueing", "distance",
64 "distance_glued_particle_to_vs"}},
66 {"mode", "bond_centers", "distance", "bond_three_particles",
67 "three_particle_binding_angle_resolution"}},
68 };
69
70public:
72 for (auto const &kv : cd_mode_to_name) {
73 cd_name_to_mode[kv.second] = kv.first;
74 }
76 {{"mode",
77 [this](Variant const &v) {
78 auto const name = get_value<std::string>(v);
79 check_mode_name(name);
80 collision_params.mode = cd_name_to_mode.at(name);
81 },
82 [this]() { return cd_mode_to_name.at(collision_params.mode); }},
83
84 {"bond_centers", collision_params.bond_centers},
85 {"bond_vs", collision_params.bond_vs},
86 {"bond_three_particles", collision_params.bond_three_particles},
87 {"three_particle_binding_angle_resolution",
89
90 {"distance", collision_params.distance},
91 {"distance_glued_particle_to_vs",
93 {"vs_placement", collision_params.vs_placement},
94
95 {"part_type_vs", collision_params.vs_particle_type},
96 {"part_type_to_be_glued", collision_params.part_type_to_be_glued},
97 {"part_type_to_attach_vs_to",
99 {"part_type_after_glueing",
101 }
102
103 Variant do_call_method(const std::string &name,
104 const VariantMap &params) override {
105 if (name == "instantiate") {
106 context()->parallel_try_catch([this, &params]() {
107 auto collision_params_backup = ::collision_params;
108 try {
109 // check provided parameters
110 check_input_parameters(params);
111 // set parameters
113 for (auto const &kv : params) {
114 do_set_parameter(get_value<std::string>(kv.first), kv.second);
115 }
116 // sanitize parameters and calculate derived parameters
118 return none;
119 } catch (...) {
120 // restore original parameters and re-throw exception
121 ::collision_params = collision_params_backup;
122 throw;
123 }
124 });
125 }
126 if (name == "params_for_mode") {
127 auto const name = get_value<std::string>(params, "mode");
128 check_mode_name(name);
129 auto const mode = cd_name_to_mode.at(name);
130 return make_vector_of_variants(cd_mode_to_parameters.at(mode));
131 }
132 return none;
133 }
134
135private:
136 void check_mode_name(std::string const &name) const {
137 if (cd_name_to_mode.count(name) == 0) {
138 throw std::invalid_argument("Unknown collision mode '" + name + "'");
139 }
140 }
141
142 void check_input_parameters(VariantMap const &params) const {
143 auto const name = get_value<std::string>(params, "mode");
144 check_mode_name(name);
145 auto const mode = cd_name_to_mode.at(name);
146 auto const expected_params = cd_mode_to_parameters.at(mode);
147 auto const expected_param_names =
148 std::set<std::string>{expected_params.begin(), expected_params.end()};
149 std::set<std::string> input_parameter_names = {};
150 for (auto const &kv : params) {
151 auto const param_name = get_value<std::string>(kv.first);
152 if (expected_param_names.count(param_name) == 0) {
153 // throw if parameter is unknown
154 std::ignore = get_parameter(param_name);
155 // throw if parameter is known but doesn't match the selected mode
156 throw std::runtime_error("Parameter '" + param_name + "' is not " +
157 "required for mode '" + name + "'");
158 }
159 input_parameter_names.insert(param_name);
160 }
161 for (auto const &param_name : expected_param_names) {
162 if (input_parameter_names.count(param_name) == 0) {
163 throw std::runtime_error("Parameter '" + param_name + "' is " +
164 "required for mode '" + name + "'");
165 }
166 }
167 }
168};
169
170} /* namespace CollisionDetection */
171} /* namespace ScriptInterface */
172
173#endif // COLLISION_DETECTION
174#endif
int part_type_after_glueing
Particle type to which the newly glued particle is converted.
Definition collision.hpp:77
void initialize()
Validates parameters and creates particle types if needed.
int part_type_to_be_glued
For mode "glue to surface": The particle type being glued.
Definition collision.hpp:72
double vs_placement
Placement of virtual sites for MODE_VS.
Definition collision.hpp:90
int bond_centers
bond type used between centers of colliding particles
Definition collision.hpp:62
CollisionModeType mode
collision protocol
Definition collision.hpp:55
int three_particle_angle_resolution
Number of angle bonds to use (angular resolution) different angle bonds with different equilibrium an...
Definition collision.hpp:84
int part_type_to_attach_vs_to
For mode "glue to surface": The particle type to which the virtual site is attached.
Definition collision.hpp:75
double dist_glued_part_to_vs
For mode "glue to surface": The distance from the particle which is to be glued to the new virtual si...
Definition collision.hpp:70
int bond_three_particles
First bond type (for zero degrees) used for the three-particle bond (angle potential)
Definition collision.hpp:80
double distance
distance at which particles are bound
Definition collision.hpp:57
int bond_vs
bond type used between virtual sites
Definition collision.hpp:64
int vs_particle_type
particle type for virtual sites created on collision
Definition collision.hpp:66
Bind parameters in the script interface.
void do_set_parameter(const std::string &name, const Variant &value) final
Variant get_parameter(const std::string &name) const final
void add_parameters(std::vector< AutoParameter > &&params)
Variant do_call_method(const std::string &name, const VariantMap &params) override
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
boost::string_ref name() const
Context * context() const
Responsible context.
Collision_parameters collision_params
Parameters for collision detection.
Definition collision.cpp:73
CollisionModeType
Protocols for collision handling.
Definition collision.hpp:30
@ BIND_CENTERS
Create bond between centers of colliding particles.
@ GLUE_TO_SURF
Glue a particle to a specific spot on another particle.
@ OFF
Deactivate collision detection.
@ BIND_THREE_PARTICLES
Three particle binding mode.
@ BIND_VS
Create a bond between the centers of the colliding particles, plus two virtual sites at the point of ...
This file contains the defaults for ESPResSo.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:82
auto make_vector_of_variants(std::vector< T > const &v)
Definition Variant.hpp:101
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
constexpr const None none
None-"literal".
Definition Variant.hpp:63
static SteepestDescentParameters params
Currently active steepest descent instance.