pnm 1.0.3
peclet.pnm — pore-network extraction from SDF geometry (Kokkos)
Loading...
Searching...
No Matches
pnm_bindings_checks.hpp
Go to the documentation of this file.
1
14#pragma once
15
16#include <cmath>
17#include <cstddef>
18#include <cstdio>
19#include <new>
20#include <optional>
21#include <stdexcept>
22#include <string>
23#include <vector>
24
25#include "pore_kernels.hpp" // pnm::Pore
26
28
29inline std::string num(double v) {
30 char b[32];
31 std::snprintf(b, sizeof b, "%g", v);
32 return b;
33}
34inline void require(bool ok, const char* who, const std::string& name, const std::string& rule,
35 double v) {
36 if (!ok) {
37 throw std::invalid_argument(std::string(who) + ": " + name + " must be " + rule + ", got " +
38 num(v));
39 }
40}
41inline std::string component(const char* name, std::size_t i) {
42 return std::string(name) + "[" + std::to_string(i) + "]";
43}
44
46inline void triple(const char* who, const char* name, std::size_t size) {
47 if (size != 3) {
48 throw std::invalid_argument(std::string(who) + ": " + name +
49 " must be 3 values (z, y, x), got " + std::to_string(size));
50 }
51}
52
53inline void finiteEach(const char* who, const char* name, const std::vector<double>& a,
54 const char* dim) {
55 for (std::size_t i = 0; i < a.size(); ++i) {
56 require(std::isfinite(static_cast<float>(a[i])), who, component(name, i),
57 std::string("finite as float32 (") + dim + ")", a[i]);
58 }
59}
60
61inline void positiveEach(const char* who, const char* name, const std::vector<double>& a,
62 const char* dim) {
63 for (std::size_t i = 0; i < a.size(); ++i) {
64 const float f = static_cast<float>(a[i]);
65 require(std::isfinite(f) && f > 0.0f, who, component(name, i),
66 std::string("finite and > 0 as float32 (") + dim + ")", a[i]);
67 }
68}
69
70inline void countEach(const char* who, const char* name, const std::vector<int>& a, int lo) {
71 for (std::size_t i = 0; i < a.size(); ++i) {
72 require(a[i] >= lo, who, component(name, i),
73 "an integer >= " + std::to_string(lo) + " (voxels)", a[i]);
74 }
75}
76
77// --- the checks of one binding entry point ------------------------------------------------
78
80inline void spacingZyx(const char* who, const std::vector<double>& spacing) {
81 triple(who, "spacing_zyx", spacing.size());
82 positiveEach(who, "spacing_zyx", spacing, "length units");
83}
84
85inline void grid(const char* who, const std::vector<double>& origin,
86 const std::vector<double>& spacing) {
87 triple(who, "origin_zyx", origin.size());
88 finiteEach(who, "origin_zyx", origin, "length units");
89 spacingZyx(who, spacing);
90}
91
93inline void globalShapeZyx(const char* who, const std::vector<int>& shape, bool sized) {
94 if (!sized) {
95 triple(who, "global_shape_zyx", shape.size());
96 }
97 countEach(who, "global_shape_zyx", shape, 1);
98}
99
102inline void topologyShape(const std::optional<std::vector<int>>& shape) {
103 if (!shape || shape->size() != 3) {
104 return;
105 }
106 countEach("extract_topology", "shape_zyx", *shape, 0);
107 std::size_t n = 1;
108 for (int s : *shape) {
109 require(!__builtin_mul_overflow(n, static_cast<std::size_t>(s), &n), "extract_topology",
110 "shape_zyx", "a grid whose voxel count fits in 64 bits", static_cast<double>(s));
111 }
112}
113
115inline void initPore(Pore* p, float x, float y, float z, float radius) {
116 require(std::isfinite(x), "Pore", "x", "finite (length units)", x);
117 require(std::isfinite(y), "Pore", "y", "finite (length units)", y);
118 require(std::isfinite(z), "Pore", "z", "finite (length units)", z);
119 require(std::isfinite(radius) && radius >= 0.0f, "Pore", "radius",
120 "finite and >= 0 (length units)", radius);
121 new (p) Pore{x, y, z, radius};
122}
123
124} // namespace pnm::binding_checks
void finiteEach(const char *who, const char *name, const std::vector< double > &a, const char *dim)
Each component finite once narrowed to float32 (a coordinate: any sign).
std::string num(double v)
std::string component(const char *name, std::size_t i)
void grid(const char *who, const std::vector< double > &origin, const std::vector< double > &spacing)
origin_zyx + spacing_zyx of extract_pores / extract_pore_network(_mpi).
void topologyShape(const std::optional< std::vector< int > > &shape)
extract_topology's shape_zyx, when given as three values: voxel counts >= 0 whose product fits in std...
void require(bool ok, const char *who, const std::string &name, const std::string &rule, double v)
void positiveEach(const char *who, const char *name, const std::vector< double > &a, const char *dim)
Each component finite and > 0 once narrowed to float32 (a cell size).
void spacingZyx(const char *who, const std::vector< double > &spacing)
spacing_zyx: the grid's cell size per axis, three values > 0 (length units).
void globalShapeZyx(const char *who, const std::vector< int > &shape, bool sized)
The global grid of the ORB decomposition: at least one voxel per axis.
void countEach(const char *who, const char *name, const std::vector< int > &a, int lo)
Each component an integer >= lo (a voxel count).
void triple(const char *who, const char *name, std::size_t size)
A z-y-x triple: exactly three values.
void initPore(Pore *p, float x, float y, float z, float radius)
Pore(x, y, z, radius): a finite centre and a finite radius >= 0 (Pore() is all zeros).
peclet.pnm — the ONE set of stage kernels shared by the single-rank (pore_extraction....