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
38#include <algorithm>
39#include <cassert>
40#include <cmath>
41#include <cstddef>
42#include <iterator>
43#include <limits>
44#include <memory>
45#include <ranges>
46#include <set>
47#include <stdexcept>
48#include <string>
49#include <tuple>
50#include <utility>
51#include <variant>
52#include <vector>
53
54namespace ScriptInterface {
55namespace Interactions {
56
57class BondedInteraction : public AutoParameters<BondedInteraction> {
58protected:
59 std::shared_ptr<::Bonded_IA_Parameters> m_bonded_ia;
60
61public:
62 std::shared_ptr<::Bonded_IA_Parameters> bonded_ia() { return m_bonded_ia; }
63 std::shared_ptr<const ::Bonded_IA_Parameters> bonded_ia() const {
64 return m_bonded_ia;
65 }
66
67protected:
70
71 std::set<std::string> get_valid_parameters() const {
72 auto const vec = valid_parameters();
73 return {vec.begin(), vec.end()};
74 }
75
76private:
77 void check_valid_parameters(VariantMap const &params) const {
78 auto const valid_keys = get_valid_parameters();
79 for (auto const &key : valid_keys) {
80 if (not params.contains(key)) {
81 throw std::runtime_error("Parameter '" + key + "' is missing");
82 }
83 }
84 for (auto const &key : std::views::elements<0>(params)) {
85 if (not valid_keys.contains(key)) {
86 throw std::runtime_error("Parameter '" + key + "' is not recognized");
87 }
88 }
89 }
90
91 void do_construct(VariantMap const &params) override {
93 check_valid_parameters(params);
95 });
96 }
97
98 virtual void construct_bond(VariantMap const &params) = 0;
99
100public:
101 bool operator==(BondedInteraction const &other) const {
102 return m_bonded_ia == other.m_bonded_ia;
103 }
104
105 Variant do_call_method(std::string const &name,
106 VariantMap const &params) override {
107 // this feature is needed to compare bonds
108 if (name == "is_same_bond") {
109 auto const bond_so =
111 return *this == *bond_so;
112 }
113 if (name == "get_num_partners") {
114 return number_of_partners(*bonded_ia());
115 }
116
117 return {};
118 }
119};
120
121template <class CoreIA> class BondedInteractionImpl : public BondedInteraction {
122public:
125 return std::get<CoreBondedInteraction>(*bonded_ia());
126 }
127};
128
129class FeneBond : public BondedInteractionImpl<::FeneBond> {
130public:
133 {"k", AutoParameter::read_only, [this]() { return get_struct().k; }},
134 {"d_r_max", AutoParameter::read_only,
135 [this]() { return get_struct().drmax; }},
136 {"r_0", AutoParameter::read_only, [this]() { return get_struct().r0; }},
137 });
138 }
139
140private:
141 void construct_bond(VariantMap const &params) override {
142 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
144 get_value<double>(params, "d_r_max"),
145 get_value<double>(params, "r_0")));
146 }
147};
148
149class HarmonicBond : public BondedInteractionImpl<::HarmonicBond> {
150public:
153 {"k", AutoParameter::read_only, [this]() { return get_struct().k; }},
154 {"r_0", AutoParameter::read_only, [this]() { return get_struct().r; }},
155 {"r_cut", AutoParameter::read_only,
156 [this]() { return get_struct().r_cut; }},
157 });
158 }
159
160private:
161 void construct_bond(VariantMap const &params) override {
163 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
165 get_value<double>(params, "r_cut")));
166 }
167};
168
169class QuarticBond : public BondedInteractionImpl<::QuarticBond> {
170public:
173 {"k0", AutoParameter::read_only, [this]() { return get_struct().k0; }},
174 {"k1", AutoParameter::read_only, [this]() { return get_struct().k1; }},
175 {"r", AutoParameter::read_only, [this]() { return get_struct().r; }},
176 {"r_cut", AutoParameter::read_only,
177 [this]() { return get_struct().r_cut; }},
178 });
179 }
180
181private:
182 void construct_bond(VariantMap const &params) override {
184 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
187 get_value<double>(params, "r_cut")));
188 }
189};
190
191class BondedCoulomb : public BondedInteractionImpl<::BondedCoulomb> {
192public:
195 {"prefactor", AutoParameter::read_only,
196 [this]() { return get_struct().prefactor; }},
197 });
198 }
199
200private:
201 void construct_bond(VariantMap const &params) override {
202 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
204 }
205};
206
207class BondedCoulombSR : public BondedInteractionImpl<::BondedCoulombSR> {
208public:
212 [this]() { return get_struct().q1q2; }},
213 });
214 }
215
216private:
217 void construct_bond(VariantMap const &params) override {
218 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
220 }
221};
222
223class AngleHarmonicBond : public BondedInteractionImpl<::AngleHarmonicBond> {
224public:
228 [this]() { return get_struct().bend; }},
230 [this]() { return get_struct().phi0; }},
231 });
232 }
233
234private:
235 void construct_bond(VariantMap const &params) override {
236 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
238 get_value<double>(params, "phi0")));
239 }
240};
241
242class AngleCosineBond : public BondedInteractionImpl<::AngleCosineBond> {
243public:
247 [this]() { return get_struct().bend; }},
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>(
257 get_value<double>(params, "phi0")));
258 }
259};
260
261class AngleCossquareBond : public BondedInteractionImpl<::AngleCossquareBond> {
262public:
266 [this]() { return get_struct().bend; }},
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>(
276 get_value<double>(params, "phi0")));
277 }
278};
279
280class DihedralBond : public BondedInteractionImpl<::DihedralBond> {
281public:
285 [this]() { return get_struct().mult; }},
287 [this]() { return get_struct().bend; }},
288 {"phase", AutoParameter::read_only,
289 [this]() { return get_struct().phase; }},
290 });
291 }
292
293private:
294 void construct_bond(VariantMap const &params) override {
296 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
298 get_value<double>(params, "phase")));
299 }
300};
301
303 : public BondedInteractionImpl<::TabulatedDistanceBond> {
304public:
308 [this]() { return get_struct().pot->minval; }},
310 [this]() { return get_struct().pot->maxval; }},
311 {"energy", AutoParameter::read_only,
312 [this]() { return get_struct().pot->energy_tab; }},
313 {"force", AutoParameter::read_only,
314 [this]() { return get_struct().pot->force_tab; }},
315 });
316 }
317
318private:
319 void construct_bond(VariantMap const &params) override {
321 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
323 get_value<std::vector<double>>(params, "energy"),
324 get_value<std::vector<double>>(params, "force")));
325 }
326};
327
328class TabulatedAngleBond : public BondedInteractionImpl<::TabulatedAngleBond> {
329public:
333 [this]() { return get_struct().pot->minval; }},
335 [this]() { return get_struct().pot->maxval; }},
336 {"energy", AutoParameter::read_only,
337 [this]() { return get_struct().pot->energy_tab; }},
338 {"force", AutoParameter::read_only,
339 [this]() { return get_struct().pot->force_tab; }},
340 });
341 }
342
343private:
344 void construct_bond(VariantMap const &params) override {
346 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
348 get_value<std::vector<double>>(params, "energy"),
349 get_value<std::vector<double>>(params, "force")));
350 }
351};
352
354 : public BondedInteractionImpl<::TabulatedDihedralBond> {
355public:
359 [this]() { return get_struct().pot->minval; }},
361 [this]() { return get_struct().pot->maxval; }},
362 {"energy", AutoParameter::read_only,
363 [this]() { return get_struct().pot->energy_tab; }},
364 {"force", AutoParameter::read_only,
365 [this]() { return get_struct().pot->force_tab; }},
366 });
367 }
368
369private:
370 void construct_bond(VariantMap const &params) override {
372 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
374 get_value<std::vector<double>>(params, "energy"),
375 get_value<std::vector<double>>(params, "force")));
376 }
377};
378
379class ThermalizedBond : public BondedInteractionImpl<::ThermalizedBond> {
380public:
383 {"temp_com", AutoParameter::read_only,
384 [this]() { return get_struct().temp_com; }},
385 {"gamma_com", AutoParameter::read_only,
386 [this]() { return get_struct().gamma_com; }},
387 {"temp_distance", AutoParameter::read_only,
388 [this]() { return get_struct().temp_distance; }},
389 {"gamma_distance", AutoParameter::read_only,
390 [this]() { return get_struct().gamma_distance; }},
391 {"r_cut", AutoParameter::read_only,
392 [this]() { return get_struct().r_cut; }},
393 });
394 }
395
396private:
397 void construct_bond(VariantMap const &params) override {
398 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
400 get_value<double>(params, "gamma_com"),
401 get_value<double>(params, "temp_distance"),
402 get_value<double>(params, "gamma_distance"),
403 get_value<double>(params, "r_cut")));
404 }
405};
406
407class RigidBond : public BondedInteractionImpl<::RigidBond> {
408public:
412 [this]() { return std::sqrt(get_struct().d2); }},
414 [this]() { return 0.5 * get_struct().p_tol; }},
416 [this]() { return get_struct().v_tol; }},
417 });
418 }
419
420private:
421 void construct_bond(VariantMap const &params) override {
423 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
425 get_value<double>(params, "vtol")));
426 }
427};
428
429class IBMTriel : public BondedInteractionImpl<::IBMTriel> {
430public:
433 {"k1", AutoParameter::read_only, [this]() { return get_struct().k1; }},
434 {"k2", AutoParameter::read_only, [this]() { return get_struct().k2; }},
436 [this]() { return std::get<0>(get_struct().p_ids); }},
438 [this]() { return std::get<1>(get_struct().p_ids); }},
440 [this]() { return std::get<2>(get_struct().p_ids); }},
441 {"maxDist", AutoParameter::read_only,
442 [this]() { return get_struct().maxDist; }},
443 {"elasticLaw", AutoParameter::read_only,
444 [this]() {
445 if (get_struct().elasticLaw == tElasticLaw::NeoHookean) {
446 return std::string("NeoHookean");
447 }
448 return std::string("Skalak");
449 }},
450 {"is_initialized", AutoParameter::read_only,
451 [this]() { return get_struct().is_initialized; }},
452 {"_cache", AutoParameter::read_only,
453 [this]() {
454 auto &s = get_struct();
455 return std::vector<double>{{s.l0, s.lp0, s.sinPhi0, s.cosPhi0,
456 s.area0, s.a1, s.a2, s.b1, s.b2}};
457 }},
458 });
459 }
460
461private:
462 void construct_bond(VariantMap const &params) override {
463 auto const law_name = get_value<std::string>(params, "elasticLaw");
465 if (law_name == "NeoHookean") {
467 } else if (law_name == "Skalak") {
469 } else {
470 throw std::invalid_argument(
471 "Invalid value for parameter 'elasticLaw': '" + law_name + "'");
472 }
474 get_value<int>(params, "ind1"), get_value<int>(params, "ind2"),
475 get_value<int>(params, "ind3"), get_value<double>(params, "maxDist"),
478 if (get_value_or<bool>(params, "is_initialized", false)) {
479 auto const cache = get_value<std::vector<double>>(params, "_cache");
480 assert(cache.size() == 9ul);
481 bond.l0 = cache[0];
482 bond.lp0 = cache[1];
483 bond.sinPhi0 = cache[2];
484 bond.cosPhi0 = cache[3];
485 bond.area0 = cache[4];
486 bond.a1 = cache[5];
487 bond.a2 = cache[6];
488 bond.b1 = cache[7];
489 bond.b2 = cache[8];
490 bond.is_initialized = true;
491 }
492 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(std::move(bond));
493 }
494};
495
496class IBMVolCons : public BondedInteractionImpl<::IBMVolCons> {
497public:
500 {"softID", AutoParameter::read_only,
501 [this]() { return static_cast<int>(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 get_struct().get_current_volume();
511 }
513 }
514
515private:
516 void construct_bond(VariantMap const &params) override {
517 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(
519 get_value<double>(params, "kappaV")));
520 }
521};
522
523class IBMTribend : public BondedInteractionImpl<::IBMTribend> {
524public:
527 {"kb", AutoParameter::read_only, [this]() { return get_struct().kb; }},
529 [this]() { return std::get<0>(get_struct().p_ids); }},
531 [this]() { return std::get<1>(get_struct().p_ids); }},
533 [this]() { return std::get<2>(get_struct().p_ids); }},
535 [this]() { return std::get<3>(get_struct().p_ids); }},
536 {"refShape", AutoParameter::read_only,
537 [this]() {
538 return std::string((get_struct().flat) ? "Flat" : "Initial");
539 }},
540 {"theta0", AutoParameter::read_only,
541 [this]() { return get_struct().theta0; }},
542 {"is_initialized", AutoParameter::read_only,
543 [this]() { return get_struct().is_initialized; }},
544 });
545 }
546
547private:
548 void construct_bond(VariantMap const &params) override {
549 auto const shape_name = get_value<std::string>(params, "refShape");
550 bool flat;
551 if (shape_name == "Flat") {
552 flat = true;
553 } else if (shape_name == "Initial") {
554 flat = false;
555 } else {
556 throw std::invalid_argument("Invalid value for parameter 'refShape': '" +
557 shape_name + "'");
558 }
560 get_value<int>(params, "ind1"), get_value<int>(params, "ind2"),
561 get_value<int>(params, "ind3"), get_value<int>(params, "ind4"),
562 get_value<double>(params, "kb"), flat);
563 if (get_value_or<bool>(params, "is_initialized", false)) {
564 bond.theta0 = get_value<double>(params, "theta0");
565 bond.is_initialized = true;
566 }
567 m_bonded_ia = std::make_shared<::Bonded_IA_Parameters>(std::move(bond));
568 }
569};
570
572 : public BondedInteractionImpl<::OifGlobalForcesBond> {
573public:
577 [this]() { return get_struct().A0_g; }},
579 [this]() { return get_struct().ka_g; }},
580 {"V0", AutoParameter::read_only, [this]() { return get_struct().V0; }},
581 {"kv", AutoParameter::read_only, [this]() { return get_struct().kv; }},
582 });
583 }
584
585private:
586 void construct_bond(VariantMap const &params) override {
588 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
589 get_value<double>(params, "A0_g"),
591 get_value<double>(params, "kv")));
592 }
593};
594
595class OifLocalForcesBond : public BondedInteractionImpl<::OifLocalForcesBond> {
596public:
599 {"r0", AutoParameter::read_only, [this]() { return get_struct().r0; }},
600 {"ks", AutoParameter::read_only, [this]() { return get_struct().ks; }},
601 {"kslin", AutoParameter::read_only,
602 [this]() { return get_struct().kslin; }},
604 [this]() { return get_struct().phi0; }},
605 {"kb", AutoParameter::read_only, [this]() { return get_struct().kb; }},
607 [this]() { return get_struct().A01; }},
609 [this]() { return get_struct().A02; }},
611 [this]() { return get_struct().kal; }},
612 {"kvisc", AutoParameter::read_only,
613 [this]() { return get_struct().kvisc; }},
614 });
615 }
616
617private:
618 void construct_bond(VariantMap const &params) override {
620 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction(
622 get_value<double>(params, "kslin"),
626 get_value<double>(params, "kvisc")));
627 }
628};
629
630class VirtualBond : public BondedInteractionImpl<::VirtualBond> {
631public:
634 std::make_shared<::Bonded_IA_Parameters>(CoreBondedInteraction());
635 }
636
637private:
638 void construct_bond(VariantMap const &) override {}
639};
640
641} // namespace Interactions
642} // namespace ScriptInterface
Data structures for bonded interactions.
int number_of_partners(Bonded_IA_Parameters const &iaparams)
Get the number of bonded partners for the specified bond.
Bind parameters in the script interface.
std::vector< std::string_view > valid_parameters() const final
void add_parameters(std::vector< AutoParameter > &&params)
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
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
std::shared_ptr<::Bonded_IA_Parameters > m_bonded_ia
std::set< std::string > get_valid_parameters() const
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
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.
std::string_view name() const
Implementation in thermostat.cpp.
tElasticLaw
Definition ibm_triel.hpp:32
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
STL namespace.
static SteepestDescentParameters params
Currently active steepest descent instance.
static constexpr const ReadOnly read_only
Recursive variant implementation.
Definition Variant.hpp:84