22 #include "getfem/getfem_generic_assembly_functions_and_operators.h"
23 #include "getfem/getfem_generic_assembly_compile_and_exec.h"
32 static GA_TOKEN_TYPE ga_char_type[256];
33 static int ga_operator_priorities[GA_NB_TOKEN_TYPE];
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;
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;
71 static bool ga_initialized = init_ga_char_type();
74 static GA_TOKEN_TYPE ga_get_token(
const std::string &expr,
78 bool fdot =
false, fE =
false;
79 GMM_ASSERT1(ga_initialized,
"Internal error");
82 while (expr[pos] ==
' ' && pos < expr.size()) ++pos;
87 if (pos >= expr.size())
return GA_END;
90 GA_TOKEN_TYPE type = ga_char_type[unsigned(expr[pos])];
91 ++pos; ++token_length;
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;
101 while (pos < expr.size()) {
102 GA_TOKEN_TYPE ctype = ga_char_type[unsigned(expr[pos])];
105 if (fdot)
return type;
106 fdot =
true; ++pos; ++token_length;
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; }
115 if (pos >= expr.size()
116 || ga_char_type[
unsigned(expr[pos])] != GA_SCALAR)
120 ++pos; ++token_length;
break;
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;
132 if (expr.compare(token_pos, token_length,
"Sym") == 0)
134 if (expr.compare(token_pos, token_length,
"Def") == 0)
136 if (expr.compare(token_pos, token_length,
"Skew") == 0)
138 if (expr.compare(token_pos, token_length,
"Trace") == 0)
140 if (expr.compare(token_pos, token_length,
"Deviator") == 0)
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)
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)
162 if (pos < expr.size() &&
163 ga_char_type[
unsigned(expr[pos])] == GA_COMMA) {
164 ++pos;
return GA_DCOMMA;
168 if (pos < expr.size() &&
169 ga_char_type[
unsigned(expr[pos])] == GA_SEMICOLON) {
170 ++pos;
return GA_DSEMICOLON;
174 if (pos < expr.size() &&
175 ga_char_type[
unsigned(expr[pos])] == GA_COLON_EQ) {
176 ++pos;
return GA_COLON_EQ;
181 default:
return type;
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),
202 if (first > 0) cerr <<
"...";
203 cerr << expr->substr(first, last-first);
204 if (last <
int(expr->size())) cerr <<
"...";
206 if (first > 0) cerr <<
" ";
207 if (
int(pos) > first)
208 cerr << std::setfill (
'-') << std::setw(
int(pos)-first) <<
'-'
209 << std::setfill (
' ');
219 void ga_tree_node::mult_test(
const pga_tree_node n0,
const pga_tree_node n1) {
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.");
228 test_function_type = test0 + test1;
231 bgeot::multi_index mi(st);
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;
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;
244 if (n0->name_test1.size()) {
245 name_test1 = n0->name_test1; qdim1 = n0->qdim1;
246 interpolate_name_test1 = n0->interpolate_name_test1;
248 name_test1 = n1->name_test1; qdim1 = n1->qdim1;
249 interpolate_name_test1 = n1->interpolate_name_test1;
252 if (n0->name_test2.size()) {
253 name_test2 = n0->name_test2; qdim2 = n0->qdim2;
254 interpolate_name_test2 = n0->interpolate_name_test2;
256 name_test2 = n1->name_test2; qdim2 = n1->qdim2;
257 interpolate_name_test2 = n1->interpolate_name_test2;
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;
266 current_node->adopt_child(
new ga_tree_node(val, pos, expr));
267 current_node = current_node->children.back();
270 GMM_ASSERT1(root ==
nullptr,
"Invalid tree operation");
271 current_node = root =
new ga_tree_node(val, pos, expr);
272 root->parent =
nullptr;
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;
280 current_node->adopt_child(
new ga_tree_node(GA_NODE_ALLINDICES, pos,expr));
281 current_node = current_node->children.back();
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;
292 while (current_node && current_node->node_type != GA_NODE_OP)
293 current_node = current_node->parent;
295 current_node->adopt_child(
new ga_tree_node(name, length, pos, expr));
296 current_node = current_node->children.back();
299 GMM_ASSERT1(root ==
nullptr,
"Invalid tree operation");
300 current_node = root =
new ga_tree_node(name, length, pos, expr);
301 root->parent =
nullptr;
305 void ga_tree::add_sub_tree(ga_tree &sub_tree) {
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);
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;
317 current_node->adopt_child(sub_tree.root);
318 current_node = sub_tree.root;
321 GMM_ASSERT1(root ==
nullptr,
"Invalid tree operation");
322 current_node = root = sub_tree.root;
323 root->parent =
nullptr;
326 sub_tree.root = sub_tree.current_node =
nullptr;
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);
341 new_node->adopt_child(current_node);
342 current_node = new_node;
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;
349 current_node->adopt_child(
new ga_tree_node(GA_NODE_C_MATRIX, pos, expr));
350 current_node = current_node->children.back();
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;
357 current_node->nbc1 = current_node->nbc2 = current_node->nbc3 = 0;
360 void ga_tree::add_op(GA_TOKEN_TYPE op_type,
size_type pos,
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);
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);
375 new_node->parent = current_node->parent;
376 if (current_node->parent)
377 current_node->parent->replace_child(current_node, new_node);
380 new_node->adopt_child(current_node);
383 if (root) new_node->adopt_child(root);
385 root->parent =
nullptr;
387 current_node = new_node;
390 void ga_tree::clear_node_rec(pga_tree_node pnode) {
392 for (pga_tree_node &child : pnode->children)
393 clear_node_rec(child);
395 current_node =
nullptr;
399 void ga_tree::clear_node(pga_tree_node pnode) {
401 pga_tree_node parent = pnode->parent;
404 for (pga_tree_node &sibling : parent->children)
405 if (sibling != pnode)
406 parent->children[j++] = sibling;
407 parent->children.resize(j);
411 clear_node_rec(pnode);
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);
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;
425 pnode->parent->replace_child(pnode, child);
428 current_node =
nullptr;
429 for (pga_tree_node &sibling : pnode->children)
430 if (sibling != child) clear_node_rec(sibling);
434 size_type ga_tree_count_nodes(pga_tree_node pnode) {
438 for (pga_tree_node child : pnode->children)
439 count += ga_tree_count_nodes(child);
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();
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],
458 pnode_new->accept_child(j);
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);
469 preallocated_nodes[i] =
new ga_tree_node();
470 ga_tree_copy_node_rec(pnode, pnode_new, preallocated_nodes);
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;
481 pnode->parent->replace_child(pnode, newop);
484 pnode->parent = newop;
485 copy_node(pnode, newop->children[1]);
486 newop->accept_child(1);
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);
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;
501 pnode->parent->replace_child(pnode, newnode);
504 newnode->adopt_child(pnode);
507 bool sub_tree_are_equal(
const pga_tree_node pnode1,
508 const pga_tree_node pnode2,
509 const ga_workspace &workspace,
int version) {
512 if (ntype1 == GA_NODE_ZERO) ntype1 = GA_NODE_CONSTANT;
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;
520 if (pnode1->op_type != pnode2->op_type)
return false;
521 if (pnode1->symmetric_op != pnode2->symmetric_op)
return false;
523 case GA_NODE_OPERATOR:
524 if (pnode1->der1 != pnode2->der1 || pnode1->der2 != pnode2->der2)
526 if (pnode1->name.compare(pnode2->name))
return false;
528 case GA_NODE_PREDEF_FUNC:
case GA_NODE_SPEC_FUNC:
529 if (pnode1->name.compare(pnode2->name))
return false;
531 case GA_NODE_CONSTANT:
case GA_NODE_ZERO:
532 if (pnode1->tensor().size() != pnode2->tensor().size())
return false;
536 if (pnode1->test_function_type != pnode2->test_function_type)
538 if ((pnode1->test_function_type & 1) &&
539 pnode1->name_test1.compare(pnode2->name_test1) != 0)
541 if ((pnode1->test_function_type & 2) &&
542 pnode1->name_test2.compare(pnode2->name_test2) != 0)
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))
551 if ((pnode1->test_function_type & 1) &&
552 pnode1->name_test1.compare(pnode2->name_test2) != 0)
554 if ((pnode1->test_function_type & 2) &&
555 pnode1->name_test2.compare(pnode2->name_test1) != 0)
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)
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())
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;
577 case GA_NODE_INTERPOLATE_FILTER:
578 if (pnode1->interpolate_name.compare(pnode2->interpolate_name) ||
579 pnode1->nbc1 != pnode2->nbc1)
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))
590 case GA_NODE_INTERPOLATE_DERIVATIVE:
591 if (pnode1->interpolate_name_der.compare(pnode2->interpolate_name_der))
593 if (pnode1->interpolate_name.compare(pnode2->interpolate_name) ||
594 pnode1->elementary_name.compare(pnode2->elementary_name))
597 const mesh_fem *mf1 = workspace.associated_mf(pnode1->name);
598 const mesh_fem *mf2 = workspace.associated_mf(pnode2->name);
601 if (pnode1->name.compare(pnode2->name) ||
602 pnode1->test_function_type != pnode2->test_function_type)
607 workspace.qdim(pnode1->name) != workspace.qdim(pnode2->name) ||
608 pnode1->test_function_type != pnode2->test_function_type)
613 workspace.qdim(pnode1->name) != workspace.qdim(pnode2->name) ||
614 pnode1->test_function_type == pnode2->test_function_type)
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))
636 const mesh_fem *mf1 = workspace.associated_mf(pnode1->name);
637 const mesh_fem *mf2 = workspace.associated_mf(pnode2->name);
640 if (pnode1->name.compare(pnode2->name) ||
641 pnode1->test_function_type != pnode2->test_function_type)
646 workspace.qdim(pnode1->name) != workspace.qdim(pnode2->name) ||
647 pnode1->test_function_type != pnode2->test_function_type)
652 workspace.qdim(pnode1->name) != workspace.qdim(pnode2->name) ||
653 pnode1->test_function_type == pnode2->test_function_type)
659 case GA_NODE_VAL_TEST:
case GA_NODE_GRAD_TEST:
660 case GA_NODE_HESS_TEST:
case GA_NODE_DIVERG_TEST:
662 const mesh_fem *mf1 = workspace.associated_mf(pnode1->name);
663 const mesh_fem *mf2 = workspace.associated_mf(pnode2->name);
666 if (pnode1->name.compare(pnode2->name) ||
667 pnode1->test_function_type != pnode2->test_function_type)
672 workspace.qdim(pnode1->name) != workspace.qdim(pnode2->name) ||
673 pnode1->test_function_type != pnode2->test_function_type)
678 workspace.qdim(pnode1->name) != workspace.qdim(pnode2->name) ||
679 pnode1->test_function_type == pnode2->test_function_type)
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;
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))
705 if (pnode1->nbc1 != pnode2->nbc1)
return false;
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],
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) )
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)))
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);
741 static void ga_print_constant_tensor(
const pga_tree_node pnode,
743 size_type nt = pnode->nb_test_functions();
744 switch (pnode->tensor_order()) {
746 str << (nt ? scalar_type(0) : pnode->tensor()[0]);
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]);
758 case 2:
case 3:
case 4:
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 <<
"[";
769 if (l != 0) str <<
",";
770 if (n2 > 1) str <<
"[";
772 if (k != 0) str <<
",";
773 if (n1 > 1) str <<
"[";
775 if (j != 0) str <<
",";
776 if (n0 > 1) str <<
"[";
778 if (i != 0) str <<
",";
779 str << (nt ? scalar_type(0) : pnode->tensor()[ii++]);
781 if (n0 > 1) str <<
"]";
783 if (n1 > 1) str <<
"]";
785 if (n2 > 1) str <<
"]";
787 if (n3 > 1) str <<
"]";
791 case 5:
case 6:
case 7:
case 8:
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]);
798 for (
size_type i = 0; i < pnode->tensor_order(); ++i) {
799 if (i != 0) str <<
", ";
800 str << pnode->tensor_proper_size(i);
805 default: GMM_ASSERT1(
false,
"Invalid tensor dimension");
807 GMM_ASSERT1(pnode->children.size() == 0,
"Invalid tree");
810 void ga_print_node(
const pga_tree_node pnode, std::ostream &str) {
812 long prec = str.precision(16);
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;
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(";
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(";
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:
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(";
887 switch(pnode->node_type) {
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:
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:
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:
934 switch(pnode->node_type) {
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]))
944 if (pnode->parent->node_type == GA_NODE_PARAMS) par =
true;
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 <<
")";
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 "
992 if (pnode->children.size() >= 2)
993 ga_print_node(pnode->children[1], str);
995 str <<
"(unknown second argument)";
1002 if (pnode->nbc1) str <<
"X(" << pnode->nbc1 <<
")";
else str <<
"X";
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;
1017 case GA_NODE_INTERPOLATE_X:
case GA_NODE_SECONDARY_DOMAIN_X:
1020 case GA_NODE_INTERPOLATE_NORMAL:
case GA_NODE_SECONDARY_DOMAIN_NORMAL:
1023 case GA_NODE_INTERPOLATE_ELT_K:
1026 case GA_NODE_INTERPOLATE_ELT_B:
1029 case GA_NODE_INTERPOLATE_DERIVATIVE:
1030 str << (pnode->test_function_type == 1 ?
"Test_" :
"Test2_")
1031 <<
"Interpolate_derivative(" << pnode->interpolate_name_der <<
","
1033 if (pnode->interpolate_name.size())
1034 str <<
"," << pnode->interpolate_name;
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:
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:
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:
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:
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_")
1095 case GA_NODE_SPEC_FUNC: str << pnode->name;
break;
1096 case GA_NODE_OPERATOR:
1097 case GA_NODE_PREDEF_FUNC:
1099 str <<
"Derivative_" << pnode->der1 <<
"_";
1100 if (pnode->der2) str << pnode->der2 <<
"_";
1102 str << pnode->name;
break;
1104 GMM_ASSERT1(pnode->test_function_type !=
size_type(-1),
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;
1113 str <<
"*(Reshape(Test_" << pnode->name_test1 <<
","
1114 << pnode->qdim1<<
")(1))";
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;
1122 str <<
"*(Reshape(Test2_" << pnode->name_test2 <<
","
1123 << pnode->qdim2<<
")(1))";
1126 if (pnode->test_function_type) str <<
")";
1129 case GA_NODE_CONSTANT:
1130 ga_print_constant_tensor(pnode, str);
1133 case GA_NODE_ALLINDICES:
1135 GMM_ASSERT1(pnode->children.size() == 0,
"Invalid tree");
1138 case GA_NODE_PARAMS:
1139 GMM_ASSERT1(pnode->children.size(),
"Invalid tree");
1140 ga_print_node(pnode->children[0], 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);
1151 GMM_ASSERT1(pnode->children.size() == 0,
"Invalid tree");
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");
1164 case GA_NODE_RESHAPE:
1166 GMM_ASSERT1(pnode->children.size() == 0,
"Invalid tree");
1169 case GA_NODE_CROSS_PRODUCT:
1170 str <<
"Cross_product";
1171 GMM_ASSERT1(pnode->children.size() == 0,
"Invalid tree");
1174 case GA_NODE_SWAP_IND:
1175 str <<
"Swap_indices";
1176 GMM_ASSERT1(pnode->children.size() == 0,
"Invalid tree");
1179 case GA_NODE_IND_MOVE_LAST:
1180 str <<
"Index_move_last";
1181 GMM_ASSERT1(pnode->children.size() == 0,
"Invalid tree");
1184 case GA_NODE_CONTRACT:
1186 GMM_ASSERT1(pnode->children.size() == 0,
"Invalid tree");
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()) {
1194 ga_print_node(pnode->children[0], 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);
1206 case 2:
case 3:
case 4:
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 <<
"[";
1217 if (l != 0) str <<
",";
1218 if (n2 > 1) str <<
"[";
1220 if (k != 0) str <<
",";
1221 if (n1 > 1) str <<
"[";
1223 if (j != 0) str <<
",";
1224 if (n0 > 1) str <<
"[";
1226 if (i != 0) str <<
",";
1227 ga_print_node(pnode->children[ii++], str);
1229 if (n0 > 1) str <<
"]";
1231 if (n1 > 1) str <<
"]";
1233 if (n2 > 1) str <<
"]";
1235 if (n3 > 1) str <<
"]";
1239 case 5:
case 6:
case 7:
case 8:
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);
1246 for (
size_type i = 0; i < pnode->tensor_order(); ++i) {
1247 if (i != 0) str <<
", ";
1248 str << pnode->tensor_proper_size(i);
1253 default: GMM_ASSERT1(
false,
"Invalid tensor dimension");
1258 str <<
"Invalid or not taken into account node type "
1259 << pnode->node_type;
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;
1270 }
else if (is_secondary)
1272 else if (is_xfem_plus || is_xfem_minus)
1275 str.precision(prec);
1278 std::string ga_tree_to_string(
const ga_tree &tree) {
1279 std::stringstream str;
1281 if (tree.root) verify_tree(tree.root, 0);
1282 if (tree.root) ga_print_node(tree.root, str);
else str <<
"0";
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; }
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; }
1308 int ga_check_name_validity(
const std::string &name) {
1309 if (name.compare(0, 11,
"Derivative_") == 0)
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
1321 if (SPEC_OP.find(name) != SPEC_OP.end())
1324 if (PREDEF_FUNCTIONS.find(name) != PREDEF_FUNCTIONS.end())
1327 if (SPEC_FUNCTIONS.find(name) != SPEC_FUNCTIONS.end())
1330 if (PREDEF_OPERATORS.tab.find(name) != PREDEF_OPERATORS.tab.end())
1333 if (name.size() >= 5 && name.compare(0, 5,
"Grad_") == 0)
1336 if (name.size() >= 5 && name.compare(0, 5,
"Hess_") == 0)
1339 if (name.size() >= 4 && name.compare(0, 4,
"Div_") == 0)
1342 if (name.size() >= 6 && name.compare(0, 6,
"Test2_") == 0)
1345 if (name.size() >= 5 && name.compare(0, 5,
"Test_") == 0)
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();
1381 static void ga_replace_macro_params
1382 (ga_tree &tree, pga_tree_node pnode,
1383 const std::vector<pga_tree_node> &children) {
1385 for (pga_tree_node &child : pnode->children)
1386 ga_replace_macro_params(tree, child, children);
1388 if (pnode->node_type == GA_NODE_MACRO_PARAM) {
1391 GMM_ASSERT1(pnode->nbc1+1 < children.size(),
"Internal error");
1392 pga_tree_node pchild = children[pnode->nbc1+1];
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_ "
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;
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;
1419 pga_tree_node pnode_old = pnode;
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);
1427 GMM_ASSERT1(pnode_old->children.empty(),
"Internal error");
1433 static void ga_expand_macro(ga_tree &tree, pga_tree_node pnode,
1434 const ga_macro_dictionary ¯o_dict) {
1437 if (pnode->node_type == GA_NODE_PARAMS) {
1439 for (
size_type i = 1; i < pnode->children.size(); ++i)
1440 ga_expand_macro(tree, pnode->children[i], macro_dict);
1442 if (pnode->children[0]->node_type != GA_NODE_NAME) {
1443 ga_expand_macro(tree, pnode->children[0], macro_dict);
1446 if (macro_dict.macro_exists(pnode->children[0]->name)) {
1448 const ga_macro &gam = macro_dict.get_macro(pnode->children[0]->name);
1450 if (gam.nb_params()==0) {
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");
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 <<
".");
1464 pga_tree_node pnode_old = pnode;
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);
1472 ga_replace_macro_params(tree, pnode, pnode_old->children);
1473 tree.clear_node_rec(pnode_old);
1478 }
else if (pnode->node_type == GA_NODE_NAME &&
1479 macro_dict.macro_exists(pnode->name)) {
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()
1488 pga_tree_node pnode_old = pnode;
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);
1496 GMM_ASSERT1(pnode_old->children.empty(),
"Internal error");
1499 for (pga_tree_node &child : pnode->children)
1500 ga_expand_macro(tree, child, macro_dict);
1504 static void ga_mark_macro_params_rec(
const pga_tree_node pnode,
1505 const std::vector<std::string> ¶ms) {
1507 for (pga_tree_node &child : pnode->children)
1508 ga_mark_macro_params_rec(child, params);
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);
1520 for (
size_type i = 0; i < params.size(); ++i)
1521 if (name.compare(params[i]) == 0) {
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;
1535 pnode->node_type = GA_NODE_MACRO_PARAM;
1536 pnode->nbc1 = i; pnode->nbc2 = po; pnode->nbc3 = pt;
1541 static void ga_mark_macro_params(ga_macro &gam,
1542 const std::vector<std::string> ¶ms,
1543 const ga_macro_dictionary ¯o_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);
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;
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");
1564 void ga_macro_dictionary::add_macro(
const ga_macro &gam)
1565 { macros[gam.name()] = gam; }
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); }
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)");
1583 static GA_TOKEN_TYPE ga_read_term(pstring expr,
size_type &pos,
1585 ga_macro_dictionary ¯o_dict) {
1587 GA_TOKEN_TYPE t_type;
1592 t_type = ga_get_token(*expr, pos, token_pos, token_length);
1600 char *endptr;
const char *nptr = &((*expr)[token_pos]);
1601 scalar_type s_read = ::strtod(nptr, &endptr);
1603 ga_throw_error(expr, token_pos,
"Bad numeric format.");
1604 tree.add_scalar(s_read, token_pos, expr);
1609 tree.add_allindices(token_pos, expr);
1613 tree.add_name(&((*expr)[token_pos]), token_length, token_pos, expr);
1617 tree.add_op(GA_UNARY_MINUS, token_pos, expr);
1624 tree.add_op(GA_SYM, token_pos, expr);
1628 tree.add_op(GA_SKEW, token_pos, expr);
1632 tree.add_op(GA_TRACE, token_pos, expr);
1636 tree.add_op(GA_DEVIATOR, token_pos, expr);
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]),
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);
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);
1671 if (t_type != GA_COLON_EQ)
1672 ga_throw_error(expr, pos-1,
"Missing := for macro definition.");
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();
1679 ga_mark_macro_params(gam, params, macro_dict);
1680 macro_dict.add_macro(gam);
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.");
1694 case GA_INTERPOLATE:
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]),
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]),
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;
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 "
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.");
1744 case GA_INTERPOLATE_DERIVATIVE:
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 "
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 "
1768 tree.current_node->name
1769 = std::string(&((*expr)[token_pos]), token_length);
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);
1783 if (t_type != GA_RPAR)
1784 ga_throw_error(expr, pos-1,
"Missing a parenthesis after "
1785 "Interpolate_derivative arguments.");
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]),
1805 tree.current_node->elementary_target = tree.current_node->name;
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);
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.");
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);
1832 if (t_type != GA_RPAR)
1833 ga_throw_error(expr, pos-1,
"Missing a parenthesis after "
1834 "Elementary_transformation arguments.");
1839 case GA_SECONDARY_DOMAIN:
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]),
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.");
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]),
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.");
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]),
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.");
1908 case GA_INTERPOLATE_FILTER:
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.");
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) {
1934 t_type = ga_read_term(expr, pos, sub_tree2, macro_dict);
1935 tree.add_sub_tree(sub_tree2);
1937 if (t_type != GA_RPAR)
1938 ga_throw_error(expr, pos-1,
"Unbalanced parenthesis.");
1944 tree.add_op(GA_PRINT, token_pos, expr);
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);
1962 GA_TOKEN_TYPE r_type;
1963 size_type nbc1(0), nbc2(0), nbc3(0), n1(0), n2(0), n3(0);
1965 bool foundcomma(
false), foundsemi(
false);
1967 r_type = ga_read_term(expr, pos, sub_tree, macro_dict);
1969 tree.add_matrix(token_pos, expr);
1971 if (sub_tree.root->node_type == GA_NODE_C_MATRIX) {
1972 bgeot::multi_index mii;
1976 r_type = ga_read_term(expr, pos, sub_tree, macro_dict);
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.");
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--)
1992 sub_tree.root->tensor().adjust_sizes(mi);
1995 mii = sub_tree.root->tensor().sizes();
1997 const bgeot::multi_index &mi=sub_tree.root->tensor().sizes();
1999 if (mii.size() == mi.size()) {
2000 for (
size_type i = 0; i < mi.size(); ++i)
2001 if (mi[i] != mii[i]) cmp =
false;
2004 ga_throw_error(expr, pos-1,
"Bad explicit "
2005 "vector/matrix/tensor format.");
2007 for (pga_tree_node &child : sub_tree.root->children) {
2008 child->parent = tree.current_node;
2009 tree.current_node->children.push_back(child);
2011 sub_tree.root->children.resize(0);
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);
2021 r_type = ga_read_term(expr, pos, sub_tree, macro_dict);
2025 tree.add_sub_tree(sub_tree);
2028 if (tensor_order < 2) ++nbc1;
2029 if (tensor_order < 3) ++nbc2;
2030 if (tensor_order < 4) ++nbc3;
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.");
2037 }
else if (r_type == GA_SEMICOLON) {
2039 ga_throw_error(expr, pos-1,
"Bad explicit "
2040 "vector/matrix/tensor format.");
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.");
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 ||
2053 ga_throw_error(expr, pos-1,
"Bad explicit "
2054 "vector/matrix/tensor format.");
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 ||
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;
2067 tree.current_node->nbc2 = tree.current_node->nbc3 = 1;
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 ']'.");
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;
2082 size_type nbl = tree.current_node->children.size()
2083 / (nbc2 * nbc1 * nbc3);
2084 switch(tensor_order) {
2105 default: GMM_ASSERT1(
false,
"Internal error");
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();
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;
2118 tree.current_node->nbc1 = tree.current_node->tensor().sizes().size();
2123 ga_throw_error(expr, token_pos,
"Unexpected token.");
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:
2132 tree.add_op(t_type, token_pos, expr);
2136 tree.add_op(t_type, token_pos, expr);
2139 case GA_END:
case GA_RPAR:
case GA_COMMA:
case GA_DCOMMA:
2140 case GA_RBRACKET:
case GA_SEMICOLON:
case GA_DSEMICOLON:
2145 GA_TOKEN_TYPE r_type;
2146 tree.add_params(token_pos, expr);
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);
2160 ga_throw_error(expr, token_pos,
"Unexpected token.");
2170 void ga_read_string_reg(
const std::string &expr, ga_tree &tree,
2171 ga_macro_dictionary ¯o_dict) {
2172 size_type pos = 0, token_pos, token_length;
2174 GA_TOKEN_TYPE t = ga_get_token(expr, pos, token_pos, token_length);
2175 if (t == GA_END)
return;
2177 pstring nexpr(
new std::string(expr));
2179 t = ga_read_term(nexpr, pos, tree, macro_dict);
2180 if (tree.root) ga_expand_macro(tree, tree.root, macro_dict);
2184 ga_throw_error(nexpr, pos-1,
"Unbalanced parenthesis.");
2187 ga_throw_error(nexpr, pos-1,
"Unbalanced bracket.");
2192 ga_throw_error(nexpr, pos-1,
"Unexpected token.");
2199 void ga_read_string(
const std::string &expr, ga_tree &tree,
2200 const ga_macro_dictionary ¯o_dict) {
2201 ga_macro_dictionary macro_dict_loc(
true, macro_dict);
2202 ga_read_string_reg(expr, tree, macro_dict_loc);
2207 std::string ga_substitute(
const std::string &expr,
2208 const std::map<std::string, std::string> &dict) {
2210 size_type pos = 0, token_pos, token_length;
2211 std::stringstream exprs;
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;
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
GEneric Tool for Finite Element Methods.