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