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