MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/mpm/grid2d.h
Go to the documentation of this file.
00001 /************************************************************************
00002  * MechSys - Open Library for Mechanical Systems                        *
00003  * Copyright (C) 2005 Dorival M. Pedroso                                *
00004  *                                                                      *
00005  * This program is free software: you can redistribute it and/or modify *
00006  * it under the terms of the GNU General Public License as published by *
00007  * the Free Software Foundation, either version 3 of the License, or    *
00008  * any later version.                                                   *
00009  *                                                                      *
00010  * This program is distributed in the hope that it will be useful,      *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
00013  * GNU General Public License for more details.                         *
00014  *                                                                      *
00015  * You should have received a copy of the GNU General Public License    *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>  *
00017  ************************************************************************/
00018 
00019 /* Grid2D - Copyright (C) 2007 Dorival de Moraes Pedroso */
00020 
00021 #ifndef MPM_GRID2D_H
00022 #define MPM_GRID2D_H
00023 
00024 // Local
00025 #include <mechsys/mpm/defs.h>
00026 
00027 namespace MPM {
00028 
00029 class Grid2D
00030 {
00031 public:
00032     /* Constructor. */
00033     Grid2D (double xMin, double yMin, size_t nRow, size_t nCol, double Lx, double Ly, ptIsFixed pIsFixed);
00034 
00035     // Methods
00036     size_t nNodes        () const { return _X.Size(); }               
00037     size_t nCells        () const { return _c.Size(); }               
00038     bool   IsInside      (Vector3D const & P, bool WithBorder=false); 
00039     void   ClearState    (bool CurrentCoords=true);                   
00040     void   SetFixed      (size_t i, FixType Type);                    
00041     void   SetLoading    (size_t i, Vector3D const & Fe);             
00042     void   FixNodes      (Array<Vector3D> * VelOrDqDt);               
00043     void   ApplyLoadings (double M);                                  
00044     void   Refine        ();                                          
00045     size_t LBN           (Vector3D const & P, bool MPM=false);        
00046     bool   IsFixed       (size_t n, size_t iD) const;                 
00047 
00048     // Access methods
00049     double                  xMin ()             const { return _xmin;          } 
00050     double                  yMin ()             const { return _ymin;          } 
00051     double                  xMax ()             const { return _xmax;          } 
00052     double                  yMax ()             const { return _ymax;          } 
00053     size_t                  nRow ()             const { return _nrow;          } 
00054     size_t                  nCol ()             const { return _ncol;          } 
00055     double                  L    (size_t iComp) const { return _L(iComp);      } 
00056     Vector3D const &        L    ()             const { return _L;             } 
00057     Array<Vector3D> const & X    ()             const { return _X;             } 
00058     Array<Vector3D> const & x    ()             const { return _x;             } 
00059     Vector3D const &        X    (size_t i)     const { return _X[i];          } 
00060     Vector3D const &        x    (size_t i)     const { return _x[i];          } 
00061     Vector3D       &        x    (size_t i)           { return _x[i];          } 
00062     Connec2D const &        C    (size_t i)     const { return _c[i];          } 
00063     size_t                  nFix ()             const { return _fixed.Size();  } 
00064     Vector3D const &        FixN (size_t i)     const { return _X[_fixed[i]];  } 
00065     FixType                 FixT (size_t i)     const { return _fixtype[i];    } 
00066     size_t                  nLd  ()             const { return _loaded.Size(); } 
00067     Vector3D const &        LdN  (size_t i)     const { return _X[_loaded[i]]; } 
00068     Vector3D const &        Ld   (size_t i)     const { return _loads[i];      } 
00069 
00070     // Set methods
00071     double   & m    (size_t i) { return _m   [i]; } 
00072     Vector3D & q    (size_t i) { return _q   [i]; } 
00073     Vector3D & v    (size_t i) { return _v   [i]; } 
00074     Vector3D & fe   (size_t i) { return _fe  [i]; } 
00075     Vector3D & fi   (size_t i) { return _fi  [i]; } 
00076     Vector3D & dqdt (size_t i) { return _dqdt[i]; } 
00077     Array<Vector3D> & q    () { return _q   ; } 
00078     Array<Vector3D> & v    () { return _v   ; } 
00079     Array<Vector3D> & dqdt () { return _dqdt; } 
00080 
00081     // Get methods
00082     double   const & m    (size_t i) const { return _m   [i]; } 
00083     Vector3D const & q    (size_t i) const { return _q   [i]; } 
00084     Vector3D const & v    (size_t i) const { return _v   [i]; } 
00085     Vector3D const & fe   (size_t i) const { return _fe  [i]; } 
00086     Vector3D const & fi   (size_t i) const { return _fi  [i]; } 
00087     Vector3D const & dqdt (size_t i) const { return _dqdt[i]; } 
00088 
00089 private:
00090     // Input
00091     double     _xmin;    
00092     double     _ymin;    
00093     size_t     _nrow;    
00094     size_t     _ncol;    
00095     Vector3D  _L;        
00096     ptIsFixed _is_fixed; 
00097 
00098     // Geometry
00099     double          _xmax; 
00100     double          _ymax; 
00101     Array<Vector3D> _X;    
00102     Array<Vector3D> _x;    
00103     Array<Connec2D> _c;    
00104 
00105     // State values
00106     Array<double>   _m;    
00107     Array<Vector3D> _q;    
00108     Array<Vector3D> _v;    
00109     Array<Vector3D> _fe;   
00110     Array<Vector3D> _fi;   
00111     Array<Vector3D> _dqdt; 
00112 
00113     // Fixed nodes
00114     Array<size_t>  _fixed;   
00115     Array<FixType> _fixtype; 
00116 
00117     // Loaded nodes
00118     Array<size_t>   _loaded; 
00119     Array<Vector3D> _loads;  
00120 
00121     // Private methods
00122     void _refine(); 
00123 
00124 }; // class Grid2D
00125 
00126 
00128 
00129 
00130 /* public */
00131 
00132 inline Grid2D::Grid2D(double xMin, double yMin, size_t nRow, size_t nCol, double Lx, double Ly, ptIsFixed pIsFixed)
00133     : _xmin     (xMin),
00134       _ymin     (yMin),
00135       _nrow     (nRow),
00136       _ncol     (nCol),
00137       _is_fixed (pIsFixed)
00138 {
00139     // Save lengths
00140     _L = Lx, Ly, 0.0;
00141 
00142     // Do generate grid
00143     _refine ();
00144 }
00145 
00146 inline bool Grid2D::IsInside(Vector3D const & P, bool WithBorder)
00147 {
00148     if (WithBorder)
00149     {
00150         if (P(0)>=_xmin+_L(0) && P(0)<=_xmax-_L(0) && P(1)>=_ymin+_L(1) && P(1)<=_ymax-_L(1)) return true;
00151         else                                                                                  return false;
00152     }
00153     else
00154     {
00155         if (P(0)>=_xmin && P(0)<=_xmax && P(1)>=_ymin && P(1)<=_ymax) return true;
00156         else                                                          return false;
00157     }       
00158 }
00159 
00160 inline void Grid2D::ClearState(bool CurrentCoords)
00161 {
00162     for (size_t i=0; i<_X.Size(); ++i)
00163     {
00164         if (CurrentCoords)
00165         _x   [i] = _X[i]; // nodes undeformed position
00166         _m   [i] = 0.0;   // nodes masses
00167         _q   [i] = 0.0;   // nodes momenta
00168         _v   [i] = 0.0;   // nodes velocities
00169         _fe  [i] = 0.0;   // nodes external forces
00170         _fi  [i] = 0.0;   // nodes internal forces
00171         _dqdt[i] = 0.0;   // nodes momenta rate
00172     }
00173 }
00174 
00175 inline void Grid2D::SetFixed(size_t i, FixType Type)
00176 {
00177     long k = _fixed.Find(i);
00178     if (k<0) // not added yet
00179     {
00180         _fixed  .Push(i);
00181         _fixtype.Push(Type);
00182     }
00183     else
00184     {
00185         _fixed  [k] = i;
00186         _fixtype[k] = Type;
00187     }
00188 }
00189 
00190 inline void Grid2D::SetLoading(size_t i, Vector3D const & Fe)
00191 {
00192     long k = _loaded.Find(i);
00193     if (k<0) // not added yet
00194     {
00195         _loaded.Push(i);
00196         _loads .Push(Fe);
00197     }
00198     else
00199     {
00200         _loaded[k] = i;
00201         _loads [k] = Fe;
00202     }
00203 }
00204 
00205 inline void Grid2D::FixNodes(Array<Vector3D> * VelOrDqDt)
00206 {
00207     for (size_t i=0; i<_fixed.Size(); ++i)
00208     {
00209         switch (_fixtype[i])
00210         {
00211             case FIX_X   : { (*VelOrDqDt)[_fixed[i]](0)=0.0;                                                                 break; }
00212             case FIX_Y   : { (*VelOrDqDt)[_fixed[i]](1)=0.0;                                                                 break; }
00213             case FIX_Z   : { (*VelOrDqDt)[_fixed[i]](2)=0.0;                                                                 break; }
00214             case FIX_XY  : { (*VelOrDqDt)[_fixed[i]](0)=0.0; (*VelOrDqDt)[_fixed[i]](1)=0.0;                                 break; }
00215             case FIX_YZ  : { (*VelOrDqDt)[_fixed[i]](1)=0.0; (*VelOrDqDt)[_fixed[i]](2)=0.0;                                 break; }
00216             case FIX_ZX  : { (*VelOrDqDt)[_fixed[i]](2)=0.0; (*VelOrDqDt)[_fixed[i]](0)=0.0;                                 break; }
00217             case FIX_XYZ : { (*VelOrDqDt)[_fixed[i]](0)=0.0; (*VelOrDqDt)[_fixed[i]](1)=0.0; (*VelOrDqDt)[_fixed[i]](2)=0.0; break; }
00218         }
00219     }
00220 }
00221 
00222 inline bool Grid2D::IsFixed(size_t n, size_t iD) const
00223 {
00224     long k = _fixed.Find(n);
00225     if (k<0) return false; // not fixed
00226     else
00227     {
00228         switch (_fixtype[k])
00229         {
00230             case FIX_X   : { if (iD==0)          return true; else return false; }
00231             case FIX_Y   : { if (iD==1)          return true; else return false; }
00232             case FIX_Z   : { if (iD==2)          return true; else return false; }
00233             case FIX_XY  : { if (iD==0 || iD==1) return true; else return false; }
00234             case FIX_YZ  : { if (iD==1 || iD==2) return true; else return false; }
00235             case FIX_ZX  : { if (iD==2 || iD==0) return true; else return false; }
00236             case FIX_XYZ : {                     return true;                    }
00237         }
00238     }
00239     return false;
00240 }
00241 
00242 inline void Grid2D::ApplyLoadings(double M)
00243 {
00244     for (size_t i=0; i<_loaded.Size(); ++i)
00245         _fe[_loaded[i]] = M*_loads[i];
00246 }
00247 
00248 inline void Grid2D::Refine()
00249 {
00250     // New geometry
00251     _nrow = 2*_nrow-1;
00252     _ncol = 2*_ncol-1;
00253     _L    = _L(0)/2.0, _L(1)/2.0, 0.0;
00254 
00255     // Do refine
00256     _refine();
00257 }
00258 
00259 inline size_t Grid2D::LBN(Vector3D const & P, bool MPM)
00260 {
00261     if (MPM) return static_cast<size_t>((P(0)-_xmin)/_L(0))   + (static_cast<size_t>((P(1)-_ymin)/_L(1))  )*_ncol;
00262     else     return static_cast<size_t>((P(0)-_xmin)/_L(0))-1 + (static_cast<size_t>((P(1)-_ymin)/_L(1))-1)*_ncol;
00263 }
00264 
00265 /* private */
00266 
00267 inline void Grid2D::_refine()
00268 {
00269     // Allocate nodes
00270     _X.Resize(_nrow*_ncol);
00271 
00272     // Set nodes coordinates
00273     size_t k = 0;
00274     for (size_t i=0; i<_nrow; ++i)
00275     for (size_t j=0; j<_ncol; ++j)
00276     {
00277         _X[k] = _xmin+j*_L(0),
00278                 _ymin+i*_L(1),
00279                 0.0;
00280         k++;
00281     }
00282     _xmax = _xmin+(_ncol-1)*_L(0);
00283     _ymax = _ymin+(_nrow-1)*_L(1);
00284 
00285     // Allocate and initialize (zero) nodes data
00286     _x   .Resize(_X.Size()); // nodes current coordinates
00287     _m   .Resize(_X.Size()); // nodes masses
00288     _q   .Resize(_X.Size()); // nodes momenta
00289     _v   .Resize(_X.Size()); // nodes velocities
00290     _fe  .Resize(_X.Size()); // nodes external forces
00291     _fi  .Resize(_X.Size()); // nodes internal forces
00292     _dqdt.Resize(_X.Size()); // nodes momenta rate
00293     ClearState();
00294 
00295     // Allocate cells connectivities
00296     _c.Resize((_nrow-1)*(_ncol-1));
00297 
00298     // Set connectivities
00299     for (size_t i=0; i<_nrow-1; ++i)
00300     {
00301         for (size_t j=0; j<_ncol-1; ++j)
00302         {
00303             _c[i*(_ncol-1)+j] = i   *_ncol+j  ,
00304                                 i   *_ncol+j+1,
00305                                (i+1)*_ncol+j+1,
00306                                (i+1)*_ncol+j  ;
00307         }
00308     }
00309 
00310     // Set current coordinates and boundary conditions
00311     _fixed  .Resize(0);
00312     _fixtype.Resize(0);
00313     FixType type;
00314     for (size_t i=0; i<_X.Size(); ++i)
00315     {
00316         _x[i] = _X[i];
00317         if ((*_is_fixed) (_X[i], type))
00318         {
00319             long k = _fixed.Find(i);
00320             if (k<0) // not added yet
00321             {
00322                 _fixed  .Push(i);
00323                 _fixtype.Push(type);
00324             }
00325             else
00326             {
00327                 _fixed  [k] = i;
00328                 _fixtype[k] = type;
00329             }
00330         }
00331     }
00332 }
00333 
00334 }; // namespace MPM
00335 
00336 #endif // MPM_GRID2D_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines