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