Page MenuHomec4science

surface.i
No OneTemporary

File Metadata

Created
Tue, Jun 4, 01:44

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 <set>
#include "surface.hh"
#include "types.hh"
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
#include <boost/preprocessor/seq.hpp>
#include "surface_generator_filter_fft.hh"
#include "smart_pointer.hh"
#include "surface_statistics.hh"
#include "statistics.hh"
#include "fftransform_fftw.hh"
#include "flood_fill.hh"
#include "model.hh"
#include "model_factory.hh"
#include "model_type.hh"
#include "contact_solver.hh"
#include "polonsky_keer_rey.hh"
#include "bem_interface.hh"
#include "bem_fft_base.hh"
#include "bem_kato.hh"
#include "bem_polonski.hh"
#include "bem_gigi.hh"
#include "bem_penalty.hh"
#include "bem_uzawa.hh"
#include "bem_gigipol.hh"
#include "bem_grid.hh"
#include "bem_grid_kato.hh"
#include "bem_grid_polonski.hh"
#include "bem_grid_teboulle.hh"
#include "bem_grid_condat.hh"
#include "bem_functional.hh"
#include "bem_meta_functional.hh"
#include "elastic_energy_functional.hh"
#include "complimentary_term_functional.hh"
#include "exponential_adhesion_functional.hh"
#include "squared_exponential_adhesion_functional.hh"
#include "maugis_adhesion_functional.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>;
%template(VectorUInt) std::vector<unsigned int>;
//%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(unsigned_int,tamaas::UInt)
%my_pointer_class(vectorReal,std::vector<tamaas::Real>)
%my_pointer_class(vectorUInt,std::vector<tamaas::UInt>)
%apply (unsigned int IN_ARRAY1[ANY]) {(tamaas::UInt n[ANY])};
/* -------------------------------------------------------------------------- */
/* Wrapping Surface class */
/* -------------------------------------------------------------------------- */
%include "tamaas.hh"
%ignore *::operator=;
%ignore *::operator++;
%ignore *::begin;
%ignore *::end;
%ignore GridBase;
%ignore Grid;
%ignore GridHermitian;
%ignore Surface;
%ignore SurfaceComplex;
%ignore GridBase::iterator;
%include "grid_base.hh"
%include "grid.hh"
%include "grid_hermitian.hh"
%include "types.hh"
%include "surface.hh"
%include "surface_complex.hh"
%inline %{
__BEGIN_TAMAAS__
template <int required_type>
void sanityCheck(PyObject * input) {
if (PyArray_TYPE((PyArrayObject*)input) != required_type)
throw Exception("Sanity check: python object stores wrong type");
}
__END_TAMAAS__
%}
%{
#include "python/grid_python.hh"
#include "python/surface_python.hh"
%}
%define %grid_typemaps(GRID_CLASS, DATA_TYPE, DATA_TYPECODE, DIM)
// Typemap for returned references
%typemap(out, fragment="NumPy_Fragments") (GRID_CLASS<DATA_TYPE, DIM> &)
{
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();
GRID_CLASS<DATA_TYPE, DIM>::value_type * data
= const_cast<GRID_CLASS<DATA_TYPE, DIM>::value_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 for returned objects as values
%typemap(out, fragment="NumPy_Fragments") (GRID_CLASS<DATA_TYPE, DIM>)
{
using namespace tamaas;
auto * one = new GRID_CLASS<DATA_TYPE, DIM>($1); // We have to copy :(
UInt additional_dim = (one->getNbComponents() == 1)? 0:1;
UInt dim = DIM+additional_dim;
npy_intp dims[dim];
for (UInt i = 0 ; i < DIM ; i++) dims[i] = (npy_intp)one->sizes()[i];
if (additional_dim)
dims[dim-1] = one->getNbComponents();
GRID_CLASS<DATA_TYPE, DIM>::value_type * data
= const_cast<GRID_CLASS<DATA_TYPE, DIM>::value_type *>(one->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) GRID_CLASS<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;
// Remove namespace (tamaas::) from class name
std::string basename = std::string("GRID_CLASS").substr(8);
std::set<std::string> types = {
"<class 'tamaas.GridPython" + std::string("DATA_TYPE") + "'>",
"<class 'tamaas."+ basename + std::string("DIM") + "d" + std::string("DATA_TYPE") + "'>"
};
if (types.find(__type) != types.end()) {
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 tamaas::GridPython< GRID_CLASS<DATA_TYPE, DIM> >($input);
sanityCheck< DATA_TYPECODE >($input);
}
}
%inline %{
namespace tamaas {
GRID_CLASS<DATA_TYPE, DIM> & convertGrid(GRID_CLASS<DATA_TYPE, DIM> & 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{
$1 = new SurfaceForPython< DATA_TYPE >($input);
sanityCheck< DATA_TYPECODE >($input);
}
}
%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>, tamaas::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 )
// Must use c-type names here for some reason
%grid_typemaps(tamaas::Grid, double, NPY_DOUBLE, 1)
%grid_typemaps(tamaas::Grid, double, NPY_DOUBLE, 2)
%grid_typemaps(tamaas::Grid, bool, NPY_BOOL, 2)
%grid_typemaps(tamaas::GridHermitian, double, NPY_COMPLEX128, 2)
%grid_typemaps(tamaas::Grid, unsigned int, NPY_UINT, 2)
// Typemaps for GridBase output
%define GRID_BASE_MACRO(GRID_CLASS, DATA_TYPE, DATA_TYPECODE, DIM)
if (GRID_CLASS<DATA_TYPE, DIM> * grid = dynamic_cast<decltype(grid)>($1)) {
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)grid->sizes()[i];
if (additional_dim)
dims[dim-1] = $1->getNbComponents();
GRID_CLASS<DATA_TYPE, DIM>::value_type * data
= const_cast<GRID_CLASS<DATA_TYPE, DIM>::value_type *>(grid->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);
}
%enddef
%define %gridbase_typemaps(GRID_CLASS, DATA_TYPE, DATA_TYPECODE)
%typemap(out, fragment="NumPy_Fragments") (tamaas::GridBase<DATA_TYPE> &) {
using namespace tamaas;
GRID_BASE_MACRO(GRID_CLASS, DATA_TYPE, DATA_TYPECODE, 1);
GRID_BASE_MACRO(GRID_CLASS, DATA_TYPE, DATA_TYPECODE, 2);
GRID_BASE_MACRO(GRID_CLASS, DATA_TYPE, DATA_TYPECODE, 3);
}
%typemap(in) tamaas::GridBase<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;
// Remove namespace (tamaas::) from class name
std::string basename = std::string("GRID_CLASS").substr(8);
std::set<std::string> types = {
"<class 'tamaas.GridPython" + std::string("DATA_TYPE") + "'>",
"<class 'tamaas."+ basename + std::string("DIM") + "d" + std::string("DATA_TYPE") + "'>"
};
if (types.find(__type) != types.end()) {
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 {
int _n = PyArray_NDIM((PyArrayObject*)$input);
switch(_n) {
case 1:
$1 = new tamaas::GridPython< GRID_CLASS<DATA_TYPE, 1> >($input);
break;
case 2:
$1 = new tamaas::GridPython< GRID_CLASS<DATA_TYPE, 2> >($input);
break;
default:
throw tamaas::Exception("typemap (GridBase): dimensions higher than 2 not supported. This typemap is designed for surfaces");
}
sanityCheck< DATA_TYPECODE >($input);
}
}
%enddef
%gridbase_typemaps(tamaas::Grid, double, NPY_DOUBLE);

Event Timeline