GetFEM  5.6
getfem_generic_assembly_interpolation.cc
1 /*===========================================================================
2 
3  Copyright (C) 2013-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/getfem_generic_assembly_compile_and_exec.h"
24 #include "getfem/getfem_generic_assembly_functions_and_operators.h"
25 
26 namespace getfem {
27 
28  //=========================================================================
29  // Interpolation functions
30  //=========================================================================
31 
32  // general Interpolation
33  void ga_interpolation(ga_workspace &workspace,
34  ga_interpolation_context &gic) {
35  ga_instruction_set gis;
36  ga_compile_interpolation(workspace, gis);
37  ga_interpolation_exec(gis, workspace, gic);
38  }
39 
40  // Interpolation on a Lagrange fem on the same mesh
41  struct ga_interpolation_context_fem_same_mesh
42  : public ga_interpolation_context {
43  base_vector &result;
44  std::vector<int> dof_count;
45  const mesh_fem &mf;
46  bool initialized;
47  bool is_torus;
48  size_type s;
49 
50  virtual bgeot::pstored_point_tab
51  ppoints_for_element(size_type cv, short_type f,
52  std::vector<size_type> &ind) const {
53  pfem pf = mf.fem_of_element(cv);
54  GMM_ASSERT1(pf->is_lagrange(),
55  "Only Lagrange fems can be used in interpolation");
56 
57  if (f != short_type(-1))
58  for (size_type i = 0; i < pf->node_convex(cv).structure()
59  ->nb_points_of_face(f); ++i)
60  ind.push_back(pf->node_convex(cv).structure()
61  ->ind_points_of_face(f)[i]);
62  else
63  for (size_type i = 0; i < pf->node_convex(cv).nb_points(); ++i)
64  ind.push_back(i);
65 
66  return pf->node_tab(cv);
67  }
68 
69  virtual bool use_pgp(size_type) const { return true; }
70  virtual bool use_mim() const { return false; }
71 
72  void init_(size_type si, size_type q, size_type qmult) {
73  s = si;
74  gmm::resize(result, qmult * mf.nb_basic_dof());
75  gmm::clear(result);
76  gmm::resize(dof_count, mf.nb_basic_dof()/q);
77  gmm::clear(dof_count);
78  initialized = true;
79  }
80 
81  virtual void store_result(size_type cv, size_type i, base_tensor &t) {
82  if (is_torus) {
83  size_type target_dim = mf.fem_of_element(cv)->target_dim();
84  GMM_ASSERT2(target_dim == 3, "Invalid torus fem.");
85  size_type qdim = 1;
86  size_type result_dim = 2;
87  if (!initialized) {init_(qdim, qdim, qdim);}
88  size_type idof = mf.ind_basic_dof_of_element(cv)[i];
89  result[idof] = t[idof%result_dim];
90  ++dof_count[idof];
91  } else {
92  size_type si = t.size();
93  size_type q = mf.get_qdim();
94  size_type qmult = si / q;
95  GMM_ASSERT1( (si % q) == 0, "Incompatibility between the mesh_fem and "
96  "the size of the expression to be interpolated");
97  if (!initialized) { init_(si, q, qmult); }
98  GMM_ASSERT1(s == si, "Internal error");
99  size_type idof = mf.ind_basic_dof_of_element(cv)[i*q];
100  gmm::add(t.as_vector(),
101  gmm::sub_vector(result, gmm::sub_interval(qmult*idof, s)));
102  (dof_count[idof/q])++;
103  }
104  }
105 
106  virtual void finalize() {
107  std::vector<size_type> data(3);
108  data[0] = initialized ? result.size() : 0;
109  data[1] = initialized ? dof_count.size() : 0;
110  data[2] = initialized ? s : 0;
111  MPI_MAX_VECTOR(data);
112  if (!initialized) {
113  if (data[0]) {
114  gmm::resize(result, data[0]);
115  gmm::resize(dof_count, data[1]);
116  gmm::clear(dof_count);
117  s = data[2];
118  }
119  gmm::clear(result);
120  }
121  if (initialized)
122  GMM_ASSERT1(gmm::vect_size(result) == data[0] && s == data[2] &&
123  gmm::vect_size(dof_count) == data[1], "Incompatible sizes");
124  MPI_SUM_VECTOR(result);
125  MPI_SUM_VECTOR(dof_count);
126  for (size_type i = 0; i < dof_count.size(); ++i)
127  if (dof_count[i])
128  gmm::scale(gmm::sub_vector(result, gmm::sub_interval(s*i, s)),
129  scalar_type(1) / scalar_type(dof_count[i]));
130  }
131 
132  virtual const mesh &linked_mesh() { return mf.linked_mesh(); }
133 
134  ga_interpolation_context_fem_same_mesh(const mesh_fem &mf_, base_vector &r)
135  : result(r), mf(mf_), initialized(false), is_torus(false) {
136  GMM_ASSERT1(!(mf.is_reduced()),
137  "Interpolation on reduced fem is not allowed");
138  if (dynamic_cast<const torus_mesh_fem*>(&mf)){
139  auto first_cv = mf.first_convex_of_basic_dof(0);
140  auto target_dim = mf.fem_of_element(first_cv)->target_dim();
141  if (target_dim == 3) is_torus = true;
142  }
143  }
144  };
145 
146  void ga_interpolation_Lagrange_fem
147  (ga_workspace &workspace, const mesh_fem &mf, base_vector &result) {
148  ga_interpolation_context_fem_same_mesh gic(mf, result);
149  ga_interpolation(workspace, gic);
150  }
151 
152  void ga_interpolation_Lagrange_fem
153  (const getfem::model &md, const std::string &expr, const mesh_fem &mf,
154  base_vector &result, const mesh_region &rg) {
155  ga_workspace workspace(md);
156  workspace.add_interpolation_expression(expr, mf.linked_mesh(), rg);
157  ga_interpolation_Lagrange_fem(workspace, mf, result);
158  }
159 
160  // Interpolation on a cloud of points
161  struct ga_interpolation_context_mti
162  : public ga_interpolation_context {
163  base_vector &result;
164  const mesh_trans_inv &mti;
165  bool initialized;
166  size_type s, nbdof;
167 
168  virtual bgeot::pstored_point_tab
169  ppoints_for_element(size_type cv, short_type,
170  std::vector<size_type> &ind) const {
171  std::vector<size_type> itab;
172  mti.points_on_convex(cv, itab);
173  std::vector<base_node> pt_tab(itab.size());
174  for (size_type i = 0; i < itab.size(); ++i) {
175  pt_tab[i] = mti.reference_coords()[itab[i]];
176  ind.push_back(i);
177  }
178  return store_point_tab(pt_tab);
179  }
180 
181  virtual bool use_pgp(size_type) const { return false; }
182  virtual bool use_mim() const { return false; }
183 
184  virtual void store_result(size_type cv, size_type i, base_tensor &t) {
185  size_type si = t.size();
186  if (!initialized) {
187  s = si;
188  gmm::resize(result, s * nbdof);
189  gmm::clear(result);
190  initialized = true;
191  }
192  GMM_ASSERT1(s == si, "Internal error");
193  size_type ipt = mti.point_on_convex(cv, i);
194  size_type dof_t = mti.id_of_point(ipt);
195  gmm::add(t.as_vector(),
196  gmm::sub_vector(result, gmm::sub_interval(s*dof_t, s)));
197  }
198 
199  virtual void finalize() {
200  std::vector<size_type> data(2);
201  data[0] = initialized ? result.size() : 0;
202  data[1] = initialized ? s : 0;
203  MPI_MAX_VECTOR(data);
204  if (!initialized) {
205  if (data[0]) {
206  gmm::resize(result, data[0]);
207  s = data[1];
208  }
209  gmm::clear(result);
210  }
211  if (initialized)
212  GMM_ASSERT1(gmm::vect_size(result) == data[0] && s == data[1],
213  "Incompatible sizes");
214  MPI_SUM_VECTOR(result);
215  }
216 
217  virtual const mesh &linked_mesh() { return mti.linked_mesh(); }
218 
219  ga_interpolation_context_mti(const mesh_trans_inv &mti_, base_vector &r,
220  size_type nbdof_ = size_type(-1))
221  : result(r), mti(mti_), initialized(false), nbdof(nbdof_) {
222  if (nbdof == size_type(-1)) nbdof = mti.nb_points();
223  }
224  };
225 
226  // Distribute to be parallelized
227  void ga_interpolation_mti
228  (const getfem::model &md, const std::string &expr, mesh_trans_inv &mti,
229  base_vector &result, const mesh_region &rg, int extrapolation,
230  const mesh_region &rg_source, size_type nbdof) {
231 
232  ga_workspace workspace(md);
233  workspace.add_interpolation_expression(expr, mti.linked_mesh(), rg);
234 
235  mti.distribute(extrapolation, rg_source);
236  ga_interpolation_context_mti gic(mti, result, nbdof);
237  ga_interpolation(workspace, gic);
238  }
239 
240 
241  // Interpolation on a im_data
242  struct ga_interpolation_context_im_data
243  : public ga_interpolation_context {
244  base_vector &result;
245  const im_data &imd;
246  bool initialized;
247  size_type s;
248 
249  virtual bgeot::pstored_point_tab
250  ppoints_for_element(size_type cv, short_type f,
251  std::vector<size_type> &ind) const {
252  pintegration_method pim =imd.linked_mesh_im().int_method_of_element(cv);
253  if (pim->type() == IM_NONE) return bgeot::pstored_point_tab();
254  GMM_ASSERT1(pim->type() == IM_APPROX, "Sorry, exact methods cannot "
255  "be used in high level generic assembly");
256  size_type i_start(0), i_end(0);
257  if (f == short_type(-1))
258  i_end = pim->approx_method()->nb_points_on_convex();
259  else {
260  i_start = pim->approx_method()->ind_first_point_on_face(f);
261  i_end = i_start + pim->approx_method()->nb_points_on_face(f);
262  }
263  for (size_type i = i_start; i < i_end; ++i) ind.push_back(i);
264  return pim->approx_method()->pintegration_points();
265  }
266 
267  virtual bool use_pgp(size_type cv) const {
268  pintegration_method pim =imd.linked_mesh_im().int_method_of_element(cv);
269  if (pim->type() == IM_NONE) return false;
270  GMM_ASSERT1(pim->type() == IM_APPROX, "Sorry, exact methods cannot "
271  "be used in high level generic assembly");
272  return !(pim->approx_method()->is_built_on_the_fly());
273  }
274  virtual bool use_mim() const { return true; }
275 
276  virtual void store_result(size_type cv, size_type i, base_tensor &t) {
277  size_type si = t.size();
278  if (!initialized) {
279  s = si;
280  GMM_ASSERT1(imd.tensor_size() == t.sizes() ||
281  (imd.tensor_size().size() == size_type(1) &&
282  imd.tensor_size()[0] == size_type(1) &&
283  si == size_type(1)),
284  "Im_data tensor size " << imd.tensor_size() <<
285  " does not match the size of the interpolated "
286  "expression " << t.sizes() << ".");
287  gmm::resize(result, s * imd.nb_filtered_index());
288  gmm::clear(result);
289  initialized = true;
290  }
291  GMM_ASSERT1(s == si, "Internal error");
292  size_type ipt = imd.filtered_index_of_point(cv, i);
293  GMM_ASSERT1(ipt != size_type(-1),
294  "Im data with no data on the current integration point.");
295  gmm::add(t.as_vector(),
296  gmm::sub_vector(result, gmm::sub_interval(s*ipt, s)));
297  }
298 
299  virtual void finalize() {
300  std::vector<size_type> data(2);
301  data[0] = initialized ? result.size() : 0;
302  data[1] = initialized ? s : 0;
303  MPI_MAX_VECTOR(data);
304  if (initialized) {
305  GMM_ASSERT1(gmm::vect_size(result) == data[0] && s == data[1],
306  "Incompatible sizes");
307  } else {
308  if (data[0]) {
309  gmm::resize(result, data[0]);
310  s = data[1];
311  }
312  gmm::clear(result);
313  }
314  MPI_SUM_VECTOR(result);
315  }
316 
317  virtual const mesh &linked_mesh() { return imd.linked_mesh(); }
318 
319  ga_interpolation_context_im_data(const im_data &imd_, base_vector &r)
320  : result(r), imd(imd_), initialized(false) { }
321  };
322 
323  void ga_interpolation_im_data
324  (ga_workspace &workspace, const im_data &imd, base_vector &result) {
325  ga_interpolation_context_im_data gic(imd, result);
326  ga_interpolation(workspace, gic);
327  }
328 
329  void ga_interpolation_im_data
330  (const getfem::model &md, const std::string &expr, const im_data &imd,
331  base_vector &result, const mesh_region &rg) {
332  ga_workspace workspace(md);
333  workspace.add_interpolation_expression
334  (expr, imd.linked_mesh_im(), rg);
335 
336  ga_interpolation_im_data(workspace, imd, result);
337  }
338 
339 
340  // Interpolation on a stored_mesh_slice
341  struct ga_interpolation_context_mesh_slice
342  : public ga_interpolation_context {
343  base_vector &result;
344  const stored_mesh_slice &sl;
345  bool initialized;
346  size_type s;
347  std::vector<size_type> first_node;
348 
349  virtual bgeot::pstored_point_tab
350  ppoints_for_element(size_type cv, short_type f,
351  std::vector<size_type> &ind) const {
352  GMM_ASSERT1(f == short_type(-1), "No support for interpolation on faces"
353  " for a stored_mesh_slice yet.");
354  size_type ic = sl.convex_pos(cv);
355  const mesh_slicer::cs_nodes_ct &nodes = sl.nodes(ic);
356  std::vector<base_node> pt_tab(nodes.size());
357  for (size_type i=0; i < nodes.size(); ++i) {
358  pt_tab[i] = nodes[i].pt_ref;
359  ind.push_back(i);
360  }
361  return store_point_tab(pt_tab);
362  }
363 
364  virtual bool use_pgp(size_type /* cv */) const { return false; } // why not?
365  virtual bool use_mim() const { return false; }
366 
367  virtual void store_result(size_type cv, size_type i, base_tensor &t) {
368  size_type si = t.size();
369  if (!initialized) {
370  s = si;
371  gmm::resize(result, s * sl.nb_points());
372  gmm::clear(result);
373  initialized = true;
374  first_node.resize(sl.nb_convex());
375  for (size_type ic=0; ic < sl.nb_convex()-1; ++ic)
376  first_node[ic+1] = first_node[ic] + sl.nodes(ic).size();
377  }
378  GMM_ASSERT1(s == si && result.size() == s * sl.nb_points(), "Internal error");
379  size_type ic = sl.convex_pos(cv);
380  size_type ipt = first_node[ic] + i;
381  gmm::add(t.as_vector(),
382  gmm::sub_vector(result, gmm::sub_interval(s*ipt, s)));
383  }
384 
385  virtual void finalize() {
386  std::vector<size_type> data(2);
387  data[0] = initialized ? result.size() : 0;
388  data[1] = initialized ? s : 0;
389  MPI_MAX_VECTOR(data);
390  if (initialized) {
391  GMM_ASSERT1(gmm::vect_size(result) == data[0] && s == data[1],
392  "Incompatible sizes");
393  } else {
394  if (data[0]) {
395  gmm::resize(result, data[0]);
396  s = data[1];
397  }
398  gmm::clear(result);
399  }
400  MPI_SUM_VECTOR(result);
401  }
402 
403  virtual const mesh &linked_mesh() { return sl.linked_mesh(); }
404 
405  ga_interpolation_context_mesh_slice(const stored_mesh_slice &sl_,
406  base_vector &r)
407  : result(r), sl(sl_), initialized(false) { }
408  };
409 
410  void ga_interpolation_mesh_slice
411  (ga_workspace &workspace, const stored_mesh_slice &sl, base_vector &result) {
412  ga_interpolation_context_mesh_slice gic(sl, result);
413  ga_interpolation(workspace, gic);
414  }
415 
416  void ga_interpolation_mesh_slice
417  (const getfem::model &md, const std::string &expr, const stored_mesh_slice &sl,
418  base_vector &result, const mesh_region &rg) {
419  ga_workspace workspace(md);
420  workspace.add_interpolation_expression(expr, sl.linked_mesh(), rg);
421  ga_interpolation_mesh_slice(workspace, sl, result);
422  }
423 
424 
425  //=========================================================================
426  // Local projection functions
427  //=========================================================================
428 
429  void ga_local_projection(const getfem::model &md, const mesh_im &mim,
430  const std::string &expr, const mesh_fem &mf,
431  base_vector &result, const mesh_region &region) {
432 
433  // The case where the expression is a vector one and mf a scalar fem is
434  // not taken into account for the moment.
435 
436  // Could be improved by not performing the assembly of the global mass matrix
437  // working locally. This means a specific assembly.
438  size_type nbdof = mf.nb_dof();
439  model_real_sparse_matrix M(nbdof, nbdof);
440  asm_mass_matrix(M, mim, mf, region);
441  // FIXME: M should be cached for performance
442 
443  ga_workspace workspace(md, ga_workspace::inherit::NONE);
444  gmm::sub_interval I(0,nbdof);
445  workspace.add_fem_variable("c__dummy_var_95_", mf, I, base_vector(nbdof));
446  if (mf.get_qdims().size() > 1)
447  workspace.add_expression("("+expr+"):Test_c__dummy_var_95_",mim,region,2);
448  else
449  workspace.add_expression("("+expr+").Test_c__dummy_var_95_",mim,region,2);
450  getfem::base_vector F(nbdof);
451  workspace.set_assembled_vector(F);
452  workspace.assembly(1);
453  gmm::resize(result, nbdof);
454 
455  getfem::base_matrix loc_M;
456  getfem::base_vector loc_U;
457  for (mr_visitor v(region, mf.linked_mesh(), true); !v.finished(); ++v) {
458  if (mf.convex_index().is_in(v.cv())) {
459  size_type nd = mf.nb_basic_dof_of_element(v.cv());
460  loc_M.base_resize(nd, nd); gmm::resize(loc_U, nd);
461  gmm::sub_index J(mf.ind_basic_dof_of_element(v.cv()));
462  gmm::copy(gmm::sub_matrix(M, J, J), loc_M);
463  gmm::lu_solve(loc_M, loc_U, gmm::sub_vector(F, J));
464  gmm::copy(loc_U, gmm::sub_vector(result, J));
465  }
466  }
467  MPI_SUM_VECTOR(result);
468  }
469 
470  //=========================================================================
471  // Interpolate transformation with an expression
472  //=========================================================================
473 
474  struct rated_box_index_compare {
475  bool operator()(
476  const std::pair<scalar_type, const bgeot::box_index*> &x,
477  const std::pair<scalar_type, const bgeot::box_index*> &y) const {
478  GMM_ASSERT2(x.second != nullptr, "x contains nullptr");
479  GMM_ASSERT2(y.second != nullptr, "y contains nullptr");
480  return (x.first < y.first) || (!(y.first < x.first) && (x.second->id < y.second->id));
481  }
482  };
483 
484  class interpolate_transformation_expression
485  : public virtual_interpolate_transformation, public context_dependencies {
486 
487  struct workspace_gis_pair : public std::pair<ga_workspace, ga_instruction_set> {
488  inline ga_workspace &workspace() { return this->first; }
489  inline ga_instruction_set &gis() { return this->second; }
490  };
491 
492  const mesh &source_mesh;
493  const mesh &target_mesh;
494  const size_type target_region;
495  std::string expr;
496  mutable bgeot::rtree element_boxes;
497  mutable bool recompute_elt_boxes;
498  mutable ga_workspace local_workspace;
499  mutable ga_instruction_set local_gis;
500  mutable bgeot::geotrans_inv_convex gic;
501  mutable base_node P;
502  mutable std::set<var_trans_pair> used_vars;
503  mutable std::set<var_trans_pair> used_data;
504  mutable std::map<var_trans_pair,
505  workspace_gis_pair> compiled_derivatives;
506  mutable bool extract_variable_done;
507  mutable bool extract_data_done;
508 
509  private:
510  mutable std::map<size_type, std::vector<size_type>> box_to_convexes;
511 
512  public:
513  void update_from_context() const {
514  recompute_elt_boxes = true;
515  }
516 
517  void extract_variables(const ga_workspace &workspace,
518  std::set<var_trans_pair> &vars,
519  bool ignore_data, const mesh &/* m */,
520  const std::string &/* interpolate_name */) const {
521  if ((ignore_data && !extract_variable_done) ||
522  (!ignore_data && !extract_data_done)) {
523  if (ignore_data)
524  used_vars.clear();
525  else
526  used_data.clear();
527  ga_workspace aux_workspace(workspace, ga_workspace::inherit::ALL);
528  aux_workspace.clear_expressions();
529  aux_workspace.add_interpolation_expression(expr, source_mesh);
530  for (size_type i = 0; i < aux_workspace.nb_trees(); ++i)
531  ga_extract_variables(aux_workspace.tree_info(i).ptree->root,
532  aux_workspace, source_mesh,
533  ignore_data ? used_vars : used_data,
534  ignore_data);
535  if (ignore_data)
536  extract_variable_done = true;
537  else
538  extract_data_done = true;
539  }
540  if (ignore_data)
541  vars.insert(used_vars.begin(), used_vars.end());
542  else
543  vars.insert(used_data.begin(), used_data.end());
544  }
545 
546  void init(const ga_workspace &workspace) const {
547  size_type N = target_mesh.dim();
548 
549  // Expression compilation
550  local_workspace = ga_workspace(workspace, ga_workspace::inherit::ALL);
551  local_workspace.clear_expressions();
552 
553  local_workspace.add_interpolation_expression(expr, source_mesh);
554  local_gis = ga_instruction_set();
555  ga_compile_interpolation(local_workspace, local_gis);
556 
557  // In fact, transformations are not allowed ... for future compatibility
558  for (const std::string &transname : local_gis.transformations)
559  local_workspace.interpolate_transformation(transname)
560  ->init(local_workspace);
561 
562  if (!extract_variable_done) {
563  std::set<var_trans_pair> vars;
564  extract_variables(workspace, vars, true, source_mesh, "");
565  }
566 
567  for (const var_trans_pair &var : used_vars) {
568  workspace_gis_pair &pwi = compiled_derivatives[var];
569  pwi.workspace() = local_workspace;
570  pwi.gis() = ga_instruction_set();
571  if (pwi.workspace().nb_trees()) {
572  ga_tree der_tree = *(pwi.workspace().tree_info(0).ptree);
573  ga_derivative(der_tree, pwi.workspace(), source_mesh,
574  var.varname, var.transname, 1);
575  pwi.workspace().clear_expressions();
576  const std::string der_expr=ga_tree_to_string(der_tree);
577  pwi.workspace().add_interpolation_expression(der_expr, source_mesh);
578  ga_compile_interpolation(pwi.workspace(), pwi.gis());
579  }
580  }
581 
582  // Element_boxes update (if necessary)
583  if (recompute_elt_boxes) {
584 
585  box_to_convexes.clear();
586  element_boxes.clear();
587  base_node bmin(N), bmax(N);
588  const mesh_region &mr = target_mesh.region(target_region);
589  const dal::bit_vector&
590  convex_index = (target_region == mesh_region::all_convexes().id())
591  ? target_mesh.convex_index() : mr.index();
592  for (dal::bv_visitor cv(convex_index); !cv.finished(); ++cv) {
593 
594  bgeot::pgeometric_trans pgt = target_mesh.trans_of_convex(cv);
595 
596  size_type nbd_t = pgt->nb_points();
597  if (nbd_t) {
598  gmm::copy(target_mesh.points_of_convex(cv)[0], bmin);
599  gmm::copy(bmin, bmax);
600  } else {
601  gmm::clear(bmin);
602  gmm::clear(bmax);
603  }
604  mesh::ref_mesh_pt_ct cvpts = target_mesh.points_of_convex(cv);
605  for (short_type ip = 1; ip < nbd_t; ++ip) {
606  // size_type ind = target_mesh.ind_points_of_convex(cv)[ip];
607  const base_node &pt = cvpts[ip];
608  for (size_type k = 0; k < N; ++k) {
609  bmin[k] = std::min(bmin[k], pt[k]);
610  bmax[k] = std::max(bmax[k], pt[k]);
611  }
612  }
613 
614  scalar_type h = bmax[0] - bmin[0];
615  for (size_type k = 1; k < N; ++k) h = std::max(h, bmax[k]-bmin[k]);
616  if (pgt->is_linear()) h *= 1E-4;
617  for (auto &&val : bmin) val -= h*0.2;
618  for (auto &&val : bmax) val += h*0.2;
619 
620  box_to_convexes[element_boxes.add_box(bmin, bmax)].push_back(cv);
621  }
622  element_boxes.build_tree();
623  recompute_elt_boxes = false;
624  }
625  }
626 
627  void finalize() const {
628  for (const std::string &transname : local_gis.transformations)
629  local_workspace.interpolate_transformation(transname)->finalize();
630  local_gis = ga_instruction_set();
631  }
632 
633  std::string expression() const { return expr; }
634 
635  // all instructions, also those computing the base functions
636  static void exec(ga_instruction_set &gis, const mesh &m) {
637  for (auto &&instr : gis.all_instructions) {
638  GMM_ASSERT1(instr.second.m == &m,
639  "Incompatibility of meshes in interpolation");
640  auto &gilb = instr.second.begin_instructions;
641  for (size_type j = 0; j < gilb.size(); ++j) j += gilb[j]->exec();
642  auto &gile = instr.second.elt_instructions;
643  for (size_type j = 0; j < gile.size(); ++j) j += gile[j]->exec();
644  auto &gil = instr.second.instructions;
645  for (size_type j = 0; j < gil.size(); ++j) j += gil[j]->exec();
646  }
647  }
648 
649  int transform(const ga_workspace &/*workspace*/, const mesh &m,
650  fem_interpolation_context &ctx_x,
651  const base_small_vector &Normal,
652  const mesh **m_t,
653  size_type &cv, short_type &face_num,
654  base_node &P_ref,
655  base_small_vector &/*N_y*/,
656  std::map<var_trans_pair, base_tensor> &derivatives,
657  bool compute_derivatives) const {
658  int ret_type = 0;
659 
660  local_gis.ctx = ctx_x;
661  local_gis.Normal = Normal;
662  local_gis.nbpt = 1;
663  local_gis.ipt = 0;
664  local_gis.pai = 0;
665  gmm::clear(local_workspace.assembled_tensor().as_vector());
666 
667  exec(local_gis, m);
668 
669  GMM_ASSERT1(local_workspace.assembled_tensor().size()==target_mesh.dim(),
670  "Wrong dimension of the transformation expression");
671  P.resize(target_mesh.dim());
672  gmm::copy(local_workspace.assembled_tensor().as_vector(), P);
673 
674  *m_t = &target_mesh;
675 
676  bgeot::rtree::pbox_cont boxes;
677  {
678  bgeot::rtree::pbox_set bset;
679  element_boxes.find_boxes_at_point(P, bset);
680 
681  // using a std::set as a sorter
682  std::set<std::pair<scalar_type, const bgeot::box_index*>,
683  rated_box_index_compare> rated_boxes;
684  for (const auto &box : bset) {
685  scalar_type rating = scalar_type(1);
686  for (size_type i = 0; i < target_mesh.dim(); ++i) {
687  scalar_type h = box->max->at(i) - box->min->at(i);
688  if (h > scalar_type(0)) {
689  scalar_type r = std::min(box->max->at(i) - P[i],
690  P[i] - box->min->at(i)) / h;
691  rating = std::min(r, rating);
692  }
693  }
694  rated_boxes.insert(std::make_pair(rating, box));
695  }
696 
697  // boxes should now be ordered in increasing rating order
698  for (const auto &p : rated_boxes)
699  boxes.push_back(p.second);
700  }
701 
702  scalar_type best_dist(1e10);
703  size_type best_cv(-1);
704  base_node best_P_ref;
705  for (size_type i = boxes.size(); i > 0 && !ret_type; --i) {
706  for (auto convex : box_to_convexes.at(boxes[i-1]->id)) {
707  gic.init(target_mesh.points_of_convex(convex),
708  target_mesh.trans_of_convex(convex));
709 
710  bool converged;
711  bool is_in = gic.invert(P, P_ref, converged, 1E-4);
712  // cout << "cv = " << convex << " P = " << P << " P_ref = " << P_ref;
713  // cout << " is_in = " << int(is_in) << endl;
714  // for (size_type iii = 0;
715  // iii < target_mesh.points_of_convex(cv).size(); ++iii)
716  // cout << target_mesh.points_of_convex(cv)[iii] << endl;
717 
718  if (converged) {
719  cv = convex;
720  if (is_in) {
721  face_num = short_type(-1); // Should detect potential faces ?
722  ret_type = 1;
723  break;
724  } else {
725  scalar_type dist
726  = target_mesh.trans_of_convex(cv)->convex_ref()->is_in(P_ref);
727  if (dist < best_dist) {
728  best_dist = dist;
729  best_cv = cv;
730  best_P_ref = P_ref;
731  }
732  }
733  }
734  }
735  }
736 
737  if (ret_type == 0 && best_dist < 5e-3) {
738  cv = best_cv;
739  P_ref = best_P_ref;
740  face_num = short_type(-1); // Should detect potential faces ?
741  ret_type = 1;
742  }
743 
744  // Note on derivatives of the transformation : for efficiency and
745  // simplicity reasons, the derivative should be computed with
746  // the value of corresponding test functions. This means that
747  // for a transformation F(u) the computed derivative is F'(u).Test_u
748  // including the Test_u.
749  if (compute_derivatives) { // Could be optimized ?
750  for (auto &&d : derivatives) {
751  workspace_gis_pair &pwi = compiled_derivatives[d.first];
752  pwi.gis().ctx = ctx_x;
753  pwi.gis().Normal = Normal;
754  pwi.gis().nbpt = 1;
755  pwi.gis().ipt = 0;
756  pwi.gis().pai = 0;
757  gmm::clear(pwi.workspace().assembled_tensor().as_vector());
758  exec(pwi.gis(), m);
759  d.second = pwi.workspace().assembled_tensor();
760  }
761  }
762 
763  return ret_type;
764  }
765 
766  interpolate_transformation_expression
767  (const mesh &sm, const mesh &tm, size_type trg, const std::string &expr_)
768  : source_mesh(sm), target_mesh(tm), target_region(trg), expr(expr_),
769  recompute_elt_boxes(true), extract_variable_done(false),
770  extract_data_done(false)
771  { this->add_dependency(tm); }
772 
773  };
774 
775 
777  (ga_workspace &workspace, const std::string &name, const mesh &sm,
778  const mesh &tm, const std::string &expr) {
780  (workspace, name, sm, tm, size_type(-1), expr);
781  }
782 
784  (ga_workspace &workspace, const std::string &name, const mesh &sm,
785  const mesh &tm, size_type trg, const std::string &expr) {
786  pinterpolate_transformation
787  p = std::make_shared<interpolate_transformation_expression>
788  (sm, tm, trg, expr);
789  workspace.add_interpolate_transformation(name, p);
790  }
791 
793  (model &md, const std::string &name, const mesh &sm, const mesh &tm,
794  const std::string &expr) {
796  (md, name, sm, tm, size_type(-1), expr);
797  }
798 
800  (model &md, const std::string &name, const mesh &sm, const mesh &tm,
801  size_type trg, const std::string &expr) {
802  pinterpolate_transformation
803  p = std::make_shared<interpolate_transformation_expression>
804  (sm, tm, trg, expr);
805  md.add_interpolate_transformation(name, p);
806  }
807 
808  //=========================================================================
809  // Interpolate transformation on neighbor element (for internal faces)
810  //=========================================================================
811 
812  class interpolate_transformation_neighbor
813  : public virtual_interpolate_transformation, public context_dependencies {
814 
815  public:
816  void update_from_context() const {}
817  void extract_variables(const ga_workspace &/* workspace */,
818  std::set<var_trans_pair> &/* vars */,
819  bool /* ignore_data */, const mesh &/* m */,
820  const std::string &/* interpolate_name */) const {}
821  void init(const ga_workspace &/* workspace */) const {}
822  void finalize() const {}
823 
824  std::string expression() const { return "X"; }
825 
826  int transform(const ga_workspace &/*workspace*/, const mesh &m_x,
827  fem_interpolation_context &ctx_x,
828  const base_small_vector &/*Normal*/, const mesh **m_t,
829  size_type &cv, short_type &face_num,
830  base_node &P_ref,
831  base_small_vector &/*N_y*/,
832  std::map<var_trans_pair, base_tensor> &/*derivatives*/,
833  bool compute_derivatives) const {
834 
835  int ret_type = 0;
836  *m_t = &m_x;
837  size_type cv_x = ctx_x.convex_num();
838  short_type face_x = ctx_x.face_num();
839  GMM_ASSERT1(face_x != short_type(-1), "Neighbor transformation can "
840  "only be applied to internal faces");
841 
842  auto adj_face = m_x.adjacent_face(cv_x, face_x);
843 
844  if (adj_face.cv != size_type(-1)) {
846  gic.init(m_x.points_of_convex(adj_face.cv),
847  m_x.trans_of_convex(adj_face.cv));
848  bool converged = true;
849  gic.invert(ctx_x.xreal(), P_ref, converged);
850  bool is_in = (ctx_x.pgt()->convex_ref()->is_in(P_ref) < 1E-4);
851  GMM_ASSERT1(is_in && converged, "Geometric transformation inversion "
852  "has failed in neighbor transformation");
853  face_num = adj_face.f;
854  cv = adj_face.cv;
855  ret_type = 1;
856  }
857  GMM_ASSERT1(!compute_derivatives,
858  "No derivative for this transformation");
859  return ret_type;
860  }
861 
862  interpolate_transformation_neighbor() { }
863 
864  };
865 
866 
867  pinterpolate_transformation interpolate_transformation_neighbor_instance() {
868  return (std::make_shared<interpolate_transformation_neighbor>());
869  }
870 
871  //=========================================================================
872  // Interpolate transformation on neighbor element (for extrapolation)
873  //=========================================================================
874 
875  class interpolate_transformation_element_extrapolation
876  : public virtual_interpolate_transformation, public context_dependencies {
877 
878  const mesh &sm;
879  std::map<size_type, size_type> elt_corr;
880 
881  public:
882  void update_from_context() const {}
883  void extract_variables(const ga_workspace &/* workspace */,
884  std::set<var_trans_pair> &/* vars */,
885  bool /* ignore_data */, const mesh &/* m */,
886  const std::string &/* interpolate_name */) const {}
887  void init(const ga_workspace &/* workspace */) const {}
888  void finalize() const {}
889  std::string expression() const { return "X"; }
890 
891  int transform(const ga_workspace &/*workspace*/, const mesh &m_x,
892  fem_interpolation_context &ctx_x,
893  const base_small_vector &/*Normal*/, const mesh **m_t,
894  size_type &cv, short_type &face_num,
895  base_node &P_ref,
896  base_small_vector &/*N_y*/,
897  std::map<var_trans_pair, base_tensor> &/*derivatives*/,
898  bool compute_derivatives) const {
899  int ret_type = 0;
900  *m_t = &m_x;
901  GMM_ASSERT1(&sm == &m_x, "Bad mesh");
902  size_type cv_x = ctx_x.convex_num(), cv_y = cv_x;
903  auto it = elt_corr.find(cv_x);
904  if (it != elt_corr.end()) cv_y = it->second;
905 
906  if (cv_x != cv_y) {
908  gic.init(m_x.points_of_convex(cv_y),
909  m_x.trans_of_convex(cv_y));
910  bool converged = true;
911  gic.invert(ctx_x.xreal(), P_ref, converged, 1E-4);
912  GMM_ASSERT1(converged, "Geometric transformation inversion "
913  "has failed in element extrapolation transformation");
914  face_num = short_type(-1);
915  cv = cv_y;
916  ret_type = 1;
917  } else {
918  cv = cv_x;
919  face_num = short_type(-1);
920  P_ref = ctx_x.xref();
921  ret_type = 1;
922  }
923  GMM_ASSERT1(!compute_derivatives,
924  "No derivative for this transformation");
925  return ret_type;
926  }
927 
928  void set_correspondence(const std::map<size_type, size_type> &ec) {
929  elt_corr = ec;
930  }
931 
932  interpolate_transformation_element_extrapolation
933  (const mesh &sm_, const std::map<size_type, size_type> &ec)
934  : sm(sm_), elt_corr(ec) { }
935  };
936 
937 
938  void add_element_extrapolation_transformation
939  (model &md, const std::string &name, const mesh &sm,
940  std::map<size_type, size_type> &elt_corr) {
941  pinterpolate_transformation
942  p = std::make_shared<interpolate_transformation_element_extrapolation>
943  (sm, elt_corr);
944  md.add_interpolate_transformation(name, p);
945  }
946 
947  void add_element_extrapolation_transformation
948  (ga_workspace &workspace, const std::string &name, const mesh &sm,
949  std::map<size_type, size_type> &elt_corr) {
950  pinterpolate_transformation
951  p = std::make_shared<interpolate_transformation_element_extrapolation>
952  (sm, elt_corr);
953  workspace.add_interpolate_transformation(name, p);
954  }
955 
956  void set_element_extrapolation_correspondence
957  (ga_workspace &workspace, const std::string &name,
958  std::map<size_type, size_type> &elt_corr) {
959  GMM_ASSERT1(workspace.interpolate_transformation_exists(name),
960  "Unknown transformation");
961  auto pit=workspace.interpolate_transformation(name).get();
962  auto cpext
963  = dynamic_cast<const interpolate_transformation_element_extrapolation *>
964  (pit);
965  GMM_ASSERT1(cpext,
966  "The transformation is not of element extrapolation type");
967  const_cast<interpolate_transformation_element_extrapolation *>(cpext)
968  ->set_correspondence(elt_corr);
969  }
970 
971  void set_element_extrapolation_correspondence
972  (model &md, const std::string &name,
973  std::map<size_type, size_type> &elt_corr) {
974  GMM_ASSERT1(md.interpolate_transformation_exists(name),
975  "Unknown transformation");
976  auto pit=md.interpolate_transformation(name).get();
977  auto cpext
978  = dynamic_cast<const interpolate_transformation_element_extrapolation *>
979  (pit);
980  GMM_ASSERT1(cpext,
981  "The transformation is not of element extrapolation type");
982  const_cast<interpolate_transformation_element_extrapolation *>(cpext)
983  ->set_correspondence(elt_corr);
984  }
985 
986 
987  //=========================================================================
988  // Secondary domains
989  //=========================================================================
990 
991 
992  class standard_secondary_domain : public virtual_secondary_domain {
993 
994  public:
995 
996  virtual const mesh_region &give_region(const mesh &,
997  size_type, short_type) const
998  { return region; }
999  // virtual void init(const ga_workspace &workspace) const = 0;
1000  // virtual void finalize() const = 0;
1001 
1002  standard_secondary_domain(const mesh_im &mim__, const mesh_region &region_)
1003  : virtual_secondary_domain(mim__, region_) {}
1004  };
1005 
1006  void add_standard_secondary_domain
1007  (model &md, const std::string &name, const mesh_im &mim,
1008  const mesh_region &rg) {
1009  psecondary_domain p = std::make_shared<standard_secondary_domain>(mim, rg);
1010  md.add_secondary_domain(name, p);
1011  }
1012 
1013  void add_standard_secondary_domain
1014  (ga_workspace &workspace, const std::string &name, const mesh_im &mim,
1015  const mesh_region &rg) {
1016  psecondary_domain p = std::make_shared<standard_secondary_domain>(mim, rg);
1017  workspace.add_secondary_domain(name, p);
1018  }
1019 
1020 
1021 } /* end of namespace */
does the inversion of the geometric transformation for a given convex
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 ...
Balanced tree of n-dimensional rectangles.
Definition: bgeot_rtree.h:97
Describe a finite element method linked to a mesh.
virtual ind_dof_ct ind_basic_dof_of_element(size_type cv) const
Give an array of the dof numbers a of convex.
virtual size_type nb_dof() const
Return the total number of degrees of freedom.
const mesh & linked_mesh() const
Return a reference to the underlying mesh.
const dal::bit_vector & convex_index() const
Get the set of convexes where a finite element has been assigned.
virtual size_type nb_basic_dof_of_element(size_type cv) const
Return the number of degrees of freedom attached to a given convex.
Describe an integration method linked to a mesh.
"iterator" class for regions.
structure used to hold a set of convexes and/or convex faces.
static mesh_region all_convexes()
provide a default value for the mesh_region parameters of assembly procedures etc.
Describe a mesh (collection of convexes (elements) and points).
Definition: getfem_mesh.h:98
`‘Model’' variables store the variables, the data and the description of a model.
Semantic analysis of assembly trees and semantic manipulations.
Compilation and execution operations.
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 resize(V &v, size_type n)
*‍/
Definition: gmm_blas.h:209
void add(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:1275
void asm_mass_matrix(const MAT &M, const mesh_im &mim, const mesh_fem &mf1, const mesh_region &rg=mesh_region::all_convexes())
generic mass matrix assembly (on the whole mesh or on the specified convex set or boundary)
std::shared_ptr< const getfem::virtual_fem > pfem
type of pointer on a fem description
Definition: getfem_fem.h:243
gmm::uint16_type short_type
used as the common short type integer in the library
Definition: bgeot_config.h:72
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
GEneric Tool for Finite Element Methods.
pinterpolate_transformation interpolate_transformation_neighbor_instance()
Create a new instance of a transformation corresponding to the interpolation on the neighbor element.
void add_interpolate_transformation_from_expression(ga_workspace &workspace, const std::string &transname, const mesh &source_mesh, const mesh &target_mesh, const std::string &expr)
Add a transformation to a workspace workspace or a model md mapping point in mesh source_mesh to mesh...
void ga_local_projection(const getfem::model &md, const mesh_im &mim, const std::string &expr, const mesh_fem &mf, base_vector &result, const mesh_region &rg=mesh_region::all_convexes())
Make an elementwise L2 projection of an expression with respect to the mesh_fem mf.