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-2026 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 <variant>
39#include <vector>
40
41/** Calculate volumes, volume force and add it to each virtual particle. */
43 if (VolumeInitDone && !BoundariesFound) {
44 return;
45 }
46 calc_volumes(cs);
47 calc_volume_force(cs);
48}
49
50/** Initialize volume conservation */
52 auto const &bonded_ias = *get_system().bonded_ias;
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::ranges::any_of(
57 std::views::elements<1>(bonded_ias), [](auto const &handle) {
58 return std::holds_alternative<IBMVolCons>(*handle);
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 &handle : std::views::elements<1>(bonded_ias)) {
69 if (auto *v = std::get_if<IBMVolCons>(handle.get())) {
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[v->softID];
76 }
77 }
78 }
79
80 VolumeInitDone = true;
81 }
82}
83
84static IBMVolCons const *
86 Particle const &p1) {
87 auto const it = boost::find_if(p1.bonds(), [&](auto const &bond) -> bool {
88 return std::holds_alternative<IBMVolCons>(*bonded_ias.at(bond.bond_id()));
89 });
90
91 return (it != p1.bonds().end())
92 ? std::get_if<IBMVolCons>(bonded_ias.at(it->bond_id()).get())
93 : nullptr;
94}
95
96/** Calculate partial volumes on all compute nodes and call MPI to sum up.
97 * See @cite zhang01b, @cite dupin08a, @cite kruger12a.
98 */
99void ImmersedBoundaries::calc_volumes(CellStructure &cs) {
100
101 if (!BoundariesFound)
102 return;
103
104 auto const &box_geo = *get_system().box_geo;
105 auto const &bonded_ias = *get_system().bonded_ias;
106
107 // Partial volumes for each soft particle, to be summed up
108 std::vector<double> tempVol(VolumesCurrent.size());
109
110 // Loop over all particles on local node
111 cs.bond_loop([&tempVol, &box_geo, &bonded_ias](
112 Particle &p1, int bond_id, std::span<Particle *> partners) {
113 auto const vol_cons_params = vol_cons_parameters(bonded_ias, p1);
114
115 if (vol_cons_params &&
116 std::holds_alternative<IBMTriel>(*bonded_ias.at(bond_id).get())) {
117 // Our particle is the leading particle of a triel
118 // Get second and third particle of the triangle
119 Particle &p2 = *partners[0];
120 Particle &p3 = *partners[1];
121
122 // Unfold position of first node.
123 // This is to get a continuous trajectory with no jumps when box
124 // boundaries are crossed.
125 auto const x1 = box_geo.unfolded_position(p1.pos(), p1.image_box());
126 auto const x2 = x1 + box_geo.get_mi_vector(p2.pos(), x1);
127 auto const x3 = x1 + box_geo.get_mi_vector(p3.pos(), x1);
128
129 // Volume of this tetrahedron
130 // See @cite zhang01b
131 // The volume can be negative, but it is not necessarily the
132 // "signed volume" in the above paper (the sign of the real
133 // "signed volume" must be calculated using the normal vector; the
134 // result of the calculation here is simply a term in the sum
135 // required to calculate the volume of a particle). Again, see the
136 // paper. This should be equivalent to the formulation using
137 // vector identities in @cite kruger12a
138
139 const double v321 = x3[0] * x2[1] * x1[2];
140 const double v231 = x2[0] * x3[1] * x1[2];
141 const double v312 = x3[0] * x1[1] * x2[2];
142 const double v132 = x1[0] * x3[1] * x2[2];
143 const double v213 = x2[0] * x1[1] * x3[2];
144 const double v123 = x1[0] * x2[1] * x3[2];
145
146 tempVol[vol_cons_params->softID] +=
147 1.0 / 6.0 * (-v321 + v231 + v312 - v132 - v213 + v123);
148 }
149 return false;
150 });
151
152 // Sum up and communicate
153 boost::mpi::all_reduce(comm_cart, tempVol.data(),
154 static_cast<int>(tempVol.size()),
155 VolumesCurrent.data(), std::plus<double>());
156}
157
158/** Calculate and add the volume force to each node */
159void ImmersedBoundaries::calc_volume_force(CellStructure &cs) {
160 if (!BoundariesFound)
161 return;
162
163 auto const &box_geo = *get_system().box_geo;
164 auto const &bonded_ias = *get_system().bonded_ias;
165
166 cs.bond_loop([this, &box_geo, &bonded_ias](Particle &p1, int bond_id,
167 std::span<Particle *> partners) {
168 if (std::holds_alternative<IBMTriel>(*bonded_ias.at(bond_id).get())) {
169 // Check if particle has an IBM Triel bonded interaction and an
170 // IBM VolCons bonded interaction. Basically this loops over all
171 // triangles, not all particles. First round to check for volume
172 // conservation.
173 auto const vol_cons_params = vol_cons_parameters(bonded_ias, p1);
174 if (not vol_cons_params)
175 return false;
176
177 auto const current_volume =
178 VolumesCurrent[static_cast<unsigned int>(vol_cons_params->softID)];
179
180 // Our particle is the leading particle of a triel
181 // Get second and third particle of the triangle
182 Particle &p2 = *partners[0];
183 Particle &p3 = *partners[1];
184
185 // Unfold position of first node.
186 // This is to get a continuous trajectory with no jumps when box
187 // boundaries are crossed.
188 auto const x1 = box_geo.unfolded_position(p1.pos(), p1.image_box());
189
190 // Unfolding seems to work only for the first particle of a triel
191 // so get the others from relative vectors considering PBC
192 auto const a12 = box_geo.get_mi_vector(p2.pos(), x1);
193 auto const a13 = box_geo.get_mi_vector(p3.pos(), x1);
194
195 // Now we have the true and good coordinates
196 // This is eq. (9) in @cite dupin08a.
197 auto const n = vector_product(a12, a13);
198 const double ln = n.norm();
199 const double A = 0.5 * ln;
200 const double fact = vol_cons_params->kappaV *
201 (current_volume - vol_cons_params->volRef) /
202 current_volume;
203
204 auto const nHat = n / ln;
205 auto const force = -fact * A * nHat;
206
207 p1.force() += force;
208 p2.force() += force;
209 p3.force() += force;
210 }
211 return false;
212 });
213}
214
216 auto const new_size = bond.softID + 1u;
217 if (new_size > VolumesCurrent.size()) {
218 VolumesCurrent.resize(new_size);
219 }
220 bond.set_volumes_view(VolumesCurrent);
221}
static IBMVolCons const * vol_cons_parameters(BondedInteractionsMap const &bonded_ias, Particle const &p1)
Data structures for bonded interactions.
container for bonded interactions.
mapped_type const & 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:435