MAST
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
fluid_elem_initialization.h
Go to the documentation of this file.
1 /*
2  * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3  * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifndef __mast_fluid_elem_initialization_h__
21 #define __mast_fluid_elem_initialization_h__
22 
23 // MAST includes
24 #include "base/nonlinear_system.h"
31 #include "fluid/flight_condition.h"
35 #include "mesh/geom_elem.h"
36 #include "base/test_comparisons.h"
37 
38 // libMesh includes
39 #include "libmesh/libmesh.h"
40 #include "libmesh/string_to_enum.h"
41 #include "libmesh/enum_order.h"
42 #include "libmesh/enum_fe_family.h"
43 #include "libmesh/enum_elem_type.h" // ElemType
44 #include "libmesh/fe_type.h" // FEFamily, Order
45 #include "libmesh/replicated_mesh.h"
46 #include "libmesh/equation_systems.h"
47 #include "libmesh/dof_map.h"
48 #include "libmesh/numeric_vector.h"
49 #include "libmesh/exodusII_io.h"
50 #include "libmesh/nonlinear_solver.h"
51 #include "libmesh/mesh_generation.h"
52 
53 extern libMesh::LibMeshInit* _libmesh_init;
54 
56 
57  libMesh::LibMeshInit& _init;
58  unsigned int _dim;
59  libMesh::UnstructuredMesh* _mesh;
60  libMesh::EquationSystems* _eq_sys;
67  MAST::FirstOrderNewmarkTransientSolver* _transient_solver;
73 
77  libMesh::FEType _fetype;
78 
79  std::set<MAST::BoundaryConditionBase*> _boundary_conditions;
80 
82  _init (*_libmesh_init),
83  _dim (0),
84  _mesh (nullptr),
85  _eq_sys (nullptr),
86  _sys (nullptr),
87  _sys_init (nullptr),
88  _discipline (nullptr),
89  _flight_cond (nullptr),
90  _assembly (nullptr),
91  _elem_ops (nullptr),
92  _transient_solver(nullptr),
93  _fluid_elem (nullptr),
94  _far_field_bc (nullptr),
95  _slip_wall_bc (nullptr),
96  _noslip_wall_bc (nullptr),
97  _initialized (false),
98  _if_viscous (false),
99  _delta (0.) {
100 
101  }
102 
103  void init(bool if_viscous) {
104 
105  // initialize the mesh. Details of parameters for each mesh are
106  // described above.
107  _mesh = new libMesh::ReplicatedMesh(_init.comm());
108 
109  _dim = 2;
110  libMesh::MeshTools::Generation::build_square(*_mesh, 1, 1);
111 
112  // create equation system
113  _eq_sys = new libMesh::EquationSystems(*_mesh);
114 
115  // add the system to be used for fluid analysis
116  _sys = &(_eq_sys->add_system<MAST::NonlinearSystem>("fluid"));
117 
118  // create the discipline where boundary conditions will be stored
119  _discipline = new MAST::ConservativeFluidDiscipline(*_eq_sys);
120 
121  // create system initialization object to add variables
122  libMesh::Order fe_order = libMesh::FIRST;
123  libMesh::FEFamily fe_family = libMesh::LAGRANGE;
124 
125  _sys_init = new MAST::ConservativeFluidSystemInitialization(*_sys,
126  _sys->name(),
127  libMesh::FEType(fe_order, fe_family),
128  _dim);
129 
130  _far_field_bc = new MAST::BoundaryConditionBase(MAST::FAR_FIELD);
131  _slip_wall_bc = new MAST::BoundaryConditionBase(MAST::SLIP_WALL);
132  _noslip_wall_bc = new MAST::BoundaryConditionBase(MAST::NO_SLIP_WALL);
133 
134  // set fluid properties
135  _flight_cond = new MAST::FlightCondition;
136  _flight_cond->flow_unit_vector(0) = 1.;
137  _flight_cond->flow_unit_vector(1) = 1.;
138  _flight_cond->flow_unit_vector(2) = 0.;
139  _flight_cond->mach = 0.5;
140  _flight_cond->gas_property.cp = 1003.;
141  _flight_cond->gas_property.cv = 716.;
142  _flight_cond->gas_property.T = 300.;
143  _flight_cond->gas_property.rho = 1.05;
144  _flight_cond->gas_property.if_viscous = if_viscous;
145  _flight_cond->init();
146 
147  // tell the discipline about the fluid values
148  _discipline->set_flight_condition(*_flight_cond);
149 
150  // initialize the equation system
151  _eq_sys->init();
152 
153  // not initialize the fluid element to be used for tests
154  _assembly = new MAST::TransientAssembly;
156  _transient_solver = new MAST::FirstOrderNewmarkTransientSolver;
157 
158  _assembly->set_discipline_and_system(*_discipline, *_sys_init);
159  _elem_ops->set_discipline_and_system(*_discipline, *_sys_init);
160  _transient_solver->set_discipline_and_system(*_discipline, *_sys_init);
161  _transient_solver->set_elem_operation_object(*_elem_ops);
162  _geom_elem = new MAST::GeomElem;
163  _geom_elem->init(**_mesh->elements_begin(), *_sys_init);
164  _fluid_elem = new MAST::ConservativeFluidElementBase(*_sys_init,
165  *_geom_elem,
166  *_flight_cond);
167 
168  _initialized = true;
169  }
170 
171 
173 
174  if (_initialized) {
175 
176  delete _assembly;
177  delete _elem_ops;
178  delete _transient_solver;
179  delete _fluid_elem;
180  delete _geom_elem;
181 
182  delete _far_field_bc;
183  delete _slip_wall_bc;
184  delete _noslip_wall_bc;
185 
186  delete _eq_sys;
187  delete _mesh;
188 
189  delete _discipline;
190  delete _sys_init;
191  delete _flight_cond;
192  }
193  }
194 
195 
197 
198  libmesh_assert(_initialized);
199 
200  unsigned int
201  n_shape = _sys->n_dofs()/(_dim+2);
202 
203  // random vector in [-1, 1] used to perturb the state vector
204  // to invoke gradients, leading to stresses and flux terms
206  rand = RealVectorX::Random(n_shape);
207 
208  s = RealVectorX::Zero(_sys->n_dofs());
209 
210  for (unsigned int i=0; i<n_shape; i++) {
211 
212  s(i) = _flight_cond->rho() * (1. + _delta * rand(i));
213  s(n_shape+i) = _flight_cond->rho_u1() * (1. + _delta * rand(i));
214  s(2*n_shape+i) = _flight_cond->rho_u2() * (1. + _delta * rand(i));
215  if (_dim > 2)
216  s(3*n_shape+i) = _flight_cond->rho_u3() * (1. + _delta * rand(i));
217  s((_dim+1)*n_shape+i) = _flight_cond->rho_e() * (1. + _delta * rand(i));
218  }
219  }
220 
221 
222 
224 
225  libmesh_assert(_initialized);
226 
228  c_sol = RealVectorX::Zero(_dim+2);
229 
230  c_sol(0) = _flight_cond->rho();
231  c_sol(1) = _flight_cond->rho_u1();
232  c_sol(2) = _flight_cond->rho_u2();
233  if (_dim > 2)
234  c_sol(3) = _flight_cond->rho_u3();
235  c_sol(_dim+1) = _flight_cond->rho_e();
236 
237  s.zero();
238  s.init(_dim, c_sol,
239  _flight_cond->gas_property.cp,
240  _flight_cond->gas_property.cv,
241  _flight_cond->gas_property.if_viscous);
242  }
243 
244  template <typename ValType>
245  bool check_jacobian(ValType& val) {
246 
247  libmesh_assert(_initialized);
248 
249  Real
250  delta = 0.;
251 
252  unsigned int
253  n = _sys->n_dofs();
254 
256  jac0 = RealMatrixX::Zero(n, n),
257  jac_fd = RealMatrixX::Zero(n, n);
258 
260  v_low = RealVectorX::Zero(n),
261  v_hi = RealVectorX::Zero(n),
262  v0 = RealVectorX::Zero(n),
263  x_low = RealVectorX::Zero(n),
264  x_hi = RealVectorX::Zero(n),
265  x0 = RealVectorX::Zero(n),
266  f0 = RealVectorX::Zero(n),
267  f_low = RealVectorX::Zero(n),
268  f_hi = RealVectorX::Zero(n);
269 
271 
272  // velocity is set to assuming a linear variation of the state from zero
273  // over dt = 1.e-2
274  v0 = x0/1.e-2;
275 
276  _fluid_elem->set_solution(x0);
277  _fluid_elem->set_velocity(v0);
278 
279  val.compute(true, f0, jac0);
280 
281  for (unsigned int i=0; i<n; i++) {
282 
283  x_low = x0;
284  x_hi = x0;
285  v_low = v0;
286  v_hi = v0;
287 
288  if (!val.jac_xdot) {
289  if (fabs(x0(i)) > 0.)
290  delta = x0(i)*val.frac;
291  else
292  delta = val.delta;
293 
294  x_low(i) -= delta;
295  x_hi(i) += delta;
296  }
297  else {
298  if (fabs(v0(i)) > 0.)
299  delta = v0(i)*val.frac;
300  else
301  delta = val.delta;
302 
303  v_low(i) -= delta;
304  v_hi(i) += delta;
305  }
306 
307 
308  // get the new residual
309  _fluid_elem->set_solution(x_low);
310  _fluid_elem->set_velocity(v_low);
311  f_low.setZero();
312  val.compute(false, f_low, jac0);
313 
314 
315  // get the new residual
316  _fluid_elem->set_solution(x_hi);
317  _fluid_elem->set_velocity(v_hi);
318  f_hi.setZero();
319  val.compute(false, f_hi, jac0);
320 
321  // set the i^th column of the finite-differenced Jacobian
322  jac_fd.col(i) = (f_hi-f_low)/2./delta;
323  }
324 
325  return MAST::compare_matrix(jac_fd, jac0, val.tol);
326  }
327 
328 };
329 
330 #endif // __mast_fluid_elem_initialization_h__
This class provides the necessary functionality for spatial discretization of the conservative fluid ...
MAST::GeomElem * _geom_elem
This class implements a system for solution of nonlinear systems.
MAST::ConservativeFluidTransientAssemblyElemOperations * _elem_ops
MAST::FlightCondition * _flight_cond
virtual void set_velocity(const RealVectorX &vec, bool if_sens=false)
stores vec as velocity for element level calculations, or its sensitivity if if_sens is true...
Definition: elem_base.cpp:98
Real mach
Flight Mach number.
Class defines the conversion and some basic operations on primitive fluid variables used in calculati...
libMesh::EquationSystems * _eq_sys
MAST::ConservativeFluidDiscipline * _discipline
MAST::BoundaryConditionBase * _far_field_bc
bool check_jacobian(ValType &val)
bool compare_matrix(const RealMatrixX &m0, const RealMatrixX &m, const Real tol)
libMesh::Real Real
RealVectorX flow_unit_vector
direction along which flow is defined
void init(bool if_viscous)
libMesh::LibMeshInit * _libmesh_init
void init()
initializes the data structures
libMesh::LibMeshInit & _init
std::set< MAST::BoundaryConditionBase * > _boundary_conditions
MAST::NonlinearSystem * _sys
Matrix< Real, Dynamic, Dynamic > RealMatrixX
MAST::ConservativeFluidElementBase * _fluid_elem
MAST::FirstOrderNewmarkTransientSolver * _transient_solver
virtual void set_discipline_and_system(MAST::PhysicsDisciplineBase &discipline, MAST::SystemInitialization &system)
attaches a system to this discipline
Matrix< Real, Dynamic, 1 > RealVectorX
void init_solution_for_elem(RealVectorX &s)
MAST::TransientAssembly * _assembly
void set_flight_condition(MAST::FlightCondition &flt)
Attaches the flight condition that specifies the far-field flow properties.
void init(const unsigned int dim, const RealVectorX &conservative_sol, const Real cp_val, const Real cv_val, bool if_viscous)
This class acts as a wrapper around libMesh::Elem for the purpose of providing a uniform interface fo...
Definition: geom_elem.h:59
MAST::BoundaryConditionBase * _noslip_wall_bc
GasProperty gas_property
Ambient air properties.
void init_primitive_sol(MAST::PrimitiveSolution &s)
Real rho
Property values for ideal gas.
Definition: gas_property.h:71
virtual void set_solution(const RealVectorX &vec, bool if_sens=false)
stores vec as solution for element level calculations, or its sensitivity if if_sens is true...
Definition: elem_base.cpp:60
virtual void set_discipline_and_system(MAST::PhysicsDisciplineBase &discipline, MAST::SystemInitialization &system)
attaches a system to this discipline
virtual void init(const libMesh::Elem &elem, const MAST::SystemInitialization &sys_init)
initialize the object for the specified reference elem.
Definition: geom_elem.cpp:134
MAST::ConservativeFluidSystemInitialization * _sys_init
libMesh::UnstructuredMesh * _mesh
MAST::BoundaryConditionBase * _slip_wall_bc