www.mooseframework.org
MooseEigenSystem.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 #include "MooseEigenSystem.h"
15 
16 #include "MaterialData.h"
17 #include "Factory.h"
18 #include "EigenKernel.h"
19 
20 MooseEigenSystem::MooseEigenSystem(FEProblemBase & fe_problem, const std::string & name)
21  : NonlinearSystem(fe_problem, name),
22  _all_eigen_vars(false),
23  _active_on_old(false),
24  _eigen_kernel_counter(0)
25 {
26 }
27 
29 
30 void
31 MooseEigenSystem::addKernel(const std::string & kernel_name,
32  const std::string & name,
33  InputParameters parameters)
34 {
35  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
36  {
37  // In the case of EigenKernels, we might need to add two to the system
38  if (parameters.have_parameter<bool>("eigen"))
39  {
40  {
41  // EigenKernel
42  parameters.set<bool>("implicit") = true;
43  std::shared_ptr<KernelBase> ekernel =
44  _factory.create<KernelBase>(kernel_name, name, parameters, tid);
45  if (parameters.get<bool>("eigen"))
46  markEigenVariable(parameters.get<NonlinearVariableName>("variable"));
47  _kernels.addObject(ekernel, tid);
48  }
49  if (parameters.get<bool>("eigen"))
50  {
51  // EigenKernel_old
52  parameters.set<bool>("implicit") = false;
53  std::string old_name(name + "_old");
54 
55  std::shared_ptr<KernelBase> ekernel =
56  _factory.create<KernelBase>(kernel_name, old_name, parameters, tid);
57  _eigen_var_names.insert(parameters.get<NonlinearVariableName>("variable"));
58  _kernels.addObject(ekernel, tid);
60  }
61  }
62  else // Standard nonlinear system kernel
63  {
64  // Create the kernel object via the factory
65  std::shared_ptr<KernelBase> kernel =
66  _factory.create<KernelBase>(kernel_name, name, parameters, tid);
67  _kernels.addObject(kernel, tid);
68  }
69  }
70 
71  if (parameters.get<std::vector<AuxVariableName>>("save_in").size() > 0)
72  _has_save_in = true;
73  if (parameters.get<std::vector<AuxVariableName>>("diag_save_in").size() > 0)
74  _has_diag_save_in = true;
75 }
76 
77 void
78 MooseEigenSystem::markEigenVariable(const VariableName & var_name)
79 {
80  _eigen_var_names.insert(var_name);
81 }
82 
83 void
85 {
86  if (tag == ALL)
87  {
88  solution().scale(scaling_factor);
89  }
90  else if (tag == EIGEN)
91  {
92  if (_all_eigen_vars)
93  {
94  solution().scale(scaling_factor);
95  }
96  else
97  {
98  for (const auto & dof : _eigen_var_indices)
99  solution().set(dof, solution()(dof) * scaling_factor);
100  }
101  }
102  solution().close();
103  update();
104 }
105 
106 void
107 MooseEigenSystem::combineSystemSolution(SYSTEMTAG tag, const std::vector<Real> & coefficients)
108 {
109  mooseAssert(coefficients.size() > 0 && coefficients.size() <= 3, "Size error on coefficients");
110  if (tag == ALL)
111  {
112  solution().scale(coefficients[0]);
113  if (coefficients.size() > 1)
114  solution().add(coefficients[1], solutionOld());
115  if (coefficients.size() > 2)
116  solution().add(coefficients[2], solutionOlder());
117  }
118  else if (tag == EIGEN)
119  {
120  if (_all_eigen_vars)
121  {
122  solution().scale(coefficients[0]);
123  if (coefficients.size() > 1)
124  solution().add(coefficients[1], solutionOld());
125  if (coefficients.size() > 2)
126  solution().add(coefficients[2], solutionOlder());
127  }
128  else
129  {
130  if (coefficients.size() > 2)
131  {
132  for (const auto & dof : _eigen_var_indices)
133  {
134  Real t = solution()(dof) * coefficients[0];
135  t += solutionOld()(dof) * coefficients[1];
136  t += solutionOlder()(dof) * coefficients[2];
137  solution().set(dof, t);
138  }
139  }
140  else if (coefficients.size() > 1)
141  {
142  for (const auto & dof : _eigen_var_indices)
143  {
144  Real t = solution()(dof) * coefficients[0];
145  t += solutionOld()(dof) * coefficients[1];
146  solution().set(dof, t);
147  }
148  }
149  else
150  {
151  for (const auto & dof : _eigen_var_indices)
152  {
153  Real t = solution()(dof) * coefficients[0];
154  solution().set(dof, t);
155  }
156  }
157  }
158  }
159  solution().close();
160  update();
161 }
162 
163 void
165 {
166  if (tag == ALL)
167  {
168  solution() = v;
169  }
170  else if (tag == EIGEN)
171  {
172  if (_all_eigen_vars)
173  {
174  solution() = v;
175  }
176  else
177  {
178  for (const auto & dof : _eigen_var_indices)
179  solution().set(dof, v);
180  }
181  }
182  solution().close();
183  update();
184 }
185 
186 void
188 {
189  if (tag == ALL)
190  {
191  solutionOld() = v;
192  }
193  else if (tag == EIGEN)
194  {
195  if (_all_eigen_vars)
196  {
197  solutionOld() = v;
198  }
199  else
200  {
201  for (const auto & dof : _eigen_var_indices)
202  solutionOld().set(dof, v);
203  }
204  }
205  solutionOld().close();
206  update();
207 }
208 
209 void
211 {
212  _active_on_old = true;
213  _fe_problem.updateActiveObjects(); // update warehouse active objects
214 }
215 
216 void
218 {
219  _active_on_old = false;
220  _fe_problem.updateActiveObjects(); // update warehouse active objects
221 }
222 
223 bool
225 {
226  return _active_on_old;
227 }
228 
229 void
231 {
232  if (tag == ALL)
233  {
234  }
235  else if (tag == EIGEN)
236  {
237  // build DoF indices for the eigen system
238  _eigen_var_indices.clear();
240  if (!_all_eigen_vars)
241  {
242  for (std::set<VariableName>::const_iterator it = getEigenVariableNames().begin();
243  it != getEigenVariableNames().end();
244  it++)
245  {
246  unsigned int i = sys().variable_number(*it);
247  std::set<dof_id_type> var_indices;
248  sys().local_dof_indices(i, var_indices);
249  _eigen_var_indices.insert(var_indices.begin(), var_indices.end());
250  }
251  }
252  }
253 }
254 
255 bool
257 {
258  return _eigen_kernel_counter > 0;
259 }
void scaleSystemSolution(SYSTEMTAG tag, Real scaling_factor)
Scale the solution vector.
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, InputParameters parameters, THREAD_ID tid=0, bool print_deprecated=true)
Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>()) ...
Definition: Factory.C:46
unsigned int _eigen_kernel_counter
counter of eigen kernels
virtual ~MooseEigenSystem()
virtual const std::string & name()
Definition: SystemBase.h:453
virtual NumericVector< Number > & solutionOld() override
SYSTEMTAG
System or kernel tags.
bool activeOnOld()
Return if eigen kernels should be on old solution.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
void updateActiveObjects()
Update the active objects in the warehouses.
Factory & _factory
Definition: SystemBase.h:481
const std::set< VariableName > & getEigenVariableNames() const
Get variable names of the eigen system.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
bool _has_save_in
If there is any Kernel or IntegratedBC having save_in.
virtual void update()
Update the system (doing libMesh magic)
Definition: SystemBase.C:665
virtual NumericVector< Number > & solution() override
void buildSystemDoFIndices(SYSTEMTAG tag=ALL)
Build DoF indices for a system.
void initSystemSolution(SYSTEMTAG tag, Real v)
Initialize the solution vector with a constant value.
void eigenKernelOnOld()
Ask eigenkernels to operate on old or current solution vectors.
void combineSystemSolution(SYSTEMTAG tag, const std::vector< Real > &coefficients)
Linear combination of the solution vectors.
FEProblemBase & _fe_problem
This is the common base class for the two main kernel types implemented in MOOSE, EigenKernel and Ker...
Definition: KernelBase.h:47
virtual NumericVector< Number > & solutionOlder() override
virtual void markEigenVariable(const VariableName &var_name)
Mark a variable as a variable of the eigen system.
std::set< dof_id_type > _eigen_var_indices
KernelWarehouse _kernels
std::set< VariableName > _eigen_var_names
bool _has_diag_save_in
If there is any Kernel or IntegratedBC having diag_save_in.
bool containsEigenKernel() const
Weather or not the system contains eigen kernels.
virtual TransientNonlinearImplicitSystem & sys()
Nonlinear system to be solved.
void addObject(std::shared_ptr< KernelBase > object, THREAD_ID tid=0) override
Add Kernel to the storage structure.
MooseEigenSystem(FEProblemBase &problem, const std::string &name)
virtual void addKernel(const std::string &kernel_name, const std::string &name, InputParameters parameters)
Adds a kernel.
void initSystemSolutionOld(SYSTEMTAG tag, Real v)
const std::vector< VariableName > & getVariableNames() const
Definition: SystemBase.h:466
unsigned int THREAD_ID
Definition: MooseTypes.h:79