GetFEM  5.5
gmm_solver_constrained_cg.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2002-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 /**@file gmm_solver_constrained_cg.h
32  @author Yves Renard <Yves.Renard@insa-lyon.fr>
33  @date October 13, 2002.
34  @brief Constrained conjugate gradient. */
35 // preconditionning does not work
36 
37 #ifndef GMM_SOLVER_CCG_H__
38 #define GMM_SOLVER_CCG_H__
39 
40 #include "gmm_kernel.h"
41 #include "gmm_iter.h"
42 
43 namespace gmm {
44 
45  template <typename CMatrix, typename CINVMatrix, typename Matps,
46  typename VectorX>
47  void pseudo_inverse(const CMatrix &C, CINVMatrix &CINV,
48  const Matps& /* PS */, VectorX&) {
49  // compute the pseudo inverse of the non-square matrix C such
50  // CINV = inv(C * trans(C)) * C.
51  // based on a conjugate gradient method.
52 
53  // optimisable : copie de la ligne, precalcul de C * trans(C).
54 
55  typedef VectorX TmpVec;
56  typedef typename linalg_traits<VectorX>::value_type value_type;
57 
58  size_type nr = mat_nrows(C), nc = mat_ncols(C);
59 
60  TmpVec d(nr), e(nr), l(nc), p(nr), q(nr), r(nr);
61  value_type rho, rho_1, alpha;
62  clear(d);
63  clear(CINV);
64 
65  for (size_type i = 0; i < nr; ++i) {
66  d[i] = 1.0; rho = 1.0;
67  clear(e);
68  copy(d, r);
69  copy(d, p);
70 
71  while (rho >= 1E-38) { /* conjugate gradient to compute e */
72  /* which is the i nd row of inv(C * trans(C)) */
73  mult(gmm::transposed(C), p, l);
74  mult(C, l, q);
75  alpha = rho / vect_sp(p, q);
76  add(scaled(p, alpha), e);
77  add(scaled(q, -alpha), r);
78  rho_1 = rho;
79  rho = vect_sp(r, r);
80  add(r, scaled(p, rho / rho_1), p);
81  }
82 
83  mult(transposed(C), e, l); /* l is the i nd row of CINV */
84  // cout << "l = " << l << endl;
85  clean(l, 1E-15);
86  copy(l, mat_row(CINV, i));
87 
88  d[i] = 0.0;
89  }
90  }
91 
92  /** Compute the minimum of @f$ 1/2((Ax).x) - bx @f$ under the contraint @f$ Cx <= f @f$ */
93  template < typename Matrix, typename CMatrix, typename Matps,
94  typename VectorX, typename VectorB, typename VectorF,
95  typename Preconditioner >
96  void constrained_cg(const Matrix& A, const CMatrix& C, VectorX& x,
97  const VectorB& b, const VectorF& f,const Matps& PS,
98  const Preconditioner& M, iteration &iter) {
99  typedef typename temporary_dense_vector<VectorX>::vector_type TmpVec;
100  typedef typename temporary_vector<CMatrix>::vector_type TmpCVec;
101  typedef row_matrix<TmpCVec> TmpCmat;
102 
103  typedef typename linalg_traits<VectorX>::value_type value_type;
104  value_type rho = 1.0, rho_1, lambda, gamma;
105  TmpVec p(vect_size(x)), q(vect_size(x)), q2(vect_size(x)),
106  r(vect_size(x)), old_z(vect_size(x)), z(vect_size(x)),
107  memox(vect_size(x));
108  std::vector<bool> satured(mat_nrows(C));
109  clear(p);
110  iter.set_rhsnorm(sqrt(vect_sp(PS, b, b)));
111  if (iter.get_rhsnorm() == 0.0) iter.set_rhsnorm(1.0);
112 
113  TmpCmat CINV(mat_nrows(C), mat_ncols(C));
114  pseudo_inverse(C, CINV, PS, x);
115 
116  while(true) {
117  // computation of residu
118  copy(z, old_z);
119  copy(x, memox);
120  mult(A, scaled(x, -1.0), b, r);
121  mult(M, r, z); // preconditionner not coherent
122  bool transition = false;
123  for (size_type i = 0; i < mat_nrows(C); ++i) {
124  value_type al = vect_sp(mat_row(C, i), x) - f[i];
125  if (al >= -1.0E-15) {
126  if (!satured[i]) { satured[i] = true; transition = true; }
127  value_type bb = vect_sp(mat_row(CINV, i), z);
128  if (bb > 0.0) add(scaled(mat_row(C, i), -bb), z);
129  }
130  else
131  satured[i] = false;
132  }
133 
134  // descent direction
135  rho_1 = rho; rho = vect_sp(PS, r, z); // ...
136 
137  if (iter.finished(rho)) break;
138 
139  if (iter.get_noisy() > 0 && transition) std::cout << "transition\n";
140  if (transition || iter.first()) gamma = 0.0;
141  else gamma = std::max(0.0, (rho - vect_sp(PS, old_z, z) ) / rho_1);
142  // std::cout << "gamma = " << gamma << endl;
143  // itl::add(r, itl::scaled(p, gamma), p);
144  add(z, scaled(p, gamma), p); // ...
145 
146  ++iter;
147  // one dimensional optimization
148  mult(A, p, q);
149  lambda = rho / vect_sp(PS, q, p);
150  for (size_type i = 0; i < mat_nrows(C); ++i)
151  if (!satured[i]) {
152  value_type bb = vect_sp(mat_row(C, i), p) - f[i];
153  if (bb > 0.0)
154  lambda = std::min(lambda, (f[i]-vect_sp(mat_row(C, i), x)) / bb);
155  }
156  add(x, scaled(p, lambda), x);
157  add(memox, scaled(x, -1.0), memox);
158 
159  }
160  }
161 
162 }
163 
164 #endif // GMM_SOLVER_CCG_H__
The Iteration object calculates whether the solution has reached the desired accuracy,...
Definition: gmm_iter.h:52
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
void clear(L &l)
clear (fill with zeros) a vector or matrix.
Definition: gmm_blas.h:58
void clean(L &l, double threshold)
Clean a vector or matrix (replace near-zero entries with zeroes).
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1663
strongest_value_type< V1, V2 >::value_type vect_sp(const V1 &v1, const V2 &v2)
*‍/
Definition: gmm_blas.h:263
void add(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:1275
Iteration object.
Include the base gmm files.
void constrained_cg(const Matrix &A, const CMatrix &C, VectorX &x, const VectorB &b, const VectorF &f, const Matps &PS, const Preconditioner &M, iteration &iter)
Compute the minimum of under the contraint .
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