Skip to content

Commit 35dca81

Browse files
added bindings for greedy module (#21)
* added bindings for greedy module * Update Greedy bindings and add tests * Update bindings and test for getNearestROM --------- Co-authored-by: Cole Kendrick <[email protected]>
1 parent 78e9756 commit 35dca81

File tree

7 files changed

+472
-0
lines changed

7 files changed

+472
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ pybind11_add_module(_pylibROM
145145
bindings/pylibROM/algo/pyParametricDMD.cpp
146146
bindings/pylibROM/algo/pyNonuniformDMD.cpp
147147
bindings/pylibROM/algo/pyAdaptiveDMD.cpp
148+
bindings/pylibROM/algo/greedy/pyGreedySampler.cpp
149+
bindings/pylibROM/algo/greedy/pyGreedyCustomSampler.cpp
150+
bindings/pylibROM/algo/greedy/pyGreedyRandomSampler.cpp
148151
bindings/pylibROM/algo/manifold_interp/pyInterpolator.cpp
149152
bindings/pylibROM/algo/manifold_interp/pyMatrixInterpolator.cpp
150153
bindings/pylibROM/algo/manifold_interp/pyVectorInterpolator.cpp
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# To add pure python routines to this module,
2+
# either define/import the python routine in this file.
3+
# This will combine both c++ bindings/pure python routines into this module.
4+
5+
# For other c++ binding modules, change the module name accordingly.
6+
from _pylibROM.algo.greedy import *
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <pybind11/pybind11.h>
2+
#include <pybind11/numpy.h>
3+
#include <pybind11/operators.h>
4+
#include <pybind11/stl.h>
5+
#include "algo/greedy/GreedyCustomSampler.h"
6+
#include "linalg/Vector.h"
7+
8+
namespace py = pybind11;
9+
using namespace CAROM;
10+
using namespace std;
11+
12+
void init_GreedyCustomSampler(pybind11::module_ &m) {
13+
py::class_<GreedyCustomSampler, GreedySampler>(m, "GreedyCustomSampler")
14+
.def(py::init<std::vector<CAROM::Vector>, bool, double, double, double, int, int, std::string, std::string, bool, int, bool>(),
15+
py::arg("parameter_points"),
16+
py::arg("check_local_rom"),
17+
py::arg("relative_error_tolerance"),
18+
py::arg("alpha"),
19+
py::arg("max_clamp"),
20+
py::arg("subset_size"),
21+
py::arg("convergence_subset_size"),
22+
py::arg("output_log_path") = "",
23+
py::arg("warm_start_file_name") = "",
24+
py::arg("use_centroid") = true,
25+
py::arg("random_seed") = 1,
26+
py::arg("debug_algorithm") = false)
27+
.def(py::init<std::vector<double>, bool, double, double, double, int, int, std::string, std::string, bool, int, bool>(),
28+
py::arg("parameter_points"),
29+
py::arg("check_local_rom"),
30+
py::arg("relative_error_tolerance"),
31+
py::arg("alpha"),
32+
py::arg("max_clamp"),
33+
py::arg("subset_size"),
34+
py::arg("convergence_subset_size"),
35+
py::arg("output_log_path") = "",
36+
py::arg("warm_start_file_name") = "",
37+
py::arg("use_centroid") = true,
38+
py::arg("random_seed") = 1,
39+
py::arg("debug_algorithm") = false)
40+
.def(py::init<std::string, std::string>(),
41+
py::arg("base_file_name"),
42+
py::arg("output_log_path") = "")
43+
.def("__del__", [](GreedyCustomSampler& self){ self.~GreedyCustomSampler(); });
44+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <pybind11/pybind11.h>
2+
#include <pybind11/numpy.h>
3+
#include <pybind11/operators.h>
4+
#include <pybind11/stl.h>
5+
#include "algo/greedy/GreedyRandomSampler.h"
6+
#include "linalg/Vector.h"
7+
8+
namespace py = pybind11;
9+
using namespace CAROM;
10+
using namespace std;
11+
12+
void init_GreedyRandomSampler(py::module &m) {
13+
py::class_<GreedyRandomSampler, GreedySampler>(m, "GreedyRandomSampler")
14+
.def(py::init<CAROM::Vector, CAROM::Vector, int, bool, double, double,double, int, int, bool, std::string, std::string, bool, int, bool>(),
15+
py::arg("param_space_min"),
16+
py::arg("param_space_max"),
17+
py::arg("num_parameter_points"),
18+
py::arg("check_local_rom"),
19+
py::arg("relative_error_tolerance"),
20+
py::arg("alpha"),
21+
py::arg("max_clamp"),
22+
py::arg("subset_size"),
23+
py::arg("convergence_subset_size"),
24+
py::arg("use_latin_hypercube"),
25+
py::arg("output_log_path") = "",
26+
py::arg("warm_start_file_name") = "",
27+
py::arg("use_centroid") = true,
28+
py::arg("random_seed") = 1,
29+
py::arg("debug_algorithm") = false
30+
)
31+
.def(py::init<double, double, int, bool, double, double,double, int, int, bool, std::string, std::string, bool, int, bool>(),
32+
py::arg("param_space_min"),
33+
py::arg("param_space_max"),
34+
py::arg("num_parameter_points"),
35+
py::arg("check_local_rom"),
36+
py::arg("relative_error_tolerance"),
37+
py::arg("alpha"),
38+
py::arg("max_clamp"),
39+
py::arg("subset_size"),
40+
py::arg("convergence_subset_size"),
41+
py::arg("use_latin_hypercube"),
42+
py::arg("output_log_path") = "",
43+
py::arg("warm_start_file_name") = "",
44+
py::arg("use_centroid") = true,
45+
py::arg("random_seed") = 1,
46+
py::arg("debug_algorithm") = false
47+
)
48+
.def(py::init<std::string, std::string>(),
49+
py::arg("base_file_name"),
50+
py::arg("output_log_path") = ""
51+
)
52+
.def("save", &GreedyRandomSampler::save, py::arg("base_file_name"))
53+
.def("__del__", [](GreedyRandomSampler& self){ self.~GreedyRandomSampler(); });
54+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#include <pybind11/pybind11.h>
2+
#include <pybind11/numpy.h>
3+
#include <pybind11/operators.h>
4+
#include <pybind11/stl.h>
5+
#include "algo/greedy/GreedySampler.h"
6+
#include "linalg/Vector.h"
7+
8+
namespace py = pybind11;
9+
using namespace CAROM;
10+
using namespace std;
11+
12+
13+
class PyGreedySampler : public GreedySampler {
14+
public:
15+
using GreedySampler::GreedySampler;
16+
17+
void save(std::string base_file_name) override {
18+
PYBIND11_OVERRIDE(void,GreedySampler,save,base_file_name );
19+
}
20+
protected:
21+
void constructParameterPoints() override {
22+
PYBIND11_OVERRIDE_PURE(void, GreedySampler, constructParameterPoints,);
23+
}
24+
void getNextParameterPointAfterConvergenceFailure() override {
25+
PYBIND11_OVERRIDE_PURE(void, GreedySampler, getNextParameterPointAfterConvergenceFailure,);
26+
}
27+
};
28+
29+
void init_GreedySampler(pybind11::module_ &m) {
30+
py::class_<GreedyErrorIndicatorPoint>(m, "GreedyErrorIndicatorPoint")
31+
.def_property_readonly("point", [](GreedyErrorIndicatorPoint &self) {
32+
return self.point.get();
33+
})
34+
.def_property_readonly("localROM", [](GreedyErrorIndicatorPoint &self) {
35+
return self.localROM.get();
36+
});
37+
38+
py::class_<GreedySampler,PyGreedySampler>(m, "GreedySampler")
39+
.def(py::init<std::vector<Vector>, bool, double, double, double, int, int, std::string, std::string, bool, int, bool>(),
40+
py::arg("parameter_points"),
41+
py::arg("check_local_rom"),
42+
py::arg("relative_error_tolerance"),
43+
py::arg("alpha"),
44+
py::arg("max_clamp"),
45+
py::arg("subset_size"),
46+
py::arg("convergence_subset_size"),
47+
py::arg("output_log_path") = "",
48+
py::arg("warm_start_file_name") = "",
49+
py::arg("use_centroid") = true,
50+
py::arg("random_seed") = 1,
51+
py::arg("debug_algorithm") = false)
52+
.def(py::init<std::vector<double>,bool, double, double, double, int, int, std::string, std::string, bool, int, bool>(),
53+
py::arg("parameter_points"),
54+
py::arg("check_local_rom"),
55+
py::arg("relative_error_tolerance"),
56+
py::arg("alpha"),
57+
py::arg("max_clamp"),
58+
py::arg("subset_size"),
59+
py::arg("convergence_subset_size"),
60+
py::arg("output_log_path") = "",
61+
py::arg("warm_start_file_name") = "",
62+
py::arg("use_centroid") = true,
63+
py::arg("random_seed") = 1,
64+
py::arg("debug_algorithm") = false)
65+
.def(py::init<Vector, Vector, int, bool, double, double, double, int, int,std::string, std::string, bool, int, bool>(),
66+
py::arg("param_space_min"), py::arg("param_space_max"), py::arg("num_parameter_points"),
67+
py::arg("check_local_rom"), py::arg("relative_error_tolerance"), py::arg("alpha"),
68+
py::arg("max_clamp"), py::arg("subset_size"), py::arg("convergence_subset_size"),
69+
py::arg("output_log_path") = "", py::arg("warm_start_file_name") = "",
70+
py::arg("use_centroid") = true, py::arg("random_seed") = 1,
71+
py::arg("debug_algorithm") = false
72+
)
73+
.def(py::init<double, double, int, bool, double, double, double, int, int,std::string, std::string, bool, int, bool>(),
74+
py::arg("param_space_min"), py::arg("param_space_max"), py::arg("num_parameter_points"),
75+
py::arg("check_local_rom"), py::arg("relative_error_tolerance"), py::arg("alpha"),
76+
py::arg("max_clamp"), py::arg("subset_size"), py::arg("convergence_subset_size"),
77+
py::arg("output_log_path") = "", py::arg("warm_start_file_name") = "",
78+
py::arg("use_centroid") = true, py::arg("random_seed") = 1,
79+
py::arg("debug_algorithm") = false
80+
)
81+
.def(py::init<std::string, std::string>(), py::arg("base_file_name"), py::arg("output_log_path") = "")
82+
.def("getNextParameterPoint", [](GreedySampler& self) -> std::unique_ptr<Vector> {
83+
std::shared_ptr<Vector> result = self.getNextParameterPoint();
84+
return std::make_unique<Vector>(*(result.get()));
85+
})
86+
.def("getNextPointRequiringRelativeError", [](GreedySampler& self) -> GreedyErrorIndicatorPoint {
87+
// Create a deepcopy of the struct, otherwise it will get freed twice
88+
GreedyErrorIndicatorPoint point = self.getNextPointRequiringRelativeError();
89+
Vector *t_pt = nullptr;
90+
Vector *t_lROM = nullptr;
91+
92+
if (point.point)
93+
{
94+
t_pt = new Vector(*(point.point));
95+
}
96+
97+
if (point.localROM)
98+
{
99+
t_lROM = new Vector(*(point.localROM));
100+
}
101+
102+
return createGreedyErrorIndicatorPoint(t_pt, t_lROM);
103+
}, py::return_value_policy::reference)
104+
.def("getNextPointRequiringErrorIndicator", [](GreedySampler& self) -> GreedyErrorIndicatorPoint {
105+
// Create a deepcopy of the struct, otherwise it will get freed twice
106+
GreedyErrorIndicatorPoint point = self.getNextPointRequiringErrorIndicator();
107+
108+
Vector *t_pt = nullptr;
109+
Vector *t_lROM = nullptr;
110+
111+
if (point.point)
112+
{
113+
t_pt = new Vector(*(point.point));
114+
}
115+
116+
if (point.localROM)
117+
{
118+
t_lROM = new Vector(*(point.localROM));
119+
}
120+
121+
return createGreedyErrorIndicatorPoint(t_pt, t_lROM);
122+
}, py::return_value_policy::reference)
123+
.def("setPointRelativeError", (void (GreedySampler::*) (double))&GreedySampler::setPointRelativeError)
124+
.def("setPointErrorIndicator", (void (GreedySampler::*) (double,int)) &GreedySampler::setPointErrorIndicator)
125+
.def("getNearestNonSampledPoint", (int (GreedySampler::*) (CAROM::Vector)) &GreedySampler::getNearestNonSampledPoint)
126+
.def("getNearestROM", [](GreedySampler& self, Vector point) -> std::unique_ptr<Vector> {
127+
std::shared_ptr<Vector> result = self.getNearestROM(point);
128+
if (!result)
129+
{
130+
return nullptr;
131+
}
132+
return std::make_unique<Vector>(*(result.get()));
133+
})
134+
.def("getParameterPointDomain", &GreedySampler::getParameterPointDomain)
135+
.def("getSampledParameterPoints", &GreedySampler::getSampledParameterPoints)
136+
.def("save", &GreedySampler::save)
137+
.def("__del__", [](GreedySampler& self){ self.~GreedySampler(); })
138+
.def("isComplete", &GreedySampler::isComplete);
139+
140+
m.def("createGreedyErrorIndicatorPoint", [](Vector* point, Vector* localROM) {
141+
return createGreedyErrorIndicatorPoint(point, localROM);
142+
});
143+
m.def("createGreedyErrorIndicatorPoint", [](Vector* point, std::shared_ptr<Vector>& localROM) {
144+
return createGreedyErrorIndicatorPoint(point, localROM);
145+
});
146+
m.def("getNearestPoint", [](std::vector<Vector>& paramPoints,Vector point) {
147+
return getNearestPoint(paramPoints, point);
148+
});
149+
m.def("getNearestPoint", [](std::vector<double>& paramPoints, double point) {
150+
return getNearestPoint(paramPoints, point);
151+
});
152+
m.def("getNearestPointIndex", [](std::vector<Vector> paramPoints, Vector point) {
153+
return getNearestPointIndex(paramPoints, point);
154+
});
155+
m.def("getNearestPointIndex", [](std::vector<double> paramPoints, double point) {
156+
return getNearestPointIndex(paramPoints, point);
157+
});
158+
}

bindings/pylibROM/pylibROM.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ void init_Interpolator(pybind11::module_ &);
2828
void init_VectorInterpolator(pybind11::module_ &);
2929
void init_MatrixInterpolator(pybind11::module_ &);
3030

31+
//algo/greedy
32+
void init_GreedySampler(pybind11::module_ &m);
33+
void init_GreedyCustomSampler(pybind11::module_ &m);
34+
void init_GreedyRandomSampler(pybind11::module_ &m);
35+
3136
//hyperreduction
3237
void init_DEIM(pybind11::module_ &m);
3338
void init_GNAT(pybind11::module_ &m);
@@ -79,6 +84,11 @@ PYBIND11_MODULE(_pylibROM, m) {
7984
init_VectorInterpolator(manifold_interp);
8085
init_MatrixInterpolator(manifold_interp);
8186

87+
py::module greedy = algo.def_submodule("greedy");
88+
init_GreedySampler(greedy);
89+
init_GreedyCustomSampler(greedy);
90+
init_GreedyRandomSampler(greedy);
91+
8292
py::module hyperreduction = m.def_submodule("hyperreduction");
8393
init_DEIM(hyperreduction);
8494
init_GNAT(hyperreduction);

0 commit comments

Comments
 (0)