diff --git a/language_bindings/python/CMakeLists.txt b/language_bindings/python/CMakeLists.txt index 6787acf..fcf0059 100644 --- a/language_bindings/python/CMakeLists.txt +++ b/language_bindings/python/CMakeLists.txt @@ -1,41 +1,42 @@ #============================================================================== # file CMakeLists.txt # # @author Till Junge # # @date 08 Jan 2018 # # @brief configuration for python binding using pybind11 # # @section LICENSE # # Copyright © 2018 Till Junge # # µSpectre is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation, either version 3, or (at # your option) any later version. # # µSpectre 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 # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with GNU Emacs; see the file COPYING. If not, write to the # Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. # set (PY_BINDING_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/bind_py_module.cc ${CMAKE_CURRENT_SOURCE_DIR}/bind_py_common.cc ${CMAKE_CURRENT_SOURCE_DIR}/bind_py_system.cc ${CMAKE_CURRENT_SOURCE_DIR}/bind_py_material.cc ${CMAKE_CURRENT_SOURCE_DIR}/bind_py_solvers.cc ${CMAKE_CURRENT_SOURCE_DIR}/bind_py_fftengine.cc + ${CMAKE_CURRENT_SOURCE_DIR}/bind_py_projections.cc ) pybind11_add_module(pyMuSpectre ${PY_BINDING_SRCS}) target_link_libraries(pyMuSpectre PRIVATE muSpectre) diff --git a/language_bindings/python/bind_py_declarations.hh b/language_bindings/python/bind_py_declarations.hh index 616da57..70a70d6 100644 --- a/language_bindings/python/bind_py_declarations.hh +++ b/language_bindings/python/bind_py_declarations.hh @@ -1,38 +1,43 @@ /** * file bind_py_common.hh * * @author Till Junge * * @date 12 Jan 2018 * * @brief header for python bindings for the common part of µSpectre * * @section LICENSE * * Copyright © 2018 Till Junge * * µSpectre is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3, or (at * your option) any later version. * * µSpectre 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 * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU Emacs; see the file COPYING. If not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ +#ifndef BIND_PY_DECLARATIONS_H +#define BIND_PY_DECLARATIONS_H + #include namespace py = pybind11; - void add_common(py::module & mod); void add_system(py::module & mod); void add_material(py::module & mod); void add_solvers(py::module & mod); void add_fft_engines(py::module & mod); +void add_projections(py::module & submodule); + +#endif /* BIND_PY_DECLARATIONS_H */ diff --git a/language_bindings/python/bind_py_fftengine.cc b/language_bindings/python/bind_py_fftengine.cc index 118e512..c0d3bbe 100644 --- a/language_bindings/python/bind_py_fftengine.cc +++ b/language_bindings/python/bind_py_fftengine.cc @@ -1,86 +1,88 @@ /** * file bind_py_fftengine.cc * * @author Till Junge * * @date 17 Jan 2018 * * @brief Python bindings for the FFT engines * * @section LICENSE * * Copyright © 2018 Till Junge * * µSpectre is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3, or (at * your option) any later version. * * µSpectre 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 * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU Emacs; see the file COPYING. If not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #include "fft/fftw_engine.hh" +#include "bind_py_declarations.hh" #include #include #include using namespace muSpectre; namespace py=pybind11; using namespace pybind11::literals; template void add_engine_helper(py::module & mod, std::string name) { using Ccoord = Ccoord_t; using Rcoord = Rcoord_t; using ArrayXXc = Eigen::Array; py::class_(mod, name.c_str()) .def(py::init()) .def("fft", [](Engine & eng, py::EigenDRef v) { using Coll_t = typename Engine::GFieldCollection_t; using Field_t = typename Engine::Field_t; Coll_t coll{}; coll.initialise(eng.get_resolutions()); Field_t & temp{make_field("temp_field", coll)}; temp.eigen() = v; return ArrayXXc{eng.fft(temp).eigen()}; }, "array"_a) .def("ifft", [](Engine & eng, py::EigenDRef v) { using Coll_t = typename Engine::GFieldCollection_t; using Field_t = typename Engine::Field_t; Coll_t coll{}; coll.initialise(eng.get_resolutions()); Field_t & temp{make_field("temp_field", coll)}; eng.get_work_space().eigen()=v; eng.ifft(temp); return Eigen::ArrayXXd{temp.eigen()}; }, "array"_a) .def("initialise", &Engine::initialise, "flags"_a=FFT_PlanFlags::estimate) .def("normalisation", &Engine::normalisation); } void add_engine(py::module & mod) { add_engine_helper< twoD, FFTW_Engine< twoD, twoD>>(mod, "FFTW_2d"); add_engine_helper>(mod, "FFTW_3d"); } void add_fft_engines(py::module & mod) { auto fft{mod.def_submodule("fft")}; fft.doc() = "bindings for µSpectre's fft engines"; add_engine(fft); + add_projections(fft); } diff --git a/language_bindings/python/bind_py_projections.cc b/language_bindings/python/bind_py_projections.cc new file mode 100644 index 0000000..713a13a --- /dev/null +++ b/language_bindings/python/bind_py_projections.cc @@ -0,0 +1,136 @@ +/** + * file bind_py_projections.cc + * + * @author Till Junge + * + * @date 18 Jan 2018 + * + * @brief Python bindings for the Projection operators + * + * @section LICENSE + * + * Copyright © 2018 Till Junge + * + * µSpectre is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3, or (at + * your option) any later version. + * + * µSpectre 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Emacs; see the file COPYING. If not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "fft/projection_small_strain.hh" +#include "fft/projection_finite_strain.hh" +#include "fft/projection_finite_strain_fast.hh" + +#include "fft/fftw_engine.hh" + +#include +#include +#include + +#include +#include + +using namespace muSpectre; +namespace py=pybind11; +using namespace pybind11::literals; + +/** + * "Trampoline" class for handling the pure virtual methods, see + * [http://pybind11.readthedocs.io/en/stable/advanced/classes.html#overriding-virtual-functions-in-python] + * for details + */ +template +class PyProjectionBase: public ProjectionBase { +public: + using Parent = ProjectionBase; + using Field_t = typename Parent::Field_t; + + void apply_projection(Field_t & field) override { + PYBIND11_OVERLOAD_PURE + (void, + Parent, + apply_projection, + field + ); + } + + Eigen::Map get_operator() override { + PYBIND11_OVERLOAD_PURE + (Eigen::Map, + Parent, + get_operator + ); + } +}; + +template +void add_proj_helper(py::module & mod, std::string name_start) { + using Ccoord = Ccoord_t; + using Rcoord = Rcoord_t; + using Engine = FFTW_Engine; + using Field_t = typename Proj::Field_t; + + static_assert(DimS == DimM, + "currently only for DimS==DimM"); + + std::stringstream name{}; + name << name_start << DimS << "d"; + + py::class_(mod, name.str().c_str()) + .def(py::init([](Ccoord res, Rcoord lengths) { + auto engine = std::make_unique(res, lengths); + return Proj(std::move(engine)); + })) + .def("initialise", &Proj::initialise, + "flags"_a=FFT_PlanFlags::estimate, + "initialises the fft engine (plan the transform)") + .def("apply_projection", + [](Proj & proj, py::EigenDRef v){ + typename Engine::GFieldCollection_t coll{}; + coll.initialise(proj.get_resolutions()); + Field_t & temp{make_field("temp_field", coll)}; + temp.eigen() = v; + proj.apply_projection(temp); + return Eigen::ArrayXXd{temp.eigen()}; + }) + .def("get_operator", &Proj::get_operator); +} + +void add_proj_dispatcher(py::module & mod) { + add_proj_helper< + ProjectionSmallStrain< twoD, twoD>, + twoD>(mod, "ProjectionSmallStrain"); + add_proj_helper< + ProjectionSmallStrain, + threeD>(mod, "ProjectionSmallStrain"); + + add_proj_helper< + ProjectionFiniteStrain< twoD, twoD>, + twoD>(mod, "ProjectionFiniteStrain"); + add_proj_helper< + ProjectionFiniteStrain, + threeD>(mod, "ProjectionFiniteStrain"); + + add_proj_helper< + ProjectionFiniteStrainFast< twoD, twoD>, + twoD>(mod, "ProjectionFiniteStrainFast"); + add_proj_helper< + ProjectionFiniteStrainFast, + threeD>(mod, "ProjectionFiniteStrainFast"); + +} + +void add_projections(py::module & mod) { + add_proj_dispatcher(mod); + +} diff --git a/tests/python_binding_tests.py b/tests/python_binding_tests.py index f4e86a9..4b3a5f5 100755 --- a/tests/python_binding_tests.py +++ b/tests/python_binding_tests.py @@ -1,105 +1,106 @@ #!/usr/bin/env python3 """ file python_binding_tests.py @author Till Junge @date 09 Jan 2018 @brief Unit tests for python bindings @section LICENCE Copyright © 2018 Till Junge µSpectre is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3, or (at your option) any later version. µSpectre 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 General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Emacs; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """ import unittest import numpy as np from python_test_imports import µ from python_fft_tests import FFT_Check +from python_projection_tests import * class SystemCheck(unittest.TestCase): def test_Construction(self): """ Simple check for system constructors """ resolution = [5,7] lengths = [5.2, 8.3] formulation = µ.Formulation.small_strain try: sys = µ.SystemFactory(resolution, lengths, formulation) mat = µ.material.MaterialHooke2d.make(sys, "material", 210e9, .33) except Exception as err: print(err) raise err class MaterialHooke2dCheck(unittest.TestCase): def setUp(self): self.resolution = [5,7] self.lengths = [5.2, 8.3] self.formulation = µ.Formulation.small_strain self.sys = µ.SystemFactory(self.resolution, self.lengths, self.formulation) self.mat = µ.material.MaterialHooke2d.make( self.sys, "material", 210e9, .33) def test_add_material(self): self.mat.add_pixel([2,1]) class SolverCheck(unittest.TestCase): def setUp(self): self.resolution = [3, 3]#[5,7] self.lengths = [3., 3.]#[5.2, 8.3] self.formulation = µ.Formulation.finite_strain self.sys = µ.SystemFactory(self.resolution, self.lengths, self.formulation) self.hard = µ.material.MaterialHooke2d.make( self.sys, "hard", 210e9, .33) self.soft = µ.material.MaterialHooke2d.make( self.sys, "soft", 70e9, .33) def test_solve(self): for i, pixel in enumerate(self.sys): if i < 3: self.hard.add_pixel(pixel) else: self.soft.add_pixel(pixel) self.sys.initialise() tol = 1e-6 Del0 = np.array([[0, .1], [0, 0]]) maxiter = 100 verbose = 0 # the following segfaults: r = µ.solvers.de_geus(self.sys, Del0, tol, tol, maxiter, verbose) #print(r) if __name__ == '__main__': unittest.main() diff --git a/tests/python_projection_tests.py b/tests/python_projection_tests.py new file mode 100644 index 0000000..aa686d2 --- /dev/null +++ b/tests/python_projection_tests.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# -*- coding:utf-8 -*- +""" +file python_projection_tests.py + +@author Till Junge + +@date 18 Jan 2018 + +@brief compare µSpectre's projection operators to GooseFFT + +@section LICENSE + +Copyright © 2018 Till Junge + +µSpectre is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as +published by the Free Software Foundation, either version 3, or (at +your option) any later version. + +µSpectre 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Emacs; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. +""" + +import unittest +import numpy as np + +from python_test_imports import µ + +def build_test_classes(Proj, name): + class Checker(unittest.TestCase): + def test_failure(self): + self.assertTrue(Proj) + Checker.__name__ = name + return Checker + +trueClass = build_test_classes(True, "trueClass") +falseClass= build_test_classes(True, "falseClass")