1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2020 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 
20 // <h1>Systems Example 3 - Navier-Stokes with SCALAR Lagrange Multiplier</h1>
21 // \author David Knezevic
22 // \date 2010
23 //
24 // This example shows how the transient Navier-Stokes problem from
25 // systems_of_equations_ex2 can be solved using a scalar Lagrange multiplier
26 // formulation to constrain the integral of the pressure variable,
27 // rather than pinning the pressure at a single point.
28 
29 // C++ include files that we need
30 #include <iostream>
31 #include <algorithm>
32 #include <sstream>
33 #include <math.h>
34 
35 // Basic include file needed for the mesh functionality.
36 #include "libmesh/libmesh.h"
37 #include "libmesh/mesh.h"
38 #include "libmesh/mesh_generation.h"
39 #include "libmesh/exodusII_io.h"
40 #include "libmesh/equation_systems.h"
41 #include "libmesh/fe.h"
42 #include "libmesh/quadrature_gauss.h"
43 #include "libmesh/dof_map.h"
44 #include "libmesh/sparse_matrix.h"
45 #include "libmesh/numeric_vector.h"
46 #include "libmesh/dense_matrix.h"
47 #include "libmesh/dense_vector.h"
48 #include "libmesh/linear_implicit_system.h"
49 #include "libmesh/transient_system.h"
50 #include "libmesh/perf_log.h"
51 #include "libmesh/boundary_info.h"
52 #include "libmesh/utility.h"
53 #include "libmesh/dirichlet_boundaries.h"
54 #include "libmesh/zero_function.h"
55 #include "libmesh/const_function.h"
56 #include "libmesh/enum_solver_package.h"
57 
58 // For systems of equations the DenseSubMatrix
59 // and DenseSubVector provide convenient ways for
60 // assembling the element matrix and vector on a
61 // component-by-component basis.
62 #include "libmesh/dense_submatrix.h"
63 #include "libmesh/dense_subvector.h"
64 
65 // The definition of a geometric element
66 #include "libmesh/elem.h"
67 
68 // Bring in everything from the libMesh namespace
69 using namespace libMesh;
70 
71 // Function prototype.  This function will assemble the system
72 // matrix and right-hand-side.
73 void assemble_stokes (EquationSystems & es,
74                       const std::string & system_name);
75 
76 // Function which sets Dirichlet BCs for the lid-driven cavity.
77 void set_lid_driven_bcs(TransientLinearImplicitSystem & system);
78 
79 // The main program.
main(int argc,char ** argv)80 int main (int argc, char ** argv)
81 {
82   // Initialize libMesh.
83   LibMeshInit init (argc, argv);
84 
85   // This example requires a linear solver package.
86   libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
87                            "--enable-petsc, --enable-trilinos, or --enable-eigen");
88 
89   // Skip this 2D example if libMesh was compiled as 1D-only.
90   libmesh_example_requires(2 <= LIBMESH_DIM, "2D support");
91 
92   // We use Dirichlet boundary conditions here
93 #ifndef LIBMESH_ENABLE_DIRICHLET
94   libmesh_example_requires(false, "--enable-dirichlet");
95 #endif
96 
97   // This example NaNs with the Eigen sparse linear solvers and
98   // Trilinos solvers, but should work OK with either PETSc or
99   // Laspack.
100   libmesh_example_requires(libMesh::default_solver_package() != EIGEN_SOLVERS, "--enable-petsc or --enable-laspack");
101   libmesh_example_requires(libMesh::default_solver_package() != TRILINOS_SOLVERS, "--enable-petsc or --enable-laspack");
102 
103   // Create a mesh, with dimension to be overridden later, distributed
104   // across the default MPI communicator.
105   Mesh mesh(init.comm());
106 
107   // Use the MeshTools::Generation mesh generator to create a uniform
108   // 2D grid on the square [-1,1]^2.  We instruct the mesh generator
109   // to build a mesh of 8x8 Quad9 elements in 2D.  Building these
110   // higher-order elements allows us to use higher-order
111   // approximation, as in example 3.
112   MeshTools::Generation::build_square (mesh,
113                                        20, 20,
114                                        0., 1.,
115                                        0., 1.,
116                                        QUAD9);
117 
118   // Print information about the mesh to the screen.
119   mesh.print_info();
120 
121   // Create an equation systems object.
122   EquationSystems equation_systems (mesh);
123 
124   // Declare the system and its variables.
125   // Creates a transient system named "Navier-Stokes"
126   TransientLinearImplicitSystem & system =
127     equation_systems.add_system<TransientLinearImplicitSystem> ("Navier-Stokes");
128 
129   // Add the variables "vel_x" & "vel_y" to "Navier-Stokes".  They
130   // will be approximated using second-order approximation.
131   system.add_variable ("vel_x", SECOND);
132   system.add_variable ("vel_y", SECOND);
133 
134   // Add the variable "p" to "Navier-Stokes". This will
135   // be approximated with a first-order basis,
136   // providing an LBB-stable pressure-velocity pair.
137   system.add_variable ("p", FIRST);
138 
139   // Add a scalar Lagrange multiplier to constrain the
140   // pressure to have zero mean.
141   system.add_variable ("alpha", FIRST, SCALAR);
142 
143   // Give the system a pointer to the matrix assembly
144   // function.
145   system.attach_assemble_function (assemble_stokes);
146 
147   // Set Dirichlet boundary conditions.
148   set_lid_driven_bcs(system);
149 
150   // Initialize the data structures for the equation system.
151   equation_systems.init ();
152 
153   // Prints information about the system to the screen.
154   equation_systems.print_info();
155 
156   // Create a performance-logging object for this example
157   PerfLog perf_log("Systems Example 3");
158 
159   // Get a reference to the Stokes system to use later.
160   TransientLinearImplicitSystem & navier_stokes_system =
161     equation_systems.get_system<TransientLinearImplicitSystem>("Navier-Stokes");
162 
163   // Now we begin the timestep loop to compute the time-accurate
164   // solution of the equations.
165   const Real dt = 0.1;
166   navier_stokes_system.time = 0.0;
167   const unsigned int n_timesteps = 15;
168 
169   // The number of steps and the stopping criterion are also required
170   // for the nonlinear iterations.
171   const unsigned int n_nonlinear_steps = 15;
172   const Real nonlinear_tolerance = TOLERANCE*10;
173 
174   // We also set a standard linear solver flag in the EquationSystems object
175   // which controls the maximum number of linear solver iterations allowed.
176   equation_systems.parameters.set<unsigned int>("linear solver maximum iterations") = 250;
177 
178   // Tell the system of equations what the timestep is by using
179   // the set_parameter function.  The matrix assembly routine can
180   // then reference this parameter.
181   equation_systems.parameters.set<Real> ("dt") = dt;
182 
183   // The kinematic viscosity, nu = mu/rho, units of length**2/time.
184   equation_systems.parameters.set<Real> ("nu") = .007;
185 
186   // The first thing to do is to get a copy of the solution at
187   // the current nonlinear iteration.  This value will be used to
188   // determine if we can exit the nonlinear loop.
189   std::unique_ptr<NumericVector<Number>>
190     last_nonlinear_soln (navier_stokes_system.solution->clone());
191 
192 #ifdef LIBMESH_HAVE_EXODUS_API
193   // Since we are not doing adaptivity, write all solutions to a single Exodus file.
194   ExodusII_IO exo_io(mesh);
195 
196   // Write out the initial condition
197   exo_io.write_equation_systems ("out.e", equation_systems);
198 #endif
199 
200   for (unsigned int t_step=1; t_step<=n_timesteps; ++t_step)
201     {
202       // Increment the time counter, set the time and the
203       // time step size as parameters in the EquationSystem.
204       navier_stokes_system.time += dt;
205 
206       // A pretty update message
207       libMesh::out << "\n\n*** Solving time step "
208                    << t_step
209                    << ", time = "
210                    << navier_stokes_system.time
211                    << " ***"
212                    << std::endl;
213 
214       // Now we need to update the solution vector from the
215       // previous time step.  This is done directly through
216       // the reference to the Stokes system.
217       *navier_stokes_system.old_local_solution = *navier_stokes_system.current_local_solution;
218 
219       // At the beginning of each solve, reset the linear solver tolerance
220       // to a "reasonable" starting value.
221       const Real initial_linear_solver_tol = 1.e-6;
222       equation_systems.parameters.set<Real> ("linear solver tolerance") = initial_linear_solver_tol;
223 
224       // We'll set this flag when convergence is (hopefully) achieved.
225       bool converged = false;
226 
227       // Now we begin the nonlinear loop
228       for (unsigned int l=0; l<n_nonlinear_steps; ++l)
229         {
230           // Update the nonlinear solution.
231           last_nonlinear_soln->zero();
232           last_nonlinear_soln->add(*navier_stokes_system.solution);
233 
234           // Assemble & solve the linear system.
235           perf_log.push("linear solve");
236           equation_systems.get_system("Navier-Stokes").solve();
237           perf_log.pop("linear solve");
238 
239           // Compute the difference between this solution and the last
240           // nonlinear iterate.
241           last_nonlinear_soln->add (-1., *navier_stokes_system.solution);
242 
243           // Close the vector before computing its norm
244           last_nonlinear_soln->close();
245 
246           // Compute the l2 norm of the difference
247           const Real norm_delta = last_nonlinear_soln->l2_norm();
248 
249           // How many iterations were required to solve the linear system?
250           const unsigned int n_linear_iterations = navier_stokes_system.n_linear_iterations();
251 
252           // What was the final residual of the linear system?
253           const Real final_linear_residual = navier_stokes_system.final_linear_residual();
254 
255           // If the solver did no work (sometimes -ksp_converged_reason
256           // says "Linear solve converged due to CONVERGED_RTOL
257           // iterations 0") but the nonlinear residual norm is above
258           // the tolerance, we need to pick an even lower linear
259           // solver tolerance and try again.  Note that the tolerance
260           // is relative to the norm of the RHS, which for this
261           // particular problem does not go to zero, since we are
262           // solving for the full solution rather than the update.
263           //
264           // Similarly, if the solver did no work and this is the 0th
265           // nonlinear step, it means that the delta between solutions
266           // is being inaccurately measured as "0" since the solution
267           // did not change.  Decrease the tolerance and try again.
268           if (n_linear_iterations == 0 &&
269               (navier_stokes_system.final_linear_residual() >= nonlinear_tolerance || l==0))
270             {
271               Real old_linear_solver_tolerance = equation_systems.parameters.get<Real> ("linear solver tolerance");
272               equation_systems.parameters.set<Real> ("linear solver tolerance") = 1.e-3 * old_linear_solver_tolerance;
273               continue;
274             }
275 
276           // Print out convergence information for the linear and
277           // nonlinear iterations.
278           libMesh::out << "Linear solver converged at step: "
279                        << n_linear_iterations
280                        << ", final residual: "
281                        << final_linear_residual
282                        << "  Nonlinear convergence: ||u - u_old|| = "
283                        << norm_delta
284                        << std::endl;
285 
286           // Terminate the solution iteration if the difference between
287           // this nonlinear iterate and the last is sufficiently small, AND
288           // if the most recent linear system was solved to a sufficient tolerance.
289           if ((norm_delta < nonlinear_tolerance) &&
290               (navier_stokes_system.final_linear_residual() < nonlinear_tolerance))
291             {
292               libMesh::out << " Nonlinear solver converged at step "
293                            << l
294                            << std::endl;
295               converged = true;
296               break;
297             }
298 
299           // Otherwise, decrease the linear system tolerance.  For the inexact Newton
300           // method, the linear solver tolerance needs to decrease as we get closer to
301           // the solution to ensure quadratic convergence.  The new linear solver tolerance
302           // is chosen (heuristically) as the square of the previous linear system residual norm.
303           //Real flr2 = final_linear_residual*final_linear_residual;
304           Real new_linear_solver_tolerance = std::min(Utility::pow<2>(final_linear_residual), initial_linear_solver_tol);
305           equation_systems.parameters.set<Real> ("linear solver tolerance") = new_linear_solver_tolerance;
306         } // end nonlinear loop
307 
308       // Don't keep going if we failed to converge.
309       libmesh_error_msg_if(!converged, "Error: Newton iterations failed to converge!");
310 
311 #ifdef LIBMESH_HAVE_EXODUS_API
312       // Write out every nth timestep to file.
313       const unsigned int write_interval = 1;
314 
315       if ((t_step+1)%write_interval == 0)
316         {
317           exo_io.write_timestep("out.e",
318                                 equation_systems,
319                                 t_step+1, // we're off by one since we wrote the IC and the Exodus numbering is 1-based.
320                                 navier_stokes_system.time);
321         }
322 #endif // #ifdef LIBMESH_HAVE_EXODUS_API
323     } // end timestep loop.
324 
325   // All done.
326   return 0;
327 }
328 
329 
330 
331 
332 
333 
334 // The matrix assembly function to be called at each time step to
335 // prepare for the linear solve.
assemble_stokes(EquationSystems & es,const std::string & libmesh_dbg_var (system_name))336 void assemble_stokes (EquationSystems & es,
337                       const std::string & libmesh_dbg_var(system_name))
338 {
339   // It is a good idea to make sure we are assembling
340   // the proper system.
341   libmesh_assert_equal_to (system_name, "Navier-Stokes");
342 
343 #if LIBMESH_DIM > 1
344   // Get a constant reference to the mesh object.
345   const MeshBase & mesh = es.get_mesh();
346 
347   // The dimension that we are running
348   const unsigned int dim = mesh.mesh_dimension();
349 
350   // Get a reference to the Stokes system object.
351   TransientLinearImplicitSystem & navier_stokes_system =
352     es.get_system<TransientLinearImplicitSystem> ("Navier-Stokes");
353 
354   // Numeric ids corresponding to each variable in the system
355   const unsigned int u_var = navier_stokes_system.variable_number ("vel_x");
356   const unsigned int v_var = navier_stokes_system.variable_number ("vel_y");
357   const unsigned int p_var = navier_stokes_system.variable_number ("p");
358   const unsigned int alpha_var = navier_stokes_system.variable_number ("alpha");
359 
360   // Get the Finite Element type for "vel_x".  Note this will be
361   // the same as the type for "vel_y".
362   FEType fe_vel_type = navier_stokes_system.variable_type(u_var);
363 
364   // Get the Finite Element type for "p".
365   FEType fe_pres_type = navier_stokes_system.variable_type(p_var);
366 
367   // Build a Finite Element object of the specified type for
368   // the velocity variables.
369   std::unique_ptr<FEBase> fe_vel  (FEBase::build(dim, fe_vel_type));
370 
371   // Build a Finite Element object of the specified type for
372   // the pressure variables.
373   std::unique_ptr<FEBase> fe_pres (FEBase::build(dim, fe_pres_type));
374 
375   // A Gauss quadrature rule for numerical integration.
376   // Let the FEType object decide what order rule is appropriate.
377   QGauss qrule (dim, fe_vel_type.default_quadrature_order());
378 
379   // Tell the finite element objects to use our quadrature rule.
380   fe_vel->attach_quadrature_rule (&qrule);
381   fe_pres->attach_quadrature_rule (&qrule);
382 
383   // Here we define some references to cell-specific data that
384   // will be used to assemble the linear system.
385   //
386   // The element Jacobian * quadrature weight at each integration point.
387   const std::vector<Real> & JxW = fe_vel->get_JxW();
388 
389   // The element shape functions evaluated at the quadrature points.
390   const std::vector<std::vector<Real>> & phi = fe_vel->get_phi();
391 
392   // The element shape function gradients for the velocity
393   // variables evaluated at the quadrature points.
394   const std::vector<std::vector<RealGradient>> & dphi = fe_vel->get_dphi();
395 
396   // The element shape functions for the pressure variable
397   // evaluated at the quadrature points.
398   const std::vector<std::vector<Real>> & psi = fe_pres->get_phi();
399 
400   // The value of the linear shape function gradients at the quadrature points
401   // const std::vector<std::vector<RealGradient>> & dpsi = fe_pres->get_dphi();
402 
403   // A reference to the DofMap object for this system.  The DofMap
404   // object handles the index translation from node and element numbers
405   // to degree of freedom numbers.  We will talk more about the DofMap
406   // in future examples.
407   const DofMap & dof_map = navier_stokes_system.get_dof_map();
408 
409   // Define data structures to contain the element matrix
410   // and right-hand-side vector contribution.  Following
411   // basic finite element terminology we will denote these
412   // "Ke" and "Fe".
413   DenseMatrix<Number> Ke;
414   DenseVector<Number> Fe;
415 
416   DenseSubMatrix<Number>
417     Kuu(Ke), Kuv(Ke), Kup(Ke),
418     Kvu(Ke), Kvv(Ke), Kvp(Ke),
419     Kpu(Ke), Kpv(Ke), Kpp(Ke);
420   DenseSubMatrix<Number> Kalpha_p(Ke), Kp_alpha(Ke);
421 
422   DenseSubVector<Number>
423     Fu(Fe),
424     Fv(Fe),
425     Fp(Fe);
426 
427   // This vector will hold the degree of freedom indices for
428   // the element.  These define where in the global system
429   // the element degrees of freedom get mapped.
430   std::vector<dof_id_type> dof_indices;
431   std::vector<dof_id_type> dof_indices_u;
432   std::vector<dof_id_type> dof_indices_v;
433   std::vector<dof_id_type> dof_indices_p;
434   std::vector<dof_id_type> dof_indices_alpha;
435 
436   // Find out what the timestep size parameter is from the system, and
437   // the value of theta for the theta method.  We use implicit Euler (theta=1)
438   // for this simulation even though it is only first-order accurate in time.
439   // The reason for this decision is that the second-order Crank-Nicolson
440   // method is notoriously oscillatory for problems with discontinuous
441   // initial data such as the lid-driven cavity.  Therefore,
442   // we sacrifice accuracy in time for stability, but since the solution
443   // reaches steady state relatively quickly we can afford to take small
444   // timesteps.  If you monitor the initial nonlinear residual for this
445   // simulation, you should see that it is monotonically decreasing in time.
446   const Real dt    = es.parameters.get<Real>("dt");
447   const Real theta = 1.;
448 
449   // The kinematic viscosity, multiplies the "viscous" terms.
450   const Real nu = es.parameters.get<Real>("nu");
451 
452   // Now we will loop over all the elements in the mesh that
453   // live on the local processor. We will compute the element
454   // matrix and right-hand-side contribution.  Since the mesh
455   // will be refined we want to only consider the ACTIVE elements,
456   // hence we use a variant of the active_elem_iterator.
457   for (const auto & elem : mesh.active_local_element_ptr_range())
458     {
459       // Get the degree of freedom indices for the
460       // current element.  These define where in the global
461       // matrix and right-hand-side this element will
462       // contribute to.
463       dof_map.dof_indices (elem, dof_indices);
464       dof_map.dof_indices (elem, dof_indices_u, u_var);
465       dof_map.dof_indices (elem, dof_indices_v, v_var);
466       dof_map.dof_indices (elem, dof_indices_p, p_var);
467       dof_map.dof_indices (elem, dof_indices_alpha, alpha_var);
468 
469       const unsigned int n_dofs   = dof_indices.size();
470       const unsigned int n_u_dofs = dof_indices_u.size();
471       const unsigned int n_v_dofs = dof_indices_v.size();
472       const unsigned int n_p_dofs = dof_indices_p.size();
473 
474       // Compute the element-specific data for the current
475       // element.  This involves computing the location of the
476       // quadrature points (q_point) and the shape functions
477       // (phi, dphi) for the current element.
478       fe_vel->reinit  (elem);
479       fe_pres->reinit (elem);
480 
481       // Zero the element matrix and right-hand side before
482       // summing them.  We use the resize member here because
483       // the number of degrees of freedom might have changed from
484       // the last element.  Note that this will be the case if the
485       // element type is different (i.e. the last element was a
486       // triangle, now we are on a quadrilateral).
487       Ke.resize (n_dofs, n_dofs);
488       Fe.resize (n_dofs);
489 
490       // Reposition the submatrices...  The idea is this:
491       //
492       //         -           -          -  -
493       //        | Kuu Kuv Kup |        | Fu |
494       //   Ke = | Kvu Kvv Kvp |;  Fe = | Fv |
495       //        | Kpu Kpv Kpp |        | Fp |
496       //         -           -          -  -
497       //
498       // The DenseSubMatrix.reposition () member takes the
499       // (row_offset, column_offset, row_size, column_size).
500       //
501       // Similarly, the DenseSubVector.reposition () member
502       // takes the (row_offset, row_size)
503       Kuu.reposition (u_var*n_u_dofs, u_var*n_u_dofs, n_u_dofs, n_u_dofs);
504       Kuv.reposition (u_var*n_u_dofs, v_var*n_u_dofs, n_u_dofs, n_v_dofs);
505       Kup.reposition (u_var*n_u_dofs, p_var*n_u_dofs, n_u_dofs, n_p_dofs);
506 
507       Kvu.reposition (v_var*n_v_dofs, u_var*n_v_dofs, n_v_dofs, n_u_dofs);
508       Kvv.reposition (v_var*n_v_dofs, v_var*n_v_dofs, n_v_dofs, n_v_dofs);
509       Kvp.reposition (v_var*n_v_dofs, p_var*n_v_dofs, n_v_dofs, n_p_dofs);
510 
511       Kpu.reposition (p_var*n_u_dofs, u_var*n_u_dofs, n_p_dofs, n_u_dofs);
512       Kpv.reposition (p_var*n_u_dofs, v_var*n_u_dofs, n_p_dofs, n_v_dofs);
513       Kpp.reposition (p_var*n_u_dofs, p_var*n_u_dofs, n_p_dofs, n_p_dofs);
514 
515       // Also, add a row and a column to constrain the pressure
516       Kp_alpha.reposition (p_var*n_u_dofs, p_var*n_u_dofs+n_p_dofs, n_p_dofs, 1);
517       Kalpha_p.reposition (p_var*n_u_dofs+n_p_dofs, p_var*n_u_dofs, 1, n_p_dofs);
518 
519 
520       Fu.reposition (u_var*n_u_dofs, n_u_dofs);
521       Fv.reposition (v_var*n_u_dofs, n_v_dofs);
522       Fp.reposition (p_var*n_u_dofs, n_p_dofs);
523 
524       // Now we will build the element matrix and right-hand-side.
525       // Constructing the RHS requires the solution and its
526       // gradient from the previous timestep.  This must be
527       // calculated at each quadrature point by summing the
528       // solution degree-of-freedom values by the appropriate
529       // weight functions.
530       for (unsigned int qp=0; qp<qrule.n_points(); qp++)
531         {
532           // Values to hold the solution & its gradient at the previous timestep.
533           Number u = 0., u_old = 0.;
534           Number v = 0., v_old = 0.;
535           Number p_old = 0.;
536           Gradient grad_u, grad_u_old;
537           Gradient grad_v, grad_v_old;
538 
539           // Compute the velocity & its gradient from the previous timestep
540           // and the old Newton iterate.
541           for (unsigned int l=0; l<n_u_dofs; l++)
542             {
543               // From the old timestep:
544               u_old += phi[l][qp]*navier_stokes_system.old_solution (dof_indices_u[l]);
545               v_old += phi[l][qp]*navier_stokes_system.old_solution (dof_indices_v[l]);
546               grad_u_old.add_scaled (dphi[l][qp], navier_stokes_system.old_solution (dof_indices_u[l]));
547               grad_v_old.add_scaled (dphi[l][qp], navier_stokes_system.old_solution (dof_indices_v[l]));
548 
549               // From the previous Newton iterate:
550               u += phi[l][qp]*navier_stokes_system.current_solution (dof_indices_u[l]);
551               v += phi[l][qp]*navier_stokes_system.current_solution (dof_indices_v[l]);
552               grad_u.add_scaled (dphi[l][qp], navier_stokes_system.current_solution (dof_indices_u[l]));
553               grad_v.add_scaled (dphi[l][qp], navier_stokes_system.current_solution (dof_indices_v[l]));
554             }
555 
556           // Compute the old pressure value at this quadrature point.
557           for (unsigned int l=0; l<n_p_dofs; l++)
558             p_old += psi[l][qp]*navier_stokes_system.old_solution (dof_indices_p[l]);
559 
560           // Definitions for convenience.  It is sometimes simpler to do a
561           // dot product if you have the full vector at your disposal.
562           const NumberVectorValue U_old (u_old, v_old);
563           const NumberVectorValue U     (u,     v);
564           const Number u_x = grad_u(0);
565           const Number u_y = grad_u(1);
566           const Number v_x = grad_v(0);
567           const Number v_y = grad_v(1);
568 
569           // First, an i-loop over the velocity degrees of freedom.
570           // We know that n_u_dofs == n_v_dofs so we can compute contributions
571           // for both at the same time.
572           for (unsigned int i=0; i<n_u_dofs; i++)
573             {
574               Fu(i) += JxW[qp]*(u_old*phi[i][qp] -                            // mass-matrix term
575                                 (1.-theta)*dt*(U_old*grad_u_old)*phi[i][qp] + // convection term
576                                 (1.-theta)*dt*p_old*dphi[i][qp](0)  -         // pressure term on rhs
577                                 (1.-theta)*dt*nu*(grad_u_old*dphi[i][qp]) +   // diffusion term on rhs
578                                 theta*dt*(U*grad_u)*phi[i][qp]);              // Newton term
579 
580 
581               Fv(i) += JxW[qp]*(v_old*phi[i][qp] -                             // mass-matrix term
582                                 (1.-theta)*dt*(U_old*grad_v_old)*phi[i][qp] +  // convection term
583                                 (1.-theta)*dt*p_old*dphi[i][qp](1) -           // pressure term on rhs
584                                 (1.-theta)*dt*nu*(grad_v_old*dphi[i][qp]) +    // diffusion term on rhs
585                                 theta*dt*(U*grad_v)*phi[i][qp]);               // Newton term
586 
587 
588               // Note that the Fp block is identically zero unless we are using
589               // some kind of artificial compressibility scheme...
590 
591               // Matrix contributions for the uu and vv couplings.
592               for (unsigned int j=0; j<n_u_dofs; j++)
593                 {
594                   Kuu(i,j) += JxW[qp]*(phi[i][qp]*phi[j][qp] +                 // mass matrix term
595                                        theta*dt*nu*(dphi[i][qp]*dphi[j][qp]) + // diffusion term
596                                        theta*dt*(U*dphi[j][qp])*phi[i][qp] +   // convection term
597                                        theta*dt*u_x*phi[i][qp]*phi[j][qp]);    // Newton term
598 
599                   Kuv(i,j) += JxW[qp]*theta*dt*u_y*phi[i][qp]*phi[j][qp];      // Newton term
600 
601                   Kvv(i,j) += JxW[qp]*(phi[i][qp]*phi[j][qp] +                 // mass matrix term
602                                        theta*dt*nu*(dphi[i][qp]*dphi[j][qp]) + // diffusion term
603                                        theta*dt*(U*dphi[j][qp])*phi[i][qp] +   // convection term
604                                        theta*dt*v_y*phi[i][qp]*phi[j][qp]);    // Newton term
605 
606                   Kvu(i,j) += JxW[qp]*theta*dt*v_x*phi[i][qp]*phi[j][qp];      // Newton term
607                 }
608 
609               // Matrix contributions for the up and vp couplings.
610               for (unsigned int j=0; j<n_p_dofs; j++)
611                 {
612                   Kup(i,j) += JxW[qp]*(-theta*dt*psi[j][qp]*dphi[i][qp](0));
613                   Kvp(i,j) += JxW[qp]*(-theta*dt*psi[j][qp]*dphi[i][qp](1));
614                 }
615             }
616 
617           // Now an i-loop over the pressure degrees of freedom.  This code computes
618           // the matrix entries due to the continuity equation.  Note: To maintain a
619           // symmetric matrix, we may (or may not) multiply the continuity equation by
620           // negative one.  Here we do not.
621           for (unsigned int i=0; i<n_p_dofs; i++)
622             {
623               Kp_alpha(i,0) += JxW[qp]*psi[i][qp];
624               Kalpha_p(0,i) += JxW[qp]*psi[i][qp];
625               for (unsigned int j=0; j<n_u_dofs; j++)
626                 {
627                   Kpu(i,j) += JxW[qp]*psi[i][qp]*dphi[j][qp](0);
628                   Kpv(i,j) += JxW[qp]*psi[i][qp]*dphi[j][qp](1);
629                 }
630             }
631         } // end of the quadrature point qp-loop
632 
633       // Since we're using heterogeneous DirichletBoundary objects for
634       // the boundary conditions, we need to call a specific function
635       // to constrain the element stiffness matrix.
636       dof_map.heterogenously_constrain_element_matrix_and_vector (Ke, Fe, dof_indices);
637 
638       // The element matrix and right-hand-side are now built
639       // for this element.  Add them to the global matrix and
640       // right-hand-side vector.  The SparseMatrix::add_matrix()
641       // and NumericVector::add_vector() members do this for us.
642       navier_stokes_system.matrix->add_matrix (Ke, dof_indices);
643       navier_stokes_system.rhs->add_vector    (Fe, dof_indices);
644     } // end of element loop
645 
646   // We can set the mean of the pressure by setting Falpha.  Typically
647   // a value of zero is chosen, but the value should be arbitrary.
648   navier_stokes_system.rhs->add(navier_stokes_system.rhs->size()-1, 10.);
649 #else
650   libmesh_ignore(es);
651 #endif
652 }
653 
654 
655 
set_lid_driven_bcs(TransientLinearImplicitSystem & system)656 void set_lid_driven_bcs(TransientLinearImplicitSystem & system)
657 {
658 #ifdef LIBMESH_ENABLE_DIRICHLET
659   unsigned short int
660     u_var = system.variable_number("vel_x"),
661     v_var = system.variable_number("vel_y");
662 
663   // Get a convenient reference to the System's DofMap
664   DofMap & dof_map = system.get_dof_map();
665 
666   {
667     // u=v=0 on bottom, left, right
668     std::set<boundary_id_type> boundary_ids;
669     boundary_ids.insert(0);
670     boundary_ids.insert(1);
671     boundary_ids.insert(3);
672 
673     std::vector<unsigned int> variables;
674     variables.push_back(u_var);
675     variables.push_back(v_var);
676 
677     dof_map.add_dirichlet_boundary(DirichletBoundary(boundary_ids,
678                                                      variables,
679                                                      ZeroFunction<Number>()));
680   }
681   {
682     // u=1 on top
683     std::set<boundary_id_type> boundary_ids;
684     boundary_ids.insert(2);
685 
686     std::vector<unsigned int> variables;
687     variables.push_back(u_var);
688 
689     dof_map.add_dirichlet_boundary(DirichletBoundary(boundary_ids,
690                                                      variables,
691                                                      ConstFunction<Number>(1.)));
692   }
693   {
694     // v=0 on top
695     std::set<boundary_id_type> boundary_ids;
696     boundary_ids.insert(2);
697 
698     std::vector<unsigned int> variables;
699     variables.push_back(v_var);
700 
701     dof_map.add_dirichlet_boundary(DirichletBoundary(boundary_ids,
702                                                      variables,
703                                                      ZeroFunction<Number>()));
704   }
705 #else
706   libmesh_ignore(system);
707 #endif
708 }
709