-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscalar_integral.cpp
More file actions
43 lines (37 loc) · 1.45 KB
/
Copy pathscalar_integral.cpp
File metadata and controls
43 lines (37 loc) · 1.45 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
#include "adaptivesimplex/adaptive/adaptive_loop.h"
#include "adaptivesimplex/adaptive/simplex_integrand.h"
#include "adaptivesimplex/core/root_mesh.h"
#include "adaptivesimplex/core/vertex_cache.h"
#include <iostream>
#include <span>
namespace adaptive = adaptivesimplex::adaptive;
namespace core = adaptivesimplex::core;
int main() {
// Start with a coarse 1D mesh over [0, 1].
auto geometry = core::root_geometry(1, 2);
auto cache = core::VertexCache<double>{};
auto integrand = adaptive::simplex_integrand(
cache,
[](std::span<const double> point) {
const double x = point[0];
return x * x;
},
[](const core::Geometry& geometry, core::SimplexId simplex_id,
const core::VertexCache<double>& vertex_cache) {
const auto& simplex = geometry.simplices().simplex(simplex_id);
double sum = 0.0;
for (const auto vertex_id : simplex.vertex_ids) {
sum += vertex_cache.get(vertex_id);
}
return simplex.volume * sum / static_cast<double>(simplex.vertex_ids.size());
}
);
const auto result = adaptive::run(geometry, integrand, adaptive::Options{
.target_error = 1e-4,
.max_refinements = 128,
.preview_depth = 1,
});
std::cout << "integral ~= " << result.integral << "\n";
std::cout << "estimated error = " << result.stopping_error << "\n";
return result.converged ? 0 : 1;
}