Page MenuHomec4science

grid_python.hh
No OneTemporary

File Metadata

Created
Tue, May 14, 20:13

grid_python.hh

/**
*
* @author Lucas Frérot <lucas.frerot@epfl.ch>
*
* @section LICENSE
*
* Copyright (©) 2016 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 __GRID_PYTHON_HH__
#define __GRID_PYTHON_HH__
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
template <typename G>
class GridPython : public G {
static constexpr UInt dim = G::dimension;
typedef typename G::value_type storage_type;
public:
GridPython(storage_type * wrapped_memory,
UInt n[dim],
Real L[dim],
UInt nb_components):
G() {
this->__init__(wrapped_memory, n, nb_components);
}
void __init__(storage_type * w, const std::array<UInt, dim> & n, UInt nb_components) {
std::copy(n.begin(), n.end(), this->n.begin());
this->nb_components = nb_components;
this->data.wrapMemory(w, this->computeSize());
this->computeStrides();
}
GridPython(PyObject * input): G() {
if (!PyArray_Check(input)) {
PyObject* obj_type = PyObject_Type(input);
std::string type = PyString_AsString(obj_type);
TAMAAS_EXCEPTION("GridPython: incompatible input which is not a numpy: " << type);
}
UInt _n = PyArray_NDIM((PyArrayObject*)input); // numpy number of dimensions
npy_intp * ndims = PyArray_DIMS((PyArrayObject*)input); // numpy dimensions
std::array<UInt, dim> grid_dims = {{0}}; // tamaas dims
if (_n >= dim) {
std::copy(ndims, ndims+dim, grid_dims.begin());
} else {
TAMAAS_EXCEPTION("GridPython: incompatible input size for numpy array: " << _n);
}
if (_n > dim) this->nb_components = ndims[dim];
else this->nb_components = 1;
PyArrayIterObject *iter = (PyArrayIterObject *)PyArray_IterNew(input);
if (iter == NULL) throw;
this->__init__(reinterpret_cast<storage_type*>(iter->dataptr),
grid_dims,
this->nb_components);
Py_DECREF(iter);
}
virtual ~GridPython() {}
};
__END_TAMAAS__
#endif // __GRID_PYTHON_HH__

Event Timeline