ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ImmersedBoundaries.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-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
21
22#include "BoxGeometry.hpp"
23#include "Particle.hpp"
25#include "communication.hpp"
26#include "ibm_volcons.hpp"
27#include "system/System.hpp"
28
30
31#include <utils/Span.hpp>
32#include <utils/Vector.hpp>
33#include <utils/constants.hpp>
34
35#include <boost/mpi/collectives/all_reduce.hpp>
36#include <boost/range/algorithm/find_if.hpp>
37
38#include <functional>
39#include <utility>
40#include <vector>
41
42/** Calculate volumes, volume force and add it to each virtual particle. */
44 if (VolumeInitDone && !BoundariesFound) {
45 return;
46 }
47 calc_volumes(cs);
48 calc_volume_force(cs);
49}
50
51/** Initialize volume conservation */
53 // Check since this function is called at the start of every integrate loop
54 // Also check if volume has been set due to reading of a checkpoint
55 if (not BoundariesFound) {
56 BoundariesFound = std::any_of(
57 bonded_ia_params.begin(), bonded_ia_params.end(), [](auto const &kv) {
58 return (boost::get<IBMVolCons>(&(*kv.second)) != nullptr);
59 });
60 }
61
62 if (!VolumeInitDone && BoundariesFound) {
63 // Calculate volumes
64 calc_volumes(cs);
65
66 // Loop through all bonded interactions and check if we need to set the
67 // reference volume
68 for (auto &kv : bonded_ia_params) {
69 if (auto *v = boost::get<IBMVolCons>(&(*kv.second))) {
70 // This check is important because InitVolumeConservation may be called
71 // accidentally during the integration. Then we must not reset the
72 // reference
73 BoundariesFound = true;
74 if (v->volRef == 0.) {
75 v->volRef = VolumesCurrent[static_cast<unsigned int>(v->softID)];
76 }
77 }
78 }
79
80 VolumeInitDone = true;
81 }
82}
83
84static const IBMVolCons *vol_cons_parameters(Particle const &p1) {
85 auto const it = boost::find_if(p1.bonds(), [](auto const &bond) -> bool {
86 return boost::get<IBMVolCons>(bonded_ia_params.at(bond.bond_id()).get());
87 });
88
89 return (it != p1.bonds().end())
90 ? boost::get<IBMVolCons>(bonded_ia_params.at(it->bond_id()).get())
91 : nullptr;
92}
93
94/** Calculate partial volumes on all compute nodes and call MPI to sum up.
95 * See @cite zhang01b, @cite dupin08a, @cite kruger12a.
96 */
97void ImmersedBoundaries::calc_volumes(CellStructure &cs) {
98
99 if (!BoundariesFound)
100 return;
101
102 auto const &box_geo = *System::get_system().box_geo;
103
104 // Partial volumes for each soft particle, to be summed up
105 std::vector<double> tempVol(VolumesCurrent.size());
106
107 // Loop over all particles on local node
108 cs.bond_loop([&tempVol, &box_geo](Particle &p1, int bond_id,
109 Utils::Span<Particle *> partners) {
110 auto const vol_cons_params = vol_cons_parameters(p1);
111
112 if (vol_cons_params &&
113 boost::get<IBMTriel>(bonded_ia_params.at(bond_id).get()) != nullptr) {
114 // Our particle is the leading particle of a triel
115 // Get second and third particle of the triangle
116 Particle &p2 = *partners[0];
117 Particle &p3 = *partners[1];
118
119 // Unfold position of first node.
120 // This is to get a continuous trajectory with no jumps when box
121 // boundaries are crossed.
122 auto const x1 = box_geo.unfolded_position(p1.pos(), p1.image_box());
123 auto const x2 = x1 + box_geo.get_mi_vector(p2.pos(), x1);
124 auto const x3 = x1 + box_geo.get_mi_vector(p3.pos(), x1);
125
126 // Volume of this tetrahedron
127 // See @cite zhang01b
128 // The volume can be negative, but it is not necessarily the
129 // "signed volume" in the above paper (the sign of the real
130 // "signed volume" must be calculated using the normal vector; the
131 // result of the calculation here is simply a term in the sum
132 // required to calculate the volume of a particle). Again, see the
133 // paper. This should be equivalent to the formulation using
134 // vector identities in @cite kruger12a
135
136 const double v321 = x3[0] * x2[1] * x1[2];
137 const double v231 = x2[0] * x3[1] * x1[2];
138 const double v312 = x3[0] * x1[1] * x2[2];
139 const double v132 = x1[0] * x3[1] * x2[2];
140 const double v213 = x2[0] * x1[1] * x3[2];
141 const double v123 = x1[0] * x2[1] * x3[2];
142
143 tempVol[static_cast<unsigned int>(vol_cons_params->softID)] +=
144 1.0 / 6.0 * (-v321 + v231 + v312 - v132 - v213 + v123);
145 }
146 return false;
147 });
148
149 // Sum up and communicate
150 boost::mpi::all_reduce(comm_cart, tempVol.data(),
151 static_cast<int>(tempVol.size()),
152 VolumesCurrent.data(), std::plus<double>());
153}
154
155/** Calculate and add the volume force to each node */
156void ImmersedBoundaries::calc_volume_force(CellStructure &cs) {
157 if (!BoundariesFound)
158 return;
159
160 auto const &box_geo = *System::get_system().box_geo;
161
162 cs.bond_loop([this, &box_geo](Particle &p1, int bond_id,
163 Utils::Span<Particle *> partners) {
164 if (boost::get<IBMTriel>(bonded_ia_params.at(bond_id).get()) != nullptr) {
165 // Check if particle has an IBM Triel bonded interaction and an
166 // IBM VolCons bonded interaction. Basically this loops over all
167 // triangles, not all particles. First round to check for volume
168 // conservation.
169 auto const vol_cons_params = vol_cons_parameters(p1);
170 if (not vol_cons_params)
171 return false;
172
173 auto const current_volume =
174 VolumesCurrent[static_cast<unsigned int>(vol_cons_params->softID)];
175
176 // Our particle is the leading particle of a triel
177 // Get second and third particle of the triangle
178 Particle &p2 = *partners[0];
179 Particle &p3 = *partners[1];
180
181 // Unfold position of first node.
182 // This is to get a continuous trajectory with no jumps when box
183 // boundaries are crossed.
184 auto const x1 = box_geo.unfolded_position(p1.pos(), p1.image_box());
185
186 // Unfolding seems to work only for the first particle of a triel
187 // so get the others from relative vectors considering PBC
188 auto const a12 = box_geo.get_mi_vector(p2.pos(), x1);
189 auto const a13 = box_geo.get_mi_vector(p3.pos(), x1);
190
191 // Now we have the true and good coordinates
192 // This is eq. (9) in @cite dupin08a.
193 auto const n = vector_product(a12, a13);
194 const double ln = n.norm();
195 const double A = 0.5 * ln;
196 const double fact = vol_cons_params->kappaV *
197 (current_volume - vol_cons_params->volRef) /
198 current_volume;
199
200 auto const nHat = n / ln;
201 auto const force = -fact * A * nHat;
202
203 p1.force() += force;
204 p2.force() += force;
205 p3.force() += force;
206 }
207 return false;
208 });
209}
static const IBMVolCons * vol_cons_parameters(Particle const &p1)
Vector implementation and trait types for boost qvm interoperability.
BondedInteractionsMap bonded_ia_params
Field containing the parameters of the bonded ia types.
Data structures for bonded interactions.
mapped_type at(key_type const &key) const
void volume_conservation(CellStructure &cs)
Calculate volumes, volume force and add it to each virtual particle.
void init_volume_conservation(CellStructure &cs)
Initialize volume conservation.
std::shared_ptr< BoxGeometry > box_geo
A stripped-down version of std::span from C++17.
Definition Span.hpp:38
boost::mpi::communicator comm_cart
The communicator.
System & get_system()
Describes a cell structure / cell system.
void bond_loop(BondKernel const &bond_kernel)
Bonded pair loop.
Parameters for IBM volume conservation bond.
Struct holding all information for one particle.
Definition Particle.hpp:393
auto const & image_box() const
Definition Particle.hpp:442
auto const & bonds() const
Definition Particle.hpp:426
auto const & pos() const
Definition Particle.hpp:429