Loading [MathJax]/extensions/MathMenu.js
OGS
ProcessVariable.cpp
Go to the documentation of this file.
1
11#include "ProcessVariable.h"
12
13#include <algorithm>
14#include <utility>
15
16#include "BaseLib/Algorithm.h"
17#include "BaseLib/Logging.h"
19#include "MeshLib/Mesh.h"
20#include "MeshLib/Node.h"
22#include "ParameterLib/Utils.h"
32
33namespace
34{
36 BaseLib::ConfigTree const& config,
37 std::vector<std::unique_ptr<MeshLib::Mesh>> const& meshes)
38{
39 //
40 // Get the mesh name from the config.
41 //
42 std::string mesh_name; // Either given directly in <mesh> or constructed
43 // from <geometrical_set>_<geometry>.
44
45#ifdef DOXYGEN_DOCU_ONLY
47 config.getConfigParameterOptional<std::string>("mesh");
48#endif // DOXYGEN_DOCU_ONLY
49
50 auto optional_mesh_name =
52 config.getConfigParameterOptional<std::string>("mesh");
53 if (optional_mesh_name)
54 {
55 mesh_name = *optional_mesh_name;
56 }
57 else
58 {
59#ifdef DOXYGEN_DOCU_ONLY
61 config.getConfigParameterOptional<std::string>("geometrical_set");
63 config.getConfigParameter<std::string>("geometry");
64#endif // DOXYGEN_DOCU_ONLY
65
66 // Looking for the mesh created before for the given geometry.
67 auto const geometrical_set_name =
69 config.getConfigParameter<std::string>("geometrical_set");
70 auto const geometry_name =
72 config.getConfigParameter<std::string>("geometry");
73
74 mesh_name = MeshGeoToolsLib::meshNameFromGeometry(geometrical_set_name,
75 geometry_name);
76 }
77
78 //
79 // Find and extract mesh from the list of meshes.
80 //
81 auto const& mesh = MeshLib::findMeshByName(meshes, mesh_name);
82 DBUG("Found mesh '{:s}' with id {:d}.", mesh.getName(), mesh.getID());
83
84 return mesh;
85}
86} // namespace
87
88namespace ProcessLib
89{
91 BaseLib::ConfigTree const& config, MeshLib::Mesh& mesh,
92 std::vector<std::unique_ptr<MeshLib::Mesh>> const& meshes,
93 std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
94 std::map<std::string,
95 std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
96 curves)
97 :
98 _name(config.getConfigParameter<std::string>("name")),
99 _mesh(mesh),
101 _n_components(config.getConfigParameter<int>("components")),
103 _shapefunction_order(config.getConfigParameter<unsigned>("order")),
104 _deactivated_subdomains(
105 createDeactivatedSubdomains(config, mesh, parameters, curves)),
106 _initial_condition(ParameterLib::findParameter<double>(
108 config.getConfigParameter<std::string>("initial_condition"),
109 parameters, _n_components, &mesh)),
110 _compensate_non_equilibrium_initial_residuum(
112 config.getConfigParameter<bool>(
113 "compensate_non_equilibrium_initial_residuum", false))
114{
115 DBUG("Constructing process variable {:s}", _name);
116
118 {
119 OGS_FATAL("The given shape function order {:d} is not supported",
121 }
122
123 // Boundary conditions
124 if (auto bcs_config =
126 config.getConfigSubtreeOptional("boundary_conditions"))
127 {
128 for (
129 auto bc_config :
131 bcs_config->getConfigSubtreeList("boundary_condition"))
132 {
133 auto const& bc_mesh = findMeshInConfig(bc_config, meshes);
134 auto component_id =
136 bc_config.getConfigParameterOptional<int>("component");
137
138 if (!component_id && _n_components == 1)
139 {
140 // default value for single component vars.
141 component_id = 0;
142 }
143
144 _bc_configs.emplace_back(std::move(bc_config), bc_mesh,
145 component_id);
146 }
147 }
148 else
149 {
150 INFO("No boundary conditions for process variable '{:s}' found.",
151 _name);
152 }
153
154 // Source terms
156 if (auto sts_config = config.getConfigSubtreeOptional("source_terms"))
157 {
158 for (
159 auto st_config :
161 sts_config->getConfigSubtreeList("source_term"))
162 {
163 MeshLib::Mesh const& st_mesh = findMeshInConfig(st_config, meshes);
164 auto component_id =
166 st_config.getConfigParameterOptional<int>("component");
167 auto const type =
169 st_config.peekConfigParameter<std::string>("type");
170
171 if (!component_id)
172 {
173 if (_n_components == 1)
174 {
175 // default value for single component vars.
176 component_id = 0;
177 }
178 else if (type == "Anchor")
179 {
180 // dummy value
181 component_id = 0;
182 }
183 else if (type == "EmbeddedAnchor")
184 {
185 // dummy value
186 component_id = 0;
187 }
188 else
189 {
190 OGS_FATAL(
191 "Specifying the component id (<component>) for a "
192 "source term for a non-scalar process variable is "
193 "mandatory.");
194 }
195 }
196
197 _source_term_configs.emplace_back(std::move(st_config), st_mesh,
198 *component_id);
199 }
200 }
201 else
202 {
203 INFO("No source terms for process variable '{:s}' found.", _name);
204 }
205
206 if (!_deactivated_subdomains.empty())
207 {
208 _is_active = getOrCreateMeshProperty<unsigned char>(
209 _mesh, _name + "_active", MeshLib::MeshItemType::Cell, 1);
210 std::fill(std::begin(*_is_active), std::end(*_is_active), 1u);
211 }
212}
213
215
216std::string const& ProcessVariable::getName() const
217{
218 return _name;
219}
220
222{
223 return _mesh;
224}
225
226std::vector<std::unique_ptr<BoundaryCondition>>
228 const NumLib::LocalToGlobalIndexMap& dof_table,
229 const int variable_id,
230 unsigned const integration_order,
231 std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
232 Process const& process,
233 std::vector<std::reference_wrapper<ProcessVariable>> const&
234 all_process_variables_for_this_process,
235 std::map<int, std::shared_ptr<MaterialPropertyLib::Medium>> const& media)
236{
237 std::vector<std::unique_ptr<BoundaryCondition>> bcs;
238 bcs.reserve(_bc_configs.size());
239
240 for (auto const& config : _bc_configs)
241 {
242 auto bc = createBoundaryCondition(
243 config, dof_table, _mesh, variable_id, integration_order,
244 _shapefunction_order, parameters, process,
245 all_process_variables_for_this_process, media);
246#ifdef USE_PETSC
247 if (bc == nullptr)
248 {
249 continue;
250 }
251#endif // USE_PETSC
252 bcs.push_back(std::move(bc));
253 }
254
256 parameters, bcs);
257
258 return bcs;
259}
260
262 const NumLib::LocalToGlobalIndexMap& dof_table, const int variable_id,
263 std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
264 std::vector<std::unique_ptr<BoundaryCondition>>& bcs)
265{
266 for (auto const& deactivated_subdomain : _deactivated_subdomains)
267 {
268 auto const& deactivated_subdomain_mesh =
269 deactivated_subdomain.deactivated_subdomain_mesh;
270 auto const* parameter = &ParameterLib::findParameter<double>(
272 bool const set_outer_nodes_dirichlet_values =
273 deactivated_subdomain.boundary_value_parameter != nullptr;
274 if (set_outer_nodes_dirichlet_values)
275 {
276 parameter = deactivated_subdomain.boundary_value_parameter;
277 }
278
279 for (int component_id = 0;
280 component_id <
281 dof_table.getNumberOfVariableComponents(variable_id);
282 component_id++)
283 {
284 auto bc = std::make_unique<DeactivatedSubdomainDirichlet>(
285 *_is_active, deactivated_subdomain.time_interval, *parameter,
286 set_outer_nodes_dirichlet_values, deactivated_subdomain_mesh,
287 dof_table, variable_id, component_id);
288
289#ifdef USE_PETSC
290 // TODO: make it work under PETSc too.
291 if (bc == nullptr)
292 {
293 continue;
294 }
295#endif // USE_PETSC
296 bcs.push_back(std::move(bc));
297 }
298 }
299}
300
302{
303 if (_deactivated_subdomains.empty())
304 {
305 return;
306 }
307
309
310 // If none of the deactivated subdomains is active at current time, then the
311 // _ids_of_active_elements remain empty.
312 if (std::none_of(begin(_deactivated_subdomains),
313 end(_deactivated_subdomains), [&](auto const& ds)
314 { return ds.isInTimeSupportInterval(time); }))
315 {
316 // Also mark all of the elements as active.
317 assert(_is_active != nullptr); // guaranteed by constructor
318 std::fill(std::begin(*_is_active), std::end(*_is_active), 1u);
319
320 return;
321 }
322
323 auto is_active_in_subdomain = [&](std::size_t const element_id,
324 DeactivatedSubdomain const& ds) -> bool
325 {
326 return (!ds.isInTimeSupportInterval(time)) ||
327 !ds.isDeactivated(*_mesh.getElement(element_id), time);
328 };
329
330 auto is_active_in_all_subdomains = [&](std::size_t const element_id) -> bool
331 {
332 return std::all_of(begin(_deactivated_subdomains),
334 [&](auto const& ds)
335 { return is_active_in_subdomain(element_id, ds); });
336 };
337
338 auto const number_of_elements = _mesh.getNumberOfElements();
339 for (std::size_t element_id = 0; element_id < number_of_elements;
340 element_id++)
341 {
342 if (is_active_in_all_subdomains(element_id))
343 {
344 _ids_of_active_elements.push_back(element_id);
345 }
346 }
347
348 // all elements are deactivated
349 std::fill(std::begin(*_is_active), std::end(*_is_active), 0u);
350
351 for (auto const id : _ids_of_active_elements)
352 {
353 (*_is_active)[id] = 1u;
354 }
355}
356
357std::vector<std::unique_ptr<SourceTermBase>> ProcessVariable::createSourceTerms(
358 const NumLib::LocalToGlobalIndexMap& dof_table, const int variable_id,
359 unsigned const integration_order,
360 std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
361 std::vector<std::reference_wrapper<ProcessVariable>> const&
362 all_process_variables_for_this_process,
363 const MeshLib::Mesh& bulk_mesh)
364{
365 std::vector<std::unique_ptr<SourceTermBase>> source_terms;
366
367 transform(cbegin(_source_term_configs), cend(_source_term_configs),
368 back_inserter(source_terms),
369 [&](auto const& config)
370 {
371 return createSourceTerm(
372 config, dof_table, config.mesh, variable_id,
373 integration_order, _shapefunction_order, parameters,
374 all_process_variables_for_this_process, bulk_mesh);
375 });
376
377 return source_terms;
378}
379
381
382} // namespace ProcessLib
#define OGS_FATAL(...)
Definition Error.h:26
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
Definition of the Mesh class.
Definition of the Node class.
std::optional< ConfigTree > getConfigSubtreeOptional(std::string const &root) const
std::optional< T > getConfigParameterOptional(std::string const &param) const
T getConfigParameter(std::string const &param) const
const Element * getElement(std::size_t idx) const
Get the element with the given index.
Definition Mesh.h:96
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:99
int getNumberOfVariableComponents(int variable_id) const
void updateDeactivatedSubdomains(double const time)
std::vector< std::unique_ptr< SourceTermBase > > createSourceTerms(const NumLib::LocalToGlobalIndexMap &dof_table, const int variable_id, unsigned const integration_order, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > const &parameters, std::vector< std::reference_wrapper< ProcessVariable > > const &all_process_variables_for_this_process, const MeshLib::Mesh &bulk_mesh)
std::vector< BoundaryConditionConfig > _bc_configs
ProcessVariable(BaseLib::ConfigTree const &config, MeshLib::Mesh &mesh, std::vector< std::unique_ptr< MeshLib::Mesh > > const &meshes, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > const &parameters, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves)
MeshLib::Mesh const & getMesh() const
Returns a mesh on which the process variable is defined.
std::vector< DeactivatedSubdomain > _deactivated_subdomains
std::string const & getName() const
MeshLib::PropertyVector< unsigned char > * _is_active
std::vector< std::size_t > _ids_of_active_elements
std::vector< SourceTermConfig > _source_term_configs
std::vector< std::unique_ptr< BoundaryCondition > > createBoundaryConditions(const NumLib::LocalToGlobalIndexMap &dof_table, const int variable_id, unsigned const integration_order, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > const &parameters, Process const &process, std::vector< std::reference_wrapper< ProcessVariable > > const &all_process_variables_for_this_process, std::map< int, std::shared_ptr< MaterialPropertyLib::Medium > > const &media)
void createBoundaryConditionsForDeactivatedSubDomains(const NumLib::LocalToGlobalIndexMap &dof_table, const int variable_id, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > const &parameters, std::vector< std::unique_ptr< BoundaryCondition > > &bcs)
std::string meshNameFromGeometry(std::string const &geometrical_set_name, std::string const &geometry_name)
Mesh & findMeshByName(std::vector< std::unique_ptr< Mesh > > const &meshes, std::string_view const name)
Definition Mesh.cpp:364
OGS_NO_DANGLING Parameter< ParameterDataType > & findParameter(std::string const &parameter_name, std::vector< std::unique_ptr< ParameterBase > > const &parameters, int const num_components, MeshLib::Mesh const *const mesh=nullptr)
Definition Utils.h:102
std::vector< DeactivatedSubdomain > createDeactivatedSubdomains(BaseLib::ConfigTree const &config, MeshLib::Mesh const &mesh, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > const &parameters, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves)
std::unique_ptr< BoundaryCondition > createBoundaryCondition(const BoundaryConditionConfig &config, const NumLib::LocalToGlobalIndexMap &dof_table, const MeshLib::Mesh &bulk_mesh, const int variable_id, const unsigned integration_order, const unsigned shapefunction_order, const std::vector< std::unique_ptr< ParameterLib::ParameterBase > > &parameters, const Process &process, std::vector< std::reference_wrapper< ProcessVariable > > const &all_process_variables_for_this_process, std::map< int, std::shared_ptr< MaterialPropertyLib::Medium > > const &media)
std::unique_ptr< SourceTermBase > createSourceTerm(const SourceTermConfig &config, const NumLib::LocalToGlobalIndexMap &dof_table_bulk, const MeshLib::Mesh &source_term_mesh, const int variable_id, const unsigned integration_order, const unsigned shapefunction_order, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > const &parameters, std::vector< std::reference_wrapper< ProcessVariable > > const &all_process_variables_for_this_process, const MeshLib::Mesh &bulk_mesh)
MeshLib::Mesh const & findMeshInConfig(BaseLib::ConfigTree const &config, std::vector< std::unique_ptr< MeshLib::Mesh > > const &meshes)
static PROCESSLIB_EXPORT const std::string zero_parameter_name