7 #include <pybind11/pybind11.h>
8 #include <pybind11/eigen.h>
9 #include <pybind11/functional.h>
10 #include <pybind11/stl.h>
12 #include <dolfin/la/GenericVector.h>
14 #include <dolfin/fem/LinearVariationalProblem.h>
15 #include <dolfin/fem/LinearVariationalSolver.h>
16 #include <dolfin/fem/Form.h>
18 #include <dolfin/function/Function.h>
19 #include <dolfin/la/EigenVector.h>
33 std::unique_ptr<char, void(*)(
void*)> res {
34 abi::__cxa_demangle(name, NULL, NULL, &status),
38 return (status==0) ? res.get() : name ;
50 pybind11::object
const& outputFieldIn,
51 std::vector<pybind11::object>
const& inputs) :
WorkPiece(inputs.size(), 1){
71 auto xList = pybind11::list(inputs.at(expr.first));
72 for(
unsigned i=0; i<expr.second.second.size(); ++i)
73 expr.second.first.attr(expr.second.second.at(i).c_str()) = xList[i];
80 *f.second = *
SwigExtract(inputs.at(f.first)).
Cast<std::shared_ptr<dolfin::Function>>();
87 dolfin::LinearVariationalSolver solver(
problem);
97 Eigen::VectorXd& inVec = *std::dynamic_pointer_cast<dolfin::EigenVector>(it->second->vector())->vec();
100 Eigen::Ref<Eigen::Matrix<double, -1, -1, 1, -1, -1>, 0, Eigen::OuterStride<-1>> vec = boost::any_cast<Eigen::Ref<Eigen::Matrix<double, -1, -1, 1, -1, -1>, 0, Eigen::OuterStride<-1>> >(inputs.at(it->first).get());
104 it->second->update();
112 unsigned inputInd = it->first;
114 std::pair<pybind11::object, std::vector<std::string>> &exprDef = it->second;
117 if(inputs.at(inputInd).get().type() ==
typeid(double)){
118 double val = boost::any_cast<double>(inputs.at(inputInd).get());
120 assert(exprDef.second.size()==1);
122 exprDef.first.attr(exprDef.second.at(0).c_str()) = pybind11::cast(val);
125 Eigen::Ref<Eigen::Matrix<double, -1, -1, 1, -1, -1>, 0, Eigen::OuterStride<-1>> vec = boost::any_cast<Eigen::Ref<Eigen::Matrix<double, -1, -1, 1, -1, -1>, 0, Eigen::OuterStride<-1>> >(inputs.at(inputInd).get());
126 assert(exprDef.second.size()==vec.rows());
127 for(
int k=0;
k<exprDef.second.size(); ++
k){
128 exprDef.first.attr(exprDef.second.at(
k).c_str()) = pybind11::cast(vec(
k,0));
136 dolfin::LinearVariationalSolver solver(
problem);
139 std::shared_ptr<dolfin::EigenVector> vec = std::dynamic_pointer_cast<dolfin::EigenVector>(
outputField->vector());
143 outputs.at(0) = Eigen::VectorXd(*vec->vec());
146 boost::any
EvaluateVec(Eigen::Ref<const Eigen::VectorXd>
const& x, std::vector<double> vals)
159 dolfin::LinearVariationalSolver solver(
problem);
162 std::shared_ptr<dolfin::EigenVector> vec = std::dynamic_pointer_cast<dolfin::EigenVector>(
outputField->vector());
172 std::string typeName = pybind11::handle(obj).ptr()->ob_type->tp_name;
174 if(typeName.compare(requiredType))
176 throw std::invalid_argument(
"FenicsPiece constructor was given an instance of \"" + typeName +
"\" but requires an instance of \"" + requiredType +
"\"");
183 for(
unsigned i=0; i<inputs.size(); ++ i){
187 if(pybind11::isinstance<pybind11::list>(inputs.at(i))){
189 pybind11::list input(inputs[i]);
191 pybind11::object expr = input[0];
195 pybind11::list part2(input[1]);
197 if(pybind11::isinstance<pybind11::list>(part2) || pybind11::isinstance<pybind11::tuple>(part2)){
199 std::vector<std::string> names;
200 for(
auto&
name : part2){
201 names.push_back(
name.cast<std::string>());
214 std::map<unsigned, std::pair<pybind11::object, std::vector<std::string>>>
inputExprs;
215 std::map<unsigned, std::shared_ptr<dolfin::Function>>
inputFuncs;
218 std::shared_ptr<dolfin::LinearVariationalProblem>
problem;
std::string demangle_typename(const char *name)
std::map< unsigned, std::shared_ptr< dolfin::Function > > inputFuncs
std::shared_ptr< dolfin::Function > outputField
virtual void EvaluateImpl(ref_vector< boost::any > const &inputs) override
User-implemented function that determines the behavior of this muq::Modeling::WorkPiece.
static void CheckProblemType(pybind11::object const &obj, std::string const &requiredType)
std::map< unsigned, std::pair< pybind11::object, std::vector< std::string > > > inputExprs
boost::any EvaluateVec(Eigen::Ref< const Eigen::VectorXd > const &x, std::vector< double > vals)
std::shared_ptr< dolfin::LinearVariationalProblem > problem
void ExtractInputs(std::vector< pybind11::object > const &inputs)
void EvaluateFunc(std::vector< pybind11::object > const &inputs)
FenicsPiece(pybind11::object const &problemIn, pybind11::object const &outputFieldIn, std::vector< pybind11::object > const &inputs)
Base class for MUQ's modelling envronment.
std::vector< boost::any > outputs
The outputs.
std::string name
A unique name for this WorkPiece. Defaults to <ClassName>_<id>
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...