| 1 |
// Copyright (C) 2017 Chris 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 <iostream> |
| 19 |
#include <memory> |
| 20 |
#include <Eigen/Dense> |
| 21 |
#include <pybind11/pybind11.h> |
| 22 |
#include <pybind11/eigen.h> |
| 23 |
#include <pybind11/numpy.h> |
| 24 |
#include <pybind11/pytypes.h> |
| 25 |
#include <pybind11/stl.h> |
| 26 |
#include <pybind11/cast.h> |
| 27 |
|
| 28 |
#ifdef HAS_PYBIND11_PETSC4PY |
| 29 |
#include <petsc4py/petsc4py.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <ufc.h> |
| 33 |
#include <dolfin/fem/fem_utils.h> |
| 34 |
#include <dolfin/fem/assemble.h> |
| 35 |
#include <dolfin/fem/assemble_local.h> |
| 36 |
#include <dolfin/fem/Assembler.h> |
| 37 |
#include <dolfin/fem/MultiMeshAssembler.h> |
| 38 |
#include <dolfin/fem/MixedAssembler.h> |
| 39 |
#include <dolfin/fem/DirichletBC.h> |
| 40 |
#include <dolfin/fem/DiscreteOperators.h> |
| 41 |
#include <dolfin/fem/DofMap.h> |
| 42 |
#include <dolfin/fem/MultiMeshDofMap.h> |
| 43 |
#include <dolfin/fem/FiniteElement.h> |
| 44 |
#include <dolfin/fem/Form.h> |
| 45 |
#include <dolfin/fem/MultiMeshForm.h> |
| 46 |
#include <dolfin/fem/LinearVariationalProblem.h> |
| 47 |
#include <dolfin/fem/LinearVariationalSolver.h> |
| 48 |
#include <dolfin/fem/MixedLinearVariationalProblem.h> |
| 49 |
#include <dolfin/fem/MixedLinearVariationalSolver.h> |
| 50 |
#include <dolfin/fem/LocalSolver.h> |
| 51 |
#include <dolfin/fem/NonlinearVariationalProblem.h> |
| 52 |
#include <dolfin/fem/NonlinearVariationalSolver.h> |
| 53 |
#include <dolfin/fem/MixedNonlinearVariationalProblem.h> |
| 54 |
#include <dolfin/fem/MixedNonlinearVariationalSolver.h> |
| 55 |
#include <dolfin/fem/PETScDMCollection.h> |
| 56 |
#include <dolfin/fem/PointSource.h> |
| 57 |
#include <dolfin/fem/SparsityPatternBuilder.h> |
| 58 |
#include <dolfin/fem/SystemAssembler.h> |
| 59 |
#include <dolfin/function/GenericFunction.h> |
| 60 |
#include <dolfin/function/FunctionSpace.h> |
| 61 |
#include <dolfin/function/MultiMeshFunctionSpace.h> |
| 62 |
#include <dolfin/function/Function.h> |
| 63 |
#include <dolfin/function/MultiMeshFunction.h> |
| 64 |
#include <dolfin/la/GenericMatrix.h> |
| 65 |
#include <dolfin/la/GenericVector.h> |
| 66 |
#include <dolfin/la/GenericTensor.h> |
| 67 |
#include <dolfin/la/SparsityPattern.h> |
| 68 |
#include <dolfin/mesh/Mesh.h> |
| 69 |
#include <dolfin/mesh/MeshFunction.h> |
| 70 |
#include <dolfin/mesh/MultiMesh.h> |
| 71 |
#include <dolfin/fem/MultiMeshDirichletBC.h> |
| 72 |
#include <dolfin/mesh/SubDomain.h> |
| 73 |
#include <dolfin/adaptivity/adapt.h> |
| 74 |
#include <dolfin/la/PETScNestMatrix.h> |
| 75 |
|
| 76 |
#include "casters.h" |
| 77 |
|
| 78 |
namespace py = pybind11; |
| 79 |
|
| 80 |
namespace dolfin_wrappers |
| 81 |
{ |
| 82 |
void fem(py::module& m) |
| 83 |
{ |
| 84 |
// UFC objects |
| 85 |
py::class_<ufc::finite_element, std::shared_ptr<ufc::finite_element>> |
| 86 |
(m, "ufc_finite_element", "UFC finite element object"); |
| 87 |
py::class_<ufc::dofmap, std::shared_ptr<ufc::dofmap>> |
| 88 |
(m, "ufc_dofmap", "UFC dofmap object"); |
| 89 |
py::class_<ufc::form, std::shared_ptr<ufc::form>> |
| 90 |
(m, "ufc_form", "UFC form object"); |
| 91 |
|
| 92 |
// Adapt functions |
| 93 |
m.def("adapt", [](dolfin::Mesh mesh){return dolfin::adapt(mesh);}, |
| 94 |
"Mapping between parent and child entities has to be set in refined mesh. See various refinement algorithms and `parameters['refinement_algorithm']`."); |
| 95 |
m.def("adapt", [](const dolfin::MeshFunction<std::size_t>& mesh_function, |
| 96 |
std::shared_ptr<const dolfin::Mesh> adapted_mesh) |
| 97 |
{ |
| 98 |
return dolfin::adapt(mesh_function, adapted_mesh);}); |
| 99 |
|
| 100 |
// Function to convert pointers (from JIT usually) to UFC objects |
| 101 |
m.def("make_ufc_finite_element", |
| 102 |
[](std::uintptr_t e) |
| 103 |
{ |
| 104 |
ufc::finite_element * p = reinterpret_cast<ufc::finite_element *>(e); |
| 105 |
return std::shared_ptr<const ufc::finite_element>(p); |
| 106 |
}); |
| 107 |
|
| 108 |
m.def("make_ufc_dofmap", |
| 109 |
[](std::uintptr_t e) |
| 110 |
{ |
| 111 |
ufc::dofmap * p = reinterpret_cast<ufc::dofmap *>(e); |
| 112 |
return std::shared_ptr<const ufc::dofmap>(p); |
| 113 |
}); |
| 114 |
|
| 115 |
m.def("make_ufc_form", |
| 116 |
[](std::uintptr_t e) |
| 117 |
{ |
| 118 |
ufc::form * p = reinterpret_cast<ufc::form *>(e); |
| 119 |
return std::shared_ptr<const ufc::form>(p); |
| 120 |
}); |
| 121 |
|
| 122 |
// dolfin::FiniteElement |
| 123 |
py::class_<dolfin::FiniteElement, std::shared_ptr<dolfin::FiniteElement>> |
| 124 |
(m, "FiniteElement", "DOLFIN FiniteElement object") |
| 125 |
.def(py::init<std::shared_ptr<const ufc::finite_element>>()) |
| 126 |
.def("num_sub_elements", &dolfin::FiniteElement::num_sub_elements) |
| 127 |
.def("evaluate_dofs", [](const dolfin::FiniteElement& self, py::object f, |
| 128 |
py::array_t<double> coordinate_dofs, |
| 129 |
int cell_orientation, const dolfin::Cell& c) |
| 130 |
{ |
| 131 |
const ufc::function* _f = nullptr; |
| 132 |
if (py::hasattr(f, "_cpp_object")) |
| 133 |
_f = f.attr("_cpp_object").cast<ufc::function*>(); |
| 134 |
else |
| 135 |
_f = f.cast<ufc::function*>(); |
| 136 |
|
| 137 |
ufc::cell ufc_cell; |
| 138 |
c.get_cell_data(ufc_cell); |
| 139 |
py::array_t<double, py::array::c_style> dofs(self.space_dimension()); |
| 140 |
self.evaluate_dofs(dofs.mutable_data(), *_f, |
| 141 |
coordinate_dofs.data(), cell_orientation, ufc_cell); |
| 142 |
return dofs; |
| 143 |
}, "Evaluate degrees of freedom on element for a given function") |
| 144 |
.def("tabulate_dof_coordinates", [](const dolfin::FiniteElement& self, const dolfin::Cell& cell) |
| 145 |
{ |
| 146 |
// Get cell vertex coordinates |
| 147 |
std::vector<double> coordinate_dofs; |
| 148 |
cell.get_coordinate_dofs(coordinate_dofs); |
| 149 |
|
| 150 |
// Tabulate the coordinates |
| 151 |
boost::multi_array<double, 2> _dof_coords; |
| 152 |
self.tabulate_dof_coordinates(_dof_coords, coordinate_dofs, cell); |
| 153 |
|
| 154 |
// Copy data and return |
| 155 |
typedef Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> EigenArray; |
| 156 |
EigenArray dof_coords = Eigen::Map<EigenArray>(_dof_coords.data(), |
| 157 |
_dof_coords.shape()[0], |
| 158 |
_dof_coords.shape()[1]); |
| 159 |
return dof_coords; |
| 160 |
}, "Tabulate coordinates of dofs on cell") |
| 161 |
.def("evaluate_basis", [](const dolfin::FiniteElement& self, int i, |
| 162 |
const py::array_t<double> x, |
| 163 |
const py::array_t<double> coordinate_dofs, |
| 164 |
int cell_orientation) |
| 165 |
{ |
| 166 |
auto ufc_element = self.ufc_element(); |
| 167 |
const std::size_t size = ufc_element->value_size(); |
| 168 |
py::array_t<double, py::array::c_style> values(size); |
| 169 |
self.evaluate_basis(i, values.mutable_data(), x.data(), coordinate_dofs.data(), |
| 170 |
cell_orientation); |
| 171 |
return values; |
| 172 |
}, "Evaluate basis function i at given point x in cell.") |
| 173 |
.def("evaluate_basis_all", [](const dolfin::FiniteElement& self, |
| 174 |
const py::array_t<double> x, |
| 175 |
const py::array_t<double> coordinate_dofs, |
| 176 |
int cell_orientation) |
| 177 |
{ |
| 178 |
auto ufc_element = self.ufc_element(); |
| 179 |
const std::size_t size = ufc_element->value_size(); |
| 180 |
const std::size_t space_dimension = ufc_element->space_dimension(); |
| 181 |
py::array_t<double, py::array::c_style> values(space_dimension*size); |
| 182 |
self.evaluate_basis_all(values.mutable_data(), x.data(), coordinate_dofs.data(), |
| 183 |
cell_orientation); |
| 184 |
return values; |
| 185 |
}, "Evaluate order n derivatives of all basis functions at given point x in cell.") |
| 186 |
.def("evaluate_basis_derivatives", [](const dolfin::FiniteElement& self, |
| 187 |
int i, int order, |
| 188 |
const py::array_t<double> x, |
| 189 |
const py::array_t<double> coordinate_dofs, |
| 190 |
int cell_orientation) |
| 191 |
{ |
| 192 |
auto ufc_element = self.ufc_element(); |
| 193 |
|
| 194 |
const std::size_t gdim = self.geometric_dimension(); |
| 195 |
const std::size_t num_derivs = pow(gdim, order); |
| 196 |
const std::size_t size = ufc_element->value_size()*num_derivs; |
| 197 |
py::array_t<double, py::array::c_style> values(size); |
| 198 |
self.evaluate_basis_derivatives(i, order, values.mutable_data(), |
| 199 |
x.data(), coordinate_dofs.data(), |
| 200 |
cell_orientation); |
| 201 |
return values; |
| 202 |
}, "Evaluate order n derivatives of basis function i at given point x in cell.") |
| 203 |
.def("evaluate_basis_derivatives_all", [](const dolfin::FiniteElement& self, |
| 204 |
int order, |
| 205 |
const py::array_t<double> x, |
| 206 |
const py::array_t<double> coordinate_dofs, |
| 207 |
int cell_orientation) |
| 208 |
{ |
| 209 |
auto ufc_element = self.ufc_element(); |
| 210 |
|
| 211 |
const std::size_t gdim = self.geometric_dimension(); |
| 212 |
const std::size_t num_derivs = pow(gdim, order); |
| 213 |
const std::size_t size = ufc_element->value_size()*num_derivs; |
| 214 |
const std::size_t space_dimension = ufc_element->space_dimension(); |
| 215 |
py::array_t<double, py::array::c_style> values(size*space_dimension); |
| 216 |
self.evaluate_basis_derivatives_all(order, values.mutable_data(), |
| 217 |
x.data(), coordinate_dofs.data(), |
| 218 |
cell_orientation); |
| 219 |
return values; |
| 220 |
}, "Evaluate all basis functions at given point x in cell.") |
| 221 |
|
| 222 |
.def("space_dimension", &dolfin::FiniteElement::space_dimension) |
| 223 |
.def("geometric_dimension", &dolfin::FiniteElement::geometric_dimension) |
| 224 |
.def("value_dimension", &dolfin::FiniteElement::value_dimension) |
| 225 |
.def("signature", &dolfin::FiniteElement::signature); |
| 226 |
|
| 227 |
// dolfin::GenericDofMap |
| 228 |
py::class_<dolfin::GenericDofMap, std::shared_ptr<dolfin::GenericDofMap>, dolfin::Variable> |
| 229 |
(m, "GenericDofMap", "DOLFIN DofMap object") |
| 230 |
.def("global_dimension", &dolfin::GenericDofMap::global_dimension, |
| 231 |
"The dimension of the global finite element function space") |
| 232 |
.def("index_map", &dolfin::GenericDofMap::index_map) |
| 233 |
.def("neighbours", &dolfin::GenericDofMap::neighbours) |
| 234 |
.def("off_process_owner", &dolfin::GenericDofMap::off_process_owner) |
| 235 |
.def("shared_nodes", &dolfin::GenericDofMap::shared_nodes) |
| 236 |
.def("cell_dofs", &dolfin::GenericDofMap::cell_dofs) |
| 237 |
.def("dofs", (std::vector<dolfin::la_index>(dolfin::GenericDofMap::*)() const) |
| 238 |
&dolfin::GenericDofMap::dofs) |
| 239 |
.def("dofs", (std::vector<dolfin::la_index>(dolfin::GenericDofMap::*)(const dolfin::Mesh&, std::size_t) const) |
| 240 |
&dolfin::GenericDofMap::dofs) |
| 241 |
.def("entity_dofs", (std::vector<dolfin::la_index>(dolfin::GenericDofMap::*)(const dolfin::Mesh&, std::size_t) const) |
| 242 |
&dolfin::GenericDofMap::entity_dofs) |
| 243 |
.def("entity_closure_dofs", (std::vector<dolfin::la_index>(dolfin::GenericDofMap::*)(const dolfin::Mesh&, std::size_t) const) |
| 244 |
&dolfin::GenericDofMap::entity_closure_dofs) |
| 245 |
.def("entity_dofs", (std::vector<dolfin::la_index>(dolfin::GenericDofMap::*)(const dolfin::Mesh&, |
| 246 |
std::size_t, |
| 247 |
const std::vector<std::size_t>&) const) |
| 248 |
&dolfin::GenericDofMap::entity_dofs) |
| 249 |
.def("entity_closure_dofs", (std::vector<dolfin::la_index>(dolfin::GenericDofMap::*)(const dolfin::Mesh&, |
| 250 |
std::size_t, |
| 251 |
const std::vector<std::size_t>&) const) |
| 252 |
&dolfin::GenericDofMap::entity_closure_dofs) |
| 253 |
.def("num_entity_dofs", &dolfin::GenericDofMap::num_entity_dofs) |
| 254 |
.def("tabulate_local_to_global_dofs", &dolfin::GenericDofMap::tabulate_local_to_global_dofs) |
| 255 |
.def("local_to_global_index", &dolfin::GenericDofMap::local_to_global_index) |
| 256 |
.def("local_to_global_unowned", |
| 257 |
[](dolfin::GenericDofMap& self) { |
| 258 |
return Eigen::Map<const Eigen::Matrix<std::size_t, Eigen::Dynamic, 1>>( |
| 259 |
self.local_to_global_unowned().data(), |
| 260 |
self.local_to_global_unowned().size()); }, |
| 261 |
py::return_value_policy::reference_internal, |
| 262 |
"Return view into unowned part of local-to-global map") |
| 263 |
.def("clear_sub_map_data", &dolfin::GenericDofMap::clear_sub_map_data) |
| 264 |
.def("tabulate_entity_dofs", [](const dolfin::GenericDofMap& instance, std::size_t entity_dim, |
| 265 |
std::size_t cell_entity_index) |
| 266 |
{ |
| 267 |
std::vector<std::size_t> dofs(instance.num_entity_dofs(entity_dim)); |
| 268 |
instance.tabulate_entity_dofs(dofs, entity_dim, cell_entity_index); |
| 269 |
return py::array_t<std::size_t>(dofs.size(), dofs.data()); |
| 270 |
}) |
| 271 |
.def("block_size", &dolfin::GenericDofMap::block_size) |
| 272 |
.def("tabulate_local_to_global_dofs", [](const dolfin::GenericDofMap& instance) |
| 273 |
{ |
| 274 |
std::vector<std::size_t> dofs; |
| 275 |
instance.tabulate_local_to_global_dofs(dofs); |
| 276 |
return py::array_t<std::size_t>(dofs.size(), dofs.data()); |
| 277 |
}) |
| 278 |
.def("set", &dolfin::GenericDofMap::set) |
| 279 |
.def_readonly("constrained_domain", &dolfin::GenericDofMap::constrained_domain); |
| 280 |
|
| 281 |
// dolfin::DofMap |
| 282 |
py::class_<dolfin::DofMap, std::shared_ptr<dolfin::DofMap>, dolfin::GenericDofMap> |
| 283 |
(m, "DofMap", "DOLFIN DofMap object") |
| 284 |
.def(py::init<std::shared_ptr<const ufc::dofmap>, const dolfin::Mesh&>()) |
| 285 |
.def(py::init<std::shared_ptr<const ufc::dofmap>, const dolfin::Mesh&, std::shared_ptr<const dolfin::SubDomain>>()) |
| 286 |
.def("ownership_range", &dolfin::DofMap::ownership_range) |
| 287 |
.def("cell_dofs", &dolfin::DofMap::cell_dofs); |
| 288 |
|
| 289 |
// dolfin::MultiMeshDofMap |
| 290 |
py::class_<dolfin::MultiMeshDofMap, std::shared_ptr<dolfin::MultiMeshDofMap>> |
| 291 |
(m, "MultiMeshDofMap", "DOLFIN MultiMeshDofMap object") |
| 292 |
.def(py::init()) |
| 293 |
.def("inactive_dofs", &dolfin::MultiMeshDofMap::inactive_dofs); |
| 294 |
|
| 295 |
// dolfin::SparsityPatternBuilder |
| 296 |
py::class_<dolfin::SparsityPatternBuilder>(m, "SparsityPatternBuilder") |
| 297 |
.def_static("build", &dolfin::SparsityPatternBuilder::build, |
| 298 |
py::arg("sparsity_pattern"), py::arg("mesh"), |
| 299 |
py::arg("dofmaps"), py::arg("cells"), |
| 300 |
py::arg("interior_facets"), py::arg("exterior_facets"), |
| 301 |
py::arg("vertices"), py::arg("diagonal"), |
| 302 |
py::arg("init")=true, py::arg("finalize")=true) |
| 303 |
.def_static("build", &dolfin::SparsityPatternBuilder::build_mixed, |
| 304 |
py::arg("sparsity_pattern"), py::arg("mesh_ids"), py::arg("mesh"), |
| 305 |
py::arg("dofmaps"), py::arg("cells"), |
| 306 |
py::arg("interior_facets"), py::arg("exterior_facets"), |
| 307 |
py::arg("vertices"), py::arg("diagonal"), |
| 308 |
py::arg("init")=true, py::arg("finalize")=true); |
| 309 |
|
| 310 |
// dolfin::DirichletBC |
| 311 |
py::class_<dolfin::DirichletBC, std::shared_ptr<dolfin::DirichletBC>, dolfin::Variable> |
| 312 |
(m, "DirichletBC", "DOLFIN DirichletBC object") |
| 313 |
.def(py::init<const dolfin::DirichletBC&>()) |
| 314 |
.def(py::init<std::shared_ptr<const dolfin::FunctionSpace>, |
| 315 |
std::shared_ptr<const dolfin::GenericFunction>, |
| 316 |
std::shared_ptr<const dolfin::SubDomain>, std::string, bool>(), |
| 317 |
py::arg("V"), py::arg("g"), py::arg("sub_domain"), |
| 318 |
py::arg("method")="topological", py::arg("check_midpoint")=true) |
| 319 |
.def(py::init<std::shared_ptr<const dolfin::FunctionSpace>, |
| 320 |
std::shared_ptr<const dolfin::GenericFunction>, |
| 321 |
std::shared_ptr<const dolfin::MeshFunction<std::size_t>>, |
| 322 |
std::size_t, std::string>(), |
| 323 |
py::arg("V"), py::arg("g"), py::arg("sub_domains"), |
| 324 |
py::arg("sub_domain"), py::arg("method")="topological") |
| 325 |
.def("function_space", &dolfin::DirichletBC::function_space) |
| 326 |
.def("homogenize", &dolfin::DirichletBC::homogenize) |
| 327 |
.def("method", &dolfin::DirichletBC::method) |
| 328 |
.def("zero", &dolfin::DirichletBC::zero) |
| 329 |
.def("markers", &dolfin::DirichletBC::markers) |
| 330 |
.def("zero_columns", &dolfin::DirichletBC::zero_columns, |
| 331 |
py::arg("A"), py::arg("b"), py::arg("diagonal_value")=0.0, py::arg("transpose")=false) |
| 332 |
.def("get_boundary_values", [](const dolfin::DirichletBC& instance) |
| 333 |
{ |
| 334 |
dolfin::DirichletBC::Map map; |
| 335 |
instance.get_boundary_values(map); |
| 336 |
return map; |
| 337 |
}) |
| 338 |
.def("apply", (void (dolfin::DirichletBC::*)(dolfin::GenericVector&) const) |
| 339 |
&dolfin::DirichletBC::apply) |
| 340 |
.def("apply", (void (dolfin::DirichletBC::*)(dolfin::GenericMatrix&) const) |
| 341 |
&dolfin::DirichletBC::apply) |
| 342 |
.def("apply", (void (dolfin::DirichletBC::*)(dolfin::GenericMatrix&, dolfin::GenericVector&) const) |
| 343 |
&dolfin::DirichletBC::apply) |
| 344 |
.def("apply", (void (dolfin::DirichletBC::*)(dolfin::GenericVector&, const dolfin::GenericVector&) const) |
| 345 |
&dolfin::DirichletBC::apply) |
| 346 |
.def("apply", (void (dolfin::DirichletBC::*)(dolfin::GenericMatrix&, dolfin::GenericVector&, const dolfin::GenericVector&) const) |
| 347 |
&dolfin::DirichletBC::apply) |
| 348 |
.def("user_sub_domain", &dolfin::DirichletBC::user_sub_domain) |
| 349 |
.def("set_value", &dolfin::DirichletBC::set_value) |
| 350 |
.def("set_value", [](dolfin::DirichletBC& self, py::object value) |
| 351 |
{ |
| 352 |
auto _u = value.attr("_cpp_object").cast<std::shared_ptr<const dolfin::GenericFunction>>(); |
| 353 |
self.set_value(_u); |
| 354 |
}) |
| 355 |
.def("value", &dolfin::DirichletBC::value); |
| 356 |
|
| 357 |
|
| 358 |
// dolfin::MultiMeshDirichletBC |
| 359 |
py::class_<dolfin::MultiMeshDirichletBC, std::shared_ptr<dolfin::MultiMeshDirichletBC>> |
| 360 |
(m, "MultiMeshDirichletBC", "DOLFIN MultiMeshDirichletBC object") |
| 361 |
.def(py::init<const dolfin::MultiMeshDirichletBC&>()) |
| 362 |
// Need to handle FunctionSpace and SubDomain coming from the Python layer |
| 363 |
// Probably better to make Python class for it similar to DirichletBC |
| 364 |
.def(py::init([](std::shared_ptr<const dolfin::MultiMeshFunctionSpace> V, |
| 365 |
py::object g, std::shared_ptr<const dolfin::SubDomain> subdomain, |
| 366 |
std::string method, bool check_midpoint, |
| 367 |
bool exclude_overlapped_boundaries) |
| 368 |
{ |
| 369 |
std::shared_ptr<const dolfin::GenericFunction> _g; |
| 370 |
if (py::hasattr(g, "_cpp_object")) |
| 371 |
_g = g.attr("_cpp_object").cast<std::shared_ptr<dolfin::GenericFunction>>(); |
| 372 |
else |
| 373 |
_g = g.cast<std::shared_ptr<dolfin::GenericFunction>>(); |
| 374 |
|
| 375 |
return dolfin::MultiMeshDirichletBC(V, _g, subdomain, method, |
| 376 |
check_midpoint, exclude_overlapped_boundaries); |
| 377 |
}), py::arg("V"), py::arg("g"), py::arg("subdomain"), |
| 378 |
py::arg("method")="topological", |
| 379 |
py::arg("check_midpoint")=true, |
| 380 |
py::arg("exclude_overlapped_boundaries")=true) |
| 381 |
.def(py::init<std::shared_ptr<const dolfin::MultiMeshFunctionSpace>, |
| 382 |
std::shared_ptr<const dolfin::GenericFunction>, |
| 383 |
std::shared_ptr<const dolfin::SubDomain>, std::string, bool, bool>(), |
| 384 |
py::arg("V"), py::arg("g"), py::arg("sub_domain"), |
| 385 |
py::arg("method")="topological", py::arg("check_midpoint")=true, |
| 386 |
py::arg("exclude_overlapped_boundaries")=true) |
| 387 |
.def(py::init<std::shared_ptr<const dolfin::MultiMeshFunctionSpace>, |
| 388 |
std::shared_ptr<const dolfin::GenericFunction>, |
| 389 |
std::shared_ptr<const dolfin::MeshFunction<std::size_t>>, |
| 390 |
std::size_t, std::size_t, std::string>(), |
| 391 |
py::arg("V"), py::arg("g"), py::arg("sub_domains"), |
| 392 |
py::arg("sub_domain"), py::arg("part"), py::arg("method")="topological") |
| 393 |
.def(py::init<std::shared_ptr<const dolfin::MultiMeshFunctionSpace>, |
| 394 |
std::shared_ptr<const dolfin::GenericFunction>, |
| 395 |
std::shared_ptr<const dolfin::SubDomain>, std::size_t, std::string, bool, bool>(), |
| 396 |
py::arg("V"), py::arg("g"), py::arg("sub_domain"), py::arg("part"), |
| 397 |
py::arg("method")="topological", py::arg("check_midpoint")=true, |
| 398 |
py::arg("exclude_overlapped_boundaries")=true) |
| 399 |
.def("function_space", &dolfin::MultiMeshDirichletBC::function_space) |
| 400 |
.def("homogenize", &dolfin::MultiMeshDirichletBC::homogenize) |
| 401 |
.def("zero", &dolfin::MultiMeshDirichletBC::zero) |
| 402 |
.def("apply", (void (dolfin::MultiMeshDirichletBC::*)(dolfin::GenericVector&) const) |
| 403 |
&dolfin::MultiMeshDirichletBC::apply) |
| 404 |
.def("apply", (void (dolfin::MultiMeshDirichletBC::*)(dolfin::GenericMatrix&) const) |
| 405 |
&dolfin::MultiMeshDirichletBC::apply) |
| 406 |
.def("apply", (void (dolfin::MultiMeshDirichletBC::*)(dolfin::GenericMatrix&, dolfin::GenericVector&) const) |
| 407 |
&dolfin::MultiMeshDirichletBC::apply) |
| 408 |
.def("apply", (void (dolfin::MultiMeshDirichletBC::*)(dolfin::GenericVector&, const dolfin::GenericVector&) const) |
| 409 |
&dolfin::MultiMeshDirichletBC::apply) |
| 410 |
.def("apply", (void (dolfin::MultiMeshDirichletBC::*)(dolfin::GenericMatrix&, dolfin::GenericVector&, const dolfin::GenericVector&) const) |
| 411 |
&dolfin::MultiMeshDirichletBC::apply); |
| 412 |
|
| 413 |
// dolfin::AssemblerBase |
| 414 |
py::class_<dolfin::AssemblerBase, std::shared_ptr<dolfin::AssemblerBase>> |
| 415 |
(m, "AssemblerBase") |
| 416 |
.def("init_global_tensor", &dolfin::AssemblerBase::init_global_tensor) |
| 417 |
.def_readwrite("add_values", &dolfin::Assembler::add_values) |
| 418 |
.def_readwrite("keep_diagonal", &dolfin::Assembler::keep_diagonal) |
| 419 |
.def_readwrite("finalize_tensor", &dolfin::Assembler::finalize_tensor); |
| 420 |
|
| 421 |
// dolfin::Assembler |
| 422 |
py::class_<dolfin::Assembler, std::shared_ptr<dolfin::Assembler>, dolfin::AssemblerBase> |
| 423 |
(m, "Assembler", "DOLFIN Assembler object") |
| 424 |
.def(py::init<>()) |
| 425 |
.def("assemble", &dolfin::Assembler::assemble); |
| 426 |
|
| 427 |
// dolfin::MixedAssembler |
| 428 |
py::class_<dolfin::MixedAssembler, std::shared_ptr<dolfin::MixedAssembler>, dolfin::AssemblerBase> |
| 429 |
(m, "MixedAssembler", "DOLFIN MixedAssembler object") |
| 430 |
.def(py::init<>()) |
| 431 |
.def("assemble", &dolfin::MixedAssembler::assemble); |
| 432 |
|
| 433 |
// dolfin::SystemAssembler |
| 434 |
py::class_<dolfin::SystemAssembler, std::shared_ptr<dolfin::SystemAssembler>, dolfin::AssemblerBase> |
| 435 |
(m, "SystemAssembler", "DOLFIN SystemAssembler object") |
| 436 |
.def(py::init<std::shared_ptr<const dolfin::Form>, std::shared_ptr<const dolfin::Form>, |
| 437 |
std::vector<std::shared_ptr<const dolfin::DirichletBC>>>()) |
| 438 |
.def(py::init<std::vector<std::shared_ptr<const dolfin::Form>>, std::vector<std::shared_ptr<const dolfin::Form>>, |
| 439 |
std::vector<std::vector<std::shared_ptr<const dolfin::DirichletBC>>>>()) |
| 440 |
.def("assemble", (void (dolfin::SystemAssembler::*)(dolfin::GenericMatrix&, dolfin::GenericVector&)) |
| 441 |
&dolfin::SystemAssembler::assemble) |
| 442 |
.def("assemble", (void (dolfin::SystemAssembler::*)(std::vector<std::shared_ptr<dolfin::GenericMatrix>>, std::vector<std::shared_ptr<dolfin::GenericVector>>)) |
| 443 |
&dolfin::SystemAssembler::assemble) |
| 444 |
.def("assemble", (void (dolfin::SystemAssembler::*)(dolfin::GenericMatrix&)) &dolfin::SystemAssembler::assemble) |
| 445 |
.def("assemble", (void (dolfin::SystemAssembler::*)(dolfin::GenericVector&)) &dolfin::SystemAssembler::assemble) |
| 446 |
.def("assemble", (void (dolfin::SystemAssembler::*)(dolfin::GenericMatrix&, dolfin::GenericVector&, |
| 447 |
const dolfin::GenericVector&)) |
| 448 |
&dolfin::SystemAssembler::assemble) |
| 449 |
.def("assemble", (void (dolfin::SystemAssembler::*)(dolfin::GenericVector&, const dolfin::GenericVector&)) |
| 450 |
&dolfin::SystemAssembler::assemble); |
| 451 |
|
| 452 |
// dolfin::DiscreteOperators |
| 453 |
py::class_<dolfin::DiscreteOperators> (m, "DiscreteOperators") |
| 454 |
.def_static("build_gradient", &dolfin::DiscreteOperators::build_gradient) |
| 455 |
.def_static("build_gradient", [](py::object V0, py::object V1) |
| 456 |
{ |
| 457 |
auto _V0 = V0.attr("_cpp_object").cast<dolfin::FunctionSpace*>(); |
| 458 |
auto _V1 = V1.attr("_cpp_object").cast<dolfin::FunctionSpace*>(); |
| 459 |
return dolfin::DiscreteOperators::build_gradient(*_V0, *_V1); |
| 460 |
}); |
| 461 |
|
| 462 |
// dolfin::Form |
| 463 |
py::class_<dolfin::Form, std::shared_ptr<dolfin::Form>> |
| 464 |
(m, "Form", "DOLFIN Form object") |
| 465 |
.def(py::init<std::size_t, std::size_t>()) |
| 466 |
.def(py::init<std::shared_ptr<const ufc::form>, |
| 467 |
std::vector<std::shared_ptr<const dolfin::FunctionSpace>>>()) |
| 468 |
.def(py::init<std::size_t, std::size_t>()) |
| 469 |
.def("function_spaces", &dolfin::Form::function_spaces, "Return function spaces for arguments") |
| 470 |
.def("function_space", &dolfin::Form::function_space, "Return function space for i-th argument") |
| 471 |
.def("num_coefficients", &dolfin::Form::num_coefficients, "Return number of coefficients in form") |
| 472 |
.def("original_coefficient_position", &dolfin::Form::original_coefficient_position) |
| 473 |
.def("set_coefficient", (void (dolfin::Form::*)(std::size_t, std::shared_ptr<const dolfin::GenericFunction>)) |
| 474 |
&dolfin::Form::set_coefficient, "Doc") |
| 475 |
.def("set_coefficient", (void (dolfin::Form::*)(std::string, std::shared_ptr<const dolfin::GenericFunction>)) |
| 476 |
&dolfin::Form::set_coefficient, "Doc") |
| 477 |
.def("set_mesh", &dolfin::Form::set_mesh) |
| 478 |
.def("set_cell_domains", &dolfin::Form::set_cell_domains) |
| 479 |
.def("set_exterior_facet_domains", &dolfin::Form::set_exterior_facet_domains) |
| 480 |
.def("set_interior_facet_domains", &dolfin::Form::set_interior_facet_domains) |
| 481 |
.def("set_vertex_domains", &dolfin::Form::set_vertex_domains) |
| 482 |
.def("rank", &dolfin::Form::rank) |
| 483 |
.def("mesh", &dolfin::Form::mesh); |
| 484 |
|
| 485 |
// dolfin::MultiMeshForm |
| 486 |
py::class_<dolfin::MultiMeshForm, std::shared_ptr<dolfin::MultiMeshForm>> |
| 487 |
(m, "MultiMeshForm", "DOLFIN MultiForm object") |
| 488 |
.def(py::init<std::shared_ptr<const dolfin::MultiMesh>>()) |
| 489 |
.def(py::init<std::shared_ptr<const dolfin::MultiMeshFunctionSpace>>()) |
| 490 |
.def(py::init<std::shared_ptr<const dolfin::MultiMeshFunctionSpace>, |
| 491 |
std::shared_ptr<const dolfin::MultiMeshFunctionSpace>>()) |
| 492 |
.def("add", &dolfin::MultiMeshForm::add) |
| 493 |
.def("build", &dolfin::MultiMeshForm::build) |
| 494 |
.def("set_multimesh_coefficient", &dolfin::MultiMeshForm::set_multimesh_coefficient); |
| 495 |
; |
| 496 |
|
| 497 |
// dolfin::MultiMeshAssembler |
| 498 |
py::class_<dolfin::MultiMeshAssembler, std::shared_ptr<dolfin::MultiMeshAssembler>> |
| 499 |
(m, "MultiMeshAssembler", "DOLFIN MultiMeshAssembler object") |
| 500 |
.def(py::init<>()) |
| 501 |
.def("assemble", &dolfin::MultiMeshAssembler::assemble); |
| 502 |
|
| 503 |
// dolfin::PointSource |
| 504 |
py::class_<dolfin::PointSource, std::shared_ptr<dolfin::PointSource>> |
| 505 |
(m, "PointSource") |
| 506 |
// FIXME: consolidate down to one intialiser when switching from |
| 507 |
// SWIG to pybind11 |
| 508 |
.def(py::init([](py::object V, const dolfin::Point& p, double value) |
| 509 |
{ |
| 510 |
std::shared_ptr<const dolfin::FunctionSpace> _V; |
| 511 |
if (py::hasattr(V, "_cpp_object")) |
| 512 |
_V = V.attr("_cpp_object").cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 513 |
else |
| 514 |
_V = V.cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 515 |
|
| 516 |
return dolfin::PointSource(_V, p, value); |
| 517 |
}), py::arg("V"), py::arg("p"), py::arg("value")) |
| 518 |
.def(py::init([](py::object V0, py::object V1, const dolfin::Point& p, double value) |
| 519 |
{ |
| 520 |
std::shared_ptr<const dolfin::FunctionSpace> _V0, _V1; |
| 521 |
if (py::hasattr(V0, "_cpp_object")) |
| 522 |
_V0 = V0.attr("_cpp_object").cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 523 |
else |
| 524 |
_V0 = V0.cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 525 |
|
| 526 |
if (py::hasattr(V1, "_cpp_object")) |
| 527 |
_V1 = V1.attr("_cpp_object").cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 528 |
else |
| 529 |
_V1 = V1.cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 530 |
|
| 531 |
return dolfin::PointSource(_V0, _V1, p, value); |
| 532 |
}), py::arg("V0"), py::arg("V1"), py::arg("p"), py::arg("value")) |
| 533 |
.def(py::init([](py::object V, const std::vector<std::pair<const dolfin::Point*, double>> values) |
| 534 |
{ |
| 535 |
std::shared_ptr<const dolfin::FunctionSpace> _V; |
| 536 |
if (py::hasattr(V, "_cpp_object")) |
| 537 |
_V = V.attr("_cpp_object").cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 538 |
else |
| 539 |
_V = V.cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 540 |
|
| 541 |
return dolfin::PointSource(_V, values); |
| 542 |
}), py::arg("V"), py::arg("values")) |
| 543 |
.def(py::init([](py::object V0, py::object V1, const std::vector<std::pair<const dolfin::Point*, double>> values) |
| 544 |
{ |
| 545 |
std::shared_ptr<const dolfin::FunctionSpace> _V0, _V1; |
| 546 |
if (py::hasattr(V0, "_cpp_object")) |
| 547 |
_V0 = V0.attr("_cpp_object").cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 548 |
else |
| 549 |
_V0 = V0.cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 550 |
|
| 551 |
if (py::hasattr(V1, "_cpp_object")) |
| 552 |
_V1 = V1.attr("_cpp_object").cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 553 |
else |
| 554 |
_V1 = V1.cast<std::shared_ptr<dolfin::FunctionSpace>>(); |
| 555 |
|
| 556 |
return dolfin::PointSource(_V0, _V1, values); |
| 557 |
}), py::arg("V0"), py::arg("V1"), py::arg("values")) |
| 558 |
// |
| 559 |
//.def(py::init<std::shared_ptr<const dolfin::FunctionSpace>, const dolfin::Point&, double>(), |
| 560 |
// py::arg("V"), py::arg("p"), py::arg("value")) |
| 561 |
//.def(py::init<std::shared_ptr<const dolfin::FunctionSpace>, std::shared_ptr<const dolfin::FunctionSpace>, const dolfin::Point&, double>(), |
| 562 |
// py::arg("V0"), py::arg("V1"), py::arg("p"), py::arg("value")) |
| 563 |
//.def(py::init<std::shared_ptr<const dolfin::FunctionSpace>, const std::vector<std::pair<const dolfin::Point*, double>>>()) |
| 564 |
//.def(py::init<std::shared_ptr<const dolfin::FunctionSpace>, std::shared_ptr<const dolfin::FunctionSpace>, |
| 565 |
// const std::vector<std::pair<const dolfin::Point*, double>>>()) |
| 566 |
.def("apply", (void (dolfin::PointSource::*)(dolfin::GenericVector&)) &dolfin::PointSource::apply) |
| 567 |
.def("apply", (void (dolfin::PointSource::*)(dolfin::GenericMatrix&)) &dolfin::PointSource::apply); |
| 568 |
|
| 569 |
// dolfin::LinearVariationalProblem |
| 570 |
py::class_<dolfin::LinearVariationalProblem, |
| 571 |
std::shared_ptr<dolfin::LinearVariationalProblem>> |
| 572 |
(m, "LinearVariationalProblem") |
| 573 |
.def(py::init<std::shared_ptr<const dolfin::Form>, |
| 574 |
std::shared_ptr<const dolfin::Form>, |
| 575 |
std::shared_ptr<dolfin::Function>, |
| 576 |
std::vector<std::shared_ptr<const dolfin::DirichletBC>>>()) |
| 577 |
.def("bcs", &dolfin::LinearVariationalProblem::bcs); |
| 578 |
|
| 579 |
// dolfin::LinearVariationalSolver |
| 580 |
py::class_<dolfin::LinearVariationalSolver, |
| 581 |
std::shared_ptr<dolfin::LinearVariationalSolver>, |
| 582 |
dolfin::Variable>(m, "LinearVariationalSolver") |
| 583 |
.def(py::init<std::shared_ptr<dolfin::LinearVariationalProblem>>()) |
| 584 |
.def("solve", &dolfin::LinearVariationalSolver::solve) |
| 585 |
.def("default_parameters", &dolfin::LinearVariationalSolver::default_parameters); |
| 586 |
|
| 587 |
// dolfin::MixedLinearVariationalProblem |
| 588 |
py::class_<dolfin::MixedLinearVariationalProblem, |
| 589 |
std::shared_ptr<dolfin::MixedLinearVariationalProblem>> |
| 590 |
(m, "MixedLinearVariationalProblem") |
| 591 |
.def(py::init<std::vector<std::vector<std::shared_ptr<const dolfin::Form>>>, |
| 592 |
std::vector<std::vector<std::shared_ptr<const dolfin::Form>>>, |
| 593 |
std::vector<std::shared_ptr<dolfin::Function>>, |
| 594 |
std::vector<std::shared_ptr<const dolfin::DirichletBC>>>()) |
| 595 |
.def("bcs", (std::vector<std::shared_ptr<const dolfin::DirichletBC>> |
| 596 |
(dolfin::MixedLinearVariationalProblem::*)(int) const) &dolfin::MixedLinearVariationalProblem::bcs) |
| 597 |
.def("bcs", (std::vector<std::vector<std::shared_ptr<const dolfin::DirichletBC>>> |
| 598 |
(dolfin::MixedLinearVariationalProblem::*)() const) &dolfin::MixedLinearVariationalProblem::bcs); |
| 599 |
|
| 600 |
// dolfin::MixedLinearVariationalSolver |
| 601 |
py::class_<dolfin::MixedLinearVariationalSolver, |
| 602 |
std::shared_ptr<dolfin::MixedLinearVariationalSolver>, |
| 603 |
dolfin::Variable>(m, "MixedLinearVariationalSolver") |
| 604 |
.def(py::init<>()) |
| 605 |
.def(py::init<std::shared_ptr<dolfin::MixedLinearVariationalProblem>>()) |
| 606 |
.def("solve", (void (dolfin::MixedLinearVariationalSolver::*)())&dolfin::MixedLinearVariationalSolver::solve) |
| 607 |
.def("solve", (void (dolfin::MixedLinearVariationalSolver::*) |
| 608 |
(std::tuple<std::vector<std::shared_ptr<dolfin::GenericMatrix>>, |
| 609 |
std::vector<std::shared_ptr<dolfin::GenericVector>>, |
| 610 |
std::vector<std::shared_ptr<dolfin::GenericVector>> >))&dolfin::MixedLinearVariationalSolver::solve) |
| 611 |
.def("assemble_system", (std::tuple<std::vector<std::shared_ptr<dolfin::GenericMatrix>>, |
| 612 |
std::vector<std::shared_ptr<dolfin::GenericVector>>, |
| 613 |
std::vector<std::shared_ptr<dolfin::GenericVector>> > |
| 614 |
(dolfin::MixedLinearVariationalSolver::*)()) &dolfin::MixedLinearVariationalSolver::assemble_system); |
| 615 |
|
| 616 |
// dolfin::NonlinearVariationalProblem |
| 617 |
py::class_<dolfin::NonlinearVariationalProblem, |
| 618 |
std::shared_ptr<dolfin::NonlinearVariationalProblem>> |
| 619 |
(m, "NonlinearVariationalProblem") |
| 620 |
.def(py::init<std::shared_ptr<const dolfin::Form>, |
| 621 |
std::shared_ptr<dolfin::Function>, |
| 622 |
std::vector<std::shared_ptr<const dolfin::DirichletBC>>, |
| 623 |
std::shared_ptr<const dolfin::Form>>()) |
| 624 |
// FIXME: is there a better way to handle the casting |
| 625 |
.def("set_bounds", (void (dolfin::NonlinearVariationalProblem::*)(std::shared_ptr<const dolfin::GenericVector>, |
| 626 |
std::shared_ptr<const dolfin::GenericVector>)) |
| 627 |
&dolfin::NonlinearVariationalProblem::set_bounds) |
| 628 |
.def("set_bounds", (void (dolfin::NonlinearVariationalProblem::*)(const dolfin::Function&, const dolfin::Function&)) |
| 629 |
&dolfin::NonlinearVariationalProblem::set_bounds) |
| 630 |
.def("set_bounds", [](dolfin::NonlinearVariationalProblem& self, py::object lb, py::object ub) |
| 631 |
{ |
| 632 |
auto& _lb = lb.attr("_cpp_object").cast<dolfin::Function&>(); |
| 633 |
auto& _ub = ub.attr("_cpp_object").cast<dolfin::Function&>(); |
| 634 |
self.set_bounds(_lb, _ub); |
| 635 |
}); |
| 636 |
|
| 637 |
|
| 638 |
// dolfin::NonlinearVariationalSolver |
| 639 |
py::class_<dolfin::NonlinearVariationalSolver, |
| 640 |
std::shared_ptr<dolfin::NonlinearVariationalSolver>, |
| 641 |
dolfin::Variable> |
| 642 |
(m, "NonlinearVariationalSolver") |
| 643 |
.def(py::init<std::shared_ptr<dolfin::NonlinearVariationalProblem>>()) |
| 644 |
.def("solve", &dolfin::NonlinearVariationalSolver::solve) |
| 645 |
.def("default_parameters", &dolfin::NonlinearVariationalSolver::default_parameters); |
| 646 |
|
| 647 |
// dolfin::MixedNonlinearVariationalProblem |
| 648 |
py::class_<dolfin::MixedNonlinearVariationalProblem, |
| 649 |
std::shared_ptr<dolfin::MixedNonlinearVariationalProblem>> |
| 650 |
(m, "MixedNonlinearVariationalProblem") |
| 651 |
.def(py::init<std::vector<std::vector<std::shared_ptr<const dolfin::Form>>>, |
| 652 |
std::vector<std::shared_ptr<dolfin::Function>>, |
| 653 |
std::vector<std::shared_ptr<const dolfin::DirichletBC>>, |
| 654 |
std::vector<std::vector<std::shared_ptr<const dolfin::Form>>>>()) |
| 655 |
.def("bcs", (std::vector<std::shared_ptr<const dolfin::DirichletBC>> |
| 656 |
(dolfin::MixedNonlinearVariationalProblem::*)(int) const) &dolfin::MixedNonlinearVariationalProblem::bcs) |
| 657 |
.def("bcs", (std::vector<std::vector<std::shared_ptr<const dolfin::DirichletBC>>> |
| 658 |
(dolfin::MixedNonlinearVariationalProblem::*)() const) &dolfin::MixedNonlinearVariationalProblem::bcs); |
| 659 |
|
| 660 |
// dolfin::MixedNonlinearVariationalSolver |
| 661 |
py::class_<dolfin::MixedNonlinearVariationalSolver, |
| 662 |
std::shared_ptr<dolfin::MixedNonlinearVariationalSolver>, |
| 663 |
dolfin::Variable>(m, "MixedNonlinearVariationalSolver") |
| 664 |
.def(py::init<std::shared_ptr<dolfin::MixedNonlinearVariationalProblem>>()) |
| 665 |
.def("solve", &dolfin::MixedNonlinearVariationalSolver::solve); |
| 666 |
|
| 667 |
// dolfin::LocalSolver |
| 668 |
py::class_<dolfin::LocalSolver, std::shared_ptr<dolfin::LocalSolver>> |
| 669 |
local_solver(m, "LocalSolver"); |
| 670 |
|
| 671 |
// dolfin::LocalSolver enums |
| 672 |
py::enum_<dolfin::LocalSolver::SolverType>(local_solver, "SolverType") |
| 673 |
.value("LU", dolfin::LocalSolver::SolverType::LU) |
| 674 |
.value("Cholesky", dolfin::LocalSolver::SolverType::Cholesky); |
| 675 |
|
| 676 |
local_solver.def(py::init<std::shared_ptr<const dolfin::Form>, |
| 677 |
std::shared_ptr<const dolfin::Form>, |
| 678 |
dolfin::LocalSolver::SolverType>()) |
| 679 |
.def(py::init<std::shared_ptr<const dolfin::Form>, |
| 680 |
dolfin::LocalSolver::SolverType>()) |
| 681 |
.def("factorize", &dolfin::LocalSolver::factorize) |
| 682 |
.def("clear_factorization", &dolfin::LocalSolver::clear_factorization) |
| 683 |
.def("solve_local", &dolfin::LocalSolver::solve_local) |
| 684 |
.def("solve_local_rhs", &dolfin::LocalSolver::solve_local_rhs) |
| 685 |
.def("solve_global_rhs", &dolfin::LocalSolver::solve_global_rhs) |
| 686 |
.def("solve_local_rhs", [](dolfin::LocalSolver& self, py::object u) |
| 687 |
{ |
| 688 |
auto _u = u.attr("_cpp_object").cast<dolfin::Function*>(); |
| 689 |
self.solve_local_rhs(*_u); |
| 690 |
}) |
| 691 |
.def("solve_global_rhs", [](dolfin::LocalSolver& self, py::object u) |
| 692 |
{ |
| 693 |
auto _u = u.attr("_cpp_object").cast<dolfin::Function*>(); |
| 694 |
self.solve_global_rhs(*_u); |
| 695 |
}); |
| 696 |
|
| 697 |
#ifdef HAS_PETSC |
| 698 |
// dolfin::PETScDMCollection |
| 699 |
py::class_<dolfin::PETScDMCollection, std::shared_ptr<dolfin::PETScDMCollection>> |
| 700 |
(m, "PETScDMCollection") |
| 701 |
.def(py::init<std::vector<std::shared_ptr<const dolfin::FunctionSpace>>>()) |
| 702 |
.def(py::init([](py::list V) |
| 703 |
{ |
| 704 |
std::vector<std::shared_ptr<const dolfin::FunctionSpace>> _V; |
| 705 |
for (auto space : V) |
| 706 |
{ |
| 707 |
auto _space = space.attr("_cpp_object").cast<std::shared_ptr<const dolfin::FunctionSpace>>(); |
| 708 |
_V.push_back(_space); |
| 709 |
} |
| 710 |
return dolfin::PETScDMCollection(_V); |
| 711 |
})) |
| 712 |
.def_static("create_transfer_matrix", &dolfin::PETScDMCollection::create_transfer_matrix) |
| 713 |
.def_static("create_transfer_matrix", [](py::object V_coarse, py::object V_fine) |
| 714 |
{ |
| 715 |
auto _V0 = V_coarse.attr("_cpp_object").cast<dolfin::FunctionSpace*>(); |
| 716 |
auto _V1 = V_fine.attr("_cpp_object").cast<dolfin::FunctionSpace*>(); |
| 717 |
return dolfin::PETScDMCollection::create_transfer_matrix(*_V0, *_V1); |
| 718 |
}) |
| 719 |
.def("check_ref_count", &dolfin::PETScDMCollection::check_ref_count) |
| 720 |
.def("get_dm", &dolfin::PETScDMCollection::get_dm); |
| 721 |
#endif |
| 722 |
|
| 723 |
// Assemble free functions |
| 724 |
m.def("assemble", (void (*)(dolfin::GenericTensor&, const dolfin::Form&)) &dolfin::assemble); |
| 725 |
m.def("assemble", (double (*)(const dolfin::Form&)) &dolfin::assemble); |
| 726 |
|
| 727 |
m.def("assemble_system", (void (*)(dolfin::GenericMatrix&, dolfin::GenericVector&, |
| 728 |
const dolfin::Form&, const dolfin::Form&, |
| 729 |
std::vector<std::shared_ptr<const dolfin::DirichletBC>>)) |
| 730 |
&dolfin::assemble_system); |
| 731 |
|
| 732 |
m.def("assemble_system", (void (*)(dolfin::GenericMatrix&, dolfin::GenericVector&, |
| 733 |
const dolfin::Form&, const dolfin::Form&, |
| 734 |
std::vector<std::shared_ptr<const dolfin::DirichletBC>>, |
| 735 |
const dolfin::GenericVector&)) |
| 736 |
&dolfin::assemble_system); |
| 737 |
|
| 738 |
m.def("assemble_local", [](const dolfin::Form& form, const dolfin::Cell& cell) |
| 739 |
{ |
| 740 |
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> m; |
| 741 |
dolfin::assemble_local(m, form, cell); |
| 742 |
return m; |
| 743 |
}); |
| 744 |
|
| 745 |
// FEM utils free functions |
| 746 |
m.def("create_mesh", dolfin::create_mesh); |
| 747 |
m.def("create_mesh", [](const py::object u) |
| 748 |
{ |
| 749 |
auto _u = u.attr("_cpp_object").cast<dolfin::Function*>(); |
| 750 |
return dolfin::create_mesh(*_u); |
| 751 |
}); |
| 752 |
|
| 753 |
m.def("set_coordinates", &dolfin::set_coordinates); |
| 754 |
m.def("set_coordinates", [](dolfin::MeshGeometry& geometry, const py::object u) |
| 755 |
{ |
| 756 |
auto _u = u.attr("_cpp_object").cast<const dolfin::Function*>(); |
| 757 |
dolfin::set_coordinates(geometry, *_u); |
| 758 |
}); |
| 759 |
|
| 760 |
m.def("get_coordinates", &dolfin::get_coordinates); |
| 761 |
m.def("get_coordinates", [](py::object u, const dolfin::MeshGeometry& geometry) |
| 762 |
{ |
| 763 |
auto _u = u.attr("_cpp_object").cast<dolfin::Function*>(); |
| 764 |
return dolfin::get_coordinates(*_u, geometry); |
| 765 |
}); |
| 766 |
|
| 767 |
m.def("vertex_to_dof_map", [](const dolfin::FunctionSpace& V) |
| 768 |
{ |
| 769 |
const auto _v2d = dolfin::vertex_to_dof_map(V); |
| 770 |
return py::array_t<dolfin::la_index>(_v2d.size(), _v2d.data()); |
| 771 |
}); |
| 772 |
|
| 773 |
m.def("vertex_to_dof_map", [](py::object V) |
| 774 |
{ |
| 775 |
auto _V = V.attr("_cpp_object").cast<dolfin::FunctionSpace*>(); |
| 776 |
const auto _v2d = dolfin::vertex_to_dof_map(*_V); |
| 777 |
return py::array_t<dolfin::la_index>(_v2d.size(), _v2d.data()); |
| 778 |
}); |
| 779 |
m.def("dof_to_vertex_map", &dolfin::dof_to_vertex_map); |
| 780 |
m.def("dof_to_vertex_map", [](py::object V) |
| 781 |
{ |
| 782 |
auto _V = V.attr("_cpp_object").cast<dolfin::FunctionSpace*>(); |
| 783 |
const auto _d2v = dolfin::dof_to_vertex_map(*_V); |
| 784 |
return py::array_t<std::size_t>(_d2v.size(), _d2v.data()); |
| 785 |
}); |
| 786 |
} |
| 787 |
} |