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_group;
37 using LinearSolver<dim, degree, number>::rhs_vector;
38
39public:
47 solve_context->get_user_inputs().nonlinear_solve_parameters.newton_solvers.at(
48 solve_group.id))
49 {}
50
54 void
55 init(const std::list<DependencyMap> &all_dependeny_sets) override
56 {
58 unsigned int num_levels = solve_context->get_dof_manager().get_dof_handlers().size();
59 newton_updates.resize(num_levels);
60 for (unsigned int relative_level = 0; relative_level < num_levels; ++relative_level)
61 {
62 newton_updates[relative_level].reinit(
63 solutions.get_solution_full_vector(relative_level));
64 }
65 }
66
70 void
71 reinit() override
72 {
74 const unsigned int num_levels = newton_updates.size();
75 for (unsigned int relative_level = 0; relative_level < num_levels; ++relative_level)
76 {
77 newton_updates[relative_level].reinit(
78 solutions.get_solution_full_vector(relative_level));
79 }
80 }
81
85 void
86 solve_level(unsigned int relative_level) override
87 {
91
96 // TODO: setup initial guess for solution vector. Maybe use old_solution if
97 // available.
98 if (!solutions.get_solution_level(relative_level).old_solutions.empty())
99 {
100 solutions.get_solution_full_vector(relative_level) =
101 solutions.get_old_solution_full_vector(0, relative_level);
102 }
103
104 // Newton iteration loop.
105 bool newton_unconverged = true;
106 unsigned int iter = 0;
107 number l2_norm = -1.0;
108 int total_lin_iters = 0;
110 {
111 // Zero out the ghosts
112
113 // Solve for Newton-residual (r)
114 Timer::start_section("Zero ghosts");
115 newton_residual.zero_out_ghost_values();
116 Timer::end_section("Zero ghosts");
117 rhs_op.compute_operator(newton_residual);
118 newton_residual.update_ghost_values(); // needed?
119
120 // Solve for Newton update. (-dr/du|Du)
122 newton_update.update_ghost_values(); // needed?
123
124 // Perform Newton update.
125 solutions.get_solution_full_vector(relative_level)
127
128 // Apply constraints to solution vector
129 solutions.apply_constraints(relative_level);
130
131 // Update the ghosts
132 Timer::start_section("Update ghosts");
133 solutions.update_ghosts(relative_level);
134 Timer::end_section("Update ghosts");
135
136 // Check convergence.
137 l2_norm = newton_residual.l2_norm();
139
140 // Todo: implement some super simple backtracking. Something like if the residual
141 // increases instead of decreasing, try again with a smaller step length.
142 }
143 if (solve_context->get_user_inputs().output_parameters.should_output(
144 solve_context->get_simulation_timer().get_increment()))
145 {
147 << " Newton solve final residual : " << l2_norm / normalization_value()
148 << " Newton steps: " << iter << " Total linear steps: " << total_lin_iters
149 << "\n"
150 << std::flush;
151 }
153 {
155 << "[Increment " << solve_context->get_simulation_timer().get_increment()
156 << "] "
157 << "Warning: Newton solver did not converge before " << newton_max_iterations
158 << " iterations.\n\n";
159 }
160 }
161
162protected:
163 std::vector<BlockVector<number>> newton_updates; //"change" term
165};
166
static dealii::ConditionalOStream & pout_summary()
Log output stream for writing a summary.log file.
Definition conditional_ostreams.cc:34
static dealii::ConditionalOStream & pout_base()
Generic parallel output stream. Used for essential information in release and debug mode.
Definition conditional_ostreams.cc:43
This class handles the explicit solves of all explicit fields.
Definition linear_solver.h:34
std::vector< MFOperator< dim, degree, number > > rhs_operators
Matrix free operators for each level.
Definition linear_solver.h:184
int do_linear_solve(BlockVector< number > &b_vector, MFOperator< dim, degree, number > &lhs_operator, BlockVector< number > &x_vector)
Definition linear_solver.h:150
double normalization_value()
Definition linear_solver.h:190
void init(const std::list< DependencyMap > &all_dependeny_sets) override
Initialize the solver.
Definition linear_solver.h:56
std::vector< BlockVector< number > > rhs_vector
Definition linear_solver.h:187
std::vector< MFOperator< dim, degree, number > > lhs_operators
Definition linear_solver.h:186
void reinit() override
Reinitialize the solver.
Definition linear_solver.h:112
This class exists to evaluate a single user-defined operator for the matrix-free implementation of so...
Definition mf_operator.h:50
This class handles the explicit solves of all explicit fields.
Definition newton_solver.h:28
void reinit() override
Reinitialize the solver.
Definition newton_solver.h:71
NonlinearSolverParameters newton_params
Definition newton_solver.h:164
void init(const std::list< DependencyMap > &all_dependeny_sets) override
Initialize the solver.
Definition newton_solver.h:55
void solve_level(unsigned int relative_level) override
Solve for a single update step.
Definition newton_solver.h:86
std::vector< BlockVector< number > > newton_updates
Definition newton_solver.h:163
NewtonSolver(SolveGroup _solve_group, const SolveContext< dim, degree, number > &_solve_context)
Constructor.
Definition newton_solver.h:43
This class provides context for a solver with ptrs to all the relevant dependencies.
Definition solve_context.h:36
Structure to hold the attributes of a solve-group.
Definition solve_group.h:59
Definition solver_base.h:32
SolveGroup solve_group
Information about the solve group this handler is responsible for.
Definition solver_base.h:259
GroupSolutionHandler< dim, number > solutions
Solution vectors for fields handled by this solver.
Definition solver_base.h:269
const SolveContext< dim, degree, number > * solve_context
Solver context provides access to external information.
Definition solver_base.h:264
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
@ Value
Use value of the variable as a criterion for refinement.
Definition grid_refiner_criterion.h:31
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:18
double step_length
Definition nonlinear_solve_parameters.h:20
double tolerance_value
Definition nonlinear_solve_parameters.h:26
unsigned int max_iterations
Definition nonlinear_solve_parameters.h:23