node.h

00001 /*************************************************************************************
00002  * MechSys - A C++ library to simulate (Continuum) Mechanical Systems                *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>       *
00004  * Copyright (C) 2005 Raul Dario Durand Farfan  <raul.durand at gmail.com>           *
00005  *                                                                                   *
00006  * This file is part of MechSys.                                                     *
00007  *                                                                                   *
00008  * MechSys is free software; you can redistribute it and/or modify it under the      *
00009  * terms of the GNU General Public License as published by the Free Software         *
00010  * Foundation; either version 2 of the License, or (at your option) any later        *
00011  * version.                                                                          *
00012  *                                                                                   *
00013  * MechSys is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   *
00015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.          *
00016  *                                                                                   *
00017  * You should have received a copy of the GNU General Public License along with      *
00018  * MechSys; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  * Fifth Floor, Boston, MA 02110-1301, USA                                           *
00020  *************************************************************************************/
00021 
00022 #ifndef MECHSYS_FEM_NODE_H
00023 #define MECHSYS_FEM_NODE_H
00024 
00025 #ifdef HAVE_CONFIG_H
00026   #include "config.h"
00027 #else
00028   #ifndef REAL
00029     #define REAL double
00030   #endif
00031 #endif
00032 
00033 #include <map>
00034 #include <sstream>
00035 
00036 #include "util/array.h"
00037 #include "util/string.h"
00038 #include "util/exception.h"
00039 #include "util/numstreams.h"
00040 #include "fem/node.h"
00041 
00042 using Util::_8s;
00043 using Util::_6;
00044 using Util::_a;
00045 
00046 namespace FEM
00047 {
00048 
00050 class Node
00051 {
00052     friend std::ostream & operator<< (std::ostream & os, FEM::Node const & node);
00053 public:
00054     Node() : _has_essential_vector(false), _has_natural_vector(false) {}
00055     struct DOFVarsStruct
00056     {
00057         REAL EssentialBry; // EssentialBry (U)
00058         REAL NaturalBry;   // NaturalBry (F)
00059         bool IsEssenPresc; // Is EssentialBry Prescribed?
00060         int  EqID;         // Position inside the system of equations
00061         REAL EssentialVal; // Essential value for the current state of this node
00062         REAL NaturalVal;   // Natural value for the current state of this node
00063         REAL _IncEssenVal; // (internal) In:  A given increment of essential value (temporary) Communication between Solver and Element
00064         REAL _IncNaturVal; // (internal) Out: A given increment of natural value (temporary) Communication between Solver and Element
00065         bool EssenPrescPersist; // Auxiliar variable only to perform persistent prescribed essential values of pwp (=0) on surfece after excavations (thats ugly and other solution must be reached in future)
00066     };
00067     // Methods
00068     void Initialize(int Number, REAL X, REAL Y, REAL Z);        // Initializes coordinates
00069     void ClearDOF();
00070     void AddDOF(String const & StrEssentialBry, String const & StrNaturalBry);
00071     bool IsEssential(String const & Name) const;
00072     bool HasVar     (String const & Name) const;
00073     DOFVarsStruct       & DOFVar(String const & Name);
00074     DOFVarsStruct const & DOFVar(String const & Name) const;
00075     Array<Node::DOFVarsStruct> & DOFVars() { return _dof_vars; }
00076     std::string OutMap();
00077     int  Number() const { return _number; }
00078     REAL X() const { return _x; }
00079     REAL Y() const { return _y; }
00080     REAL Z() const { return _z; }
00081     int & Refs() { return _refs; };
00082     int const & Refs() const { return _refs; };
00083     void ClearBryValues() // Just boundary information => do NOT change EssentialVal and NaturalVal (U and F values)
00084     {
00085         for (size_t i=0; i<_dof_vars.size(); ++i)
00086         {
00087             // Default => (ux=?,fx=0,False,-1)
00088             _dof_vars[i].EssentialBry = 0.0;
00089               _dof_vars[i].NaturalBry = 0.0;
00090             _dof_vars[i].IsEssenPresc = false;
00091                     _dof_vars[i].EqID = -1;
00092             if (_dof_vars[i].EssenPrescPersist) _dof_vars[i].IsEssenPresc = true; // that is ugly (other solution must be reached for pwp after excavations)
00093         }
00094     }
00095     String Out(bool PrintCaptionOnly=false) const;
00096     // Vectors
00097     void SetEssentialVector (String const & UX, String const & UY, String const & UZ);
00098     void SetNaturalVector   (String const & FX, String const & FY, String const & FZ);
00099     void OutEssentialVector (String & Str) const;
00100     void OutNaturalVector   (String & Str) const;
00101     bool HasEssentialVector () const { return _has_essential_vector; }
00102     bool HasNaturalVector   () const { return _has_natural_vector;   }
00103     int  nDOF() const { return _dof_vars.size(); }
00104 private:
00105     // Auxiliar structures
00106     struct DOFInfo
00107     {
00108         size_t ArrayPos;    // Position inside the array of DOF variables
00109         bool   IsEssential; // Is essential? false => natural
00110     };
00111     // Data
00112     int  _number;
00113     int  _refs;
00114     REAL _x;
00115     REAL _y;
00116     REAL _z;
00117     std::map<String, DOFInfo, std::less<String> > _dof_map;
00118     Array<DOFVarsStruct>                          _dof_vars;
00119     // Vectors
00120     String _UX;
00121     String _UY;
00122     String _UZ;
00123     String _FX;
00124     String _FY;
00125     String _FZ;
00126     bool   _has_essential_vector;
00127     bool   _has_natural_vector;
00128 }; // class Node
00129 
00130 std::ostream & operator<< (std::ostream & os, FEM::Node::DOFVarsStruct const & Vars);
00131 std::ostream & operator<< (std::ostream & os, Array<FEM::Node::DOFVarsStruct> const & AVars);
00132 std::ostream & operator<< (std::ostream & os, FEM::Node const & node);
00133 std::ostream & operator<< (std::ostream & os, Array<FEM::Node> const & nodes);
00134 
00135 
00137 
00138 
00139 inline void Node::Initialize(int Number, REAL X, REAL Y, REAL Z) // {{{
00140 {
00141     _number = Number;
00142     _x = X;
00143     _y = Y;
00144     _z = Z;
00145     _refs = 0;
00146 }
00147 
00148 // }}}
00149 
00150 inline void Node::ClearDOF()
00151 {
00152     _dof_vars.resize(0);
00153     _dof_map.clear();
00154 }
00155 
00156 inline void Node::AddDOF(String const & StrEssentialBry, String const & StrNaturalBry) // {{{
00157 {
00158     // Check if StrEssentialBry was not already added
00159     if (_dof_map.find(StrEssentialBry)==_dof_map.end())
00160     {
00161         // Get initial size of _dof_map
00162         size_t n_dof = _dof_map.size()/2;
00163 
00164         // Add EssentialBry DOF to DOFMap
00165         DOFInfo          dof_info = {n_dof,true}; // {ArrayPos,IsEssential}
00166         _dof_map[StrEssentialBry] = dof_info;
00167 
00168         // Add NaturalBry DOF to DOFMap
00169               dof_info.ArrayPos = n_dof;
00170            dof_info.IsEssential = false;
00171         _dof_map[StrNaturalBry] = dof_info;
00172 
00173         // Setup _dof_vars (Degree of Freedom nodal variables). IEP=Is Essential Prescribed?
00174         DOFVarsStruct dof_struct = {0,0,false,-1,0,0,0,0,false}; // {u_bry,f_bry,IEP,EqID,U,F,dU,dF}   Default => (ux=?,fx=0,false,-1,0,0,0,0)
00175         _dof_vars.push_back(dof_struct);
00176     }
00177 }
00178 
00179 // }}}
00180 
00181 inline bool Node::IsEssential(String const & Name) const // {{{
00182 {
00183     std::map<String, DOFInfo>::const_iterator it = _dof_map.find(Name);
00184     if (it==_dof_map.end())
00185         throw new Fatal(_("Node::IsEssential: Could not find DOF variable name < %s > inside Node"), Name.c_str());
00186     return it->second.IsEssential;
00187 }
00188 
00189 // }}}
00190 
00191 inline bool Node::HasVar(String const & Name) const // {{{
00192 {
00193     std::map<String, DOFInfo>::const_iterator it = _dof_map.find(Name);
00194     if (it==_dof_map.end()) return false;
00195     return true;
00196 }
00197 
00198 // }}}
00199 
00200 inline Node::DOFVarsStruct & Node::DOFVar(String const & Name) // {{{
00201 {
00202     // Find Name
00203     std::map<String, DOFInfo>::const_iterator it = _dof_map.find(Name);
00204     if (it==_dof_map.end())
00205         throw new Fatal(_("Node::DOFVarsStruct: Node coords=<%.6f; %.6f; %.6f>. Could not find DOF variable name < %s > inside Node"), _x, _y, _z, Name.c_str());
00206 
00207     // Return a reference to a DOFVarsStruct
00208     return _dof_vars[ it->second.ArrayPos ];
00209 }
00210 
00211 // }}}
00212 
00213 inline Node::DOFVarsStruct const & Node::DOFVar(String const & Name) const // {{{
00214 {
00215     // Find Name
00216     std::map<String, DOFInfo>::const_iterator it = _dof_map.find(Name);
00217     if (it==_dof_map.end())
00218         throw new Fatal(_("Node::DOFVarsStruct: Node coords=<%.6f; %.6f; %.6f>. Could not find DOF variable name < %s > inside Node"), _x, _y, _z, Name.c_str());
00219 
00220     // Return a reference to a DOFVarsStruct
00221     return _dof_vars[ it->second.ArrayPos ];
00222 }
00223 
00224 // }}}
00225 
00226 inline std::string Node::OutMap() // {{{
00227 {
00228     std::ostringstream oss;
00229     std::map<String, DOFInfo>::const_iterator it;
00230     for (it=_dof_map.begin(); it!=_dof_map.end(); ++it)
00231     {
00232         oss << _6()<< it->first << _8s()<< it->second.ArrayPos << _a()<< it->second.IsEssential << std::endl;
00233     }
00234     return oss.str();
00235 }
00236 
00237 // }}}
00238 
00239 inline String Node::Out(bool PrintCaptionOnly) const // {{{
00240 {
00241     // Auxiliar variables
00242     std::ostringstream oss;
00243     std::map<String, DOFInfo>::const_iterator it;
00244 
00245     // Print caption
00246     if (PrintCaptionOnly)
00247     {
00248         // Print essential var name
00249         for (it=_dof_map.begin(); it!=_dof_map.end(); ++it)
00250             if (it->second.IsEssential==true)
00251                 oss << _8s()<< it->first;
00252 
00253         // Print natural var name
00254         for (it=_dof_map.begin(); it!=_dof_map.end(); ++it)
00255             if (it->second.IsEssential==false)
00256                 oss << _8s()<< it->first;
00257 
00258         oss << std::endl;
00259     }
00260     else
00261     {
00262         // Print essential values
00263         for (it=_dof_map.begin(); it!=_dof_map.end(); ++it)
00264             if (it->second.IsEssential==true)
00265                 oss << _8s()<< _dof_vars[ it->second.ArrayPos ].EssentialVal ;
00266 
00267         // Print natural values
00268         for (it=_dof_map.begin(); it!=_dof_map.end(); ++it)
00269             if (it->second.IsEssential==false)
00270                 oss << _8s()<< _dof_vars[ it->second.ArrayPos ].NaturalVal ;
00271 
00272         oss << std::endl;
00273     }
00274 
00275     return oss.str(); 
00276 } // }}}
00277 
00278 inline void Node::SetEssentialVector(String const & UX, String const & UY, String const & UZ) // {{{
00279 {
00280     _UX = UX;
00281     _UY = UY;
00282     _UZ = UZ;
00283     _has_essential_vector = true;
00284 } // }}}
00285 
00286 inline void Node::SetNaturalVector(String const & FX, String const & FY, String const & FZ) // {{{
00287 {
00288     _FX = FX;
00289     _FY = FY;
00290     _FZ = FZ;
00291     _has_natural_vector = true;
00292 } // }}}
00293 
00294 inline void Node::OutEssentialVector(String & Str) const // {{{
00295 {
00296     if (_has_essential_vector)
00297     {
00298         std::map<String, DOFInfo>::const_iterator it;
00299         it=_dof_map.find(_UX); REAL ux=_dof_vars[ it->second.ArrayPos ].EssentialVal;
00300         it=_dof_map.find(_UY); REAL uy=_dof_vars[ it->second.ArrayPos ].EssentialVal;
00301         it=_dof_map.find(_UZ); REAL uz=_dof_vars[ it->second.ArrayPos ].EssentialVal;
00302         Str.Printf(_(" %e %e %e "),ux,uy,uz);
00303     }
00304     else Str.Printf(_(" 0 0 0 "));
00305 
00306 } // }}}
00307 
00308 inline void Node::OutNaturalVector(String & Str) const // {{{
00309 {
00310     if (_has_natural_vector)
00311     {
00312         std::map<String, DOFInfo>::const_iterator it;
00313         it=_dof_map.find(_FX); REAL fx=_dof_vars[ it->second.ArrayPos ].NaturalVal;
00314         it=_dof_map.find(_FY); REAL fy=_dof_vars[ it->second.ArrayPos ].NaturalVal;
00315         it=_dof_map.find(_FZ); REAL fz=_dof_vars[ it->second.ArrayPos ].NaturalVal;
00316         Str.Printf(_(" %e %e %e "),fx,fy,fz);
00317     }
00318     else Str.Printf(_(" 0 0 0 "));
00319     
00320 } // }}}
00321 
00322 // ------------------------- operator << {{{
00323 
00324 std::ostream & operator<< (std::ostream & os, FEM::Node::DOFVarsStruct const & V)
00325 {
00326     os << _8s()<< V.EssentialBry << _8s()<< V.NaturalBry << _a()<< V.IsEssenPresc << _6()<< V.EqID << _8s()<< V.EssentialVal << _8s()<< V.NaturalVal;
00327     return os;
00328 }
00329 
00330 std::ostream & operator<< (std::ostream & os, Array<FEM::Node::DOFVarsStruct> const & AVars)
00331 {
00332     for (size_t i=0; i<AVars.size(); ++i)
00333         os << AVars[i] << std::endl;
00334     return os;
00335 }
00336 
00337 std::ostream & operator<< (std::ostream & os, FEM::Node const & node)
00338 {
00339     os << _8s()<< node._x << _8s()<< node._y << _8s()<< node._z << std::endl;
00340     for (size_t i=0; i<node._dof_vars.size(); ++i)
00341     {
00342         os << node._dof_vars[i];
00343         if (i!=node._dof_vars.size()-1) os << std::endl;
00344     }
00345     os << std::endl;
00346     return os;
00347 }
00348 
00349 std::ostream & operator<< (std::ostream & os, Array<FEM::Node> const & nodes)
00350 {
00351     os << _("Number of nodes = ") << nodes.size() << std::endl;
00352     for (size_t i=0; i<nodes.size(); ++i)
00353         os << _6()<< i+1 << nodes[i];
00354     return os;
00355 }
00356 
00357 // }}}
00358 
00359 }; //namespace FEM
00360 
00361 #endif // MECHSYS_FEM_NODE_H
00362 
00363 // vim:fdm=marker

Generated on Wed Jan 24 15:56:25 2007 for MechSys by  doxygen 1.4.7