OGS
CreateHeatTransportBHEProcess.cpp
Go to the documentation of this file.
1 
12 
13 #include <vector>
14 
15 #include "BHE/BHETypes.h"
16 #include "BHE/CreateBHE1PType.h"
17 #include "BHE/CreateBHECoaxial.h"
18 #include "BHE/CreateBHEUType.h"
21 #include "ParameterLib/Utils.h"
23 #ifdef OGS_USE_PYTHON
24 #include <pybind11/pybind11.h>
25 #endif // OGS_USE_PYTHON
26 
27 namespace ProcessLib
28 {
29 namespace HeatTransportBHE
30 {
31 std::unique_ptr<Process> createHeatTransportBHEProcess(
32  std::string name,
33  MeshLib::Mesh& mesh,
34  std::unique_ptr<ProcessLib::AbstractJacobianAssembler>&& jacobian_assembler,
35  std::vector<ProcessVariable> const& variables,
36  std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
37  unsigned const integration_order,
38  BaseLib::ConfigTree const& config,
39  std::map<std::string,
40  std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
41  curves,
42  std::map<int, std::shared_ptr<MaterialPropertyLib::Medium>> const& media)
43 {
45  config.checkConfigParameter("type", "HEAT_TRANSPORT_BHE");
46 
47  DBUG("Create HeatTransportBHE Process.");
48 
49  // Process variable.
50 
52  auto const pv_config = config.getConfigSubtree("process_variables");
53  std::vector<std::vector<std::reference_wrapper<ProcessVariable>>>
54  process_variables;
55 
56  // reading primary variables for each
57  // BHE----------------------------------------------------------
58  auto range =
60  pv_config.getConfigParameterList<std::string>("process_variable");
61  std::vector<std::reference_wrapper<ProcessVariable>> per_process_variables;
62 
63  for (std::string const& pv_name : range)
64  {
65  if (pv_name != "temperature_soil" &&
66  pv_name.find("temperature_BHE") == std::string::npos)
67  {
68  OGS_FATAL(
69  "Found a process variable name '{:s}'. It should be "
70  "'temperature_soil' or 'temperature_BHE_X'");
71  }
72  auto variable = std::find_if(variables.cbegin(), variables.cend(),
73  [&pv_name](ProcessVariable const& v)
74  { return v.getName() == pv_name; });
75 
76  if (variable == variables.end())
77  {
78  OGS_FATAL(
79  "Could not find process variable '{:s}' in the provided "
80  "variables list for config tag <{:s}>.",
81  pv_name, "process_variable");
82  }
83  DBUG("Found process variable '{:s}' for config tag <{:s}>.",
84  variable->getName(), "process_variable");
85 
86  per_process_variables.emplace_back(
87  const_cast<ProcessVariable&>(*variable));
88  }
89  process_variables.push_back(std::move(per_process_variables));
90  // end of reading primary variables for each
91  // BHE----------------------------------------------------------
92 
93  // reading BHE parameters --------------------------------------------------
94  std::vector<BHE::BHETypes> bhes;
95 
96  auto const& bhe_configs =
98  config.getConfigSubtree("borehole_heat_exchangers");
99 
100  auto const using_server_communication =
102  config.getConfigParameter<bool>("use_server_communication", false);
103 
104  for (
105  auto const& bhe_config :
107  bhe_configs.getConfigSubtreeList("borehole_heat_exchanger"))
108  {
109  // read in the parameters
110  const std::string bhe_type =
112  bhe_config.getConfigParameter<std::string>("type");
113 
114  if (bhe_type == "1U")
115  {
116  bhes.emplace_back(
117  BHE::createBHEUType<BHE::BHE_1U>(bhe_config, curves));
118  continue;
119  }
120 
121  if (bhe_type == "CXA")
122  {
123  bhes.emplace_back(
124  BHE::createBHECoaxial<BHE::BHE_CXA>(bhe_config, curves));
125  continue;
126  }
127 
128  if (bhe_type == "CXC")
129  {
130  bhes.emplace_back(
131  BHE::createBHECoaxial<BHE::BHE_CXC>(bhe_config, curves));
132  continue;
133  }
134 
135  if (bhe_type == "2U")
136  {
137  bhes.emplace_back(
138  BHE::createBHEUType<BHE::BHE_2U>(bhe_config, curves));
139  continue;
140  }
141 
142  if (bhe_type == "1P")
143  {
144  bhes.emplace_back(
145  BHE::createBHE1PType<BHE::BHE_1P>(bhe_config, curves));
146  continue;
147  }
148  OGS_FATAL("Unknown BHE type '{:s}'.", bhe_type);
149  }
150  // end of reading BHE parameters -------------------------------------------
151 
152  auto media_map =
154 
155  // find if bhe uses python boundary condition
156  auto const using_tespy =
157  visit([](auto const& bhe) { return bhe.use_python_bcs; }, bhes[0]);
158 
161  // create a pythonBoundaryCondition object
162  if (using_tespy || using_server_communication)
163  {
164 #ifdef OGS_USE_PYTHON
165  // Evaluate Python code in scope of main module
166  pybind11::object scope =
167  pybind11::module::import("__main__").attr("__dict__");
168 
169  if (!scope.contains("bc_bhe"))
170  OGS_FATAL(
171  "Function 'bc_bhe' is not defined in the python script file, "
172  "or there was no python script file specified.");
173 
174  py_object =
175  scope["bc_bhe"]
177 
178  if (py_object == nullptr)
179  OGS_FATAL(
180  "Not able to access the correct bc pointer from python script "
181  "file specified.");
182 
183  // create BHE network dataframe from Python
184  py_object->dataframe_network = py_object->initializeDataContainer();
185  if (!py_object->isOverriddenEssential())
186  {
187  DBUG(
188  "Method `initializeDataContainer' not overridden in Python "
189  "script.");
190  }
191  // clear ogs bc_node_id memory in dataframe
192  std::get<3>(py_object->dataframe_network).clear(); // ogs_bc_node_id
193 
194  // here calls the tespyHydroSolver to get the pipe flow velocity in bhe
195  // network
196  /* for 2U type the flowrate initialization process below causes conflict
197  // replace the value in flow velocity Matrix _u
198  auto const tespy_flow_rate = std::get<4>(py_object->dataframe_network);
199  const std::size_t n_bhe = tespy_flow_rate.size();
200  if (bhes.size() != n_bhe)
201  OGS_FATAL(
202  "The number of BHEs defined in OGS and TESPy are not the "
203  "same!");
204 
205  for (std::size_t idx_bhe = 0; idx_bhe < n_bhe; idx_bhe++)
206  {
207  // the flow_rate in OGS should be updated from the flow_rate
208  // computed by TESPy.
209  auto update_flow_rate = [&](auto& bhe) {
210  bhe.updateHeatTransferCoefficients(tespy_flow_rate[idx_bhe]);
211  };
212  visit(update_flow_rate, bhes[idx_bhe]);
213  }
214  */
215 #else
216  OGS_FATAL(
217  "Input files suggest the coupling of BHE with pipe network. "
218  "This means the compiling flag OGS_USE_PYTHON must be switched "
219  "on. ");
220 #endif // OGS_USE_PYTHON
221  }
222 
223  HeatTransportBHEProcessData process_data(
224  std::move(media_map), std::move(bhes), py_object, using_tespy,
225  using_server_communication);
226 
227  SecondaryVariableCollection secondary_variables;
228 
229  ProcessLib::createSecondaryVariables(config, secondary_variables);
230 
231  return std::make_unique<HeatTransportBHEProcess>(
232  std::move(name), mesh, std::move(jacobian_assembler), parameters,
233  integration_order, std::move(process_variables),
234  std::move(process_data), std::move(secondary_variables));
235 }
236 } // namespace HeatTransportBHE
237 } // namespace ProcessLib
#define OGS_FATAL(...)
Definition: Error.h:26
void DBUG(char const *fmt, Args const &... args)
Definition: Logging.h:27
void checkConfigParameter(std::string const &param, T const &value) const
T getConfigParameter(std::string const &param) const
ConfigTree getConfigSubtree(std::string const &root) const
Definition: ConfigTree.cpp:146
Range< ValueIterator< T > > getConfigParameterList(std::string const &param) const
Handles configuration of several secondary variables from the project file.
std::unique_ptr< MaterialSpatialDistributionMap > createMaterialSpatialDistributionMap(std::map< int, std::shared_ptr< Medium >> const &media, MeshLib::Mesh const &mesh)
std::unique_ptr< Process > createHeatTransportBHEProcess(std::string name, MeshLib::Mesh &mesh, std::unique_ptr< ProcessLib::AbstractJacobianAssembler > &&jacobian_assembler, std::vector< ProcessVariable > const &variables, std::vector< std::unique_ptr< ParameterLib::ParameterBase >> const &parameters, unsigned const integration_order, BaseLib::ConfigTree const &config, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation >> const &curves, std::map< int, std::shared_ptr< MaterialPropertyLib::Medium >> const &media)
void createSecondaryVariables(BaseLib::ConfigTree const &config, SecondaryVariableCollection &secondary_variables)