PRISMS-PF Manual
Loading...
Searching...
No Matches
newton_solver.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2025 PRISMS Center at the University of Michigan
2// SPDX-License-Identifier: GNU Lesser General Public Version 2.1
3
4#pragma once
5
6#include <deal.II/lac/affine_constraints.h>
7#include <deal.II/lac/solver_cg.h>
8
10#include <prismspf/core/types.h>
11
15
16#include <prismspf/config.h>
17
19
20template <unsigned int dim, unsigned int degree, typename number>
21class SolveContext;
22
26template <unsigned int dim, unsigned int degree, typename number>
27class NewtonSolver : public LinearSolver<dim, degree, number>
28{
29protected:
30 using SolverBase<dim, degree, number>::solutions;
31 using SolverBase<dim, degree, number>::solve_context;
32 using SolverBase<dim, degree, number>::solve_block;
33 using LinearSolver<dim, degree, number>::do_linear_solve;
34 using LinearSolver<dim, degree, number>::normalization_value;
35 using LinearSolver<dim, degree, number>::lhs_operator;
36 using LinearSolver<dim, degree, number>::rhs_operator;
37 using LinearSolver<dim, degree, number>::rhs_vector;
38
39public:
45 const SolveContext<dim, degree, number> &_solve_context)
46 : LinearSolver<dim, degree, number>(_solve_block, _solve_context)
47 {}
48
52 void
53 init(const std::list<DependencyMap> &all_dependeny_sets) override
54 {
56 newton_update.reinit(solutions.get_solution_full_vector(0));
57 }
58
62 void
63 reinit() override
64 {
66 newton_update.reinit(solutions.get_solution_full_vector(0));
67 }
68
72 void
73 solve_impl() override
74 {
75 const number newton_step_length = newton_params().step_length;
76 const number newton_tolerance =
78 unsigned int newton_max_iterations = newton_params().max_iterations;
79
80 BlockVector<number> &newton_residual = rhs_vector;
83 // TODO: setup initial guess for solution vector. Maybe use old_solution if
84 // available.
85 if (!solutions.get_solution_level(0).old_solutions.empty())
86 {
87 solutions.get_solution_full_vector(0) =
88 solutions.get_old_solution_full_vector(0, 0);
89 }
90
91 // Newton iteration loop.
92 bool newton_unconverged = true;
93 unsigned int iter = 0;
94 number l2_norm = -1.0;
95 int total_lin_iters = 0;
96 while (newton_unconverged && iter < newton_max_iterations)
97 {
98 // Apply constraints to solution vector
99 solutions.apply_constraints(0);
100 solutions.update_ghosts(0);
101
102 // Solve for Newton-residual (r)
103 Timer::start_section("Zero ghosts");
104 newton_residual.zero_out_ghost_values();
105 Timer::end_section("Zero ghosts");
106 rhs_op.compute_operator(newton_residual);
107 newton_residual.update_ghost_values();
108
109 // Check convergence.
110 l2_norm = newton_residual.l2_norm();
111 newton_unconverged = l2_norm > newton_tolerance;
112 if (!newton_unconverged || iter == newton_max_iterations)
113 {
114 continue;
115 }
116
117 // Solve for Newton update. (-dr/du|Du)
118 total_lin_iters += do_linear_solve(newton_residual, lhs_op, newton_update);
119 newton_update.update_ghost_values();
120
121 // Zero out the ghosts
122 solutions.get_solution_full_vector(0).zero_out_ghost_values();
123
124 // Perform Newton update.
125 solutions.get_solution_full_vector(0).add(newton_step_length, newton_update);
126
127 // Update the ghosts
128 Timer::start_section("Update ghosts");
129 solutions.update_ghosts(0);
130 Timer::end_section("Update ghosts");
131
132 iter++;
133 // Todo: implement some super simple backtracking. Something like if the residual
134 // increases instead of decreasing, try again with a smaller step length.
135 }
136 if (solve_context->get_user_inputs().output_parameters.should_output(
137 solve_context->get_simulation_timer().get_increment()))
138 {
140 << " Newton solve final residual : " << l2_norm / normalization_value()
141 << " Newton steps: " << iter << " Total linear steps: " << total_lin_iters
142 << "\n"
143 << std::flush;
144 }
145 if (iter >= newton_max_iterations)
146 {
148 << "[Increment " << solve_context->get_simulation_timer().get_increment()
149 << "] "
150 << "Warning: Newton solver did not converge before " << newton_max_iterations
151 << " iterations.\n\n";
152 }
153 }
154
155private:
157
158 [[nodiscard]] const NonlinearSolverParameters &
160 {
161 return solve_block.nonlinear_solver_parameters;
162 };
163};
164
165PRISMS_PF_END_NAMESPACE
static dealii::ConditionalOStream & pout_summary()
Log output stream for writing a summary.log file.
Definition conditional_ostreams.cc:35
static dealii::ConditionalOStream & pout_base()
Generic parallel output stream. Used for essential information in release and debug mode.
Definition conditional_ostreams.cc:44
MFOperator< dim, degree, number > rhs_operator
Matrix free operators.
Definition linear_solver.h:210
BlockVector< number > rhs_vector
Definition linear_solver.h:213
double normalization_value()
Definition linear_solver.h:216
int do_linear_solve(BlockVector< number > &b_vector, MFOperator< dim, degree, number > &lhs_matrix, BlockVector< number > &x_vector)
Definition linear_solver.h:164
MFOperator< dim, degree, number > lhs_operator
Definition linear_solver.h:212
void init(const std::list< DependencyMap > &all_dependeny_sets) override
Initialize the solver.
Definition linear_solver.h:74
LinearSolver(SolveBlock _solve_block, const SolveContext< dim, degree, number > &_solve_context)
Constructor.
Definition linear_solver.h:51
void reinit() override
Reinitialize the solver.
Definition linear_solver.h:107
This class exists to evaluate a single user-defined operator for the matrix-free implementation of so...
Definition mf_operator.h:55
void compute_operator(BlockVector< number > &dst, const BlockVector< number > &src=BlockVector< number >()) const
Calls cell_loop on function that calls user-defined operator.
Definition mf_operator.cc:18
void reinit() override
Reinitialize the solver.
Definition newton_solver.h:63
BlockVector< number > newton_update
Definition newton_solver.h:156
void solve_impl() override
Solve for a single update step.
Definition newton_solver.h:73
void init(const std::list< DependencyMap > &all_dependeny_sets) override
Initialize the solver.
Definition newton_solver.h:53
const NonlinearSolverParameters & newton_params() const
Definition newton_solver.h:159
NewtonSolver(SolveBlock _solve_block, const SolveContext< dim, degree, number > &_solve_context)
Constructor.
Definition newton_solver.h:44
Structure to hold the attributes of a solve-block.
Definition solve_block.h:59
This class provides context for a solver with ptrs to all the relevant dependencies.
Definition solve_context.h:34
SolverBase(SolveBlock _solve_block, const SolveContext< dim, degree, number > &_solve_context)
Constructor.
Definition solver_base.h:36
GroupSolutionHandler< dim, number > solutions
Solution vectors for fields handled by this solver.
Definition solver_base.h:271
const SolveContext< dim, degree, number > * solve_context
Solver context provides access to external information.
Definition solver_base.h:266
SolveBlock solve_block
Information about the solve block this handler is responsible for.
Definition solver_base.h:261
static void start_section(const char *name)
Start a new timer section.
Definition timer.cc:116
static void end_section(const char *name)
End the timer section.
Definition timer.cc:127
dealii::LinearAlgebra::distributed::BlockVector< number > BlockVector
Typedef for solution block vector.
Definition group_solution_handler.h:29
Definition conditional_ostreams.cc:20
Struct that stores relevant nonlinear solve information of a certain field.
Definition nonlinear_solve_parameters.h:20
double step_length
Definition nonlinear_solve_parameters.h:22
double tolerance_value
Definition nonlinear_solve_parameters.h:28
unsigned int max_iterations
Definition nonlinear_solve_parameters.h:25