GetFEM  5.5
getfem_deformable_mesh.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2012-2026 Andriy Andreykiv
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 /** @file getfem_deformable_mesh.h
32 @author "Andriy Andreykiv" <andriy.andreykiv@gmail.com>
33 @date August 7, 2012.
34 @brief A class adaptor to deform a mesh.
35 */
36 
37 #pragma once
38 #ifndef GETFEM_DEFORMABLE_MESH_H__
39 #define GETFEM_DEFORMABLE_MESH_H__
40 
41 #include <getfem/getfem_mesh.h>
42 #include <getfem/getfem_mesh_fem.h>
43 #include <getfem/getfem_models.h>
44 
45 namespace getfem {
46 
47  /** An object function that first deforms and then remembers
48  to restore a mesh if it has to be restored
49  for other bricks. By default the mesh is deformed on
50  construct and undeformed in the destructor (by RAII principle)
51  but it's also possible to specify deform_on_construct = false
52  and then call explicitely deform() and undeform() methods.
53  Optional to_be_restored flag will control whether the mesh will be restored
54  when the deformator destructs.
55  */
56  template<class VECTOR = model_real_plain_vector>
58  {
59  public:
60  temporary_mesh_deformator(const mesh_fem &mf, const VECTOR &dU,
61  bool deform_on_construct = true, bool to_be_restored = true)
62  : dU_(mf.nb_basic_dof()),
63  mf_(mf),
64  deform_on_construct_(deform_on_construct),
65  is_deformed_(false),
66  to_be_restored_(to_be_restored){
67  mf.extend_vector(dU, dU_);
68  if (deform_on_construct_) deform();
69  }
70 
71  void deform(){
72  if (is_deformed_) return;
73  initial_nodes_ = mf_.linked_mesh().points();
74  deforming_mesh_(dU_);
75  is_deformed_ = true;
76  }
77 
78  void undeform(){
79  if (!is_deformed_) return;
80  restore_();
81  is_deformed_ = false;
82  }
83 
85  if (to_be_restored_ && deform_on_construct_){
86  undeform();
87  }
88  }
89 
90  private:
91  void deforming_mesh_(VECTOR &dU){
92  auto &m_ = const_cast<getfem::mesh &>(mf_.linked_mesh());
93  auto &ppts = m_.points();
94  auto init_nb_points = ppts.card();
95 
96  dal::bit_vector conv_indices = mf_.convex_index();
97  //this vector will track if a point can be deformed
98  std::vector<bool> deform_pt_flag(ppts.size(), true);
99  size_type cv;
100  for (cv << conv_indices; cv != bgeot::size_type(-1); cv << conv_indices)
101  {
102  getfem::mesh::ind_set pt_index = m_.ind_points_of_convex(cv);
103  getfem::mesh_fem::ind_dof_ct dof = mf_.ind_basic_dof_of_element(cv);
104  bgeot::size_type num_points = m_.structure_of_convex(cv)->nb_points();
105 
106  GMM_ASSERT2(dof.size() % num_points == 0,
107  "mesh_fem should be isoparametric to the mesh, "
108  "with nb_points() of convex == size of ind_basic_dof_of_element / qdim()");
109 
110  size_type ddim = dof.size() / num_points;
111  GMM_ASSERT2(ddim <= 3, "dimension of dof is greater than 3");
112 
113  for (size_type pt = 0; pt < num_points; ++pt)
114  {
115  /** iterate through each components of point [pt]and deform the component*/
116  if (deform_pt_flag[pt_index[pt]])
117  for (size_type comp = 0; comp < ddim; ++comp)
118  //move pts by dU;
119  ppts[pt_index[pt]][comp] += dU[dof[pt*ddim + comp]];
120 
121  //flag current [pt] to deformed
122  deform_pt_flag[pt_index[pt]] = false;
123  }
124  ppts.resort();
125  }
126  GMM_ASSERT1(ppts.card() == init_nb_points,
127  "Error, after deforming the mesh, number of nodes are different.");
128  }
129 
130  void restore_()
131  {
132  auto &pts = const_cast<getfem::mesh &>(mf_.linked_mesh()).points();
133  GMM_ASSERT1(pts.size() == initial_nodes_.size(), "Internal error, incorrect number of points.");
134  for (size_type i = 0; i < pts.size(); ++i) gmm::copy(initial_nodes_[i], pts[i]);
135  }
136 
137  VECTOR dU_;
138  const mesh_fem &mf_;
139  getfem::mesh::PT_TAB initial_nodes_;
140  bool deform_on_construct_;
141  bool is_deformed_;
142  bool to_be_restored_;
143  };
144 
145 }//end of getfem namespace
146 
147 #endif //GETFEM_DEFORMABLE_MESH_H__
Store a set of points, identifying points that are nearer than a certain very small distance.
Describe a finite element method linked to a mesh.
virtual ind_dof_ct ind_basic_dof_of_element(size_type cv) const
Give an array of the dof numbers a of convex.
const mesh & linked_mesh() const
Return a reference to the underlying mesh.
virtual size_type nb_basic_dof() const
Return the total number of basic degrees of freedom (before the optional reduction).
const dal::bit_vector & convex_index() const
Get the set of convexes where a finite element has been assigned.
Describe a mesh (collection of convexes (elements) and points).
Definition: getfem_mesh.h:98
An object function that first deforms and then remembers to restore a mesh if it has to be restored f...
Define a getfem::getfem_mesh object.
Define the getfem::mesh_fem class.
Model representation in Getfem.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48
GEneric Tool for Finite Element Methods.