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