-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdiffusion_thomas_algorithm.cc
More file actions
333 lines (305 loc) · 13.1 KB
/
diffusion_thomas_algorithm.cc
File metadata and controls
333 lines (305 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright 2025 compiler-research.org, Salvador de la Torre Gonzalez, Luciana
* Melina Luque
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* SPDX-License-Identifier: Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file contains a model developed under Google Summer of Code (GSoC)
* for the compiler-research.org organization.
*/
#include "diffusion_thomas_algorithm.h"
#include "cart_cell.h"
#include "hyperparams.h"
#include "tumor_cell.h"
#include "core/agent/agent.h"
#include "core/container/math_array.h"
#include "core/diffusion/diffusion_grid.h"
#include "core/param/param.h"
#include "core/real_t.h"
#include "core/resource_manager.h"
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <string>
#include <utility>
#include <vector>
namespace bdm {
DiffusionThomasAlgorithm::DiffusionThomasAlgorithm(int substance_id,
std::string substance_name,
real_t dc, real_t mu,
int resolution, real_t dt,
bool dirichlet_border)
: DiffusionGrid(substance_id, std::move(substance_name), dc, mu,
resolution),
resolution_(static_cast<int>(GetResolution())),
d_space_(static_cast<real_t>(Simulation::GetActive()
->GetParam()
->Get<SimParam>()
->bounded_space_length) /
static_cast<real_t>(resolution_)),
dirichlet_border_(dirichlet_border),
jump_i_(1),
jump_j_(resolution_),
jump_(resolution_ * resolution_),
spatial_diffusion_coeff_(dc * dt / (d_space_ * d_space_)),
neg_diffusion_factor_(-spatial_diffusion_coeff_),
temporal_decay_coeff_(mu * dt / 3.0),
central_coeff_(1.0 + 2 * spatial_diffusion_coeff_ +
temporal_decay_coeff_),
edge_coeff_(1.0 + spatial_diffusion_coeff_ + temporal_decay_coeff_),
thomas_c_x_(resolution_, neg_diffusion_factor_),
thomas_denom_x_(resolution_, central_coeff_),
thomas_c_y_(resolution_, neg_diffusion_factor_),
thomas_denom_y_(resolution_, central_coeff_),
thomas_c_z_(resolution_, neg_diffusion_factor_),
thomas_denom_z_(resolution_, central_coeff_) {
SetTimeStep(dt);
// Initialize the denominators and coefficients for the Thomas algorithm
InitializeThomasAlgorithmVectors(thomas_denom_x_, thomas_c_x_);
InitializeThomasAlgorithmVectors(thomas_denom_y_, thomas_c_y_);
InitializeThomasAlgorithmVectors(thomas_denom_z_, thomas_c_z_);
}
void DiffusionThomasAlgorithm::InitializeThomasAlgorithmVectors(
std::vector<real_t>& thomas_denom, std::vector<real_t>& thomas_c) const {
thomas_denom[0] = edge_coeff_;
thomas_denom[resolution_ - 1] = edge_coeff_;
if (resolution_ == 1) {
thomas_denom[0] = 1.0 + temporal_decay_coeff_;
}
thomas_c[0] /= thomas_denom[0];
for (int i = 1; i < resolution_; ++i) {
thomas_denom[i] += spatial_diffusion_coeff_ * thomas_c[i - 1];
thomas_c[i] /= thomas_denom[i];
}
}
// Apply Dirichlet boundary conditions to the grid
void DiffusionThomasAlgorithm::ApplyDirichletBoundaryConditions() {
// FIXME: Fix BioDynaMo by returning a view or c++20 std::span.
const int32_t* dimensions_ptr = GetDimensionsPtr();
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
const real_t origin = dimensions_ptr[0];
const real_t simulated_time = GetSimulatedTime();
#pragma omp parallel
{
// We apply the Dirichlet boundary conditions to the first and last layers in
// each direction For z=0 and z=resolution_-1
#pragma omp for collapse(2)
for (int y = 0; y < resolution_; y++) {
for (int x = 0; x < resolution_; x++) {
const real_t real_x = origin + x * d_space_;
const real_t real_y = origin + y * d_space_;
// For z=0
int z = 0;
real_t real_z = origin + z * d_space_;
SetConcentration(x, y, z,
GetBoundaryCondition()->Evaluate(
real_x, real_y, real_z, simulated_time));
// For z=resolution_-1
z = resolution_ - 1;
real_z = origin + z * d_space_;
SetConcentration(x, y, z,
GetBoundaryCondition()->Evaluate(
real_x, real_y, real_z, simulated_time));
}
}
// For y=0 and y=resolution_-1
#pragma omp for collapse(2)
for (int z = 0; z < resolution_; z++) {
for (int x = 0; x < resolution_; x++) {
const real_t real_x = origin + x * d_space_;
const real_t real_z = origin + z * d_space_;
// For y=0
int y = 0;
real_t real_y = origin + y * d_space_;
SetConcentration(x, y, z,
GetBoundaryCondition()->Evaluate(
real_x, real_y, real_z, simulated_time));
// For y=resolution_-1
y = resolution_ - 1;
real_y = origin + y * d_space_;
SetConcentration(x, y, z,
GetBoundaryCondition()->Evaluate(
real_x, real_y, real_z, simulated_time));
}
}
// For x=0 and x=resolution_-1
#pragma omp for collapse(2)
for (int z = 0; z < resolution_; z++) {
for (int y = 0; y < resolution_; y++) {
const real_t real_y = origin + y * d_space_;
const real_t real_z = origin + z * d_space_;
// For x=0
int x = 0;
real_t real_x = origin + x * d_space_;
SetConcentration(x, y, z,
GetBoundaryCondition()->Evaluate(
real_x, real_y, real_z, simulated_time));
// For x=resolution_-1
x = resolution_ - 1;
real_x = origin + x * d_space_;
SetConcentration(x, y, z,
GetBoundaryCondition()->Evaluate(
real_x, real_y, real_z, simulated_time));
}
}
}
}
// Sets the concentration at a specific voxel
void DiffusionThomasAlgorithm::SetConcentration(size_t idx, real_t amount) {
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
const real_t* all_concentrations = GetAllConcentrations();
const real_t current_concentration = all_concentrations[idx];
ChangeConcentrationBy(idx, amount - current_concentration,
InteractionMode::kAdditive, false);
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
}
// Flattens the 3D coordinates (x, y, z) into a 1D index
size_t DiffusionThomasAlgorithm::GetBoxIndex(size_t x, size_t y,
size_t z) const {
assert(static_cast<int>(x) < resolution_ &&
static_cast<int>(y) < resolution_ &&
static_cast<int>(z) < resolution_ &&
"GetBoxIndex: coordinate out of bounds");
return z * resolution_ * resolution_ + y * resolution_ + x;
}
void DiffusionThomasAlgorithm::Step(real_t /*dt*/) {
// check if diffusion coefficient and decay constant are 0
// i.e. if we don't need to calculate diffusion update
if (IsFixedSubstance()) {
return;
}
DiffuseChemical();
// This should be done considering different border cases instead of using the
// dirichlet_border_ flag. However, there is a bug in BioDynaMo that makes
// bc_type be "Neumann" no matter what. In future versions of BioDynaMo this
// should be fixed
}
// This method solves the Diffusion Diferential equation using the Alternating
// Direction Implicit approach
void DiffusionThomasAlgorithm::DiffuseChemical() {
ApplyBoundaryConditionsIfNeeded();
// Solve for X-direction (direction = 0)
SolveDirectionThomas(0);
ApplyBoundaryConditionsIfNeeded();
// Solve for Y-direction (direction = 1)
SolveDirectionThomas(1);
ApplyBoundaryConditionsIfNeeded();
// Solve for Z-direction (direction = 2)
SolveDirectionThomas(2);
ApplyBoundaryConditionsIfNeeded();
// Change of concentration levels because of agents
ComputeConsumptionsSecretions();
}
void DiffusionThomasAlgorithm::ApplyBoundaryConditionsIfNeeded() {
if (dirichlet_border_) {
ApplyDirichletBoundaryConditions();
}
}
void DiffusionThomasAlgorithm::SolveDirectionThomas(int direction) {
const std::array<const std::vector<real_t>*, 3> all_denoms = {
&thomas_denom_x_, &thomas_denom_y_, &thomas_denom_z_};
const std::array<const std::vector<real_t>*, 3> all_c = {
&thomas_c_x_, &thomas_c_y_, &thomas_c_z_};
const std::array<int, 3> all_jumps = {jump_i_, jump_j_, jump_};
const std::vector<real_t>& thomas_denom = *all_denoms.at(direction);
const std::vector<real_t>& thomas_c = *all_c.at(direction);
const int jump = all_jumps.at(direction);
#pragma omp parallel for collapse(2)
for (int outer = 0; outer < resolution_; outer++) {
for (int middle = 0; middle < resolution_; middle++) {
// Forward elimination step
ForwardElimination(direction, outer, middle, thomas_denom, jump);
// Back substitution step
BackSubstitution(direction, outer, middle, thomas_c, jump);
}
}
}
void DiffusionThomasAlgorithm::ForwardElimination(
int direction, int outer, int middle,
const std::vector<real_t>& thomas_denom, int jump) {
// Get initial index based on direction
size_t ind = GetLoopIndex(direction, outer, middle, 0);
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
const real_t* all_concentrations = GetAllConcentrations();
const real_t initial_concentration = all_concentrations[ind];
SetConcentration(ind, initial_concentration / thomas_denom[0]);
// Forward elimination loop
for (int inner = 1; inner < resolution_; inner++) {
ind = GetLoopIndex(direction, outer, middle, inner);
const real_t current_concentration = all_concentrations[ind];
const real_t prev_concentration = all_concentrations[ind - jump];
SetConcentration(ind, (current_concentration +
spatial_diffusion_coeff_ * prev_concentration) /
thomas_denom[inner]);
}
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
}
void DiffusionThomasAlgorithm::BackSubstitution(
int direction, int outer, int middle, const std::vector<real_t>& thomas_c,
int jump) {
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
const real_t* all_concentrations = GetAllConcentrations();
// Back substitution loop
for (int inner = resolution_ - 2; inner >= 0; inner--) {
const size_t ind = GetLoopIndex(direction, outer, middle, inner);
const real_t current_concentration = all_concentrations[ind];
const real_t next_concentration = all_concentrations[ind + jump];
SetConcentration(
ind, current_concentration - thomas_c[inner] * next_concentration);
}
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
}
size_t DiffusionThomasAlgorithm::GetLoopIndex(int direction, int outer,
int middle, int inner) const {
switch (direction) {
case 0: // X-direction: outer=k, middle=j, inner=i
return GetBoxIndex(inner, middle, outer);
case 1: // Y-direction: outer=k, middle=i, inner=j
return GetBoxIndex(middle, inner, outer);
case 2: // Z-direction: outer=j, middle=i, inner=k
return GetBoxIndex(middle, outer, inner);
default:
return 0;
}
}
void DiffusionThomasAlgorithm::ComputeConsumptionsSecretions() {
// This method is called to compute the consumptions and secretions of
// substances by the tumor cells. It iterates over all agents and applies the
// consumption and secretion behaviors defined in the TumorCell class.
ResourceManager* rm = bdm::Simulation::GetActive()->GetResourceManager();
// in a future version of BioDynaMo this should be parallelized getting the
// agents inside each chemical voxel and treating each voxel independently.
// Fixme:this dynamic casting could be optimized in a future version
rm->ForEachAgent([this](bdm::Agent* agent) {
if (auto* cell = dynamic_cast<TumorCell*>(agent)) {
// Handle TumorCell agents
const Real3& pos = cell->GetPosition();
const real_t conc = this->GetValue(pos);
const real_t new_conc =
cell->ConsumeSecreteSubstance(GetContinuumId(), conc);
this->ChangeConcentrationBy(pos, new_conc - conc,
InteractionMode::kAdditive, false);
} else if (auto* cell = dynamic_cast<CarTCell*>(agent)) {
// Handle CarTCell agents
const Real3& pos = cell->GetPosition();
const real_t conc = GetValue(pos);
const real_t new_conc =
cell->ConsumeSecreteSubstance(GetContinuumId(), conc);
ChangeConcentrationBy(pos, new_conc - conc, InteractionMode::kAdditive,
false);
}
});
}
} // namespace bdm