GetFEM  5.6
getfem_model_solvers.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2004-2026 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 /**
32  @file getfem_model_solvers.h
33  @author Yves Renard <Yves.Renard@insa-lyon.fr>
34  @date June 15, 2004.
35  @brief Standard solvers for model bricks
36  @see getfem_modeling.h
37 */
38 
39 #ifndef GETFEM_MODEL_SOLVERS_H__
40 #define GETFEM_MODEL_SOLVERS_H__
41 #include "getfem_models.h"
44 #include "gmm/gmm_iter.h"
45 #include "gmm/gmm_iter_solvers.h"
46 #include "gmm/gmm_dense_qr.h"
47 
48 //#include "gmm/gmm_inoutput.h"
49 
50 
51 namespace getfem {
52 
53  template <typename T> struct sort_abs_val_
54  { bool operator()(T x, T y) { return (gmm::abs(x) < gmm::abs(y)); } };
55 
56  template <typename MAT> void print_eigval(const MAT &M) {
57  // just to test a stiffness matrix if needing
58  typedef typename gmm::linalg_traits<MAT>::value_type T;
59  size_type n = gmm::mat_nrows(M);
60  gmm::dense_matrix<T> MM(n, n), Q(n, n);
61  std::vector<T> eigval(n);
62  gmm::copy(M, MM);
63  // gmm::symmetric_qr_algorithm(MM, eigval, Q);
64  gmm::implicit_qr_algorithm(MM, eigval, Q);
65  std::sort(eigval.begin(), eigval.end(), sort_abs_val_<T>());
66  cout << "eival = " << eigval << endl;
67 // cout << "vectp : " << gmm::mat_col(Q, n-1) << endl;
68 // cout << "vectp : " << gmm::mat_col(Q, n-2) << endl;
69 // double emax, emin;
70 // cout << "condition number" << condition_number(MM,emax,emin) << endl;
71 // cout << "emin = " << emin << endl;
72 // cout << "emax = " << emax << endl;
73  }
74 
75 
76  /* ***************************************************************** */
77  /* Linear solvers definition */
78  /* ***************************************************************** */
79 
80  template <typename MAT, typename VECT>
81  struct abstract_linear_solver {
82  typedef MAT MATRIX;
83  typedef VECT VECTOR;
84  virtual void operator ()(const MAT &, VECT &, const VECT &,
85  gmm::iteration &) const = 0;
86  virtual ~abstract_linear_solver() {}
87  };
88 
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,
93  gmm::iteration &iter) const {
95  gmm::cg(M, x, b, P, iter);
96  if (!iter.converged()) GMM_WARNING2("cg did not converge!");
97  }
98  };
99 
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,
104  gmm::iteration &iter) const {
106  gmm::gmres(M, x, b, P, 500, iter);
107  if (!iter.converged()) GMM_WARNING2("gmres did not converge!");
108  }
109  };
110 
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,
115  gmm::iteration &iter) const {
116  gmm::identity_matrix P;
117  gmm::gmres(M, x, b, P, 500, iter);
118  if (!iter.converged()) GMM_WARNING2("gmres did not converge!");
119  }
120  };
121 
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,
126  gmm::iteration &iter) const {
127  gmm::ilut_precond<MAT> P(M, 40, 1E-7);
128  gmm::gmres(M, x, b, P, 500, iter);
129  if (!iter.converged()) GMM_WARNING2("gmres did not converge!");
130  }
131  };
132 
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,
137  gmm::iteration &iter) const {
138  gmm::ilutp_precond<MAT> P(M, 20, 1E-7);
139  gmm::gmres(M, x, b, P, 500, iter);
140  if (!iter.converged()) GMM_WARNING2("gmres did not converge!");
141  }
142  };
143 
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,
149  gmm::iteration &iter) const {
150  double rcond;
151  /*gmm::HarwellBoeing_IO::write("test.hb", M);
152  std::fstream f("bbb", std::ios::out);
153  for (unsigned i=0; i < gmm::vect_size(b); ++i) f << b[i] << "\n";*/
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;
157  }
158  };
159 #endif
160 
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,
164  gmm::iteration &iter) const {
165  typedef typename gmm::linalg_traits<MAT>::value_type T;
166  gmm::dense_matrix<T> MM(gmm::mat_nrows(M),gmm::mat_ncols(M));
167  gmm::copy(M, MM);
168  // Report a singular matrix through iter, as the sparse direct solvers
169  // do, instead of throwing.
170  gmm::lapack_ipvt ipvt(gmm::mat_nrows(MM));
171  size_type info = gmm::lu_factor(MM, ipvt);
172  if (info == 0)
173  gmm::lu_solve(MM, ipvt, x, b);
174  else
175  GMM_WARNING1("dense LU solve failed: singular matrix (pivot " << info << ")");
176  iter.enforce_converged(info == 0);
177  }
178  };
179 
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,
184  gmm::iteration &iter) const {
185  bool ok = gmm::MUMPS_solve(M, x, b, false);
186  iter.enforce_converged(ok);
187  }
188  };
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,
192  gmm::iteration &iter) const {
193  bool ok = gmm::MUMPS_solve(M, x, b, true);
194  iter.enforce_converged(ok);
195  }
196  };
197 #endif
198 
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,
204  gmm::iteration &iter) const {
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;
209  }
210  };
211 
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,
216  gmm::iteration &iter) const {
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;
221  }
222  };
223 #endif
224 
225 
226  /* ***************************************************************** */
227  /* Newton Line search definition */
228  /* ***************************************************************** */
229 
230  struct abstract_newton_line_search {
231  double conv_alpha, conv_r;
232  size_t it, itmax, glob_it;
233  // size_t tot_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) {
238  // tot_it += it; cout << "tot_it = " << tot_it << endl; it = 0;
239  return conv_alpha;
240  };
241  virtual double converged_residual(void) { return conv_r; };
242  virtual ~abstract_newton_line_search() { }
243  };
244 
245  // Dummy linesearch for the newton with step control
246  struct newton_search_with_step_control : public abstract_newton_line_search {
247 
248  virtual void init_search(double /*r*/, size_t /*git*/, double /*R0*/ = 0.0)
249  { GMM_ASSERT1(false, "Not to be used"); }
250 
251  virtual double next_try(void)
252  { GMM_ASSERT1(false, "Not to be used"); }
253 
254  virtual bool is_converged(double /*r*/, double /*R1*/ = 0.0)
255  { GMM_ASSERT1(false, "Not to be used"); }
256 
257  newton_search_with_step_control() {}
258  };
259 
260 
261  struct quadratic_newton_line_search : public abstract_newton_line_search {
262  double R0_, R1_;
263 
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");
267  glob_it = git;
268  conv_alpha = alpha = double(1); conv_r = first_res = r; it = 0;
269  R0_ = R0;
270  }
271  virtual double next_try(void) {
272  ++it;
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;
277  }
278  virtual bool is_converged(double r, double R1 = 0.0) {
279  conv_r = r;
280  R1_ = R1; return ((gmm::abs(R1_) < gmm::abs(R0_*0.5)) || it >= itmax);
281  }
282  quadratic_newton_line_search(size_t imax = size_t(-1)) { itmax = imax; }
283  };
284 
285 
286  struct simplest_newton_line_search : public abstract_newton_line_search {
287  double alpha, alpha_mult, first_res, alpha_max_ratio, alpha_min,
288  alpha_threshold_res;
289  virtual void init_search(double r, size_t git, double = 0.0) {
290  glob_it = git;
291  conv_alpha = alpha = double(1); conv_r = first_res = r; it = 0;
292  }
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) {
296  conv_r = r;
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)
300  || it >= itmax);
301  }
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)
308  { itmax = imax; }
309  };
310 
311  struct default_newton_line_search : public abstract_newton_line_search {
312  // This line search try to detect where is the minimum, dividing the step
313  // by a factor two each time.
314  // - it stops if the residual is less than the previous residual
315  // times alpha_min_ratio (= 0.9).
316  // - if the minimal step is reached with a residual greater than
317  // the previous residual times alpha_min_ratio then it decides
318  // between two options :
319  // - return with the step corresponding to the smallest residual
320  // - return with a greater residual corresponding to a residual
321  // less than the previous residual times alpha_max_ratio.
322  // the decision is taken regarding the previous iterations.
323  // - in order to shorten the line search, the process stops when
324  // the residual increases three times consecutively.
325  // possible improvment : detect the curvature at the origin
326  // (only one evaluation) and take it into account.
327  // Fitted to some experiments in contrib/tests_newton
328 
329  double alpha, alpha_old, alpha_mult, first_res, alpha_max_ratio;
330  double alpha_min_ratio, alpha_min;
331  size_type count, count_pat;
332  bool max_ratio_reached;
333  double alpha_max_ratio_reached, r_max_ratio_reached;
334  size_type it_max_ratio_reached;
335 
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; }
340  };
341 
342  /* the former default_newton_line_search */
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) {
347  glob_it = git;
348  conv_alpha = alpha = double(1);
349  prev_res = conv_r = first_res = r; it = 0;
350  }
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)
356  || it >= itmax)
357  { conv_r = r; return true; }
358  if (it > 1 && r > prev_res && prev_res < alpha_max_ratio * first_res)
359  return true;
360  prev_res = conv_r = r;
361  return false;
362  }
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; }
369  };
370 
371 
372  struct systematic_newton_line_search : public abstract_newton_line_search {
373  double alpha, alpha_mult, first_res;
374  double alpha_min, prev_res;
375  bool first;
376  virtual void init_search(double r, size_t git, double = 0.0) {
377  glob_it = git;
378  conv_alpha = alpha = double(1);
379  prev_res = conv_r = first_res = r; it = 0; first = true;
380  }
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) {
384  // cout << "a = " << alpha / alpha_mult << " r = " << r << endl;
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;
388  return false;
389  }
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; }
394  };
395 
396 
397  /* ***************************************************************** */
398  /* Newton(-Raphson) algorithm with step control. */
399  /* ***************************************************************** */
400  /* Still solves a problem F(X) = 0 starting at X0 but setting */
401  /* B0 = F(X0) and solving */
402  /* F(X) = (1-alpha)B0 (1) */
403  /* with alpha growing from 0 to 1. */
404  /* A very basic line search is applied. */
405  /* */
406  /* Step 0 : set alpha0 = 0, alpha = 1, X0 given and B0 = F(X0). */
407  /* Step 1 : Set Ri = F(Xi) - (1-alpha)B0 */
408  /* If ||Ri|| < rho, Xi+1 = Xi and go to step 2 */
409  /* Perform Newton step on problem (1) */
410  /* If the Newton do not converge (stagnation) */
411  /* alpha <- max(alpha0+1E-4, (alpha+alpha0)/2) */
412  /* Loop on step 1 with Xi unchanged */
413  /* Step 2 : if alpha=1 stop */
414  /* else alpha0 <- alpha, */
415  /* alpha <- min(1,alpha0+2*(alpha-alpha0)), */
416  /* Go to step 1 with Xi+1 */
417  /* being the result of Newton iterations of step1. */
418  /* */
419  /*********************************************************************/
420 
421  template <typename PB>
422  void Newton_with_step_control(PB &pb, gmm::iteration &iter)
423  {
424  typedef typename gmm::linalg_traits<typename PB::VECTOR>::value_type T;
425  typedef typename gmm::number_traits<T>::magnitude_type R;
426  gmm::iteration iter_linsolv0 = iter;
427  iter_linsolv0.reduce_noisy();
428  iter_linsolv0.set_resmax(iter.get_resmax()/20.0);
429  iter_linsolv0.set_maxiter(10000); // arbitrary
430 
431  pb.compute_residual();
432  R approx_eln = pb.approx_external_load_norm();
433  if (approx_eln == R(0)) approx_eln = R(1);
434 
435  typename PB::VECTOR b0(gmm::vect_size(pb.residual()));
436  gmm::copy(pb.residual(), b0);
437  typename PB::VECTOR Xi(gmm::vect_size(pb.residual()));
438  gmm::copy(pb.state_vector(), Xi);
439 
440  typename PB::VECTOR dr(gmm::vect_size(pb.residual()));
441 
442  R alpha0(0), alpha(1), res0(gmm::vect_norm1(b0)), minres(res0);
443  // const newton_search_with_step_control *ls
444  // = dynamic_cast<const newton_search_with_step_control *>(&(pb.ls));
445  // GMM_ASSERT1(ls, "Internal error");
446  size_type nit = 0, stagn = 0;
447  R coeff = R(2);
448 
449  scalar_type crit = pb.residual_norm() / approx_eln;
450  if (iter.finished(crit)) return;
451  for(;;) {
452 
453  crit = gmm::vect_dist1(pb.residual(), gmm::scaled(b0, R(1)-alpha))
454  / approx_eln;
455  if (!iter.converged(crit)) {
456  gmm::iteration iter_linsolv = iter_linsolv0;
457 
458  int is_singular = 1;
459  while (is_singular) { // Linear system solve
460  gmm::clear(dr);
461  pb.add_to_residual(b0, alpha-R(1)); // canceled at next compute_residual
462  iter_linsolv.init();
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()) {
470  is_singular++;
471  if (is_singular <= 4) {
472  if (iter.get_noisy())
473  cout << "Singular tangent matrix:"
474  " perturbation of the state vector." << endl;
475  pb.perturbation();
476  pb.compute_residual(); // cancels add_to_residual
477  } else {
478  if (iter.get_noisy())
479  cout << "Singular tangent matrix: perturbation failed, "
480  << "aborting." << endl;
481  return;
482  }
483  }
484  else is_singular = 0;
485  }
486  if (iter.get_noisy() > 1) cout << "linear solver done" << endl;
487 
488  gmm::add(dr, pb.state_vector());
489  pb.compute_residual(); // cancels add_to_residual
490  R res = gmm::vect_dist1(pb.residual(), gmm::scaled(b0, R(1)-alpha));
491  R dec = R(1)/R(2), coeff2 = coeff * R(1.5);
492 
493  while (dec > R(1E-5) && res >= res0 * coeff) {
494  gmm::add(gmm::scaled(dr, -dec), pb.state_vector());
495  pb.compute_residual();
496  R res2 = gmm::vect_dist1(pb.residual(), gmm::scaled(b0, R(1)-alpha));
497  if (res2 < res*R(0.95) || res2 >= res0 * coeff2) {
498  dec /= R(2); res = res2; coeff2 *= R(1.5);
499  } else {
500  gmm::add(gmm::scaled(dr, dec), pb.state_vector());
501  break;
502  }
503  }
504  dec *= R(2);
505 
506  nit++;
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) {
511  stagn++;
512 
513  if ((stagn > 10 && alpha > alpha0 + R(5E-2)) || cut) {
514  alpha = (alpha + alpha0) / R(2);
515  if (near_end) alpha = R(1);
516  gmm::copy(Xi, pb.state_vector());
517  pb.compute_residual();
518  res = gmm::vect_dist1(pb.residual(), gmm::scaled(b0, R(1)-alpha));
519  nit = 0;
520  stagn = 0; coeff = R(2);
521  }
522  }
523  if (res < minres || (alpha == R(1) && nit < 10)) stagn = 0;
524  res0 = res;
525  if (nit < 5) minres = res0; else minres = std::min(minres, res0);
526 
527  if (iter.get_noisy())
528  cout << "step control [" << std::setw(8) << alpha0 << ","
529  << std::setw(8) << alpha << "," << std::setw(10) << dec << "]";
530  ++iter;
531  // crit = std::min(res / approx_eln,
532  // gmm::vect_norm1(dr) / std::max(1E-25,pb.state_norm()));
533  crit = res / approx_eln;
534  }
535 
536  if (iter.finished(crit)) {
537  if (iter.converged() && alpha < R(1)) {
538  R a = alpha;
539  alpha = std::min(R(1), alpha*R(3) - alpha0*R(2));
540  alpha0 = a;
541  gmm::copy(pb.state_vector(), Xi);
542  nit = 0; stagn = 0; coeff = R(2);
543  } else return;
544  }
545  }
546  }
547 
548 
549 
550  /* ***************************************************************** */
551  /* Classical Newton(-Raphson) algorithm. */
552  /* ***************************************************************** */
553 
554  template <typename PB>
555  void classical_Newton(PB &pb, gmm::iteration &iter)
556  {
557  typedef typename gmm::linalg_traits<typename PB::VECTOR>::value_type T;
558  typedef typename gmm::number_traits<T>::magnitude_type R;
559  gmm::iteration iter_linsolv0 = iter;
560  iter_linsolv0.reduce_noisy();
561  iter_linsolv0.set_resmax(iter.get_resmax()/20.0);
562  iter_linsolv0.set_maxiter(10000); // arbitrary
563 
564  pb.compute_residual();
565  R approx_eln = pb.approx_external_load_norm();
566  if (approx_eln == R(0)) approx_eln = R(1);
567 
568  typename PB::VECTOR dr(gmm::vect_size(pb.residual()));
569 
570  scalar_type crit = pb.residual_norm() / approx_eln;
571  while (!iter.finished(crit)) {
572  gmm::iteration iter_linsolv = iter_linsolv0;
573 
574  int is_singular = 1;
575  while (is_singular) {
576  gmm::clear(dr);
577  iter_linsolv.init();
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()) {
585  is_singular++;
586  if (is_singular <= 4) {
587  if (iter.get_noisy())
588  cout << "Singular tangent matrix:"
589  " perturbation of the state vector." << endl;
590  pb.perturbation();
591  pb.compute_residual();
592  } else {
593  if (iter.get_noisy())
594  cout << "Singular tangent matrix: perturbation failed, aborting."
595  << endl;
596  return;
597  }
598  }
599  else is_singular = 0;
600  }
601 
602  if (iter.get_noisy() > 1) cout << "linear solver done" << endl;
603  R alpha = pb.line_search(dr, iter); //it is assumed that the linesearch
604  //executes a pb.compute_residual();
605  if (iter.get_noisy()) cout << "alpha = " << std::setw(6) << alpha << " ";
606  ++iter;
607  crit = std::min(pb.residual_norm() / approx_eln,
608  gmm::vect_norm1(dr) / std::max(1E-25, pb.state_norm()));
609  }
610  }
611 
612 
613 
614  //---------------------------------------------------------------------
615  // Default linear solver.
616  //---------------------------------------------------------------------
617 
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;
624 
625  template<typename MATRIX, typename VECTOR>
626  std::shared_ptr<abstract_linear_solver<MATRIX, VECTOR>>
627  default_linear_solver(const model &md) {
628 
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>>();
632  else
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>>();
638  else
639  return std::make_shared
640  <linear_solver_distributed_mumps<MATRIX, VECTOR>>();
641 #else
642  size_type ndof = md.nb_dof(), max3d = 15000, dim = md.leading_dimension();
643 # if defined(GMM_USES_MUMPS)
644  max3d = 250000;
645 # endif
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>>();
650  else
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>>();
654 # else
655  static_assert(false,
656  "At least one direct solver (MUMPS or SuperLU) is required");
657 # endif
658  }
659  else {
660  if (md.is_coercive())
661  return std::make_shared
662  <linear_solver_cg_preconditioned_ildlt<MATRIX, VECTOR>>();
663  else {
664  if (dim <= 2)
665  return std::make_shared
666  <linear_solver_gmres_preconditioned_ilut<MATRIX,VECTOR>>();
667  else
668  return std::make_shared
669  <linear_solver_gmres_preconditioned_ilu<MATRIX,VECTOR>>();
670  }
671  }
672 #endif
673  return std::shared_ptr<abstract_linear_solver<MATRIX, VECTOR>>();
674  }
675 
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>>();
683 #else
684  GMM_ASSERT1(false, "SuperLU is not interfaced");
685 #endif
686  }
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>>();
693 # else
694  return std::make_shared
695  <linear_solver_distributed_mumps<MATRIX, VECTOR>>();
696 # endif
697 #else
698  GMM_ASSERT1(false, "Mumps is not interfaced");
699 #endif
700  }
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);
715  else
716  GMM_ASSERT1(false, "Unknown linear solver");
717  return std::shared_ptr<abstract_linear_solver<MATRIX, VECTOR>>();
718  }
719 
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);
724  }
725 
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);
730  }
731 
732  //---------------------------------------------------------------------
733  // Standard solve.
734  //---------------------------------------------------------------------
735 
736 
737  /** A default solver for the model brick system.
738  Of course it could be not very well suited for a particular
739  problem, so it could be copied and adapted to change solvers,
740  add a special treatment on the problem, etc ... This is in
741  fact a model for your own solver.
742 
743  For small problems, a direct solver is used (gmm::SuperLU_solve),
744  for larger problems, a conjugate gradient gmm::cg (if the problem is
745  coercive) or a gmm::gmres is used (preconditioned with an incomplete
746  factorization).
747 
748  When MPI/METIS is enabled, a partition is done via METIS, and a parallel
749  solver can be used.
750 
751  Note that it is possible to disable some variables
752  (see the md.disable_variable(varname) method) in order to
753  solve the problem only with respect to a subset of variables (the
754  disabled variables are the considered as data) for instance to
755  replace the global Newton strategy with a fixed point one.
756 
757  @ingroup bricks
758  */
759  void standard_solve(model &md, gmm::iteration &iter,
760  rmodel_plsolver_type lsolver,
761  abstract_newton_line_search &ls);
762 
763  void standard_solve(model &md, gmm::iteration &iter,
764  cmodel_plsolver_type lsolver,
765  abstract_newton_line_search &ls);
766 
767  void standard_solve(model &md, gmm::iteration &iter,
768  rmodel_plsolver_type lsolver);
769 
770  void standard_solve(model &md, gmm::iteration &iter,
771  cmodel_plsolver_type lsolver);
772 
773  void standard_solve(model &md, gmm::iteration &iter);
774 
775 } /* end of namespace getfem. */
776 
777 
778 #endif /* GETFEM_MODEL_SOLVERS_H__ */
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,...
Definition: gmm_iter.h:52
Model representation in Getfem.
Interface with MUMPS (LU direct solver for sparse matrices).
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
number_traits< typename linalg_traits< V1 >::value_type >::magnitude_type vect_dist1(const V1 &v1, const V2 &v2)
1-distance between two vectors
Definition: gmm_blas.h:657
void clear(L &l)
clear (fill with zeros) a vector or matrix.
Definition: gmm_blas.h:58
void add(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:1275
size_type lu_factor(DenseMatrix &A, Pvector &ipvt)
LU Factorization of a general (dense) matrix (real or complex).
Definition: gmm_dense_lu.h:92
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.
Definition: gmm_dense_lu.h:129
Dense QR factorization.
Iteration object.
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
Definition: bgeot_poly.h:48
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 .
Definition: bgeot_poly.cc:46
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.