GetFEM  5.6
bgeot_geotrans_inv.cc
1 /*===========================================================================
2 
3  Copyright (C) 2000-2026 Yves Renard
4 
5  This file is a part of GetFEM
6 
7  GetFEM is free software; you can redistribute it and/or modify it
8  under the terms of the GNU Lesser General Public License as published
9  by the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version along with the GCC Runtime Library
11  Exception either version 3.1 or (at your option) any later version.
12  This program is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15  License and GCC Runtime Library Exception for more details.
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see https://www.gnu.org/licenses/.
18 
19 ===========================================================================*/
20 
23 #include "getfem/bgeot_torus.h"
24 #include "gmm/gmm_solver_bfgs.h"
25 
26 
27 namespace bgeot
28 {
29  void project_into_convex(base_node &xref, const pgeometric_trans pgt) {
30 
31  auto pgt_torus = std::dynamic_pointer_cast<const torus_geom_trans>(pgt);
32  const pgeometric_trans
33  orig_pgt = pgt_torus ? pgt_torus->get_original_transformation()
34  : pgt;
35  pconvex_structure cvs = orig_pgt->basic_structure();
36  dim_type n = cvs->dim();
37 
38  if (cvs == pyramid_QK_structure(1)) {
39  // reference pyramid: 0 <= x2 <= 1 and |x0|, |x1| <= 1 - x2
40  xref[2] = std::min(std::max(xref[2], 0.0), 1.0);
41  scalar_type h = 1.0 - xref[2];
42  for (dim_type k = 0; k < 2; ++k)
43  xref[k] = std::min(std::max(xref[k], -h), h);
44  return;
45  }
46 
47  for (auto &coord : xref) {
48  if (coord < 0.0) coord = 0.0;
49  if (coord > 1.0) coord = 1.0;
50  }
51 
52  if (cvs == simplex_structure(n)) {
53  scalar_type sum_coordinates = 0.0;
54  for (const auto &coord : xref) sum_coordinates += coord;
55 
56  if (sum_coordinates > 1.0) gmm::scale(xref, 1.0 / sum_coordinates);
57  }
58  else if (n >= 3 && cvs == prism_P1_structure(n)) {
59  // simplex in the first n-1 coordinates
60  scalar_type sum_coordinates = 0.0;
61  for (dim_type k = 0; k < n-1; ++k) sum_coordinates += xref[k];
62  if (sum_coordinates > 1.0)
63  for (dim_type k = 0; k < n-1; ++k) xref[k] /= sum_coordinates;
64  }
65  }
66 
68  scalar_type IN_EPS,
69  bool project_into_element) {
70  bool converged = true;
71  return invert(x, xref, converged, IN_EPS, project_into_element);
72  }
73 
75  bool &converged,
76  scalar_type IN_EPS,
77  bool project_into_element) {
78  assert(pgt);
79  xref.resize(pgt->structure()->dim());
80  converged = true;
81  if (pgt->is_linear())
82  return invert_lin(x, xref, IN_EPS);
83  else
84  return invert_nonlin(x, xref, IN_EPS, converged, false,
85  project_into_element);
86  }
87 
88  /* inversion for linear geometric transformations */
89  bool geotrans_inv_convex::invert_lin(const base_node& x, base_node& xref,
90  scalar_type IN_EPS) {
91  // the point relative to the first node, as Grel: the residual does not
92  // depend on the element position
93  base_node xrel(x); gmm::add(gmm::scaled(mat_col(G, 0), -1.0), xrel);
94  mult(transposed(B), xrel, xref);
95  base_node y = pgt->transform(xref, Grel);
96  add(gmm::scaled(xrel, -1.0), y);
97 
98  return (pgt->convex_ref()->is_in(xref) < IN_EPS) &&
99  (gmm::vect_norm2(y) < IN_EPS);
100  }
101 
102  bool geotrans_inv_convex::update_B() {
103  if (P != N) {
104  pgt->compute_K_matrix(G, pc, K);
105  gmm::mult(gmm::transposed(K), K, CS);
106  double det = bgeot::lu_inverse(&(*(CS.begin())), P, false);
107  if (det == 0)
108  return false;
109  gmm::mult(K, CS, B);
110  } else {
111  // L'inversion peut être optimisée par le non calcul global de B
112  // et la resolution d'un système linéaire.
113  base_matrix KT(K.nrows(), K.ncols());
114  pgt->compute_K_matrix(G, pc, KT);
115  gmm::copy(gmm::transposed(KT), K);
116  gmm::copy(K, B);
117  double det = bgeot::lu_inverse(&(*(K.begin())), P, false);
118  if (det == 0)
119  return false;
120  B.swap(K);
121  }
122  return true;
123  }
124 
125  class geotrans_inv_convex_bfgs {
126  geotrans_inv_convex &gic;
127  base_node x;
128  public:
129  geotrans_inv_convex_bfgs(geotrans_inv_convex &gic_,
130  const base_node &x_) : gic(gic_), x(x_) {}
131  scalar_type operator()(const base_node& xref) const {
132  base_node r = gic.pgt->transform(xref, gic.G) - x;
133  return gmm::vect_norm2_sqr(r)/2.;
134  }
135  void operator()(const base_node& xref, base_small_vector& gr) const {
136  gic.pgt->poly_vector_grad(xref, gic.pc);
137  gic.update_B();
138  base_node r = gic.pgt->transform(xref, gic.G) - x;
139  gr.resize(xref.size());
140  gmm::mult(gmm::transposed(gic.K), r, gr);
141  }
142  };
143 
144  void geotrans_inv_convex::update_linearization() {
145 
146  const convex_ind_ct &dir_pt_ind = pgt->structure()->ind_dir_points();
147  const stored_point_tab &ref_nodes = pgt->geometric_nodes();
148 
149  has_linearized_approx = true;
150 
151  auto n_points = dir_pt_ind.size();
152  auto N_ref = ref_nodes.begin()->size();
153 
154  std::vector<base_node> dir_pts, dir_pts_ref;
155  for (auto i : dir_pt_ind) {
156  dir_pts.push_back(base_node(N));
157  gmm::copy(mat_col(G, i), dir_pts.back());
158  dir_pts_ref.push_back(ref_nodes[i]);
159  }
160 
161  base_matrix K_lin(N, n_points - 1),
162  B_transp_lin(n_points - 1, N),
163  K_ref_lin(N_ref, n_points - 1);
164 
165  P_lin = dir_pts[0];
166  P_ref_lin = dir_pts_ref[0];
167 
168  for (size_type i = 1; i < n_points; ++i) {
169  add(dir_pts[i], gmm::scaled(P_lin, -1.0), mat_col(K_lin, i - 1));
170  add(dir_pts_ref[i], gmm::scaled(P_ref_lin, -1.0),
171  mat_col(K_ref_lin, i - 1));
172  }
173 
174  if (K_lin.nrows() == K_lin.ncols()) {
175  lu_inverse(K_lin);
176  gmm::copy(K_lin, B_transp_lin);
177  }
178  else {
179  base_matrix temp(n_points - 1, n_points - 1);
180  mult(transposed(K_lin), K_lin, temp);
181  lu_inverse(temp);
182  mult(temp, transposed(K_lin), B_transp_lin);
183  }
184 
185  K_ref_B_transp_lin.base_resize(N_ref, N);
186  mult(K_ref_lin, B_transp_lin, K_ref_B_transp_lin);
187  }
188 
189 
190  /* inversion for non-linear geometric transformations
191  (Newton on Grad(pgt)(y - pgt(x)) = 0 )
192  */
193  bool geotrans_inv_convex::invert_nonlin(const base_node& x,
194  base_node& xref,
195  scalar_type IN_EPS,
196  bool &converged,
197  bool /* throw_except */,
198  bool project_into_element) {
199  converged = false;
200  base_node x0_ref(P), x0rel(N), diff(N), xref_prev(P);
201  // the point relative to the first node, as Grel
202  base_node xrel(x); gmm::add(gmm::scaled(mat_col(G, 0), -1.0), xrel);
203  scalar_type maxnormG = gmm::mat_maxnorm(Grel); // element size
204  { // find initial guess
205  x0_ref = pgt->geometric_nodes()[0];
206  scalar_type res = gmm::vect_dist2(mat_col(Grel, 0), xrel);
207  for (size_type j = 1; j < pgt->nb_points(); ++j) {
208  scalar_type res0 = gmm::vect_dist2(mat_col(Grel, j), xrel);
209  if (res0 < res) {
210  res = res0;
211  x0_ref = pgt->geometric_nodes()[j];
212  }
213  }
214  if (res < maxnormG * IN_EPS/100.) {
215  gmm::copy(x0_ref, xref);
216  if (project_into_element) project_into_convex(xref, pgt);
217  x0rel = pgt->transform(xref, Grel);
218  add(x0rel, gmm::scaled(xrel, -1.0), diff);
219  res = gmm::vect_norm2(diff);
220  if (res < maxnormG * IN_EPS/100. &&
221  pgt->convex_ref()->is_in(xref) < IN_EPS) {
222  converged = true;
223  return true;
224  }
225  }
226 
227  scalar_type res0 = std::numeric_limits<scalar_type>::max();
228  if (has_linearized_approx) {
229 
230  add(x, gmm::scaled(P_lin, -1.0), diff);
231  mult(K_ref_B_transp_lin, diff, xref);
232  gmm::add(P_ref_lin, xref);
233 
234  if (project_into_element) project_into_convex(xref, pgt);
235  res0 = gmm::vect_dist2(pgt->transform(xref, Grel), xrel);
236  }
237 
238  if (res < res0) gmm::copy(x0_ref, xref);
239  if (res < IN_EPS) // TODO: fix this too intrusive hack
240  xref *= 0.999888783; // For pyramid element to avoid the singularity
241  }
242 
243  add(pgt->transform(xref, Grel), gmm::scaled(xrel, -1.0), diff);
244  scalar_type res = gmm::vect_norm2(diff);
245  scalar_type res0 = std::numeric_limits<scalar_type>::max();
246  scalar_type factor = 1.0;
247 
248  while (res >= maxnormG * IN_EPS/100.) {
249  if ((gmm::abs(res - res0) < IN_EPS/100.) || (factor < IN_EPS)) { // stalled?
250  // relaxed convergence criterion depending on the size and position
251  // of the real element
252  converged = (res < maxnormG * IN_EPS/100.);
253  if (!converged) { // stalled at rounding level, e.g. near a pyramid
254  // apex: accept if the Newton correction is below the tolerance
255  pgt->poly_vector_grad(xref, pc);
256  if (update_B()) {
257  mult(transposed(B), diff, x0_ref);
258  converged = (gmm::vect_norm2(x0_ref) < IN_EPS);
259  if (converged && P != N) { // the residual normal to the element
260  base_node rn(N); // is not reduced by the correction
261  gmm::mult(K, gmm::scaled(x0_ref, -1.0), diff, rn);
262  converged = (gmm::vect_norm2(rn) < maxnormG * IN_EPS/100.);
263  }
264  }
265  }
266  return converged && (pgt->convex_ref()->is_in(xref) < IN_EPS);
267  }
268  if (res > res0) { // back to the previous iterate, with a shorter step
269  gmm::copy(xref_prev, xref);
270  x0rel = pgt->transform(xref, Grel);
271  add(x0rel, gmm::scaled(xrel, -1.0), diff);
272  factor *= 0.5;
273  } else {
274  if (factor < 1.0-IN_EPS) factor *= 2.0;
275  res0 = res;
276  }
277  pgt->poly_vector_grad(xref, pc);
278  if (!update_B()) {
279  converged = false;
280  return false;
281  }
282  mult(transposed(B), diff, x0_ref);
283  gmm::copy(xref, xref_prev);
284  add(gmm::scaled(x0_ref, -factor), xref);
285  if (project_into_element) project_into_convex(xref, pgt);
286  x0rel = pgt->transform(xref, Grel);
287  add(x0rel, gmm::scaled(xrel, -1.0), diff);
288  res = gmm::vect_norm2(diff);
289  }
290  converged = true;
291  return (pgt->convex_ref()->is_in(xref) < IN_EPS);
292  }
293 
294 } /* end of namespace bgeot. */
Inversion of geometric transformations.
Mesh structure definition.
Provides mesh of torus.
bool invert(const base_node &x, base_node &xref, scalar_type IN_EPS=1e-12, bool project_into_element=false)
given the node on the real element, returns the node on the reference element (even if it is outside ...
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norm2(const V &v)
Euclidean norm of a vector.
Definition: gmm_blas.h:556
number_traits< typename linalg_traits< M >::value_type >::magnitude_type mat_maxnorm(const M &m)
*‍/
Definition: gmm_blas.h:869
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norm2_sqr(const V &v)
squared Euclidean norm of a vector.
Definition: gmm_blas.h:543
number_traits< typename linalg_traits< V1 >::value_type >::magnitude_type vect_dist2(const V1 &v1, const V2 &v2)
Euclidean distance between two vectors.
Definition: gmm_blas.h:596
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1663
void add(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:1275
Implements BFGS (Broyden, Fletcher, Goldfarb, Shanno) algorithm.
Basic Geometric Tools.
pconvex_structure pyramid_QK_structure(dim_type k)
Give a pointer on the 3D pyramid structure for a degree k = 1 or 2.
std::shared_ptr< const convex_structure > pconvex_structure
Pointer on a convex structure description.
pconvex_structure prism_P1_structure(dim_type nc)
Give a pointer on the structures of a prism of dimension d.
pconvex_structure simplex_structure(dim_type nc)
Give a pointer on the structures of a simplex of dimension d.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48
std::shared_ptr< const bgeot::geometric_trans > pgeometric_trans
pointer type for a geometric transformation