MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/models/flowstate.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_FLOWSTATE_H
00021 #define MECHSYS_FLOWSTATE_H
00022 
00023 // Std Lib
00024 #include <iostream>
00025 #include <cmath>   // for sqrt
00026 
00027 // MechSys
00028 #include <mechsys/models/model.h>
00029 #include <mechsys/linalg/matvec.h>
00030 
00031 class FlowState : public State
00032 {
00033 public:
00034     // Constructor
00035     FlowState (int NDim);
00036 
00037     // Methods
00038     void   Init    (SDPair const & Ini, size_t NIvs=0);
00039     void   Backup  () { VelBkp=Vel; GraBkp=Gra; IvsBkp=Ivs; }
00040     void   Restore () { Vel=VelBkp; Gra=GraBkp; Ivs=IvsBkp; }
00041     size_t PckSize () const { return 2*size(Vel)+size(Ivs); }
00042     void   Pack    (Array<double>       & V) const;
00043     void   Unpack  (Array<double> const & V);
00044     void   Output  (SDPair & KeysVals) const;
00045 
00046     // Data
00047     Vec_t Vel, VelBkp; 
00048     Vec_t Gra, GraBkp; 
00049     Vec_t Ivs, IvsBkp; 
00050 };
00051 
00052 
00054 
00055 
00056 inline FlowState::FlowState (int NDim)
00057     : State(NDim)
00058 {
00059     Vel   .change_dim(NDim);  set_to_zero(Vel   );
00060     Gra   .change_dim(NDim);  set_to_zero(Gra   );
00061     VelBkp.change_dim(NDim);  set_to_zero(VelBkp);
00062     GraBkp.change_dim(NDim);  set_to_zero(GraBkp);
00063 }
00064 
00065 inline void FlowState::Init (SDPair const & Ini, size_t NIvs)
00066 {
00067     if (Ini.HasKey("vx")) Vel(0) = Ini("vx");
00068     if (Ini.HasKey("vy")) Vel(1) = Ini("vy");
00069     if (num_rows(Vel)>2)
00070     {
00071         if (Ini.HasKey("vz")) Vel(2) = Ini("vz");
00072     }
00073     else
00074     {
00075         bool error = false;
00076         String key;
00077         if (Ini.HasKey("vz")) { error=true; key="vz"; }
00078         if (error) throw new Fatal("FlowState::Init: For a 2D state, there are only 4 stress components. %s is not available",key.CStr());
00079     }
00080     if (NIvs>0)
00081     {
00082         Ivs.change_dim (NIvs);
00083         set_to_zero (Ivs);
00084         for (size_t i=0; i<NIvs; ++i)
00085         {
00086             String buf;
00087             buf.Printf ("z%d",i);
00088             if (Ini.HasKey(buf)) Ivs(i) = Ini(buf);
00089         }
00090         IvsBkp.change_dim (NIvs);
00091         IvsBkp = Ivs;
00092     }
00093 }
00094 
00095 inline void FlowState::Pack (Array<double> & V) const
00096 {
00097     size_t nrw = size(Vel);
00098     size_t niv = size(Ivs);
00099     V.Resize (2*nrw + niv);
00100     for (size_t i=0; i<nrw; ++i)
00101     {
00102         V[    i] = Vel(i);
00103         V[nrw+i] = Gra(i);
00104     }
00105     for (size_t i=0; i<niv; ++i) V[2*nrw+i] = Ivs(i);
00106 }
00107 
00108 inline void FlowState::Unpack (Array<double> const & V)
00109 {
00110     if (V.Size()!=PckSize()) throw new Fatal("FlowState::Unpack: Size of given vector (%zd) is different of correct size of Pack (%zd)",V.Size(),PckSize());
00111     size_t nrw = size(Vel);
00112     size_t niv = size(Ivs);
00113     for (size_t i=0; i<nrw; ++i)
00114     {
00115         Vel(i) = V[    i];
00116         Gra(i) = V[nrw+i];
00117     }
00118     for (size_t i=0; i<niv; ++i) Ivs(i) = V[2*nrw+i];
00119 }
00120 
00121 inline void FlowState::Output (SDPair & KeysVals) const
00122 {
00123     size_t ndim = size(Vel);
00124     if (ndim==2)
00125     {
00126         KeysVals.Set("vx vy  gx gy",
00127                      Vel(0), Vel(1),
00128                      Gra(0), Gra(1));
00129     }
00130     else
00131     {
00132         KeysVals.Set("vx vy vz  gx gy gz",
00133                      Vel(0), Vel(1), Vel(2),
00134                      Gra(0), Gra(1), Gra(2));
00135     }
00136 }
00137 
00138 
00139 #endif // MECHSYS_FLOWSTATE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines