diff --git a/python/SConscript b/python/SConscript index 6998e2e..1870dc4 100644 --- a/python/SConscript +++ b/python/SConscript @@ -1,91 +1,93 @@ from __future__ import print_function from os.path import abspath, basename from distutils.version import StrictVersion from subprocess import check_output, STDOUT import re Import('main_env') env_swig = main_env.Clone( tools=['swig'], SHLIBPREFIX='', ) if env_swig['SWIGVERSION'] is None: print("Could not find swig version") Exit(1) if StrictVersion(env_swig['SWIGVERSION']) < StrictVersion("3.0"): print("Swig version {} is not supported by tamaas".format(env_swig['SWIGVERSION'])) Exit(1) # Check user's prefered version version_out = check_output([main_env['py_exec'], "--version"], stderr=STDOUT, universal_newlines=True).split(' ') # Matching version version_re = re.compile(r"^\d(\.\d+)*") version_string = version_re.match(version_out[-1]).group() # Verifying version version = StrictVersion(version_string) python3 = version >= StrictVersion("3.0") print("Building wrapper for python version {}".format(version)) # Run code below to get versions from user preference and not scons versions_script = """ from __future__ import print_function import distutils.sysconfig as dsys from numpy.distutils.misc_util import get_numpy_include_dirs as numpy_dirs print(dsys.get_python_inc()) for d in numpy_dirs(): print(d)""" includes = check_output([main_env['py_exec'], "-c", versions_script], universal_newlines=True).split('\n') env_swig.AppendUnique(CPPPATH=includes) env_swig.AppendUnique(SWIGPATH=['$CPPPATH']) env_swig.AppendUnique(SWIGFLAGS=['-python', '-c++']) if python3: env_swig.AppendUnique(SWIGFLAGS=['-py3']) verbose = main_env['verbose'] if not verbose: env_swig.AppendUnique(SWIGFLAGS=['-w325', '-w315']) if basename(env_swig['CXX']) != "clang++": # for some warning in wrapper code env_swig.AppendUnique(CXXFLAGS=['-Wno-maybe-uninitialized']) else: env_swig.AppendUnique(CXXFLAGS=['-Wno-uninitialized', '-Wno-dynamic-class-memaccess']) # env_swig.SharedLibrary( # target='_tamaas.so', # source=['tamaas.i'], # LIBS=['Tamaas'], # RPATH=[abspath('../src')] # ) # Pybind11 wrapper env_pybind = main_env.Clone(SHLIBPREFIX='') includes += [Dir('#third-party/pybind11/include')] extension = check_output([main_env['py_exec'] + '-config', "--extension-suffix"], universal_newlines=True).split('\n')[0] env_pybind.AppendUnique(CPPPATH=includes) pybind_sources = Split(""" tamaas_module.cpp wrap/core.cpp wrap/percolation.cpp wrap/surface.cpp +wrap/model.cpp +wrap/solvers.cpp """) env_pybind.SharedLibrary( target='tamaas' + extension, source=pybind_sources, LIBS=['Tamaas'], RPATH=[abspath('../src')] ) diff --git a/python/wrap.hh b/python/cast.hh similarity index 64% copy from python/wrap.hh copy to python/cast.hh index a137b7a..fa1ddcb 100644 --- a/python/wrap.hh +++ b/python/cast.hh @@ -1,207 +1,195 @@ /** * @file * * @author Lucas Frérot * * @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 . * */ /* -------------------------------------------------------------------------- */ -#ifndef __WRAP_HH__ -#define __WRAP_HH__ +#ifndef __CAST_HH__ +#define __CAST_HH__ /* -------------------------------------------------------------------------- */ -#include "grid.hh" +#include "grid_base.hh" #include "surface.hh" -#include "tamaas.hh" +#include "numpy.hh" /* -------------------------------------------------------------------------- */ -#include -// keep order #include -#include -/* -------------------------------------------------------------------------- */ - -namespace tamaas { -namespace wrap { -namespace py = pybind11; - -template -using numpy = py::array_t; - -/// Class wrapping a numpy array -template -class GridNumpy : public Parent { -public: - GridNumpy(numpy& 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()); - } -}; - -/// Class wrapping a numpy array -template -class SurfaceNumpy : public Parent { -public: - SurfaceNumpy(numpy& 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 -} // namespace tamaas - +#include /* -------------------------------------------------------------------------- */ namespace pybind11 { namespace detail { /** * Type caster for grid classes * inspired by https://tinyurl.com/y8m47qh3 from T. De Geus * and pybind11/eigen.h */ template