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(
void)
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 &mf,
size_type cv,
128 base_matrix &M)
const = 0;
129 virtual ~virtual_elementary_transformation() {}
132 typedef std::shared_ptr<const virtual_elementary_transformation>
133 pelementary_transformation;
143 std::string macro_name_;
148 ga_macro(
const std::string &name,
const ga_tree &t,
size_type nbp_);
149 ga_macro(
const ga_macro &);
151 ga_macro &operator =(
const ga_macro &);
153 const std::string &name()
const {
return macro_name_; }
154 std::string &name() {
return macro_name_; }
155 size_type nb_params()
const {
return nbp; }
157 const ga_tree& tree()
const {
return *ptree; }
158 ga_tree& tree() {
return *ptree; }
162 class ga_macro_dictionnary {
165 const ga_macro_dictionnary *parent;
166 std::map<std::string, ga_macro> macros;
169 bool macro_exists(
const std::string &name)
const;
170 const ga_macro &get_macro(
const std::string &name)
const;
172 void add_macro(
const ga_macro &gam);
173 void add_macro(
const std::string &name,
const std::string &expr);
174 void del_macro(
const std::string &name);
176 ga_macro_dictionnary() : parent(0) {}
177 ga_macro_dictionnary(
bool,
const ga_macro_dictionnary& gamd)
187 struct ga_nonlinear_operator {
189 typedef std::vector<const base_tensor *> arg_list;
191 virtual bool result_size(
const arg_list &args,
192 bgeot::multi_index &sizes)
const = 0;
194 virtual void value(
const arg_list &args, base_tensor &result)
const = 0;
196 virtual void derivative(
const arg_list &args,
size_type i,
197 base_tensor &result)
const = 0;
199 virtual void second_derivative(
const arg_list &args,
size_type i,
200 size_type j, base_tensor &result)
const = 0;
202 virtual ~ga_nonlinear_operator() {}
205 struct ga_predef_operator_tab {
206 typedef std::map<std::string, std::shared_ptr<ga_nonlinear_operator>> T;
209 void add_method(
const std::string &name,
210 const std::shared_ptr<ga_nonlinear_operator> &pt)
212 ga_predef_operator_tab();
219 typedef scalar_type (*pscalar_func_onearg)(scalar_type);
220 typedef scalar_type (*pscalar_func_twoargs)(scalar_type, scalar_type);
222 void ga_define_function(
const std::string &name,
size_type nb_args,
223 const std::string &expr,
const std::string &der1=
"",
224 const std::string &der2=
"");
225 void ga_define_function(
const std::string &name, pscalar_func_onearg f,
226 const std::string &der1=
"");
227 void ga_define_function(
const std::string &name, pscalar_func_twoargs f2,
228 const std::string &der1=
"",
229 const std::string &der2=
"");
231 void ga_undefine_function(
const std::string &name);
232 bool ga_function_exists(
const std::string &name);
242 const ga_workspace *parent_workspace;
243 bool enable_all_md_variables;
247 struct var_description {
253 const model_real_plain_vector *V;
255 bgeot::multi_index qdims;
261 for (
size_type i = 0; i < qdims.size(); ++i) q *= qdims[i];
265 var_description(
bool is_var,
bool is_fem,
266 const mesh_fem *mmf, gmm::sub_interval I_,
267 const model_real_plain_vector *v,
const im_data *imd_,
269 : is_variable(is_var), is_fem_dofs(is_fem), mf(mmf), I(I_), V(v),
270 imd(imd_), qdims(1) {
271 GMM_ASSERT1(Q > 0,
"Bad dimension");
274 var_description() : is_variable(false), is_fem_dofs(false),
275 mf(0), V(0), imd(0), qdims(1) { qdims[0] = 1; }
280 struct tree_description {
287 std::string varname_interpolation;
288 std::string name_test1, name_test2;
289 std::string interpolate_name_test1, interpolate_name_test2;
292 const mesh_region *rg;
295 : interpolation(0), varname_interpolation(
""),
296 name_test1(
""), name_test2(
""),
297 interpolate_name_test1(
""), interpolate_name_test2(
""),
298 mim(0), m(0), rg(0), ptree(0) {}
299 void copy(
const tree_description& td);
300 tree_description(
const tree_description& td) {
copy(td); }
301 tree_description &operator =(
const tree_description& td);
305 mutable std::set<var_trans_pair> test1, test2;
306 var_trans_pair selected_test1, selected_test2;
311 std::map<const mesh *, std::list<mesh_region> > registred_mesh_regions;
314 register_region(
const mesh &m,
const mesh_region ®ion);
317 mutable std::map<std::string, gmm::sub_interval> int_disabled_variables;
319 typedef std::map<std::string, var_description> VAR_SET;
321 std::map<std::string, pinterpolate_transformation> transformations;
322 std::map<std::string, pelementary_transformation> elem_transformations;
323 std::vector<tree_description> trees;
325 std::map<std::string, std::vector<std::string> > variable_groups;
327 ga_macro_dictionnary macro_dict;
333 m_tree() : ptree(0), meshdim(-1), ignore_X(false) {}
334 m_tree(
const m_tree& o);
335 m_tree &operator =(
const m_tree& o);
339 void add_tree(ga_tree &tree,
const mesh &m,
const mesh_im &mim,
340 const mesh_region &rg,
341 const std::string &expr,
size_type add_derivative_order,
342 bool scalar_expr,
size_type for_interpolation,
343 const std::string varname_interpolation);
346 std::shared_ptr<model_real_sparse_matrix> K;
347 model_real_sparse_matrix unreduced_K;
348 std::shared_ptr<base_vector> V;
349 base_vector unreduced_V;
350 base_tensor assemb_t;
351 bool include_empty_int_pts =
false;
355 const model_real_sparse_matrix &assembled_matrix()
const {
return *K;}
356 model_real_sparse_matrix &assembled_matrix() {
return *K; }
357 scalar_type &assembled_potential()
358 { GMM_ASSERT1(assemb_t.size() == 1,
"Bad result size");
return assemb_t[0]; }
359 const scalar_type &assembled_potential()
const 360 { GMM_ASSERT1(assemb_t.size() == 1,
"Bad result size");
return assemb_t[0]; }
361 const base_vector &assembled_vector()
const {
return *V; }
362 base_vector &assembled_vector() {
return *V; }
363 void set_assembled_matrix(model_real_sparse_matrix &K_) {
364 K = std::shared_ptr<model_real_sparse_matrix>
365 (std::shared_ptr<model_real_sparse_matrix>(), &K_);
367 void set_assembled_vector(base_vector &V_) {
368 V = std::shared_ptr<base_vector>
369 (std::shared_ptr<base_vector>(), &V_);
371 base_tensor &assembled_tensor() {
return assemb_t; }
372 const base_tensor &assembled_tensor()
const {
return assemb_t; }
374 model_real_sparse_matrix &unreduced_matrix()
375 {
return unreduced_K; }
376 base_vector &unreduced_vector() {
return unreduced_V; }
382 size_type add_expression(
const std::string &expr,
const mesh_im &mim,
386 void add_function_expression(
const std::string &expr);
388 void add_interpolation_expression
389 (
const std::string &expr,
const mesh &m,
391 void add_interpolation_expression
392 (
const std::string &expr,
const mesh_im &mim,
394 void add_assignment_expression
395 (
const std::string &dataname,
const std::string &expr,
397 size_type order = 1,
bool before =
false);
400 void clear_expressions();
403 void print(std::ostream &str);
406 tree_description &tree_info(
size_type i);
409 void add_fem_variable(
const std::string &name,
const mesh_fem &mf,
410 const gmm::sub_interval &I,
411 const model_real_plain_vector &VV);
412 void add_fixed_size_variable(
const std::string &name,
413 const gmm::sub_interval &I,
414 const model_real_plain_vector &VV);
415 void add_fem_constant(
const std::string &name,
const mesh_fem &mf,
416 const model_real_plain_vector &VV);
417 void add_fixed_size_constant(
const std::string &name,
418 const model_real_plain_vector &VV);
419 void add_im_data(
const std::string &name,
const im_data &imd,
420 const model_real_plain_vector &VV);
422 bool used_variables(std::vector<std::string> &vl,
423 std::vector<std::string> &vl_test1,
424 std::vector<std::string> &vl_test2,
425 std::vector<std::string> &dl,
428 bool variable_exists(
const std::string &name)
const;
430 const std::string &variable_in_group(
const std::string &group_name,
431 const mesh &m)
const;
433 void define_variable_group(
const std::string &group_name,
434 const std::vector<std::string> &nl);
436 bool variable_group_exists(
const std::string &name)
const;
438 bool variable_or_group_exists(
const std::string &name)
const 439 {
return variable_exists(name) || variable_group_exists(name); }
441 const std::vector<std::string> &
442 variable_group(
const std::string &group_name)
const;
444 const std::string& first_variable_of_group(
const std::string &name)
const;
446 bool is_constant(
const std::string &name)
const;
448 bool is_disabled_variable(
const std::string &name)
const;
450 const scalar_type &factor_of_variable(
const std::string &name)
const;
452 const gmm::sub_interval &
453 interval_of_disabled_variable(
const std::string &name)
const;
455 const gmm::sub_interval &
456 interval_of_variable(
const std::string &name)
const;
458 const mesh_fem *associated_mf(
const std::string &name)
const;
460 const im_data *associated_im_data(
const std::string &name)
const;
462 size_type qdim(
const std::string &name)
const;
464 bgeot::multi_index qdims(
const std::string &name)
const;
466 const model_real_plain_vector &value(
const std::string &name)
const;
467 scalar_type get_time_step()
const;
470 bool macro_exists(
const std::string &name)
const 471 {
return macro_dict.macro_exists(name); }
473 void add_macro(
const std::string &name,
const std::string &expr)
474 { macro_dict.add_macro(name, expr); }
476 void del_macro(
const std::string &name) { macro_dict.del_macro(name); }
478 const std::string& get_macro(
const std::string &name)
const;
480 const ga_macro_dictionnary ¯o_dictionnary()
const {
return macro_dict; }
484 void add_interpolate_transformation(
const std::string &name,
485 pinterpolate_transformation ptrans);
487 bool interpolate_transformation_exists(
const std::string &name)
const;
489 pinterpolate_transformation
490 interpolate_transformation(
const std::string &name)
const;
492 void add_elementary_transformation(
const std::string &name,
493 pelementary_transformation ptrans)
494 { elem_transformations[name] = ptrans; }
496 bool elementary_transformation_exists(
const std::string &name)
const;
498 pelementary_transformation
499 elementary_transformation(
const std::string &name)
const;
503 std::string extract_constant_term(
const mesh &m);
504 std::string extract_order1_term(
const std::string &varname);
505 std::string extract_order0_term();
506 std::string extract_Neumann_term(
const std::string &varname);
511 void set_include_empty_int_points(
bool include);
512 bool include_empty_int_points()
const;
514 ga_workspace(
const getfem::model &md_,
bool enable_all_variables =
false);
515 ga_workspace(
bool,
const ga_workspace &gaw);
522 std::string ga_substitute(
const std::string &expr,
523 const std::map<std::string, std::string> &dict);
525 inline std::string ga_substitute(
const std::string &expr,
526 const std::string &o1,
const std::string &s1) {
527 std::map<std::string, std::string> dict;
529 return ga_substitute(expr, dict);
532 inline std::string ga_substitute(
const std::string &expr,
533 const std::string &o1,
const std::string &s1,
534 const std::string &o2,
const std::string &s2) {
535 std::map<std::string, std::string> dict;
536 dict[o1] = s1; dict[o2] = s2;
537 return ga_substitute(expr, dict);
540 inline std::string ga_substitute(
const std::string &expr,
541 const std::string &o1,
const std::string &s1,
542 const std::string &o2,
const std::string &s2,
543 const std::string &o3,
const std::string &s3) {
544 std::map<std::string, std::string> dict;
545 dict[o1] = s1; dict[o2] = s2; dict[o3] = s3;
546 return ga_substitute(expr, dict);
549 inline std::string ga_substitute(
const std::string &expr,
550 const std::string &o1,
const std::string &s1,
551 const std::string &o2,
const std::string &s2,
552 const std::string &o3,
const std::string &s3,
553 const std::string &o4,
const std::string &s4) {
554 std::map<std::string, std::string> dict;
555 dict[o1] = s1; dict[o2] = s2; dict[o3] = s3; dict[o4] = s4;
556 return ga_substitute(expr, dict);
564 struct ga_instruction_set;
567 mutable ga_workspace local_workspace;
569 mutable ga_instruction_set *gis;
572 ga_function() : local_workspace(), expr(
""), gis(0) {}
573 ga_function(
const model &md,
const std::string &e);
574 ga_function(
const ga_workspace &workspace_,
const std::string &e);
575 ga_function(
const std::string &e);
576 ga_function(
const ga_function &gaf);
577 ga_function &operator =(
const ga_function &gaf);
579 const std::string &expression()
const {
return expr; }
580 const base_tensor &eval()
const;
581 void derivative(
const std::string &variable);
582 void compile()
const;
583 ga_workspace &workspace()
const {
return local_workspace; }
591 struct ga_interpolation_context {
593 virtual bgeot::pstored_point_tab
595 std::vector<size_type> &ind)
const = 0;
598 {
return *ppoints_for_element(cv, f, ind); }
599 virtual bool use_pgp(
size_type cv)
const = 0;
600 virtual bool use_mim()
const = 0;
602 virtual void finalize() = 0;
603 virtual const mesh &linked_mesh() = 0;
604 virtual ~ga_interpolation_context() {}
611 void ga_interpolation(ga_workspace &workspace,
612 ga_interpolation_context &gic);
614 void ga_interpolation_Lagrange_fem
615 (ga_workspace &workspace,
const mesh_fem &mf, base_vector &result);
617 void ga_interpolation_Lagrange_fem
618 (
const getfem::model &md,
const std::string &expr,
const mesh_fem &mf,
621 void ga_interpolation_mti
622 (
const getfem::model &md,
const std::string &expr, mesh_trans_inv &mti,
624 int extrapolation = 0,
628 void ga_interpolation_im_data
629 (ga_workspace &workspace,
const im_data &imd, base_vector &result);
631 void ga_interpolation_im_data
632 (
const getfem::model &md,
const std::string &expr,
const im_data &imd,
635 void ga_interpolation_mesh_slice
636 (ga_workspace &workspace,
const stored_mesh_slice &sl, base_vector &result);
638 void ga_interpolation_mesh_slice
639 (
const getfem::model &md,
const std::string &expr,
const stored_mesh_slice &sl,
654 const std::string &expr,
const mesh_fem &mf,
672 (ga_workspace &workspace,
const std::string &transname,
673 const mesh &source_mesh,
const mesh &target_mesh,
const std::string &expr);
675 (ga_workspace &workspace,
const std::string &transname,
676 const mesh &source_mesh,
const mesh &target_mesh,
677 size_type target_region,
const std::string &expr);
679 (model &md,
const std::string &transname,
680 const mesh &source_mesh,
const mesh &target_mesh,
const std::string &expr);
682 (model &md,
const std::string &transname,
683 const mesh &source_mesh,
const mesh &target_mesh,
684 size_type target_region,
const std::string &expr);
695 (ga_workspace &workspace,
const std::string &transname,
696 const mesh &source_mesh,
const std::string &source_displacements,
697 const mesh_region &source_region,
const mesh &target_mesh,
698 const std::string &target_displacements,
const mesh_region &target_region);
703 (model &md,
const std::string &transname,
704 const mesh &source_mesh,
const std::string &source_displacements,
705 const mesh_region &source_region,
const mesh &target_mesh,
706 const std::string &target_displacements,
const mesh_region &target_region);
724 void add_element_extrapolation_transformation
725 (model &md,
const std::string &name,
const mesh &sm,
726 std::map<size_type, size_type> &elt_corr);
728 void add_element_extrapolation_transformation
729 (ga_workspace &workspace,
const std::string &name,
const mesh &sm,
730 std::map<size_type, size_type> &elt_corr);
735 void set_element_extrapolation_correspondance
736 (model &md,
const std::string &name,
737 std::map<size_type, size_type> &elt_corr);
739 void set_element_extrapolation_correspondance
740 (ga_workspace &workspace,
const std::string &name,
741 std::map<size_type, size_type> &elt_corr);
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...
static mesh_region all_convexes()
provide a default value for the mesh_region parameters of assembly procedures etc.
void interpolation(const mesh_fem &mf_source, const mesh_fem &mf_target, const VECTU &U, VECTV &V, int extrapolation=0, double EPS=1E-10, mesh_region rg_source=mesh_region::all_convexes(), mesh_region rg_target=mesh_region::all_convexes())
interpolation/extrapolation of (mf_source, U) on mf_target.
pinterpolate_transformation interpolate_transformation_neighbour_instance()
Create a new instance of a transformation corresponding to the interpolation on the neighbour element...
``Model'' variables store the variables, the data and the description of a model. ...
size_t size_type
used as the common size type in the library
void copy(const L1 &l1, L2 &l2)
*/
Interpolation of fields from a mesh_fem onto another.
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.
sparse vector built upon std::vector.
GEneric Tool for Finite Element Methods.
gmm::uint16_type short_type
used as the common short type integer in the library
Define the class getfem::stored_mesh_slice.