Logbook  (07-04-2025)
Static problems
solver.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 SolverMMS_H__
13 #define SolverMMS_H__
14 
15 #include <deal.II/grid/grid_in.h>
16 #include <deal.II/grid/grid_out.h>
17 #include <deal.II/grid/grid_tools.h>
18 
19 #include "exact_solution.hpp"
20 #include "settings.hpp"
21 #include "static_scalar_solver.hpp"
22 
23 using namespace StaticScalarSolver;
24 
29 template<int dim>
30 class SolverMMS
31  : public SettingsMMS
32  , public Solver<dim>
33 {
34 public:
35  SolverMMS() = delete;
36 
52  SolverMMS(unsigned int p,
53  unsigned int mapping_degree,
54  unsigned int r,
55  std::string fname)
56  : Solver<dim>(p,
57  mapping_degree,
58  1, // The PDE right-hand side is free-current density.
59  fname,
60  &exact_solution,
61  false, // Is axisymmetric.
62  false, // Is vector potential.
63  print_time_tables,
64  project_exact_solution)
65  , fname(fname)
66  {
67  if (DIMENSION__ == 2) {
68  if (HYPERCUBE__ == 1) {
69  fname_mesh = "../../gmsh/data/square_r" + std::to_string(r) + ".msh";
70  } else {
71  fname_mesh = "../../gmsh/data/circle_r" + std::to_string(r) + ".msh";
72  }
73  } else {
74  if (HYPERCUBE__ == 1) {
75  fname_mesh = "../../gmsh/data/cube_r" + std::to_string(r) + ".msh";
76  } else {
77  fname_mesh = "../../gmsh/data/sphere_r" + std::to_string(r) + ".msh";
78  }
79  }
80 
82  }
83 
84  ~SolverMMS() = default;
85 
86 private:
87  const std::string fname;
88 
89  std::string fname_mesh;
90 
91  const ExactSolutionMMS_PHI<dim> exact_solution;
92 
93  virtual void make_mesh() override final;
94  virtual void fill_dirichlet_stack() override final;
95  virtual void solve() override final;
96 };
97 
98 template<int dim>
99 void
100 SolverMMS<dim>::fill_dirichlet_stack()
101 {
102  Solver<dim>::dirichlet_stack = { { bid_dirichlet, &exact_solution } };
103 }
104 
105 template<int dim>
106 void
108 {
109  SolverControl control(Solver<dim>::system_rhs.size(),
110  1e-8 * Solver<dim>::system_rhs.l2_norm(),
111  false,
112  false);
113 
114  if (log_cg_convergence)
115  control.enable_history_data();
116 
117  GrowingVectorMemory<Vector<double>> memory;
118  SolverCG<Vector<double>> cg(control, memory);
119 
120  PreconditionJacobi<SparseMatrix<double>> preconditioner;
121  preconditioner.initialize(Solver<dim>::system_matrix, 1.0);
122 
126  preconditioner);
127 
129 
130  if (log_cg_convergence) {
131  const std::vector<double> history_data = control.get_history_data();
132 
133  std::ofstream ofs(fname + "_cg_convergence.csv");
134 
135  unsigned int i = 1;
136  for (auto item : history_data) {
137  ofs << i << ", " << item << "\n";
138  i++;
139  }
140  ofs.close();
141  }
142 }
143 
144 template<int dim>
145 void
147 {
148  GridIn<dim> gridin;
149  gridin.attach_triangulation(Solver<dim>::triangulation);
150 
151  std::ifstream ifs(fname_mesh);
152  gridin.read_msh(ifs);
153 }
154 #endif
Describes exact solution, , of the Method of manufactured solutions (mms/) numerical experiment in tw...
Global settings for the Method of manufactured solutions (mms/) numerical experiment.
Definition: settings.hpp:25
Implements the solver of the Method of manufactured solutions (mms/) numerical experiment.
Definition: solver.hpp:33
SolverMMS(unsigned int p, unsigned int mapping_degree, unsigned int r, std::string fname)
Definition: solver.hpp:52
Solves static scalar boundary value problem.
void run()
Runs the simulation.