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