Skip to content

Commit 43e1711

Browse files
committed
Try fixing the ptr arithmetics
1 parent af43e1a commit 43e1711

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/diffusion_thomas_algorithm.cc

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ void DiffusionThomasAlgorithm::InitializeThomasAlgorithmVectors(
8888

8989
// Apply Dirichlet boundary conditions to the grid
9090
void DiffusionThomasAlgorithm::ApplyDirichletBoundaryConditions() {
91+
// FIXME: Fix BioDynamo to return a view or std::span.
9192
const auto* dimensions_ptr = GetDimensionsPtr();
92-
const real_t origin = dimensions_ptr
93-
[0]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
93+
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
94+
const real_t origin = dimensions_ptr[0];
9495
const real_t simulated_time = GetSimulatedTime();
9596
#pragma omp parallel
9697
{
@@ -164,10 +165,22 @@ void DiffusionThomasAlgorithm::ApplyDirichletBoundaryConditions() {
164165
}
165166
}
166167

168+
class ConcentrationView {
169+
const real_t* data_;
170+
size_t size_;
171+
public:
172+
ConcentrationView(const real_t* data, size_t size) : data_(data), size_(size) {}
173+
174+
const real_t& at(size_t i) const {
175+
if (i >= size_) throw std::out_of_range("Index out of range");
176+
return data_[i];
177+
}
178+
};
179+
167180
// Sets the concentration at a specific voxel
168181
void DiffusionThomasAlgorithm::SetConcentration(size_t idx, real_t amount) {
169-
const auto* all_concentrations = GetAllConcentrations();
170-
const real_t current_concentration = all_concentrations[idx];
182+
ConcentrationView all_concentrations(GetAllConcentrations(), GetConcentrationCount());
183+
const real_t current_concentration = ConcentrationView.at(idx);
171184
ChangeConcentrationBy(idx, amount - current_concentration,
172185
InteractionMode::kAdditive, false);
173186
}
@@ -270,8 +283,8 @@ void DiffusionThomasAlgorithm::ForwardElimination(
270283
const std::vector<real_t>& thomas_denom, unsigned int jump) {
271284
// Get initial index based on direction
272285
size_t ind = GetLoopIndex(direction, outer, middle, 0);
273-
const auto* all_concentrations = GetAllConcentrations();
274-
const real_t initial_concentration = all_concentrations[ind];
286+
ConcentrationView all_concentrations(GetAllConcentrations(), GetConcentrationCount());
287+
const real_t initial_concentration = all_concentrations.at(ind);
275288
SetConcentration(ind, initial_concentration / thomas_denom[0]);
276289

277290
// Forward elimination loop
@@ -288,14 +301,14 @@ void DiffusionThomasAlgorithm::ForwardElimination(
288301
void DiffusionThomasAlgorithm::BackSubstitution(
289302
unsigned int direction, unsigned int outer, unsigned int middle,
290303
const std::vector<real_t>& thomas_c, unsigned int jump) {
291-
const auto* all_concentrations = GetAllConcentrations();
304+
ConcentrationView all_concentrations(GetAllConcentrations(), GetConcentrationCount());
292305

293306
// Back substitution loop
294307
for (int inner = static_cast<int>(resolution_) - 2; inner >= 0; inner--) {
295308
const size_t ind = GetLoopIndex(direction, outer, middle,
296309
static_cast<unsigned int>(inner));
297-
const real_t current_concentration = all_concentrations[ind];
298-
const real_t next_concentration = all_concentrations[ind + jump];
310+
const real_t current_concentration = all_concentrations.at(ind);
311+
const real_t next_concentration = all_concentrations.at(ind + jump);
299312
SetConcentration(
300313
ind, current_concentration -
301314
thomas_c[static_cast<size_t>(inner)] * next_concentration);

0 commit comments

Comments
 (0)