Reference documentation for deal.II version GIT relicensing-422-gb369f187d8 2024-04-17 18:10:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
step-55.h
Go to the documentation of this file.
1
234 *  
235 *   namespace LA
236 *   {
237 *   #if defined(DEAL_II_WITH_PETSC) && !defined(DEAL_II_PETSC_WITH_COMPLEX) && \
238 *   !(defined(DEAL_II_WITH_TRILINOS) && defined(FORCE_USE_OF_TRILINOS))
239 *   using namespace dealii::LinearAlgebraPETSc;
240 *   # define USE_PETSC_LA
241 *   #elif defined(DEAL_II_WITH_TRILINOS)
242 *   using namespace dealii::LinearAlgebraTrilinos;
243 *   #else
244 *   # error DEAL_II_WITH_PETSC or DEAL_II_WITH_TRILINOS required
245 *   #endif
246 *   } // namespace LA
247 *  
248 *   #include <deal.II/lac/vector.h>
249 *   #include <deal.II/lac/full_matrix.h>
250 *   #include <deal.II/lac/solver_cg.h>
251 *   #include <deal.II/lac/solver_gmres.h>
252 *   #include <deal.II/lac/solver_minres.h>
253 *   #include <deal.II/lac/affine_constraints.h>
254 *   #include <deal.II/lac/dynamic_sparsity_pattern.h>
255 *  
256 *   #include <deal.II/lac/petsc_sparse_matrix.h>
257 *   #include <deal.II/lac/petsc_vector.h>
258 *   #include <deal.II/lac/petsc_solver.h>
259 *   #include <deal.II/lac/petsc_precondition.h>
260 *  
261 *   #include <deal.II/grid/grid_generator.h>
262 *   #include <deal.II/grid/manifold_lib.h>
263 *   #include <deal.II/grid/grid_tools.h>
264 *   #include <deal.II/dofs/dof_handler.h>
265 *   #include <deal.II/dofs/dof_renumbering.h>
266 *   #include <deal.II/dofs/dof_tools.h>
267 *   #include <deal.II/fe/fe_values.h>
268 *   #include <deal.II/fe/fe_q.h>
269 *   #include <deal.II/fe/fe_system.h>
270 *   #include <deal.II/numerics/vector_tools.h>
271 *   #include <deal.II/numerics/data_out.h>
272 *   #include <deal.II/numerics/error_estimator.h>
273 *  
274 *   #include <deal.II/base/utilities.h>
275 *   #include <deal.II/base/conditional_ostream.h>
276 *   #include <deal.II/base/index_set.h>
277 *   #include <deal.II/lac/sparsity_tools.h>
278 *   #include <deal.II/distributed/tria.h>
279 *   #include <deal.II/distributed/grid_refinement.h>
280 *  
281 *   #include <cmath>
282 *   #include <fstream>
283 *   #include <iostream>
284 *  
285 *   namespace Step55
286 *   {
287 *   using namespace dealii;
288 *  
289 * @endcode
290 *
291 *
292 * <a name="step_55-Linearsolversandpreconditioners"></a>
293 * <h3>Linear solvers and preconditioners</h3>
294 *
295
296 *
297 * We need a few helper classes to represent our solver strategy
298 * described in the introduction.
299 *
300
301 *
302 *
303 * @code
304 *   namespace LinearSolvers
305 *   {
306 * @endcode
307 *
308 * This class exposes the action of applying the inverse of a
309 * giving matrix via the function
310 * InverseMatrix::vmult(). Internally, the inverse is not formed
311 * explicitly. Instead, a linear solver with CG is performed. This
312 * class extends the InverseMatrix class in @ref step_22 "step-22" with an option
313 * to specify a preconditioner, and to allow for different vector
314 * types in the vmult function. We use the same mechanism as in
315 * @ref step_31 "step-31" to convert a run-time exception into a failed assertion
316 * should the inner solver not converge.
317 *
318 * @code
319 *   template <class Matrix, class Preconditioner>
320 *   class InverseMatrix : public Subscriptor
321 *   {
322 *   public:
323 *   InverseMatrix(const Matrix &m, const Preconditioner &preconditioner);
324 *  
325 *   template <typename VectorType>
326 *   void vmult(VectorType &dst, const VectorType &src) const;
327 *  
328 *   private:
330 *   const Preconditioner &preconditioner;
331 *   };
332 *  
333 *  
334 *   template <class Matrix, class Preconditioner>
335 *   InverseMatrix<Matrix, Preconditioner>::InverseMatrix(
336 *   const Matrix &m,
337 *   const Preconditioner &preconditioner)
338 *   : matrix(&m)
339 *   , preconditioner(preconditioner)
340 *   {}
341 *  
342 *  
343 *  
344 *   template <class Matrix, class Preconditioner>
345 *   template <typename VectorType>
346 *   void
347 *   InverseMatrix<Matrix, Preconditioner>::vmult(VectorType &dst,
348 *   const VectorType &src) const
349 *   {
350 *   SolverControl solver_control(src.size(), 1e-8 * src.l2_norm());
351 *   SolverCG<VectorType> cg(solver_control);
352 *   dst = 0;
353 *  
354 *   try
355 *   {
356 *   cg.solve(*matrix, dst, src, preconditioner);
357 *   }
358 *   catch (std::exception &e)
359 *   {
360 *   Assert(false, ExcMessage(e.what()));
361 *   }
362 *   }
363 *  
364 *  
365 * @endcode
366 *
367 * The class A template class for a simple block diagonal preconditioner
368 * for 2x2 matrices.
369 *
370 * @code
371 *   template <class PreconditionerA, class PreconditionerS>
372 *   class BlockDiagonalPreconditioner : public Subscriptor
373 *   {
374 *   public:
375 *   BlockDiagonalPreconditioner(const PreconditionerA &preconditioner_A,
376 *   const PreconditionerS &preconditioner_S);
377 *  
378 *   void vmult(LA::MPI::BlockVector &dst,
379 *   const LA::MPI::BlockVector &src) const;
380 *  
381 *   private:
382 *   const PreconditionerA &preconditioner_A;
383 *   const PreconditionerS &preconditioner_S;
384 *   };
385 *  
386 *   template <class PreconditionerA, class PreconditionerS>
387 *   BlockDiagonalPreconditioner<PreconditionerA, PreconditionerS>::
388 *   BlockDiagonalPreconditioner(const PreconditionerA &preconditioner_A,
389 *   const PreconditionerS &preconditioner_S)
390 *   : preconditioner_A(preconditioner_A)
391 *   , preconditioner_S(preconditioner_S)
392 *   {}
393 *  
394 *  
395 *   template <class PreconditionerA, class PreconditionerS>
396 *   void BlockDiagonalPreconditioner<PreconditionerA, PreconditionerS>::vmult(
397 *   LA::MPI::BlockVector &dst,
398 *   const LA::MPI::BlockVector &src) const
399 *   {
400 *   preconditioner_A.vmult(dst.block(0), src.block(0));
401 *   preconditioner_S.vmult(dst.block(1), src.block(1));
402 *   }
403 *  
404 *   } // namespace LinearSolvers
405 *  
406 * @endcode
407 *
408 *
409 * <a name="step_55-Problemsetup"></a>
410 * <h3>Problem setup</h3>
411 *
412
413 *
414 * The following classes represent the right hand side and the exact
415 * solution for the test problem.
416 *
417
418 *
419 *
420 * @code
421 *   template <int dim>
422 *   class RightHandSide : public Function<dim>
423 *   {
424 *   public:
425 *   RightHandSide()
426 *   : Function<dim>(dim + 1)
427 *   {}
428 *  
429 *   virtual void vector_value(const Point<dim> &p,
430 *   Vector<double> &value) const override;
431 *   };
432 *  
433 *  
434 *   template <int dim>
435 *   void RightHandSide<dim>::vector_value(const Point<dim> &p,
436 *   Vector<double> &values) const
437 *   {
438 *   const double R_x = p[0];
439 *   const double R_y = p[1];
440 *  
441 *   constexpr double pi = numbers::PI;
442 *   constexpr double pi2 = numbers::PI * numbers::PI;
443 *  
444 * @endcode
445 *
446 * velocity
447 *
448 * @code
449 *   values[0] = -1.0L / 2.0L * (-2 * std::sqrt(25.0 + 4 * pi2) + 10.0) *
450 *   std::exp(R_x * (-2 * std::sqrt(25.0 + 4 * pi2) + 10.0)) -
451 *   0.4 * pi2 * std::exp(R_x * (-std::sqrt(25.0 + 4 * pi2) + 5.0)) *
452 *   std::cos(2 * R_y * pi) +
453 *   0.1 *
454 *   Utilities::fixed_power<2>(-std::sqrt(25.0 + 4 * pi2) + 5.0) *
455 *   std::exp(R_x * (-std::sqrt(25.0 + 4 * pi2) + 5.0)) *
456 *   std::cos(2 * R_y * pi);
457 *   values[1] = 0.2 * pi * (-std::sqrt(25.0 + 4 * pi2) + 5.0) *
458 *   std::exp(R_x * (-std::sqrt(25.0 + 4 * pi2) + 5.0)) *
459 *   std::sin(2 * R_y * pi) -
460 *   0.05 *
461 *   Utilities::fixed_power<3>(-std::sqrt(25.0 + 4 * pi2) + 5.0) *
462 *   std::exp(R_x * (-std::sqrt(25.0 + 4 * pi2) + 5.0)) *
463 *   std::sin(2 * R_y * pi) / pi;
464 *  
465 * @endcode
466 *
467 * pressure
468 *
469 * @code
470 *   values[dim] = 0;
471 *   }
472 *  
473 *  
474 *   template <int dim>
475 *   class ExactSolution : public Function<dim>
476 *   {
477 *   public:
478 *   ExactSolution()
479 *   : Function<dim>(dim + 1)
480 *   {}
481 *  
482 *   virtual void vector_value(const Point<dim> &p,
483 *   Vector<double> &values) const override;
484 *   };
485 *  
486 *   template <int dim>
487 *   void ExactSolution<dim>::vector_value(const Point<dim> &p,
488 *   Vector<double> &values) const
489 *   {
490 *   const double R_x = p[0];
491 *   const double R_y = p[1];
492 *  
493 *   constexpr double pi = numbers::PI;
494 *   constexpr double pi2 = numbers::PI * numbers::PI;
495 *  
496 * @endcode
497 *
498 * velocity
499 *
500 * @code
501 *   values[0] = -std::exp(R_x * (-std::sqrt(25.0 + 4 * pi2) + 5.0)) *
502 *   std::cos(2 * R_y * pi) +
503 *   1;
504 *   values[1] = (1.0L / 2.0L) * (-std::sqrt(25.0 + 4 * pi2) + 5.0) *
505 *   std::exp(R_x * (-std::sqrt(25.0 + 4 * pi2) + 5.0)) *
506 *   std::sin(2 * R_y * pi) / pi;
507 *  
508 * @endcode
509 *
510 * pressure
511 *
512 * @code
513 *   values[dim] =
514 *   -1.0L / 2.0L * std::exp(R_x * (-2 * std::sqrt(25.0 + 4 * pi2) + 10.0)) -
515 *   2.0 *
516 *   (-6538034.74494422 +
517 *   0.0134758939981709 * std::exp(4 * std::sqrt(25.0 + 4 * pi2))) /
518 *   (-80.0 * std::exp(3 * std::sqrt(25.0 + 4 * pi2)) +
519 *   16.0 * std::sqrt(25.0 + 4 * pi2) *
520 *   std::exp(3 * std::sqrt(25.0 + 4 * pi2))) -
521 *   1634508.68623606 * std::exp(-3.0 * std::sqrt(25.0 + 4 * pi2)) /
522 *   (-10.0 + 2.0 * std::sqrt(25.0 + 4 * pi2)) +
523 *   (-0.00673794699908547 * std::exp(std::sqrt(25.0 + 4 * pi2)) +
524 *   3269017.37247211 * std::exp(-3 * std::sqrt(25.0 + 4 * pi2))) /
525 *   (-8 * std::sqrt(25.0 + 4 * pi2) + 40.0) +
526 *   0.00336897349954273 * std::exp(1.0 * std::sqrt(25.0 + 4 * pi2)) /
527 *   (-10.0 + 2.0 * std::sqrt(25.0 + 4 * pi2));
528 *   }
529 *  
530 *  
531 *  
532 * @endcode
533 *
534 *
535 * <a name="step_55-Themainprogram"></a>
536 * <h3>The main program</h3>
537 *
538
539 *
540 * The main class is very similar to @ref step_40 "step-40", except that matrices and
541 * vectors are now block versions, and we store a std::vector<IndexSet>
542 * for owned and relevant DoFs instead of a single IndexSet. We have
543 * exactly two IndexSets, one for all velocity unknowns and one for all
544 * pressure unknowns.
545 *
546 * @code
547 *   template <int dim>
548 *   class StokesProblem
549 *   {
550 *   public:
551 *   StokesProblem(unsigned int velocity_degree);
552 *  
553 *   void run();
554 *  
555 *   private:
556 *   void make_grid();
557 *   void setup_system();
558 *   void assemble_system();
559 *   void solve();
560 *   void refine_grid();
561 *   void output_results(const unsigned int cycle) const;
562 *  
563 *   unsigned int velocity_degree;
564 *   double viscosity;
565 *   MPI_Comm mpi_communicator;
566 *  
567 *   FESystem<dim> fe;
569 *   DoFHandler<dim> dof_handler;
570 *  
571 *   std::vector<IndexSet> owned_partitioning;
572 *   std::vector<IndexSet> relevant_partitioning;
573 *  
574 *   AffineConstraints<double> constraints;
575 *  
576 *   LA::MPI::BlockSparseMatrix system_matrix;
577 *   LA::MPI::BlockSparseMatrix preconditioner_matrix;
578 *   LA::MPI::BlockVector locally_relevant_solution;
579 *   LA::MPI::BlockVector system_rhs;
580 *  
581 *   ConditionalOStream pcout;
582 *   TimerOutput computing_timer;
583 *   };
584 *  
585 *  
586 *  
587 *   template <int dim>
588 *   StokesProblem<dim>::StokesProblem(unsigned int velocity_degree)
589 *   : velocity_degree(velocity_degree)
590 *   , viscosity(0.1)
591 *   , mpi_communicator(MPI_COMM_WORLD)
592 *   , fe(FE_Q<dim>(velocity_degree) ^ dim, FE_Q<dim>(velocity_degree - 1))
593 *   , triangulation(mpi_communicator,
597 *   , dof_handler(triangulation)
598 *   , pcout(std::cout,
599 *   (Utilities::MPI::this_mpi_process(mpi_communicator) == 0))
600 *   , computing_timer(mpi_communicator,
601 *   pcout,
604 *   {}
605 *  
606 *  
607 * @endcode
608 *
609 * The Kovasznay flow is defined on the domain [-0.5, 1.5]^2, which we
610 * create by passing the min and max values to GridGenerator::hyper_cube.
611 *
612 * @code
613 *   template <int dim>
614 *   void StokesProblem<dim>::make_grid()
615 *   {
617 *   triangulation.refine_global(3);
618 *   }
619 *  
620 * @endcode
621 *
622 *
623 * <a name="step_55-SystemSetup"></a>
624 * <h3>System Setup</h3>
625 *
626
627 *
628 * The construction of the block matrices and vectors is new compared to
629 * @ref step_40 "step-40" and is different compared to serial codes like @ref step_22 "step-22", because
630 * we need to supply the set of rows that belong to our processor.
631 *
632 * @code
633 *   template <int dim>
634 *   void StokesProblem<dim>::setup_system()
635 *   {
636 *   TimerOutput::Scope t(computing_timer, "setup");
637 *  
638 *   dof_handler.distribute_dofs(fe);
639 *  
640 * @endcode
641 *
642 * Put all dim velocities into block 0 and the pressure into block 1,
643 * then reorder the unknowns by block. Finally count how many unknowns
644 * we have per block.
645 *
646 * @code
647 *   std::vector<unsigned int> stokes_sub_blocks(dim + 1, 0);
648 *   stokes_sub_blocks[dim] = 1;
649 *   DoFRenumbering::component_wise(dof_handler, stokes_sub_blocks);
650 *  
651 *   const std::vector<types::global_dof_index> dofs_per_block =
652 *   DoFTools::count_dofs_per_fe_block(dof_handler, stokes_sub_blocks);
653 *  
654 *   const unsigned int n_u = dofs_per_block[0];
655 *   const unsigned int n_p = dofs_per_block[1];
656 *  
657 *   pcout << " Number of degrees of freedom: " << dof_handler.n_dofs() << " ("
658 *   << n_u << '+' << n_p << ')' << std::endl;
659 *  
660 * @endcode
661 *
662 * We split up the IndexSet for locally owned and locally relevant DoFs
663 * into two IndexSets based on how we want to create the block matrices
664 * and vectors.
665 *
666 * @code
667 *   const IndexSet &locally_owned_dofs = dof_handler.locally_owned_dofs();
668 *   owned_partitioning.resize(2);
669 *   owned_partitioning[0] = locally_owned_dofs.get_view(0, n_u);
670 *   owned_partitioning[1] = locally_owned_dofs.get_view(n_u, n_u + n_p);
671 *  
672 *   const IndexSet locally_relevant_dofs =
674 *   relevant_partitioning.resize(2);
675 *   relevant_partitioning[0] = locally_relevant_dofs.get_view(0, n_u);
676 *   relevant_partitioning[1] = locally_relevant_dofs.get_view(n_u, n_u + n_p);
677 *  
678 * @endcode
679 *
680 * Setting up the constraints for boundary conditions and hanging nodes
681 * is identical to @ref step_40 "step-40". Even though we don't have any hanging nodes
682 * because we only perform global refinement, it is still a good idea
683 * to put this function call in, in case adaptive refinement gets
684 * introduced later.
685 *
686 * @code
687 *   {
688 *   constraints.reinit(locally_owned_dofs, locally_relevant_dofs);
689 *  
690 *   const FEValuesExtractors::Vector velocities(0);
691 *   DoFTools::make_hanging_node_constraints(dof_handler, constraints);
692 *   VectorTools::interpolate_boundary_values(dof_handler,
693 *   0,
694 *   ExactSolution<dim>(),
695 *   constraints,
696 *   fe.component_mask(velocities));
697 *   constraints.close();
698 *   }
699 *  
700 * @endcode
701 *
702 * Now we create the system matrix based on a BlockDynamicSparsityPattern.
703 * We know that we won't have coupling between different velocity
704 * components (because we use the laplace and not the deformation tensor)
705 * and no coupling between pressure with its test functions, so we use
706 * a Table to communicate this coupling information to
708 *
709 * @code
710 *   {
711 *   system_matrix.clear();
712 *  
713 *   Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1);
714 *   for (unsigned int c = 0; c < dim + 1; ++c)
715 *   for (unsigned int d = 0; d < dim + 1; ++d)
716 *   if (c == dim && d == dim)
717 *   coupling[c][d] = DoFTools::none;
718 *   else if (c == dim || d == dim || c == d)
719 *   coupling[c][d] = DoFTools::always;
720 *   else
721 *   coupling[c][d] = DoFTools::none;
722 *  
723 *   BlockDynamicSparsityPattern dsp(relevant_partitioning);
724 *  
726 *   dof_handler, coupling, dsp, constraints, false);
727 *  
729 *   dsp,
730 *   dof_handler.locally_owned_dofs(),
731 *   mpi_communicator,
732 *   locally_relevant_dofs);
733 *  
734 *   system_matrix.reinit(owned_partitioning, dsp, mpi_communicator);
735 *   }
736 *  
737 * @endcode
738 *
739 * The preconditioner matrix has a different coupling (we only fill in
740 * the 1,1 block with the @ref GlossMassMatrix "mass matrix"), otherwise this code is identical
741 * to the construction of the system_matrix above.
742 *
743 * @code
744 *   {
745 *   preconditioner_matrix.clear();
746 *  
747 *   Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1);
748 *   for (unsigned int c = 0; c < dim + 1; ++c)
749 *   for (unsigned int d = 0; d < dim + 1; ++d)
750 *   if (c == dim && d == dim)
751 *   coupling[c][d] = DoFTools::always;
752 *   else
753 *   coupling[c][d] = DoFTools::none;
754 *  
755 *   BlockDynamicSparsityPattern dsp(relevant_partitioning);
756 *  
758 *   dof_handler, coupling, dsp, constraints, false);
760 *   dsp,
761 *   Utilities::MPI::all_gather(mpi_communicator,
762 *   dof_handler.locally_owned_dofs()),
763 *   mpi_communicator,
764 *   locally_relevant_dofs);
765 *   preconditioner_matrix.reinit(owned_partitioning, dsp, mpi_communicator);
766 *   }
767 *  
768 * @endcode
769 *
770 * Finally, we construct the block vectors with the right sizes. The
771 * function call with two std::vector<IndexSet> will create a ghosted
772 * vector.
773 *
774 * @code
775 *   locally_relevant_solution.reinit(owned_partitioning,
776 *   relevant_partitioning,
777 *   mpi_communicator);
778 *   system_rhs.reinit(owned_partitioning, mpi_communicator);
779 *   }
780 *  
781 *  
782 *  
783 * @endcode
784 *
785 *
786 * <a name="step_55-Assembly"></a>
787 * <h3>Assembly</h3>
788 *
789
790 *
791 * This function assembles the system matrix, the preconditioner matrix,
792 * and the right hand side. The code is pretty standard.
793 *
794 * @code
795 *   template <int dim>
796 *   void StokesProblem<dim>::assemble_system()
797 *   {
798 *   TimerOutput::Scope t(computing_timer, "assembly");
799 *  
800 *   system_matrix = 0;
801 *   preconditioner_matrix = 0;
802 *   system_rhs = 0;
803 *  
804 *   const QGauss<dim> quadrature_formula(velocity_degree + 1);
805 *  
806 *   FEValues<dim> fe_values(fe,
807 *   quadrature_formula,
810 *  
811 *   const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
812 *   const unsigned int n_q_points = quadrature_formula.size();
813 *  
814 *   FullMatrix<double> cell_matrix(dofs_per_cell, dofs_per_cell);
815 *   FullMatrix<double> cell_matrix2(dofs_per_cell, dofs_per_cell);
816 *   Vector<double> cell_rhs(dofs_per_cell);
817 *  
818 *   const RightHandSide<dim> right_hand_side;
819 *   std::vector<Vector<double>> rhs_values(n_q_points, Vector<double>(dim + 1));
820 *  
821 *   std::vector<Tensor<2, dim>> grad_phi_u(dofs_per_cell);
822 *   std::vector<double> div_phi_u(dofs_per_cell);
823 *   std::vector<double> phi_p(dofs_per_cell);
824 *  
825 *   std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
826 *   const FEValuesExtractors::Vector velocities(0);
827 *   const FEValuesExtractors::Scalar pressure(dim);
828 *  
829 *   for (const auto &cell : dof_handler.active_cell_iterators())
830 *   if (cell->is_locally_owned())
831 *   {
832 *   cell_matrix = 0;
833 *   cell_matrix2 = 0;
834 *   cell_rhs = 0;
835 *  
836 *   fe_values.reinit(cell);
837 *   right_hand_side.vector_value_list(fe_values.get_quadrature_points(),
838 *   rhs_values);
839 *   for (unsigned int q = 0; q < n_q_points; ++q)
840 *   {
841 *   for (unsigned int k = 0; k < dofs_per_cell; ++k)
842 *   {
843 *   grad_phi_u[k] = fe_values[velocities].gradient(k, q);
844 *   div_phi_u[k] = fe_values[velocities].divergence(k, q);
845 *   phi_p[k] = fe_values[pressure].value(k, q);
846 *   }
847 *  
848 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
849 *   {
850 *   for (unsigned int j = 0; j < dofs_per_cell; ++j)
851 *   {
852 *   cell_matrix(i, j) +=
853 *   (viscosity *
854 *   scalar_product(grad_phi_u[i], grad_phi_u[j]) -
855 *   div_phi_u[i] * phi_p[j] - phi_p[i] * div_phi_u[j]) *
856 *   fe_values.JxW(q);
857 *  
858 *   cell_matrix2(i, j) += 1.0 / viscosity * phi_p[i] *
859 *   phi_p[j] * fe_values.JxW(q);
860 *   }
861 *  
862 *   const unsigned int component_i =
863 *   fe.system_to_component_index(i).first;
864 *   cell_rhs(i) += fe_values.shape_value(i, q) *
865 *   rhs_values[q](component_i) * fe_values.JxW(q);
866 *   }
867 *   }
868 *  
869 *  
870 *   cell->get_dof_indices(local_dof_indices);
871 *   constraints.distribute_local_to_global(cell_matrix,
872 *   cell_rhs,
873 *   local_dof_indices,
874 *   system_matrix,
875 *   system_rhs);
876 *  
877 *   constraints.distribute_local_to_global(cell_matrix2,
878 *   local_dof_indices,
879 *   preconditioner_matrix);
880 *   }
881 *  
882 *   system_matrix.compress(VectorOperation::add);
883 *   preconditioner_matrix.compress(VectorOperation::add);
884 *   system_rhs.compress(VectorOperation::add);
885 *   }
886 *  
887 *  
888 *  
889 * @endcode
890 *
891 *
892 * <a name="step_55-Solving"></a>
893 * <h3>Solving</h3>
894 *
895
896 *
897 * This function solves the linear system with MINRES with a block diagonal
898 * preconditioner and AMG for the two diagonal blocks as described in the
899 * introduction. The preconditioner applies a v cycle to the 0,0 block
900 * and a CG with the mass matrix for the 1,1 block (the Schur complement).
901 *
902 * @code
903 *   template <int dim>
904 *   void StokesProblem<dim>::solve()
905 *   {
906 *   TimerOutput::Scope t(computing_timer, "solve");
907 *  
908 *   LA::MPI::PreconditionAMG prec_A;
909 *   {
910 *   LA::MPI::PreconditionAMG::AdditionalData data;
911 *  
912 *   #ifdef USE_PETSC_LA
913 *   data.symmetric_operator = true;
914 *   #endif
915 *   prec_A.initialize(system_matrix.block(0, 0), data);
916 *   }
917 *  
918 *   LA::MPI::PreconditionAMG prec_S;
919 *   {
920 *   LA::MPI::PreconditionAMG::AdditionalData data;
921 *  
922 *   #ifdef USE_PETSC_LA
923 *   data.symmetric_operator = true;
924 *   #endif
925 *   prec_S.initialize(preconditioner_matrix.block(1, 1), data);
926 *   }
927 *  
928 * @endcode
929 *
930 * The InverseMatrix is used to solve for the mass matrix:
931 *
932 * @code
933 *   using mp_inverse_t = LinearSolvers::InverseMatrix<LA::MPI::SparseMatrix,
934 *   LA::MPI::PreconditionAMG>;
935 *   const mp_inverse_t mp_inverse(preconditioner_matrix.block(1, 1), prec_S);
936 *  
937 * @endcode
938 *
939 * This constructs the block preconditioner based on the preconditioners
940 * for the individual blocks defined above.
941 *
942 * @code
943 *   const LinearSolvers::BlockDiagonalPreconditioner<LA::MPI::PreconditionAMG,
944 *   mp_inverse_t>
945 *   preconditioner(prec_A, mp_inverse);
946 *  
947 * @endcode
948 *
949 * With that, we can finally set up the linear solver and solve the system:
950 *
951 * @code
952 *   SolverControl solver_control(system_matrix.m(),
953 *   1e-10 * system_rhs.l2_norm());
954 *  
955 *   SolverMinRes<LA::MPI::BlockVector> solver(solver_control);
956 *  
957 *   LA::MPI::BlockVector distributed_solution(owned_partitioning,
958 *   mpi_communicator);
959 *  
960 *   constraints.set_zero(distributed_solution);
961 *  
962 *   solver.solve(system_matrix,
963 *   distributed_solution,
964 *   system_rhs,
965 *   preconditioner);
966 *  
967 *   pcout << " Solved in " << solver_control.last_step() << " iterations."
968 *   << std::endl;
969 *  
970 *   constraints.distribute(distributed_solution);
971 *  
972 * @endcode
973 *
974 * Like in @ref step_56 "step-56", we subtract the mean pressure to allow error
975 * computations against our reference solution, which has a mean value
976 * of zero.
977 *
978 * @code
979 *   locally_relevant_solution = distributed_solution;
980 *   const double mean_pressure =
981 *   VectorTools::compute_mean_value(dof_handler,
982 *   QGauss<dim>(velocity_degree + 2),
983 *   locally_relevant_solution,
984 *   dim);
985 *   distributed_solution.block(1).add(-mean_pressure);
986 *   locally_relevant_solution.block(1) = distributed_solution.block(1);
987 *   }
988 *  
989 *  
990 *  
991 * @endcode
992 *
993 *
994 * <a name="step_55-Therest"></a>
995 * <h3>The rest</h3>
996 *
997
998 *
999 * The remainder of the code that deals with mesh refinement, output, and
1000 * the main loop is pretty standard.
1001 *
1002 * @code
1003 *   template <int dim>
1004 *   void StokesProblem<dim>::refine_grid()
1005 *   {
1006 *   TimerOutput::Scope t(computing_timer, "refine");
1007 *  
1008 *   triangulation.refine_global();
1009 *   }
1010 *  
1011 *  
1012 *  
1013 *   template <int dim>
1014 *   void StokesProblem<dim>::output_results(const unsigned int cycle) const
1015 *   {
1016 *   {
1017 *   const ComponentSelectFunction<dim> pressure_mask(dim, dim + 1);
1018 *   const ComponentSelectFunction<dim> velocity_mask(std::make_pair(0, dim),
1019 *   dim + 1);
1020 *  
1021 *   Vector<double> cellwise_errors(triangulation.n_active_cells());
1022 *   QGauss<dim> quadrature(velocity_degree + 2);
1023 *  
1024 *   VectorTools::integrate_difference(dof_handler,
1025 *   locally_relevant_solution,
1026 *   ExactSolution<dim>(),
1027 *   cellwise_errors,
1028 *   quadrature,
1030 *   &velocity_mask);
1031 *  
1032 *   const double error_u_l2 =
1034 *   cellwise_errors,
1036 *  
1037 *   VectorTools::integrate_difference(dof_handler,
1038 *   locally_relevant_solution,
1039 *   ExactSolution<dim>(),
1040 *   cellwise_errors,
1041 *   quadrature,
1043 *   &pressure_mask);
1044 *  
1045 *   const double error_p_l2 =
1047 *   cellwise_errors,
1049 *  
1050 *   pcout << "error: u_0: " << error_u_l2 << " p_0: " << error_p_l2
1051 *   << std::endl;
1052 *   }
1053 *  
1054 *  
1055 *   std::vector<std::string> solution_names(dim, "velocity");
1056 *   solution_names.emplace_back("pressure");
1057 *   std::vector<DataComponentInterpretation::DataComponentInterpretation>
1058 *   data_component_interpretation(
1060 *   data_component_interpretation.push_back(
1062 *  
1063 *   DataOut<dim> data_out;
1064 *   data_out.attach_dof_handler(dof_handler);
1065 *   data_out.add_data_vector(locally_relevant_solution,
1066 *   solution_names,
1068 *   data_component_interpretation);
1069 *  
1070 *   LA::MPI::BlockVector interpolated;
1071 *   interpolated.reinit(owned_partitioning, MPI_COMM_WORLD);
1072 *   VectorTools::interpolate(dof_handler, ExactSolution<dim>(), interpolated);
1073 *  
1074 *   LA::MPI::BlockVector interpolated_relevant(owned_partitioning,
1075 *   relevant_partitioning,
1076 *   MPI_COMM_WORLD);
1077 *   interpolated_relevant = interpolated;
1078 *   {
1079 *   std::vector<std::string> solution_names(dim, "ref_u");
1080 *   solution_names.emplace_back("ref_p");
1081 *   data_out.add_data_vector(interpolated_relevant,
1082 *   solution_names,
1084 *   data_component_interpretation);
1085 *   }
1086 *  
1087 *  
1088 *   Vector<float> subdomain(triangulation.n_active_cells());
1089 *   for (unsigned int i = 0; i < subdomain.size(); ++i)
1090 *   subdomain(i) = triangulation.locally_owned_subdomain();
1091 *   data_out.add_data_vector(subdomain, "subdomain");
1092 *  
1093 *   data_out.build_patches();
1094 *  
1095 *   data_out.write_vtu_with_pvtu_record(
1096 *   "./", "solution", cycle, mpi_communicator, 2);
1097 *   }
1098 *  
1099 *  
1100 *  
1101 *   template <int dim>
1102 *   void StokesProblem<dim>::run()
1103 *   {
1104 *   #ifdef USE_PETSC_LA
1105 *   pcout << "Running using PETSc." << std::endl;
1106 *   #else
1107 *   pcout << "Running using Trilinos." << std::endl;
1108 *   #endif
1109 *   const unsigned int n_cycles = 5;
1110 *   for (unsigned int cycle = 0; cycle < n_cycles; ++cycle)
1111 *   {
1112 *   pcout << "Cycle " << cycle << ':' << std::endl;
1113 *  
1114 *   if (cycle == 0)
1115 *   make_grid();
1116 *   else
1117 *   refine_grid();
1118 *  
1119 *   setup_system();
1120 *  
1121 *   assemble_system();
1122 *   solve();
1123 *  
1124 *   if (Utilities::MPI::n_mpi_processes(mpi_communicator) <= 32)
1125 *   {
1126 *   TimerOutput::Scope t(computing_timer, "output");
1127 *   output_results(cycle);
1128 *   }
1129 *  
1130 *   computing_timer.print_summary();
1131 *   computing_timer.reset();
1132 *  
1133 *   pcout << std::endl;
1134 *   }
1135 *   }
1136 *   } // namespace Step55
1137 *  
1138 *  
1139 *  
1140 *   int main(int argc, char *argv[])
1141 *   {
1142 *   try
1143 *   {
1144 *   using namespace dealii;
1145 *   using namespace Step55;
1146 *  
1147 *   Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1);
1148 *  
1149 *   StokesProblem<2> problem(2);
1150 *   problem.run();
1151 *   }
1152 *   catch (std::exception &exc)
1153 *   {
1154 *   std::cerr << std::endl
1155 *   << std::endl
1156 *   << "----------------------------------------------------"
1157 *   << std::endl;
1158 *   std::cerr << "Exception on processing: " << std::endl
1159 *   << exc.what() << std::endl
1160 *   << "Aborting!" << std::endl
1161 *   << "----------------------------------------------------"
1162 *   << std::endl;
1163 *  
1164 *   return 1;
1165 *   }
1166 *   catch (...)
1167 *   {
1168 *   std::cerr << std::endl
1169 *   << std::endl
1170 *   << "----------------------------------------------------"
1171 *   << std::endl;
1172 *   std::cerr << "Unknown exception!" << std::endl
1173 *   << "Aborting!" << std::endl
1174 *   << "----------------------------------------------------"
1175 *   << std::endl;
1176 *   return 1;
1177 *   }
1178 *  
1179 *   return 0;
1180 *   }
1181 * @endcode
1182<a name="step_55-Results"></a><h1>Results</h1>
1183
1184
1185As expected from the discussion above, the number of iterations is independent
1186of the number of processors and only very slightly dependent on @f$h@f$:
1187
1188<table>
1189<tr>
1190 <th colspan="2">PETSc</th>
1191 <th colspan="8">number of processors</th>
1192</tr>
1193<tr>
1194 <th>cycle</th>
1195 <th>dofs</th>
1196 <th>1</th>
1197 <th>2</th>
1198 <th>4</th>
1199 <th>8</th>
1200 <th>16</th>
1201 <th>32</th>
1202 <th>64</th>
1203 <th>128</th>
1204</tr>
1205<tr>
1206 <td>0</td>
1207 <td>659</td>
1208 <td>49</td>
1209 <td>49</td>
1210 <td>49</td>
1211 <td>51</td>
1212 <td>51</td>
1213 <td>51</td>
1214 <td>49</td>
1215 <td>49</td>
1216</tr>
1217<tr>
1218 <td>1</td>
1219 <td>2467</td>
1220 <td>52</td>
1221 <td>52</td>
1222 <td>52</td>
1223 <td>52</td>
1224 <td>52</td>
1225 <td>54</td>
1226 <td>54</td>
1227 <td>53</td>
1228</tr>
1229<tr>
1230 <td>2</td>
1231 <td>9539</td>
1232 <td>56</td>
1233 <td>56</td>
1234 <td>56</td>
1235 <td>54</td>
1236 <td>56</td>
1237 <td>56</td>
1238 <td>54</td>
1239 <td>56</td>
1240</tr>
1241<tr>
1242 <td>3</td>
1243 <td>37507</td>
1244 <td>57</td>
1245 <td>57</td>
1246 <td>57</td>
1247 <td>57</td>
1248 <td>57</td>
1249 <td>56</td>
1250 <td>57</td>
1251 <td>56</td>
1252</tr>
1253<tr>
1254 <td>4</td>
1255 <td>148739</td>
1256 <td>58</td>
1257 <td>59</td>
1258 <td>57</td>
1259 <td>59</td>
1260 <td>57</td>
1261 <td>57</td>
1262 <td>57</td>
1263 <td>57</td>
1264</tr>
1265<tr>
1266 <td>5</td>
1267 <td>592387</td>
1268 <td>60</td>
1269 <td>60</td>
1270 <td>59</td>
1271 <td>59</td>
1272 <td>59</td>
1273 <td>59</td>
1274 <td>59</td>
1275 <td>59</td>
1276</tr>
1277<tr>
1278 <td>6</td>
1279 <td>2364419</td>
1280 <td>62</td>
1281 <td>62</td>
1282 <td>61</td>
1283 <td>61</td>
1284 <td>61</td>
1285 <td>61</td>
1286 <td>61</td>
1287 <td>61</td>
1288</tr>
1289</table>
1290
1291<table>
1292<tr>
1293 <th colspan="2">Trilinos</th>
1294 <th colspan="8">number of processors</th>
1295</tr>
1296<tr>
1297 <th>cycle</th>
1298 <th>dofs</th>
1299 <th>1</th>
1300 <th>2</th>
1301 <th>4</th>
1302 <th>8</th>
1303 <th>16</th>
1304 <th>32</th>
1305 <th>64</th>
1306 <th>128</th>
1307</tr>
1308<tr>
1309 <td>0</td>
1310 <td>659</td>
1311 <td>37</td>
1312 <td>37</td>
1313 <td>37</td>
1314 <td>37</td>
1315 <td>37</td>
1316 <td>37</td>
1317 <td>37</td>
1318 <td>37</td>
1319</tr>
1320<tr>
1321 <td>1</td>
1322 <td>2467</td>
1323 <td>92</td>
1324 <td>89</td>
1325 <td>89</td>
1326 <td>82</td>
1327 <td>86</td>
1328 <td>81</td>
1329 <td>78</td>
1330 <td>78</td>
1331</tr>
1332<tr>
1333 <td>2</td>
1334 <td>9539</td>
1335 <td>102</td>
1336 <td>99</td>
1337 <td>96</td>
1338 <td>95</td>
1339 <td>95</td>
1340 <td>88</td>
1341 <td>83</td>
1342 <td>95</td>
1343</tr>
1344<tr>
1345 <td>3</td>
1346 <td>37507</td>
1347 <td>107</td>
1348 <td>105</td>
1349 <td>104</td>
1350 <td>99</td>
1351 <td>100</td>
1352 <td>96</td>
1353 <td>96</td>
1354 <td>90</td>
1355</tr>
1356<tr>
1357 <td>4</td>
1358 <td>148739</td>
1359 <td>112</td>
1360 <td>112</td>
1361 <td>111</td>
1362 <td>111</td>
1363 <td>127</td>
1364 <td>126</td>
1365 <td>115</td>
1366 <td>117</td>
1367</tr>
1368<tr>
1369 <td>5</td>
1370 <td>592387</td>
1371 <td>116</td>
1372 <td>115</td>
1373 <td>114</td>
1374 <td>112</td>
1375 <td>118</td>
1376 <td>120</td>
1377 <td>131</td>
1378 <td>130</td>
1379</tr>
1380<tr>
1381 <td>6</td>
1382 <td>2364419</td>
1383 <td>130</td>
1384 <td>126</td>
1385 <td>120</td>
1386 <td>120</td>
1387 <td>121</td>
1388 <td>122</td>
1389 <td>121</td>
1390 <td>123</td>
1391</tr>
1392</table>
1393
1394While the PETSc results show a constant number of iterations, the iterations
1395increase when using Trilinos. This is likely because of the different settings
1396used for the AMG preconditioner. For performance reasons we do not allow
1397coarsening below a couple thousand unknowns. As the coarse solver is an exact
1398solve (we are using LU by default), a change in number of levels will
1399influence the quality of a V-cycle. Therefore, a V-cycle is closer to an exact
1400solver for smaller problem sizes.
1401
1402<a name="step-55-extensions"></a>
1403<a name="step_55-Possibilitiesforextensions"></a><h3>Possibilities for extensions</h3>
1404
1405
1406<a name="step_55-InvestigateTrilinositerations"></a><h4>Investigate Trilinos iterations</h4>
1407
1408
1409Play with the smoothers, smoothing steps, and other properties for the
1410Trilinos AMG to achieve an optimal preconditioner.
1411
1412<a name="step_55-SolvetheOseenprobleminsteadoftheStokessystem"></a><h4>Solve the Oseen problem instead of the Stokes system</h4>
1413
1414
1415This change requires changing the outer solver to GMRES or BiCGStab, because
1416the system is no longer symmetric.
1417
1418You can prescribe the exact flow solution as @f$b@f$ in the convective term @f$b
1419\cdot \nabla u@f$. This should give the same solution as the original problem,
1420if you set the right hand side to zero.
1421
1422<a name="step_55-Adaptiverefinement"></a><h4>Adaptive refinement</h4>
1423
1424
1425So far, this tutorial program refines the mesh globally in each step.
1426Replacing the code in StokesProblem::refine_grid() by something like
1427@code
1428Vector<float> estimated_error_per_cell(triangulation.n_active_cells());
1429
1430FEValuesExtractors::Vector velocities(0);
1431KellyErrorEstimator<dim>::estimate(
1432 dof_handler,
1433 QGauss<dim - 1>(fe.degree + 1),
1434 std::map<types::boundary_id, const Function<dim> *>(),
1435 locally_relevant_solution,
1436 estimated_error_per_cell,
1437 fe.component_mask(velocities));
1438parallel::distributed::GridRefinement::refine_and_coarsen_fixed_number(
1439 triangulation, estimated_error_per_cell, 0.3, 0.0);
1440triangulation.execute_coarsening_and_refinement();
1441@endcode
1442makes it simple to explore adaptive mesh refinement.
1443 *
1444 *
1445<a name="step_55-PlainProg"></a>
1446<h1> The plain program</h1>
1447@include "step-55.cc"
1448*/
void attach_dof_handler(const DoFHandler< dim, spacedim > &)
Definition fe_q.h:550
virtual void vector_value(const Point< dim > &p, Vector< RangeNumberType > &values) const
IndexSet get_view(const size_type begin, const size_type end) const
Definition index_set.cc:270
Definition point.h:111
@ wall_times
Definition timer.h:651
__global__ void set(Number *val, const Number s, const size_type N)
#define Assert(cond, exc)
void loop(IteratorType begin, std_cxx20::type_identity_t< IteratorType > end, DOFINFO &dinfo, INFOBOX &info, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &cell_worker, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &boundary_worker, const std::function< void(DOFINFO &, DOFINFO &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, AssemblerType &assembler, const LoopControl &lctrl=LoopControl())
Definition loop.h:442
void make_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternBase &sparsity_pattern, const AffineConstraints< number > &constraints={}, const bool keep_constrained_dofs=true, const types::subdomain_id subdomain_id=numbers::invalid_subdomain_id)
@ update_values
Shape function values.
@ update_JxW_values
Transformed quadrature weights.
@ update_gradients
Shape function gradients.
@ update_quadrature_points
Transformed quadrature points.
std::vector< value_type > split(const typename ::Triangulation< dim, spacedim >::cell_iterator &parent, const value_type parent_value)
void component_wise(DoFHandler< dim, spacedim > &dof_handler, const std::vector< unsigned int > &target_component=std::vector< unsigned int >())
IndexSet extract_locally_relevant_dofs(const DoFHandler< dim, spacedim > &dof_handler)
std::vector< types::global_dof_index > count_dofs_per_fe_block(const DoFHandler< dim, spacedim > &dof, const std::vector< unsigned int > &target_block=std::vector< unsigned int >())
void hyper_cube(Triangulation< dim, spacedim > &tria, const double left=0., const double right=1., const bool colorize=false)
spacedim const Point< spacedim > & p
Definition grid_tools.h:990
const std::vector< bool > & used
const Triangulation< dim, spacedim > & tria
spacedim & mesh
Definition grid_tools.h:989
if(marked_vertices.size() !=0) for(auto it
for(unsigned int j=best_vertex+1;j< vertices.size();++j) if(vertices_to_use[j])
@ matrix
Contents is actually a matrix.
@ symmetric
Matrix is symmetric.
@ diagonal
Matrix is diagonal.
void cell_matrix(FullMatrix< double > &M, const FEValuesBase< dim > &fe, const FEValuesBase< dim > &fetest, const ArrayView< const std::vector< double > > &velocity, const double factor=1.)
Definition advection.h:74
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
void distribute_sparsity_pattern(DynamicSparsityPattern &dsp, const IndexSet &locally_owned_rows, const MPI_Comm mpi_comm, const IndexSet &locally_relevant_rows)
std::vector< unsigned int > serial(const std::vector< unsigned int > &targets, const std::function< RequestType(const unsigned int)> &create_request, const std::function< AnswerType(const unsigned int, const RequestType &)> &answer_request, const std::function< void(const unsigned int, const AnswerType &)> &process_answer, const MPI_Comm comm)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
Definition mpi.cc:92
std::vector< T > all_gather(const MPI_Comm comm, const T &object_to_send)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
Definition mpi.cc:107
double compute_global_error(const Triangulation< dim, spacedim > &tria, const InVector &cellwise_error, const NormType &norm, const double exponent=2.)
void interpolate(const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof, const Function< spacedim, typename VectorType::value_type > &function, VectorType &vec, const ComponentMask &component_mask={})
void integrate_difference(const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof, const ReadVector< Number > &fe_function, const Function< spacedim, Number > &exact_solution, OutVector &difference, const Quadrature< dim > &q, const NormType &norm, const Function< spacedim, double > *weight=nullptr, const double exponent=2.)
Number compute_mean_value(const hp::MappingCollection< dim, spacedim > &mapping_collection, const DoFHandler< dim, spacedim > &dof, const hp::QCollection< dim > &q_collection, const ReadVector< Number > &v, const unsigned int component)
void run(const Iterator &begin, const std_cxx20::type_identity_t< Iterator > &end, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length, const unsigned int chunk_size)
int(&) functions(const void *v1, const void *v2)
static constexpr double PI
Definition numbers.h:259
const InputIterator OutputIterator const Function & function
Definition parallel.h:168
STL namespace.
::VectorizedArray< Number, width > exp(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > cos(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sin(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
Definition types.h:32
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation