WrappedArray.h
Go to the documentation of this file.
/*****************************************************************************
*
* Copyright (c) 2003-2020 by The University of Queensland
* http://www.uq.edu.au
*
* Primary Business: Queensland, Australia
* Licensed under the Apache License, version 2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Development until 2012 by Earth Systems Science Computational Center (ESSCC)
* Development 2012-2013 by School of Earth Sciences
* Development from 2014-2017 by Centre for Geoscience Computing (GeoComp)
* Development from 2019 by School of Earth and Environmental Sciences
**
*****************************************************************************/
#ifndef WrappedArray_20081202_H
#define WrappedArray_20081202_H
#include "system_dep.h"
#include "DataTypes.h"
#include "boost/python/extract.hpp"
#include "boost/python/object.hpp"
#include <complex>
namespace escript
{
class WrappedArray
{
public:
WrappedArray(const boost::python::object& obj_in);
~WrappedArray();
unsigned int getRank() const;
const DataTypes::ShapeType& getShape() const;
bool isComplex() const;
DataTypes::real_t getElt() const;
DataTypes::real_t getElt(unsigned int i) const;
DataTypes::real_t getElt(unsigned int i, unsigned int j) const;
DataTypes::real_t getElt(unsigned int i, unsigned int j, unsigned int k) const;
DataTypes::real_t getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const;
DataTypes::cplx_t getEltC() const;
DataTypes::cplx_t getEltC(unsigned int i) const;
DataTypes::cplx_t getEltC(unsigned int i, unsigned int j) const;
DataTypes::cplx_t getEltC(unsigned int i, unsigned int j, unsigned int k) const;
DataTypes::cplx_t getEltC(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const;
void convertArray() const;
private:
void convertArrayR() const;
void convertArrayC() const;
template<typename T> void convertNumpyArray(const T* array, const std::vector<int>& strides) const;
template<typename T> void convertNumpyArrayC(const T* array, const std::vector<int>& strides) const;
const boost::python::object& obj;
int rank;
mutable bool converted; // has the array been converted to a C array
bool iscomplex; // is the wrapped array storing complex values?
escript::DataTypes::ShapeType shape;
DataTypes::real_t scalar_r;
DataTypes::cplx_t scalar_c;
mutable DataTypes::real_t* dat_r; // real data
mutable DataTypes::cplx_t* dat_c; // complex data - only one of these members should be used
};
inline bool WrappedArray::isComplex() const
{
return iscomplex;
}
inline unsigned int
WrappedArray::getRank() const
{
return rank;
}
inline const DataTypes::ShapeType&
WrappedArray::getShape() const
{
return shape;
}
inline DataTypes::real_t
WrappedArray::getElt() const
{
if (iscomplex)
{
return nan("");
}
return scalar_r;
}
inline DataTypes::real_t
WrappedArray::getElt(unsigned int i) const
{ // __float__ added to deal with numpy. If this causes problems we may have to register a custom converter
if (iscomplex)
{
return nan("");
}
return (dat_r!=0)?dat_r[i]:(boost::python::extract<DataTypes::real_t>(obj[i].attr("__float__")()));
}
inline
DataTypes::real_t
WrappedArray::getElt(unsigned int i, unsigned int j) const
{
if (iscomplex)
{
return nan("");
}
return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<DataTypes::real_t>(obj[i][j].attr("__float__")()));
}
inline
DataTypes::real_t
WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k) const
{
if (iscomplex)
{
return nan("");
}
return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<DataTypes::real_t>(obj[i][j][k].attr("__float__")()));
}
inline
DataTypes::real_t
WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const
{
if (iscomplex)
{
return nan("");
}
return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<DataTypes::real_t>(obj[i][j][k][m].attr("__float__")()));
}
inline DataTypes::cplx_t
WrappedArray::getEltC() const
{
if (!iscomplex)
{
return scalar_r;
}
return scalar_c;
}
inline DataTypes::cplx_t
WrappedArray::getEltC(unsigned int i) const
{
if (!iscomplex) // let's try to get a real value out instead
{
return (dat_r!=0)?dat_r[i]:(boost::python::extract<DataTypes::real_t>(obj[i]));
}
return (dat_c!=0)?dat_c[i]:(boost::python::extract<DataTypes::cplx_t>(obj[i])); // don't know if this will work with numpy
}
inline
DataTypes::cplx_t
WrappedArray::getEltC(unsigned int i, unsigned int j) const
{
if (!iscomplex)
{
return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<DataTypes::real_t>(obj[i][j]));
}
return (dat_c!=0)?dat_c[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<DataTypes::cplx_t>(obj[i][j]));
}
inline
DataTypes::cplx_t
WrappedArray::getEltC(unsigned int i, unsigned int j, unsigned int k) const
{
if (!iscomplex)
{
return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<DataTypes::real_t>(obj[i][j][k]));
}
return (dat_c!=0)?dat_c[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<DataTypes::cplx_t>(obj[i][j][k]));
}
inline
DataTypes::cplx_t
WrappedArray::getEltC(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const
{
if (!iscomplex)
{
return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<DataTypes::real_t>(obj[i][j][k][m]));
}
return (dat_c!=0)?dat_c[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<DataTypes::cplx_t>(obj[i][j][k][m]));
}
}
#endif
escript::WrappedArray::dat_r
DataTypes::real_t * dat_r
Definition: WrappedArray.h:66
escript::WrappedArray::convertNumpyArrayC
void convertNumpyArrayC(const T *array, const std::vector< int > &strides) const
Definition: WrappedArray.cpp:300
escript::DataTypes::real_t
double real_t
type of all real-valued scalars in escript
Definition: DataTypes.h:79
escript::WrappedArray::getEltC
DataTypes::cplx_t getEltC() const
Definition: WrappedArray.h:147
escript::WrappedArray::shape
escript::DataTypes::ShapeType shape
Definition: WrappedArray.h:63
escript::WrappedArray::getElt
DataTypes::real_t getElt() const
Definition: WrappedArray.h:89
escript::WrappedArray::dat_c
DataTypes::cplx_t * dat_c
Definition: WrappedArray.h:67
escript::WrappedArray
Definition: WrappedArray.h:32
escript::WrappedArray::isComplex
bool isComplex() const
Definition: WrappedArray.h:71
ESCRIPT_MAX_DATA_RANK
#define ESCRIPT_MAX_DATA_RANK
Definition: Data.h:39
escript::WrappedArray::converted
bool converted
Definition: WrappedArray.h:61
system_dep.h
escript::WrappedArray::WrappedArray
WrappedArray(const boost::python::object &obj_in)
Definition: WrappedArray.cpp:153
escript::WrappedArray::rank
int rank
Definition: WrappedArray.h:60
escript::WrappedArray::getRank
unsigned int getRank() const
Definition: WrappedArray.h:77
escript::WrappedArray::convertArrayR
void convertArrayR() const
Definition: WrappedArray.cpp:417
escript::DataException
Definition: DataException.h:41
escript::DataTypes::ShapeType
std::vector< int > ShapeType
The shape of a single datapoint.
Definition: DataTypes.h:71
escript::WrappedArray::~WrappedArray
~WrappedArray()
Definition: WrappedArray.cpp:543
escript::WrappedArray::convertNumpyArray
void convertNumpyArray(const T *array, const std::vector< int > &strides) const
Definition: WrappedArray.cpp:359
escript::DataTypes::noValues
int noValues(const ShapeType &shape)
Calculate the number of values in a datapoint with the given shape.
Definition: DataTypes.cpp:91
escript::WrappedArray::convertArray
void convertArray() const
Definition: WrappedArray.cpp:531
escript::WrappedArray::scalar_c
DataTypes::cplx_t scalar_c
Definition: WrappedArray.h:65
WrappedArray.h
escript::WrappedArray::convertArrayC
void convertArrayC() const
Definition: WrappedArray.cpp:474
escript::WrappedArray::iscomplex
bool iscomplex
Definition: WrappedArray.h:62
escript::WrappedArray::obj
const boost::python::object & obj
Definition: WrappedArray.h:59
escript
Definition: AbstractContinuousDomain.cpp:23
escript::DataTypes::getRelIndex
vec_size_type getRelIndex(const DataTypes::ShapeType &shape, vec_size_type i)
Compute the offset (in 1D vector) of a given subscript with a shape.
Definition: DataTypes.h:262
DataTypes.h
escript::WrappedArray::scalar_r
DataTypes::real_t scalar_r
Definition: WrappedArray.h:64
Data.h
escript::DataTypes::cplx_t
std::complex< real_t > cplx_t
complex data type
Definition: DataTypes.h:82
escript::WrappedArray::getShape
const DataTypes::ShapeType & getShape() const
Definition: WrappedArray.h:83
DataException.h