| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2023 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_nonlinear_h |
| 16 |
#define dealii_nonlinear_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/base/exceptions.h> |
| 21 |
#include <deal.II/base/mpi_stub.h> |
| 22 |
|
| 23 |
#include <deal.II/lac/petsc_snes.h> |
| 24 |
#include <deal.II/lac/vector.h> |
| 25 |
|
| 26 |
#include <deal.II/sundials/kinsol.h> |
| 27 |
|
| 28 |
#include <deal.II/trilinos/nox.h> |
| 29 |
|
| 30 |
|
| 31 |
DEAL_II_NAMESPACE_OPEN |
| 32 |
|
| 33 |
/** |
| 34 |
* This class offers a unified interface to several nonlinear solver |
| 35 |
* implementations like KINSOL, NOX, and SNES. |
| 36 |
* |
| 37 |
* KINSOL nonlinear solvers are part of the SUNDIALS package, while |
| 38 |
* NOX is part of Trilinos and SNES is part of PETSc, respectively. |
| 39 |
* If no solver is manually specified, this class will automaticlaly |
| 40 |
* choose one of the available solvers depending on the enabled |
| 41 |
* dependencies. |
| 42 |
* |
| 43 |
* By calling the @p solve function of this @p |
| 44 |
* NonlinearSolverSelector, it selects the @p solve function of that |
| 45 |
* @p Solver that was specified in the constructor of this class, |
| 46 |
* similar to the SolverSelector class. |
| 47 |
* |
| 48 |
* <h3>Usage</h3> |
| 49 |
* An example of code one would run with this class is the |
| 50 |
* following: |
| 51 |
* @code |
| 52 |
* // Generate a @p NonlinearSolverSelector that uses @p KINSOL |
| 53 |
* NonlinearSolverSelector<Vector<double>>::AdditionalData additional_data; |
| 54 |
* additional_data.solver_type = |
| 55 |
* NonlinearSolverSelector<Vector<double>>::AdditionalData::SolverType::kinsol; |
| 56 |
* |
| 57 |
* NonlinearSolverSelector<Vector<double>> nonlinear_solver(additional_data); |
| 58 |
* |
| 59 |
* // Functions that are required for solving a nonlinear problem, |
| 60 |
* // which are utilized in both @p KINSOL and @p NOX. |
| 61 |
* nonlinear_solver.reinit_vector = [&](Vector<double> &x) {...}; |
| 62 |
* |
| 63 |
* nonlinear_solver.residual = |
| 64 |
* [&](const Vector<double> &evaluation_point, |
| 65 |
* Vector<double> & residual) {...}; |
| 66 |
* |
| 67 |
* nonlinear_solver.setup_jacobian = |
| 68 |
* [&](const Vector<double> ¤t_u) {...}; |
| 69 |
* |
| 70 |
* nonlinear_solver.solve_with_jacobian = |
| 71 |
* [&](const Vector<double> &rhs, |
| 72 |
* Vector<double> & dst, |
| 73 |
* const double tolerance) {...}; |
| 74 |
* |
| 75 |
* // Calling the @p solve function with an initial guess. |
| 76 |
* nonlinear_solver.solve(current_solution); |
| 77 |
* @endcode |
| 78 |
*/ |
| 79 |
template <typename VectorType = Vector<double>> |
| 80 |
class NonlinearSolverSelector |
| 81 |
{ |
| 82 |
public: |
| 83 |
/** |
| 84 |
* Additional data that will be sent through the NonlinearSolverSelector |
| 85 |
* class, then into the specific nonlinear solver classes themselves. |
| 86 |
* Many of these parameters can also be foud in the documentation for |
| 87 |
* @p KINSOL and @p NOX. |
| 88 |
*/ |
| 89 |
class AdditionalData |
| 90 |
{ |
| 91 |
public: |
| 92 |
/** |
| 93 |
* NonlinearSolverSelector solution strategy. The solver types included in |
| 94 |
* this class both use a Newton-Krylov solver with a line search and even |
| 95 |
* Picard iterations (KINSOL). |
| 96 |
*/ |
| 97 |
enum SolutionStrategy |
| 98 |
{ |
| 99 |
/** |
| 100 |
* Standard Newton iteration. |
| 101 |
*/ |
| 102 |
newton, |
| 103 |
/** |
| 104 |
* Newton iteration with line search. |
| 105 |
*/ |
| 106 |
linesearch, |
| 107 |
/** |
| 108 |
* Picard iteration. |
| 109 |
*/ |
| 110 |
picard, |
| 111 |
}; |
| 112 |
|
| 113 |
enum SolverType |
| 114 |
{ |
| 115 |
/** |
| 116 |
* Default parameter, will use whatever solver is available |
| 117 |
* with KINSOL as the priority. |
| 118 |
*/ |
| 119 |
automatic, |
| 120 |
/** |
| 121 |
* KINSOL nonlinear solver, part of the SUNDIALS package. |
| 122 |
*/ |
| 123 |
kinsol, |
| 124 |
/** |
| 125 |
* NOX nonlinear solver, part of the TRILINOS package. |
| 126 |
*/ |
| 127 |
nox, |
| 128 |
/* |
| 129 |
* Use the PETSc SNES solver. |
| 130 |
*/ |
| 131 |
petsc_snes |
| 132 |
}; |
| 133 |
|
| 134 |
/** |
| 135 |
* Initialization parameters for NonlinearSolverSelector. |
| 136 |
* |
| 137 |
* @param solver_type Nonlinear solver type. |
| 138 |
* @param strategy Method of solving the nonlinear problem. |
| 139 |
* @param maximum_non_linear_iterations Maximum number of nonlinear |
| 140 |
* iterations. |
| 141 |
* @param function_tolerance Absolute stopping tolerance for the norm |
| 142 |
* of the residual $F(u)$. |
| 143 |
* @param relative_tolerance Relative stopping tolerance. |
| 144 |
* @param step_tolerance Tolerance for minimum scaled step length |
| 145 |
* @param anderson_subspace_size Size of the Anderson acceleration |
| 146 |
* subspace, use 0 to disable. |
| 147 |
*/ |
| 148 |
AdditionalData(const SolverType &solver_type = automatic, |
| 149 |
const SolutionStrategy &strategy = linesearch, |
| 150 |
const unsigned int maximum_non_linear_iterations = 200, |
| 151 |
const double function_tolerance = 1e-8, |
| 152 |
const double relative_tolerance = 1e-5, |
| 153 |
const double step_tolerance = 0.0, |
| 154 |
const unsigned int anderson_subspace_size = 0); |
| 155 |
|
| 156 |
/** |
| 157 |
* The type of nonlinear solver to use. If the default 'automatic' is used, |
| 158 |
* it will choose the first available package in the order KINSOL, NOX, or |
| 159 |
* SNES. |
| 160 |
*/ |
| 161 |
SolverType solver_type; |
| 162 |
|
| 163 |
/** |
| 164 |
* The solution strategy to use. For this class, you can choose from |
| 165 |
* SolutionStrategy::newton, SolutionStrategy::linesearch, or |
| 166 |
* SolutionStrategy::picard. More details on this can be found on the |
| 167 |
* @p KINSOL documentation. |
| 168 |
*/ |
| 169 |
SolutionStrategy strategy; |
| 170 |
|
| 171 |
/** |
| 172 |
* Maximum number of nonlinear iterations allowed. |
| 173 |
*/ |
| 174 |
unsigned int maximum_non_linear_iterations; |
| 175 |
|
| 176 |
/** |
| 177 |
* A scalar used as a stopping tolerance on the scaled |
| 178 |
* maximum norm of the system function $F(u)$ or $G(u)$. |
| 179 |
* |
| 180 |
* If set to zero, default values will be used. |
| 181 |
*/ |
| 182 |
double function_tolerance; |
| 183 |
|
| 184 |
/** |
| 185 |
* Relative $l_2$ tolerance of the residual to be reached. |
| 186 |
* |
| 187 |
* @note Solver terminates successfully if either the function |
| 188 |
* tolerance or the relative tolerance has been reached. |
| 189 |
*/ |
| 190 |
const double relative_tolerance; |
| 191 |
|
| 192 |
/** |
| 193 |
* A scalar used as a stopping tolerance on the minimum |
| 194 |
* scaled step length. |
| 195 |
* |
| 196 |
* If set to zero, default values will be used. |
| 197 |
*/ |
| 198 |
double step_tolerance; |
| 199 |
|
| 200 |
/** |
| 201 |
* The size of the subspace used with Anderson acceleration |
| 202 |
* in conjunction with Picard iteration. |
| 203 |
* |
| 204 |
* If you set this to 0, no acceleration is used. |
| 205 |
*/ |
| 206 |
unsigned int anderson_subspace_size; |
| 207 |
}; |
| 208 |
|
| 209 |
/** |
| 210 |
* Constructor, filling in default values |
| 211 |
*/ |
| 212 |
NonlinearSolverSelector(); |
| 213 |
|
| 214 |
/** |
| 215 |
* Constructor, selecting the solver and other parameters specified in |
| 216 |
* @p additional_data. |
| 217 |
* |
| 218 |
* @deprecated Use the other constructor with MPI_Comm instead. |
| 219 |
*/ |
| 220 |
DEAL_II_DEPRECATED |
| 221 |
NonlinearSolverSelector(const AdditionalData &additional_data); |
| 222 |
|
| 223 |
/** |
| 224 |
* Constructor. |
| 225 |
* |
| 226 |
* @param additional_data NonlinearSolverSelector configuration data |
| 227 |
* @param mpi_communicator MPI communicator used by the nonlinear solver. |
| 228 |
*/ |
| 229 |
NonlinearSolverSelector(const AdditionalData &additional_data, |
| 230 |
const MPI_Comm &mpi_communicator); |
| 231 |
|
| 232 |
/** |
| 233 |
* Select a new nonlinear solver. All solver names used in this class are |
| 234 |
* all lower case. |
| 235 |
*/ |
| 236 |
void |
| 237 |
select(const typename AdditionalData::SolverType &type); |
| 238 |
|
| 239 |
/** |
| 240 |
* Set the generic additional data for all nonlinear solvers. |
| 241 |
*/ |
| 242 |
void |
| 243 |
set_data(const AdditionalData &additional_data); |
| 244 |
|
| 245 |
/** |
| 246 |
* Set the additional data for NOX. See TrilinosWrappers::NOXSolver |
| 247 |
* for more information. |
| 248 |
*/ |
| 249 |
#ifdef DEAL_II_TRILINOS_WITH_NOX |
| 250 |
void |
| 251 |
set_data( |
| 252 |
const typename TrilinosWrappers::NOXSolver<VectorType>::AdditionalData |
| 253 |
&additional_data, |
| 254 |
const Teuchos::RCP<Teuchos::ParameterList> ¶meters = |
| 255 |
Teuchos::rcp(new Teuchos::ParameterList)); |
| 256 |
#endif |
| 257 |
|
| 258 |
/** |
| 259 |
* Set the additional data for KINSOL. See SUNDIALS::KINSOL for more |
| 260 |
* information. |
| 261 |
*/ |
| 262 |
#ifdef DEAL_II_WITH_SUNDIALS |
| 263 |
void |
| 264 |
set_data(const typename SUNDIALS::KINSOL<VectorType>::AdditionalData |
| 265 |
&additional_data); |
| 266 |
#endif |
| 267 |
|
| 268 |
/** |
| 269 |
* Set the additional data for SNES. See PETScWrappers::NonlinearSolverData |
| 270 |
* for more information. |
| 271 |
*/ |
| 272 |
#ifdef DEAL_II_WITH_PETSC |
| 273 |
void |
| 274 |
set_data(const typename PETScWrappers::NonlinearSolverData &additional_data); |
| 275 |
#endif |
| 276 |
|
| 277 |
/** |
| 278 |
* Solve the nonlinear system. |
| 279 |
* |
| 280 |
* The content of `initial_guess_and_solution` is used as an initial guess |
| 281 |
* and the final solution is stored in the same vector. |
| 282 |
*/ |
| 283 |
void |
| 284 |
solve(VectorType &initial_guess_and_solution); |
| 285 |
|
| 286 |
/** |
| 287 |
* A function object that users need to supply and that is intended to |
| 288 |
* reinitize the given vector to its correct size, and block structure (if |
| 289 |
* block vectors are used), along with any |
| 290 |
* other properties necessary. |
| 291 |
*/ |
| 292 |
std::function<void(VectorType &)> reinit_vector; |
| 293 |
|
| 294 |
/** |
| 295 |
* A function object that users should supply and that is intended to |
| 296 |
* compute the residual `dst = F(src)`. |
| 297 |
* |
| 298 |
* @note This variable represents a |
| 299 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 300 |
* See there for a description of how to deal with errors and other |
| 301 |
* requirements and conventions. Some of the underlying packages |
| 302 |
* used by this class can deal with recoverable exceptions, whereas |
| 303 |
* others cannot. As a consequence, if a callback |
| 304 |
* throws an exception of type RecoverableUserCallbackError, then this |
| 305 |
* exception may or may not be treated like any other exception. |
| 306 |
*/ |
| 307 |
std::function<void(const VectorType &src, VectorType &dst)> residual; |
| 308 |
|
| 309 |
/** |
| 310 |
* A function object that users may supply and that is intended to |
| 311 |
* prepare the linear solver for subsequent calls to |
| 312 |
* solve_with_jacobian). |
| 313 |
* |
| 314 |
* The job of setup_jacobian() is to prepare the linear solver for |
| 315 |
* subsequent calls to solve_with_jacobian(), in the solution of linear |
| 316 |
* systems $Ax = b$. The exact nature of this system depends on the |
| 317 |
* SolutionStrategy that has been selected. |
| 318 |
* |
| 319 |
* In the cases strategy = SolutionStrategy::newton or |
| 320 |
* SolutionStrategy::linesearch, $A$ is the Jacobian $J = \partial |
| 321 |
* F/\partial u$. If strategy = SolutionStrategy::picard, $A$ is the |
| 322 |
* approximate Jacobian matrix $L$. |
| 323 |
* |
| 324 |
* @param current_u Current value of $u$ |
| 325 |
* |
| 326 |
* @note This variable represents a |
| 327 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 328 |
* See there for a description of how to deal with errors and other |
| 329 |
* requirements and conventions. Some of the underlying packages |
| 330 |
* used by this class can deal with recoverable exceptions, whereas |
| 331 |
* others cannot. As a consequence, if a callback |
| 332 |
* throws an exception of type RecoverableUserCallbackError, then this |
| 333 |
* exception may or may not be treated like any other exception. |
| 334 |
*/ |
| 335 |
std::function<void(const VectorType ¤t_u)> setup_jacobian; |
| 336 |
|
| 337 |
/** |
| 338 |
* A function object that users may supply and that is intended to solve |
| 339 |
* a linear system with the Jacobian matrix. |
| 340 |
* |
| 341 |
* @note PETSc SNES does not provide us with a linear tolerance to solve |
| 342 |
* linear system with. We will provide a value of 1e-6 in that case. |
| 343 |
* |
| 344 |
* @param[in] rhs The system right hand side to solve for. |
| 345 |
* @param[out] dst The solution of $J^{-1} * \texttt{src}$. |
| 346 |
* @param[in] tolerance The tolerance with which to solve the linear system |
| 347 |
* of equations. |
| 348 |
* |
| 349 |
* @note This variable represents a |
| 350 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 351 |
* See there for a description of how to deal with errors and other |
| 352 |
* requirements and conventions. Some of the underlying packages |
| 353 |
* used by this class can deal with recoverable exceptions, whereas |
| 354 |
* others cannot. As a consequence, if a callback |
| 355 |
* throws an exception of type RecoverableUserCallbackError, then this |
| 356 |
* exception may or may not be treated like any other exception. |
| 357 |
*/ |
| 358 |
std::function< |
| 359 |
void(const VectorType &rhs, VectorType &dst, const double tolerance)> |
| 360 |
solve_with_jacobian; |
| 361 |
|
| 362 |
private: |
| 363 |
/** |
| 364 |
* NonlinearSolverSelector configuration data. |
| 365 |
*/ |
| 366 |
AdditionalData additional_data; |
| 367 |
|
| 368 |
private: |
| 369 |
/** |
| 370 |
* The MPI communicator to be used by this solver, if any. |
| 371 |
*/ |
| 372 |
MPI_Comm mpi_communicator; |
| 373 |
|
| 374 |
/** |
| 375 |
* KINSOL configuration data |
| 376 |
*/ |
| 377 |
#ifdef DEAL_II_WITH_SUNDIALS |
| 378 |
typename SUNDIALS::KINSOL<VectorType>::AdditionalData additional_data_kinsol; |
| 379 |
#endif |
| 380 |
|
| 381 |
/** |
| 382 |
* NOX configuration data |
| 383 |
*/ |
| 384 |
#ifdef DEAL_II_TRILINOS_WITH_NOX |
| 385 |
typename TrilinosWrappers::NOXSolver<VectorType>::AdditionalData |
| 386 |
additional_data_nox; |
| 387 |
Teuchos::RCP<Teuchos::ParameterList> parameters_nox = |
| 388 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 389 |
#endif |
| 390 |
|
| 391 |
/** |
| 392 |
* PETSc SNES configuration data |
| 393 |
*/ |
| 394 |
#ifdef DEAL_II_WITH_PETSC |
| 395 |
typename PETScWrappers::NonlinearSolverData additional_data_petsc_snes; |
| 396 |
#endif |
| 397 |
|
| 398 |
/** |
| 399 |
* Solve with PETSc SNES. Internal functions specialized for PETSc |
| 400 |
* Vectors. This is necessary to only instantiate the SNES solvers |
| 401 |
* with PETSc Vectors, as this is the only vector type currently |
| 402 |
* supported. |
| 403 |
*/ |
| 404 |
void |
| 405 |
solve_with_petsc(VectorType &initial_guess_and_solution); |
| 406 |
}; |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
template <typename VectorType> |
| 411 |
void |
| 412 |
NonlinearSolverSelector<VectorType>::set_data( |
| 413 |
const AdditionalData &additional_data) |
| 414 |
{ |
| 415 |
(void)additional_data; |
| 416 |
|
| 417 |
#ifdef DEAL_II_WITH_SUNDIALS |
| 418 |
// These if statements pass on the strategy to the other nonlinear solvers |
| 419 |
if (additional_data.strategy == |
| 420 |
NonlinearSolverSelector<VectorType>::AdditionalData::linesearch) |
| 421 |
additional_data_kinsol.strategy = |
| 422 |
SUNDIALS::KINSOL<VectorType>::AdditionalData::linesearch; |
| 423 |
else if (additional_data.strategy == |
| 424 |
NonlinearSolverSelector<VectorType>::AdditionalData::newton) |
| 425 |
additional_data_kinsol.strategy = |
| 426 |
SUNDIALS::KINSOL<VectorType>::AdditionalData::newton; |
| 427 |
else if (additional_data.strategy == |
| 428 |
NonlinearSolverSelector<VectorType>::AdditionalData::picard) |
| 429 |
additional_data_kinsol.strategy = |
| 430 |
SUNDIALS::KINSOL<VectorType>::AdditionalData::picard; |
| 431 |
|
| 432 |
// Setting data points in the KINSOL class from the NonlinearSolverSelector |
| 433 |
// class |
| 434 |
additional_data_kinsol.maximum_non_linear_iterations = |
| 435 |
additional_data.maximum_non_linear_iterations; |
| 436 |
additional_data_kinsol.function_tolerance = |
| 437 |
additional_data.function_tolerance; |
| 438 |
additional_data_kinsol.step_tolerance = additional_data.step_tolerance; |
| 439 |
additional_data_kinsol.anderson_subspace_size = |
| 440 |
additional_data.anderson_subspace_size; |
| 441 |
#endif |
| 442 |
|
| 443 |
// Do the same thing we did above but with NOX |
| 444 |
#ifdef DEAL_II_TRILINOS_WITH_NOX |
| 445 |
parameters_nox->set("Nonlinear Solver", "Line Search Based"); |
| 446 |
Teuchos::ParameterList &Line_Search = parameters_nox->sublist("Line Search"); |
| 447 |
Line_Search.set("Method", "Full Step"); |
| 448 |
|
| 449 |
additional_data_nox.max_iter = additional_data.maximum_non_linear_iterations; |
| 450 |
additional_data_nox.abs_tol = additional_data.function_tolerance; |
| 451 |
additional_data_nox.rel_tol = additional_data.relative_tolerance; |
| 452 |
#endif |
| 453 |
|
| 454 |
#ifdef DEAL_II_WITH_PETSC |
| 455 |
additional_data_petsc_snes.options_prefix = ""; |
| 456 |
|
| 457 |
if (additional_data.anderson_subspace_size > 0 && |
| 458 |
additional_data.strategy == |
| 459 |
NonlinearSolverSelector<VectorType>::AdditionalData::picard) |
| 460 |
{ |
| 461 |
additional_data_petsc_snes.snes_type = "anderson"; |
| 462 |
// TODO additional_data.anderson_subspace_size; |
| 463 |
} |
| 464 |
else if (additional_data.strategy == |
| 465 |
NonlinearSolverSelector<VectorType>::AdditionalData::linesearch) |
| 466 |
{ |
| 467 |
additional_data_petsc_snes.snes_type = "newtonls"; |
| 468 |
additional_data_petsc_snes.snes_linesearch_type = "bt"; |
| 469 |
} |
| 470 |
else if (additional_data.strategy == |
| 471 |
NonlinearSolverSelector<VectorType>::AdditionalData::newton) |
| 472 |
{ |
| 473 |
additional_data_petsc_snes.snes_linesearch_type = "newtonls"; |
| 474 |
additional_data_petsc_snes.snes_linesearch_type = "basic"; |
| 475 |
} |
| 476 |
else if (additional_data.strategy == |
| 477 |
NonlinearSolverSelector<VectorType>::AdditionalData::picard) |
| 478 |
additional_data_petsc_snes.snes_type = "nrichardson"; |
| 479 |
|
| 480 |
additional_data_petsc_snes.absolute_tolerance = |
| 481 |
additional_data.function_tolerance; |
| 482 |
additional_data_petsc_snes.relative_tolerance = |
| 483 |
additional_data.relative_tolerance; |
| 484 |
additional_data_petsc_snes.step_tolerance = additional_data.step_tolerance; |
| 485 |
additional_data_petsc_snes.maximum_non_linear_iterations = |
| 486 |
additional_data.maximum_non_linear_iterations; |
| 487 |
additional_data_petsc_snes.max_n_function_evaluations = -1; |
| 488 |
|
| 489 |
#endif |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
template <typename VectorType> |
| 495 |
NonlinearSolverSelector<VectorType>::NonlinearSolverSelector() |
| 496 |
: mpi_communicator(MPI_COMM_SELF) |
| 497 |
{} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
template <typename VectorType> |
| 502 |
NonlinearSolverSelector<VectorType>::NonlinearSolverSelector( |
| 503 |
const AdditionalData &additional_data) |
| 504 |
: additional_data(additional_data) |
| 505 |
, mpi_communicator(MPI_COMM_SELF) |
| 506 |
{ |
| 507 |
set_data(additional_data); |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
template <typename VectorType> |
| 513 |
NonlinearSolverSelector<VectorType>::NonlinearSolverSelector( |
| 514 |
const AdditionalData &additional_data, |
| 515 |
const MPI_Comm &mpi_communicator) |
| 516 |
: additional_data(additional_data) |
| 517 |
, mpi_communicator(mpi_communicator) |
| 518 |
{ |
| 519 |
set_data(additional_data); |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
template <typename VectorType> |
| 525 |
void |
| 526 |
NonlinearSolverSelector<VectorType>::select( |
| 527 |
const typename AdditionalData::SolverType &type) |
| 528 |
{ |
| 529 |
additional_data.solver_type = type; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
template <typename VectorType> |
| 535 |
NonlinearSolverSelector<VectorType>::AdditionalData::AdditionalData( |
| 536 |
const SolverType &solver_type, |
| 537 |
const SolutionStrategy &strategy, |
| 538 |
const unsigned int maximum_non_linear_iterations, |
| 539 |
const double function_tolerance, |
| 540 |
const double relative_tolerance, |
| 541 |
const double step_tolerance, |
| 542 |
const unsigned int anderson_subspace_size) |
| 543 |
: solver_type(solver_type) |
| 544 |
, strategy(strategy) |
| 545 |
, maximum_non_linear_iterations(maximum_non_linear_iterations) |
| 546 |
, function_tolerance(function_tolerance) |
| 547 |
, relative_tolerance(relative_tolerance) |
| 548 |
, step_tolerance(step_tolerance) |
| 549 |
, anderson_subspace_size(anderson_subspace_size) |
| 550 |
{} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
#ifdef DEAL_II_TRILINOS_WITH_NOX |
| 555 |
template <typename VectorType> |
| 556 |
void |
| 557 |
NonlinearSolverSelector<VectorType>::set_data( |
| 558 |
const typename TrilinosWrappers::NOXSolver<VectorType>::AdditionalData |
| 559 |
&additional_data, |
| 560 |
const Teuchos::RCP<Teuchos::ParameterList> ¶meters) |
| 561 |
{ |
| 562 |
additional_data_nox = additional_data; |
| 563 |
parameters_nox = parameters; |
| 564 |
} |
| 565 |
#endif |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
#ifdef DEAL_II_WITH_SUNDIALS |
| 570 |
template <typename VectorType> |
| 571 |
void |
| 572 |
NonlinearSolverSelector<VectorType>::set_data( |
| 573 |
const typename SUNDIALS::KINSOL<VectorType>::AdditionalData &additional_data) |
| 574 |
{ |
| 575 |
additional_data_kinsol = additional_data; |
| 576 |
} |
| 577 |
#endif |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
template <typename VectorType> |
| 582 |
void |
| 583 |
NonlinearSolverSelector<VectorType>::solve_with_petsc( |
| 584 |
VectorType & /*initial_guess_and_solution*/) |
| 585 |
{ |
| 586 |
AssertThrow(false, |
| 587 |
ExcMessage("PETSc SNES requires you to use PETSc vectors.")); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
#ifdef DEAL_II_WITH_PETSC |
| 593 |
template <> |
| 594 |
void |
| 595 |
NonlinearSolverSelector<PETScWrappers::MPI::Vector>::solve_with_petsc( |
| 596 |
PETScWrappers::MPI::Vector &initial_guess_and_solution) |
| 597 |
{ |
| 598 |
PETScWrappers::NonlinearSolver<PETScWrappers::MPI::Vector> nonlinear_solver( |
| 599 |
additional_data_petsc_snes, mpi_communicator); |
| 600 |
|
| 601 |
nonlinear_solver.residual = residual; |
| 602 |
|
| 603 |
nonlinear_solver.setup_jacobian = setup_jacobian; |
| 604 |
|
| 605 |
nonlinear_solver.solve_with_jacobian = |
| 606 |
[&](const PETScWrappers::MPI::Vector &src, |
| 607 |
PETScWrappers::MPI::Vector &dst) { |
| 608 |
// PETSc does not gives a tolerance, so we have to choose something |
| 609 |
// reasonable to provide to the user: |
| 610 |
const double tolerance = 1e-6; |
| 611 |
solve_with_jacobian(src, dst, tolerance); |
| 612 |
}; |
| 613 |
|
| 614 |
nonlinear_solver.solve(initial_guess_and_solution); |
| 615 |
} |
| 616 |
#endif |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
template <typename VectorType> |
| 621 |
void |
| 622 |
NonlinearSolverSelector<VectorType>::solve( |
| 623 |
VectorType &initial_guess_and_solution) |
| 624 |
{ |
| 625 |
// The "auto" solver_type will default to kinsol, however if KINSOL is not |
| 626 |
// available then we will use NOX. |
| 627 |
if (additional_data.solver_type == AdditionalData::SolverType::automatic) |
| 628 |
{ |
| 629 |
#ifdef DEAL_II_WITH_PETSC |
| 630 |
additional_data.solver_type = AdditionalData::SolverType::petsc_snes; |
| 631 |
#endif |
| 632 |
#ifdef DEAL_II_TRILINOS_WITH_NOX |
| 633 |
additional_data.solver_type = AdditionalData::SolverType::nox; |
| 634 |
#endif |
| 635 |
#ifdef DEAL_II_WITH_SUNDIALS |
| 636 |
additional_data.solver_type = AdditionalData::SolverType::kinsol; |
| 637 |
#endif |
| 638 |
|
| 639 |
// If "auto" is still the solver type we cannot solve the problem |
| 640 |
if (additional_data.solver_type == AdditionalData::SolverType::automatic) |
| 641 |
AssertThrow(false, ExcMessage("No valid solver type.")); |
| 642 |
} |
| 643 |
|
| 644 |
if (additional_data.solver_type == AdditionalData::SolverType::kinsol) |
| 645 |
{ |
| 646 |
#ifdef DEAL_II_WITH_SUNDIALS |
| 647 |
SUNDIALS::KINSOL<VectorType> nonlinear_solver(additional_data_kinsol, |
| 648 |
mpi_communicator); |
| 649 |
|
| 650 |
nonlinear_solver.reinit_vector = reinit_vector; |
| 651 |
nonlinear_solver.residual = residual; |
| 652 |
|
| 653 |
// We cannot simply set these two functions equal to each other |
| 654 |
// because they have a different number of inputs. |
| 655 |
nonlinear_solver.setup_jacobian = [&](const VectorType ¤t_u, |
| 656 |
const VectorType & /*current_f*/) { |
| 657 |
return NonlinearSolverSelector<VectorType>::setup_jacobian(current_u); |
| 658 |
}; |
| 659 |
|
| 660 |
nonlinear_solver.solve_with_jacobian = solve_with_jacobian; |
| 661 |
|
| 662 |
nonlinear_solver.solve(initial_guess_and_solution); |
| 663 |
#else |
| 664 |
AssertThrow( |
| 665 |
false, ExcMessage("You do not have SUNDIALS configured with deal.II!")); |
| 666 |
#endif |
| 667 |
} |
| 668 |
else if (additional_data.solver_type == AdditionalData::SolverType::nox) |
| 669 |
{ |
| 670 |
#ifdef DEAL_II_TRILINOS_WITH_NOX |
| 671 |
|
| 672 |
TrilinosWrappers::NOXSolver<VectorType> nonlinear_solver( |
| 673 |
additional_data_nox, parameters_nox); |
| 674 |
|
| 675 |
// Do the same thing for NOX that we did with KINSOL. |
| 676 |
nonlinear_solver.residual = residual; |
| 677 |
|
| 678 |
// setup_jacobian for NOX has the same number of arguments for the same |
| 679 |
// function in NonlinearSolverSelector. |
| 680 |
nonlinear_solver.setup_jacobian = setup_jacobian; |
| 681 |
|
| 682 |
nonlinear_solver.solve_with_jacobian = solve_with_jacobian; |
| 683 |
|
| 684 |
nonlinear_solver.solve(initial_guess_and_solution); |
| 685 |
#else |
| 686 |
AssertThrow( |
| 687 |
false, ExcMessage("You do not have Trilinos configured with deal.II")); |
| 688 |
#endif |
| 689 |
} |
| 690 |
else if (additional_data.solver_type == |
| 691 |
AdditionalData::SolverType::petsc_snes) |
| 692 |
{ |
| 693 |
// Forward to internal function specializations, which throws for |
| 694 |
// non-supported vector types: |
| 695 |
solve_with_petsc(initial_guess_and_solution); |
| 696 |
} |
| 697 |
else |
| 698 |
{ |
| 699 |
const std::string solvers = "" |
| 700 |
#ifdef DEAL_II_WITH_SUNDIALS |
| 701 |
"kinsol\n" |
| 702 |
#endif |
| 703 |
#ifdef DEAL_II_TRILINOS_WITH_NOX |
| 704 |
"NOX\n" |
| 705 |
#endif |
| 706 |
#ifdef DEAL_II_WITH_PETSC |
| 707 |
"SNES\n" |
| 708 |
#endif |
| 709 |
; |
| 710 |
|
| 711 |
AssertThrow(false, |
| 712 |
ExcMessage( |
| 713 |
"Invalid nonlinear solver specified. " |
| 714 |
"The solvers available in your installation are:\n" + |
| 715 |
solvers)); |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
DEAL_II_NAMESPACE_CLOSE |
| 720 |
|
| 721 |
#endif |