GRINS-0.7.0
spalart_allmaras.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // GRINS - General Reacting Incompressible Navier-Stokes
5 //
6 // Copyright (C) 2014-2016 Paul T. Bauman, Roy H. Stogner
7 // Copyright (C) 2010-2013 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 
26 // This class
27 #include "grins/spalart_allmaras.h"
28 
29 // GRINS
30 #include "grins/assembly_context.h"
33 
35 #include "grins/parsed_viscosity.h"
39 
40 // libMesh
41 #include "libmesh/quadrature.h"
42 #include "libmesh/elem.h"
43 
44 namespace GRINS
45 {
46 
47  template<class Mu>
48  SpalartAllmaras<Mu>::SpalartAllmaras(const std::string& physics_name, const GetPot& input )
49  : TurbulenceModelsBase<Mu>(physics_name, input), // Define class variables
50  _flow_vars(input,PhysicsNaming::incompressible_navier_stokes()),
51  _press_var(input,PhysicsNaming::incompressible_navier_stokes(), true /*is_constraint_var*/),
52  _turbulence_vars(input, PhysicsNaming::spalart_allmaras()),
53  _spalart_allmaras_helper(input),
54  _sa_params(input),
55  _no_of_walls(input("Physics/"+PhysicsNaming::spalart_allmaras()+"/no_of_walls", 0)),
56  _infinite_distance(input("Physics/"+PhysicsNaming::spalart_allmaras()+"/infinite_distance", false))
57  {
58  // Loop over the _no_of_walls and fill the wall_ids set
59  for(unsigned int i = 0; i != _no_of_walls; i++)
60  {
61  _wall_ids.insert(input("Physics/"+PhysicsNaming::spalart_allmaras()+"/wall_ids", 0, i ));
62  }
63 
64  std::cout<<"No of walls: "<<_no_of_walls<<std::endl;
65 
66  for( std::set<libMesh::boundary_id_type>::iterator b_id = _wall_ids.begin(); b_id != _wall_ids.end(); ++b_id )
67  {
68  std::cout<<"Boundary Id: "<<*b_id<<std::endl;
69  }
70 
71  this->register_variables();
72 
73  this->_ic_handler = new GenericICHandler( physics_name, input );
74  }
75 
76  template<class Mu>
78  {
79  return;
80  }
81 
82  template<class Mu>
84  {
86  this->_press_var);
88  this->_flow_vars);
90  this->_turbulence_vars);
91  }
92 
93  template<class Mu>
94  void SpalartAllmaras<Mu>::init_variables( libMesh::FEMSystem* system )
95  {
96  // Init base class.
98 
99  this->_turbulence_vars.init(system);
100  this->_flow_vars.init(system);
101  this->_press_var.init(system);
102 
103  // Init the variables belonging to SA helper
104  _spalart_allmaras_helper.init_variables(system);
105 
106  // Initialize Boundary Mesh
107  this->boundary_mesh.reset(new libMesh::SerialMesh(system->get_mesh().comm() , this->_dim));
108 
109  // Use the _wall_ids set to build the boundary mesh object
110  (system->get_mesh()).boundary_info->sync(_wall_ids, *boundary_mesh);
111 
112  //this->distance_function.reset(new DistanceFunction(system->get_equation_systems(), dynamic_cast<libMesh::UnstructuredMesh&>(system->get_mesh()) ));
113  this->distance_function.reset(new DistanceFunction(system->get_equation_systems(), *boundary_mesh));
114 
115  // For now, we are hacking this. Without this initialize function being called
116  // the distance variable will just be zero. For the channel flow, we are just
117  // going to analytically compute the wall distance
118  //this->distance_function->initialize();
119 
120  return;
121  }
122 
123  template<class Mu>
125  {
126  // We should prerequest all the data
127  // we will need to build the linear system
128  // or evaluate a quantity of interest.
129  context.get_element_fe(_turbulence_vars.nu())->get_JxW();
130  context.get_element_fe(_turbulence_vars.nu())->get_phi();
131  context.get_element_fe(_turbulence_vars.nu())->get_dphi();
132  context.get_element_fe(_turbulence_vars.nu())->get_xyz();
133 
134  context.get_element_fe(_turbulence_vars.nu())->get_phi();
135  context.get_element_fe(_turbulence_vars.nu())->get_xyz();
136 
137  context.get_side_fe(_turbulence_vars.nu())->get_JxW();
138  context.get_side_fe(_turbulence_vars.nu())->get_phi();
139  context.get_side_fe(_turbulence_vars.nu())->get_dphi();
140  context.get_side_fe(_turbulence_vars.nu())->get_xyz();
141 
142  return;
143  }
144 
145  template<class Mu>
146  void SpalartAllmaras<Mu>::set_time_evolving_vars( libMesh::FEMSystem* system )
147  {
148  // Tell the system to march velocity forward in time, but
149  // leave p as a constraint only
150  system->time_evolving(this->_turbulence_vars.nu());
151 
152  return;
153  }
154 
155  template<class Mu>
156  void SpalartAllmaras<Mu>::element_time_derivative( bool compute_jacobian,
157  AssemblyContext& context,
158  CachedValues& /*cache*/ )
159  {
160 #ifdef GRINS_USE_GRVY_TIMERS
161  this->_timer->BeginTimer("SpalartAllmaras::element_time_derivative");
162 #endif
163 
164  // Get a pointer to the current element, we need this for computing
165  // the distance to wall for the quadrature points
166  libMesh::Elem &elem_pointer = context.get_elem();
167 
168  // We get some references to cell-specific data that
169  // will be used to assemble the linear system.
170 
171  // Element Jacobian * quadrature weights for interior integration.
172  const std::vector<libMesh::Real> &JxW =
173  context.get_element_fe(this->_turbulence_vars.nu())->get_JxW();
174 
175  // The viscosity shape functions at interior quadrature points.
176  const std::vector<std::vector<libMesh::Real> >& nu_phi =
177  context.get_element_fe(this->_turbulence_vars.nu())->get_phi();
178 
179  // The viscosity shape function gradients (in global coords.)
180  // at interior quadrature points.
181  const std::vector<std::vector<libMesh::RealGradient> >& nu_gradphi =
182  context.get_element_fe(this->_turbulence_vars.nu())->get_dphi();
183 
184  // The number of local degrees of freedom in each variable.
185  const unsigned int n_nu_dofs = context.get_dof_indices(this->_turbulence_vars.nu()).size();
186 
187  // The subvectors and submatrices we need to fill:
188  //
189  // K_{\alpha \beta} = R_{\alpha},{\beta} = \partial{ R_{\alpha} } / \partial{ {\beta} } (where R denotes residual)
190  // e.g., for \alpha = v and \beta = u we get: K{vu} = R_{v},{u}
191  // Note that Kpu, Kpv, Kpw and Fp comes as constraint.
192 
193  //libMesh::DenseSubMatrix<libMesh::Number> &Knunu = context.get_elem_jacobian(this->_turbulence_vars.nu(), this->_turbulence_vars.nu()); // R_{nu},{nu}
194 
195  libMesh::DenseSubVector<libMesh::Number> &Fnu = context.get_elem_residual(this->_turbulence_vars.nu()); // R_{nu}
196 
197  // Now we will build the element Jacobian and residual.
198  // Constructing the residual requires the solution and its
199  // gradient from the previous timestep. This must be
200  // calculated at each quadrature point by summing the
201  // solution degree-of-freedom values by the appropriate
202  // weight functions.
203  unsigned int n_qpoints = context.get_element_qrule().n_points();
204 
205  // Auto pointer to distance fcn evaluated at quad points
206  libMesh::UniquePtr< libMesh::DenseVector<libMesh::Real> > distance_qp;
207 
208  // Fill the vector of distances to quadrature points
209  distance_qp = this->distance_function->interpolate(&elem_pointer, context.get_element_qrule().get_points());
210 
211  for (unsigned int qp=0; qp != n_qpoints; qp++)
212  {
213  // Compute the solution & its gradient at the old Newton iterate.
214  libMesh::Number nu;
215  nu = context.interior_value(this->_turbulence_vars.nu(), qp);
216 
217  libMesh::Gradient grad_nu;
218  grad_nu = context.interior_gradient(this->_turbulence_vars.nu(), qp);
219 
220  libMesh::Real jac = JxW[qp];
221 
222  // The physical viscosity
223  libMesh::Real mu_qp = this->_mu(context, qp);
224 
225  // The vorticity value
226  libMesh::Real vorticity_value_qp = this->_spalart_allmaras_helper.vorticity(context, qp);
227 
228  // The flow velocity
229  libMesh::Number u,v;
230  u = context.interior_value(this->_flow_vars.u(), qp);
231  v = context.interior_value(this->_flow_vars.v(), qp);
232 
233  libMesh::NumberVectorValue U(u,v);
234  if (this->_dim == 3)
235  U(2) = context.interior_value(this->_flow_vars.w(), qp);
236 
237  //The source term
238  libMesh::Real S_tilde = this->_sa_params.source_fn(nu, mu_qp, (*distance_qp)(qp), vorticity_value_qp, _infinite_distance);
239 
240  // The ft2 function needed for the negative S-A model
241  libMesh::Real chi = nu/mu_qp;
242  libMesh::Real f_t2 = this->_sa_params.get_c_t3()*exp(-this->_sa_params.get_c_t4()*chi*chi);
243 
244  libMesh::Real source_term = this->_sa_params.get_cb1()*(1 - f_t2)*S_tilde*nu;
245  // For a negative turbulent viscosity nu < 0.0 we need to use a different production function
246  if(nu < 0.0)
247  {
248  source_term = this->_sa_params.get_cb1()*(1 - this->_sa_params.get_c_t3())*vorticity_value_qp*nu;
249  }
250 
251  // The wall destruction term
252  libMesh::Real fw = this->_sa_params.destruction_fn(nu, (*distance_qp)(qp), S_tilde, _infinite_distance);
253 
254  libMesh::Real nud = 0.0;
255  if(_infinite_distance)
256  {
257  nud = 0.0;
258  }
259  else
260  {
261  nud = nu/(*distance_qp)(qp);
262  }
263  libMesh::Real nud2 = nud*nud;
264  libMesh::Real kappa2 = (this->_sa_params.get_kappa())*(this->_sa_params.get_kappa());
265  libMesh::Real cw1 = this->_sa_params.get_cb1()/kappa2 + (1.0 + this->_sa_params.get_cb2())/this->_sa_params.get_sigma();
266  libMesh::Real destruction_term = (cw1*fw - (this->_sa_params.get_cb1()/kappa2)*f_t2)*nud2;
267 
268  // For a negative turbulent viscosity nu < 0.0 we need to use a different production function
269  if(nu < 0.0)
270  {
271  destruction_term = -cw1*nud2;
272  }
273 
274  libMesh::Real fn1 = 1.0;
275  // For a negative turbulent viscosity, fn1 needs to be calculated
276  if(nu < 0.0)
277  {
278  libMesh::Real chi3 = chi*chi*chi;
279  fn1 = (this->_sa_params.get_c_n1() + chi3)/(this->_sa_params.get_c_n1() - chi3);
280  }
281 
282  // First, an i-loop over the viscosity degrees of freedom.
283  for (unsigned int i=0; i != n_nu_dofs; i++)
284  {
285  Fnu(i) += jac *
286  ( -this->_rho*(U*grad_nu)*nu_phi[i][qp] // convection term (assumes incompressibility)
287  +source_term*nu_phi[i][qp] // source term
288  + (1./this->_sa_params.get_sigma())*(-(mu_qp+(fn1*nu))*grad_nu*nu_gradphi[i][qp] + this->_sa_params.get_cb2()*grad_nu*grad_nu*nu_phi[i][qp]) // diffusion term
289  - destruction_term*nu_phi[i][qp]); // destruction term
290 
291  // Compute the jacobian if not using numerical jacobians
292  if (compute_jacobian)
293  {
294  libmesh_not_implemented();
295  } // end - if (compute_jacobian)
296 
297  } // end of the outer dof (i) loop
298  } // end of the quadrature point (qp) loop
299 
300 #ifdef GRINS_USE_GRVY_TIMERS
301  this->_timer->EndTimer("SpalartAllmaras::element_time_derivative");
302 #endif
303 
304  return;
305  }
306 
307  template<class K>
308  void SpalartAllmaras<K>::mass_residual( bool compute_jacobian,
309  AssemblyContext& context,
310  CachedValues& /*cache*/ )
311  {
312 #ifdef GRINS_USE_GRVY_TIMERS
313  this->_timer->BeginTimer("SpalartAllmaras::mass_residual");
314 #endif
315 
316  // First we get some references to cell-specific data that
317  // will be used to assemble the linear system.
318 
319  // Element Jacobian * quadrature weights for interior integration.
320  const std::vector<libMesh::Real> &JxW =
321  context.get_element_fe(this->_turbulence_vars.nu())->get_JxW();
322 
323  // The viscosity shape functions at interior quadrature points.
324  const std::vector<std::vector<libMesh::Real> >& nu_phi =
325  context.get_element_fe(this->_turbulence_vars.nu())->get_phi();
326 
327  // The number of local degrees of freedom in each variable.
328  const unsigned int n_nu_dofs = context.get_dof_indices(this->_turbulence_vars.nu()).size();
329 
330  // The subvectors and submatrices we need to fill:
331  libMesh::DenseSubVector<libMesh::Real> &F = context.get_elem_residual(this->_turbulence_vars.nu());
332 
333  //libMesh::DenseSubMatrix<libMesh::Real> &M = context.get_elem_jacobian(this->_turbulence_vars.nu(), this->_turbulence_vars.nu());
334 
335  unsigned int n_qpoints = context.get_element_qrule().n_points();
336 
337  for (unsigned int qp = 0; qp != n_qpoints; ++qp)
338  {
339  // For the mass residual, we need to be a little careful.
340  // The time integrator is handling the time-discretization
341  // for us so we need to supply M(u_fixed)*u' for the residual.
342  // u_fixed will be given by the fixed_interior_value function
343  // while u' will be given by the interior_rate function.
344  libMesh::Real nu_dot;
345  context.interior_rate(this->_turbulence_vars.nu(), qp, nu_dot);
346 
347  for (unsigned int i = 0; i != n_nu_dofs; ++i)
348  {
349  F(i) += -JxW[qp]*this->_rho*nu_dot*nu_phi[i][qp];
350 
351  if( compute_jacobian )
352  {
353  libmesh_not_implemented();
354  }// End of check on Jacobian
355 
356  } // End of element dof loop
357 
358  } // End of the quadrature point loop
359 
360 #ifdef GRINS_USE_GRVY_TIMERS
361  this->_timer->EndTimer("SpalartAllmaras::mass_residual");
362 #endif
363 
364  return;
365  }
366 
367  template<class Mu>
369  ( const std::string & param_name,
371  const
372  {
373  ParameterUser::register_parameter(param_name, param_pointer);
374  this->_mu.register_parameter(param_name, param_pointer);
375  this->_sa_params.register_parameter(param_name, param_pointer);
376  }
377 
378 } // namespace GRINS
379 
380 // Instantiate
virtual void init_context(AssemblyContext &context)
Initialize context for added physics variables.
GRINS::ICHandlingBase * _ic_handler
Definition: physics.h:269
static PhysicsName spalart_allmaras()
static void check_and_register_variable(const std::string &var_name, const FEVariablesBase &variable)
First check if var_name is registered and then register.
Base class for reading and handling initial conditions for physics classes.
Physics class for Turbulence Models.
virtual void element_time_derivative(bool compute_jacobian, AssemblyContext &context, CachedValues &)
Time dependent part(s) of physics for element interiors.
GRINS namespace.
virtual void init_variables(libMesh::FEMSystem *system)
Initialize variables for this physics.
virtual void init_variables(libMesh::FEMSystem *system)
Initialize variables for this physics.
static std::string velocity_section()
INSTANTIATE_TURBULENCE_MODELS_SUBCLASS(SpalartAllmaras)
static std::string pressure_section()
virtual void mass_residual(bool compute_jacobian, AssemblyContext &context, CachedValues &cache)
Mass matrix part(s) for element interiors. All boundary terms lie within the time_derivative part...
std::set< libMesh::boundary_id_type > _wall_ids
virtual void register_parameter(const std::string &param_name, libMesh::ParameterMultiAccessor< libMesh::Number > &param_pointer) const
Each subclass will register its copy of an independent.
static std::string turbulence_section()
virtual void register_parameter(const std::string &param_name, libMesh::ParameterMultiAccessor< libMesh::Number > &param_pointer) const
Each subclass will register its copy of an independent.
virtual void set_time_evolving_vars(libMesh::FEMSystem *system)
Sets velocity variables to be time-evolving.

Generated on Thu Jun 2 2016 21:52:28 for GRINS-0.7.0 by  doxygen 1.8.10