![]() |
Vulpes
0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
|
Crank-Nicolson solver for linear and nonlinear problems of the form. More...
#include <cranknicolson_generalized.hpp>
Classes | |
| struct | Terms |
| Terms requested to the AssembleFunc, nullptr if not needed in the call. More... | |
Public Types | |
| using | real_t = PetscScalar |
| using | ForceFunc = std::function<void(real_t, la::Vector&)> |
| using | LinearizeFunc |
| using | AssembleFunc |
Public Member Functions | |
| CrankNicolsonGeneralized (real_t time_step, real_t initial_time, real_t final_time, const la::SparseMatrix &lin_op, const la::SparseMatrix &mass, const la::Vector &u0, const AssembleFunc &assemble_func, bool nonlinear) | |
| CrankNicolsonGeneralized (real_t time_step, real_t initial_time, real_t final_time, const la::SparseMatrix &lin_op, const la::SparseMatrix &mass, const la::Vector &u0, const ForceFunc &force_func, const LinearizeFunc &linearize_func={}) | |
| void | step () |
| void | setSolverType (la::LinearSolver::Type solver_type, la::LinearSolver::PrecondType pc_type) |
| void | setNearNullSpace (const la::Vector &v) |
| void | setLinearPreconditioner (bool linear_preconditioner) |
| void | setPicard (PetscInt max_iterations, real_t tolerance) |
| PetscInt | lastPicardIterations () const |
| auto & | getSolver () |
| const la::Vector & | getSolution () const |
| const la::Vector & | getUstar () const |
| real_t | time () const |
| real_t | finalTime () const |
Private Member Functions | |
| void | assembleStaticSystem () |
| void | setupSolver (const la::SparseMatrix &lhs) |
| void | assembleTerms (real_t t_force, real_t t_half, const Terms &terms) |
| void | computeForcing () |
| void | solveLinearized () |
Private Attributes | |
| real_t | m_time_step |
| real_t | m_initial_time |
| real_t | m_time |
| real_t | m_final_time |
| PetscInt | m_iter = 0 |
| const la::SparseMatrix & | m_mass |
| const la::SparseMatrix & | m_lin_op |
| la::SparseMatrix | m_LHS0 |
| la::SparseMatrix | m_RHS0 |
| std::unique_ptr< la::SparseMatrix > | m_LHS |
| std::unique_ptr< la::SparseMatrix > | m_nl_matrix |
| la::Vector | m_u |
| la::Vector | m_u_old |
| la::Vector | m_u_oold |
| la::Vector | m_u_star |
| la::Vector | m_u_prev |
| la::Vector * | m_f_old = nullptr |
| la::Vector * | m_f_new = nullptr |
| la::Vector | m_f_1 |
| la::Vector | m_f_2 |
| la::Vector | m_forcing |
| la::Vector | m_nl_vector |
| la::Vector | m_tmp |
| la::Vector | m_rhs |
| la::LinearSolver | m_solver |
| la::LinearSolver::Type | m_solver_type = la::LinearSolver::PrecondOnly |
| la::LinearSolver::PrecondType | m_pc_type = la::LinearSolver::MUMPS |
| bool | m_solver_ready = false |
| bool | m_linear_preconditioner = false |
| PetscInt | m_max_picard_iterations = 0 |
| real_t | m_picard_tolerance = 1e-10 |
| PetscInt | m_picard_iterations = 0 |
| const AssembleFunc | m_assemble_func |
| const bool | m_nonlinear |
Crank-Nicolson solver for linear and nonlinear problems of the form.
M du/dt + A u + N(u) u = g(u) + F(t)
A is the static linear operator (any linear reaction must be added to it, e.g. A - alpha M for Fisher-Kolmogorov), N(u) a solution dependent matrix and g(u) a solution dependent vector. Every step solves
(M + h/2 (A + N*)) u_{n+1} = (M - h/2 (A + N*)) u_{n}
with N* = N(u*), g* = g(u*).
All the terms are assembled by the user through a single AssembleFunc, called once per step (and once per Picard iteration) with the terms needed in that call, so that F(t_{n+1}), N(u*) and g(u*) can be integrated in the same loop on the cells. The requested terms are zeroed before the call and compressed afterwards by the solver. The matrix has the sparsity pattern of M + A, entries outside that pattern are not allowed. Alternatively, a ForceFunc and an optional LinearizeFunc can be passed: they are called one after the other (two loops on the cells).
Algebraic multigrid (e.g. GAMG): stepper.setSolverType(la::LinearSolver::GMRES, la::LinearSolver::GAMG); stepper.getSolver().setOptionsGAMG(data); stepper.setNearNullSpace(v); // e.g. the constant function for DG The LHS matrices are created inside the solver, so the near null space must be set here and not on M or A. They inherit the block size of M (see the aij_block_size of la::SparseMatrix, needed by the point-block smoother). For nonlinear problems the multigrid hierarchy is rebuilt at every solve: DataGAMG::reuse_interpolation keeps the prolongators, while setLinearPreconditioner(true) builds it only once on M + h A / 2.
Assemble the requested terms: force at t_force, nl_matrix and nl_vector at t_half and u_star
| using vulpes::timeintegration::CrankNicolsonGeneralized::ForceFunc = std::function<void(real_t, la::Vector&)> |
| using vulpes::timeintegration::CrankNicolsonGeneralized::real_t = PetscScalar |
| vulpes::timeintegration::CrankNicolsonGeneralized::CrankNicolsonGeneralized | ( | real_t | time_step, |
| real_t | initial_time, | ||
| real_t | final_time, | ||
| const la::SparseMatrix & | lin_op, | ||
| const la::SparseMatrix & | mass, | ||
| const la::Vector & | u0, | ||
| const AssembleFunc & | assemble_func, | ||
| bool | nonlinear ) |
| time_step | step size h |
| initial_time | initial time |
| final_time | final time |
| lin_op | static linear operator A |
| mass | mass matrix M |
| u0 | initial condition |
| assemble_func | assembles F, N and g (see AssembleFunc) |
| nonlinear | false if the AssembleFunc never fills N and g |
| vulpes::timeintegration::CrankNicolsonGeneralized::CrankNicolsonGeneralized | ( | real_t | time_step, |
| real_t | initial_time, | ||
| real_t | final_time, | ||
| const la::SparseMatrix & | lin_op, | ||
| const la::SparseMatrix & | mass, | ||
| const la::Vector & | u0, | ||
| const ForceFunc & | force_func, | ||
| const LinearizeFunc & | linearize_func = {} ) |
|
private |
|
private |
|
private |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Nonlinear problems only: precondition M + h (A + N*) / 2 with the static M + h A / 2, so that the preconditioner is set up once instead of at every solve. Effective when N* is small compared to M + h A / 2 (e.g. a reaction term).
| void vulpes::timeintegration::CrankNicolsonGeneralized::setNearNullSpace | ( | const la::Vector & | v | ) |
Near null space of the LHS matrices, used by the algebraic multigrid preconditioners (see la::SparseMatrix::setNearNullSpace())
|
inline |
Picard iterations per step (0 = semi-implicit scheme) and relative tolerance on the increment ||u^{k+1} - u^k|| / ||u^{k+1}||.
|
inline |
Linear solver used at every step (default: MUMPS direct solve). Other options (tolerances, ...) can be set on getSolver().
|
private |
|
private |
| void vulpes::timeintegration::CrankNicolsonGeneralized::step | ( | ) |
|
inline |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |