| 1 |
/// Copyright (C) 2017 Chris N. Richardson and Garth N. Wells |
| 2 |
// |
| 3 |
// This file is part of DOLFIN. |
| 4 |
// |
| 5 |
// DOLFIN is free software: you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU Lesser General Public License as published by |
| 7 |
// the Free Software Foundation, either version 3 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// DOLFIN is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU Lesser General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
#include <memory> |
| 19 |
|
| 20 |
#include <pybind11/pybind11.h> |
| 21 |
#include <pybind11/numpy.h> |
| 22 |
#include <pybind11/stl.h> |
| 23 |
#include <pybind11/eigen.h> |
| 24 |
#include <pybind11/operators.h> |
| 25 |
|
| 26 |
#include <dolfin/common/Array.h> |
| 27 |
#include <dolfin/common/Hierarchical.h> |
| 28 |
#include <dolfin/function/assign.h> |
| 29 |
#include <dolfin/function/Constant.h> |
| 30 |
#include <dolfin/function/Expression.h> |
| 31 |
#include <dolfin/function/Function.h> |
| 32 |
#include <dolfin/function/FunctionAssigner.h> |
| 33 |
#include <dolfin/function/FunctionAXPY.h> |
| 34 |
#include <dolfin/function/FunctionSpace.h> |
| 35 |
#include <dolfin/function/MultiMeshFunction.h> |
| 36 |
#include <dolfin/function/MultiMeshFunctionSpace.h> |
| 37 |
#include <dolfin/function/MultiMeshSubSpace.h> |
| 38 |
#include <dolfin/function/LagrangeInterpolator.h> |
| 39 |
#include <dolfin/function/SpecialFunctions.h> |
| 40 |
#include <dolfin/fem/FiniteElement.h> |
| 41 |
#include <dolfin/fem/GenericDofMap.h> |
| 42 |
#include <dolfin/fem/MultiMeshDofMap.h> |
| 43 |
#include <dolfin/geometry/Point.h> |
| 44 |
#include <dolfin/la/GenericVector.h> |
| 45 |
#include <dolfin/la/GenericMatrix.h> |
| 46 |
#include <dolfin/mesh/Mesh.h> |
| 47 |
|
| 48 |
namespace py = pybind11; |
| 49 |
|
| 50 |
namespace dolfin_wrappers |
| 51 |
{ |
| 52 |
|
| 53 |
void function(py::module& m) |
| 54 |
{ |
| 55 |
// ufc::shape |
| 56 |
py::class_<ufc::shape>(m, "ufc_shape"); |
| 57 |
|
| 58 |
// ufc::cell |
| 59 |
py::class_<ufc::cell, std::shared_ptr<ufc::cell>>(m, "ufc_cell") |
| 60 |
.def(py::init<>()) |
| 61 |
.def_readonly("cell_shape", &ufc::cell::cell_shape) |
| 62 |
.def_readonly("topological_dimension", &ufc::cell::topological_dimension) |
| 63 |
.def_readonly("geometric_dimension", &ufc::cell::geometric_dimension) |
| 64 |
.def_readonly("local_facet", &ufc::cell::local_facet) |
| 65 |
.def_readonly("mesh_identifier", &ufc::cell::mesh_identifier) |
| 66 |
.def_readonly("index", &ufc::cell::index); |
| 67 |
|
| 68 |
// ufc::function |
| 69 |
py::class_<ufc::function, std::shared_ptr<ufc::function>>(m, "ufc_function"); |
| 70 |
|
| 71 |
// dolfin::GenericFunction |
| 72 |
py::class_<dolfin::GenericFunction, std::shared_ptr<dolfin::GenericFunction>, |
| 73 |
ufc::function, dolfin::Variable> |
| 74 |
(m, "GenericFunction") |
| 75 |
.def("value_dimension", &dolfin::GenericFunction::value_dimension) |
| 76 |
.def("value_size", &dolfin::GenericFunction::value_size) |
| 77 |
.def("value_rank", &dolfin::GenericFunction::value_rank) |
| 78 |
.def_property_readonly("value_shape", &dolfin::GenericFunction::value_shape) |
| 79 |
// FIXME: Change eval function to return NumPy array |
| 80 |
// FIXME: Add C++ version that takes a dolfin::Cell |
| 81 |
.def("eval", [](const dolfin::GenericFunction& self, Eigen::Ref<Eigen::VectorXd> u, |
| 82 |
Eigen::Ref<const Eigen::VectorXd> x, const dolfin::Cell& cell) |
| 83 |
{ |
| 84 |
ufc::cell ufc_cell; |
| 85 |
cell.get_cell_data(ufc_cell); |
| 86 |
self.eval(u, x, ufc_cell); |
| 87 |
}, "Evaluate GenericFunction (cell version)") |
| 88 |
.def("eval", (void (dolfin::GenericFunction::*)(Eigen::Ref<Eigen::VectorXd>, |
| 89 |
Eigen::Ref<const Eigen::VectorXd>, const ufc::cell&) const) |
| 90 |
&dolfin::GenericFunction::eval, |
| 91 |
"Evaluate GenericFunction (cell version)") |
| 92 |
|
| 93 |
.def("eval", (void (dolfin::GenericFunction::*)(Eigen::Ref<Eigen::VectorXd>, |
| 94 |
Eigen::Ref<const Eigen::VectorXd>) const) |
| 95 |
&dolfin::GenericFunction::eval, py::arg("values"), py::arg("x"), "Evaluate GenericFunction") |
| 96 |
.def("compute_vertex_values", [](dolfin::GenericFunction& self, const dolfin::Mesh& mesh) |
| 97 |
{ std::vector<double> values; |
| 98 |
self.compute_vertex_values(values, mesh); |
| 99 |
return py::array_t<double>(values.size(), values.data()); |
| 100 |
}, "Compute values at all mesh vertices") |
| 101 |
.def("compute_vertex_values", [](dolfin::GenericFunction& self) |
| 102 |
{ |
| 103 |
auto V = self.function_space(); |
| 104 |
if (!V) |
| 105 |
throw py::value_error("GenericFunction has no function space. You must supply a mesh."); |
| 106 |
auto mesh = V->mesh(); |
| 107 |
if (!mesh) |
| 108 |
throw py::value_error("GenericFunction has no function space mesh. You must supply a mesh."); |
| 109 |
std::vector<double> values; |
| 110 |
self.compute_vertex_values(values, *mesh); |
| 111 |
// FIXME: this causes a copy, we should rewrite the C++ interface to use Eigen when SWIG is removed |
| 112 |
return py::array_t<double>(values.size(), values.data()); |
| 113 |
}, "Compute values at all mesh vertices by using the mesh function.function_space().mesh()") |
| 114 |
.def("function_space", &dolfin::GenericFunction::function_space) |
| 115 |
|
| 116 |
.def("restrict", [](const dolfin::GenericFunction& self, |
| 117 |
const dolfin::FiniteElement& element, |
| 118 |
const dolfin::Cell& cell) |
| 119 |
{ |
| 120 |
ufc::cell ufc_cell; |
| 121 |
cell.get_cell_data(ufc_cell); |
| 122 |
|
| 123 |
std::vector<double> coordinate_dofs; |
| 124 |
cell.get_coordinate_dofs(coordinate_dofs); |
| 125 |
|
| 126 |
std::size_t s_dim = element.space_dimension(); |
| 127 |
Eigen::VectorXd w(s_dim); |
| 128 |
self.restrict(w.data(), element, cell, |
| 129 |
coordinate_dofs.data(), ufc_cell); |
| 130 |
|
| 131 |
return w; // no copy |
| 132 |
}); |
| 133 |
|
| 134 |
// Create dolfin::Expression from a JIT pointer |
| 135 |
m.def("make_dolfin_expression", |
| 136 |
[](std::uintptr_t e) |
| 137 |
{ |
| 138 |
dolfin::Expression *p = reinterpret_cast<dolfin::Expression *>(e); |
| 139 |
return std::shared_ptr<const dolfin::Expression>(p); |
| 140 |
}, "Create a dolfin::Expression object from a pointer integer, typically returned by a just-in-time compiler"); |
| 141 |
|
| 142 |
// dolfin::Expression trampoline (used for overloading virtual |
| 143 |
// function from Python) |
| 144 |
class PyExpression : public dolfin::Expression |
| 145 |
{ |
| 146 |
using dolfin::Expression::Expression; |
| 147 |
|
| 148 |
void eval(Eigen::Ref<Eigen::VectorXd> values, |
| 149 |
Eigen::Ref<const Eigen::VectorXd> x) const override |
| 150 |
{ PYBIND11_OVERLOAD(void, dolfin::Expression, eval, values, x); } |
| 151 |
|
| 152 |
void eval(Eigen::Ref<Eigen::VectorXd> values, |
| 153 |
Eigen::Ref<const Eigen::VectorXd> x, |
| 154 |
const ufc::cell& cell) const override |
| 155 |
{ PYBIND11_OVERLOAD_NAME(void, dolfin::Expression, "eval_cell", eval, values, x, cell); } |
| 156 |
|
| 157 |
}; |
| 158 |
|
| 159 |
// dolfin:Expression |
| 160 |
py::class_<dolfin::Expression, PyExpression, std::shared_ptr<dolfin::Expression>, |
| 161 |
dolfin::GenericFunction>(m, "Expression", "An Expression is a function (field) that can appear as a coefficient in a form") |
| 162 |
.def(py::init<>()) |
| 163 |
.def(py::init<std::size_t>()) |
| 164 |
.def(py::init<std::size_t, std::size_t>()) |
| 165 |
.def(py::init<std::vector<std::size_t>>()) |
| 166 |
.def("__call__", [](const dolfin::Expression& self, Eigen::Ref<const Eigen::VectorXd> x) |
| 167 |
{ |
| 168 |
Eigen::VectorXd f(self.value_size()); |
| 169 |
self.eval(f, x); |
| 170 |
return f; |
| 171 |
}) |
| 172 |
.def("__call__", [](const dolfin::Expression& self, const dolfin::Point& p) |
| 173 |
{ |
| 174 |
const Eigen::Map<Eigen::VectorXd> x(const_cast<double*>(p.coordinates()), 3); |
| 175 |
Eigen::VectorXd f(self.value_size()); |
| 176 |
self.eval(f, x); |
| 177 |
return f; |
| 178 |
}) |
| 179 |
.def("value_dimension", &dolfin::Expression::value_dimension) |
| 180 |
.def("get_property", &dolfin::Expression::get_property) |
| 181 |
.def("set_property", [](dolfin::Expression& self, std::string name, py::object value) |
| 182 |
{ |
| 183 |
if (py::isinstance<dolfin::GenericFunction>(value)) |
| 184 |
{ |
| 185 |
auto _v = value.cast<std::shared_ptr<dolfin::GenericFunction>>(); |
| 186 |
self.set_generic_function(name, _v); |
| 187 |
} |
| 188 |
else if (py::hasattr(value, "_cpp_object")) |
| 189 |
{ |
| 190 |
auto _v = value.attr("_cpp_object").cast<std::shared_ptr<dolfin::GenericFunction>>(); |
| 191 |
self.set_generic_function(name, _v); |
| 192 |
} |
| 193 |
else |
| 194 |
{ |
| 195 |
double _v = value.cast<double>(); |
| 196 |
self.set_property(name, _v); |
| 197 |
} |
| 198 |
}) |
| 199 |
.def("get_generic_function", &dolfin::Expression::get_generic_function); |
| 200 |
|
| 201 |
// dolfin::Constant |
| 202 |
py::class_<dolfin::Constant, std::shared_ptr<dolfin::Constant>, dolfin::Expression> |
| 203 |
(m, "Constant") |
| 204 |
.def(py::init<double>()) |
| 205 |
.def(py::init<std::vector<double>>()) |
| 206 |
.def(py::init<std::vector<std::size_t>, std::vector<double>>()) |
| 207 |
.def("values", [](const dolfin::Constant& self) |
| 208 |
{ auto v = self.values(); return py::array_t<double>(v.size(), v.data()); }) |
| 209 |
.def("__float__", [](const dolfin::Constant& instance) -> double { return instance; }) |
| 210 |
/* |
| 211 |
.def("_assign", [](dolfin::Constant& self, const dolfin::Constant& other) -> const dolfin::Constant& |
| 212 |
{self = other;}) |
| 213 |
.def("_assign", [](dolfin::Constant& self, double value) -> const dolfin::Constant& |
| 214 |
{self = value;}) |
| 215 |
*/ |
| 216 |
.def("assign", [](dolfin::Constant& self, const dolfin::Constant& other) {self = other;}) |
| 217 |
.def("assign", [](dolfin::Constant& self, double value) {self = value;}) |
| 218 |
/* |
| 219 |
.def("_assign", (const dolfin::Constant& (dolfin::Constant::*)(const dolfin::Constant&)) |
| 220 |
&dolfin::Constant::operator=) |
| 221 |
.def("_assign", (const dolfin::Constant& (dolfin::Constant::*)(double)) |
| 222 |
&dolfin::Constant::operator=) |
| 223 |
*/ |
| 224 |
.def("str", &dolfin::Constant::str); |
| 225 |
|
| 226 |
// dolfin::FacetArea |
| 227 |
py::class_<dolfin::FacetArea, std::shared_ptr<dolfin::FacetArea>, |
| 228 |
dolfin::Expression, dolfin::GenericFunction> |
| 229 |
(m, "FacetArea") |
| 230 |
.def(py::init<std::shared_ptr<const dolfin::Mesh>>()); |
| 231 |
|
| 232 |
// dolfin::MeshCoordinates |
| 233 |
py::class_<dolfin::MeshCoordinates, std::shared_ptr<dolfin::MeshCoordinates>, |
| 234 |
dolfin::Expression, dolfin::GenericFunction> |
| 235 |
(m, "MeshCoordinates") |
| 236 |
.def(py::init<std::shared_ptr<const dolfin::Mesh>>()); |
| 237 |
|
| 238 |
|
| 239 |
// Hierarchical dolfin::Function |
| 240 |
py::class_<dolfin::Hierarchical<dolfin::Function>, std::shared_ptr<dolfin::Hierarchical<dolfin::Function>>> |
| 241 |
(m, "HierarchicalFunction", "some description") |
| 242 |
.def("root_node", [](dolfin::Hierarchical<dolfin::Function>& self) |
| 243 |
{ return self.root_node_shared_ptr(); } ) |
| 244 |
.def("leaf_node", [](dolfin::Hierarchical<dolfin::Function>& self) |
| 245 |
{ return self.leaf_node_shared_ptr(); } ); |
| 246 |
//.def("root_node", &dolfin::Hierarchical<dolfin::Function>::root_node_shared_ptr) |
| 247 |
//.def("leaf_node", &dolfin::Hierarchical<dolfin::Function>::leaf_node_shared_ptr); |
| 248 |
|
| 249 |
// dolfin::Function |
| 250 |
py::class_<dolfin::Function, std::shared_ptr<dolfin::Function>, dolfin::GenericFunction, |
| 251 |
dolfin::Hierarchical<dolfin::Function>> |
| 252 |
(m, "Function", "A finite element function") |
| 253 |
.def(py::init<std::shared_ptr<const dolfin::FunctionSpace>>(), "Create a function on the given function space") |
| 254 |
.def(py::init<std::shared_ptr<const dolfin::FunctionSpace>, std::string>()) |
| 255 |
.def(py::init<dolfin::Function&, std::size_t>()) |
| 256 |
.def(py::init<std::shared_ptr<dolfin::FunctionSpace>, std::shared_ptr<dolfin::GenericVector>>()) |
| 257 |
.def("_assign", (const dolfin::Function& (dolfin::Function::*)(const dolfin::Function&)) |
| 258 |
&dolfin::Function::operator=) |
| 259 |
.def("_assign", (const dolfin::Function& (dolfin::Function::*)(const dolfin::Expression&)) |
| 260 |
&dolfin::Function::operator=) |
| 261 |
.def("_assign", (void (dolfin::Function::*)(const dolfin::FunctionAXPY&)) |
| 262 |
&dolfin::Function::operator=) |
| 263 |
.def("_in", &dolfin::Function::in) |
| 264 |
.def("__call__", [](dolfin::Function& self, std::vector<double>& p) |
| 265 |
{ |
| 266 |
// FIXME - remove Array and replace with Eigen in DOLFIN |
| 267 |
const dolfin::Array<double> x(p.size(), p.data()); |
| 268 |
Eigen::VectorXd values(self.value_size()); |
| 269 |
dolfin::Array<double> _values(self.value_size(), values.data()); |
| 270 |
self.eval(_values, x); |
| 271 |
return values; |
| 272 |
}) |
| 273 |
.def("extrapolate", &dolfin::Function::extrapolate) |
| 274 |
.def("extrapolate", [](dolfin::Function& instance, const py::object v) |
| 275 |
{ |
| 276 |
auto _v = v.attr("_cpp_object").cast<dolfin::Function*>(); |
| 277 |
instance.extrapolate(*_v); |
| 278 |
}) |
| 279 |
|
| 280 |
.def("sub", &dolfin::Function::operator[]) |
| 281 |
.def("get_allow_extrapolation", &dolfin::Function::get_allow_extrapolation) |
| 282 |
.def("interpolate", (void (dolfin::Function::*)(const dolfin::GenericFunction&)) |
| 283 |
&dolfin::Function::interpolate, "Interpolate the function u") |
| 284 |
.def("interpolate", [](dolfin::Function& instance, const py::object v) |
| 285 |
{ |
| 286 |
auto _v = v.attr("_cpp_object").cast<dolfin::GenericFunction*>(); |
| 287 |
instance.interpolate(*_v); |
| 288 |
}, "Interpolate the function u") |
| 289 |
.def("set_allow_extrapolation", &dolfin::Function::set_allow_extrapolation) |
| 290 |
// FIXME: A lot of error when using non-const version - misused |
| 291 |
// by Python interface? |
| 292 |
.def("vector", (std::shared_ptr<const dolfin::GenericVector> (dolfin::Function::*)() const) |
| 293 |
&dolfin::Function::vector, "Return the vector associated with the finite element Function"); |
| 294 |
|
| 295 |
// FIXME: why is this floating here? |
| 296 |
m.def("interpolate", [](const dolfin::GenericFunction& f, |
| 297 |
std::shared_ptr<const dolfin::FunctionSpace> V) |
| 298 |
{ |
| 299 |
auto g = std::make_shared<dolfin::Function>(V); |
| 300 |
g->interpolate(f); |
| 301 |
return g; |
| 302 |
}); |
| 303 |
|
| 304 |
// dolfin::FunctionAXPY |
| 305 |
py::class_<dolfin::FunctionAXPY, std::shared_ptr<dolfin::FunctionAXPY>> function_axpy(m, "FunctionAXPY"); |
| 306 |
function_axpy |
| 307 |
.def(py::init<std::shared_ptr<const dolfin::Function>, double>()) |
| 308 |
.def(py::init<std::shared_ptr<const dolfin::Function>, std::shared_ptr<const dolfin::Function>, |
| 309 |
dolfin::FunctionAXPY::Direction>()) |
| 310 |
.def(py::init<std::vector<std::pair<double, std::shared_ptr<const dolfin::Function>>>>()) |
| 311 |
.def(py::init([](std::vector<std::pair<double, py::object>> fun) |
| 312 |
{ |
| 313 |
std::vector<std::pair<double, std::shared_ptr<const dolfin::Function>>> a; |
| 314 |
for (auto p : fun) |
| 315 |
a.push_back({p.first, p.second.attr("_cpp_object").cast<std::shared_ptr<const dolfin::Function>>()}); |
| 316 |
return dolfin::FunctionAXPY(a); |
| 317 |
})) |
| 318 |
.def(py::init([](py::object f1, double a) |
| 319 |
{ |
| 320 |
auto _f1 = f1.attr("_cpp_object").cast<std::shared_ptr<const dolfin::Function>>(); |
| 321 |
return dolfin::FunctionAXPY(_f1, a); |
| 322 |
})) |
| 323 |
.def(py::self + py::self) |
| 324 |
.def(py::self + std::shared_ptr<const dolfin::Function>()) |
| 325 |
.def("__add__", [](dolfin::FunctionAXPY& self, py::object f1) |
| 326 |
{ |
| 327 |
return (self + f1.attr("_cpp_object").cast<std::shared_ptr<const dolfin::Function>>()); |
| 328 |
}) |
| 329 |
.def(py::self - py::self) |
| 330 |
.def(py::self - std::shared_ptr<const dolfin::Function>()) |
| 331 |
.def("__sub__", [](dolfin::FunctionAXPY& self, py::object f1) |
| 332 |
{ |
| 333 |
return (self - f1.attr("_cpp_object").cast<std::shared_ptr<const dolfin::Function>>()); |
| 334 |
}) |
| 335 |
.def(py::self * float()) |
| 336 |
.def(py::self / float()); |
| 337 |
|
| 338 |
// dolfin::FunctionAXPY enum |
| 339 |
py::enum_<dolfin::FunctionAXPY::Direction>(function_axpy, "Direction") |
| 340 |
.value("ADD_ADD", dolfin::FunctionAXPY::Direction::ADD_ADD) |
| 341 |
.value("SUB_ADD", dolfin::FunctionAXPY::Direction::SUB_ADD) |
| 342 |
.value("ADD_SUB", dolfin::FunctionAXPY::Direction::ADD_SUB) |
| 343 |
.value("SUB_SUB", dolfin::FunctionAXPY::Direction::SUB_SUB); |
| 344 |
|
| 345 |
// dolfin::FunctionSpace |
| 346 |
py::class_<dolfin::FunctionSpace, std::shared_ptr<dolfin::FunctionSpace>, dolfin::Variable> |
| 347 |
(m, "FunctionSpace", py::dynamic_attr()) |
| 348 |
.def(py::init<std::shared_ptr<dolfin::Mesh>, std::shared_ptr<dolfin::FiniteElement>, |
| 349 |
std::shared_ptr<dolfin::GenericDofMap>>()) |
| 350 |
.def(py::init<const dolfin::FunctionSpace&>()) |
| 351 |
.def("__eq__", &dolfin::FunctionSpace::operator==) |
| 352 |
.def("dim", &dolfin::FunctionSpace::dim) |
| 353 |
.def("collapse", [](dolfin::FunctionSpace& self) |
| 354 |
{ |
| 355 |
std::unordered_map<std::size_t, std::size_t> dofs; |
| 356 |
auto V = self.collapse(dofs); |
| 357 |
return std::pair<std::shared_ptr<dolfin::FunctionSpace>, std::unordered_map<std::size_t, std::size_t>>({V, dofs}); |
| 358 |
}) |
| 359 |
.def("component", &dolfin::FunctionSpace::component) |
| 360 |
.def("contains", &dolfin::FunctionSpace::contains) |
| 361 |
.def("element", &dolfin::FunctionSpace::element) |
| 362 |
.def("mesh", &dolfin::FunctionSpace::mesh) |
| 363 |
.def("dofmap", &dolfin::FunctionSpace::dofmap) |
| 364 |
.def("set_x", &dolfin::FunctionSpace::set_x) |
| 365 |
.def("sub", (std::shared_ptr<dolfin::FunctionSpace> (dolfin::FunctionSpace::*)(std::size_t) const) |
| 366 |
&dolfin::FunctionSpace::sub) |
| 367 |
.def("extract_sub_space", &dolfin::FunctionSpace::extract_sub_space) |
| 368 |
.def("tabulate_dof_coordinates", [](const dolfin::FunctionSpace& self) |
| 369 |
{ |
| 370 |
const std::size_t gdim = self.element()->geometric_dimension(); |
| 371 |
std::vector<double> coords = self.tabulate_dof_coordinates(); |
| 372 |
assert(coords.size() % gdim == 0); |
| 373 |
|
| 374 |
py::array_t<double> c({coords.size()/gdim, gdim}, coords.data() ); |
| 375 |
return c; |
| 376 |
}); |
| 377 |
|
| 378 |
// dolfin::LagrangeInterpolator |
| 379 |
py::class_<dolfin::LagrangeInterpolator> (m, "LagrangeInterpolator") |
| 380 |
.def_static("interpolate", (void (*)(dolfin::Function&, const dolfin::Function&)) |
| 381 |
&dolfin::LagrangeInterpolator::interpolate) |
| 382 |
.def_static("interpolate", [](py::object f1, py::object f2) |
| 383 |
{ |
| 384 |
auto _f1 = f1.attr("_cpp_object").cast<dolfin::Function*>(); |
| 385 |
auto _f2cpp = f2.attr("_cpp_object"); |
| 386 |
if (py::isinstance<dolfin::Function>(_f2cpp)) |
| 387 |
{ |
| 388 |
auto _f2 = _f2cpp.cast<const dolfin::Function*>(); |
| 389 |
dolfin::LagrangeInterpolator::interpolate(*_f1, *_f2); |
| 390 |
} |
| 391 |
else if (py::isinstance<dolfin::Expression>(_f2cpp)) |
| 392 |
{ |
| 393 |
auto _f2 = _f2cpp.cast<const dolfin::Expression*>(); |
| 394 |
dolfin::LagrangeInterpolator::interpolate(*_f1, *_f2); |
| 395 |
} |
| 396 |
else |
| 397 |
throw py::type_error("Can only interpolate Expression or Function"); |
| 398 |
}); |
| 399 |
|
| 400 |
// dolfin::FunctionAssigner |
| 401 |
py::class_<dolfin::FunctionAssigner, std::shared_ptr<dolfin::FunctionAssigner>> |
| 402 |
(m, "FunctionAssigner") |
| 403 |
.def(py::init<std::shared_ptr<const dolfin::FunctionSpace>, |
| 404 |
std::shared_ptr<const dolfin::FunctionSpace>>()) |
| 405 |
.def(py::init<std::vector<std::shared_ptr<const dolfin::FunctionSpace>>, |
| 406 |
std::shared_ptr<const dolfin::FunctionSpace>>()) |
| 407 |
.def(py::init<std::shared_ptr<const dolfin::FunctionSpace>, |
| 408 |
std::vector<std::shared_ptr<const dolfin::FunctionSpace>>>()) |
| 409 |
.def(py::init([](py::object V0, py::object V1) |
| 410 |
{ |
| 411 |
if (py::isinstance<py::list>(V0)) |
| 412 |
{ |
| 413 |
std::vector<std::shared_ptr<const dolfin::FunctionSpace>> _V0; |
| 414 |
for (auto V : py::cast<py::list>(V0)) |
| 415 |
_V0.push_back(V.attr("_cpp_object").cast<std::shared_ptr<const dolfin::FunctionSpace>>()); |
| 416 |
auto _V1 = V1.attr("_cpp_object").cast<std::shared_ptr<const dolfin::FunctionSpace>>(); |
| 417 |
return dolfin::FunctionAssigner(_V0, _V1); |
| 418 |
} |
| 419 |
else if (py::isinstance<py::list>(V1)) |
| 420 |
{ |
| 421 |
std::vector<std::shared_ptr<const dolfin::FunctionSpace>> _V1; |
| 422 |
for (auto V : py::cast<py::list>(V1)) |
| 423 |
_V1.push_back(V.attr("_cpp_object").cast<std::shared_ptr<const dolfin::FunctionSpace>>()); |
| 424 |
auto _V0 = V0.attr("_cpp_object").cast<std::shared_ptr<const dolfin::FunctionSpace>>(); |
| 425 |
return dolfin::FunctionAssigner(_V0, _V1); |
| 426 |
} |
| 427 |
else |
| 428 |
{ |
| 429 |
auto _V0 = V0.attr("_cpp_object").cast<std::shared_ptr<const dolfin::FunctionSpace>>(); |
| 430 |
auto _V1 = V1.attr("_cpp_object").cast<std::shared_ptr<const dolfin::FunctionSpace>>(); |
| 431 |
return dolfin::FunctionAssigner(_V0, _V1); |
| 432 |
} |
| 433 |
})) |
| 434 |
.def("assign", (void (dolfin::FunctionAssigner::*)(std::shared_ptr<dolfin::Function>, |
| 435 |
std::shared_ptr<const dolfin::Function>) const) |
| 436 |
&dolfin::FunctionAssigner::assign) |
| 437 |
.def("assign", [](const dolfin::FunctionAssigner& self, py::object v0, py::object v1) |
| 438 |
{ |
| 439 |
if (py::isinstance<py::list>(v0)) |
| 440 |
{ |
| 441 |
std::vector<std::shared_ptr<dolfin::Function>> _v0; |
| 442 |
for (auto v : py::cast<py::list>(v0)) |
| 443 |
_v0.push_back(v.attr("_cpp_object").cast<std::shared_ptr<dolfin::Function>>()); |
| 444 |
auto _v1 = v1.attr("_cpp_object").cast<std::shared_ptr<const dolfin::Function>>(); |
| 445 |
self.assign(_v0, _v1); |
| 446 |
return; |
| 447 |
} |
| 448 |
else if (py::isinstance<py::list>(v1)) |
| 449 |
{ |
| 450 |
auto _v0 = v0.attr("_cpp_object").cast<std::shared_ptr<dolfin::Function>>(); |
| 451 |
std::vector<std::shared_ptr<const dolfin::Function>> _v1; |
| 452 |
for (auto v : py::cast<py::list>(v1)) |
| 453 |
_v1.push_back(v.attr("_cpp_object").cast<std::shared_ptr<const dolfin::Function>>()); |
| 454 |
self.assign(_v0, _v1); |
| 455 |
return; |
| 456 |
} |
| 457 |
else |
| 458 |
{ |
| 459 |
auto _v0 = v0.attr("_cpp_object").cast<std::shared_ptr<dolfin::Function>>(); |
| 460 |
auto _v1 = v1.attr("_cpp_object").cast<std::shared_ptr<const dolfin::Function>>(); |
| 461 |
self.assign(_v0, _v1); |
| 462 |
return; |
| 463 |
} |
| 464 |
}); |
| 465 |
py::class_<dolfin::MultiMeshFunction, std::shared_ptr<dolfin::MultiMeshFunction>, dolfin::Variable>(m, "MultiMeshFunction") |
| 466 |
.def(py::init<std::shared_ptr<dolfin::MultiMeshFunctionSpace>>()) |
| 467 |
.def("vector", static_cast<std::shared_ptr<dolfin::GenericVector>(dolfin::MultiMeshFunction::*)()>(&dolfin::MultiMeshFunction::vector)) |
| 468 |
.def("part", (std::shared_ptr<const dolfin::Function> (dolfin::MultiMeshFunction::*)(std::size_t) const)&dolfin::MultiMeshFunction::part) |
| 469 |
.def("part", (std::shared_ptr<const dolfin::Function> (dolfin::MultiMeshFunction::*)(std::size_t, bool) const)&dolfin::MultiMeshFunction::part) |
| 470 |
.def("assign_part", (void (dolfin::MultiMeshFunction::*)(std::size_t, const dolfin::Function)) &dolfin::MultiMeshFunction::assign_part); |
| 471 |
|
| 472 |
py::class_<dolfin::MultiMeshFunctionSpace, std::shared_ptr<dolfin::MultiMeshFunctionSpace>> |
| 473 |
(m, "MultiMeshFunctionSpace", py::dynamic_attr()) |
| 474 |
.def(py::init<std::shared_ptr<dolfin::MultiMesh>>()) |
| 475 |
.def("add", [](dolfin::MultiMeshFunctionSpace& self, py::object v0) |
| 476 |
{ |
| 477 |
auto _v0 = v0.attr("_cpp_object").cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 478 |
self.add(_v0); |
| 479 |
return; |
| 480 |
}) |
| 481 |
.def("num_parts", &dolfin::MultiMeshFunctionSpace::num_parts) |
| 482 |
.def("build", static_cast<void(dolfin::MultiMeshFunctionSpace::*)()>(&dolfin::MultiMeshFunctionSpace::build)) |
| 483 |
.def("multimesh", &dolfin::MultiMeshFunctionSpace::multimesh) |
| 484 |
.def("part", &dolfin::MultiMeshFunctionSpace::part) |
| 485 |
.def("lock_inactive_dofs", &dolfin::MultiMeshFunctionSpace::lock_inactive_dofs) |
| 486 |
.def("dofmap", &dolfin::MultiMeshFunctionSpace::dofmap) |
| 487 |
.def("dim", &dolfin::MultiMeshFunctionSpace::dim); |
| 488 |
|
| 489 |
// dolfin::assign interface |
| 490 |
m.def("assign", [](py::object v0, py::object v1) |
| 491 |
{ |
| 492 |
if (py::isinstance<py::list>(v0)) |
| 493 |
{ |
| 494 |
std::vector<std::shared_ptr<dolfin::Function>> _v0; |
| 495 |
for (auto v : py::cast<py::list>(v0)) |
| 496 |
_v0.push_back(v.attr("_cpp_object").cast<std::shared_ptr<dolfin::Function>>()); |
| 497 |
auto _v1 = v1.attr("_cpp_object").cast<std::shared_ptr<const dolfin::Function>>(); |
| 498 |
dolfin::assign(_v0, _v1); |
| 499 |
return; |
| 500 |
} |
| 501 |
else if (py::isinstance<py::list>(v1)) |
| 502 |
{ |
| 503 |
auto _v0 = v0.attr("_cpp_object").cast<std::shared_ptr<dolfin::Function>>(); |
| 504 |
std::vector<std::shared_ptr<const dolfin::Function>> _v1; |
| 505 |
for (auto v : py::cast<py::list>(v1)) |
| 506 |
_v1.push_back(v.attr("_cpp_object").cast<std::shared_ptr<const dolfin::Function>>()); |
| 507 |
dolfin::assign(_v0, _v1); |
| 508 |
return; |
| 509 |
} |
| 510 |
else |
| 511 |
{ |
| 512 |
auto _v0 = v0.attr("_cpp_object").cast<std::shared_ptr<dolfin::Function>>(); |
| 513 |
auto _v1 = v1.attr("_cpp_object").cast<std::shared_ptr<const dolfin::Function>>(); |
| 514 |
dolfin::assign(_v0, _v1); |
| 515 |
return; |
| 516 |
} |
| 517 |
}); |
| 518 |
|
| 519 |
py::class_<dolfin::MultiMeshSubSpace, std::shared_ptr<dolfin::MultiMeshSubSpace>, dolfin::MultiMeshFunctionSpace>(m, "MultiMeshSubSpace") |
| 520 |
.def(py::init<dolfin::MultiMeshFunctionSpace&, std::size_t, std::size_t>()) |
| 521 |
.def(py::init<dolfin::MultiMeshFunctionSpace&, std::vector<std::size_t>>()) |
| 522 |
.def(py::init([](py::object function_space, std::size_t i) |
| 523 |
{ |
| 524 |
auto f_s = function_space.attr("_cpp_object").cast<dolfin::MultiMeshFunctionSpace>(); |
| 525 |
return dolfin::MultiMeshSubSpace(f_s, i); |
| 526 |
} |
| 527 |
)); |
| 528 |
} |
| 529 |
} |