ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
BondedInteraction.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/** @file
23 * The ScriptInterface counterparts of the bonded interactions parameters
24 * structs from the core are defined here.
25 *
26 */
27
30#include "core/thermostat.hpp"
31
35
36#include <boost/algorithm/string/predicate.hpp>
37#include <boost/variant.hpp>
38
39#include <algorithm>
40#include <cassert>
41#include <cmath>
42#include <cstddef>
43#include <iterator>
44#include <limits>
45#include <memory>
46#include <set>
47#include <stdexcept>
48#include <string>
49#include <vector>
50
51namespace ScriptInterface {
52namespace Interactions {
53
54class BondedInteraction : public AutoParameters<BondedInteraction> {
55protected:
56 std::shared_ptr<::Bonded_IA_Parameters> m_bonded_ia;
57
58public:
59 std::shared_ptr<::Bonded_IA_Parameters> bonded_ia() { return m_bonded_ia; }
60 std::shared_ptr<const ::Bonded_IA_Parameters> bonded_ia() const {
61 return m_bonded_ia;
62 }
63
64protected:
67
68 virtual std::set<std::string> get_valid_parameters() const {
69 auto const vec = valid_parameters();
70 auto valid_keys = std::set<std::string>();
71 std::transform(vec.begin(), vec.end(),
72 std::inserter(valid_keys, valid_keys.begin()),
73 [](auto const &key) { return std::string{key}; });
74 return valid_keys;
75 }
76
77private:
78 void check_valid_parameters(VariantMap const &params) const {
79 auto const valid_keys = get_valid_parameters();
80 for (auto const &key : valid_keys) {
81 if (params.count(std::string(key)) == 0) {
82 throw std::runtime_error("Parameter '" + key + "' is missing");
83 }
84 }
85 for (auto const &kv : params) {
86 if (valid_keys.count(kv.first) == 0) {
87 throw std::runtime_error("Parameter '" + kv.first +
88 "' is not recognized");
89 }
90 }
91 }
92
93 void do_construct(VariantMap const &params) override {
94 // Check if initialization "by id" or "by parameters"
95 if (params.find("bond_id") != params.end()) {
96 auto const bond_id = get_value<int>(params, "bond_id");
97 context()->parallel_try_catch([&]() {
98 if (not ::bonded_ia_params.contains(bond_id)) {
99 throw std::runtime_error("No bond with id " +
100 std::to_string(bond_id) +
101 " exists in the ESPResSo core");
102 }
103 });
104 m_bonded_ia = ::bonded_ia_params.at(bond_id);
105 } else {
106 context()->parallel_try_catch([&]() {
107 check_valid_parameters(params);
108 construct_bond(params);
109 });
110 }
111 }
112
113 virtual void construct_bond(VariantMap const &params) = 0;
114
115public:
116 bool operator==(BondedInteraction const &other) const {
117 return m_bonded_ia == other.m_bonded_ia;
118 }
119
120 Variant do_call_method(std::string const &name,
121 VariantMap const &params) override {
122 // this feature is needed to compare bonds
123 if (name == "is_same_bond") {
124 auto const bond_so =
125 get_value<std::shared_ptr<BondedInteraction>>(params, "bond");
126 return *this == *bond_so;
127 }
128 if (name == "get_num_partners") {
129 return number_of_partners(*bonded_ia());
130 }
131 if (name == "get_zero_based_type") {
132 auto const bond_id = get_value<int>(params, "bond_id");
133 return ::bonded_ia_params.get_zero_based_type(bond_id);
134 }
135
136 return {};
137 }
138};
139
140template <class CoreIA> class BondedInteractionImpl : public BondedInteraction {
141public:
142 using CoreBondedInteraction = CoreIA;
144 return boost::get<CoreBondedInteraction>(*bonded_ia());
145 }
146};
147
148class FeneBond : public BondedInteractionImpl<::FeneBond> {
149public:
151 add_parameters({
152 {"k", AutoParameter::read_only, [this]() { return get_struct().k; }},
153 {"d_r_max", AutoParameter::read_only,
154 [this]() { return get_struct().drmax; }},
155 {"r_0", AutoParameter::read_only, [this]() { return get_struct().r0; }},
156 });
157 }
158
159private:
160 void construct_bond(VariantMap const &params) override {
161 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
162 CoreBondedInteraction(get_value<double>(params, "k"),
163 get_value<double>(params, "d_r_max"),
164 get_value<double>(params, "r_0")));
165 }
166};
167
168class HarmonicBond : public BondedInteractionImpl<::HarmonicBond> {
169public:
171 add_parameters({
172 {"k", AutoParameter::read_only, [this]() { return get_struct().k; }},
173 {"r_0", AutoParameter::read_only, [this]() { return get_struct().r; }},
174 {"r_cut", AutoParameter::read_only,
175 [this]() { return get_struct().r_cut; }},
176 });
177 }
178
179private:
180 void construct_bond(VariantMap const &params) override {
181 m_bonded_ia =
182 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
183 get_value<double>(params, "k"), get_value<double>(params, "r_0"),
184 get_value<double>(params, "r_cut")));
185 }
186};
187
188class QuarticBond : public BondedInteractionImpl<::QuarticBond> {
189public:
191 add_parameters({
192 {"k0", AutoParameter::read_only, [this]() { return get_struct().k0; }},
193 {"k1", AutoParameter::read_only, [this]() { return get_struct().k1; }},
194 {"r", AutoParameter::read_only, [this]() { return get_struct().r; }},
195 {"r_cut", AutoParameter::read_only,
196 [this]() { return get_struct().r_cut; }},
197 });
198 }
199
200private:
201 void construct_bond(VariantMap const &params) override {
202 m_bonded_ia =
203 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
204 get_value<double>(params, "k0"), get_value<double>(params, "k1"),
205 get_value<double>(params, "r"),
206 get_value<double>(params, "r_cut")));
207 }
208};
209
210class BondedCoulomb : public BondedInteractionImpl<::BondedCoulomb> {
211public:
213 add_parameters({
214 {"prefactor", AutoParameter::read_only,
215 [this]() { return get_struct().prefactor; }},
216 });
217 }
218
219private:
220 void construct_bond(VariantMap const &params) override {
221 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
222 CoreBondedInteraction(get_value<double>(params, "prefactor")));
223 }
224};
225
226class BondedCoulombSR : public BondedInteractionImpl<::BondedCoulombSR> {
227public:
229 add_parameters({
230 {"q1q2", AutoParameter::read_only,
231 [this]() { return get_struct().q1q2; }},
232 });
233 }
234
235private:
236 void construct_bond(VariantMap const &params) override {
237 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
238 CoreBondedInteraction(get_value<double>(params, "q1q2")));
239 }
240};
241
242class AngleHarmonicBond : public BondedInteractionImpl<::AngleHarmonicBond> {
243public:
245 add_parameters({
246 {"bend", AutoParameter::read_only,
247 [this]() { return get_struct().bend; }},
248 {"phi0", AutoParameter::read_only,
249 [this]() { return get_struct().phi0; }},
250 });
251 }
252
253private:
254 void construct_bond(VariantMap const &params) override {
255 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
256 CoreBondedInteraction(get_value<double>(params, "bend"),
257 get_value<double>(params, "phi0")));
258 }
259};
260
261class AngleCosineBond : public BondedInteractionImpl<::AngleCosineBond> {
262public:
264 add_parameters({
265 {"bend", AutoParameter::read_only,
266 [this]() { return get_struct().bend; }},
267 {"phi0", AutoParameter::read_only,
268 [this]() { return get_struct().phi0; }},
269 });
270 }
271
272private:
273 void construct_bond(VariantMap const &params) override {
274 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
275 CoreBondedInteraction(get_value<double>(params, "bend"),
276 get_value<double>(params, "phi0")));
277 }
278};
279
280class AngleCossquareBond : public BondedInteractionImpl<::AngleCossquareBond> {
281public:
283 add_parameters({
284 {"bend", AutoParameter::read_only,
285 [this]() { return get_struct().bend; }},
286 {"phi0", AutoParameter::read_only,
287 [this]() { return get_struct().phi0; }},
288 });
289 }
290
291private:
292 void construct_bond(VariantMap const &params) override {
293 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
294 CoreBondedInteraction(get_value<double>(params, "bend"),
295 get_value<double>(params, "phi0")));
296 }
297};
298
299class DihedralBond : public BondedInteractionImpl<::DihedralBond> {
300public:
302 add_parameters({
303 {"mult", AutoParameter::read_only,
304 [this]() { return get_struct().mult; }},
305 {"bend", AutoParameter::read_only,
306 [this]() { return get_struct().bend; }},
307 {"phase", AutoParameter::read_only,
308 [this]() { return get_struct().phase; }},
309 });
310 }
311
312private:
313 void construct_bond(VariantMap const &params) override {
314 m_bonded_ia =
315 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
316 get_value<int>(params, "mult"), get_value<double>(params, "bend"),
317 get_value<double>(params, "phase")));
318 }
319};
320
322 : public BondedInteractionImpl<::TabulatedDistanceBond> {
323public:
325 add_parameters({
326 {"min", AutoParameter::read_only,
327 [this]() { return get_struct().pot->minval; }},
328 {"max", AutoParameter::read_only,
329 [this]() { return get_struct().pot->maxval; }},
330 {"energy", AutoParameter::read_only,
331 [this]() { return get_struct().pot->energy_tab; }},
332 {"force", AutoParameter::read_only,
333 [this]() { return get_struct().pot->force_tab; }},
334 });
335 }
336
337private:
338 void construct_bond(VariantMap const &params) override {
339 m_bonded_ia =
340 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
341 get_value<double>(params, "min"), get_value<double>(params, "max"),
342 get_value<std::vector<double>>(params, "energy"),
343 get_value<std::vector<double>>(params, "force")));
344 }
345};
346
347class TabulatedAngleBond : public BondedInteractionImpl<::TabulatedAngleBond> {
348public:
350 add_parameters({
351 {"min", AutoParameter::read_only,
352 [this]() { return get_struct().pot->minval; }},
353 {"max", AutoParameter::read_only,
354 [this]() { return get_struct().pot->maxval; }},
355 {"energy", AutoParameter::read_only,
356 [this]() { return get_struct().pot->energy_tab; }},
357 {"force", AutoParameter::read_only,
358 [this]() { return get_struct().pot->force_tab; }},
359 });
360 }
361
362private:
363 void construct_bond(VariantMap const &params) override {
364 m_bonded_ia =
365 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
366 get_value<double>(params, "min"), get_value<double>(params, "max"),
367 get_value<std::vector<double>>(params, "energy"),
368 get_value<std::vector<double>>(params, "force")));
369 }
370};
371
373 : public BondedInteractionImpl<::TabulatedDihedralBond> {
374public:
376 add_parameters({
377 {"min", AutoParameter::read_only,
378 [this]() { return get_struct().pot->minval; }},
379 {"max", AutoParameter::read_only,
380 [this]() { return get_struct().pot->maxval; }},
381 {"energy", AutoParameter::read_only,
382 [this]() { return get_struct().pot->energy_tab; }},
383 {"force", AutoParameter::read_only,
384 [this]() { return get_struct().pot->force_tab; }},
385 });
386 }
387
388private:
389 void construct_bond(VariantMap const &params) override {
390 m_bonded_ia =
391 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
392 get_value<double>(params, "min"), get_value<double>(params, "max"),
393 get_value<std::vector<double>>(params, "energy"),
394 get_value<std::vector<double>>(params, "force")));
395 }
396};
397
398class ThermalizedBond : public BondedInteractionImpl<::ThermalizedBond> {
399public:
401 add_parameters({
402 {"temp_com", AutoParameter::read_only,
403 [this]() { return get_struct().temp_com; }},
404 {"gamma_com", AutoParameter::read_only,
405 [this]() { return get_struct().gamma_com; }},
406 {"temp_distance", AutoParameter::read_only,
407 [this]() { return get_struct().temp_distance; }},
408 {"gamma_distance", AutoParameter::read_only,
409 [this]() { return get_struct().gamma_distance; }},
410 {"r_cut", AutoParameter::read_only,
411 [this]() { return get_struct().r_cut; }},
412 });
413 }
414
415private:
416 void construct_bond(VariantMap const &params) override {
417 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
418 CoreBondedInteraction(get_value<double>(params, "temp_com"),
419 get_value<double>(params, "gamma_com"),
420 get_value<double>(params, "temp_distance"),
421 get_value<double>(params, "gamma_distance"),
422 get_value<double>(params, "r_cut")));
423 }
424};
425
426class RigidBond : public BondedInteractionImpl<::RigidBond> {
427public:
429 add_parameters({
430 {"r", AutoParameter::read_only,
431 [this]() { return std::sqrt(get_struct().d2); }},
432 {"ptol", AutoParameter::read_only,
433 [this]() { return 0.5 * get_struct().p_tol; }},
434 {"vtol", AutoParameter::read_only,
435 [this]() { return get_struct().v_tol; }},
436 });
437 }
438
439private:
440 void construct_bond(VariantMap const &params) override {
441 m_bonded_ia =
442 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
443 get_value<double>(params, "r"), get_value<double>(params, "ptol"),
444 get_value<double>(params, "vtol")));
445 }
446};
447
448class IBMTriel : public BondedInteractionImpl<::IBMTriel> {
449public:
451 add_parameters({
452 {"k1", AutoParameter::read_only, [this]() { return get_struct().k1; }},
453 {"k2", AutoParameter::read_only, [this]() { return get_struct().k2; }},
454 {"maxDist", AutoParameter::read_only,
455 [this]() { return get_struct().maxDist; }},
456 {"elasticLaw", AutoParameter::read_only,
457 [this]() {
458 if (get_struct().elasticLaw == tElasticLaw::NeoHookean) {
459 return std::string("NeoHookean");
460 }
461 return std::string("Skalak");
462 }},
463 });
464 }
465
466private:
467 void construct_bond(VariantMap const &params) override {
468 auto const law_name = get_value<std::string>(params, "elasticLaw");
469 tElasticLaw elastic_law;
470 if (law_name == "NeoHookean") {
471 elastic_law = tElasticLaw::NeoHookean;
472 } else if (law_name == "Skalak") {
473 elastic_law = tElasticLaw::Skalak;
474 } else {
475 throw std::invalid_argument(
476 "Invalid value for parameter 'elasticLaw': '" + law_name + "'");
477 }
478 m_bonded_ia =
479 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
480 get_value<int>(params, "ind1"), get_value<int>(params, "ind2"),
481 get_value<int>(params, "ind3"),
482 get_value<double>(params, "maxDist"), elastic_law,
483 get_value<double>(params, "k1"), get_value<double>(params, "k2")));
484 }
485
486 std::set<std::string> get_valid_parameters() const override {
487 auto names =
489 names.insert("ind1");
490 names.insert("ind2");
491 names.insert("ind3");
492 return names;
493 }
494};
495
496class IBMVolCons : public BondedInteractionImpl<::IBMVolCons> {
497public:
499 add_parameters({
500 {"softID", AutoParameter::read_only,
501 [this]() { return get_struct().softID; }},
502 {"kappaV", AutoParameter::read_only,
503 [this]() { return get_struct().kappaV; }},
504 });
505 }
506
507 Variant do_call_method(std::string const &name,
508 VariantMap const &params) override {
509 if (name == "current_volume") {
510 return ::immersed_boundaries.get_current_volume(get_struct().softID);
511 }
512 return BondedInteraction::do_call_method(name, params);
513 }
514
515private:
516 void construct_bond(VariantMap const &params) override {
517 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
518 CoreBondedInteraction(get_value<int>(params, "softID"),
519 get_value<double>(params, "kappaV")));
520 }
521};
522
523class IBMTribend : public BondedInteractionImpl<::IBMTribend> {
524public:
526 add_parameters({
527 {"kb", AutoParameter::read_only, [this]() { return get_struct().kb; }},
528 {"refShape", AutoParameter::read_only,
529 [this]() {
530 return (m_flat) ? std::string("Flat") : std::string("Initial");
531 }},
532 {"theta0", AutoParameter::read_only,
533 [this]() { return get_struct().theta0; }},
534 });
535 }
536
537private:
538 bool m_flat;
539 void construct_bond(VariantMap const &params) override {
540 auto const shape_name = get_value<std::string>(params, "refShape");
541 if (shape_name == "Flat") {
542 m_flat = true;
543 } else if (shape_name == "Initial") {
544 m_flat = false;
545 } else {
546 throw std::invalid_argument("Invalid value for parameter 'refShape': '" +
547 shape_name + "'");
548 }
549 m_bonded_ia =
550 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
551 get_value<int>(params, "ind1"), get_value<int>(params, "ind2"),
552 get_value<int>(params, "ind3"), get_value<int>(params, "ind4"),
553 get_value<double>(params, "kb"), m_flat));
554 }
555
556 std::set<std::string> get_valid_parameters() const override {
557 auto names =
559 names.erase("theta0");
560 names.insert("ind1");
561 names.insert("ind2");
562 names.insert("ind3");
563 names.insert("ind4");
564 return names;
565 }
566};
567
569 : public BondedInteractionImpl<::OifGlobalForcesBond> {
570public:
572 add_parameters({
573 {"A0_g", AutoParameter::read_only,
574 [this]() { return get_struct().A0_g; }},
575 {"ka_g", AutoParameter::read_only,
576 [this]() { return get_struct().ka_g; }},
577 {"V0", AutoParameter::read_only, [this]() { return get_struct().V0; }},
578 {"kv", AutoParameter::read_only, [this]() { return get_struct().kv; }},
579 });
580 }
581
582private:
583 void construct_bond(VariantMap const &params) override {
584 m_bonded_ia =
585 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
586 get_value<double>(params, "A0_g"),
587 get_value<double>(params, "ka_g"), get_value<double>(params, "V0"),
588 get_value<double>(params, "kv")));
589 }
590};
591
592class OifLocalForcesBond : public BondedInteractionImpl<::OifLocalForcesBond> {
593public:
595 add_parameters({
596 {"r0", AutoParameter::read_only, [this]() { return get_struct().r0; }},
597 {"ks", AutoParameter::read_only, [this]() { return get_struct().ks; }},
598 {"kslin", AutoParameter::read_only,
599 [this]() { return get_struct().kslin; }},
600 {"phi0", AutoParameter::read_only,
601 [this]() { return get_struct().phi0; }},
602 {"kb", AutoParameter::read_only, [this]() { return get_struct().kb; }},
603 {"A01", AutoParameter::read_only,
604 [this]() { return get_struct().A01; }},
605 {"A02", AutoParameter::read_only,
606 [this]() { return get_struct().A02; }},
607 {"kal", AutoParameter::read_only,
608 [this]() { return get_struct().kal; }},
609 {"kvisc", AutoParameter::read_only,
610 [this]() { return get_struct().kvisc; }},
611 });
612 }
613
614private:
615 void construct_bond(VariantMap const &params) override {
616 m_bonded_ia =
617 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
618 get_value<double>(params, "r0"), get_value<double>(params, "ks"),
619 get_value<double>(params, "kslin"),
620 get_value<double>(params, "phi0"), get_value<double>(params, "kb"),
621 get_value<double>(params, "A01"), get_value<double>(params, "A02"),
622 get_value<double>(params, "kal"),
623 get_value<double>(params, "kvisc")));
624 }
625};
626
627class VirtualBond : public BondedInteractionImpl<::VirtualBond> {
628public:
630 m_bonded_ia =
631 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction());
632 }
633
634private:
635 void construct_bond(VariantMap const &) override {}
636};
637
638} // namespace Interactions
639} // namespace ScriptInterface
BondedInteractionsMap bonded_ia_params
Field containing the parameters of the bonded ia types.
Data structures for bonded interactions.
int number_of_partners(Bonded_IA_Parameters const &iaparams)
Return the number of bonded partners for the specified bond.
mapped_type at(key_type const &key) const
Bind parameters in the script interface.
Utils::Span< const boost::string_ref > valid_parameters() const final
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
virtual std::set< std::string > get_valid_parameters() const
std::shared_ptr<::Bonded_IA_Parameters > m_bonded_ia
bool operator==(BondedInteraction const &other) const
std::shared_ptr< const ::Bonded_IA_Parameters > bonded_ia() const
void do_construct(VariantMap const &params) override
virtual void construct_bond(VariantMap const &params)=0
std::shared_ptr<::Bonded_IA_Parameters > bonded_ia()
Variant do_call_method(std::string const &name, VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
std::set< std::string > get_valid_parameters() const override
std::set< std::string > get_valid_parameters() const override
void construct_bond(VariantMap const &params) override
Variant do_call_method(std::string const &name, VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &params) override
void construct_bond(VariantMap const &) override
Context * context() const
Responsible context.
Implementation in thermostat.cpp.
tElasticLaw
Definition ibm_triel.hpp:31
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
static SteepestDescentParameters params
Currently active steepest descent instance.