euler_solver.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 #include "libmesh/diff_system.h"
20 #include "libmesh/euler_solver.h"
21 
22 namespace libMesh
23 {
24 
25 
26 
28  : FirstOrderUnsteadySolver(s), theta(1.)
29 {
30 }
31 
32 
33 
35 {
36 }
37 
38 
39 
41 {
42  if (theta == 0.5)
43  return 2.;
44  return 1.;
45 }
46 
47 
48 
49 
50 bool EulerSolver::element_residual (bool request_jacobian,
51  DiffContext & context)
52 {
54 
55  return this->_general_residual(request_jacobian,
56  context,
63 }
64 
65 
66 
67 bool EulerSolver::side_residual (bool request_jacobian,
68  DiffContext & context)
69 {
70  return this->_general_residual(request_jacobian,
71  context,
77  false);
78 }
79 
80 
81 
82 bool EulerSolver::nonlocal_residual (bool request_jacobian,
83  DiffContext & context)
84 {
86 
87  return this->_general_residual(request_jacobian,
88  context,
95 }
96 
97 
98 
99 bool EulerSolver::_general_residual (bool request_jacobian,
100  DiffContext & context,
101  ResFuncType mass,
102  ResFuncType damping,
103  ResFuncType time_deriv,
104  ResFuncType constraint,
105  ReinitFuncType reinit_func,
106  bool compute_second_order_eqns)
107 {
108  unsigned int n_dofs = context.get_elem_solution().size();
109 
110  // We might need to save the old jacobian in case one of our physics
111  // terms later is unable to update it analytically.
112  DenseMatrix<Number> old_elem_jacobian(n_dofs, n_dofs);
113  if (request_jacobian)
114  old_elem_jacobian.swap(context.get_elem_jacobian());
115 
116  // Local nonlinear solution at old timestep
117  DenseVector<Number> old_elem_solution(n_dofs);
118  for (unsigned int i=0; i != n_dofs; ++i)
119  old_elem_solution(i) =
121 
122  // Local time derivative of solution
123  context.get_elem_solution_rate() = context.get_elem_solution();
124  context.get_elem_solution_rate() -= old_elem_solution;
126  context.get_elem_solution_rate() *=
128 
129  // If we are asked to compute residuals for second order variables,
130  // we also populate the acceleration part so the user can use that.
132  this->prepare_accel(context);
133 
134  // Local nonlinear solution at time t_theta
135  DenseVector<Number> theta_solution(context.get_elem_solution());
136  theta_solution *= theta;
137  theta_solution.add(1. - theta, old_elem_solution);
138 
141 
142  // If a fixed solution is requested, we'll use theta_solution
144  context.get_elem_fixed_solution() = theta_solution;
145 
146  // Move theta_->elem_, elem_->theta_
147  context.get_elem_solution().swap(theta_solution);
148 
149  // Move the mesh into place first if necessary, set t = t_{\theta}
150  (context.*reinit_func)(theta);
151 
152  // Get the time derivative at t_theta
153  bool jacobian_computed =
154  (_system.get_physics()->*time_deriv)(request_jacobian, context);
155 
156  jacobian_computed = (_system.get_physics()->*mass)(jacobian_computed, context) &&
157  jacobian_computed;
158 
159  // If we have second-order variables, we need to get damping terms
160  // and the velocity equations
162  {
163  jacobian_computed = (_system.get_physics()->*damping)(jacobian_computed, context) &&
164  jacobian_computed;
165 
166  jacobian_computed = this->compute_second_order_eqns(jacobian_computed, context) &&
167  jacobian_computed;
168  }
169 
170  // Restore the elem position if necessary, set t = t_{n+1}
171  (context.*reinit_func)(1);
172 
173  // Move elem_->elem_, theta_->theta_
174  context.get_elem_solution().swap(theta_solution);
175  context.elem_solution_derivative = 1;
176 
177  // Add the constraint term
178  jacobian_computed = (_system.get_physics()->*constraint)(jacobian_computed, context) &&
179  jacobian_computed;
180 
181  // Add back (or restore) the old jacobian
182  if (request_jacobian)
183  {
184  if (jacobian_computed)
185  context.get_elem_jacobian() += old_elem_jacobian;
186  else
187  context.get_elem_jacobian().swap(old_elem_jacobian);
188  }
189 
190  return jacobian_computed;
191 }
192 
193 
194 } // namespace libMesh
virtual unsigned int size() const override
Definition: dense_vector.h:92
virtual bool side_constraint(bool request_jacobian, DiffContext &)
Definition: diff_physics.h:191
virtual bool nonlocal_residual(bool request_jacobian, DiffContext &) override
Definition: euler_solver.C:82
const DenseMatrix< Number > & get_elem_jacobian() const
Definition: diff_context.h:283
virtual ~EulerSolver()
Definition: euler_solver.C:34
bool compute_second_order_eqns(bool compute_jacobian, DiffContext &c)
void swap(DenseMatrix< T > &other_matrix)
Definition: dense_matrix.h:762
boostcopy::enable_if_c< ScalarTraits< T2 >::value, void >::type add(const T2 factor, const DenseVector< T3 > &vec)
Definition: dense_vector.h:436
const DenseVector< Number > & get_elem_fixed_solution() const
Definition: diff_context.h:215
const DenseVector< Number > & get_elem_solution_rate() const
Definition: diff_context.h:145
virtual bool side_damping_residual(bool request_jacobian, DiffContext &)
Definition: diff_physics.h:391
virtual bool element_residual(bool request_jacobian, DiffContext &) override
Definition: euler_solver.C:50
Number old_nonlinear_solution(const dof_id_type global_dof_number) const
virtual bool nonlocal_time_derivative(bool request_jacobian, DiffContext &)
Definition: diff_physics.h:209
virtual bool damping_residual(bool request_jacobian, DiffContext &)
Definition: diff_physics.h:374
virtual bool _general_residual(bool request_jacobian, DiffContext &, ResFuncType mass, ResFuncType damping, ResFuncType time_deriv, ResFuncType constraint, ReinitFuncType reinit, bool compute_second_order_eqns)
Definition: euler_solver.C:99
virtual bool nonlocal_constraint(bool request_jacobian, DiffContext &)
Definition: diff_physics.h:227
virtual void elem_reinit(Real)
Definition: diff_context.h:76
bool _eulerian_time_deriv(bool request_jacobian, DiffContext &)
void swap(DenseVector< T > &other_vector)
Definition: dense_vector.h:346
virtual bool mass_residual(bool request_jacobian, DiffContext &)
Definition: diff_physics.h:318
sys_type & _system
Definition: time_solver.h:258
Real elem_solution_rate_derivative
Definition: diff_context.h:507
const DenseVector< Number > & get_elem_solution() const
Definition: diff_context.h:111
virtual Real error_order() const override
Definition: euler_solver.C:40
bool use_fixed_solution
Definition: system.h:1493
const std::vector< dof_id_type > & get_dof_indices() const
Definition: diff_context.h:367
bool have_second_order_scalar_vars() const
Definition: diff_system.C:335
const DifferentiablePhysics * get_physics() const
Definition: diff_system.h:182
virtual bool side_residual(bool request_jacobian, DiffContext &) override
Definition: euler_solver.C:67
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void elem_side_reinit(Real)
Definition: diff_context.h:82
virtual bool nonlocal_damping_residual(bool request_jacobian, DiffContext &)
Definition: diff_physics.h:406
virtual bool nonlocal_mass_residual(bool request_jacobian, DiffContext &c)
virtual bool side_mass_residual(bool request_jacobian, DiffContext &)
Definition: diff_physics.h:335
virtual bool side_time_derivative(bool request_jacobian, DiffContext &)
Definition: diff_physics.h:171
virtual bool element_constraint(bool request_jacobian, DiffContext &)
Definition: diff_physics.h:142
EulerSolver(sys_type &s)
Definition: euler_solver.C:27
virtual void nonlocal_reinit(Real)
Definition: diff_context.h:94