GetFEM  5.6
getfem_mesh_im_level_set.cc
1 /*===========================================================================
2 
3  Copyright (C) 2005-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 
22 #include "getfem/getfem_mesher.h"
23 #include "getfem/bgeot_kdtree.h"
24 
25 namespace getfem {
26 
28  { is_adapted = false; }
29 
30  void mesh_im_level_set::clear_build_methods() {
31  for (size_type i = 0; i < build_methods.size(); ++i)
32  del_stored_object(build_methods[i]);
33  build_methods.clear();
34  cut_im.clear();
35  }
36 
37  void mesh_im_level_set::clear(void) {
38  mesh_im::clear();
39  clear_build_methods();
40  is_adapted = false;
41  }
42 
43  void mesh_im_level_set::init_with_mls(mesh_level_set &me,
44  int integrate_where_,
45  pintegration_method reg,
46  pintegration_method sing) {
47  init_with_mesh(me.linked_mesh());
48  cut_im.init_with_mesh(me.linked_mesh());
49  mls = &me;
50  integrate_where = integrate_where_;
51  set_simplex_im(reg, sing);
52  this->add_dependency(*mls);
53  is_adapted = false;
54  }
55 
56  mesh_im_level_set::mesh_im_level_set(mesh_level_set &me,
57  int integrate_where_,
58  pintegration_method reg,
59  pintegration_method sing) {
60  mls = 0;
61  init_with_mls(me, integrate_where_, reg, sing);
62  }
63 
64  mesh_im_level_set::mesh_im_level_set(void)
65  { mls = 0; is_adapted = false; }
66 
67 
68  pintegration_method
70  if (!is_adapted) const_cast<mesh_im_level_set *>(this)->adapt();
71  if (cut_im.convex_index().is_in(cv))
72  return cut_im.int_method_of_element(cv);
73  else {
74  if (ignored_im.is_in(cv)) //integrate_where == INTEGRATE_BOUNDARY)
75  return getfem::im_none();
76 
78  }
79  }
80 
81  DAL_SIMPLE_KEY(special_imls_key, papprox_integration);
82 
83  /* only for INTEGRATE_INSIDE or INTEGRATE_OUTSIDE */
84  mesh_im_level_set::bool2 mesh_im_level_set::is_point_in_selected_area2
85  (const std::vector<pmesher_signed_distance> &mesherls0,
86  const std::vector<pmesher_signed_distance> &mesherls1,
87  const base_node& P) {
88  bool isin = true;
89  int isbin = 0;
90  for (unsigned i = 0; i < mls->nb_level_sets(); ++i) {
91  isin = isin && ((*(mesherls0[i]))(P) < 0);
92  if (gmm::abs((*(mesherls0[i]))(P)) < 1e-7)
93  isbin = i+1;
94  if (mls->get_level_set(i)->has_secondary())
95  isin = isin && ((*(mesherls1[i]))(P) < 0);
96  }
97  bool2 b;
98  b.in = ((integrate_where & INTEGRATE_OUTSIDE)) ? !isin : isin;
99  b.bin = isbin;
100  return b;
101  }
102 
103 
104  /* very rustic set operations evaluator */
105  struct is_in_eval {
106  dal::bit_vector in; // levelsets for which the point is "inside"
107  dal::bit_vector bin; // levelsets for which the point is on the boundary
108  typedef mesh_im_level_set::bool2 bool2;
109  bool2 do_expr(const char *&s) {
110  bool2 r;
111  if (*s == '(') {
112  r = do_expr(++s);
113  GMM_ASSERT1(*s++ == ')',
114  "expecting ')' in csg expression at '" << s-1 << "'");
115  } else if (*s == '!') { // complementary
116  r = do_expr(++s); r.in = !r.in;
117  } else if (*s >= 'a' && *s <= 'z') {
118  unsigned idx = (*s) - 'a';
119  r.in = in.is_in(idx);
120  r.bin = bin.is_in(idx) ? idx+1 : 0;
121  ++s;
122  } else
123  GMM_ASSERT1(false, "parse error in csg expression at '" << s << "'");
124  if (*s == '+') { // Union
125  //cerr << "s = " << s << ", r = " << r << "\n";
126  bool2 a = r, b = do_expr(++s);
127  //cerr << "->b = " << b << "\n";
128  r.in = b.in || a.in;
129  if (b.bin && !a.in) r.bin = b.bin;
130  else if (a.bin && !b.in) r.bin = a.bin;
131  else r.bin = 0;
132  } else if (*s == '-') { // Set difference
133  bool2 a = r, b = do_expr(++s);
134  r.in = a.in && !b.in;
135  if (a.bin && !b.in) r.bin = a.bin;
136  else if (a.in && b.bin) r.bin = b.bin;
137  else r.bin = 0;
138  } else if (*s == '*') { // Intersection
139  bool2 a = r, b = do_expr(++s);
140  r.in = a.in && b.in;
141  if (a.bin && b.in) r.bin = a.bin;
142  else if (a.in && b.bin) r.bin = b.bin;
143  else r.bin = 0;
144  }
145  return r;
146  }
147  bool2 is_in(const char*s) {
148  bool2 b = do_expr(s);
149  GMM_ASSERT1(!(*s), "parse error in CSG expression at " << s);
150  return b;
151  }
152  void check() {
153  const char *s[] = { "a*b*c*d",
154  "a+b+c+d",
155  "(a+b+c+d)",
156  "d*(a+b+c)",
157  "(a+b)-(c+d)",
158  "((a+b)-(c+d))",
159  "!a",
160  0 };
161  for (const char **p = s; *p; ++p)
162  cerr << *p << "\n";
163  for (unsigned c=0; c < 16; ++c) {
164  in[0] = (c&1); bin[0] = 1;
165  in[1] = (c&2); bin[1] = 1;
166  in[2] = (c&4); bin[2] = 1;
167  in[3] = (c&8); bin[3] = 1;
168  cerr << in[0] << in[1] << in[2] << in[3] << ": ";
169  for (const char **p = s; *p; ++p) {
170  bool2 b = is_in(*p);
171  cerr << b.in << "/" << b.bin << " ";
172  }
173  cerr << "\n";
174  }
175  }
176  };
177 
178  mesh_im_level_set::bool2
179  mesh_im_level_set::is_point_in_selected_area
180  (const std::vector<pmesher_signed_distance> &mesherls0,
181  const std::vector<pmesher_signed_distance> &mesherls1,
182  const base_node& P) {
183  is_in_eval ev;
184  for (unsigned i = 0; i < mls->nb_level_sets(); ++i) {
185  bool sec = mls->get_level_set(i)->has_secondary();
186  scalar_type d1 = (*(mesherls0[i]))(P);
187  scalar_type d2 = (sec ? (*(mesherls1[i]))(P) : -1);
188  if (d1 < 0 && d2 < 0) ev.in.add(i);
189  // if ((integrate_where & INTEGRATE_OUTSIDE) /*&& !sec*/)
190  // ev.in[i].flip();
191 
192  if (gmm::abs(d1) < 1e-7 && d2 < 1e-7)
193  ev.bin.add(i);
194  }
195 
196 
197  bool2 r;
198  if (ls_csg_description.size())
199  r = ev.is_in(ls_csg_description.c_str());
200  else {
201  r.in = (ev.in.card() == mls->nb_level_sets());
202  r.bin = (ev.bin.card() >= 1 && ev.in.card() >= mls->nb_level_sets()-1);
203  }
204 
205  if (integrate_where & INTEGRATE_OUTSIDE) r.in = !(r.in);
206 
207 
208 
209  /*bool2 r2 = is_point_in_selected_area2(mesherls0,mesherls1,P);
210  if (r2.in != r.in || r2.bin != r.bin) {
211  cerr << "ev.in = " << ev.in << ", bin=" << ev.bin<<"\n";
212  cerr << "is_point_in_selected_area2("<<P <<"): r="<<r.in<<"/"<<r.bin
213  << ", r2=" << r2.in<<"/"<<r2.bin <<"\n";
214  assert(0);
215  }*/
216 
217  return r;
218  }
219 
220  void mesh_im_level_set::build_method_of_convex(size_type cv) {
221  const mesh &msh(mls->mesh_of_convex(cv));
222  GMM_ASSERT3(msh.convex_index().card() != 0, "Internal error");
223  base_matrix G;
224  base_node B;
225 
226  std::vector<pmesher_signed_distance> mesherls0(mls->nb_level_sets());
227  std::vector<pmesher_signed_distance> mesherls1(mls->nb_level_sets());
228  dal::bit_vector convexes_arein;
229 
230  //std::fstream totof("totof", std::ios::out | std::ios::app);
231  for (unsigned i = 0; i < mls->nb_level_sets(); ++i) {
232  mesherls0[i] = mls->get_level_set(i)->mls_of_convex(cv, 0, false);
233  if (mls->get_level_set(i)->has_secondary())
234  mesherls1[i] = mls->get_level_set(i)->mls_of_convex(cv, 1, false);
235  }
236 
237  if (integrate_where != (INTEGRATE_ALL)) {
238  for (dal::bv_visitor scv(msh.convex_index()); !scv.finished(); ++scv) {
239  B = gmm::mean_value(msh.points_of_convex(scv));
240  convexes_arein[scv] =
241  is_point_in_selected_area(mesherls0, mesherls1, B).in;
242  }
243  }
244 
245  bgeot::pgeometric_trans pgt = linked_mesh().trans_of_convex(cv);
247  = msh.trans_of_convex(msh.convex_index().first_true());
248  dim_type n = pgt->dim();
249 
250  if (base_singular_pim) GMM_ASSERT1
251  ((n != 2 ||
252  base_singular_pim->structure()== bgeot::parallelepiped_structure(2))
253  && (n != 3
254  || base_singular_pim->structure() == bgeot::prism_P1_structure(3))
255  && (n >= 2) && (n <= 3),
256  "Base integration method for quasi polar integration not convenient");
257 
258  auto new_approx = std::make_shared<approx_integration>(pgt->convex_ref());
259  new_approx->set_built_on_the_fly();
260  base_matrix KK(n,n), CS(n,n);
261  base_matrix pc(pgt2->nb_points(), n);
262  std::vector<size_type> ptsing;
263 
264  // cout << "testing convex " << cv << ", " << msh.convex_index().card() << " subconvexes\n";
265 
266  for (dal::bv_visitor i(msh.convex_index()); !i.finished(); ++i) {
267  papprox_integration pai = regular_simplex_pim->approx_method();
268 
269  GMM_ASSERT1(regular_simplex_pim->structure() == bgeot::simplex_structure(n), "Base integration method should be defined on a simplex of same dimension than the mesh");
270 
271  if ((integrate_where != INTEGRATE_ALL) &&
272  !convexes_arein[i]) continue;
273 
274  if (base_singular_pim && mls->crack_tip_convexes().is_in(cv)) {
275  ptsing.resize(0);
276  unsigned sing_ls = unsigned(-1);
277 
278  for (unsigned ils = 0; ils < mls->nb_level_sets(); ++ils)
279  if (mls->get_level_set(ils)->has_secondary()) {
280  for (unsigned ipt = 0; ipt <= n; ++ipt) {
281  // the vertex ipt (not the node ipt: curved sub-simplices
282  // have more nodes than vertices)
283  const base_node &P = msh.points_of_convex(i)
284  [msh.structure_of_convex(i)->ind_dir_points()[ipt]];
285  if (gmm::abs((*(mesherls0[ils]))(P)) < 1E-10
286  && gmm::abs((*(mesherls1[ils]))(P)) < 1E-10) {
287  if (sing_ls == unsigned(-1)) sing_ls = ils;
288  GMM_ASSERT1(sing_ls == ils, "Two singular point in one "
289  "sub element : " << sing_ls << ", " << ils <<
290  ". To be done.");
291  ptsing.push_back(ipt);
292  }
293  }
294  }
295  assert(ptsing.size() < n);
296 
297  if (ptsing.size() > 0) {
298  std::stringstream sts;
299  sts << "IM_QUASI_POLAR(" << name_of_int_method(base_singular_pim)
300  << ", " << ptsing[0];
301  if (ptsing.size() > 1) sts << ", " << ptsing[1];
302  sts << ")";
303  pai = int_method_descriptor(sts.str())->approx_method();
304  }
305  }
306 
307  base_matrix G2;
308  vectors_to_base_matrix(G2, linked_mesh().points_of_convex(cv));
310  cc(linked_mesh().trans_of_convex(cv), pai->point(0), G2);
311 
312  if (integrate_where & (INTEGRATE_INSIDE | INTEGRATE_OUTSIDE)) {
313 
314  vectors_to_base_matrix(G, msh.points_of_convex(i));
315  bgeot::geotrans_interpolation_context c(msh.trans_of_convex(i),
316  pai->point(0), G);
317 
318  for (size_type j = 0; j < pai->nb_points_on_convex(); ++j) {
319  c.set_xref(pai->point(j));
320  pgt2->poly_vector_grad(pai->point(j), pc);
321  gmm::mult(G,pc,KK);
322  scalar_type J = gmm::lu_det(KK);
323  new_approx->add_point(c.xreal(), pai->coeff(j) * gmm::abs(J));
324 
325  /*if (integrate_where == INTEGRATE_INSIDE) {
326  cc.set_xref(c.xreal());
327  totof << cc.xreal()[0] << "\t" << cc.xreal()[1] << "\t"
328  << pai->coeff(j) * gmm::abs(J) << "\n";
329  }*/
330  }
331  }
332 
333  // pgt2 = msh.trans_of_convex(i);
334 
335  for (short_type f = 0; f < pgt2->structure()->nb_faces(); ++f) {
336  short_type ff = short_type(-1);
337  unsigned isin = unsigned(-1);
338 
339  if (integrate_where == INTEGRATE_BOUNDARY) {
340  bool lisin = true;
341  for (const base_node &pt : msh.points_of_face_of_convex(i, f)) {
342  isin = is_point_in_selected_area(mesherls0, mesherls1, pt).bin;
343  //cerr << pt << ":" << isin << " ";
344  if (!isin) { lisin = false; break; }
345  }
346  if (!lisin) continue;
347  else isin--;
348  // a face between two selected sub-convexes is not on the boundary
349  // (e.g. a sub-convex with all its vertices on the level set)
350  size_type nb = msh.neighbor_of_convex(i, f);
351  if (nb != size_type(-1) && convexes_arein.is_in(nb)) continue;
352  } else {
353  B = gmm::mean_value(msh.points_of_face_of_convex(i, f));
354  if (pgt->convex_ref()->is_in(B) < -1E-7) continue;
355  for (short_type fi = 0; fi < pgt->structure()->nb_faces(); ++fi) {
356  if (gmm::abs(pgt->convex_ref()->is_in_face(fi, B)) < 2E-6) ff = fi;
357  }
358 
359  if (ff == short_type(-1)) {
360  cout << "Distance to the element : "
361  << pgt->convex_ref()->is_in(B) << endl;
362  for (short_type fi = 0; fi < pgt->structure()->nb_faces(); ++fi) {
363  cout << "Distance to face " << fi << " : "
364  << gmm::abs(pgt->convex_ref()->is_in_face(fi, B)) << endl;
365  }
366  GMM_ASSERT3(false, "the point is neither in the interior nor "
367  "the boundary of the element");
368  }
369  }
370 
371  vectors_to_base_matrix(G, msh.points_of_convex(i));
372  bgeot::geotrans_interpolation_context c(msh.trans_of_convex(i),
373  pai->point(0), G);
374 
375 
376  for (size_type j = 0; j < pai->nb_points_on_face(f); ++j) {
377  if (gmm::abs(c.J()) > 1E-11) {
378  c.set_xref(pai->point_on_face(f, j));
379  base_small_vector un = pgt2->normals()[f], up(msh.dim());
380  gmm::mult(c.B(), un, up);
381  scalar_type nup = gmm::vect_norm2(up);
382 
383  scalar_type nnup(1);
384  if (integrate_where == INTEGRATE_BOUNDARY) {
385  cc.set_xref(c.xreal());
386  mesherls0[isin]->grad(c.xreal(), un);
387  un /= gmm::vect_norm2(un);
388  gmm::mult(cc.B(), un, up);
389  nnup = gmm::vect_norm2(up);
390  }
391  new_approx->add_point(c.xreal(), pai->coeff_on_face(f, j)
392  * gmm::abs(c.J()) * nup * nnup, ff);
393  }
394  }
395  }
396  }
397 
398  if (new_approx->nb_points()) {
399  new_approx->valid_method();
400  pintegration_method
401  pim = std::make_shared<integration_method>(new_approx);
402  dal::pstatic_stored_object_key
403  pk = std::make_shared<special_imls_key>(new_approx);
404  dal::add_stored_object(pk, pim, new_approx->ref_convex(),
405  new_approx->pintegration_points());
406  build_methods.push_back(pim);
407  cut_im.set_integration_method(cv, pim);
408  }
409  }
410 
412  GMM_ASSERT1(linked_mesh_ != 0, "mesh level set uninitialized");
413  context_check();
414  clear_build_methods();
415  ignored_im.clear();
416  for (dal::bv_visitor cv(linked_mesh().convex_index());
417  !cv.finished(); ++cv) {
418  if (mls->is_convex_cut(cv)) build_method_of_convex(cv);
419 
420  if (!cut_im.convex_index().is_in(cv)) {
421  /* not exclusive with mls->is_convex_cut ... sometimes, cut cv
422  contains no integration points.. */
423 
424  if (integrate_where == INTEGRATE_BOUNDARY) {
425  ignored_im.add(cv);
426  } else if (integrate_where != (INTEGRATE_OUTSIDE|INTEGRATE_INSIDE)) {
427  /* remove convexes that are not in the integration area */
428  std::vector<pmesher_signed_distance> mesherls0(mls->nb_level_sets());
429  std::vector<pmesher_signed_distance> mesherls1(mls->nb_level_sets());
430  for (unsigned i = 0; i < mls->nb_level_sets(); ++i) {
431  mesherls0[i] = mls->get_level_set(i)->mls_of_convex(cv, 0, false);
432  if (mls->get_level_set(i)->has_secondary())
433  mesherls1[i] = mls->get_level_set(i)->mls_of_convex(cv,1, false);
434  }
435 
436  base_node B(gmm::mean_value(linked_mesh().trans_of_convex(cv)
437  ->convex_ref()->points()));
438  if (!is_point_in_selected_area(mesherls0, mesherls1, B).in)
439  ignored_im.add(cv);
440  }
441  }
442  }
443  is_adapted = true; touch();
444  // cout << "Number of built methods : " << build_methods.size() << endl;
445  }
446 
447  void mesh_im_level_set::compute_normal_vector
448  (const fem_interpolation_context &ctx, base_small_vector &vec) const {
449  size_type nb_ls = mls->nb_level_sets(), j = 0;
450  std::vector<pmesher_signed_distance> mesherls0(nb_ls);
451  if (vec.size() != linked_mesh().dim()) vec.resize(linked_mesh().dim());
452  base_small_vector un(ctx.pgt()->dim());
453 
454  if (nb_ls == 0) {
455  gmm::clear(vec); return;
456  } else if (nb_ls == 1) {
457  mesherls0[0]
458  = mls->get_level_set(0)->mls_of_convex(ctx.convex_num(), 0, false);
459  } else {
460  scalar_type d_min(0);
461  for (unsigned i = 0; i < nb_ls; ++i) {
462  mesherls0[i]
463  = mls->get_level_set(i)->mls_of_convex(ctx.convex_num(), 0, false);
464  scalar_type d = gmm::abs((*(mesherls0[i]))(ctx.xref()));
465  if (i == 0 || d < d_min) { d_min = d; j = i; }
466  }
467  }
468  mesherls0[j]->grad(ctx.xref(), un);
469  gmm::mult(ctx.B(), un, vec);
470  scalar_type no = gmm::vect_norm2(vec);
471  if (no != scalar_type(0))
472  gmm::scale(vec, scalar_type(1) / gmm::vect_norm2(vec));
473  }
474 
475 
476 
478  { is_adapted = false; }
479 
480  void mesh_im_cross_level_set::clear_build_methods() {
481  for (size_type i = 0; i < build_methods.size(); ++i)
482  if (build_methods[i].get()) del_stored_object(build_methods[i]);
483  build_methods.clear();
484  cut_im.clear();
485  }
486 
487  void mesh_im_cross_level_set::clear(void)
488  { mesh_im::clear(); clear_build_methods(); is_adapted = false; }
489 
490  void mesh_im_cross_level_set::init_with_mls(mesh_level_set &me,
491  size_type ind_ls1_, size_type ind_ls2_,
492  pintegration_method pim) {
493  init_with_mesh(me.linked_mesh());
494  cut_im.init_with_mesh(me.linked_mesh());
495  mls = &me;
496  ind_ls1 = ind_ls1_; ind_ls2 = ind_ls2_;
497  set_segment_im(pim);
498  this->add_dependency(*mls);
499  is_adapted = false;
500  }
501 
502  mesh_im_cross_level_set::mesh_im_cross_level_set(mesh_level_set &me,
503  size_type ind_ls1_, size_type ind_ls2_,
504  pintegration_method pim)
505  { mls = 0; init_with_mls(me, ind_ls1_, ind_ls2_, pim); }
506 
507  mesh_im_cross_level_set::mesh_im_cross_level_set(void)
508  { mls = 0; is_adapted = false; }
509 
510 
511  pintegration_method
513  if (!is_adapted) const_cast<mesh_im_cross_level_set *>(this)->adapt();
514  if (cut_im.convex_index().is_in(cv))
515  return cut_im.int_method_of_element(cv);
516  else {
517  if (ignored_im.is_in(cv)) return getfem::im_none();
518 
520  }
521  }
522 
523  static bool is_point_in_intersection
524  (const std::vector<pmesher_signed_distance> &mesherls0,
525  const std::vector<pmesher_signed_distance> &mesherls1,
526  const base_node& P) {
527 
528  bool r = true;
529  for (unsigned i = 0; i < mesherls0.size(); ++i) {
530  bool sec = (dynamic_cast<const mesher_level_set *>(mesherls1[i].get()))->is_initialized();
531  scalar_type d1 = (*(mesherls0[i]))(P);
532  scalar_type d2 = (sec ? (*(mesherls1[i]))(P) : -1);
533  if (!(gmm::abs(d1) < 1e-7 && d2 < 1e-7)) r = false;
534  }
535  return r;
536  }
537 
538  static bool is_edges_intersect(const base_node &PP1, const base_node &PP2,
539  const base_node &PR1, const base_node &PR2) {
540  size_type n = gmm::vect_size(PP1), k1 = 0;
541  scalar_type c1 = scalar_type(0);
542  base_node V = PR2 - PR1;
543  for (size_type k = 0; k < n; ++k)
544  if (gmm::abs(V[k]) > gmm::abs(c1)) { c1 = V[k]; k1 = k; }
545 
546  scalar_type alpha1 = (PP1[k1] - PR1[k1]) / c1;
547  scalar_type alpha2 = (PP2[k1] - PR1[k1]) / c1;
548  base_node W1 = PP1 - PR1 - alpha1 * V;
549  base_node W2 = PP2 - PR1 - alpha2 * V;
550  if (gmm::vect_norm2(W1) > 1e-7*gmm::vect_norm2(V)) return false;
551  if (gmm::vect_norm2(W2) > 1e-7*gmm::vect_norm2(V)) return false;
552  if (alpha1 > 1.-1e-7 && alpha2 > 1.-1e-7) return false;
553  if (alpha1 < 1e-7 && alpha2 < 1e-7) return false;
554  return true;
555  }
556 
557 
558  void mesh_im_cross_level_set::build_method_of_convex
559  (size_type cv, mesh &global_intersection, bgeot::rtree &rtree_seg) {
560  const mesh &msh(mls->mesh_of_convex(cv));
561  GMM_ASSERT3(msh.convex_index().card() != 0, "Internal error");
562  base_matrix G;
563  base_node B;
564 
565  std::vector<pmesher_signed_distance> mesherls0(2);
566  std::vector<pmesher_signed_distance> mesherls1(2);
567  dal::bit_vector convexes_arein;
568 
569  mesherls0[0] = mls->get_level_set(ind_ls1)->mls_of_convex(cv, 0, false);
570  mesherls0[1] = mls->get_level_set(ind_ls2)->mls_of_convex(cv, 0, false);
571  if (mls->get_level_set(ind_ls1)->has_secondary())
572  mesherls1[0] = mls->get_level_set(ind_ls1)->mls_of_convex(cv, 1, false);
573  if (mls->get_level_set(ind_ls2)->has_secondary())
574  mesherls1[1] = mls->get_level_set(ind_ls2)->mls_of_convex(cv, 1, false);
575 
576  bgeot::pgeometric_trans pgt = linked_mesh().trans_of_convex(cv);
578  = msh.trans_of_convex(msh.convex_index().first_true());
579  dim_type n = pgt->dim();
580 
581  auto new_approx = std::make_shared<approx_integration>(pgt->convex_ref());
582  new_approx->set_built_on_the_fly();
583  base_matrix KK(n,n), CS(n,n);
584  base_matrix pc(pgt2->nb_points(), n);
585  std::vector<size_type> ptsing;
586 
587  for (dal::bv_visitor i(msh.convex_index()); !i.finished(); ++i) {
588  papprox_integration pai = segment_pim->approx_method();
589  GMM_ASSERT1(gmm::vect_size(pai->point(0)) == 1,
590  "A segment integration method is needed");
591 
592  base_matrix G2;
593  vectors_to_base_matrix(G2, linked_mesh().points_of_convex(cv));
595  cc(linked_mesh().trans_of_convex(cv), base_node(n), G2);
596 
597  mesh::ref_mesh_pt_ct cvpts = msh.points_of_convex(i);
598 
599  dal::bit_vector ptinter;
600  for (short_type k = 0; k < n; ++k) {
601  size_type ipt = msh.structure_of_convex(i)->ind_dir_points()[k];
602  const base_node &P = cvpts[ipt];
603  if (is_point_in_intersection(mesherls0, mesherls1, P))
604  ptinter.add(ipt);
605  }
606 
607  switch (n) {
608  case 2:
609  for (short_type k = 0; k < n; ++k) {
610  size_type ipt = msh.structure_of_convex(i)->ind_dir_points()[k];
611  if (ptinter.is_in(ipt)) {
612 
613  const base_node &P = cvpts[ipt];
614  cc.set_xref(P);
615 
616  if (global_intersection.search_point(cc.xreal())
617  == size_type(-1)) {
618  global_intersection.add_point(cc.xreal());
619  new_approx->add_point(msh.points_of_convex(i)[ipt],
620  scalar_type(1));
621  }
622 
623  }
624  }
625  break;
626  case 3:
627  {
628  for (short_type k1 = 1; k1 < n; ++k1) {
629  size_type ipt1 = msh.structure_of_convex(i)->ind_dir_points()[k1];
630  for (short_type k2 = 0; k2 < k1; ++k2) {
631  size_type ipt2=msh.structure_of_convex(i)->ind_dir_points()[k2];
632  if (ptinter.is_in(ipt1) && ptinter.is_in(ipt2)) {
633 
634  const base_node &P1 = cvpts[ipt1];
635  const base_node &P2 = cvpts[ipt2];
636  cc.set_xref(P1);
637  base_node PR1 = cc.xreal();
638  cc.set_xref(P2);
639  base_node PR2 = cc.xreal();
640 
641  size_type i1 = global_intersection.search_point(PR1);
642  size_type i2 = global_intersection.search_point(PR2);
643 
644  if (i1 == size_type(-1) || i2 == size_type(-1) ||
645  !global_intersection.nb_convex_with_edge(i1, i2)) {
646 
647  base_node min(n), max(n);
648  for (size_type k = 0; k < n; ++k) {
649  min[k] = std::min(PR1[k], PR2[k]);
650  max[k] = std::max(PR1[k], PR2[k]);
651  }
652  bgeot::rtree::pbox_set boxlst;
653  rtree_seg.find_intersecting_boxes(min, max, boxlst);
654 
655  bool found_intersect = false;
656 
657  for (bgeot::rtree::pbox_set::const_iterator
658  it=boxlst.begin(); it != boxlst.end(); ++it) {
659  mesh::ref_mesh_pt_ct intersect_cvpts
660  = global_intersection.points_of_convex((*it)->id);
661  const base_node &PP1 = intersect_cvpts[0];
662  const base_node &PP2 = intersect_cvpts[1];
663  if (is_edges_intersect(PP1, PP2, PR1, PR2))
664  { found_intersect = true; break; }
665  }
666 
667  if (!found_intersect) {
668 
669  i1 = global_intersection.add_point(PR1);
670  i2 = global_intersection.add_point(PR2);
671 
672  size_type is = global_intersection.add_segment(i1, i2);
673 
674  rtree_seg.add_box(min, max, is);
675  rtree_seg.build_tree(); // Not efficient !
676 
677 
678  const base_node &PE1
679  = msh.trans_of_convex(i)->convex_ref()->points()[ipt1];
680  const base_node &PE2
681  = msh.trans_of_convex(i)->convex_ref()->points()[ipt1];
682  base_node V = PE2 - PE1, W1(n), W2(n);
683 
684  base_matrix G3;
685  vectors_to_base_matrix(G3, msh.points_of_convex(i));
687  ccc(msh.trans_of_convex(i), base_node(n), G3);
688 
689  for (size_type j=0; j < pai->nb_points_on_convex(); ++j) {
690  base_node PE = pai->point(j)[0] * PE2
691  + (scalar_type(1) - pai->point(j)[0]) * PE1;
692  ccc.set_xref(PE);
693  cc.set_xref(ccc.xreal());
694  gmm::mult(ccc.K(), V, W1);
695  gmm::mult(cc.K(), W1, W2);
696  new_approx->add_point(ccc.xreal(),
697  pai->coeff(j) * gmm::vect_norm2(W2));
698  }
699  }
700  }
701  }
702  }
703  }
704  }
705  break;
706  default: GMM_ASSERT1(false, "internal error");
707 
708  }
709  }
710 
711  if (new_approx->nb_points()) {
712  new_approx->valid_method();
713  pintegration_method
714  pim = std::make_shared<integration_method>(new_approx);
715  dal::pstatic_stored_object_key
716  pk = std::make_shared<special_imls_key>(new_approx);
717  dal::add_stored_object(pk, pim, new_approx->ref_convex(),
718  new_approx->pintegration_points());
719  build_methods.push_back(pim);
720  cut_im.set_integration_method(cv, pim);
721  }
722  }
723 
725  GMM_ASSERT1(linked_mesh_ != 0, "mesh level set uninitialized");
726  GMM_ASSERT1(linked_mesh_->dim() > 1 && linked_mesh_->dim() <= 3,
727  "Sorry, works only in dimension 2 or 3");
728 
729  context_check();
730  clear_build_methods();
731  ignored_im.clear();
732  mesh global_intersection;
733  bgeot::rtree rtree_seg;
734 
735  std::vector<size_type> icv;
736  std::vector<dal::bit_vector> ils;
737  mls->find_level_set_potential_intersections(icv, ils);
738 
739  for (size_type i = 0; i < icv.size(); ++i) {
740  if (ils[i].is_in(ind_ls1) && ils[i].is_in(ind_ls2)) {
741  build_method_of_convex(icv[i], global_intersection, rtree_seg);
742  }
743  }
744 
745  for (dal::bv_visitor cv(linked_mesh().convex_index());
746  !cv.finished(); ++cv)
747  if (!cut_im.convex_index().is_in(cv)) ignored_im.add(cv);
748 
749  is_adapted = true; touch();
750  }
751 
752 
753 } /* end of namespace getfem. */
754 
755 
756 
Simple implementation of a KD-tree.
the geotrans_interpolation_context structure is passed as the argument of geometric transformation in...
const base_node & xref() const
coordinates of the current point, in the reference convex.
Balanced tree of n-dimensional rectangles.
Definition: bgeot_rtree.h:97
bool context_check() const
return true if update_from_context was called
structure passed as the argument of fem interpolation functions.
Definition: getfem_fem.h:749
Describe an adaptable integration method linked to a mesh cut by at least two level sets on the inter...
virtual pintegration_method int_method_of_element(size_type cv) const
return the integration method associated with an element (in no integration is associated,...
void update_from_context(void) const
this function has to be defined and should update the object when the context is modified.
void set_segment_im(pintegration_method pim)
Set the specific integration methods.
void adapt(void)
Apply the adequate integration methods.
Describe an adaptable integration method linked to a mesh cut by a level set.
void adapt(void)
Apply the adequate integration methods.
void set_simplex_im(pintegration_method reg, pintegration_method sing=0)
Set the specific integration methods.
virtual pintegration_method int_method_of_element(size_type cv) const
return the integration method associated with an element (in no integration is associated,...
void update_from_context(void) const
this function has to be defined and should update the object when the context is modified.
virtual pintegration_method int_method_of_element(size_type cv) const
return the integration method associated with an element (in no integration is associated,...
const mesh & linked_mesh() const
Give a reference to the linked mesh of type mesh.
const dal::bit_vector & convex_index(void) const
Get the set of convexes where an integration method has been assigned.
void set_integration_method(size_type cv, pintegration_method pim)
Set the integration method of a convex.
Keep informations about a mesh crossed by level-sets.
size_type nb_level_sets(void) const
Get number of level-sets referenced in this object.
Describe a mesh (collection of convexes (elements) and points).
Definition: getfem_mesh.h:98
a subclass of mesh_im which is conformal to a number of level sets.
An experimental mesher.
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
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1663
linalg_traits< DenseMatrixLU >::value_type lu_det(const DenseMatrixLU &LU, const Pvector &pvector)
Compute the matrix determinant (via a LU factorization)
Definition: gmm_dense_lu.h:240
size_type convex_num() const
get the current convex number
Definition: getfem_fem.cc:52
gmm::uint16_type short_type
used as the common short type integer in the library
Definition: bgeot_config.h:72
pconvex_structure parallelepiped_structure(dim_type nc, dim_type k)
Give a pointer on the structures of a parallelepiped of dimension d.
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
void add_stored_object(pstatic_stored_object_key k, pstatic_stored_object o, permanence perm)
Add an object with two optional dependencies.
GEneric Tool for Finite Element Methods.
std::string name_of_int_method(pintegration_method p)
Get the string name of an integration method .
pintegration_method int_method_descriptor(std::string name, bool throw_if_not_found=true)
Get an integration method from its name .
pintegration_method im_none(void)
return IM_NONE