Page MenuHomec4science

surface.i
No OneTemporary

File Metadata

Created
Tue, May 14, 12:43

surface.i

/**
*
* @author Guillaume Anciaux <guillaume.anciaux@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/>.
*
*/
/* -------------------------------------------------------------------------- */
%{
#include <complex>
#include "map_2d.hh"
#include "surface.hh"
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
#include "surface_generator_filter_fft.hh"
#include "smart_pointer.hh"
#include "surface_statistics.hh"
#include "bem_kato.hh"
#include "bem_polonski.hh"
#include "bem_penalty.hh"
#include "bem_gigi.hh"
#include "bem_gigipol.hh"
#include "bem_uzawa.hh"
#include "bem_fft_base.hh"
#include "bem_grid.hh"
#include "contact_area.hh"
#include "contact_cluster_collection.hh"
#include "functional.hh"
#include "meta_functional.hh"
#include "elastic_energy_functional.hh"
#include "complimentary_term_functional.hh"
#include "exponential_adhesion_functional.hh"
#include "maugis_adhesion_functional.hh"
#include "surface_timer.hh"
#include "fftransform_fftw.hh"
%}
%include "cpointer.i"
%include "typemaps.i"
%include "numpy.i"
%init %{
import_array();
%}
/* -------------------------------------------------------------------------- */
/* Wrapping returned pointers
/* -------------------------------------------------------------------------- */
//%include "typemaps.i"
%include "std_string.i"
%include "std_vector.i"
%include "smart_pointer.hh"
%template(VectorReal) std::vector<double>;
//%pointer_functions(int, intp);
//%pointer_functions(long, longp);
//%pointer_functions(unsigned int, uintp);
//%pointer_functions(double, doublep);
//%pointer_functions(float, floatp);
//%pointer_functions(std::complex<double>, comlpexp);
%define %my_pointer_class(NAME,TYPE)
%template(SmartPointer##NAME) SmartPointer< TYPE >;
%typemap(out) TYPE & {
#define TOREPLACE SWIGTYPE_p_SmartPointerT_##NAME##_t
SmartPointer<TYPE> * ptr = new SmartPointer<TYPE>($1);
$result = SWIG_NewPointerObj(SWIG_as_voidptr(ptr), TOREPLACE, 0 | 0 );
#undef TOREPLACE
}
%enddef
%my_pointer_class(int,int)
%my_pointer_class(double,double)
%my_pointer_class(long,long)
%my_pointer_class(vectorReal,std::vector<tamaas::Real>)
/* -------------------------------------------------------------------------- */
/* Wrapping Surface class */
/* -------------------------------------------------------------------------- */
%include "tamaas.hh"
%ignore tamaas::Grid::operator+=;
%ignore tamaas::Grid::operator*=;
%ignore tamaas::Grid::operator-=;
%ignore tamaas::Grid::operator/=;
%include "grid.hh"
namespace tamaas {
%template (Grid2dReal) Grid<Real, 2>;
%template (Grid2dComplex) Grid<complex, 2>;
%template (Grid2dInt) Grid<Int, 2>;
%template (Grid2dUInt) Grid<UInt, 2>;
}
%include "grid_hermitian.hh"
%include "map_2d.hh"
namespace tamaas {
%template(Map2dComplex) Map2d<complex>;
%template(Map2dInt) Map2d<int>;
%template(Map2dReal) Map2d<Real>;
}
%include "map_2d_square.hh"
namespace tamaas {
%template(Map2dSquareComplex) Map2dSquare<complex>;
%template(Map2dSquareInt) Map2dSquare<int>;
%template(Map2dSquareReal) Map2dSquare<Real>;
}
%include "surface.hh"
%include "surface_complex.hh"
namespace tamaas {
%template(SurfaceReal) Surface<Real>;
%template(SurfaceInt) Surface<int>;
%template(SurfaceRealComplex) SurfaceComplex<Real>;
}
%inline %{
__BEGIN_TAMAAS__
template <typename T, UInt d>
class GridForPython : public Grid<T, d> {
public:
GridForPython(T * wrapped_memory, UInt n[d], Real L[d], UInt nb_components):
Grid<T, d>() {
this->__init__(wrapped_memory, n, L);
}
void __init__(T * w, UInt n[d], UInt nb_components) {
memcpy(this->n, n, d * sizeof(UInt));
memset(this->L, 1, d * sizeof(Real));
this->nb_components = nb_components;
this->data.wrapMemory(w, this->computeSize());
}
GridForPython(PyObject * input): Grid<T, d>() {
if (!PyArray_Check(input)) {
PyObject* obj_type = PyObject_Type(input);
std::string type = PyString_AsString(obj_type);
throw Exception("GridForPython: 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
UInt grid_dims[d] = {0}; // tamaas dims
if (_n >= d) {
for (UInt i = 0 ; i < d ; ++i)
grid_dims[i] = ndims[i];
} else {
throw Exception("GridForPython: incompatible input size for numpy array: " + _n);
}
if (_n > d) this->nb_components = ndims[d+1];
else this->nb_components = 1;
PyArrayIterObject *iter = (PyArrayIterObject *)PyArray_IterNew(input);
if (iter == NULL) throw;
this->__init__(( T *)(iter->dataptr), grid_dims, this->nb_components);
Py_DECREF(iter);
}
virtual ~GridForPython() {}
};
__END_TAMAAS__
%}
%inline %{
__BEGIN_TAMAAS__
template <typename T>
class SurfaceForPython : public Surface<T>{
public:
SurfaceForPython(T * wrapped_memory,
UInt n,
Real L)
: Surface<T>(0,L){
this->__init__(wrapped_memory,n,L);
};
void __init__(T * wrapped_memory,
UInt n,
Real L){
this->n[0] = n;
this->n[1] = n;
this->data.wrapMemory(wrapped_memory,n*n);
}
SurfaceForPython(PyObject * input,Real L = 1.)
: Surface<T>(0,L){
if (!PyArray_Check(input)){
PyObject* obj_type = PyObject_Type(input);
std::string type = PyString_AsString(obj_type);
throw Exception("SurfaceForPython: incompatible input which is not a numpy: " + type);
}
UInt _n = PyArray_NDIM((PyArrayObject*)input);
if (_n != 2) SURFACE_FATAL("SurfaceForPython: incompatible numpy dimension " << _n);
npy_intp * ndims = PyArray_DIMS((PyArrayObject*)input);
UInt sz = ndims[0];
UInt sz2 = ndims[1];
if (sz != sz2) SURFACE_FATAL("SurfaceForPython: incompatible numpy dimensions " << sz << " " << sz2);
PyArrayIterObject *iter = (PyArrayIterObject *)PyArray_IterNew(input);
if (iter == NULL) throw;
this->__init__(( T *)(iter->dataptr),sz,L);
Py_DECREF(iter);
};
~SurfaceForPython(){
};
void resize(UInt new_size){
if (this->size() != new_size)
throw Exception("SurfaceForPython: cannot resize a temporary vector");
}
};
__END_TAMAAS__
%}
%define %grid_typemaps(GRID_CLASS, DATA_TYPE, DATA_TYPECODE, DIM)
%typemap(out, fragment="NumPy_Fragments") (GRID_CLASS &)
{
using namespace tamaas;
UInt additional_dim = ($1->getNbComponents() == 1)? 0:1;
UInt dim = DIM+additional_dim;
npy_intp dims[dim];
for (UInt i = 0 ; i < DIM ; i++) dims[i] = (npy_intp)$1->sizes()[i];
if (additional_dim)
dims[dim-1] = $1->getNbComponents();
DATA_TYPE * data = const_cast<DATA_TYPE *>($1->getInternalData());
PyObject* obj = PyArray_SimpleNewFromData(dim,
dims,
DATA_TYPECODE,
&data[0]);
PyArrayObject* array = (PyArrayObject*) obj;
if (!array) SWIG_fail;
$result = SWIG_Python_AppendOutput($result, obj);
}
%typemap(in) tamaas::Grid<DATA_TYPE, DIM> & {
using namespace tamaas;
if (!PyArray_Check($input)) {
PyObject* objectsRepresentation = PyObject_Repr(PyObject_Type($input));
const char* s = PyString_AsString(objectsRepresentation);
std::string __type = s;
if ("<class 'tamaas.GridForPython" + std::string("DATA_TYPE") + "'>" == __type) {
SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,0);
}
else if ("<class 'tamaas.Grid" + std::string("DATA_TYPE") + "'>" == __type) {
SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,0);
}
else throw tamaas::Exception("typemap: incompatible input which is not a proper type: " + __type + " (DATATYPE = " + std::string("DATA_TYPE") + ")");
} else {
$1 = new GridForPython< DATA_TYPE, DIM >($input);
}
}
%inline %{
namespace tamaas {
GRID_CLASS & convertGrid(GRID_CLASS & s){
return s;
}
}
%}
%enddef
%define %surface_typemaps(SURFACE_CLASS, DATA_TYPE, DATA_TYPECODE)
%typemap(out, fragment="NumPy_Fragments") (SURFACE_CLASS &)
{
using namespace tamaas;
UInt nz = $1->size();
npy_intp dims[2] = {nz,nz};
DATA_TYPE * data = const_cast<DATA_TYPE *>($1->getInternalData());
PyObject* obj = PyArray_SimpleNewFromData(2, dims, DATA_TYPECODE, &data[0]);
PyArrayObject* array = (PyArrayObject*) obj;
if (!array) SWIG_fail;
$result = SWIG_Python_AppendOutput($result, obj);
}
%typemap(in) tamaas::Surface< DATA_TYPE > & {
using namespace tamaas;
if (!PyArray_Check($input)){
PyObject* objectsRepresentation = PyObject_Repr(PyObject_Type($input));
const char* s = PyString_AsString(objectsRepresentation);
std::string __type = s;
if ("<class 'tamaas.SurfaceForPython" + std::string("DATA_TYPE") + "'>" == __type) {
SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,0);
}
else if ("<class 'tamaas.Surface" + std::string("DATA_TYPE") + "'>" == __type) {
SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,0);
}
else if ("<class 'tamaas.SurfaceComplex" + std::string("DATA_TYPE") + "'>" == __type) {
SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,0);
}
else throw tamaas::Exception("typemap: incompatible input which is not a proper type: " + __type + " (DATATYPE = " + std::string("DATA_TYPE") + ")");
}
else{
UInt _n = PyArray_NDIM((PyArrayObject*)$input);
if (_n != 2) SURFACE_FATAL("incompatible numpy dimension " << _n);
npy_intp * ndims = PyArray_DIMS((PyArrayObject*)$input);
UInt sz = ndims[0];
UInt sz2 = ndims[1];
if (sz != sz2) SURFACE_FATAL("incompatible numpy dimensions " << sz << " " << sz2);
PyArrayIterObject *iter = (PyArrayIterObject *)PyArray_IterNew($input);
if (iter == NULL) throw;
$1 = new SurfaceForPython< DATA_TYPE >(( DATA_TYPE *)(iter->dataptr),sz,1.);
PyArray_ITER_NEXT(iter);
}
}
%inline %{
namespace tamaas {
SURFACE_CLASS & convertSurface(SURFACE_CLASS & s){
return s;
}
}
%}
%enddef
%surface_typemaps(tamaas::Surface<Real>, Real, NPY_DOUBLE)
%surface_typemaps(tamaas::SurfaceComplex<Real>, std::complex<Real>, NPY_COMPLEX128 )
%surface_typemaps(tamaas::Surface<unsigned long>, unsigned long, NPY_UINT )
%surface_typemaps(tamaas::Surface<UInt>, UInt, NPY_UINT )
%surface_typemaps(tamaas::Surface<int>, int, NPY_INT )
%grid_typemaps(tamaas::Grid2dReal, Real, NPY_DOUBLE, 2)
%grid_typemaps(tamaas::Grid2dComplex, tamaas::complex, NPY_COMPLEX128, 2)
%grid_typemaps(tamaas::Grid2dUInt, UInt, NPY_UINT, 2)
%grid_typemaps(tamaas::SurfaceComplex<Real>, tamaas::complex, NPY_COMPLEX128, 2)
namespace tamaas {
%template(SurfaceForPythonReal) SurfaceForPython< Real >;
}
%include "surface_timer.hh"

Event Timeline