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 #define BOOST_ALLOW_DEPRECATED_HEADERS
13 
14 #include "solver.hpp"
15 #include <fstream>
16 
17 void
18 SolverSSOLI::make_mesh()
19 {
20  GridIn<3> gridin;
21 
22  gridin.attach_triangulation(Solver1<3>::triangulation);
23  std::ifstream ifs("../../gmsh/data/sphere_r" + std::to_string(r) + ".msh");
24  gridin.read_msh(ifs);
25 
26  mark_materials();
27 }
28 
29 void
30 SolverSSOLI::fill_dirichlet_stack()
31 {
32  dirichlet_stack = {};
33 }
34 
35 void
36 SolverSSOLI::solve()
37 {
38  SolverControl control(1000 * Solver1<3>::system_rhs.size(),
39  1e-6 * Solver1<3>::system_rhs.l2_norm(),
40  false,
41  false);
42 
44  control.enable_history_data();
45 
46  GrowingVectorMemory<Vector<double>> memory;
47  SolverCG<Vector<double>> cg(control, memory);
48 
49  PreconditionSSOR<SparseMatrix<double>> preconditioner;
50  preconditioner.initialize(Solver1<3>::system_matrix, 1.2);
51 
55  preconditioner);
56 
58 
60  const std::vector<double> history_data = control.get_history_data();
61 
62  std::ofstream ofs(fname + "_cg_convergence.csv");
63 
64  unsigned int i = 1;
65  for (auto item : history_data) {
66  ofs << i << ", " << item << "\n";
67  i++;
68  }
69  ofs.close();
70  }
71 }
72 
73 void
74 SolverSSOLI::mark_materials()
75 {
76  Solver1<3>::triangulation.reset_all_manifolds();
77 
78  for (auto cell : Solver1<3>::triangulation.active_cell_iterators()) {
79  for (unsigned int f = 0; f < GeometryInfo<3>::faces_per_cell; ++f) {
80 
81  double dif_norm = 0.0;
82  double dif_norm_a = 0.0;
83 
84  for (unsigned int v = 0; v < GeometryInfo<3>::vertices_per_face; v++) {
85  dif_norm_a += std::abs(cell->face(f)->vertex(v).norm() - a);
86  dif_norm += std::abs(cell->face(f)->vertex(0).norm() -
87  cell->face(f)->vertex(v).norm());
88  }
89 
90  if ((dif_norm < eps) && (cell->center().norm() > rd1))
91  cell->face(f)->set_all_manifold_ids(1);
92 
93  if (dif_norm_a < eps)
94  if (std::abs(cell->center().norm()) < a) {
95  cell->face(f)->set_user_index(1);
96  cell->set_user_index(1);
97  }
98  }
99  }
100 
101  Solver1<3>::triangulation.set_manifold(1, sphere);
102 }
const double a
The radius of the coil.
Definition: settings.hpp:56
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:104
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:79
const double rd1
The radius of the sphere that encloses the cube in the middle of the mesh.
Definition: settings.hpp:51
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.
Vector< double > system_rhs
The system right-hand side vector.
Triangulation< dim > triangulation
The mesh.
std::map< types::boundary_id, const Function< dim > * > dirichlet_stack
A map that contains pairs of boundary IDs and the corresponding Dirichlet boundary conditions....