Skip to content
Snippets Groups Projects
Commit 0c3995c0 authored by Christoph Lehmann's avatar Christoph Lehmann
Browse files

[PL] extracted Python BC cmake target

parent 704adf47
No related branches found
No related tags found
No related merge requests found
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
#include "PhaseFieldIrreversibleDamageOracleBoundaryCondition.h" #include "PhaseFieldIrreversibleDamageOracleBoundaryCondition.h"
#include "RobinBoundaryCondition.h" #include "RobinBoundaryCondition.h"
#ifdef OGS_USE_PYTHON #ifdef OGS_USE_PYTHON
#include "PythonBoundaryCondition.h" #include "Python/PythonBoundaryCondition.h"
#endif #endif
namespace ProcessLib namespace ProcessLib
......
add_library(ProcessLibBoundaryConditionPython
PythonBoundaryCondition.cpp
PythonBoundaryCondition.h
PythonBoundaryConditionLocalAssembler.h
PythonBoundaryConditionPythonSideInterface.h)
target_compile_definitions(ProcessLibBoundaryConditionPython
PUBLIC OGS_USE_PYTHON)
target_link_libraries(ProcessLibBoundaryConditionPython
PUBLIC BaseLib MathLib MeshLib NumLib logog
PRIVATE pybind11::pybind11)
# For the embedded Python module
add_library(ProcessLibBoundaryConditionPythonModule
PythonBoundaryConditionModule.cpp
PythonBoundaryConditionModule.h)
target_link_libraries(ProcessLibBoundaryConditionPythonModule
PUBLIC
ProcessLibBoundaryConditionPython
pybind11::pybind11)
...@@ -7,11 +7,9 @@ ...@@ -7,11 +7,9 @@
* *
*/ */
#ifdef OGS_USE_PYTHON
#include "PythonBoundaryCondition.h" #include "PythonBoundaryCondition.h"
#include <pybind11/eval.h> #include <pybind11/pybind11.h>
#include <iostream> #include <iostream>
#include "MeshLib/MeshSearch/NodeSearch.h" #include "MeshLib/MeshSearch/NodeSearch.h"
...@@ -83,6 +81,7 @@ void PythonBoundaryCondition::getEssentialBCValues( ...@@ -83,6 +81,7 @@ void PythonBoundaryCondition::getEssentialBCValues(
NumLib::IndexValueVector<GlobalIndexType>& bc_values) const NumLib::IndexValueVector<GlobalIndexType>& bc_values) const
{ {
FlushStdoutGuard guard(_flush_stdout); FlushStdoutGuard guard(_flush_stdout);
(void)guard;
auto const nodes = _bc_data.boundary_mesh.getNodes(); auto const nodes = _bc_data.boundary_mesh.getNodes();
...@@ -120,7 +119,7 @@ void PythonBoundaryCondition::getEssentialBCValues( ...@@ -120,7 +119,7 @@ void PythonBoundaryCondition::getEssentialBCValues(
if (dof_idx == NumLib::MeshComponentMap::nop) if (dof_idx == NumLib::MeshComponentMap::nop)
{ {
// TODO extend Python BC to mixed FEM // TODO extend Python BC to mixed FEM ansatz functions
OGS_FATAL( OGS_FATAL(
"No d.o.f. found for (node=%d, var=%d, comp=%d). " "No d.o.f. found for (node=%d, var=%d, comp=%d). "
"That might be due to the use of mixed FEM ansatz " "That might be due to the use of mixed FEM ansatz "
...@@ -146,14 +145,17 @@ void PythonBoundaryCondition::getEssentialBCValues( ...@@ -146,14 +145,17 @@ void PythonBoundaryCondition::getEssentialBCValues(
} }
if (!pair_flag_value.first) if (!pair_flag_value.first)
return; continue;
MeshLib::Location l(_bc_data.bulk_mesh_id, MeshLib::MeshItemType::Node, MeshLib::Location l(_bc_data.bulk_mesh_id, MeshLib::MeshItemType::Node,
bulk_node_id); bulk_node_id);
const auto dof_idx = _bc_data.dof_table_bulk.getGlobalIndex( const auto dof_idx = _bc_data.dof_table_bulk.getGlobalIndex(
l, _bc_data.global_component_id); l, _bc_data.global_component_id);
if (dof_idx == NumLib::MeshComponentMap::nop) if (dof_idx == NumLib::MeshComponentMap::nop)
continue; OGS_FATAL(
"Logic error. This error should already have occured while "
"gathering primary variables. Something nasty is going on!");
// For the DDC approach (e.g. with PETSc option), the negative // For the DDC approach (e.g. with PETSc option), the negative
// index of g_idx means that the entry by that index is a ghost // index of g_idx means that the entry by that index is a ghost
// one, which should be dropped. Especially for PETSc routines // one, which should be dropped. Especially for PETSc routines
...@@ -235,5 +237,3 @@ std::unique_ptr<PythonBoundaryCondition> createPythonBoundaryCondition( ...@@ -235,5 +237,3 @@ std::unique_ptr<PythonBoundaryCondition> createPythonBoundaryCondition(
} }
} // namespace ProcessLib } // namespace ProcessLib
#endif // OGS_USE_PYTHON
...@@ -9,19 +9,15 @@ ...@@ -9,19 +9,15 @@
#pragma once #pragma once
#ifdef OGS_USE_PYTHON
#include <pybind11/pybind11.h>
#include "BoundaryCondition.h"
#include "NumLib/DOF/LocalToGlobalIndexMap.h" #include "NumLib/DOF/LocalToGlobalIndexMap.h"
#include "NumLib/IndexValueVector.h" #include "NumLib/IndexValueVector.h"
#include "ProcessLib/BoundaryCondition/BoundaryCondition.h"
#include "ProcessLib/BoundaryCondition/GenericNaturalBoundaryConditionLocalAssembler.h"
#include "GenericNaturalBoundaryConditionLocalAssembler.h" #include "PythonBoundaryConditionPythonSideInterface.h"
namespace ProcessLib namespace ProcessLib
{ {
class PythonBoundaryConditionPythonSideInterface;
//! Groups data used by essential and natural BCs, in particular by the //! Groups data used by essential and natural BCs, in particular by the
//! local assemblers of the latter. //! local assemblers of the latter.
struct PythonBoundaryConditionData struct PythonBoundaryConditionData
...@@ -87,5 +83,3 @@ std::unique_ptr<PythonBoundaryCondition> createPythonBoundaryCondition( ...@@ -87,5 +83,3 @@ std::unique_ptr<PythonBoundaryCondition> createPythonBoundaryCondition(
unsigned const global_dim); unsigned const global_dim);
} // namespace ProcessLib } // namespace ProcessLib
#endif
...@@ -13,12 +13,18 @@ ...@@ -13,12 +13,18 @@
#include "MathLib/LinAlg/Eigen/EigenMapTools.h" #include "MathLib/LinAlg/Eigen/EigenMapTools.h"
#include "NumLib/DOF/DOFTableUtil.h" #include "NumLib/DOF/DOFTableUtil.h"
#include "ProcessLib/BoundaryCondition/GenericNaturalBoundaryConditionLocalAssembler.h"
#include "GenericNaturalBoundaryConditionLocalAssembler.h" #include "PythonBoundaryConditionPythonSideInterface.h"
#include "PythonBoundaryConditionDetail.h"
namespace ProcessLib namespace ProcessLib
{ {
//! Can be thrown to indicate that a member function is not overridden in a
//! derived class (in particular, if a Python class inherits from a C++ class).
struct MethodNotOverriddenInDerivedClassException
{
};
template <typename ShapeFunction, typename IntegrationMethod, template <typename ShapeFunction, typename IntegrationMethod,
unsigned GlobalDim> unsigned GlobalDim>
class PythonBoundaryConditionLocalAssembler final class PythonBoundaryConditionLocalAssembler final
...@@ -78,7 +84,7 @@ public: ...@@ -78,7 +84,7 @@ public:
for (unsigned element_node_id = 0; element_node_id < num_nodes; for (unsigned element_node_id = 0; element_node_id < num_nodes;
++element_node_id) ++element_node_id)
{ {
auto* node = _element.getNode(element_node_id); auto const* const node = _element.getNode(element_node_id);
auto const boundary_node_id = node->getID(); auto const boundary_node_id = node->getID();
auto const bulk_node_id = auto const bulk_node_id =
bulk_node_ids_map[boundary_node_id]; bulk_node_ids_map[boundary_node_id];
...@@ -89,7 +95,7 @@ public: ...@@ -89,7 +95,7 @@ public:
_data.dof_table_bulk.getGlobalIndex(loc, var, comp); _data.dof_table_bulk.getGlobalIndex(loc, var, comp);
if (dof_idx == NumLib::MeshComponentMap::nop) if (dof_idx == NumLib::MeshComponentMap::nop)
{ {
// TODO extend Python BC to mixed FEM // TODO extend Python BC to mixed FEM ansatz functions
OGS_FATAL( OGS_FATAL(
"No d.o.f. found for (node=%d, var=%d, comp=%d). " "No d.o.f. found for (node=%d, var=%d, comp=%d). "
"That might be due to the use of mixed FEM ansatz " "That might be due to the use of mixed FEM ansatz "
......
...@@ -7,16 +7,44 @@ ...@@ -7,16 +7,44 @@
* *
*/ */
#ifdef OGS_USE_PYTHON #include "PythonBoundaryConditionModule.h"
#include <pybind11/embed.h>
#include <pybind11/stl.h> #include <pybind11/stl.h>
#include <logog/include/logog.hpp>
#include "PythonBoundaryConditionDetail.h" #include "PythonBoundaryConditionPythonSideInterface.h"
namespace ProcessLib namespace ProcessLib
{ {
//! Trampoline class allowing methods of class
//! PythonBoundaryConditionPythonSideInterface to be overridden on the Python
//! side. Cf. https://pybind11.readthedocs.io/en/stable/advanced/classes.html
class PythonBoundaryConditionPythonSideInterfaceTrampoline
: public PythonBoundaryConditionPythonSideInterface
{
public:
using PythonBoundaryConditionPythonSideInterface::
PythonBoundaryConditionPythonSideInterface;
std::pair<bool, double> getDirichletBCValue(
double t, std::array<double, 3> x, std::size_t node_id,
std::vector<double> const& primary_variables) const override
{
using Ret = std::pair<bool, double>;
PYBIND11_OVERLOAD(Ret, PythonBoundaryConditionPythonSideInterface,
getDirichletBCValue, t, x, node_id,
primary_variables);
}
std::tuple<bool, double, std::vector<double>> getFlux(
double t, std::array<double, 3> x,
std::vector<double> const& primary_variables) const override
{
using Ret = std::tuple<bool, double, std::vector<double>>;
PYBIND11_OVERLOAD(Ret, PythonBoundaryConditionPythonSideInterface,
getFlux, t, x, primary_variables);
}
};
void pythonBindBoundaryCondition(pybind11::module& m) void pythonBindBoundaryCondition(pybind11::module& m)
{ {
namespace py = pybind11; namespace py = pybind11;
...@@ -33,12 +61,3 @@ void pythonBindBoundaryCondition(pybind11::module& m) ...@@ -33,12 +61,3 @@ void pythonBindBoundaryCondition(pybind11::module& m)
} }
} // namespace ProcessLib } // namespace ProcessLib
PYBIND11_EMBEDDED_MODULE(OpenGeoSys, m)
{
DBUG("Binding Python module OpenGeoSys.");
ProcessLib::pythonBindBoundaryCondition(m);
}
#endif // OGS_USE_PYTHON
/**
* \copyright
* Copyright (c) 2012-2018, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#pragma once
#include <pybind11/pybind11.h>
namespace ProcessLib
{
//! Creates Python bindings for the Python BC class.
void pythonBindBoundaryCondition(pybind11::module& m);
} // namespace ProcessLib
...@@ -9,15 +9,8 @@ ...@@ -9,15 +9,8 @@
#pragma once #pragma once
#include <pybind11/pybind11.h>
namespace ProcessLib namespace ProcessLib
{ {
//! Can be thrown to indicate that a member function is not overridden in a
//! derived class (in particular, if a Python class inherits from a C++ class).
struct MethodNotOverriddenInDerivedClassException
{
};
//! Base class for boundary conditions. //! Base class for boundary conditions.
//! This class will get Python bindings and is intended to be to be derived in //! This class will get Python bindings and is intended to be to be derived in
//! Python. //! Python.
...@@ -79,33 +72,4 @@ private: ...@@ -79,33 +72,4 @@ private:
//! Tells if getFlux() has been overridden in the derived class in Python. //! Tells if getFlux() has been overridden in the derived class in Python.
mutable bool _overridden_natural = true; mutable bool _overridden_natural = true;
}; };
//! Trampoline class allowing the the abstract base class to be overridden.
//! Cf. https://pybind11.readthedocs.io/en/stable/advanced/classes.html
class PythonBoundaryConditionPythonSideInterfaceTrampoline
: public PythonBoundaryConditionPythonSideInterface
{
public:
using PythonBoundaryConditionPythonSideInterface::
PythonBoundaryConditionPythonSideInterface;
std::pair<bool, double> getDirichletBCValue(
double t, std::array<double, 3> x, std::size_t node_id,
std::vector<double> const& primary_variables) const override
{
using Ret = std::pair<bool, double>;
PYBIND11_OVERLOAD(Ret, PythonBoundaryConditionPythonSideInterface,
getDirichletBCValue, t, x, node_id,
primary_variables);
}
std::tuple<bool, double, std::vector<double>> getFlux(
double t, std::array<double, 3> x,
std::vector<double> const& primary_variables) const override
{
using Ret = std::tuple<bool, double, std::vector<double>>;
PYBIND11_OVERLOAD(Ret, PythonBoundaryConditionPythonSideInterface,
getFlux, t, x, primary_variables);
}
};
} // namespace ProcessLib } // namespace ProcessLib
...@@ -26,8 +26,9 @@ target_link_libraries(ProcessLib ...@@ -26,8 +26,9 @@ target_link_libraries(ProcessLib
) )
if(OGS_USE_PYTHON) if(OGS_USE_PYTHON)
target_link_libraries(ProcessLib PRIVATE pybind11::module) add_subdirectory(BoundaryCondition/Python)
target_include_directories(ProcessLib PRIVATE ${PYTHON_INCLUDE_DIRS}) target_link_libraries(ProcessLib
PUBLIC ProcessLibBoundaryConditionPython)
endif() endif()
if(OGS_INSITU) if(OGS_INSITU)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment