Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
vulpes::timeintegration::CrankNicolsonGeneralized Class Reference

Crank-Nicolson solver for linear and nonlinear problems of the form. More...

#include <cranknicolson_generalized.hpp>

Collaboration diagram for vulpes::timeintegration::CrankNicolsonGeneralized:
[legend]

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

Detailed Description

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}

  • h/2 (F_{n} + F_{n+1}) + h g*

with N* = N(u*), g* = g(u*).

  • linear problem: the LHS is factorized only once;
  • Picard iterations disabled (default): u* is the Adams-Bashforth extrapolation 3/2 u_{n} - 1/2 u_{n-1}, one solve per step;
  • Picard iterations enabled: after the first solve the step is repeated with u* = (u_{n} + u_{n+1}^k) / 2 until convergence (fully implicit CN).

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.

Member Typedef Documentation

◆ AssembleFunc

Initial value:
std::function<void(
real_t t_force,
real_t t_half,
const la::Vector& u_star,
const Terms& terms)>
A wrapper around PETSc Vec for distributed vectors.
Definition vector.hpp:54
PetscScalar real_t
Definition cranknicolson_generalized.hpp:78
Terms requested to the AssembleFunc, nullptr if not needed in the call.
Definition cranknicolson_generalized.hpp:87

Assemble the requested terms: force at t_force, nl_matrix and nl_vector at t_half and u_star

◆ ForceFunc

◆ LinearizeFunc

Initial value:
std::function<void(
real_t t_half,
const la::Vector& u_star,
la::SparseMatrix& nl_matrix,
la::Vector& nl_vector)>
Definition sparse_matrix.hpp:52

◆ real_t

Constructor & Destructor Documentation

◆ CrankNicolsonGeneralized() [1/2]

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 )
Parameters
time_stepstep size h
initial_timeinitial time
final_timefinal time
lin_opstatic linear operator A
massmass matrix M
u0initial condition
assemble_funcassembles F, N and g (see AssembleFunc)
nonlinearfalse if the AssembleFunc never fills N and g

◆ CrankNicolsonGeneralized() [2/2]

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 = {} )

Member Function Documentation

◆ assembleStaticSystem()

void vulpes::timeintegration::CrankNicolsonGeneralized::assembleStaticSystem ( )
private

◆ assembleTerms()

void vulpes::timeintegration::CrankNicolsonGeneralized::assembleTerms ( real_t t_force,
real_t t_half,
const Terms & terms )
private

◆ computeForcing()

void vulpes::timeintegration::CrankNicolsonGeneralized::computeForcing ( )
private

◆ finalTime()

real_t vulpes::timeintegration::CrankNicolsonGeneralized::finalTime ( ) const
inline

◆ getSolution()

const la::Vector & vulpes::timeintegration::CrankNicolsonGeneralized::getSolution ( ) const
inline

◆ getSolver()

auto & vulpes::timeintegration::CrankNicolsonGeneralized::getSolver ( )
inline

◆ getUstar()

const la::Vector & vulpes::timeintegration::CrankNicolsonGeneralized::getUstar ( ) const
inline

◆ lastPicardIterations()

PetscInt vulpes::timeintegration::CrankNicolsonGeneralized::lastPicardIterations ( ) const
inline

◆ setLinearPreconditioner()

void vulpes::timeintegration::CrankNicolsonGeneralized::setLinearPreconditioner ( bool linear_preconditioner)
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).

◆ setNearNullSpace()

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())

◆ setPicard()

void vulpes::timeintegration::CrankNicolsonGeneralized::setPicard ( PetscInt max_iterations,
real_t tolerance )
inline

Picard iterations per step (0 = semi-implicit scheme) and relative tolerance on the increment ||u^{k+1} - u^k|| / ||u^{k+1}||.

◆ setSolverType()

void vulpes::timeintegration::CrankNicolsonGeneralized::setSolverType ( la::LinearSolver::Type solver_type,
la::LinearSolver::PrecondType pc_type )
inline

Linear solver used at every step (default: MUMPS direct solve). Other options (tolerances, ...) can be set on getSolver().

◆ setupSolver()

void vulpes::timeintegration::CrankNicolsonGeneralized::setupSolver ( const la::SparseMatrix & lhs)
private

◆ solveLinearized()

void vulpes::timeintegration::CrankNicolsonGeneralized::solveLinearized ( )
private

◆ step()

void vulpes::timeintegration::CrankNicolsonGeneralized::step ( )

◆ time()

real_t vulpes::timeintegration::CrankNicolsonGeneralized::time ( ) const
inline

Member Data Documentation

◆ m_assemble_func

const AssembleFunc vulpes::timeintegration::CrankNicolsonGeneralized::m_assemble_func
private

◆ m_f_1

la::Vector vulpes::timeintegration::CrankNicolsonGeneralized::m_f_1
private

◆ m_f_2

la::Vector vulpes::timeintegration::CrankNicolsonGeneralized::m_f_2
private

◆ m_f_new

la::Vector* vulpes::timeintegration::CrankNicolsonGeneralized::m_f_new = nullptr
private

◆ m_f_old

la::Vector* vulpes::timeintegration::CrankNicolsonGeneralized::m_f_old = nullptr
private

◆ m_final_time

real_t vulpes::timeintegration::CrankNicolsonGeneralized::m_final_time
private

◆ m_forcing

la::Vector vulpes::timeintegration::CrankNicolsonGeneralized::m_forcing
private

◆ m_initial_time

real_t vulpes::timeintegration::CrankNicolsonGeneralized::m_initial_time
private

◆ m_iter

PetscInt vulpes::timeintegration::CrankNicolsonGeneralized::m_iter = 0
private

◆ m_LHS

std::unique_ptr<la::SparseMatrix> vulpes::timeintegration::CrankNicolsonGeneralized::m_LHS
private

◆ m_LHS0

la::SparseMatrix vulpes::timeintegration::CrankNicolsonGeneralized::m_LHS0
private

◆ m_lin_op

const la::SparseMatrix& vulpes::timeintegration::CrankNicolsonGeneralized::m_lin_op
private

◆ m_linear_preconditioner

bool vulpes::timeintegration::CrankNicolsonGeneralized::m_linear_preconditioner = false
private

◆ m_mass

const la::SparseMatrix& vulpes::timeintegration::CrankNicolsonGeneralized::m_mass
private

◆ m_max_picard_iterations

PetscInt vulpes::timeintegration::CrankNicolsonGeneralized::m_max_picard_iterations = 0
private

◆ m_nl_matrix

std::unique_ptr<la::SparseMatrix> vulpes::timeintegration::CrankNicolsonGeneralized::m_nl_matrix
private

◆ m_nl_vector

la::Vector vulpes::timeintegration::CrankNicolsonGeneralized::m_nl_vector
private

◆ m_nonlinear

const bool vulpes::timeintegration::CrankNicolsonGeneralized::m_nonlinear
private

◆ m_pc_type

la::LinearSolver::PrecondType vulpes::timeintegration::CrankNicolsonGeneralized::m_pc_type = la::LinearSolver::MUMPS
private

◆ m_picard_iterations

PetscInt vulpes::timeintegration::CrankNicolsonGeneralized::m_picard_iterations = 0
private

◆ m_picard_tolerance

real_t vulpes::timeintegration::CrankNicolsonGeneralized::m_picard_tolerance = 1e-10
private

◆ m_rhs

la::Vector vulpes::timeintegration::CrankNicolsonGeneralized::m_rhs
private

◆ m_RHS0

la::SparseMatrix vulpes::timeintegration::CrankNicolsonGeneralized::m_RHS0
private

◆ m_solver

la::LinearSolver vulpes::timeintegration::CrankNicolsonGeneralized::m_solver
private

◆ m_solver_ready

bool vulpes::timeintegration::CrankNicolsonGeneralized::m_solver_ready = false
private

◆ m_solver_type

la::LinearSolver::Type vulpes::timeintegration::CrankNicolsonGeneralized::m_solver_type = la::LinearSolver::PrecondOnly
private

◆ m_time

real_t vulpes::timeintegration::CrankNicolsonGeneralized::m_time
private

◆ m_time_step

real_t vulpes::timeintegration::CrankNicolsonGeneralized::m_time_step
private

◆ m_tmp

la::Vector vulpes::timeintegration::CrankNicolsonGeneralized::m_tmp
private

◆ m_u

la::Vector vulpes::timeintegration::CrankNicolsonGeneralized::m_u
private

◆ m_u_old

la::Vector vulpes::timeintegration::CrankNicolsonGeneralized::m_u_old
private

◆ m_u_oold

la::Vector vulpes::timeintegration::CrankNicolsonGeneralized::m_u_oold
private

◆ m_u_prev

la::Vector vulpes::timeintegration::CrankNicolsonGeneralized::m_u_prev
private

◆ m_u_star

la::Vector vulpes::timeintegration::CrankNicolsonGeneralized::m_u_star
private

The documentation for this class was generated from the following files: