MUQ  0.4.3
PolynomialChaosWrapper.cpp
Go to the documentation of this file.
1 #include "AllClassWrappers.h"
2 
7 
9 
10 #include "MUQ/Modeling/ModPiece.h"
11 
12 #include <pybind11/pybind11.h>
13 #include <pybind11/stl.h>
14 #include <pybind11/eigen.h>
15 
16 #include <string>
17 
18 #include <functional>
19 #include <vector>
20 
22 using namespace muq::Modeling;
23 using namespace muq::Utilities;
24 using namespace muq::Approximation;
25 
26 namespace py = pybind11;
27 
29 {
30 
31  py::class_<PolynomialChaosExpansion, BasisExpansion, std::shared_ptr<PolynomialChaosExpansion>> pce(m,"PolynomialChaosExpansion");
32  pce
33  .def(py::init<std::shared_ptr<OrthogonalPolynomial> const&, std::shared_ptr<muq::Utilities::MultiIndexSet> const&, Eigen::MatrixXd const&>())
34  .def(py::init<std::shared_ptr<OrthogonalPolynomial> const&, std::shared_ptr<muq::Utilities::MultiIndexSet> const&, unsigned int >())
35  .def(py::init<std::vector<std::shared_ptr<IndexedScalarBasis>> const&, std::shared_ptr<muq::Utilities::MultiIndexSet> const&, Eigen::MatrixXd const&>())
36  .def(py::init<std::vector<std::shared_ptr<IndexedScalarBasis>> const&, std::shared_ptr<muq::Utilities::MultiIndexSet> const&, unsigned int>())
37  .def("Variance", &PolynomialChaosExpansion::Variance)
38  .def("Covariance", &PolynomialChaosExpansion::Covariance)
39  .def("Mean", &PolynomialChaosExpansion::Mean)
40  .def("Magnitude", &PolynomialChaosExpansion::Magnitude)
41  .def("TotalSensitivity", (Eigen::VectorXd (PolynomialChaosExpansion::*)(unsigned int) const) &PolynomialChaosExpansion::TotalSensitivity)
42  .def("TotalSensitivity", (Eigen::MatrixXd (PolynomialChaosExpansion::*)() const) &PolynomialChaosExpansion::TotalSensitivity)
43  .def("SobolSensitivity", (Eigen::VectorXd (PolynomialChaosExpansion::*)(unsigned int) const) &PolynomialChaosExpansion::SobolSensitivity)
44  .def("SobolSensitivity", (Eigen::VectorXd (PolynomialChaosExpansion::*)(std::vector<unsigned int> const&) const) &PolynomialChaosExpansion::SobolSensitivity)
45  .def("MainSensitivity", &PolynomialChaosExpansion::MainSensitivity)
46  .def("ToHDF5", (void (PolynomialChaosExpansion::*)(std::string, std::string) const) &PolynomialChaosExpansion::ToHDF5, py::arg("filename"),py::arg("groupName")="/")
47  .def_static("FromHDF5", [](std::string filename, std::string dsetName){return PolynomialChaosExpansion::FromHDF5(filename, dsetName);})
48  .def_static("FromHDF5", [](std::string filename){return PolynomialChaosExpansion::FromHDF5(filename, "/");});
49 
50  py::class_<PCEFactory, std::shared_ptr<PCEFactory>> pceFactory(m, "PCEFactory");
51  pceFactory
52  .def(py::init<std::vector<std::shared_ptr<Quadrature>> const&, std::vector<std::shared_ptr<IndexedScalarBasis>> const&>())
53  .def(py::init<std::vector<std::shared_ptr<Quadrature>> const&, std::shared_ptr<muq::Utilities::MultiIndex> const&,std::vector<std::shared_ptr<IndexedScalarBasis>> const&>())
54  .def(py::init<std::vector<std::shared_ptr<Quadrature>> const&, std::shared_ptr<muq::Utilities::MultiIndex> const&, std::vector<std::shared_ptr<IndexedScalarBasis>> const&,std::shared_ptr<muq::Utilities::MultiIndexSet> const&>())
55  .def("Compute", (std::shared_ptr<PolynomialChaosExpansion>(PCEFactory::*)(std::vector<Eigen::VectorXd> const&, std::shared_ptr<MultiIndex> const&) ) &PCEFactory::Compute)
56  .def("Compute", (std::shared_ptr<PolynomialChaosExpansion>(PCEFactory::*)(std::shared_ptr<ModPiece> const&) ) &PCEFactory::Compute)
57  .def("QuadPts", (std::vector<Eigen::VectorXd>const&(PCEFactory::*)() const) &PCEFactory::QuadPts)
58  .def("QuadPts", (std::vector<Eigen::VectorXd>const&(PCEFactory::*)(std::shared_ptr<MultiIndex> const&)) &PCEFactory::QuadPts);
59 
60 
61  py::class_<AdaptiveSmolyakPCE, std::shared_ptr<AdaptiveSmolyakPCE>> adaptSmoly(m,"AdaptiveSmolyakPCE");
62  adaptSmoly
63  .def(py::init<std::shared_ptr<muq::Modeling::ModPiece>, std::vector<std::shared_ptr<Quadrature>>,std::vector<std::shared_ptr<IndexedScalarBasis>>>())
64  .def("Compute", [](AdaptiveSmolyakPCE & self,
65  std::shared_ptr<MultiIndexSet> const& fixedSet,
66  py::dict d) {return self.Compute(fixedSet, ConvertDictToPtree(d));})
67  .def("Adapt", [](AdaptiveSmolyakPCE & self, py::dict d) {return self.Adapt(ConvertDictToPtree(d));})
68  .def("Error", &SmolyakEstimator<std::shared_ptr<PolynomialChaosExpansion>>::Error)
69  .def("NumEvals", &SmolyakEstimator<std::shared_ptr<PolynomialChaosExpansion>>::NumEvals)
70  .def("ErrorHistory", &SmolyakEstimator<std::shared_ptr<PolynomialChaosExpansion>>::ErrorHistory)
71  .def("EvalHistory", &SmolyakEstimator<std::shared_ptr<PolynomialChaosExpansion>>::EvalHistory)
72  .def("TimeHistory", &SmolyakEstimator<std::shared_ptr<PolynomialChaosExpansion>>::TimeHistory)
73  .def("PointHistory", &SmolyakEstimator<std::shared_ptr<PolynomialChaosExpansion>>::PointHistory)
74  .def("TermHistory", &SmolyakEstimator<std::shared_ptr<PolynomialChaosExpansion>>::TermHistory);
75 
76 }
Factory class for constructing a pseudo-spectral polynomial chaos approximation using a fixed quadrat...
Definition: PCEFactory.h:22
std::vector< Eigen::VectorXd > const & QuadPts() const
Definition: PCEFactory.h:107
std::shared_ptr< PolynomialChaosExpansion > Compute(std::shared_ptr< muq::Modeling::ModPiece > const &model)
Definition: PCEFactory.cpp:65
A class for representing and using expansions of orthogonal multivariate polynomials.
static std::shared_ptr< PolynomialChaosExpansion > FromHDF5(std::string filename, std::string groupName="/")
Loads an expansion from an HDF5 file.
Eigen::VectorXd Variance() const
compute the variance of the current expansion
Eigen::VectorXd Magnitude() const
Compute the L2 norm of each output.
Eigen::MatrixXd TotalSensitivity() const
Compute all Sobol total sensitivities. Rows are outputs, each column is an input.
virtual void ToHDF5(muq::Utilities::H5Object &group) const override
Write PCE to HDF5 file.
Eigen::VectorXd SobolSensitivity(unsigned int targetDim) const
Compute the main sensitivity index for the input dimension, for each output dimension.
void PolynomialChaosWrapper(pybind11::module &m)
boost::property_tree::ptree ConvertDictToPtree(pybind11::dict dict)