25 #include "getfem/getfem_generic_assembly_compile_and_exec.h"
26 #include "getfem/getfem_generic_assembly_functions_and_operators.h"
28 #if defined(GMM_USES_BLAS)
33 #define GA_DEBUG_INFO(a)
40 template <
class VEC1,
class VEC2>
41 inline void copy_scaled_4(
const VEC1 &v1,
const scalar_type a, VEC2 &v2) {
42 auto it1 = v1.begin();
43 auto it2 = v2.begin(), it2e = v2.end();
46 *it2++ = (*it1++) * a;
47 *it2++ = (*it1++) * a;
48 *it2++ = (*it1++) * a;
49 *it2++ = (*it1++) * a;
52 *it2++ = (*it1++) * a;
55 template <
class VEC1,
class VEC2>
56 inline void add_scaled_4(
const VEC1 &v1,
const scalar_type a, VEC2 &v2) {
57 auto it1 = v1.begin();
58 auto it2 = v2.begin(), it2e = v2.end();
61 *it2++ += (*it1++) * a;
62 *it2++ += (*it1++) * a;
63 *it2++ += (*it1++) * a;
64 *it2++ += (*it1++) * a;
67 *it2++ += (*it1++) * a;
70 template <
class VEC1,
class VEC2>
71 inline void copy_scaled_8(
const VEC1 &v1,
const scalar_type a, VEC2 &v2) {
72 auto it1 = v1.begin();
73 auto it2 = v2.begin(), it2e = v2.end();
76 *it2++ = (*it1++) * a;
77 *it2++ = (*it1++) * a;
78 *it2++ = (*it1++) * a;
79 *it2++ = (*it1++) * a;
80 *it2++ = (*it1++) * a;
81 *it2++ = (*it1++) * a;
82 *it2++ = (*it1++) * a;
83 *it2++ = (*it1++) * a;
86 *it2++ = (*it1++) * a;
89 template <
class VEC1,
class VEC2>
90 inline void add_scaled_8(
const VEC1 &v1,
const scalar_type a, VEC2 &v2) {
91 auto it1 = v1.begin();
92 auto it2 = v2.begin(), it2e = v2.end();
95 *it2++ += (*it1++) * a;
96 *it2++ += (*it1++) * a;
97 *it2++ += (*it1++) * a;
98 *it2++ += (*it1++) * a;
99 *it2++ += (*it1++) * a;
100 *it2++ += (*it1++) * a;
101 *it2++ += (*it1++) * a;
102 *it2++ += (*it1++) * a;
105 *it2++ += (*it1++) * a;
108 bool operator <(
const gauss_pt_corresp &gpc1,
109 const gauss_pt_corresp &gpc2) {
110 if (gpc1.pai != gpc2.pai)
111 return (gpc1.pai < gpc2.pai );
112 if (gpc1.nodes.size() != gpc2.nodes.size())
113 return (gpc1.nodes.size() < gpc2.nodes.size());
114 for (
size_type i = 0; i < gpc1.nodes.size(); ++i)
115 if (gpc1.nodes[i] != gpc2.nodes[i])
116 return (gpc1.nodes[i] < gpc2.nodes[i]);
117 if (gpc1.pgt1 != gpc2.pgt1)
118 return (gpc1.pgt1 < gpc2.pgt1);
119 if (gpc1.pgt2 != gpc2.pgt2)
120 return (gpc1.pgt2 < gpc2.pgt2);
124 bool operator <(
const ga_instruction_set::region_mim &rm1,
125 const ga_instruction_set::region_mim &rm2) {
126 if (rm1.mim() != rm2.mim())
return (rm1.mim() < rm2.mim());
127 if (rm1.region() != rm2.region())
return (rm1.region() < rm2.region());
128 return (rm1.psd() < rm2.psd());
135 struct ga_instruction_extract_local_im_data :
public ga_instruction {
138 papprox_integration &pai;
139 const base_vector &U;
140 const fem_interpolation_context &ctx;
143 GA_DEBUG_INFO(
"Instruction: extract local im data");
147 GMM_ASSERT1(imd.linked_mesh_im().int_method_of_element(cv)
148 ->approx_method() == pai,
"Im data have to be used only "
149 "on their original integration method.");
151 size_type ipt = imd.filtered_index_of_point(cv, ctx.ii());
153 "Im data with no data on the current integration point.");
154 auto it = U.begin()+ipt*qdim;
155 std::copy(it, it+qdim, t.begin());
158 ga_instruction_extract_local_im_data
159 (base_tensor &t_,
const im_data &imd_,
const base_vector &U_,
160 papprox_integration &pai_,
const fem_interpolation_context &ctx_,
162 : t(t_), imd(imd_), pai(pai_), U(U_), ctx(ctx_), qdim(qdim_),
167 struct ga_instruction_slice_local_dofs :
public ga_instruction {
169 const base_vector &U;
170 const fem_interpolation_context &ctx;
174 GA_DEBUG_INFO(
"Instruction: Slice local dofs");
175 GMM_ASSERT1(qmult1 != 0 && qmult2 != 0,
"Internal error");
177 coeff, qmult1, qmult2);
180 ga_instruction_slice_local_dofs(
const mesh_fem &mf_,
const base_vector &U_,
181 const fem_interpolation_context &ctx_,
184 : mf(mf_), U(U_), ctx(ctx_), coeff(coeff_),
185 qmult1(qmult1_), qmult2(qmult2_) {}
188 struct ga_instruction_update_pfp :
public ga_instruction {
190 const fem_interpolation_context &ctx;
191 fem_precomp_pool &fp_pool;
195 GA_DEBUG_INFO(
"Instruction: Pfp update");
196 if (ctx.have_pgp()) {
198 ? ctx.convex_num() : mf.convex_index().first_true();
199 pfem pf = mf.fem_of_element(cv);
200 if (!pfp || pf != pfp->get_pfem() ||
201 ctx.pgp()->get_ppoint_tab() != pfp->get_ppoint_tab()) {
202 pfp = fp_pool(pf, ctx.pgp()->get_ppoint_tab());
210 ga_instruction_update_pfp(
const mesh_fem &mf_, pfem_precomp &pfp_,
211 const fem_interpolation_context &ctx_,
212 fem_precomp_pool &fp_pool_)
213 : mf(mf_), ctx(ctx_), fp_pool(fp_pool_), pfp(pfp_) {}
216 struct ga_instruction_first_ind_tensor :
public ga_instruction {
218 const fem_interpolation_context &ctx;
220 const mesh_fem *mfn, **mfg;
223 GA_DEBUG_INFO(
"Instruction: adapt first index of tensor");
224 const mesh_fem &mf = *(mfg ? *mfg : mfn);
225 GA_DEBUG_ASSERT(mfg ? *mfg : mfn,
"Internal error");
226 size_type cv_1 = ctx.is_convex_num_valid()
227 ? ctx.convex_num() : mf.convex_index().first_true();
228 pfem pf = mf.fem_of_element(cv_1);
229 GMM_ASSERT1(pf,
"An element without finite element method defined");
230 size_type Qmult = qdim / pf->target_dim();
232 if (t.sizes()[0] != s)
233 { bgeot::multi_index mi = t.sizes(); mi[0] = s; t.adjust_sizes(mi); }
237 ga_instruction_first_ind_tensor(base_tensor &t_,
238 const fem_interpolation_context &ctx_,
240 const mesh_fem **mfg_)
241 : t(t_), ctx(ctx_), qdim(qdim_), mfn(mfn_), mfg(mfg_) {}
244 struct ga_instruction_second_ind_tensor
245 :
public ga_instruction_first_ind_tensor {
248 GA_DEBUG_INFO(
"Instruction: adapt second index of tensor");
249 const mesh_fem &mf = *(mfg ? *mfg : mfn);
250 size_type cv_1 = ctx.is_convex_num_valid()
251 ? ctx.convex_num() : mf.convex_index().first_true();
252 pfem pf = mf.fem_of_element(cv_1);
253 GMM_ASSERT1(pf,
"An element without finite element methode defined");
254 size_type Qmult = qdim / pf->target_dim();
256 if (t.sizes()[1] != s)
257 { bgeot::multi_index mi = t.sizes(); mi[1] = s; t.adjust_sizes(mi); }
261 ga_instruction_second_ind_tensor(base_tensor &t_,
262 fem_interpolation_context &ctx_,
264 const mesh_fem **mfg_)
265 : ga_instruction_first_ind_tensor(t_, ctx_, qdim_, mfn_, mfg_) {}
269 struct ga_instruction_two_first_ind_tensor :
public ga_instruction {
271 const fem_interpolation_context &ctx1, &ctx2;
273 const mesh_fem *mfn1, **mfg1;
275 const mesh_fem *mfn2, **mfg2;
278 GA_DEBUG_INFO(
"Instruction: adapt two first indices of tensor");
279 const mesh_fem &mf1 = *(mfg1 ? *mfg1 : mfn1);
280 const mesh_fem &mf2 = *(mfg2 ? *mfg2 : mfn2);
281 size_type cv_1 = ctx1.is_convex_num_valid()
282 ? ctx1.convex_num() : mf1.convex_index().first_true();
283 size_type cv_2 = ctx2.is_convex_num_valid()
284 ? ctx2.convex_num() : mf2.convex_index().first_true();
285 pfem pf1 = mf1.fem_of_element(cv_1);
286 GMM_ASSERT1(pf1,
"An element without finite element method defined");
287 pfem pf2 = mf2.fem_of_element(cv_2);
288 GMM_ASSERT1(pf2,
"An element without finite element method defined");
289 size_type Qmult1 = qdim1 / pf1->target_dim();
290 size_type s1 = pf1->nb_dof(cv_1) * Qmult1;
291 size_type Qmult2 = qdim2 / pf2->target_dim();
292 size_type s2 = pf2->nb_dof(cv_2) * Qmult2;
293 GMM_ASSERT1(s1 > 0 && s2 >0,
"Element without degrees of freedom");
294 if (t.sizes()[0] != s1 || t.sizes()[1] != s2) {
295 bgeot::multi_index mi = t.sizes();
296 mi[0] = s1; mi[1] = s2;
302 ga_instruction_two_first_ind_tensor
303 (base_tensor &t_,
const fem_interpolation_context &ctx1_,
304 const fem_interpolation_context &ctx2_,
305 size_type qdim1_,
const mesh_fem *mfn1_,
const mesh_fem **mfg1_,
306 size_type qdim2_,
const mesh_fem *mfn2_,
const mesh_fem **mfg2_)
307 : t(t_), ctx1(ctx1_), ctx2(ctx2_), qdim1(qdim1_), mfn1(mfn1_),
308 mfg1(mfg1_), qdim2(qdim2_), mfn2(mfn2_), mfg2(mfg2_) {}
312 struct ga_instruction_X_component :
public ga_instruction {
314 const fem_interpolation_context &ctx;
318 GA_DEBUG_INFO(
"Instruction: X component");
323 ga_instruction_X_component
324 (scalar_type &t_,
const fem_interpolation_context &ctx_,
size_type n_)
325 : t(t_), ctx(ctx_), n(n_) {}
328 struct ga_instruction_X :
public ga_instruction {
330 const fem_interpolation_context &ctx;
333 GA_DEBUG_INFO(
"Instruction: X");
334 GA_DEBUG_ASSERT(t.size() == ctx.xreal().size(),
"dimensions mismatch");
339 ga_instruction_X(base_tensor &t_,
const fem_interpolation_context &ctx_)
340 : t(t_), ctx(ctx_) {}
343 struct ga_instruction_copy_small_vect :
public ga_instruction {
345 const base_small_vector &vec;
348 GA_DEBUG_INFO(
"Instruction: copy small vector");
349 GMM_ASSERT1(t.size() == vec.size(),
"Invalid vector size.");
353 ga_instruction_copy_small_vect(base_tensor &t_,
354 const base_small_vector &vec_)
355 : t(t_), vec(vec_) {}
358 struct ga_instruction_copy_Normal :
public ga_instruction_copy_small_vect {
361 GA_DEBUG_INFO(
"Instruction: unit normal vector");
362 GMM_ASSERT1(t.size() == vec.size(),
"Invalid outward unit normal "
363 "vector. Possible reasons: not on boundary (Normal is "
364 "defined on faces, and inside the elements of curves in "
365 "2D and surfaces in 3D) or transformation failed.");
369 ga_instruction_copy_Normal(base_tensor &t_,
370 const base_small_vector &Normal_)
371 : ga_instruction_copy_small_vect(t_, Normal_) {}
374 struct ga_instruction_level_set_normal_vector :
public ga_instruction {
376 const mesh_im_level_set *mimls;
377 const fem_interpolation_context &ctx;
378 base_small_vector vec;
381 GA_DEBUG_INFO(
"Instruction: unit normal vector to a level-set");
382 mimls->compute_normal_vector(ctx, vec);
383 GMM_ASSERT1(t.size() == vec.size(),
"Invalid outward unit normal "
384 "vector. Possible reasons: not on boundary or "
385 "transformation failed.");
389 ga_instruction_level_set_normal_vector
390 (base_tensor &t_,
const mesh_im_level_set *mimls_,
391 const fem_interpolation_context &ctx_)
392 : t(t_), mimls(mimls_), ctx(ctx_), vec(t.size()) {}
395 struct ga_instruction_element_size :
public ga_instruction {
400 GA_DEBUG_INFO(
"Instruction: element_size");
401 GMM_ASSERT1(t.size() == 1,
"Invalid element size.");
405 ga_instruction_element_size(base_tensor &t_, scalar_type &es_)
409 struct ga_instruction_element_K :
public ga_instruction {
411 const fem_interpolation_context &ctx;
414 GA_DEBUG_INFO(
"Instruction: element_K");
415 GMM_ASSERT1(t.size() == (ctx.K()).size(),
"Invalid tensor size.");
416 gmm::copy(ctx.K().as_vector(), t.as_vector());
419 ga_instruction_element_K(base_tensor &t_,
420 const fem_interpolation_context &ct)
424 struct ga_instruction_element_B :
public ga_instruction {
426 const fem_interpolation_context &ctx;
429 GA_DEBUG_INFO(
"Instruction: element_B");
430 GMM_ASSERT1(t.size() == (ctx.B()).size(),
"Invalid tensor size.");
431 gmm::copy(ctx.B().as_vector(), t.as_vector());
434 ga_instruction_element_B(base_tensor &t_,
435 const fem_interpolation_context &ct)
448 struct ga_instruction_element_curvature :
public ga_instruction {
450 const fem_interpolation_context &ctx;
451 base_matrix PC, Htau, bB;
452 base_small_vector n, bab;
455 GA_DEBUG_INFO(
"Instruction: element_curvature");
458 GMM_ASSERT1(P+1 == N && (N == 2 || N == 3),
"element_curvature is "
459 "only defined for curves in 2D and surfaces in 3D");
460 GMM_ASSERT1(t.size() == N*N,
"Invalid tensor size.");
462 if (pgt->is_linear())
return 0;
464 const base_matrix &K = ctx.K(), &B = ctx.B();
467 n[0] = K(1,0); n[1] = -K(0,0);
469 n[0] = K(1,0)*K(2,1) - K(2,0)*K(1,1);
470 n[1] = K(2,0)*K(0,1) - K(0,0)*K(2,1);
471 n[2] = K(0,0)*K(1,1) - K(1,0)*K(0,1);
473 gmm::scale(n, scalar_type(1)/gmm::vect_norm2(n));
476 Htau.base_resize(N, P*P);
477 if (ctx.have_pgp() && ctx.ii() !=
size_type(-1))
478 gmm::mult(ctx.G(), ctx.pgp()->hessian(ctx.ii()), Htau);
480 PC.base_resize(pgt->nb_points(), P*P);
481 pgt->poly_vector_hess(ctx.xref(), PC);
487 gmm::mult(gmm::transposed(Htau), n, bab);
490 bB.base_resize(P, N);
495 bB(a, j) += bab[a + P*c] * B(j, c);
499 for (
size_type a = 0; a < P; ++a) s += B(i, a) * bB(a, j);
504 ga_instruction_element_curvature(base_tensor &t_,
505 const fem_interpolation_context &ct)
509 struct ga_instruction_val_base :
public ga_instruction {
511 fem_interpolation_context &ctx;
513 const pfem_precomp &pfp;
516 GA_DEBUG_INFO(
"Instruction: compute value of base functions");
521 if (ctx.have_pgp()) ctx.pfp_base_value(t, pfp);
523 ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
524 GMM_ASSERT1(ctx.pf(),
"Undefined finite element method");
530 ga_instruction_val_base(base_tensor &tt, fem_interpolation_context &ct,
531 const mesh_fem &mf_,
const pfem_precomp &pfp_)
532 : t(tt), ctx(ct), mf(mf_), pfp(pfp_) {}
535 struct ga_instruction_xfem_plus_val_base :
public ga_instruction {
537 fem_interpolation_context &ctx;
542 GA_DEBUG_INFO(
"Instruction: compute value of base functions");
543 if (ctx.have_pgp()) ctx.set_pfp(pfp);
544 else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
545 GMM_ASSERT1(ctx.pf(),
"Undefined finite element method");
546 int old_xfem_side = ctx.xfem_side();
547 ctx.set_xfem_side(1);
549 ctx.set_xfem_side(old_xfem_side);
553 ga_instruction_xfem_plus_val_base(base_tensor &tt,
554 fem_interpolation_context &ct,
555 const mesh_fem &mf_, pfem_precomp &pfp_)
556 : t(tt), ctx(ct), mf(mf_), pfp(pfp_) {}
559 struct ga_instruction_xfem_minus_val_base :
public ga_instruction {
561 fem_interpolation_context &ctx;
566 GA_DEBUG_INFO(
"Instruction: compute value of base functions");
567 if (ctx.have_pgp()) ctx.set_pfp(pfp);
568 else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
569 GMM_ASSERT1(ctx.pf(),
"Undefined finite element method");
570 int old_xfem_side = ctx.xfem_side();
571 ctx.set_xfem_side(-1);
573 ctx.set_xfem_side(old_xfem_side);
577 ga_instruction_xfem_minus_val_base
578 (base_tensor &tt, fem_interpolation_context &ct,
579 const mesh_fem &mf_, pfem_precomp &pfp_)
580 : t(tt), ctx(ct), mf(mf_), pfp(pfp_) {}
583 struct ga_instruction_grad_base :
public ga_instruction_val_base {
586 GA_DEBUG_INFO(
"Instruction: compute gradient of base functions");
591 if (ctx.have_pgp()) ctx.pfp_grad_base_value(t, pfp);
593 ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
594 GMM_ASSERT1(ctx.pf(),
"Undefined finite element method");
595 ctx.grad_base_value(t);
600 ga_instruction_grad_base(base_tensor &tt, fem_interpolation_context &ct,
601 const mesh_fem &mf_, pfem_precomp &pfp_)
602 : ga_instruction_val_base(tt, ct, mf_, pfp_)
606 struct ga_instruction_xfem_plus_grad_base :
public ga_instruction_val_base {
609 GA_DEBUG_INFO(
"Instruction: compute gradient of base functions");
610 if (ctx.have_pgp()) ctx.set_pfp(pfp);
611 else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
612 GMM_ASSERT1(ctx.pf(),
"Undefined finite element method");
613 int old_xfem_side = ctx.xfem_side();
614 ctx.set_xfem_side(1);
615 ctx.grad_base_value(t);
616 ctx.set_xfem_side(old_xfem_side);
620 ga_instruction_xfem_plus_grad_base
621 (base_tensor &tt, fem_interpolation_context &ct,
622 const mesh_fem &mf_, pfem_precomp &pfp_)
623 : ga_instruction_val_base(tt, ct, mf_, pfp_)
627 struct ga_instruction_xfem_minus_grad_base :
public ga_instruction_val_base {
630 GA_DEBUG_INFO(
"Instruction: compute gradient of base functions");
631 if (ctx.have_pgp()) ctx.set_pfp(pfp);
632 else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
633 GMM_ASSERT1(ctx.pf(),
"Undefined finite element method");
634 int old_xfem_side = ctx.xfem_side();
635 ctx.set_xfem_side(-1);
636 ctx.grad_base_value(t);
637 ctx.set_xfem_side(old_xfem_side);
641 ga_instruction_xfem_minus_grad_base
642 (base_tensor &tt, fem_interpolation_context &ct,
643 const mesh_fem &mf_, pfem_precomp &pfp_)
644 : ga_instruction_val_base(tt, ct, mf_, pfp_)
649 struct ga_instruction_hess_base :
public ga_instruction_val_base {
652 GA_DEBUG_INFO(
"Instruction: compute Hessian of base functions");
653 if (ctx.have_pgp()) ctx.set_pfp(pfp);
654 else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
655 GMM_ASSERT1(ctx.pf(),
"Undefined finite element method");
656 ctx.hess_base_value(t);
660 ga_instruction_hess_base(base_tensor &tt, fem_interpolation_context &ct,
661 const mesh_fem &mf_, pfem_precomp &pfp_)
662 : ga_instruction_val_base(tt, ct, mf_, pfp_)
666 struct ga_instruction_xfem_plus_hess_base :
public ga_instruction_val_base {
669 GA_DEBUG_INFO(
"Instruction: compute Hessian of base functions");
670 if (ctx.have_pgp()) ctx.set_pfp(pfp);
671 else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
672 GMM_ASSERT1(ctx.pf(),
"Undefined finite element method");
673 int old_xfem_side = ctx.xfem_side();
674 ctx.set_xfem_side(1);
675 ctx.hess_base_value(t);
676 ctx.set_xfem_side(old_xfem_side);
680 ga_instruction_xfem_plus_hess_base
681 (base_tensor &tt, fem_interpolation_context &ct,
682 const mesh_fem &mf_, pfem_precomp &pfp_)
683 : ga_instruction_val_base(tt, ct, mf_, pfp_)
687 struct ga_instruction_xfem_minus_hess_base :
public ga_instruction_val_base {
690 GA_DEBUG_INFO(
"Instruction: compute Hessian of base functions");
691 if (ctx.have_pgp()) ctx.set_pfp(pfp);
692 else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
693 GMM_ASSERT1(ctx.pf(),
"Undefined finite element method");
694 int old_xfem_side = ctx.xfem_side();
695 ctx.set_xfem_side(-1);
696 ctx.hess_base_value(t);
697 ctx.set_xfem_side(old_xfem_side);
701 ga_instruction_xfem_minus_hess_base
702 (base_tensor &tt, fem_interpolation_context &ct,
703 const mesh_fem &mf_, pfem_precomp &pfp_)
704 : ga_instruction_val_base(tt, ct, mf_, pfp_)
708 struct ga_instruction_val :
public ga_instruction {
711 const base_tensor &Z;
712 const base_vector &coeff;
716 GA_DEBUG_INFO(
"Instruction: variable value");
718 if (!ndof) {
gmm::clear(t.as_vector());
return 0; }
719 GA_DEBUG_ASSERT(t.size() == qdim,
"dimensions mismatch");
722 GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof,
723 "Wrong size for coeff vector");
724 auto itc = coeff.begin();
auto itZ = Z.begin();
725 a = (*itc++) * (*itZ++);
726 while (itc != coeff.end()) a += (*itc++) * (*itZ++);
729 if (target_dim == 1) {
730 GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof*qdim,
731 "Wrong size for coeff vector");
732 auto itc = coeff.begin();
auto itZ = Z.begin();
733 for (
auto it = t.begin(); it != t.end(); ++it)
734 *it = (*itc++) * (*itZ);
736 for (
size_type j = 1; j < ndof; ++j, ++itZ) {
737 for (
auto it = t.begin(); it != t.end(); ++it)
738 *it += (*itc++) * (*itZ);
742 GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof*Qmult,
743 "Wrong size for coeff vector");
746 auto itc = coeff.begin();
749 for (
size_type q = 0; q < Qmult; ++q, ++itc) {
750 for (
size_type r = 0; r < target_dim; ++r)
751 *it++ += (*itc) * Z[j + r*ndof];
759 ga_instruction_val(base_tensor &tt,
const base_tensor &Z_,
761 : a(tt[0]), t(tt), Z(Z_), coeff(co), qdim(q) {}
764 struct ga_instruction_grad :
public ga_instruction_val {
767 GA_DEBUG_INFO(
"Instruction: gradient");
769 if (!ndof) {
gmm::clear(t.as_vector());
return 0; }
772 GA_DEBUG_ASSERT(t.size() == N,
"dimensions mismatch");
773 GA_DEBUG_ASSERT(coeff.size() == ndof,
"Wrong size for coeff vector");
774 auto itZ = Z.begin();
775 for (
auto it = t.begin(); it != t.end(); ++it) {
776 auto itc = coeff.begin();
777 *it = (*itc++) * (*itZ++);
778 while (itc != coeff.end()) *it += (*itc++) * (*itZ++);
782 if (target_dim == 1) {
783 GA_DEBUG_ASSERT(t.size() == N*qdim,
"dimensions mismatch");
784 GA_DEBUG_ASSERT(coeff.size() == ndof*qdim,
785 "Wrong size for coeff vector");
787 auto itZ = Z.begin();
auto it = t.begin() + q;
790 auto itc = coeff.begin() + q;
791 *it = (*itc) * (*itZ++);
793 { itc += qdim; *it += (*itc) * (*itZ++); }
798 GA_DEBUG_ASSERT(t.size() == N*qdim,
"dimensions mismatch");
799 GA_DEBUG_ASSERT(coeff.size() == ndof*Qmult,
800 "Wrong size for coeff vector");
803 auto itZ = Z.begin();
805 for (
size_type r = 0; r < target_dim; ++r)
807 t[r + q*target_dim + k*qdim] += coeff[j*Qmult+q] * (*itZ++);
814 ga_instruction_grad(base_tensor &tt,
const base_tensor &Z_,
816 : ga_instruction_val(tt, Z_, co, q)
821 struct ga_instruction_hess :
public ga_instruction_val {
824 GA_DEBUG_INFO(
"Instruction: Hessian");
826 if (!ndof) {
gmm::clear(t.as_vector());
return 0; }
827 size_type NN = gmm::sqr(t.sizes().back());
828 GA_DEBUG_ASSERT(NN == Z.sizes()[2],
"Internal error");
830 GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof,
831 "Wrong size for coeff vector");
832 auto it = Z.begin();
auto itt = t.begin();
833 for (
size_type kl = 0; kl < NN; ++kl, ++itt) {
834 *itt = scalar_type(0);
835 for (
auto itc = coeff.begin(); itc != coeff.end(); ++itc, ++it)
836 *itt += (*itc) * (*it);
838 GMM_ASSERT1(itt == t.end(),
"dimensions mismatch");
841 if (target_dim == 1) {
842 GA_DEBUG_ASSERT(t.size() == NN*qdim,
"dimensions mismatch");
843 GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof*qdim,
844 "Wrong size for coeff vector");
847 base_tensor::const_iterator it = Z.begin();
849 for (
size_type j = 0; j < ndof; ++j, ++it)
850 t[q + kl*qdim] += coeff[j*qdim+q] * (*it);
854 GA_DEBUG_ASSERT(t.size() == NN*qdim,
"dimensions mismatch");
855 GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof*Qmult,
856 "Wrong size for coeff vector");
859 base_tensor::const_iterator it = Z.begin();
861 for (
size_type r = 0; r < target_dim; ++r)
862 for (
size_type j = 0; j < ndof; ++j, ++it)
863 t[r + q*target_dim + kl*qdim] += coeff[j*Qmult+q] * (*it);
870 ga_instruction_hess(base_tensor &tt,
const base_tensor &Z_,
872 : ga_instruction_val(tt, Z_, co, q)
876 struct ga_instruction_diverg :
public ga_instruction_val {
879 GA_DEBUG_INFO(
"Instruction: divergence");
881 if (!ndof) {
gmm::clear(t.as_vector());
return 0; }
885 GA_DEBUG_ASSERT(Qmult*target_dim == N && (Qmult == 1 || target_dim == 1),
886 "Dimensions mismatch for divergence operator");
887 GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof*Qmult,
888 "Wrong size for coeff vector");
890 t[0] = scalar_type(0);
891 base_tensor::const_iterator it = Z.begin();
894 if (k) it += (N*ndof + 1);
897 t[0] += coeff[j] * (*it);
905 t[0] += coeff[j*N+k] * (*it);
911 ga_instruction_diverg(base_tensor &tt,
const base_tensor &Z_,
913 : ga_instruction_val(tt, Z_, co, q)
917 struct ga_instruction_copy_val_base :
public ga_instruction {
919 const base_tensor &Z;
923 GA_DEBUG_INFO(
"Instruction: value of test functions");
925 GA_DEBUG_ASSERT(t.size() == Z.size(),
"Wrong size for base vector");
926 std::copy(Z.begin(), Z.end(), t.begin());
931 std::copy(Z.begin(), Z.end(), t.begin());
933 if (target_dim == 1) {
935 GA_DEBUG_ASSERT(t.size() == Z.size() * Qmult * Qmult,
936 "Wrong size for base vector");
937 std::fill(t.begin(), t.end(), scalar_type(0));
938 auto itZ = Z.begin();
943 for (
size_type i = 0; i < ndof; ++i, ++itZ) {
947 for (
size_type j = 1; j < Qmult; ++j) { it2 += sss; *it2 = *itZ; }
951 GA_DEBUG_ASSERT(t.size() == Z.size() * Qmult * Qmult,
952 "Wrong size for base vector");
953 std::fill(t.begin(), t.end(), scalar_type(0));
954 auto itZ = Z.begin();
958 for (
size_type k = 0; k < target_dim; ++k)
959 for (
size_type i = 0; i < ndof; ++i, ++itZ)
961 t[i*Qmult + j + s*(j*target_dim + k)] = *itZ;
968 ga_instruction_copy_val_base(base_tensor &tt,
const base_tensor &Z_,
972 struct ga_instruction_copy_grad_base :
public ga_instruction_copy_val_base {
975 GA_DEBUG_INFO(
"Instruction: gradient of test functions");
977 std::copy(Z.begin(), Z.end(), t.begin());
982 std::copy(Z.begin(), Z.end(), t.begin());
984 if (target_dim == 1) {
987 GA_DEBUG_ASSERT(t.size() == Z.size() * Qmult * Qmult,
988 "Wrong size for gradient vector");
989 std::fill(t.begin(), t.end(), scalar_type(0));
990 base_tensor::const_iterator itZ = Z.begin();
991 size_type s = t.sizes()[0], sss = s+1, ssss = s*target_dim*Qmult;
995 base_tensor::iterator it = t.begin() + (ssss*l);
996 for (
size_type i = 0; i < ndof; ++i, ++itZ) {
998 base_tensor::iterator it2 = it;
1000 for (
size_type j = 1; j < Qmult; ++j) { it2+=sss; *it2=*itZ; }
1006 GA_DEBUG_ASSERT(t.size() == Z.size() * Qmult * Qmult,
1007 "Wrong size for gradient vector");
1008 std::fill(t.begin(), t.end(), scalar_type(0));
1009 base_tensor::const_iterator itZ = Z.begin();
1010 size_type s = t.sizes()[0], ss = s * qdim;
1014 for (
size_type k = 0; k < target_dim; ++k)
1015 for (
size_type i = 0; i < ndof; ++i, ++itZ)
1017 t[i*Qmult + j + s*(j*target_dim + k) + ss*l] = *itZ;
1024 ga_instruction_copy_grad_base(base_tensor &tt,
const base_tensor &Z_,
1026 : ga_instruction_copy_val_base(tt,Z_,q) {}
1029 struct ga_instruction_copy_vect_val_base :
public ga_instruction {
1031 const base_tensor &Z;
1034 virtual int exec() {
1035 GA_DEBUG_INFO(
"Instruction: vectorized value of test functions");
1038 GA_DEBUG_ASSERT(t.size() == Z.size() * qdim * qdim,
1039 "Wrong size for base vector");
1041 auto itZ = Z.begin();
1045 auto it = t.begin();
1046 for (
size_type i = 0; i < ndof; ++i, ++itZ) {
1050 for (
size_type j = 1; j < qdim; ++j) { it2 += sss; *it2 = *itZ; }
1055 ga_instruction_copy_vect_val_base(base_tensor &tt,
const base_tensor &Z_,
1059 struct ga_instruction_copy_vect_grad_base
1060 :
public ga_instruction_copy_vect_val_base {
1062 virtual int exec() {
1063 GA_DEBUG_INFO(
"Instruction: vectorized gradient of test functions");
1066 GA_DEBUG_ASSERT(t.size() == Z.size() * qdim * qdim,
1067 "Wrong size for gradient vector");
1069 base_tensor::const_iterator itZ = Z.begin();
1070 size_type s = t.sizes()[0], sss = s+1, ssss = s*qdim;
1074 base_tensor::iterator it = t.begin() + (ssss*l);
1075 for (
size_type i = 0; i < ndof; ++i, ++itZ) {
1077 base_tensor::iterator it2 = it;
1079 for (
size_type j = 1; j < qdim; ++j) { it2+=sss; *it2=*itZ; }
1085 ga_instruction_copy_vect_grad_base(base_tensor &tt,
const base_tensor &Z_,
1087 : ga_instruction_copy_vect_val_base(tt,Z_,q) {}
1090 struct ga_instruction_copy_hess_base :
public ga_instruction_copy_val_base {
1092 virtual int exec() {
1093 GA_DEBUG_INFO(
"Instruction: Hessian of test functions");
1097 gmm::copy(Z.as_vector(), t.as_vector());
1100 GA_DEBUG_ASSERT(t.size() == Z.size() * Qmult * Qmult,
1101 "Wrong size for Hessian vector");
1103 base_tensor::const_iterator itZ = Z.begin();
1104 size_type s = t.sizes()[0], ss = s * qdim;
1109 for (
size_type k = 0; k < target_dim; ++k)
1110 for (
size_type i = 0; i < ndof; ++i, ++itZ)
1112 t[i*Qmult + j + s*(j*target_dim + k) + ss*lm] = *itZ;
1117 ga_instruction_copy_hess_base(base_tensor &tt,
const base_tensor &Z_,
1119 : ga_instruction_copy_val_base(tt, Z_, q) {}
1122 struct ga_instruction_copy_diverg_base :
public ga_instruction_copy_val_base {
1124 virtual int exec() {
1125 GA_DEBUG_INFO(
"Instruction: divergence of test functions");
1130 GA_DEBUG_ASSERT(Qmult*target_dim == N && (Qmult == 1 || target_dim == 1),
1131 "Dimensions mismatch for divergence operator");
1132 GA_DEBUG_ASSERT(t.size() == ndof * Qmult,
1133 "Wrong size for divergence vector");
1135 base_tensor::const_iterator itZ = Z.begin();
1139 base_tensor::iterator it = t.begin();
1140 if (l) itZ += target_dim*ndof+1;
1142 if (i) { ++it; ++itZ; }
1149 base_tensor::iterator it = t.begin() + j;
1152 if (i) { it += Qmult; ++itZ; }
1160 ga_instruction_copy_diverg_base(base_tensor &tt,
const base_tensor &Z_,
1162 : ga_instruction_copy_val_base(tt, Z_, q) {}
1165 struct ga_instruction_elementary_trans {
1166 const base_vector &coeff_in;
1167 base_vector coeff_out;
1168 pelementary_transformation elemtrans;
1169 const mesh_fem &mf1, &mf2;
1170 const fem_interpolation_context &ctx;
1175 if (icv != ctx.convex_num() || M.size() == 0) {
1176 M.base_resize(m, n);
1177 icv = ctx.convex_num();
1178 elemtrans->give_transformation(mf1, mf2, icv, M);
1180 coeff_out.resize(gmm::mat_nrows(M));
1184 ga_instruction_elementary_trans
1185 (
const base_vector &co, pelementary_transformation e,
1186 const mesh_fem &mf1_,
const mesh_fem &mf2_,
1187 const fem_interpolation_context &ctx_, base_matrix &M_,
1189 : coeff_in(co), elemtrans(e), mf1(mf1_), mf2(mf2_), ctx(ctx_),
1191 ~ga_instruction_elementary_trans() {};
1194 struct ga_instruction_elementary_trans_val
1195 :
public ga_instruction_val, ga_instruction_elementary_trans {
1197 virtual int exec() {
1198 GA_DEBUG_INFO(
"Instruction: variable value with elementary "
1202 do_transformation(coeff_in.size(), ndof*Qmult);
1203 return ga_instruction_val::exec();
1206 ga_instruction_elementary_trans_val
1207 (base_tensor &tt,
const base_tensor &Z_,
const base_vector &co,
size_type q,
1208 pelementary_transformation e,
const mesh_fem &mf1_,
const mesh_fem &mf2_,
1209 fem_interpolation_context &ctx_, base_matrix &M_,
size_type &icv_)
1210 : ga_instruction_val(tt, Z_, coeff_out, q),
1211 ga_instruction_elementary_trans(co, e, mf1_, mf2_, ctx_, M_, icv_) {}
1214 struct ga_instruction_elementary_trans_grad
1215 :
public ga_instruction_grad, ga_instruction_elementary_trans {
1217 virtual int exec() {
1218 GA_DEBUG_INFO(
"Instruction: gradient with elementary transformation");
1221 do_transformation(coeff_in.size(), ndof*Qmult);
1222 return ga_instruction_grad::exec();
1225 ga_instruction_elementary_trans_grad
1226 (base_tensor &tt,
const base_tensor &Z_,
const base_vector &co,
size_type q,
1227 pelementary_transformation e,
const mesh_fem &mf1_,
const mesh_fem &mf2_,
1228 fem_interpolation_context &ctx_, base_matrix &M_,
size_type &icv_)
1229 : ga_instruction_grad(tt, Z_, coeff_out, q),
1230 ga_instruction_elementary_trans(co, e, mf1_, mf2_, ctx_, M_, icv_) {}
1233 struct ga_instruction_elementary_trans_hess
1234 :
public ga_instruction_hess, ga_instruction_elementary_trans {
1236 virtual int exec() {
1237 GA_DEBUG_INFO(
"Instruction: Hessian with elementary transformation");
1240 do_transformation(coeff_in.size(), ndof*Qmult);
1241 return ga_instruction_hess::exec();
1244 ga_instruction_elementary_trans_hess
1245 (base_tensor &tt,
const base_tensor &Z_,
const base_vector &co,
size_type q,
1246 pelementary_transformation e,
const mesh_fem &mf1_,
const mesh_fem &mf2_,
1247 fem_interpolation_context &ctx_, base_matrix &M_,
size_type &icv_)
1248 : ga_instruction_hess(tt, Z_, coeff_out, q),
1249 ga_instruction_elementary_trans(co, e, mf1_, mf2_, ctx_, M_, icv_) {}
1252 struct ga_instruction_elementary_trans_diverg
1253 :
public ga_instruction_diverg, ga_instruction_elementary_trans {
1255 virtual int exec() {
1256 GA_DEBUG_INFO(
"Instruction: divergence with elementary transformation");
1259 do_transformation(coeff_in.size(), ndof*Qmult);
1260 return ga_instruction_diverg::exec();
1263 ga_instruction_elementary_trans_diverg
1264 (base_tensor &tt,
const base_tensor &Z_,
const base_vector &co,
size_type q,
1265 pelementary_transformation e,
const mesh_fem &mf1_,
const mesh_fem &mf2_,
1266 fem_interpolation_context &ctx_, base_matrix &M_,
size_type &icv_)
1267 : ga_instruction_diverg(tt, Z_, coeff_out, q),
1268 ga_instruction_elementary_trans(co, e, mf1_, mf2_, ctx_, M_, icv_) {}
1271 struct ga_instruction_update_group_info :
public ga_instruction {
1272 const ga_workspace &workspace;
1273 const ga_instruction_set &gis;
1274 const ga_instruction_set::interpolate_info &inin;
1275 const std::string gname;
1276 ga_instruction_set::variable_group_info &vgi;
1278 virtual int exec() {
1279 GA_DEBUG_INFO(
"Instruction: Update group info for "+gname);
1280 if (vgi.cached_mesh && vgi.cached_mesh == inin.m)
1283 vgi.cached_mesh = inin.m;
1284 const std::string &varname
1285 = inin.m ? workspace.variable_in_group(gname, *(inin.m))
1286 : workspace.first_variable_of_group(gname);
1287 vgi.varname = &varname;
1288 vgi.mf = workspace.associated_mf(varname);
1289 GA_DEBUG_ASSERT(vgi.mf,
"Group variable should always have a mesh_fem");
1290 vgi.reduced_mf = vgi.mf->is_reduced();
1291 if (vgi.reduced_mf) {
1292 const auto it = gis.really_extended_vars.find(varname);
1293 GA_DEBUG_ASSERT(it != gis.really_extended_vars.end(),
1294 "Variable " << varname <<
" not in extended variables");
1295 vgi.U = &(it->second);
1296 vgi.I = &(workspace.temporary_interval_of_variable(varname));
1298 vgi.U = &(workspace.value(varname));
1299 vgi.I = &(workspace.interval_of_variable(varname));
1301 vgi.alpha = workspace.factor_of_variable(varname);
1305 ga_instruction_update_group_info
1306 (
const ga_workspace &workspace_,
const ga_instruction_set &gis_,
1307 const ga_instruction_set::interpolate_info &inin_,
1308 const std::string &gname_, ga_instruction_set::variable_group_info &vgi_)
1309 : workspace(workspace_), gis(gis_), inin(inin_), gname(gname_), vgi(vgi_)
1313 struct ga_instruction_interpolate_filter :
public ga_instruction {
1315 const ga_instruction_set::interpolate_info &inin;
1319 virtual int exec() {
1320 GA_DEBUG_INFO(
"Instruction: interpolated filter");
1321 if ((pt_type ==
size_type(-1) && inin.pt_type) ||
1322 (pt_type !=
size_type(-1) && inin.pt_type == pt_type)) {
1323 GA_DEBUG_INFO(
"Instruction: interpolated filter: pass");
1327 GA_DEBUG_INFO(
"Instruction: interpolated filter: filtered");
1334 ga_instruction_interpolate_filter
1335 (base_tensor &t_,
const ga_instruction_set::interpolate_info &inin_,
1337 : t(t_), inin(inin_), pt_type(ind_), nb(nb_) {}
1340 struct ga_instruction_copy_interpolated_small_vect :
public ga_instruction {
1342 const base_small_vector &vec;
1343 const ga_instruction_set::interpolate_info &inin;
1345 virtual int exec() {
1346 GA_DEBUG_INFO(
"Instruction: copy small vector");
1347 GMM_ASSERT1(!(inin.has_ctx) || inin.ctx.is_convex_num_valid(),
1348 "Invalid element, probably transformation failed");
1349 GMM_ASSERT1(t.size() == vec.size(),
1350 "Invalid vector size: " << t.size() <<
"!=" << vec.size());
1354 ga_instruction_copy_interpolated_small_vect
1355 (base_tensor &t_,
const base_small_vector &vec_,
1356 const ga_instruction_set::interpolate_info &inin_)
1357 : t(t_), vec(vec_), inin(inin_) {}
1360 struct ga_instruction_interpolate :
public ga_instruction {
1363 const mesh_fem *mfn, **mfg;
1364 const base_vector *Un, **Ug;
1365 fem_interpolation_context &ctx;
1369 fem_precomp_pool &fp_pool;
1370 ga_instruction_set::interpolate_info &inin;
1372 virtual int exec() {
1373 GMM_ASSERT1(ctx.is_convex_num_valid(),
"No valid element for the "
1374 "transformation. Probably transformation failed");
1375 const mesh_fem &mf = *(mfg ? *mfg : mfn);
1376 const base_vector &U = *(Ug ? *Ug : Un);
1377 GMM_ASSERT1(&(mf.linked_mesh()) == *m,
"Interpolation of a variable "
1378 "on another mesh than the one it is defined on");
1380 pfem pf = mf.fem_of_element(ctx.convex_num());
1381 GMM_ASSERT1(pf,
"Undefined finite element method");
1382 if (ctx.have_pgp()) {
1384 inin.pfps[&mf] = fp_pool(pf, ctx.pgp()->get_ppoint_tab());
1385 ctx.set_pfp(inin.pfps[&mf]);
1392 ga_instruction_interpolate
1393 (base_tensor &tt,
const mesh **m_,
const mesh_fem *mfn_,
1394 const mesh_fem **mfg_,
const base_vector *Un_,
const base_vector **Ug_,
1396 fem_precomp_pool &fp_pool_, ga_instruction_set::interpolate_info &inin_)
1397 : t(tt), m(m_), mfn(mfn_), mfg(mfg_), Un(Un_), Ug(Ug_),
1398 ctx(ctx_), qdim(q), ipt(ipt_), fp_pool(fp_pool_), inin(inin_) {}
1401 struct ga_instruction_interpolate_val :
public ga_instruction_interpolate {
1403 virtual int exec() {
1404 GA_DEBUG_INFO(
"Instruction: interpolated variable value");
1405 ga_instruction_interpolate::exec();
1406 ctx.pf()->interpolation(ctx, coeff, t.as_vector(), dim_type(qdim));
1411 ga_instruction_interpolate_val
1412 (base_tensor &tt,
const mesh **m_,
const mesh_fem *mfn_,
1413 const mesh_fem **mfg_,
const base_vector *Un_,
const base_vector **Ug_,
1415 fem_precomp_pool &fp_pool_, ga_instruction_set::interpolate_info &inin_)
1416 : ga_instruction_interpolate(tt, m_, mfn_, mfg_, Un_, Ug_,ctx_, q, ipt_,
1421 struct ga_instruction_interpolate_grad :
public ga_instruction_interpolate {
1423 virtual int exec() {
1424 GA_DEBUG_INFO(
"Instruction: interpolated variable grad");
1425 ga_instruction_interpolate::exec();
1426 base_matrix v(qdim, ctx.N());
1427 ctx.pf()->interpolation_grad(ctx, coeff, v, dim_type(qdim));
1428 gmm::copy(v.as_vector(), t.as_vector());
1432 ga_instruction_interpolate_grad
1433 (base_tensor &tt,
const mesh **m_,
const mesh_fem *mfn_,
1434 const mesh_fem **mfg_,
const base_vector *Un_,
const base_vector **Ug_,
1436 fem_precomp_pool &fp_pool_, ga_instruction_set::interpolate_info &inin_)
1437 : ga_instruction_interpolate(tt, m_, mfn_, mfg_, Un_, Ug_, ctx_, q, ipt_,
1442 struct ga_instruction_interpolate_hess :
public ga_instruction_interpolate {
1444 virtual int exec() {
1445 GA_DEBUG_INFO(
"Instruction: interpolated variable hessian");
1446 ga_instruction_interpolate::exec();
1447 base_matrix v(qdim, ctx.N()*ctx.N());
1448 ctx.pf()->interpolation_hess(ctx, coeff, v, dim_type(qdim));
1449 gmm::copy(v.as_vector(), t.as_vector());
1453 ga_instruction_interpolate_hess
1454 (base_tensor &tt,
const mesh **m_,
const mesh_fem *mfn_,
1455 const mesh_fem **mfg_,
const base_vector *Un_,
const base_vector **Ug_,
1457 fem_precomp_pool &fp_pool_, ga_instruction_set::interpolate_info &inin_)
1458 : ga_instruction_interpolate(tt, m_, mfn_, mfg_, Un_, Ug_, ctx_, q, ipt_,
1463 struct ga_instruction_interpolate_diverg :
public ga_instruction_interpolate {
1465 virtual int exec() {
1466 GA_DEBUG_INFO(
"Instruction: interpolated variable divergence");
1467 ga_instruction_interpolate::exec();
1468 ctx.pf()->interpolation_diverg(ctx, coeff, t[0]);
1472 ga_instruction_interpolate_diverg
1473 (base_tensor &tt,
const mesh **m_,
const mesh_fem *mfn_,
1474 const mesh_fem **mfg_,
const base_vector *Un_,
const base_vector **Ug_,
1476 fem_precomp_pool &fp_pool_, ga_instruction_set::interpolate_info &inin_)
1477 : ga_instruction_interpolate(tt, m_, mfn_, mfg_, Un_, Ug_, ctx_, q, ipt_,
1482 struct ga_instruction_interpolate_base {
1485 const mesh_fem *mfn, **mfg;
1487 ga_instruction_set::interpolate_info &inin;
1488 fem_precomp_pool &fp_pool;
1490 virtual int exec() {
1491 GMM_ASSERT1(inin.ctx.is_convex_num_valid(),
"No valid element for "
1492 "the transformation. Probably transformation failed");
1493 const mesh_fem &mf = *(mfg ? *mfg : mfn);
1494 GMM_ASSERT1(&(mf.linked_mesh()) == *m,
"Interpolation of a variable "
1495 "on another mesh than the one it is defined on");
1497 pfem pf = mf.fem_of_element(inin.ctx.convex_num());
1498 GMM_ASSERT1(pf,
"Undefined finite element method");
1500 if (inin.ctx.have_pgp()) {
1502 inin.pfps[&mf] = fp_pool(pf, inin.ctx.pgp()->get_ppoint_tab());
1503 inin.ctx.set_pfp(inin.pfps[&mf]);
1505 inin.ctx.set_pf(pf);
1510 ga_instruction_interpolate_base
1511 (
const mesh **m_,
const mesh_fem *mfn_,
const mesh_fem **mfg_,
1512 const size_type &ipt_, ga_instruction_set::interpolate_info &inin_,
1513 fem_precomp_pool &fp_pool_)
1514 : m(m_), mfn(mfn_), mfg(mfg_), ipt(ipt_), inin(inin_),
1515 fp_pool(fp_pool_) {}
1518 struct ga_instruction_interpolate_val_base
1519 :
public ga_instruction_copy_val_base, ga_instruction_interpolate_base {
1521 virtual int exec() {
1522 GA_DEBUG_INFO(
"Instruction: interpolated base value");
1523 ga_instruction_interpolate_base::exec();
1524 inin.ctx.pf()->real_base_value(inin.ctx, ZZ);
1525 return ga_instruction_copy_val_base::exec();
1528 ga_instruction_interpolate_val_base
1529 (base_tensor &t_,
const mesh **m_,
const mesh_fem *mfn_,
1531 ga_instruction_set::interpolate_info &inin_, fem_precomp_pool &fp_pool_)
1532 : ga_instruction_copy_val_base(t_, ZZ, q),
1533 ga_instruction_interpolate_base(m_, mfn_, mfg_, ipt_,
1537 struct ga_instruction_interpolate_grad_base
1538 :
public ga_instruction_copy_grad_base, ga_instruction_interpolate_base {
1540 virtual int exec() {
1541 GA_DEBUG_INFO(
"Instruction: interpolated base grad");
1542 ga_instruction_interpolate_base::exec();
1543 inin.ctx.pf()->real_grad_base_value(inin.ctx, ZZ);
1544 return ga_instruction_copy_grad_base::exec();
1547 ga_instruction_interpolate_grad_base
1548 (base_tensor &t_,
const mesh **m_,
const mesh_fem *mfn_,
1550 ga_instruction_set::interpolate_info &inin_, fem_precomp_pool &fp_pool_)
1551 : ga_instruction_copy_grad_base(t_, ZZ, q),
1552 ga_instruction_interpolate_base(m_, mfn_, mfg_, ipt_,
1556 struct ga_instruction_interpolate_hess_base
1557 :
public ga_instruction_copy_hess_base, ga_instruction_interpolate_base {
1559 virtual int exec() {
1560 GA_DEBUG_INFO(
"Instruction: interpolated base hessian");
1561 ga_instruction_interpolate_base::exec();
1562 inin.ctx.pf()->real_hess_base_value(inin.ctx, ZZ);
1563 return ga_instruction_copy_hess_base::exec();
1566 ga_instruction_interpolate_hess_base
1567 (base_tensor &t_,
const mesh **m_,
const mesh_fem *mfn_,
1569 ga_instruction_set::interpolate_info &inin_, fem_precomp_pool &fp_pool_)
1570 : ga_instruction_copy_hess_base(t_, ZZ, q),
1571 ga_instruction_interpolate_base(m_, mfn_, mfg_, ipt_,
1575 struct ga_instruction_interpolate_diverg_base
1576 :
public ga_instruction_copy_diverg_base, ga_instruction_interpolate_base {
1578 virtual int exec() {
1579 GA_DEBUG_INFO(
"Instruction: interpolated base divergence");
1580 ga_instruction_interpolate_base::exec();
1581 inin.ctx.pf()->real_grad_base_value(inin.ctx, ZZ);
1582 return ga_instruction_copy_diverg_base::exec();
1585 ga_instruction_interpolate_diverg_base
1586 (base_tensor &t_,
const mesh **m_,
const mesh_fem *mfn_,
1588 ga_instruction_set::interpolate_info &inin_, fem_precomp_pool &fp_pool_)
1589 : ga_instruction_copy_diverg_base(t_, ZZ, q),
1590 ga_instruction_interpolate_base(m_, mfn_, mfg_, ipt_,
1595 struct ga_instruction_elementary_trans_base {
1598 pelementary_transformation elemtrans;
1599 const mesh_fem &mf1, &mf2;
1600 const fem_interpolation_context &ctx;
1605 if (icv != ctx.convex_num() || M.size() == 0) {
1606 M.base_resize(m, n);
1607 icv = ctx.convex_num();
1608 elemtrans->give_transformation(mf1, mf2, icv, M);
1610 t_out.mat_reduction(t_in, M, 0);
1613 ga_instruction_elementary_trans_base
1614 (base_tensor &t_, pelementary_transformation e,
const mesh_fem &mf1_,
1615 const mesh_fem &mf2_,
1616 const fem_interpolation_context &ctx_, base_matrix &M_,
size_type &icv_)
1617 : t_out(t_), elemtrans(e), mf1(mf1_), mf2(mf2_), ctx(ctx_),
1621 struct ga_instruction_elementary_trans_val_base
1622 :
public ga_instruction_copy_val_base,
1623 ga_instruction_elementary_trans_base {
1625 virtual int exec() {
1626 GA_DEBUG_INFO(
"Instruction: value of test functions with elementary "
1630 t_in.adjust_sizes(Qmult*ndof, Qmult*Z.sizes()[1]);
1631 ga_instruction_copy_val_base::exec();
1632 do_transformation(t_out.sizes()[0], ndof*Qmult);
1636 ga_instruction_elementary_trans_val_base
1637 (base_tensor &t_,
const base_tensor &Z_,
size_type q,
1638 pelementary_transformation e,
const mesh_fem &mf1_,
const mesh_fem &mf2_,
1639 fem_interpolation_context &ctx_, base_matrix &M_,
size_type &icv_)
1640 : ga_instruction_copy_val_base(t_in, Z_, q),
1641 ga_instruction_elementary_trans_base(t_, e, mf1_, mf2_, ctx_,
1645 struct ga_instruction_elementary_trans_grad_base
1646 :
public ga_instruction_copy_grad_base,
1647 ga_instruction_elementary_trans_base {
1649 virtual int exec() {
1650 GA_DEBUG_INFO(
"Instruction: gradient of test functions with elementary "
1654 t_in.adjust_sizes(Qmult*ndof, Qmult*Z.sizes()[1], Z.sizes()[2]);
1655 ga_instruction_copy_grad_base::exec();
1656 do_transformation(t_out.sizes()[0], ndof*Qmult);
1660 ga_instruction_elementary_trans_grad_base
1661 (base_tensor &t_,
const base_tensor &Z_,
size_type q,
1662 pelementary_transformation e,
const mesh_fem &mf1_,
const mesh_fem &mf2_,
1663 fem_interpolation_context &ctx_, base_matrix &M_,
size_type &icv_)
1664 : ga_instruction_copy_grad_base(t_in, Z_, q),
1665 ga_instruction_elementary_trans_base(t_, e, mf1_, mf2_, ctx_,
1669 struct ga_instruction_elementary_trans_hess_base
1670 :
public ga_instruction_copy_hess_base,
1671 ga_instruction_elementary_trans_base {
1673 virtual int exec() {
1674 GA_DEBUG_INFO(
"Instruction: Hessian of test functions with elementary "
1678 t_in.adjust_sizes(Qmult*ndof, Qmult*Z.sizes()[1], Z.sizes()[2]);
1679 ga_instruction_copy_hess_base::exec();
1680 do_transformation(t_out.sizes()[0], ndof*Qmult);
1684 ga_instruction_elementary_trans_hess_base
1685 (base_tensor &t_,
const base_tensor &Z_,
size_type q,
1686 pelementary_transformation e,
const mesh_fem &mf1_,
const mesh_fem &mf2_,
1687 fem_interpolation_context &ctx_, base_matrix &M_,
size_type &icv_)
1688 : ga_instruction_copy_hess_base(t_in, Z_, q),
1689 ga_instruction_elementary_trans_base(t_, e, mf1_, mf2_, ctx_,
1693 struct ga_instruction_elementary_trans_diverg_base
1694 :
public ga_instruction_copy_diverg_base,
1695 ga_instruction_elementary_trans_base {
1697 virtual int exec() {
1698 GA_DEBUG_INFO(
"Instruction: divergence of test functions with elementary "
1702 t_in.adjust_sizes(Qmult*ndof);
1703 ga_instruction_copy_diverg_base::exec();
1704 do_transformation(t_out.sizes()[0], ndof*Qmult);
1708 ga_instruction_elementary_trans_diverg_base
1709 (base_tensor &t_,
const base_tensor &Z_,
size_type q,
1710 pelementary_transformation e,
const mesh_fem &mf1_,
const mesh_fem &mf2_,
1711 fem_interpolation_context &ctx_, base_matrix &M_,
size_type &icv_)
1712 : ga_instruction_copy_diverg_base(t_in, Z_, q),
1713 ga_instruction_elementary_trans_base(t_, e, mf1_, mf2_, ctx_,
1718 struct ga_instruction_add :
public ga_instruction {
1720 const base_tensor &tc1, &tc2;
1721 virtual int exec() {
1722 GA_DEBUG_INFO(
"Instruction: addition");
1723 GA_DEBUG_ASSERT(t.size() == tc1.size(),
1724 "internal error " << t.size() <<
" != " << tc1.size());
1725 GA_DEBUG_ASSERT(t.size() == tc2.size(),
1726 "internal error " << t.size() <<
" != " << tc2.size());
1727 gmm::add(tc1.as_vector(), tc2.as_vector(), t.as_vector());
1730 ga_instruction_add(base_tensor &t_,
1731 const base_tensor &tc1_,
const base_tensor &tc2_)
1732 : t(t_), tc1(tc1_), tc2(tc2_) {}
1735 struct ga_instruction_add_to :
public ga_instruction {
1737 const base_tensor &tc1;
1738 virtual int exec() {
1739 GA_DEBUG_INFO(
"Instruction: addition");
1740 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"internal error " << t.size()
1741 <<
" incompatible with " << tc1.size());
1742 gmm::add(tc1.as_vector(), t.as_vector());
1745 ga_instruction_add_to(base_tensor &t_,
const base_tensor &tc1_)
1746 : t(t_), tc1(tc1_) {}
1749 struct ga_instruction_add_to_coeff :
public ga_instruction {
1751 const base_tensor &tc1;
1753 virtual int exec() {
1754 GA_DEBUG_INFO(
"Instruction: addition with scale");
1755 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"internal error " << t.size()
1756 <<
" incompatible with " << tc1.size());
1757 gmm::add(gmm::scaled(tc1.as_vector(), coeff), t.as_vector());
1760 ga_instruction_add_to_coeff(base_tensor &t_,
const base_tensor &tc1_,
1761 scalar_type &coeff_)
1762 : t(t_), tc1(tc1_), coeff(coeff_) {}
1765 struct ga_instruction_sub :
public ga_instruction {
1767 const base_tensor &tc1, &tc2;
1768 virtual int exec() {
1769 GA_DEBUG_INFO(
"Instruction: subtraction");
1770 GA_DEBUG_ASSERT(t.size() == tc1.size() && t.size() == tc2.size(),
1772 gmm::add(tc1.as_vector(), gmm::scaled(tc2.as_vector(), scalar_type(-1)),
1776 ga_instruction_sub(base_tensor &t_,
1777 const base_tensor &tc1_,
const base_tensor &tc2_)
1778 : t(t_), tc1(tc1_), tc2(tc2_) {}
1781 struct ga_instruction_opposite :
public ga_instruction {
1783 virtual int exec() {
1784 GA_DEBUG_INFO(
"Instruction: multiplication with -1");
1785 gmm::scale(t.as_vector(), scalar_type(-1));
1788 ga_instruction_opposite(base_tensor &t_) : t(t_) {}
1791 struct ga_instruction_print_tensor :
public ga_instruction {
1793 pga_tree_node pnode;
1794 const fem_interpolation_context &ctx;
1796 virtual int exec() {
1797 GA_DEBUG_INFO(
"Instruction: tensor print");
1798 cout <<
"Print term "; ga_print_node(pnode, cout);
1799 cout <<
" on Gauss point " << ipt <<
"/" << nbpt <<
" of element "
1800 << ctx.convex_num() <<
": " << t << endl;
1803 ga_instruction_print_tensor(base_tensor &t_, pga_tree_node pnode_,
1804 const fem_interpolation_context &ctx_,
1806 : t(t_), pnode(pnode_), ctx(ctx_), nbpt(nbpt_), ipt(ipt_) {}
1809 struct ga_instruction_copy_tensor :
public ga_instruction {
1811 const base_tensor &tc1;
1812 virtual int exec() {
1813 GA_DEBUG_INFO(
"Instruction: tensor copy");
1814 std::copy(tc1.begin(), tc1.end(), t.begin());
1818 ga_instruction_copy_tensor(base_tensor &t_,
const base_tensor &tc1_)
1819 : t(t_), tc1(tc1_) {}
1822 struct ga_instruction_clear_tensor :
public ga_instruction {
1824 virtual int exec() {
1825 GA_DEBUG_INFO(
"Instruction: clear tensor");
1826 std::fill(t.begin(), t.end(), scalar_type(0));
1829 ga_instruction_clear_tensor(base_tensor &t_) : t(t_) {}
1832 struct ga_instruction_copy_tensor_possibly_void :
public ga_instruction {
1834 const base_tensor &tc1;
1835 virtual int exec() {
1836 GA_DEBUG_INFO(
"Instruction: tensor copy possibly void");
1838 gmm::copy(tc1.as_vector(), t.as_vector());
1843 ga_instruction_copy_tensor_possibly_void(base_tensor &t_,
1844 const base_tensor &tc1_)
1845 : t(t_), tc1(tc1_) {}
1848 struct ga_instruction_copy_scalar :
public ga_instruction {
1849 scalar_type &t;
const scalar_type &t1;
1850 virtual int exec() {
1851 GA_DEBUG_INFO(
"Instruction: scalar copy");
1855 ga_instruction_copy_scalar(scalar_type &t_,
const scalar_type &t1_)
1859 struct ga_instruction_copy_vect :
public ga_instruction {
1861 const base_vector &t1;
1862 virtual int exec() {
1863 GA_DEBUG_INFO(
"Instruction: fixed size tensor copy");
1867 ga_instruction_copy_vect(base_vector &t_,
const base_vector &t1_)
1871 struct ga_instruction_trace :
public ga_instruction {
1873 const base_tensor &tc1;
1876 virtual int exec() {
1877 GA_DEBUG_INFO(
"Instruction: Trace");
1878 GA_DEBUG_ASSERT(t.size()*n*n == tc1.size(),
"Wrong sizes");
1880 auto it = t.begin();
1881 auto it1 = tc1.begin();
1882 for (; it != t.end(); ++it, ++it1) {
1885 for (
size_type i = 1; i < n; ++i) { it2 += s; *it += *it2; }
1890 ga_instruction_trace(base_tensor &t_,
const base_tensor &tc1_,
size_type n_)
1891 : t(t_), tc1(tc1_), n(n_) {}
1894 struct ga_instruction_deviator :
public ga_instruction {
1896 const base_tensor &tc1;
1899 virtual int exec() {
1900 GA_DEBUG_INFO(
"Instruction: Deviator");
1901 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
1903 gmm::copy(tc1.as_vector(), t.as_vector());
1907 base_tensor::iterator it = t.begin();
1908 base_tensor::const_iterator it1 = tc1.begin();
1909 for (; j < nb; ++it, ++it1, ++j) {
1911 base_tensor::const_iterator it2 = it1;
1913 for (
size_type i = 1; i < n; ++i) { it2 += s; tr += *it2; }
1914 tr /= scalar_type(n);
1916 base_tensor::iterator it3 = it;
1918 for (
size_type i = 1; i < n; ++i) { it3 += s; *it3 -= tr; }
1923 ga_instruction_deviator(base_tensor &t_,
const base_tensor &tc1_,
1925 : t(t_), tc1(tc1_), n(n_) {}
1928 struct ga_instruction_transpose :
public ga_instruction {
1930 const base_tensor &tc1;
1932 virtual int exec() {
1933 GA_DEBUG_INFO(
"Instruction: transpose");
1934 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
1937 auto it = t.begin();
1949 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
1952 ga_instruction_transpose(base_tensor &t_,
const base_tensor &tc1_,
1954 : t(t_), tc1(tc1_), J(J_), K(K_), I(I_) {}
1957 struct ga_instruction_swap_indices :
public ga_instruction {
1959 const base_tensor &tc1;
1961 virtual int exec() {
1962 GA_DEBUG_INFO(
"Instruction: swap indices");
1963 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
1964 size_type ii1 = t.size() / (nn1*nn2*ii2*ii3);
1966 auto it = t.begin();
1971 size_type ind = j*ii1+k*ii1*nn1+l*ii1*nn1*ii2+i*ii1*nn1*ii2*nn2;
1972 for (
size_type m = 0; m < ii1; ++m, ++it)
1975 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
1978 ga_instruction_swap_indices(base_tensor &t_,
const base_tensor &tc1_,
1981 : t(t_), tc1(tc1_), nn1(n1_), nn2(n2_), ii2(i2_), ii3(i3_) {}
1984 struct ga_instruction_index_move_last :
public ga_instruction {
1986 const base_tensor &tc1;
1988 virtual int exec() {
1989 GA_DEBUG_INFO(
"Instruction: swap indices");
1990 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
1993 auto it = t.begin();
1997 for (
size_type k = 0; k < ii1; ++k, ++it)
2000 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2003 ga_instruction_index_move_last(base_tensor &t_,
const base_tensor &tc1_,
2005 : t(t_), tc1(tc1_), nn(n_), ii2(i2_) {}
2008 struct ga_instruction_transpose_no_test :
public ga_instruction {
2010 const base_tensor &tc1;
2012 virtual int exec() {
2013 GA_DEBUG_INFO(
"Instruction: transpose");
2014 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
2016 auto it = t.begin();
2021 for (
size_type k = 0; k < n2; ++k, ++it)
2022 *it = tc1[s2 + k*n1];
2025 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2028 ga_instruction_transpose_no_test(base_tensor &t_,
const base_tensor &tc1_,
2031 : t(t_), tc1(tc1_), n1(n1_), n2(n2_), nn(nn_) {}
2034 struct ga_instruction_transpose_test :
public ga_instruction {
2036 const base_tensor &tc1;
2037 virtual int exec() {
2038 GA_DEBUG_INFO(
"Instruction: copy tensor and transpose test functions");
2039 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
2040 GA_DEBUG_ASSERT(t.sizes().size() >= 2,
"Wrong sizes");
2042 size_type s1 = t.sizes()[0], s2 = t.sizes()[1], s3 = s1*s2;
2044 base_tensor::iterator it = t.begin();
2047 for (
size_type i = 0; i < s1; ++i, ++it)
2048 *it = tc1[j+s2*i+k*s3];
2051 ga_instruction_transpose_test(base_tensor &t_,
const base_tensor &tc1_)
2052 : t(t_), tc1(tc1_) {}
2055 struct ga_instruction_sym :
public ga_instruction {
2057 const base_tensor &tc1;
2058 virtual int exec() {
2059 GA_DEBUG_INFO(
"Instruction: symmetric part");
2060 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
2062 size_type s1 = t.sizes()[order-2], s2 = t.sizes()[order-1];
2066 base_tensor::iterator it = t.begin() + s*(i + s1*j);
2067 base_tensor::const_iterator it1 = tc1.begin() + s*(i + s1*j),
2068 it1T = tc1.begin() + s*(j + s2*i);
2069 for (
size_type k = 0; k < s; ++k) *it++ = 0.5*(*it1++ + *it1T++);
2073 ga_instruction_sym(base_tensor &t_,
const base_tensor &tc1_)
2074 : t(t_), tc1(tc1_) {}
2077 struct ga_instruction_skew :
public ga_instruction {
2079 const base_tensor &tc1;
2080 virtual int exec() {
2081 GA_DEBUG_INFO(
"Instruction: skew-symmetric part");
2082 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
2084 size_type s1 = t.sizes()[order-2], s2 = t.sizes()[order-1];
2088 base_tensor::iterator it = t.begin() + s*(i + s1*j);
2089 base_tensor::const_iterator it1 = tc1.begin() + s*(i + s1*j),
2090 it1T = tc1.begin() + s*(j + s2*i);
2091 for (
size_type k = 0; k < s; ++k) *it++ = 0.5*(*it1++ - *it1T++);
2095 ga_instruction_skew(base_tensor &t_,
const base_tensor &tc1_)
2096 : t(t_), tc1(tc1_) {}
2099 struct ga_instruction_scalar_add :
public ga_instruction {
2101 const scalar_type &c, &d;
2102 virtual int exec() {
2103 GA_DEBUG_INFO(
"Instruction: scalar addition");
2107 ga_instruction_scalar_add(scalar_type &t_,
const scalar_type &c_,
2108 const scalar_type &d_)
2109 : t(t_), c(c_), d(d_) {}
2112 struct ga_instruction_scalar_sub :
public ga_instruction {
2114 const scalar_type &c, &d;
2115 virtual int exec() {
2116 GA_DEBUG_INFO(
"Instruction: scalar subtraction");
2120 ga_instruction_scalar_sub(scalar_type &t_,
const scalar_type &c_,
2121 const scalar_type &d_)
2122 : t(t_), c(c_), d(d_) {}
2125 struct ga_instruction_scalar_scalar_mult :
public ga_instruction {
2127 const scalar_type &c, &d;
2128 virtual int exec() {
2129 GA_DEBUG_INFO(
"Instruction: scalar multiplication");
2133 ga_instruction_scalar_scalar_mult(scalar_type &t_,
const scalar_type &c_,
2134 const scalar_type &d_)
2135 : t(t_), c(c_), d(d_) {}
2138 struct ga_instruction_scalar_scalar_div :
public ga_instruction {
2140 const scalar_type &c, &d;
2141 virtual int exec() {
2142 GA_DEBUG_INFO(
"Instruction: scalar division");
2146 ga_instruction_scalar_scalar_div(scalar_type &t_,
const scalar_type &c_,
2147 const scalar_type &d_)
2148 : t(t_), c(c_), d(d_) {}
2151 template<
int I>
inline void dax__(base_tensor::iterator &it,
2152 base_tensor::const_iterator &itx,
2153 const scalar_type &a) {
2154 constexpr
int I1 = I/8;
2155 constexpr
int I2 = I - I1*8;
2156 for (
int i=0; i < I1; ++i)
2157 dax__<8>(it, itx , a);
2158 dax__<I2>(it, itx , a);
2160 template<>
inline void dax__<8>(base_tensor::iterator &it,
2161 base_tensor::const_iterator &itx,
2162 const scalar_type &a) {
2172 template<>
inline void dax__<7>(base_tensor::iterator &it,
2173 base_tensor::const_iterator &itx,
2174 const scalar_type &a) {
2183 template<>
inline void dax__<6>(base_tensor::iterator &it,
2184 base_tensor::const_iterator &itx,
2185 const scalar_type &a) {
2193 template<>
inline void dax__<5>(base_tensor::iterator &it,
2194 base_tensor::const_iterator &itx,
2195 const scalar_type &a) {
2202 template<>
inline void dax__<4>(base_tensor::iterator &it,
2203 base_tensor::const_iterator &itx,
2204 const scalar_type &a) {
2210 template<>
inline void dax__<3>(base_tensor::iterator &it,
2211 base_tensor::const_iterator &itx,
2212 const scalar_type &a) {
2217 template<>
inline void dax__<2>(base_tensor::iterator &it,
2218 base_tensor::const_iterator &itx,
2219 const scalar_type &a) {
2223 template<>
inline void dax__<1>(base_tensor::iterator &it,
2224 base_tensor::const_iterator &itx,
2225 const scalar_type &a) {
2228 template<>
inline void dax__<0>(base_tensor::iterator &,
2229 base_tensor::const_iterator &,
2230 const scalar_type &) {}
2233 template<
int I>
inline
2234 void reduc_elem_unrolled__(base_tensor::iterator &it,
2235 base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2237 *it = it1[0] * it2[0];
2238 for (
int i=1; i < I; ++i)
2239 *it += it1[i*s1] * it2[i*s2];
2242 void reduc_elem_unrolled__<9>(base_tensor::iterator &it,
2243 base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2245 *it = it1[0] * it2[0]
2247 + it1[2*s1] * it2[2*s2]
2248 + it1[3*s1] * it2[3*s2]
2249 + it1[4*s1] * it2[4*s2]
2250 + it1[5*s1] * it2[5*s2]
2251 + it1[6*s1] * it2[6*s2]
2252 + it1[7*s1] * it2[7*s2]
2253 + it1[8*s1] * it2[8*s2];
2256 void reduc_elem_unrolled__<8>(base_tensor::iterator &it,
2257 base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2259 *it = it1[0] * it2[0]
2261 + it1[2*s1] * it2[2*s2]
2262 + it1[3*s1] * it2[3*s2]
2263 + it1[4*s1] * it2[4*s2]
2264 + it1[5*s1] * it2[5*s2]
2265 + it1[6*s1] * it2[6*s2]
2266 + it1[7*s1] * it2[7*s2];
2269 void reduc_elem_unrolled__<7>(base_tensor::iterator &it,
2270 base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2272 *it = it1[0] * it2[0]
2274 + it1[2*s1] * it2[2*s2]
2275 + it1[3*s1] * it2[3*s2]
2276 + it1[4*s1] * it2[4*s2]
2277 + it1[5*s1] * it2[5*s2]
2278 + it1[6*s1] * it2[6*s2];
2281 void reduc_elem_unrolled__<6>(base_tensor::iterator &it,
2282 base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2284 *it = it1[0] * it2[0]
2286 + it1[2*s1] * it2[2*s2]
2287 + it1[3*s1] * it2[3*s2]
2288 + it1[4*s1] * it2[4*s2]
2289 + it1[5*s1] * it2[5*s2];
2292 void reduc_elem_unrolled__<5>(base_tensor::iterator &it,
2293 base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2295 *it = it1[0] * it2[0]
2297 + it1[2*s1] * it2[2*s2]
2298 + it1[3*s1] * it2[3*s2]
2299 + it1[4*s1] * it2[4*s2];
2302 void reduc_elem_unrolled__<4>(base_tensor::iterator &it,
2303 base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2305 *it = it1[0] * it2[0]
2307 + it1[2*s1] * it2[2*s2]
2308 + it1[3*s1] * it2[3*s2];
2311 void reduc_elem_unrolled__<3>(base_tensor::iterator &it,
2312 base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2314 *it = it1[0] * it2[0]
2316 + it1[2*s1] * it2[2*s2];
2319 void reduc_elem_unrolled__<2>(base_tensor::iterator &it,
2320 base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2322 *it = it1[0] * it2[0]
2323 + it1[s1] * it2[s2];
2326 void reduc_elem_unrolled__<1>(base_tensor::iterator &it,
2327 base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2329 { *it = it1[0] * it2[0]; }
2332 struct ga_instruction_scalar_mult :
public ga_instruction {
2334 const base_tensor &tc1;
2335 const scalar_type &c;
2336 virtual int exec() {
2337 GA_DEBUG_INFO(
"Instruction: multiplication of a tensor by a scalar " << c);
2338 gmm::copy(gmm::scaled(tc1.as_vector(), c), t.as_vector());
2341 ga_instruction_scalar_mult(base_tensor &t_,
2342 const base_tensor &tc1_,
const scalar_type &c_)
2343 : t(t_), tc1(tc1_), c(c_) {}
2346 struct ga_instruction_scalar_div :
public ga_instruction {
2348 const base_tensor &tc1;
2349 const scalar_type &c;
2350 virtual int exec() {
2351 GA_DEBUG_INFO(
"Instruction: division of a tensor by a scalar");
2352 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
2353 base_tensor::iterator it = t.begin();
2354 base_tensor::const_iterator it1 = tc1.cbegin();
2355 for (; it != t.end(); ++it, ++it1) *it = *it1/c;
2358 ga_instruction_scalar_div(base_tensor &t_,
2359 const base_tensor &tc1_,
const scalar_type &c_)
2360 : t(t_), tc1(tc1_), c(c_) {}
2364 struct ga_instruction_cross_product_tf :
public ga_instruction {
2366 const base_tensor &tc1, &tc2;
2368 virtual int exec() {
2369 GA_DEBUG_INFO(
"Instruction: Cross product with test functions");
2371 size_type n1 = tc1.size() / 3, n2 = tc2.size() / 3, nn=n1*n2;
2372 GA_DEBUG_ASSERT(t.size() == nn*3,
"Bad tensor size for cross product");
2373 size_type mm=2*nn, n1_2 = 2*n1, n2_2 = 2*n2;
2374 base_tensor::iterator it = t.begin();
2375 base_tensor::const_iterator it2 = tc2.cbegin();
2377 for (
size_type i = 0; i < n2; ++i, ++it2) {
2378 base_tensor::const_iterator it1 = tc1.cbegin();
2379 for (
size_type j = 0; j < n1; ++j, ++it, ++it1) {
2380 *it = - it1[n1] *it2[n2_2] + it1[n1_2]*it2[n2];
2381 it[nn] = - it1[n1_2]*it2[0] + it1[0] *it2[n2_2];
2382 it[mm] = - it1[0] *it2[n2] + it1[n1] *it2[0];
2386 for (
size_type i = 0; i < n2; ++i, ++it2) {
2387 base_tensor::const_iterator it1 = tc1.cbegin();
2388 for (
size_type j = 0; j < n1; ++j, ++it, ++it1) {
2389 *it = it1[n1] *it2[n2_2] - it1[n1_2]*it2[n2];
2390 it[nn] = it1[n1_2]*it2[0] - it1[0] *it2[n2_2];
2391 it[mm] = it1[0] *it2[n2] - it1[n1] *it2[0];
2397 ga_instruction_cross_product_tf(base_tensor &t_,
2398 const base_tensor &tc1_,
2399 const base_tensor &tc2_,
bool inv_)
2400 : t(t_), tc1(tc1_), tc2(tc2_), inv(inv_) {}
2404 struct ga_instruction_cross_product :
public ga_instruction {
2406 const base_tensor &tc1, &tc2;
2407 virtual int exec() {
2408 GA_DEBUG_INFO(
"Instruction: Cross product with test functions");
2409 GA_DEBUG_ASSERT(t.size() == 3 && tc1.size() == 3 && tc2.size() == 3,
2410 "Bad tensor size for cross product");
2411 t[0] = tc1[1]*tc2[2] - tc1[2]*tc2[1];
2412 t[1] = tc1[2]*tc2[0] - tc1[0]*tc2[2];
2413 t[2] = tc1[0]*tc2[1] - tc1[1]*tc2[0];
2416 ga_instruction_cross_product(base_tensor &t_,
2417 const base_tensor &tc1_,
const base_tensor &tc2_)
2418 : t(t_), tc1(tc1_), tc2(tc2_) {}
2424 struct ga_instruction_dotmult :
public ga_instruction {
2426 const base_tensor &tc1, &tc2;
2427 virtual int exec() {
2428 GA_DEBUG_INFO(
"Instruction: componentwise multiplication");
2429 size_type s2 = tc2.size(), s1_1 = tc1.size() / s2;
2430 GA_DEBUG_ASSERT(t.size() == s1_1*s2,
"Wrong sizes");
2432 base_tensor::iterator it = t.begin();
2434 for (
size_type m = 0; m < s1_1; ++m, ++it)
2435 *it = tc1[m+s1_1*i] * tc2[i];
2438 ga_instruction_dotmult(base_tensor &t_,
2439 const base_tensor &tc1_,
const base_tensor &tc2_)
2440 : t(t_), tc1(tc1_), tc2(tc2_) {}
2443 struct ga_instruction_dotdiv :
public ga_instruction {
2445 const base_tensor &tc1, &tc2;
2446 virtual int exec() {
2447 GA_DEBUG_INFO(
"Instruction: componentwise division");
2448 size_type s2 = tc2.size(), s1_1 = tc1.size() / s2;
2449 GA_DEBUG_ASSERT(t.size() == s1_1*s2,
"Wrong sizes");
2451 base_tensor::iterator it = t.begin();
2453 for (
size_type m = 0; m < s1_1; ++m, ++it)
2454 *it = tc1[m+s1_1*i] / tc2[i];
2457 ga_instruction_dotdiv(base_tensor &t_,
2458 const base_tensor &tc1_,
const base_tensor &tc2_)
2459 : t(t_), tc1(tc1_), tc2(tc2_) {}
2463 struct ga_instruction_dotmult_spec :
public ga_instruction {
2465 const base_tensor &tc1, &tc2;
2466 virtual int exec() {
2467 GA_DEBUG_INFO(
"Instruction: specific componentwise multiplication");
2468 size_type s2_1 = tc2.sizes()[0], s2_2 = tc2.size() / s2_1;
2471 base_tensor::iterator it = t.begin();
2474 for (
size_type m = 0; m < s1_1; ++m, ++it)
2475 *it = tc1[m+s1_1*i] * tc2[n+s2_1*i];
2476 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2479 ga_instruction_dotmult_spec(base_tensor &t_,
2480 const base_tensor &tc1_,
const base_tensor &tc2_)
2481 : t(t_), tc1(tc1_), tc2(tc2_) {}
2485 struct ga_instruction_contract_1_1 :
public ga_instruction {
2487 const base_tensor &tc1;
2489 virtual int exec() {
2490 GA_DEBUG_INFO(
"Instruction: single contraction on a single tensor");
2492 size_type ii1 = tc1.size() / (nn*nn*ii2*ii3);
2494 base_tensor::iterator it = t.begin();
2497 for (
size_type k = 0; k < ii1; ++k, ++it) {
2498 *it = scalar_type(0);
2499 size_type pre_ind = k+j*ii1*nn+i*ii1*nn*ii2*nn;
2501 *it += tc1[pre_ind+n*ii1+n*ii1*nn*ii2];
2504 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2507 ga_instruction_contract_1_1(base_tensor &t_,
const base_tensor &tc1_,
2509 : t(t_), tc1(tc1_), nn(n_), ii2(i2_), ii3(i3_) {}
2513 struct ga_instruction_contract_2_1 :
public ga_instruction {
2515 const base_tensor &tc1, &tc2;
2517 virtual int exec() {
2518 GA_DEBUG_INFO(
"Instruction: single contraction on two tensors");
2520 size_type ift1 = tc1.size() / (nn*ii1*ii2);
2521 size_type ift2 = tc2.size() / (nn*ii3*ii4);
2523 base_tensor::iterator it = t.begin();
2529 for (
size_type q = 0; q < ift1; ++q, ++it) {
2530 *it = scalar_type(0);
2531 size_type ind1 = q+l*ift1+k*ift1*ii1*nn;
2532 size_type ind2 = p+j*ift2+i*ift2*ii3*nn;
2534 *it += tc1[ind1+n*ift1*ii1] * tc2[ind2+n*ift2*ii3];
2537 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2540 ga_instruction_contract_2_1(base_tensor &t_,
2541 const base_tensor &tc1_,
const base_tensor &tc2_,
2544 : t(t_), tc1(tc1_), tc2(tc2_), nn(n_),
2545 ii1(i1_), ii2(i2_), ii3(i3_), ii4(i4_) {}
2549 struct ga_instruction_contract_2_1_rev :
public ga_instruction {
2551 const base_tensor &tc1, &tc2;
2553 virtual int exec() {
2554 GA_DEBUG_INFO(
"Instruction: single contraction on two tensors");
2556 size_type ift1 = tc1.size() / (nn*ii1*ii2);
2557 size_type ift2 = tc2.size() / (nn*ii3*ii4);
2559 base_tensor::iterator it = t.begin();
2565 for (
size_type p = 0; p < ift2; ++p, ++it) {
2566 *it = scalar_type(0);
2567 size_type ind1 = q+l*ift1+k*ift1*ii1*nn;
2568 size_type ind2 = p+j*ift2+i*ift2*ii3*nn;
2570 *it += tc1[ind1+n*ift1*ii1] * tc2[ind2+n*ift2*ii3];
2573 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2576 ga_instruction_contract_2_1_rev(base_tensor &t_,
2577 const base_tensor &tc1_,
const base_tensor &tc2_,
2580 : t(t_), tc1(tc1_), tc2(tc2_), nn(n_),
2581 ii1(i1_), ii2(i2_), ii3(i3_), ii4(i4_) {}
2585 struct ga_instruction_contract_2_2 :
public ga_instruction {
2587 const base_tensor &tc1, &tc2;
2588 size_type nn1, nn2, ii1, ii2, ii3, ii4, ii5, ii6;
2590 virtual int exec() {
2591 GA_DEBUG_INFO(
"Instruction: single contraction on two tensors");
2593 size_type ift1 = tc1.size() / (nn1*nn2*ii1*ii2*ii3);
2594 size_type ift2 = tc2.size() / (nn1*nn2*ii3*ii4*ii5);
2596 size_type sn1 = ift2*ii4, sn2 = ift2*ii4*nn1*ii5;
2597 if (inv_tc2) std::swap(sn1, sn2);
2599 base_tensor::iterator it = t.begin();
2607 for (
size_type s = 0; s < ift1; ++s, ++it) {
2608 *it = scalar_type(0);
2610 = s+q*ift1+p*ift1*ii1*nn1+l*ift1*ii1*nn1*ii2*nn2;
2612 = r+k*ift2+j*ift2*ii4*nn1+i*ift2*ii4*nn1*ii5*nn2;
2615 *it += tc1[ind1+n1*ift1*ii1+n2*ift1*ii1*nn1*ii2]
2616 * tc2[ind2+n1*sn1+n2*sn2];
2619 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2622 ga_instruction_contract_2_2(base_tensor &t_,
2623 const base_tensor &tc1_,
const base_tensor &tc2_,
2628 : t(t_), tc1(tc1_), tc2(tc2_), nn1(n1_), nn2(n2_),
2629 ii1(i1_), ii2(i2_), ii3(i3_), ii4(i4_), ii5(i5_), ii6(i6_),
2634 struct ga_instruction_contract_2_2_rev :
public ga_instruction {
2636 const base_tensor &tc1, &tc2;
2637 size_type nn1, nn2, ii1, ii2, ii3, ii4, ii5, ii6;
2639 virtual int exec() {
2640 GA_DEBUG_INFO(
"Instruction: single contraction on two tensors");
2642 size_type ift1 = tc1.size() / (nn1*nn2*ii1*ii2*ii3);
2643 size_type ift2 = tc2.size() / (nn1*nn2*ii3*ii4*ii5);
2645 size_type sn1 = ift2*ii4, sn2 = ift2*ii4*nn1*ii5;
2646 if (inv_tc2) std::swap(sn1, sn2);
2648 base_tensor::iterator it = t.begin();
2656 for (
size_type r = 0; r < ift2; ++r, ++it) {
2657 *it = scalar_type(0);
2659 = s+q*ift1+p*ift1*ii1*nn1+l*ift1*ii1*nn1*ii2*nn2;
2661 = r+k*ift2+j*ift2*ii4*nn1+i*ift2*ii4*nn1*ii5*nn2;
2664 *it += tc1[ind1+n1*ift1*ii1+n2*ift1*ii1*nn1*ii2]
2665 * tc2[ind2+n1*sn1+n2*sn2];
2668 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2671 ga_instruction_contract_2_2_rev(base_tensor &t_,
2672 const base_tensor &tc1_,
const base_tensor &tc2_,
2677 : t(t_), tc1(tc1_), tc2(tc2_), nn1(n1_), nn2(n2_),
2678 ii1(i1_), ii2(i2_), ii3(i3_), ii4(i4_), ii5(i5_), ii6(i6_),
2684 struct ga_instruction_matrix_mult :
public ga_instruction {
2686 const base_tensor &tc1, &tc2;
2688 virtual int exec() {
2689 GA_DEBUG_INFO(
"Instruction: order one contraction "
2690 "(dot product or matrix multiplication)");
2693 #if defined(GA_USES_BLAS)
2695 const BLAS_INT M_=BLAS_INT(M), J_=BLAS_INT(J), K_=BLAS_INT(K);
2696 constexpr
char notrans =
'N';
2697 constexpr scalar_type one(1), zero(0);
2698 gmm::dgemm_(¬rans, ¬rans, &M_, &K_, &J_, &one,
2699 &(tc1[0]), &M_, &(tc2[0]), &J_, &zero, &(t[0]), &M_);
2703 auto it = t.begin();
2704 if (M==2 && J==2 && K == 2) {
2705 *it++ = tc1[0]*tc2[0] + tc1[2]*tc2[1];
2706 *it++ = tc1[1]*tc2[0] + tc1[3]*tc2[1];
2707 *it++ = tc1[0]*tc2[2] + tc1[2]*tc2[3];
2708 *it++ = tc1[1]*tc2[2] + tc1[3]*tc2[3];
2709 }
else if (M==3 && J==3 && K == 3) {
2710 *it++ = tc1[0]*tc2[0] + tc1[3]*tc2[1] + tc1[6]*tc2[2];
2711 *it++ = tc1[1]*tc2[0] + tc1[4]*tc2[1] + tc1[7]*tc2[2];
2712 *it++ = tc1[2]*tc2[0] + tc1[5]*tc2[1] + tc1[8]*tc2[2];
2713 *it++ = tc1[0]*tc2[3] + tc1[3]*tc2[4] + tc1[6]*tc2[5];
2714 *it++ = tc1[1]*tc2[3] + tc1[4]*tc2[4] + tc1[7]*tc2[5];
2715 *it++ = tc1[2]*tc2[3] + tc1[5]*tc2[4] + tc1[8]*tc2[5];
2716 *it++ = tc1[0]*tc2[6] + tc1[3]*tc2[7] + tc1[6]*tc2[8];
2717 *it++ = tc1[1]*tc2[6] + tc1[4]*tc2[7] + tc1[7]*tc2[8];
2718 *it++ = tc1[2]*tc2[6] + tc1[5]*tc2[7] + tc1[8]*tc2[8];
2721 for (
size_type m = 0; m < M; ++m, ++it) {
2722 *it = scalar_type(0);
2724 *it += tc1[m+M*j] * tc2[j+J*k];
2727 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2731 ga_instruction_matrix_mult(base_tensor &t_,
2732 const base_tensor &tc1_,
2734 : t(t_), tc1(tc1_), tc2(tc2_), J(J_) {}
2738 struct ga_instruction_matrix_mult_spec :
public ga_instruction {
2740 const base_tensor &tc1, &tc2;
2743 virtual int exec() {
2744 GA_DEBUG_INFO(
"Instruction: specific order one contraction "
2745 "(dot product or matrix multiplication)");
2746 const size_type MI = tc1.size() / J, M = MI / I,
2747 NJ = tc2.size() / K, N = NJ / J;
2748 #if defined(GA_USES_BLAS)
2749 const BLAS_INT J_ = BLAS_INT(J), M_ = BLAS_INT(M), N_ = BLAS_INT(N),
2751 constexpr
char notrans =
'N', trans =
'T';
2752 constexpr scalar_type one(1), zero(0);
2754 auto it = t.begin();
2756 for (
size_type i = 0; i < I; ++i, it += MN)
2757 gmm::dgemm_(¬rans, &trans, &M_, &N_, &J_, &one,
2758 &(tc1[M*i]), &MI_, &(tc2[NJ*k]), &N_, &zero,
2760 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2762 auto it = t.begin();
2766 for (
size_type m = 0; m < M; ++m, ++it) {
2767 *it = scalar_type(0);
2769 *it += tc1[m+M*i+MI*j] * tc2[n+N*j+NJ*k];
2771 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2775 ga_instruction_matrix_mult_spec(base_tensor &t_,
2776 const base_tensor &tc1_,
2777 const base_tensor &tc2_,
2779 : t(t_), tc1(tc1_), tc2(tc2_), J(J_), I(I_), K(K_) {}
2783 struct ga_instruction_matrix_mult_spec2 :
public ga_instruction {
2785 const base_tensor &tc1, &tc2;
2788 virtual int exec() {
2789 GA_DEBUG_INFO(
"Instruction: specific order one contraction "
2790 "(dot product or matrix multiplication)");
2792 NJ = tc2.size() / K, N = NJ / J;
2793 #if defined(GA_USES_BLAS)
2794 const BLAS_INT J_ = BLAS_INT(J), MI_ = BLAS_INT(MI), N_ = BLAS_INT(N);
2795 constexpr
char notrans =
'N', trans =
'T';
2796 constexpr scalar_type one(1), zero(0);
2798 auto it = t.begin();
2799 for (
size_type k = 0; k < K; ++k, it += NMI)
2800 gmm::dgemm_(¬rans, &trans, &N_, &MI_, &J_, &one,
2801 &(tc2[NJ*k]), &N_, &(tc1[0]), &MI_, &zero,
2803 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2805 auto it = t.begin();
2808 for (
size_type n = 0; n < N; ++n, ++it) {
2809 *it = scalar_type(0);
2811 *it += tc1[mi+MI*j] * tc2[n+N*j+NJ*k];
2813 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
2817 ga_instruction_matrix_mult_spec2(base_tensor &t_,
2818 const base_tensor &tc1_,
2819 const base_tensor &tc2_,
2821 : t(t_), tc1(tc1_), tc2(tc2_), J(J_), I(I_), K(K_) {}
2825 struct ga_instruction_contraction :
public ga_instruction {
2827 const base_tensor &tc1, &tc2;
2829 virtual int exec() {
2830 GA_DEBUG_INFO(
"Instruction: contraction operation of size " << I);
2833 GA_DEBUG_ASSERT(t.size() == N*M,
"Internal error");
2834 #if defined(GA_USES_BLAS)
2836 BLAS_INT N_ = BLAS_INT(N), I_ = BLAS_INT(I), M_ = BLAS_INT(M);
2837 char notrans =
'N', trans =
'T';
2838 static const scalar_type one(1), zero(0);
2839 gmm::dgemm_(¬rans, &trans, &M_, &N_, &I_, &one,
2840 &(tc2[0]), &M_, &(tc1[0]), &N_, &zero, &(t[0]), &M_);
2844 auto it1=tc1.cbegin(), it2=tc2.cbegin(), it2end=it2+M;
2846 for (
auto it = t.begin(); it != t.end(); ++it) {
2847 reduc_elem_unrolled__<7>(it, it1, it2, N, M);
2848 if (++it2 == it2end) { it2 = tc2.cbegin(), ++it1; }
2851 for (
auto it = t.begin(); it != t.end(); ++it) {
2852 reduc_elem_unrolled__<8>(it, it1, it2, N, M);
2853 if (++it2 == it2end) { it2 = tc2.cbegin(), ++it1; }
2856 for (
auto it = t.begin(); it != t.end(); ++it) {
2857 reduc_elem_unrolled__<9>(it, it1, it2, N, M);
2858 if (++it2 == it2end) { it2 = tc2.cbegin(), ++it1; }
2861 for (
auto it = t.begin(); it != t.end(); ++it) {
2862 reduc_elem_unrolled__<10>(it, it1, it2, N, M);
2863 if (++it2 == it2end) { it2 = tc2.cbegin(), ++it1; }
2866 for (
auto it = t.begin(); it != t.end(); ++it) {
2867 auto it11 = it1, it22 = it2;
2868 scalar_type a = (*it11) * (*it22);
2870 { it11 += N; it22 += M; a += (*it11) * (*it22); }
2872 if (++it2 == it2end) { it2 = tc2.cbegin(), ++it1; }
2885 ga_instruction_contraction(base_tensor &t_,
2886 const base_tensor &tc1_,
2888 : t(t_), tc1(tc1_), tc2(tc2_), I(I_) {}
2892 struct ga_instruction_contraction_opt0_2 :
public ga_instruction {
2894 const base_tensor &tc1, &tc2;
2896 virtual int exec() {
2897 GA_DEBUG_INFO(
"Instruction: contraction operation of size " << n*q <<
2898 " optimized for vectorized second tensor of type 2");
2899 size_type nn = n*q, s1 = tc1.size()/nn, s2 = tc2.size()/nn, s2_q = s2/q;
2901 GA_DEBUG_ASSERT(t.size() == s1*s2,
"Internal error");
2903 auto it = t.begin();
2904 auto it1 = tc1.cbegin();
2905 for (
size_type i = 0; i < s1; ++i, ++it1) {
2906 auto it2 = tc2.cbegin();
2910 for (
size_type l = 0; l < q; ++l, ++it) {
2912 auto ittt1 = itt1, ittt2 = it2;
2913 *it = *ittt1 * (*ittt2);
2915 ittt1 += s1_qq, ittt2 += s2_qq; *it += *ittt1 * (*ittt2);
2926 ga_instruction_contraction_opt0_2(base_tensor &t_,
2927 const base_tensor &tc1_,
2928 const base_tensor &tc2_,
2930 : t(t_), tc1(tc1_), tc2(tc2_), n(n_), q(q_) {}
2935 struct ga_instruction_contraction_opt0_2_unrolled :
public ga_instruction {
2937 const base_tensor &tc1, &tc2;
2939 virtual int exec() {
2940 GA_DEBUG_INFO(
"Instruction: unrolled contraction of size " << N*q <<
2941 " optimized for vectorized second tensor of type 2");
2942 size_type nn = N*q, s1 = tc1.size()/nn, s2 = tc2.size()/nn, s2_q = s2/q;
2944 GA_DEBUG_ASSERT(t.size() == s1*s2,
"Internal error");
2946 auto it = t.begin();
2947 auto it1 = tc1.cbegin();
2948 for (
size_type i = 0; i < s1; ++i, ++it1) {
2949 auto it2 = tc2.cbegin();
2953 for (
size_type l = 0; l < q; ++l, ++it) {
2955 auto ittt1 = itt1, ittt2 = it2;
2956 *it = *ittt1 * (*ittt2);
2958 ittt1 += s1_qq, ittt2 += s2_qq; *it += *ittt1 * (*ittt2);
2965 ga_instruction_contraction_opt0_2_unrolled(base_tensor &t_,
2966 const base_tensor &tc1_,
2967 const base_tensor &tc2_,
2969 : t(t_), tc1(tc1_), tc2(tc2_), q(q_) {}
2973 template <
int N,
int Q>
2974 struct ga_instruction_contraction_opt0_2_dunrolled :
public ga_instruction {
2976 const base_tensor &tc1, &tc2;
2977 virtual int exec() {
2978 GA_DEBUG_INFO(
"Instruction: unrolled contraction of size " << N*Q
2979 <<
" optimized for vectorized second tensor of type 2");
2980 size_type s1 = tc1.size()/(N*Q), s2 = tc2.size()/(N*Q), s2_q = s2/Q;
2982 GA_DEBUG_ASSERT(t.size() == s1*s2,
"Internal error");
2984 auto it = t.begin();
2985 auto it1 = tc1.cbegin();
2986 for (
size_type i = 0; i < s1; ++i, ++it1) {
2987 auto it2 = tc2.cbegin();
2991 for (
size_type l = 0; l < Q; ++l, ++it) {
2993 auto ittt1 = itt1, ittt2 = it2;
2994 *it = *ittt1 * (*ittt2);
2996 ittt1 += s1_qq, ittt2 += s2_qq; *it += *ittt1 * (*ittt2);
3003 ga_instruction_contraction_opt0_2_dunrolled(base_tensor &t_,
3004 const base_tensor &tc1_,
3005 const base_tensor &tc2_)
3006 : t(t_), tc1(tc1_), tc2(tc2_) {}
3010 struct ga_instruction_contraction_opt2_0 :
public ga_instruction {
3012 const base_tensor &tc1, &tc2;
3014 virtual int exec() {
3015 GA_DEBUG_INFO(
"Instruction: contraction operation of size " << n*q <<
3016 " optimized for vectorized second tensor of type 2");
3017 size_type nn = n*q, s1 = tc1.size()/nn, s2 = tc2.size()/nn;
3018 size_type s1_q = s1/q, s1_qq = s1*q, s2_qq = s2*q;
3019 GA_DEBUG_ASSERT(t.size() == s1*s2,
"Internal error");
3021 auto it = t.begin();
3023 auto it1 = tc1.cbegin() + i*q;
3025 auto it2 = tc2.cbegin() + l*s2;
3026 for (
size_type j = 0; j < s2; ++j, ++it, ++it2) {
3027 auto itt1 = it1, itt2 = it2;
3028 *it = *itt1 * (*itt2);
3030 itt1 += s1_qq, itt2 += s2_qq; *it += *itt1 * (*itt2);
3037 ga_instruction_contraction_opt2_0(base_tensor &t_,
3038 const base_tensor &tc1_,
3039 const base_tensor &tc2_,
3041 : t(t_), tc1(tc1_), tc2(tc2_), n(n_), q(q_) { }
3046 struct ga_instruction_contraction_opt2_0_unrolled :
public ga_instruction {
3048 const base_tensor &tc1, &tc2;
3050 virtual int exec() {
3051 GA_DEBUG_INFO(
"Instruction: unrolled contraction of size " << N*q
3052 <<
" optimized for vectorized second tensor of type 2");
3053 size_type nn = N*q, s1 = tc1.size()/nn, s2 = tc2.size()/nn;
3054 size_type s1_q = s1/q, s1_qq = s1*q, s2_qq = s2*q;
3055 GA_DEBUG_ASSERT(t.size() == s1*s2,
"Internal error");
3057 auto it = t.begin();
3058 auto it1 = tc1.cbegin();
3059 for (
size_type i = 0; i < s1_q; ++i, it1 += q) {
3061 auto it2 = tc2.cbegin() + l*s2;
3062 for (
size_type j = 0; j < s2; ++j, ++it, ++it2) {
3063 auto itt1 = it1, itt2 = it2;
3064 *it = *itt1 * (*itt2);
3066 itt1 += s1_qq, itt2 += s2_qq; *it += *itt1 * (*itt2);
3073 ga_instruction_contraction_opt2_0_unrolled(base_tensor &t_,
3074 const base_tensor &tc1_,
3075 const base_tensor &tc2_,
3077 : t(t_), tc1(tc1_), tc2(tc2_), q(q_) {}
3081 template <
int N,
int Q>
3082 struct ga_instruction_contraction_opt2_0_dunrolled :
public ga_instruction {
3084 const base_tensor &tc1, &tc2;
3085 virtual int exec() {
3086 GA_DEBUG_INFO(
"Instruction: unrolled contraction of size " << N*Q
3087 <<
" optimized for vectorized second tensor of type 2");
3088 size_type s1 = tc1.size()/(N*Q), s2 = tc2.size()/(N*Q);
3089 size_type s1_q = s1/Q, s1_qq = s1*Q, s2_qq = s2*Q;
3090 GA_DEBUG_ASSERT(t.size() == s1*s2,
"Internal error");
3092 auto it = t.begin();
3093 auto it1 = tc1.cbegin();
3094 for (
size_type i = 0; i < s1_q; ++i, it1 += Q) {
3096 auto it2 = tc2.cbegin() + l*s2;
3097 for (
size_type j = 0; j < s2; ++j, ++it, ++it2) {
3098 auto itt1 = it1, itt2 = it2;
3099 *it = *itt1 * (*itt2);
3101 itt1 += s1_qq, itt2 += s2_qq; *it += *itt1 * (*itt2);
3108 ga_instruction_contraction_opt2_0_dunrolled(base_tensor &t_,
3109 const base_tensor &tc1_,
3110 const base_tensor &tc2_)
3111 : t(t_), tc1(tc1_), tc2(tc2_) {}
3115 struct ga_instruction_contraction_opt0_1 :
public ga_instruction {
3117 const base_tensor &tc1, &tc2;
3119 virtual int exec() {
3120 GA_DEBUG_INFO(
"Instruction: contraction operation of size " << nn <<
3121 " optimized for vectorized second tensor of type 1");
3122 size_type ss1=tc1.size(), s1 = ss1/nn, s2=tc2.size()/nn, s2_n=s2/nn;
3124 auto it = t.begin();
3125 auto it1 = tc1.cbegin();
3126 for (
size_type i = 0; i < s1; ++i, ++it1) {
3127 auto it2 = tc2.cbegin();
3131 *it++ = (*itt1) * (*it2);
3133 { itt1 += s1; *it++ = (*itt1) * (*it2); }
3138 ga_instruction_contraction_opt0_1(base_tensor &t_,
3139 const base_tensor &tc1_,
3140 const base_tensor &tc2_,
3142 : t(t_), tc1(tc1_), tc2(tc2_), nn(n_) {}
3145 template<
int N>
inline void reduc_elem_unrolled_opt1_
3146 (
const base_vector::iterator &it,
const base_vector::const_iterator &it1,
3148 it[N-1] = it1[(N-1)*s1] * a;
3149 reduc_elem_unrolled_opt1_<N-1>(it, it1, a, s1);
3151 template<>
inline void reduc_elem_unrolled_opt1_<1>
3152 (
const base_vector::iterator &it,
const base_vector::const_iterator &it1,
3154 { *it = (*it1) * a; }
3158 struct ga_instruction_contraction_opt0_1_unrolled :
public ga_instruction {
3160 const base_tensor &tc1, &tc2;
3161 virtual int exec() {
3162 GA_DEBUG_INFO(
"Instruction: unrolled contraction operation of size " << N
3163 <<
" optimized for vectorized second tensor of type 1");
3164 size_type s1 = tc1.size()/N, s2 = tc2.size()/N;
3165 auto it = t.begin();
3166 auto it1 = tc1.cbegin();
3167 for (
size_type i = 0; i < s1; ++i, ++it1) {
3168 auto it2 = tc2.cbegin(), it2e = it2 + s2;
3169 for (; it2 != it2e; it2 += N, it += N)
3170 reduc_elem_unrolled_opt1_<N>(it, it1, *it2, s1);
3174 ga_instruction_contraction_opt0_1_unrolled(base_tensor &t_,
3175 const base_tensor &tc1_,
3176 const base_tensor &tc2_)
3177 : t(t_), tc1(tc1_), tc2(tc2_) {}
3181 struct ga_instruction_contraction_opt1_1 :
public ga_instruction {
3183 const base_tensor &tc1, &tc2;
3185 virtual int exec() {
3186 GA_DEBUG_INFO(
"Instruction: contraction operation of size " << nn <<
3187 " optimized for both vectorized tensor of type 1");
3188 size_type s1 = tc1.size()/nn, s2 = tc2.size()/nn, s2_1 = s2+1;
3189 GA_DEBUG_ASSERT(t.size() == s2*s1,
"Internal error");
3193 auto it2 = tc2.cbegin();
3196 auto it1 = tc1.cbegin();
3197 auto it = t.begin() + j*nn;
3199 if (i) { it1 += nn, it += s2*nn; }
3200 scalar_type a = (*it1) * (*it2);
3202 *itt = a; itt += s2_1; *itt = a;
3203 for (
size_type k = 2; k < nn; ++k) { itt += s2_1; *itt = a; }
3208 ga_instruction_contraction_opt1_1(base_tensor &t_,
3209 const base_tensor &tc1_,
3211 : t(t_), tc1(tc1_), tc2(tc2_), nn(n_) {}
3219 struct ga_instruction_contraction_opt2_2 :
public ga_instruction {
3221 const base_tensor &tc1, &tc2;
3223 virtual int exec() {
3224 GA_DEBUG_INFO(
"Instruction: contraction operation of size " << n*q <<
3225 " optimized for both vectorized tensor of type 2");
3226 size_type nn = n*q, s1 = tc1.size()/nn, s2 = tc2.size()/nn;
3227 size_type ss1 = s1/q, ss2 = s2/q, s1_qq = s1*q, s2_qq = s2*q;
3229 GA_DEBUG_ASSERT(t.size() == s2*s1,
"Internal error");
3233 auto it2 = tc2.cbegin() + j*q;
3234 auto it = t.begin() + j*q;
3235 for (
size_type i = 0; i < ss1; ++i, it += s2_qq) {
3236 auto it1 = tc1.cbegin() + i*q;
3237 scalar_type a = scalar_type(0);
3239 a += it1[m*s1_qq] * it2[m*s2_qq];
3242 for (
size_type k = 1; k < q; ++k) { itt += s2_1; *itt = a; }
3247 ga_instruction_contraction_opt2_2(base_tensor &t_,
3248 const base_tensor &tc1_,
3249 const base_tensor &tc2_,
3251 : t(t_), tc1(tc1_), tc2(tc2_), q(q_), n(n_) {}
3257 struct ga_instruction_contraction_unrolled
3258 :
public ga_instruction {
3260 const base_tensor &tc1, &tc2;
3261 virtual int exec() {
3262 GA_DEBUG_INFO(
"Instruction: unrolled contraction operation of size " << I);
3263 size_type N = tc1.size()/I, M = tc2.size()/I;
3264 GA_DEBUG_ASSERT(t.size() == N*M,
"Internal error, " << t.size()
3265 <<
" != " << N <<
"*" << M);
3266 auto it1=tc1.cbegin(), it2=tc2.cbegin(), it2end=it2+M;
3267 for (
auto it = t.begin(); it != t.end(); ++it) {
3268 reduc_elem_unrolled__<I>(it, it1, it2, N, M);
3269 if (++it2 == it2end) { it2 = tc2.cbegin(), ++it1; }
3273 ga_instruction_contraction_unrolled(base_tensor &t_,
3274 const base_tensor &tc1_,
3275 const base_tensor &tc2_)
3276 : t(t_), tc1(tc1_), tc2(tc2_) {}
3281 struct ga_instruction_contraction_unrolled<1> :
public ga_instruction {
3283 const base_tensor &tc1, &tc2;
3284 virtual int exec() {
3285 GA_DEBUG_INFO(
"Instruction: unrolled contraction operation of size 1");
3286 size_type N = tc1.size(), M = tc2.size();
3287 GA_DEBUG_ASSERT(t.size() == N*M,
"Internal error, " << t.size()
3288 <<
" != " << N <<
"*" << M);
3290 base_tensor::iterator it = t.begin();
3291 base_tensor::const_iterator it1 = tc1.cbegin();
3294 for (
size_type n = 0; n < N; ++n, ++it1)
3295 *it++ = tc2[0] * (*it1);
3298 for (
size_type n = 0; n < N; ++n, ++it1) {
3299 base_tensor::const_iterator it2 = tc2.cbegin();
3300 dax__<2>(it, it2, *it1);
3304 for (
size_type n = 0; n < N; ++n, ++it1) {
3305 base_tensor::const_iterator it2 = tc2.cbegin();
3306 dax__<4>(it, it2, *it1);
3310 for (
size_type n = 0; n < N; ++n, ++it1) {
3311 base_tensor::const_iterator it2 = tc2.cbegin();
3312 dax__<4>(it, it2, *it1);
3316 const int M1 = int(M)/4;
3317 const int M2 = int(M) - M1*4;
3318 for (
size_type n = 0; n < N; ++n, ++it1) {
3319 base_tensor::const_iterator it2 = tc2.cbegin();
3320 for (
int mm=0; mm < M1; ++mm)
3321 dax__<4>(it, it2, *it1);
3322 for (
int mm=0; mm < M2; ++mm)
3323 *it++ = (*it2++) * (*it1);
3328 ga_instruction_contraction_unrolled(base_tensor &t_,
3329 const base_tensor &tc1_,
3330 const base_tensor &tc2_)
3331 : t(t_), tc1(tc1_), tc2(tc2_) {}
3334 template<
int N,
int S2>
3335 inline void reduc_elem_d_unrolled__(base_tensor::iterator &it,
3336 base_tensor::const_iterator &it1,
3337 base_tensor::const_iterator &it2,
3339 reduc_elem_unrolled__<N>(it, it1, it2, s1, s2);
3340 reduc_elem_d_unrolled__<N, S2-1>(++it, it1, ++it2, s1, s2);
3345 template<>
inline void reduc_elem_d_unrolled__<1, 0>
3346 (base_tensor::iterator &, base_tensor::const_iterator &,
3348 template<>
inline void reduc_elem_d_unrolled__<2, 0>
3349 (base_tensor::iterator &, base_tensor::const_iterator &,
3351 template<>
inline void reduc_elem_d_unrolled__<3, 0>
3352 (base_tensor::iterator &, base_tensor::const_iterator &,
3354 template<>
inline void reduc_elem_d_unrolled__<4, 0>
3355 (base_tensor::iterator &, base_tensor::const_iterator &,
3357 template<>
inline void reduc_elem_d_unrolled__<5, 0>
3358 (base_tensor::iterator &, base_tensor::const_iterator &,
3360 template<>
inline void reduc_elem_d_unrolled__<6, 0>
3361 (base_tensor::iterator &, base_tensor::const_iterator &,
3363 template<>
inline void reduc_elem_d_unrolled__<7, 0>
3364 (base_tensor::iterator &, base_tensor::const_iterator &,
3366 template<>
inline void reduc_elem_d_unrolled__<8, 0>
3367 (base_tensor::iterator &, base_tensor::const_iterator &,
3369 template<>
inline void reduc_elem_d_unrolled__<9, 0>
3370 (base_tensor::iterator &, base_tensor::const_iterator &,
3372 template<>
inline void reduc_elem_d_unrolled__<10, 0>
3373 (base_tensor::iterator &, base_tensor::const_iterator &,
3375 template<>
inline void reduc_elem_d_unrolled__<11, 0>
3376 (base_tensor::iterator &, base_tensor::const_iterator &,
3378 template<>
inline void reduc_elem_d_unrolled__<12, 0>
3379 (base_tensor::iterator &, base_tensor::const_iterator &,
3381 template<>
inline void reduc_elem_d_unrolled__<13, 0>
3382 (base_tensor::iterator &, base_tensor::const_iterator &,
3384 template<>
inline void reduc_elem_d_unrolled__<14, 0>
3385 (base_tensor::iterator &, base_tensor::const_iterator &,
3387 template<>
inline void reduc_elem_d_unrolled__<15, 0>
3388 (base_tensor::iterator &, base_tensor::const_iterator &,
3390 template<>
inline void reduc_elem_d_unrolled__<16, 0>
3391 (base_tensor::iterator &, base_tensor::const_iterator &,
3396 template<
int I,
int M>
3397 struct ga_ins_red_d_unrolled :
public ga_instruction {
3399 const base_tensor &tc1, &tc2;
3400 virtual int exec() {
3401 GA_DEBUG_INFO(
"Instruction: doubly unrolled contraction operation of size "
3403 size_type N = tc1.size()/I, M_ = tc2.size()/I;
3404 GA_DEBUG_ASSERT(M_ == M,
"Internal error");
3405 GA_DEBUG_ASSERT(t.size() == N*M,
"Internal error, " << t.size()
3406 <<
" != " << N <<
"*" << M);
3407 auto it = t.begin();
3408 auto it1 = tc1.cbegin();
3409 for (
size_type n = 0; n < N; ++n, ++it1) {
3410 auto it2 = tc2.cbegin();
3411 reduc_elem_d_unrolled__<I, M>(it, it1, it2, N, M);
3413 GA_DEBUG_ASSERT(it == t.end(),
"Internal error");
3416 ga_ins_red_d_unrolled(base_tensor &t_,
3417 const base_tensor &tc1_,
const base_tensor &tc2_)
3418 : t(t_), tc1(tc1_), tc2(tc2_) {}
3423 static bool ga_structure_mask(
const assembly_tensor &t,
size_type &Q,
3424 std::vector<bool> &mask) {
3425 if (t.sparsity() != 2 && t.sparsity() != 4)
return false;
3427 if (t.sparsity() == 4) { mask = *(t.mask());
return true; }
3428 const bgeot::multi_index &sz = t.tensor().sizes();
3429 if (sz.size() < 2 || sz.size() > 3 || sz[1] != Q)
return false;
3430 size_type nd = (sz.size() > 2) ? sz[2] : 1;
3431 mask.assign(Q*Q*nd,
false);
3434 mask[q + Q*(q + Q*l)] =
true;
3439 static bool ga_is_sum_with_transpose(
const pga_tree_node child0,
3440 const pga_tree_node child1,
3441 const ga_workspace &workspace) {
3442 auto is_transpose = [&workspace](
const pga_tree_node a,
3443 const pga_tree_node b) {
3444 return a->node_type == GA_NODE_OP && a->op_type == GA_QUOTE
3445 && sub_tree_are_equal(a->children[0], b, workspace, 1);
3447 return is_transpose(child0, child1) || is_transpose(child1, child0);
3451 static void ga_propagate_sum_structure(assembly_tensor &t,
3452 const assembly_tensor &tc1,
3453 const assembly_tensor &tc2,
3454 bool symmetric =
false) {
3455 if (tc1.sparsity() == tc2.sparsity() && tc1.qdim() == tc2.qdim()
3456 && tc1.sparsity() != 4) { t.copy_structure(tc1);
return; }
3458 std::vector<bool> m1, m2;
3459 if (ga_structure_mask(tc1, Q1, m1) && ga_structure_mask(tc2, Q2, m2)
3460 && Q1 == Q2 && m1.size() == m2.size()) {
3461 for (
size_type i = 0; i < m1.size(); ++i) m1[i] = m1[i] || m2[i];
3462 t.set_mask(Q1, std::make_shared<std::vector<bool>>(std::move(m1)),
3463 symmetric || (tc1.mask_symmetric() && tc2.mask_symmetric()));
3469 static bool ga_matrix_op_mask(
const assembly_tensor &tc,
size_type d,
3471 std::vector<bool> &res) {
3472 std::vector<bool> m;
3473 if (!ga_structure_mask(tc, Q, m) || m.size() != Q*d*d)
return false;
3474 res.assign(m.size(),
false);
3478 res[q + Q*(i + d*j)] = m[q + Q*(j + d*i)]
3479 || (keep_original && m[q + Q*(i + d*j)]);
3484 struct ga_instruction_sym_masked :
public ga_instruction {
3486 const base_tensor &tc1;
3488 std::vector<std::array<size_type, 3>> entries;
3489 virtual int exec() {
3490 GA_DEBUG_INFO(
"Instruction: symmetric part with a mask");
3491 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
3492 size_type s = t.size() / (d*d), nb = s / Q;
3493 for (
const auto &e : entries) {
3494 auto it = t.begin() + e[0] + s*e[1];
3495 auto it1 = tc1.cbegin() + e[0] + s*e[1];
3496 auto it1T = tc1.cbegin() + e[0] + s*e[2];
3497 for (
size_type a = 0; a < nb; ++a, it += Q, it1 += Q, it1T += Q)
3498 *it = 0.5*(*it1 + *it1T);
3502 ga_instruction_sym_masked(base_tensor &t_,
const base_tensor &tc1_,
3504 const std::vector<bool> &mask)
3505 : t(t_), tc1(tc1_), Q(Q_), d(d_) {
3509 if (mask[q + Q*(i + d*j)])
3510 entries.push_back({{q, i + d*j, j + d*i}});
3519 struct ga_instruction_contraction_masked :
public ga_instruction {
3521 struct term {
size_type k1, k2; scalar_type w; };
3522 struct block {
size_type q1, q2, first, last; };
3524 const base_tensor &tc1, &tc2;
3526 std::vector<slice> slices1, slices2;
3527 std::vector<term> terms;
3528 std::vector<block> blocks;
3529 std::vector<scalar_type> packed1, packed2, buffer;
3532 const std::vector<slice> &slices,
3533 std::vector<scalar_type> &packed) {
3535 packed.resize(ss*slices.size());
3536 auto it = packed.begin();
3537 for (
const slice &sl : slices) {
3538 auto itc = tc.cbegin() + sl.q + s*sl.p;
3539 for (
size_type i = 0; i < ss; ++i, itc += Q_) *it++ = *itc;
3543 virtual int exec() {
3544 GA_DEBUG_INFO(
"Instruction: contraction of size " << n
3545 <<
" of masked tensors");
3546 size_type s1 = tc1.size()/n, s2 = tc2.size()/n;
3548 GA_DEBUG_ASSERT(t.size() == s1*s2,
"Internal error");
3549 gather(tc1, s1, Q, slices1, packed1);
3550 gather(tc2, s2, Q, slices2, packed2);
3551 buffer.resize(ss1*ss2);
3553 for (
const block &b : blocks) {
3554 std::fill(buffer.begin(), buffer.end(), scalar_type(0));
3555 for (
size_type l = b.first; l < b.last; ++l) {
3556 const term &tm = terms[l];
3557 const scalar_type *p1 = packed1.data() + tm.k1*ss1;
3558 const scalar_type *p2 = packed2.data() + tm.k2*ss2;
3559 scalar_type *pb = buffer.data();
3560 for (
size_type i = 0; i < ss1; ++i, pb += ss2) {
3561 scalar_type a = tm.w * p1[i];
3562 for (
size_type j = 0; j < ss2; ++j) pb[j] += a * p2[j];
3565 const scalar_type *pb = buffer.data();
3566 auto itt = t.begin() + b.q2 + s2*b.q1;
3567 for (
size_type i = 0; i < ss1; ++i, itt += s2*Q) {
3569 for (
size_type j = 0; j < ss2; ++j, it += Q) *it = *pb++;
3575 static size_type slice_index(std::vector<slice> &slices,
3576 std::vector<size_type> &index,
3579 if (k ==
size_type(-1)) { k = slices.size(); slices.push_back({q, p}); }
3583 ga_instruction_contraction_masked(base_tensor &t_,
const base_tensor &tc1_,
3586 const std::vector<bool> &m1,
3587 const std::vector<bool> &m2,
3589 : t(t_), tc1(tc1_), tc2(tc2_), Q(Q_), n(n_) {
3598 if (i > j)
continue;
3599 if (i < j) w = scalar_type(2);
3601 if (m1[q1 + Q*p] && m2[q2 + Q*p])
3602 terms.push_back(term{slice_index(slices1, ind1, q1, p, Q),
3603 slice_index(slices2, ind2, q2, p, Q), w});
3605 if (terms.size() > first)
3606 blocks.push_back(block{q1, q2, first, terms.size()});
3611 #if defined(GA_USES_BLAS)
3616 struct ga_instruction_contraction_sym :
public ga_instruction {
3618 const base_tensor &tc1, &tc2;
3620 std::vector<size_type> ind;
3621 std::vector<scalar_type> weight, packed1, packed2;
3622 virtual int exec() {
3623 GA_DEBUG_INFO(
"Instruction: contraction of size " << n
3624 <<
" of symmetric tensors");
3625 size_type s1 = tc1.size()/n, s2 = tc2.size()/n, nb = ind.size();
3626 GA_DEBUG_ASSERT(t.size() == s1*s2,
"Internal error");
3627 packed1.resize(s1*nb);
3628 packed2.resize(s2*nb);
3629 auto it1 = packed1.begin(), it2 = packed2.begin();
3631 auto itc1 = tc1.cbegin() + s1*ind[k], itc2 = tc2.cbegin() + s2*ind[k];
3632 scalar_type w = weight[k];
3633 for (
size_type i = 0; i < s1; ++i) *it1++ = w * itc1[i];
3634 it2 = std::copy(itc2, itc2 + s2, it2);
3636 BLAS_INT N_ = BLAS_INT(s1), M_ = BLAS_INT(s2), K_ = BLAS_INT(nb);
3637 char notrans =
'N', trans =
'T';
3638 static const scalar_type one(1), zero(0);
3639 gmm::dgemm_(¬rans, &trans, &M_, &N_, &K_, &one, packed2.data(), &M_,
3640 packed1.data(), &N_, &zero, &(t[0]), &M_);
3643 ga_instruction_contraction_sym(base_tensor &t_,
const base_tensor &tc1_,
3646 : t(t_), tc1(tc1_), tc2(tc2_), n(n_) {
3649 ind.push_back(i + d*j);
3650 weight.push_back(i == j ? scalar_type(1) : scalar_type(2));
3660 static pga_instruction ga_masked_contraction_switch
3661 (assembly_tensor &t_, assembly_tensor &tc1_, assembly_tensor &tc2_,
3665 if (tc1_.sparsity() != 4 || tc2_.sparsity() != 4)
3666 return pga_instruction();
3668 if (n <= 4)
return pga_instruction();
3670 std::vector<bool> m1, m2;
3671 if (!ga_structure_mask(tc1_, Q1, m1) || !ga_structure_mask(tc2_, Q2, m2)
3672 || Q1 != Q2 || m1.size() != Q1*n || m2.size() != Q1*n)
3673 return pga_instruction();
3675 if (tc1_.mask_symmetric() && tc2_.mask_symmetric())
3676 sym_d =
size_type(round(sqrt(scalar_type(n))));
3677 #if defined(GA_USES_BLAS)
3679 return std::make_shared<ga_instruction_contraction_sym>
3680 (t_.tensor(), tc1_.tensor(), tc2_.tensor(), n, sym_d);
3685 auto pgai = std::make_shared<ga_instruction_contraction_masked>
3686 (t_.tensor(), tc1_.tensor(), tc2_.tensor(), Q1, n, m1, m2, sym_d);
3687 if (100*pgai->terms.size() > max_percent*Q1*Q1*n)
3688 return pga_instruction();
3695 static bool ga_contraction_is_opt2_2(
const assembly_tensor &tc1_,
3696 const assembly_tensor &tc2_,
3698 if (tc1_.sparsity() != 2 || tc2_.sparsity() != 2
3699 || tc1_.qdim() != tc2_.qdim())
return false;
3700 const bgeot::multi_index &sz1 = tc1_.tensor().sizes();
3701 const bgeot::multi_index &sz2 = tc2_.tensor().sizes();
3703 if (sz1.size() != sz2.size() || sz1.size() < 2 || sz1.size() > 3
3704 || sz1[1] != q || sz2[1] != q)
return false;
3705 size_type n1 = (sz1.size() > 2) ? sz1[2] : 1;
3706 size_type n2 = (sz2.size() > 2) ? sz2[2] : 1;
3707 return n1 == n2 && n1*q == n;
3710 pga_instruction ga_instruction_contraction_switch
3711 (assembly_tensor &t_, assembly_tensor &tc1_, assembly_tensor &tc2_,
3713 base_tensor &t = t_.tensor(), &tc1 = tc1_.tensor(), &tc2 = tc2_.tensor();
3715 if (tc1_.sparsity() == 1 && tc2_.sparsity() == 1 &&
3716 tc1_.qdim() == n && tc2_.qdim() == n) {
3718 t_.set_sparsity(10, tc1_.qdim());
3719 return std::make_shared<ga_instruction_contraction_opt1_1>(t, tc1, tc2, n);
3722 if (ga_contraction_is_opt2_2(tc1_, tc2_, n)) {
3724 t_.set_sparsity(10, tc1_.qdim());
3725 return std::make_shared<ga_instruction_contraction_opt2_2>
3726 (t, tc1, tc2, tc1_.qdim(), n/tc1_.qdim());
3729 pga_instruction pgai_masked
3730 = ga_masked_contraction_switch(t_, tc1_, tc2_, n, to_clear);
3731 if (pgai_masked)
return pgai_masked;
3734 if (tc2_.sparsity() == 1) {
3737 return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<2>>
3740 return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<3>>
3743 return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<4>>
3746 return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<5>>
3749 return std::make_shared<ga_instruction_contraction_opt0_1>(t,tc1,tc2,n);
3752 if (tc2_.sparsity() == 2) {
3754 size_type n2 = (tc2.sizes().size() > 2) ? tc2.sizes()[2] : 1;
3761 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<1,2>>
3765 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<1,3>>
3769 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<1,4>>
3772 return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<1>>
3779 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<2,2>>
3783 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<2,3>>
3787 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<2,4>>
3790 return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<2>>
3797 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<3,2>>
3801 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<3,3>>
3805 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<3,4>>
3808 return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<3>>
3812 return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<4>>
3815 return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<5>>
3818 return std::make_shared<ga_instruction_contraction_opt0_2>
3823 if (tc1_.sparsity() == 2) {
3825 size_type n1 = (tc1.sizes().size() > 2) ? tc1.sizes()[2] : 1;
3832 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<1,2>>
3836 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<1,3>>
3840 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<1,4>>
3843 return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<1>>
3850 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<2,2>>
3854 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<2,3>>
3858 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<2,4>>
3861 return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<2>>
3868 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<3,2>>
3872 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<3,3>>
3876 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<3,4>>
3879 return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<3>>
3882 return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<3>>
3885 return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<4>>
3888 return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<5>>
3891 return std::make_shared<ga_instruction_contraction_opt2_0>
3892 (t,tc1,tc2, n1, q1);
3898 case 1 :
return std::make_shared<ga_instruction_contraction_unrolled< 1>>
3900 case 2 :
return std::make_shared<ga_instruction_contraction_unrolled< 2>>
3902 case 3 :
return std::make_shared<ga_instruction_contraction_unrolled< 3>>
3904 case 4 :
return std::make_shared<ga_instruction_contraction_unrolled< 4>>
3906 case 5 :
return std::make_shared<ga_instruction_contraction_unrolled< 5>>
3908 case 6 :
return std::make_shared<ga_instruction_contraction_unrolled< 6>>
3912 default :
return std::make_shared<ga_instruction_contraction>
3917 pga_instruction ga_uniform_instruction_contraction_switch
3918 (assembly_tensor &t_, assembly_tensor &tc1_, assembly_tensor &tc2_,
3920 base_tensor &t = t_.tensor(), &tc1 = tc1_.tensor(), &tc2 = tc2_.tensor();
3922 if (tc1_.sparsity() == 1 && tc2_.sparsity() == 1 &&
3923 tc1_.qdim() == n && tc2_.qdim() == n) {
3925 t_.set_sparsity(10, tc1_.qdim());
3926 return std::make_shared<ga_instruction_contraction_opt1_1>(t,tc1,tc2,n);
3929 if (ga_contraction_is_opt2_2(tc1_, tc2_, n)) {
3931 t_.set_sparsity(10, tc1_.qdim());
3932 return std::make_shared<ga_instruction_contraction_opt2_2>
3933 (t, tc1, tc2, tc1_.qdim(), n/tc1_.qdim());
3936 pga_instruction pgai_masked
3937 = ga_masked_contraction_switch(t_, tc1_, tc2_, n, to_clear);
3938 if (pgai_masked)
return pgai_masked;
3940 if (tc2_.sparsity() == 1) {
3943 return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<2>>
3946 return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<3>>
3949 return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<4>>
3952 return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<5>>
3955 return std::make_shared<ga_instruction_contraction_opt0_1>(t,tc1,tc2, n);
3958 if (tc2_.sparsity() == 2) {
3960 size_type n2 = (tc2.sizes().size() > 2) ? tc2.sizes()[2] : 1;
3967 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<1,2>>
3971 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<1,3>>
3975 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<1,4>>
3978 return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<1>>
3985 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<2,2>>
3989 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<2,3>>
3993 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<2,4>>
3996 return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<2>>
4003 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<3,2>>
4007 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<3,3>>
4011 std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<3,4>>
4014 return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<3>>
4018 return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<4>>
4021 return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<5>>
4024 return std::make_shared<ga_instruction_contraction_opt0_2>
4029 if (tc1_.sparsity() == 2) {
4031 size_type n1 = (tc1.sizes().size() > 2) ? tc1.sizes()[2] : 1;
4038 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<1,2>>
4042 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<1,3>>
4046 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<1,4>>
4049 return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<1>>
4056 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<2,2>>
4060 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<2,3>>
4064 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<2,4>>
4067 return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<2>>
4074 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<3,2>>
4078 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<3,3>>
4082 std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<3,4>>
4085 return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<3>>
4088 return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<3>>
4091 return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<4>>
4094 return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<5>>
4097 return std::make_shared<ga_instruction_contraction_opt2_0>
4098 (t,tc1,tc2, n1, q1);
4108 case 2:
return std::make_shared<ga_ins_red_d_unrolled<2,1>>(t, tc1, tc2);
4109 case 3:
return std::make_shared<ga_ins_red_d_unrolled<3,1>>(t, tc1, tc2);
4110 case 4:
return std::make_shared<ga_ins_red_d_unrolled<4,1>>(t, tc1, tc2);
4111 default:
return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4115 case 2:
return std::make_shared<ga_ins_red_d_unrolled<2,2>>(t, tc1, tc2);
4116 case 3:
return std::make_shared<ga_ins_red_d_unrolled<3,2>>(t, tc1, tc2);
4117 case 4:
return std::make_shared<ga_ins_red_d_unrolled<4,2>>(t, tc1, tc2);
4118 default:
return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4122 case 2:
return std::make_shared<ga_ins_red_d_unrolled<2,3>>(t, tc1, tc2);
4123 case 3:
return std::make_shared<ga_ins_red_d_unrolled<3,3>>(t, tc1, tc2);
4124 case 4:
return std::make_shared<ga_ins_red_d_unrolled<4,3>>(t, tc1, tc2);
4125 default:
return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4129 case 2:
return std::make_shared<ga_ins_red_d_unrolled<2,4>>(t, tc1, tc2);
4130 case 3:
return std::make_shared<ga_ins_red_d_unrolled<3,4>>(t, tc1, tc2);
4131 case 4:
return std::make_shared<ga_ins_red_d_unrolled<4,4>>(t, tc1, tc2);
4132 default:
return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4136 case 2:
return std::make_shared<ga_ins_red_d_unrolled<2,5>>(t, tc1, tc2);
4137 case 3:
return std::make_shared<ga_ins_red_d_unrolled<3,5>>(t, tc1, tc2);
4138 case 4:
return std::make_shared<ga_ins_red_d_unrolled<4,5>>(t, tc1, tc2);
4139 default:
return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4143 case 2:
return std::make_shared<ga_ins_red_d_unrolled<2,6>>(t, tc1, tc2);
4144 case 3:
return std::make_shared<ga_ins_red_d_unrolled<3,6>>(t, tc1, tc2);
4145 case 4:
return std::make_shared<ga_ins_red_d_unrolled<4,6>>(t, tc1, tc2);
4146 default:
return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4150 case 2:
return std::make_shared<ga_ins_red_d_unrolled<2,7>>(t, tc1, tc2);
4151 case 3:
return std::make_shared<ga_ins_red_d_unrolled<3,7>>(t, tc1, tc2);
4152 case 4:
return std::make_shared<ga_ins_red_d_unrolled<4,7>>(t, tc1, tc2);
4153 default:
return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4157 case 2:
return std::make_shared<ga_ins_red_d_unrolled<2,8>>(t, tc1, tc2);
4158 case 3:
return std::make_shared<ga_ins_red_d_unrolled<3,8>>(t, tc1, tc2);
4159 case 4:
return std::make_shared<ga_ins_red_d_unrolled<4,8>>(t, tc1, tc2);
4160 default:
return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4164 case 2:
return std::make_shared<ga_ins_red_d_unrolled<2,9>>(t, tc1, tc2);
4165 case 3:
return std::make_shared<ga_ins_red_d_unrolled<3,9>>(t, tc1, tc2);
4166 case 4:
return std::make_shared<ga_ins_red_d_unrolled<4,9>>(t, tc1, tc2);
4167 default:
return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4171 case 2:
return std::make_shared<ga_ins_red_d_unrolled<2,10>>(t, tc1, tc2);
4172 case 3:
return std::make_shared<ga_ins_red_d_unrolled<3,10>>(t, tc1, tc2);
4173 case 4:
return std::make_shared<ga_ins_red_d_unrolled<4,10>>(t, tc1, tc2);
4174 default:
return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4176 default:
return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4182 struct ga_instruction_spec_contraction :
public ga_instruction {
4184 const base_tensor &tc1, &tc2;
4186 virtual int exec() {
4187 GA_DEBUG_INFO(
"Instruction: specific contraction operation of "
4189 size_type s1 = tc1.sizes()[0], s11 = tc1.size() / (s1*nn), s111 = s1*s11;
4191 base_tensor::iterator it = t.begin();
4194 for (
size_type m = 0; m < s1; ++m, ++it) {
4195 *it = scalar_type(0);
4197 *it += tc1[m+i*s1+j*s111] * tc2[n+j*s2];
4199 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
4202 ga_instruction_spec_contraction(base_tensor &t_,
4203 const base_tensor &tc1_,
4205 : t(t_), tc1(tc1_), tc2(tc2_), nn(n_) {}
4209 struct ga_instruction_spec2_contraction :
public ga_instruction {
4211 const base_tensor &tc1, &tc2;
4213 virtual int exec() {
4214 GA_DEBUG_INFO(
"Instruction: second specific contraction operation of "
4216 size_type s1 = tc1.sizes()[0], s11 = tc1.size() / (s1*nn), s111 = s1*s11;
4217 size_type s2 = tc2.sizes()[0], s22 = tc2.size() / (s2*nn), s222 = s2*s22;
4218 base_tensor::iterator it = t.begin();
4222 for (
size_type n = 0; n < s2; ++n, ++it) {
4223 *it = scalar_type(0);
4225 *it += tc1[m+i*s1+k*s111] * tc2[n+j*s2+k*s222];
4227 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
4230 ga_instruction_spec2_contraction(base_tensor &t_,
4231 const base_tensor &tc1_,
4233 : t(t_), tc1(tc1_), tc2(tc2_), nn(n_) {}
4237 struct ga_instruction_simple_tmult :
public ga_instruction {
4239 const base_tensor &tc1, &tc2;
4240 virtual int exec() {
4241 GA_DEBUG_INFO(
"Instruction: simple tensor product");
4243 GA_DEBUG_ASSERT(t.size() == s1 * tc2.size(),
"Wrong sizes");
4244 base_tensor::const_iterator it2=tc2.cbegin(), it1=tc1.cbegin(), it1end=it1 + s1;
4245 for (base_tensor::iterator it = t.begin(); it != t.end(); ++it) {
4246 *it = *(it2) * (*it1);
4247 if (++it1 == it1end) { it1 = tc1.cbegin(), ++it2; }
4251 ga_instruction_simple_tmult(base_tensor &t_,
4252 const base_tensor &tc1_,
const base_tensor &tc2_)
4253 : t(t_), tc1(tc1_), tc2(tc2_) {}
4258 template<
int IJ>
struct ga_instruction_simple_tmult_unrolled
4259 :
public ga_instruction {
4261 const base_tensor &tc1, &tc2;
4262 virtual int exec() {
4264 GA_DEBUG_ASSERT(tc1.size() == IJ,
4265 "Wrong sizes " << tc1.size() <<
" != " << IJ);
4266 GA_DEBUG_INFO(
"Instruction: simple tensor product, unrolled with "
4267 << IJ <<
" operations");
4268 GA_DEBUG_ASSERT(t.size() == IJ * KL,
4269 "Wrong sizes " << t.size() <<
" != " << IJ <<
"*" << KL);
4271 const BLAS_INT IJ_=BLAS_INT(IJ), KL_=BLAS_INT(KL), INC(1);
4272 const scalar_type one(1);
4273 std::fill(t.begin(), t.end(), scalar_type(0));
4274 gmm::dger_(&IJ_, &KL_, &one, &tc1[0], &INC, &tc2[0], &INC, &(t[0]), &IJ_);
4276 base_tensor::iterator it = t.begin();
4277 base_tensor::const_iterator it2 = tc2.cbegin();
4278 for (
size_type kl = 0; kl < KL; ++kl, ++it2) {
4279 base_tensor::const_iterator it1 = tc1.cbegin();
4280 dax__<IJ>(it, it1, *it2);
4282 GA_DEBUG_ASSERT(it == t.end(),
"Internal error");
4286 ga_instruction_simple_tmult_unrolled(base_tensor &t_,
4287 const base_tensor &tc1_,
4288 const base_tensor &tc2_)
4289 : t(t_), tc1(tc1_), tc2(tc2_) {}
4292 pga_instruction ga_uniform_instruction_simple_tmult
4293 (base_tensor &t,
const base_tensor &tc1,
const base_tensor &tc2) {
4294 switch(tc1.size()) {
4295 case 1 : GMM_ASSERT1(
false,
"size 1 should not happen");
4296 case 2 :
return std::make_shared<ga_instruction_simple_tmult_unrolled< 2>>
4298 case 3 :
return std::make_shared<ga_instruction_simple_tmult_unrolled< 3>>
4300 case 4 :
return std::make_shared<ga_instruction_simple_tmult_unrolled< 4>>
4302 case 5 :
return std::make_shared<ga_instruction_simple_tmult_unrolled< 5>>
4304 case 6 :
return std::make_shared<ga_instruction_simple_tmult_unrolled< 6>>
4306 case 7 :
return std::make_shared<ga_instruction_simple_tmult_unrolled< 7>>
4308 case 8 :
return std::make_shared<ga_instruction_simple_tmult_unrolled< 8>>
4310 case 9 :
return std::make_shared<ga_instruction_simple_tmult_unrolled< 9>>
4312 case 10 :
return std::make_shared<ga_instruction_simple_tmult_unrolled<10>>
4314 case 11 :
return std::make_shared<ga_instruction_simple_tmult_unrolled<11>>
4316 case 12 :
return std::make_shared<ga_instruction_simple_tmult_unrolled<12>>
4318 case 13 :
return std::make_shared<ga_instruction_simple_tmult_unrolled<13>>
4320 case 14 :
return std::make_shared<ga_instruction_simple_tmult_unrolled<14>>
4322 case 15 :
return std::make_shared<ga_instruction_simple_tmult_unrolled<15>>
4324 case 16 :
return std::make_shared<ga_instruction_simple_tmult_unrolled<16>>
4326 default :
return std::make_shared<ga_instruction_simple_tmult>
4333 struct ga_instruction_spec_tmult :
public ga_instruction {
4335 const base_tensor &tc1, &tc2;
4337 virtual int exec() {
4338 GA_DEBUG_INFO(
"Instruction: specific tensor product");
4339 GA_DEBUG_ASSERT(t.size() == tc1.size() * tc2.size(),
"Wrong sizes");
4342 auto it = t.begin();
4347 for (
auto it1 = tc1.cbegin(); it1 != tc1.end(); ++it1)
4349 *it++ = (*it1) * tc2[n+N*j];
4355 auto it1 = tc1.cbegin() + M*i;
4356 dax__<2>(it, it1, tc2[n+N*j]);
4363 auto it1 = tc1.cbegin() + M*i;
4364 dax__<3>(it, it1, tc2[n+N*j]);
4371 auto it1 = tc1.cbegin() + M*i;
4372 dax__<4>(it, it1, tc2[n+N*j]);
4379 auto it1 = tc1.cbegin() + M*i;
4380 dax__<5>(it, it1, tc2[n+N*j]);
4387 auto it1 = tc1.cbegin() + M*i;
4388 dax__<6>(it, it1, tc2[n+N*j]);
4395 auto it1 = tc1.cbegin() + M*i;
4396 dax__<7>(it, it1, tc2[n+N*j]);
4403 auto it1 = tc1.cbegin() + M*i;
4404 dax__<8>(it, it1, tc2[n+N*j]);
4408 const int M1 = int(M)/8;
4409 const int M2 = int(M) - M1*8;
4415 auto it1 = tc1.cbegin() + M*i;
4416 for (
int mm=0; mm < M1; ++mm)
4417 dax__<8>(it, it1, tc2[n+N*j]);
4424 auto it1 = tc1.cbegin() + M*i;
4425 for (
int mm=0; mm < M1; ++mm)
4426 dax__<8>(it, it1, tc2[n+N*j]);
4427 dax__<1>(it, it1, tc2[n+N*j]);
4434 auto it1 = tc1.cbegin() + M*i;
4435 for (
int mm=0; mm < M1; ++mm)
4436 dax__<8>(it, it1, tc2[n+N*j]);
4437 dax__<2>(it, it1, tc2[n+N*j]);
4444 auto it1 = tc1.cbegin() + M*i;
4445 for (
int mm=0; mm < M1; ++mm)
4446 dax__<8>(it, it1, tc2[n+N*j]);
4447 dax__<3>(it, it1, tc2[n+N*j]);
4454 auto it1 = tc1.cbegin() + M*i;
4455 for (
int mm=0; mm < M1; ++mm)
4456 dax__<8>(it, it1, tc2[n+N*j]);
4457 dax__<4>(it, it1, tc2[n+N*j]);
4464 auto it1 = tc1.cbegin() + M*i;
4465 for (
int mm=0; mm < M1; ++mm)
4466 dax__<8>(it, it1, tc2[n+N*j]);
4467 dax__<5>(it, it1, tc2[n+N*j]);
4474 auto it1 = tc1.cbegin() + M*i;
4475 for (
int mm=0; mm < M1; ++mm)
4476 dax__<8>(it, it1, tc2[n+N*j]);
4477 dax__<6>(it, it1, tc2[n+N*j]);
4484 auto it1 = tc1.cbegin() + M*i;
4485 for (
int mm=0; mm < M1; ++mm)
4486 dax__<8>(it, it1, tc2[n+N*j]);
4487 dax__<7>(it, it1, tc2[n+N*j]);
4491 GMM_ASSERT1(
false,
"should not happen");
4494 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
4500 scalar_type val = tc2[n+N*j];
4502 *it = tc1[m+M*i] * val;
4504 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
4508 ga_instruction_spec_tmult(base_tensor &t_,
4509 const base_tensor &tc1_,
4510 const base_tensor &tc2_,
4512 : t(t_), tc1(tc1_), tc2(tc2_), I(I_), J(J_) {}
4516 struct ga_instruction_spec2_tmult :
public ga_instruction {
4518 const base_tensor &tc1, &tc2;
4519 virtual int exec() {
4520 GA_DEBUG_INFO(
"Instruction: second specific tensor product");
4521 GA_DEBUG_ASSERT(t.size() == tc1.size() * tc2.size(),
"Wrong sizes");
4523 size_type M = tc2.sizes()[0], J = tc2.size() / M;
4525 base_tensor::iterator it = t.begin();
4529 *it = tc1[i] * tc2[m+M*j];
4530 GA_DEBUG_ASSERT(it == t.end(),
"Wrong sizes");
4533 ga_instruction_spec2_tmult(base_tensor &t_,
4534 const base_tensor &tc1_,
const base_tensor &tc2_)
4535 : t(t_), tc1(tc1_), tc2(tc2_) {}
4540 struct ga_instruction_simple_c_matrix :
public ga_instruction {
4542 std::vector<scalar_type *> components;
4543 virtual int exec() {
4544 GA_DEBUG_INFO(
"Instruction: gathering components for explicit "
4546 GA_DEBUG_ASSERT(t.size() == components.size(),
"Wrong sizes");
4547 for (
size_type i = 0; i < components.size(); ++i)
4548 t[i] = *(components[i]);
4551 ga_instruction_simple_c_matrix(base_tensor &t_,
4552 std::vector<scalar_type *> &components_)
4553 : t(t_), components(components_) {}
4556 struct ga_instruction_c_matrix_with_tests :
public ga_instruction {
4558 const std::vector<const base_tensor *> components;
4559 virtual int exec() {
4560 GA_DEBUG_INFO(
"Instruction: gathering components for explicit "
4561 "matrix with tests functions");
4562 size_type s = t.size() / components.size();
4563 GA_DEBUG_ASSERT(s,
"Wrong sizes");
4564 base_tensor::iterator it = t.begin();
4565 for (
size_type i = 0; i < components.size(); ++i) {
4566 const base_tensor &t1 = *(components[i]);
4567 if (t1.size() > 1) {
4568 GA_DEBUG_ASSERT(t1.size() == s,
"Wrong sizes, " << t1.size()
4570 for (
size_type j = 0; j < s; ++j) *it++ = t1[j];
4572 for (
size_type j = 0; j < s; ++j) *it++ = t1[0];
4577 ga_instruction_c_matrix_with_tests
4578 (base_tensor &t_,
const std::vector<const base_tensor *> &components_)
4579 : t(t_), components(components_) {}
4582 struct ga_instruction_eval_func_1arg_1res :
public ga_instruction {
4584 const scalar_type &c;
4585 pscalar_func_onearg f1;
4586 virtual int exec() {
4587 GA_DEBUG_INFO(
"Instruction: evaluation of a one argument "
4588 "predefined function on a scalar");
4592 ga_instruction_eval_func_1arg_1res(scalar_type &t_,
const scalar_type &c_,
4593 pscalar_func_onearg f1_)
4594 : t(t_), c(c_), f1(f1_) {}
4597 struct ga_instruction_eval_func_1arg_1res_expr :
public ga_instruction {
4599 const scalar_type &c;
4600 const ga_predef_function &F;
4601 virtual int exec() {
4602 GA_DEBUG_INFO(
"Instruction: evaluation of a one argument "
4603 "predefined function on a scalar");
4607 ga_instruction_eval_func_1arg_1res_expr(scalar_type &t_,
4608 const scalar_type &c_,
4609 const ga_predef_function &F_)
4610 : t(t_), c(c_), F(F_) {}
4613 struct ga_instruction_eval_func_1arg :
public ga_instruction {
4615 const base_tensor &tc1;
4616 pscalar_func_onearg f1;
4617 virtual int exec() {
4618 GA_DEBUG_INFO(
"Instruction: evaluation of a one argument "
4619 "predefined function on tensor");
4620 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
4621 for (
size_type i = 0; i < t.size(); ++i)
4622 t[i] = (*f1)(tc1[i]);
4625 ga_instruction_eval_func_1arg(base_tensor &t_,
4626 const base_tensor &c_, pscalar_func_onearg f1_)
4627 : t(t_), tc1(c_), f1(f1_) {}
4630 struct ga_instruction_eval_func_1arg_expr :
public ga_instruction {
4632 const base_tensor &tc1;
4633 const ga_predef_function &F;
4634 virtual int exec() {
4635 GA_DEBUG_INFO(
"Instruction: evaluation of a one argument "
4636 "predefined function on tensor");
4637 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
4638 for (
size_type i = 0; i < t.size(); ++i)
4642 ga_instruction_eval_func_1arg_expr(base_tensor &t_,
4643 const base_tensor &c_,
4644 const ga_predef_function &F_)
4645 : t(t_), tc1(c_), F(F_) {}
4648 struct ga_instruction_eval_func_2arg_1res :
public ga_instruction {
4650 const scalar_type &c, &d;
4651 pscalar_func_twoargs f2;
4652 virtual int exec() {
4653 GA_DEBUG_INFO(
"Instruction: evaluation of a two arguments "
4654 "predefined function on two scalar");
4658 ga_instruction_eval_func_2arg_1res(scalar_type &t_,
const scalar_type &c_,
4659 const scalar_type &d_,
4660 pscalar_func_twoargs f2_)
4661 : t(t_), c(c_), d(d_), f2(f2_) {}
4664 struct ga_instruction_eval_func_2arg_1res_expr :
public ga_instruction {
4666 const scalar_type &c, &d;
4667 const ga_predef_function &F;
4668 virtual int exec() {
4669 GA_DEBUG_INFO(
"Instruction: evaluation of a two arguments "
4670 "predefined function on two scalar");
4674 ga_instruction_eval_func_2arg_1res_expr(scalar_type &t_,
4675 const scalar_type &c_,
4676 const scalar_type &d_,
4677 const ga_predef_function &F_)
4678 : t(t_), c(c_), d(d_), F(F_) {}
4681 struct ga_instruction_eval_func_2arg_first_scalar :
public ga_instruction {
4683 const base_tensor &tc1, &tc2;
4684 pscalar_func_twoargs f2;
4685 virtual int exec() {
4686 GA_DEBUG_INFO(
"Instruction: evaluation of a two arguments "
4687 "predefined function on one scalar and one tensor");
4688 GA_DEBUG_ASSERT(t.size() == tc2.size(),
"Wrong sizes");
4689 for (
size_type i = 0; i < t.size(); ++i)
4690 t[i] = (*f2)(tc1[0], tc2[i]);
4693 ga_instruction_eval_func_2arg_first_scalar(base_tensor &t_,
4694 const base_tensor &c_,
4695 const base_tensor &d_,
4696 pscalar_func_twoargs f2_)
4697 : t(t_), tc1(c_), tc2(d_), f2(f2_) {}
4700 struct ga_instruction_eval_func_2arg_first_scalar_expr
4701 :
public ga_instruction {
4703 const base_tensor &tc1, &tc2;
4704 const ga_predef_function &F;
4705 virtual int exec() {
4706 GA_DEBUG_INFO(
"Instruction: evaluation of a two arguments "
4707 "predefined function on one scalar and one tensor");
4708 GA_DEBUG_ASSERT(t.size() == tc2.size(),
"Wrong sizes");
4709 for (
size_type i = 0; i < t.size(); ++i)
4710 t[i] = F(tc1[0], tc2[i]);
4713 ga_instruction_eval_func_2arg_first_scalar_expr(base_tensor &t_,
4714 const base_tensor &c_,
4715 const base_tensor &d_,
4716 const ga_predef_function &F_)
4717 : t(t_), tc1(c_), tc2(d_), F(F_) {}
4720 struct ga_instruction_eval_func_2arg_second_scalar :
public ga_instruction {
4722 const base_tensor &tc1, &tc2;
4723 pscalar_func_twoargs f2;
4724 virtual int exec() {
4725 GA_DEBUG_INFO(
"Instruction: evaluation of a two arguments "
4726 "predefined function on one tensor and one scalar");
4727 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
4728 for (
size_type i = 0; i < t.size(); ++i)
4729 t[i] = (*f2)(tc1[i], tc2[0]);
4732 ga_instruction_eval_func_2arg_second_scalar(base_tensor &t_,
4733 const base_tensor &c_,
4734 const base_tensor &d_,
4735 pscalar_func_twoargs f2_)
4736 : t(t_), tc1(c_), tc2(d_), f2(f2_) {}
4739 struct ga_instruction_eval_func_2arg_second_scalar_expr
4740 :
public ga_instruction {
4742 const base_tensor &tc1, &tc2;
4743 const ga_predef_function &F;
4744 virtual int exec() {
4745 GA_DEBUG_INFO(
"Instruction: evaluation of a two arguments "
4746 "predefined function on one tensor and one scalar");
4747 GA_DEBUG_ASSERT(t.size() == tc1.size(),
"Wrong sizes");
4748 for (
size_type i = 0; i < t.size(); ++i)
4749 t[i] = F(tc1[i], tc2[0]);
4752 ga_instruction_eval_func_2arg_second_scalar_expr(base_tensor &t_,
4753 const base_tensor &c_,
4754 const base_tensor &d_,
4755 const ga_predef_function &F_)
4756 : t(t_), tc1(c_), tc2(d_), F(F_) {}
4759 struct ga_instruction_eval_func_2arg :
public ga_instruction {
4761 const base_tensor &tc1, &tc2;
4762 pscalar_func_twoargs f2;
4763 virtual int exec() {
4764 GA_DEBUG_INFO(
"Instruction: evaluation of a two arguments "
4765 "predefined function on two tensors");
4766 GA_DEBUG_ASSERT(t.size() == tc1.size() && t.size() == tc2.size(),
4768 for (
size_type i = 0; i < t.size(); ++i)
4769 t[i] = (*f2)(tc1[i], tc2[i]);
4772 ga_instruction_eval_func_2arg(base_tensor &t_,
4773 const base_tensor &c_,
4774 const base_tensor &d_, pscalar_func_twoargs f2_)
4775 : t(t_), tc1(c_), tc2(d_), f2(f2_) {}
4778 struct ga_instruction_eval_func_2arg_expr :
public ga_instruction {
4780 const base_tensor &tc1, &tc2;
4781 const ga_predef_function &F;
4782 virtual int exec() {
4783 GA_DEBUG_INFO(
"Instruction: evaluation of a two arguments "
4784 "predefined function on two tensors");
4785 GA_DEBUG_ASSERT(t.size() == tc1.size() && t.size() == tc2.size(),
4787 for (
size_type i = 0; i < t.size(); ++i)
4788 t[i] = F(tc1[i], tc2[i]);
4791 ga_instruction_eval_func_2arg_expr(base_tensor &t_,
4792 const base_tensor &c_,
4793 const base_tensor &d_,
4794 const ga_predef_function &F_)
4795 : t(t_), tc1(c_), tc2(d_), F(F_) {}
4798 struct ga_instruction_eval_OP :
public ga_instruction {
4800 const ga_nonlinear_operator &OP;
4801 ga_nonlinear_operator::arg_list args;
4802 virtual int exec() {
4803 GA_DEBUG_INFO(
"Instruction: operator evaluation");
4807 ga_instruction_eval_OP(base_tensor &t_,
const ga_nonlinear_operator &OP_,
4808 ga_nonlinear_operator::arg_list &args_)
4809 : t(t_), OP(OP_), args(args_) {}
4812 struct ga_instruction_eval_derivative_OP :
public ga_instruction {
4814 const ga_nonlinear_operator &OP;
4815 ga_nonlinear_operator::arg_list args;
4817 virtual int exec() {
4818 GA_DEBUG_INFO(
"Instruction: operator derivative evaluation");
4819 OP.derivative(args, der1, t);
4822 ga_instruction_eval_derivative_OP(base_tensor &t_,
4823 const ga_nonlinear_operator &OP_,
4824 ga_nonlinear_operator::arg_list &args_,
4826 : t(t_), OP(OP_), args(args_), der1(der1_) {}
4829 struct ga_instruction_eval_second_derivative_OP :
public ga_instruction {
4831 const ga_nonlinear_operator &OP;
4832 ga_nonlinear_operator::arg_list args;
4834 virtual int exec() {
4835 GA_DEBUG_INFO(
"Instruction: operator second derivative evaluation");
4836 OP.second_derivative(args, der1, der2, t);
4839 ga_instruction_eval_second_derivative_OP
4840 (base_tensor &t_,
const ga_nonlinear_operator &OP_,
4842 : t(t_), OP(OP_), args(args_), der1(der1_), der2(der2_) {}
4845 struct ga_instruction_tensor_slice :
public ga_instruction {
4847 const base_tensor &tc1;
4848 bgeot::multi_index mi, indices;
4849 virtual int exec() {
4850 GA_DEBUG_INFO(
"Instruction: tensor slice");
4852 for (bgeot::multi_index mi3(order); !mi3.finished(t.sizes());
4853 mi3.incrementation(t.sizes())) {
4855 mi[indices[j]] = mi3[j];
4860 ga_instruction_tensor_slice(base_tensor &t_,
4861 const base_tensor &tc1_,
4862 bgeot::multi_index &mi_,
4863 bgeot::multi_index &indices_)
4864 : t(t_), tc1(tc1_), mi(mi_), indices(indices_) {}
4867 struct ga_instruction_transformation_call :
public ga_instruction {
4868 const ga_workspace &workspace;
4869 ga_instruction_set::interpolate_info &inin;
4870 pinterpolate_transformation trans;
4871 fem_interpolation_context &ctx;
4872 const base_small_vector &Normal;
4876 virtual int exec() {
4877 GA_DEBUG_INFO(
"Instruction: call interpolate transformation");
4881 inin.pt_type = trans->transform(workspace, m, ctx, Normal, &(inin.m), cv,
4882 face_num, P_ref, inin.Normal,
4883 inin.derivatives, compute_der);
4886 inin.m->points_of_convex(cv, inin.G);
4887 inin.ctx.change((inin.m)->trans_of_convex(cv),
4888 0, P_ref, inin.G, cv, face_num);
4889 inin.has_ctx =
true;
4892 gmm::scale(inin.Normal, 1.0/gmm::vect_norm2(inin.Normal));
4895 inin.pt_y = inin.ctx.xreal();
4897 inin.ctx.invalid_convex_num();
4898 inin.Normal.resize(0);
4900 inin.has_ctx =
false;
4903 inin.ctx.invalid_convex_num();
4904 inin.Normal.resize(0);
4905 inin.pt_y.resize(0);
4906 inin.has_ctx =
false;
4908 GA_DEBUG_INFO(
"Instruction: end of call interpolate transformation");
4911 ga_instruction_transformation_call
4912 (
const ga_workspace &w, ga_instruction_set::interpolate_info &i,
4913 pinterpolate_transformation t, fem_interpolation_context &ctxx,
4914 const base_small_vector &No,
const mesh &mm,
bool compute_der_)
4915 : workspace(w), inin(i), trans(t), ctx(ctxx), Normal(No), m(mm),
4916 compute_der(compute_der_) {}
4919 struct ga_instruction_neighbor_transformation_call :
public ga_instruction {
4920 const ga_workspace &workspace;
4921 ga_instruction_set::interpolate_info &inin;
4922 pinterpolate_transformation trans;
4923 fem_interpolation_context &ctx;
4924 base_small_vector dummy_normal;
4927 papprox_integration &pai;
4929 std::map<gauss_pt_corresp, bgeot::pstored_point_tab> &neighbor_corresp;
4931 virtual int exec() {
4932 bool cancel_optimization =
false;
4933 GA_DEBUG_INFO(
"Instruction: call interpolate neighbor transformation");
4935 if (!(ctx.have_pgp()) || !pai || pai->is_built_on_the_fly()
4936 || cancel_optimization) {
4937 inin.ctx.invalid_convex_num();
4942 auto adj_face = m.adjacent_face(cv, f);
4944 GMM_WARNING2(
"Adjacent face not found, "
4945 "probably an non-interior face");
4946 inin.ctx.invalid_convex_num();
4948 gauss_pt_corresp gpc;
4949 gpc.pgt1 = m.trans_of_convex(cv);
4950 gpc.pgt2 = m.trans_of_convex(adj_face.cv);
4952 auto inds_pt1 = m.ind_points_of_face_of_convex(cv, f);
4953 auto inds_pt2 = m.ind_points_of_face_of_convex(adj_face.cv,
4955 auto str1 = gpc.pgt1->structure();
4956 auto str2 = gpc.pgt2->structure();
4957 size_type nbptf1 = str1->nb_points_of_face(f);
4958 size_type nbptf2 = str2->nb_points_of_face(adj_face.f);
4959 gpc.nodes.resize(nbptf1*2);
4960 for (
size_type i = 0; i < nbptf1; ++i) {
4961 gpc.nodes[2*i] = str1->ind_points_of_face(f)[i];
4963 for (
size_type j = 0; j < nbptf2; ++j) {
4964 if (inds_pt2[j] == inds_pt1[i]) {
4965 gpc.nodes[2*i+1] = str2->ind_points_of_face(adj_face.f)[j];
4970 GMM_ASSERT1(found,
"Internal error");
4972 bgeot::pstored_point_tab pspt = 0;
4973 auto itm = neighbor_corresp.find(gpc);
4974 if (itm != neighbor_corresp.end()) {
4977 size_type nbpt = pai->nb_points_on_face(f);
4979 gic.init(m.points_of_convex(adj_face.cv), gpc.pgt2);
4980 size_type first_ind = pai->ind_first_point_on_face(f);
4982 &spt = *(pai->pintegration_points());
4984 m.points_of_convex(cv, G);
4985 fem_interpolation_context ctx_x(gpc.pgt1, 0, spt[0], G, cv, f);
4986 std::vector<base_node> P_ref(nbpt);
4989 ctx_x.set_xref(spt[first_ind+i]);
4990 bool converged =
true;
4991 gic.
invert(ctx_x.xreal(), P_ref[i], converged);
4992 bool is_in = (gpc.pgt2->convex_ref()->is_in(P_ref[i]) < 1E-4);
4993 GMM_ASSERT1(is_in && converged,
"Geometric transformation "
4994 "inversion has failed in neighbor transformation");
4996 pspt = store_point_tab(P_ref);
4997 neighbor_corresp[gpc] = pspt;
4999 m.points_of_convex(adj_face.cv, inin.G);
5000 bgeot::pgeotrans_precomp pgp = gp_pool(gpc.pgt2, pspt);
5001 inin.ctx.change(pgp, 0, 0, inin.G, adj_face.cv, adj_face.f);
5006 if (inin.ctx.have_pgp() && inin.ctx.is_convex_num_valid()) {
5007 inin.ctx.set_ii(ipt);
5009 inin.has_ctx =
true;
5010 inin.pt_y = inin.ctx.xreal();
5012 gmm::scale(inin.Normal, 1.0/gmm::vect_norm2(inin.Normal));
5019 inin.pt_type = trans->transform(workspace, m, ctx, dummy_normal,
5020 &(inin.m), cv, face_num, P_ref,
5021 dummy_normal, inin.derivatives,
5025 inin.m->points_of_convex(cv, inin.G);
5026 inin.ctx.change((inin.m)->trans_of_convex(cv),
5027 0, P_ref, inin.G, cv, face_num);
5028 inin.has_ctx =
true;
5031 gmm::scale(inin.Normal, 1.0/gmm::vect_norm2(inin.Normal));
5034 inin.pt_y = inin.ctx.xreal();
5036 inin.ctx.invalid_convex_num();
5038 inin.has_ctx =
false;
5041 inin.ctx.invalid_convex_num();
5042 inin.Normal.resize(0);
5043 inin.pt_y.resize(0);
5044 inin.has_ctx =
false;
5047 GA_DEBUG_INFO(
"Instruction: end of call neighbor interpolate "
5051 ga_instruction_neighbor_transformation_call
5052 (
const ga_workspace &w, ga_instruction_set::interpolate_info &i,
5053 pinterpolate_transformation t, fem_interpolation_context &ctxx,
5054 const mesh &mm,
size_type &ipt_, papprox_integration &pai_,
5056 std::map<gauss_pt_corresp, bgeot::pstored_point_tab> &neighbor_corresp_)
5057 : workspace(w), inin(i), trans(t), ctx(ctxx), m(mm),
5058 ipt(ipt_), pai(pai_), gp_pool(gp_pool_),
5059 neighbor_corresp(neighbor_corresp_) {}
5063 struct ga_instruction_scalar_assembly :
public ga_instruction {
5064 const base_tensor &t;
5065 scalar_type &E, &coeff;
5066 virtual int exec() {
5067 GA_DEBUG_INFO(
"Instruction: scalar term assembly");
5071 ga_instruction_scalar_assembly(
const base_tensor &t_, scalar_type &E_,
5072 scalar_type &coeff_)
5073 : t(t_), E(E_), coeff(coeff_) {}
5076 struct ga_instruction_vector_assembly_mf :
public ga_instruction
5078 const base_tensor &t;
5079 base_vector &VI, &Vi;
5080 const fem_interpolation_context &ctx;
5081 const gmm::sub_interval *
const&I, *
const I__;
5082 const mesh_fem *
const&mf, *
const mf__;
5083 const bool &reduced_mf;
5084 const scalar_type &coeff;
5087 const bool interpolate;
5088 virtual int exec() {
5089 GA_DEBUG_INFO(
"Instruction: vector term assembly for fem variable");
5090 bool empty_weight = (coeff == scalar_type(0));
5091 if (ipt == 0 || interpolate) {
5092 if (empty_weight) elem.resize(0);
5093 elem.resize(t.size());
5095 copy_scaled_4(t, coeff, elem);
5096 }
else if (!empty_weight)
5098 add_scaled_4(t, coeff, elem);
5100 if (ipt == nbpt-1 || interpolate) {
5101 GA_DEBUG_ASSERT(mf,
"Internal error");
5102 if (!ctx.is_convex_num_valid())
return 0;
5105 if (qmult > 1) qmult /= mf->fem_of_element(cv_1)->target_dim();
5106 base_vector &V = reduced_mf ? Vi : VI;
5107 GA_DEBUG_ASSERT(V.size() >= I->first() + mf->nb_basic_dof(),
5108 "Bad assembly vector size " << V.size() <<
">=" <<
5109 I->first() <<
"+"<< mf->nb_basic_dof());
5110 auto itr = elem.cbegin();
5111 auto itw = V.begin() + I->first();
5112 for (
const auto &dof : mf->ind_scalar_basic_dof_of_element(cv_1))
5114 *(itw+dof+q) += *itr++;
5115 GMM_ASSERT1(itr == elem.end(),
"Internal error");
5120 ga_instruction_vector_assembly_mf
5121 (
const base_tensor &t_, base_vector &VI_, base_vector &Vi_,
5122 const fem_interpolation_context &ctx_,
5123 const gmm::sub_interval *&I_,
const mesh_fem *&mf_,
5124 const bool &reduced_mf_,
5127 : t(t_), VI(VI_), Vi(Vi_), ctx(ctx_),
5128 I(I_), I__(nullptr), mf(mf_), mf__(nullptr), reduced_mf(reduced_mf_),
5129 coeff(coeff_), nbpt(nbpt_), ipt(ipt_), interpolate(interpolate_) {}
5131 ga_instruction_vector_assembly_mf
5132 (
const base_tensor &t_, base_vector &V_,
5133 const fem_interpolation_context &ctx_,
5134 const gmm::sub_interval &I_,
const mesh_fem &mf_,
5137 : t(t_), VI(V_), Vi(V_), ctx(ctx_),
5138 I(I__), I__(&I_), mf(mf__), mf__(&mf_), reduced_mf(false_),
5139 coeff(coeff_), nbpt(nbpt_), ipt(ipt_), interpolate(interpolate_) {}
5141 const bool false_=
false;
5144 struct ga_instruction_vector_assembly_imd :
public ga_instruction {
5145 const base_tensor &t;
5147 const fem_interpolation_context &ctx;
5148 const gmm::sub_interval &I;
5152 const bool initialize;
5153 virtual int exec() {
5154 GA_DEBUG_INFO(
"Instruction: vector term assembly for im_data variable");
5156 size_type i = t.size() * imd.filtered_index_of_point(cv, ctx.ii());
5157 GMM_ASSERT1(i+t.size() <= I.size(),
5158 "Internal error "<<i<<
"+"<<t.size()<<
" <= "<<I.size());
5159 auto itw = V.begin() + I.first() + i;
5161 for (
const auto &val : t.as_vector())
5164 for (
const auto &val : t.as_vector())
5165 *itw++ += coeff*val;
5168 ga_instruction_vector_assembly_imd
5169 (
const base_tensor &t_, base_vector &V_,
5170 const fem_interpolation_context &ctx_,
const gmm::sub_interval &I_,
5171 const im_data &imd_, scalar_type &coeff_,
const size_type &ipt_,
5172 bool initialize_=
false)
5173 : t(t_), V(V_), ctx(ctx_), I(I_), imd(imd_), coeff(coeff_), ipt(ipt_),
5174 initialize(initialize_)
5178 struct ga_instruction_vector_assembly :
public ga_instruction {
5179 const base_tensor &t;
5181 const gmm::sub_interval &I;
5183 virtual int exec() {
5184 GA_DEBUG_INFO(
"Instruction: vector term assembly for "
5185 "fixed size variable");
5186 gmm::add(gmm::scaled(t.as_vector(), coeff), gmm::sub_vector(V, I));
5189 ga_instruction_vector_assembly(
const base_tensor &t_, base_vector &V_,
5190 const gmm::sub_interval &I_,
5191 scalar_type &coeff_)
5192 : t(t_), V(V_), I(I_), coeff(coeff_) {}
5195 struct ga_instruction_assignment :
public ga_instruction {
5196 const base_tensor &t;
5198 const fem_interpolation_context &ctx;
5200 virtual int exec() {
5201 GA_DEBUG_INFO(
"Instruction: Assignment to im_data");
5202 imd->set_tensor(V, ctx.convex_num(), ctx.ii(), t);
5205 ga_instruction_assignment(
const base_tensor &t_, base_vector &V_,
5206 const fem_interpolation_context &ctx_,
5207 const im_data *imd_)
5208 : t(t_), V(V_), ctx(ctx_), imd(imd_) {}
5211 struct ga_instruction_extract_residual_on_imd_dofs :
public ga_instruction {
5213 const base_vector &V;
5214 const fem_interpolation_context &ctx;
5215 const gmm::sub_interval &I;
5218 virtual int exec() {
5219 GA_DEBUG_INFO(
"Instruction: extract residual for im_data variable");
5222 size_type i = t.size() * imd.filtered_index_of_point(cv, ctx.ii());
5223 GMM_ASSERT1(i+t.size() <= I.size(),
5224 "Internal error "<<i<<
"+"<<t.size()<<
" <= "<<I.size());
5225 for (
auto &&val : t.as_vector())
5226 val = V[ifirst+(i++)];
5229 ga_instruction_extract_residual_on_imd_dofs
5230 (base_tensor &t_,
const base_vector &V_,
5231 const fem_interpolation_context &ctx_,
const gmm::sub_interval &I_,
5232 const im_data &imd_,
const size_type &ipt_)
5233 : t(t_), V(V_), ctx(ctx_), I(I_), imd(imd_), ipt(ipt_)
5238 template <
class MAT>
5239 inline void add_elem_matrix
5240 (MAT &K,
const std::vector<size_type> &dofs1,
5241 const std::vector<size_type> &dofs2, std::vector<size_type> &,
5242 const base_vector &elem, scalar_type threshold,
size_type ) {
5244 base_vector::const_iterator it = elem.cbegin();
5247 if (gmm::abs(*it) > threshold)
5248 K(dof1, dof2) += *it;
5260 inline void add_elem_matrix
5262 const std::vector<size_type> &dofs1,
const std::vector<size_type> &dofs2,
5263 std::vector<size_type> &dofs1_sort,
5264 const base_vector &elem, scalar_type threshold,
size_type N) {
5268 dofs1_sort.resize(s1);
5271 while (j > 0 && dofs1[i] < dofs1[dofs1_sort[k]])
5272 { dofs1_sort[j] = dofs1_sort[k]; j--; k--; }
5281 gmm::elt_rsvector_<scalar_type> ev;
5284 base_vector::const_iterator it = elem.cbegin();
5287 if (first) first =
false;
5289 std::vector<gmm::elt_rsvector_<scalar_type>> &col = K[dof2];
5293 col.reserve(maxest);
5296 if (gmm::abs(ev.e) > threshold) {
5305 if (gmm::abs(ev.e) > threshold) {
5312 if (col[l].c < ev.c) {
5320 auto itc = col.begin() + ind;
5321 if (ind != nb && itc->c == ev.c)
5324 if (nb - ind > 1300)
5325 GMM_WARNING2(
"Inefficient addition of element in rsvector with "
5326 << col.size() - ind <<
" non-zero entries");
5329 itc = col.begin() + ind;
5330 auto ite = col.end();
5333 for (; ite != itc; --ite) { --itee; *ite = *itee; }
5346 inline void add_elem_matrix_contiguous_rows
5349 const std::vector<size_type> &dofs2,
5350 const base_vector &elem, scalar_type threshold) {
5352 gmm::elt_rsvector_<scalar_type> ev;
5354 base_vector::const_iterator it = elem.cbegin();
5357 if (first) first =
false;
5359 std::vector<gmm::elt_rsvector_<scalar_type>> &col = K[dof2];
5366 if (gmm::abs(ev.e) > threshold) {
5375 if (gmm::abs(ev.e) > threshold) {
5382 if (col[l].c < ev.c) {
5390 auto itc = col.begin() + ind;
5391 if (ind != nb && itc->c == ev.c)
5394 if (nb - ind > 1300)
5395 GMM_WARNING2(
"Inefficient addition of element in rsvector with "
5396 << col.size() - ind <<
" non-zero entries");
5399 itc = col.begin() + ind;
5400 auto ite = col.end();
5403 for (; ite != itc; --ite) { --itee; *ite = *itee; }
5415 inline void populate_dofs_vector
5416 (std::vector<size_type> &dofs,
5418 const getfem::mesh::ind_set &mfdofs)
5420 dofs.assign(size, ifirst);
5421 auto itd = dofs.begin();
5423 for (
const auto &dof : mfdofs) *itd++ += dof;
5425 for (
const auto &dof : mfdofs)
5426 for (
size_type q = 0; q < qmult; ++q) *itd++ += dof + q;
5429 inline void populate_dofs_vector
5431 const getfem::mesh::ind_set &mfdofs)
5433 dofs.assign(size, ifirst);
5434 auto itd = dofs.begin();
5435 for (
const auto &dof : mfdofs) *itd++ += dof;
5439 inline void populate_contiguous_dofs_vector
5442 dofs.assign(size, ifirst);
5443 for (
size_type i=0; i < size; ++i) dofs[i] += i;
5446 struct ga_instruction_matrix_assembly_base :
public ga_instruction {
5447 const base_tensor &t;
5448 const fem_interpolation_context &ctx1, &ctx2;
5449 const scalar_type &alpha1, &alpha2, &coeff;
5453 std::vector<size_type> dofs1, dofs2, dofs1_sort;
5454 void add_tensor_to_element_matrix(
bool initialize,
bool empty_weight) {
5456 if (empty_weight) elem.resize(0);
5457 elem.resize(t.size());
5459 copy_scaled_4(t, coeff*alpha1*alpha2, elem);
5460 }
else if (!empty_weight)
5463 add_scaled_4(t, coeff*alpha1*alpha2, elem);
5465 ga_instruction_matrix_assembly_base
5466 (
const base_tensor &t_,
5467 const fem_interpolation_context &ctx1_,
5468 const fem_interpolation_context &ctx2_,
5469 const scalar_type &a1,
const scalar_type &a2,
const scalar_type &coeff_,
5471 : t(t_), ctx1(ctx1_), ctx2(ctx2_), alpha1(a1), alpha2(a2),
5472 coeff(coeff_), nbpt(nbpt_), ipt(ipt_), interpolate(interpolate_),
5473 dofs1(0), dofs2(0), dofs1_sort(0)
5476 const bool false_=
false;
5481 struct ga_instruction_matrix_assembly_mf_mf
5482 :
public ga_instruction_matrix_assembly_base
5484 model_real_sparse_matrix &Krr, &Kru, &Kur, &Kuu;
5485 const gmm::sub_interval *
const&I1, *
const&I2, *
const I1__, *
const I2__;
5486 const mesh_fem *
const&mf1, *
const&mf2, *
const mf1__, *
const mf2__;
5487 const bool &reduced_mf1, &reduced_mf2;
5488 virtual int exec() {
5489 GA_DEBUG_INFO(
"Instruction: matrix term assembly mf-mf");
5490 if (!ctx1.is_convex_num_valid() || !ctx2.is_convex_num_valid())
return 0;
5492 bool initialize = (ipt == 0 || interpolate);
5493 bool empty_weight = (coeff == scalar_type(0));
5494 add_tensor_to_element_matrix(initialize, empty_weight);
5496 if (ipt == nbpt-1 || interpolate) {
5497 model_real_sparse_matrix &K = reduced_mf1 ? (reduced_mf2 ? Kuu : Kur)
5498 : (reduced_mf2 ? Kru : Krr);
5499 GA_DEBUG_ASSERT(I1->size() && I2->size(),
"Internal error");
5502 if (ninf == scalar_type(0))
return 0;
5504 size_type s1 = t.sizes()[0], s2 = t.sizes()[1];
5505 size_type cv1 = ctx1.convex_num(), cv2 = ctx2.convex_num();
5506 size_type ifirst1 = I1->first(), ifirst2 = I2->first();
5510 if (qmult1 > 1) qmult1 /= mf1->fem_of_element(cv1)->target_dim();
5511 populate_dofs_vector(dofs1, s1, ifirst1, qmult1,
5512 mf1->ind_scalar_basic_dof_of_element(cv1));
5513 if (mf1 == mf2 && cv1 == cv2) {
5514 if (ifirst1 == ifirst2) {
5515 add_elem_matrix(K, dofs1, dofs1, dofs1_sort, elem, ninf*1E-14, N);
5517 populate_dofs_vector(dofs2, dofs1.size(), ifirst2 - ifirst1, dofs1);
5518 add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, N);
5521 N = std::max(N, ctx2.N());
5523 if (qmult2 > 1) qmult2 /= mf2->fem_of_element(cv2)->target_dim();
5524 populate_dofs_vector(dofs2, s2, ifirst2, qmult2,
5525 mf2->ind_scalar_basic_dof_of_element(cv2));
5526 add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, N);
5532 ga_instruction_matrix_assembly_mf_mf
5533 (
const base_tensor &t_,
5534 model_real_sparse_matrix &Krr_, model_real_sparse_matrix &Kru_,
5535 model_real_sparse_matrix &Kur_, model_real_sparse_matrix &Kuu_,
5536 const fem_interpolation_context &ctx1_,
5537 const fem_interpolation_context &ctx2_,
5538 const ga_instruction_set::variable_group_info &vgi1,
5539 const ga_instruction_set::variable_group_info &vgi2,
5542 : ga_instruction_matrix_assembly_base
5543 (t_, ctx1_, ctx2_, vgi1.
alpha, vgi2.
alpha, coeff_, nbpt_, ipt_,
5545 Krr(Krr_), Kru(Kru_), Kur(Kur_), Kuu(Kuu_),
5546 I1(vgi1.I), I2(vgi2.I), I1__(nullptr), I2__(nullptr),
5547 mf1(vgi1.mf), mf2(vgi2.mf), mf1__(nullptr), mf2__(nullptr),
5548 reduced_mf1(vgi1.reduced_mf), reduced_mf2(vgi2.reduced_mf) {}
5550 ga_instruction_matrix_assembly_mf_mf
5551 (
const base_tensor &t_,
5552 model_real_sparse_matrix &Kxr_, model_real_sparse_matrix &Kxu_,
5553 const fem_interpolation_context &ctx1_,
5554 const fem_interpolation_context &ctx2_,
5555 const gmm::sub_interval &I1_,
const mesh_fem &mf1_,
const scalar_type &a1,
5556 const ga_instruction_set::variable_group_info &vgi2,
5559 : ga_instruction_matrix_assembly_base
5560 (t_, ctx1_, ctx2_, a1, vgi2.
alpha, coeff_, nbpt_, ipt_, interpolate_),
5561 Krr(Kxr_), Kru(Kxu_), Kur(Kxr_), Kuu(Kxu_),
5562 I1(I1__), I2(vgi2.I), I1__(&I1_), I2__(nullptr),
5563 mf1(mf1__), mf2(vgi2.mf), mf1__(&mf1_), mf2__(nullptr),
5564 reduced_mf1(false_), reduced_mf2(vgi2.reduced_mf) {}
5566 ga_instruction_matrix_assembly_mf_mf
5567 (
const base_tensor &t_,
5568 model_real_sparse_matrix &Krx_, model_real_sparse_matrix &Kux_,
5569 const fem_interpolation_context &ctx1_,
5570 const fem_interpolation_context &ctx2_,
5571 const ga_instruction_set::variable_group_info &vgi1,
5572 const gmm::sub_interval &I2_,
const mesh_fem &mf2_,
const scalar_type &a2,
5575 : ga_instruction_matrix_assembly_base
5576 (t_, ctx1_, ctx2_, vgi1.
alpha, a2, coeff_, nbpt_, ipt_, interpolate_),
5577 Krr(Krx_), Kru(Krx_), Kur(Kux_), Kuu(Kux_),
5578 I1(vgi1.I), I2(I2__), I1__(nullptr), I2__(&I2_),
5579 mf1(vgi1.mf), mf2(mf2__), mf1__(nullptr), mf2__(&mf2_),
5580 reduced_mf1(vgi1.reduced_mf), reduced_mf2(false_) {}
5582 ga_instruction_matrix_assembly_mf_mf
5583 (
const base_tensor &t_, model_real_sparse_matrix &K_,
5584 const fem_interpolation_context &ctx1_,
5585 const fem_interpolation_context &ctx2_,
5586 const gmm::sub_interval &I1_,
const mesh_fem &mf1_,
const scalar_type &a1,
5587 const gmm::sub_interval &I2_,
const mesh_fem &mf2_,
const scalar_type &a2,
5590 : ga_instruction_matrix_assembly_base
5591 (t_, ctx1_, ctx2_, a1, a2, coeff_, nbpt_, ipt_, interpolate_),
5592 Krr(K_), Kru(K_), Kur(K_), Kuu(K_),
5593 I1(I1__), I2(I2__), I1__(&I1_), I2__(&I2_),
5594 mf1(mf1__), mf2(mf2__), mf1__(&mf1_), mf2__(&mf2_),
5595 reduced_mf1(false_), reduced_mf2(false_) {}
5599 struct ga_instruction_matrix_assembly_imd_mf
5600 :
public ga_instruction_matrix_assembly_base
5602 model_real_sparse_matrix &Kxr, &Kxu;
5603 const gmm::sub_interval *I1, *I2__, *
const &I2;
5604 const im_data *imd1;
5605 const mesh_fem *
const mf2__, *
const &mf2;
5606 const bool &reduced_mf2;
5607 virtual int exec() {
5608 GA_DEBUG_INFO(
"Instruction: matrix term assembly "
5609 "(imdata or fixed size)-mf");
5610 if (!ctx1.is_convex_num_valid() || !ctx2.is_convex_num_valid())
return 0;
5612 bool empty_weight = (coeff == scalar_type(0));
5613 add_tensor_to_element_matrix(
true, empty_weight);
5616 if (ninf == scalar_type(0))
return 0;
5618 model_real_sparse_matrix &K = reduced_mf2 ? Kxu : Kxr;
5619 GA_DEBUG_ASSERT(I1->size() && I2->size(),
"Internal error");
5620 size_type s1 = t.sizes()[0], s2 = t.sizes()[1];
5621 size_type cv1 = ctx1.convex_num(), cv2 = ctx2.convex_num();
5622 size_type ifirst1 = I1->first(), ifirst2 = I2->first();
5623 if (imd1) ifirst1 += s1 * imd1->filtered_index_of_point(cv1, ctx1.ii());
5625 populate_contiguous_dofs_vector(dofs1, s1, ifirst1);
5627 if (qmult2 > 1) qmult2 /= mf2->fem_of_element(cv2)->target_dim();
5628 populate_dofs_vector(dofs2, s2, ifirst2, qmult2,
5629 mf2->ind_scalar_basic_dof_of_element(cv2));
5630 add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, ctx2.N());
5634 ga_instruction_matrix_assembly_imd_mf
5635 (
const base_tensor &t_,
5636 model_real_sparse_matrix &Kxr_, model_real_sparse_matrix &Kxu_,
5637 const fem_interpolation_context &ctx1_,
5638 const fem_interpolation_context &ctx2_,
5639 const gmm::sub_interval &I1_,
const im_data *imd1_,
const scalar_type &a1,
5640 const ga_instruction_set::variable_group_info &vgi2,
5641 const scalar_type &coeff_,
const size_type &ipt_)
5642 : ga_instruction_matrix_assembly_base
5643 (t_, ctx1_, ctx2_, a1, vgi2.
alpha, coeff_, zero_, ipt_, false),
5644 Kxr(Kxr_), Kxu(Kxu_), I1(&I1_), I2__(nullptr), I2(vgi2.I),
5645 imd1(imd1_), mf2__(nullptr), mf2(vgi2.mf), reduced_mf2(vgi2.reduced_mf)
5648 ga_instruction_matrix_assembly_imd_mf
5649 (
const base_tensor &t_, model_real_sparse_matrix &K_,
5650 const fem_interpolation_context &ctx1_,
5651 const fem_interpolation_context &ctx2_,
5652 const gmm::sub_interval &I1_,
const im_data *imd1_,
const scalar_type &a1,
5653 const gmm::sub_interval &I2_,
const mesh_fem &mf2_,
const scalar_type &a2,
5654 const scalar_type &coeff_,
const size_type &ipt_)
5655 : ga_instruction_matrix_assembly_base
5656 (t_, ctx1_, ctx2_, a1, a2, coeff_, zero_, ipt_, false),
5657 Kxr(K_), Kxu(K_), I1(&I1_), I2__(&I2_), I2(I2__),
5658 imd1(imd1_), mf2__(&mf2_), mf2(mf2__), reduced_mf2(false_) {}
5661 struct ga_instruction_matrix_assembly_mf_imd
5662 :
public ga_instruction_matrix_assembly_base
5664 model_real_sparse_matrix &Krx, &Kux;
5665 const gmm::sub_interval *
const &I1, *
const I1__, *I2;
5666 const mesh_fem *
const &mf1, *
const mf1__;
5667 const bool &reduced_mf1;
5668 const im_data *imd2;
5669 virtual int exec() {
5670 GA_DEBUG_INFO(
"Instruction: matrix term assembly "
5671 "mf-(imdata or fixed size)");
5672 if (!ctx1.is_convex_num_valid() || !ctx2.is_convex_num_valid())
return 0;
5674 bool empty_weight = (coeff == scalar_type(0));
5675 add_tensor_to_element_matrix(
true, empty_weight);
5678 if (ninf == scalar_type(0))
return 0;
5680 model_real_sparse_matrix &K = reduced_mf1 ? Kux : Krx;
5681 GA_DEBUG_ASSERT(I1->size() && I2->size(),
"Internal error");
5682 size_type s1 = t.sizes()[0], s2 = t.sizes()[1];
5683 size_type cv1 = ctx1.convex_num(), cv2 = ctx2.convex_num();
5684 size_type ifirst1 = I1->first(), ifirst2 = I2->first();
5685 if (imd2) ifirst2 += s2 * imd2->filtered_index_of_point(cv2, ctx2.ii());
5688 if (qmult1 > 1) qmult1 /= mf1->fem_of_element(cv1)->target_dim();
5689 populate_dofs_vector(dofs1, s1, ifirst1, qmult1,
5690 mf1->ind_scalar_basic_dof_of_element(cv1));
5691 populate_contiguous_dofs_vector(dofs2, s2, ifirst2);
5692 add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, ctx1.N());
5696 ga_instruction_matrix_assembly_mf_imd
5697 (
const base_tensor &t_,
5698 model_real_sparse_matrix &Krx_, model_real_sparse_matrix &Kux_,
5699 const fem_interpolation_context &ctx1_,
5700 const fem_interpolation_context &ctx2_,
5701 const ga_instruction_set::variable_group_info &vgi1,
5702 const gmm::sub_interval &I2_,
const im_data *imd2_,
const scalar_type &a2,
5703 const scalar_type &coeff_,
const size_type &ipt_)
5704 : ga_instruction_matrix_assembly_base
5705 (t_, ctx1_, ctx2_, vgi1.
alpha, a2, coeff_, zero_, ipt_, false),
5706 Krx(Krx_), Kux(Kux_), I1(vgi1.I), I1__(nullptr), I2(&I2_),
5707 mf1(vgi1.mf), mf1__(nullptr), reduced_mf1(vgi1.reduced_mf), imd2(imd2_)
5710 ga_instruction_matrix_assembly_mf_imd
5711 (
const base_tensor &t_, model_real_sparse_matrix &K_,
5712 const fem_interpolation_context &ctx1_,
5713 const fem_interpolation_context &ctx2_,
5714 const gmm::sub_interval &I1_,
const mesh_fem &mf1_,
const scalar_type &a1,
5715 const gmm::sub_interval &I2_,
const im_data *imd2_,
const scalar_type &a2,
5716 const scalar_type &coeff_,
const size_type &ipt_)
5717 : ga_instruction_matrix_assembly_base
5718 (t_, ctx1_, ctx2_, a1, a2, coeff_, zero_, ipt_, false),
5719 Krx(K_), Kux(K_), I1(I1__), I1__(&I1_), I2(&I2_),
5720 mf1(mf1__), mf1__(&mf1_), reduced_mf1(false_), imd2(imd2_) {}
5725 struct ga_instruction_matrix_assembly_imd_imd
5726 :
public ga_instruction_matrix_assembly_base
5728 model_real_sparse_matrix &K;
5729 const gmm::sub_interval &I1, &I2;
5730 const im_data *imd1, *imd2;
5731 virtual int exec() {
5732 GA_DEBUG_INFO(
"Instruction: matrix term assembly "
5733 "(imdata or fixed size)-(imdata or fixed size)");
5734 GA_DEBUG_ASSERT(I1.size() && I2.size(),
"Internal error");
5736 bool empty_weight = (coeff == scalar_type(0));
5737 add_tensor_to_element_matrix(
true, empty_weight);
5740 if (ninf == scalar_type(0))
return 0;
5742 size_type s1 = t.sizes()[0], s2 = t.sizes()[1];
5743 size_type ifirst1 = I1.first(), ifirst2 = I2.first();
5745 ifirst1 += s1 * imd1->filtered_index_of_point(ctx1.convex_num(), ctx1.ii());
5747 ifirst2 += s2 * imd2->filtered_index_of_point(ctx2.convex_num(), ctx2.ii());
5749 populate_contiguous_dofs_vector(dofs2, s2, ifirst2);
5750 add_elem_matrix_contiguous_rows(K, ifirst1, s1, dofs2, elem, ninf*1E-14);
5753 ga_instruction_matrix_assembly_imd_imd
5754 (
const base_tensor &t_, model_real_sparse_matrix &K_,
5755 const fem_interpolation_context &ctx1_,
5756 const fem_interpolation_context &ctx2_,
5757 const gmm::sub_interval &I1_,
const im_data *imd1_,
const scalar_type &a1,
5758 const gmm::sub_interval &I2_,
const im_data *imd2_,
const scalar_type &a2,
5759 const scalar_type &coeff_,
const size_type &ipt_)
5760 : ga_instruction_matrix_assembly_base
5761 (t_, ctx1_, ctx2_, a1, a2, coeff_, zero_, ipt_, false),
5762 K(K_), I1(I1_), I2(I2_), imd1(imd1_), imd2(imd2_) {}
5766 struct ga_instruction_matrix_assembly_standard_scalar
5767 :
public ga_instruction_matrix_assembly_base
5769 model_real_sparse_matrix &K;
5770 const gmm::sub_interval &I1, &I2;
5771 const mesh_fem *pmf1, *pmf2;
5772 virtual int exec() {
5773 GA_DEBUG_INFO(
"Instruction: matrix term assembly for standard "
5776 elem.resize(t.size());
5778 copy_scaled_4(t, coeff*alpha1*alpha2, elem);
5782 add_scaled_4(t, coeff*alpha1*alpha2, elem);
5784 if (ipt == nbpt-1) {
5785 GA_DEBUG_ASSERT(I1.size() && I2.size(),
"Internal error");
5788 if (ninf == scalar_type(0))
return 0;
5790 size_type cv1 = ctx1.convex_num(), cv2 = ctx2.convex_num(), N=ctx1.N();
5792 auto &ct1 = pmf1->ind_scalar_basic_dof_of_element(cv1);
5793 GA_DEBUG_ASSERT(ct1.size() == t.sizes()[0],
"Internal error");
5794 populate_dofs_vector(dofs1, ct1.size(), I1.first(), ct1);
5796 if (pmf2 == pmf1 && cv1 == cv2) {
5797 if (I1.first() == I2.first()) {
5798 add_elem_matrix(K, dofs1, dofs1, dofs1_sort, elem, ninf*1E-14, N);
5800 populate_dofs_vector(dofs2, dofs1.size(), I2.first() - I1.first(),
5802 add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, N);
5806 auto &ct2 = pmf2->ind_scalar_basic_dof_of_element(cv2);
5807 GA_DEBUG_ASSERT(ct2.size() == t.sizes()[1],
"Internal error");
5808 populate_dofs_vector(dofs2, ct2.size(), I2.first(), ct2);
5809 add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, N);
5814 ga_instruction_matrix_assembly_standard_scalar
5815 (
const base_tensor &t_, model_real_sparse_matrix &K_,
5816 const fem_interpolation_context &ctx1_,
5817 const fem_interpolation_context &ctx2_,
5818 const gmm::sub_interval &I1_,
const gmm::sub_interval &I2_,
5819 const mesh_fem *mfn1_,
const mesh_fem *mfn2_,
5820 const scalar_type &a1,
const scalar_type &a2,
const scalar_type &coeff_,
5822 : ga_instruction_matrix_assembly_base
5823 (t_, ctx1_, ctx2_, a1, a2, coeff_, nbpt_, ipt_, false),
5824 K(K_), I1(I1_), I2(I2_), pmf1(mfn1_), pmf2(mfn2_) {}
5827 struct ga_instruction_matrix_assembly_standard_vector
5828 :
public ga_instruction_matrix_assembly_base
5830 model_real_sparse_matrix &K;
5831 const gmm::sub_interval &I1, &I2;
5832 const mesh_fem *pmf1, *pmf2;
5833 virtual int exec() {
5834 GA_DEBUG_INFO(
"Instruction: matrix term assembly for standard "
5837 elem.resize(t.size());
5838 copy_scaled_8(t, coeff*alpha1*alpha2, elem);
5843 add_scaled_8(t, coeff*alpha1*alpha2, elem);
5845 if (ipt == nbpt-1) {
5846 GA_DEBUG_ASSERT(I1.size() && I2.size(),
"Internal error");
5849 if (ninf == scalar_type(0))
return 0;
5850 size_type s1 = t.sizes()[0], s2 = t.sizes()[1], N = ctx1.N();
5852 size_type cv1 = ctx1.convex_num(), cv2 = ctx2.convex_num();
5855 if (qmult1 > 1) qmult1 /= pmf1->fem_of_element(cv1)->target_dim();
5856 populate_dofs_vector(dofs1, s1, I1.first(), qmult1,
5857 pmf1->ind_scalar_basic_dof_of_element(cv1));
5859 if (pmf2 == pmf1 && cv1 == cv2 && I1.first() == I2.first()) {
5860 add_elem_matrix(K, dofs1, dofs1, dofs1_sort, elem, ninf*1E-14, N);
5862 if (pmf2 == pmf1 && cv1 == cv2) {
5863 populate_dofs_vector(dofs2, dofs1.size(), I2.first() - I1.first(),
5868 if (qmult2 > 1) qmult2 /= pmf2->fem_of_element(cv2)->target_dim();
5869 populate_dofs_vector(dofs2, s2, I2.first(), qmult2,
5870 pmf2->ind_scalar_basic_dof_of_element(cv2));
5872 add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, N);
5877 ga_instruction_matrix_assembly_standard_vector
5878 (
const base_tensor &t_, model_real_sparse_matrix &K_,
5879 const fem_interpolation_context &ctx1_,
5880 const fem_interpolation_context &ctx2_,
5881 const gmm::sub_interval &I1_,
const gmm::sub_interval &I2_,
5882 const mesh_fem *mfn1_,
const mesh_fem *mfn2_,
5883 const scalar_type &a1,
const scalar_type &a2,
const scalar_type &coeff_,
5885 : ga_instruction_matrix_assembly_base
5886 (t_, ctx1_, ctx2_, a1, a2, coeff_, nbpt_, ipt_, false),
5887 K(K_), I1(I1_), I2(I2_), pmf1(mfn1_), pmf2(mfn2_) {}
5891 struct ga_instruction_matrix_assembly_standard_vector_opt10
5892 :
public ga_instruction_matrix_assembly_base
5894 model_real_sparse_matrix &K;
5895 const gmm::sub_interval &I1, &I2;
5896 const mesh_fem *pmf1, *pmf2;
5897 virtual int exec() {
5898 GA_DEBUG_INFO(
"Instruction: matrix term assembly for standard "
5899 "vector fems optimized for format 10 qdim " << QQ);
5901 size_type ss1 = t.sizes()[0]/QQ, ss2 = t.sizes()[1]/QQ;
5902 scalar_type e = coeff*alpha1*alpha2;
5904 elem.resize(ss1*ss2);
5905 auto itel = elem.begin();
5907 auto it = t.begin() + j*s1_q;
5908 for (
size_type i = 0; i < ss1; ++i, it += QQ)
5909 *itel++ = (*it) * e;
5912 auto itel = elem.begin();
5914 auto it = t.begin() + j*s1_q;
5915 for (
size_type i = 0; i < ss1; ++i, it += QQ)
5916 *itel++ += (*it) * e;
5919 if (ipt == nbpt-1) {
5920 GA_DEBUG_ASSERT(I1.size() && I2.size(),
"Internal error");
5923 if (ninf == scalar_type(0))
return 0;
5925 size_type cv1 = ctx1.convex_num(), cv2 = ctx2.convex_num();
5926 size_type i1 = I1.first(), i2 = I2.first();
5928 populate_dofs_vector(dofs1, ss1, i1,
5929 pmf1->ind_scalar_basic_dof_of_element(cv1));
5930 bool same_dofs(pmf2 == pmf1 && cv1 == cv2 && i1 == i2);
5934 populate_dofs_vector(dofs2, ss2, i2,
5935 pmf2->ind_scalar_basic_dof_of_element(cv2));
5937 std::vector<size_type> &dofs2_ = same_dofs ? dofs1 : dofs2;
5938 add_elem_matrix(K, dofs1, dofs2_, dofs1_sort, elem, ninf, N);
5939 for (
size_type i = 0; i < ss1; ++i) (dofs1[i])++;
5940 if (!same_dofs)
for (
size_type i = 0; i < ss2; ++i) (dofs2[i])++;
5941 add_elem_matrix(K, dofs1, dofs2_, dofs1_sort, elem, ninf, N);
5943 for (
size_type i = 0; i < ss1; ++i) (dofs1[i])++;
5944 if (!same_dofs)
for (
size_type i = 0; i < ss2; ++i) (dofs2[i])++;
5945 add_elem_matrix(K, dofs1, dofs2_, dofs1_sort, elem, ninf, N);
5951 ga_instruction_matrix_assembly_standard_vector_opt10
5952 (
const base_tensor &t_, model_real_sparse_matrix &Kn_,
5953 const fem_interpolation_context &ctx1_,
5954 const fem_interpolation_context &ctx2_,
5955 const gmm::sub_interval &In1_,
const gmm::sub_interval &In2_,
5956 const mesh_fem *mfn1_,
const mesh_fem *mfn2_,
5957 const scalar_type &a1,
const scalar_type &a2,
const scalar_type &coeff_,
5959 : ga_instruction_matrix_assembly_base
5960 (t_, ctx1_, ctx2_, a1, a2, coeff_, nbpt_, ipt_, false),
5961 K(Kn_), I1(In1_), I2(In2_), pmf1(mfn1_), pmf2(mfn2_)
5963 static_assert(QQ >= 2 && QQ <=3,
5964 "Template implemented only for QQ=2 and QQ=3");
5969 struct ga_instruction_condensation_sub :
public ga_instruction {
5972 gmm::dense_matrix<base_tensor *> KQJprime;
5973 std::vector<base_tensor *> RQprime;
5974 gmm::dense_matrix<base_tensor const *> KQQloc, KQJloc;
5975 base_tensor invKqqqq, Kqqjj;
5977 std::vector<std::array<size_type,3>> partQ, partJ;
5978 const scalar_type &coeff;
5979 virtual int exec() {
5980 GA_DEBUG_INFO(
"Instruction: variable cluster subdiagonal condensation");
5982 for (
const auto &qqq1 : partQ) {
5983 size_type q1 = qqq1[0], qq1start = qqq1[1], qq1end = qqq1[2];
5984 for (
const auto &qqq2 : partQ) {
5985 size_type q2 = qqq2[0], qq2start = qqq2[1], qq2end = qqq2[2];
5986 if (KQQloc(q1,q2)) {
5987 auto itr = KQQloc(q1,q2)->cbegin();
5988 GMM_ASSERT1(KQQloc(q1,q2)->size()
5989 == (qq1end-qq1start)*(qq2end-qq2start),
5991 for (
size_type qq2=qq2start; qq2 < qq2end; ++qq2)
5992 for (
size_type qq1=qq1start; qq1 < qq1end; ++qq1)
5993 invKqqqq(qq1,qq2) = *itr++;
5998 bgeot::lu_inverse(&(invKqqqq[0]), invKqqqq.size(0));
6002 for (
auto &&jjj : partJ) {
6005 for (
const auto &qqq : partQ) {
6009 GMM_ASSERT1(new_j == KQJloc(q,j)->size(1),
"Internal error");
6011 new_j = KQJloc(q,j)->size(1);
6015 for (
const auto &qqq : partQ) {
6017 KQJprime(q,j)->adjust_sizes(qqq[2]-qqq[1], new_j);
6024 Kqqjj.adjust_sizes(partQ.back()[2], partJ.back()[2]);
6030 for (
const auto &jjj : partJ) {
6031 size_type j = jjj[0], jjstart = jjj[1], jjend = jjj[2];
6032 for (
const auto &qqq2 : partQ) {
6033 size_type q2 = qqq2[0], qq2start = qqq2[1], qq2end = qqq2[2];
6035 auto itr = KQJloc(q2,j)->begin();
6036 for (
size_type jj=jjstart; jj < jjend; ++jj) {
6037 for (
size_type qq2=qq2start; qq2 < qq2end; ++qq2, ++itr) {
6038 for (
size_type qq1=0; qq1 < partQ.back()[2]; ++qq1) {
6039 Kqqjj(qq1,jj) += invKqqqq(qq1,qq2)*(*itr);
6044 GMM_ASSERT1(itr == KQJloc(q2,j)->cend(),
"Internal error");
6048 for (
const auto &qqq2 : partQ) {
6049 size_type q2 = qqq2[0], qq2start = qqq2[1], qq2end = qqq2[2];
6051 auto itr = RQprime[q2]->cbegin();
6052 for (
size_type qq2=qq2start; qq2 < qq2end; ++qq2, ++itr) {
6053 for (
size_type qq1=0; qq1 < invKqqqq.size(0); ++qq1)
6054 Rqq[qq1] += invKqqqq(qq1,qq2)*(*itr);
6056 GMM_ASSERT1(itr == RQprime[q2]->cend(),
"Internal error");
6062 for (
const auto &qqq1 : partQ) {
6063 size_type q1 = qqq1[0], qq1start = qqq1[1], qq1end = qqq1[2];
6065 auto itw = RQprime[q1]->begin();
6066 for (
size_type qq1=qq1start; qq1 < qq1end; ++qq1)
6067 *itw++ = Rqq[qq1]/coeff;
6069 for (
const auto &jjj2 : partJ) {
6070 size_type j2 = jjj2[0], jj2start = jjj2[1], jj2end = jjj2[2];
6071 auto itw = KQJprime(q1,j2)->begin();
6072 for (
size_type jj2=jj2start; jj2 < jj2end; ++jj2)
6073 for (
size_type qq1=qq1start; qq1 < qq1end; ++qq1)
6074 *itw++ = Kqqjj(qq1,jj2);
6080 ga_instruction_condensation_sub(gmm::dense_matrix<base_tensor *> &KQJpr,
6081 std::vector<base_tensor *> &RQpr,
6082 const gmm::dense_matrix<base_tensor *> &KQQ,
6083 const gmm::dense_matrix<base_tensor *> &KQJ,
6084 const std::set<size_type> &Qset,
6085 const scalar_type &coeff_)
6086 : KQJprime(KQJpr), RQprime(RQpr), coeff(coeff_)
6089 KQQloc.resize(KQQ.nrows(), KQQ.ncols());
6090 KQJloc.resize(KQJ.nrows(), KQJ.ncols());
6091 for (
size_type i=0; i < KQQ.as_vector().size(); ++i) KQQloc[i] = KQQ[i];
6092 for (
size_type i=0; i < KQJ.as_vector().size(); ++i) KQJloc[i] = KQJ[i];
6094 for (
size_type j=0; j < KQJ.ncols(); ++j)
6097 partJ.push_back(std::array<size_type,3>{j,0,0});
6103 partQ.push_back(std::array<size_type,3>{q,0,0});
6105 for (
auto &qqq1 : partQ) {
6110 GMM_ASSERT1(new_q == KQQ(q1,q2)->size(0) &&
6111 new_q == KQQ(q2,q1)->size(1),
"Internal error");
6113 new_q = KQQ(q1,q2)->size(0);
6118 invKqqqq.adjust_sizes(partQ.back()[2], partQ.back()[2]);
6119 Rqq.resize(partQ.back()[2]);
6125 struct ga_instruction_condensation_super_K :
public ga_instruction {
6127 std::vector<base_tensor *> KiQ, KQj;
6130 virtual int exec() {
6131 GA_DEBUG_INFO(
"Instruction: contribution of condensation to kept part");
6135 Kij.adjust_sizes(m,n);
6138 const base_tensor &K1 = *KiQ[k], &K2 = *KQj[k];
6140 GMM_ASSERT1(K1.size(0) == m && K2.size(1) == n && K2.size(0) == qqsize,
6143 base_tensor::iterator it = Kij.begin();
6145 for (
size_type ii = 0; ii < m; ++ii, ++it)
6146 for (
size_type qq = 0; qq < qqsize; ++qq)
6147 *it -= K1[ii+qq*m] * K2[qq+jj*qqsize];
6148 GA_DEBUG_ASSERT(it == Kij.end(),
"Wrong sizes");
6152 ga_instruction_condensation_super_K(base_tensor &Kij_,
6153 const std::vector<base_tensor *> KiQ_,
6154 const std::vector<base_tensor *> KQj_)
6155 : Kij(Kij_), KiQ(KiQ_), KQj(KQj_)
6158 GMM_ASSERT1(KiQ.size() == KQj.size(),
"Internal error");
6162 struct ga_instruction_condensation_super_R :
public ga_instruction {
6164 std::vector<base_tensor *> KiQ, RQpr;
6167 virtual int exec() {
6168 GA_DEBUG_INFO(
"Instruction: contribution of condensation to primary rhs");
6174 const base_tensor &K1 = *KiQ[k], &R2 = *RQpr[k];
6176 GMM_ASSERT1(K1.size(0) == m && R2.size(0) == qqsize,
"Internal error");
6177 base_tensor::iterator it = Ri.begin();
6178 for (
size_type ii = 0; ii < m; ++ii, ++it)
6179 for (
size_type qq = 0; qq < qqsize; ++qq)
6180 *it -= K1[ii+qq*m] * R2[qq];
6181 GA_DEBUG_ASSERT(it == Ri.end(),
"Wrong sizes");
6185 ga_instruction_condensation_super_R(base_tensor &Ri_,
6186 const std::vector<base_tensor *> KiQ_,
6187 const std::vector<base_tensor *> RQpr_)
6188 : Ri(Ri_), KiQ(KiQ_), RQpr(RQpr_)
6191 GMM_ASSERT1(KiQ.size() == RQpr.size(),
"Internal error");
6199 static void extend_variable_in_gis(
const ga_workspace &workspace,
6200 const std::string &varname,
6201 ga_instruction_set &gis) {
6202 if (workspace.variable_group_exists(varname)) {
6203 for (
const std::string &v : workspace.variable_group(varname))
6204 extend_variable_in_gis(workspace, v, gis);
6205 }
else if (gis.extended_vars.count(varname) == 0) {
6206 const mesh_fem *mf = workspace.associated_mf(varname);
6207 if (mf->is_reduced()) {
6208 auto n = (mf->get_qdim() == 1) ? workspace.qdim(varname) : 1;
6209 base_vector &U = gis.really_extended_vars[varname];
6211 mf->extend_vector(workspace.value(varname), U);
6212 gis.extended_vars[varname] = &(gis.really_extended_vars[varname]);
6214 gis.extended_vars[varname] = &(workspace.value(varname));
6219 static void ga_clear_node_list
6220 (pga_tree_node pnode, std::map<scalar_type,
6221 std::list<pga_tree_node> > &node_list) {
6222 std::list<pga_tree_node> &loc_node_list = node_list[pnode->hash_value];
6223 for (std::list<pga_tree_node>::iterator it = loc_node_list.begin();
6224 it != loc_node_list.end(); ) {
6225 if (*it == pnode) it = loc_node_list.erase(it);
else ++it;
6227 for (
size_type i = 0; i < pnode->children.size(); ++i)
6228 ga_clear_node_list(pnode->children[i], node_list);
6233 static void ga_compile_node(
const pga_tree_node pnode,
6234 ga_workspace &workspace,
6235 ga_instruction_set &gis,
6236 ga_instruction_set::region_mim_instructions &rmi,
6237 const mesh &m,
bool function_case,
6238 ga_if_hierarchy &if_hierarchy) {
6240 if (pnode->node_type == GA_NODE_PREDEF_FUNC ||
6241 pnode->node_type == GA_NODE_OPERATOR ||
6242 pnode->node_type == GA_NODE_SPEC_FUNC ||
6243 pnode->node_type == GA_NODE_CONSTANT ||
6244 pnode->node_type == GA_NODE_ALLINDICES ||
6245 pnode->node_type == GA_NODE_RESHAPE ||
6246 pnode->node_type == GA_NODE_SWAP_IND ||
6247 pnode->node_type == GA_NODE_IND_MOVE_LAST ||
6248 pnode->node_type == GA_NODE_CONTRACT)
return;
6252 pga_instruction pgai;
6253 ga_if_hierarchy *pif_hierarchy = &if_hierarchy;
6254 ga_if_hierarchy new_if_hierarchy;
6256 const mesh_fem *mf1 = 0, *mf2 = 0;
6257 const mesh_fem **mfg1 = 0, **mfg2 = 0;
6258 fem_interpolation_context *pctx1 = 0, *pctx2 = 0;
6259 bool tensor_to_clear =
false;
6260 bool tensor_to_adapt =
false;
6262 if (pnode->test_function_type) {
6263 if (pnode->name_test1.size())
6264 mf1 = workspace.associated_mf(pnode->name_test1);
6267 const std::string &intn1 = pnode->interpolate_name_test1;
6269 if (workspace.secondary_domain_exists(intn1)) {
6270 pctx1 = &(rmi.secondary_domain_infos.ctx);
6272 tensor_to_adapt =
true;
6273 pctx1 = &(rmi.interpolate_infos[intn1].ctx);
6274 if (workspace.variable_group_exists(pnode->name_test1)) {
6275 ga_instruction_set::variable_group_info &vgi =
6276 rmi.interpolate_infos[intn1].groups_info[pnode->name_test1];
6283 if (pnode->name_test2.size())
6284 mf2 = workspace.associated_mf(pnode->name_test2);
6287 const std::string &intn2 = pnode->interpolate_name_test2;
6289 if (workspace.secondary_domain_exists(intn2)) {
6290 pctx2 = &(rmi.secondary_domain_infos.ctx);
6292 tensor_to_adapt =
true;
6293 pctx2 = &(rmi.interpolate_infos[intn2].ctx);
6294 if (workspace.variable_group_exists(pnode->name_test2)) {
6295 ga_instruction_set::variable_group_info &vgi =
6296 rmi.interpolate_infos[intn2].groups_info[pnode->name_test2];
6307 pnode->t.set_to_original();
6308 pnode->t.set_sparsity(0, 0);
6309 bool is_uniform =
false;
6310 if (pnode->test_function_type == 1) {
6312 pgai = std::make_shared<ga_instruction_first_ind_tensor>
6313 (pnode->tensor(), *pctx1, pnode->qdim1, mf1, mfg1);
6314 if (mf1 && mf1->is_uniform())
6315 { is_uniform =
true; pctx1->invalid_convex_num(); }
6316 }
else if (pnode->test_function_type == 2) {
6318 pgai = std::make_shared<ga_instruction_first_ind_tensor>
6319 (pnode->tensor(), *pctx2, pnode->qdim2, mf2, mfg2);
6320 if (mf2 && mf2->is_uniform())
6321 { is_uniform =
true; pctx2->invalid_convex_num(); }
6322 }
else if (pnode->test_function_type == 3) {
6323 if ((mf1 || mfg1) && (mf2 || mfg2)) {
6324 pgai = std::make_shared<ga_instruction_two_first_ind_tensor>
6325 (pnode->tensor(), *pctx1, *pctx2, pnode->qdim1, mf1, mfg1,
6326 pnode->qdim2, mf2, mfg2);
6327 if (mf1 && mf1->is_uniform() && mf2 && mf2->is_uniform()) {
6329 pctx1->invalid_convex_num();
6330 pctx2->invalid_convex_num();
6332 }
else if (mf1 || mfg1) {
6333 pgai = std::make_shared<ga_instruction_first_ind_tensor>
6334 (pnode->tensor(), *pctx1, pnode->qdim1, mf1, mfg1);
6335 if (mf1 && mf1->is_uniform())
6336 { is_uniform =
true; pctx1->invalid_convex_num(); }
6337 }
else if (mf2 || mfg2) {
6338 pgai = std::make_shared<ga_instruction_second_ind_tensor>
6339 (pnode->tensor(), *pctx2, pnode->qdim2, mf2, mfg2);
6340 if (mf2 && mf2->is_uniform())
6341 { is_uniform =
true; pctx2->invalid_convex_num(); }
6346 pnode->t.set_to_original();
6347 if (rmi.node_list.count(pnode->hash_value) != 0) {
6348 for (pga_tree_node &pnode1 : rmi.node_list[pnode->hash_value]) {
6352 if (sub_tree_are_equal(pnode, pnode1, workspace, 1)) {
6353 pnode->t.set_to_copy(pnode1->t);
6356 if (sub_tree_are_equal(pnode, pnode1, workspace, 2)) {
6358 if (pnode->nb_test_functions() == 2) {
6362 else { rmi.instructions.push_back(std::move(pgai)); }
6364 pgai = std::make_shared<ga_instruction_transpose_test>
6365 (pnode->tensor(), pnode1->tensor());
6366 rmi.instructions.push_back(std::move(pgai));
6368 pnode->t.set_to_copy(pnode1->t);
6373 std::stringstream ss;
6374 ss <<
"Detected wrong equivalent nodes:" << endl;
6375 ga_print_node(pnode, ss);
6376 ss << endl <<
" and " << endl;
6377 ga_print_node(pnode1, ss);
6378 ss << endl <<
"No problem, but hash values could be adapted." << endl;
6379 GMM_TRACE2(ss.str());
6384 if (is_uniform) { pgai->exec(); }
6386 if (tensor_to_adapt)
6387 rmi.instructions.push_back(std::move(pgai));
6389 rmi.elt_instructions.push_back(std::move(pgai));
6393 size_type interpolate_filter_inst = rmi.instructions.size();
6394 if (pnode->node_type == GA_NODE_INTERPOLATE_FILTER) {
6395 pgai = pga_instruction();
6396 rmi.instructions.push_back(std::move(pgai));
6397 if_hierarchy.increment();
6398 new_if_hierarchy.child_of(if_hierarchy);
6399 pif_hierarchy = &new_if_hierarchy;
6402 for (
size_type i = 0; i < pnode->children.size(); ++i)
6403 ga_compile_node(pnode->children[i], workspace, gis, rmi, m,
6404 function_case, *pif_hierarchy);
6406 if (pnode->node_type == GA_NODE_INTERPOLATE_FILTER) {
6407 const std::string &intn = pnode->interpolate_name;
6408 ga_instruction_set::interpolate_info &inin = rmi.interpolate_infos[intn];
6409 pgai = std::make_shared<ga_instruction_interpolate_filter>
6410 (pnode->tensor(), inin, pnode->nbc1,
6411 int(rmi.instructions.size() - interpolate_filter_inst));
6412 rmi.instructions[interpolate_filter_inst].swap(pgai);
6413 pgai = std::make_shared<ga_instruction_copy_tensor>
6414 (pnode->tensor(), pnode->children[0]->tensor());
6415 rmi.instructions.push_back(std::move(pgai));
6416 ga_clear_node_list(pnode->children[0], rmi.node_list);
6419 static scalar_type minus = -scalar_type(1);
6420 size_type nbch = pnode->children.size();
6421 pga_tree_node child0 = (nbch > 0) ? pnode->children[0] : 0;
6422 pga_tree_node child1 = (nbch > 1) ? pnode->children[1] : 0;
6423 bgeot::multi_index mi;
6424 const bgeot::multi_index &size0 = child0 ? child0->t.sizes() : mi;
6426 size_type dim0 = child0 ? child0->tensor_order() : 0;
6427 size_type dim1 = child1 ? child1->tensor_order() : 0;
6429 switch (pnode->node_type) {
6431 case GA_NODE_PREDEF_FUNC:
case GA_NODE_OPERATOR:
case GA_NODE_SPEC_FUNC:
6432 case GA_NODE_CONSTANT:
case GA_NODE_ALLINDICES:
case GA_NODE_ZERO:
6433 case GA_NODE_RESHAPE:
case GA_NODE_CROSS_PRODUCT:
6434 case GA_NODE_SWAP_IND:
case GA_NODE_IND_MOVE_LAST:
6435 case GA_NODE_CONTRACT:
case GA_NODE_INTERPOLATE_FILTER:
6439 GMM_ASSERT1(!function_case,
6440 "No use of X is allowed in scalar functions");
6442 GA_DEBUG_ASSERT(pnode->tensor().size() == 1,
"dimensions mismatch");
6443 GMM_ASSERT1(pnode->nbc1 <= m.dim(),
6444 "Bad index for X in expression");
6445 pgai = std::make_shared<ga_instruction_X_component>
6446 (pnode->tensor()[0], gis.ctx, pnode->nbc1-1);
6448 if (pnode->tensor().size() != m.dim())
6449 pnode->init_vector_tensor(m.dim());
6450 pgai = std::make_shared<ga_instruction_X>(pnode->tensor(), gis.ctx);
6452 rmi.instructions.push_back(std::move(pgai));
6455 case GA_NODE_ELT_SIZE:
6456 GMM_ASSERT1(!function_case,
6457 "No use of element_size is allowed in functions");
6458 if (pnode->tensor().size() != 1) pnode->init_scalar_tensor(0);
6459 pgai = std::make_shared<ga_instruction_element_size>
6460 (pnode->tensor(), gis.elt_size);
6461 gis.need_elt_size =
true;
6462 rmi.instructions.push_back(std::move(pgai));
6466 GMM_ASSERT1(!function_case,
6467 "No use of element_K is allowed in functions");
6468 pgai = std::make_shared<ga_instruction_element_K>(pnode->tensor(),
6470 rmi.instructions.push_back(std::move(pgai));
6474 GMM_ASSERT1(!function_case,
6475 "No use of element_B is allowed in functions");
6476 pgai = std::make_shared<ga_instruction_element_B>(pnode->tensor(),
6478 rmi.instructions.push_back(std::move(pgai));
6481 case GA_NODE_ELT_CURVATURE:
6482 GMM_ASSERT1(!function_case,
6483 "No use of element_curvature is allowed in functions");
6484 pgai = std::make_shared<ga_instruction_element_curvature>
6485 (pnode->tensor(), gis.ctx);
6486 rmi.instructions.push_back(std::move(pgai));
6489 case GA_NODE_NORMAL:
6491 GMM_ASSERT1(!function_case,
6492 "No use of Normal is allowed in functions");
6493 if (pnode->tensor().size() != m.dim())
6494 pnode->init_vector_tensor(m.dim());
6495 const mesh_im_level_set *mimls
6496 =
dynamic_cast<const mesh_im_level_set *
>(rmi.im);
6497 if (mimls && mimls->location()==mesh_im_level_set::INTEGRATE_BOUNDARY) {
6499 pgai = std::make_shared<ga_instruction_level_set_normal_vector>
6500 (pnode->tensor(), mimls, gis.ctx);
6501 rmi.instructions.push_back(std::move(pgai));
6503 pgai = std::make_shared<ga_instruction_copy_Normal>
6504 (pnode->tensor(), gis.Normal);
6505 rmi.instructions.push_back(std::move(pgai));
6510 case GA_NODE_INTERPOLATE_X:
6511 case GA_NODE_INTERPOLATE_NORMAL:
6512 GMM_ASSERT1(!function_case,
6513 "No use of Interpolate is allowed in functions");
6514 if (pnode->tensor().size() != m.dim())
6515 pnode->init_vector_tensor(m.dim());
6516 if (pnode->node_type == GA_NODE_INTERPOLATE_X)
6517 pgai = std::make_shared<ga_instruction_copy_interpolated_small_vect>
6519 rmi.interpolate_infos[pnode->interpolate_name].pt_y,
6520 rmi.interpolate_infos[pnode->interpolate_name]);
6521 else if (pnode->node_type == GA_NODE_INTERPOLATE_NORMAL)
6522 pgai = std::make_shared<ga_instruction_copy_Normal>
6524 rmi.interpolate_infos[pnode->interpolate_name].Normal);
6525 rmi.instructions.push_back(std::move(pgai));
6528 case GA_NODE_INTERPOLATE_ELT_K:
6529 case GA_NODE_INTERPOLATE_ELT_B:
6530 GMM_ASSERT1(!function_case,
6531 "No use of Interpolate is allowed in functions");
6532 if (pnode->node_type == GA_NODE_INTERPOLATE_ELT_K)
6533 pgai = std::make_shared<ga_instruction_element_K>
6535 rmi.interpolate_infos[pnode->interpolate_name].ctx);
6536 else if (pnode->node_type == GA_NODE_INTERPOLATE_ELT_B)
6537 pgai = std::make_shared<ga_instruction_element_B>
6539 rmi.interpolate_infos[pnode->interpolate_name].ctx);
6540 rmi.instructions.push_back(std::move(pgai));
6543 case GA_NODE_SECONDARY_DOMAIN_X:
6544 case GA_NODE_SECONDARY_DOMAIN_NORMAL:
6546 GMM_ASSERT1(!function_case,
6547 "No use of Secondary_domain is allowed in functions");
6548 auto psd = workspace.secondary_domain(pnode->interpolate_name);
6549 size_type sddim = psd->mim().linked_mesh().dim();
6550 if (pnode->tensor().size() != sddim)
6551 pnode->init_vector_tensor(sddim);
6552 if (pnode->node_type == GA_NODE_SECONDARY_DOMAIN_X)
6553 pgai = std::make_shared<ga_instruction_X>
6554 (pnode->tensor(), rmi.secondary_domain_infos.ctx);
6555 else if (pnode->node_type == GA_NODE_SECONDARY_DOMAIN_NORMAL)
6556 pgai = std::make_shared<ga_instruction_copy_Normal>
6557 (pnode->tensor(), rmi.secondary_domain_infos.Normal);
6558 rmi.instructions.push_back(std::move(pgai));
6562 case GA_NODE_VAL:
case GA_NODE_GRAD:
6563 case GA_NODE_HESS:
case GA_NODE_DIVERG:
6564 case GA_NODE_ELEMENTARY_VAL:
case GA_NODE_ELEMENTARY_GRAD:
6565 case GA_NODE_ELEMENTARY_HESS:
case GA_NODE_ELEMENTARY_DIVERG:
6566 case GA_NODE_XFEM_PLUS_VAL:
case GA_NODE_XFEM_PLUS_GRAD:
6567 case GA_NODE_XFEM_PLUS_HESS:
case GA_NODE_XFEM_PLUS_DIVERG:
6568 case GA_NODE_XFEM_MINUS_VAL:
case GA_NODE_XFEM_MINUS_GRAD:
6569 case GA_NODE_XFEM_MINUS_HESS:
case GA_NODE_XFEM_MINUS_DIVERG:
6571 bool is_elementary = (pnode->node_type == GA_NODE_ELEMENTARY_VAL ||
6572 pnode->node_type == GA_NODE_ELEMENTARY_GRAD ||
6573 pnode->node_type == GA_NODE_ELEMENTARY_HESS ||
6574 pnode->node_type == GA_NODE_ELEMENTARY_DIVERG);
6575 if (function_case) {
6576 GMM_ASSERT1(!is_elementary,
6577 "No elementary transformation is allowed in functions");
6578 GMM_ASSERT1(pnode->node_type != GA_NODE_XFEM_PLUS_VAL &&
6579 pnode->node_type != GA_NODE_XFEM_PLUS_GRAD &&
6580 pnode->node_type != GA_NODE_XFEM_PLUS_HESS &&
6581 pnode->node_type != GA_NODE_XFEM_PLUS_DIVERG,
6582 "Xfem_plus not allowed in functions");
6583 GMM_ASSERT1(pnode->node_type != GA_NODE_XFEM_MINUS_VAL &&
6584 pnode->node_type != GA_NODE_XFEM_MINUS_GRAD &&
6585 pnode->node_type != GA_NODE_XFEM_MINUS_HESS &&
6586 pnode->node_type != GA_NODE_XFEM_MINUS_DIVERG,
6587 "Xfem_plus not allowed in functions");
6588 const mesh_fem *mf = workspace.associated_mf(pnode->name);
6589 const im_data *imd = workspace.associated_im_data(pnode->name);
6590 GMM_ASSERT1(!mf,
"No fem expression is allowed in "
6591 "function expression");
6592 GMM_ASSERT1(!imd,
"No integration method data is allowed in "
6593 "function expression");
6594 if (gmm::vect_size(workspace.value(pnode->name)) == 1)
6595 pgai = std::make_shared<ga_instruction_copy_scalar>
6596 (pnode->tensor()[0], (workspace.value(pnode->name))[0]);
6598 pgai = std::make_shared<ga_instruction_copy_vect>
6599 (pnode->tensor().as_vector(), workspace.value(pnode->name));
6600 rmi.instructions.push_back(std::move(pgai));
6602 const mesh_fem *mf = workspace.associated_mf(pnode->name), *mfo=mf;
6603 const im_data *imd = workspace.associated_im_data(pnode->name);
6605 if (is_elementary) {
6606 mf = workspace.associated_mf(pnode->elementary_target);
6607 GMM_ASSERT1(mf && mfo,
6608 "Wrong context for elementary transformation");
6609 GMM_ASSERT1(&(mfo->linked_mesh()) == &(m),
6610 "The finite element of variable " << pnode->name
6611 <<
" has to be defined on the same mesh as the "
6612 <<
"integration method or interpolation used");
6616 GMM_ASSERT1(pnode->node_type == GA_NODE_VAL,
6617 "Only values can be extracted on im_data (no " <<
6618 "gradient, Hessian, xfem or elementary tranformation" <<
6620 pgai = std::make_shared<ga_instruction_extract_local_im_data>
6621 (pnode->tensor(), *imd, workspace.value(pnode->name),
6622 gis.pai, gis.ctx, workspace.qdim(pnode->name));
6623 rmi.instructions.push_back(std::move(pgai));
6625 GMM_ASSERT1(mf,
"Internal error");
6627 GMM_ASSERT1(&(mf->linked_mesh()) == &(m),
6628 "The finite element of variable " <<
6629 (is_elementary ? pnode->elementary_target : pnode->name)
6630 <<
" has to be defined on the same mesh as the "
6631 <<
"integration method or interpolation used");
6634 if (rmi.local_dofs.count(pnode->name) == 0) {
6635 rmi.local_dofs[pnode->name] = base_vector(1);
6636 extend_variable_in_gis(workspace, pnode->name, gis);
6639 if (qmult2 > 1 && !(mfo->is_uniformly_vectorized()))
6641 pgai = std::make_shared<ga_instruction_slice_local_dofs>
6642 (*mfo, *(gis.extended_vars[pnode->name]), gis.ctx,
6643 rmi.local_dofs[pnode->name],
6644 workspace.qdim(pnode->name) / mfo->get_qdim(), qmult2);
6645 rmi.elt_instructions.push_back(std::move(pgai));
6649 if (mf->is_uniform()) {
6650 if (rmi.pfps.count(mf) == 0) {
6652 pgai = std::make_shared<ga_instruction_update_pfp>
6653 (*mf, rmi.pfps[mf], gis.ctx, gis.fp_pool);
6654 rmi.begin_instructions.push_back(std::move(pgai));
6656 }
else if (rmi.pfps.count(mf) == 0 ||
6657 !if_hierarchy.is_compatible(rmi.pfp_hierarchy[mf])) {
6658 rmi.pfp_hierarchy[mf].push_back(if_hierarchy);
6660 pgai = std::make_shared<ga_instruction_update_pfp>
6661 (*mf, rmi.pfps[mf], gis.ctx, gis.fp_pool);
6662 rmi.instructions.push_back(std::move(pgai));
6666 pgai = pga_instruction();
6667 switch (pnode->node_type) {
6668 case GA_NODE_VAL:
case GA_NODE_ELEMENTARY_VAL:
6669 if (rmi.base.count(mf) == 0 ||
6670 !if_hierarchy.is_compatible(rmi.base_hierarchy[mf])) {
6671 rmi.base_hierarchy[mf].push_back(if_hierarchy);
6672 pgai = std::make_shared<ga_instruction_val_base>
6673 (rmi.base[mf], gis.ctx, *mf, rmi.pfps[mf]);
6676 case GA_NODE_XFEM_PLUS_VAL:
6677 if (rmi.xfem_plus_base.count(mf) == 0 ||
6678 !if_hierarchy.is_compatible(rmi.xfem_plus_base_hierarchy[mf]))
6680 rmi.xfem_plus_base_hierarchy[mf].push_back(if_hierarchy);
6681 pgai = std::make_shared<ga_instruction_xfem_plus_val_base>
6682 (rmi.xfem_plus_base[mf], gis.ctx, *mf, rmi.pfps[mf]);
6685 case GA_NODE_XFEM_MINUS_VAL:
6686 if (rmi.xfem_minus_base.count(mf) == 0 ||
6687 !if_hierarchy.is_compatible(rmi.xfem_minus_base_hierarchy[mf]))
6689 rmi.xfem_minus_base_hierarchy[mf].push_back(if_hierarchy);
6690 pgai = std::make_shared<ga_instruction_xfem_minus_val_base>
6691 (rmi.xfem_minus_base[mf], gis.ctx, *mf, rmi.pfps[mf]);
6694 case GA_NODE_GRAD:
case GA_NODE_DIVERG:
6695 case GA_NODE_ELEMENTARY_GRAD:
case GA_NODE_ELEMENTARY_DIVERG:
6696 if (rmi.grad.count(mf) == 0 ||
6697 !if_hierarchy.is_compatible(rmi.grad_hierarchy[mf])) {
6698 rmi.grad_hierarchy[mf].push_back(if_hierarchy);
6699 pgai = std::make_shared<ga_instruction_grad_base>
6700 (rmi.grad[mf], gis.ctx, *mf, rmi.pfps[mf]);
6703 case GA_NODE_XFEM_PLUS_GRAD:
case GA_NODE_XFEM_PLUS_DIVERG:
6704 if (rmi.xfem_plus_grad.count(mf) == 0 ||
6705 !if_hierarchy.is_compatible(rmi.xfem_plus_grad_hierarchy[mf]))
6707 rmi.xfem_plus_grad_hierarchy[mf].push_back(if_hierarchy);
6708 pgai = std::make_shared<ga_instruction_xfem_plus_grad_base>
6709 (rmi.xfem_plus_grad[mf], gis.ctx, *mf, rmi.pfps[mf]);
6712 case GA_NODE_XFEM_MINUS_GRAD:
case GA_NODE_XFEM_MINUS_DIVERG:
6713 if (rmi.xfem_minus_grad.count(mf) == 0 ||
6714 !if_hierarchy.is_compatible(rmi.xfem_minus_grad_hierarchy[mf]))
6716 rmi.xfem_minus_grad_hierarchy[mf].push_back(if_hierarchy);
6717 pgai = std::make_shared<ga_instruction_xfem_minus_grad_base>
6718 (rmi.xfem_minus_grad[mf], gis.ctx, *mf, rmi.pfps[mf]);
6721 case GA_NODE_HESS:
case GA_NODE_ELEMENTARY_HESS:
6722 if (rmi.hess.count(mf) == 0 ||
6723 !if_hierarchy.is_compatible(rmi.hess_hierarchy[mf])) {
6724 rmi.hess_hierarchy[mf].push_back(if_hierarchy);
6725 pgai = std::make_shared<ga_instruction_hess_base>
6726 (rmi.hess[mf], gis.ctx, *mf, rmi.pfps[mf]);
6729 case GA_NODE_XFEM_PLUS_HESS:
6730 if (rmi.xfem_plus_hess.count(mf) == 0 ||
6731 !if_hierarchy.is_compatible(rmi.xfem_plus_hess_hierarchy[mf]))
6733 rmi.xfem_plus_hess_hierarchy[mf].push_back(if_hierarchy);
6734 pgai = std::make_shared<ga_instruction_xfem_plus_hess_base>
6735 (rmi.xfem_plus_hess[mf], gis.ctx, *mf, rmi.pfps[mf]);
6738 case GA_NODE_XFEM_MINUS_HESS:
6739 if (rmi.xfem_minus_hess.count(mf) == 0 ||
6740 !if_hierarchy.is_compatible(rmi.xfem_minus_hess_hierarchy[mf]))
6742 rmi.xfem_minus_hess_hierarchy[mf].push_back(if_hierarchy);
6743 pgai = std::make_shared<ga_instruction_xfem_minus_hess_base>
6744 (rmi.xfem_minus_hess[mf], gis.ctx, *mf, rmi.pfps[mf]);
6748 default : GMM_ASSERT1(
false,
"Internal error");
6750 if (pgai) rmi.instructions.push_back(std::move(pgai));
6753 switch (pnode->node_type) {
6755 pgai = std::make_shared<ga_instruction_val>
6756 (pnode->tensor(), rmi.base[mf], rmi.local_dofs[pnode->name],
6757 workspace.qdim(pnode->name));
6760 pgai = std::make_shared<ga_instruction_grad>
6761 (pnode->tensor(), rmi.grad[mf],
6762 rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6765 pgai = std::make_shared<ga_instruction_hess>
6766 (pnode->tensor(), rmi.hess[mf],
6767 rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6769 case GA_NODE_DIVERG:
6770 pgai = std::make_shared<ga_instruction_diverg>
6771 (pnode->tensor(), rmi.grad[mf],
6772 rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6774 case GA_NODE_XFEM_PLUS_VAL:
6775 pgai = std::make_shared<ga_instruction_val>
6776 (pnode->tensor(), rmi.xfem_plus_base[mf],
6777 rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6779 case GA_NODE_XFEM_PLUS_GRAD:
6780 pgai = std::make_shared<ga_instruction_grad>
6781 (pnode->tensor(), rmi.xfem_plus_grad[mf],
6782 rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6784 case GA_NODE_XFEM_PLUS_HESS:
6785 pgai = std::make_shared<ga_instruction_hess>
6786 (pnode->tensor(), rmi.xfem_plus_hess[mf],
6787 rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6789 case GA_NODE_XFEM_PLUS_DIVERG:
6790 pgai = std::make_shared<ga_instruction_diverg>
6791 (pnode->tensor(), rmi.xfem_plus_grad[mf],
6792 rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6794 case GA_NODE_XFEM_MINUS_VAL:
6795 pgai = std::make_shared<ga_instruction_val>
6796 (pnode->tensor(), rmi.xfem_minus_base[mf],
6797 rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6799 case GA_NODE_XFEM_MINUS_GRAD:
6800 pgai = std::make_shared<ga_instruction_grad>
6801 (pnode->tensor(), rmi.xfem_minus_grad[mf],
6802 rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6804 case GA_NODE_XFEM_MINUS_HESS:
6805 pgai = std::make_shared<ga_instruction_hess>
6806 (pnode->tensor(), rmi.xfem_minus_hess[mf],
6807 rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6809 case GA_NODE_XFEM_MINUS_DIVERG:
6810 pgai = std::make_shared<ga_instruction_diverg>
6811 (pnode->tensor(), rmi.xfem_minus_grad[mf],
6812 rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6814 case GA_NODE_ELEMENTARY_VAL:
6816 ga_instruction_set::elementary_trans_info &eti
6817 = rmi.elementary_trans_infos
6818 [std::make_tuple(pnode->elementary_name, mfo, mf)];
6820 std::make_shared<ga_instruction_elementary_trans_val>
6821 (pnode->tensor(), rmi.base[mf],
6822 rmi.local_dofs[pnode->name],
6823 workspace.qdim(pnode->elementary_target),
6824 workspace.elementary_transformation(pnode->elementary_name),
6825 *mfo, *mf, gis.ctx, eti.M, eti.icv);
6828 case GA_NODE_ELEMENTARY_GRAD:
6830 ga_instruction_set::elementary_trans_info &eti
6831 = rmi.elementary_trans_infos
6832 [std::make_tuple(pnode->elementary_name, mfo, mf)];
6834 std::make_shared<ga_instruction_elementary_trans_grad>
6835 (pnode->tensor(), rmi.grad[mf],
6836 rmi.local_dofs[pnode->name],
6837 workspace.qdim(pnode->elementary_target),
6838 workspace.elementary_transformation(pnode->elementary_name),
6839 *mfo, *mf, gis.ctx, eti.M, eti.icv);
6842 case GA_NODE_ELEMENTARY_HESS:
6844 ga_instruction_set::elementary_trans_info &eti
6845 = rmi.elementary_trans_infos
6846 [std::make_tuple(pnode->elementary_name, mfo, mf)];
6848 std::make_shared<ga_instruction_elementary_trans_hess>
6849 (pnode->tensor(), rmi.hess[mf],
6850 rmi.local_dofs[pnode->name],
6851 workspace.qdim(pnode->elementary_target),
6852 workspace.elementary_transformation(pnode->elementary_name),
6853 *mfo, *mf, gis.ctx, eti.M, eti.icv);
6856 case GA_NODE_ELEMENTARY_DIVERG:
6858 ga_instruction_set::elementary_trans_info &eti
6859 = rmi.elementary_trans_infos
6860 [std::make_tuple(pnode->elementary_name, mfo, mf)];
6862 std::make_shared<ga_instruction_elementary_trans_diverg>
6863 (pnode->tensor(), rmi.grad[mf],
6864 rmi.local_dofs[pnode->name],
6865 workspace.qdim(pnode->elementary_target),
6866 workspace.elementary_transformation(pnode->elementary_name),
6867 *mfo, *mf, gis.ctx, eti.M, eti.icv);
6872 rmi.instructions.push_back(std::move(pgai));
6878 case GA_NODE_SECONDARY_DOMAIN_VAL:
case GA_NODE_SECONDARY_DOMAIN_GRAD:
6879 case GA_NODE_SECONDARY_DOMAIN_HESS:
case GA_NODE_SECONDARY_DOMAIN_DIVERG:
6881 GMM_ASSERT1(!function_case,
"internal error");
6882 const mesh_fem *mf = workspace.associated_mf(pnode->name);
6883 const im_data *imd = workspace.associated_im_data(pnode->name);
6884 const std::string &intn = pnode->interpolate_name;
6885 auto &sdi = rmi.secondary_domain_infos;
6887 fem_interpolation_context *pctx = &(sdi.ctx);
6888 papprox_integration pai = sdi.pai;
6889 psecondary_domain psd = workspace.secondary_domain(intn);
6892 pgai = std::make_shared<ga_instruction_extract_local_im_data>
6893 (pnode->tensor(), *imd, workspace.value(pnode->name),
6894 pai, *pctx, workspace.qdim(pnode->name));
6895 rmi.instructions.push_back(std::move(pgai));
6897 GMM_ASSERT1(mf,
"Internal error");
6898 GMM_ASSERT1(&(mf->linked_mesh()) == &(psd->mim().linked_mesh()),
6899 "The finite element of variable " << pnode->name <<
6900 " has to be defined on the same mesh as the "
6901 "integration method or interpolation used on the "
6902 "secondary domain");
6905 if (sdi.local_dofs.count(pnode->name) == 0) {
6906 sdi.local_dofs[pnode->name] = base_vector(1);
6907 extend_variable_in_gis(workspace, pnode->name, gis);
6909 if (qmult2 > 1 && !(mf->is_uniformly_vectorized()))
6911 pgai = std::make_shared<ga_instruction_slice_local_dofs>
6912 (*mf, *(gis.extended_vars[pnode->name]), *pctx,
6913 sdi.local_dofs[pnode->name],
6914 workspace.qdim(pnode->name) / mf->get_qdim(), qmult2);
6915 rmi.elt_instructions.push_back(std::move(pgai));
6919 if (mf->is_uniform()) {
6920 if (sdi.pfps.count(mf) == 0) {
6922 pgai = std::make_shared<ga_instruction_update_pfp>
6923 (*mf, sdi.pfps[mf], *pctx, gis.fp_pool);
6924 rmi.begin_instructions.push_back(std::move(pgai));
6926 }
else if (sdi.pfps.count(mf) == 0 ||
6927 !if_hierarchy.is_compatible(rmi.pfp_hierarchy[mf])) {
6928 rmi.pfp_hierarchy[mf].push_back(if_hierarchy);
6930 pgai = std::make_shared<ga_instruction_update_pfp>
6931 (*mf, sdi.pfps[mf], *pctx, gis.fp_pool);
6932 rmi.instructions.push_back(std::move(pgai));
6936 pgai = pga_instruction();
6937 switch (pnode->node_type) {
6938 case GA_NODE_SECONDARY_DOMAIN_VAL:
6939 if (sdi.base.count(mf) == 0 ||
6940 !(if_hierarchy.is_compatible(rmi.base_hierarchy[mf]))) {
6941 rmi.base_hierarchy[mf].push_back(if_hierarchy);
6942 pgai = std::make_shared<ga_instruction_val_base>
6943 (sdi.base[mf], *pctx, *mf, sdi.pfps[mf]);
6946 case GA_NODE_SECONDARY_DOMAIN_GRAD:
6947 case GA_NODE_SECONDARY_DOMAIN_DIVERG:
6948 if (sdi.grad.count(mf) == 0 ||
6949 !(if_hierarchy.is_compatible(rmi.grad_hierarchy[mf]))) {
6950 rmi.grad_hierarchy[mf].push_back(if_hierarchy);
6951 pgai = std::make_shared<ga_instruction_grad_base>
6952 (sdi.grad[mf], *pctx, *mf, sdi.pfps[mf]);
6955 case GA_NODE_SECONDARY_DOMAIN_HESS:
6956 if (sdi.hess.count(mf) == 0 ||
6957 !(if_hierarchy.is_compatible(rmi.hess_hierarchy[mf]))) {
6958 rmi.hess_hierarchy[mf].push_back(if_hierarchy);
6959 pgai = std::make_shared<ga_instruction_hess_base>
6960 (sdi.hess[mf], *pctx, *mf, sdi.pfps[mf]);
6963 default : GMM_ASSERT1(
false,
"Internal error");
6965 if (pgai) rmi.instructions.push_back(std::move(pgai));
6968 switch (pnode->node_type) {
6969 case GA_NODE_SECONDARY_DOMAIN_VAL:
6970 pgai = std::make_shared<ga_instruction_val>
6971 (pnode->tensor(), sdi.base[mf], sdi.local_dofs[pnode->name],
6972 workspace.qdim(pnode->name));
6974 case GA_NODE_SECONDARY_DOMAIN_GRAD:
6975 pgai = std::make_shared<ga_instruction_grad>
6976 (pnode->tensor(), sdi.grad[mf],
6977 sdi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6979 case GA_NODE_SECONDARY_DOMAIN_HESS:
6980 pgai = std::make_shared<ga_instruction_hess>
6981 (pnode->tensor(), sdi.hess[mf],
6982 sdi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6984 case GA_NODE_SECONDARY_DOMAIN_DIVERG:
6985 pgai = std::make_shared<ga_instruction_diverg>
6986 (pnode->tensor(), sdi.grad[mf],
6987 sdi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6991 rmi.instructions.push_back(std::move(pgai));
6996 case GA_NODE_INTERPOLATE_VAL:
case GA_NODE_INTERPOLATE_GRAD:
6997 case GA_NODE_INTERPOLATE_HESS:
case GA_NODE_INTERPOLATE_DIVERG:
6999 extend_variable_in_gis(workspace, pnode->name, gis);
7001 const mesh_fem *mfn = workspace.associated_mf(pnode->name), **mfg = 0;
7002 const std::string &intn = pnode->interpolate_name;
7003 const base_vector *Un = gis.extended_vars[pnode->name], **Ug = 0;
7004 fem_interpolation_context *pctx = &(rmi.interpolate_infos[intn].ctx);
7005 const mesh **m2 = &(rmi.interpolate_infos[intn].m);
7006 if (workspace.variable_group_exists(pnode->name)) {
7007 ga_instruction_set::variable_group_info &vgi =
7008 rmi.interpolate_infos[intn].groups_info[pnode->name];
7009 mfg = &(vgi.mf); mfn = 0; Ug = &(vgi.U); Un = 0;
7012 if (pnode->node_type == GA_NODE_INTERPOLATE_VAL) {
7014 pgai = std::make_shared<ga_instruction_interpolate_val>
7015 (pnode->tensor(), m2, mfn, mfg, Un, Ug, *pctx,
7016 workspace.qdim(pnode->name),
7017 gis.ipt, gis.fp_pool, rmi.interpolate_infos[intn]);
7018 }
else if (pnode->node_type == GA_NODE_INTERPOLATE_GRAD) {
7020 pgai = std::make_shared<ga_instruction_interpolate_grad>
7021 (pnode->tensor(), m2, mfn, mfg, Un, Ug, *pctx,
7022 workspace.qdim(pnode->name),
7023 gis.ipt, gis.fp_pool, rmi.interpolate_infos[intn]);
7024 }
else if (pnode->node_type == GA_NODE_INTERPOLATE_HESS) {
7026 pgai = std::make_shared<ga_instruction_interpolate_hess>
7027 (pnode->tensor(), m2, mfn, mfg, Un, Ug, *pctx,
7028 workspace.qdim(pnode->name),
7029 gis.ipt, gis.fp_pool, rmi.interpolate_infos[intn]);
7031 pgai = std::make_shared<ga_instruction_interpolate_diverg>
7032 (pnode->tensor(), m2, mfn, mfg, Un, Ug, *pctx,
7033 workspace.qdim(pnode->name),
7034 gis.ipt, gis.fp_pool, rmi.interpolate_infos[intn]);
7036 rmi.instructions.push_back(std::move(pgai));
7040 case GA_NODE_INTERPOLATE_DERIVATIVE:
7041 GMM_ASSERT1(!function_case,
7042 "No use of Interpolate is allowed in functions");
7043 pgai = std::make_shared<ga_instruction_copy_tensor_possibly_void>
7045 rmi.interpolate_infos[pnode->interpolate_name_der]
7046 .derivatives[var_trans_pair(pnode->name, pnode->interpolate_name)]);
7047 rmi.instructions.push_back(std::move(pgai));
7050 case GA_NODE_VAL_TEST:
case GA_NODE_GRAD_TEST:
7051 case GA_NODE_HESS_TEST:
case GA_NODE_DIVERG_TEST:
7052 case GA_NODE_ELEMENTARY_VAL_TEST:
case GA_NODE_ELEMENTARY_GRAD_TEST:
7053 case GA_NODE_ELEMENTARY_HESS_TEST:
case GA_NODE_ELEMENTARY_DIVERG_TEST:
7054 case GA_NODE_XFEM_PLUS_VAL_TEST:
case GA_NODE_XFEM_PLUS_GRAD_TEST:
7055 case GA_NODE_XFEM_PLUS_HESS_TEST:
case GA_NODE_XFEM_PLUS_DIVERG_TEST:
7056 case GA_NODE_XFEM_MINUS_VAL_TEST:
case GA_NODE_XFEM_MINUS_GRAD_TEST:
7057 case GA_NODE_XFEM_MINUS_HESS_TEST:
case GA_NODE_XFEM_MINUS_DIVERG_TEST:
7061 bool is_elementary = (pnode->node_type==GA_NODE_ELEMENTARY_VAL_TEST ||
7062 pnode->node_type==GA_NODE_ELEMENTARY_GRAD_TEST ||
7063 pnode->node_type==GA_NODE_ELEMENTARY_HESS_TEST ||
7064 pnode->node_type==GA_NODE_ELEMENTARY_DIVERG_TEST);
7065 const mesh_fem *mf = workspace.associated_mf(pnode->name), *mfo=mf;
7066 if (is_elementary) {
7067 mf = workspace.associated_mf(pnode->elementary_target);
7068 GMM_ASSERT1(mf && mfo,
7069 "Wrong context for elementary transformation");
7070 GMM_ASSERT1(&(mfo->linked_mesh()) == &(m),
7071 "The finite element of variable " << pnode->name
7072 <<
" has to be defined on the same mesh as the "
7073 <<
"integration method or interpolation used");
7077 GMM_ASSERT1(&(mf->linked_mesh()) == &(m),
7078 "The finite element of variable " <<
7079 (is_elementary ? pnode->elementary_target : pnode->name)
7080 <<
" and the applied integration method have to be"
7081 <<
" defined on the same mesh");
7085 if (rmi.pfps.count(mf) == 0) {
7087 pgai = std::make_shared<ga_instruction_update_pfp>
7088 (*mf, rmi.pfps[mf], gis.ctx, gis.fp_pool);
7089 rmi.begin_instructions.push_back(std::move(pgai));
7091 }
else if (rmi.pfps.count(mf) == 0 ||
7092 !if_hierarchy.is_compatible(rmi.pfp_hierarchy[mf])) {
7093 rmi.pfp_hierarchy[mf].push_back(if_hierarchy);
7095 pgai = std::make_shared<ga_instruction_update_pfp>
7096 (*mf, rmi.pfps[mf], gis.ctx, gis.fp_pool);
7097 rmi.instructions.push_back(std::move(pgai));
7101 pgai = pga_instruction();
7102 switch (pnode->node_type) {
7103 case GA_NODE_VAL_TEST:
case GA_NODE_ELEMENTARY_VAL_TEST:
7104 if (rmi.base.count(mf) == 0 ||
7105 !if_hierarchy.is_compatible(rmi.base_hierarchy[mf])) {
7106 rmi.base_hierarchy[mf].push_back(if_hierarchy);
7107 pgai = std::make_shared<ga_instruction_val_base>
7108 (rmi.base[mf], gis.ctx, *mf, rmi.pfps[mf]);
7111 case GA_NODE_XFEM_PLUS_VAL_TEST:
7112 if (rmi.xfem_plus_base.count(mf) == 0 ||
7113 !if_hierarchy.is_compatible(rmi.xfem_plus_base_hierarchy[mf]))
7115 rmi.xfem_plus_base_hierarchy[mf].push_back(if_hierarchy);
7116 pgai = std::make_shared<ga_instruction_xfem_plus_val_base>
7117 (rmi.xfem_plus_base[mf], gis.ctx, *mf, rmi.pfps[mf]);
7120 case GA_NODE_XFEM_MINUS_VAL_TEST:
7121 if (rmi.xfem_minus_base.count(mf) == 0 ||
7122 !if_hierarchy.is_compatible(rmi.xfem_minus_base_hierarchy[mf]))
7124 rmi.xfem_minus_base_hierarchy[mf].push_back(if_hierarchy);
7125 pgai = std::make_shared<ga_instruction_xfem_minus_val_base>
7126 (rmi.xfem_minus_base[mf], gis.ctx, *mf, rmi.pfps[mf]);
7129 case GA_NODE_GRAD_TEST:
case GA_NODE_DIVERG_TEST:
7130 case GA_NODE_ELEMENTARY_GRAD_TEST:
7131 case GA_NODE_ELEMENTARY_DIVERG_TEST:
7132 if (rmi.grad.count(mf) == 0 ||
7133 !if_hierarchy.is_compatible(rmi.grad_hierarchy[mf])) {
7134 rmi.grad_hierarchy[mf].push_back(if_hierarchy);
7135 pgai = std::make_shared<ga_instruction_grad_base>
7136 (rmi.grad[mf], gis.ctx, *mf, rmi.pfps[mf]);
7139 case GA_NODE_XFEM_PLUS_GRAD_TEST:
case GA_NODE_XFEM_PLUS_DIVERG_TEST:
7140 if (rmi.xfem_plus_grad.count(mf) == 0 ||
7141 !if_hierarchy.is_compatible(rmi.xfem_plus_grad_hierarchy[mf]))
7143 rmi.xfem_plus_grad_hierarchy[mf].push_back(if_hierarchy);
7144 pgai = std::make_shared<ga_instruction_xfem_plus_grad_base>
7145 (rmi.xfem_plus_grad[mf], gis.ctx, *mf, rmi.pfps[mf]);
7148 case GA_NODE_XFEM_MINUS_GRAD_TEST:
7149 case GA_NODE_XFEM_MINUS_DIVERG_TEST:
7150 if (rmi.xfem_minus_grad.count(mf) == 0 ||
7151 !if_hierarchy.is_compatible(rmi.xfem_minus_grad_hierarchy[mf]))
7153 rmi.xfem_minus_grad_hierarchy[mf].push_back(if_hierarchy);
7154 pgai = std::make_shared<ga_instruction_xfem_minus_grad_base>
7155 (rmi.xfem_minus_grad[mf], gis.ctx, *mf, rmi.pfps[mf]);
7158 case GA_NODE_HESS_TEST:
case GA_NODE_ELEMENTARY_HESS_TEST:
7159 if (rmi.hess.count(mf) == 0 ||
7160 !if_hierarchy.is_compatible(rmi.hess_hierarchy[mf])) {
7161 rmi.hess_hierarchy[mf].push_back(if_hierarchy);
7162 pgai = std::make_shared<ga_instruction_hess_base>
7163 (rmi.hess[mf], gis.ctx, *mf, rmi.pfps[mf]);
7166 case GA_NODE_XFEM_PLUS_HESS_TEST:
7167 if (rmi.xfem_plus_hess.count(mf) == 0 ||
7168 !if_hierarchy.is_compatible(rmi.xfem_plus_hess_hierarchy[mf]))
7170 rmi.xfem_plus_hess_hierarchy[mf].push_back(if_hierarchy);
7171 pgai = std::make_shared<ga_instruction_xfem_plus_hess_base>
7172 (rmi.xfem_plus_hess[mf], gis.ctx, *mf, rmi.pfps[mf]);
7175 case GA_NODE_XFEM_MINUS_HESS_TEST:
7176 if (rmi.xfem_minus_hess.count(mf) == 0 ||
7177 !if_hierarchy.is_compatible(rmi.xfem_minus_hess_hierarchy[mf]))
7179 rmi.xfem_minus_hess_hierarchy[mf].push_back(if_hierarchy);
7180 pgai = std::make_shared<ga_instruction_xfem_minus_hess_base>
7181 (rmi.xfem_minus_hess[mf], gis.ctx, *mf, rmi.pfps[mf]);
7185 default : GMM_ASSERT1(
false,
"Internal error");
7187 if (pgai) rmi.instructions.push_back(std::move(pgai));
7190 switch(pnode->node_type) {
7191 case GA_NODE_VAL_TEST:
7193 if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized()) {
7194 pnode->t.set_sparsity(1, mf->get_qdim());
7195 tensor_to_clear =
true;
7196 pgai = std::make_shared<ga_instruction_copy_vect_val_base>
7197 (pnode->tensor(), rmi.base[mf], mf->get_qdim());
7199 pgai = std::make_shared<ga_instruction_copy_val_base>
7200 (pnode->tensor(), rmi.base[mf], mf->get_qdim());
7203 case GA_NODE_GRAD_TEST:
7205 if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized()) {
7206 pnode->t.set_sparsity(2, mf->get_qdim());
7207 tensor_to_clear =
true;
7208 pgai = std::make_shared<ga_instruction_copy_vect_grad_base>
7209 (pnode->tensor(), rmi.grad[mf], mf->get_qdim());
7211 pgai = std::make_shared<ga_instruction_copy_grad_base>
7212 (pnode->tensor(), rmi.grad[mf], mf->get_qdim());
7215 case GA_NODE_HESS_TEST:
7217 pgai = std::make_shared<ga_instruction_copy_hess_base>
7218 (pnode->tensor(), rmi.hess[mf], mf->get_qdim());
7219 if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7220 pnode->t.set_sparsity(3, mf->get_qdim());
7222 case GA_NODE_DIVERG_TEST:
7224 pgai = std::make_shared<ga_instruction_copy_diverg_base>
7225 (pnode->tensor(), rmi.grad[mf], mf->get_qdim());
7227 case GA_NODE_XFEM_PLUS_VAL_TEST:
7229 pgai = std::make_shared<ga_instruction_copy_val_base>
7230 (pnode->tensor(), rmi.xfem_plus_base[mf], mf->get_qdim());
7231 if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7232 pnode->t.set_sparsity(1, mf->get_qdim());
7234 case GA_NODE_XFEM_PLUS_GRAD_TEST:
7236 pgai = std::make_shared<ga_instruction_copy_grad_base>
7237 (pnode->tensor(), rmi.xfem_plus_grad[mf], mf->get_qdim());
7238 if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7239 pnode->t.set_sparsity(2, mf->get_qdim());
7241 case GA_NODE_XFEM_PLUS_HESS_TEST:
7243 pgai = std::make_shared<ga_instruction_copy_hess_base>
7244 (pnode->tensor(), rmi.xfem_plus_hess[mf], mf->get_qdim());
7245 if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7246 pnode->t.set_sparsity(3, mf->get_qdim());
7248 case GA_NODE_XFEM_PLUS_DIVERG_TEST:
7250 pgai = std::make_shared<ga_instruction_copy_diverg_base>
7251 (pnode->tensor(), rmi.xfem_plus_grad[mf], mf->get_qdim());
7253 case GA_NODE_XFEM_MINUS_VAL_TEST:
7255 pgai = std::make_shared<ga_instruction_copy_val_base>
7256 (pnode->tensor(), rmi.xfem_minus_base[mf], mf->get_qdim());
7257 if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7258 pnode->t.set_sparsity(1, mf->get_qdim());
7260 case GA_NODE_XFEM_MINUS_GRAD_TEST:
7262 pgai = std::make_shared<ga_instruction_copy_grad_base>
7263 (pnode->tensor(), rmi.xfem_minus_grad[mf], mf->get_qdim());
7264 if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7265 pnode->t.set_sparsity(2, mf->get_qdim());
7267 case GA_NODE_XFEM_MINUS_HESS_TEST:
7269 pgai = std::make_shared<ga_instruction_copy_hess_base>
7270 (pnode->tensor(), rmi.xfem_minus_hess[mf], mf->get_qdim());
7271 if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7272 pnode->t.set_sparsity(3, mf->get_qdim());
7274 case GA_NODE_XFEM_MINUS_DIVERG_TEST:
7276 pgai = std::make_shared<ga_instruction_copy_diverg_base>
7277 (pnode->tensor(), rmi.xfem_minus_grad[mf], mf->get_qdim());
7279 case GA_NODE_ELEMENTARY_VAL_TEST:
7281 ga_instruction_set::elementary_trans_info &eti
7282 = rmi.elementary_trans_infos
7283 [std::make_tuple(pnode->elementary_name, mfo, mf)];
7285 std::make_shared<ga_instruction_elementary_trans_val_base>
7286 (pnode->tensor(), rmi.base[mf], mf->get_qdim(),
7287 workspace.elementary_transformation(pnode->elementary_name),
7288 *mfo, *mf, gis.ctx, eti.M, eti.icv);
7291 case GA_NODE_ELEMENTARY_GRAD_TEST:
7293 ga_instruction_set::elementary_trans_info &eti
7294 = rmi.elementary_trans_infos
7295 [std::make_tuple(pnode->elementary_name, mfo, mf)];
7297 std::make_shared<ga_instruction_elementary_trans_grad_base>
7298 (pnode->tensor(), rmi.grad[mf], mf->get_qdim(),
7299 workspace.elementary_transformation(pnode->elementary_name),
7300 *mfo, *mf, gis.ctx, eti.M, eti.icv);
7303 case GA_NODE_ELEMENTARY_HESS_TEST:
7305 ga_instruction_set::elementary_trans_info &eti
7306 = rmi.elementary_trans_infos
7307 [std::make_tuple(pnode->elementary_name, mfo, mf)];
7309 std::make_shared<ga_instruction_elementary_trans_hess_base>
7310 (pnode->tensor(), rmi.hess[mf], mf->get_qdim(),
7311 workspace.elementary_transformation(pnode->elementary_name),
7312 *mfo, *mf, gis.ctx, eti.M, eti.icv);
7315 case GA_NODE_ELEMENTARY_DIVERG_TEST:
7317 ga_instruction_set::elementary_trans_info &eti
7318 = rmi.elementary_trans_infos
7319 [std::make_tuple(pnode->elementary_name, mfo, mf)];
7321 std::make_shared<ga_instruction_elementary_trans_diverg_base>
7322 (pnode->tensor(), rmi.grad[mf], mf->get_qdim(),
7323 workspace.elementary_transformation(pnode->elementary_name),
7324 *mfo, *mf, gis.ctx, eti.M, eti.icv);
7329 if (pgai) rmi.instructions.push_back(std::move(pgai));
7331 workspace.add_temporary_interval_for_unreduced_variable(pnode->name);
7335 case GA_NODE_SECONDARY_DOMAIN_VAL_TEST:
7336 case GA_NODE_SECONDARY_DOMAIN_GRAD_TEST:
7337 case GA_NODE_SECONDARY_DOMAIN_HESS_TEST:
7338 case GA_NODE_SECONDARY_DOMAIN_DIVERG_TEST:
7340 GMM_ASSERT1(!function_case,
"internal error");
7341 const mesh_fem *mf = workspace.associated_mf(pnode->name);
7342 const std::string &intn = pnode->interpolate_name;
7343 auto &sdi = rmi.secondary_domain_infos;
7345 fem_interpolation_context *pctx = &(sdi.ctx);
7346 papprox_integration pai = sdi.pai;
7347 psecondary_domain psd = workspace.secondary_domain(intn);
7349 GMM_ASSERT1(&(mf->linked_mesh()) == &(psd->mim().linked_mesh()),
7350 "The finite element of variable " << pnode->name <<
7351 " and the applied integration method have to be"
7352 " defined on the same mesh for secondary domain");
7356 if (sdi.pfps.count(mf) == 0) {
7358 pgai = std::make_shared<ga_instruction_update_pfp>
7359 (*mf, sdi.pfps[mf], *pctx, gis.fp_pool);
7360 rmi.begin_instructions.push_back(std::move(pgai));
7362 }
else if (sdi.pfps.count(mf) == 0 ||
7363 !if_hierarchy.is_compatible(rmi.pfp_hierarchy[mf])) {
7364 rmi.pfp_hierarchy[mf].push_back(if_hierarchy);
7366 pgai = std::make_shared<ga_instruction_update_pfp>
7367 (*mf, sdi.pfps[mf], *pctx, gis.fp_pool);
7368 rmi.instructions.push_back(std::move(pgai));
7372 pgai = pga_instruction();
7373 switch (pnode->node_type) {
7374 case GA_NODE_SECONDARY_DOMAIN_VAL_TEST:
7375 if (sdi.base.count(mf) == 0 ||
7376 !(if_hierarchy.is_compatible(rmi.base_hierarchy[mf]))) {
7377 rmi.base_hierarchy[mf].push_back(if_hierarchy);
7378 pgai = std::make_shared<ga_instruction_val_base>
7379 (sdi.base[mf], *pctx, *mf, sdi.pfps[mf]);
7382 case GA_NODE_SECONDARY_DOMAIN_GRAD_TEST:
7383 case GA_NODE_SECONDARY_DOMAIN_DIVERG_TEST:
7384 if (sdi.grad.count(mf) == 0 ||
7385 !(if_hierarchy.is_compatible(rmi.grad_hierarchy[mf]))) {
7386 rmi.grad_hierarchy[mf].push_back(if_hierarchy);
7387 pgai = std::make_shared<ga_instruction_grad_base>
7388 (sdi.grad[mf], *pctx, *mf, sdi.pfps[mf]);
7391 case GA_NODE_SECONDARY_DOMAIN_HESS_TEST:
7392 if (sdi.hess.count(mf) == 0 ||
7393 !(if_hierarchy.is_compatible(rmi.hess_hierarchy[mf]))) {
7394 rmi.hess_hierarchy[mf].push_back(if_hierarchy);
7395 pgai = std::make_shared<ga_instruction_hess_base>
7396 (sdi.hess[mf], *pctx, *mf, sdi.pfps[mf]);
7399 default : GMM_ASSERT1(
false,
"Internal error");
7401 if (pgai) rmi.instructions.push_back(std::move(pgai));
7404 switch(pnode->node_type) {
7405 case GA_NODE_SECONDARY_DOMAIN_VAL_TEST:
7407 if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized()) {
7408 pnode->t.set_sparsity(1, mf->get_qdim());
7409 tensor_to_clear =
true;
7410 pgai = std::make_shared<ga_instruction_copy_vect_val_base>
7411 (pnode->tensor(), sdi.base[mf], mf->get_qdim());
7413 pgai = std::make_shared<ga_instruction_copy_val_base>
7414 (pnode->tensor(), sdi.base[mf], mf->get_qdim());
7417 case GA_NODE_SECONDARY_DOMAIN_GRAD_TEST:
7419 if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized()) {
7420 pnode->t.set_sparsity(2, mf->get_qdim());
7421 tensor_to_clear =
true;
7422 pgai = std::make_shared<ga_instruction_copy_vect_grad_base>
7423 (pnode->tensor(), sdi.grad[mf], mf->get_qdim());
7425 pgai = std::make_shared<ga_instruction_copy_grad_base>
7426 (pnode->tensor(), sdi.grad[mf], mf->get_qdim());
7429 case GA_NODE_SECONDARY_DOMAIN_HESS_TEST:
7431 pgai = std::make_shared<ga_instruction_copy_hess_base>
7432 (pnode->tensor(), sdi.hess[mf], mf->get_qdim());
7433 if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7434 pnode->t.set_sparsity(3, mf->get_qdim());
7436 case GA_NODE_SECONDARY_DOMAIN_DIVERG_TEST:
7438 pgai = std::make_shared<ga_instruction_copy_diverg_base>
7439 (pnode->tensor(), sdi.grad[mf], mf->get_qdim());
7443 if (pgai) rmi.instructions.push_back(std::move(pgai));
7445 workspace.add_temporary_interval_for_unreduced_variable(pnode->name);
7449 case GA_NODE_INTERPOLATE_VAL_TEST:
case GA_NODE_INTERPOLATE_GRAD_TEST:
7450 case GA_NODE_INTERPOLATE_HESS_TEST:
case GA_NODE_INTERPOLATE_DIVERG_TEST:
7452 const mesh_fem *mfn = workspace.associated_mf(pnode->name), **mfg = 0;
7453 const std::string &intn = pnode->interpolate_name;
7454 const mesh **m2 = &(rmi.interpolate_infos[intn].m);
7455 if (workspace.variable_group_exists(pnode->name)) {
7456 ga_instruction_set::variable_group_info &vgi =
7457 rmi.interpolate_infos[intn].groups_info[pnode->name];
7458 mfg = &(vgi.mf); mfn = 0;
7461 if (pnode->node_type == GA_NODE_INTERPOLATE_VAL_TEST) {
7463 pgai = std::make_shared<ga_instruction_interpolate_val_base>
7464 (pnode->tensor(), m2, mfn, mfg, gis.ipt,
7465 workspace.qdim(pnode->name), rmi.interpolate_infos[intn],
7467 }
else if (pnode->node_type == GA_NODE_INTERPOLATE_GRAD_TEST) {
7469 pgai = std::make_shared<ga_instruction_interpolate_grad_base>
7470 (pnode->tensor(), m2, mfn, mfg, gis.ipt,
7471 workspace.qdim(pnode->name),
7472 rmi.interpolate_infos[intn], gis.fp_pool);
7473 }
else if (pnode->node_type == GA_NODE_INTERPOLATE_HESS_TEST) {
7475 pgai = std::make_shared<ga_instruction_interpolate_hess_base>
7476 (pnode->tensor(), m2, mfn, mfg, gis.ipt,
7477 workspace.qdim(pnode->name),
7478 rmi.interpolate_infos[intn], gis.fp_pool);
7481 pgai = std::make_shared<ga_instruction_interpolate_diverg_base>
7482 (pnode->tensor(), m2, mfn, mfg, gis.ipt,
7483 workspace.qdim(pnode->name),
7484 rmi.interpolate_infos[intn], gis.fp_pool);
7486 rmi.instructions.push_back(std::move(pgai));
7487 workspace.add_temporary_interval_for_unreduced_variable(pnode->name);
7492 switch(pnode->op_type) {
7495 if (pnode->tensor().size() == 1) {
7496 GA_DEBUG_ASSERT(child0->tensor().size() == 1,
7497 "Internal error: child0 not scalar");
7498 GA_DEBUG_ASSERT(child1->tensor().size() == 1,
7499 "Internal error: child1 not scalar");
7500 pgai = std::make_shared<ga_instruction_scalar_add>
7501 (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7503 pgai = std::make_shared<ga_instruction_add>
7504 (pnode->tensor(), child0->tensor(), child1->tensor());
7506 ga_propagate_sum_structure
7507 (pnode->t, child0->t, child1->t,
7508 pnode->nb_test_functions() == 1 && pnode->tensor_order() == 2
7509 && ga_is_sum_with_transpose(child0, child1, workspace));
7510 rmi.instructions.push_back(std::move(pgai));
7514 if (pnode->tensor().size() == 1) {
7515 GA_DEBUG_ASSERT(child0->tensor().size() == 1,
7516 "Internal error: child0 not scalar");
7517 GA_DEBUG_ASSERT(child1->tensor().size() == 1,
7518 "Internal error: child1 not scalar");
7519 pgai = std::make_shared<ga_instruction_scalar_sub>
7520 (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7522 pgai = std::make_shared<ga_instruction_sub>
7523 (pnode->tensor(), child0->tensor(), child1->tensor());
7525 ga_propagate_sum_structure(pnode->t, child0->t, child1->t);
7526 rmi.instructions.push_back(std::move(pgai));
7529 case GA_UNARY_MINUS:
7530 if (pnode->tensor().size() == 1) {
7531 GA_DEBUG_ASSERT(child0->tensor().size() == 1,
"Internal error");
7532 pgai = std::make_shared<ga_instruction_scalar_scalar_mult>
7533 (pnode->tensor()[0], child0->tensor()[0], minus);
7535 pgai = std::make_shared<ga_instruction_scalar_mult>
7536 (pnode->tensor(), child0->tensor(), minus);
7538 pnode->t.copy_structure(child0->t);
7539 rmi.instructions.push_back(std::move(pgai));
7543 case GA_DOT:
case GA_COLON:
case GA_MULT:
7545 size_type tps0 = child0->tensor_proper_size();
7546 size_type tps1 = child1->tensor_proper_size();
7547 size_type s1 = (tps0 * tps1) / pnode->tensor_proper_size();
7550 pgai = pga_instruction();
7551 if ((pnode->op_type == GA_DOT && dim1 <= 1) ||
7552 (pnode->op_type == GA_COLON && dim1 <= 2) ||
7553 (pnode->op_type == GA_MULT && dim0 == 4) ||
7554 (pnode->op_type == GA_MULT && dim1 <= 1) ||
7555 child0->tensor().size() == 1 || tps1 == 1) {
7557 if (child0->tensor().size() == 1 && child1->tensor().size() == 1) {
7558 pgai = std::make_shared<ga_instruction_scalar_scalar_mult>
7559 (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7561 else if (child0->tensor().size() == 1) {
7562 pnode->t.copy_structure(child1->t);
7563 pgai = std::make_shared<ga_instruction_scalar_mult>
7564 (pnode->tensor(), child1->tensor(), child0->tensor()[0]);
7566 else if (child1->tensor().size() == 1) {
7567 pnode->t.copy_structure(child0->t);
7568 pgai = std::make_shared<ga_instruction_scalar_mult>
7569 (pnode->tensor(), child0->tensor(), child1->tensor()[0]);
7571 else if (pnode->test_function_type < 3) {
7574 pgai = ga_uniform_instruction_simple_tmult
7575 (pnode->tensor(), child0->tensor(), child1->tensor());
7577 pgai = std::make_shared<ga_instruction_simple_tmult>
7578 (pnode->tensor(), child0->tensor(), child1->tensor());
7582 pgai = ga_uniform_instruction_simple_tmult
7583 (pnode->tensor(), child1->tensor(), child0->tensor());
7585 pgai = std::make_shared<ga_instruction_simple_tmult>
7586 (pnode->tensor(), child1->tensor(), child0->tensor());
7587 }
else if (is_uniform)
7588 pgai = ga_uniform_instruction_contraction_switch
7589 (pnode->t, child0->t, child1->t, s2, tensor_to_clear);
7591 pgai = ga_instruction_contraction_switch
7592 (pnode->t, child0->t, child1->t, s2, tensor_to_clear);
7595 if (child1->test_function_type == 1 ||
7596 child1->test_function_type == 3) {
7597 if (child1->test_function_type == 3 ||
7598 child1->tensor_proper_size() <= s2) {
7601 pgai = ga_uniform_instruction_simple_tmult
7602 (pnode->tensor(), child1->tensor(), child0->tensor());
7604 pgai = std::make_shared<ga_instruction_simple_tmult>
7605 (pnode->tensor(), child1->tensor(), child0->tensor());
7606 }
else if (is_uniform)
7607 pgai = ga_uniform_instruction_contraction_switch
7608 (pnode->t, child0->t, child1->t, s2, tensor_to_clear);
7610 pgai = ga_instruction_contraction_switch
7611 (pnode->t, child0->t, child1->t, s2, tensor_to_clear);
7613 pgai = std::make_shared<ga_instruction_spec_contraction>
7614 (pnode->tensor(), child1->tensor(), child0->tensor(), s2);
7615 }
else if (child1->test_function_type == 0 ||
7616 (child0->tensor_proper_size() == s2 &&
7617 child1->tensor_proper_size() == s2)) {
7620 pgai = ga_uniform_instruction_simple_tmult
7621 (pnode->tensor(), child0->tensor(), child1->tensor());
7623 pgai = std::make_shared<ga_instruction_simple_tmult>
7624 (pnode->tensor(), child0->tensor(), child1->tensor());
7627 pgai = ga_uniform_instruction_contraction_switch
7628 (pnode->t, child1->t, child0->t, s2, tensor_to_clear);
7630 pgai = ga_instruction_contraction_switch
7631 (pnode->t, child1->t, child0->t, s2, tensor_to_clear);
7634 if (child0->tensor_proper_size() == s2)
7635 pgai = ga_uniform_instruction_contraction_switch
7636 (pnode->t, child1->t, child0->t, s2, tensor_to_clear);
7637 else if (child1->tensor_proper_size() == s2)
7638 pgai = std::make_shared<ga_instruction_spec_contraction>
7639 (pnode->tensor(), child0->tensor(), child1->tensor(), s2);
7641 pgai = std::make_shared<ga_instruction_spec2_contraction>
7642 (pnode->tensor(), child0->tensor(), child1->tensor(), s2);
7647 if (pnode->test_function_type < 3) {
7650 pgai = ga_uniform_instruction_simple_tmult
7651 (pnode->tensor(), child0->tensor(), child1->tensor());
7653 pgai = std::make_shared<ga_instruction_simple_tmult>
7654 (pnode->tensor(), child0->tensor(), child1->tensor());
7656 if (child1->test_function_type == 0)
7657 pgai = std::make_shared<ga_instruction_matrix_mult>
7658 (pnode->tensor(), child0->tensor(), child1->tensor(), s2);
7660 pgai = std::make_shared<ga_instruction_matrix_mult_spec>
7661 (pnode->tensor(), child0->tensor(), child1->tensor(),
7662 s2, tps0/s2, tps1/s2);
7665 if (child0->tensor_proper_size() == 1) {
7666 if (child0->test_function_type == 0 ||
7667 child0->test_function_type == 1) {
7669 pgai = ga_uniform_instruction_simple_tmult
7670 (pnode->tensor(), child0->tensor(), child1->tensor());
7672 pgai = std::make_shared<ga_instruction_simple_tmult>
7673 (pnode->tensor(), child0->tensor(), child1->tensor());
7675 pgai = std::make_shared<ga_instruction_spec_tmult>
7676 (pnode->tensor(), child1->tensor(), child0->tensor(),
7679 if (child1->test_function_type == 0)
7680 pgai = std::make_shared<ga_instruction_matrix_mult>
7681 (pnode->tensor(), child0->tensor(), child1->tensor(), s2);
7682 else if (child1->test_function_type == 2)
7683 pgai = std::make_shared<ga_instruction_matrix_mult_spec>
7684 (pnode->tensor(), child0->tensor(), child1->tensor(),
7685 s2, tps0/s2, tps1/s2);
7687 pgai = std::make_shared<ga_instruction_matrix_mult_spec2>
7688 (pnode->tensor(), child0->tensor(), child1->tensor(),
7689 s2, tps0/s2, tps1/s2);
7693 rmi.instructions.push_back(std::move(pgai));
7698 if (child0->tensor().size() == 1 && child1->tensor().size() == 1) {
7699 pgai = std::make_shared<ga_instruction_scalar_scalar_div>
7700 (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7701 }
else if (child1->tensor().size() == 1) {
7702 pnode->t.copy_structure(child0->t);
7703 pgai = std::make_shared<ga_instruction_scalar_div>
7704 (pnode->tensor(), child0->tensor(), child1->tensor()[0]);
7705 }
else GMM_ASSERT1(
false,
"Internal error");
7706 rmi.instructions.push_back(std::move(pgai));
7710 pnode->t.set_to_copy(child0->t);
7711 pgai = std::make_shared<ga_instruction_print_tensor>
7712 (pnode->tensor(), child0, gis.ctx, gis.nbpt, gis.ipt);
7713 rmi.instructions.push_back(std::move(pgai));
7717 if (pnode->tensor_proper_size() > 1) {
7718 size_type n1 = child0->tensor_proper_size(0);
7719 size_type n2 = (child0->tensor_order() > 1) ?
7720 child0->tensor_proper_size(1) : 1;
7722 for (
size_type i = 2; i < child0->tensor_order(); ++i)
7723 nn *= child0->tensor_proper_size(i);
7724 if (child0->nb_test_functions() == 0)
7725 pgai = std::make_shared<ga_instruction_transpose_no_test>
7726 (pnode->tensor(), child0->tensor(), n1, n2, nn);
7728 pgai = std::make_shared<ga_instruction_transpose>
7729 (pnode->tensor(), child0->tensor(), n1, n2, nn);
7730 if (child0->nb_test_functions() == 1
7731 && child0->tensor_order() == 2 && n1 == n2) {
7733 std::vector<bool> mask;
7734 if (ga_matrix_op_mask(child0->t, n1,
false, Q, mask))
7736 (Q, std::make_shared<std::vector<bool>>(std::move(mask)),
7737 child0->t.mask_symmetric());
7739 rmi.instructions.push_back(std::move(pgai));
7741 pnode->t.set_to_copy(child0->t);
7746 if (pnode->tensor_proper_size() != 1) {
7748 std::vector<bool> mask;
7749 if (pnode->nb_test_functions() == 1 && child0->tensor_order() == 2
7750 && ga_matrix_op_mask(child0->t, d,
true, Q, mask)) {
7751 pgai = std::make_shared<ga_instruction_sym_masked>
7752 (pnode->tensor(), child0->tensor(), Q, d, mask);
7754 (Q, std::make_shared<std::vector<bool>>(std::move(mask)),
true);
7755 tensor_to_clear =
true;
7757 pgai = std::make_shared<ga_instruction_sym>
7758 (pnode->tensor(), child0->tensor());
7759 rmi.instructions.push_back(std::move(pgai));
7761 pnode->t.set_to_copy(child0->t);
7767 pgai = std::make_shared<ga_instruction_skew>
7768 (pnode->tensor(), child0->tensor());
7769 rmi.instructions.push_back(std::move(pgai));
7775 size_type N = (child0->tensor_proper_size() == 1) ? 1:size0.back();
7777 pnode->t.set_to_copy(child0->t);
7779 pgai = std::make_shared<ga_instruction_trace>
7780 (pnode->tensor(), child0->tensor(), N);
7781 rmi.instructions.push_back(std::move(pgai));
7788 size_type N = (child0->tensor_proper_size() == 1) ? 1:size0.back();
7789 pgai = std::make_shared<ga_instruction_deviator>
7790 (pnode->tensor(), child0->tensor(), N);
7791 rmi.instructions.push_back(std::move(pgai));
7797 if (child0->tensor().size() == 1 && child1->tensor().size() == 1) {
7798 pgai = std::make_shared<ga_instruction_scalar_scalar_mult>
7799 (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7800 }
else if (child0->tensor().size() == 1) {
7801 pnode->t.copy_structure(child1->t);
7802 pgai = std::make_shared<ga_instruction_scalar_mult>
7803 (pnode->tensor(), child1->tensor(), child0->tensor()[0]);
7805 else if (child1->tensor().size() == 1) {
7806 pnode->t.copy_structure(child0->t);
7807 pgai = std::make_shared<ga_instruction_scalar_mult>
7808 (pnode->tensor(), child0->tensor(), child1->tensor()[0]);
7810 else if (child1->test_function_type == 0)
7811 pgai = std::make_shared<ga_instruction_dotmult>
7812 (pnode->tensor(), child0->tensor(), child1->tensor());
7813 else if (child0->test_function_type == 0)
7814 pgai = std::make_shared<ga_instruction_dotmult>
7815 (pnode->tensor(), child1->tensor(), child0->tensor());
7816 else if (child0->test_function_type == 1)
7817 pgai = std::make_shared<ga_instruction_dotmult_spec>
7818 (pnode->tensor(), child0->tensor(), child1->tensor());
7820 pgai = std::make_shared<ga_instruction_dotmult_spec>
7821 (pnode->tensor(), child1->tensor(), child0->tensor());
7823 rmi.instructions.push_back(std::move(pgai));
7828 if (child0->tensor().size() == 1 && child1->tensor().size() == 1) {
7829 pgai = std::make_shared<ga_instruction_scalar_scalar_div>
7830 (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7831 }
else if (child1->tensor().size() == 1) {
7832 pnode->t.copy_structure(child0->t);
7833 pgai = std::make_shared<ga_instruction_scalar_div>
7834 (pnode->tensor(), child0->tensor(), child1->tensor()[0]);
7835 }
else if (child1->test_function_type == 0) {
7836 pgai = std::make_shared<ga_instruction_dotdiv>
7837 (pnode->tensor(), child0->tensor(), child1->tensor());
7838 }
else GMM_ASSERT1(
false,
"Internal error");
7839 rmi.instructions.push_back(std::move(pgai));
7844 if (child0->tensor().size() == 1 && child1->tensor().size() == 1) {
7845 pgai = std::make_shared<ga_instruction_scalar_scalar_mult>
7846 (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7847 }
else if (child0->tensor().size() == 1) {
7848 pnode->t.copy_structure(child1->t);
7849 pgai = std::make_shared<ga_instruction_scalar_mult>
7850 (pnode->tensor(), child1->tensor(), child0->tensor()[0]);
7852 else if (child1->tensor().size() == 1) {
7853 pnode->t.copy_structure(child0->t);
7854 pgai = std::make_shared<ga_instruction_scalar_mult>
7855 (pnode->tensor(), child0->tensor(), child1->tensor()[0]);
7857 else if (child1->test_function_type == 0) {
7859 pgai = ga_uniform_instruction_simple_tmult
7860 (pnode->tensor(), child0->tensor(), child1->tensor());
7862 pgai = std::make_shared<ga_instruction_simple_tmult>
7863 (pnode->tensor(), child0->tensor(), child1->tensor());
7864 }
else if (child1->tensor_proper_size() == 1)
7865 pgai = std::make_shared<ga_instruction_spec2_tmult>
7866 (pnode->tensor(), child0->tensor(), child1->tensor());
7868 pgai = std::make_shared<ga_instruction_spec_tmult>
7869 (pnode->tensor(), child0->tensor(), child1->tensor(),
7870 child0->tensor_proper_size(),
7871 child1->tensor_proper_size());
7873 rmi.instructions.push_back(std::move(pgai));
7876 default:GMM_ASSERT1(
false,
"Unexpected operation. Internal error.");
7880 case GA_NODE_C_MATRIX:
7882 if (pnode->test_function_type) {
7883 std::vector<const base_tensor *> components(pnode->children.size());
7884 for (
size_type i = 0; i < pnode->children.size(); ++i)
7885 components[i] = &(pnode->children[i]->tensor());
7886 pgai = std::make_shared<ga_instruction_c_matrix_with_tests>
7887 (pnode->tensor(), components);
7889 std::vector<scalar_type *> components(pnode->children.size());
7890 for (
size_type i = 0; i < pnode->children.size(); ++i)
7891 components[i] = &(pnode->children[i]->tensor()[0]);
7892 pgai = std::make_shared<ga_instruction_simple_c_matrix>
7893 (pnode->tensor(), components);
7895 rmi.instructions.push_back(std::move(pgai));
7899 case GA_NODE_PARAMS:
7900 if (child0->node_type == GA_NODE_RESHAPE) {
7901 pgai = std::make_shared<ga_instruction_copy_tensor>(pnode->tensor(),
7903 rmi.instructions.push_back(std::move(pgai));
7904 }
else if (child0->node_type == GA_NODE_CROSS_PRODUCT) {
7905 pga_tree_node child2 = pnode->children[2];
7906 if (child1->test_function_type==2 && child2->test_function_type==1)
7907 pgai = std::make_shared<ga_instruction_cross_product_tf>
7908 (pnode->tensor(), child2->tensor(), child1->tensor(),
true);
7909 else if (child1->test_function_type || child2->test_function_type)
7910 pgai = std::make_shared<ga_instruction_cross_product_tf>
7911 (pnode->tensor(), child1->tensor(), child2->tensor(),
false);
7913 pgai = std::make_shared<ga_instruction_cross_product>
7914 (pnode->tensor(), child1->tensor(), child2->tensor());
7915 rmi.instructions.push_back(std::move(pgai));
7916 }
else if (child0->node_type == GA_NODE_IND_MOVE_LAST) {
7918 ind =
size_type(round(pnode->children[2]->tensor()[0])-1);
7920 for (
size_type i = 0; i < child1->tensor_order(); ++i)
7921 if (i>ind) ii2 *= child1->tensor_proper_size(i);
7922 size_type nn = child1->tensor_proper_size(ind);
7923 pgai = std::make_shared<ga_instruction_index_move_last>
7924 (pnode->tensor(), child1->tensor(), nn, ii2);
7925 rmi.instructions.push_back(std::move(pgai));
7926 }
else if (child0->node_type == GA_NODE_SWAP_IND) {
7929 ind[i] =
size_type(round(pnode->children[i]->tensor()[0])-1);
7930 if (ind[2] > ind[3]) std::swap(ind[2], ind[3]);
7932 for (
size_type i = 0; i < child1->tensor_order(); ++i) {
7933 if (i>ind[2] && i<ind[3]) ii2 *= child1->tensor_proper_size(i);
7934 if (i>ind[3]) ii3 *= child1->tensor_proper_size(i);
7936 size_type nn1 = child1->tensor_proper_size(ind[2]);
7937 size_type nn2 = child1->tensor_proper_size(ind[3]);
7939 pgai = std::make_shared<ga_instruction_swap_indices>
7940 (pnode->tensor(), child1->tensor(), nn1, nn2, ii2, ii3);
7941 rmi.instructions.push_back(std::move(pgai));
7942 }
else if (child0->node_type == GA_NODE_CONTRACT) {
7943 std::vector<size_type> ind(2), indsize(2);
7944 pga_tree_node child2(0);
7945 if (pnode->children.size() == 4)
7946 { ind[0] = 2; ind[1] = 3; }
7947 else if (pnode->children.size() == 5)
7948 { ind[0] = 2; ind[1] = 4; child2 = pnode->children[3]; }
7949 else if (pnode->children.size() == 7) {
7950 ind.resize(4); indsize.resize(4);
7951 ind[0] = 2; ind[1] = 3; ind[2] = 5; ind[3] = 6;
7952 child2 = pnode->children[4];
7955 for (
size_type i = 1; i < pnode->children.size(); ++i) {
7957 ind[kk] =
size_type(round(pnode->children[i]->tensor()[0])-1);
7958 indsize[kk] = pnode->children[ll]->tensor_proper_size(ind[kk]);
7963 if (pnode->children.size() == 4) {
7965 if (i1 > i2) std::swap(i1, i2);
7967 for (
size_type i = 0; i < child1->tensor_order(); ++i) {
7968 if (i > i1 && i < i2) ii2 *= child1->tensor_proper_size(i);
7969 if (i > i2) ii3 *= child1->tensor_proper_size(i);
7971 pgai = std::make_shared<ga_instruction_contract_1_1>
7972 (pnode->tensor(), child1->tensor(), indsize[0], ii2, ii3);
7974 else if (pnode->children.size() == 5) {
7977 size_type ii1 = 1, ii2 = 1, ii3 = 1, ii4 = 1;
7978 for (
size_type i = 0; i < child1->tensor_order(); ++i) {
7979 if (i < i1) ii1 *= child1->tensor_proper_size(i);
7980 if (i > i1) ii2 *= child1->tensor_proper_size(i);
7982 for (
size_type i = 0; i < child2->tensor_order(); ++i) {
7983 if (i < i2) ii3 *= child2->tensor_proper_size(i);
7984 if (i > i2) ii4 *= child2->tensor_proper_size(i);
7986 if (child1->test_function_type==1 && child2->test_function_type==2)
7987 pgai = std::make_shared<ga_instruction_contract_2_1_rev>
7988 (pnode->tensor(), child1->tensor(), child2->tensor(),
7989 indsize[0], ii1, ii2, ii3, ii4);
7991 pgai = std::make_shared<ga_instruction_contract_2_1>
7992 (pnode->tensor(), child1->tensor(), child2->tensor(),
7993 indsize[0], ii1, ii2, ii3, ii4);
7995 else if (pnode->children.size() == 7) {
7997 size_type i1 = ind[0], i2 = ind[1], i3 = ind[2], i4 = ind[3];
7998 size_type nn1 = indsize[0], nn2 = indsize[1];
7999 size_type ii1 = 1, ii2 = 1, ii3 = 1, ii4 = 1, ii5 = 1, ii6 = 1;
8001 { std::swap(i1, i2); std::swap(i3, i4); std::swap(nn1, nn2); }
8002 for (
size_type i = 0; i < child1->tensor_order(); ++i) {
8003 if (i < i1) ii1 *= child1->tensor_proper_size(i);
8004 if (i > i1 && i < i2) ii2 *= child1->tensor_proper_size(i);
8005 if (i > i2) ii3 *= child1->tensor_proper_size(i);
8007 for (
size_type i = 0; i < child2->tensor_order(); ++i) {
8008 if (i < i3 && i < i4) ii4 *= child2->tensor_proper_size(i);
8009 if ((i > i3 && i < i4) || (i > i4 && i < i3))
8010 ii5 *= child2->tensor_proper_size(i);
8011 if (i > i3 && i > i4) ii6 *= child2->tensor_proper_size(i);
8013 if (child1->test_function_type==1 && child2->test_function_type==2)
8014 pgai = std::make_shared<ga_instruction_contract_2_2_rev>
8015 (pnode->tensor(), child1->tensor(), child2->tensor(),
8016 nn1, nn2, ii1, ii2, ii3, ii4, ii5, ii6, i4 < i3);
8018 pgai = std::make_shared<ga_instruction_contract_2_2>
8019 (pnode->tensor(), child1->tensor(), child2->tensor(),
8020 nn1, nn2, ii1, ii2, ii3, ii4, ii5, ii6, i4 < i3);
8022 rmi.instructions.push_back(std::move(pgai));
8023 }
else if (child0->node_type == GA_NODE_PREDEF_FUNC) {
8025 std::string name = child0->name;
8026 const ga_predef_function_tab &PREDEF_FUNCTIONS
8028 ga_predef_function_tab::const_iterator it = PREDEF_FUNCTIONS.find(name);
8029 const ga_predef_function &F = it->second;
8031 pga_tree_node child2 = (nbargs == 2) ? pnode->children[2] : child1;
8034 if (child1->tensor().size() == 1) {
8036 pgai = std::make_shared<ga_instruction_eval_func_1arg_1res>
8037 (pnode->tensor()[0], child1->tensor()[0], F.f1());
8039 pgai = std::make_shared<ga_instruction_eval_func_1arg_1res_expr>
8040 (pnode->tensor()[0], child1->tensor()[0], F);
8043 pgai = std::make_shared<ga_instruction_eval_func_1arg>
8044 (pnode->tensor(), child1->tensor(), F.f1());
8046 pgai = std::make_shared<ga_instruction_eval_func_1arg_expr>
8047 (pnode->tensor(), child1->tensor(), F);
8050 if (child1->tensor().size() == 1 && child2->tensor().size() == 1) {
8052 pgai = std::make_shared<ga_instruction_eval_func_2arg_1res>
8053 (pnode->tensor()[0], child1->tensor()[0], child2->tensor()[0],
8056 pgai = std::make_shared<ga_instruction_eval_func_2arg_1res_expr>
8057 (pnode->tensor()[0], child1->tensor()[0], child2->tensor()[0],
8059 }
else if (child1->tensor().size() == 1) {
8062 std::make_shared<ga_instruction_eval_func_2arg_first_scalar>
8063 (pnode->tensor(), child1->tensor(), child2->tensor(), F.f2());
8066 std::make_shared<ga_instruction_eval_func_2arg_first_scalar_expr>
8067 (pnode->tensor(), child1->tensor(), child2->tensor(), F);
8068 }
else if (child2->tensor().size() == 1) {
8071 std::make_shared<ga_instruction_eval_func_2arg_second_scalar>
8072 (pnode->tensor(), child1->tensor(), child2->tensor(), F.f2());
8075 std::make_shared<ga_instruction_eval_func_2arg_second_scalar_expr>
8076 (pnode->tensor(), child1->tensor(), child2->tensor(), F);
8079 pgai = std::make_shared<ga_instruction_eval_func_2arg>
8080 (pnode->tensor(), child1->tensor(), child2->tensor(), F.f2());
8082 pgai = std::make_shared<ga_instruction_eval_func_2arg_expr>
8083 (pnode->tensor(), child1->tensor(), child2->tensor(), F);
8086 rmi.instructions.push_back(std::move(pgai));
8088 }
else if (child0->node_type == GA_NODE_SPEC_FUNC) {
8090 GMM_ASSERT1(
false,
"Internal error");
8092 }
else if (child0->node_type == GA_NODE_OPERATOR) {
8094 ga_predef_operator_tab &PREDEF_OPERATORS
8096 ga_predef_operator_tab::T::iterator it
8097 = PREDEF_OPERATORS.tab.find(child0->name);
8098 const ga_nonlinear_operator &OP = *(it->second);
8099 ga_nonlinear_operator::arg_list args;
8100 for (
size_type i = 1; i < pnode->children.size(); ++i)
8101 args.push_back(&(pnode->children[i]->tensor()));
8103 if (child0->der1 && child0->der2 == 0) {
8104 pgai = std::make_shared<ga_instruction_eval_derivative_OP>
8105 (pnode->tensor(), OP, args, child0->der1);
8106 }
else if (child0->der1 && child0->der2) {
8107 pgai = std::make_shared<ga_instruction_eval_second_derivative_OP>
8108 (pnode->tensor(), OP, args, child0->der1, child0->der2);
8110 pgai = std::make_shared<ga_instruction_eval_OP>(pnode->tensor(),
8113 rmi.instructions.push_back(std::move(pgai));
8116 bgeot::multi_index mi1(size0.size()), indices;
8117 size_type nb_test = pnode->nb_test_functions();
8118 if (pnode->tensor().size() == 1) {
8119 for (
size_type i = 0; i < child0->tensor_order(); ++i)
8120 mi1[i+nb_test] =
size_type(round(pnode->children[i+1]->tensor()[0])-1);
8121 pgai = std::make_shared<ga_instruction_copy_scalar>
8122 (pnode->tensor()[0], child0->tensor()(mi1));
8124 for (
size_type i = 0; i < nb_test; ++i) indices.push_back(i);
8125 for (
size_type i = 0; i < child0->tensor_order(); ++i) {
8126 if (pnode->children[i+1]->node_type != GA_NODE_ALLINDICES)
8128 =
size_type(round(pnode->children[i+1]->tensor()[0])- 1);
8130 indices.push_back(i+nb_test);
8132 pgai = std::make_shared<ga_instruction_tensor_slice>
8133 (pnode->tensor(), child0->tensor(), mi1, indices);
8135 rmi.instructions.push_back(std::move(pgai));
8140 default:GMM_ASSERT1(
false,
"Unexpected node type " << pnode->node_type
8141 <<
" in compilation. Internal error.");
8143 if (tensor_to_clear) {
8146 pgai = std::make_shared<ga_instruction_clear_tensor>(pnode->tensor());
8147 rmi.elt_instructions.push_back(std::move(pgai));
8150 rmi.node_list[pnode->hash_value].push_back(pnode);
8153 void ga_compile_function(ga_workspace &workspace,
8154 ga_instruction_set &gis,
bool scalar) {
8155 for (
size_type i = 0; i < workspace.nb_trees(); ++i) {
8156 const ga_workspace::tree_description &td = workspace.tree_info(i);
8158 gis.trees.push_back(*(td.ptree));
8159 pga_tree_node root = gis.trees.back().root;
8161 GMM_ASSERT1(!scalar || (root->tensor().size() == 1),
8162 "The result of the given expression is not a scalar");
8163 ga_instruction_set::region_mim rm(td.mim, td.rg, 0);
8164 gis.all_instructions[rm].m = td.m;
8165 ga_if_hierarchy if_hierarchy;
8166 ga_compile_node(root, workspace, gis, gis.all_instructions[rm],
8167 *(td.m),
true, if_hierarchy);
8169 gis.coeff = scalar_type(1);
8170 pga_instruction pgai;
8171 workspace.assembled_tensor() = root->tensor();
8172 pgai = std::make_shared<ga_instruction_add_to_coeff>
8173 (workspace.assembled_tensor(), root->tensor(), gis.coeff);
8174 gis.all_instructions[rm].instructions.push_back(std::move(pgai));
8179 static bool ga_node_used_interpolates
8180 (
const pga_tree_node pnode,
const ga_workspace &workspace,
8181 std::map<std::string, std::set<std::string> > &interpolates,
8182 std::set<std::string> &interpolates_der) {
8184 bool intrpl(pnode->node_type == GA_NODE_INTERPOLATE_VAL ||
8185 pnode->node_type == GA_NODE_INTERPOLATE_GRAD ||
8186 pnode->node_type == GA_NODE_INTERPOLATE_HESS ||
8187 pnode->node_type == GA_NODE_INTERPOLATE_DIVERG);
8188 bool intrpl_test(pnode->node_type == GA_NODE_INTERPOLATE_VAL_TEST ||
8189 pnode->node_type == GA_NODE_INTERPOLATE_GRAD_TEST ||
8190 pnode->node_type == GA_NODE_INTERPOLATE_HESS_TEST ||
8191 pnode->node_type == GA_NODE_INTERPOLATE_DIVERG_TEST);
8193 if (intrpl || intrpl_test ||
8194 pnode->node_type == GA_NODE_INTERPOLATE_FILTER ||
8195 pnode->node_type == GA_NODE_INTERPOLATE_X ||
8196 pnode->node_type == GA_NODE_INTERPOLATE_NORMAL) {
8197 interpolates[pnode->interpolate_name].size();
8198 if (intrpl || intrpl_test) {
8199 if (workspace.variable_group_exists(pnode->name))
8200 interpolates[pnode->interpolate_name].insert(pnode->name);
8204 if (pnode->node_type == GA_NODE_INTERPOLATE_DERIVATIVE) {
8205 interpolates_der.insert(pnode->interpolate_name_der);
8206 interpolates[pnode->interpolate_name_der].size();
8207 if (workspace.variable_group_exists(pnode->name))
8208 interpolates[pnode->interpolate_name_der].insert(pnode->name);
8210 for (
size_type i = 0; i < pnode->children.size(); ++i)
8211 found = ga_node_used_interpolates(pnode->children[i], workspace,
8212 interpolates, interpolates_der)
8218 static void ga_compile_interpolate_trans
8219 (
const pga_tree_node pnode,
const ga_workspace &workspace,
8220 ga_instruction_set &gis, ga_instruction_set::region_mim_instructions &rmi,
8223 std::set<std::string> interpolates_der;
8224 std::map<std::string, std::set<std::string> > transformations;
8225 ga_node_used_interpolates(pnode, workspace, transformations,
8228 for (
const auto &transformation : transformations) {
8229 const std::string &transname = transformation.first;
8230 bool compute_der = (interpolates_der.count(transname) != 0);
8231 if (rmi.transformations.count(transname) == 0 ||
8232 (compute_der && rmi.transformations_der.count(transname) == 0)) {
8233 rmi.transformations[transname].size();
8234 gis.transformations.insert(transname);
8235 if (compute_der) rmi.transformations_der.insert(transname);
8236 pga_instruction pgai;
8237 if (transname.compare(
"neighbor_element") == 0 ||
8238 transname.compare(
"neighbour_elt") == 0) {
8239 pgai = std::make_shared<ga_instruction_neighbor_transformation_call>
8240 (workspace, rmi.interpolate_infos[transname],
8241 workspace.interpolate_transformation(transname), gis.ctx,
8242 m, gis.ipt, gis.pai, gis.gp_pool, gis.neighbor_corresp);
8244 pgai = std::make_shared<ga_instruction_transformation_call>
8245 (workspace, rmi.interpolate_infos[transname],
8246 workspace.interpolate_transformation(transname), gis.ctx,
8247 gis.Normal, m, compute_der);
8249 if (pgai) rmi.instructions.push_back(std::move(pgai));
8252 for (
const std::string &nodename : transformation.second) {
8253 if (rmi.transformations[transname].count(nodename) == 0) {
8254 auto&& inin = rmi.interpolate_infos[transname];
8255 pga_instruction pgai =
8256 std::make_shared<ga_instruction_update_group_info>
8257 (workspace, gis, inin, nodename, inin.groups_info[nodename]);
8258 rmi.instructions.push_back(std::move(pgai));
8259 rmi.transformations[transname].insert(nodename);
8265 void ga_compile_interpolation(ga_workspace &workspace,
8266 ga_instruction_set &gis) {
8267 gis.transformations.clear();
8268 gis.all_instructions.clear();
8269 for (
size_type i = 0; i < workspace.nb_trees(); ++i) {
8270 const ga_workspace::tree_description &td = workspace.tree_info(i);
8271 if (td.operation != ga_workspace::ASSEMBLY) {
8272 gis.trees.push_back(*(td.ptree));
8275 const mesh *m = td.m;
8276 GMM_ASSERT1(m,
"Internal error");
8277 ga_semantic_analysis(gis.trees.back(), workspace, *m,
8278 ref_elt_dim_of_mesh(*m, *(td.rg)),
true,
false);
8279 pga_tree_node root = gis.trees.back().root;
8282 ga_instruction_set::region_mim rm(td.mim, td.rg, 0);
8283 auto &rmi = gis.all_instructions[rm];
8287 ga_compile_interpolate_trans(root, workspace, gis, rmi, *(td.m));
8288 ga_compile_node(root, workspace, gis, rmi, *(td.m),
false,
8289 rmi.current_hierarchy);
8292 workspace.assembled_tensor() = root->tensor();
8293 pga_instruction pgai = std::make_shared<ga_instruction_add_to>
8294 (workspace.assembled_tensor(), root->tensor());
8295 rmi.instructions.push_back(std::move(pgai));
8302 struct var_set : std::map<std::string,size_type> {
8304 size_type operator[](
const std::string &name) {
8307 auto it = find(name);
8314 std::string operator[](
const size_type &
id)
const {
8315 for (
const auto &key_value : *
this)
8316 if (key_value.second ==
id)
8317 return key_value.first;
8318 return std::string(
"");
8323 struct condensation_description {
8324 var_set Ivars, Jvars, Qvars;
8327 std::vector<std::set<size_type>> Qclusters, Jclusters;
8333 std::vector<size_type> cluster_of_Qvar;
8335 gmm::dense_matrix<base_tensor *> KQQ,
8339 std::vector<base_tensor *> RI,
8343 void ga_compile(ga_workspace &workspace,
8344 ga_instruction_set &gis,
size_type order,
bool condensation) {
8345 gis.transformations.clear();
8346 gis.all_instructions.clear();
8347 gis.unreduced_terms.clear();
8348 workspace.clear_temporary_variable_intervals();
8350 std::map<const ga_instruction_set::region_mim, condensation_description>
8353 if (condensation && order == 2) {
8354 for (
size_type i = 0; i < workspace.nb_trees(); ++i) {
8355 const ga_workspace::tree_description &td = workspace.tree_info(i);
8356 if (td.order != 2 && td.order !=
size_type(-1))
8358 ga_tree tree(*(td.ptree));
8359 ga_semantic_analysis(tree, workspace, td.mim->linked_mesh(),
8360 ref_elt_dim_of_mesh(td.mim->linked_mesh(),*(td.rg)),
8362 pga_tree_node root = tree.root;
8365 v1_is_intern = workspace.is_internal_variable(root->name_test1),
8366 v2_is_intern = workspace.is_internal_variable(root->name_test2);
8367 if (v1_is_intern || v2_is_intern) {
8368 GMM_ASSERT1(tree.secondary_domain.empty(),
8369 "Condensed variable cannot be used in secondary domain");
8371 for (
const auto &key_val : condensations) {
8372 const ga_instruction_set::region_mim rm0 = key_val.first;
8373 const condensation_description &CC0 = key_val.second;
8374 if (rm0.mim() == td.mim && rm0.region() != td.rg
8375 && (CC0.Qvars.count(root->name_test1) ||
8376 CC0.Qvars.count(root->name_test2))) {
8378 (*(rm0.region()), *(td.rg));
8379 GMM_ASSERT1(intrsct.is_empty(),
8380 "Cannot condense coupled variables between "
8381 "intersecting regions");
8384 const ga_instruction_set::region_mim rm(td.mim, td.rg,
nullptr);
8386 condensation_description &CC = condensations[rm];
8388 q1 = v1_is_intern ? CC.Qvars[root->name_test1] :
size_type(-1),
8389 q2 = v2_is_intern ? CC.Qvars[root->name_test2] :
size_type(-1);
8391 std::vector<size_type> selected_clusters;
8392 for (
size_type j=0; j < CC.Qclusters.size(); ++j)
8393 if (CC.Qclusters[j].count(q1) || CC.Qclusters[j].count(q2))
8394 selected_clusters.push_back(j);
8396 if (selected_clusters.empty()) {
8397 CC.Qclusters.push_back(std::set<size_type>());
8398 if (q1 !=
size_type(-1)) CC.Qclusters.back().insert(q1);
8399 if (q2 !=
size_type(-1)) CC.Qclusters.back().insert(q2);
8401 auto &target = CC.Qclusters[selected_clusters[0]];
8402 if (q1 !=
size_type(-1)) target.insert(q1);
8403 if (q2 !=
size_type(-1)) target.insert(q2);
8404 for (
size_type j=selected_clusters.size()-1; j > 1; --j) {
8405 auto &source = CC.Qclusters[selected_clusters[j]];
8406 target.insert(source.begin(), source.end());
8407 CC.Qclusters.erase(CC.Qclusters.begin() + selected_clusters[j]);
8414 for (
auto &key_value : condensations) {
8415 condensation_description &CC = key_value.second;
8424 CC.Jclusters.resize(CC.Qclusters.size());
8426 CC.cluster_of_Qvar.resize(Qsize);
8427 for (
size_type i=0; i < CC.Qclusters.size(); ++i)
8428 for (
const size_type &var : CC.Qclusters[i])
8429 CC.cluster_of_Qvar[var] = i;
8434 CC.KQQ.resize(Qsize, Qsize);
8435 CC.RQpr.resize(Qsize);
8437 bgeot::multi_index mi(1);
8438 mi[0] = workspace.associated_im_data(CC.Qvars[q]) ->nb_tensor_elem();
8439 gis.condensation_tensors.push_back
8440 (std::make_shared<base_tensor>(mi));
8441 CC.RQpr[q] = gis.condensation_tensors.back().get();
8446 std::array<ga_workspace::operation_type,3>
8447 phases{ga_workspace::PRE_ASSIGNMENT,
8448 ga_workspace::ASSEMBLY,
8449 ga_workspace::POST_ASSIGNMENT};
8450 for (
const auto &phase : phases) {
8452 for (
size_type i = 0; i < workspace.nb_trees(); ++i) {
8453 const ga_workspace::tree_description &td = workspace.tree_info(i);
8454 if (td.operation != phase)
8457 if (td.order == order || td.order ==
size_type(-1)) {
8458 std::list<ga_tree> &trees = (phase == ga_workspace::ASSEMBLY)
8460 : gis.interpolation_trees;
8461 trees.push_back(*(td.ptree));
8463 ga_semantic_analysis(trees.back(), workspace, td.mim->linked_mesh(),
8464 ref_elt_dim_of_mesh(td.mim->linked_mesh(),*(td.rg)),
8466 pga_tree_node root = trees.back().root;
8471 psecondary_domain psd(0);
8472 if (trees.back().secondary_domain.size())
8473 psd = workspace.secondary_domain(trees.back().secondary_domain);
8474 ga_instruction_set::region_mim rm(td.mim, td.rg, psd);
8475 auto &rmi = gis.all_instructions[rm];
8479 ga_compile_interpolate_trans(root, workspace, gis, rmi, *(td.m));
8480 ga_compile_node(root, workspace, gis, rmi, *(td.m),
false,
8481 rmi.current_hierarchy);
8485 if (phase != ga_workspace::ASSEMBLY) {
8486 if (!td.varname_interpolation.empty()) {
8488 = workspace.associated_im_data(td.varname_interpolation);
8489 auto &V =
const_cast<model_real_plain_vector &
>
8490 (workspace.value(td.varname_interpolation));
8491 GMM_ASSERT1(imd,
"Internal error");
8492 auto pgai = std::make_shared<ga_instruction_assignment>
8493 (root->tensor(), V, gis.ctx, imd);
8494 rmi.instructions.push_back(std::move(pgai));
8497 pga_instruction pgai;
8500 workspace.assembled_tensor() = root->tensor();
8501 pgai = std::make_shared<ga_instruction_add_to_coeff>
8502 (workspace.assembled_tensor(), root->tensor(), gis.coeff);
8506 GMM_ASSERT1(root->tensor_proper_size() == 1,
8507 "Invalid vector or tensor quantity. An order 1 "
8508 "weak form has to be a scalar quantity");
8509 const mesh_fem *
const
8510 mf = workspace.associated_mf(root->name_test1);
8511 const im_data *
const
8512 imd = workspace.associated_im_data(root->name_test1);
8513 workspace.add_temporary_interval_for_unreduced_variable
8516 base_vector &Vu = workspace.unreduced_vector(),
8517 &Vr = workspace.assembled_vector();
8519 const std::string &intn1 = root->interpolate_name_test1;
8520 bool secondary = !intn1.empty() &&
8521 workspace.secondary_domain_exists(intn1);
8522 fem_interpolation_context
8523 &ctx = intn1.empty() ? gis.ctx
8524 : (secondary ? rmi.secondary_domain_infos.ctx
8525 : rmi.interpolate_infos[intn1].ctx);
8527 !(intn1.empty() || intn1 ==
"neighbor_element"
8528 || intn1 ==
"neighbour_elt" || secondary);
8530 if (intn1.size() && !secondary &&
8531 workspace.variable_group_exists(root->name_test1)) {
8532 ga_instruction_set::variable_group_info
8533 &vgi = rmi.interpolate_infos[intn1]
8534 .groups_info[root->name_test1];
8535 pgai = std::make_shared<ga_instruction_vector_assembly_mf>
8536 (root->tensor(), Vr, Vu, ctx,
8537 vgi.I, vgi.mf, vgi.reduced_mf,
8538 gis.coeff, gis.nbpt, gis.ipt, interpolate);
8539 for (
const std::string &name
8540 : workspace.variable_group(root->name_test1))
8541 gis.unreduced_terms.emplace(name,
"");
8543 base_vector &V = mf->is_reduced() ? Vu : Vr;
8544 const gmm::sub_interval
8545 &I = mf->is_reduced()
8546 ? workspace.temporary_interval_of_variable
8548 : workspace.interval_of_variable(root->name_test1);
8549 pgai = std::make_shared<ga_instruction_vector_assembly_mf>
8550 (root->tensor(), V, ctx, I, *mf,
8551 gis.coeff, gis.nbpt, gis.ipt, interpolate);
8552 if (mf->is_reduced())
8553 gis.unreduced_terms.emplace(root->name_test1,
"");
8556 GMM_ASSERT1(root->interpolate_name_test1.size() == 0,
8557 "Interpolate transformation on integration "
8559 if (!workspace.is_internal_variable(root->name_test1) ||
8561 pgai = std::make_shared<ga_instruction_vector_assembly_imd>
8562 (root->tensor(), Vr, gis.ctx,
8563 workspace.interval_of_variable(root->name_test1),
8564 *imd, gis.coeff, gis.ipt);
8567 pgai = std::make_shared<ga_instruction_vector_assembly>
8568 (root->tensor(), Vr,
8569 workspace.interval_of_variable(root->name_test1),
8575 GMM_ASSERT1(root->tensor_proper_size() == 1,
8576 "Invalid vector or tensor quantity. An order 2 "
8577 "weak form has to be a scalar quantity");
8578 const mesh_fem *mf1=workspace.associated_mf(root->name_test1),
8579 *mf2=workspace.associated_mf(root->name_test2);
8581 *imd1 = workspace.associated_im_data(root->name_test1),
8582 *imd2 = workspace.associated_im_data(root->name_test2);
8583 const std::string &intn1 = root->interpolate_name_test1,
8584 &intn2 = root->interpolate_name_test2;
8585 bool secondary1 = intn1.size() &&
8586 workspace.secondary_domain_exists(intn1);
8587 bool secondary2 = intn2.size() &&
8588 workspace.secondary_domain_exists(intn2);
8589 fem_interpolation_context
8590 &ctx1 = intn1.empty() ? gis.ctx
8591 : (secondary1 ? rmi.secondary_domain_infos.ctx
8592 : rmi.interpolate_infos[intn1].ctx),
8593 &ctx2 = intn2.empty() ? gis.ctx
8594 : (secondary2 ? rmi.secondary_domain_infos.ctx
8595 : rmi.interpolate_infos[intn2].ctx);
8596 bool interpolate = !(intn1.empty() || intn1 ==
"neighbor_element"
8597 || intn1 ==
"neighbour_elt"
8599 !(intn2.empty() || intn2 ==
"neighbor_element"
8600 || intn2 ==
"neighbour_elt"
8603 workspace.add_temporary_interval_for_unreduced_variable
8605 workspace.add_temporary_interval_for_unreduced_variable
8608 bool has_var_group1 = (!intn1.empty() && !secondary1 &&
8609 workspace.variable_group_exists
8610 (root->name_test1));
8611 bool has_var_group2 = (!intn2.empty() && !secondary2 &&
8612 workspace.variable_group_exists
8613 (root->name_test2));
8614 bool simple = !interpolate &&
8615 !has_var_group1 && !has_var_group2 &&
8616 mf1 && !(mf1->is_reduced()) &&
8617 mf2 && !(mf2->is_reduced());
8620 auto &Krr = workspace.assembled_matrix();
8621 auto &Kru = workspace.col_unreduced_matrix();
8622 auto &Kur = workspace.row_unreduced_matrix();
8623 auto &Kuu = workspace.row_col_unreduced_matrix();
8626 const gmm::sub_interval
8627 &I1 = workspace.interval_of_variable(root->name_test1),
8628 &I2 = workspace.interval_of_variable(root->name_test2);
8630 &alpha1 = workspace.factor_of_variable(root->name_test1),
8631 &alpha2 = workspace.factor_of_variable(root->name_test2);
8632 if (mf1->get_qdim() == 1 && mf2->get_qdim() == 1)
8633 pgai = std::make_shared
8634 <ga_instruction_matrix_assembly_standard_scalar>
8635 (root->tensor(), Krr, ctx1, ctx2, I1, I2, mf1, mf2,
8636 alpha1, alpha2, gis.coeff, gis.nbpt, gis.ipt);
8637 else if (root->sparsity() == 10 && root->t.qdim() == 2)
8638 pgai = std::make_shared
8639 <ga_instruction_matrix_assembly_standard_vector_opt10<2>>
8640 (root->tensor(), Krr, ctx1, ctx2, I1, I2, mf1, mf2,
8641 alpha1, alpha2, gis.coeff, gis.nbpt, gis.ipt);
8642 else if (root->sparsity() == 10 && root->t.qdim() == 3)
8643 pgai = std::make_shared
8644 <ga_instruction_matrix_assembly_standard_vector_opt10<3>>
8645 (root->tensor(), Krr, ctx1, ctx2, I1, I2, mf1, mf2,
8646 alpha1, alpha2, gis.coeff, gis.nbpt, gis.ipt);
8648 pgai = std::make_shared
8649 <ga_instruction_matrix_assembly_standard_vector>
8650 (root->tensor(), Krr, ctx1, ctx2, I1, I2, mf1, mf2,
8651 alpha1, alpha2, gis.coeff, gis.nbpt, gis.ipt);
8652 }
else if (condensation &&
8653 workspace.is_internal_variable(root->name_test1) &&
8654 workspace.is_internal_variable(root->name_test2)) {
8658 GMM_ASSERT1(imd1 && imd2,
"Internal error");
8659 GMM_ASSERT1(!interpolate,
"Internal error");
8663 condensation_description &CC = condensations[rm];
8664 GMM_ASSERT1(CC.Qvars.count(root->name_test1) > 0 &&
8665 CC.Qvars.count(root->name_test2) > 0,
8667 size_type q1 = CC.Qvars[root->name_test1],
8668 q2 = CC.Qvars[root->name_test2];
8669 if (!CC.KQQ(q1,q2)) {
8671 gis.condensation_tensors.push_back
8672 (std::make_shared<base_tensor>(s1,s2));
8673 CC.KQQ(q1,q2) = gis.condensation_tensors.back().get();
8674 pgai = std::make_shared<ga_instruction_copy_vect>
8675 (CC.KQQ(q1,q2)->as_vector(), root->tensor().as_vector());
8678 pgai = std::make_shared<ga_instruction_add_to>
8679 (*CC.KQQ(q1,q2), root->tensor());
8681 rmi.instructions.push_back(std::move(pgai));
8682 }
else if (condensation &&
8683 workspace.is_internal_variable(root->name_test1)) {
8687 GMM_ASSERT1(imd1,
"Internal error");
8688 GMM_ASSERT1(!interpolate,
"Internal error");
8691 condensation_description &CC = condensations[rm];
8692 GMM_ASSERT1(CC.Qvars.count(root->name_test1),
8694 size_type q1 = CC.Qvars[root->name_test1],
8695 j2 = CC.Jvars[root->name_test2];
8696 CC.Jclusters[CC.cluster_of_Qvar[q1]].insert(j2);
8697 if (q1 >= CC.KQJ.nrows() || j2 >= CC.KQJ.ncols())
8698 CC.KQJ.resize(std::max(CC.KQJ.nrows(), q1+1),
8699 std::max(CC.KQJ.ncols(), j2+1));
8700 if (!CC.KQJ(q1,j2)) {
8704 gis.condensation_tensors.push_back
8705 (std::make_shared<base_tensor>(root->tensor()));
8706 GMM_ASSERT1(root->tensor().size(0) == s1,
"Internal error");
8707 CC.KQJ(q1,j2) = gis.condensation_tensors.back().get();
8708 if (mf2 && !mf2->is_uniform())
8709 rmi.instructions.push_back(std::make_shared
8710 <ga_instruction_second_ind_tensor>
8711 (*CC.KQJ(q1,j2), ctx2, root->qdim2, mf2,
nullptr));
8712 pgai = std::make_shared<ga_instruction_copy_vect>
8713 (CC.KQJ(q1,j2)->as_vector(), root->tensor().as_vector());
8717 pgai = std::make_shared<ga_instruction_add_to>
8718 (*CC.KQJ(q1,j2), root->tensor());
8720 rmi.instructions.push_back(std::move(pgai));
8721 }
else if (condensation &&
8722 workspace.is_internal_variable(root->name_test2)) {
8726 GMM_ASSERT1(imd2,
"Internal error");
8727 GMM_ASSERT1(!interpolate,
"Internal error");
8730 condensation_description &CC = condensations[rm];
8731 GMM_ASSERT1(CC.Qvars.count(root->name_test2),
8733 size_type i1 = CC.Ivars[root->name_test1],
8734 q2 = CC.Qvars[root->name_test2];
8735 if (i1 >= CC.KIQ.nrows() || q2 >= CC.KIQ.ncols())
8736 CC.KIQ.resize(std::max(CC.KIQ.nrows(), i1+1),
8737 std::max(CC.KIQ.ncols(), q2+1));
8738 if (!CC.KIQ(i1,q2)) {
8742 gis.condensation_tensors.push_back
8743 (std::make_shared<base_tensor>(root->tensor()));
8744 GMM_ASSERT1(root->tensor().size(1) == s2,
8746 CC.KIQ(i1,q2) = gis.condensation_tensors.back().get();
8747 if (mf1 && !mf1->is_uniform())
8748 rmi.instructions.push_back(std::make_shared
8749 <ga_instruction_first_ind_tensor>
8750 (*CC.KIQ(i1,q2), ctx1, root->qdim1, mf1,
nullptr));
8751 pgai = std::make_shared<ga_instruction_copy_vect>
8752 (CC.KIQ(i1,q2)->as_vector(), root->tensor().as_vector());
8756 pgai = std::make_shared<ga_instruction_add_to>
8757 (*CC.KIQ(i1,q2), root->tensor());
8759 rmi.instructions.push_back(std::move(pgai));
8760 }
else if (!workspace.is_internal_variable(root->name_test1) &&
8761 !workspace.is_internal_variable(root->name_test2)) {
8763 if ((mf1 && mf1->is_reduced()) || (mf2 && mf2->is_reduced())
8764 || has_var_group1 || has_var_group2)
8765 gis.unreduced_terms.emplace(root->name_test1,
8768 auto &Kxu = (mf1 && mf1->is_reduced()) ? Kuu : Kru;
8769 auto &Kxr = (mf1 && mf1->is_reduced()) ? Kur : Krr;
8770 auto &Kux = (mf2 && mf2->is_reduced()) ? Kuu : Kur;
8771 auto &Krx = (mf2 && mf2->is_reduced()) ? Kru : Krr;
8772 auto &Kxx = (mf2 && mf2->is_reduced()) ? Kxu : Kxr;
8775 &alpha1 = workspace.factor_of_variable(root->name_test1),
8776 &alpha2 = workspace.factor_of_variable(root->name_test2);
8778 if (has_var_group1) {
8779 ga_instruction_set::variable_group_info
8780 &vgi1 = rmi.interpolate_infos[intn1]
8781 .groups_info[root->name_test1];
8782 if (has_var_group2) {
8783 ga_instruction_set::variable_group_info
8784 &vgi2 = rmi.interpolate_infos[intn2]
8785 .groups_info[root->name_test2];
8786 pgai = std::make_shared
8787 <ga_instruction_matrix_assembly_mf_mf>
8788 (root->tensor(), Krr, Kru, Kur, Kuu, ctx1, ctx2,
8790 gis.coeff, gis.nbpt, gis.ipt, interpolate);
8792 const gmm::sub_interval &I2 = mf2 && mf2->is_reduced()
8793 ? workspace.temporary_interval_of_variable
8795 : workspace.interval_of_variable(root->name_test2);
8797 pgai = std::make_shared
8798 <ga_instruction_matrix_assembly_mf_mf>
8799 (root->tensor(), Krx, Kux, ctx1, ctx2,
8800 vgi1, I2, *mf2, alpha2,
8801 gis.coeff, gis.nbpt, gis.ipt, interpolate);
8803 pgai = std::make_shared
8804 <ga_instruction_matrix_assembly_mf_imd>
8805 (root->tensor(), Krr, Kur, ctx1, ctx2,
8806 vgi1, I2, imd2, alpha2, gis.coeff, gis.ipt);
8809 const gmm::sub_interval &I1 = mf1 && mf1->is_reduced()
8810 ? workspace.temporary_interval_of_variable
8812 : workspace.interval_of_variable(root->name_test1);
8813 if (has_var_group2) {
8814 ga_instruction_set::variable_group_info
8815 &vgi2 = rmi.interpolate_infos[intn2]
8816 .groups_info[root->name_test2];
8818 pgai = std::make_shared
8819 <ga_instruction_matrix_assembly_mf_mf>
8820 (root->tensor(), Kxr, Kxu, ctx1, ctx2,
8821 I1, *mf1, alpha1, vgi2,
8822 gis.coeff, gis.nbpt, gis.ipt, interpolate);
8824 pgai = std::make_shared
8825 <ga_instruction_matrix_assembly_imd_mf>
8826 (root->tensor(), Krr, Kru, ctx1, ctx2,
8827 I1, imd1, alpha1, vgi2, gis.coeff, gis.ipt);
8829 const gmm::sub_interval &I2 = mf2 && mf2->is_reduced()
8830 ? workspace.temporary_interval_of_variable
8832 : workspace.interval_of_variable(root->name_test2);
8834 pgai = std::make_shared
8835 <ga_instruction_matrix_assembly_mf_mf>
8836 (root->tensor(), Kxx, ctx1, ctx2,
8837 I1, *mf1, alpha1, I2, *mf2, alpha2,
8838 gis.coeff, gis.nbpt, gis.ipt, interpolate);
8840 pgai = std::make_shared
8841 <ga_instruction_matrix_assembly_mf_imd>
8842 (root->tensor(), Kxr, ctx1, ctx2,
8843 I1, *mf1, alpha1, I2, imd2, alpha2,
8844 gis.coeff, gis.ipt);
8846 pgai = std::make_shared
8847 <ga_instruction_matrix_assembly_imd_mf>
8848 (root->tensor(), Krx, ctx1, ctx2,
8849 I1, imd1, alpha1, I2, *mf2, alpha2,
8850 gis.coeff, gis.ipt);
8852 pgai = std::make_shared
8853 <ga_instruction_matrix_assembly_imd_imd>
8854 (root->tensor(), Krr, ctx1, ctx2,
8855 I1, imd1, alpha1, I2, imd2, alpha2,
8856 gis.coeff, gis.ipt);
8864 rmi.instructions.push_back(std::move(pgai));
8870 if (condensation && order == 2 && phase == ga_workspace::ASSEMBLY) {
8872 auto &Krr = workspace.assembled_matrix();
8873 auto &Kru = workspace.col_unreduced_matrix();
8874 auto &Kur = workspace.row_unreduced_matrix();
8875 auto &Kuu = workspace.row_col_unreduced_matrix();
8877 for (
auto &&key_val : condensations) {
8878 const ga_instruction_set::region_mim rm = key_val.first;
8879 condensation_description &CC = key_val.second;
8880 auto &rmi = gis.all_instructions[rm];
8882 CC.KQJpr.resize(CC.KQJ.nrows(), CC.KQJ.ncols());
8883 for (
size_type k=0; k < CC.KQJpr.size(); ++k) {
8884 gis.condensation_tensors.push_back
8885 (std::make_shared<base_tensor>(2,2));
8886 CC.KQJpr[k] = gis.condensation_tensors.back().get();
8889 pga_instruction pgai;
8892 for (
size_type k=0; k < CC.Qclusters.size(); ++k) {
8896 std::string name_test1 = CC.Qvars[q1];
8897 const im_data *imd1 = workspace.associated_im_data(name_test1);
8898 const gmm::sub_interval
8899 &I1 = workspace.interval_of_variable(name_test1);
8901 std::make_shared<ga_instruction_extract_residual_on_imd_dofs>
8902 (*(CC.RQpr[q1]), workspace.cached_vector(),
8903 gis.ctx, I1, *imd1, gis.ipt);
8904 rmi.instructions.push_back(std::move(pgai));
8910 pgai = std::make_shared<ga_instruction_condensation_sub>
8911 (CC.KQJpr, CC.RQpr, CC.KQQ, CC.KQJ, CC.Qclusters[k], gis.coeff);
8912 rmi.instructions.push_back(std::move(pgai));
8917 std::string name_test1 = CC.Qvars[q1];
8918 const im_data *imd1 = workspace.associated_im_data(name_test1);
8921 const gmm::sub_interval
8922 &I1 = workspace.interval_of_variable(name_test1);
8923 GMM_ASSERT1(imd1,
"Internal error");
8925 std::string name_test2 = CC.Jvars[j2];
8926 const mesh_fem *mf2 = workspace.associated_mf(name_test2);
8927 const im_data *imd2 = workspace.associated_im_data(name_test2);
8934 const gmm::sub_interval
8935 &I2 = mf2 && mf2->is_reduced()
8936 ? workspace.temporary_interval_of_variable(name_test2)
8937 : workspace.interval_of_variable(name_test2);
8938 const base_tensor &Kq1j2pr = *(CC.KQJpr(q1,j2));
8939 model_real_sparse_matrix
8940 &KQJpr = mf2 && mf2->is_reduced()
8941 ? workspace.col_unreduced_matrix()
8942 : workspace.internal_coupling_matrix();
8945 std::make_shared<ga_instruction_matrix_assembly_imd_mf>
8946 (Kq1j2pr, KQJpr, gis.ctx, gis.ctx,
8947 I1, imd1, gis.ONE, I2, *mf2, gis.ONE, gis.ONE, gis.ipt);
8949 if (mf2->is_reduced())
8950 gis.unreduced_terms.emplace(name_test1, name_test2);
8953 std::make_shared<ga_instruction_matrix_assembly_imd_imd>
8954 (Kq1j2pr, KQJpr, gis.ctx, gis.ctx,
8955 I1, imd1, gis.ONE, I2, imd2, gis.ONE, gis.ONE, gis.ipt);
8956 rmi.instructions.push_back(std::move(pgai));
8958 const bool initialize =
true;
8959 pgai = std::make_shared<ga_instruction_vector_assembly_imd>
8960 (*(CC.RQpr[q1]), workspace.assembled_vector(),
8961 gis.ctx, I1, *imd1, gis.ONE, gis.ipt, initialize);
8962 rmi.instructions.push_back(std::move(pgai));
8967 for (
size_type i1=0; i1 < CC.Ivars.size(); ++i1) {
8969 std::string name_test1 = CC.Ivars[i1];
8970 const mesh_fem *mf1 = workspace.associated_mf(name_test1);
8971 const im_data *imd1 = workspace.associated_im_data(name_test1);
8973 &alpha1 = workspace.factor_of_variable(name_test1);
8974 const gmm::sub_interval
8975 &I1 = mf1 && mf1->is_reduced()
8976 ? workspace.temporary_interval_of_variable(name_test1)
8977 : workspace.interval_of_variable(name_test1);
8981 std::vector<std::set<size_type>> Q_of_J(CC.Jvars.size());
8982 for (
size_type q=0; q < CC.Qvars.size(); ++q)
8986 Q_of_J[j].insert(q);
8989 for (
size_type j2=0; j2 < CC.Jvars.size(); ++j2) {
8990 if (Q_of_J[j2].size()) {
8991 std::vector<base_tensor *> Ki1Q, KQj2;
8993 Ki1Q.push_back(CC.KIQ(i1,q));
8994 KQj2.push_back(CC.KQJpr(q,j2));
8997 gis.condensation_tensors.push_back
8998 (std::make_shared<base_tensor>());
8999 base_tensor &Kij = *gis.condensation_tensors.back();
9000 pgai = std::make_shared<ga_instruction_condensation_super_K>
9002 rmi.instructions.push_back(std::move(pgai));
9004 std::string name_test2 = CC.Jvars[j2];
9005 const mesh_fem *mf2 = workspace.associated_mf(name_test2);
9006 const im_data *imd2 = workspace.associated_im_data(name_test2);
9011 &alpha2 = workspace.factor_of_variable(name_test2);
9012 const gmm::sub_interval
9013 &I2 = mf2 && mf2->is_reduced()
9014 ? workspace.temporary_interval_of_variable(name_test2)
9015 : workspace.interval_of_variable(name_test2);
9017 auto &Kxu = (mf1 && mf1->is_reduced()) ? Kuu : Kru;
9018 auto &Kxr = (mf1 && mf1->is_reduced()) ? Kur : Krr;
9019 auto &Krx = (mf2 && mf2->is_reduced()) ? Kru : Krr;
9020 auto &Kxx = (mf2 && mf2->is_reduced()) ? Kxu : Kxr;
9022 if ((mf1 && mf1->is_reduced()) || (mf2 && mf2->is_reduced()))
9023 gis.unreduced_terms.emplace(name_test1, name_test2);
9026 pgai = std::make_shared
9027 <ga_instruction_matrix_assembly_mf_mf>
9028 (Kij, Kxx, gis.ctx, gis.ctx,
9029 I1, *mf1, alpha1, I2, *mf2, alpha2,
9030 gis.coeff, gis.nbpt, gis.ipt,
false);
9032 pgai = std::make_shared
9033 <ga_instruction_matrix_assembly_mf_imd>
9034 (Kij, Kxr, gis.ctx, gis.ctx,
9035 I1, *mf1, alpha1, I2, imd2, alpha2,
9036 gis.coeff, gis.ipt);
9038 pgai = std::make_shared
9039 <ga_instruction_matrix_assembly_imd_mf>
9040 (Kij, Krx, gis.ctx, gis.ctx,
9041 I1, imd1, alpha1, I2, *mf2, alpha2,
9042 gis.coeff, gis.ipt);
9044 pgai = std::make_shared
9045 <ga_instruction_matrix_assembly_imd_imd>
9046 (Kij, Krr, gis.ctx, gis.ctx,
9047 I1, imd1, alpha1, I2, imd2, alpha2,
9048 gis.coeff, gis.ipt);
9049 rmi.instructions.push_back(std::move(pgai));
9054 std::vector<base_tensor *> Ki1Q, RQpr;
9055 for (
size_type q=0; q < CC.Qvars.size(); ++q)
9057 Ki1Q.push_back(CC.KIQ(i1,q));
9058 RQpr.push_back(CC.RQpr[q]);
9060 gis.condensation_tensors.push_back
9061 (std::make_shared<base_tensor>());
9062 base_tensor &Ri = *gis.condensation_tensors.back();
9063 pgai = std::make_shared<ga_instruction_condensation_super_R>
9065 rmi.instructions.push_back(std::move(pgai));
9067 base_vector &R = mf1->is_reduced() ? workspace.unreduced_vector()
9068 : workspace.assembled_vector();
9070 pgai = std::make_shared<ga_instruction_vector_assembly_mf>
9071 (Ri, R, gis.ctx, I1, *mf1, gis.coeff, gis.nbpt, gis.ipt,
false);
9073 pgai = std::make_shared<ga_instruction_vector_assembly_imd>
9074 (Ri, R, gis.ctx, I1, *imd1, gis.coeff, gis.ipt);
9076 pgai = std::make_shared<ga_instruction_vector_assembly>
9077 (Ri, R, I1, gis.coeff);
9078 rmi.instructions.push_back(std::move(pgai));
9093 void ga_function_exec(ga_instruction_set &gis) {
9095 for (
auto &&instr : gis.all_instructions) {
9096 const auto &gil = instr.second.instructions;
9097 for (
size_type j = 0; j < gil.size(); ++j) j += gil[j]->exec();
9101 void ga_interpolation_exec(ga_instruction_set &gis,
9102 ga_workspace &workspace,
9103 ga_interpolation_context &gic) {
9105 base_small_vector un, up;
9107 for (
const std::string &t : gis.transformations)
9108 workspace.interpolate_transformation(t)->init(workspace);
9110 for (
auto &&instr : gis.all_instructions) {
9113 const mesh_region ®ion = *(instr.first.region());
9115 GMM_ASSERT1(&m == &(gic.linked_mesh()),
9116 "Incompatibility of meshes in interpolation");
9117 const auto &gilb = instr.second.begin_instructions;
9118 const auto &gile = instr.second.elt_instructions;
9119 const auto &gil = instr.second.instructions;
9122 std::vector<size_type> ind;
9123 auto pai_old = papprox_integration{};
9125 if (gic.use_mim()) {
9132 bgeot::pstored_point_tab pspt
9133 = gic.ppoints_for_element(v.cv(), v.f(), ind);
9135 if (pspt.get() && ind.size() && pspt->size()) {
9136 m.points_of_convex(v.cv(), G);
9138 up.resize(G.nrows());
9139 un.resize(pgt->dim());
9141 if (gis.ctx.have_pgp() && gis.ctx.pgt() == pgt && pai_old == gis.pai) {
9142 gis.ctx.change(gis.ctx.pgp(), 0, 0, G, v.cv(), v.f());
9144 if (!(gic.use_pgp(v.cv()))) {
9145 gis.ctx.change(pgt, 0, (*pspt)[0], G, v.cv(), v.f());
9147 gis.ctx.change(gis.gp_pool(pgt, pspt), 0, 0, G, v.cv(), v.f());
9152 if (gis.need_elt_size)
9156 gis.nbpt = pspt->size();
9157 for (
size_type ii = 0; ii < ind.size(); ++ii) {
9159 if (gis.ctx.have_pgp()) gis.ctx.set_ii(ind[ii]);
9160 else gis.ctx.set_xref((*pspt)[gis.ipt]);
9162 if (ii == 0 || !(pgt->is_linear())) {
9165 const base_matrix& B = gis.ctx.B();
9169 gmm::scale(up,1.0/nup);
9175 gmm::clear(workspace.assembled_tensor().as_vector());
9177 for (
size_type j = 0; j < gilb.size(); ++j) j += gilb[j]->exec();
9178 for (
size_type j = 0; j < gile.size(); ++j) j += gile[j]->exec();
9180 for (
size_type j = 0; j < gil.size(); ++j) j += gil[j]->exec();
9181 gic.store_result(v.cv(), ind[ii], workspace.assembled_tensor());
9186 for (
const std::string &t : gis.transformations)
9187 workspace.interpolate_transformation(t)->finalize();
9192 void ga_exec(ga_instruction_set &gis, ga_workspace &workspace) {
9194 base_small_vector un;
9195 scalar_type J1(0), J2(0);
9197 for (
const std::string &t : gis.transformations)
9198 workspace.interpolate_transformation(t)->init(workspace);
9200 for (
auto &instr : gis.all_instructions) {
9202 psecondary_domain psd = instr.first.psd();
9204 GMM_ASSERT1(&m == &(mim.
linked_mesh()),
"Incompatibility of meshes");
9205 const auto &gilb = instr.second.begin_instructions;
9206 const auto &gile = instr.second.elt_instructions;
9207 const auto &gil = instr.second.instructions;
9221 const mesh_region ®ion = *(instr.first.region());
9226 pintegration_method pim = 0;
9227 papprox_integration pai = 0;
9228 bgeot::pstored_point_tab pspt = 0, old_pspt = 0;
9229 bgeot::pgeotrans_precomp pgp = 0;
9230 bool first_gp =
true;
9234 if (v.cv() != old_cv) {
9235 pgt = m.trans_of_convex(v.cv());
9237 m.points_of_convex(v.cv(), G1);
9239 if (pim->type() == IM_NONE)
continue;
9240 GMM_ASSERT1(pim->type() == IM_APPROX,
"Sorry, exact methods "
9241 "cannot be used in high level generic assembly");
9242 pai = pim->approx_method();
9243 pspt = pai->pintegration_points();
9245 if (pgp && gis.pai == pai && pgt_old == pgt) {
9246 gis.ctx.change(pgp, 0, 0, G1, v.cv(), v.f());
9248 if (pai->is_built_on_the_fly()) {
9249 gis.ctx.change(pgt, 0, (*pspt)[0], G1, v.cv(), v.f());
9252 pgp = gis.gp_pool(pgt, pspt);
9253 gis.ctx.change(pgp, 0, 0, G1, v.cv(), v.f());
9255 pgt_old = pgt; gis.pai = pai;
9257 if (gis.need_elt_size)
9262 if (pim->type() == IM_NONE)
continue;
9263 gis.ctx.set_face_num(v.f());
9265 if (pspt != old_pspt) { first_gp =
true; old_pspt = pspt; }
9270 gis.nbpt = pai->nb_points_on_face(v.f());
9271 first_ind = pai->ind_first_point_on_face(v.f());
9273 gis.nbpt = pai->nb_points_on_convex();
9275 for (gis.ipt = 0; gis.ipt < gis.nbpt; ++(gis.ipt)) {
9276 if (pgp) gis.ctx.set_ii(first_ind+gis.ipt);
9277 else gis.ctx.set_xref((*pspt)[first_ind+gis.ipt]);
9278 if (gis.ipt == 0 || !(pgt->is_linear())) {
9282 gis.Normal.resize(G1.nrows());
9283 un.resize(pgt->dim());
9288 gmm::scale(gis.Normal, 1.0/nup);
9293 auto ipt_coeff = pai->coeff(first_ind+gis.ipt);
9294 gis.coeff = J1 * ipt_coeff;
9295 bool enable_ipt = (gmm::abs(ipt_coeff) > 0.0 ||
9296 workspace.include_empty_int_points());
9297 if (!enable_ipt) gis.coeff = scalar_type(0);
9299 for (
size_type j=0; j < gilb.size(); ++j) j+=gilb[j]->exec();
9303 for (
size_type j=0; j < gile.size(); ++j) j+=gile[j]->exec();
9305 if (enable_ipt || gis.ipt == 0 || gis.ipt == gis.nbpt-1) {
9306 for (
size_type j=0; j < gil.size(); ++j) j+=gil[j]->exec();
9313 GA_DEBUG_INFO(
"-----------------------------");
9317 auto &sdi = instr.second.secondary_domain_infos;
9318 const mesh_region ®ion1 = *(instr.first.region());
9324 pintegration_method pim1 = 0, pim2 = 0;
9325 papprox_integration pai1 = 0, pai2 = 0;
9326 bgeot::pstored_point_tab pspt1=0, old_pspt1=0, pspt2=0, old_pspt2=0;
9327 bgeot::pgeotrans_precomp pgp1 = 0, pgp2 = 0;
9328 bool first_gp =
true;
9332 if (v1.cv() != old_cv1) {
9333 pgt1 = m.trans_of_convex(v1.cv());
9335 m.points_of_convex(v1.cv(), G1);
9337 if (pim1->type() == IM_NONE)
continue;
9338 GMM_ASSERT1(pim1->type() == IM_APPROX,
"Sorry, exact methods "
9339 "cannot be used in high level generic assembly");
9340 pai1 = pim1->approx_method();
9341 pspt1 = pai1->pintegration_points();
9342 if (pspt1->size()) {
9343 if (pgp1 && gis.pai == pai1 && pgt1_old == pgt1) {
9344 gis.ctx.change(pgp1, 0, 0, G1, v1.cv(), v1.f());
9346 if (pai1->is_built_on_the_fly()) {
9347 gis.ctx.change(pgt1, 0, (*pspt1)[0], G1, v1.cv(), v1.f());
9350 pgp1 = gis.gp_pool(pgt1, pspt1);
9351 gis.ctx.change(pgp1, 0, 0, G1, v1.cv(), v1.f());
9353 pgt1_old = pgt1; gis.pai = pai1;
9355 if (gis.need_elt_size)
9360 if (pim1->type() == IM_NONE)
continue;
9361 gis.ctx.set_face_num(v1.f());
9363 if (pspt1 != old_pspt1) { first_gp =
true; old_pspt1 = pspt1; }
9364 if (pspt1->size()) {
9368 nbpt1 = pai1->nb_points_on_face(v1.f());
9369 first_ind1 = pai1->ind_first_point_on_face(v1.f());
9371 nbpt1 = pai1->nb_points_on_convex();
9374 const mesh &m2 = psd->mim().linked_mesh();
9375 const mesh_region ®ion2 = psd->give_region(m, v1.cv(), v1.f());
9377 !v2.finished(); ++v2) {
9378 if (v2.cv() != old_cv2) {
9379 pgt2 = m2.trans_of_convex(v2.cv());
9380 pim2 = psd->mim().int_method_of_element(v2.cv());
9381 m2.points_of_convex(v2.cv(), G2);
9383 if (pim2->type() == IM_NONE)
continue;
9384 GMM_ASSERT1(pim2->type() == IM_APPROX,
"Sorry, exact methods "
9385 "cannot be used in high level generic assembly");
9386 pai2 = pim2->approx_method();
9387 pspt2 = pai2->pintegration_points();
9388 if (pspt2->size()) {
9389 if (pgp2 && sdi.pai == pai2 && pgt2_old == pgt2) {
9390 sdi.ctx.change(pgp2, 0, 0, G2, v2.cv(), v2.f());
9392 if (pai2->is_built_on_the_fly()) {
9393 sdi.ctx.change(pgt2, 0, (*pspt2)[0], G2,v2.cv(),v2.f());
9396 pgp2 = gis.gp_pool(pgt2, pspt2);
9397 sdi.ctx.change(pgp2, 0, 0, G2, v2.cv(), v2.f());
9399 pgt2_old = pgt2; sdi.pai = pai2;
9404 if (pim2->type() == IM_NONE)
continue;
9405 sdi.ctx.set_face_num(v2.f());
9407 if (pspt2 != old_pspt2) { first_gp =
true; old_pspt2 = pspt2; }
9408 if (pspt2->size()) {
9412 nbpt2 = pai2->nb_points_on_face(v2.f());
9413 first_ind2 = pai2->ind_first_point_on_face(v2.f());
9415 nbpt2 = gis.nbpt = pai2->nb_points_on_convex();
9417 gis.nbpt = nbpt1 * nbpt2;
9419 for (
size_type ipt1=0; ipt1 < nbpt1; ++ipt1) {
9420 for (
size_type ipt2=0; ipt2 < nbpt2; ++ipt2, ++(gis.ipt)) {
9422 if (pgp1) gis.ctx.set_ii(first_ind1+ipt1);
9423 else gis.ctx.set_xref((*pspt1)[first_ind1+ipt1]);
9424 if (pgp2) sdi.ctx.set_ii(first_ind2+ipt2);
9425 else sdi.ctx.set_xref((*pspt2)[first_ind2+ipt2]);
9427 if (gis.ipt == 0 || !(pgt1->is_linear())) {
9430 gis.Normal.resize(G1.nrows());
9431 un.resize(pgt1->dim());
9436 gmm::scale(gis.Normal, 1.0/nup);
9443 if (gis.ipt == 0 || !(pgt2->is_linear())) {
9446 sdi.Normal.resize(G2.nrows());
9447 un.resize(pgt2->dim());
9452 gmm::scale(sdi.Normal, 1.0/nup);
9459 auto ipt_coeff = pai1->coeff(first_ind1+ipt1)
9460 * pai2->coeff(first_ind2+ipt2);
9461 gis.coeff = J1 * J2 * ipt_coeff;
9462 bool enable_ipt = (gmm::abs(ipt_coeff) > 0.0 ||
9463 workspace.include_empty_int_points());
9464 if (!enable_ipt) gis.coeff = scalar_type(0);
9467 for (
size_type j=0; j < gilb.size(); ++j)
9472 for (
size_type j=0; j < gile.size(); ++j)
9475 if (enable_ipt || gis.ipt == 0 || gis.ipt == gis.nbpt-1) {
9476 for (
size_type j=0; j < gil.size(); ++j)
9487 GA_DEBUG_INFO(
"-----------------------------");
9492 for (
const std::string &t : gis.transformations)
9493 workspace.interpolate_transformation(t)->finalize();
does the inversion of the geometric transformation for a given convex
bool invert(const base_node &x, base_node &xref, scalar_type IN_EPS=1e-12, bool project_into_element=false)
given the node on the real element, returns the node on the reference element (even if it is outside ...
The object geotrans_precomp_pool Allow to allocate a certain number of geotrans_precomp and automatic...
static T & instance()
Instance from the current thread.
Describe an integration method linked to a mesh.
virtual pintegration_method int_method_of_element(size_type cv) const
return the integration method associated with an element (in no integration is associated,...
const mesh & linked_mesh() const
Give a reference to the linked mesh of type mesh.
const dal::bit_vector & convex_index(void) const
Get the set of convexes where an integration method has been assigned.
"iterator" class for regions.
static mesh_region intersection(const mesh_region &a, const mesh_region &b)
return the intersection of two mesh regions
Describe a mesh (collection of convexes (elements) and points).
virtual scalar_type convex_radius_estimate(size_type ic) const
Return an estimate of the convex largest dimension.
sparse vector built upon std::vector.
Semantic analysis of assembly trees and semantic manipulations.
Compilation and execution operations.
a subclass of mesh_im which is conformal to a number of level sets.
void copy(const L1 &l1, L2 &l2)
*/
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norm2(const V &v)
Euclidean norm of a vector.
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norminf(const V &v)
Infinity norm of a vector.
void clear(L &l)
clear (fill with zeros) a vector or matrix.
void resize(V &v, size_type n)
*/
void clean(L &l, double threshold)
Clean a vector or matrix (replace near-zero entries with zeroes).
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*/
void add(const L1 &l1, L2 &l2)
*/
scalar_type APIDECL convex_radius_estimate(bgeot::pgeometric_trans pgt, const base_matrix &pts)
rough estimate of the radius of the convex using the largest eigenvalue of the jacobian of the geomet...
std::shared_ptr< const getfem::virtual_fem > pfem
type of pointer on a fem description
gmm::uint16_type short_type
used as the common short type integer in the library
base_small_vector compute_unit_element_normal(const geotrans_interpolation_context &c)
Unit normal vector to an element of dimension N-1 in dimension N (a curve in 2D or a surface in 3D),...
base_small_vector compute_normal(const geotrans_interpolation_context &c, size_type face)
norm of returned vector is the ratio between the face surface on the real element and the face surfac...
size_t size_type
used as the common size type in the library
std::shared_ptr< const bgeot::geometric_trans > pgeometric_trans
pointer type for a geometric transformation
size_type alpha(short_type n, short_type d)
Return the value of which is the number of monomials of a polynomial of variables and degree .
GEneric Tool for Finite Element Methods.
void slice_vector_on_basic_dof_of_element(const mesh_fem &mf, const VEC1 &vec, size_type cv, VEC2 &coeff, size_type qmult1=size_type(-1), size_type qmult2=size_type(-1))
Given a mesh_fem.