diff --git a/python/dumux/assembly/__init__.py b/python/dumux/assembly/__init__.py index b9e9ddcd3a9c3fdedf863ddd3c9c2050974744cf..18f74dc55231e25c6845e025ab9c055a46d0ef77 100644 --- a/python/dumux/assembly/__init__.py +++ b/python/dumux/assembly/__init__.py @@ -1,3 +1,6 @@ +# pylint: skip-file +# until that decorator bit can be removed again + """Classes and function related to the assembly of linear systems""" from dune.generator.generator import SimpleGenerator @@ -5,6 +8,25 @@ from dune.common.hashit import hashIt from dumux.wrapping import cppWrapperCreator, cppWrapperClassAlias +def decoratePre(pre): + def wrappedPre(*args, **kwargs): + preamble = pre(*args, **kwargs) + newPreamble = "" + for line in preamble.split("\n"): + newPreamble += line + "\n" + if line.startswith("#include <config.h>"): + newPreamble += "#undef DUMUX_MULTITHREADING_BACKEND\n" + newPreamble += "#define DUMUX_MULTITHREADING_BACKEND Serial\n" + return newPreamble + + return wrappedPre + + +myAttributes = vars(SimpleGenerator).copy() +myAttributes["pre"] = decoratePre(myAttributes["pre"]) +MySimpleGenerator = type("MySimpleGenerator", (object,), myAttributes) + + @cppWrapperCreator def _createFVAssembler(*, problem, gridVariables, model, diffMethod="numeric", isImplicit=True): """ @@ -40,13 +62,20 @@ def _createFVAssembler(*, problem, gridVariables, model, diffMethod="numeric", i includes += ["dumux/python/assembly/fvassembler.hh"] moduleName = "fvassembler_" + hashIt(assemblerType) - generator = SimpleGenerator("FVAssembler", "Dumux::Python") + # remark: use SimpleGenerator again starting with dune 2.9 + generator = MySimpleGenerator("FVAssembler", "Dumux::Python") module = generator.load( includes, assemblerType, moduleName, holder="std::shared_ptr", preamble=model.cppHeader, + # make sure the assembler is compiled with the Serial backend + # as currently the assembly in combination with Python is not thread-safe + # the following is nicer but only works with dune > 2.8 + # extraCMake=[ + # "target_compile_definitions(TARGET PUBLIC DUMUX_MULTITHREADING_BACKEND=Serial)" + # ], ) return module.FVAssembler(problem, problem.gridGeometry(), gridVariables) diff --git a/python/dumux/common/CMakeLists.txt b/python/dumux/common/CMakeLists.txt index 65f0a6b1f590e515998bbf8fcb038b2c90ea9f88..21bad44ccf825f9063e6fdfac6fc8c2d58acfe09 100644 --- a/python/dumux/common/CMakeLists.txt +++ b/python/dumux/common/CMakeLists.txt @@ -2,7 +2,14 @@ add_python_targets(common __init__ properties ) -dune_add_pybind11_module(NAME _common) + +# currently the Python bindings only work with +# serial dumux code +dune_add_pybind11_module( + NAME _common + COMPILE_DEFINITIONS DUMUX_MULTITHREADING_BACKEND=Serial +) + set_property(TARGET _common PROPERTY LINK_LIBRARIES dunecommon dunegrid APPEND) if(SKBUILD)