MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/models/model.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_MODEL_H
00021 #define MECHSYS_MODEL_H
00022 
00023 // Std Lib
00024 #include <iostream>
00025 #include <fstream>
00026 
00027 // MechSys
00028 #include <mechsys/geomtype.h>
00029 #include <mechsys/util/maps.h>
00030 #include <mechsys/util/fatal.h>
00031 #include <mechsys/linalg/matvec.h>
00032 #include <mechsys/numerical/odesolver.h>
00033 
00034 class State
00035 {
00036 public:
00037     State (int NDim) {}
00038     virtual ~State () {}
00039     virtual void   Init    (SDPair const & Ini, size_t NIvs=0) =0;
00040     virtual void   Backup  () =0;
00041     virtual void   Restore () =0;
00042     virtual size_t PckSize ()                        const =0; 
00043     virtual void   Pack    (Array<double>       & V) const =0; 
00044     virtual void   Unpack  (Array<double> const & V)       =0; 
00045     virtual void   Output  (SDPair & KeysVals)       const =0; 
00046 };
00047 
00048 #include <mechsys/models/equilibstate.h>
00049 
00050 class Model
00051 {
00052 public:
00053     // static
00054     static Vec_t I;     
00055     static Mat_t Psd;   
00056     static Mat_t IdyI;  
00057 
00058     // Constructor & Destructor
00059     Model (int NDim, SDPair const & Prms, size_t NIvs=0, char const * Name="__unnamed_model__"); 
00060     virtual ~Model () {}
00061 
00062     // Methods
00063     virtual void   InitIvs      (SDPair const & Ini, State * Sta)                              const =0;
00064     virtual void   Rate         (State const * Sta, Vec_t const & DEpsDt, Vec_t & DSigDt, Vec_t & DIvsDt) const { throw new Fatal("Model::Rate: This method is not available in this model (%s)",Name.CStr()); }
00065     virtual void   TgIncs       (State const * Sta, Vec_t & DEps, Vec_t & DSig, Vec_t & DIvs)  const { throw new Fatal("Model::TgIncs: This method is not available in this model (%s)",Name.CStr()); }
00066     virtual void   UpdateSta    (ATensor2 const & F, State * Sta)                              const { throw new Fatal("Model::UpdateSta: This method is not available in this model (%s)",Name.CStr()); }
00067     virtual void   InvTgIncs    (State const * Sta, Vec_t & DSig, Vec_t & DEps, Vec_t & DIvs)  const { throw new Fatal("Model::InvTgIncs: This method is not available in this model (%s)",Name.CStr()); }
00068     virtual void   Stiffness    (State const * Sta, Mat_t & D)                                 const { throw new Fatal("Model::Stiffness: This method is not available in this model (%s)",Name.CStr()); }
00069     virtual void   Stiffness    (State const * Sta, Mat_t & D, Vec_t & Dw)                     const { throw new Fatal("Model::Stiffness(Dw): This method is not available in this model (%s)",Name.CStr()); }
00070     virtual void   Hydraulic    (State const * Sta, Mat_t & Kw, double & ChiW, double & InvQs) const { throw new Fatal("Model::Hydraulic: This method is not available in this model (%s)",Name.CStr()); }
00071     virtual bool   LoadCond     (State const * Sta, Vec_t const & DEps, double & alpInt)       const { alpInt=-1; return false; }
00072     virtual size_t CorrectDrift (State * Sta)                                                  const { return 0; }
00073     virtual size_t CorrectDrift (State * Sta, double & pw)                                     const { return 0; }
00074     virtual void   UpdatePath   (State const * Sta, Vec_t const & DEps, Vec_t const & DSig)    const {}
00075 
00076     // Methods: HydroMech
00077     virtual void   TgIncs       (State const * Sta, double pw, Vec_t & DEps, double Dpw, Vec_t & DSig, Vec_t & DIvs)  const { throw new Fatal("Model::TgIncs(HydroMech): This method is not available in this model (%s)",Name.CStr()); }
00078     virtual void   Stiffness    (State const * Sta, double pw, Mat_t & D, Vec_t & d)                                  const { throw new Fatal("Model::Stiffness(HydroMech): This method is not available in this model (%s)",Name.CStr()); }
00079     virtual bool   LoadCond     (State const * Sta, double pw, Vec_t const & DEps, double Dpw)                        const { throw new Fatal("Model::LoadCond(HydroMech): This method is not available in this model (%s)",Name.CStr()); }
00080 
00081     // Data
00082     int           NDim;    
00083     SDPair        Prms;    
00084     GeomType      GTy;     
00085     String        Name;    
00086     size_t        NCps;    
00087     size_t        NIvs;    
00088     Array<String> IvNames; 
00089     bool          HMCoup;  
00090 
00091     // Data to be set by derived classes
00092     bool UseUpdateSta; 
00093 
00094     // General constants
00095     double Grav;     
00096     double GamW;     
00097     double Rho;      
00098     double RhoS;     
00099     double Por;      
00100     double GamNat;   
00101     double GamSat;   
00102 
00103 #define STRESSUPDATE_DECLARE
00104     #include <mechsys/models/stressupdate.h>
00105     mutable StressUpdate SUp;
00106 #undef STRESSUPDATE_DECLARE
00107 };
00108 
00109 Vec_t Model::I;
00110 Mat_t Model::Psd;
00111 Mat_t Model::IdyI;
00112 
00113 
00115 
00116 
00117 inline Model::Model (int TheNDim, SDPair const & ThePrms, size_t NIv, char const * TheName)
00118     : NDim(TheNDim), Prms(ThePrms), GTy(SDPairToGType(ThePrms,(TheNDim==3?"d3d":"d2d"))), 
00119       Name(TheName), NCps(2*NDim),  NIvs(NIv), HMCoup(false), UseUpdateSta(false)
00120 {
00121     // constants
00122     Grav   = (Prms.HasKey("grav")   ? Prms("grav")   : -1);
00123     GamW   = (Prms.HasKey("gamW")   ? Prms("gamW")   : -1);
00124     Rho    = (Prms.HasKey("rho")    ? Prms("rho")    : -1);
00125     RhoS   = (Prms.HasKey("rhoS")   ? Prms("rhoS")   : -1);
00126     Por    = (Prms.HasKey("por")    ? Prms("por")    : -1);
00127     GamNat = (Prms.HasKey("gamNat") ? Prms("gamNat") : -1);
00128     GamSat = (Prms.HasKey("gamSat") ? Prms("gamSat") : -1);
00129     if (Prms.HasKey("gamNat") && !Prms.HasKey("gamSat")) GamSat = GamNat;
00130     if (Prms.HasKey("gamSat") && !Prms.HasKey("gamNat")) GamNat = GamSat;
00131     if (Prms.HasKey("rhoS"))
00132     {
00133         double rho_w = Prms("gamW") / Prms("grav");
00134         double n     = Prms("por");
00135         Rho = n*rho_w + (1.0-n)*Prms("rhoS");
00136         if (!Prms.HasKey("gamNat")) GamNat = Rho * Grav;
00137         if (!Prms.HasKey("gamSat")) GamSat = GamNat;
00138     }
00139 
00140     // stress update
00141     SUp.SetModel   (this);
00142     IvNames.Resize (NIv);
00143 
00144     // static variables
00145     if (size(I)==0)
00146     {
00147         I.change_dim(NCps);
00148         I(0) = 1.;
00149         I(1) = 1.;
00150         I(2) = 1.;
00151         Calc_Psd  (NCps, Psd);
00152         Calc_IdyI (NCps, IdyI);
00153     }
00154 }
00155 
00156 std::ostream & operator<< (std::ostream & os, Model const & D)
00157 {
00158     os << D.Name << " " << D.NDim << "D " << GTypeToStr(D.GTy) << std::endl;
00159     os << "  Prms = {" << D.Prms << "}\n";
00160     os << "  Gravity                               : grav   = " << D.Grav   << std::endl;
00161     os << "  Unit weight of water                  : gamW   = " << D.GamW   << std::endl;
00162     os << "  Density of the material               : rho    = " << D.Rho    << std::endl;
00163     os << "  Density of solids                     : rhoS   = " << D.RhoS   << std::endl;
00164     os << "  Porosity of material                  : por    = " << D.Por    << std::endl;
00165     os << "  Natural unit weight of geo-material   : gamNat = " << D.GamNat << std::endl;
00166     os << "  Saturated unit weight of geo-material : gamSat = " << D.GamSat << std::endl;
00167     return os;
00168 }
00169 
00170 
00171 #define STRESSUPDATE_IMPLEMENT
00172   #include <mechsys/models/stressupdate.h>
00173 #undef STRESSUPDATE_IMPLEMENT
00174 
00175 
00177 
00178 
00179 SDPair MODEL; 
00180 
00181 typedef std::map<String,Array<String> > Str2ArrayStr_t; 
00182 
00183 Str2ArrayStr_t MODEL_PRM_NAMES; 
00184 Str2ArrayStr_t MODEL_IVS_NAMES; 
00185 
00186 typedef Model * (*ModelMakerPtr)(int NDim, SDPair const & Prms, Model const * AnotherMdl);
00187 
00188 typedef std::map<String, ModelMakerPtr> ModelFactory_t;
00189 
00190 ModelFactory_t ModelFactory;
00191 
00192 Model * AllocModel(String const & Name, int NDim, SDPair const & Prms, Model const * Mdl)
00193 {
00194     ModelFactory_t::iterator it = ModelFactory.find(Name);
00195     if (it==ModelFactory.end()) throw new Fatal("AllocModel: '%s' is not available", Name.CStr());
00196 
00197     Model * ptr = (*it->second)(NDim,Prms,Mdl);
00198 
00199     return ptr;
00200 }
00201 
00202 Model * AllocModel(double IDinMODEL, int NDim, SDPair const & Prms, Model const * Mdl)
00203 {
00204     String model_name;
00205     MODEL.Val2Key (IDinMODEL, model_name);
00206     return AllocModel (model_name, NDim, Prms, Mdl);
00207 }
00208 
00209 Array<String> MODEL_CTE_NAMES; 
00210 
00211 int ModelRegister()
00212 {
00213     MODEL_CTE_NAMES.Resize(7);
00214     MODEL_CTE_NAMES = "grav", "gamW", "rho", "rhoS", "por", "gamNat", "gamSat";
00215     return 0;
00216 }
00217 
00218 int __Model_dummy_int = ModelRegister();
00219 
00220 
00222 
00223 
00224 #ifdef USE_BOOST_PYTHON
00225 double PyMODEL (BPy::str const & Key) { return MODEL(BPy::extract<char const *>(Key)()); }
00226 #endif
00227 
00228 #endif // MECHSYS_MODEL_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines