https://mooseframework.inl.gov
PODResidualTransfer.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 // StochasticTools includes
11 #include "PODResidualTransfer.h"
12 #include "NonlinearSystemBase.h"
13 
14 registerMooseObject("StochasticToolsApp", PODResidualTransfer);
15 
18 {
20  params.addClassDescription("Transfers residual vectors from the sub-application to a "
21  "a container in the Trainer object.");
22  params.suppressParameter<MultiMooseEnum>("direction");
23  params.suppressParameter<MultiAppName>("multi_app");
24  return params;
25 }
26 
28  : PODSamplerSolutionTransfer(parameters)
29 {
30  if (hasToMultiApp())
31  paramError("to_multi_app", "To and between multiapp directions are not implemented");
32 }
33 
34 void
36 {
37  const unsigned int total_base_num = _trainer.getSumBaseSize();
38 
39  // Looping over sub-apps
40  for (unsigned int base_i = 0; base_i < total_base_num; ++base_i)
41  if (getFromMultiApp()->hasLocalApp(base_i))
42  transferResidual(base_i, base_i);
43 }
44 
45 void
47 {
49 }
50 
51 void
53 {
54  const std::vector<std::string> & var_names = _trainer.getVarNames();
55  const std::vector<std::string> & tag_names = _trainer.getTagNames();
56  const std::vector<std::string> & tag_types = _trainer.getTagTypes();
57 
58  // Getting reference to the non-linear system
59  FEProblemBase & app_problem = getFromMultiApp()->appProblemBase(multi_app_i);
60  NonlinearSystemBase & nl = app_problem.getNonlinearSystemBase(/*nl_sys_num=*/0);
61 
62  // Looping over the residual tags and extracting the corresponding vector.
63  for (unsigned int tag_i = 0; tag_i < tag_names.size(); ++tag_i)
64  {
65  // If the tag corresponds to an independent operator, it is enough to
66  // transfer it once.
67  if (base_i > 0 && (tag_types[tag_i] == "src" || tag_types[tag_i] == "src_dir"))
68  continue;
69 
70  TagID tag_id = app_problem.getVectorTagID(tag_names[tag_i]);
71 
72  // Fetching the corresponding residual vector and extracting the parts for
73  // each variable.
74  NumericVector<Number> & full_residual = nl.getVector(tag_id);
75  std::vector<DenseVector<Real>> split_residual(var_names.size());
76 
77  for (unsigned int var_i = 0; var_i < var_names.size(); ++var_i)
78  {
79  // Getting the DoF indices of the variable.
80  nl.setVariableGlobalDoFs(var_names[var_i]);
81  const std::vector<dof_id_type> & var_dofs = nl.getVariableGlobalDoFs();
82 
83  // Extracting the corresponding part of the residual vector.
84  full_residual.localize(split_residual[var_i].get_values(), var_dofs);
85  }
86 
87  // Inserting the contribution of this residual into the reduced operator in
88  // the trainer.
89  _trainer.addToReducedOperator(base_i, tag_i, split_residual);
90  }
91 }
registerMooseObject("StochasticToolsApp", PODResidualTransfer)
virtual TagID getVectorTagID(const TagName &tag_name) const
dof_id_type _global_index
Index for tracking the row index when using batch mode operation.
void addToReducedOperator(unsigned int base_i, unsigned int tag_i, std::vector< DenseVector< Real >> &residual)
Adding the contribution of a residual to the reduced operators.
const std::shared_ptr< MultiApp > getFromMultiApp() const
unsigned int TagID
Transfer solutions from sub-applications to a container in a Trainer.
virtual void executeFromMultiapp() override
void suppressParameter(const std::string &name)
PODResidualTransfer(const InputParameters &parameters)
Transfers residuals for given variables and vector tags from a sub-subapplication to a PODReducedBasi...
static InputParameters validParams()
const std::vector< std::string > & getTagNames() const
void transferResidual(dof_id_type base_i, dof_id_type multi_app_i)
Adds the variable-residuals to the trainer.
const std::vector< dof_id_type > & getVariableGlobalDoFs()
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
void paramError(const std::string &param, Args... args) const
static InputParameters validParams()
const std::vector< std::string > & getTagTypes() const
bool hasToMultiApp() const
void addClassDescription(const std::string &doc_string)
virtual void execute() override
Transfer callback that will transfer residuals with given tags from the subapplication.
PODReducedBasisTrainer & _trainer
The trainer object to save the solution vector into or to fetch the artificial solution vectors from...
void setVariableGlobalDoFs(const std::string &var_name)
processor_id_type processor_id() const
const std::vector< std::string > & getVarNames() const
virtual NumericVector< Number > & getVector(const std::string &name)
unsigned int getSumBaseSize() const
Getting the overall base size, which is the sum of the individual bases.
uint8_t dof_id_type
virtual void localize(std::vector< Number > &v_local) const =0