Skip to content

Commit

Permalink
Update the non uniform heat equation example
Browse files Browse the repository at this point in the history
  • Loading branch information
tpadioleau committed Jan 2, 2025
1 parent 4ac5ed1 commit 2d4de40
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions examples/non_uniform_heat_equation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// SPDX-License-Identifier: MIT

//! [includes]
#ifndef NDEBUG
#include <algorithm>
#endif
#include <cassert>
#include <cmath>
#include <cstddef>
Expand Down Expand Up @@ -36,54 +34,54 @@ std::vector<double> generate_random_vector(int n, double lower_bound, double hig

double const dx = (higher_bound - lower_bound) / (n - 1);

std::vector<double> vec(n);
std::vector<double> points(n);

// Generate a uniform mesh
for (int i = 0; i < n; ++i) {
vec[i] = lower_bound + i * dx;
points[i] = lower_bound + i * dx;
}
// Add a random perturbation
for (int i = 1; i < n - 1; ++i) {
vec[i] += dis(gen) * dx;
points[i] += dis(gen) * dx;
}

assert(std::is_sorted(vec.begin(), vec.end()));
assert(std::is_sorted(points.begin(), points.end()));

return vec;
return points;
}
//! [vector_generator]

std::vector<double> periodic_extrapolation_left(int gw, std::vector<double> const& points)
{
assert(gw > 0);
assert(points.size() >= gw);
assert(gw >= 0);
assert(points.size() > gw);
assert(std::is_sorted(points.begin(), points.end()));

std::vector<double> ghost(gw);
if (gw > 0) {
auto rit1 = ghost.rbegin();
auto const rit1_e = ghost.rend();
auto rit2 = std::next(points.crbegin());
for (; rit1 != rit1_e; ++rit1, ++rit2) {
*rit1 = *rit2 - (points.back() - points.front());
}
double const period = points.back() - points.front();
auto const it = std::next(points.crbegin());
std::transform(it, std::next(it, gw), ghost.rbegin(), [period](double pos) {
return pos - period;
});
}

return ghost;
}

std::vector<double> periodic_extrapolation_right(int gw, std::vector<double> const& points)
{
assert(gw > 0);
assert(points.size() >= gw);
assert(gw >= 0);
assert(points.size() > gw);
assert(std::is_sorted(points.begin(), points.end()));

std::vector<double> ghost(gw);
if (gw > 0) {
auto it1 = ghost.begin();
auto const it1_e = ghost.end();
auto it2 = std::next(points.cbegin());
for (; it1 != it1_e; ++it1, ++it2) {
*it1 = *it2 + (points.back() - points.front());
}
double const period = points.back() - points.front();
auto const it = std::next(points.cbegin());
std::transform(it, std::next(it, gw), ghost.begin(), [period](double pos) {
return pos + period;
});
}

return ghost;
Expand Down

0 comments on commit 2d4de40

Please sign in to comment.