OGS
CreateHeatTransportBHEProcess.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 <pybind11/pybind11.h>
7
8#include <algorithm>
9#include <map>
10#include <numeric>
11#include <ranges>
12#include <vector>
13
14#include "BHE/BHETypes.h"
15#include "BHE/CreateBHE1PType.h"
17#include "BHE/CreateBHEUType.h"
21#include "ParameterLib/Utils.h"
24
25namespace ProcessLib
26{
27namespace HeatTransportBHE
28{
29using BHECreatorFunc = std::function<BHE::BHETypes(
31 std::vector<std::unique_ptr<ParameterLib::ParameterBase>>&,
32 std::map<std::string,
33 std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&)>;
34
35std::map<std::string_view, BHECreatorFunc> bheCreators = {
36 {"1U",
37 [](auto& config, auto& parameters, auto& curves)
38 {
39 return BHE::BHE_1U(
40 BHE::createBHEUType<BHE::BHE_1U>(config, parameters, curves));
41 }},
42 {"2U",
43 [](auto& config, auto& parameters, auto& curves)
44 {
45 return BHE::BHE_2U(
46 BHE::createBHEUType<BHE::BHE_2U>(config, parameters, curves));
47 }},
48 {"CXA",
49 [](auto& config, auto& parameters, auto& curves)
50 {
51 return BHE::BHE_CXA(
52 BHE::createBHECoaxial<BHE::BHE_CXA>(config, parameters, curves));
53 }},
54 {"CXC",
55 [](auto& config, auto& parameters, auto& curves)
56 {
57 return BHE::BHE_CXC(
58 BHE::createBHECoaxial<BHE::BHE_CXC>(config, parameters, curves));
59 }},
60 {"1P", [](auto& config, auto& parameters, auto& curves)
61 {
62 return BHE::BHE_1P(
63 BHE::createBHE1PType<BHE::BHE_1P>(config, parameters, curves));
64 }}};
65
67 const std::string& bhe_type,
68 const std::vector<int>& bhe_ids_of_this_bhe,
69 const BaseLib::ConfigTree& bhe_config,
70 std::vector<std::unique_ptr<ParameterLib::ParameterBase>>& parameters,
71 std::map<std::string,
72 std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
73 curves,
74 BHEMeshData const& bhe_mesh_data,
75 std::map<int, BHE::BHETypes>& bhes_map)
76{
77 auto bhe_creator_it = bheCreators.find(bhe_type);
78 if (bhe_creator_it == bheCreators.end())
79 {
80 OGS_FATAL("Unknown BHE type: {:s}", bhe_type);
81 }
82 for (auto const& id : bhe_ids_of_this_bhe)
83 {
84 if (id < 0 || id >= static_cast<int>(bhe_mesh_data.BHE_nodes.size()))
85 {
87 "BHE id {:d} is out of range. The mesh contains {:d} "
88 "BHE(s) (valid ids: 0 to {:d}).",
89 id, bhe_mesh_data.BHE_nodes.size(),
90 static_cast<int>(bhe_mesh_data.BHE_nodes.size()) - 1);
91 }
92 std::pair<std::map<int, BHE::BHETypes>::iterator, bool> result;
93 if (id == bhe_ids_of_this_bhe[0])
94 {
95 result = bhes_map.try_emplace(
96 id, bhe_creator_it->second(bhe_config, parameters, curves));
97 }
98 else
99 {
100 // Grouped BHEs share all configuration including parameter
101 // references. Spatial variation comes from the SpatialPosition
102 // set per-element in the assembler, so a plain copy suffices.
103 auto const& first_bhe =
104 bhes_map.find(bhe_ids_of_this_bhe[0])->second;
105 result = bhes_map.try_emplace(id, first_bhe);
106 }
107 if (!result.second)
108 {
109 OGS_FATAL(
110 "BHE with id '{:d}' is already present in the list! Check for "
111 "duplicate definitions of BHE ids.",
112 id);
113 }
114 }
115}
116
117std::unique_ptr<Process> createHeatTransportBHEProcess(
118 std::string const& name,
119 MeshLib::Mesh& mesh,
120 std::unique_ptr<ProcessLib::AbstractJacobianAssembler>&& jacobian_assembler,
121 std::vector<ProcessVariable> const& variables,
122 std::vector<std::unique_ptr<ParameterLib::ParameterBase>>& parameters,
123 unsigned const integration_order,
124 BaseLib::ConfigTree const& config,
125 std::map<std::string,
126 std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
127 curves,
128 std::map<int, std::shared_ptr<MaterialPropertyLib::Medium>> const& media)
129{
131 config.checkConfigParameter("type", "HEAT_TRANSPORT_BHE");
132
133 DBUG("Create HeatTransportBHE Process.");
134
136
138 auto const pv_config = config.getConfigSubtree("process_variables");
139 std::vector<std::vector<std::reference_wrapper<ProcessVariable>>>
140 process_variables;
141
142 // reading primary variables for each
143 // BHE----------------------------------------------------------
145 auto range =
147 pv_config.getConfigParameterList<std::string>("process_variable");
148 std::vector<std::reference_wrapper<ProcessVariable>> per_process_variables;
149
150 for (std::string const& pv_name : range)
151 {
152 if (pv_name != "temperature_soil" &&
153 pv_name.find("temperature_BHE") == std::string::npos)
154 {
155 OGS_FATAL(
156 "Found a process variable name '{}'. It should be "
157 "'temperature_soil' or 'temperature_BHE_X'",
158 pv_name);
159 }
160 auto variable = std::find_if(variables.cbegin(), variables.cend(),
161 [&pv_name](ProcessVariable const& v)
162 { return v.getName() == pv_name; });
163
164 if (variable == variables.end())
165 {
166 OGS_FATAL(
167 "Could not find process variable '{:s}' in the provided "
168 "variables list for config tag <{:s}>.",
169 pv_name, "process_variable");
170 }
171 DBUG("Found process variable '{:s}' for config tag <{:s}>.",
172 variable->getName(), "process_variable");
173
174 per_process_variables.emplace_back(
175 const_cast<ProcessVariable&>(*variable));
176 }
177 process_variables.push_back(std::move(per_process_variables));
178 // end of reading primary variables for each
179 // BHE----------------------------------------------------------
180
182 // reading BHE parameters --------------------------------------------------
183
184 auto bhe_mesh_data = getBHEDataInMesh(mesh);
185
186 auto const& bhe_configs =
188 config.getConfigSubtree("borehole_heat_exchangers");
189
190 auto const using_server_communication =
192 config.getConfigParameter<bool>("use_server_communication", false);
193
194 auto const mass_lumping =
196 config.getConfigParameter<bool>("mass_lumping", false);
197
198 auto const using_algebraic_bc =
200 config.getConfigParameter<bool>("use_algebraic_bc", false);
201
202 auto const weighting_factor =
204 config.getConfigParameter<float>("weighting_factor", 1000.0);
205
206 auto const is_linear =
208 config.getConfigParameter<bool>("linear", false);
209 if (is_linear)
210 {
211 if (!using_algebraic_bc)
212 {
213 WARN(
214 "You specified that the process simulated by OGS is linear. "
215 "With that optimization the process will be assembled only "
216 "once and the non-linear solver will only do iterations per "
217 "time step to fulfill the BHE boundary conditions. No other "
218 "non-linearities will be resolved and OGS will not detect if "
219 "there are any non-linearities. It is your responsibility to "
220 "ensure that the assembled equation systems are linear, "
221 "indeed! There is no safety net!");
222 }
223 else
224 {
225 WARN(
226 "You specified that the process simulated by OGS is linear. "
227 "With that optimization the process will be assembled only "
228 "once and the non-linear solver will do only one iteration per "
229 "time step. No non-linearities will be resolved and OGS will "
230 "not detect if there are any non-linearities. It is your "
231 "responsibility to ensure that the assembled equation systems "
232 "are linear, indeed! There is no safety net!");
233 }
234 }
235
236 std::map<int, BHE::BHETypes> bhes_map;
237
238 int bhe_iterator = 0;
239
240 for (
241 auto const& bhe_config :
243 bhe_configs.getConfigSubtreeList("borehole_heat_exchanger"))
244 {
245 auto const bhe_id_string =
247 bhe_config.getConfigAttribute<std::string>(
248 "id", std::to_string(bhe_iterator));
249
250 std::vector<int> bhe_ids_of_this_bhe;
251
252 if (bhe_id_string == "*")
253 {
254 int const size = static_cast<int>(bhe_mesh_data.BHE_mat_IDs.size());
255 bhe_ids_of_this_bhe.resize(size);
256 std::iota(bhe_ids_of_this_bhe.begin(), bhe_ids_of_this_bhe.end(),
257 0);
258 }
259 else
260 {
261 bhe_ids_of_this_bhe =
263 }
264
265 // read in the parameters
266 const std::string bhe_type =
268 bhe_config.getConfigParameter<std::string>("type");
269
270 createAndInsertBHE(bhe_type, bhe_ids_of_this_bhe, bhe_config,
271 parameters, curves, bhe_mesh_data, bhes_map);
272 bhe_iterator++;
273 }
274
275 if (static_cast<int>(bhes_map.size()) - 1 != bhes_map.rbegin()->first)
276 {
277 OGS_FATAL(
278 "The maximum given BHE id '{:d}' did not match the number of given "
279 "BHE definitions '{:d}'. The BHE ids needs to be defined starting "
280 "from 0, so the maximum BHE id needs to be number of BHE "
281 "definitions minus 1. After all definitions there are no gaps "
282 "allowed between the given ids.",
283 bhes_map.rbegin()->first, bhes_map.size());
284 }
285
286 std::vector<BHE::BHETypes> bhes;
287 bhes.reserve(bhes_map.size());
288 std::ranges::copy(bhes_map | std::views::values, std::back_inserter(bhes));
289 // end of reading BHE parameters
290 // -------------------------------------------
291
292 auto media_map =
294
295 // find if bhe uses python boundary condition
296 auto const using_tespy =
297 visit([](auto const& bhe) { return bhe.use_python_bcs; }, bhes[0]);
298
301 // create a pythonBoundaryCondition object
302 if (using_tespy || using_server_communication)
303 {
304 // Evaluate Python code in scope of main module
305 pybind11::object scope =
306 pybind11::module::import("__main__").attr("__dict__");
307
308 if (!scope.contains("bc_bhe"))
309 OGS_FATAL(
310 "Function 'bc_bhe' is not defined in the python script file, "
311 "or there was no python script file specified.");
312
313 py_object =
314 scope["bc_bhe"]
316
317 if (py_object == nullptr)
318 OGS_FATAL(
319 "Not able to access the correct bc pointer from python script "
320 "file specified.");
321
322 // create BHE network dataframe from Python
323 py_object->dataframe_network = py_object->initializeDataContainer();
324 if (!py_object->isOverriddenEssential())
325 {
326 DBUG(
327 "Method `initializeDataContainer' not overridden in Python "
328 "script.");
329 }
330 // clear ogs bc_node_id memory in dataframe
331 std::get<3>(py_object->dataframe_network).clear(); // ogs_bc_node_id
332
333 // here calls the tespyHydroSolver to get the pipe flow velocity in bhe
334 // network
335 /* for 2U type the flowrate initialization process below causes conflict
336 // replace the value in flow velocity Matrix _u
337 auto const tespy_flow_rate = std::get<4>(py_object->dataframe_network);
338 const std::size_t n_bhe = tespy_flow_rate.size();
339 if (bhes.size() != n_bhe)
340 OGS_FATAL(
341 "The number of BHEs defined in OGS and TESPy are not the "
342 "same!");
343
344 for (std::size_t idx_bhe = 0; idx_bhe < n_bhe; idx_bhe++)
345 {
346 // the flow_rate in OGS should be updated from the flow_rate
347 // computed by TESPy.
348 auto update_flow_rate = [&](auto& bhe) {
349 bhe.updateHeatTransferCoefficients(tespy_flow_rate[idx_bhe]);
350 };
351 visit(update_flow_rate, bhes[idx_bhe]);
352 }
353 */
354 }
355
356 HeatTransportBHEProcessData process_data(
357 std::move(media_map), std::move(bhes), py_object, using_tespy,
358 using_server_communication, mass_lumping,
359 {using_algebraic_bc, weighting_factor}, is_linear);
360
361 SecondaryVariableCollection secondary_variables;
362
363 ProcessLib::createSecondaryVariables(config, secondary_variables);
364
365 return std::make_unique<HeatTransportBHEProcess>(
366 std::move(name), mesh, std::move(jacobian_assembler), parameters,
367 integration_order, std::move(process_variables),
368 std::move(process_data), std::move(secondary_variables),
369 std::move(bhe_mesh_data));
370}
371} // namespace HeatTransportBHE
372} // namespace ProcessLib
#define OGS_FATAL(...)
Definition Error.h:10
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
T getConfigParameter(std::string const &param) const
ConfigTree getConfigSubtree(std::string const &root) const
Range< ValueIterator< T > > getConfigParameterList(std::string const &param) const
void checkConfigParameter(std::string const &param, std::string_view const value) const
Handles configuration of several secondary variables from the project file.
std::vector< int > splitMaterialIdString(std::string const &material_id_string, char const separator)
MaterialSpatialDistributionMap createMaterialSpatialDistributionMap(std::map< int, std::shared_ptr< Medium > > const &media, MeshLib::Mesh const &mesh)
T_BHE createBHE1PType(BaseLib::ConfigTree const &config, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > &parameters, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves)
std::variant< BHE_1U, BHE_CXA, BHE_CXC, BHE_2U, BHE_1P > BHETypes
Definition BHETypes.h:19
T_BHE createBHEUType(BaseLib::ConfigTree const &config, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > &parameters, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves)
T_BHE createBHECoaxial(BaseLib::ConfigTree const &config, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > &parameters, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves)
std::function< BHE::BHETypes( BaseLib::ConfigTree const &, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > &, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &)> BHECreatorFunc
std::map< std::string_view, BHECreatorFunc > bheCreators
void createAndInsertBHE(const std::string &bhe_type, const std::vector< int > &bhe_ids_of_this_bhe, const BaseLib::ConfigTree &bhe_config, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > &parameters, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves, BHEMeshData const &bhe_mesh_data, std::map< int, BHE::BHETypes > &bhes_map)
std::unique_ptr< Process > createHeatTransportBHEProcess(std::string const &name, MeshLib::Mesh &mesh, std::unique_ptr< ProcessLib::AbstractJacobianAssembler > &&jacobian_assembler, std::vector< ProcessVariable > const &variables, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > &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)
BHEMeshData getBHEDataInMesh(MeshLib::Mesh const &mesh)
void createSecondaryVariables(BaseLib::ConfigTree const &config, SecondaryVariableCollection &secondary_variables)
std::vector< std::vector< MeshLib::Node * > > BHE_nodes