OGS 6.2.0-97-g4a610c866
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  OGS_FATAL(
45  "Function `%s' is not defined in the python script file, or there "
46  "was no python script file specified.",
47  source_term_object.c_str());
48 
49  auto* source_term = scope[source_term_object.c_str()]
50  .cast<ProcessLib::SourceTerms::Python::
51  PythonSourceTermPythonSideInterface*>();
52 
53  // In case of partitioned mesh the source_term could be empty, i.e. there is
54  // no source_term condition.
55 #ifdef USE_PETSC
56  // This can be extracted to createSourceTerm() but then the config
57  // parameters are not read and will cause an error.
58  // TODO (naumov): Add a function to ConfigTree for skipping the tags of the
59  // subtree and move the code up in createSourceTerm().
60  if (source_term_mesh.getDimension() == 0 &&
61  source_term_mesh.getNumberOfNodes() == 0 &&
62  source_term_mesh.getNumberOfElements() == 0)
63  {
64  return nullptr;
65  }
66 #endif // USE_PETSC
67 
68  auto const global_component_id =
69  dof_table->getGlobalComponent(variable_id, component_id);
70  return std::make_unique<ProcessLib::SourceTerms::Python::PythonSourceTerm>(
71  std::move(dof_table),
73  source_term, bulk_mesh_id, global_component_id, source_term_mesh},
74  integration_order, shapefunction_order, global_dim, flush_stdout);
75 }
76 
77 } // 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