Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F84027326
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 20, 08:21
Size
3 KB
Mime Type
text/x-c++
Expires
Sun, Sep 22, 08:21 (2 d)
Engine
blob
Format
Raw Data
Handle
20202567
Attached To
rTAMAAS tamaas
numpy.hh
View Options
/**
* @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
;
/// 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