Skip to content

Commit 58cb15b

Browse files
committed
Expose quadrature weight transfer to Python
1 parent 8168d04 commit 58cb15b

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

bindings/pypbat/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ target_sources(PhysicsBasedAnimationToolkit_Python
33
FILE_SET api
44
FILES
55
"Math.h"
6+
"MomentFitting.h"
67
)
78

89
target_sources(PhysicsBasedAnimationToolkit_Python
910
PRIVATE
1011
"Math.cpp"
12+
"MomentFitting.cpp"
1113
)
1214

1315
add_subdirectory(linalg)

bindings/pypbat/math/Math.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Math.h"
22

3+
#include "MomentFitting.h"
34
#include "linalg/LinAlg.h"
45

56
#include <string>
@@ -10,7 +11,7 @@ namespace math {
1011

1112
void Bind(pybind11::module& m)
1213
{
13-
namespace pyb = pybind11;
14+
BindMomentFitting(m);
1415
auto mlinalg = m.def_submodule("linalg");
1516
linalg::Bind(mlinalg);
1617
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "MomentFitting.h"
2+
3+
#include <exception>
4+
#include <pbat/common/ConstexprFor.h>
5+
#include <pbat/math/MomentFitting.h>
6+
#include <pybind11/eigen.h>
7+
8+
namespace pbat {
9+
namespace py {
10+
namespace math {
11+
12+
void BindMomentFitting(pybind11::module& m)
13+
{
14+
namespace pyb = pybind11;
15+
m.def(
16+
"transfer_quadrature",
17+
[](Eigen::Ref<IndexVectorX const> const& S1,
18+
Eigen::Ref<MatrixX const> const& X1,
19+
Eigen::Ref<IndexVectorX const> const& S2,
20+
Eigen::Ref<MatrixX const> const& X2,
21+
Eigen::Ref<VectorX const> const& w2,
22+
Index order,
23+
bool bEvaluateError,
24+
Index maxIterations,
25+
Scalar precision) {
26+
VectorX w1, err;
27+
common::ForRange<1, 4>([&]<auto Order>() {
28+
if (order == Order)
29+
{
30+
std::tie(w1, err) = pbat::math::TransferQuadrature<
31+
Order>(S1, X1, S2, X2, w2, bEvaluateError, maxIterations, precision);
32+
}
33+
});
34+
if (w1.size() == 0)
35+
throw std::invalid_argument("transfer_quadrature only accepts 1 <= order <= 4.");
36+
return std::make_pair(w1, err);
37+
},
38+
pyb::arg("S1"),
39+
pyb::arg("X1"),
40+
pyb::arg("S2"),
41+
pyb::arg("X2"),
42+
pyb::arg("w2"),
43+
pyb::arg("order") = 1,
44+
pyb::arg("with_error") = false,
45+
pyb::arg("max_iters") = 20,
46+
pyb::arg("precision") = std::numeric_limits<Scalar>::epsilon(),
47+
"Obtain weights w1 by transferring an existing quadrature rule (X2,w2) "
48+
"defined on a domain composed of simplices onto a new quadrature rule "
49+
"(X1,w1) defined on the same domain, given fixed quadrature points X1. "
50+
"S1 is a |X1.shape[1]| array of simplex indices associated with the "
51+
"corresponding quadrature point in columns (i.e. the quadrature points) "
52+
"of X1. S2 is the same for columns of X2. w2 are the quadrature weights "
53+
"associated with X2. If with_error==True, the polynomial integration error "
54+
"is computed and returned.");
55+
}
56+
57+
} // namespace math
58+
} // namespace py
59+
} // namespace pbat
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef PYPBAT_MATH_MOMENT_FITTING_H
2+
#define PYPBAT_MATH_MOMENT_FITTING_H
3+
4+
#include <pybind11/pybind11.h>
5+
6+
namespace pbat {
7+
namespace py {
8+
namespace math {
9+
10+
void BindMomentFitting(pybind11::module& m);
11+
12+
} // namespace math
13+
} // namespace py
14+
} // namespace pbat
15+
16+
#endif // PYPBAT_MATH_MOMENT_FITTING_H

python/pbatoolkit/math/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from .._pbat import math as _math
2+
import sys
3+
import inspect
4+
5+
__module = sys.modules[__name__]
6+
for _name, _attr in inspect.getmembers(_math):
7+
if not _name.startswith("__"):
8+
setattr(__module, _name, _attr)

0 commit comments

Comments
 (0)