Skip to content

Commit

Permalink
dmd/euler example (#12)
Browse files Browse the repository at this point in the history
* bindings for AdaptiveDMD and NonuniformDMD.

* added ci test for DMD class.

* parallel test for DMD.

* fix on parallel dmd test.

* initial commit

* initial commit

* dg_euler.py in progress...

* finished dg_euler testing example

* minor changes and added mesh file

* fom timer corrected

* minor corrections to dg_euler.py

* corrections to dg_euler.py and pyAdaptiveDMD.cpp

---------

Co-authored-by: Kevin Chung <[email protected]>
Co-authored-by: Henry Yu <[email protected]>
Co-authored-by: Henry Yu <[email protected]>
Co-authored-by: Henry Yu <[email protected]>
  • Loading branch information
5 people authored Sep 5, 2023
1 parent 4a8b2bc commit 72c8cdf
Show file tree
Hide file tree
Showing 11 changed files with 1,192 additions and 302 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ jobs:
pytest test_pyBasisReader.py --verbose
echo run pyBasisWriter unit test
pytest test_pyBasisWriter.py --verbose
echo run pyDMD unit test
pytest test_pyDMD.py --verbose
mpirun -n 2 pytest test_pyDMD.py --verbose
echo run pyDEIM unit test
pytest test_pyDEIM.py --verbose
echo run pyGNAT unit test
Expand Down Expand Up @@ -127,6 +130,9 @@ jobs:
pytest test_pyBasisReader.py --verbose
echo run pyBasisWriter unit test
pytest test_pyBasisWriter.py --verbose
echo run pyDMD unit test
pytest test_pyDMD.py --verbose
mpirun -n 2 pytest test_pyDMD.py --verbose
echo run pyDEIM unit test
pytest test_pyDEIM.py --verbose
echo run pyGNAT unit test
Expand Down Expand Up @@ -188,6 +194,9 @@ jobs:
pytest test_pyBasisReader.py --verbose
echo run pyBasisWriter unit test
pytest test_pyBasisWriter.py --verbose
echo run pyDMD unit test
pytest test_pyDMD.py --verbose
mpirun -n 2 pytest test_pyDMD.py --verbose
echo run pyDEIM unit test
pytest test_pyDEIM.py --verbose
echo run pyGNAT unit test
Expand Down
25 changes: 13 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,23 @@ link_libraries(
add_subdirectory("extern/pybind11")

pybind11_add_module(_pylibROM
bindings/pylibROM/pylibROM.cpp
bindings/pylibROM/pylibROM.cpp

bindings/pylibROM/linalg/pyMatrix.cpp
bindings/pylibROM/linalg/pyVector.cpp
bindings/pylibROM/linalg/pyBasisGenerator.cpp
bindings/pylibROM/linalg/pyBasisReader.cpp
bindings/pylibROM/linalg/pyMatrix.cpp
bindings/pylibROM/linalg/pyVector.cpp
bindings/pylibROM/linalg/pyBasisGenerator.cpp
bindings/pylibROM/linalg/pyBasisReader.cpp
bindings/pylibROM/linalg/pyBasisWriter.cpp
bindings/pylibROM/linalg/pyOptions.cpp
bindings/pylibROM/linalg/pyNNLS.cpp
bindings/pylibROM/linalg/svd/pySVD.cpp
bindings/pylibROM/linalg/svd/pyStaticSVD.cpp
bindings/pylibROM/linalg/svd/pyIncrementalSVD.cpp
bindings/pylibROM/linalg/pyOptions.cpp
bindings/pylibROM/linalg/pyNNLS.cpp
bindings/pylibROM/linalg/svd/pySVD.cpp
bindings/pylibROM/linalg/svd/pyStaticSVD.cpp
bindings/pylibROM/linalg/svd/pyIncrementalSVD.cpp

bindings/pylibROM/algo/pyDMD.cpp
bindings/pylibROM/algo/pyDMD.cpp
bindings/pylibROM/algo/pyParametricDMD.cpp

bindings/pylibROM/algo/pyNonuniformDMD.cpp
bindings/pylibROM/algo/pyAdaptiveDMD.cpp
bindings/pylibROM/hyperreduction/pyDEIM.cpp
bindings/pylibROM/hyperreduction/pyGNAT.cpp
bindings/pylibROM/hyperreduction/pyQDEIM.cpp
Expand Down
32 changes: 32 additions & 0 deletions bindings/pylibROM/algo/pyAdaptiveDMD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "algo/AdaptiveDMD.h"
#include "linalg/Vector.h"
#include "linalg/Matrix.h"

namespace py = pybind11;
using namespace CAROM;

void init_AdaptiveDMD(pybind11::module_ &m){

py::class_<AdaptiveDMD, DMD>(m, "AdaptiveDMD")
.def(py::init<int, double, std::string, std::string, double, bool, Vector*>(),
py::arg("dim"),
py::arg("desired_dt") = -1.0,
py::arg("rbf") = "G",
py::arg("interp_method") = "LS",
py::arg("closest_rbf_val") = 0.9,
py::arg("alt_output_basis") = false,
py::arg("state_offset") = nullptr)
.def("train", (void (AdaptiveDMD::*)(double, const Matrix*, double)) &AdaptiveDMD::train,
py::arg("energy_fraction").noconvert(),
py::arg("W0") = nullptr,
py::arg("linearity_tol") = 0.0)
.def("train", (void (AdaptiveDMD::*)(int, const Matrix*, double)) &AdaptiveDMD::train,
py::arg("k").noconvert(),
py::arg("W0") = nullptr,
py::arg("linearity_tol") = 0.0)
.def("getTrueDt", &AdaptiveDMD::getTrueDt)
.def("getInterpolatedSnapshots", &AdaptiveDMD::getInterpolatedSnapshots);
}

2 changes: 1 addition & 1 deletion bindings/pylibROM/algo/pyDMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void init_DMD(pybind11::module_ &m) {
return new DMD(dim, dt, alt_output_basis, vec);
}), py::arg("dim"), py::arg("dt"), py::arg("alt_output_basis") = false, py::arg("vec") = nullptr)

// .def("setOffset", &PyDMD::setOffset, py::arg("offset_vector"), py::arg("order")) //problem if we want to name the wrapper as DMD. Could get rid of the using namespace directive?
.def("setOffset", &DMD::setOffset, py::arg("offset_vector"), py::arg("order"))
.def("takeSample", [](DMD &self, py::array_t<double> &u_in, double t) {
self.takeSample(getVectorPointer(u_in), t);
})
Expand Down
40 changes: 40 additions & 0 deletions bindings/pylibROM/algo/pyNonuniformDMD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Created by barrow9 on 6/4/23.
//
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>
#include "librom.h"
#include "python_utils/cpp_utils.hpp"

namespace py = pybind11;
using namespace CAROM;

void init_NonuniformDMD(pybind11::module_ &m) {

py::class_<NonuniformDMD, DMD>(m, "NonuniformDMD")

//constructor, default.
.def(py::init<std::string>()) //constructor a

.def(py::init([](int dim,
bool alt_output_basis,
Vector* state_offset,
Vector* derivative_offset) {
return new NonuniformDMD(dim, alt_output_basis, state_offset, derivative_offset);
}),
py::arg("dim"),
py::arg("alt_output_basis") = false,
py::arg("state_offset") = nullptr,
py::arg("derivative_offset") = nullptr)

//TODO: needed explicitly?
.def("__del__", [](NonuniformDMD& self) { self.~NonuniformDMD(); }) // Destructor

.def("setOffset", &NonuniformDMD::setOffset, py::arg("offset_vector"), py::arg("order"))

.def("load", &NonuniformDMD::load, py::arg("base_file_name"))
.def("save", &NonuniformDMD::save, py::arg("base_file_name"));

}
4 changes: 4 additions & 0 deletions bindings/pylibROM/pylibROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ void init_IncrementalSVD(pybind11::module_ &m);
//algo
void init_DMD(pybind11::module_ &);
void init_ParametricDMD(pybind11::module_ &m);
void init_NonuniformDMD(pybind11::module_ &m);
void init_AdaptiveDMD(pybind11::module_ &m);

//hyperreduction
void init_DEIM(pybind11::module_ &m);
Expand Down Expand Up @@ -64,6 +66,8 @@ PYBIND11_MODULE(_pylibROM, m) {
py::module algo = m.def_submodule("algo");
init_DMD(algo);
init_ParametricDMD(algo);
init_AdaptiveDMD(algo);
init_NonuniformDMD(algo);

py::module hyperreduction = m.def_submodule("hyperreduction");
init_DEIM(hyperreduction);
Expand Down
85 changes: 85 additions & 0 deletions examples/data/periodic-square.mesh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
MFEM mesh v1.0

#
# MFEM Geometry Types (see mesh/geom.hpp):
#
# POINT = 0
# SEGMENT = 1
# TRIANGLE = 2
# SQUARE = 3
# TETRAHEDRON = 4
# CUBE = 5
#

dimension
2

# format: <attribute> <geometry type> <vertex 0> <vertex 1> ...
elements
9
1 3 0 1 4 3
2 3 1 2 5 4
3 3 2 0 3 5
4 3 3 4 7 6
5 3 4 5 8 7
6 3 5 3 6 8
7 3 6 7 1 0
8 3 7 8 2 1
9 3 8 6 0 2

boundary
0

vertices
9

nodes
FiniteElementSpace
FiniteElementCollection: L2_T1_2D_P1
VDim: 2
Ordering: 1

-1 -1
-0.333333333 -1
-1 -0.333333333
-0.333333333 -0.333333333

-0.333333333 -1
+0.333333333 -1
-0.333333333 -0.333333333
+0.333333333 -0.333333333

+0.333333333 -1
+1 -1
+0.333333333 -0.333333333
+1 -0.333333333

-1 -0.333333333
-0.333333333 -0.333333333
-1 +0.333333333
-0.333333333 +0.333333333

-0.333333333 -0.333333333
+0.333333333 -0.333333333
-0.333333333 +0.333333333
+0.333333333 +0.333333333

+0.333333333 -0.333333333
+1 -0.333333333
+0.333333333 +0.333333333
+1 +0.333333333

-1 +0.333333333
-0.333333333 +0.333333333
-1 +1
-0.333333333 +1

-0.333333333 +0.333333333
+0.333333333 +0.333333333
-0.333333333 +1
+0.333333333 +1

+0.333333333 +0.333333333
+1 +0.333333333
+0.333333333 +1
+1 +1
Loading

0 comments on commit 72c8cdf

Please sign in to comment.