peclet-dem 1.1.0
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
dem_bindings_checks.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <array>
12#include <cmath>
13#include <cstddef>
14#include <cstdint>
15#include <cstdio>
16#include <stdexcept>
17#include <string>
18#include <tuple>
19#include <vector>
20
21#include "narrowphase.hpp" // kMaxMaterials
22
24
26using F3Arg = std::array<float, 3>;
27
28inline std::string num(double v) {
29 char b[32];
30 std::snprintf(b, sizeof b, "%g", v);
31 return b;
32}
33inline void require(bool ok, const char* who, const std::string& name, const std::string& rule,
34 double v) {
35 if (!ok) {
36 throw std::invalid_argument(std::string(who) + ": " + name + " must be " + rule + ", got " +
37 num(v));
38 }
39}
40inline void finite(const char* who, const std::string& name, double v, const char* dim) {
41 require(std::isfinite(v), who, name, std::string("finite (") + dim + ")", v);
42}
43inline void positive(const char* who, const std::string& name, double v, const char* dim) {
44 require(std::isfinite(v) && v > 0.0, who, name, std::string("finite and > 0 (") + dim + ")", v);
45}
46inline void nonneg(const char* who, const std::string& name, double v, const char* dim) {
47 require(std::isfinite(v) && v >= 0.0, who, name, std::string("finite and >= 0 (") + dim + ")", v);
48}
49
50inline void restitutionCoef(const char* who, const char* name, double v) {
51 require(std::isfinite(v) && v >= 0.0 && v <= 1.0, who, name,
52 "finite and in [0, 1] (dimensionless)", v);
53}
54
55inline void poissonRatio(const char* who, const char* name, double v) {
56 require(std::isfinite(v) && v > -1.0 && v <= 0.5, who, name,
57 "finite and in (-1, 0.5] (dimensionless)", v);
58}
59
60inline std::string component(const char* name, std::size_t i) {
61 return std::string(name) + "[" + std::to_string(i) + "]";
62}
63template <class T, std::size_t N>
64inline void finiteEach(const char* who, const char* name, const std::array<T, N>& a,
65 const char* dim) {
66 for (std::size_t i = 0; i < N; ++i) {
67 finite(who, component(name, i), a[i], dim);
68 }
69}
70template <class T, std::size_t N>
71inline void positiveEach(const char* who, const char* name, const std::array<T, N>& a,
72 const char* dim) {
73 for (std::size_t i = 0; i < N; ++i) {
74 positive(who, component(name, i), a[i], dim);
75 }
76}
77template <class T, std::size_t N>
78inline void nonnegEach(const char* who, const char* name, const std::array<T, N>& a,
79 const char* dim) {
80 for (std::size_t i = 0; i < N; ++i) {
81 nonneg(who, component(name, i), a[i], dim);
82 }
83}
84
85inline void count(const char* who, const std::string& name, std::int64_t v, std::int64_t lo) {
86 require(v >= lo, who, name, "an integer >= " + std::to_string(lo), static_cast<double>(v));
87}
88
90inline void sampleCounts(const char* who, int rx, int ry, int rz) {
91 count(who, "resolution[0]", rx, 1);
92 count(who, "resolution[1]", ry, 1);
93 count(who, "resolution[2]", rz, 1);
94}
95
96inline void sdfShapeArgs(const char* who, const F3Arg& origin, const F3Arg& spacing,
97 const F3Arg& inv_inertia, float bounding_radius) {
98 finiteEach(who, "origin", origin, "length");
99 positiveEach(who, "spacing", spacing, "length");
100 nonnegEach(who, "inv_inertia", inv_inertia, "1/length^2");
101 positive(who, "bounding_radius", bounding_radius, "length");
102}
103
104inline void domainCorners(const F3Arg& mn, const F3Arg& mx) {
105 finiteEach("set_domain", "min", mn, "length");
106 finiteEach("set_domain", "max", mx, "length");
107 for (std::size_t i = 0; i < 3; ++i) {
108 require(mx[i] > mn[i], "set_domain", component("max", i),
109 "> min[" + std::to_string(i) + "] = " + num(mn[i]) + " (length)", mx[i]);
110 }
111}
112
113inline void plane(const F3Arg& p, const F3Arg& n) {
114 finiteEach("add_plane", "point", p, "length");
115 finiteEach("add_plane", "normal", n, "dimensionless");
116 require(n[0] != 0.0f || n[1] != 0.0f || n[2] != 0.0f, "add_plane", "normal", "a non-zero vector",
117 0.0);
118}
119
120inline void mpiBox(const std::tuple<double, double, double>& origin,
121 const std::tuple<double, double, double>& extent,
122 const std::tuple<long, long, long>& cells) {
123 const auto [ox, oy, oz] = origin;
124 const auto [ex, ey, ez] = extent;
125 const auto [cx, cy, cz] = cells;
126 finiteEach("init_mpi", "origin", std::array<double, 3>{ox, oy, oz}, "length");
127 positiveEach("init_mpi", "extent", std::array<double, 3>{ex, ey, ez}, "length");
128 const std::array<std::int64_t, 3> c{cx, cy, cz};
129 for (std::size_t i = 0; i < 3; ++i) {
130 count("init_mpi", component("cells", i), c[i], 1);
131 }
132}
133
135inline void materialIds(const std::vector<int>& ids) {
136 for (std::size_t i = 0; i < ids.size(); ++i) {
137 require(ids[i] >= 0 && ids[i] <= kMaxMaterialId, "set_material_ids", component("ids", i),
138 "an integer in [0, " + std::to_string(kMaxMaterialId) + "]", ids[i]);
139 }
140}
141
142} // namespace peclet::dem::binding_checks
void mpiBox(const std::tuple< double, double, double > &origin, const std::tuple< double, double, double > &extent, const std::tuple< long, long, long > &cells)
init_mpi's global box: finite origin, positive extent, at least one ORB cell per axis.
void materialIds(const std::vector< int > &ids)
void nonnegEach(const char *who, const char *name, const std::array< T, N > &a, const char *dim)
std::string component(const char *name, std::size_t i)
Per-component forms for a triple (or a quaternion): the parameter is named name[i].
void sampleCounts(const char *who, int rx, int ry, int rz)
A reconstruction grid's samples per axis (get_sdf_grid / export_sdf): each >= 1.
void finiteEach(const char *who, const char *name, const std::array< T, N > &a, const char *dim)
void restitutionCoef(const char *who, const char *name, double v)
A restitution coefficient: [0, 1], dimensionless.
void positive(const char *who, const std::string &name, double v, const char *dim)
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::array< T, N > &a, const char *dim)
void poissonRatio(const char *who, const char *name, double v)
An isotropic solid's Poisson ratio: (-1, 0.5], dimensionless.
void finite(const char *who, const std::string &name, double v, const char *dim)
void nonneg(const char *who, const std::string &name, double v, const char *dim)
std::array< float, 3 > F3Arg
A triple: any sequence of three floats (tuple, list, (3,) array).
void domainCorners(const F3Arg &mn, const F3Arg &mx)
set_domain(min, max): finite corners, max > min on every axis.
void plane(const F3Arg &p, const F3Arg &n)
add_plane: a finite point and a finite, non-zero normal.
constexpr int kMaxMaterialId
Material ids index the 8-entry pair tables (narrowphase.hpp kMaxMaterials).
void count(const char *who, const std::string &name, std::int64_t v, std::int64_t lo)
A count (capacity, iterations, samples per axis): an integer >= lo.
void sdfShapeArgs(const char *who, const F3Arg &origin, const F3Arg &spacing, const F3Arg &inv_inertia, float bounding_radius)
The shared arguments of set_sdf_shape / add_sdf_shape.
constexpr int kMaxMaterials
Pair-material lookup: flat [K*K*2] table, entry ((a*K + b)*2) = restitution, +1 = friction.
dem — portable (Kokkos) narrow-phase: SDF point-shell collision + boundary planes.
static long N[64]
Definition pmpi.cpp:4