Logbook  (23-11-2026)
Static problems
static_vector_solver_i.hpp
1 /******************************************************************************
2  * Copyright (C) Siarhei Uzunbajakau, 2023.
3  *
4  * This program is free software. You can use, modify, and redistribute it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation, either version 3 or (at your option) any later version.
7  * This program is distributed without any warranty.
8  *
9  * Refer to COPYING.LESSER for more details.
10  ******************************************************************************/
11 
12 #ifndef StaticVectorSolverI_H__
13 #define StaticVectorSolverI_H__
14 
15 #include <deal.II/base/exceptions.h>
16 #include <deal.II/base/tensor.h>
17 #include <deal.II/base/thread_management.h>
18 #include <deal.II/base/timer.h>
19 #include <deal.II/base/types.h>
20 #include <deal.II/base/work_stream.h>
21 
22 #include <deal.II/grid/tria.h>
23 
24 #include <deal.II/dofs/dof_handler.h>
25 #include <deal.II/dofs/dof_renumbering.h>
26 #include <deal.II/dofs/dof_tools.h>
27 
28 #include <deal.II/fe/fe_nedelec.h>
29 #include <deal.II/fe/fe_values.h>
30 
31 #include <deal.II/lac/affine_constraints.h>
32 #include <deal.II/lac/dynamic_sparsity_pattern.h>
33 #include <deal.II/lac/precondition.h>
34 #include <deal.II/lac/solver_cg.h>
35 #include <deal.II/lac/solver_control.h>
36 #include <deal.II/lac/sparse_matrix.h>
37 #include <deal.II/lac/vector.h>
38 
39 #include <deal.II/numerics/data_out.h>
40 #include <deal.II/numerics/vector_tools.h>
41 #include <deal.II/numerics/vector_tools_common.h>
42 #include <deal.II/numerics/vector_tools_project.h>
43 
44 #include <fstream>
45 #include <iomanip>
46 #include <ios>
47 #include <iostream>
48 #include <map>
49 
50 #include "constants.hpp"
51 #include "settings.hpp"
52 #include "static_vector_input.hpp"
53 
54 #define VE scratch_data.ve
55 
56 #define TMR(__name) TimerOutput::Scope timer_section(timer, __name)
57 
58 using namespace dealii;
59 
60 namespace StaticVectorSolver {
317 template<int dim, int stage = 1>
318 class Solver1
319 {
320 public:
321  Solver1() = delete;
322 
352  Solver1(unsigned int p,
353  unsigned int mapping_degree,
354  unsigned int type_of_pde_rhs = 3,
355  double eta_squared = 0.0,
356  std::string fname = "data",
357  const Function<dim>* exact_solution = nullptr,
358  bool print_time_tables = false,
359  bool project_exact_solution = false,
360  bool write_higher_order_cells = false)
361  : fe(p)
362  , mapping_degree(mapping_degree)
363  , type_of_pde_rhs(type_of_pde_rhs)
364  , eta_squared(eta_squared)
365  , fname(fname)
366  , exact_solution(exact_solution)
367  , print_time_tables(print_time_tables)
368  , project_exact_solution(project_exact_solution)
369  , write_higher_order_cells(write_higher_order_cells)
370  {
371  Assert(((dim == 2) || (dim == 3)), ExcInternalError());
372  Assert(p < 5, ExcInternalError());
373  Assert(type_of_pde_rhs < 4, ExcInternalError());
374  }
375 
385  virtual void make_mesh() = 0;
386 
416  virtual void fill_dirichlet_stack() = 0;
417 
421  virtual void solve() = 0;
422 
432  void setup();
433 
437  void assemble();
438 
443 
455 
488  void save() const;
489 
505  void save_matrix_and_rhs_to_csv(std::string fname) const;
506 
511  void clear()
512  {
513  system_matrix.clear();
514  system_rhs.reinit(0);
515  }
516 
520  const Triangulation<dim>& get_tria() const { return triangulation; }
521 
525  const DoFHandler<dim>& get_dof_handler() const { return dof_handler; }
526 
530  const Vector<double>& get_solution() const { return solution; }
531 
535  unsigned int get_n_cells() const
536  {
537  return static_cast<unsigned int>(triangulation.n_active_cells());
538  }
539 
543  unsigned int get_n_dofs() const
544  {
545  return static_cast<unsigned int>(dof_handler.n_dofs());
546  }
547 
551  unsigned int get_rhs_type() const { return type_of_pde_rhs; }
552 
556  double get_L2_norm() const { return L2_norm; }
557 
561  double get_Linfty_norm() const { return Linfty_norm; }
562 
567  unsigned int get_mapping_degree() const { return mapping_degree; }
568 
576  void run()
577  {
578  TimerOutput::OutputFrequency tf =
579  (print_time_tables) ? TimerOutput::summary : TimerOutput::never;
580 
581  TimerOutput timer(std::cout, tf, TimerOutput::cpu_and_wall_times_grouped);
582 
583  {
584  TMR("Make mesh");
585  make_mesh();
586  }
587  {
588  TMR("Fill Dirichlet stack");
589  fill_dirichlet_stack();
590  }
591  {
592  TMR("Setup");
593  setup();
594  }
595  {
596  TMR("Assemble");
597  assemble();
598  }
599  {
600  TMR("Solve");
601  solve();
602  }
603 
604  if (exact_solution) {
605  if (project_exact_solution) {
606  TMR("Project exact solution");
607  project_exact_solution_fcn();
608  }
609 
610  {
611  TMR("Compute error norms");
612  compute_error_norms();
613  }
614  }
615 
616  {
617  TMR("Save");
618  save();
619  }
620  };
621 
622  virtual ~Solver1() = default;
623 
624 protected:
629  std::map<types::boundary_id, const Function<dim>*> dirichlet_stack;
630 
634  Triangulation<dim> triangulation;
635 
639  const FE_Nedelec<dim> fe;
640 
644  DoFHandler<dim> dof_handler;
645 
650  Vector<double> solution;
651 
655  Vector<double> projected_exact_solution;
656 
660  AffineConstraints<double> constraints;
661 
665  SparsityPattern sparsity_pattern;
666 
670  SparseMatrix<double> system_matrix;
671 
675  Vector<double> system_rhs;
676 
680  double L2_norm;
681 
685  double Linfty_norm;
686 
687 private:
688  const unsigned int mapping_degree;
689  const unsigned int type_of_pde_rhs;
690  const double eta_squared;
691  const std::string fname;
692  const Function<dim>* exact_solution;
693  const bool print_time_tables;
694  const bool project_exact_solution;
695  const bool write_higher_order_cells;
696 
697  Vector<float> L2_per_cell;
698  Vector<float> Linfty_per_cell;
699 
700  // ----------------------------------------------------------------------------
701  // These structures and functions are related to the Work Stream algorithm.
702  // See article "WorkStream – A Design Pattern for Multicore-Enabled Finite
703  // Element Computations." by BRUNO TURCKSIN, MARTIN KRONBICHLER,
704  // WOLFGANG BANGERTH for more details.
705  // ----------------------------------------------------------------------------
706  struct AssemblyScratchData
707  {
708  AssemblyScratchData(const FiniteElement<dim>& fe,
709  unsigned int mapping_degree,
710  unsigned int type_of_pde_rhs,
711  double eta_squared);
712 
713  AssemblyScratchData(const AssemblyScratchData& scratch_data);
714 
715  TheCoefficient<dim, stage> the_coefficient;
716  PdeRhs<dim, stage> pde_rhs;
717  Gamma<dim, stage> gamma;
718  RobinRhs<dim, stage> robin_rhs;
719  FreeSurfaceCurrent<dim, stage> free_surface_current;
720 
721  MappingQ<dim> mapping;
723  FEValues<dim> fe_values;
724  FEFaceValues<dim> fe_face_values;
725 
726  const unsigned int dofs_per_cell;
727  const unsigned int n_q_points;
728  const unsigned int n_q_points_face;
729 
730  std::vector<double> the_coefficient_list;
731  std::vector<Tensor<1, dim>> pde_rhs_list;
732  std::vector<Tensor<1, dim>> pde_rhs_list_face;
733  std::vector<double> gamma_list;
734  std::vector<Tensor<1, dim>> robin_rhs_list;
735  std::vector<Tensor<1, dim>> free_surface_current_list;
736 
737  const unsigned int type_of_pde_rhs;
738  const double eta_squared;
739  const FEValuesExtractors::Vector ve;
740  bool do_robin;
741  bool do_K;
742  bool do_T_on_boundary;
743  };
744 
745  struct AssemblyCopyData
746  {
747  FullMatrix<double> cell_matrix;
748  Vector<double> cell_rhs;
749  std::vector<types::global_dof_index> local_dof_indices;
750  };
751 
752  void system_matrix_local(
753  const typename DoFHandler<dim>::active_cell_iterator& cell,
754  AssemblyScratchData& scratch_data,
755  AssemblyCopyData& copy_data);
756 
757  void copy_local_to_global(const AssemblyCopyData& copy_data);
758  //-----------------------------------------------------------------------------
759  //-----------------------------------------------------------------------------
760  //-----------------------------------------------------------------------------
761 };
762 
763 template<int dim, int stage>
764 void
766 {
767  dof_handler.reinit(triangulation);
768  dof_handler.distribute_dofs(fe);
769 
770  constraints.clear();
771  DoFTools::make_hanging_node_constraints(dof_handler, constraints);
772 
773 #pragma GCC diagnostic push
774 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
775  for (auto item : dirichlet_stack) {
776  Assert(item.first % 2 == 1, ExcInternalError());
777  }
778 #pragma GCC diagnostic pop
779 
780  for (auto item : dirichlet_stack)
781  VectorTools::project_boundary_values_curl_conforming_l2(
782  dof_handler,
783  0, // first vector component
784  *item.second, // boundary function
785  item.first, // boundary id
786  constraints, // constraints
787  MappingQ<dim>(mapping_degree));
788 
789  constraints.close();
790 
791  DynamicSparsityPattern dsp(dof_handler.n_dofs(), dof_handler.n_dofs());
792  DoFTools::make_sparsity_pattern(dof_handler, dsp, constraints, false);
793 
794  sparsity_pattern.copy_from(dsp);
795  system_matrix.reinit(sparsity_pattern);
796  solution.reinit(dof_handler.n_dofs());
797  system_rhs.reinit(dof_handler.n_dofs());
798 
799  if (project_exact_solution)
800  projected_exact_solution.reinit(dof_handler.n_dofs());
801 
802  if (exact_solution) {
803  L2_per_cell.reinit(triangulation.n_active_cells());
804  Linfty_per_cell.reinit(triangulation.n_active_cells());
805  }
806 }
807 
808 template<int dim, int stage>
809 void
811 {
812  WorkStream::run(
813  dof_handler.begin_active(),
814  dof_handler.end(),
815  *this,
816  &Solver1::system_matrix_local,
817  &Solver1::copy_local_to_global,
818  AssemblyScratchData(fe, mapping_degree, type_of_pde_rhs, eta_squared),
819  AssemblyCopyData());
820 }
821 
822 template<int dim, int stage>
824  const FiniteElement<dim>& fe,
825  unsigned int mapping_degree,
826  unsigned int type_of_pde_rhs,
827  double eta_squared)
828  : mapping(mapping_degree)
829  , qt(fe.degree - 1)
830  , fe_values(mapping,
831  fe,
832  QGauss<dim>(qt.sim()),
833  update_gradients | update_values | update_quadrature_points |
834  update_JxW_values)
835  , fe_face_values(mapping,
836  fe,
837  QGauss<dim - 1>(qt.sim()),
838  update_values | update_normal_vectors |
839  update_quadrature_points | update_JxW_values)
840  , dofs_per_cell(fe_values.dofs_per_cell)
841  , n_q_points(fe_values.get_quadrature().size())
842  , n_q_points_face(fe_face_values.get_quadrature().size())
843  , the_coefficient_list(n_q_points)
844  , pde_rhs_list(n_q_points, Tensor<1, dim>())
845  , pde_rhs_list_face(n_q_points_face, Tensor<1, dim>())
846  , gamma_list(n_q_points_face)
847  , robin_rhs_list(n_q_points_face, Tensor<1, dim>())
848  , free_surface_current_list(n_q_points_face, Tensor<1, dim>())
849  , type_of_pde_rhs(type_of_pde_rhs)
850  , eta_squared(eta_squared)
851  , ve(0)
852 {
853 }
854 
855 template<int dim, int stage>
856 Solver1<dim, stage>::AssemblyScratchData::AssemblyScratchData(
857  const AssemblyScratchData& scratch_data)
858  : mapping(scratch_data.mapping.get_degree())
859  , qt(scratch_data.qt)
860  , fe_values(mapping,
861  scratch_data.fe_values.get_fe(),
862  scratch_data.fe_values.get_quadrature(),
863  update_gradients | update_values | update_quadrature_points |
864  update_JxW_values)
865  , fe_face_values(mapping,
866  scratch_data.fe_face_values.get_fe(),
867  scratch_data.fe_face_values.get_quadrature(),
868  update_values | update_normal_vectors |
869  update_quadrature_points | update_JxW_values)
870  , dofs_per_cell(fe_values.dofs_per_cell)
871  , n_q_points(fe_values.get_quadrature().size())
872  , n_q_points_face(fe_face_values.get_quadrature().size())
873  , the_coefficient_list(n_q_points)
874  , pde_rhs_list(n_q_points, Tensor<1, dim>())
875  , pde_rhs_list_face(n_q_points_face, Tensor<1, dim>())
876  , gamma_list(n_q_points_face)
877  , robin_rhs_list(n_q_points_face, Tensor<1, dim>())
878  , free_surface_current_list(n_q_points_face, Tensor<1, dim>())
879  , type_of_pde_rhs(scratch_data.type_of_pde_rhs)
880  , eta_squared(scratch_data.eta_squared)
881  , ve(0)
882 {
883 }
884 
885 template<int dim, int stage>
886 void
887 Solver1<dim, stage>::system_matrix_local(
888  const typename DoFHandler<dim>::active_cell_iterator& cell,
889  AssemblyScratchData& scratch_data,
890  AssemblyCopyData& copy_data)
891 {
892  // See the following boxes:
893  // (1) Recipe for static vector solver in 3D
894  // (2) Recipe for static vector solver in 2D
895  // (3) Recipe for static vector solver in 3D (current vect. potential)
896 
897  // The comments below refer to these recipes by number, i.e., recipe (1),
898  // recipe (2), and recipe (3).
899 
900  copy_data.cell_matrix.reinit(scratch_data.dofs_per_cell,
901  scratch_data.dofs_per_cell);
902 
903  copy_data.cell_rhs.reinit(scratch_data.dofs_per_cell);
904 
905  copy_data.local_dof_indices.resize(scratch_data.dofs_per_cell);
906 
907  scratch_data.fe_values.reinit(cell);
908 
909  scratch_data.the_coefficient.value_list(
910  scratch_data.fe_values.get_quadrature_points(),
911  cell->material_id(),
912  cell->user_index(),
913  scratch_data.the_coefficient_list);
914 
915  scratch_data.pde_rhs.value_list(
916  scratch_data.fe_values.get_quadrature_points(),
917  cell->material_id(),
918  cell->user_index(),
919  scratch_data.pde_rhs_list);
920 
921  // The curl of a Nedelec shape function is Tensor<1,3> in three dimensions and
922  // Tensor<1,1> in two dimensions. It is somewhat difficult to fit this into
923  // the "dim" class template paradigm of deal.II. Consequently, we have to use
924  // the if (dim==2) and if (dim==3) filters or instantiate this function
925  // template explicitly for two and three dimensions. We choose the former.
926  //
927  // If the current vector potential, T, is used in recipes (1) and (2), it must
928  // be implemented by the same class that implements the right-hand side of the
929  // curl-curl equation. Neglect the fact that the right-hand side is the curl
930  // of the current vector potential, not the current vector potential itself.
931  // So, "pde_rhs" is a misnomer if the current vector potential is used on the
932  // right-hand side of the PDE. The current vector potential must be
933  // Tensor<1,3> in three dimensions. In two dimensions it must fill the first
934  // component of Tensor<1,2>. The second component is ignored:
935  //
936  // Tensor<1,2> a;
937  // a[0] = T;
938  // a[1] = whatever;
939  //
940  // The same holds for the free-current density, J_f, if the right-hand side of
941  // the PDE is the curl of J_f, i.e., recipe (3).
942 
943  for (unsigned int q_index = 0; q_index < scratch_data.n_q_points; ++q_index) {
944  for (unsigned int i = 0; i < scratch_data.dofs_per_cell; ++i) {
945  for (unsigned int j = 0; j < scratch_data.dofs_per_cell; ++j) {
946  copy_data.cell_matrix(i, j) +=
947  ( // Integral I_a1+I_a3 in recipes (1), (2), and (3).
948  (1.0 / scratch_data.the_coefficient_list[q_index]) * // 1 / mu
949  scratch_data.fe_values[VE].curl(i, q_index) * // curl N_i
950  scratch_data.fe_values[VE].curl(j, q_index) // curl N_j
951  + scratch_data.eta_squared * // eta^2
952  scratch_data.fe_values[VE].value(i, q_index) * // N_i
953  scratch_data.fe_values[VE].value(j, q_index) // N_j
954  ) *
955  scratch_data.fe_values.JxW(q_index); // dV (dS in 2D)
956  }
957 
958  switch (scratch_data.type_of_pde_rhs) {
959  case 0:
960  // Integral I_b3 in recipes (1) and (2) with J_f=0.
961  copy_data.cell_rhs(i) = 0.0;
962  break;
963  case 1:
964  // Integral I_b3 in recipes (1) and (2).
965  copy_data.cell_rhs(i) +=
966  scratch_data.pde_rhs_list[q_index] * // J_f
967  scratch_data.fe_values[VE].value(i, q_index) * // N_i
968  scratch_data.fe_values.JxW(q_index); // dV (dS in 2D)
969  break;
970  case 2:
971  case 3:
972 #if DIMENSION__ == 2
973  // Integral I_b3-1 in recipe (2)
974 #if DEAL_II_VERSION_GTE(9, 8, 0)
975  copy_data.cell_rhs(i) +=
976  scratch_data.pde_rhs_list[q_index][0] * // T
977  scratch_data.fe_values[VE].curl(i, q_index) * // curl_s N_i
978  scratch_data.fe_values.JxW(q_index); // dS
979 #else
980  copy_data.cell_rhs(i) +=
981  scratch_data.pde_rhs_list[q_index][0] * // T
982  scratch_data.fe_values[VE].curl(i, q_index)[0] * // curl_s N_i
983  scratch_data.fe_values.JxW(q_index); // dS
984 #endif
985 #endif
986 #if DIMENSION__ == 3
987  // Integral I_b3-1 in recipe (1) and (3).
988  copy_data.cell_rhs(i) +=
989  (scratch_data.pde_rhs_list[q_index] *
990  scratch_data.fe_values[VE].curl(i, q_index)) *
991  scratch_data.fe_values.JxW(q_index);
992  // If recipe (1): T.(curl N_i)dV.
993  // If recipe (3): J_f.(curl N_i)dV.
994  // So, the scratch_data.pde_rhs_list must contain the values of T,
995  // or the values of J_f, depending on the context.
996 #endif
997  break;
998  default:
999  Assert(false, ExcInternalError());
1000  break;
1001  }
1002  }
1003  }
1004 
1005  for (unsigned int f = 0; f < GeometryInfo<dim>::faces_per_cell; ++f) {
1006  scratch_data.do_robin = ((cell->face(f)->at_boundary()) &&
1007  (cell->face(f)->boundary_id() % 2 == 0) &&
1008  (cell->face(f)->boundary_id() != 0));
1009 
1010  scratch_data.do_K =
1011  ((cell->user_index() > 0) && (cell->face(f)->user_index() > 0));
1012 
1013  scratch_data.do_T_on_boundary =
1014  ((cell->face(f)->at_boundary()) && (scratch_data.type_of_pde_rhs == 3));
1015 
1016  Assert(!(scratch_data.do_robin && scratch_data.do_K), ExcInternalError());
1017 
1018  if (scratch_data.do_robin || scratch_data.do_K ||
1019  scratch_data.do_T_on_boundary) {
1020 
1021  scratch_data.fe_face_values.reinit(cell, f);
1022 
1023  if (scratch_data.do_robin) {
1024  scratch_data.gamma.value_list(
1025  scratch_data.fe_face_values.get_quadrature_points(),
1026  scratch_data.fe_face_values.get_normal_vectors(),
1027  cell->face(f)->boundary_id(),
1028  cell->material_id(),
1029  cell->user_index(),
1030  cell->face(f)->user_index(),
1031  scratch_data.gamma_list);
1032 
1033  scratch_data.robin_rhs.value_list(
1034  scratch_data.fe_face_values.get_quadrature_points(),
1035  scratch_data.fe_face_values.get_normal_vectors(),
1036  cell->face(f)->boundary_id(),
1037  cell->material_id(),
1038  cell->user_index(),
1039  cell->face(f)->user_index(),
1040  scratch_data.robin_rhs_list);
1041  }
1042 
1043  if (scratch_data.do_K) {
1044  scratch_data.free_surface_current.value_list(
1045  scratch_data.fe_face_values.get_quadrature_points(),
1046  scratch_data.fe_face_values.get_normal_vectors(),
1047  cell->material_id(),
1048  cell->user_index(),
1049  cell->face(f)->user_index(),
1050  scratch_data.free_surface_current_list);
1051  }
1052 
1053  if (scratch_data.do_T_on_boundary) {
1054  scratch_data.pde_rhs.value_list(
1055  scratch_data.fe_face_values.get_quadrature_points(),
1056  cell->material_id(),
1057  cell->user_index(),
1058  scratch_data.pde_rhs_list_face);
1059  }
1060 
1061  for (unsigned int q_index_face = 0;
1062  q_index_face < scratch_data.n_q_points_face;
1063  ++q_index_face) {
1064  for (unsigned int i = 0; i < scratch_data.dofs_per_cell; ++i) {
1065  if (scratch_data.do_robin) {
1066  for (unsigned int j = 0; j < scratch_data.dofs_per_cell; ++j) {
1067  if (dim == 2) {
1068  // Integral I_a2 in recipe (2).
1069  copy_data.cell_matrix(i, j) +=
1070  scratch_data.gamma_list[q_index_face] * // gamma
1071  (scratch_data.fe_face_values.normal_vector(q_index_face)[0] *
1072  scratch_data.fe_face_values[VE].value(i, q_index_face)[1] -
1073  scratch_data.fe_face_values.normal_vector(q_index_face)[1] *
1074  scratch_data.fe_face_values[VE].value(i,
1075  q_index_face)[0]) *
1076  (scratch_data.fe_face_values.normal_vector(q_index_face)[0] *
1077  scratch_data.fe_face_values[VE].value(j, q_index_face)[1] -
1078  scratch_data.fe_face_values.normal_vector(q_index_face)[1] *
1079  scratch_data.fe_face_values[VE].value(j,
1080  q_index_face)[0]) *
1081  scratch_data.fe_face_values.JxW(q_index_face); // dl
1082  } else if (dim == 3) {
1083  // Integral I_a2 in recipe (1).
1084  copy_data.cell_matrix(i, j) +=
1085  scratch_data.gamma_list[q_index_face] * // gamma
1086  ((scratch_data.fe_face_values.normal_vector(q_index_face)[1] *
1087  scratch_data.fe_face_values[VE].value(i,
1088  q_index_face)[2] -
1089  scratch_data.fe_face_values.normal_vector(q_index_face)[2] *
1090  scratch_data.fe_face_values[VE].value(i,
1091  q_index_face)[1]) *
1092  (scratch_data.fe_face_values.normal_vector(
1093  q_index_face)[1] *
1094  scratch_data.fe_face_values[VE].value(j,
1095  q_index_face)[2] -
1096  scratch_data.fe_face_values.normal_vector(
1097  q_index_face)[2] *
1098  scratch_data.fe_face_values[VE].value(
1099  j, q_index_face)[1]) +
1100  (scratch_data.fe_face_values.normal_vector(q_index_face)[0] *
1101  scratch_data.fe_face_values[VE].value(i,
1102  q_index_face)[2] -
1103  scratch_data.fe_face_values.normal_vector(q_index_face)[2] *
1104  scratch_data.fe_face_values[VE].value(i,
1105  q_index_face)[0]) *
1106  (scratch_data.fe_face_values.normal_vector(
1107  q_index_face)[0] *
1108  scratch_data.fe_face_values[VE].value(j,
1109  q_index_face)[2] -
1110  scratch_data.fe_face_values.normal_vector(
1111  q_index_face)[2] *
1112  scratch_data.fe_face_values[VE].value(
1113  j, q_index_face)[0]) +
1114  (scratch_data.fe_face_values.normal_vector(q_index_face)[0] *
1115  scratch_data.fe_face_values[VE].value(i,
1116  q_index_face)[1] -
1117  scratch_data.fe_face_values.normal_vector(q_index_face)[1] *
1118  scratch_data.fe_face_values[VE].value(i,
1119  q_index_face)[0]) *
1120  (scratch_data.fe_face_values.normal_vector(
1121  q_index_face)[0] *
1122  scratch_data.fe_face_values[VE].value(j,
1123  q_index_face)[1] -
1124  scratch_data.fe_face_values.normal_vector(
1125  q_index_face)[1] *
1126  scratch_data.fe_face_values[VE].value(
1127  j, q_index_face)[0])) *
1128  scratch_data.fe_face_values.JxW(
1129  q_index_face); // (n x N_i) . (n x N_j) dS
1130  } else {
1131  Assert(false, ExcInternalError());
1132  }
1133  }
1134 
1135  // Integral I_b1 in recipes (1), (2), and (3).
1136  copy_data.cell_rhs(i) +=
1137  -scratch_data.robin_rhs_list[q_index_face] * // Q
1138  scratch_data.fe_face_values[VE].value(i, q_index_face) * // N_i
1139  scratch_data.fe_face_values.JxW(q_index_face); // dS (dl in 2D)
1140  } // if (scratch_data.do_robin)
1141 
1142  if (scratch_data.do_K) {
1143  // Integral I_b2 in recipes (1) and (2).
1144  copy_data.cell_rhs(i) +=
1145  scratch_data.free_surface_current_list[q_index_face] * // K_f
1146  scratch_data.fe_face_values[VE].value(i, q_index_face) * // N_i
1147  scratch_data.fe_face_values.JxW(q_index_face); // dS (dl in 2D)
1148  }
1149 
1150  if (scratch_data.do_T_on_boundary) {
1151  if (dim == 2) {
1152  // Integral I_b3-2 in recipe (2).
1153  copy_data.cell_rhs(i) -=
1154  scratch_data.pde_rhs_list_face[q_index_face][0] * // T
1155  (scratch_data.fe_face_values.normal_vector(q_index_face)[0] *
1156  scratch_data.fe_face_values[VE].value(i, q_index_face)[1] -
1157  scratch_data.fe_face_values.normal_vector(q_index_face)[1] *
1158  scratch_data.fe_face_values[VE].value(i,
1159  q_index_face)[0] // S
1160  ) * // n x N_i
1161  scratch_data.fe_face_values.JxW(q_index_face); // dl
1162  } else if (dim == 3) {
1163  // Integral I_b3-2 in recipes (1) and (3).
1164  copy_data.cell_rhs(i) -=
1165  (scratch_data.pde_rhs_list_face[q_index_face][0] *
1166  (scratch_data.fe_face_values.normal_vector(q_index_face)[1] *
1167  scratch_data.fe_face_values[VE].value(i,
1168  q_index_face)[2] -
1169  scratch_data.fe_face_values.normal_vector(q_index_face)[2] *
1170  scratch_data.fe_face_values[VE].value(i,
1171  q_index_face)[1]) -
1172  scratch_data.pde_rhs_list_face[q_index_face][1] *
1173  (scratch_data.fe_face_values.normal_vector(q_index_face)[0] *
1174  scratch_data.fe_face_values[VE].value(i,
1175  q_index_face)[2] -
1176  scratch_data.fe_face_values.normal_vector(q_index_face)[2] *
1177  scratch_data.fe_face_values[VE].value(i,
1178  q_index_face)[0]) +
1179  scratch_data.pde_rhs_list_face[q_index_face][2] *
1180  (scratch_data.fe_face_values.normal_vector(q_index_face)[0] *
1181  scratch_data.fe_face_values[VE].value(i,
1182  q_index_face)[1] -
1183  scratch_data.fe_face_values.normal_vector(q_index_face)[1] *
1184  scratch_data.fe_face_values[VE].value(i,
1185  q_index_face)[0])) *
1186  scratch_data.fe_face_values.JxW(
1187  q_index_face); // If recipe (1): T.(n x N_i)dS.
1188  } // If recipe (3): J_f.(n x N_i)dS.
1189  // So, the scratch_data.pde_rhs_list_face must contain the values
1190  // of T or the values of J_f, depending on the context.
1191  else {
1192  Assert(false, ExcInternalError());
1193  }
1194  }
1195  }
1196  }
1197  }
1198  }
1199  cell->get_dof_indices(copy_data.local_dof_indices);
1200 }
1201 
1202 template<int dim, int stage>
1203 void
1204 Solver1<dim, stage>::copy_local_to_global(const AssemblyCopyData& copy_data)
1205 {
1206  constraints.distribute_local_to_global(copy_data.cell_matrix,
1207  copy_data.cell_rhs,
1208  copy_data.local_dof_indices,
1209  system_matrix,
1210  system_rhs);
1211 }
1212 
1213 template<int dim, int stage>
1214 void
1216 {
1217  Weight<dim, stage> weight;
1218  const Function<dim, double>* mask = &weight;
1219 
1220  Constants::QuadratureTableVector<dim> qt(dof_handler.get_fe().degree - 1);
1221  QGauss<dim> quadrature(qt.enorm());
1222 
1223  VectorTools::integrate_difference(MappingQ<dim>(mapping_degree),
1224  dof_handler,
1225  solution,
1226  *exact_solution,
1227  L2_per_cell,
1228  quadrature,
1229  VectorTools::L2_norm,
1230  mask);
1231 
1232  L2_norm = VectorTools::compute_global_error(
1233  triangulation, L2_per_cell, VectorTools::L2_norm);
1234 
1235  VectorTools::integrate_difference(MappingQ<dim>(mapping_degree),
1236  dof_handler,
1237  solution,
1238  *exact_solution,
1239  Linfty_per_cell,
1240  QGauss<dim>(1),
1241  VectorTools::Linfty_norm,
1242  mask);
1243 
1244  Linfty_norm = Linfty_per_cell.linfty_norm();
1245 }
1246 
1247 template<int dim, int stage>
1248 void
1250 {
1251  Constants::QuadratureTableVector<dim> qt(fe.degree - 1);
1252 
1253  AffineConstraints<double> constraints_empty;
1254  constraints_empty.close();
1255 
1256  VectorTools::project(MappingQ<dim>(mapping_degree),
1257  dof_handler,
1258  constraints_empty,
1259  QGauss<dim>(qt.sim()),
1260  *exact_solution,
1261  projected_exact_solution);
1262 }
1263 
1264 template<int dim, int stage>
1265 void
1267 {
1268  std::vector<std::string> solution_names(dim, "VectorField");
1269  std::vector<DataComponentInterpretation::DataComponentInterpretation>
1270  interpretation(dim,
1271  DataComponentInterpretation::component_is_part_of_vector);
1272 
1273  DataOut<dim> data_out;
1274 
1275  data_out.add_data_vector(
1276  dof_handler, solution, solution_names, interpretation);
1277 
1278  if (project_exact_solution && exact_solution) {
1279  std::vector<std::string> solution_names_ex(dim, "VectorFieldExact");
1280 
1281  data_out.add_data_vector(
1282  dof_handler, projected_exact_solution, solution_names_ex, interpretation);
1283  }
1284 
1285  if (exact_solution) {
1286  data_out.add_data_vector(L2_per_cell, "L2norm");
1287  data_out.add_data_vector(Linfty_per_cell, "LinftyNorm");
1288  }
1289 
1290  std::ofstream ofs;
1291 
1292  if (write_higher_order_cells) {
1293  DataOutBase::VtkFlags flags;
1294  flags.write_higher_order_cells = true;
1295  data_out.set_flags(flags);
1296 
1297  const MappingQ<dim> mapping(mapping_degree);
1298 
1299  data_out.build_patches(mapping,
1300  fe.degree + 2,
1301  DataOut<dim>::CurvedCellRegion::curved_inner_cells);
1302 
1303  ofs.open(fname + ".vtu");
1304  data_out.write_vtu(ofs);
1305 
1306  } else {
1307 
1308  data_out.build_patches();
1309 
1310  ofs.open(fname + ".vtk");
1311  data_out.write_vtk(ofs);
1312  }
1313 
1314  ofs.close();
1315 }
1316 
1317 template<int dim, int stage>
1318 void
1320 {
1321  std::ofstream ofs_matrix(fname + "_matrix.csv");
1322  std::ofstream ofs_rhs(fname + "_rhs.csv");
1323 
1324  for (unsigned int i = 0; i < system_matrix.m(); ++i) {
1325  ofs_rhs << system_rhs(i);
1326  if (i < (system_matrix.m() - 1))
1327  ofs_rhs << "\n";
1328 
1329  for (unsigned int j = 0; j < system_matrix.n(); ++j) {
1330  ofs_matrix << std::scientific << std::setprecision(16)
1331  << system_matrix.el(i, j);
1332 
1333  if (j < (system_matrix.m() - 1))
1334  ofs_matrix << ", ";
1335  }
1336  if (i < (system_matrix.m() - 1))
1337  ofs_matrix << "\n";
1338  }
1339 
1340  ofs_rhs.close();
1341  ofs_matrix.close();
1342 }
1343 
1344 } // namespace StaticVectorSolver
1345 
1346 #endif
The tables that contain the amount of quadrature points used in vector problems.
Definition: constants.hpp:101
unsigned int enorm() const
Returns the amount of quadrature points used when calculating the error norms.
unsigned int sim() const
Returns the amount of quadrature points used when assembling system if linear equations.
Implements the coefficient on the left-hand side of the Robin boundary condition (iii) of the static...
Implements the source vector field on the right-hand side of the partial differential equation (i) of...
Implements the vector field, , on the right-hand side of the Robin boundary condition (iii) of the st...
Solves static vector boundary value problem.
unsigned int get_n_dofs() const
Returns the total amount of the degrees of freedom.
void assemble()
Assembles the system matrix and the right-hand side vector.
SparsityPattern sparsity_pattern
The sparsity pattern of the system matrix.
void project_exact_solution_fcn()
Projects exact solution.
void compute_error_norms()
Computes error norms.
unsigned int get_mapping_degree() const
Returns degree of the interpolating Lagrange polynomials used for mapping from the reference cell to ...
void save_matrix_and_rhs_to_csv(std::string fname) const
Saves the system matrix and the right-hand side into a csv file.
double get_L2_norm() const
Returns error norm.
double get_Linfty_norm() const
Returns error norm.
const FE_Nedelec< dim > fe
The finite elements.
void save() const
Saves simulation results into a vtk or vtu file.
virtual void fill_dirichlet_stack()=0
Initializes the data member StaticVectorSolver::Solver1::dirichlet_stack.
DoFHandler< dim > dof_handler
The dof handler.
virtual void solve()=0
Solves the system of linear equations.
AffineConstraints< double > constraints
The constraints associated with the Dirichlet boundary conditions.
Vector< double > solution
The solution vector, that is, degrees of freedom yielded by the simulation.
SparseMatrix< double > system_matrix
The system matrix.
Solver1(unsigned int p, unsigned int mapping_degree, unsigned int type_of_pde_rhs=3, double eta_squared=0.0, std::string fname="data", const Function< dim > *exact_solution=nullptr, bool print_time_tables=false, bool project_exact_solution=false, bool write_higher_order_cells=false)
The only constructor.
virtual void make_mesh()=0
Initializes the data member StaticVectorSolver::Solver1::triangulation.
Vector< double > system_rhs
The system right-hand side vector.
Triangulation< dim > triangulation
The mesh.
unsigned int get_n_cells() const
Returns the number of active cells in the mesh.
void setup()
Initializes system matrix and the right-hand side vector.
void run()
Runs the simulation.
std::map< types::boundary_id, const Function< dim > * > dirichlet_stack
A map that contains pairs of boundary IDs and the corresponding Dirichlet boundary conditions....
const DoFHandler< dim > & get_dof_handler() const
Returns a reference to dof handler.
const Vector< double > & get_solution() const
Returns a reference to solution.
const Triangulation< dim > & get_tria() const
Returns a reference to triangulation.
Vector< double > projected_exact_solution
The projected exact solution vector.
void clear()
Releases computer memory associated with system matrix and right-hand side.
unsigned int get_rhs_type() const
Returns the value of type_of_pde_rhs.
Implements the permeability, , in the partial differential equation (i) of the vector boundary value ...
Implements the weight function for calculating the and error norms.