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