PRISMS-PF Manual v3.0-pre
All Classes Functions Variables Enumerations Pages
linear_solver_identity.h
1// SPDX-FileCopyrightText: © 2025 PRISMS Center at the University of Michigan
2// SPDX-License-Identifier: GNU Lesser General Public Version 2.1
3
4#ifndef linear_solver_identity_h
5#define linear_solver_identity_h
6
7#include <deal.II/lac/precondition.h>
8#include <deal.II/lac/solver_cg.h>
9#include <deal.II/matrix_free/matrix_free.h>
10
11#include <prismspf/config.h>
12#include <prismspf/solvers/linear_solver_base.h>
13
14#ifdef PRISMS_PF_WITH_CALIPER
15# include <caliper/cali.h>
16#endif
17
18PRISMS_PF_BEGIN_NAMESPACE
19
24template <int dim, int degree>
25class identitySolver : public linearSolverBase<dim, degree>
26{
27public:
29 using VectorType = dealii::LinearAlgebra::distributed::Vector<double>;
30
34 identitySolver(const userInputParameters<dim> &_user_inputs,
35 const variableAttributes &_variable_attributes,
36 const matrixfreeHandler<dim> &_matrix_free_handler,
37 const constraintHandler<dim> &_constraint_handler,
38 solutionHandler<dim> &_solution_handler);
39
43 ~identitySolver() override = default;
44
48 void
49 init() override;
50
54 void
55 reinit() override;
56
60 void
61 solve(const double step_length = 1.0) override;
62};
63
64template <int dim, int degree>
66 const userInputParameters<dim> &_user_inputs,
67 const variableAttributes &_variable_attributes,
68 const matrixfreeHandler<dim> &_matrix_free_handler,
69 const constraintHandler<dim> &_constraint_handler,
70 solutionHandler<dim> &_solution_handler)
71 : linearSolverBase<dim, degree>(_user_inputs,
72 _variable_attributes,
73 _matrix_free_handler,
74 _constraint_handler,
75 _solution_handler)
76{}
77
78template <int dim, int degree>
79inline void
81{
82 this->system_matrix->clear();
83 this->system_matrix->initialize(this->matrix_free_handler.get_matrix_free());
84 this->update_system_matrix->clear();
85 this->update_system_matrix->initialize(this->matrix_free_handler.get_matrix_free());
86
87 this->system_matrix->add_global_to_local_mapping(
88 this->residual_global_to_local_solution);
89 this->system_matrix->add_src_solution_subset(this->residual_src);
90
91 this->update_system_matrix->add_global_to_local_mapping(
92 this->newton_update_global_to_local_solution);
93 this->update_system_matrix->add_src_solution_subset(this->newton_update_src);
94
95 // Apply constraints
96 this->constraint_handler.get_constraint(this->field_index)
97 .distribute(*(this->solution_handler.solution_set.at(
98 std::make_pair(this->field_index, dependencyType::NORMAL))));
99}
100
101template <int dim, int degree>
102inline void
105
106template <int dim, int degree>
107inline void
108identitySolver<dim, degree>::solve(const double step_length)
109{
110 auto *solution = this->solution_handler.solution_set.at(
111 std::make_pair(this->field_index, dependencyType::NORMAL));
112
113 // Compute the residual
114 this->system_matrix->compute_residual(*this->residual, *solution);
116 << " field: " << this->field_index
117 << " Initial residual: " << this->residual->l2_norm() << std::flush;
118
119 // Determine the residual tolerance
120 this->compute_solver_tolerance();
121
122 // Update solver controls
123 this->solver_control.set_tolerance(this->tolerance);
124 dealii::SolverCG<VectorType> cg(this->solver_control);
125
126 try
127 {
128 *this->newton_update = 0.0;
129 cg.solve(*(this->update_system_matrix),
130 *this->newton_update,
131 *this->residual,
132 dealii::PreconditionIdentity());
133 }
134 catch (...)
135 {
137 << "Warning: linear solver did not converge as per set tolerances.\n";
138 }
139 this->constraint_handler.get_constraint(this->field_index)
140 .set_zero(*this->newton_update);
141
143 << " Final residual: " << this->solver_control.last_value()
144 << " Steps: " << this->solver_control.last_step() << "\n"
145 << std::flush;
146
147 // Update the solutions
148 (*solution).add(step_length, *this->newton_update);
149 this->solution_handler.update(fieldSolveType::NONEXPLICIT_LINEAR, this->field_index);
150
151 // Apply constraints
152 // This may be redundant with the constraints on the update step.
153 this->constraint_handler.get_constraint(this->field_index).distribute(*solution);
154}
155
156PRISMS_PF_END_NAMESPACE
157
158#endif
static dealii::ConditionalOStream & pout_base()
Generic parallel output stream. Used for essential information in release and debug mode.
Definition conditional_ostreams.cc:31
static dealii::ConditionalOStream & pout_summary()
Log output stream for writing a summary.log file.
Definition conditional_ostreams.cc:22
The class handles the generation and application of boundary conditions based on the user-inputs.
Definition constraint_handler.h:25
Definition explicit_base.h:27
Class that handles the assembly and solving of a field with the identity preconditioner (no precondit...
Definition linear_solver_identity.h:26
void reinit() override
Reinitialize the system.
Definition linear_solver_identity.h:103
void init() override
Initialize the system.
Definition linear_solver_identity.h:80
identitySolver(const userInputParameters< dim > &_user_inputs, const variableAttributes &_variable_attributes, const matrixfreeHandler< dim > &_matrix_free_handler, const constraintHandler< dim > &_constraint_handler, solutionHandler< dim > &_solution_handler)
Constructor.
Definition linear_solver_identity.h:65
void solve(const double step_length=1.0) override
Solve the system Ax=b.
Definition linear_solver_identity.h:108
~identitySolver() override=default
Destructor.
Base class that handles the assembly and linear solving of a field.
Definition linear_solver_base.h:36
This class handlers the management and access of the matrix-free objects.
Definition matrix_free_handler.h:25
Class that manages solution initialization and swapping with old solutions.
Definition solution_handler.h:22
Definition user_input_parameters.h:22
Structure to hold the variable attributes of a field. This includes things like the name,...
Definition variable_attributes.h:39