18 #ifdef D2K_WITH_SUNDIALS 23 #ifdef DEAL_II_WITH_TRILINOS 28 #ifdef DEAL_II_WITH_PETSC 40 #ifdef DEAL_II_WITH_MPI 41 #include <nvector/nvector_parallel.h> 49 #ifdef DEAL_II_WITH_MPI 50 template <
typename VEC>
55 kinsol(
"KINSOL for IMEX",comm),
62 template <
typename VEC>
72 template <
typename VEC>
84 template <
typename VEC>
90 template <
typename VEC>
95 "Conditional statement can be used as well:\n" 99 "Absolute error tolerance",
"1e-6",
103 "Relative error tolerance",
"1e-5",
107 "Initial time",
"0.0",
115 "Intervals between outputs",
"1",
119 "Maximum number of outer nonlinear iterations",
"5",
121 "At each outer iteration the Jacobian is updated if it is set that the \n" 122 "Jacobian is continuously updated and a cycle of inner iterations is \n" 126 "Maximum number of inner nonlinear iterations",
"3",
128 "At each inner iteration the Jacobian is NOT updated.");
131 "Newton relaxation parameter",
"1",
135 "Update continuously Jacobian",
"true",
139 "Number of elements in backtracking sequence",
"5",
141 "In the line seach method with backtracking the following alphas are\n" 142 "tested: 1, 1/2, 1/4,..., 2^-i. This parameter sets the maximum i.");
144 "Method used",
"fixed_alpha",
146 "Fixed alpha means that the parsed alpha is used in each Newton iteration\n" 147 "LS_backtracking is the line search with backtracking method.");
150 "Print useful informations",
"false",
154 "Use the KINSOL solver",
"true",
159 template <
typename VEC>
167 template <
typename VEC>
170 unsigned int step_number = 0;
190 std::function<int(const VEC &, VEC &)> my_residual = [&] (
const VEC &y, VEC &res)
194 int ret = this->
residual(t,y,solution_dot,res);
200 std::function<int(const VEC &)> my_jac = [&] (
const VEC &y)
207 std::function<int(const VEC &, VEC &)> my_solve = [&] (
const VEC &, VEC &dst)
216 kinsol.residual = my_residual;
217 kinsol.setup_jacobian = my_jac;
218 kinsol.solve_linear_system = my_solve;
224 bool update_Jacobian =
true;
227 (void) update_Jacobian;
231 output_step(t, solution, solution_dot, step_number);
239 kinsol.initialize_solver(solution);
250 pcout <<
"Solving for t = " << t
257 compute_y_dot(solution,*previous_solution,alpha,solution_dot);
260 do_newton(t,alpha,update_Jacobian,*previous_solution,solution,solution_dot);
277 kinsol.initialize_solver(solution);
295 output_step(t, solution, solution_dot, step_number);
304 *previous_solution = solution;
313 template <
typename VEC>
316 const VEC &previous_solution,
326 *first_trial = solution;
327 double n_alpha = 1.0;
329 first_trial->sadd(1.0, n_alpha, update);
331 solution_dot = *first_trial;
332 solution_dot -= previous_solution;
333 solution_dot *= alpha;
335 this->
residual(t, *first_trial, solution_dot, *first_residual);
337 double first_res_norm =
vector_norm(*first_residual);
344 *second_trial = solution;
346 n_alpha = 1.0/(std::pow(2.0,i));
348 second_trial->sadd(1.0, n_alpha, update);
350 solution_dot = *second_trial;
351 solution_dot -= previous_solution;
352 solution_dot *= alpha;
354 this->
residual(t, *second_trial, solution_dot, *second_residual);
355 double second_res_norm =
vector_norm(*second_residual);
356 if (first_res_norm < second_res_norm)
358 solution = *first_trial;
359 residual = *first_residual;
361 solution_dot = *first_trial;
362 solution_dot -= previous_solution;
363 solution_dot *= alpha;
364 n_alpha = 1.0/(std::pow(2.0,i-1));
370 *first_trial = *second_trial;
371 *first_residual = *second_residual;
372 first_res_norm = second_res_norm;
376 solution = *second_trial;
377 residual = *second_residual;
386 template <
typename VEC>
390 const bool update_Jacobian,
391 const VEC &previous_solution,
403 unsigned int inner_iter = 0;
404 unsigned int outer_iter = 0;
405 unsigned int nonlin_iter = 0;
406 this->
residual(t, solution, solution_dot, *res);
407 double res_norm = 0.0;
408 double solution_norm = 0.0;
421 if (update_Jacobian ==
true)
432 res_norm >
rel_tol*solution_norm)
444 if (
method ==
"LS_backtracking")
454 else if (
method ==
"fixed_alpha")
461 compute_y_dot(solution,previous_solution,alpha, solution_dot);
476 << nonlin_iter + inner_iter
478 << std::setw(19) << std::scientific << res_norm
480 << std::setw(19) << std::scientific << solution_norm
481 <<
" solution norm\n" 483 <<
" newton alpha\n\n" 492 << nonlin_iter + inner_iter
494 << std::setw(19) << std::scientific << res_norm
497 <<
" newton alpha\n\n" 501 this->
residual(t,solution,solution_dot,*res);
504 nonlin_iter += inner_iter;
506 if (std::fabs(res_norm) <
abs_tol ||
507 std::fabs(res_norm) <
rel_tol*solution_norm)
511 << std::setw(19) << std::scientific << res_norm
514 <<
" iterations)\n\n" 522 << std::setw(19) << std::scientific << res_norm
523 <<
" (not converged in " 524 << std::setw(3) << nonlin_iter
525 <<
" iterations)\n\n" 528 ExcMessage (
"No convergence in nonlinear solver"));
534 res_norm >
rel_tol*solution_norm);
537 template <
typename VEC>
547 prev /= (-1.0*alpha);
558 template <
typename VEC>
561 std::string variables =
"t";
562 std::map<std::string,double> constants;
571 double result = fp.
value(time);
575 template <
typename VEC>
586 *first_guess_dot = y_dot;
596 do_newton(t,0.0,
true,*previous_solution,y,y_dot);
599 *update -= *first_guess;
602 *first_guess_dot += *update;
603 y_dot = *first_guess_dot;
609 template<
typename VEC>
682 return vector.l2_norm();
688 template class deal2lkit::IMEXStepper<BlockVector<double> >;
690 #ifdef DEAL_II_WITH_MPI 692 #ifdef DEAL_II_WITH_TRILINOS 693 template class deal2lkit::IMEXStepper<TrilinosWrappers::MPI::Vector>;
694 template class deal2lkit::IMEXStepper<TrilinosWrappers::MPI::BlockVector>;
697 #ifdef DEAL_II_WITH_PETSC 698 template class deal2lkit::IMEXStepper<PETScWrappers::MPI::Vector>;
699 template class deal2lkit::IMEXStepper<PETScWrappers::MPI::BlockVector>;
double line_search_with_backtracking(const VEC &update, const VEC &prev_sol, const double &alpha, const double &t, VEC &sol, VEC &sol_dot, VEC &residual)
line search algorithm with backtracking.The following sequence of Newton relaxation parameters is tes...
unsigned int max_outer_non_linear_iterations
Maximum number of outer iterations for Newton method.
static::ExceptionBase & ExcPureFunctionCalled()
bool update_jacobian_continuously
Jacobian is updated at each outer iteration and time step.
std::function< int(const VEC &src, VEC &dst)> jacobian_vmult
Compute the matrix-vector product Jacobian times src, and the result is put in dst.
double abs_tol
Absolute error tolerance for non linear iterations.
A parameter acceptor base class.
ConditionalOStream pcout
Output stream.
virtual void declare_parameters(ParameterHandler &prm)
Declare parameters for this class to function properly.
void set_functions_to_trigger_an_assert()
Set the std::functions above to trigger an assert if they are not implemented.
void do_newton(const double t, const double alpha, const bool update_Jacobian, const VEC &previous_solution, VEC &solution, VEC &solution_dot)
find solution applying the newton method with given
KINSOLInterface< VEC > kinsol
kinsol solver
void set_initial_time(const double &t)
Set initial time equal to t disregarding what is written in the parameter file.
std::string method
method used for alpha selection
static ParameterHandler prm
Static parameter.
bool verbose
print useful informations
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
bool use_kinsol
use kinsol solver true or false
unsigned int max_inner_non_linear_iterations
Maximum number of inner iterations for Newton method.
std::function< VEC &()> get_lumped_mass_matrix
Return the lumped mass matrix vector.
unsigned int solve_dae(VEC &solution, VEC &solution_dot)
Evolve.
#define AssertThrow(cond, exc)
void compute_y_dot(const VEC &y, const VEC &prev, const double alpha, VEC &y_dot)
double step_size
Step size.
std::function< void(const double t, const VEC &sol, const VEC &sol_dot, const unsigned int step_number)> output_step
Store solutions to file.
std::function< int(const VEC &rhs, VEC &dst)> solve_jacobian_system
Solve linear system.
std::string _step_size
user defined step_size
static::ExceptionBase & ExcMessage(std::string arg1)
Epetra_Comm * duplicate_communicator(const Epetra_Comm &communicator)
ConditionalOStream pcout
Output stream.
unsigned int output_period
Seconds between each output.
double final_time
Final time.
double rel_tol
Relative error tolerance for non linear iterations.
void compute_consistent_initial_conditions(const double &t, VEC &y, VEC &y_dot)
compute_consistent_initial_conditions
virtual double value(const Point< dim > &p, const unsigned int component=0) const
void compute_previous_solution(const VEC &sol, const VEC &sol_dot, const double &alpha, VEC &prev)
compute previous solution from given
IMEXStepper(std::string &name="")
Constructor for the IMEXStepper class.
std::function< bool(const double t, VEC &sol, VEC &sol_dot)> solver_should_restart
Evaluate wether the mesh should be refined or not.
std::function< int(const double t, const VEC &y, const VEC &y_dot, VEC &res)> residual
Compute residual.
double get_alpha() const
if initial time is different from final time (i.e., we are solving a time-dep problem and not a stati...
void initialize(const std::string &vars, const std::vector< std::string > &expressions, const ConstMap &constants, const bool time_dependent=false)
std::function< int(const double t, const VEC &y, const VEC &y_dot, const double alpha)> setup_jacobian
Compute Jacobian.
double evaluate_step_size(const double &t)
evaluate step size at time t according to the expression stored in _step_size
unsigned int n_max_backtracking
i max
void set_functions_to_trigger_an_assert()
This function is executed at construction time to set the std::function above to trigger an assert if...
unsigned int this_mpi_process(const MPI_Comm &mpi_communicator)
double initial_time
Initial time for the ode.
void add_parameter(ParameterHandler &prm, T *parameter, const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
Add a parameter the given parameter list.
std::function< shared_ptr< VEC >)> create_new_vector
Return a shared_ptr<VEC>.
std::function< double(const VEC &vector)> vector_norm
Return the norm of vector.
double newton_alpha
Alpha to use in Newton method for the update of the solution.