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