OGS
AddProcessDataToMesh.cpp
Go to the documentation of this file.
1
12
13#include "InfoLib/GitInfo.h"
19#ifdef USE_PETSC
21#endif
23
35
37 NumLib::Time const& t,
38 std::vector<GlobalVector*> const& x,
39 std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_tables,
41 std::string const& output_name,
42 MeshLib::Mesh& mesh)
43{
44 DBUG(" secondary variable {:s}", output_name);
45
46 auto& nodal_values_mesh = *MeshLib::getOrCreateMeshProperty<double>(
47 mesh, output_name, MeshLib::MeshItemType::Node,
49 if (nodal_values_mesh.size() !=
51 {
53 "Nodal property `{:s}' does not have the right number of "
54 "components. Expected: {:d}, actual: {:d}",
55 output_name,
57 nodal_values_mesh.size());
58 }
59
60 std::unique_ptr<GlobalVector> result_cache;
61 auto const& nodal_values =
62 var.fcts.eval_field(t(), x, dof_tables, result_cache);
63
64#ifdef USE_PETSC
65 std::size_t const global_vector_size =
66 nodal_values.getLocalSize() + nodal_values.getGhostSize();
67#else
68 std::size_t const global_vector_size = nodal_values.size();
69#endif
70 if (nodal_values_mesh.size() != global_vector_size)
71 {
73 "Secondary variable `{:s}' did not evaluate to the right number of "
74 "components. Expected: {:d}, actual: {:d}.",
75 var.name, nodal_values_mesh.size(), global_vector_size);
76 }
77
78 // Copy result
79 nodal_values.copyValues(nodal_values_mesh);
80}
81
83 NumLib::Time const& t,
84 std::vector<GlobalVector*> const& x,
85 std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_table,
87 std::string const& output_name,
88 MeshLib::Mesh& mesh)
89{
90 if (!var.fcts.eval_residuals)
91 {
92 return;
93 }
94
95 DBUG(" secondary variable {:s} residual", output_name);
96 auto const& property_name_res = output_name + "_residual";
97
98 auto& residuals_mesh = *MeshLib::getOrCreateMeshProperty<double>(
99 mesh, property_name_res, MeshLib::MeshItemType::Cell,
100 var.fcts.num_components);
101 if (residuals_mesh.size() !=
103 {
104 OGS_FATAL(
105 "Cell property `{:s}' does not have the right number of "
106 "components. Expected: {:d}, actual: {:d}",
107 property_name_res,
109 residuals_mesh.size());
110 }
111
112 std::unique_ptr<GlobalVector> result_cache;
113 auto const& residuals =
114 var.fcts.eval_residuals(t(), x, dof_table, result_cache);
115#ifdef USE_PETSC
116 std::size_t const global_vector_size =
117 residuals.getLocalSize() + residuals.getGhostSize();
118#else
119 std::size_t const global_vector_size = residuals.size();
120#endif
121 if (residuals_mesh.size() != global_vector_size)
122 {
123 OGS_FATAL(
124 "The residual of secondary variable `{:s}' did not evaluate to the "
125 "right number of components. Expected: {:d}, actual: {:d}.",
126 var.name, residuals_mesh.size(), global_vector_size);
127 }
128
129 // Copy result
130 residuals.copyValues(residuals_mesh);
131}
132
133static std::vector<double> copySolutionVector(GlobalVector const& x)
134{
135 std::vector<double> x_copy;
136 x.copyValues(x_copy);
137 return x_copy;
138}
139
141 [[maybe_unused]] MeshLib::Mesh const& mesh,
142 [[maybe_unused]] NumLib::LocalToGlobalIndexMap const& dof_table,
143 [[maybe_unused]] NumLib::LocalToGlobalIndexMap const& bulk_mesh_dof_table)
144{
145#ifdef USE_PETSC
146
147 if (&bulk_mesh_dof_table != &dof_table)
148 {
149 auto const bulk_id_string =
151 if (!mesh.getProperties().existsPropertyVector<std::size_t>(
152 bulk_id_string))
153 {
154 OGS_FATAL(
155 "The required bulk node ids map does not exist in "
156 "the boundary mesh '{:s}' or has the wrong data "
157 "type (should be equivalent to C++ data type "
158 "std::size_t which is an unsigned integer of size "
159 "{:d} or UInt64 in vtk terminology).",
160 mesh.getName(), sizeof(std::size_t));
161 }
162 return mesh.getProperties().getPropertyVector<std::size_t>(
163 bulk_id_string);
164 }
165#endif
166
167 return nullptr;
168}
169
171 std::size_t const mesh_id, std::size_t const node_id,
172 [[maybe_unused]] bool const is_ghost_node, int const global_component_id,
173 GlobalVector const& x, NumLib::LocalToGlobalIndexMap const& dof_table,
174 [[maybe_unused]] NumLib::LocalToGlobalIndexMap const& bulk_mesh_dof_table,
175 [[maybe_unused]] MeshLib::PropertyVector<std::size_t> const* const
176 bulk_node_id_map)
177{
178#ifdef USE_PETSC
179 if (is_ghost_node && &bulk_mesh_dof_table != &dof_table)
180 {
181 auto const bulk_node_id = (*bulk_node_id_map)[node_id];
182 std::size_t const bulk_mesh_id = 0;
183
184 MeshLib::Location const loc(bulk_mesh_id, MeshLib::MeshItemType::Node,
185 bulk_node_id);
186
187 // early return!
188 return bulk_mesh_dof_table.getLocalIndex(
189 loc, global_component_id, x.getRangeBegin(), x.getRangeEnd());
190 }
191#endif
192
193 MeshLib::Location const loc(mesh_id, MeshLib::MeshItemType::Node, node_id);
194
195 return dof_table.getLocalIndex(loc, global_component_id, x.getRangeBegin(),
196 x.getRangeEnd());
197}
198
199static bool isGhostNode([[maybe_unused]] MeshLib::Mesh const& mesh,
200 [[maybe_unused]] std::size_t const node_id)
201{
202#ifndef USE_PETSC
203 return false;
204#else
205 return static_cast<MeshLib::NodePartitionedMesh const&>(mesh).isGhostNode(
206 node_id);
207#endif
208}
209
239static std::set<std::string> addPrimaryVariablesToMesh(
240 MeshLib::Mesh& mesh,
241 GlobalVector const& x,
242 std::vector<std::reference_wrapper<ProcessLib::ProcessVariable>> const&
243 process_variables,
244 std::set<std::string> const& output_variables,
245 NumLib::LocalToGlobalIndexMap const& dof_table,
246 NumLib::LocalToGlobalIndexMap const& bulk_mesh_dof_table)
247{
248 if (dof_table.getNumberOfVariables() !=
249 bulk_mesh_dof_table.getNumberOfVariables() ||
250 dof_table.getNumberOfGlobalComponents() !=
251 bulk_mesh_dof_table.getNumberOfGlobalComponents())
252 {
253 OGS_FATAL(
254 "The d.o.f. table for the passed mesh must have the same number of "
255 "variables and global components as the d.o.f. table for the full "
256 "simulation domain. But the values differ: {} != {} (variables) or "
257 "{} != {} (global components).",
258 dof_table.getNumberOfVariables(),
259 bulk_mesh_dof_table.getNumberOfVariables(),
260 dof_table.getNumberOfGlobalComponents(),
261 bulk_mesh_dof_table.getNumberOfGlobalComponents());
262 }
263
264 auto const number_of_dof_variables = dof_table.getNumberOfVariables();
265
266 if (number_of_dof_variables != static_cast<int>(process_variables.size()))
267 {
268 OGS_FATAL(
269 "The number of variables in the d.o.f. table differs from the "
270 "number of primary variables of the process {} != {}.",
271 number_of_dof_variables, process_variables.size());
272 }
273
274 auto const x_copy = copySolutionVector(x);
275 std::set<std::string> names_of_already_output_variables;
276
277 int global_component_offset_next = 0;
278
279 auto const* const bulk_node_id_map = getBulkNodeIdMapForPetscIfNecessary(
280 mesh, dof_table, bulk_mesh_dof_table);
281
282 for (int variable_id = 0; variable_id < number_of_dof_variables;
283 ++variable_id)
284 {
285 auto const& pv = process_variables[variable_id].get();
286 auto const n_components = pv.getNumberOfGlobalComponents();
287
288 // increase global component offset even if we do not add anything in
289 // this iteration
290 int global_component_offset = global_component_offset_next;
291 global_component_offset_next += n_components;
292
293 if (!output_variables.empty() &&
294 !output_variables.contains(pv.getName()))
295 {
296 continue;
297 }
298
299 names_of_already_output_variables.insert(pv.getName());
300
301 DBUG(" process variable {:s}", pv.getName());
302
303 auto& output_data = *MeshLib::getOrCreateMeshProperty<double>(
304 mesh, pv.getName(), MeshLib::MeshItemType::Node, n_components);
305
306 // mesh subsets are the same for all components
307 int const dummy_component_id = 0;
308 auto const& mesh_subset =
309 dof_table.getMeshSubset(variable_id, dummy_component_id);
310 auto const mesh_id = mesh_subset.getMeshID();
311
312 for (auto const* node : mesh_subset.getNodes())
313 {
314 auto const node_id = node->getID();
315 auto const is_ghost_node = isGhostNode(mesh, node_id);
316
317 for (int component_id = 0; component_id < n_components;
318 ++component_id)
319 {
320 auto const global_component_id =
321 global_component_offset + component_id;
322
323 auto const in_index = getIndexForComponentInSolutionVector(
324 mesh_id, node_id, is_ghost_node, global_component_id, x,
325 dof_table, bulk_mesh_dof_table, bulk_node_id_map);
326
327 // per node ordering of components
328 auto const out_index = node_id * n_components + component_id;
329
330 // request for index of linear quantities at higher order nodes
331 // results in returning NumLib::MeshComponentMap::nop
332 if (in_index == NumLib::MeshComponentMap::nop)
333 {
334 output_data[out_index] = 0;
335 continue;
336 }
337
338 output_data[out_index] = x_copy[in_index];
339 }
340 }
341 }
342
343 return names_of_already_output_variables;
344}
345
347 ProcessLib::SecondaryVariableCollection const& secondary_variables,
348 std::set<std::string>& names_of_already_output_variables,
349 NumLib::Time const& t, std::vector<GlobalVector*> const& xs,
350 MeshLib::Mesh& mesh,
351 std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_tables,
352 bool const output_residuals)
353{
354 for (auto const& external_variable_name : secondary_variables)
355 {
356 auto const& name = external_variable_name.first;
357 if (!names_of_already_output_variables.insert(name).second)
358 {
359 // no insertion took place, output already done
360 continue;
361 }
362
363 addSecondaryVariableNodes(t, xs, dof_tables,
364 secondary_variables.get(name), name, mesh);
365
366 if (output_residuals)
367 {
369 t, xs, dof_tables, secondary_variables.get(name), name, mesh);
370 }
371 }
372}
373
374namespace ProcessLib
375{
377 std::vector<GlobalVector*> const& xs,
378 int const process_id,
379 ProcessOutputData const& process_output_data,
380 bool const output_secondary_variables,
381 OutputDataSpecification const& process_output)
382{
383 DBUG("Process output data.");
384
385 auto const& pod = process_output_data;
386 auto const& process_variables = pod.getProcessVariables(process_id);
387 auto const& secondary_variables = pod.getSecondaryVariables();
388 auto const* const integration_point_writers =
389 pod.getIntegrationPointWriters();
390 auto const& bulk_mesh_dof_table = pod.getBulkMeshDofTable(process_id);
391 auto const& output_mesh_dof_table = pod.getOutputMeshDofTable(process_id);
392 auto& output_mesh = pod.getOutputMesh();
393
394 auto const& output_variables = process_output.output_variables;
395
396 addOgsVersion(output_mesh);
397
398 auto names_of_already_output_variables = addPrimaryVariablesToMesh(
399 output_mesh, *xs[process_id], process_variables, output_variables,
400 output_mesh_dof_table, bulk_mesh_dof_table);
401
402 if (output_secondary_variables)
403 {
404 auto const& output_mesh_dof_tables =
405 pod.getOutputMeshDofTablesOfAllProcesses();
406
407 addSecondaryVariablesToMesh(secondary_variables,
408 names_of_already_output_variables, t, xs,
409 output_mesh, output_mesh_dof_tables,
410 process_output.output_residuals);
411 }
412
413 if (integration_point_writers)
414 {
415 addIntegrationPointDataToMesh(output_mesh, *integration_point_writers);
416 }
417}
418} // namespace ProcessLib
static void addSecondaryVariableNodes(NumLib::Time const &t, std::vector< GlobalVector * > const &x, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_tables, ProcessLib::SecondaryVariable const &var, std::string const &output_name, MeshLib::Mesh &mesh)
static std::vector< double > copySolutionVector(GlobalVector const &x)
static GlobalIndexType getIndexForComponentInSolutionVector(std::size_t const mesh_id, std::size_t const node_id, bool const is_ghost_node, int const global_component_id, GlobalVector const &x, NumLib::LocalToGlobalIndexMap const &dof_table, NumLib::LocalToGlobalIndexMap const &bulk_mesh_dof_table, MeshLib::PropertyVector< std::size_t > const *const bulk_node_id_map)
MeshLib::PropertyVector< std::size_t > const * getBulkNodeIdMapForPetscIfNecessary(MeshLib::Mesh const &mesh, NumLib::LocalToGlobalIndexMap const &dof_table, NumLib::LocalToGlobalIndexMap const &bulk_mesh_dof_table)
static std::set< std::string > addPrimaryVariablesToMesh(MeshLib::Mesh &mesh, GlobalVector const &x, std::vector< std::reference_wrapper< ProcessLib::ProcessVariable > > const &process_variables, std::set< std::string > const &output_variables, NumLib::LocalToGlobalIndexMap const &dof_table, NumLib::LocalToGlobalIndexMap const &bulk_mesh_dof_table)
static void addSecondaryVariablesToMesh(ProcessLib::SecondaryVariableCollection const &secondary_variables, std::set< std::string > &names_of_already_output_variables, NumLib::Time const &t, std::vector< GlobalVector * > const &xs, MeshLib::Mesh &mesh, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_tables, bool const output_residuals)
static void addSecondaryVariableResiduals(NumLib::Time const &t, std::vector< GlobalVector * > const &x, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_table, ProcessLib::SecondaryVariable const &var, std::string const &output_name, MeshLib::Mesh &mesh)
static bool isGhostNode(MeshLib::Mesh const &mesh, std::size_t const node_id)
static void addOgsVersion(MeshLib::Mesh &mesh)
#define OGS_FATAL(...)
Definition Error.h:26
Git information.
GlobalMatrix::IndexType GlobalIndexType
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
Definition of mesh class for partitioned mesh (by node) for parallel computing within the framework o...
Global vector based on Eigen vector.
Definition EigenVector.h:25
IndexType getRangeEnd() const
return an end index of the active data range
Definition EigenVector.h:48
void copyValues(std::vector< double > &u) const
static constexpr IndexType getRangeBegin()
return a start index of the active data range
Definition EigenVector.h:45
std::size_t getMeshID() const
return this mesh ID
Definition MeshSubset.h:76
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:103
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:100
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:97
MeshLib::MeshSubset const & getMeshSubset(int const variable_id, int const component_id) const
GlobalIndexType getLocalIndex(MeshLib::Location const &l, std::size_t const comp_id, std::size_t const range_begin, std::size_t const range_end) const
static constexpr NUMLIB_EXPORT GlobalIndexType const nop
Holds all data of a process that are needed for output.
std::vector< std::reference_wrapper< ProcessVariable > > const & getProcessVariables(int const process_id) const
Handles configuration of several secondary variables from the project file.
SecondaryVariable const & get(std::string const &external_name) const
Returns the secondary variable with the given external name.
const std::string OGS_VERSION
Definition GitInfo.cpp:20
GITINFOLIB_EXPORT const std::string ogs_version
PropertyVector< T > * getOrCreateMeshProperty(Mesh &mesh, std::string const &property_name, MeshItemType const item_type, int const number_of_components)
constexpr std::string_view getBulkIDString(MeshItemType mesh_item_type)
Definition Properties.h:188
void addProcessDataToMesh(NumLib::Time const &t, std::vector< GlobalVector * > const &xs, int const process_id, ProcessOutputData const &process_output_data, bool const output_secondary_variables, OutputDataSpecification const &process_output)
Holds information about which variables to write to output files.
bool output_residuals
Tells if also to output extrapolation residuals.
std::set< std::string > output_variables
All variables that shall be output.
Function const eval_field
Computes the value of the field at every node of the underlying mesh.
const unsigned num_components
Number of components of the variable.
Stores information about a specific secondary variable.
SecondaryVariableFunctions fcts
Functions used for computing the secondary variable.
std::string const name
Name of the variable; used, e.g., for output.