| 1 |
// Copyright (C) 2017 Garth N. Wells |
| 2 |
// |
| 3 |
// This file is part of FFC. |
| 4 |
// |
| 5 |
// FFC 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 |
// FFC 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 FFC. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
#include <iostream> |
| 19 |
#include <memory> |
| 20 |
#include <stdexcept> |
| 21 |
#include <vector> |
| 22 |
#include <pybind11/pybind11.h> |
| 23 |
#include <pybind11/numpy.h> |
| 24 |
|
| 25 |
#include <ufc.h> |
| 26 |
|
| 27 |
namespace py = pybind11; |
| 28 |
|
| 29 |
namespace ufc_wrappers |
| 30 |
{ |
| 31 |
void check_array_shape(py::array_t<double> x, |
| 32 |
const std::vector<std::size_t> shape) |
| 33 |
{ |
| 34 |
const py::buffer_info x_info = x.request(); |
| 35 |
if ((std::size_t) x_info.ndim != shape.size()) |
| 36 |
throw std::runtime_error("NumPy array has wrong number of dimensions"); |
| 37 |
|
| 38 |
const auto& x_shape = x_info.shape; |
| 39 |
for (std::size_t i =0; i < shape.size();++i) |
| 40 |
{ |
| 41 |
if ((std::size_t) x_shape[i] != shape[i]) |
| 42 |
throw std::runtime_error("NumPy array has wrong size"); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// Interface function to |
| 47 |
// ufc::finite_element::evaluate_reference_basis |
| 48 |
py::array_t<double> evaluate_reference_basis(ufc::finite_element &instance, |
| 49 |
py::array_t<double> x) |
| 50 |
{ |
| 51 |
// Dimensions |
| 52 |
const std::size_t num_dofs = instance.space_dimension(); |
| 53 |
const std::size_t ref_size = instance.reference_value_size(); |
| 54 |
const std::size_t tdim = instance.topological_dimension(); |
| 55 |
|
| 56 |
// Extract point data |
| 57 |
const py::buffer_info x_info = x.request(); |
| 58 |
const auto& x_shape = x_info.shape; |
| 59 |
const std::size_t num_points = x_shape[0]; |
| 60 |
check_array_shape(x, {num_points, tdim}); |
| 61 |
|
| 62 |
// Create object to hold data to be computed and returned |
| 63 |
py::array_t<double, py::array::c_style> f({num_points, num_dofs, ref_size}); |
| 64 |
|
| 65 |
// Evaluate basis |
| 66 |
instance.evaluate_reference_basis(f.mutable_data(), num_points, x.data()); |
| 67 |
|
| 68 |
return f; |
| 69 |
} |
| 70 |
|
| 71 |
py::array_t<double> |
| 72 |
evaluate_reference_basis_derivatives(const ufc::finite_element &instance, |
| 73 |
std::size_t order, py::array_t<double> x) |
| 74 |
{ |
| 75 |
// Dimensions |
| 76 |
const std::size_t tdim = instance.topological_dimension(); |
| 77 |
const std::size_t num_dofs = instance.space_dimension(); |
| 78 |
const std::size_t ref_size = instance.reference_value_size(); |
| 79 |
|
| 80 |
// Extract point data |
| 81 |
py::buffer_info x_info = x.request(); |
| 82 |
const auto& x_shape = x_info.shape; |
| 83 |
const std::size_t num_points = x_shape[0]; |
| 84 |
check_array_shape(x, {num_points, tdim}); |
| 85 |
|
| 86 |
// Shape of data to be computed and returned, and create |
| 87 |
const std::size_t num_derivs = pow(tdim, order); |
| 88 |
py::array_t<double, py::array::c_style> |
| 89 |
f({num_points, num_dofs, num_derivs, ref_size}); |
| 90 |
|
| 91 |
// Evaluate basis derivatives |
| 92 |
instance.evaluate_reference_basis_derivatives(f.mutable_data(), order, |
| 93 |
num_points, x.data()); |
| 94 |
|
| 95 |
return f; |
| 96 |
} |
| 97 |
|
| 98 |
py::array_t<double> |
| 99 |
transform_reference_basis_derivatives(ufc::finite_element &instance, |
| 100 |
std::size_t order, |
| 101 |
py::array_t<double> reference_values, |
| 102 |
py::array_t<double> X, |
| 103 |
py::array_t<double> J, |
| 104 |
py::array_t<double> detJ, |
| 105 |
py::array_t<double> K, |
| 106 |
int orientation) |
| 107 |
{ |
| 108 |
// Dimensions |
| 109 |
const std::size_t gdim = instance.geometric_dimension(); |
| 110 |
const std::size_t tdim = instance.topological_dimension(); |
| 111 |
const std::size_t num_dofs = instance.space_dimension(); |
| 112 |
const std::size_t ref_size = instance.reference_value_size(); |
| 113 |
|
| 114 |
const std::size_t num_derivs = std::pow(tdim, order); |
| 115 |
|
| 116 |
// Extract reference value data (reference_values) |
| 117 |
py::buffer_info info_reference_values = reference_values.request(); |
| 118 |
const auto& shape_reference_values = info_reference_values.shape; |
| 119 |
const std::size_t num_points = shape_reference_values[0]; |
| 120 |
check_array_shape(reference_values, {num_points, num_dofs, num_derivs, |
| 121 |
ref_size}); |
| 122 |
|
| 123 |
// Extract point data (X) |
| 124 |
py::buffer_info x_info = X.request(); |
| 125 |
check_array_shape(X, {num_points, tdim}); |
| 126 |
|
| 127 |
// Extract Jacobian data (J) |
| 128 |
py::buffer_info info_J = J.request(); |
| 129 |
check_array_shape(J, {num_points, gdim, tdim}); |
| 130 |
|
| 131 |
// Extract determinant of Jacobian data (detJ) |
| 132 |
py::buffer_info info_detJ = detJ.request(); |
| 133 |
check_array_shape(detJ, {num_points}); |
| 134 |
|
| 135 |
// Extract inverse of Jacobian data (K) |
| 136 |
py::buffer_info info_K = K.request(); |
| 137 |
check_array_shape(K, {num_points, tdim, gdim}); |
| 138 |
|
| 139 |
// Shape of data to be computed and returned, and create |
| 140 |
py::array_t<double, py::array::c_style> |
| 141 |
f({num_points, num_dofs, num_derivs, ref_size}); |
| 142 |
|
| 143 |
// Evaluate basis derivatives |
| 144 |
instance.transform_reference_basis_derivatives(f.mutable_data(), order, |
| 145 |
num_points, |
| 146 |
reference_values.data(), |
| 147 |
X.data(), J.data(), |
| 148 |
detJ.data(), K.data(), |
| 149 |
orientation); |
| 150 |
|
| 151 |
return f; |
| 152 |
} |
| 153 |
|
| 154 |
// Remove from UFC |
| 155 |
py::array_t<double> evaluate_basis(ufc::finite_element &instance, |
| 156 |
std::size_t i, |
| 157 |
py::array_t<double> x, |
| 158 |
py::array_t<double> coordinate_dofs, |
| 159 |
int cell_orientation) |
| 160 |
{ |
| 161 |
// Dimensions |
| 162 |
const std::size_t gdim = instance.geometric_dimension(); |
| 163 |
const std::size_t tdim = instance.topological_dimension(); |
| 164 |
const std::size_t ref_size = instance.reference_value_size(); |
| 165 |
|
| 166 |
// Extract point data (x) |
| 167 |
py::buffer_info info_x = x.request(); |
| 168 |
check_array_shape(x, {gdim}); |
| 169 |
|
| 170 |
// Extract coordinate data (coordinate_dofs) |
| 171 |
py::buffer_info info_coordinate_dofs = coordinate_dofs.request(); |
| 172 |
|
| 173 |
// FIXME: this assumes an affine map |
| 174 |
check_array_shape(coordinate_dofs, {tdim + 1, gdim}); |
| 175 |
|
| 176 |
// Shape of data to be computed and returned, and create |
| 177 |
py::array_t<double, py::array::c_style> values(ref_size); |
| 178 |
|
| 179 |
instance.evaluate_basis(i, values.mutable_data(), x.data(), coordinate_dofs.data(), |
| 180 |
cell_orientation, nullptr); |
| 181 |
|
| 182 |
return values; |
| 183 |
} |
| 184 |
|
| 185 |
// Remove from UFC |
| 186 |
py::array_t<double> evaluate_basis_all(ufc::finite_element &instance, |
| 187 |
py::array_t<double> x, |
| 188 |
py::array_t<double> coordinate_dofs, |
| 189 |
int cell_orientation) |
| 190 |
{ |
| 191 |
// Dimensions |
| 192 |
const std::size_t gdim = instance.geometric_dimension(); |
| 193 |
const std::size_t tdim = instance.topological_dimension(); |
| 194 |
const std::size_t num_dofs = instance.space_dimension(); |
| 195 |
const std::size_t ref_size = instance.reference_value_size(); |
| 196 |
|
| 197 |
// Extract point data (x) |
| 198 |
py::buffer_info info_x = x.request(); |
| 199 |
check_array_shape(x, {gdim}); |
| 200 |
|
| 201 |
// Extract reference value data (coordinate_dofs) |
| 202 |
py::buffer_info info_coordinate_dofs = coordinate_dofs.request(); |
| 203 |
|
| 204 |
// FIXME: this assumes an affine map |
| 205 |
check_array_shape(coordinate_dofs, {tdim + 1, gdim}); |
| 206 |
|
| 207 |
// Shape of data to be computed and returned, and create |
| 208 |
py::array_t<double, py::array::c_style> f({num_dofs, ref_size}); |
| 209 |
|
| 210 |
// Call UFC function |
| 211 |
instance.evaluate_basis_all(f.mutable_data(), x.data(), |
| 212 |
coordinate_dofs.data(), |
| 213 |
cell_orientation, nullptr); |
| 214 |
|
| 215 |
return f; |
| 216 |
} |
| 217 |
|
| 218 |
// Remove from UFC |
| 219 |
py::array_t<double> evaluate_basis_derivatives(ufc::finite_element &instance, |
| 220 |
std::size_t i, |
| 221 |
std::size_t n, |
| 222 |
py::array_t<double> x, |
| 223 |
py::array_t<double> coordinate_dofs, |
| 224 |
int cell_orientation) |
| 225 |
{ |
| 226 |
// Dimensions |
| 227 |
const std::size_t gdim = instance.geometric_dimension(); |
| 228 |
const std::size_t tdim = instance.topological_dimension(); |
| 229 |
|
| 230 |
// Extract point data (x) |
| 231 |
py::buffer_info info_x = x.request(); |
| 232 |
check_array_shape(x, {gdim}); |
| 233 |
|
| 234 |
// Extract reference value data (coordinate_dofs) |
| 235 |
py::buffer_info info_coordinate_dofs = coordinate_dofs.request(); |
| 236 |
|
| 237 |
// FIXME: this assumes an affine map |
| 238 |
check_array_shape(coordinate_dofs, {tdim + 1, gdim}); |
| 239 |
|
| 240 |
// Shape of data to be computed and returned, and create |
| 241 |
py::array_t<double, py::array::c_style> f(gdim); |
| 242 |
|
| 243 |
// Call UFC function |
| 244 |
instance.evaluate_basis_derivatives(i, n, f.mutable_data(), x.data(), |
| 245 |
coordinate_dofs.data(), |
| 246 |
cell_orientation, nullptr); |
| 247 |
|
| 248 |
return f; |
| 249 |
} |
| 250 |
|
| 251 |
// Remove from UFC |
| 252 |
py::array_t<double> |
| 253 |
evaluate_basis_derivatives_all(ufc::finite_element &instance, |
| 254 |
std::size_t n, |
| 255 |
py::array_t<double> x, |
| 256 |
py::array_t<double> coordinate_dofs, |
| 257 |
int cell_orientation) |
| 258 |
{ |
| 259 |
// Dimensions |
| 260 |
const std::size_t gdim = instance.geometric_dimension(); |
| 261 |
const std::size_t tdim = instance.topological_dimension(); |
| 262 |
const std::size_t num_dofs = instance.space_dimension(); |
| 263 |
|
| 264 |
// Extract point data (x) |
| 265 |
py::buffer_info info_x = x.request(); |
| 266 |
check_array_shape(x, {gdim}); |
| 267 |
|
| 268 |
// Extract coordinate data (coordinate_dofs) |
| 269 |
py::buffer_info info_coordinate_dofs = coordinate_dofs.request(); |
| 270 |
|
| 271 |
// FIXME: this assumes an affine map |
| 272 |
check_array_shape(coordinate_dofs, {tdim + 1, gdim}); |
| 273 |
|
| 274 |
// Shape of data to be computed and returned, and create |
| 275 |
py::array_t<double, py::array::c_style> f({num_dofs, gdim}); |
| 276 |
|
| 277 |
// Call UFC function |
| 278 |
instance.evaluate_basis_derivatives_all(n, f.mutable_data(), x.data(), |
| 279 |
coordinate_dofs.data(), |
| 280 |
cell_orientation, nullptr); |
| 281 |
|
| 282 |
return f; |
| 283 |
} |
| 284 |
|
| 285 |
double evaluate_dof(ufc::finite_element &instance, |
| 286 |
std::size_t i, |
| 287 |
const ufc::function& f, |
| 288 |
py::array_t<double> coordinate_dofs, |
| 289 |
int cell_orientation, |
| 290 |
const ufc::cell& cell) |
| 291 |
{ |
| 292 |
// Dimensions |
| 293 |
const std::size_t gdim = instance.geometric_dimension(); |
| 294 |
const std::size_t tdim = instance.topological_dimension(); |
| 295 |
|
| 296 |
// Extract coordinate data (coordinate_dofs) |
| 297 |
py::buffer_info info_coordinate_dofs = coordinate_dofs.request(); |
| 298 |
|
| 299 |
// FIXME: this assumes an affine map |
| 300 |
check_array_shape(coordinate_dofs, {tdim + 1, gdim}); |
| 301 |
|
| 302 |
// Call UFC function |
| 303 |
return instance.evaluate_dof(i, f , coordinate_dofs.data(), |
| 304 |
cell_orientation, cell, nullptr); |
| 305 |
} |
| 306 |
|
| 307 |
py::array_t<double> evaluate_dofs(ufc::finite_element &instance, |
| 308 |
const ufc::function& f, |
| 309 |
py::array_t<double> coordinate_dofs, |
| 310 |
int cell_orientation, |
| 311 |
const ufc::cell& cell) |
| 312 |
{ |
| 313 |
// Dimensions |
| 314 |
const std::size_t gdim = instance.geometric_dimension(); |
| 315 |
const std::size_t tdim = instance.topological_dimension(); |
| 316 |
const std::size_t num_dofs = instance.space_dimension(); |
| 317 |
|
| 318 |
// Extract coordinate data (coordinate_dofs) |
| 319 |
py::buffer_info info_coordinate_dofs = coordinate_dofs.request(); |
| 320 |
|
| 321 |
// FIXME: this assumes an affine map |
| 322 |
check_array_shape(coordinate_dofs, {tdim + 1, gdim}); |
| 323 |
|
| 324 |
// Shape of data to be computed and returned, and create |
| 325 |
py::array_t<double, py::array::c_style> values(num_dofs); |
| 326 |
|
| 327 |
// Call UFC function |
| 328 |
instance.evaluate_dofs(values.mutable_data(), f , coordinate_dofs.data(), |
| 329 |
cell_orientation, cell, nullptr); |
| 330 |
|
| 331 |
return values; |
| 332 |
} |
| 333 |
|
| 334 |
py::array_t<double> interpolate_vertex_values(ufc::finite_element &instance, |
| 335 |
py::array_t<double> dof_values, |
| 336 |
py::array_t<double> coordinate_dofs, |
| 337 |
int cell_orientation) |
| 338 |
{ |
| 339 |
// Dimensions |
| 340 |
const std::size_t gdim = instance.geometric_dimension(); |
| 341 |
const std::size_t tdim = instance.topological_dimension(); |
| 342 |
const std::size_t num_dofs = instance.space_dimension(); |
| 343 |
const std::size_t num_components = instance.value_size(); |
| 344 |
|
| 345 |
// Extract dof values (dof_values) |
| 346 |
py::buffer_info info_dof_values = dof_values.request(); |
| 347 |
check_array_shape(dof_values, {num_dofs}); |
| 348 |
|
| 349 |
// Extract coordinate data (coordinate_dofs) |
| 350 |
py::buffer_info info_coordinate_dofs = coordinate_dofs.request(); |
| 351 |
|
| 352 |
// FIXME: this assumes an affine map |
| 353 |
check_array_shape(coordinate_dofs, {tdim + 1, gdim}); |
| 354 |
|
| 355 |
// Shape of data to be computed and returned, and create |
| 356 |
std::size_t num_vertices = tdim + 1; |
| 357 |
py::array_t<double, py::array::c_style> vertex_values({num_vertices, |
| 358 |
num_components}); |
| 359 |
|
| 360 |
// Call UFC function |
| 361 |
instance.interpolate_vertex_values(vertex_values.mutable_data(), |
| 362 |
dof_values.data(), |
| 363 |
coordinate_dofs.data(), |
| 364 |
cell_orientation, nullptr); |
| 365 |
|
| 366 |
return vertex_values; |
| 367 |
} |
| 368 |
|
| 369 |
py::array_t<double> tabulate_dof_coordinates(ufc::finite_element &instance, |
| 370 |
py::array_t<double> coordinate_dofs) |
| 371 |
{ |
| 372 |
// Dimensions |
| 373 |
const std::size_t gdim = instance.geometric_dimension(); |
| 374 |
const std::size_t tdim = instance.topological_dimension(); |
| 375 |
const std::size_t num_dofs = instance.space_dimension(); |
| 376 |
//const std::size_t num_components = instance.value_size(); |
| 377 |
|
| 378 |
// Extract coordinate data (coordinate_dofs) |
| 379 |
py::buffer_info info_coordinate_dofs = coordinate_dofs.request(); |
| 380 |
|
| 381 |
// FIXME: this assumes an affine map |
| 382 |
check_array_shape(coordinate_dofs, {tdim + 1, gdim}); |
| 383 |
|
| 384 |
// Shape of data to be computed and returned, and create |
| 385 |
py::array_t<double, py::array::c_style> coords({num_dofs, gdim}); |
| 386 |
|
| 387 |
|
| 388 |
// Call UFC function |
| 389 |
instance.tabulate_dof_coordinates(coords.mutable_data(), |
| 390 |
coordinate_dofs.data(), nullptr); |
| 391 |
|
| 392 |
return coords; |
| 393 |
} |
| 394 |
|
| 395 |
py::array_t<double> |
| 396 |
tabulate_reference_dof_coordinates(ufc::finite_element &instance) |
| 397 |
{ |
| 398 |
// Dimensions |
| 399 |
const std::size_t gdim = instance.geometric_dimension(); |
| 400 |
const std::size_t num_dofs = instance.space_dimension(); |
| 401 |
|
| 402 |
// Shape of data to be computed and returned, and create |
| 403 |
py::array_t<double, py::array::c_style> coords({num_dofs, gdim}); |
| 404 |
|
| 405 |
// Call UFC function |
| 406 |
instance.tabulate_reference_dof_coordinates(coords.mutable_data()); |
| 407 |
|
| 408 |
return coords; |
| 409 |
} |
| 410 |
} |