Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F85186188
numpy.hh
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Sep 27, 09:16
Size
2 KB
Mime Type
text/x-c++
Expires
Sun, Sep 29, 09:16 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
21137590
Attached To
rTAMAAS tamaas
numpy.hh
View Options
/**
* @file
* @section LICENSE
*
* Copyright (©) 2016-19 EPFL (École Polytechnique Fédérale de Lausanne),
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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;
/// Type alias for usual numpy array type
template <typename T>
using numpy = py::array_t<T, py::array::c_style | py::array::forcecast>;
/// GridBase helper class wrapping numpy array
template <typename T>
class GridBaseNumpy : public GridBase<T> {
public:
GridBaseNumpy(numpy<T>& buffer) : GridBase<T>() {
this->nb_components = 1;
this->data.wrapMemory(buffer.mutable_data(), buffer.size());
}
};
/// Grid helper class wrapping 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());
}
};
/// Surface helper class wrapping 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
Log In to Comment