Page MenuHomec4science

numpy.hh
No OneTemporary

File Metadata

Created
Sun, Apr 28, 07:14

numpy.hh

/**
* @file
*
* @author Lucas Frérot <lucas.frerot@epfl.ch>
*
* @section LICENSE
*
* Copyright (©) 2017 EPFL (Ecole Polytechnique Fédérale de
* Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des
* Solides)
*
* Tamaas 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.
*
* Tamaas 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 Tamaas. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#ifndef __NUMPY_HH__
#define __NUMPY_HH__
/* -------------------------------------------------------------------------- */
#include "tamaas.hh"
#include "grid_base.hh"
/* -------------------------------------------------------------------------- */
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
__BEGIN_TAMAAS__
namespace wrap {
namespace py = pybind11;
template <typename T>
using numpy = py::array_t<T, py::array::c_style | py::array::forcecast>;
template <typename T>
class GridBaseNumpy : public GridBase<T> {
public:
GridBaseNumpy(numpy<T>& buffer) : GridBase<T>() {
this->nb_components = 1;
if (buffer.ndim() == 2)
this->nb_components = buffer.shape()[1];
else if (buffer.ndim() > 2)
TAMAAS_EXCEPTION("Dimension of numpy array not compatible with GridBase");
this->data.wrapMemory(buffer.mutable_data(), buffer.size());
}
};
/// Class wrapping a numpy array
template <class Parent>
class GridNumpy : public Parent {
public:
GridNumpy(numpy<typename Parent::value_type>& buffer) : Parent() {
auto* array_shape = buffer.shape();
if (buffer.ndim() > Parent::dimension + 1 ||
buffer.ndim() < Parent::dimension)
TAMAAS_EXCEPTION(
"Numpy array dimension do not match expected grid dimensions");
if (buffer.ndim() == Parent::dimension + 1)
this->nb_components = array_shape[buffer.ndim() - 1];
std::copy_n(array_shape, Parent::dimension, this->n.begin());
this->computeStrides();
this->data.wrapMemory(buffer.mutable_data(), this->computeSize());
}
};
/// Class wrapping a numpy array
template <class Parent>
class SurfaceNumpy : public Parent {
public:
SurfaceNumpy(numpy<typename Parent::value_type>& buffer) : Parent() {
auto* array_shape = buffer.shape();
if (buffer.ndim() != 2)
TAMAAS_EXCEPTION(
"Numpy array dimension do not match expected surface dimensions");
std::copy_n(array_shape, Parent::dimension, this->n.begin());
this->computeStrides();
this->data.wrapMemory(buffer.mutable_data(), this->computeSize());
}
};
} // namespace wrap
__END_TAMAAS__
#endif // __NUMPY_HH__

Event Timeline