39 #ifndef GETFEM_MODEL_SOLVERS_H__
40 #define GETFEM_MODEL_SOLVERS_H__
53 template <
typename T>
struct sort_abs_val_
54 {
bool operator()(T x, T y) {
return (gmm::abs(x) < gmm::abs(y)); } };
56 template <
typename MAT>
void print_eigval(
const MAT &M) {
58 typedef typename gmm::linalg_traits<MAT>::value_type T;
60 gmm::dense_matrix<T> MM(n, n), Q(n, n);
61 std::vector<T> eigval(n);
64 gmm::implicit_qr_algorithm(MM, eigval, Q);
65 std::sort(eigval.begin(), eigval.end(), sort_abs_val_<T>());
66 cout <<
"eival = " << eigval << endl;
80 template <
typename MAT,
typename VECT>
81 struct abstract_linear_solver {
84 virtual void operator ()(
const MAT &, VECT &,
const VECT &,
86 virtual ~abstract_linear_solver() {}
89 template <
typename MAT,
typename VECT>
90 struct linear_solver_cg_preconditioned_ildlt
91 :
public abstract_linear_solver<MAT, VECT> {
92 void operator ()(
const MAT &M, VECT &x,
const VECT &b,
95 gmm::cg(M, x, b, P, iter);
96 if (!iter.converged()) GMM_WARNING2(
"cg did not converge!");
100 template <
typename MAT,
typename VECT>
101 struct linear_solver_gmres_preconditioned_ilu
102 :
public abstract_linear_solver<MAT, VECT> {
103 void operator ()(
const MAT &M, VECT &x,
const VECT &b,
107 if (!iter.converged()) GMM_WARNING2(
"gmres did not converge!");
111 template <
typename MAT,
typename VECT>
112 struct linear_solver_gmres_unpreconditioned
113 :
public abstract_linear_solver<MAT, VECT> {
114 void operator ()(
const MAT &M, VECT &x,
const VECT &b,
116 gmm::identity_matrix P;
118 if (!iter.converged()) GMM_WARNING2(
"gmres did not converge!");
122 template <
typename MAT,
typename VECT>
123 struct linear_solver_gmres_preconditioned_ilut
124 :
public abstract_linear_solver<MAT, VECT> {
125 void operator ()(
const MAT &M, VECT &x,
const VECT &b,
129 if (!iter.converged()) GMM_WARNING2(
"gmres did not converge!");
133 template <
typename MAT,
typename VECT>
134 struct linear_solver_gmres_preconditioned_ilutp
135 :
public abstract_linear_solver<MAT, VECT> {
136 void operator ()(
const MAT &M, VECT &x,
const VECT &b,
140 if (!iter.converged()) GMM_WARNING2(
"gmres did not converge!");
144 #if defined(GMM_USES_SUPERLU)
145 template <
typename MAT,
typename VECT>
146 struct linear_solver_superlu
147 :
public abstract_linear_solver<MAT, VECT> {
148 void operator ()(
const MAT &M, VECT &x,
const VECT &b,
154 int info = gmm::SuperLU_solve(M, x, b, rcond);
155 iter.enforce_converged(info == 0);
156 if (iter.get_noisy()) cout <<
"condition number: " << 1.0/rcond<< endl;
161 template <
typename MAT,
typename VECT>
162 struct linear_solver_dense_lu :
public abstract_linear_solver<MAT, VECT> {
163 void operator ()(
const MAT &M, VECT &x,
const VECT &b,
165 typedef typename gmm::linalg_traits<MAT>::value_type T;
166 gmm::dense_matrix<T> MM(gmm::mat_nrows(M),gmm::mat_ncols(M));
170 gmm::lapack_ipvt ipvt(gmm::mat_nrows(MM));
175 GMM_WARNING1(
"dense LU solve failed: singular matrix (pivot " << info <<
")");
176 iter.enforce_converged(info == 0);
180 #if defined(GMM_USES_MUMPS)
181 template <
typename MAT,
typename VECT>
182 struct linear_solver_mumps :
public abstract_linear_solver<MAT, VECT> {
183 void operator ()(
const MAT &M, VECT &x,
const VECT &b,
185 bool ok = gmm::MUMPS_solve(M, x, b,
false);
186 iter.enforce_converged(ok);
189 template <
typename MAT,
typename VECT>
190 struct linear_solver_mumps_sym :
public abstract_linear_solver<MAT, VECT> {
191 void operator ()(
const MAT &M, VECT &x,
const VECT &b,
193 bool ok = gmm::MUMPS_solve(M, x, b,
true);
194 iter.enforce_converged(ok);
199 #if GETFEM_PARA_LEVEL > 1 && GETFEM_PARA_SOLVER == MUMPS_PARA_SOLVER
200 template <
typename MAT,
typename VECT>
201 struct linear_solver_distributed_mumps
202 :
public abstract_linear_solver<MAT, VECT> {
203 void operator ()(
const MAT &M, VECT &x,
const VECT &b,
205 double tt_ref=MPI_Wtime();
206 bool ok = MUMPS_distributed_matrix_solve(M, x, b,
false);
207 iter.enforce_converged(ok);
208 if (MPI_IS_MASTER()) cout<<
"UNSYMMETRIC MUMPS time "<< MPI_Wtime() - tt_ref<<endl;
212 template <
typename MAT,
typename VECT>
213 struct linear_solver_distributed_mumps_sym
214 :
public abstract_linear_solver<MAT, VECT> {
215 void operator ()(
const MAT &M, VECT &x,
const VECT &b,
217 double tt_ref=MPI_Wtime();
218 bool ok = MUMPS_distributed_matrix_solve(M, x, b,
true);
219 iter.enforce_converged(ok);
220 if (MPI_IS_MASTER()) cout<<
"SYMMETRIC MUMPS time "<< MPI_Wtime() - tt_ref<<endl;
230 struct abstract_newton_line_search {
231 double conv_alpha, conv_r;
232 size_t it, itmax, glob_it;
234 virtual void init_search(
double r,
size_t git,
double R0 = 0.0) = 0;
235 virtual double next_try(
void) = 0;
236 virtual bool is_converged(
double,
double R1 = 0.0) = 0;
237 virtual double converged_value(
void) {
241 virtual double converged_residual(
void) {
return conv_r; };
242 virtual ~abstract_newton_line_search() { }
246 struct newton_search_with_step_control :
public abstract_newton_line_search {
248 virtual void init_search(
double ,
size_t ,
double = 0.0)
249 { GMM_ASSERT1(
false,
"Not to be used"); }
251 virtual double next_try(
void)
252 { GMM_ASSERT1(
false,
"Not to be used"); }
254 virtual bool is_converged(
double ,
double = 0.0)
255 { GMM_ASSERT1(
false,
"Not to be used"); }
257 newton_search_with_step_control() {}
261 struct quadratic_newton_line_search :
public abstract_newton_line_search {
264 double alpha, first_res;
265 virtual void init_search(
double r,
size_t git,
double R0 = 0.0) {
266 GMM_ASSERT1(R0 != 0.0,
"You have to specify R0");
268 conv_alpha =
alpha = double(1); conv_r = first_res = r; it = 0;
271 virtual double next_try(
void) {
273 if (it == 1)
return double(1);
274 GMM_ASSERT1(R1_ != 0.0,
"You have to specify R1");
275 double a = R0_ / R1_;
276 return (a < 0) ? (a*0.5 + sqrt(a*a*0.25-a)) : a*0.5;
278 virtual bool is_converged(
double r,
double R1 = 0.0) {
280 R1_ = R1;
return ((gmm::abs(R1_) < gmm::abs(R0_*0.5)) || it >= itmax);
282 quadratic_newton_line_search(
size_t imax =
size_t(-1)) { itmax = imax; }
286 struct simplest_newton_line_search :
public abstract_newton_line_search {
287 double alpha, alpha_mult, first_res, alpha_max_ratio, alpha_min,
289 virtual void init_search(
double r,
size_t git,
double = 0.0) {
291 conv_alpha =
alpha = double(1); conv_r = first_res = r; it = 0;
293 virtual double next_try(
void)
294 { conv_alpha =
alpha;
alpha *= alpha_mult; ++it;
return conv_alpha; }
295 virtual bool is_converged(
double r,
double = 0.0) {
297 return ((it <= 1 && r < first_res)
298 || (r <= first_res * alpha_max_ratio && r <= alpha_threshold_res)
299 || (conv_alpha <= alpha_min && r < first_res * 1e5)
302 simplest_newton_line_search
303 (
size_t imax =
size_t(-1),
double a_max_ratio = 6.0/5.0,
304 double a_min = 1.0/1000.0,
double a_mult = 3.0/5.0,
305 double a_threshold_res = 1e50)
306 : alpha_mult(a_mult), alpha_max_ratio(a_max_ratio), alpha_min(a_min),
307 alpha_threshold_res(a_threshold_res)
311 struct default_newton_line_search :
public abstract_newton_line_search {
329 double alpha, alpha_old, alpha_mult, first_res, alpha_max_ratio;
330 double alpha_min_ratio, alpha_min;
332 bool max_ratio_reached;
333 double alpha_max_ratio_reached, r_max_ratio_reached;
336 virtual void init_search(
double r,
size_t git,
double = 0.0);
337 virtual double next_try(
void);
338 virtual bool is_converged(
double r,
double = 0.0);
339 default_newton_line_search(
void) { count_pat = 0; }
343 struct basic_newton_line_search :
public abstract_newton_line_search {
344 double alpha, alpha_mult, first_res, alpha_max_ratio;
345 double alpha_min, prev_res, alpha_max_augment;
346 virtual void init_search(
double r,
size_t git,
double = 0.0) {
348 conv_alpha =
alpha = double(1);
349 prev_res = conv_r = first_res = r; it = 0;
351 virtual double next_try(
void)
352 { conv_alpha =
alpha;
alpha *= alpha_mult; ++it;
return conv_alpha; }
353 virtual bool is_converged(
double r,
double = 0.0) {
354 if (glob_it == 0 || (r < first_res /
double(2))
355 || (conv_alpha <= alpha_min && r < first_res * alpha_max_augment)
357 { conv_r = r;
return true; }
358 if (it > 1 && r > prev_res && prev_res < alpha_max_ratio * first_res)
360 prev_res = conv_r = r;
363 basic_newton_line_search
364 (
size_t imax =
size_t(-1),
365 double a_max_ratio = 5.0/3.0,
366 double a_min = 1.0/1000.0,
double a_mult = 3.0/5.0,
double a_augm = 2.0)
367 : alpha_mult(a_mult), alpha_max_ratio(a_max_ratio),
368 alpha_min(a_min), alpha_max_augment(a_augm) { itmax = imax; }
372 struct systematic_newton_line_search :
public abstract_newton_line_search {
373 double alpha, alpha_mult, first_res;
374 double alpha_min, prev_res;
376 virtual void init_search(
double r,
size_t git,
double = 0.0) {
378 conv_alpha =
alpha = double(1);
379 prev_res = conv_r = first_res = r; it = 0; first =
true;
381 virtual double next_try(
void)
382 {
double a =
alpha;
alpha *= alpha_mult; ++it;
return a; }
383 virtual bool is_converged(
double r,
double = 0.0) {
385 if (r < conv_r || first)
386 { conv_r = r; conv_alpha =
alpha / alpha_mult; first =
false; }
387 if ((alpha <= alpha_min*alpha_mult) || it >= itmax)
return true;
390 systematic_newton_line_search
391 (
size_t imax =
size_t(-1),
392 double a_min = 1.0/10000.0,
double a_mult = 3.0/5.0)
393 : alpha_mult(a_mult), alpha_min(a_min) { itmax = imax; }
421 template <
typename PB>
424 typedef typename gmm::linalg_traits<typename PB::VECTOR>::value_type T;
425 typedef typename gmm::number_traits<T>::magnitude_type R;
427 iter_linsolv0.reduce_noisy();
428 iter_linsolv0.set_resmax(iter.get_resmax()/20.0);
429 iter_linsolv0.set_maxiter(10000);
431 pb.compute_residual();
432 R approx_eln = pb.approx_external_load_norm();
433 if (approx_eln == R(0)) approx_eln = R(1);
435 typename PB::VECTOR b0(gmm::vect_size(pb.residual()));
437 typename PB::VECTOR Xi(gmm::vect_size(pb.residual()));
440 typename PB::VECTOR dr(gmm::vect_size(pb.residual()));
442 R alpha0(0),
alpha(1), res0(gmm::vect_norm1(b0)), minres(res0);
449 scalar_type crit = pb.residual_norm() / approx_eln;
450 if (iter.finished(crit))
return;
455 if (!iter.converged(crit)) {
459 while (is_singular) {
461 pb.add_to_residual(b0, alpha-R(1));
463 if (iter.get_noisy() > 1)
464 cout <<
"starting tangent matrix computation" << endl;
465 pb.compute_tangent_matrix();
466 if (iter.get_noisy() > 1)
467 cout <<
"starting linear solver" << endl;
468 pb.linear_solve(dr, iter_linsolv);
469 if (!iter_linsolv.converged()) {
471 if (is_singular <= 4) {
472 if (iter.get_noisy())
473 cout <<
"Singular tangent matrix:"
474 " perturbation of the state vector." << endl;
476 pb.compute_residual();
478 if (iter.get_noisy())
479 cout <<
"Singular tangent matrix: perturbation failed, "
480 <<
"aborting." << endl;
484 else is_singular = 0;
486 if (iter.get_noisy() > 1) cout <<
"linear solver done" << endl;
489 pb.compute_residual();
491 R dec = R(1)/R(2), coeff2 = coeff * R(1.5);
493 while (dec > R(1E-5) && res >= res0 * coeff) {
494 gmm::add(gmm::scaled(dr, -dec), pb.state_vector());
495 pb.compute_residual();
497 if (res2 < res*R(0.95) || res2 >= res0 * coeff2) {
498 dec /= R(2); res = res2; coeff2 *= R(1.5);
500 gmm::add(gmm::scaled(dr, dec), pb.state_vector());
507 coeff = std::max(R(1.05), coeff*R(0.93));
508 bool near_end = (iter.get_iteration() > iter.get_maxiter()/2);
509 bool cut = (
alpha < R(1)) && near_end;
510 if ((res > minres && nit > 4) || cut) {
513 if ((stagn > 10 && alpha > alpha0 + R(5E-2)) || cut) {
515 if (near_end)
alpha = R(1);
517 pb.compute_residual();
520 stagn = 0; coeff = R(2);
523 if (res < minres || (alpha == R(1) && nit < 10)) stagn = 0;
525 if (nit < 5) minres = res0;
else minres = std::min(minres, res0);
527 if (iter.get_noisy())
528 cout <<
"step control [" << std::setw(8) << alpha0 <<
","
529 << std::setw(8) <<
alpha <<
"," << std::setw(10) << dec <<
"]";
533 crit = res / approx_eln;
536 if (iter.finished(crit)) {
537 if (iter.converged() && alpha < R(1)) {
539 alpha = std::min(R(1), alpha*R(3) - alpha0*R(2));
542 nit = 0; stagn = 0; coeff = R(2);
554 template <
typename PB>
557 typedef typename gmm::linalg_traits<typename PB::VECTOR>::value_type T;
558 typedef typename gmm::number_traits<T>::magnitude_type R;
560 iter_linsolv0.reduce_noisy();
561 iter_linsolv0.set_resmax(iter.get_resmax()/20.0);
562 iter_linsolv0.set_maxiter(10000);
564 pb.compute_residual();
565 R approx_eln = pb.approx_external_load_norm();
566 if (approx_eln == R(0)) approx_eln = R(1);
568 typename PB::VECTOR dr(gmm::vect_size(pb.residual()));
570 scalar_type crit = pb.residual_norm() / approx_eln;
571 while (!iter.finished(crit)) {
575 while (is_singular) {
578 if (iter.get_noisy() > 1)
579 cout <<
"starting computing tangent matrix" << endl;
580 pb.compute_tangent_matrix();
581 if (iter.get_noisy() > 1)
582 cout <<
"starting linear solver" << endl;
583 pb.linear_solve(dr, iter_linsolv);
584 if (!iter_linsolv.converged()) {
586 if (is_singular <= 4) {
587 if (iter.get_noisy())
588 cout <<
"Singular tangent matrix:"
589 " perturbation of the state vector." << endl;
591 pb.compute_residual();
593 if (iter.get_noisy())
594 cout <<
"Singular tangent matrix: perturbation failed, aborting."
599 else is_singular = 0;
602 if (iter.get_noisy() > 1) cout <<
"linear solver done" << endl;
603 R
alpha = pb.line_search(dr, iter);
605 if (iter.get_noisy()) cout <<
"alpha = " << std::setw(6) <<
alpha <<
" ";
607 crit = std::min(pb.residual_norm() / approx_eln,
608 gmm::vect_norm1(dr) / std::max(1E-25, pb.state_norm()));
618 typedef std::shared_ptr<abstract_linear_solver<model_real_sparse_matrix,
619 model_real_plain_vector> >
620 rmodel_plsolver_type;
621 typedef std::shared_ptr<abstract_linear_solver<model_complex_sparse_matrix,
622 model_complex_plain_vector> >
623 cmodel_plsolver_type;
625 template<
typename MATRIX,
typename VECTOR>
626 std::shared_ptr<abstract_linear_solver<MATRIX, VECTOR>>
627 default_linear_solver(
const model &md) {
629 #if GETFEM_PARA_LEVEL == 1 && GETFEM_PARA_SOLVER == MUMPS_PARA_SOLVER
630 if (md.is_symmetric())
631 return std::make_shared<linear_solver_mumps_sym<MATRIX, VECTOR>>();
633 return std::make_shared<linear_solver_mumps<MATRIX, VECTOR>>();
634 #elif GETFEM_PARA_LEVEL > 1 && GETFEM_PARA_SOLVER == MUMPS_PARA_SOLVER
635 if (md.is_symmetric())
636 return std::make_shared
637 <linear_solver_distributed_mumps_sym<MATRIX, VECTOR>>();
639 return std::make_shared
640 <linear_solver_distributed_mumps<MATRIX, VECTOR>>();
642 size_type ndof = md.nb_dof(), max3d = 15000, dim = md.leading_dimension();
643 # if defined(GMM_USES_MUMPS)
646 if ((ndof<300000 && dim<=2) || (ndof<max3d && dim<=3) || (ndof<1000)) {
647 # if defined(GMM_USES_MUMPS)
648 if (md.is_symmetric())
649 return std::make_shared<linear_solver_mumps_sym<MATRIX, VECTOR>>();
651 return std::make_shared<linear_solver_mumps<MATRIX, VECTOR>>();
652 # elif defined(GMM_USES_SUPERLU)
653 return std::make_shared<linear_solver_superlu<MATRIX, VECTOR>>();
656 "At least one direct solver (MUMPS or SuperLU) is required");
660 if (md.is_coercive())
661 return std::make_shared
662 <linear_solver_cg_preconditioned_ildlt<MATRIX, VECTOR>>();
665 return std::make_shared
666 <linear_solver_gmres_preconditioned_ilut<MATRIX,VECTOR>>();
668 return std::make_shared
669 <linear_solver_gmres_preconditioned_ilu<MATRIX,VECTOR>>();
673 return std::shared_ptr<abstract_linear_solver<MATRIX, VECTOR>>();
676 template <
typename MATRIX,
typename VECTOR>
677 std::shared_ptr<abstract_linear_solver<MATRIX, VECTOR>>
678 select_linear_solver(
const model &md,
const std::string &name) {
679 std::shared_ptr<abstract_linear_solver<MATRIX, VECTOR>> p;
680 if (bgeot::casecmp(name,
"superlu") == 0) {
681 #if defined(GMM_USES_SUPERLU)
682 return std::make_shared<linear_solver_superlu<MATRIX, VECTOR>>();
684 GMM_ASSERT1(
false,
"SuperLU is not interfaced");
687 else if (bgeot::casecmp(name,
"dense_lu") == 0)
688 return std::make_shared<linear_solver_dense_lu<MATRIX, VECTOR>>();
689 else if (bgeot::casecmp(name,
"mumps") == 0) {
690 #if defined(GMM_USES_MUMPS)
691 # if GETFEM_PARA_LEVEL <= 1
692 return std::make_shared<linear_solver_mumps<MATRIX, VECTOR>>();
694 return std::make_shared
695 <linear_solver_distributed_mumps<MATRIX, VECTOR>>();
698 GMM_ASSERT1(
false,
"Mumps is not interfaced");
701 else if (bgeot::casecmp(name,
"cg/ildlt") == 0)
702 return std::make_shared
703 <linear_solver_cg_preconditioned_ildlt<MATRIX, VECTOR>>();
704 else if (bgeot::casecmp(name,
"gmres/ilu") == 0)
705 return std::make_shared
706 <linear_solver_gmres_preconditioned_ilu<MATRIX, VECTOR>>();
707 else if (bgeot::casecmp(name,
"gmres/ilut") == 0)
708 return std::make_shared
709 <linear_solver_gmres_preconditioned_ilut<MATRIX, VECTOR>>();
710 else if (bgeot::casecmp(name,
"gmres/ilutp") == 0)
711 return std::make_shared
712 <linear_solver_gmres_preconditioned_ilutp<MATRIX, VECTOR>>();
713 else if (bgeot::casecmp(name,
"auto") == 0)
714 return default_linear_solver<MATRIX, VECTOR>(md);
716 GMM_ASSERT1(
false,
"Unknown linear solver");
717 return std::shared_ptr<abstract_linear_solver<MATRIX, VECTOR>>();
720 inline rmodel_plsolver_type rselect_linear_solver(
const model &md,
721 const std::string &name) {
722 return select_linear_solver<model_real_sparse_matrix,
723 model_real_plain_vector>(md, name);
726 inline cmodel_plsolver_type cselect_linear_solver(
const model &md,
727 const std::string &name) {
728 return select_linear_solver<model_complex_sparse_matrix,
729 model_complex_plain_vector>(md, name);
760 rmodel_plsolver_type lsolver,
761 abstract_newton_line_search &ls);
764 cmodel_plsolver_type lsolver,
765 abstract_newton_line_search &ls);
768 rmodel_plsolver_type lsolver);
771 cmodel_plsolver_type lsolver);
Incomplete Level 0 LDLT Preconditioner.
Incomplete LU without fill-in Preconditioner.
Incomplete LU with threshold and K fill-in Preconditioner.
ILUTP: Incomplete LU with threshold and K fill-in Preconditioner and column pivoting.
The Iteration object calculates whether the solution has reached the desired accuracy,...
Model representation in Getfem.
Interface with MUMPS (LU direct solver for sparse matrices).
void copy(const L1 &l1, L2 &l2)
*/
number_traits< typename linalg_traits< V1 >::value_type >::magnitude_type vect_dist1(const V1 &v1, const V2 &v2)
1-distance between two vectors
void clear(L &l)
clear (fill with zeros) a vector or matrix.
void add(const L1 &l1, L2 &l2)
*/
size_type lu_factor(DenseMatrix &A, Pvector &ipvt)
LU Factorization of a general (dense) matrix (real or complex).
void lu_solve(const DenseMatrix &LU, const Pvector &pvector, VectorX &x, const VectorB &b)
LU Solve : Solve equation Ax=b, given an LU factored matrix.
Include standard gmm iterative solvers (cg, gmres, ...)
void gmres(const Mat &A, Vec &x, const VecB &b, const Precond &M, int restart, iteration &outer, Basis &KS)
Generalized Minimum Residual.
Interface with SuperLU (LU direct solver for sparse matrices).
size_t size_type
used as the common size type in the library
size_type alpha(short_type n, short_type d)
Return the value of which is the number of monomials of a polynomial of variables and degree .
GEneric Tool for Finite Element Methods.
void standard_solve(model &md, gmm::iteration &iter, rmodel_plsolver_type lsolver, abstract_newton_line_search &ls)
A default solver for the model brick system.