Logbook  (07-04-2025)
Static problems
solver.cpp
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 #include "solver.hpp"
13 
14 void
15 SolverMWR::make_mesh()
16 {
17  GridIn<2> gridin;
18  gridin.attach_triangulation(triangulation);
19 
20  std::ifstream ifs("../../gmsh/data/circle_r" + std::to_string(r) + ".msh");
21  gridin.read_msh(ifs);
22 
23  mark_materials();
24 }
25 
26 void
27 SolverMWR::fill_dirichlet_stack()
28 {
29  Solver<2>::dirichlet_stack = { { bid, &dirichlet } };
30 }
31 
32 void
33 SolverMWR::mark_materials()
34 {
35  Solver<2>::triangulation.reset_all_manifolds();
36 
37  for (auto cell : Solver<2>::triangulation.active_cell_iterators()) {
38  if (cell->center().norm() < a) {
39  cell->set_material_id(mid_1);
40  } else {
41  cell->set_material_id(mid_2);
42  }
43 
44  if (cell->center().norm() > d1)
45  for (unsigned int f = 0; f < GeometryInfo<2>::faces_per_cell; f++) {
46  double dif_norm = 0.0;
47  for (unsigned int v = 1; v < GeometryInfo<2>::vertices_per_face; v++)
48  dif_norm += std::abs(cell->face(f)->vertex(0).norm() -
49  cell->face(f)->vertex(v).norm());
50 
51  if ((dif_norm < eps) && (cell->center().norm()))
52  cell->face(f)->set_all_manifold_ids(1);
53  }
54  }
55 
56  Solver<2>::triangulation.set_manifold(1, sphere);
57 }
58 
59 void
60 SolverMWR::data_slice(std::string fname)
61 {
62  GridGenerator::hyper_cube(triangulation_slice, 0.0 + eps, b - eps);
63  triangulation_slice.refine_global(nr_slice_global_refs);
64 
65  dof_handler_slice.reinit(triangulation_slice);
66  dof_handler_slice.distribute_dofs(fe_slice);
67  solution_slice.reinit(dof_handler_slice.n_dofs());
68 
69  Functions::FEFieldFunction<2> potential(Solver<2>::dof_handler,
71 
72  VectorTools::interpolate(dof_handler_slice, potential, solution_slice);
73 
74  DataOut<1, 2> data_out;
75 
76  data_out.attach_dof_handler(dof_handler_slice);
77  data_out.add_data_vector(solution_slice, "solution_slice");
78  data_out.build_patches();
79 
80  std::ofstream out(fname + "_slice" + ".gpi");
81 
82  data_out.write_gnuplot(out);
83  out.close();
84 }
85 
86 void
87 SolverMWR::solve()
88 {
89  SolverControl control(Solver<2>::system_rhs.size(),
90  1e-8 * Solver<2>::system_rhs.l2_norm(),
91  false,
92  false);
93 
95  control.enable_history_data();
96 
97  GrowingVectorMemory<Vector<double>> memory;
98  SolverCG<Vector<double>> cg(control, memory);
99 
100  PreconditionJacobi<SparseMatrix<double>> preconditioner;
101  preconditioner.initialize(Solver<2>::system_matrix, 1.0);
102 
103  cg.solve(Solver<2>::system_matrix,
106  preconditioner);
107 
109 
110  if (log_cg_convergence) {
111  const std::vector<double> history_data = control.get_history_data();
112 
113  std::ofstream ofs(fname + "_cg_convergence.csv");
114 
115  unsigned int i = 1;
116  for (auto item : history_data) {
117  ofs << i << ", " << item << "\n";
118  i++;
119  }
120  ofs.close();
121  }
122 }
const double b
The radius of the outer boundary of the problem domain.
Definition: settings.hpp:75
const double a
The radius of the wire.
Definition: settings.hpp:70
const double eps
Two values in double format are considered to be equal if the absolute value of their difference is l...
Definition: settings.hpp:103
const types::material_id mid_1
The ID of the material inside the wire.
Definition: settings.hpp:86
const types::boundary_id bid
The ID of the boundary of the problem domain. The boundary ID is set in the geo files that are locate...
Definition: settings.hpp:97
const bool log_cg_convergence
If set to true, saves the residual at each iteration of the CG solver. The names of the files fit the...
Definition: settings.hpp:128
const types::material_id mid_2
The ID of the material outside the wire.
Definition: settings.hpp:91
const double d1
The half-side of the square in the middle of the mesh.
Definition: settings.hpp:59
std::map< types::boundary_id, const Function< dim > * > dirichlet_stack
A map that contains pairs of boundary IDs and the corresponding Dirichlet boundary conditions.
Triangulation< dim > triangulation
The mesh.
DoFHandler< dim > dof_handler
The degrees-of-freedom handler.
Vector< double > system_rhs
The system right-hand side vector.
Vector< double > solution
The solution vector, i.e., degrees of freedom yielded by the simulation.
AffineConstraints< double > constraints
The constraints associated with the Dirichlet boundary conditions.
SparseMatrix< double > system_matrix
The system matrix.