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 SolverCBND_H__
13 #define SolverCBND_H__
14 
15 #define BOOST_ALLOW_DEPRECATED_HEADERS
16 
17 #include <deal.II/base/function.h>
18 #include <deal.II/grid/grid_generator.h>
19 #include <deal.II/grid/grid_in.h>
20 #include <deal.II/grid/manifold_lib.h>
21 
22 #include <deal.II/numerics/fe_field_function.h>
23 
24 #include "exact_solution.hpp"
25 #include "settings.hpp"
26 #include "static_scalar_solver.hpp"
27 
28 #define TMR(__name) TimerOutput::Scope timer_section(timer, __name)
29 
30 using namespace StaticScalarSolver;
31 
36 template<int dim>
38  : public SettingsCBND
39  , public Solver<dim>
40 {
41 public:
42  SolverCBND() = delete;
43 
59  SolverCBND(unsigned int p,
60  unsigned int mapping_degree,
61  unsigned int r,
62  std::string fname)
63  : Solver<dim>(p,
64  mapping_degree,
65  1, // The right-hand side is free-current density.
66  fname,
67  &exact_solution,
68  false, // Is axisymmetric.
69  false, // Is vector potential.
70  print_time_tables,
71  project_exact_solution) // , true)
72  , r(r)
73  , fname(fname)
74  , dirichlet_function_in(1.0)
75  , fe_slice(1)
76  {
77  TimerOutput::OutputFrequency tf =
78  (print_time_tables) ? TimerOutput::summary : TimerOutput::never;
79 
80  TimerOutput timer(std::cout, tf, TimerOutput::cpu_and_wall_times_grouped);
81 
82  {
83  TMR("Solver run");
85  }
86  {
87  TMR("Data slice");
88  data_slice(fname);
89  }
90  }
91 
92  unsigned int n_cells;
93 
94  ~SolverCBND() = default;
95 
96 private:
97  const unsigned int r;
98  const std::string fname;
99  const ExactSolutionCBND_PHI<dim> exact_solution;
100  const dealii::Functions::ZeroFunction<dim> dirichlet_function_out;
101  const dealii::Functions::ConstantFunction<dim> dirichlet_function_in;
102 
103  // The amount of global mesh refinements that need to be done to the
104  // one- dimensional mesh used for the plot of potential vs. \f$x\f$
105  // coordinate.
106  const unsigned int nr_slice_global_refs = 10;
107 
108  // These four data members are needed for making the plot of
109  // potential vs. x coordinate.
110 
111  Triangulation<1, dim> triangulation_slice;
112  FE_Q<1, dim> fe_slice;
113  DoFHandler<1, dim> dof_handler_slice;
114  Vector<double> solution_slice;
115 
116  // SphericalManifold<dim> sphere;
117 
118  virtual void make_mesh() override final;
119  virtual void fill_dirichlet_stack() override final;
120  virtual void solve() override final;
121 
122  // This function makes the plot of potential vs. x coordinate.
123  void data_slice(std::string fname);
124 
125  void attach_manifold_refine();
126 };
127 
128 template<int dim>
129 void
130 SolverCBND<dim>::fill_dirichlet_stack()
131 {
132 #if IS_BC_EXACT__ == 1
133  Solver<dim>::dirichlet_stack = { { bid_in, &exact_solution },
134  { bid_out, &exact_solution } };
135 #endif
136 #if IS_BC_EXACT__ == 0
137  Solver<dim>::dirichlet_stack = { { bid_in, &dirichlet_function_in },
138  { bid_out, &dirichlet_function_out } };
139 #endif
140 }
141 
142 template<int dim>
143 void
144 SolverCBND<dim>::data_slice(std::string fname)
145 {
146 
147  if (dim == 2) {
148  GridGenerator::hyper_cube(triangulation_slice, a + eps, b - eps);
149  } else {
150  GridGenerator::hyper_cube(triangulation_slice, a + eps, b - eps);
151  }
152 
153  triangulation_slice.refine_global(nr_slice_global_refs);
154 
155  dof_handler_slice.reinit(triangulation_slice);
156  dof_handler_slice.distribute_dofs(fe_slice);
157  solution_slice.reinit(dof_handler_slice.n_dofs());
158 
159  Functions::FEFieldFunction<dim> potential(Solver<dim>::dof_handler,
161 
162  VectorTools::interpolate(dof_handler_slice, potential, solution_slice);
163 
164  DataOut<1, dim> data_out;
165 
166  data_out.attach_dof_handler(dof_handler_slice);
167  data_out.add_data_vector(solution_slice, "solution_slice");
168  data_out.build_patches();
169 
170  std::ofstream out(fname + "_slice" + ".gpi");
171 
172  data_out.write_gnuplot(out);
173  out.close();
174 }
175 
176 template<int dim>
177 void
179 {
180  // Solver<dim>::triangulation.set_all_manifold_ids(0);
181  // Solver<dim>::triangulation.set_manifold(0,sphere);
182 
183  // for (unsigned int i = 0; i < 3; ++i)
184  // {
185  // for (auto &cell: Solver<dim>::triangulation.active_cell_iterators())
186  // if (cell->at_boundary())
187  // cell->set_refine_flag();
188 
189  // Solver<dim>::triangulation.execute_coarsening_and_refinement();
190  // Solver<dim>::triangulation.set_all_manifold_ids(0);
191  // Solver<dim>::triangulation.set_manifold(0,sphere);
192  // }
193 }
194 
195 template<int dim>
196 void
198 {
199  SolverControl control(Solver<dim>::system_rhs.size(),
200  1e-8 * Solver<dim>::system_rhs.l2_norm(),
201  false,
202  false);
203 
204  if (log_cg_convergence)
205  control.enable_history_data();
206 
207  GrowingVectorMemory<Vector<double>> memory;
208  SolverCG<Vector<double>> cg(control, memory);
209 
210  PreconditionJacobi<SparseMatrix<double>> preconditioner;
211  preconditioner.initialize(Solver<dim>::system_matrix, 1.0);
212 
216  preconditioner);
217 
219 
220  if (log_cg_convergence) {
221  const std::vector<double> history_data = control.get_history_data();
222 
223  std::ofstream ofs(fname + "_cg_convergence.csv");
224 
225  unsigned int i = 1;
226  for (auto item : history_data) {
227  ofs << i << ", " << item << "\n";
228  i++;
229  }
230  ofs.close();
231  }
232 }
233 
234 #endif
Describes exact solution, , of the Effect of curved boundaries (cbnd/) numerical experiment.
Global settings for the Effect of curved boundaries (cbnd/) numerical experiment.
Definition: settings.hpp:25
Implements the solver of the Effect of curved boundaries (cbnd/) numerical experiment.
Definition: solver.hpp:40
SolverCBND(unsigned int p, unsigned int mapping_degree, unsigned int r, std::string fname)
Definition: solver.hpp:59
Solves static scalar boundary value problem.
void run()
Runs the simulation.