deal.II version GIT relicensing-6842-g793a97d2aa 2026-10-02 14:00:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
nonlinear.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR 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// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_nonlinear_h
14#define dealii_nonlinear_h
15
16#include <deal.II/base/config.h>
17
20
22#include <deal.II/lac/vector.h>
23
25
27
28
30
77template <typename VectorType = Vector<double>>
79{
80public:
88 {
89 public:
110
112 {
126 /*
127 * Use the PETSc SNES solver.
128 */
130 };
131
148 const unsigned int maximum_non_linear_iterations = 200,
149 const double function_tolerance = 1e-8,
150 const double relative_tolerance = 1e-5,
151 const double step_tolerance = 0.0,
152 const unsigned int anderson_subspace_size = 0);
153
160
168
173
181
188 const double relative_tolerance;
189
197
205 };
206
211
220
225 void
226 select(const typename AdditionalData::SolverType &type);
227
231 void
233
238#ifdef DEAL_II_TRILINOS_WITH_NOX
239 void
240 set_data(
243 const Teuchos::RCP<Teuchos::ParameterList> &parameters =
244 Teuchos::rcp(new Teuchos::ParameterList));
245#endif
246
251#ifdef DEAL_II_WITH_SUNDIALS
252 void
255#endif
256
261#ifdef DEAL_II_WITH_PETSC
262 void
264#endif
265
272 void
273 solve(VectorType &initial_guess_and_solution);
274
281 std::function<void(VectorType &)> reinit_vector;
282
296 std::function<void(const VectorType &src, VectorType &dst)> residual;
297
324 std::function<void(const VectorType &current_u)> setup_jacobian;
325
347 std::function<
348 void(const VectorType &rhs, VectorType &dst, const double tolerance)>
350
351private:
356
357private:
362
366#ifdef DEAL_II_WITH_SUNDIALS
368#endif
369
373#ifdef DEAL_II_TRILINOS_WITH_NOX
376 Teuchos::RCP<Teuchos::ParameterList> parameters_nox =
377 Teuchos::rcp(new Teuchos::ParameterList);
378#endif
379
383#ifdef DEAL_II_WITH_PETSC
385#endif
386
393 void
394 solve_with_petsc(VectorType &initial_guess_and_solution);
395};
396
397
398
399template <typename VectorType>
400void
402 const AdditionalData &additional_data)
403{
404 (void)additional_data;
405
406#ifdef DEAL_II_WITH_SUNDIALS
407 // These if statements pass on the strategy to the other nonlinear solvers
408 if (additional_data.strategy ==
410 additional_data_kinsol.strategy =
412 else if (additional_data.strategy ==
414 additional_data_kinsol.strategy =
416 else if (additional_data.strategy ==
418 additional_data_kinsol.strategy =
420
421 // Setting data points in the KINSOL class from the NonlinearSolverSelector
422 // class
423 additional_data_kinsol.maximum_non_linear_iterations =
424 additional_data.maximum_non_linear_iterations;
425 additional_data_kinsol.function_tolerance =
426 additional_data.function_tolerance;
427 additional_data_kinsol.step_tolerance = additional_data.step_tolerance;
428 additional_data_kinsol.anderson_subspace_size =
429 additional_data.anderson_subspace_size;
430#endif
431
432// Do the same thing we did above but with NOX
433#ifdef DEAL_II_TRILINOS_WITH_NOX
434 parameters_nox->set("Nonlinear Solver", "Line Search Based");
435 Teuchos::ParameterList &Line_Search = parameters_nox->sublist("Line Search");
436 Line_Search.set("Method", "Full Step");
437
438 additional_data_nox.max_iter = additional_data.maximum_non_linear_iterations;
439 additional_data_nox.abs_tol = additional_data.function_tolerance;
440 additional_data_nox.rel_tol = additional_data.relative_tolerance;
441#endif
442
443#ifdef DEAL_II_WITH_PETSC
444 additional_data_petsc_snes.options_prefix = "";
445
446 if (additional_data.anderson_subspace_size > 0 &&
447 additional_data.strategy ==
449 {
450 additional_data_petsc_snes.snes_type = "anderson";
451 // TODO additional_data.anderson_subspace_size;
452 }
453 else if (additional_data.strategy ==
455 {
456 additional_data_petsc_snes.snes_type = "newtonls";
457 additional_data_petsc_snes.snes_linesearch_type = "bt";
458 }
459 else if (additional_data.strategy ==
461 {
462 additional_data_petsc_snes.snes_linesearch_type = "newtonls";
463 additional_data_petsc_snes.snes_linesearch_type = "basic";
464 }
465 else if (additional_data.strategy ==
467 additional_data_petsc_snes.snes_type = "nrichardson";
468
469 additional_data_petsc_snes.absolute_tolerance =
470 additional_data.function_tolerance;
471 additional_data_petsc_snes.relative_tolerance =
472 additional_data.relative_tolerance;
473 additional_data_petsc_snes.step_tolerance = additional_data.step_tolerance;
474 additional_data_petsc_snes.maximum_non_linear_iterations =
475 additional_data.maximum_non_linear_iterations;
476 additional_data_petsc_snes.max_n_function_evaluations = -1;
477
478#endif
479}
480
481
482
483template <typename VectorType>
485 : mpi_communicator(MPI_COMM_SELF)
486{}
487
488
489
490template <typename VectorType>
492 const AdditionalData &additional_data,
493 const MPI_Comm &mpi_communicator)
494 : additional_data(additional_data)
495 , mpi_communicator(mpi_communicator)
496{
498}
499
500
501
502template <typename VectorType>
503void
505 const typename AdditionalData::SolverType &type)
506{
507 additional_data.solver_type = type;
508}
509
510
511
512template <typename VectorType>
514 const SolverType &solver_type,
515 const SolutionStrategy &strategy,
516 const unsigned int maximum_non_linear_iterations,
517 const double function_tolerance,
518 const double relative_tolerance,
519 const double step_tolerance,
520 const unsigned int anderson_subspace_size)
521 : solver_type(solver_type)
522 , strategy(strategy)
523 , maximum_non_linear_iterations(maximum_non_linear_iterations)
524 , function_tolerance(function_tolerance)
525 , relative_tolerance(relative_tolerance)
526 , step_tolerance(step_tolerance)
527 , anderson_subspace_size(anderson_subspace_size)
528{}
529
530
531
532#ifdef DEAL_II_TRILINOS_WITH_NOX
533template <typename VectorType>
534void
538 const Teuchos::RCP<Teuchos::ParameterList> &parameters)
539{
541 parameters_nox = parameters;
542}
543#endif
544
545
546
547#ifdef DEAL_II_WITH_SUNDIALS
548template <typename VectorType>
549void
555#endif
556
557
558
559template <typename VectorType>
560void
562 VectorType & /*initial_guess_and_solution*/)
563{
564 AssertThrow(false,
565 ExcMessage("PETSc SNES requires you to use PETSc vectors."));
566}
567
568
569
570#ifdef DEAL_II_WITH_PETSC
571template <>
572void
574 PETScWrappers::MPI::Vector &initial_guess_and_solution)
575{
578
579 nonlinear_solver.residual = residual;
580
581 nonlinear_solver.setup_jacobian = setup_jacobian;
582
583 nonlinear_solver.solve_with_jacobian =
584 [&](const PETScWrappers::MPI::Vector &src,
586 // PETSc does not gives a tolerance, so we have to choose something
587 // reasonable to provide to the user:
588 const double tolerance = 1e-6;
589 solve_with_jacobian(src, dst, tolerance);
590 };
591
592 nonlinear_solver.solve(initial_guess_and_solution);
593}
594#endif
595
596
597
598template <typename VectorType>
599void
601 VectorType &initial_guess_and_solution)
602{
603 // The "auto" solver_type will default to kinsol, however if KINSOL is not
604 // available then we will use NOX.
606 {
607#ifdef DEAL_II_WITH_PETSC
609#endif
610#ifdef DEAL_II_TRILINOS_WITH_NOX
612#endif
613#ifdef DEAL_II_WITH_SUNDIALS
615#endif
616
617 // If "auto" is still the solver type we cannot solve the problem
619 AssertThrow(false, ExcMessage("No valid solver type."));
620 }
621
623 {
624#ifdef DEAL_II_WITH_SUNDIALS
627
628 nonlinear_solver.reinit_vector = reinit_vector;
629 nonlinear_solver.residual = residual;
630
631 // We cannot simply set these two functions equal to each other
632 // because they have a different number of inputs.
633 nonlinear_solver.setup_jacobian = [&](const VectorType &current_u,
634 const VectorType & /*current_f*/) {
636 };
637
638 nonlinear_solver.solve_with_jacobian = solve_with_jacobian;
639
640 nonlinear_solver.solve(initial_guess_and_solution);
641#else
643 false, ExcMessage("You do not have SUNDIALS configured with deal.II!"));
644#endif
645 }
647 {
648#ifdef DEAL_II_TRILINOS_WITH_NOX
649
652
653 // Do the same thing for NOX that we did with KINSOL.
654 nonlinear_solver.residual = residual;
655
656 // setup_jacobian for NOX has the same number of arguments for the same
657 // function in NonlinearSolverSelector.
658 nonlinear_solver.setup_jacobian = setup_jacobian;
659
660 nonlinear_solver.solve_with_jacobian = solve_with_jacobian;
661
662 nonlinear_solver.solve(initial_guess_and_solution);
663#else
665 false, ExcMessage("You do not have Trilinos configured with deal.II"));
666#endif
667 }
668 else if (additional_data.solver_type ==
670 {
671 // Forward to internal function specializations, which throws for
672 // non-supported vector types:
673 solve_with_petsc(initial_guess_and_solution);
674 }
675 else
676 {
677 const std::string solvers = ""
678#ifdef DEAL_II_WITH_SUNDIALS
679 "kinsol\n"
680#endif
681#ifdef DEAL_II_TRILINOS_WITH_NOX
682 "NOX\n"
683#endif
684#ifdef DEAL_II_WITH_PETSC
685 "SNES\n"
686#endif
687 ;
688
689 AssertThrow(false,
691 "Invalid nonlinear solver specified. "
692 "The solvers available in your installation are:\n" +
693 solvers));
694 }
695}
696
698
699#endif
AdditionalData(const SolverType &solver_type=automatic, const SolutionStrategy &strategy=linesearch, const unsigned int maximum_non_linear_iterations=200, const double function_tolerance=1e-8, const double relative_tolerance=1e-5, const double step_tolerance=0.0, const unsigned int anderson_subspace_size=0)
Definition nonlinear.h:513
TrilinosWrappers::NOXSolver< VectorType >::AdditionalData additional_data_nox
Definition nonlinear.h:375
void set_data(const AdditionalData &additional_data)
Definition nonlinear.h:401
void solve_with_petsc(VectorType &initial_guess_and_solution)
Definition nonlinear.h:561
void set_data(const typename PETScWrappers::NonlinearSolverData &additional_data)
SUNDIALS::KINSOL< VectorType >::AdditionalData additional_data_kinsol
Definition nonlinear.h:367
std::function< void(const VectorType &src, VectorType &dst)> residual
Definition nonlinear.h:296
std::function< void(VectorType &)> reinit_vector
Definition nonlinear.h:281
AdditionalData additional_data
Definition nonlinear.h:355
Teuchos::RCP< Teuchos::ParameterList > parameters_nox
Definition nonlinear.h:376
PETScWrappers::NonlinearSolverData additional_data_petsc_snes
Definition nonlinear.h:384
void solve(VectorType &initial_guess_and_solution)
Definition nonlinear.h:600
void select(const typename AdditionalData::SolverType &type)
Definition nonlinear.h:504
std::function< void(const VectorType &current_u)> setup_jacobian
Definition nonlinear.h:324
std::function< void(const VectorType &rhs, VectorType &dst, const double tolerance)> solve_with_jacobian
Definition nonlinear.h:349
std::function< void(const VectorType &src, VectorType &dst)> solve_with_jacobian
Definition petsc_snes.h:445
std::function< void(const VectorType &x, VectorType &res)> residual
Definition petsc_snes.h:387
unsigned int solve(VectorType &x)
std::function< void(const VectorType &x)> setup_jacobian
Definition petsc_snes.h:431
std::function< void(const VectorType &current_u, const VectorType &current_f)> setup_jacobian
Definition kinsol.h:558
unsigned int solve(VectorType &initial_guess_and_solution)
Definition kinsol.cc:231
std::function< void(const VectorType &src, VectorType &dst)> residual
Definition kinsol.h:493
std::function< void(const VectorType &rhs, VectorType &dst, const double tolerance)> solve_with_jacobian
Definition kinsol.h:601
std::function< void(VectorType &)> reinit_vector
Definition kinsol.h:478
std::function< void(const VectorType &y, VectorType &x, const double tolerance)> solve_with_jacobian
Definition nox.h:345
std::function< void(const VectorType &u, VectorType &F)> residual
Definition nox.h:263
unsigned int solve(VectorType &solution)
std::function< void(const VectorType &current_u)> setup_jacobian
Definition nox.h:277
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)