OGS
CreatePythonSourceTerm.cpp
Go to the documentation of this file.
1
12
13#include <pybind11/pybind11.h>
14
15#include "BaseLib/ConfigTree.h"
16#include "MeshLib/Mesh.h"
19#include "PythonSourceTerm.h"
20
21namespace ProcessLib
22{
23std::unique_ptr<SourceTerm> createPythonSourceTerm(
24 BaseLib::ConfigTree const& config, MeshLib::Mesh const& source_term_mesh,
25 std::unique_ptr<NumLib::LocalToGlobalIndexMap> dof_table,
26 int const variable_id, int const component_id,
27 unsigned const integration_order, unsigned const shapefunction_order,
28 unsigned const global_dim,
29 std::vector<std::reference_wrapper<ProcessVariable>> const&
30 all_process_variables_for_this_process)
31{
32 DBUG("Constructing PythonSourceTerm from config.");
34 config.checkConfigParameter("type", "Python");
35
36 auto const source_term_object =
38 config.getConfigParameter<std::string>("source_term_object");
40 auto const flush_stdout = config.getConfigParameter("flush_stdout", false);
41
42 // Evaluate Python code in scope of main module
43 pybind11::object scope =
44 pybind11::module::import("__main__").attr("__dict__");
45
46 if (!scope.contains(source_term_object))
47 {
49 "Function `{:s}' is not defined in the python script file, or "
50 "there was no python script file specified.",
51 source_term_object);
52 }
53
54 auto* source_term = scope[source_term_object.c_str()]
55 .cast<ProcessLib::SourceTerms::Python::
56 PythonSourceTermPythonSideInterface*>();
57
58 // In case of partitioned mesh the source_term could be empty, i.e. there is
59 // no source_term condition.
60#ifdef USE_PETSC
61 // This can be extracted to createSourceTerm() but then the config
62 // parameters are not read and will cause an error.
63 // TODO (naumov): Add a function to ConfigTree for skipping the tags of the
64 // subtree and move the code up in createSourceTerm().
65 if (source_term_mesh.getDimension() == 0 &&
66 source_term_mesh.getNumberOfNodes() == 0 &&
67 source_term_mesh.getNumberOfElements() == 0)
68 {
69 return nullptr;
70 }
71#endif // USE_PETSC
72
73 auto const global_component_id =
74 dof_table->getGlobalComponent(variable_id, component_id);
75 return std::make_unique<ProcessLib::SourceTerms::Python::PythonSourceTerm>(
76 std::move(dof_table),
78 {source_term, global_component_id, source_term_mesh,
79 all_process_variables_for_this_process, shapefunction_order}},
80 integration_order, global_dim, flush_stdout);
81}
82
83} // namespace ProcessLib
#define OGS_FATAL(...)
Definition Error.h:26
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
Definition of the Mesh class.
T getConfigParameter(std::string const &param) const
void checkConfigParameter(std::string const &param, std::string_view const value) const
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition Mesh.h:88
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:100
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:97
std::unique_ptr< SourceTerm > createPythonSourceTerm(BaseLib::ConfigTree const &config, MeshLib::Mesh const &source_term_mesh, std::unique_ptr< NumLib::LocalToGlobalIndexMap > dof_table, int const variable_id, int const component_id, unsigned const integration_order, unsigned const shapefunction_order, unsigned const global_dim, std::vector< std::reference_wrapper< ProcessVariable > > const &all_process_variables_for_this_process)