39 #ifndef GETFEM_GENERIC_ASSEMBLY_H__
40 #define GETFEM_GENERIC_ASSEMBLY_H__
52 #define INFINITY std::numeric_limits<scalar_type>::infinity()
63 typedef std::vector<scalar_type> model_real_plain_vector;
64 typedef std::vector<complex_type> model_complex_plain_vector;
66 typedef gmm::col_matrix<model_real_sparse_vector> model_real_sparse_matrix;
67 typedef gmm::col_matrix<model_complex_sparse_vector>
68 model_complex_sparse_matrix;
70 typedef gmm::row_matrix<model_real_sparse_vector>
71 model_real_row_sparse_matrix;
72 typedef gmm::row_matrix<model_complex_sparse_vector>
73 model_complex_row_sparse_matrix;
78 int ga_check_name_validity(
const std::string &name);
84 struct var_trans_pair {
85 std::string varname, transname;
86 bool operator <(
const var_trans_pair &vt)
const {
87 return (varname < vt.varname) ||
88 (!(varname > vt.varname) && transname < vt.transname);
90 var_trans_pair() : varname(), transname() {}
91 var_trans_pair(
const std::string &v,
const std::string &t)
92 : varname(v), transname(t) {}
95 class APIDECL virtual_interpolate_transformation {
98 virtual void extract_variables
99 (
const ga_workspace &workspace, std::set<var_trans_pair> &vars,
100 bool ignore_data,
const mesh &m,
101 const std::string &interpolate_name)
const = 0;
102 virtual void init(
const ga_workspace &workspace)
const = 0;
103 virtual int transform
104 (
const ga_workspace &workspace,
const mesh &m,
105 fem_interpolation_context &ctx_x,
const base_small_vector &Normal,
107 base_node &P_ref, base_small_vector &N_y,
108 std::map<var_trans_pair, base_tensor> &derivatives,
109 bool compute_derivatives)
const = 0;
110 virtual void finalize()
const = 0;
111 virtual std::string expression()
const {
return std::string(); }
113 virtual ~virtual_interpolate_transformation() {}
116 typedef std::shared_ptr<const virtual_interpolate_transformation>
117 pinterpolate_transformation;
123 class APIDECL virtual_elementary_transformation {
127 virtual void give_transformation(
const mesh_fem &mf1,
const mesh_fem &mf2,
129 virtual ~virtual_elementary_transformation() {}
132 typedef std::shared_ptr<const virtual_elementary_transformation>
133 pelementary_transformation;
139 class APIDECL virtual_secondary_domain {
142 const mesh_region region;
146 const mesh_im &mim()
const {
return mim_; }
147 virtual const mesh_region &give_region(
const mesh &m,
152 virtual_secondary_domain(
const mesh_im &mim__,
const mesh_region ®ion_)
153 : mim_(mim__), region(region_) {}
154 virtual ~virtual_secondary_domain() {}
157 typedef std::shared_ptr<const virtual_secondary_domain> psecondary_domain;
167 std::string macro_name_;
172 ga_macro(
const std::string &name,
const ga_tree &t,
size_type nbp_);
173 ga_macro(
const ga_macro &);
175 ga_macro &operator =(
const ga_macro &);
177 const std::string &name()
const {
return macro_name_; }
178 std::string &name() {
return macro_name_; }
179 size_type nb_params()
const {
return nbp; }
181 const ga_tree& tree()
const {
return *ptree; }
182 ga_tree& tree() {
return *ptree; }
186 class ga_macro_dictionary {
189 const ga_macro_dictionary *parent;
190 std::map<std::string, ga_macro> macros;
193 bool macro_exists(
const std::string &name)
const;
194 const ga_macro &get_macro(
const std::string &name)
const;
196 void add_macro(
const ga_macro &gam);
197 void add_macro(
const std::string &name,
const std::string &expr);
198 void del_macro(
const std::string &name);
200 ga_macro_dictionary() : parent(0) {}
201 ga_macro_dictionary(
bool,
const ga_macro_dictionary& gamd)
211 struct ga_nonlinear_operator {
213 typedef std::vector<const base_tensor *> arg_list;
215 virtual bool result_size(
const arg_list &args,
216 bgeot::multi_index &sizes)
const = 0;
218 virtual void value(
const arg_list &args, base_tensor &result)
const = 0;
220 virtual void derivative(
const arg_list &args,
size_type i,
221 base_tensor &result)
const = 0;
223 virtual void second_derivative(
const arg_list &args,
size_type i,
224 size_type j, base_tensor &result)
const = 0;
226 virtual ~ga_nonlinear_operator() {}
229 struct ga_predef_operator_tab {
230 typedef std::map<std::string, std::shared_ptr<ga_nonlinear_operator>> T;
233 void add_method(
const std::string &name,
234 const std::shared_ptr<ga_nonlinear_operator> &pt)
236 ga_predef_operator_tab();
243 typedef scalar_type (*pscalar_func_onearg)(scalar_type);
244 typedef scalar_type (*pscalar_func_twoargs)(scalar_type, scalar_type);
246 void ga_define_function(
const std::string &name,
size_type nb_args,
247 const std::string &expr,
const std::string &der1=
"",
248 const std::string &der2=
"");
249 void ga_define_function(
const std::string &name, pscalar_func_onearg f,
250 const std::string &der1=
"");
251 void ga_define_function(
const std::string &name, pscalar_func_twoargs f2,
252 const std::string &der1=
"",
253 const std::string &der2=
"");
255 void ga_undefine_function(
const std::string &name);
256 bool ga_function_exists(
const std::string &name);
266 const ga_workspace *parent_workspace;
267 bool with_parent_variables;
268 size_type nb_prim_dof, nb_intern_dof, first_intern_dof, nb_tmp_dof;
272 struct var_description {
278 const model_real_plain_vector *V;
279 bgeot::multi_index qdims;
286 for (
size_type i = 0; i < qdims.size(); ++i) q *= qdims[i];
290 var_description(
bool is_var,
const mesh_fem *mf_,
const im_data *imd_,
291 gmm::sub_interval I_,
const model_real_plain_vector *V_,
293 : is_variable(is_var), mf(mf_), imd(imd_),
294 I(I_), V(V_), qdims(1), is_internal(is_intern_)
296 GMM_ASSERT1(Q > 0,
"Bad dimension");
303 enum operation_type {ASSEMBLY,
307 struct tree_description {
312 operation_type operation;
313 std::string varname_interpolation;
314 std::string name_test1, name_test2;
315 std::string interpolate_name_test1, interpolate_name_test2;
316 std::string secondary_domain;
319 const mesh_region *rg;
322 : operation(ASSEMBLY), varname_interpolation(
""),
323 name_test1(
""), name_test2(
""),
324 interpolate_name_test1(
""), interpolate_name_test2(
""),
325 mim(0), m(0), rg(0), ptree(0) {}
326 void copy(
const tree_description& td);
327 tree_description(
const tree_description& td) {
copy(td); }
328 tree_description &operator =(
const tree_description& td);
332 mutable std::set<var_trans_pair> test1, test2;
333 var_trans_pair selected_test1, selected_test2;
338 std::map<const mesh *, std::list<mesh_region> > registred_mesh_regions;
340 const mesh_region ®ister_region(
const mesh &m,
const mesh_region &rg);
343 typedef std::map<std::string, var_description> VAR_SET;
346 std::map<std::string, gmm::sub_interval> reenabled_var_intervals,
349 std::map<std::string, pinterpolate_transformation> transformations;
350 std::map<std::string, pelementary_transformation> elem_transformations;
351 std::map<std::string, psecondary_domain> secondary_domains;
353 std::vector<tree_description> trees;
355 std::map<std::string, std::vector<std::string> > variable_groups;
357 ga_macro_dictionary macro_dict;
361 void add_tree(ga_tree &tree,
const mesh &m,
const mesh_im &mim,
362 const mesh_region &rg,
363 const std::string &expr,
size_type add_derivative_order,
364 bool scalar_expr, operation_type op_type=ASSEMBLY,
365 const std::string varname_interpolation=
"");
367 std::shared_ptr<model_real_sparse_matrix> K, KQJpr;
368 std::shared_ptr<base_vector> V;
371 model_real_sparse_matrix col_unreduced_K,
374 base_vector unreduced_V, cached_V;
375 base_tensor assemb_t;
376 bool include_empty_int_pts =
false;
380 void set_assembled_matrix(model_real_sparse_matrix &K_) {
381 K = std::shared_ptr<model_real_sparse_matrix>
382 (std::shared_ptr<model_real_sparse_matrix>(), &K_);
384 void set_assembled_vector(base_vector &V_) {
385 V = std::shared_ptr<base_vector>
386 (std::shared_ptr<base_vector>(), &V_);
389 const model_real_sparse_matrix &assembled_matrix()
const {
return *K; }
390 model_real_sparse_matrix &assembled_matrix() {
return *K; }
391 const base_vector &assembled_vector()
const {
return *V; }
392 base_vector &assembled_vector() {
return *V; }
393 const base_vector &cached_vector()
const {
return cached_V; }
394 const base_tensor &assembled_tensor()
const {
return assemb_t; }
395 base_tensor &assembled_tensor() {
return assemb_t; }
396 const scalar_type &assembled_potential()
const {
397 GMM_ASSERT1(assemb_t.size() == 1,
"Bad result size");
400 scalar_type &assembled_potential() {
401 GMM_ASSERT1(assemb_t.size() == 1,
"Bad result size");
404 model_real_sparse_matrix &row_unreduced_matrix()
405 {
return row_unreduced_K; }
406 model_real_sparse_matrix &col_unreduced_matrix()
407 {
return col_unreduced_K; }
408 model_real_sparse_matrix &row_col_unreduced_matrix()
409 {
return row_col_unreduced_K; }
410 base_vector &unreduced_vector() {
return unreduced_V; }
412 void set_internal_coupling_matrix(model_real_sparse_matrix &KQJpr_) {
413 KQJpr = std::shared_ptr<model_real_sparse_matrix>
414 (std::shared_ptr<model_real_sparse_matrix>(), &KQJpr_);
419 const model_real_sparse_matrix &internal_coupling_matrix()
const
421 model_real_sparse_matrix &internal_coupling_matrix() {
return *KQJpr; }
427 size_type add_expression(
const std::string &expr,
const mesh_im &mim,
430 const std::string &secondary_dom =
"");
432 void add_function_expression(
const std::string &expr);
434 void add_interpolation_expression
435 (
const std::string &expr,
const mesh &m,
437 void add_interpolation_expression
438 (
const std::string &expr,
const mesh_im &mim,
440 void add_assignment_expression
441 (
const std::string &dataname,
const std::string &expr,
443 size_type order = 1,
bool before =
false);
446 void clear_expressions();
449 void print(std::ostream &str);
452 tree_description &tree_info(
size_type i);
455 void add_fem_variable(
const std::string &name,
const mesh_fem &mf,
456 const gmm::sub_interval &I,
457 const model_real_plain_vector &VV);
458 void add_im_variable(
const std::string &name,
const im_data &imd,
459 const gmm::sub_interval &I,
460 const model_real_plain_vector &VV);
461 void add_internal_im_variable(
const std::string &name,
const im_data &imd,
462 const gmm::sub_interval &I,
463 const model_real_plain_vector &VV);
464 void add_fixed_size_variable(
const std::string &name,
465 const gmm::sub_interval &I,
466 const model_real_plain_vector &VV);
467 void add_fem_constant(
const std::string &name,
const mesh_fem &mf,
468 const model_real_plain_vector &VV);
469 void add_fixed_size_constant(
const std::string &name,
470 const model_real_plain_vector &VV);
471 void add_im_data(
const std::string &name,
const im_data &imd,
472 const model_real_plain_vector &VV);
474 bool used_variables(std::vector<std::string> &vl,
475 std::vector<std::string> &vl_test1,
476 std::vector<std::string> &vl_test2,
477 std::vector<std::string> &dl,
481 bool variable_exists(
const std::string &name)
const;
483 bool is_internal_variable(
const std::string &name)
const;
485 const std::string &variable_in_group(
const std::string &group_name,
486 const mesh &m)
const;
488 void define_variable_group(
const std::string &group_name,
489 const std::vector<std::string> &nl);
491 bool variable_group_exists(
const std::string &name)
const;
493 bool variable_or_group_exists(
const std::string &name)
const
494 {
return variable_exists(name) || variable_group_exists(name); }
496 const std::vector<std::string> &
497 variable_group(
const std::string &group_name)
const;
499 const std::string& first_variable_of_group(
const std::string &name)
const;
501 bool is_constant(
const std::string &name)
const;
503 bool is_disabled_variable(
const std::string &name)
const;
505 const scalar_type &factor_of_variable(
const std::string &name)
const;
507 const gmm::sub_interval &
508 interval_of_variable(
const std::string &name)
const;
510 const mesh_fem *associated_mf(
const std::string &name)
const;
512 const im_data *associated_im_data(
const std::string &name)
const;
514 size_type qdim(
const std::string &name)
const;
516 bgeot::multi_index qdims(
const std::string &name)
const;
518 const model_real_plain_vector &value(
const std::string &name)
const;
519 scalar_type get_time_step()
const;
522 bool macro_exists(
const std::string &name)
const
523 {
return macro_dict.macro_exists(name); }
525 void add_macro(
const std::string &name,
const std::string &expr)
526 { macro_dict.add_macro(name, expr); }
528 void del_macro(
const std::string &name) { macro_dict.del_macro(name); }
530 const std::string& get_macro(
const std::string &name)
const;
532 const ga_macro_dictionary ¯o_dictionary()
const {
return macro_dict; }
536 void add_interpolate_transformation(
const std::string &name,
537 pinterpolate_transformation ptrans);
539 bool interpolate_transformation_exists(
const std::string &name)
const;
541 pinterpolate_transformation
542 interpolate_transformation(
const std::string &name)
const;
544 void add_elementary_transformation(
const std::string &name,
545 pelementary_transformation ptrans)
546 { elem_transformations[name] = ptrans; }
548 bool elementary_transformation_exists(
const std::string &name)
const;
550 pelementary_transformation
551 elementary_transformation(
const std::string &name)
const;
553 void add_secondary_domain(
const std::string &name,
554 psecondary_domain psecdom);
556 bool secondary_domain_exists(
const std::string &name)
const;
558 psecondary_domain secondary_domain(
const std::string &name)
const;
561 std::string extract_constant_term(
const mesh &m);
562 std::string extract_order1_term(
const std::string &varname);
563 std::string extract_order0_term();
564 std::string extract_Neumann_term(
const std::string &varname);
566 void assembly(
size_type order,
bool condensation=
false);
568 void set_include_empty_int_points(
bool include);
569 bool include_empty_int_points()
const;
571 size_type nb_primary_dof()
const {
return nb_prim_dof; }
572 size_type nb_internal_dof()
const {
return nb_intern_dof; }
573 size_type first_internal_dof()
const {
return first_intern_dof; }
574 size_type nb_temporary_dof()
const {
return nb_tmp_dof; }
576 void add_temporary_interval_for_unreduced_variable(
const std::string &name);
578 void clear_temporary_variable_intervals() {
579 tmp_var_intervals.clear();
583 const gmm::sub_interval &
584 temporary_interval_of_variable(
const std::string &name)
const {
585 static const gmm::sub_interval empty_interval;
586 const auto it = tmp_var_intervals.find(name);
587 return (it != tmp_var_intervals.end()) ? it->second : empty_interval;
590 enum class inherit { NONE, ENABLED, ALL };
593 const inherit var_inherit=inherit::ENABLED);
594 explicit ga_workspace(
const ga_workspace &gaw,
595 const inherit var_inherit);
602 std::string ga_substitute(
const std::string &expr,
603 const std::map<std::string, std::string> &dict);
605 inline std::string ga_substitute(
const std::string &expr,
606 const std::string &o1,
const std::string &s1) {
607 std::map<std::string, std::string> dict;
609 return ga_substitute(expr, dict);
612 inline std::string ga_substitute(
const std::string &expr,
613 const std::string &o1,
const std::string &s1,
614 const std::string &o2,
const std::string &s2) {
615 std::map<std::string, std::string> dict;
616 dict[o1] = s1; dict[o2] = s2;
617 return ga_substitute(expr, dict);
620 inline std::string ga_substitute(
const std::string &expr,
621 const std::string &o1,
const std::string &s1,
622 const std::string &o2,
const std::string &s2,
623 const std::string &o3,
const std::string &s3) {
624 std::map<std::string, std::string> dict;
625 dict[o1] = s1; dict[o2] = s2; dict[o3] = s3;
626 return ga_substitute(expr, dict);
629 inline std::string ga_substitute(
const std::string &expr,
630 const std::string &o1,
const std::string &s1,
631 const std::string &o2,
const std::string &s2,
632 const std::string &o3,
const std::string &s3,
633 const std::string &o4,
const std::string &s4) {
634 std::map<std::string, std::string> dict;
635 dict[o1] = s1; dict[o2] = s2; dict[o3] = s3; dict[o4] = s4;
636 return ga_substitute(expr, dict);
644 struct ga_instruction_set;
647 mutable ga_workspace local_workspace;
649 mutable ga_instruction_set *gis;
652 ga_function() : local_workspace(), expr(
""), gis(0) {}
653 ga_function(
const model &md,
const std::string &e);
654 ga_function(
const ga_workspace &workspace_,
const std::string &e);
655 ga_function(
const std::string &e);
656 ga_function(
const ga_function &gaf);
657 ga_function &operator =(
const ga_function &gaf);
659 const std::string &expression()
const {
return expr; }
660 const base_tensor &eval()
const;
661 void derivative(
const std::string &variable);
662 void compile()
const;
663 ga_workspace &workspace()
const {
return local_workspace; }
671 struct ga_interpolation_context {
673 virtual bgeot::pstored_point_tab
675 std::vector<size_type> &ind)
const = 0;
678 {
return *ppoints_for_element(cv, f, ind); }
679 virtual bool use_pgp(
size_type cv)
const = 0;
680 virtual bool use_mim()
const = 0;
682 virtual void finalize() = 0;
683 virtual const mesh &linked_mesh() = 0;
684 virtual ~ga_interpolation_context() {}
691 void ga_interpolation(ga_workspace &workspace,
692 ga_interpolation_context &gic);
694 void ga_interpolation_Lagrange_fem
695 (ga_workspace &workspace,
const mesh_fem &mf, base_vector &result);
697 void ga_interpolation_Lagrange_fem
698 (
const getfem::model &md,
const std::string &expr,
const mesh_fem &mf,
701 void ga_interpolation_mti
702 (
const getfem::model &md,
const std::string &expr, mesh_trans_inv &mti,
704 int extrapolation = 0,
708 void ga_interpolation_im_data
709 (ga_workspace &workspace,
const im_data &imd, base_vector &result);
711 void ga_interpolation_im_data
712 (
const getfem::model &md,
const std::string &expr,
const im_data &imd,
715 void ga_interpolation_mesh_slice
716 (ga_workspace &workspace,
const stored_mesh_slice &sl, base_vector &result);
718 void ga_interpolation_mesh_slice
719 (
const getfem::model &md,
const std::string &expr,
const stored_mesh_slice &sl,
734 const std::string &expr,
const mesh_fem &mf,
752 (ga_workspace &workspace,
const std::string &transname,
753 const mesh &source_mesh,
const mesh &target_mesh,
const std::string &expr);
755 (ga_workspace &workspace,
const std::string &transname,
756 const mesh &source_mesh,
const mesh &target_mesh,
757 size_type target_region,
const std::string &expr);
759 (model &md,
const std::string &transname,
760 const mesh &source_mesh,
const mesh &target_mesh,
const std::string &expr);
762 (model &md,
const std::string &transname,
763 const mesh &source_mesh,
const mesh &target_mesh,
764 size_type target_region,
const std::string &expr);
775 (ga_workspace &workspace,
const std::string &transname,
776 const mesh &source_mesh,
const std::string &source_displacements,
777 const mesh_region &source_region,
const mesh &target_mesh,
778 const std::string &target_displacements,
const mesh_region &target_region);
783 (model &md,
const std::string &transname,
784 const mesh &source_mesh,
const std::string &source_displacements,
785 const mesh_region &source_region,
const mesh &target_mesh,
786 const std::string &target_displacements,
const mesh_region &target_region);
804 void add_element_extrapolation_transformation
805 (model &md,
const std::string &name,
const mesh &sm,
806 std::map<size_type, size_type> &elt_corr);
808 void add_element_extrapolation_transformation
809 (ga_workspace &workspace,
const std::string &name,
const mesh &sm,
810 std::map<size_type, size_type> &elt_corr);
815 void set_element_extrapolation_correspondence
816 (model &md,
const std::string &name,
817 std::map<size_type, size_type> &elt_corr);
819 void set_element_extrapolation_correspondence
820 (ga_workspace &workspace,
const std::string &name,
821 std::map<size_type, size_type> &elt_corr);
827 void add_standard_secondary_domain
828 (model &md,
const std::string &name,
const mesh_im &mim,
831 void add_standard_secondary_domain
832 (ga_workspace &workspace,
const std::string &name,
const mesh_im &mim,
static mesh_region all_convexes()
provide a default value for the mesh_region parameters of assembly procedures etc.
`‘Model’' variables store the variables, the data and the description of a model.
sparse vector built upon std::vector.
Interpolation of fields from a mesh_fem onto another.
Define the class getfem::stored_mesh_slice.
void copy(const L1 &l1, L2 &l2)
*/
gmm::uint16_type short_type
used as the common short type integer in the library
size_t size_type
used as the common size type in the library
GEneric Tool for Finite Element Methods.
pinterpolate_transformation interpolate_transformation_neighbor_instance()
Create a new instance of a transformation corresponding to the interpolation on the neighbor element.
void add_interpolate_transformation_on_deformed_domains(ga_workspace &workspace, const std::string &transname, const mesh &source_mesh, const std::string &source_displacements, const mesh_region &source_region, const mesh &target_mesh, const std::string &target_displacements, const mesh_region &target_region)
Add a transformation to the workspace that creates an identity mapping between two meshes in deformed...
void add_interpolate_transformation_from_expression(ga_workspace &workspace, const std::string &transname, const mesh &source_mesh, const mesh &target_mesh, const std::string &expr)
Add a transformation to a workspace workspace or a model md mapping point in mesh source_mesh to mesh...
void ga_local_projection(const getfem::model &md, const mesh_im &mim, const std::string &expr, const mesh_fem &mf, base_vector &result, const mesh_region &rg=mesh_region::all_convexes())
Make an elementwise L2 projection of an expression with respect to the mesh_fem mf.