GetFEM  5.6
getfem_generic_assembly_tree.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 
22 #include "getfem/getfem_generic_assembly_functions_and_operators.h"
23 #include "getfem/getfem_generic_assembly_compile_and_exec.h"
24 
25 
26 namespace getfem {
27 
28  //=========================================================================
29  // Lexical analysis for the generic assembly language
30  //=========================================================================
31 
32  static GA_TOKEN_TYPE ga_char_type[256];
33  static int ga_operator_priorities[GA_NB_TOKEN_TYPE];
34 
35  // Initialize ga_char_type and ga_operator_priorities arrays
36  static bool init_ga_char_type() {
37  for (int i = 0; i < 256; ++i) ga_char_type[i] = GA_INVALID;
38  ga_char_type['+'] = GA_PLUS; ga_char_type['-'] = GA_MINUS;
39  ga_char_type['*'] = GA_MULT; ga_char_type['/'] = GA_DIV;
40  ga_char_type[':'] = GA_COLON; ga_char_type['\''] = GA_QUOTE;
41  ga_char_type['.'] = GA_DOT; ga_char_type['@'] = GA_TMULT;
42  ga_char_type[','] = GA_COMMA; ga_char_type[';'] = GA_SEMICOLON;
43  ga_char_type['('] = GA_LPAR; ga_char_type[')'] = GA_RPAR;
44  ga_char_type['['] = GA_LBRACKET; ga_char_type[']'] = GA_RBRACKET;
45  ga_char_type['_'] = GA_NAME; ga_char_type['='] = GA_COLON_EQ;
46  for (unsigned i = 'a'; i <= 'z'; ++i) ga_char_type[i] = GA_NAME;
47  for (unsigned i = 'A'; i <= 'Z'; ++i) ga_char_type[i] = GA_NAME;
48  for (unsigned i = '0'; i <= '9'; ++i) ga_char_type[i] = GA_SCALAR;
49 
50  for (unsigned i=0; i < GA_NB_TOKEN_TYPE; ++i) ga_operator_priorities[i] = 0;
51  ga_operator_priorities[GA_PLUS] = 1;
52  ga_operator_priorities[GA_MINUS] = 1;
53  ga_operator_priorities[GA_MULT] = 2;
54  ga_operator_priorities[GA_DIV] = 2;
55  ga_operator_priorities[GA_COLON] = 2;
56  ga_operator_priorities[GA_DOT] = 2;
57  ga_operator_priorities[GA_DOTMULT] = 2;
58  ga_operator_priorities[GA_DOTDIV] = 2;
59  ga_operator_priorities[GA_TMULT] = 2;
60  ga_operator_priorities[GA_QUOTE] = 3;
61  ga_operator_priorities[GA_UNARY_MINUS] = 3;
62  ga_operator_priorities[GA_SYM] = 4;
63  ga_operator_priorities[GA_SKEW] = 4;
64  ga_operator_priorities[GA_TRACE] = 4;
65  ga_operator_priorities[GA_DEVIATOR] = 4;
66  ga_operator_priorities[GA_PRINT] = 4;
67 
68  return true;
69  }
70 
71  static bool ga_initialized = init_ga_char_type();
72 
73  // Get the next token in the string at position 'pos' end return its type
74  static GA_TOKEN_TYPE ga_get_token(const std::string &expr,
75  size_type &pos,
76  size_type &token_pos,
77  size_type &token_length) {
78  bool fdot = false, fE = false;
79  GMM_ASSERT1(ga_initialized, "Internal error");
80 
81  // Ignore white spaces
82  while (expr[pos] == ' ' && pos < expr.size()) ++pos;
83  token_pos = pos;
84  token_length = 0;
85 
86  // Detecting end of expression
87  if (pos >= expr.size()) return GA_END;
88 
89  // Treating the different cases (Operation, name or number)
90  GA_TOKEN_TYPE type = ga_char_type[unsigned(expr[pos])];
91  ++pos; ++token_length;
92  if (type == GA_DOT) {
93  if (pos >= expr.size()) return type;
94  if (expr[pos] == '*') { ++pos; ++token_length; return GA_DOTMULT; }
95  if (expr[pos] == '/') { ++pos; ++token_length; return GA_DOTDIV; }
96  if (ga_char_type[unsigned(expr[pos])] != GA_SCALAR) return type;
97  fdot = true; type = GA_SCALAR;
98  }
99  switch (type) {
100  case GA_SCALAR:
101  while (pos < expr.size()) {
102  GA_TOKEN_TYPE ctype = ga_char_type[unsigned(expr[pos])];
103  switch (ctype) {
104  case GA_DOT:
105  if (fdot) return type;
106  fdot = true; ++pos; ++token_length;
107  break;
108  case GA_NAME:
109  if (fE || (expr[pos] != 'E' && expr[pos] != 'e')) return type;
110  fE = true; fdot = true; ++pos; ++token_length;
111  if (pos < expr.size()) {
112  if (expr[pos] == '+' || expr[pos] == '-')
113  { ++pos; ++token_length; }
114  }
115  if (pos >= expr.size()
116  || ga_char_type[unsigned(expr[pos])] != GA_SCALAR)
117  return GA_INVALID;
118  break;
119  case GA_SCALAR:
120  ++pos; ++token_length; break;
121  default:
122  return type;
123  }
124  }
125  return type;
126  case GA_NAME:
127  while (pos < expr.size()) {
128  GA_TOKEN_TYPE ctype = ga_char_type[unsigned(expr[pos])];
129  if (ctype != GA_SCALAR && ctype != GA_NAME) break;
130  ++pos; ++token_length;
131  }
132  if (expr.compare(token_pos, token_length, "Sym") == 0)
133  return GA_SYM;
134  if (expr.compare(token_pos, token_length, "Def") == 0)
135  return GA_DEF;
136  if (expr.compare(token_pos, token_length, "Skew") == 0)
137  return GA_SKEW;
138  if (expr.compare(token_pos, token_length, "Trace") == 0)
139  return GA_TRACE;
140  if (expr.compare(token_pos, token_length, "Deviator") == 0)
141  return GA_DEVIATOR;
142  if (expr.compare(token_pos, token_length, "Interpolate") == 0)
143  return GA_INTERPOLATE;
144  if (expr.compare(token_pos, token_length, "Interpolate_derivative") == 0)
145  return GA_INTERPOLATE_DERIVATIVE;
146  if (expr.compare(token_pos, token_length, "Interpolate_filter") == 0)
147  return GA_INTERPOLATE_FILTER;
148  if (expr.compare(token_pos, token_length,
149  "Elementary_transformation") == 0)
150  return GA_ELEMENTARY;
151  if (expr.compare(token_pos, token_length, "Secondary_domain") == 0 ||
152  expr.compare(token_pos, token_length, "Secondary_Domain") == 0)
153  return GA_SECONDARY_DOMAIN;
154  if (expr.compare(token_pos, token_length, "Xfem_plus") == 0)
155  return GA_XFEM_PLUS;
156  if (expr.compare(token_pos, token_length, "Xfem_minus") == 0)
157  return GA_XFEM_MINUS;
158  if (expr.compare(token_pos, token_length, "Print") == 0)
159  return GA_PRINT;
160  return type;
161  case GA_COMMA:
162  if (pos < expr.size() &&
163  ga_char_type[unsigned(expr[pos])] == GA_COMMA) {
164  ++pos; return GA_DCOMMA;
165  }
166  return type;
167  case GA_SEMICOLON:
168  if (pos < expr.size() &&
169  ga_char_type[unsigned(expr[pos])] == GA_SEMICOLON) {
170  ++pos; return GA_DSEMICOLON;
171  }
172  return type;
173  case GA_COLON:
174  if (pos < expr.size() &&
175  ga_char_type[unsigned(expr[pos])] == GA_COLON_EQ) {
176  ++pos; return GA_COLON_EQ;
177  }
178  return type;
179  case GA_COLON_EQ:
180  return GA_INVALID;
181  default: return type;
182  }
183  }
184 
185  //=========================================================================
186  // Error handling
187  //=========================================================================
188 
189  void ga_throw_error_msg(pstring expr, size_type pos,
190  const std::string &msg) {
191  int length_before = 70, length_after = 70;
192  if (expr && expr->size()) {
193  int first = std::max(0, int(pos)-length_before);
194  int last = std::min(int(pos)+length_after, int(expr->size()));
195  if (last - first < length_before+length_after)
196  first = std::max(0, int(pos)-length_before
197  -(length_before+length_after-last+first));
198  if (last - first < length_before+length_after)
199  last = std::min(int(pos)+length_after
200  +(length_before+length_after-last+first),
201  int(expr->size()));
202  if (first > 0) cerr << "...";
203  cerr << expr->substr(first, last-first);
204  if (last < int(expr->size())) cerr << "...";
205  cerr << endl;
206  if (first > 0) cerr << " ";
207  if (int(pos) > first)
208  cerr << std::setfill ('-') << std::setw(int(pos)-first) << '-'
209  << std::setfill (' ');
210  cerr << "^" << endl;
211  }
212  cerr << msg << endl;
213  }
214 
215  //=========================================================================
216  // Tree structure
217  //=========================================================================
218 
219  void ga_tree_node::mult_test(const pga_tree_node n0, const pga_tree_node n1) {
220 
221  size_type test0 = n0->test_function_type, test1 = n1->test_function_type;
222  if (test0 && test1 && (test0 == test1 || test0 >= 3 || test1 >= 3))
223  ga_throw_error(expr, pos,
224  "Incompatibility of test functions in product.");
225  GMM_ASSERT1(test0 != size_type(-1) && test1 != size_type(-1),
226  "internal error");
227 
228  test_function_type = test0 + test1;
229 
230  size_type st = nb_test_functions();
231  bgeot::multi_index mi(st);
232 
233  switch (test0) {
234  case 1: mi[0] = n0->t.sizes()[0]; break;
235  case 2: mi[st-1] = n0->t.sizes()[0]; break;
236  case 3: mi[0] = n0->t.sizes()[0]; mi[1] = n0->t.sizes()[1]; break;
237  }
238  switch (test1) {
239  case 1: mi[0] = n1->t.sizes()[0]; break;
240  case 2: mi[st-1] = n1->t.sizes()[0]; break;
241  case 3: mi[0] = n1->t.sizes()[0]; mi[1] = n1->t.sizes()[1]; break;
242  }
243 
244  if (n0->name_test1.size()) {
245  name_test1 = n0->name_test1; qdim1 = n0->qdim1;
246  interpolate_name_test1 = n0->interpolate_name_test1;
247  } else {
248  name_test1 = n1->name_test1; qdim1 = n1->qdim1;
249  interpolate_name_test1 = n1->interpolate_name_test1;
250  }
251 
252  if (n0->name_test2.size()) {
253  name_test2 = n0->name_test2; qdim2 = n0->qdim2;
254  interpolate_name_test2 = n0->interpolate_name_test2;
255  } else {
256  name_test2 = n1->name_test2; qdim2 = n1->qdim2;
257  interpolate_name_test2 = n1->interpolate_name_test2;
258  }
259  t.adjust_sizes(mi);
260  }
261 
262  void ga_tree::add_scalar(scalar_type val, size_type pos, pstring expr) {
263  while (current_node && current_node->node_type != GA_NODE_OP)
264  current_node = current_node->parent;
265  if (current_node) {
266  current_node->adopt_child(new ga_tree_node(val, pos, expr));
267  current_node = current_node->children.back();
268  }
269  else {
270  GMM_ASSERT1(root == nullptr, "Invalid tree operation");
271  current_node = root = new ga_tree_node(val, pos, expr);
272  root->parent = nullptr;
273  }
274  }
275 
276  void ga_tree::add_allindices(size_type pos, pstring expr) {
277  while (current_node && current_node->node_type != GA_NODE_OP)
278  current_node = current_node->parent;
279  if (current_node) {
280  current_node->adopt_child(new ga_tree_node(GA_NODE_ALLINDICES, pos,expr));
281  current_node = current_node->children.back();
282  }
283  else {
284  GMM_ASSERT1(root == nullptr, "Invalid tree operation");
285  current_node = root = new ga_tree_node(GA_NODE_ALLINDICES, pos, expr);
286  root->parent = nullptr;
287  }
288  }
289 
290  void ga_tree::add_name(const char *name, size_type length, size_type pos,
291  pstring expr) {
292  while (current_node && current_node->node_type != GA_NODE_OP)
293  current_node = current_node->parent;
294  if (current_node) {
295  current_node->adopt_child(new ga_tree_node(name, length, pos, expr));
296  current_node = current_node->children.back();
297  }
298  else {
299  GMM_ASSERT1(root == nullptr, "Invalid tree operation");
300  current_node = root = new ga_tree_node(name, length, pos, expr);
301  root->parent = nullptr;
302  }
303  }
304 
305  void ga_tree::add_sub_tree(ga_tree &sub_tree) {
306  if (current_node &&
307  (current_node->node_type == GA_NODE_PARAMS ||
308  current_node->node_type == GA_NODE_INTERPOLATE_FILTER ||
309  current_node->node_type == GA_NODE_C_MATRIX)) {
310  GMM_ASSERT1(sub_tree.root, "Invalid tree operation");
311  current_node->adopt_child(sub_tree.root);
312  } else {
313  GMM_ASSERT1(sub_tree.root, "Invalid tree operation");
314  while (current_node && current_node->node_type != GA_NODE_OP)
315  current_node = current_node->parent;
316  if (current_node) {
317  current_node->adopt_child(sub_tree.root);
318  current_node = sub_tree.root;
319  }
320  else {
321  GMM_ASSERT1(root == nullptr, "Invalid tree operation");
322  current_node = root = sub_tree.root;
323  root->parent = nullptr;
324  }
325  }
326  sub_tree.root = sub_tree.current_node = nullptr;
327  }
328 
329  void ga_tree::add_params(size_type pos, pstring expr) {
330  GMM_ASSERT1(current_node, "internal error");
331  while (current_node && current_node->parent &&
332  current_node->parent->node_type == GA_NODE_OP &&
333  ga_operator_priorities[current_node->parent->op_type] >= 4)
334  current_node = current_node->parent;
335  pga_tree_node new_node = new ga_tree_node(GA_NODE_PARAMS, pos, expr);
336  new_node->parent = current_node->parent;
337  if (current_node->parent)
338  current_node->parent->replace_child(current_node, new_node);
339  else
340  root = new_node;
341  new_node->adopt_child(current_node);
342  current_node = new_node;
343  }
344 
345  void ga_tree::add_matrix(size_type pos, pstring expr) {
346  while (current_node && current_node->node_type != GA_NODE_OP)
347  current_node = current_node->parent;
348  if (current_node) {
349  current_node->adopt_child(new ga_tree_node(GA_NODE_C_MATRIX, pos, expr));
350  current_node = current_node->children.back();
351  }
352  else {
353  GMM_ASSERT1(root == nullptr, "Invalid tree operation");
354  current_node = root = new ga_tree_node(GA_NODE_C_MATRIX, pos, expr);
355  root->parent = nullptr;
356  }
357  current_node->nbc1 = current_node->nbc2 = current_node->nbc3 = 0;
358  }
359 
360  void ga_tree::add_op(GA_TOKEN_TYPE op_type, size_type pos,
361  pstring expr) {
362  while (current_node && current_node->parent &&
363  current_node->parent->node_type == GA_NODE_OP &&
364  ga_operator_priorities[current_node->parent->op_type]
365  >= ga_operator_priorities[op_type])
366  current_node = current_node->parent;
367  pga_tree_node new_node = new ga_tree_node(op_type, pos, expr);
368  if (current_node) {
369  if (op_type == GA_UNARY_MINUS
370  || op_type == GA_SYM || op_type == GA_SKEW
371  || op_type == GA_TRACE || op_type == GA_DEVIATOR
372  || op_type == GA_PRINT) {
373  current_node->adopt_child(new_node);
374  } else {
375  new_node->parent = current_node->parent;
376  if (current_node->parent)
377  current_node->parent->replace_child(current_node, new_node);
378  else
379  root = new_node;
380  new_node->adopt_child(current_node);
381  }
382  } else {
383  if (root) new_node->adopt_child(root);
384  root = new_node;
385  root->parent = nullptr;
386  }
387  current_node = new_node;
388  }
389 
390  void ga_tree::clear_node_rec(pga_tree_node pnode) {
391  if (pnode) {
392  for (pga_tree_node &child : pnode->children)
393  clear_node_rec(child);
394  delete pnode;
395  current_node = nullptr;
396  }
397  }
398 
399  void ga_tree::clear_node(pga_tree_node pnode) {
400  if (pnode) {
401  pga_tree_node parent = pnode->parent;
402  if (parent) { // keep all siblings of pnode
403  size_type j = 0;
404  for (pga_tree_node &sibling : parent->children)
405  if (sibling != pnode)
406  parent->children[j++] = sibling;
407  parent->children.resize(j);
408  } else
409  root = nullptr;
410  }
411  clear_node_rec(pnode);
412  }
413 
414  void ga_tree::clear_children(pga_tree_node pnode) {
415  for (pga_tree_node &child : pnode->children)
416  clear_node_rec(child);
417  pnode->children.resize(0);
418  }
419 
420  void ga_tree::replace_node_by_child(pga_tree_node pnode, size_type i) {
421  GMM_ASSERT1(i < pnode->children.size(), "Internal error");
422  pga_tree_node child = pnode->children[i];
423  child->parent = pnode->parent;
424  if (pnode->parent)
425  pnode->parent->replace_child(pnode, child);
426  else
427  root = child;
428  current_node = nullptr;
429  for (pga_tree_node &sibling : pnode->children)
430  if (sibling != child) clear_node_rec(sibling);
431  delete pnode;
432  }
433 
434  size_type ga_tree_count_nodes(pga_tree_node pnode) {
435  size_type count = 0;
436  if (pnode) {
437  count++;
438  for (pga_tree_node child : pnode->children)
439  count += ga_tree_count_nodes(child);
440  }
441  return count;
442  }
443 
444  void ga_tree_copy_node_rec(pga_tree_node pnode,
445  pga_tree_node &pnode_new,
446  std::vector<pga_tree_node> &preallocated_nodes) {
447  GMM_ASSERT1(pnode_new == nullptr, "Internal error");
448  GMM_ASSERT1(preallocated_nodes.size() > 0,
449  "Internal error, too small preallocation of nodes");
450  pnode_new = preallocated_nodes.back();
451  *pnode_new = *pnode;
452  preallocated_nodes.pop_back();
453  pnode_new->parent = nullptr;
454  for (size_type j = 0; j < pnode_new->children.size(); ++j) {
455  pnode_new->children[j] = nullptr;
456  ga_tree_copy_node_rec(pnode->children[j], pnode_new->children[j],
457  preallocated_nodes);
458  pnode_new->accept_child(j);
459  }
460  }
461 
462  // This function allocates a new node "pnode_new", copies the content of "pnode"
463  // into the newly allocated node (including deep copies of all of its children)
464  void ga_tree::copy_node(pga_tree_node pnode,
465  pga_tree_node &pnode_new) {
466  const size_type count = ga_tree_count_nodes(pnode);
467  std::vector<pga_tree_node> preallocated_nodes(count);
468  for (size_type i=0; i < count; ++i) // heap pre-allocation for performance reasons
469  preallocated_nodes[i] = new ga_tree_node();
470  ga_tree_copy_node_rec(pnode, pnode_new, preallocated_nodes);
471  }
472 
473  void ga_tree::duplicate_with_operation(pga_tree_node pnode,
474  GA_TOKEN_TYPE op_type) {
475  pga_tree_node newop = new ga_tree_node(op_type, pnode->pos, pnode->expr);
476  newop->children.resize(2, nullptr);
477  newop->children[0] = pnode;
478  newop->pos = pnode->pos; newop->expr = pnode->expr;
479  newop->parent = pnode->parent;
480  if (pnode->parent)
481  pnode->parent->replace_child(pnode, newop);
482  else
483  root = newop;
484  pnode->parent = newop;
485  copy_node(pnode, newop->children[1]);
486  newop->accept_child(1);
487  }
488 
489  void ga_tree::add_child(pga_tree_node pnode, GA_NODE_TYPE node_type) {
490  pga_tree_node newnode=new ga_tree_node();
491  newnode->pos = pnode->pos; newnode->expr = pnode->expr;
492  newnode->node_type = node_type; pnode->adopt_child(newnode);
493  }
494 
495  void ga_tree::insert_node(pga_tree_node pnode, GA_NODE_TYPE node_type) {
496  pga_tree_node newnode = new ga_tree_node();
497  newnode->node_type = node_type;
498  newnode->parent = pnode->parent;
499  newnode->pos = pnode->pos; newnode->expr = pnode->expr;
500  if (pnode->parent)
501  pnode->parent->replace_child(pnode, newnode);
502  else
503  root = newnode;
504  newnode->adopt_child(pnode);
505  }
506 
507  bool sub_tree_are_equal(const pga_tree_node pnode1,
508  const pga_tree_node pnode2,
509  const ga_workspace &workspace, int version) {
510 
511  size_type ntype1 = pnode1->node_type;
512  if (ntype1 == GA_NODE_ZERO) ntype1 = GA_NODE_CONSTANT;
513  size_type ntype2 = pnode2->node_type;
514  if (ntype2 == GA_NODE_ZERO) ntype2 = GA_NODE_CONSTANT;
515  if (ntype1 != ntype2) return false;
516  if (pnode1->children.size() != pnode2->children.size()) return false;
517 
518  switch(ntype1) {
519  case GA_NODE_OP:
520  if (pnode1->op_type != pnode2->op_type) return false;
521  if (pnode1->symmetric_op != pnode2->symmetric_op) return false;
522  break;
523  case GA_NODE_OPERATOR:
524  if (pnode1->der1 != pnode2->der1 || pnode1->der2 != pnode2->der2)
525  return false;
526  if (pnode1->name.compare(pnode2->name)) return false;
527  break;
528  case GA_NODE_PREDEF_FUNC: case GA_NODE_SPEC_FUNC:
529  if (pnode1->name.compare(pnode2->name)) return false;
530  break;
531  case GA_NODE_CONSTANT: case GA_NODE_ZERO:
532  if (pnode1->tensor().size() != pnode2->tensor().size()) return false;
533 
534  switch(version) {
535  case 0: case 1:
536  if (pnode1->test_function_type != pnode2->test_function_type)
537  return false;
538  if ((pnode1->test_function_type & 1) &&
539  pnode1->name_test1.compare(pnode2->name_test1) != 0)
540  return false;
541  if ((pnode1->test_function_type & 2) &&
542  pnode1->name_test2.compare(pnode2->name_test2) != 0)
543  return false;
544  break;
545  case 2:
546  if ((pnode1->test_function_type == 1 &&
547  pnode2->test_function_type == 1) ||
548  (pnode1->test_function_type == 2 &&
549  pnode2->test_function_type == 2))
550  return false;
551  if ((pnode1->test_function_type & 1) &&
552  pnode1->name_test1.compare(pnode2->name_test2) != 0)
553  return false;
554  if ((pnode1->test_function_type & 2) &&
555  pnode1->name_test2.compare(pnode2->name_test1) != 0)
556  return false;
557  break;
558  }
559  if (pnode1->tensor().size() != 1 &&
560  pnode1->t.sizes().size() != pnode2->t.sizes().size()) return false;
561  for (size_type i = 0; i < pnode1->t.sizes().size(); ++i)
562  if (pnode1->t.sizes()[i] != pnode2->t.sizes()[i]) return false;
563  for (size_type i = 0; i < pnode1->tensor().size(); ++i)
564  if (gmm::abs(pnode1->tensor()[i] - pnode2->tensor()[i]) > 1E-25)
565  return false;
566  break;
567  case GA_NODE_C_MATRIX:
568  if (pnode1->children.size() != pnode2->children.size() ||
569  pnode1->nb_test_functions() != pnode2->nb_test_functions() ||
570  pnode1->t.sizes().size() != pnode2->t.sizes().size())
571  return false;
572  for (size_type i=pnode1->nb_test_functions();
573  i < pnode1->t.sizes().size(); ++i)
574  if (pnode1->t.sizes()[i] != pnode2->t.sizes()[i]) return false;
575  if (pnode1->nbc1 != pnode2->nbc1) return false;
576  break;
577  case GA_NODE_INTERPOLATE_FILTER:
578  if (pnode1->interpolate_name.compare(pnode2->interpolate_name) ||
579  pnode1->nbc1 != pnode2->nbc1)
580  return false;
581  break;
582  case GA_NODE_INTERPOLATE_X:
583  case GA_NODE_SECONDARY_DOMAIN_X:
584  case GA_NODE_INTERPOLATE_ELT_K: case GA_NODE_INTERPOLATE_ELT_B:
585  case GA_NODE_INTERPOLATE_NORMAL:
586  case GA_NODE_SECONDARY_DOMAIN_NORMAL:
587  if (pnode1->interpolate_name.compare(pnode2->interpolate_name))
588  return false;
589  break;
590  case GA_NODE_INTERPOLATE_DERIVATIVE:
591  if (pnode1->interpolate_name_der.compare(pnode2->interpolate_name_der))
592  return false;
593  if (pnode1->interpolate_name.compare(pnode2->interpolate_name) ||
594  pnode1->elementary_name.compare(pnode2->elementary_name))
595  return false;
596  {
597  const mesh_fem *mf1 = workspace.associated_mf(pnode1->name);
598  const mesh_fem *mf2 = workspace.associated_mf(pnode2->name);
599  switch (version) {
600  case 0:
601  if (pnode1->name.compare(pnode2->name) ||
602  pnode1->test_function_type != pnode2->test_function_type)
603  return false;
604  break;
605  case 1:
606  if (mf1 != mf2 ||
607  workspace.qdim(pnode1->name) != workspace.qdim(pnode2->name) ||
608  pnode1->test_function_type != pnode2->test_function_type)
609  return false;
610  break;
611  case 2:
612  if (mf1 != mf2 ||
613  workspace.qdim(pnode1->name) != workspace.qdim(pnode2->name) ||
614  pnode1->test_function_type == pnode2->test_function_type)
615  return false;
616  break;
617  }
618  }
619  break;
620  case GA_NODE_INTERPOLATE_VAL_TEST: case GA_NODE_INTERPOLATE_GRAD_TEST:
621  case GA_NODE_INTERPOLATE_HESS_TEST: case GA_NODE_INTERPOLATE_DIVERG_TEST:
622  case GA_NODE_ELEMENTARY_VAL_TEST: case GA_NODE_ELEMENTARY_GRAD_TEST:
623  case GA_NODE_ELEMENTARY_HESS_TEST: case GA_NODE_ELEMENTARY_DIVERG_TEST:
624  case GA_NODE_SECONDARY_DOMAIN_VAL_TEST:
625  case GA_NODE_SECONDARY_DOMAIN_GRAD_TEST:
626  case GA_NODE_SECONDARY_DOMAIN_HESS_TEST:
627  case GA_NODE_SECONDARY_DOMAIN_DIVERG_TEST:
628  case GA_NODE_XFEM_PLUS_VAL_TEST: case GA_NODE_XFEM_PLUS_GRAD_TEST:
629  case GA_NODE_XFEM_PLUS_HESS_TEST: case GA_NODE_XFEM_PLUS_DIVERG_TEST:
630  case GA_NODE_XFEM_MINUS_VAL_TEST: case GA_NODE_XFEM_MINUS_GRAD_TEST:
631  case GA_NODE_XFEM_MINUS_HESS_TEST: case GA_NODE_XFEM_MINUS_DIVERG_TEST:
632  if (pnode1->interpolate_name.compare(pnode2->interpolate_name) ||
633  pnode1->elementary_name.compare(pnode2->elementary_name))
634  return false;
635  {
636  const mesh_fem *mf1 = workspace.associated_mf(pnode1->name);
637  const mesh_fem *mf2 = workspace.associated_mf(pnode2->name);
638  switch (version) {
639  case 0:
640  if (pnode1->name.compare(pnode2->name) ||
641  pnode1->test_function_type != pnode2->test_function_type)
642  return false;
643  break;
644  case 1:
645  if (mf1 != mf2 ||
646  workspace.qdim(pnode1->name) != workspace.qdim(pnode2->name) ||
647  pnode1->test_function_type != pnode2->test_function_type)
648  return false;
649  break;
650  case 2:
651  if (mf1 != mf2 ||
652  workspace.qdim(pnode1->name) != workspace.qdim(pnode2->name) ||
653  pnode1->test_function_type == pnode2->test_function_type)
654  return false;
655  break;
656  }
657  }
658  break;
659  case GA_NODE_VAL_TEST: case GA_NODE_GRAD_TEST:
660  case GA_NODE_HESS_TEST: case GA_NODE_DIVERG_TEST:
661  {
662  const mesh_fem *mf1 = workspace.associated_mf(pnode1->name);
663  const mesh_fem *mf2 = workspace.associated_mf(pnode2->name);
664  switch (version) {
665  case 0:
666  if (pnode1->name.compare(pnode2->name) ||
667  pnode1->test_function_type != pnode2->test_function_type)
668  return false;
669  break;
670  case 1:
671  if (mf1 != mf2 ||
672  workspace.qdim(pnode1->name) != workspace.qdim(pnode2->name) ||
673  pnode1->test_function_type != pnode2->test_function_type)
674  return false;
675  break;
676  case 2:
677  if (mf1 != mf2 ||
678  workspace.qdim(pnode1->name) != workspace.qdim(pnode2->name) ||
679  pnode1->test_function_type == pnode2->test_function_type)
680  return false;
681  break;
682  }
683  }
684  break;
685  case GA_NODE_VAL: case GA_NODE_GRAD:
686  case GA_NODE_HESS: case GA_NODE_DIVERG:
687  if (pnode1->name.compare(pnode2->name)) return false;
688  break;
689  case GA_NODE_INTERPOLATE_VAL: case GA_NODE_INTERPOLATE_GRAD:
690  case GA_NODE_INTERPOLATE_HESS: case GA_NODE_INTERPOLATE_DIVERG:
691  case GA_NODE_ELEMENTARY_VAL: case GA_NODE_ELEMENTARY_GRAD:
692  case GA_NODE_ELEMENTARY_HESS: case GA_NODE_ELEMENTARY_DIVERG:
693  case GA_NODE_SECONDARY_DOMAIN_VAL: case GA_NODE_SECONDARY_DOMAIN_GRAD:
694  case GA_NODE_SECONDARY_DOMAIN_HESS: case GA_NODE_SECONDARY_DOMAIN_DIVERG:
695  case GA_NODE_XFEM_PLUS_VAL: case GA_NODE_XFEM_PLUS_GRAD:
696  case GA_NODE_XFEM_PLUS_HESS: case GA_NODE_XFEM_PLUS_DIVERG:
697  case GA_NODE_XFEM_MINUS_VAL: case GA_NODE_XFEM_MINUS_GRAD:
698  case GA_NODE_XFEM_MINUS_HESS: case GA_NODE_XFEM_MINUS_DIVERG:
699  if (pnode1->interpolate_name.compare(pnode2->interpolate_name) ||
700  pnode1->elementary_name.compare(pnode2->elementary_name) ||
701  pnode1->name.compare(pnode2->name))
702  return false;
703  break;
704  case GA_NODE_X:
705  if (pnode1->nbc1 != pnode2->nbc1) return false;
706  break;
707 
708  default:break;
709  }
710 
711  if (version && ntype1 == GA_NODE_OP && pnode1->symmetric_op) {
712  if (sub_tree_are_equal(pnode1->children[0], pnode2->children[0],
713  workspace, version) &&
714  sub_tree_are_equal(pnode1->children[1], pnode2->children[1],
715  workspace, version))
716  return true;
717  if (sub_tree_are_equal(pnode1->children[1], pnode2->children[0],
718  workspace, version) &&
719  sub_tree_are_equal(pnode1->children[0], pnode2->children[1],
720  workspace, version) )
721  return true;
722  return false;
723  } else {
724  for (size_type i = 0; i < pnode1->children.size(); ++i)
725  if (!(sub_tree_are_equal(pnode1->children[i], pnode2->children[i],
726  workspace, version)))
727  return false;
728  }
729  return true;
730  }
731 
732  static void verify_tree(const pga_tree_node pnode,
733  const pga_tree_node parent) {
734  GMM_ASSERT1(pnode->parent == parent,
735  "Invalid tree node " << pnode->node_type);
736  for (pga_tree_node &child : pnode->children)
737  verify_tree(child, pnode);
738  }
739 
740 
741  static void ga_print_constant_tensor(const pga_tree_node pnode,
742  std::ostream &str) {
743  size_type nt = pnode->nb_test_functions(); // for printing zero tensors
744  switch (pnode->tensor_order()) {
745  case 0:
746  str << (nt ? scalar_type(0) : pnode->tensor()[0]);
747  break;
748 
749  case 1:
750  str << "[";
751  for (size_type i = 0; i < pnode->tensor_proper_size(0); ++i) {
752  if (i != 0) str << ",";
753  str << (nt ? scalar_type(0) : pnode->tensor()[i]);
754  }
755  str << "]";
756  break;
757 
758  case 2: case 3: case 4:
759  {
760  size_type ii(0);
761  size_type n0 = pnode->tensor_proper_size(0);
762  size_type n1 = pnode->tensor_proper_size(1);
763  size_type n2 = ((pnode->tensor_order() > 2) ?
764  pnode->tensor_proper_size(2) : 1);
765  size_type n3 = ((pnode->tensor_order() > 3) ?
766  pnode->tensor_proper_size(3) : 1);
767  if (n3 > 1) str << "[";
768  for (size_type l = 0; l < n3; ++l) {
769  if (l != 0) str << ",";
770  if (n2 > 1) str << "[";
771  for (size_type k = 0; k < n2; ++k) {
772  if (k != 0) str << ",";
773  if (n1 > 1) str << "[";
774  for (size_type j = 0; j < n1; ++j) {
775  if (j != 0) str << ",";
776  if (n0 > 1) str << "[";
777  for (size_type i = 0; i < n0; ++i) {
778  if (i != 0) str << ",";
779  str << (nt ? scalar_type(0) : pnode->tensor()[ii++]);
780  }
781  if (n0 > 1) str << "]";
782  }
783  if (n1 > 1) str << "]";
784  }
785  if (n2 > 1) str << "]";
786  }
787  if (n3 > 1) str << "]";
788  }
789  break;
790 
791  case 5: case 6: case 7: case 8:
792  str << "Reshape([";
793  for (size_type i = 0; i < pnode->tensor_proper_size(); ++i) {
794  if (i != 0) str << ";";
795  str << (nt ? scalar_type(0) : pnode->tensor()[i]);
796  }
797  str << "]";
798  for (size_type i = 0; i < pnode->tensor_order(); ++i) {
799  if (i != 0) str << ", ";
800  str << pnode->tensor_proper_size(i);
801  }
802  str << ")";
803  break;
804 
805  default: GMM_ASSERT1(false, "Invalid tensor dimension");
806  }
807  GMM_ASSERT1(pnode->children.size() == 0, "Invalid tree");
808  }
809 
810  void ga_print_node(const pga_tree_node pnode, std::ostream &str) {
811  if (!pnode) return;
812  long prec = str.precision(16);
813 
814  bool is_interpolate(false), is_elementary(false), is_secondary(false);
815  bool is_xfem_plus(false), is_xfem_minus(false);
816  switch(pnode->node_type) {
817  case GA_NODE_INTERPOLATE:
818  case GA_NODE_INTERPOLATE_X:
819  case GA_NODE_INTERPOLATE_ELT_K: case GA_NODE_INTERPOLATE_ELT_B:
820  case GA_NODE_INTERPOLATE_NORMAL:
821  case GA_NODE_INTERPOLATE_VAL:
822  case GA_NODE_INTERPOLATE_GRAD:
823  case GA_NODE_INTERPOLATE_HESS:
824  case GA_NODE_INTERPOLATE_DIVERG:
825  case GA_NODE_INTERPOLATE_VAL_TEST:
826  case GA_NODE_INTERPOLATE_GRAD_TEST:
827  case GA_NODE_INTERPOLATE_HESS_TEST:
828  case GA_NODE_INTERPOLATE_DIVERG_TEST:
829  str << "Interpolate(";
830  is_interpolate = true;
831  break;
832  case GA_NODE_ELEMENTARY:
833  case GA_NODE_ELEMENTARY_VAL:
834  case GA_NODE_ELEMENTARY_GRAD:
835  case GA_NODE_ELEMENTARY_HESS:
836  case GA_NODE_ELEMENTARY_DIVERG:
837  case GA_NODE_ELEMENTARY_VAL_TEST:
838  case GA_NODE_ELEMENTARY_GRAD_TEST:
839  case GA_NODE_ELEMENTARY_HESS_TEST:
840  case GA_NODE_ELEMENTARY_DIVERG_TEST:
841  is_elementary = true;
842  str << "Elementary_transformation(";
843  break;
844  case GA_NODE_SECONDARY_DOMAIN:
845  case GA_NODE_SECONDARY_DOMAIN_X:
846  case GA_NODE_SECONDARY_DOMAIN_NORMAL:
847  case GA_NODE_SECONDARY_DOMAIN_VAL:
848  case GA_NODE_SECONDARY_DOMAIN_GRAD:
849  case GA_NODE_SECONDARY_DOMAIN_HESS:
850  case GA_NODE_SECONDARY_DOMAIN_DIVERG:
851  case GA_NODE_SECONDARY_DOMAIN_VAL_TEST:
852  case GA_NODE_SECONDARY_DOMAIN_GRAD_TEST:
853  case GA_NODE_SECONDARY_DOMAIN_HESS_TEST:
854  case GA_NODE_SECONDARY_DOMAIN_DIVERG_TEST:
855  str << "Secondary_domain(";
856  is_secondary = true;
857  break;
858 
859  case GA_NODE_XFEM_PLUS:
860  case GA_NODE_XFEM_PLUS_VAL:
861  case GA_NODE_XFEM_PLUS_GRAD:
862  case GA_NODE_XFEM_PLUS_HESS:
863  case GA_NODE_XFEM_PLUS_DIVERG:
864  case GA_NODE_XFEM_PLUS_VAL_TEST:
865  case GA_NODE_XFEM_PLUS_GRAD_TEST:
866  case GA_NODE_XFEM_PLUS_HESS_TEST:
867  case GA_NODE_XFEM_PLUS_DIVERG_TEST:
868  is_xfem_plus = true;
869  str << "Xfem_plus(";
870  break;
871  case GA_NODE_XFEM_MINUS:
872  case GA_NODE_XFEM_MINUS_VAL:
873  case GA_NODE_XFEM_MINUS_GRAD:
874  case GA_NODE_XFEM_MINUS_HESS:
875  case GA_NODE_XFEM_MINUS_DIVERG:
876  case GA_NODE_XFEM_MINUS_VAL_TEST:
877  case GA_NODE_XFEM_MINUS_GRAD_TEST:
878  case GA_NODE_XFEM_MINUS_HESS_TEST:
879  case GA_NODE_XFEM_MINUS_DIVERG_TEST:
880  is_xfem_minus = true;
881  str << "Xfem_minus(";
882  break;
883  default:
884  break;
885  }
886 
887  switch(pnode->node_type) {
888  case GA_NODE_GRAD:
889  case GA_NODE_INTERPOLATE_GRAD:
890  case GA_NODE_ELEMENTARY_GRAD:
891  case GA_NODE_SECONDARY_DOMAIN_GRAD:
892  case GA_NODE_XFEM_PLUS_GRAD:
893  case GA_NODE_XFEM_MINUS_GRAD:
894  case GA_NODE_GRAD_TEST:
895  case GA_NODE_INTERPOLATE_GRAD_TEST:
896  case GA_NODE_ELEMENTARY_GRAD_TEST:
897  case GA_NODE_SECONDARY_DOMAIN_GRAD_TEST:
898  case GA_NODE_XFEM_PLUS_GRAD_TEST:
899  case GA_NODE_XFEM_MINUS_GRAD_TEST:
900  str << "Grad_";
901  break;
902  case GA_NODE_HESS:
903  case GA_NODE_INTERPOLATE_HESS:
904  case GA_NODE_ELEMENTARY_HESS:
905  case GA_NODE_SECONDARY_DOMAIN_HESS:
906  case GA_NODE_XFEM_PLUS_HESS:
907  case GA_NODE_XFEM_MINUS_HESS:
908  case GA_NODE_HESS_TEST:
909  case GA_NODE_INTERPOLATE_HESS_TEST:
910  case GA_NODE_ELEMENTARY_HESS_TEST:
911  case GA_NODE_SECONDARY_DOMAIN_HESS_TEST:
912  case GA_NODE_XFEM_PLUS_HESS_TEST:
913  case GA_NODE_XFEM_MINUS_HESS_TEST:
914  str << "Hess_";
915  break;
916  case GA_NODE_DIVERG:
917  case GA_NODE_INTERPOLATE_DIVERG:
918  case GA_NODE_SECONDARY_DOMAIN_DIVERG:
919  case GA_NODE_ELEMENTARY_DIVERG:
920  case GA_NODE_XFEM_PLUS_DIVERG:
921  case GA_NODE_XFEM_MINUS_DIVERG:
922  case GA_NODE_DIVERG_TEST:
923  case GA_NODE_INTERPOLATE_DIVERG_TEST:
924  case GA_NODE_ELEMENTARY_DIVERG_TEST:
925  case GA_NODE_SECONDARY_DOMAIN_DIVERG_TEST:
926  case GA_NODE_XFEM_PLUS_DIVERG_TEST:
927  case GA_NODE_XFEM_MINUS_DIVERG_TEST:
928  str << "Div_";
929  break;
930  default:
931  break;
932  }
933 
934  switch(pnode->node_type) {
935  case GA_NODE_OP:
936  {
937  bool par = false;
938  if (pnode->parent) {
939  if (pnode->parent->node_type == GA_NODE_OP &&
940  (ga_operator_priorities[pnode->op_type] >= 2 ||
941  ga_operator_priorities[pnode->op_type]
942  < ga_operator_priorities[pnode->parent->op_type]))
943  par = true;
944  if (pnode->parent->node_type == GA_NODE_PARAMS) par = true;
945  }
946 
947 
948  if (par) str << "(";
949  if (pnode->op_type == GA_UNARY_MINUS) {
950  GMM_ASSERT1(pnode->children.size() == 1, "Invalid tree");
951  str << "-"; ga_print_node(pnode->children[0], str);
952  } else if (pnode->op_type == GA_QUOTE) {
953  GMM_ASSERT1(pnode->children.size() == 1, "Invalid tree");
954  ga_print_node(pnode->children[0], str); str << "'";
955  } else if (pnode->op_type == GA_SYM) {
956  GMM_ASSERT1(pnode->children.size() == 1, "Invalid tree");
957  str << "Sym("; ga_print_node(pnode->children[0], str); str << ")";
958  } else if (pnode->op_type == GA_SKEW) {
959  GMM_ASSERT1(pnode->children.size() == 1, "Invalid tree");
960  str << "Skew("; ga_print_node(pnode->children[0], str); str << ")";
961  } else if (pnode->op_type == GA_TRACE) {
962  GMM_ASSERT1(pnode->children.size() == 1, "Invalid tree");
963  str << "Trace("; ga_print_node(pnode->children[0], str); str << ")";
964  } else if (pnode->op_type == GA_DEVIATOR) {
965  GMM_ASSERT1(pnode->children.size() == 1, "Invalid tree with "
966  << pnode->children.size() << " children instead of 1");
967  str << "Deviator("; ga_print_node(pnode->children[0], str); str<<")";
968  } else if (pnode->op_type == GA_PRINT) {
969  GMM_ASSERT1(pnode->children.size() == 1, "Invalid tree");
970  str << "Print("; ga_print_node(pnode->children[0], str); str << ")";
971  } else {
972  if (!par && pnode->op_type == GA_MULT &&
973  (pnode->children.size() == 1 ||
974  pnode->test_function_type == size_type(-1) ||
975  (pnode->children[0]->tensor_order() == 4 &&
976  pnode->children[1]->tensor_order() == 2)))
977  { par = true; str << "("; }
978  ga_print_node(pnode->children[0], str);
979  switch (pnode->op_type) {
980  case GA_PLUS: str << "+"; break;
981  case GA_MINUS: str << "-"; break;
982  case GA_MULT: str << "*"; break;
983  case GA_DIV: str << "/"; break;
984  case GA_COLON: str << ":"; break;
985  case GA_DOT: str << "."; break;
986  case GA_DOTMULT: str << ".*"; break;
987  case GA_DOTDIV: str << "./"; break;
988  case GA_TMULT: str << "@"; break;
989  default: GMM_ASSERT1(false, "Invalid or not taken into account "
990  "operation");
991  }
992  if (pnode->children.size() >= 2)
993  ga_print_node(pnode->children[1], str);
994  else
995  str << "(unknown second argument)";
996  }
997  if (par) str << ")";
998  }
999  break;
1000 
1001  case GA_NODE_X:
1002  if (pnode->nbc1) str << "X(" << pnode->nbc1 << ")"; else str << "X";
1003  break;
1004  case GA_NODE_ELT_SIZE: str << "element_size"; break;
1005  case GA_NODE_ELT_K: str << "element_K"; break;
1006  case GA_NODE_ELT_B: str << "element_B"; break;
1007  case GA_NODE_ELT_CURVATURE: str << "element_curvature"; break;
1008  case GA_NODE_NORMAL: str << "Normal"; break;
1009  case GA_NODE_INTERPOLATE_FILTER:
1010  str << "Interpolate_filter(" << pnode->interpolate_name << ",";
1011  ga_print_node(pnode->children[0], str);
1012  if (pnode->children.size() == 2)
1013  { str << ","; ga_print_node(pnode->children[1], str); }
1014  else if (pnode->nbc1 != size_type(-1)) str << "," << pnode->nbc1;
1015  str << ")";
1016  break;
1017  case GA_NODE_INTERPOLATE_X: case GA_NODE_SECONDARY_DOMAIN_X:
1018  str << "X";
1019  break;
1020  case GA_NODE_INTERPOLATE_NORMAL: case GA_NODE_SECONDARY_DOMAIN_NORMAL:
1021  str << "Normal";
1022  break;
1023  case GA_NODE_INTERPOLATE_ELT_K:
1024  str << "element_K";
1025  break;
1026  case GA_NODE_INTERPOLATE_ELT_B:
1027  str << "element_B";
1028  break;
1029  case GA_NODE_INTERPOLATE_DERIVATIVE:
1030  str << (pnode->test_function_type == 1 ? "Test_" : "Test2_")
1031  << "Interpolate_derivative(" << pnode->interpolate_name_der << ","
1032  << pnode->name;
1033  if (pnode->interpolate_name.size())
1034  str << "," << pnode->interpolate_name;
1035  str << ")";
1036  break;
1037  case GA_NODE_INTERPOLATE:
1038  case GA_NODE_ELEMENTARY:
1039  case GA_NODE_SECONDARY_DOMAIN:
1040  case GA_NODE_XFEM_PLUS:
1041  case GA_NODE_XFEM_MINUS:
1042  case GA_NODE_VAL:
1043  case GA_NODE_INTERPOLATE_VAL:
1044  case GA_NODE_ELEMENTARY_VAL:
1045  case GA_NODE_SECONDARY_DOMAIN_VAL:
1046  case GA_NODE_XFEM_PLUS_VAL:
1047  case GA_NODE_XFEM_MINUS_VAL:
1048  case GA_NODE_GRAD:
1049  case GA_NODE_INTERPOLATE_GRAD:
1050  case GA_NODE_SECONDARY_DOMAIN_GRAD:
1051  case GA_NODE_ELEMENTARY_GRAD:
1052  case GA_NODE_XFEM_PLUS_GRAD:
1053  case GA_NODE_XFEM_MINUS_GRAD:
1054  case GA_NODE_HESS:
1055  case GA_NODE_INTERPOLATE_HESS:
1056  case GA_NODE_SECONDARY_DOMAIN_HESS:
1057  case GA_NODE_ELEMENTARY_HESS:
1058  case GA_NODE_XFEM_PLUS_HESS:
1059  case GA_NODE_XFEM_MINUS_HESS:
1060  case GA_NODE_DIVERG:
1061  case GA_NODE_INTERPOLATE_DIVERG:
1062  case GA_NODE_ELEMENTARY_DIVERG:
1063  case GA_NODE_SECONDARY_DOMAIN_DIVERG:
1064  case GA_NODE_XFEM_PLUS_DIVERG:
1065  case GA_NODE_XFEM_MINUS_DIVERG:
1066  str << pnode->name;
1067  break;
1068  case GA_NODE_VAL_TEST:
1069  case GA_NODE_INTERPOLATE_VAL_TEST:
1070  case GA_NODE_ELEMENTARY_VAL_TEST:
1071  case GA_NODE_SECONDARY_DOMAIN_VAL_TEST:
1072  case GA_NODE_XFEM_PLUS_VAL_TEST:
1073  case GA_NODE_XFEM_MINUS_VAL_TEST:
1074  case GA_NODE_GRAD_TEST:
1075  case GA_NODE_INTERPOLATE_GRAD_TEST:
1076  case GA_NODE_ELEMENTARY_GRAD_TEST:
1077  case GA_NODE_SECONDARY_DOMAIN_GRAD_TEST:
1078  case GA_NODE_XFEM_PLUS_GRAD_TEST:
1079  case GA_NODE_XFEM_MINUS_GRAD_TEST:
1080  case GA_NODE_HESS_TEST:
1081  case GA_NODE_INTERPOLATE_HESS_TEST:
1082  case GA_NODE_ELEMENTARY_HESS_TEST:
1083  case GA_NODE_SECONDARY_DOMAIN_HESS_TEST:
1084  case GA_NODE_XFEM_PLUS_HESS_TEST:
1085  case GA_NODE_XFEM_MINUS_HESS_TEST:
1086  case GA_NODE_DIVERG_TEST:
1087  case GA_NODE_INTERPOLATE_DIVERG_TEST:
1088  case GA_NODE_ELEMENTARY_DIVERG_TEST:
1089  case GA_NODE_SECONDARY_DOMAIN_DIVERG_TEST:
1090  case GA_NODE_XFEM_PLUS_DIVERG_TEST:
1091  case GA_NODE_XFEM_MINUS_DIVERG_TEST:
1092  str << (pnode->test_function_type == 1 ? "Test_" : "Test2_")
1093  << pnode->name;
1094  break;
1095  case GA_NODE_SPEC_FUNC: str << pnode->name; break;
1096  case GA_NODE_OPERATOR:
1097  case GA_NODE_PREDEF_FUNC:
1098  if (pnode->der1) {
1099  str << "Derivative_" << pnode->der1 << "_";
1100  if (pnode->der2) str << pnode->der2 << "_";
1101  }
1102  str << pnode->name; break;
1103  case GA_NODE_ZERO:
1104  GMM_ASSERT1(pnode->test_function_type != size_type(-1),
1105  "Internal error");
1106  if (pnode->test_function_type) str << "(";
1107  ga_print_constant_tensor(pnode, str);
1108  if (pnode->name_test1.size()) {
1109  GMM_ASSERT1(pnode->qdim1 > 0, "Internal error");
1110  if (pnode->qdim1 == 1)
1111  str << "*Test_" << pnode->name_test1;
1112  else {
1113  str << "*(Reshape(Test_" << pnode->name_test1 << ","
1114  << pnode->qdim1<< ")(1))";
1115  }
1116  }
1117  if (pnode->name_test2.size()) {
1118  GMM_ASSERT1(pnode->qdim2 > 0, "Internal error");
1119  if (pnode->qdim2 == 1)
1120  str << "*Test2_" << pnode->name_test2;
1121  else {
1122  str << "*(Reshape(Test2_" << pnode->name_test2 << ","
1123  << pnode->qdim2<< ")(1))";
1124  }
1125  }
1126  if (pnode->test_function_type) str << ")";
1127  break;
1128 
1129  case GA_NODE_CONSTANT:
1130  ga_print_constant_tensor(pnode, str);
1131  break;
1132 
1133  case GA_NODE_ALLINDICES:
1134  str << ":";
1135  GMM_ASSERT1(pnode->children.size() == 0, "Invalid tree");
1136  break;
1137 
1138  case GA_NODE_PARAMS:
1139  GMM_ASSERT1(pnode->children.size(), "Invalid tree");
1140  ga_print_node(pnode->children[0], str);
1141  str << "(";
1142  for (size_type i = 1; i < pnode->children.size(); ++i) {
1143  if (i > 1) str << ", ";
1144  ga_print_node(pnode->children[i], str);
1145  }
1146  str << ")";
1147  break;
1148 
1149  case GA_NODE_NAME:
1150  str << pnode->name;
1151  GMM_ASSERT1(pnode->children.size() == 0, "Invalid tree");
1152  break;
1153 
1154  case GA_NODE_MACRO_PARAM:
1155  if (pnode->nbc2 == 1) str << "Grad_";
1156  if (pnode->nbc2 == 2) str << "Hess_";
1157  if (pnode->nbc2 == 3) str << "Div_";
1158  if (pnode->nbc3 == 1) str << "Test_";
1159  if (pnode->nbc3 == 2) str << "Test2_";
1160  str << "P" << pnode->nbc1;
1161  GMM_ASSERT1(pnode->children.size() == 0, "Invalid tree");
1162  break;
1163 
1164  case GA_NODE_RESHAPE:
1165  str << "Reshape";
1166  GMM_ASSERT1(pnode->children.size() == 0, "Invalid tree");
1167  break;
1168 
1169  case GA_NODE_CROSS_PRODUCT:
1170  str << "Cross_product";
1171  GMM_ASSERT1(pnode->children.size() == 0, "Invalid tree");
1172  break;
1173 
1174  case GA_NODE_SWAP_IND:
1175  str << "Swap_indices";
1176  GMM_ASSERT1(pnode->children.size() == 0, "Invalid tree");
1177  break;
1178 
1179  case GA_NODE_IND_MOVE_LAST:
1180  str << "Index_move_last";
1181  GMM_ASSERT1(pnode->children.size() == 0, "Invalid tree");
1182  break;
1183 
1184  case GA_NODE_CONTRACT:
1185  str << "Contract";
1186  GMM_ASSERT1(pnode->children.size() == 0, "Invalid tree");
1187  break;
1188 
1189  case GA_NODE_C_MATRIX:
1190  GMM_ASSERT1(pnode->children.size(), "Invalid tree");
1191  GMM_ASSERT1(pnode->nbc1 == pnode->tensor_order(), "Invalid C_MATRIX");
1192  switch (pnode->tensor_order()) {
1193  case 0:
1194  ga_print_node(pnode->children[0], str);
1195  break;
1196 
1197  case 1:
1198  str << "[";
1199  for (size_type i = 0; i < pnode->tensor_proper_size(0); ++i) {
1200  if (i != 0) str << ",";
1201  ga_print_node(pnode->children[i], str);
1202  }
1203  str << "]";
1204  break;
1205 
1206  case 2: case 3: case 4:
1207  {
1208  size_type ii(0);
1209  size_type n0 = pnode->tensor_proper_size(0);
1210  size_type n1 = pnode->tensor_proper_size(1);
1211  size_type n2 = ((pnode->tensor_order() > 2) ?
1212  pnode->tensor_proper_size(2) : 1);
1213  size_type n3 = ((pnode->tensor_order() > 3) ?
1214  pnode->tensor_proper_size(3) : 1);
1215  if (n3 > 1) str << "[";
1216  for (size_type l = 0; l < n3; ++l) {
1217  if (l != 0) str << ",";
1218  if (n2 > 1) str << "[";
1219  for (size_type k = 0; k < n2; ++k) {
1220  if (k != 0) str << ",";
1221  if (n1 > 1) str << "[";
1222  for (size_type j = 0; j < n1; ++j) {
1223  if (j != 0) str << ",";
1224  if (n0 > 1) str << "[";
1225  for (size_type i = 0; i < n0; ++i) {
1226  if (i != 0) str << ",";
1227  ga_print_node(pnode->children[ii++], str);
1228  }
1229  if (n0 > 1) str << "]";
1230  }
1231  if (n1 > 1) str << "]";
1232  }
1233  if (n2 > 1) str << "]";
1234  }
1235  if (n3 > 1) str << "]";
1236  }
1237  break;
1238 
1239  case 5: case 6: case 7: case 8:
1240  str << "Reshape([";
1241  for (size_type i = 0; i < pnode->tensor_proper_size(); ++i) {
1242  if (i != 0) str << ";";
1243  ga_print_node(pnode->children[i], str);
1244  }
1245  str << "]";
1246  for (size_type i = 0; i < pnode->tensor_order(); ++i) {
1247  if (i != 0) str << ", ";
1248  str << pnode->tensor_proper_size(i);
1249  }
1250  str << ")";
1251  break;
1252 
1253  default: GMM_ASSERT1(false, "Invalid tensor dimension");
1254  }
1255  break;
1256 
1257  default:
1258  str << "Invalid or not taken into account node type "
1259  << pnode->node_type;
1260  break;
1261  }
1262 
1263  if (is_interpolate)
1264  str << "," << pnode->interpolate_name << ")";
1265  else if (is_elementary) {
1266  str << "," << pnode->elementary_name;
1267  if (pnode->name.compare(pnode->elementary_target) != 0)
1268  str << "," << pnode->elementary_target;
1269  str << ")";
1270  } else if (is_secondary)
1271  str << ")";
1272  else if (is_xfem_plus || is_xfem_minus)
1273  str << ")";
1274 
1275  str.precision(prec);
1276  }
1277 
1278  std::string ga_tree_to_string(const ga_tree &tree) {
1279  std::stringstream str;
1280  str.precision(16);
1281  if (tree.root) verify_tree(tree.root, 0);
1282  if (tree.root) ga_print_node(tree.root, str); else str << "0";
1283  return str.str();
1284  }
1285 
1286  size_type ga_parse_prefix_operator(std::string &name) {
1287  if (name.size() >= 5 && name.compare(0, 5, "Grad_") == 0)
1288  { name = name.substr(5); return 1; }
1289  else if (name.size() >= 5 && name.compare(0, 5, "Hess_") == 0)
1290  { name = name.substr(5); return 2; }
1291  else if (name.size() >= 4 && name.compare(0, 4, "Div_") == 0)
1292  { name = name.substr(4); return 3; }
1293  return 0;
1294  }
1295 
1296  size_type ga_parse_prefix_test(std::string &name) {
1297  if (name.size() >= 5 && name.compare(0, 5, "Test_") == 0)
1298  { name = name.substr(5); return 1; }
1299  else if (name.size() >= 6 && name.compare(0, 6, "Test2_") == 0)
1300  { name = name.substr(6); return 2; }
1301  return 0;
1302  }
1303 
1304  // 0 : ok
1305  // 1 : function or operator name or "X"
1306  // 2 : reserved prefix Grad, Hess, Div, Derivative_ Test and Test2
1307  // 3 : reserved prefix Dot and Previous
1308  int ga_check_name_validity(const std::string &name) {
1309  if (name.compare(0, 11, "Derivative_") == 0)
1310  return 2;
1311 
1312  const ga_predef_operator_tab &PREDEF_OPERATORS
1314  const ga_spec_function_tab &SPEC_FUNCTIONS
1316  const ga_spec_op_tab &SPEC_OP
1318  const ga_predef_function_tab &PREDEF_FUNCTIONS
1320 
1321  if (SPEC_OP.find(name) != SPEC_OP.end())
1322  return 1;
1323 
1324  if (PREDEF_FUNCTIONS.find(name) != PREDEF_FUNCTIONS.end())
1325  return 1;
1326 
1327  if (SPEC_FUNCTIONS.find(name) != SPEC_FUNCTIONS.end())
1328  return 1;
1329 
1330  if (PREDEF_OPERATORS.tab.find(name) != PREDEF_OPERATORS.tab.end())
1331  return 1;
1332 
1333  if (name.size() >= 5 && name.compare(0, 5, "Grad_") == 0)
1334  return 2;
1335 
1336  if (name.size() >= 5 && name.compare(0, 5, "Hess_") == 0)
1337  return 2;
1338 
1339  if (name.size() >= 4 && name.compare(0, 4, "Div_") == 0)
1340  return 2;
1341 
1342  if (name.size() >= 6 && name.compare(0, 6, "Test2_") == 0)
1343  return 2;
1344 
1345  if (name.size() >= 5 && name.compare(0, 5, "Test_") == 0)
1346  return 2;
1347 
1348 // if (name.size() >= 4 && name.compare(0, 4, "Dot_") == 0)
1349 // return 3;
1350 // if (name.size() >= 5 && name.compare(0, 5, "Dot2_") == 0)
1351 // return 3;
1352 
1353 // if (name.size() >= 9 && name.compare(0, 9, "Previous_") == 0)
1354 // return 3;
1355 // if (name.size() >= 10 && name.compare(0, 10, "Previous2_") == 0)
1356 // return 3;
1357 // if (name.size() >= 12 && name.compare(0, 12, "Previous1_2_") == 0)
1358 // return 3;
1359 
1360  return 0;
1361  }
1362 
1363  //=========================================================================
1364  // Structure dealing with macros.
1365  //=========================================================================
1366 
1367  ga_macro::ga_macro() : ptree(new ga_tree), nbp(0) {}
1368  ga_macro::~ga_macro() { delete ptree; }
1369  ga_macro::ga_macro(const std::string &name, const ga_tree &t, size_type nbp_)
1370  : ptree(new ga_tree(t)), macro_name_(name), nbp(nbp_) {}
1371  ga_macro::ga_macro(const ga_macro &gam)
1372  : ptree(new ga_tree(gam.tree())), macro_name_(gam.name()),
1373  nbp(gam.nb_params()) {}
1374  ga_macro &ga_macro::operator =(const ga_macro &gam) {
1375  delete ptree; ptree = new ga_tree(gam.tree());
1376  macro_name_ = gam.name();
1377  nbp = gam.nb_params();
1378  return *this;
1379  }
1380 
1381  static void ga_replace_macro_params
1382  (ga_tree &tree, pga_tree_node pnode,
1383  const std::vector<pga_tree_node> &children) {
1384  if (!pnode) return;
1385  for (pga_tree_node &child : pnode->children)
1386  ga_replace_macro_params(tree, child, children);
1387 
1388  if (pnode->node_type == GA_NODE_MACRO_PARAM) {
1389  size_type po = pnode->nbc2;
1390  size_type pt = pnode->nbc3;
1391  GMM_ASSERT1(pnode->nbc1+1 < children.size(), "Internal error");
1392  pga_tree_node pchild = children[pnode->nbc1+1];
1393 
1394  if (po || pt || pnode->op_type != GA_NAME) {
1395  if (!(pchild->children.empty()) || pchild->node_type != GA_NODE_NAME)
1396  ga_throw_error(pchild->expr, pchild->pos, "Error in macro "
1397  "expansion. Only variable name are allowed for macro "
1398  "parameter preceded by Grad_ Hess_ Test_ or Test2_ "
1399  "prefixes.");
1400  switch(pnode->op_type) {
1401  case GA_NAME : pnode->node_type = GA_NODE_NAME; break;
1402  case GA_INTERPOLATE : pnode->node_type = GA_NODE_INTERPOLATE; break;
1403  case GA_INTERPOLATE_DERIVATIVE :
1404  pnode->node_type = GA_NODE_INTERPOLATE_DERIVATIVE; break;
1405  case GA_ELEMENTARY : pnode->node_type = GA_NODE_ELEMENTARY; break;
1406  case GA_SECONDARY_DOMAIN :
1407  pnode->node_type = GA_NODE_SECONDARY_DOMAIN; break;
1408  case GA_XFEM_PLUS : pnode->node_type = GA_NODE_XFEM_PLUS; break;
1409  case GA_XFEM_MINUS: pnode->node_type = GA_NODE_XFEM_MINUS; break;
1410  default:break;
1411  }
1412  pnode->name = pchild->name;
1413  if (pt == 1) pnode->name = "Test_" + pnode->name;
1414  if (pt == 2) pnode->name = "Test2_" + pnode->name;
1415  if (po == 1) pnode->name = "Grad_" + pnode->name;
1416  if (po == 2) pnode->name = "Hess_" + pnode->name;
1417  if (po == 3) pnode->name = "Div_" + pnode->name;
1418  } else {
1419  pga_tree_node pnode_old = pnode;
1420  pnode = nullptr;
1421  tree.copy_node(pchild, pnode);
1422  pnode->parent = pnode_old->parent;
1423  if (pnode_old->parent)
1424  pnode_old->parent->replace_child(pnode_old, pnode);
1425  else
1426  tree.root = pnode;
1427  GMM_ASSERT1(pnode_old->children.empty(), "Internal error");
1428  delete pnode_old;
1429  }
1430  }
1431  }
1432 
1433  static void ga_expand_macro(ga_tree &tree, pga_tree_node pnode,
1434  const ga_macro_dictionary &macro_dict) {
1435  if (!pnode) return;
1436 
1437  if (pnode->node_type == GA_NODE_PARAMS) {
1438 
1439  for (size_type i = 1; i < pnode->children.size(); ++i)
1440  ga_expand_macro(tree, pnode->children[i], macro_dict);
1441 
1442  if (pnode->children[0]->node_type != GA_NODE_NAME) {
1443  ga_expand_macro(tree, pnode->children[0], macro_dict);
1444  } else {
1445 
1446  if (macro_dict.macro_exists(pnode->children[0]->name)) {
1447 
1448  const ga_macro &gam = macro_dict.get_macro(pnode->children[0]->name);
1449 
1450  if (gam.nb_params()==0) { // Macro without parameters
1451  pga_tree_node pnode_old = pnode->children[0];
1452  pnode->children[0] = nullptr;
1453  tree.copy_node(gam.tree().root, pnode->children[0]);
1454  pnode->children[0]->parent = pnode_old->parent;
1455  GMM_ASSERT1(pnode_old->children.empty(), "Internal error");
1456  delete pnode_old;
1457  } else { // Macro with parameters
1458  if (gam.nb_params()+1 != pnode->children.size())
1459  ga_throw_error(pnode->expr, pnode->pos,
1460  "Bad number of parameters in the use of macro '"
1461  << gam.name() << "'. Expected " << gam.nb_params()
1462  << " found " << pnode->children.size()-1 << ".");
1463 
1464  pga_tree_node pnode_old = pnode;
1465  pnode = nullptr;
1466  tree.copy_node(gam.tree().root, pnode);
1467  pnode->parent = pnode_old->parent;
1468  if (pnode_old->parent)
1469  pnode_old->parent->replace_child(pnode_old, pnode);
1470  else
1471  tree.root = pnode;
1472  ga_replace_macro_params(tree, pnode, pnode_old->children);
1473  tree.clear_node_rec(pnode_old);
1474  }
1475  }
1476  }
1477 
1478  } else if (pnode->node_type == GA_NODE_NAME &&
1479  macro_dict.macro_exists(pnode->name)) {
1480  // Macro without parameters
1481  const ga_macro &gam = macro_dict.get_macro(pnode->name);
1482  if (gam.nb_params() != 0)
1483  ga_throw_error(pnode->expr, pnode->pos,
1484  "Bad number of parameters in the use of macro '"
1485  << gam.name() << "'. Expected " << gam.nb_params()
1486  << " none found.");
1487 
1488  pga_tree_node pnode_old = pnode;
1489  pnode = nullptr;
1490  tree.copy_node(gam.tree().root, pnode);
1491  pnode->parent = pnode_old->parent;
1492  if (pnode_old->parent)
1493  pnode_old->parent->replace_child(pnode_old, pnode);
1494  else
1495  tree.root = pnode;
1496  GMM_ASSERT1(pnode_old->children.empty(), "Internal error");
1497  delete pnode_old;
1498  } else {
1499  for (pga_tree_node &child : pnode->children)
1500  ga_expand_macro(tree, child, macro_dict);
1501  }
1502  }
1503 
1504  static void ga_mark_macro_params_rec(const pga_tree_node pnode,
1505  const std::vector<std::string> &params) {
1506  if (!pnode) return;
1507  for (pga_tree_node &child : pnode->children)
1508  ga_mark_macro_params_rec(child, params);
1509 
1510  if (pnode->node_type == GA_NODE_NAME ||
1511  pnode->node_type == GA_NODE_INTERPOLATE ||
1512  pnode->node_type == GA_NODE_ELEMENTARY ||
1513  pnode->node_type == GA_NODE_SECONDARY_DOMAIN ||
1514  pnode->node_type == GA_NODE_XFEM_PLUS ||
1515  pnode->node_type == GA_NODE_XFEM_MINUS) {
1516  std::string name = pnode->name;
1517  size_type po = ga_parse_prefix_operator(name);
1518  size_type pt = ga_parse_prefix_test(name);
1519 
1520  for (size_type i = 0; i < params.size(); ++i)
1521  if (name.compare(params[i]) == 0) {
1522  pnode->name = name;
1523  switch(pnode->node_type) {
1524  case GA_NODE_NAME : pnode->op_type = GA_NAME; break;
1525  case GA_NODE_INTERPOLATE : pnode->op_type = GA_INTERPOLATE; break;
1526  case GA_NODE_INTERPOLATE_DERIVATIVE :
1527  pnode->op_type = GA_INTERPOLATE_DERIVATIVE; break;
1528  case GA_NODE_ELEMENTARY : pnode->op_type = GA_ELEMENTARY; break;
1529  case GA_NODE_SECONDARY_DOMAIN :
1530  pnode->op_type = GA_SECONDARY_DOMAIN; break;
1531  case GA_NODE_XFEM_PLUS : pnode->op_type = GA_XFEM_PLUS; break;
1532  case GA_NODE_XFEM_MINUS: pnode->op_type = GA_XFEM_MINUS; break;
1533  default:break;
1534  }
1535  pnode->node_type = GA_NODE_MACRO_PARAM;
1536  pnode->nbc1 = i; pnode->nbc2 = po; pnode->nbc3 = pt;
1537  }
1538  }
1539  }
1540 
1541  static void ga_mark_macro_params(ga_macro &gam,
1542  const std::vector<std::string> &params,
1543  const ga_macro_dictionary &macro_dict) {
1544  if (gam.tree().root) {
1545  ga_mark_macro_params_rec(gam.tree().root, params);
1546  ga_expand_macro(gam.tree(), gam.tree().root, macro_dict);
1547  }
1548  }
1549 
1550  bool ga_macro_dictionary::macro_exists(const std::string &name) const {
1551  if (macros.find(name) != macros.end()) return true;
1552  if (parent && parent->macro_exists(name)) return true;
1553  return false;
1554  }
1555 
1556  const ga_macro &
1557  ga_macro_dictionary::get_macro(const std::string &name) const {
1558  auto it = macros.find(name);
1559  if (it != macros.end()) return it->second;
1560  if (parent) return parent->get_macro(name);
1561  GMM_ASSERT1(false, "Undefined macro");
1562  }
1563 
1564  void ga_macro_dictionary::add_macro(const ga_macro &gam)
1565  { macros[gam.name()] = gam; }
1566 
1567  void ga_macro_dictionary::add_macro(const std::string &name,
1568  const std::string &expr)
1569  { ga_tree tree; ga_read_string_reg("Def "+name+":="+expr, tree, *this); }
1570 
1571  void ga_macro_dictionary::del_macro(const std::string &name) {
1572  auto it = macros.find(name);
1573  GMM_ASSERT1(it != macros.end(), "Undefined macro (at this level)");
1574  macros.erase(it);
1575  }
1576 
1577 
1578  //=========================================================================
1579  // Syntax analysis for the generic assembly language
1580  //=========================================================================
1581 
1582  // Read a term with an (implicit) pushdown automaton.
1583  static GA_TOKEN_TYPE ga_read_term(pstring expr, size_type &pos,
1584  ga_tree &tree,
1585  ga_macro_dictionary &macro_dict) {
1586  size_type token_pos, token_length;
1587  GA_TOKEN_TYPE t_type;
1588  int state = 1; // 1 = reading term, 2 = reading after term
1589 
1590  for (;;) {
1591 
1592  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1593 
1594  switch (state) {
1595 
1596  case 1:
1597  switch (t_type) {
1598  case GA_SCALAR:
1599  {
1600  char *endptr; const char *nptr = &((*expr)[token_pos]);
1601  scalar_type s_read = ::strtod(nptr, &endptr);
1602  if (endptr == nptr)
1603  ga_throw_error(expr, token_pos, "Bad numeric format.");
1604  tree.add_scalar(s_read, token_pos, expr);
1605  }
1606  state = 2; break;
1607 
1608  case GA_COLON:
1609  tree.add_allindices(token_pos, expr);
1610  state = 2; break;
1611 
1612  case GA_NAME:
1613  tree.add_name(&((*expr)[token_pos]), token_length, token_pos, expr);
1614  state = 2; break;
1615 
1616  case GA_MINUS: // unary -
1617  tree.add_op(GA_UNARY_MINUS, token_pos, expr);
1618  state = 1; break;
1619 
1620  case GA_PLUS: // unary +
1621  state = 1; break;
1622 
1623  case GA_SYM:
1624  tree.add_op(GA_SYM, token_pos, expr);
1625  state = 1; break;
1626 
1627  case GA_SKEW:
1628  tree.add_op(GA_SKEW, token_pos, expr);
1629  state = 1; break;
1630 
1631  case GA_TRACE:
1632  tree.add_op(GA_TRACE, token_pos, expr);
1633  state = 1; break;
1634 
1635  case GA_DEVIATOR:
1636  tree.add_op(GA_DEVIATOR, token_pos, expr);
1637  state = 1; break;
1638 
1639  case GA_DEF:
1640  {
1641  ga_macro gam;
1642  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1643  if (t_type != GA_NAME)
1644  ga_throw_error(expr, pos,
1645  "Macro definition should begin with macro name");
1646  gam.name() = std::string(&((*expr)[token_pos]), token_length);
1647  if (ga_check_name_validity(gam.name()))
1648  ga_throw_error(expr, pos-1, "Invalid macro name.")
1649  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1650  std::vector<std::string> params;
1651  if (t_type == GA_LPAR) {
1652  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1653  while (t_type == GA_NAME) {
1654  params.push_back(std::string(&((*expr)[token_pos]),
1655  token_length));
1656  if (ga_check_name_validity(params.back()))
1657  ga_throw_error(expr, pos-1, "Invalid macro parameter name.");
1658  for (size_type i = 0; i+1 < params.size(); ++i)
1659  if (params.back().compare(params[i]) == 0)
1660  ga_throw_error(expr, pos-1,
1661  "Invalid repeated macro parameter name.");
1662  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1663  if (t_type == GA_COMMA)
1664  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1665  }
1666  if (t_type != GA_RPAR)
1667  ga_throw_error(expr, pos-1,
1668  "Missing right parenthesis in macro definition.");
1669  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1670  }
1671  if (t_type != GA_COLON_EQ)
1672  ga_throw_error(expr, pos-1, "Missing := for macro definition.");
1673 
1674  t_type = ga_read_term(expr, pos, gam.tree(), macro_dict);
1675  if (gam.tree().root)
1676  ga_expand_macro(gam.tree(), gam.tree().root, macro_dict);
1677  gam.nb_params() = params.size();
1678  if (params.size())
1679  ga_mark_macro_params(gam, params, macro_dict);
1680  macro_dict.add_macro(gam);
1681 
1682  // cout << "macro \"" << gam.name() << "\" registered with "
1683  // << gam.nb_params() << " params := "
1684  // << ga_tree_to_string(gam.tree()) << endl;
1685 
1686  if (t_type == GA_END) return t_type;
1687  else if (t_type != GA_SEMICOLON)
1688  ga_throw_error(expr, pos-1,
1689  "Syntax error at the end of macro definition.");
1690  state = 1;
1691  }
1692  break;
1693 
1694  case GA_INTERPOLATE:
1695  {
1696  tree.add_scalar(scalar_type(0), token_pos, expr);
1697  tree.current_node->node_type = GA_NODE_INTERPOLATE;
1698  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1699  if (t_type != GA_LPAR)
1700  ga_throw_error(expr, pos-1, "Missing interpolate arguments.");
1701  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1702  if (t_type != GA_NAME)
1703  ga_throw_error(expr, pos,
1704  "First argument of Interpolate should be a "
1705  "variable, test function, X or Normal.");
1706  tree.current_node->name = std::string(&((*expr)[token_pos]),
1707  token_length);
1708  if (tree.current_node->name.compare("Grad") == 0) {
1709  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1710  if (t_type != GA_LPAR)
1711  ga_throw_error(expr, pos-1, "Missing Grad argument.");
1712  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1713  if (t_type != GA_NAME)
1714  ga_throw_error(expr, pos,
1715  "Argument of Grad should be a variable name, here.");
1716  tree.current_node->name = std::string(&((*expr)[token_pos]),
1717  token_length);
1718  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1719  if (t_type != GA_RPAR)
1720  ga_throw_error(expr, pos-1,
1721  "The sole argument of Grad should be a variable name, here.");
1722  tree.current_node->name = "Grad_" + tree.current_node->name;
1723  }
1724 
1725  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1726  if (t_type != GA_COMMA)
1727  ga_throw_error(expr, pos, "Bad format for Interpolate "
1728  "arguments.");
1729  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1730  if (t_type != GA_NAME)
1731  ga_throw_error(expr, pos,
1732  "Second argument of Interpolate should be a "
1733  "transformation name.");
1734  tree.current_node->interpolate_name
1735  = std::string(&((*expr)[token_pos]), token_length);
1736  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1737  if (t_type != GA_RPAR)
1738  ga_throw_error(expr, pos-1, "Missing a parenthesis after "
1739  "interpolate arguments.");
1740  state = 2;
1741  }
1742  break;
1743 
1744  case GA_INTERPOLATE_DERIVATIVE:
1745  {
1746  tree.add_scalar(scalar_type(0), token_pos, expr);
1747  tree.current_node->node_type = GA_NODE_INTERPOLATE_DERIVATIVE;
1748  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1749  if (t_type != GA_LPAR)
1750  ga_throw_error(expr, pos-1,
1751  "Missing Interpolate_derivative arguments.");
1752  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1753  if (t_type != GA_NAME)
1754  ga_throw_error(expr, pos,
1755  "First argument of Interpolate should the "
1756  "interpolate transformation name ");
1757  tree.current_node->interpolate_name_der
1758  = std::string(&((*expr)[token_pos]), token_length);
1759  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1760  if (t_type != GA_COMMA)
1761  ga_throw_error(expr, pos, "Bad format for Interpolate_derivative "
1762  "arguments.");
1763  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1764  if (t_type != GA_NAME)
1765  ga_throw_error(expr, pos,
1766  "Second argument of Interpolate should be a "
1767  "variable name.");
1768  tree.current_node->name
1769  = std::string(&((*expr)[token_pos]), token_length);
1770 
1771  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1772  tree.current_node->interpolate_name = "";
1773  if (t_type == GA_COMMA) {
1774  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1775  if (t_type != GA_NAME)
1776  ga_throw_error(expr, pos,
1777  "Third argument of Interpolate should be a "
1778  "interpolate transformation name.");
1779  tree.current_node->interpolate_name
1780  = std::string(&((*expr)[token_pos]), token_length);
1781  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1782  }
1783  if (t_type != GA_RPAR)
1784  ga_throw_error(expr, pos-1, "Missing a parenthesis after "
1785  "Interpolate_derivative arguments.");
1786  state = 2;
1787  }
1788  break;
1789 
1790  case GA_ELEMENTARY:
1791  {
1792  tree.add_scalar(scalar_type(0), token_pos, expr);
1793  tree.current_node->node_type = GA_NODE_ELEMENTARY;
1794  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1795  if (t_type != GA_LPAR)
1796  ga_throw_error(expr, pos-1,
1797  "Missing Elementary_transformation arguments.");
1798  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1799  if (t_type != GA_NAME)
1800  ga_throw_error(expr, pos,
1801  "First argument of Elementary_transformation "
1802  "should be a variable or a test function.");
1803  tree.current_node->name = std::string(&((*expr)[token_pos]),
1804  token_length);
1805  tree.current_node->elementary_target = tree.current_node->name;
1806 
1807  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1808  if (t_type != GA_COMMA)
1809  ga_throw_error(expr, pos, "Bad format for "
1810  "Elementary_transformation arguments.");
1811  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1812  if (t_type != GA_NAME)
1813  ga_throw_error(expr, pos,
1814  "Second argument of Elementary_transformation "
1815  "should be a transformation name.");
1816  tree.current_node->elementary_name
1817  = std::string(&((*expr)[token_pos]), token_length);
1818  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1819 
1820  if (t_type == GA_COMMA) {
1821  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1822  if (t_type != GA_NAME)
1823  ga_throw_error(expr, pos,
1824  "Third argument of Elementary_transformation "
1825  "should be a variable or data name.");
1826 
1827  tree.current_node->elementary_target =
1828  std::string(&((*expr)[token_pos]), token_length);
1829  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1830  }
1831 
1832  if (t_type != GA_RPAR)
1833  ga_throw_error(expr, pos-1, "Missing a parenthesis after "
1834  "Elementary_transformation arguments.");
1835  state = 2;
1836  }
1837  break;
1838 
1839  case GA_SECONDARY_DOMAIN:
1840  {
1841  tree.add_scalar(scalar_type(0), token_pos, expr);
1842  tree.current_node->node_type = GA_NODE_SECONDARY_DOMAIN;
1843  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1844  if (t_type != GA_LPAR)
1845  ga_throw_error(expr, pos-1,"Missing Secondary_domain arguments.");
1846  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1847  if (t_type != GA_NAME)
1848  ga_throw_error(expr, pos,
1849  "First argument of Secondary_domain should be a "
1850  "variable, test function, X or Normal.");
1851  tree.current_node->name = std::string(&((*expr)[token_pos]),
1852  token_length);
1853  tree.current_node->interpolate_name = tree.secondary_domain;
1854  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1855  if (t_type != GA_RPAR)
1856  ga_throw_error(expr, pos-1, "Missing a parenthesis after "
1857  "Secondary_domain arguments.");
1858  state = 2;
1859  }
1860  break;
1861 
1862  case GA_XFEM_PLUS:
1863  {
1864  tree.add_scalar(scalar_type(0), token_pos, expr);
1865  tree.current_node->node_type = GA_NODE_XFEM_PLUS;
1866  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1867  if (t_type != GA_LPAR)
1868  ga_throw_error(expr, pos-1,
1869  "Missing Xfem_plus arguments.");
1870  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1871  if (t_type != GA_NAME)
1872  ga_throw_error(expr, pos,
1873  "The argument of Xfem_plus should be a "
1874  "variable or a test function.");
1875  tree.current_node->name = std::string(&((*expr)[token_pos]),
1876  token_length);
1877  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1878  if (t_type != GA_RPAR)
1879  ga_throw_error(expr, pos-1, "Missing a parenthesis after "
1880  "Xfem_plus argument.");
1881  state = 2;
1882  }
1883  break;
1884 
1885  case GA_XFEM_MINUS:
1886  {
1887  tree.add_scalar(scalar_type(0), token_pos, expr);
1888  tree.current_node->node_type = GA_NODE_XFEM_MINUS;
1889  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1890  if (t_type != GA_LPAR)
1891  ga_throw_error(expr, pos-1,
1892  "Missing Xfem_minus arguments.");
1893  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1894  if (t_type != GA_NAME)
1895  ga_throw_error(expr, pos,
1896  "The argument of Xfem_minus should be a "
1897  "variable or a test function.");
1898  tree.current_node->name = std::string(&((*expr)[token_pos]),
1899  token_length);
1900  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1901  if (t_type != GA_RPAR)
1902  ga_throw_error(expr, pos-1, "Missing a parenthesis after "
1903  "Xfem_minus argument.");
1904  state = 2;
1905  }
1906  break;
1907 
1908  case GA_INTERPOLATE_FILTER:
1909  {
1910  tree.add_scalar(scalar_type(0), token_pos, expr);
1911  tree.current_node->node_type = GA_NODE_INTERPOLATE_FILTER;
1912  tree.current_node->nbc1 = size_type(-1);
1913  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1914  if (t_type != GA_LPAR)
1915  ga_throw_error(expr, pos-1, "Missing interpolate arguments.");
1916  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1917  if (t_type != GA_NAME)
1918  ga_throw_error(expr, pos, "First argument of Interpolate_filter "
1919  "should be a transformation name.");
1920  tree.current_node->interpolate_name
1921  = std::string(&((*expr)[token_pos]), token_length);
1922  t_type = ga_get_token(*expr, pos, token_pos, token_length);
1923  if (t_type != GA_COMMA)
1924  ga_throw_error(expr, pos,
1925  "Bad format for Interpolate_filter arguments.");
1926  ga_tree sub_tree;
1927  t_type = ga_read_term(expr, pos, sub_tree, macro_dict);
1928  if (t_type != GA_RPAR && t_type != GA_COMMA)
1929  ga_throw_error(expr, pos-1,
1930  "Bad format for Interpolate_filter arguments.");
1931  tree.add_sub_tree(sub_tree);
1932  if (t_type == GA_COMMA) {
1933  ga_tree sub_tree2;
1934  t_type = ga_read_term(expr, pos, sub_tree2, macro_dict);
1935  tree.add_sub_tree(sub_tree2);
1936  }
1937  if (t_type != GA_RPAR)
1938  ga_throw_error(expr, pos-1, "Unbalanced parenthesis.");
1939  state = 2;
1940  }
1941  break;
1942 
1943  case GA_PRINT:
1944  tree.add_op(GA_PRINT, token_pos, expr);
1945  state = 1; break;
1946 
1947  case GA_LPAR: // Parenthesed expression
1948  {
1949  ga_tree sub_tree;
1950  GA_TOKEN_TYPE r_type;
1951  r_type = ga_read_term(expr, pos, sub_tree, macro_dict);
1952  if (r_type != GA_RPAR)
1953  ga_throw_error(expr, pos-1, "Unbalanced parenthesis.");
1954  tree.add_sub_tree(sub_tree);
1955  state = 2;
1956  }
1957  break;
1958 
1959  case GA_LBRACKET: // Explicit vector/matrix or tensor
1960  {
1961  ga_tree sub_tree;
1962  GA_TOKEN_TYPE r_type;
1963  size_type nbc1(0), nbc2(0), nbc3(0), n1(0), n2(0), n3(0);
1964  size_type tensor_order(1);
1965  bool foundcomma(false), foundsemi(false);
1966 
1967  r_type = ga_read_term(expr, pos, sub_tree, macro_dict);
1968  size_type nb_comp = 0;
1969  tree.add_matrix(token_pos, expr);
1970 
1971  if (sub_tree.root->node_type == GA_NODE_C_MATRIX) { // nested format
1972  bgeot::multi_index mii;
1973  do {
1974  if (nb_comp) {
1975  sub_tree.clear();
1976  r_type = ga_read_term(expr, pos, sub_tree, macro_dict);
1977  }
1978  // in the nested format only "," and "]" are expected
1979  if (sub_tree.root->node_type != GA_NODE_C_MATRIX ||
1980  (r_type != GA_COMMA && r_type != GA_RBRACKET))
1981  ga_throw_error(expr, pos-1, "Bad explicit "
1982  "vector/matrix/tensor format.");
1983 
1984  // convert a row vector [a,b] to a column vector [a;b]
1985  if (sub_tree.root->marked &&
1986  sub_tree.root->tensor().sizes()[0] == 1 &&
1987  sub_tree.root->tensor().size() != 1) {
1988  bgeot::multi_index mi = sub_tree.root->tensor().sizes();
1989  for (size_type i = mi.size()-1; i > 0; i--)
1990  mi[i-1] = mi[i];
1991  mi.pop_back();
1992  sub_tree.root->tensor().adjust_sizes(mi);
1993  }
1994  if (!nb_comp)
1995  mii = sub_tree.root->tensor().sizes();
1996  else {
1997  const bgeot::multi_index &mi=sub_tree.root->tensor().sizes();
1998  bool cmp = true;
1999  if (mii.size() == mi.size()) {
2000  for (size_type i = 0; i < mi.size(); ++i)
2001  if (mi[i] != mii[i]) cmp = false;
2002  } else cmp = false;
2003  if (!cmp)
2004  ga_throw_error(expr, pos-1, "Bad explicit "
2005  "vector/matrix/tensor format.");
2006  }
2007  for (pga_tree_node &child : sub_tree.root->children) {
2008  child->parent = tree.current_node;
2009  tree.current_node->children.push_back(child);
2010  }
2011  sub_tree.root->children.resize(0);
2012  nb_comp++;
2013  } while (r_type != GA_RBRACKET);
2014  tree.current_node->marked = false;
2015  mii.push_back(nb_comp);
2016  tree.current_node->tensor().adjust_sizes(mii);
2017  } else { // non nested format
2018  do {
2019  if (nb_comp) {
2020  sub_tree.clear();
2021  r_type = ga_read_term(expr, pos, sub_tree, macro_dict);
2022  }
2023  nb_comp++;
2024 
2025  tree.add_sub_tree(sub_tree);
2026 
2027  ++n1; ++n2; ++n3;
2028  if (tensor_order < 2) ++nbc1;
2029  if (tensor_order < 3) ++nbc2;
2030  if (tensor_order < 4) ++nbc3;
2031 
2032  if (r_type == GA_COMMA) {
2033  if (!foundcomma && tensor_order > 1)
2034  ga_throw_error(expr, pos-1, "Bad explicit "
2035  "vector/matrix/tensor format.");
2036  foundcomma = true;
2037  } else if (r_type == GA_SEMICOLON) {
2038  if (n1 != nbc1)
2039  ga_throw_error(expr, pos-1, "Bad explicit "
2040  "vector/matrix/tensor format.");
2041  n1 = 0;
2042  tensor_order = std::max(tensor_order, size_type(2));
2043  } else if (r_type == GA_DCOMMA) {
2044  if (n1 != nbc1 || n2 != nbc2)
2045  ga_throw_error(expr, pos-1, "Bad explicit "
2046  "vector/matrix/tensor format.");
2047  foundsemi = true;
2048  n2 = n1 = 0;
2049  tensor_order = std::max(tensor_order, size_type(3));
2050  } else if (r_type == GA_DSEMICOLON) {
2051  if (n1 != nbc1 || n2 != nbc2 || n3 != nbc3 ||
2052  tensor_order < 3)
2053  ga_throw_error(expr, pos-1, "Bad explicit "
2054  "vector/matrix/tensor format.");
2055  n3 = n2 = n1 = 0;
2056  tensor_order = std::max(tensor_order, size_type(4));
2057  } else if (r_type == GA_RBRACKET) {
2058  if (n1 != nbc1 || n2 != nbc2 || n3 != nbc3 ||
2059  tensor_order == 3)
2060  ga_throw_error(expr, pos-1, "Bad explicit "
2061  "vector/matrix/tensor format.");
2062  tree.current_node->nbc1 = nbc1;
2063  if (tensor_order == 4) {
2064  tree.current_node->nbc2 = nbc2/nbc1;
2065  tree.current_node->nbc3 = nbc3/nbc2;
2066  } else {
2067  tree.current_node->nbc2 = tree.current_node->nbc3 = 1;
2068  }
2069  } else {
2070  ga_throw_error(expr, pos-1, "The explicit "
2071  "vector/matrix/tensor components should be "
2072  "separated by ',', ';', ',,' and ';;' and "
2073  "be ended by ']'.");
2074  }
2075 
2076  } while (r_type != GA_RBRACKET);
2077  bgeot::multi_index mi;
2078  nbc1 = tree.current_node->nbc1;
2079  nbc2 = tree.current_node->nbc2;
2080  nbc3 = tree.current_node->nbc3;
2081 
2082  size_type nbl = tree.current_node->children.size()
2083  / (nbc2 * nbc1 * nbc3);
2084  switch(tensor_order) {
2085  case 1:
2086  // mi.push_back(1);
2087  mi.push_back(nbc1);
2088  break;
2089  case 2:
2090  mi.push_back(nbl);
2091  if (nbc1 > 1)
2092  mi.push_back(nbc1);
2093  break;
2094  case 3:
2095  mi.push_back(nbl);
2096  mi.push_back(nbc2);
2097  mi.push_back(nbc1);
2098  break;
2099  case 4:
2100  mi.push_back(nbl);
2101  mi.push_back(nbc3);
2102  mi.push_back(nbc2);
2103  mi.push_back(nbc1);
2104  break;
2105  default: GMM_ASSERT1(false, "Internal error");
2106  }
2107  tree.current_node->tensor().adjust_sizes(mi);
2108  std::vector<pga_tree_node> children = tree.current_node->children;
2109  auto it = tree.current_node->children.begin();
2110  for (size_type i = 0; i < nbc1; ++i)
2111  for (size_type j = 0; j < nbc2; ++j)
2112  for (size_type k = 0; k < nbc3; ++k)
2113  for (size_type l = 0; l < nbl; ++l, ++it)
2114  *it = children[i+nbc1*(j+nbc2*(k+nbc3*l))];
2115  tree.current_node->marked = true;
2116  }
2117  }
2118  tree.current_node->nbc1 = tree.current_node->tensor().sizes().size();
2119  state = 2;
2120  break;
2121 
2122  default:
2123  ga_throw_error(expr, token_pos, "Unexpected token.");
2124  }
2125  break;
2126 
2127  case 2:
2128  switch (t_type) {
2129  case GA_PLUS: case GA_MINUS: case GA_MULT: case GA_DIV:
2130  case GA_COLON: case GA_DOT: case GA_DOTMULT: case GA_DOTDIV:
2131  case GA_TMULT:
2132  tree.add_op(t_type, token_pos, expr);
2133  state = 1;
2134  break;
2135  case GA_QUOTE:
2136  tree.add_op(t_type, token_pos, expr);
2137  state = 2;
2138  break;
2139  case GA_END: case GA_RPAR: case GA_COMMA: case GA_DCOMMA:
2140  case GA_RBRACKET: case GA_SEMICOLON: case GA_DSEMICOLON:
2141  return t_type;
2142  case GA_LPAR: // Parameter list
2143  {
2144  ga_tree sub_tree;
2145  GA_TOKEN_TYPE r_type;
2146  tree.add_params(token_pos, expr);
2147  do {
2148  r_type = ga_read_term(expr, pos, sub_tree, macro_dict);
2149  if (r_type != GA_RPAR && r_type != GA_COMMA)
2150  ga_throw_error(expr, pos-((r_type != GA_END)?1:0),
2151  "Parameters should be separated "
2152  "by ',' and parameter list ended by ')'.");
2153  tree.add_sub_tree(sub_tree);
2154  } while (r_type != GA_RPAR);
2155  state = 2;
2156  }
2157  break;
2158 
2159  default:
2160  ga_throw_error(expr, token_pos, "Unexpected token.");
2161  }
2162  break;
2163  }
2164  }
2165 
2166  return GA_INVALID;
2167  }
2168 
2169  // Syntax analysis of a string. Conversion to a tree. register the macros.
2170  void ga_read_string_reg(const std::string &expr, ga_tree &tree,
2171  ga_macro_dictionary &macro_dict) {
2172  size_type pos = 0, token_pos, token_length;
2173  tree.clear();
2174  GA_TOKEN_TYPE t = ga_get_token(expr, pos, token_pos, token_length);
2175  if (t == GA_END) return;
2176  pos = 0;
2177  pstring nexpr(new std::string(expr));
2178 
2179  t = ga_read_term(nexpr, pos, tree, macro_dict);
2180  if (tree.root) ga_expand_macro(tree, tree.root, macro_dict);
2181 
2182  switch (t) {
2183  case GA_RPAR:
2184  ga_throw_error(nexpr, pos-1, "Unbalanced parenthesis.");
2185  break;
2186  case GA_RBRACKET:
2187  ga_throw_error(nexpr, pos-1, "Unbalanced bracket.");
2188  break;
2189  case GA_END:
2190  break;
2191  default:
2192  ga_throw_error(nexpr, pos-1, "Unexpected token.");
2193  break;
2194  }
2195  }
2196 
2197  // Syntax analysis of a string. Conversion to a tree.
2198  // Do not register the macros (but expand them).
2199  void ga_read_string(const std::string &expr, ga_tree &tree,
2200  const ga_macro_dictionary &macro_dict) {
2201  ga_macro_dictionary macro_dict_loc(true, macro_dict);
2202  ga_read_string_reg(expr, tree, macro_dict_loc);
2203  }
2204 
2205  // Small tool to make basic substitutions into an assembly string
2206  // Should be replaced by macros now.
2207  std::string ga_substitute(const std::string &expr,
2208  const std::map<std::string, std::string> &dict) {
2209  if (dict.size()) {
2210  size_type pos = 0, token_pos, token_length;
2211  std::stringstream exprs;
2212 
2213  while (true) {
2214  GA_TOKEN_TYPE t_type = ga_get_token(expr, pos, token_pos, token_length);
2215  if (t_type == GA_END) return exprs.str();
2216  std::string name(&(expr[token_pos]), token_length);
2217  if (t_type == GA_NAME && dict.find(name) != dict.end())
2218  exprs << dict.at(name); else exprs << name;
2219  }
2220  }
2221  return expr;
2222  }
2223 
2224 } /* end of namespace */
static T & instance()
Instance from the current thread.
Compilation and execution operations.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48
GEneric Tool for Finite Element Methods.