https://mooseframework.inl.gov
PODSamplerSolutionTransfer.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
12 #include "NonlinearSystemBase.h"
13 #include "Sampler.h"
14 
15 registerMooseObject("StochasticToolsApp", PODSamplerSolutionTransfer);
16 
19 {
21  params.addClassDescription("Transfers solution vectors from the sub-applications to a "
22  "a container in the Trainer object and back.");
23  params.addRequiredParam<UserObjectName>("trainer_name",
24  "Trainer object that contains the solutions"
25  " for different samples.");
26  return params;
27 }
28 
30  : StochasticToolsTransfer(parameters),
32  _pod_multi_app(hasFromMultiApp()
33  ? std::dynamic_pointer_cast<PODFullSolveMultiApp>(getFromMultiApp())
34  : std::dynamic_pointer_cast<PODFullSolveMultiApp>(getToMultiApp())),
35  _trainer(getSurrogateTrainer<PODReducedBasisTrainer>("trainer_name"))
36 {
37  if (!_pod_multi_app)
38  paramError("multi_app", "The Multiapp given is not a PODFullsolveMultiapp!");
39 }
40 
41 void
43 {
44  const auto multi_app = hasFromMultiApp() ? getFromMultiApp() : getToMultiApp();
45 
46  // Checking if the subapplication has the requested variables
47  const std::vector<std::string> & var_names = _trainer.getVarNames();
48  const dof_id_type n = multi_app->numGlobalApps();
49  for (MooseIndex(n) i = 0; i < n; i++)
50  {
51  if (multi_app->hasLocalApp(i))
52  for (auto var_name : var_names)
53  if (!multi_app->appProblemBase(i).hasVariable(var_name))
54  mooseError("Variable '" + var_name + "' not found on sub-application ", i, "!");
55  }
56 }
57 
58 void
60 {
61 
62  const std::vector<std::string> & var_names = _trainer.getVarNames();
63 
64  // Selecting the appropriate action based on the drection.
65  switch (_direction)
66  {
67  case FROM_MULTIAPP:
68 
69  // Looping over sub-apps created for different samples
71  ++i)
72  {
73  // Getting reference to the solution vector of the sub-app.
74  FEProblemBase & app_problem = getFromMultiApp()->appProblemBase(i);
75  NonlinearSystemBase & nl = app_problem.getNonlinearSystemBase(/*nl_sys_num=*/0);
76  NumericVector<Number> & solution = nl.solution();
77 
78  // Looping over the variables to extract the corresponding solution values
79  // and copy them into the container of the trainer.
80  for (unsigned int v_index = 0; v_index < var_names.size(); ++v_index)
81  {
82  // Getting the corresponding DoF indices for the variable.
83  nl.setVariableGlobalDoFs(var_names[v_index]);
84  const std::vector<dof_id_type> & var_dofs = nl.getVariableGlobalDoFs();
85 
86  // Initializing a temporary vector for the partial solution.
87  std::shared_ptr<DenseVector<Real>> tmp = std::make_shared<DenseVector<Real>>();
88  solution.localize(tmp->get_values(), var_dofs);
89 
90  // Copying the temporary vector into the trainer.
91  _trainer.addSnapshot(v_index, i, tmp);
92  }
93  }
94  break;
95 
96  case TO_MULTIAPP:
97 
98  // Looping over all the variables in the trainer to copy the corresponding
99  // basis vectors into the solution.
100  unsigned int counter = 0;
101  for (unsigned int var_i = 0; var_i < var_names.size(); ++var_i)
102  {
103  // Looping over the bases of the given variable and plugging them into
104  // a sub-application.
105  unsigned int var_base_num = _trainer.getBaseSize(var_i);
106  for (unsigned int base_i = 0; base_i < var_base_num; ++base_i)
107  {
108  if (getToMultiApp()->hasLocalApp(counter))
109  {
110  // Getting the reference to the solution vector in the subapp.
111  FEProblemBase & app_problem = getToMultiApp()->appProblemBase(counter);
112  NonlinearSystemBase & nl = app_problem.getNonlinearSystemBase(/*nl_sys_num=*/0);
113  NumericVector<Number> & solution = nl.solution();
114 
115  // Zeroing the solution to make sure that only the required part
116  // is non-zero after copy.
117  solution.zero();
118 
119  // Getting the degrees of freedom for the given variable.
120  nl.setVariableGlobalDoFs(var_names[var_i]);
121  const std::vector<dof_id_type> & var_dofs = nl.getVariableGlobalDoFs();
122 
123  // Fetching the basis vector and plugging it into the solution.
124  const DenseVector<Real> & base_vector = _trainer.getBasisVector(var_i, base_i);
125  solution.insert(base_vector, var_dofs);
126  solution.close();
127 
128  // Make sure that the sub-application uses this vector to evaluate the
129  // residual.
130  nl.setSolution(solution);
131  }
132  counter++;
133  }
134  }
135  break;
136  }
137 }
138 
139 void
141 {
142 }
143 
144 void
146 {
147  if (_pod_multi_app->snapshotGeneration())
148  {
149  const std::vector<std::string> & var_names = _trainer.getVarNames();
150 
151  const dof_id_type n = getFromMultiApp()->numGlobalApps();
152 
153  for (MooseIndex(n) i = 0; i < n; i++)
154  {
155  if (getFromMultiApp()->hasLocalApp(i))
156  {
157  // Getting reference to the solution vector of the sub-app.
158  FEProblemBase & app_problem = getFromMultiApp()->appProblemBase(i);
159  NonlinearSystemBase & nl = app_problem.getNonlinearSystemBase(/*nl_sys_num=*/0);
160  NumericVector<Number> & solution = nl.solution();
161 
162  // Looping over the variables to extract the corresponding solution values
163  // and copy them into the container of the trainer.
164  for (unsigned int var_i = 0; var_i < var_names.size(); ++var_i)
165  {
166  // Getting the corresponding DoF indices for the variable.
167  nl.setVariableGlobalDoFs(var_names[var_i]);
168  const std::vector<dof_id_type> & var_dofs = nl.getVariableGlobalDoFs();
169 
170  // Initializing a temporary vector for the partial solution.
171  std::shared_ptr<DenseVector<Real>> tmp = std::make_shared<DenseVector<Real>>();
172  solution.localize(tmp->get_values(), var_dofs);
173 
174  // Copying the temporary vector into the trainer.
175  _trainer.addSnapshot(var_i, _global_index, tmp);
176  }
177  }
178  }
179  }
180 }
181 
182 void
184 {
185 }
186 
187 void
189 {
190 }
191 
192 void
194 {
195  if (!_pod_multi_app->snapshotGeneration())
196  {
197  const std::vector<std::string> & var_names = _trainer.getVarNames();
199 
200  // Getting the reference to the solution vector in the subapp.
201  FEProblemBase & app_problem = getToMultiApp()->appProblemBase(processor_id());
202  NonlinearSystemBase & nl = app_problem.getNonlinearSystemBase(/*nl_sys_num=*/0);
203  NumericVector<Number> & solution = nl.solution();
204 
205  // Zeroing the solution to make sure that only the required part
206  // is non-zero after copy.
207  solution.zero();
208 
209  // Getting the degrees of freedom for the given variable.
210  nl.setVariableGlobalDoFs(var_names[var_i]);
211  const std::vector<dof_id_type> & var_dofs = nl.getVariableGlobalDoFs();
212 
213  // Fetching the basis vector and plugging it into the solution.
214  const DenseVector<Real> & base_vector = _trainer.getBasisVector(_global_index);
215  solution.insert(base_vector, var_dofs);
216  solution.close();
217 
218  // Make sure that the sub-application uses this vector to evaluate the
219  // residual.
220  nl.setSolution(solution);
221  }
222 }
223 
224 void
226 {
227 }
dof_id_type _global_index
Index for tracking the row index when using batch mode operation.
virtual void insert(const Number *v, const std::vector< numeric_index_type > &dof_indices)
Sampler * _sampler_ptr
Pointer to the Sampler object used by the SamplerTransientMultiApp or SamplerFullSolveMultiApp.
std::shared_ptr< PODFullSolveMultiApp > _pod_multi_app
The input multiapp casted into a PODFullSolveMultiapp to get access to the specific pod attributes...
virtual void executeFromMultiapp() override
const std::shared_ptr< MultiApp > getFromMultiApp() const
NumericVector< Number > & solution()
Transfer solutions from sub-applications to a container in a Trainer.
virtual void executeToMultiapp() override
registerMooseObject("StochasticToolsApp", PODSamplerSolutionTransfer)
dof_id_type getLocalRowBegin() const
void setSolution(const NumericVector< Number > &soln)
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
const std::shared_ptr< MultiApp > getToMultiApp() const
virtual void finalizeToMultiapp() override
bool hasFromMultiApp() const
PODSamplerSolutionTransfer(const InputParameters &parameters)
void addRequiredParam(const std::string &name, const std::string &doc_string)
The class creates an additional API to allow Transfers to work when running the StochasticTools<FullS...
void addSnapshot(unsigned int var_i, unsigned int glob_i, const std::shared_ptr< DenseVector< Real >> &snapshot)
Adding a snapshot for a variable.
virtual void zero()=0
virtual void initializeToMultiapp() override
Methods for transferring data to sub-applications to the master application.
static InputParameters validParams()
const std::vector< dof_id_type > & getVariableGlobalDoFs()
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
virtual void initialSetup() override
void paramError(const std::string &param, Args... args) const
virtual void initializeFromMultiapp() override
Methods used when running in batch mode (see SamplerFullSolveMultiApp)
unsigned int getVariableIndex(unsigned int glob_i) const
Getting appropriate variable index for a global base index.
virtual void close()=0
dof_id_type getLocalRowEnd() const
static InputParameters validParams()
Interface for objects that need to use samplers.
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
MooseEnum _direction
unsigned int getBaseSize(unsigned int var_i) const
Getting the base size for variable with index v_ind.
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 void finalizeFromMultiapp() override
const DenseVector< Real > & getBasisVector(unsigned int var_i, unsigned int base_i) const
Getting a basis vector for a given variable.
uint8_t dof_id_type
virtual void localize(std::vector< Number > &v_local) const =0