www.mooseframework.org
MooseEigenSystem.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 #include "MooseEigenSystem.h"
11 
12 #include "MaterialData.h"
13 #include "Factory.h"
14 #include "EigenKernel.h"
15 
16 MooseEigenSystem::MooseEigenSystem(FEProblemBase & fe_problem, const std::string & name)
17  : NonlinearSystem(fe_problem, name),
18  _all_eigen_vars(false),
19  _active_on_old(false),
20  _eigen_kernel_counter(0)
21 {
22 }
23 
25 
26 void
27 MooseEigenSystem::addKernel(const std::string & kernel_name,
28  const std::string & name,
29  InputParameters & parameters)
30 {
31  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
32  {
33  // In the case of EigenKernels, we might need to add two to the system
34  if (parameters.have_parameter<bool>("eigen"))
35  {
36  {
37  // EigenKernel
38  parameters.set<bool>("implicit") = true;
39  std::shared_ptr<KernelBase> ekernel =
40  _factory.create<KernelBase>(kernel_name, name, parameters, tid);
41  if (parameters.get<bool>("eigen"))
42  markEigenVariable(parameters.get<NonlinearVariableName>("variable"));
43  _kernels.addObject(ekernel, tid);
44  }
45  if (parameters.get<bool>("eigen"))
46  {
47  // EigenKernel_old
48  parameters.set<bool>("implicit") = false;
49  std::string old_name(name + "_old");
50 
51  std::shared_ptr<KernelBase> ekernel =
52  _factory.create<KernelBase>(kernel_name, old_name, parameters, tid);
53  _eigen_var_names.insert(parameters.get<NonlinearVariableName>("variable"));
54  _kernels.addObject(ekernel, tid);
56  }
57  }
58  else // Standard nonlinear system kernel
59  {
60  // Create the kernel object via the factory
61  std::shared_ptr<KernelBase> kernel =
62  _factory.create<KernelBase>(kernel_name, name, parameters, tid);
63  _kernels.addObject(kernel, tid);
64  }
65  }
66 
67  if (parameters.get<std::vector<AuxVariableName>>("save_in").size() > 0)
68  _has_save_in = true;
69  if (parameters.get<std::vector<AuxVariableName>>("diag_save_in").size() > 0)
70  _has_diag_save_in = true;
71 }
72 
73 void
74 MooseEigenSystem::markEigenVariable(const VariableName & var_name)
75 {
76  _eigen_var_names.insert(var_name);
77 }
78 
79 void
81 {
82  if (tag == ALL)
83  {
84  solution().scale(scaling_factor);
85  }
86  else if (tag == EIGEN)
87  {
88  if (_all_eigen_vars)
89  {
90  solution().scale(scaling_factor);
91  }
92  else
93  {
94  for (const auto & dof : _eigen_var_indices)
95  solution().set(dof, solution()(dof) * scaling_factor);
96  }
97  }
98  solution().close();
99  update();
100 }
101 
102 void
103 MooseEigenSystem::combineSystemSolution(SYSTEMTAG tag, const std::vector<Real> & coefficients)
104 {
105  mooseAssert(coefficients.size() > 0 && coefficients.size() <= 3, "Size error on coefficients");
106  if (tag == ALL)
107  {
108  solution().scale(coefficients[0]);
109  if (coefficients.size() > 1)
110  solution().add(coefficients[1], solutionOld());
111  if (coefficients.size() > 2)
112  solution().add(coefficients[2], solutionOlder());
113  }
114  else if (tag == EIGEN)
115  {
116  if (_all_eigen_vars)
117  {
118  solution().scale(coefficients[0]);
119  if (coefficients.size() > 1)
120  solution().add(coefficients[1], solutionOld());
121  if (coefficients.size() > 2)
122  solution().add(coefficients[2], solutionOlder());
123  }
124  else
125  {
126  if (coefficients.size() > 2)
127  {
128  for (const auto & dof : _eigen_var_indices)
129  {
130  Real t = solution()(dof) * coefficients[0];
131  t += solutionOld()(dof) * coefficients[1];
132  t += solutionOlder()(dof) * coefficients[2];
133  solution().set(dof, t);
134  }
135  }
136  else if (coefficients.size() > 1)
137  {
138  for (const auto & dof : _eigen_var_indices)
139  {
140  Real t = solution()(dof) * coefficients[0];
141  t += solutionOld()(dof) * coefficients[1];
142  solution().set(dof, t);
143  }
144  }
145  else
146  {
147  for (const auto & dof : _eigen_var_indices)
148  {
149  Real t = solution()(dof) * coefficients[0];
150  solution().set(dof, t);
151  }
152  }
153  }
154  }
155  solution().close();
156  update();
157 }
158 
159 void
161 {
162  if (tag == ALL)
163  {
164  solution() = v;
165  }
166  else if (tag == EIGEN)
167  {
168  if (_all_eigen_vars)
169  {
170  solution() = v;
171  }
172  else
173  {
174  for (const auto & dof : _eigen_var_indices)
175  solution().set(dof, v);
176  }
177  }
178  solution().close();
179  update();
180 }
181 
182 void
184 {
185  if (tag == ALL)
186  {
187  solutionOld() = v;
188  }
189  else if (tag == EIGEN)
190  {
191  if (_all_eigen_vars)
192  {
193  solutionOld() = v;
194  }
195  else
196  {
197  for (const auto & dof : _eigen_var_indices)
198  solutionOld().set(dof, v);
199  }
200  }
201  solutionOld().close();
202  update();
203 }
204 
205 void
207 {
208  _active_on_old = true;
209  _fe_problem.updateActiveObjects(); // update warehouse active objects
210 }
211 
212 void
214 {
215  _active_on_old = false;
216  _fe_problem.updateActiveObjects(); // update warehouse active objects
217 }
218 
219 bool
221 {
222  return _active_on_old;
223 }
224 
225 void
227 {
228  if (tag == ALL)
229  {
230  }
231  else if (tag == EIGEN)
232  {
233  // build DoF indices for the eigen system
234  _eigen_var_indices.clear();
236  if (!_all_eigen_vars)
237  {
238  for (std::set<VariableName>::const_iterator it = getEigenVariableNames().begin();
239  it != getEigenVariableNames().end();
240  it++)
241  {
242  unsigned int i = sys().variable_number(*it);
243  std::set<dof_id_type> var_indices;
244  sys().local_dof_indices(i, var_indices);
245  _eigen_var_indices.insert(var_indices.begin(), var_indices.end());
246  }
247  }
248  }
249 }
250 
251 bool
253 {
254  return _eigen_kernel_counter > 0;
255 }
std::string name(const ElemQuality q)
const std::set< VariableName > & getEigenVariableNames() const
Get variable names of the eigen system.
virtual void update(bool update_libmesh_system=true)
Update the system (doing libMesh magic)
Definition: SystemBase.C:1218
void scaleSystemSolution(SYSTEMTAG tag, Real scaling_factor)
Scale the solution vector.
unsigned int _eigen_kernel_counter
counter of eigen kernels
virtual ~MooseEigenSystem()
unsigned int n_threads()
NumericVector< Number > & solution()
Definition: SystemBase.h:182
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
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.
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
Definition: Factory.C:110
virtual void updateActiveObjects()
Update the active objects in the warehouses.
Factory & _factory
Definition: SystemBase.h:957
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
NumericVector< Number > & solutionOlder()
Definition: SystemBase.h:184
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 const std::string & name() const
Definition: SystemBase.C:1293
void buildSystemDoFIndices(SYSTEMTAG tag=ALL)
Build DoF indices for a system.
virtual void scale(const Number factor)=0
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.
This is the common base class for the three main kernel types implemented in MOOSE, Kernel, VectorKernel and ArrayKernel.
Definition: KernelBase.h:22
MooseObjectTagWarehouse< KernelBase > _kernels
virtual void markEigenVariable(const VariableName &var_name)
Mark a variable as a variable of the eigen system.
virtual void close()=0
std::set< dof_id_type > _eigen_var_indices
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.
std::set< VariableName > _eigen_var_names
FEProblemBase & _fe_problem
the governing finite element/volume problem
Definition: SystemBase.h:954
bool _has_diag_save_in
If there is any Kernel or IntegratedBC having diag_save_in.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
bool containsEigenKernel() const
Weather or not the system contains eigen kernels.
Nonlinear system to be solved.
MooseEigenSystem(FEProblemBase &problem, const std::string &name)
const std::vector< VariableName > & getVariableNames() const
Definition: SystemBase.h:853
virtual NonlinearImplicitSystem & sys()
virtual void set(const numeric_index_type i, const Number value)=0
void initSystemSolutionOld(SYSTEMTAG tag, Real v)
virtual void add(const numeric_index_type i, const Number value)=0
NumericVector< Number > & solutionOld()
Definition: SystemBase.h:183
virtual void addObject(std::shared_ptr< T > object, THREAD_ID tid=0, bool recurse=true) override
Adds an object to the storage structure.
virtual void addKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters)
Adds a kernel.
unsigned int THREAD_ID
Definition: MooseTypes.h:198