GetFEM  5.4.3
getfem_deformable_mesh.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2012-2020 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, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 
32 /** @file getfem_deformable_mesh.h
33 @author "Andriy Andreykiv" <andriy.andreykiv@gmail.com>
34 @date August 7, 2012.
35 @brief A class adaptor to deform a mesh.
36 */
37 
38 #pragma once
39 #ifndef GETFEM_DEFORMABLE_MESH_H__
40 #define GETFEM_DEFORMABLE_MESH_H__
41 
42 #include <getfem/getfem_mesh.h>
43 #include <getfem/getfem_mesh_fem.h>
44 #include <getfem/getfem_models.h>
45 
46 namespace getfem {
47 
48  /** An object function that first deforms and then remembers
49  to restore a mesh if it has to be restored
50  for other bricks. By default the mesh is deformed on
51  construct and undeformed in the destructor (by RAII principle)
52  but it's also possible to specify deform_on_construct = false
53  and then call explicitely deform() and undeform() methods.
54  Optional to_be_restored flag will control whether the mesh will be restored
55  when the deformator destructs.
56  */
57  template<class VECTOR = model_real_plain_vector>
59  {
60  public:
61  temporary_mesh_deformator(const mesh_fem &mf, const VECTOR &dU,
62  bool deform_on_construct = true, bool to_be_restored = true)
63  : dU_(mf.nb_basic_dof()),
64  mf_(mf),
65  deform_on_construct_(deform_on_construct),
66  is_deformed_(false),
67  to_be_restored_(to_be_restored){
68  mf.extend_vector(dU, dU_);
69  if (deform_on_construct_) deform();
70  }
71 
72  void deform(){
73  if (is_deformed_) return;
74  initial_nodes_ = mf_.linked_mesh().points();
75  deforming_mesh_(dU_);
76  is_deformed_ = true;
77  }
78 
79  void undeform(){
80  if (!is_deformed_) return;
81  restore_();
82  is_deformed_ = false;
83  }
84 
86  if (to_be_restored_ && deform_on_construct_){
87  undeform();
88  }
89  }
90 
91  private:
92  void deforming_mesh_(VECTOR &dU){
93  auto &m_ = const_cast<getfem::mesh &>(mf_.linked_mesh());
94  auto &ppts = m_.points();
95  auto init_nb_points = ppts.card();
96 
97  dal::bit_vector conv_indices = mf_.convex_index();
98  //this vector will track if a point can be deformed
99  std::vector<bool> deform_pt_flag(ppts.size(), true);
100  size_type cv;
101  for (cv << conv_indices; cv != bgeot::size_type(-1); cv << conv_indices)
102  {
103  getfem::mesh::ind_set pt_index = m_.ind_points_of_convex(cv);
104  getfem::mesh_fem::ind_dof_ct dof = mf_.ind_basic_dof_of_element(cv);
105  bgeot::size_type num_points = m_.structure_of_convex(cv)->nb_points();
106 
107  GMM_ASSERT2(dof.size() % num_points == 0,
108  "mesh_fem should be isoparametric to the mesh, "
109  "with nb_points() of convex == size of ind_basic_dof_of_element / qdim()");
110 
111  size_type ddim = dof.size() / num_points;
112  GMM_ASSERT2(ddim <= 3, "dimension of dof is greater than 3");
113 
114  for (size_type pt = 0; pt < num_points; ++pt)
115  {
116  /** iterate through each components of point [pt]and deform the component*/
117  if (deform_pt_flag[pt_index[pt]])
118  for (size_type comp = 0; comp < ddim; ++comp)
119  //move pts by dU;
120  ppts[pt_index[pt]][comp] += dU[dof[pt*ddim + comp]];
121 
122  //flag current [pt] to deformed
123  deform_pt_flag[pt_index[pt]] = false;
124  }
125  ppts.resort();
126  }
127  GMM_ASSERT1(ppts.card() == init_nb_points,
128  "Error, after deforming the mesh, number of nodes are different.");
129  }
130 
131  void restore_()
132  {
133  auto &pts = const_cast<getfem::mesh &>(mf_.linked_mesh()).points();
134  GMM_ASSERT1(pts.size() == initial_nodes_.size(), "Internal error, incorrect number of points.");
135  for (size_type i = 0; i < pts.size(); ++i) gmm::copy(initial_nodes_[i], pts[i]);
136  }
137 
138  VECTOR dU_;
139  const mesh_fem &mf_;
140  getfem::mesh::PT_TAB initial_nodes_;
141  bool deform_on_construct_;
142  bool is_deformed_;
143  bool to_be_restored_;
144  };
145 
146 }//end of getfem namespace
147 
148 #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:99
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:49
GEneric Tool for Finite Element Methods.