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