Loading [MathJax]/extensions/TeX/newcommand.js
 deal.II version GIT relicensing-3075-gc235bd4825 2025-04-15 08:10:00+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\}}
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
nonlinear.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2023 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
22
24#include <deal.II/lac/vector.h>
25
27
29
30
32
79template <typename VectorType = Vector<double>>
81{
82public:
90 {
91 public:
112
114 {
128 /*
129 * Use the PETSc SNES solver.
130 */
132 };
133
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
162
170
175
183
190 const double relative_tolerance;
191
199
207 };
208
213
222
231
236 void
237 select(const typename AdditionalData::SolverType &type);
238
242 void
244
249#ifdef DEAL_II_TRILINOS_WITH_NOX
250 void
251 set_data(
254 const Teuchos::RCP<Teuchos::ParameterList> &parameters =
255 Teuchos::rcp(new Teuchos::ParameterList));
256#endif
257
262#ifdef DEAL_II_WITH_SUNDIALS
263 void
266#endif
267
272#ifdef DEAL_II_WITH_PETSC
273 void
275#endif
276
283 void
284 solve(VectorType &initial_guess_and_solution);
285
292 std::function<void(VectorType &)> reinit_vector;
293
307 std::function<void(const VectorType &src, VectorType &dst)> residual;
308
335 std::function<void(const VectorType &current_u)> setup_jacobian;
336
358 std::function<
359 void(const VectorType &rhs, VectorType &dst, const double tolerance)>
361
362private:
367
368private:
373
377#ifdef DEAL_II_WITH_SUNDIALS
379#endif
380
384#ifdef DEAL_II_TRILINOS_WITH_NOX
387 Teuchos::RCP<Teuchos::ParameterList> parameters_nox =
388 Teuchos::rcp(new Teuchos::ParameterList);
389#endif
390
394#ifdef DEAL_II_WITH_PETSC
396#endif
397
404 void
405 solve_with_petsc(VectorType &initial_guess_and_solution);
406};
407
408
409
410template <typename VectorType>
411void
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 ==
421 additional_data_kinsol.strategy =
423 else if (additional_data.strategy ==
425 additional_data_kinsol.strategy =
427 else if (additional_data.strategy ==
429 additional_data_kinsol.strategy =
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 ==
460 {
461 additional_data_petsc_snes.snes_type = "anderson";
462 // TODO additional_data.anderson_subspace_size;
463 }
464 else if (additional_data.strategy ==
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 ==
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 ==
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
494template <typename VectorType>
496 : mpi_communicator(MPI_COMM_SELF)
497{}
498
499
500
501template <typename VectorType>
503 const AdditionalData &additional_data)
504 : additional_data(additional_data)
505 , mpi_communicator(MPI_COMM_SELF)
506{
508}
509
510
511
512template <typename VectorType>
514 const AdditionalData &additional_data,
515 const MPI_Comm &mpi_communicator)
516 : additional_data(additional_data)
517 , mpi_communicator(mpi_communicator)
518{
520}
521
522
523
524template <typename VectorType>
525void
527 const typename AdditionalData::SolverType &type)
528{
529 additional_data.solver_type = type;
530}
531
532
533
534template <typename VectorType>
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
555template <typename VectorType>
556void
560 const Teuchos::RCP<Teuchos::ParameterList> &parameters)
561{
563 parameters_nox = parameters;
564}
565#endif
566
567
568
569#ifdef DEAL_II_WITH_SUNDIALS
570template <typename VectorType>
571void
577#endif
578
579
580
581template <typename VectorType>
582void
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
593template <>
594void
596 PETScWrappers::MPI::Vector &initial_guess_and_solution)
597{
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,
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
620template <typename VectorType>
621void
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.
628 {
629#ifdef DEAL_II_WITH_PETSC
631#endif
632#ifdef DEAL_II_TRILINOS_WITH_NOX
634#endif
635#ifdef DEAL_II_WITH_SUNDIALS
637#endif
638
639 // If "auto" is still the solver type we cannot solve the problem
641 AssertThrow(false, ExcMessage("No valid solver type."));
642 }
643
645 {
646#ifdef DEAL_II_WITH_SUNDIALS
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 &current_u,
656 const VectorType & /*current_f*/) {
658 };
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 SUNDIALS configured with deal.II!"));
666#endif
667 }
669 {
670#ifdef DEAL_II_TRILINOS_WITH_NOX
671
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
687 false, ExcMessage("You do not have Trilinos configured with deal.II"));
688#endif
689 }
690 else if (additional_data.solver_type ==
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,
713 "Invalid nonlinear solver specified. "
714 "The solvers available in your installation are:\n" +
715 solvers));
716 }
717}
718
720
721#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:535
TrilinosWrappers::NOXSolver< VectorType >::AdditionalData additional_data_nox
Definition nonlinear.h:386
void set_data(const AdditionalData &additional_data)
Definition nonlinear.h:412
void solve_with_petsc(VectorType &initial_guess_and_solution)
Definition nonlinear.h:583
void set_data(const typename PETScWrappers::NonlinearSolverData &additional_data)
SUNDIALS::KINSOL< VectorType >::AdditionalData additional_data_kinsol
Definition nonlinear.h:378
std::function< void(const VectorType &src, VectorType &dst)> residual
Definition nonlinear.h:307
std::function< void(VectorType &)> reinit_vector
Definition nonlinear.h:292
AdditionalData additional_data
Definition nonlinear.h:366
Teuchos::RCP< Teuchos::ParameterList > parameters_nox
Definition nonlinear.h:387
PETScWrappers::NonlinearSolverData additional_data_petsc_snes
Definition nonlinear.h:395
void solve(VectorType &initial_guess_and_solution)
Definition nonlinear.h:622
void select(const typename AdditionalData::SolverType &type)
Definition nonlinear.h:526
std::function< void(const VectorType &current_u)> setup_jacobian
Definition nonlinear.h:335
std::function< void(const VectorType &rhs, VectorType &dst, const double tolerance)> solve_with_jacobian
Definition nonlinear.h:360
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:559
unsigned int solve(VectorType &initial_guess_and_solution)
Definition kinsol.cc:224
std::function< void(const VectorType &src, VectorType &dst)> residual
Definition kinsol.h:494
std::function< void(const VectorType &rhs, VectorType &dst, const double tolerance)> solve_with_jacobian
Definition kinsol.h:602
std::function< void(VectorType &)> reinit_vector
Definition kinsol.h:479
std::function< void(const VectorType &y, VectorType &x, const double tolerance)> solve_with_jacobian
Definition nox.h:286
std::function< void(const VectorType &u, VectorType &F)> residual
Definition nox.h:204
unsigned int solve(VectorType &solution)
std::function< void(const VectorType &current_u)> setup_jacobian
Definition nox.h:218
#define DEAL_II_DEPRECATED
Definition config.h:280
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:35
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:36
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)