GetFEM  5.6
getfem_global_function.cc
1 /*===========================================================================
2 
3  Copyright (C) 2004-2026 Yves Renard
4  Copyright (C) 2016-2022 Konstantinos Poulios
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 ===========================================================================*/
21 
24 
25 namespace getfem {
26 
27 
28  // Partial implementation of abstract class global_function_simple
29 
30  scalar_type global_function_simple::val
31  (const fem_interpolation_context &c) const {
32  base_node pt = c.xreal();
33  GMM_ASSERT1(pt.size() == dim_, "Point of wrong size (" << pt.size() << ") "
34  << "passed to a global function of dim = "<< dim_ <<".");
35  return this->val(pt);
36  }
37 
38  void global_function_simple::grad
39  (const fem_interpolation_context &c, base_small_vector &g) const {
40  base_node pt = c.xreal();
41  GMM_ASSERT1(pt.size() == dim_, "Point of wrong size (" << pt.size() << ") "
42  << "passed to a global function of dim = "<< dim_ <<".");
43  this->grad(pt, g);
44  }
45 
46  void global_function_simple::hess
47  (const fem_interpolation_context &c, base_matrix &h) const {
48  base_node pt = c.xreal();
49  GMM_ASSERT1(pt.size() == dim_, "Point of wrong size (" << pt.size() << ") "
50  << "passed to a global function of dim = "<< dim_ <<".");
51  this->hess(pt, h);
52  }
53 
54  // Implementation of global_function_parser
55 
56  scalar_type global_function_parser::val(const base_node &pt) const {
57  const bgeot::base_tensor &t = tensor_val(pt);
58  GMM_ASSERT1(t.size() == 1, "Wrong size of expression result "
59  << f_val.expression());
60  return t[0];
61  }
62 
63  const base_tensor &global_function_parser::tensor_val(const base_node &pt) const {
64  gmm::copy(pt, pt_);
65  return f_val.eval();
66  }
67 
68  void global_function_parser::grad(const base_node &pt, base_small_vector &g) const {
69  g.resize(dim_);
70  gmm::copy(pt, pt_);
71  const bgeot::base_tensor &t = f_grad.eval();
72  GMM_ASSERT1(t.size() == dim_, "Wrong size of expression result "
73  << f_grad.expression());
74  gmm::copy(t.as_vector(), g);
75 
76  }
77 
78  void global_function_parser::hess(const base_node &pt, base_matrix &h) const {
79  h.resize(dim_, dim_);
80  gmm::copy(pt, pt_);
81  const bgeot::base_tensor &t = f_hess.eval();
82  GMM_ASSERT1(t.size() == size_type(dim_*dim_),
83  "Wrong size of expression result " << f_hess.expression());
84  gmm::copy(t.as_vector(), h.as_vector());
85  }
86 
87  global_function_parser::global_function_parser(dim_type dim__,
88  const std::string &sval,
89  const std::string &sgrad,
90  const std::string &shess)
91  : global_function_simple(dim__),
92  f_val(gw, sval), f_grad(gw, sgrad), f_hess(gw, shess) {
93 
94  size_type N(dim_);
95  pt_.resize(N);
96  gmm::fill(pt_, scalar_type(0));
97  gw.add_fixed_size_variable("X", gmm::sub_interval(0, N), pt_);
98  if (N >= 1) gw.add_macro("x", "X(1)");
99  if (N >= 2) gw.add_macro("y", "X(2)");
100  if (N >= 3) gw.add_macro("z", "X(3)");
101  if (N >= 4) gw.add_macro("w", "X(4)");
102 
103  f_val.compile();
104  f_grad.compile();
105  f_hess.compile();
106  }
107 
108  // Implementation of global_function_sum
109 
110  scalar_type global_function_sum::val
111  (const fem_interpolation_context &c) const {
112  scalar_type res(0);
113  for (const auto &f : functions)
114  res += f->val(c);
115  return res;
116  }
117 
118  void global_function_sum::grad
119  (const fem_interpolation_context &c, base_small_vector &g) const {
120  g.resize(dim_);
121  gmm::clear(g);
122  base_small_vector gg(dim_);
123  for (const auto &f : functions) {
124  f->grad(c, gg);
125  gmm::add(gg, g);
126  }
127  }
128 
129  void global_function_sum::hess
130  (const fem_interpolation_context &c, base_matrix &h) const {
131  h.resize(dim_, dim_);
132  gmm::clear(h);
133  base_matrix hh(dim_, dim_);
134  for (const auto &f : functions) {
135  f->hess(c, hh);
136  gmm::add(hh.as_vector(), h.as_vector());
137  }
138  }
139 
140  bool global_function_sum::is_in_support(const base_node &p) const {
141  for (const auto &f : functions)
142  if (f->is_in_support(p)) return true;
143  return false;
144  }
145 
146  void global_function_sum::bounding_box
147  (base_node &bmin_, base_node &bmax_) const {
148  if (functions.size() > 0)
149  functions[0]->bounding_box(bmin_, bmax_);
150  base_node bmin0(dim()), bmax0(dim());
151  for (const auto &f : functions) {
152  f->bounding_box(bmin0, bmax0);
153  for (size_type i=0; i < dim(); ++i) {
154  if (bmin0[i] < bmin_[i]) bmin_[i] = bmin0[i];
155  if (bmax0[i] > bmax_[i]) bmax_[i] = bmax0[i];
156  }
157  }
158  }
159 
160  global_function_sum::global_function_sum(const std::vector<pglobal_function> &funcs)
161  : global_function((funcs.size() > 0) ? funcs[0]->dim() : 0), functions(funcs) {
162  for (const auto &f : functions)
163  GMM_ASSERT1(f->dim() == dim(), "Incompatible dimensions among the provided"
164  " global functions");
165  }
166 
167  global_function_sum::global_function_sum(pglobal_function f1, pglobal_function f2)
168  : global_function(f1->dim()), functions(2) {
169  functions[0] = f1;
170  functions[1] = f2;
171  GMM_ASSERT1(f1->dim() == dim() && f2->dim() == dim(),
172  "Incompatible dimensions between the provided global functions");
173  }
174 
175  global_function_sum::global_function_sum(pglobal_function f1, pglobal_function f2,
176  pglobal_function f3)
177  : global_function(f1->dim()), functions(3) {
178  functions[0] = f1;
179  functions[1] = f2;
180  functions[2] = f3;
181  GMM_ASSERT1(f1->dim() == dim() && f2->dim() == dim() && f3->dim() == dim(),
182  "Incompatible dimensions between the provided global functions");
183  }
184 
185  global_function_sum::global_function_sum(pglobal_function f1, pglobal_function f2,
186  pglobal_function f3, pglobal_function f4)
187  : global_function(f1->dim()), functions(4) {
188  functions[0] = f1;
189  functions[1] = f2;
190  functions[2] = f3;
191  functions[3] = f4;
192  GMM_ASSERT1(f1->dim() == dim() && f2->dim() == dim() && f3->dim() == dim(),
193  "Incompatible dimensions between the provided global functions");
194  }
195 
196 
197  // Implementation of global_function_product
198 
199  scalar_type global_function_product::val
200  (const fem_interpolation_context &c) const {
201  return f1->val(c) * f2->val(c);
202  }
203 
204  void global_function_product::grad
205  (const fem_interpolation_context &c, base_small_vector &g) const {
206  g.resize(dim_);
207  base_small_vector gg(dim_);
208  f1->grad(c, gg);
209  gmm::copy(gmm::scaled(gg, f2->val(c)), g);
210  f2->grad(c, gg);
211  gmm::add(gmm::scaled(gg, f1->val(c)), g);
212  }
213 
214  void global_function_product::hess
215  (const fem_interpolation_context &c, base_matrix &h) const {
216  h.resize(dim_, dim_);
217  gmm::clear(h);
218  base_matrix hh(dim_, dim_);
219  f1->hess(c, hh);
220  gmm::copy(gmm::scaled(hh, f2->val(c)), h);
221  f2->hess(c, hh);
222  gmm::add(gmm::scaled(hh, f1->val(c)), h);
223  base_small_vector g1(dim_), g2(dim_);
224  f1->grad(c, g1);
225  f2->grad(c, g2);
226  gmm::rank_one_update(h, g1, g2);
227  gmm::rank_one_update(h, g2, g1);
228  }
229 
230  bool global_function_product::is_in_support(const base_node &p) const {
231  return f1->is_in_support(p) && f2->is_in_support(p);
232  }
233 
234  void global_function_product::bounding_box
235  (base_node &bmin_, base_node &bmax_) const {
236  base_node bmin0(dim()), bmax0(dim());
237  f1->bounding_box(bmin0, bmax0);
238  f2->bounding_box(bmin_, bmax_);
239  for (size_type i=0; i < dim(); ++i) {
240  if (bmin0[i] > bmin_[i]) bmin_[i] = bmin0[i];
241  if (bmax0[i] < bmax_[i]) bmax_[i] = bmax0[i];
242  if (bmin_[i] > bmax_[i])
243  GMM_WARNING1("Global function product with vanishing basis function");
244  }
245  }
246 
247  global_function_product::global_function_product(pglobal_function f1_, pglobal_function f2_)
248  : global_function(f1_->dim()), f1(f1_), f2(f2_) {
249  GMM_ASSERT1(f2->dim() == dim(), "Incompatible dimensions between the provided"
250  " global functions");
251  }
252 
253 
254  // Implementation of global_function_bounded
255 
256  bool global_function_bounded::is_in_support(const base_node &pt) const {
257  if (has_expr) {
258  gmm::copy(pt, pt_);
259  const bgeot::base_tensor &t = f_val.eval();
260  GMM_ASSERT1(t.size() == 1, "Wrong size of expression result "
261  << f_val.expression());
262  return (t[0] > scalar_type(0));
263  }
264  return true;
265  }
266 
267  global_function_bounded::global_function_bounded(pglobal_function f_,
268  const base_node &bmin_,
269  const base_node &bmax_,
270  const std::string &is_in_expr)
271  : global_function(f_->dim()), f(f_), bmin(bmin_), bmax(bmax_),
272  f_val(gw, is_in_expr) {
273 
274  has_expr = !is_in_expr.empty();
275  if (has_expr) {
276  size_type N(dim_);
277  pt_.resize(N);
278  gmm::fill(pt_, scalar_type(0));
279  gw.add_fixed_size_variable("X", gmm::sub_interval(0, N), pt_);
280  if (N >= 1) gw.add_macro("x", "X(1)");
281  if (N >= 2) gw.add_macro("y", "X(2)");
282  if (N >= 3) gw.add_macro("z", "X(3)");
283  if (N >= 4) gw.add_macro("w", "X(4)");
284  f_val.compile();
285  }
286  }
287 
288  // Implementation of some useful xy functions
289 
290  parser_xy_function::parser_xy_function(const std::string &sval,
291  const std::string &sgrad,
292  const std::string &shess)
293  : f_val(gw, sval), f_grad(gw, sgrad), f_hess(gw, shess),
294  ptx(1), pty(1), ptr(1), ptt(1) {
295 
296  gw.add_fixed_size_constant("x", ptx);
297  gw.add_fixed_size_constant("y", pty);
298  gw.add_fixed_size_constant("r", ptr);
299  gw.add_fixed_size_constant("theta", ptt);
300 
301  f_val.compile();
302  f_grad.compile();
303  f_hess.compile();
304  }
305 
306  scalar_type
307  parser_xy_function::val(scalar_type x, scalar_type y) const {
308  ptx[0] = double(x); // x
309  pty[0] = double(y); // y
310  ptr[0] = double(sqrt(fabs(x*x+y*y))); // r
311  ptt[0] = double(atan2(y,x)); // theta
312 
313  const bgeot::base_tensor &t = f_val.eval();
314  GMM_ASSERT1(t.size() == 1, "Wrong size of expression result "
315  << f_val.expression());
316  return t[0];
317  }
318 
319  base_small_vector
320  parser_xy_function::grad(scalar_type x, scalar_type y) const {
321  ptx[0] = double(x); // x
322  pty[0] = double(y); // y
323  ptr[0] = double(sqrt(fabs(x*x+y*y))); // r
324  ptt[0] = double(atan2(y,x)); // theta
325 
326  base_small_vector res(2);
327  const bgeot::base_tensor &t = f_grad.eval();
328  GMM_ASSERT1(t.size() == 2, "Wrong size of expression result "
329  << f_grad.expression());
330  gmm::copy(t.as_vector(), res);
331  return res;
332  }
333 
334  base_matrix
335  parser_xy_function::hess(scalar_type x, scalar_type y) const {
336  ptx[0] = double(x); // x
337  pty[0] = double(y); // y
338  ptr[0] = double(sqrt(fabs(x*x+y*y))); // r
339  ptt[0] = double(atan2(y,x)); // theta
340 
341  base_matrix res(2,2);
342  const bgeot::base_tensor &t = f_hess.eval();
343  GMM_ASSERT1(t.size() == 4, "Wrong size of expression result "
344  << f_hess.expression());
345  gmm::copy(t.as_vector(), res.as_vector());
346  return res;
347  }
348 
349  /* the basic singular functions for 2D cracks */
350  scalar_type
351  crack_singular_xy_function::val(scalar_type x, scalar_type y) const {
352  scalar_type sgny = (y < 0 ? -1.0 : 1.0);
353  scalar_type r = sqrt(x*x + y*y);
354  if (r < 1e-10) return 0;
355 
356  /* The absolute value is unfortunately necessary, otherwise, sqrt(-1e-16)
357  can be required ...
358  */
359  scalar_type sin2 = sqrt(gmm::abs(.5-x/(2*r))) * sgny;
360  scalar_type cos2 = sqrt(gmm::abs(.5+x/(2*r)));
361 
362  scalar_type res = 0;
363  switch(l){
364 
365  /* First order enrichement displacement field (linear elasticity) */
366 
367  case 0 : res = sqrt(r)*sin2; break;
368  case 1 : res = sqrt(r)*cos2; break;
369  case 2 : res = sin2*y/sqrt(r); break;
370  case 3 : res = cos2*y/sqrt(r); break;
371 
372  /* Second order enrichement of displacement field (linear elasticity) */
373 
374  case 4 : res = sqrt(r)*r*sin2; break;
375  case 5 : res = sqrt(r)*r*cos2; break;
376  case 6 : res = sin2*cos2*cos2*r*sqrt(r); break;
377  case 7 : res = cos2*cos2*cos2*r*sqrt(r); break;
378 
379  /* First order enrichement of pressure field (linear elasticity) mixed formulation */
380 
381  case 8 : res = -sin2/sqrt(r); break;
382  case 9 : res = cos2/sqrt(r); break;
383 
384  /* Second order enrichement of pressure field (linear elasticity) mixed formulation */
385 
386  case 10 : res = sin2*sqrt(r); break; // cos2*cos2
387  case 11 : res = cos2*sqrt(r); break;
388 
389  /* First order enrichement of displacement field (nonlinear elasticity)[Rodney Stephenson Journal of elasticity VOL.12 No. 1, January 1982] */
390 
391  case 12 : res = r*sin2*sin2; break;
392  case 13 : res = sqrt(r)*sin2; break;
393 
394 /* First order enrichement of pressure field (nonlinear elasticity) */
395 
396  case 14 : res = sin2/r; break;
397  case 15 : res = cos2/r; break;
398 
399  default: GMM_ASSERT2(false, "arg");
400  }
401  return res;
402  }
403 
404 
405  base_small_vector
406  crack_singular_xy_function::grad(scalar_type x, scalar_type y) const {
407  scalar_type sgny = (y < 0 ? -1.0 : 1.0);
408  scalar_type r = sqrt(x*x + y*y);
409 
410  if (r < 1e-10) {
411  GMM_WARNING0("Warning, point close to the singularity (r=" << r << ")");
412  }
413 
414  /* The absolute value is unfortunately necessary, otherwise, sqrt(-1e-16)
415  can be required ...
416  */
417  scalar_type sin2 = sqrt(gmm::abs(.5-x/(2*r))) * sgny;
418  scalar_type cos2 = sqrt(gmm::abs(.5+x/(2*r)));
419 
420  base_small_vector res(2);
421  switch(l){
422  /* First order enrichement displacement field (linear elasticity) */
423  case 0 :
424  res[0] = -sin2/(2*sqrt(r));
425  res[1] = cos2/(2*sqrt(r));
426  break;
427  case 1 :
428  res[0] = cos2/(2*sqrt(r));
429  res[1] = sin2/(2*sqrt(r));
430  break;
431  case 2 :
432  res[0] = cos2*((-5*cos2*cos2) + 1. + 4*(cos2*cos2*cos2*cos2))/sqrt(r);
433  res[1] = sin2*((-3*cos2*cos2) + 1. + 4*(cos2*cos2*cos2*cos2))/sqrt(r);
434  break;
435  case 3 :
436  res[0] = -cos2*cos2*sin2*((4*cos2*cos2) - 3.)/sqrt(r);
437  res[1] = cos2*((4*cos2*cos2*cos2*cos2) + 2. - (5*cos2*cos2))/sqrt(r);
438  break;
439  /* Second order enrichement of displacement field (linear elasticity) */
440 
441  case 4 :
442  res[0] = sin2 *((4*cos2*cos2)-3.)*sqrt(r)/2.;
443  res[1] = cos2*(5. - (4*cos2*cos2))*sqrt(r)/2. ;
444  break;
445  case 5 :
446  res[0] = cos2*((4*cos2*cos2)-1.)*sqrt(r)/2.;
447  res[1] = sin2*((4*cos2*cos2)+1.)*sqrt(r)/2. ;
448  break;
449 
450  case 6 :
451  res[0] = sin2*cos2*cos2*sqrt(r)/2.;
452  res[1] = cos2*(2. - (cos2*cos2))*sqrt(r)/2.;
453  break;
454  case 7 :
455  res[0] = 3*cos2*cos2*cos2*sqrt(r)/2.;
456  res[1] = 3*sin2*cos2*cos2*sqrt(r)/2.;
457  break;
458 
459  /* First order enrichement of pressure field (linear elasticity) mixed formulation */
460 
461  case 8 :
462  res[0] =sin2*((4*cos2*cos2)-1.)/(2*sqrt(r)*r);
463  res[1] =-cos2*((4*cos2*cos2)-3.)/(2*sqrt(r)*r);
464  break;
465  case 9 :
466  res[0] =-cos2*((4*cos2*cos2) - 3.)/(2*sqrt(r)*r);
467  res[1] =-sin2*((4*cos2*cos2)-1.)/(2*sqrt(r)*r);
468  break;
469 
470  /* Second order enrichement of pressure field (linear elasticity) mixed formulation */
471  case 10 :
472  res[0] = -sin2/(2*sqrt(r));
473  res[1] = cos2/(2*sqrt(r));
474  break;
475  case 11 :
476  res[0] = cos2/(2*sqrt(r));
477  res[1] = sin2/(2*sqrt(r));
478  break;
479 
480  /* First order enrichement of displacement field (nonlinear elasticity)[Rodney Stephenson Journal of elasticity VOL.12 No. 1, January 1982] */
481 
482  case 12 :
483  res[0] = -sin2*sin2;
484  res[1] = cos2*sin2;
485  break;
486  case 13 :
487  res[0] = -sin2/(2*sqrt(r));
488  res[1] = cos2/(2*sqrt(r));
489  break;
490 
491 /* First order enrichement of pressure field (****Nonlinear elasticity*****) */
492 
493 
494  case 14 :
495  res[0] = -sin2*((3*cos2*cos2) - 1.)/(r*r);
496  res[1] = cos2*((6*cos2*cos2) - 5.)/(2*r*r);
497  break;
498  case 15 :
499  res[0] = -cos2*((3*cos2*cos2) - 2.)/(r*r);
500  res[1] = -sin2*((6*cos2*cos2) - 1.)/(2*r*r);
501  break;
502 
503 
504  default: GMM_ASSERT2(false, "oups");
505  }
506  return res;
507  }
508 
509  base_matrix
510  crack_singular_xy_function::hess(scalar_type x, scalar_type y) const {
511  scalar_type sgny = (y < 0 ? -1.0 : 1.0);
512  scalar_type r = sqrt(x*x + y*y);
513 
514  if (r < 1e-10) {
515  GMM_WARNING0("Warning, point close to the singularity (r=" << r << ")");
516  }
517 
518  /* The absolute value is unfortunately necessary, otherwise, sqrt(-1e-16)
519  can be required ...
520  */
521  scalar_type sin2 = sqrt(gmm::abs(.5-x/(2*r))) * sgny;
522  scalar_type cos2 = sqrt(gmm::abs(.5+x/(2*r)));
523 
524  base_matrix res(2,2);
525  switch(l){
526  case 0 :
527  res(0,0) = (-sin2*x*x + 2.0*cos2*x*y + sin2*y*y) / (4*pow(r, 3.5));
528  res(0,1) = (-cos2*x*x - 2.0*sin2*x*y + cos2*y*y) / (4*pow(r, 3.5));
529  res(1,0) = res(0, 1);
530  res(1,1) = (sin2*x*x - 2.0*cos2*x*y - sin2*y*y) / (4*pow(r, 3.5));
531  break;
532  case 1 :
533  res(0,0) = (-cos2*x*x - 2.0*sin2*x*y + cos2*y*y) / (4*pow(r, 3.5));
534  res(0,1) = (sin2*x*x - 2.0*cos2*x*y - sin2*y*y) / (4*pow(r, 3.5));
535  res(1,0) = res(0, 1);
536  res(1,1) = (cos2*x*x + 2.0*sin2*x*y - cos2*y*y) / (4*pow(r, 3.5));
537  break;
538  case 2 :
539  res(0,0) = 3.0*y*(sin2*x*x + 2.0*cos2*x*y - sin2*y*y) / (4*pow(r, 4.5));
540  res(0,1) = (-2.0*sin2*x*x*x - 5.0*cos2*y*x*x + 4.0*sin2*y*y*x + cos2*y*y*y)
541  / (4*pow(r, 4.5));
542  res(1,0) = res(0, 1);
543  res(1,1) = (4.0*cos2*x*x*x - 7.0*sin2*y*x*x - 2.0*cos2*y*y*x - sin2*y*y*y)
544  / (4*pow(r, 4.5));
545  break;
546  case 3 :
547  res(0,0) = 3.0*y*(cos2*x*x - 2.0*sin2*x*y - cos2*y*y) / (4*pow(r, 4.5));
548  res(0,1) = (-2.0*cos2*x*x*x + 5.0*sin2*y*x*x + 4.0*cos2*y*y*x - sin2*y*y*y)
549  / (4*pow(r, 4.5));
550  res(1,0) = res(0, 1);
551  res(1,1) = (-4.0*sin2*x*x*x - 7.0*cos2*y*x*x + 2.0*sin2*y*y*x - cos2*y*y*y)
552  / (4*pow(r, 4.5));
553  break;
554  case 4 :
555  res(0,0) = (sin2*(-4*cos2*cos2*cos2*cos2 + 3*cos2*cos2 + 0.75))/sqrt(r);
556  res(0,1) = (cos2*(4*cos2*cos2*cos2*cos2 - 5*cos2*cos2 + 1.25))/sqrt(r);
557  res(1,1) = (sin2*(4*cos2*cos2*cos2*cos2 - 3*cos2*cos2 + 1.25))/sqrt(r);
558  res(1,0) = res(0,1);
559  break;
560  case 5 :
561  res(0,0) = (cos2*(-4*cos2*cos2*cos2*cos2 + 5*cos2*cos2 - 0.25))/sqrt(r);
562  res(0,1) = (sin2*(-4*cos2*cos2*cos2*cos2 + 3*cos2*cos2 - 0.25))/sqrt(r);
563  res(1,1) = (cos2*(4*cos2*cos2*cos2*cos2 - 5*cos2*cos2 + 2.25))/sqrt(r);
564  res(1,0) = res(0,1);
565  break;
566  case 6 :
567  res(0,0) = (cos2*cos2*sin2*(0.75 - cos2*cos2))/sqrt(r);
568  res(0,1) = (cos2*(cos2*cos2*cos2*cos2 - 1.25*cos2*cos2 + 0.5))/sqrt(r);
569  res(1,1) = (sin2*(cos2*cos2*cos2*cos2 - 0.75*cos2*cos2 + 0.5))/sqrt(r);
570  res(1,0) = res(0,1);
571  break;
572  case 7 :
573  res(0,0) = (3*cos2*cos2*cos2*(1.25 - cos2*cos2))/sqrt(r);
574  res(0,1) = (3*cos2*cos2*sin2*(0.75 - cos2*cos2))/sqrt(r);
575  res(1,1) = (3*cos2*(cos2*cos2*cos2*cos2 - 1.25*cos2*cos2 + 0.5))/sqrt(r);
576  res(1,0) = res(0,1);
577  break;
578  case 8 :
579  res(0,0) = (3*sin2*(-4*cos2*cos2*cos2*cos2 + 3*cos2*cos2 - 0.25))/(r*r*sqrt(r));
580  res(0,1) = (3*cos2*(4*cos2*cos2*cos2*cos2 - 5*cos2*cos2 + 1.25))/(r*r*sqrt(r));
581  res(1,1) = (3*sin2*(4*cos2*cos2*cos2*cos2 - 3*cos2*cos2 + 0.25))/(r*r*sqrt(r));
582  res(1,0) = res(0,1);
583  break;
584  case 9 :
585  res(0,0) = (3*cos2*(4*cos2*cos2*cos2*cos2 - 5*cos2*cos2 + 1.25))/(r*r*sqrt(r));
586  res(0,1) = (3*sin2*(4*cos2*cos2*cos2*cos2 - 3*cos2*cos2 + 0.25))/(r*r*sqrt(r));
587  res(1,1) = (3*cos2*(-4*cos2*cos2*cos2*cos2 + 5*cos2*cos2 - 1.25))/(r*r*sqrt(r));
588  res(1,0) = res(0,1);
589  break;
590  case 10 :
591  res(0,0) = (sin2*(cos2*cos2 - 0.25))/(r*sqrt(r));
592  res(0,1) = (cos2*(0.75 - cos2*cos2))/(r*sqrt(r));
593  res(1,1) = (sin2*(0.25 - cos2*cos2))/(r*sqrt(r));
594  res(1,0) = res(0,1);
595  break;
596  case 11 :
597  res(0,0) = (cos2*(0.75 - cos2*cos2))/(r*sqrt(r));
598  res(0,1) = (sin2*(0.25 - cos2*cos2))/(r*sqrt(r));
599  res(1,1) = (cos2*(cos2*cos2 - 0.75))/(r*sqrt(r));
600  res(1,0) = res(0,1);
601  break;
602  case 12 :
603  res(0,0) = (2*cos2*cos2*(1 - cos2*cos2))/r;
604  res(0,1) = (cos2*sin2*(1 - 2*cos2*cos2))/r;
605  res(1,1) = (2*cos2*cos2*cos2*cos2 - 2*cos2*cos2 + 0.5)/r;
606  res(1,0) = res(0,1);
607  break;
608  case 13 :
609  res(0,0) = (sin2*(cos2*cos2 - 0.25))/(r*sqrt(r));
610  res(0,1) = (cos2*(0.75 - cos2*cos2))/(r*sqrt(r));
611  res(1,1) = (sin2*(0.25 - cos2*cos2))/(r*sqrt(r));
612  res(1,0) = res(0,1);
613  break;
614  case 14 :
615  res(0,0) = (sin2*(21*cos2*cos2*cos2*cos2 - 17*cos2*cos2 + 2))/(r*r*r);
616  res(0,1) = (cos2*(-21*cos2*cos2*cos2*cos2 + 27.5*cos2*cos2 - 7.5))/(r*r*r);
617  res(1,1) = (sin2*(-21*cos2*cos2*cos2*cos2 + 17*cos2*cos2 - 1.25))/(r*r*r);
618  res(1,0) = res(0,1);
619  break;
620  case 15 :
621  res(0,0) = (cos2*(21*cos2*cos2*cos2*cos2 - 25*cos2*cos2 + 6))/(r*r*r);
622  res(0,1) = (sin2*(21*cos2*cos2*cos2*cos2 - 14.5*cos2*cos2 + 1))/(r*r*r);
623  res(1,1) = (cos2*(-21*cos2*cos2*cos2*cos2 + 25*cos2*cos2 - 5.25))/(r*r*r);
624  res(1,0) = res(0,1);
625  break;
626  default: GMM_ASSERT2(false, "oups");
627  }
628  return res;
629  }
630 
631  scalar_type
632  cutoff_xy_function::val(scalar_type x, scalar_type y) const {
633  scalar_type res = 1;
634  switch (fun) {
635  case EXPONENTIAL_CUTOFF: {
636  res = (a4>0) ? exp(-a4 * gmm::sqr(x*x+y*y)) : 1;
637  break;
638  }
639  case POLYNOMIAL_CUTOFF: {
640  assert(r0 > r1);
641  scalar_type r = gmm::sqrt(x*x+y*y);
642 
643  if (r <= r1) res = 1;
644  else if (r >= r0) res = 0;
645  else {
646  // scalar_type c = 6./(r0*r0*r0 - r1*r1*r1 + 3*r1*r0*(r1-r0));
647  // scalar_type k = -(c/6.)*(-pow(r0,3.) + 3*r1*pow(r0,2.));
648  // res = (c/3.)*pow(r,3.) - (c*(r0 + r1)/2.)*pow(r,2.) +
649  // c*r0*r1*r + k;
650  scalar_type c = 1./pow(r0-r1,3.0);
651  res = c*(r*(r*(2.0*r-3.0*(r0+r1))+6.0*r1*r0) + r0*r0*(r0-3.0*r1));
652  }
653  break;
654  }
655  case POLYNOMIAL2_CUTOFF: {
656  assert(r0 > r1);
657  scalar_type r = gmm::sqrt(x*x+y*y);
658  if (r <= r1) res = scalar_type(1);
659  else if (r >= r0) res = scalar_type(0);
660  else {
661 // scalar_type c = 1./((-1./30.)*(pow(r1,5) - pow(r0,5))
662 // + (1./6.)*(pow(r1,4)*r0 - r1*pow(r0,4))
663 // - (1./3.)*(pow(r1,3)*pow(r0,2) -
664 // pow(r1,2)*pow(r0,3)));
665 // scalar_type k = 1. - c*((-1./30.)*pow(r1,5) +
666 // (1./6.)*pow(r1,4)*r0 -
667 // (1./3.)*pow(r1,3)*pow(r0,2));
668 // res = c*( (-1./5.)*pow(r,5) + (1./2.)* (r1+r0)*pow(r,4) -
669 // (1./3.)*(pow(r1,2)+pow(r0,2) + 4.*r0*r1)*pow(r,3) +
670 // r0*r1*(r0+r1)*pow(r,2) - pow(r0,2)*pow(r1,2)*r) + k;
671  res = (r*(r*(r*(r*(-6.0*r + 15.0*(r0+r1))
672  - 10.0*(r0*r0 + 4.0*r1*r0 + r1*r1))
673  + 30.0 * r0*r1*(r0+r1)) - 30.0*r1*r1*r0*r0)
674  + r0*r0*r0*(r0*r0-5.0*r1*r0+10*r1*r1)) / pow(r0-r1, 5.0);
675  }
676  break;
677  }
678  default : res = 1;
679  }
680  return res;
681  }
682 
683  base_small_vector
684  cutoff_xy_function::grad(scalar_type x, scalar_type y) const {
685  base_small_vector res(2);
686  switch (fun) {
687  case EXPONENTIAL_CUTOFF: {
688  scalar_type r2 = x*x+y*y, ratio = -4.*exp(-a4*r2*r2)*a4*r2;
689  res[0] = ratio*x;
690  res[1] = ratio*y;
691  break;
692  }
693  case POLYNOMIAL_CUTOFF: {
694  scalar_type r = gmm::sqrt(x*x+y*y);
695  scalar_type ratio = 0;
696 
697  if ( r > r1 && r < r0 ) {
698  // scalar_type c = 6./(pow(r0,3.) - pow(r1,3.) + 3*r1*r0*(r1-r0));
699  // ratio = c*(r - r0)*(r - r1);
700  ratio = 6.*(r - r0)*(r - r1)/pow(r0-r1, 3.);
701  }
702  res[0] = ratio*x/r;
703  res[1] = ratio*y/r;
704  break;
705  }
706  case POLYNOMIAL2_CUTOFF: {
707  scalar_type r = gmm::sqrt(x*x+y*y);
708  scalar_type ratio = 0;
709  if (r > r1 && r < r0) {
710 // scalar_type c = 1./((-1./30.)*(pow(r1,5) - pow(r0,5))
711 // + (1./6.)*(pow(r1,4)*r0 - r1*pow(r0,4))
712 // - (1./3.)*(pow(r1,3)*pow(r0,2)
713 // - pow(r1,2)*pow(r0,3)));
714 // ratio = - c*gmm::sqr(r-r0)*gmm::sqr(r-r1);
715  ratio = -30.0*gmm::sqr(r-r0)*gmm::sqr(r-r1) / pow(r0-r1, 5.0);
716  }
717  res[0] = ratio*x/r;
718  res[1] = ratio*y/r;
719  break;
720  }
721  default :
722  res[0] = 0;
723  res[1] = 0;
724  }
725  return res;
726  }
727 
728  base_matrix
729  cutoff_xy_function::hess(scalar_type x, scalar_type y) const {
730  base_matrix res(2,2);
731  switch (fun) {
732  case EXPONENTIAL_CUTOFF: {
733  scalar_type r2 = x*x+y*y, r4 = r2*r2;
734  res(0,0) = 4.0*a4*(-3.0*x*x - y*y + 4.0*a4*x*x*r4)*exp(-a4*r4);
735  res(1,0) = 8.0*a4*x*y*(-1.0 + 2.0*a4*r4)*exp(-a4*r4);
736  res(0,1) = res(1,0);
737  res(1,1) = 4.0*a4*(-3.0*y*y - x*x + 4.0*a4*y*y*r4)*exp(-a4*r4);
738  break;
739  }
740  case POLYNOMIAL_CUTOFF: {
741  scalar_type r2 = x*x+y*y, r = gmm::sqrt(r2), c=6./(pow(r0-r1,3.)*r*r2);
742  if ( r > r1 && r < r0 ) {
743  res(0,0) = c*(x*x*r2 + r1*r0*y*y - r*r2*(r0+r1) + r2*r2);
744  res(1,0) = c*x*y*(r2 - r1*r0);
745  res(0,1) = res(1,0);
746  res(1,1) = c*(y*y*r2 + r1*r0*x*x - r*r2*(r0+r1) + r2*r2);
747  }
748  break;
749  }
750  case POLYNOMIAL2_CUTOFF: {
751  scalar_type r2 = x*x+y*y, r = gmm::sqrt(r2), r3 = r*r2;
752  if (r > r1 && r < r0) {
753  scalar_type dp = -30.0*(r1-r)*(r1-r)*(r0-r)*(r0-r) / pow(r0-r1, 5.0);
754  scalar_type ddp = 60.0*(r1-r)*(r0-r)*(r0+r1-2.0*r) / pow(r0-r1, 5.0);
755  scalar_type rx= x/r, ry= y/r, rxx= y*y/r3, rxy= -x*y/r3, ryy= x*x/r3;
756  res(0,0) = ddp*rx*rx + dp*rxx;
757  res(1,0) = ddp*rx*ry + dp*rxy;
758  res(0,1) = res(1,0);
759  res(1,1) = ddp*ry*ry + dp*ryy;
760  }
761  break;
762  }
763  }
764  return res;
765  }
766 
767  cutoff_xy_function::cutoff_xy_function(int fun_num, scalar_type r,
768  scalar_type r1_, scalar_type r0_)
769  {
770  fun = fun_num;
771  r1 = r1_; r0 = r0_;
772  a4 = 0;
773  if (r > 0) a4 = pow(2.7/r,4.);
774  }
775 
776 
777  struct global_function_on_levelsets_2D_ :
778  public global_function, public context_dependencies {
779  const std::vector<level_set> dummy_lsets;
780  const std::vector<level_set> &lsets;
781  const level_set &ls;
782  mutable pmesher_signed_distance mls_x, mls_y;
783  mutable size_type cv;
784 
785  pxy_function fn;
786 
787  void update_mls(size_type cv_, size_type n) const {
788  if (cv_ != cv) {
789  cv=cv_;
790  if (lsets.size() == 0) {
791  mls_x = ls.mls_of_convex(cv, 1);
792  mls_y = ls.mls_of_convex(cv, 0);
793  } else {
794  base_node pt(n);
795  scalar_type d = scalar_type(-2);
796  for (const level_set &ls_ : lsets) {
797  pmesher_signed_distance mls_xx, mls_yy;
798  mls_xx = ls_.mls_of_convex(cv, 1);
799  mls_yy = ls_.mls_of_convex(cv, 0);
800  scalar_type x = (*mls_xx)(pt), y = (*mls_yy)(pt);
801  scalar_type d2 = gmm::sqr(x) + gmm::sqr(y);
802  if (d < scalar_type(-1) || d2 < d) {
803  d = d2;
804  mls_x = mls_xx;
805  mls_y = mls_yy;
806  }
807  }
808  }
809  }
810  }
811 
812  virtual scalar_type val(const fem_interpolation_context& c) const {
813  update_mls(c.convex_num(), c.xref().size());
814  scalar_type x = (*mls_x)(c.xref());
815  scalar_type y = (*mls_y)(c.xref());
816  if (c.xfem_side() > 0 && y <= 1E-13) y = 1E-13;
817  if (c.xfem_side() < 0 && y >= -1E-13) y = -1E-13;
818  return fn->val(x,y);
819  }
820  virtual void grad(const fem_interpolation_context& c,
821  base_small_vector &g) const {
822  size_type P = c.xref().size();
823  base_small_vector dx(P), dy(P), dfr(2);
824 
825  update_mls(c.convex_num(), P);
826  scalar_type x = mls_x->grad(c.xref(), dx);
827  scalar_type y = mls_y->grad(c.xref(), dy);
828  if (c.xfem_side() > 0 && y <= 0) y = 1E-13;
829  if (c.xfem_side() < 0 && y >= 0) y = -1E-13;
830 
831  base_small_vector gfn = fn->grad(x,y);
832  gmm::mult(c.B(), gfn[0]*dx + gfn[1]*dy, g);
833  }
834  virtual void hess(const fem_interpolation_context&c,
835  base_matrix &h) const {
836  size_type P = c.xref().size(), N = c.N();
837  base_small_vector dx(P), dy(P), dfr(2), dx_real(N), dy_real(N);
838 
839  update_mls(c.convex_num(), P);
840  scalar_type x = mls_x->grad(c.xref(), dx);
841  scalar_type y = mls_y->grad(c.xref(), dy);
842  if (c.xfem_side() > 0 && y <= 0) y = 1E-13;
843  if (c.xfem_side() < 0 && y >= 0) y = -1E-13;
844 
845  base_small_vector gfn = fn->grad(x,y);
846  base_matrix hfn = fn->hess(x,y);
847 
848  base_matrix hx, hy, hx_real(N*N, 1), hy_real(N*N, 1);
849  mls_x->hess(c.xref(), hx);
850  mls_y->hess(c.xref(), hy);
851  gmm::reshape(hx, P*P, 1);
852  gmm::reshape(hy, P*P, 1);
853 
854  // real Hessians B3 h - B32 d (as fem_interpolation_context)
855  gmm::mult(c.B3(), hx, hx_real);
856  gmm::mult_add(c.B32(), gmm::scaled(dx, -1.0), gmm::mat_col(hx_real, 0));
857  gmm::mult(c.B3(), hy, hy_real);
858  gmm::mult_add(c.B32(), gmm::scaled(dy, -1.0), gmm::mat_col(hy_real, 0));
859  gmm::mult(c.B(), dx, dx_real);
860  gmm::mult(c.B(), dy, dy_real);
861 
862  for (size_type i = 0; i < N; ++i)
863  for (size_type j = 0; j < N; ++j) {
864  h(i, j) = hfn(0,0) * dx_real[i] * dx_real[j]
865  + hfn(0,1) * dx_real[i] * dy_real[j]
866  + hfn(1,0) * dy_real[i] * dx_real[j]
867  + hfn(1,1) * dy_real[i] * dy_real[j]
868  + gfn[0] * hx_real(i * N + j, 0) + gfn[1] * hy_real(i*N+j,0);
869  }
870  }
871 
872  void update_from_context() const { cv = size_type(-1); }
873 
874  global_function_on_levelsets_2D_(const std::vector<level_set> &lsets_,
875  const pxy_function &fn_)
876  : global_function(lsets_.size() ? lsets_[0].linked_mesh().dim() : 2),
877  dummy_lsets(0, dummy_level_set()),
878  lsets(lsets_), ls(dummy_level_set()), fn(fn_) {
879  GMM_ASSERT1(lsets.size() > 0, "The list of level sets should"
880  " contain at least one level set.");
881  cv = size_type(-1);
882  for (const level_set &ls_ : lsets)
883  this->add_dependency(ls_);
884  }
885 
886  global_function_on_levelsets_2D_(const level_set &ls_,
887  const pxy_function &fn_)
888  : global_function(ls_.linked_mesh().dim()),
889  dummy_lsets(0, dummy_level_set()),
890  lsets(dummy_lsets), ls(ls_), fn(fn_) {
891  cv = size_type(-1);
892  this->add_dependency(ls);
893  }
894 
895  };
896 
897  pglobal_function
898  global_function_on_level_sets(const std::vector<level_set> &lsets,
899  const pxy_function &fn) {
900  return std::make_shared<global_function_on_levelsets_2D_>(lsets, fn);
901  }
902 
903  pglobal_function
904  global_function_on_level_set(const level_set &ls,
905  const pxy_function &fn) {
906  return std::make_shared<global_function_on_levelsets_2D_>(ls, fn);
907  }
908 
909 
910 
911 
912  // Global function for bspline basis
913  const scalar_type eps(1e-12);
914 
915  // order k = 3
916  scalar_type bsp3_01(scalar_type t) {
917  return (t >= -eps && t < 1) ? pow(1.-t,2)
918  : 0;
919  }
920  scalar_type bsp3_01_der(scalar_type t) {
921  return (t >= -eps && t < 1) ? 2.*t-2.
922  : 0;
923  }
924  scalar_type bsp3_01_der2(scalar_type t) {
925  return (t >= eps && t <= 1.-eps) ? 2.
926  : 0;
927  }
928  scalar_type bsp3_01_der2_with_hint(scalar_type t, scalar_type t_hint) {
929  return (t > -eps && t < 1.+eps && t_hint > 0 && t_hint < 1) ? 2.
930  : 0;
931  }
932  scalar_type bsp3_02(scalar_type t) {
933  if (t >= -eps) {
934  if (t < 1)
935  return (-1.5*t+2.)*t;
936  else if (t < 2)
937  return 0.5*pow(2.-t,2);
938  }
939  return 0;
940  }
941  scalar_type bsp3_02_der(scalar_type t) {
942  if (t >= -eps) {
943  if (t < 1)
944  return -3.*t+2.;
945  else if (t < 2)
946  return t-2.;
947  }
948  return 0;
949  }
950  scalar_type bsp3_02_der2(scalar_type t) {
951  if (t >= eps) {
952  if (t < 1.-eps)
953  return -3.;
954  else if (t < 1.+eps)
955  return 0;
956  else if (t <= 2.-eps)
957  return 1.;
958  }
959  return 0;
960  }
961  scalar_type bsp3_03(scalar_type t) {
962  if (t >= -eps) {
963  if (t < 1) {
964  return 0.5*t*t;
965  } else if (t < 2) {
966  t -= 1.;
967  return t*(1-t)+0.5;
968  } else if (t < 3) {
969  t = 3.-t;
970  return 0.5*t*t;
971  }
972  }
973  return 0;
974  }
975  scalar_type bsp3_03_der(scalar_type t) {
976  if (t >= -eps) {
977  if (t < 1) {
978  return t;
979  } else if (t < 2) {
980  t -= 1.;
981  return 1.-2.*t;
982  } else if (t < 3) {
983  return t-3.;
984  }
985  }
986  return 0;
987  }
988  scalar_type bsp3_03_der2(scalar_type t) {
989  if (t >= -eps) {
990  if (t < eps)
991  return 0;
992  else if (t <= 1.-eps)
993  return 1.;
994  else if (t < 1.+eps)
995  return 0;
996  else if (t <= 2.-eps)
997  return -2.;
998  else if (t < 2.+eps)
999  return 0;
1000  else if (t <= 3.-eps)
1001  return 1.;
1002  else if (t < 3.+eps)
1003  return 0;
1004  }
1005  return 0;
1006  }
1007 
1008  // order k = 4
1009  scalar_type bsp4_01(scalar_type t) {
1010  return (t >= -eps && t < 1) ? pow(1.-t,3)
1011  : 0;
1012  }
1013  scalar_type bsp4_01_der(scalar_type t) {
1014  return (t >= -eps && t < 1) ? -3*pow(1.-t,2)
1015  : 0;
1016  }
1017  scalar_type bsp4_01_der2(scalar_type t) {
1018  return (t >= -eps && t < 1) ? 6*(1.-t)
1019  : 0;
1020  }
1021  scalar_type bsp4_02(scalar_type t) {
1022  if (t >= -eps) {
1023  if (t < 1) {
1024  return ((7./4.*t-9./2.)*t+3.)*t;
1025  } else if (t < 2) {
1026  return 1./4.*pow(2.-t,3);
1027  }
1028  }
1029  return 0;
1030  }
1031  scalar_type bsp4_02_der(scalar_type t) {
1032  if (t >= -eps) {
1033  if (t < 1) {
1034  return (21./4.*t-9.)*t+3.;
1035  } else if (t < 2) {
1036  return -3./4.*pow(2.-t,2);
1037  }
1038  }
1039  return 0;
1040  }
1041  scalar_type bsp4_02_der2(scalar_type t) {
1042  if (t >= -eps) {
1043  if (t < 1) {
1044  return 21./2.*t-9.;
1045  } else if (t < 2) {
1046  return 3./2.*(2.-t);
1047  }
1048  }
1049  return 0;
1050  }
1051  scalar_type bsp4_03(scalar_type t) {
1052  if (t >= -eps) {
1053  if (t < 1) {
1054  return (-11./12.*t+3./2.)*t*t;
1055  } else if (t < 2) {
1056  t -= 1;
1057  return ((7./12.*t-5./4.)*t+1./4.)*t+7./12.;
1058  } else if (t < 3) {
1059  return 1./6.*pow(3.-t,3);
1060  }
1061  }
1062  return 0;
1063  }
1064  scalar_type bsp4_03_der(scalar_type t) {
1065  if (t >= -eps) {
1066  if (t < 1) {
1067  return (-11./4.*t+3.)*t;
1068  } else if (t < 2) {
1069  t -= 1;
1070  return (7./4.*t-5./2.)*t+1./4.;
1071  } else if (t < 3) {
1072  return -1./2.*pow(3.-t,2);
1073  }
1074  }
1075  return 0;
1076  }
1077  scalar_type bsp4_03_der2(scalar_type t) {
1078  if (t >= -eps) {
1079  if (t < 1) {
1080  return -11./2.*t+3.;
1081  } else if (t < 2) {
1082  t -= 1;
1083  return 7./2.*t-5./2.;
1084  } else if (t < 3) {
1085  return 3.-t;
1086  }
1087  }
1088  return 0;
1089  }
1090  scalar_type bsp4_04(scalar_type t) {
1091  if (t > 2) {
1092  t = 4.-t;
1093  }
1094  if (t >= -eps) {
1095  if (t < 1) {
1096  return 1./6.*pow(t,3);
1097  } else if (t <= 2) {
1098  t = t-1.;
1099  return ((-1./2.*t+1./2.)*t+1./2.)*t+1./6.;
1100  }
1101  }
1102  return 0;
1103  }
1104  scalar_type bsp4_04_der(scalar_type t) {
1105  scalar_type sgn(1);
1106  if (t > 2) {
1107  t = 4.-t;
1108  sgn = -1.;
1109  }
1110  if (t >= -eps) {
1111  if (t < 1) {
1112  return 1./2.*pow(t,2)*sgn;
1113  } else if (t < 2) {
1114  t = t-1.;
1115  return ((-3./2.*t+1.)*t+1./2.)*sgn;
1116  }
1117  }
1118  return 0;
1119  }
1120  scalar_type bsp4_04_der2(scalar_type t) {
1121  if (t > 2) {
1122  t = 4.-t;
1123  }
1124  if (t >= -eps) {
1125  if (t < 1) {
1126  return t;
1127  } else if (t < 2) {
1128  t = t-1.;
1129  return -3.*t+1.;
1130  }
1131  }
1132  return 0;
1133  }
1134 
1135 
1136  // order k = 5
1137  scalar_type bsp5_01(scalar_type t) {
1138  return (t >= -eps && t < 1) ? pow(1.-t,4)
1139  : 0;
1140  }
1141  scalar_type bsp5_01_der(scalar_type t) {
1142  return (t >= -eps && t < 1) ? -4.*pow(1.-t,3)
1143  : 0;
1144  }
1145  scalar_type bsp5_01_der2(scalar_type t) {
1146  return (t >= -eps && t < 1) ? 12.*pow(1.-t,2)
1147  : 0;
1148  }
1149  scalar_type bsp5_02(scalar_type t) {
1150  if (t >= -eps) {
1151  if (t < 1) {
1152  return (((-15./8.*t+7.)*t-9.)*t+4.)*t;
1153  } else if (t < 2) {
1154  return 1./8.*pow(2.-t,4);
1155  }
1156  }
1157  return 0;
1158  }
1159  scalar_type bsp5_02_der(scalar_type t) {
1160  if (t >= -eps) {
1161  if (t < 1) {
1162  return ((-15./2.*t+21.)*t-18.)*t+4.;
1163  } else if (t < 2) {
1164  return -1./2.*pow(2.-t,3);
1165  }
1166  }
1167  return 0;
1168  }
1169  scalar_type bsp5_02_der2(scalar_type t) {
1170  if (t >= -eps) {
1171  if (t < 1) {
1172  return (-45./2.*t+42.)*t-18.;
1173  } else if (t < 2) {
1174  return 3./2.*pow(2.-t,2);
1175  }
1176  }
1177  return 0;
1178  }
1179  scalar_type bsp5_03(scalar_type t) {
1180  if (t >= -eps) {
1181  if (t < 1) {
1182  return ((85./72.*t-11./3.)*t+3.)*t*t;
1183  } else if (t < 2) {
1184  t = 2.-t;
1185  return (((-23./72*t+2./9.)*t+1./3.)*t+2./9.)*t+1./18.;
1186  } else if (t < 3) {
1187  return 1./18.*pow(3.-t,4);
1188  }
1189  }
1190  return 0;
1191  }
1192  scalar_type bsp5_03_der(scalar_type t) {
1193  if (t >= -eps) {
1194  if (t < 1) {
1195  return ((85./18.*t-11.)*t+6.)*t;
1196  } else if (t < 2) {
1197  t = 2.-t;
1198  return -(((-23./18*t+2./3.)*t+2./3.)*t+2./9.);
1199  } else if (t < 3) {
1200  return -2./9.*pow(3.-t,3);
1201  }
1202  }
1203  return 0;
1204  }
1205  scalar_type bsp5_03_der2(scalar_type t) {
1206  if (t >= -eps) {
1207  if (t < 1) {
1208  return (85./6.*t-22.)*t+6.;
1209  } else if (t < 2) {
1210  t = 2.-t;
1211  return (-23./6*t+4./3.)*t+2./3.;
1212  } else if (t < 3) {
1213  return 2./3.*pow(3.-t,2);
1214  }
1215  }
1216  return 0;
1217  }
1218  scalar_type bsp5_04(scalar_type t) {
1219  if (t >= -eps) {
1220  if (t < 1) {
1221  return (-25./72.*t+2./3.)*t*t*t;
1222  } else if (t < 2) {
1223  t = 2.-t;
1224  return (((23./72.*t-5./9.)*t-1./3.)*t+4./9.)*t+4./9.;
1225  } else if (t < 3) {
1226  t = t-2.;
1227  return (((-13./72.*t+5./9.)*t-1./3.)*t-4./9.)*t+4./9.;
1228  } else if (t < 4) {
1229  return 1./24.*pow(4.-t,4);
1230  }
1231  }
1232  return 0;
1233  }
1234  scalar_type bsp5_04_der(scalar_type t) {
1235  if (t >= -eps) {
1236  if (t < 1) {
1237  return (-25./18.*t+2.)*t*t;
1238  } else if (t < 2) {
1239  t = 2.-t;
1240  return -(((23./18.*t-5./3.)*t-2./3.)*t+4./9.);
1241  } else if (t < 3) {
1242  t = t-2.;
1243  return ((-13./18.*t+5./3.)*t-2./3.)*t-4./9.;
1244  } else if (t < 4) {
1245  return -1./6.*pow(4.-t,3);
1246  }
1247  }
1248  return 0;
1249  }
1250  scalar_type bsp5_04_der2(scalar_type t) {
1251  if (t >= -eps) {
1252  if (t < 1) {
1253  return (-25./6.*t+4.)*t;
1254  } else if (t < 2) {
1255  t = 2.-t;
1256  return (23./6.*t-10./3.)*t-2./3.;
1257  } else if (t < 3) {
1258  t = t-2.;
1259  return (-13./6.*t+10./3.)*t-2./3.;
1260  } else if (t < 4) {
1261  return 1./2.*pow(4.-t,2);
1262  }
1263  }
1264  return 0;
1265  }
1266  scalar_type bsp5_05(scalar_type t) {
1267  if (t > scalar_type(2.5)) {
1268  t = 5.-t;
1269  }
1270  if (t >= -eps) {
1271  if (t < 1) {
1272  return 1./24.*pow(t,4);
1273  } else if (t < 2) {
1274  t = t-1.;
1275  return (((-1./6.*t+1./6.)*t+1./4.)*t+1./6.)*t+1./24.;
1276  } else if (t < 3) {
1277  t = pow(t-2.5,2);
1278  return (1./4.*t-5./8.)*t+115./192.;
1279  }
1280  }
1281  return 0;
1282  }
1283  scalar_type bsp5_05_der(scalar_type t) {
1284  scalar_type sgn(1);
1285  if (t > scalar_type(2.5)) {
1286  t = 5.-t;
1287  sgn = -1.;
1288  }
1289  if (t >= -eps) {
1290  if (t < 1) {
1291  return 1./6.*pow(t,3)*sgn;
1292  } else if (t < 2) {
1293  t = t-1.;
1294  return (((-2./3.*t+1./2.)*t+1./2)*t+1./6.)*sgn;
1295  } else if (t < 3) {
1296  t = t-2.5;
1297  return (t*t-5./4.)*t*sgn;
1298  }
1299  }
1300  return 0;
1301  }
1302  scalar_type bsp5_05_der2(scalar_type t) {
1303  if (t > scalar_type(2.5)) {
1304  t = 5.-t;
1305  }
1306  if (t >= -eps) {
1307  if (t < 1) {
1308  return 1./2.*pow(t,2);
1309  } else if (t < 2) {
1310  t = t-1.;
1311  return (-2.*t+1.)*t+1./2;
1312  } else if (t < 3) {
1313  t = t-2.5;
1314  return 3*t*t-5./4.;
1315  }
1316  }
1317  return 0;
1318  }
1319 
1320 
1321 
1322  class global_function_x_bspline_
1323  : public global_function_simple, public context_dependencies {
1324  scalar_type xmin, xmax, xscale;
1325  std::function<scalar_type(scalar_type)> fx, fx_der, fx_der2;
1326  public:
1327  void update_from_context() const {}
1328 
1329  virtual scalar_type val(const base_node &pt) const
1330  {
1331  return fx(xscale*(pt[0]-xmin));
1332  }
1333  virtual void grad(const base_node &pt, base_small_vector &g) const
1334  {
1335  scalar_type dx = xscale*(pt[0]-xmin);
1336  g.resize(dim_);
1337  g[0] = xscale * fx_der(dx);
1338  }
1339  virtual void hess(const base_node &pt, base_matrix &h) const
1340  {
1341  scalar_type dx = xscale*(pt[0]-xmin);
1342  h.resize(dim_, dim_);
1343  gmm::clear(h);
1344  h(0,0) = xscale*xscale * fx_der2(dx);
1345  }
1346 
1347  virtual bool is_in_support(const base_node &pt) const {
1348  scalar_type dx = pt[0]-(xmin+xmax)/2;
1349  return (gmm::abs(dx)+1e-9 < gmm::abs(xmax-xmin)/2);
1350  }
1351 
1352  virtual void bounding_box(base_node &bmin, base_node &bmax) const {
1353  GMM_ASSERT1(bmin.size() == dim_ && bmax.size() == dim_,
1354  "Wrong dimensions");
1355  bmin[0] = std::min(xmin,xmax);
1356  bmax[0] = std::max(xmin,xmax);
1357  }
1358 
1359  global_function_x_bspline_(const scalar_type &xmin_, const scalar_type &xmax_,
1360  const size_type &order, const size_type &xtype)
1361  : global_function_simple(1), xmin(xmin_), xmax(xmax_),
1362  xscale(scalar_type(xtype)/(xmax-xmin))
1363  {
1364  GMM_ASSERT1(order >= 3 && order <= 5, "Only orders 3 to 5 are supported");
1365  GMM_ASSERT1(xtype >= 1 && xtype <= order, "Wrong input");
1366  if (order == 3) {
1367  if (xtype == 1) {
1368  fx = bsp3_01; fx_der = bsp3_01_der; fx_der2 = bsp3_01_der2;
1369  } else if (xtype == 2) {
1370  fx = bsp3_02; fx_der = bsp3_02_der; fx_der2 = bsp3_02_der2;
1371  } else if (xtype == 3) {
1372  fx = bsp3_03; fx_der = bsp3_03_der; fx_der2 = bsp3_03_der2;
1373  }
1374  } else if (order == 4) {
1375  if (xtype == 1) {
1376  fx = bsp4_01; fx_der = bsp4_01_der; fx_der2 = bsp4_01_der2;
1377  } else if (xtype == 2) {
1378  fx = bsp4_02; fx_der = bsp4_02_der; fx_der2 = bsp4_02_der2;
1379  } else if (xtype == 3) {
1380  fx = bsp4_03; fx_der = bsp4_03_der; fx_der2 = bsp4_03_der2;
1381  } else if (xtype == 4) {
1382  fx = bsp4_04; fx_der = bsp4_04_der; fx_der2 = bsp4_04_der2;
1383  }
1384  } else if (order == 5) {
1385  if (xtype == 1) {
1386  fx = bsp5_01; fx_der = bsp5_01_der; fx_der2 = bsp5_01_der2;
1387  } else if (xtype == 2) {
1388  fx = bsp5_02; fx_der = bsp5_02_der; fx_der2 = bsp5_02_der2;
1389  } else if (xtype == 3) {
1390  fx = bsp5_03; fx_der = bsp5_03_der; fx_der2 = bsp5_03_der2;
1391  } else if (xtype == 4) {
1392  fx = bsp5_04; fx_der = bsp5_04_der; fx_der2 = bsp5_04_der2;
1393  } else if (xtype == 5) {
1394  fx = bsp5_05; fx_der = bsp5_05_der; fx_der2 = bsp5_05_der2;
1395  }
1396  }
1397  }
1398 
1399  virtual ~global_function_x_bspline_()
1400  { DAL_STORED_OBJECT_DEBUG_DESTROYED(this, "Global function x bspline"); }
1401  };
1402 
1403 
1404 
1405  class global_function_xy_bspline_
1406  : public global_function_simple, public context_dependencies {
1407  scalar_type xmin, ymin, xmax, ymax, xscale, yscale;
1408  std::function<scalar_type(scalar_type)>
1409  fx, fy, fx_der, fy_der, fx_der2, fy_der2;
1410  public:
1411  void update_from_context() const {}
1412 
1413  virtual scalar_type val(const base_node &pt) const
1414  {
1415  return fx(xscale*(pt[0]-xmin))
1416  * fy(yscale*(pt[1]-ymin));
1417  }
1418  virtual void grad(const base_node &pt, base_small_vector &g) const
1419  {
1420  scalar_type dx = xscale*(pt[0]-xmin),
1421  dy = yscale*(pt[1]-ymin);
1422  g.resize(dim_);
1423  g[0] = xscale * fx_der(dx) * fy(dy);
1424  g[1] = fx(dx) * yscale * fy_der(dy);
1425  }
1426  virtual void hess(const base_node &pt, base_matrix &h) const
1427  {
1428  scalar_type dx = xscale*(pt[0]-xmin),
1429  dy = yscale*(pt[1]-ymin);
1430  h.resize(dim_, dim_);
1431  gmm::clear(h);
1432  h(0,0) = xscale*xscale * fx_der2(dx) * fy(dy);
1433  h(0,1) = h(1,0) = xscale * fx_der(dx) * yscale * fy_der(dy);
1434  h(1,1) = fx(dx) * yscale*yscale * fy_der2(dy);
1435  }
1436 
1437  virtual bool is_in_support(const base_node &pt) const {
1438  scalar_type dx = pt[0]-(xmin+xmax)/2,
1439  dy = pt[1]-(ymin+ymax)/2;
1440  return (gmm::abs(dx)+1e-9 < gmm::abs(xmax-xmin)/2) &&
1441  (gmm::abs(dy)+1e-9 < gmm::abs(ymax-ymin)/2);
1442  }
1443 
1444  virtual void bounding_box(base_node &bmin, base_node &bmax) const {
1445  GMM_ASSERT1(bmin.size() == dim_ && bmax.size() == dim_,
1446  "Wrong dimensions");
1447  bmin[0] = std::min(xmin,xmax);
1448  bmin[1] = std::min(ymin,ymax);
1449  bmax[0] = std::max(xmin,xmax);
1450  bmax[1] = std::max(ymin,ymax);
1451  }
1452 
1453  global_function_xy_bspline_(const scalar_type &xmin_, const scalar_type &xmax_,
1454  const scalar_type &ymin_, const scalar_type &ymax_,
1455  const size_type &order,
1456  const size_type &xtype, const size_type &ytype)
1457  : global_function_simple(2), xmin(xmin_), ymin(ymin_), xmax(xmax_), ymax(ymax_),
1458  xscale(scalar_type(xtype)/(xmax-xmin)), yscale(scalar_type(ytype)/(ymax-ymin))
1459  {
1460  GMM_ASSERT1(order >= 3 && order <= 5, "Wrong input");
1461  GMM_ASSERT1(xtype >= 1 && xtype <= order &&
1462  ytype >= 1 && ytype <= order, "Wrong input");
1463  if (order == 3) {
1464  if (xtype == 1) {
1465  fx = bsp3_01; fx_der = bsp3_01_der; fx_der2 = bsp3_01_der2;
1466  } else if (xtype == 2) {
1467  fx = bsp3_02; fx_der = bsp3_02_der; fx_der2 = bsp3_02_der2;
1468  } else if (xtype == 3) {
1469  fx = bsp3_03; fx_der = bsp3_03_der; fx_der2 = bsp3_03_der2;
1470  }
1471 
1472  if (ytype == 1) {
1473  fy = bsp3_01; fy_der = bsp3_01_der; fy_der2 = bsp3_01_der2;
1474  } else if (ytype == 2) {
1475  fy = bsp3_02; fy_der = bsp3_02_der; fy_der2 = bsp3_02_der2;
1476  } else if (ytype == 3) {
1477  fy = bsp3_03; fy_der = bsp3_03_der; fy_der2 = bsp3_03_der2;
1478  }
1479  } else if (order == 4) {
1480  if (xtype == 1) {
1481  fx = bsp4_01; fx_der = bsp4_01_der; fx_der2 = bsp4_01_der2;
1482  } else if (xtype == 2) {
1483  fx = bsp4_02; fx_der = bsp4_02_der; fx_der2 = bsp4_02_der2;
1484  } else if (xtype == 3) {
1485  fx = bsp4_03; fx_der = bsp4_03_der; fx_der2 = bsp4_03_der2;
1486  } else if (xtype == 4) {
1487  fx = bsp4_04; fx_der = bsp4_04_der; fx_der2 = bsp4_04_der2;
1488  }
1489 
1490  if (ytype == 1) {
1491  fy = bsp4_01; fy_der = bsp4_01_der; fy_der2 = bsp4_01_der2;
1492  } else if (ytype == 2) {
1493  fy = bsp4_02; fy_der = bsp4_02_der; fy_der2 = bsp4_02_der2;
1494  } else if (ytype == 3) {
1495  fy = bsp4_03; fy_der = bsp4_03_der; fy_der2 = bsp4_03_der2;
1496  } else if (ytype == 4) {
1497  fy = bsp4_04; fy_der = bsp4_04_der; fy_der2 = bsp4_04_der2;
1498  }
1499 
1500  } else if (order == 5) {
1501  if (xtype == 1) {
1502  fx = bsp5_01; fx_der = bsp5_01_der; fx_der2 = bsp5_01_der2;
1503  } else if (xtype == 2) {
1504  fx = bsp5_02; fx_der = bsp5_02_der; fx_der2 = bsp5_02_der2;
1505  } else if (xtype == 3) {
1506  fx = bsp5_03; fx_der = bsp5_03_der; fx_der2 = bsp5_03_der2;
1507  } else if (xtype == 4) {
1508  fx = bsp5_04; fx_der = bsp5_04_der; fx_der2 = bsp5_04_der2;
1509  } else if (xtype == 5) {
1510  fx = bsp5_05; fx_der = bsp5_05_der; fx_der2 = bsp5_05_der2;
1511  }
1512 
1513  if (ytype == 1) {
1514  fy = bsp5_01; fy_der = bsp5_01_der; fy_der2 = bsp5_01_der2;
1515  } else if (ytype == 2) {
1516  fy = bsp5_02; fy_der = bsp5_02_der; fy_der2 = bsp5_02_der2;
1517  } else if (ytype == 3) {
1518  fy = bsp5_03; fy_der = bsp5_03_der; fy_der2 = bsp5_03_der2;
1519  } else if (ytype == 4) {
1520  fy = bsp5_04; fy_der = bsp5_04_der; fy_der2 = bsp5_04_der2;
1521  } else if (ytype == 5) {
1522  fy = bsp5_05; fy_der = bsp5_05_der; fy_der2 = bsp5_05_der2;
1523  }
1524  }
1525  }
1526 
1527  virtual ~global_function_xy_bspline_()
1528  { DAL_STORED_OBJECT_DEBUG_DESTROYED(this, "Global function xy bspline"); }
1529  };
1530 
1531 
1532  class global_function_xyz_bspline_
1533  : public global_function_simple, public context_dependencies {
1534  scalar_type xmin, ymin, zmin, xmax, ymax, zmax, xscale, yscale, zscale;
1535  std::function<scalar_type(scalar_type)>
1536  fx, fy, fz, fx_der, fy_der, fz_der, fx_der2, fy_der2, fz_der2;
1537  public:
1538  void update_from_context() const {}
1539 
1540  virtual scalar_type val(const base_node &pt) const
1541  {
1542  return fx(xscale*(pt[0]-xmin))
1543  * fy(yscale*(pt[1]-ymin))
1544  * fz(zscale*(pt[2]-zmin));
1545  }
1546  virtual void grad(const base_node &pt, base_small_vector &g) const
1547  {
1548  scalar_type dx = xscale*(pt[0]-xmin),
1549  dy = yscale*(pt[1]-ymin),
1550  dz = zscale*(pt[2]-zmin);
1551  g.resize(dim_);
1552  g[0] = xscale * fx_der(dx) * fy(dy) * fz(dz);
1553  g[1] = fx(dx) * yscale * fy_der(dy) * fz(dz);
1554  g[2] = fx(dx) * fy(dy) * zscale * fz_der(dz);
1555  }
1556  virtual void hess(const base_node &pt, base_matrix &h) const
1557  {
1558  scalar_type dx = xscale*(pt[0]-xmin),
1559  dy = yscale*(pt[1]-ymin),
1560  dz = zscale*(pt[2]-zmin);
1561  h.resize(dim_, dim_);
1562  gmm::clear(h);
1563  h(0,0) = xscale*xscale * fx_der2(dx) * fy(dy) * fz(dz);
1564  h(1,1) = fx(dx) * yscale*yscale * fy_der2(dy) * fz(dz);
1565  h(2,2) = fx(dx) * fy(dy) * zscale*zscale * fz_der2(dz);
1566  h(0,1) = h(1,0) = xscale * fx_der(dx) * yscale * fy_der(dy) * fz(dz);
1567  h(0,2) = h(2,0) = xscale * fx_der(dx) * fy(dy) * zscale * fz_der(dz);
1568  h(1,2) = h(2,1) = fx(dx) * yscale * fy_der(dy) * zscale * fz_der(dz);
1569  }
1570 
1571  virtual bool is_in_support(const base_node &pt) const {
1572  scalar_type dx = pt[0]-(xmin+xmax)/2,
1573  dy = pt[1]-(ymin+ymax)/2,
1574  dz = pt[2]-(zmin+zmax)/2;
1575  return (gmm::abs(dx)+1e-9 < gmm::abs(xmax-xmin)/2) &&
1576  (gmm::abs(dy)+1e-9 < gmm::abs(ymax-ymin)/2) &&
1577  (gmm::abs(dz)+1e-9 < gmm::abs(zmax-zmin)/2);
1578  }
1579 
1580  virtual void bounding_box(base_node &bmin, base_node &bmax) const {
1581  GMM_ASSERT1(bmin.size() == dim_ && bmax.size() == dim_,
1582  "Wrong dimensions");
1583  bmin[0] = std::min(xmin,xmax);
1584  bmin[1] = std::min(ymin,ymax);
1585  bmin[2] = std::min(zmin,zmax);
1586  bmax[0] = std::max(xmin,xmax);
1587  bmax[1] = std::max(ymin,ymax);
1588  bmax[2] = std::max(zmin,zmax);
1589  }
1590 
1591  global_function_xyz_bspline_(const scalar_type &xmin_, const scalar_type &xmax_,
1592  const scalar_type &ymin_, const scalar_type &ymax_,
1593  const scalar_type &zmin_, const scalar_type &zmax_,
1594  const size_type &order,
1595  const size_type &xtype,
1596  const size_type &ytype,
1597  const size_type &ztype)
1598  : global_function_simple(3), xmin(xmin_), ymin(ymin_), zmin(zmin_),
1599  xmax(xmax_), ymax(ymax_), zmax(zmax_),
1600  xscale(scalar_type(xtype)/(xmax-xmin)),
1601  yscale(scalar_type(ytype)/(ymax-ymin)),
1602  zscale(scalar_type(ztype)/(zmax-zmin))
1603  {
1604  GMM_ASSERT1(order >= 3 && order <= 5, "Wrong input");
1605  GMM_ASSERT1(xtype >= 1 && xtype <= order &&
1606  ytype >= 1 && ytype <= order &&
1607  ztype >= 1 && ztype <= order, "Wrong input");
1608  if (order == 3) {
1609 
1610  if (xtype == 1) {
1611  fx = bsp3_01; fx_der = bsp3_01_der; fx_der2 = bsp3_01_der2;
1612  } else if (xtype == 2) {
1613  fx = bsp3_02; fx_der = bsp3_02_der; fx_der2 = bsp3_02_der2;
1614  } else if (xtype == 3) {
1615  fx = bsp3_03; fx_der = bsp3_03_der; fx_der2 = bsp3_03_der2;
1616  }
1617 
1618  if (ytype == 1) {
1619  fy = bsp3_01; fy_der = bsp3_01_der; fy_der2 = bsp3_01_der2;
1620  } else if (ytype == 2) {
1621  fy = bsp3_02; fy_der = bsp3_02_der; fy_der2 = bsp3_02_der2;
1622  } else if (ytype == 3) {
1623  fy = bsp3_03; fy_der = bsp3_03_der; fy_der2 = bsp3_03_der2;
1624  }
1625 
1626  if (ztype == 1) {
1627  fz = bsp3_01; fz_der = bsp3_01_der; fz_der2 = bsp3_01_der2;
1628  } else if (ztype == 2) {
1629  fz = bsp3_02; fz_der = bsp3_02_der; fz_der2 = bsp3_02_der2;
1630  } else if (ztype == 3) {
1631  fz = bsp3_03; fz_der = bsp3_03_der; fz_der2 = bsp3_03_der2;
1632  }
1633 
1634  } else if (order == 4) {
1635 
1636  if (xtype == 1) {
1637  fx = bsp4_01; fx_der = bsp4_01_der; fx_der2 = bsp4_01_der2;
1638  } else if (xtype == 2) {
1639  fx = bsp4_02; fx_der = bsp4_02_der; fx_der2 = bsp4_02_der2;
1640  } else if (xtype == 3) {
1641  fx = bsp4_03; fx_der = bsp4_03_der; fx_der2 = bsp4_03_der2;
1642  } else if (xtype == 4) {
1643  fx = bsp4_04; fx_der = bsp4_04_der; fx_der2 = bsp4_04_der2;
1644  }
1645 
1646  if (ytype == 1) {
1647  fy = bsp4_01; fy_der = bsp4_01_der; fy_der2 = bsp4_01_der2;
1648  } else if (ytype == 2) {
1649  fy = bsp4_02; fy_der = bsp4_02_der; fy_der2 = bsp4_02_der2;
1650  } else if (ytype == 3) {
1651  fy = bsp4_03; fy_der = bsp4_03_der; fy_der2 = bsp4_03_der2;
1652  } else if (ytype == 4) {
1653  fy = bsp4_04; fy_der = bsp4_04_der; fy_der2 = bsp4_04_der2;
1654  }
1655 
1656  if (ztype == 1) {
1657  fz = bsp4_01; fz_der = bsp4_01_der; fz_der2 = bsp4_01_der2;
1658  } else if (ztype == 2) {
1659  fz = bsp4_02; fz_der = bsp4_02_der; fz_der2 = bsp4_02_der2;
1660  } else if (ztype == 3) {
1661  fz = bsp4_03; fz_der = bsp4_03_der; fz_der2 = bsp4_03_der2;
1662  } else if (ztype == 4) {
1663  fz = bsp4_04; fz_der = bsp4_04_der; fz_der2 = bsp4_04_der2;
1664  }
1665 
1666  } else if (order == 5) {
1667 
1668  if (xtype == 1) {
1669  fx = bsp5_01; fx_der = bsp5_01_der; fx_der2 = bsp5_01_der2;
1670  } else if (xtype == 2) {
1671  fx = bsp5_02; fx_der = bsp5_02_der; fx_der2 = bsp5_02_der2;
1672  } else if (xtype == 3) {
1673  fx = bsp5_03; fx_der = bsp5_03_der; fx_der2 = bsp5_03_der2;
1674  } else if (xtype == 4) {
1675  fx = bsp5_04; fx_der = bsp5_04_der; fx_der2 = bsp5_04_der2;
1676  } else if (xtype == 5) {
1677  fx = bsp5_05; fx_der = bsp5_05_der; fx_der2 = bsp5_05_der2;
1678  }
1679 
1680  if (ytype == 1) {
1681  fy = bsp5_01; fy_der = bsp5_01_der; fy_der2 = bsp5_01_der2;
1682  } else if (ytype == 2) {
1683  fy = bsp5_02; fy_der = bsp5_02_der; fy_der2 = bsp5_02_der2;
1684  } else if (ytype == 3) {
1685  fy = bsp5_03; fy_der = bsp5_03_der; fy_der2 = bsp5_03_der2;
1686  } else if (ytype == 4) {
1687  fy = bsp5_04; fy_der = bsp5_04_der; fy_der2 = bsp5_04_der2;
1688  } else if (ytype == 5) {
1689  fy = bsp5_05; fy_der = bsp5_05_der; fy_der2 = bsp5_05_der2;
1690  }
1691 
1692  if (ztype == 1) {
1693  fz = bsp5_01; fz_der = bsp5_01_der; fz_der2 = bsp5_01_der2;
1694  } else if (ztype == 2) {
1695  fz = bsp5_02; fz_der = bsp5_02_der; fz_der2 = bsp5_02_der2;
1696  } else if (ztype == 3) {
1697  fz = bsp5_03; fz_der = bsp5_03_der; fz_der2 = bsp5_03_der2;
1698  } else if (ztype == 4) {
1699  fz = bsp5_04; fz_der = bsp5_04_der; fz_der2 = bsp5_04_der2;
1700  } else if (ztype == 5) {
1701  fz = bsp5_05; fz_der = bsp5_05_der; fz_der2 = bsp5_05_der2;
1702  }
1703 
1704  }
1705  }
1706 
1707  virtual ~global_function_xyz_bspline_()
1708  { DAL_STORED_OBJECT_DEBUG_DESTROYED(this, "Global function xyz bspline"); }
1709  };
1710 
1711 
1712  pglobal_function
1713  global_function_bspline(const scalar_type xmin, const scalar_type xmax,
1714  const size_type order, const size_type xtype) {
1715  return std::make_shared<global_function_x_bspline_>
1716  (xmin, xmax, order, xtype);
1717  }
1718 
1719  pglobal_function
1720  global_function_bspline(const scalar_type xmin, const scalar_type xmax,
1721  const scalar_type ymin, const scalar_type ymax,
1722  const size_type order,
1723  const size_type xtype, const size_type ytype) {
1724  return std::make_shared<global_function_xy_bspline_>
1725  (xmin, xmax, ymin, ymax, order, xtype, ytype);
1726  }
1727 
1728  pglobal_function
1729  global_function_bspline(const scalar_type xmin, const scalar_type xmax,
1730  const scalar_type ymin, const scalar_type ymax,
1731  const scalar_type zmin, const scalar_type zmax,
1732  const size_type order,
1733  const size_type xtype,
1734  const size_type ytype,
1735  const size_type ztype) {
1736  return std::make_shared<global_function_xyz_bspline_>
1737  (xmin, xmax, ymin, ymax, zmin, zmax, order,
1738  xtype, ytype, ztype);
1739  }
1740 
1741 
1742 
1743  // interpolator on mesh_fem
1744 
1745  interpolator_on_mesh_fem::interpolator_on_mesh_fem
1746  (const mesh_fem &mf_, const std::vector<scalar_type> &U_)
1747  : mf(mf_), U(U_) {
1748 
1749  if (mf.is_reduced()) {
1750  gmm::resize(U, mf.nb_basic_dof());
1751  gmm::mult(mf.extension_matrix(), U_, U);
1752  }
1753  base_node bmin, bmax;
1754  scalar_type EPS=1E-13;
1755  cv_stored = size_type(-1);
1756  boxtree.clear();
1757  for (dal::bv_visitor cv(mf.convex_index()); !cv.finished(); ++cv) {
1758  bounding_box(bmin, bmax, mf.linked_mesh().points_of_convex(cv),
1759  mf.linked_mesh().trans_of_convex(cv));
1760  for (auto&& val : bmin) val -= EPS;
1761  for (auto&& val : bmax) val += EPS;
1762  boxtree.add_box(bmin, bmax, cv);
1763  }
1764  boxtree.build_tree();
1765  }
1766 
1767  bool interpolator_on_mesh_fem::find_a_point(const base_node &pt,
1768  base_node &ptr,
1769  size_type &cv) const {
1770  bool gt_invertible;
1771  if (cv_stored != size_type(-1) && gic.invert(pt, ptr, gt_invertible)) {
1772  cv = cv_stored;
1773  if (gt_invertible)
1774  return true;
1775  }
1776 
1777  boxtree.find_boxes_at_point(pt, boxlst);
1778  for (const auto &box : boxlst) {
1780  (mf.linked_mesh().convex(box->id),
1781  mf.linked_mesh().trans_of_convex(box->id));
1782  cv_stored = box->id;
1783  if (gic.invert(pt, ptr, gt_invertible)) {
1784  cv = box->id;
1785  return true;
1786  }
1787  }
1788  return false;
1789  }
1790 
1791  bool interpolator_on_mesh_fem::eval(const base_node &pt, base_vector &val,
1792  base_matrix &grad) const {
1793  base_node ptref;
1794  size_type cv;
1795  base_vector coeff;
1796  dim_type q = mf.get_qdim(), N = mf.linked_mesh().dim();
1797  if (find_a_point(pt, ptref, cv)) {
1798  pfem pf = mf.fem_of_element(cv);
1800  mf.linked_mesh().trans_of_convex(cv);
1801  base_matrix G;
1802  vectors_to_base_matrix(G, mf.linked_mesh().points_of_convex(cv));
1803  fem_interpolation_context fic(pgt, pf, ptref, G, cv, short_type(-1));
1804  slice_vector_on_basic_dof_of_element(mf, U, cv, coeff);
1805  // coeff.resize(mf.nb_basic_dof_of_element(cv));
1806  // gmm::copy(gmm::sub_vector
1807  // (U,gmm::sub_index(mf.ind_basic_dof_of_element(cv))), coeff);
1808  val.resize(q);
1809  pf->interpolation(fic, coeff, val, q);
1810  grad.resize(q, N);
1811  pf->interpolation_grad(fic, coeff, grad, q);
1812  return true;
1813  } else
1814  return false;
1815  }
1816 }
1817 
1818 /* end of namespace getfem */
does the inversion of the geometric transformation for a given convex
Definition of global functions to be used as base or enrichment functions in fem.
Define level-sets.
void mult_add(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1790
void reshape(M &v, size_type m, size_type n)
*‍/
Definition: gmm_blas.h:250
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
void fill(L &l, typename gmm::linalg_traits< L >::value_type x)
*‍/
Definition: gmm_blas.h:103
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 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
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
void add_dependency(pstatic_stored_object o1, pstatic_stored_object o2)
Add a dependency, object o1 will depend on object o2.
GEneric Tool for Finite Element Methods.
void slice_vector_on_basic_dof_of_element(const mesh_fem &mf, const VEC1 &vec, size_type cv, VEC2 &coeff, size_type qmult1=size_type(-1), size_type qmult2=size_type(-1))
Given a mesh_fem.