OGS 6.2.0-97-g4a610c866
ProcessVariable.cpp
Go to the documentation of this file.
1 
10 #include "ProcessVariable.h"
11 
12 #include <algorithm>
13 #include <logog/include/logog.hpp>
14 #include <utility>
15 
16 #include "BaseLib/Algorithm.h"
17 #include "BaseLib/TimeInterval.h"
19 #include "MeshLib/Mesh.h"
20 #include "MeshLib/Node.h"
21 #include "ParameterLib/Utils.h"
27 
28 namespace
29 {
31  BaseLib::ConfigTree const& config,
32  std::vector<std::unique_ptr<MeshLib::Mesh>> const& meshes)
33 {
34  //
35  // Get the mesh name from the config.
36  //
37  std::string mesh_name; // Either given directly in <mesh> or constructed
38  // from <geometrical_set>_<geometry>.
39 
40 #ifdef DOXYGEN_DOCU_ONLY
41  config.getConfigParameterOptional<std::string>("mesh");
43 #endif // DOXYGEN_DOCU_ONLY
44 
45  auto optional_mesh_name =
47  config.getConfigParameterOptional<std::string>("mesh");
48  if (optional_mesh_name)
49  {
50  mesh_name = *optional_mesh_name;
51  }
52  else
53  {
54 #ifdef DOXYGEN_DOCU_ONLY
55  config.getConfigParameterOptional<std::string>("geometrical_set");
58  config.getConfigParameter<std::string>("geometry");
59 #endif // DOXYGEN_DOCU_ONLY
60 
61  // Looking for the mesh created before for the given geometry.
62  auto const geometrical_set_name =
64  config.getConfigParameter<std::string>("geometrical_set");
65  auto const geometry_name =
67  config.getConfigParameter<std::string>("geometry");
68 
69  mesh_name = MeshGeoToolsLib::meshNameFromGeometry(geometrical_set_name,
70  geometry_name);
71  }
72 
73  //
74  // Find and extract mesh from the list of meshes.
75  //
76  auto const& mesh = *BaseLib::findElementOrError(
77  begin(meshes), end(meshes),
78  [&mesh_name](auto const& mesh) {
79  assert(mesh != nullptr);
80  return mesh->getName() == mesh_name;
81  },
82  "Required mesh with name '" + mesh_name + "' not found.");
83  DBUG("Found mesh '%s' with id %d.", mesh.getName().c_str(), mesh.getID());
84 
85  return mesh;
86 }
87 } // namespace
88 
89 namespace ProcessLib
90 {
92  BaseLib::ConfigTree const& config,
93  MeshLib::Mesh& mesh,
94  std::vector<std::unique_ptr<MeshLib::Mesh>> const& meshes,
95  std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters)
96  :
97  _name(config.getConfigParameter<std::string>("name")),
98  _mesh(mesh),
100  _n_components(config.getConfigParameter<int>("components")),
102  _shapefunction_order(config.getConfigParameter<unsigned>("order")),
103  _deactivated_subdomains(createDeactivatedSubdomains(config, mesh)),
104  _initial_condition(ParameterLib::findParameter<double>(
106  config.getConfigParameter<std::string>("initial_condition"),
107  parameters, _n_components))
108 {
109  DBUG("Constructing process variable %s", _name.c_str());
110 
112  {
113  OGS_FATAL("The given shape function order %d is not supported",
115  }
116 
117  // Boundary conditions
118  if (auto bcs_config =
120  config.getConfigSubtreeOptional("boundary_conditions"))
121  {
122  for (
123  auto bc_config :
125  bcs_config->getConfigSubtreeList("boundary_condition"))
126  {
127  auto const& mesh = findMeshInConfig(bc_config, meshes);
128  auto component_id =
130  bc_config.getConfigParameterOptional<int>("component");
131 
132  if (!component_id && _n_components == 1)
133  {
134  // default value for single component vars.
135  component_id = 0;
136  }
137 
138  _bc_configs.emplace_back(std::move(bc_config), mesh, component_id);
139  }
140  }
141  else
142  {
143  INFO("No boundary conditions for process variable '%s' found.",
144  _name.c_str());
145  }
146 
147  // Source terms
149  if (auto sts_config = config.getConfigSubtreeOptional("source_terms"))
150  {
151  for (
152  auto st_config :
154  sts_config->getConfigSubtreeList("source_term"))
155  {
156  MeshLib::Mesh const& mesh = findMeshInConfig(st_config, meshes);
157  auto component_id =
159  st_config.getConfigParameterOptional<int>("component");
160 
161  if (!component_id && _n_components == 1)
162  {
163  // default value for single component vars.
164  component_id = 0;
165  }
166 
167  _source_term_configs.emplace_back(std::move(st_config), mesh,
168  component_id);
169  }
170  }
171  else
172  {
173  INFO("No source terms for process variable '%s' found.", _name.c_str());
174  }
175 }
176 
178  : _name(std::move(other._name)),
179  _mesh(other._mesh),
183  _initial_condition(std::move(other._initial_condition)),
184  _bc_configs(std::move(other._bc_configs)),
186 {
187 }
188 
189 std::string const& ProcessVariable::getName() const
190 {
191  return _name;
192 }
193 
195 {
196  return _mesh;
197 }
198 
199 std::vector<std::unique_ptr<BoundaryCondition>>
201  const NumLib::LocalToGlobalIndexMap& dof_table,
202  const int variable_id,
203  unsigned const integration_order,
204  std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
205  Process const& process)
206 {
207  std::vector<std::unique_ptr<BoundaryCondition>> bcs;
208  bcs.reserve(_bc_configs.size());
209 
210  for (auto& config : _bc_configs)
211  {
212  auto bc = createBoundaryCondition(
213  config, dof_table, _mesh, variable_id, integration_order,
214  _shapefunction_order, parameters, process);
215 #ifdef USE_PETSC
216  if (bc == nullptr)
217  {
218  continue;
219  }
220 #endif // USE_PETSC
221  bcs.push_back(std::move(bc));
222  }
223 
224  if (_deactivated_subdomains.empty())
225  {
226  return bcs;
227  }
228 
230  parameters, bcs);
231  return bcs;
232 }
233 
235  const NumLib::LocalToGlobalIndexMap& dof_table, const int variable_id,
236  std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
237  std::vector<std::unique_ptr<BoundaryCondition>>& bcs)
238 {
239  auto& parameter = ParameterLib::findParameter<double>(
241 
242  for (auto const& deactivated_subdomain : _deactivated_subdomains)
243  {
244  auto const& deactivated_subdomain_meshes =
245  deactivated_subdomain->deactivated_subdomain_meshes;
246  for (auto const& deactivated_subdomain_mesh :
247  deactivated_subdomain_meshes)
248  {
249  for (int component_id = 0;
250  component_id < dof_table.getNumberOfComponents();
251  component_id++)
252  {
253  // Copy the time interval.
254  std::unique_ptr<BaseLib::TimeInterval> time_interval =
255  std::make_unique<BaseLib::TimeInterval>(
256  *deactivated_subdomain->time_interval);
257 
258  auto bc = std::make_unique<
260  std::move(time_interval), parameter,
261  *(deactivated_subdomain_mesh->mesh),
262  deactivated_subdomain_mesh->inactive_nodes, dof_table,
263  variable_id, component_id);
264 
265 #ifdef USE_PETSC
266  // TODO: make it work under PETSc too.
267  if (bc == nullptr)
268  {
269  continue;
270  }
271 #endif // USE_PETSC
272  bcs.push_back(std::move(bc));
273  }
274  }
275  }
276 }
277 
279 {
280  if (_deactivated_subdomains.empty())
281  {
282  _ids_of_active_elements.clear();
283  return;
284  }
285 
286  auto found_a_set =
287  std::find_if(_deactivated_subdomains.begin(),
289  [&](auto& _deactivated_subdomain) {
290  return _deactivated_subdomain->includesTimeOf(time);
291  });
292 
293  if (found_a_set == _deactivated_subdomains.end())
294  {
295  _ids_of_active_elements.clear();
296  return;
297  }
298 
299  // Already initialized.
300  if (!_ids_of_active_elements.empty())
301  {
302  return;
303  }
304 
305  auto const& deactivated_materialIDs = (*found_a_set)->materialIDs;
306 
307  auto const* const material_ids = MeshLib::materialIDs(_mesh);
308  _ids_of_active_elements.clear();
309  auto const number_of_elements = _mesh.getNumberOfElements();
310 
311  for (std::size_t i = 0; i < number_of_elements; i++)
312  {
313  if (std::binary_search(deactivated_materialIDs.begin(),
314  deactivated_materialIDs.end(),
315  (*material_ids)[i]))
316  {
317  continue;
318  }
320  }
321 }
322 
323 std::vector<std::unique_ptr<SourceTerm>> ProcessVariable::createSourceTerms(
324  const NumLib::LocalToGlobalIndexMap& dof_table,
325  const int variable_id,
326  unsigned const integration_order,
327  std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters)
328 {
329  std::vector<std::unique_ptr<SourceTerm>> source_terms;
330 
331  for (auto& config : _source_term_configs)
332  {
333  source_terms.emplace_back(createSourceTerm(
334  config, dof_table, config.mesh, variable_id, integration_order,
335  _shapefunction_order, parameters));
336  }
337 
338  return source_terms;
339 }
340 
341 } // namespace ProcessLib
void updateDeactivatedSubdomains(double const time)
std::vector< std::unique_ptr< SourceTerm > > 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::unique_ptr< DeactivatedSubdomain const > > _deactivated_subdomains
const Element * getElement(std::size_t idx) const
Get the element with the given index.
Definition: Mesh.h:87
std::string meshNameFromGeometry(std::string const &geometrical_set_name, std::string const &geometry_name)
std::vector< SourceTermConfig > _source_term_configs
Definition of the Node class.
std::vector< std::size_t > _ids_of_active_elements
std::unique_ptr< SourceTerm > 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)
Definition of the Mesh class.
T getConfigParameter(std::string const &param) const
static const std::string zero_parameter_name
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)
Parameter< ParameterDataType > & findParameter(std::string const &parameter_name, std::vector< std::unique_ptr< ParameterBase >> const &parameters, int const num_components)
Definition: Utils.h:84
ParameterLib::Parameter< double > const & _initial_condition
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::string const & getName() const
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition: Mesh.cpp:403
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< BoundaryConditionConfig > _bc_configs
virtual std::size_t getID() const final
Returns the ID of the element.
Definition: Element.h:90
std::size_t getNumberOfElements() const
Get the number of elements.
Definition: Mesh.h:96
boost::optional< T > getConfigParameterOptional(std::string const &param) const
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
boost::optional< ConfigTree > getConfigSubtreeOptional(std::string const &root) const
Definition: ConfigTree.cpp:160
MeshLib::Mesh const & getMesh() const
Returns a mesh on which the process variable is defined.
std::vector< std::unique_ptr< DeactivatedSubdomain const > > createDeactivatedSubdomains(BaseLib::ConfigTree const &config, MeshLib::Mesh const &mesh)
std::iterator_traits< InputIt >::reference findElementOrError(InputIt begin, InputIt end, Predicate predicate, std::string const &error="")
Definition: Algorithm.h:67
MeshLib::Mesh const & findMeshInConfig(BaseLib::ConfigTree const &config, std::vector< std::unique_ptr< MeshLib::Mesh >> const &meshes)
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)