MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/models/equilibstate.h
Go to the documentation of this file.
00001 /************************************************************************
00002  * MechSys - Open Library for Mechanical Systems                        *
00003  * Copyright (C) 2005 Dorival M. Pedroso, Raul Durand                   *
00004  * Copyright (C) 2009 Sergio Galindo                                    *
00005  *                                                                      *
00006  * This program is free software: you can redistribute it and/or modify *
00007  * it under the terms of the GNU General Public License as published by *
00008  * the Free Software Foundation, either version 3 of the License, or    *
00009  * any later version.                                                   *
00010  *                                                                      *
00011  * This program is distributed in the hope that it will be useful,      *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
00014  * GNU General Public License for more details.                         *
00015  *                                                                      *
00016  * You should have received a copy of the GNU General Public License    *
00017  * along with this program. If not, see <http://www.gnu.org/licenses/>  *
00018  ************************************************************************/
00019 
00020 #ifndef MECHSYS_EQUILIBSTATE_H
00021 #define MECHSYS_EQUILIBSTATE_H
00022 
00023 // Std Lib
00024 #include <iostream>
00025 #include <cmath>   // for sqrt
00026 
00027 // MechSys
00028 #include <mechsys/util/string.h>
00029 #include <mechsys/models/model.h>
00030 #include <mechsys/linalg/matvec.h>
00031 
00032 class EquilibState : public State
00033 {
00034 public:
00035     // static
00036     static Array<String> Keys;
00037 
00038     // Constructor
00039     EquilibState (int NDim);
00040 
00041     // Methods
00042     void   Init    (SDPair const & Ini, size_t NIvs=0);
00043     void   Backup  () { SigBkp=Sig; EpsBkp=Eps; IvsBkp=Ivs; LdgBkp=Ldg; }
00044     void   Restore () { Sig=SigBkp; Eps=EpsBkp; Ivs=IvsBkp; Ldg=LdgBkp; }
00045     size_t PckSize () const { return 2*size(Sig)+size(Ivs)+1; }
00046     void   Pack    (Array<double>       & V) const;
00047     void   Unpack  (Array<double> const & V);
00048 
00049     // Auxiliary methods
00050     void Output (SDPair & KeysVals) const;
00051     void Output (std::ostream & os, bool WithHeader=false, char const * NF="%16.8e") const;
00052 
00053     // Operators
00054     void operator= (EquilibState const & Another);
00055 
00056     // Data
00057     Vec_t Sig, SigBkp; 
00058     Vec_t Eps, EpsBkp; 
00059     Vec_t Ivs, IvsBkp; 
00060     bool  Ldg, LdgBkp; 
00061 
00062     Array<Vec_t> dSigdt;
00063 };
00064 
00065 Array<String> EquilibState::Keys;
00066 
00067 
00069 
00070 
00071 inline EquilibState::EquilibState (int NDim)
00072     : State(NDim), Ldg(false), LdgBkp(false)
00073 {
00074     int ncomp = NDim*2; // number of stress/strain components
00075     Sig   .change_dim(ncomp);  set_to_zero(Sig   );
00076     Eps   .change_dim(ncomp);  set_to_zero(Eps   );
00077     SigBkp.change_dim(ncomp);  set_to_zero(SigBkp);
00078     EpsBkp.change_dim(ncomp);  set_to_zero(EpsBkp);
00079 
00080     if (Keys.Size()==0)
00081     {
00082         Keys.Resize (2*ncomp+4);
00083         if (NDim==2) 
00084         {
00085             Keys = "sx",   "sy",   "sz", "sxy",
00086                    "ex",   "ey",   "ez", "exy",
00087                    "pcam", "qcam", "ev", "ed";
00088         }
00089         else
00090         {
00091             Keys = "sx",   "sy",   "sz", "sxy", "syz", "szx",
00092                    "ex",   "ey",   "ez", "exy", "eyz", "ezx",
00093                    "pcam", "qcam", "ev", "ed";
00094         }
00095     }
00096 }
00097 
00098 inline void EquilibState::Init (SDPair const & Ini, size_t NIvs)
00099 {
00100     set_to_zero (Sig);
00101     set_to_zero (Eps);
00102 
00103     if (Ini.HasKey("sx"))  Sig(0) = Ini("sx");
00104     if (Ini.HasKey("sy"))  Sig(1) = Ini("sy");
00105     if (Ini.HasKey("sz"))  Sig(2) = Ini("sz");
00106     if (Ini.HasKey("sxy")) Sig(3) = Ini("sxy")*sqrt(2.0);
00107     if (num_rows(Sig)>4)
00108     {
00109         if (Ini.HasKey("syz")) Sig(4) = Ini("syz")*sqrt(2.0);
00110         if (Ini.HasKey("sxz")) Sig(5) = Ini("sxz")*sqrt(2.0);
00111     }
00112     else
00113     {
00114         bool error = false;
00115         String key;
00116         if (Ini.HasKey("syz")) { error=true; key="syz"; }
00117         if (Ini.HasKey("sxz")) { error=true; key="sxz"; }
00118         if (error) throw new Fatal("EquilibState::Init: For a 2D state, there are only 4 stress components. %s is not available",key.CStr());
00119     }
00120     SigBkp = Sig;
00121     EpsBkp = Eps;
00122 
00123     if (NIvs>0)
00124     {
00125         Ivs.change_dim (NIvs);
00126         set_to_zero (Ivs);
00127         for (size_t i=0; i<NIvs; ++i)
00128         {
00129             String buf;
00130             buf.Printf ("z%d",i);
00131             if (Ini.HasKey(buf)) Ivs(i) = Ini(buf);
00132         }
00133         IvsBkp.change_dim (NIvs);
00134         IvsBkp = Ivs;
00135     }
00136 }
00137 
00138 inline void EquilibState::Pack (Array<double> & V) const
00139 {
00140     size_t ncp = size(Sig);
00141     size_t niv = size(Ivs);
00142     V.Resize (2*ncp + niv + 1);
00143     for (size_t i=0; i<ncp; ++i)
00144     {
00145         V[    i] = Sig(i);
00146         V[ncp+i] = Eps(i);
00147     }
00148     for (size_t i=0; i<niv; ++i) V[2*ncp+i] = Ivs(i);
00149     V[2*ncp + niv] = static_cast<double>(Ldg);
00150 }
00151 
00152 inline void EquilibState::Unpack (Array<double> const & V)
00153 {
00154     if (V.Size()!=PckSize()) throw new Fatal("EquilibState::Unpack: Size of given vector (%zd) is different of correct size of Pack (%zd)",V.Size(),PckSize());
00155     size_t ncp = size(Sig);
00156     size_t niv = size(Ivs);
00157     for (size_t i=0; i<ncp; ++i)
00158     {
00159         Sig(i) = V[    i];
00160         Eps(i) = V[ncp+i];
00161     }
00162     for (size_t i=0; i<niv; ++i) Ivs(i) = V[2*ncp+i];
00163     Ldg = static_cast<bool>(V[2*ncp + niv]);
00164 }
00165 
00166 inline void EquilibState::Output (SDPair & KeysVals) const
00167 {
00168     size_t ncp = size(Sig);
00169     if (ncp==4)
00170     {
00171         KeysVals.Set("sx sy sz sxy  ex ey ez exy  pcam qcam  ev ed",
00172                      Sig(0), Sig(1), Sig(2), Sig(3)/Util::SQ2,
00173                      Eps(0), Eps(1), Eps(2), Eps(3)/Util::SQ2,
00174                      Calc_pcam(Sig), Calc_qcam(Sig), Calc_ev(Eps), Calc_ed(Eps));
00175     }
00176     else
00177     {
00178         KeysVals.Set("sx sy sz sxy syz szx  ex ey ez exy eyz ezx  pcam qcam  ev ed",
00179                      Sig(0), Sig(1), Sig(2), Sig(3)/Util::SQ2, Sig(4)/Util::SQ2, Sig(5)/Util::SQ2,
00180                      Eps(0), Eps(1), Eps(2), Eps(3)/Util::SQ2, Eps(4)/Util::SQ2, Eps(5)/Util::SQ2,
00181                      Calc_pcam(Sig), Calc_qcam(Sig), Calc_ev(Eps), Calc_ed(Eps));
00182     }
00183 }
00184 
00185 inline void EquilibState::Output (std::ostream & os, bool WithHeader, char const * NF) const
00186 {
00187     size_t ncp = size(Sig);
00188     size_t niv = size(Ivs);
00189     String buf;
00190     if (WithHeader)
00191     {
00192         String nf, str;
00193         nf.TextFmt(NF);
00194         if (ncp>4)
00195         {
00196             str.Printf(nf.CStr(),"sx");  buf.append(str);
00197             str.Printf(nf.CStr(),"sy");  buf.append(str);
00198             str.Printf(nf.CStr(),"sz");  buf.append(str);
00199             str.Printf(nf.CStr(),"sxy"); buf.append(str);
00200             str.Printf(nf.CStr(),"syz"); buf.append(str);
00201             str.Printf(nf.CStr(),"szx"); buf.append(str);
00202             str.Printf(nf.CStr(),"ex");  buf.append(str);
00203             str.Printf(nf.CStr(),"ey");  buf.append(str);
00204             str.Printf(nf.CStr(),"ez");  buf.append(str);
00205             str.Printf(nf.CStr(),"exy"); buf.append(str);
00206             str.Printf(nf.CStr(),"eyz"); buf.append(str);
00207             str.Printf(nf.CStr(),"ezx"); buf.append(str);
00208         }
00209         else
00210         {
00211             str.Printf(nf.CStr(),"sx");  buf.append(str);
00212             str.Printf(nf.CStr(),"sy");  buf.append(str);
00213             str.Printf(nf.CStr(),"sz");  buf.append(str);
00214             str.Printf(nf.CStr(),"sxy"); buf.append(str);
00215             str.Printf(nf.CStr(),"ex");  buf.append(str);
00216             str.Printf(nf.CStr(),"ey");  buf.append(str);
00217             str.Printf(nf.CStr(),"ez");  buf.append(str);
00218             str.Printf(nf.CStr(),"exy"); buf.append(str);
00219         }
00220         for (size_t i=0; i<niv; ++i)
00221         { 
00222             str.Printf("z%d",i);
00223             str.Printf(nf.CStr(), str.CStr()); buf.append(str);
00224         }
00225         os << buf << "\n";
00226     }
00227     for (size_t i=0; i<ncp; ++i) { buf.Printf(NF,Sig(i)); os<<buf; }
00228     for (size_t i=0; i<ncp; ++i) { buf.Printf(NF,Eps(i)); os<<buf; }
00229     for (size_t i=0; i<niv; ++i) { buf.Printf(NF,Ivs(i)); os<<buf; }
00230     os << "\n";
00231 }
00232 
00233 inline void EquilibState::operator= (EquilibState const & A)
00234 {
00235     Sig = A.Sig;  SigBkp = A.SigBkp;
00236     Eps = A.Eps;  EpsBkp = A.EpsBkp;
00237     Ivs = A.Ivs;  IvsBkp = A.IvsBkp;
00238     Ldg = A.Ldg;  LdgBkp = A.LdgBkp;
00239 }
00240 
00241 
00242 #endif // MECHSYS_EQUILIBSTATE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines