equipment.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_LABTESTSIM_EQUIPMENT_H
00023 #define MECHSYS_LABTESTSIM_EQUIPMENT_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 <cmath>
00034 #include <fstream>
00035 
00036 #include "models/equilibmodel.h"
00037 #include "tensors/functions.h"
00038 #include "tensors/tensors.h"
00039 #include "util/numstreams.h"
00040 #include "util/exception.h"
00041 #include "util/string.h"
00042 #include "util/array.h"
00043 #include "util/util.h" // for Sort
00044 
00045 namespace LabTestSim
00046 {
00047 
00048 // Auxiliar structure
00049 struct State
00050 {
00051     REAL Sa;
00052     REAL Sr;
00053     REAL St;
00054     REAL Pp; // Pore-pressure
00055 }; // struct State
00056 
00057 template<typename T_Model>
00058 class Equipment
00059 {
00060 public:
00061 
00062     // Constructor
00063     Equipment (T_Model * pModel, String const & strEquipment, bool StopOnFailure, bool SaveIntStates, bool Silent=false);
00064 
00065     // Destructor
00066     virtual ~Equipment() {}
00067 
00068     // Methods
00069     void                     Run       (int nDiv, Array<State> const & States);
00070     void                     WriteFile (String const & Filename, bool PrintPrincVals=false, bool PrintHead=true);
00071     int                      nStates   ()            const { return _SIG.size();   }
00072     Tensors::Tensor2 const & Sig       (int i_state) const { return _SIG[i_state]; }
00073     Tensors::Tensor2 const & Eps       (int i_state) const { return _EPS[i_state]; }
00074 
00075 protected:
00076     // Data
00077     T_Model * _model;
00078     String    _equipment;
00079     bool      _stop_on_failure;
00080     bool      _save_int_states; // Save intermediary states?
00081     bool      _silent;          // Do not show warning messages
00082 
00083     // Methods
00084     virtual int  _exx_idx() const =0;
00085     virtual bool _do_update_one_increment(State const & DeltaState) =0;
00086 
00087 private:
00088     // Data
00089     Array<Tensors::Tensor2> _SIG; // Stress tensor's        size == num states
00090     Array<Tensors::Tensor2> _EPS; // Strain tensor's        size == num states
00091     Array<Array<REAL>     > _ISV; // Internal State Values  size == num states
00092     Array<REAL            > _PPR; // Pore-pressures
00093 
00094 }; // class Equipment
00095 
00096 
00098 
00099 
00100 template<typename T_Model>
00101 inline Equipment<T_Model>::Equipment(T_Model * pModel, String const & strEquipment, bool StopOnFailure, bool SaveIntStates, bool Silent) // {{{
00102     : _model           (pModel)       ,
00103       _equipment       (strEquipment) ,
00104       _stop_on_failure (StopOnFailure),
00105       _save_int_states (SaveIntStates),
00106       _silent          (Silent)
00107 {
00108 } // }}}
00109 
00110 template<typename T_Model>
00111 inline void Equipment<T_Model>::Run(int nDiv, Array<State> const & States ) // {{{
00112 {
00113     // Number of states
00114     size_t n_states = States.size();
00115 
00116     // Re-allocate arrays
00117     if (_save_int_states)
00118     {
00119         _SIG.resize(1); // will use push_back
00120         _EPS.resize(1);
00121         _ISV.resize(1);
00122 #ifdef USE_COUPLEDMODEL
00123         _PPR.resize(1); // pore-pressures
00124 #endif
00125     }
00126     else
00127     {
00128         _SIG.resize(n_states);
00129         _EPS.resize(n_states);
00130         _ISV.resize(n_states);
00131 #ifdef USE_COUPLEDMODEL
00132         _PPR.resize(n_states);
00133 #endif
00134     }
00135 
00136     // Save initial state
00137     Array<REAL> tmp_isv; _model->InternalStateValues(tmp_isv);
00138     _SIG[0] = _model->Sig();
00139     _EPS[0] = _model->Eps();
00140     _ISV[0] = tmp_isv;
00141 #ifdef USE_COUPLEDMODEL
00142     _PPR[0] = _model->Pp();
00143 #endif
00144 
00145     // Loop along final states
00146     for (size_t i_state=1; i_state<n_states; ++i_state)
00147     {
00148         // Delta
00149         Tensors::Tensor2 const & sig = _model->Sig();
00150 #ifdef USE_COUPLEDMODEL
00151         REAL ppr = _model->Pp();
00152 #endif
00153         State delta_state;
00154         delta_state.Sa  = (States[i_state].Sa  - sig(0))/nDiv;
00155         delta_state.Sr  = (States[i_state].Sr  - sig(1))/nDiv;
00156         delta_state.St  = (States[i_state].St  - sig(2))/nDiv;
00157 #ifdef USE_COUPLEDMODEL
00158         delta_state.Pp  = (States[i_state].Pp  - ppr   )/nDiv;
00159 #endif
00160 
00161         // Loop along divisions
00162         for (int j=0; j<nDiv; ++j)
00163         {
00164             bool ok = _do_update_one_increment(delta_state);
00165             if (_save_int_states)
00166             {
00167                 _model->InternalStateValues(tmp_isv);
00168                 _SIG.push_back(_model->Sig());
00169                 _EPS.push_back(_model->Eps());
00170                 _ISV.push_back(tmp_isv);
00171 #ifdef USE_COUPLEDMODEL
00172                 _PPR.push_back(_model->Pp());
00173 #endif
00174             }
00175             if ((!ok) && _stop_on_failure) return;
00176             if (!ok) break;
00177         }
00178 
00179         // Save state
00180         if (!_save_int_states)
00181         {
00182             _model->InternalStateValues(tmp_isv);
00183             _SIG[i_state] = _model->Sig();
00184             _EPS[i_state] = _model->Eps();
00185             _ISV[i_state] = tmp_isv;
00186 #ifdef USE_COUPLEDMODEL
00187             _PPR[i_state] = _model->Pp();
00188 #endif
00189         }
00190     }
00191 } // }}}
00192 
00193 template<typename T_Model>
00194 inline void Equipment<T_Model>::WriteFile(String const & Filename, bool PrintPrincVals, bool PrintHead) // {{{
00195 {
00196 
00197     using Util::_8s;
00198 
00199     std::ofstream of(Filename.GetSTL().c_str(), std::ios::out);
00200     if (!of.fail())
00201     {
00202         if (PrintHead)
00203         {
00204             // Heading
00205             of << _8s()<< "p"  << _8s()<< "q"  << _8s()<< "sin3th" << _8s()<< "Ev" << _8s()<< "Ed";
00206             of << _8s()<< "Sa" << _8s()<< "Sr" << _8s()<< "St"     << _8s()<< "Sat";
00207             of << _8s()<< "Ea" << _8s()<< "Er" << _8s()<< "Et"     << _8s()<< "Eat";
00208 
00209             // Internal state values names
00210             Array<String> names;  _model->InternalStateNames(names);
00211             for (size_t i=0; i<names.size(); ++i)
00212                 of << _8s()<< names[i];
00213 
00214             // Principal values
00215             if (PrintPrincVals)
00216             {
00217                 of << _8s()<< "S1" << _8s()<< "S2" << _8s()<< "S3";
00218                 of << _8s()<< "E1" << _8s()<< "E2" << _8s()<< "E3";
00219             }
00220 
00221 #ifdef USE_COUPLEDMODEL
00222             // Pore-pressure
00223             of << _8s()<< "Pp";
00224 #endif
00225 
00226             // Next line
00227             of << std::endl;
00228         }
00229 
00230         REAL sq2 = sqrt(2.0);
00231 
00232         for (size_t i=0; i<_EPS.size(); ++i)
00233         {
00234             // Invariants
00235             REAL p,q,sin3th; Tensors::Tensor2 S; Tensors::Stress_p_q_S_sin3th(_SIG[i], p,q,S,sin3th);
00236             REAL Ev,Ed;                          Tensors::Strain_Ev_Ed       (_EPS[i], Ev,Ed);
00237 
00238             // Output values
00239             of << _8s()<< p                << _8s()<< q                << _8s()<< sin3th           << _8s()<< Ev*100.0 << _8s()<< Ed*100.0;
00240             of << _8s()<< _SIG[i](0)       << _8s()<< _SIG[i](1)       << _8s()<< _SIG[i](2)       << _8s()<< _SIG[i](5)/sq2;
00241             of << _8s()<< _EPS[i](0)*100.0 << _8s()<< _EPS[i](1)*100.0 << _8s()<< _EPS[i](2)*100.0 << _8s()<< _EPS[i](5)*100.0/sq2;
00242 
00243             // Output internal values
00244             for (size_t j=0; j<_ISV[i].size(); ++j)
00245                 of << _8s()<< _ISV[i][j];
00246 
00247             // Principal values
00248             if (PrintPrincVals)
00249             {
00250                 REAL S[3]; Tensors::Eigenvals(_SIG[i], S);  Util::Sort(S,3);
00251                 REAL E[3]; Tensors::Eigenvals(_EPS[i], E);  Util::Sort(E,3);
00252                 of << _8s()<< S[2] << _8s()<< S[1] << _8s()<< S[0];
00253                 of << _8s()<< E[2] << _8s()<< E[1] << _8s()<< E[0];
00254             }
00255 
00256 #ifdef USE_COUPLEDMODEL
00257             // Pore-pressure
00258             of << _8s()<< _PPR[i];
00259 #endif
00260 
00261             // Next line
00262             of << std::endl;
00263         }
00264 
00265         of.close();
00266     }
00267     else
00268         throw new Fatal(_("Equipment::WriteFile: Could not open file < %s > for output"), Filename.c_str());
00269 
00270 } // }}}
00271 
00272 }; // namespace LabTestSim
00273 
00274 #endif // MECHSYS_LABTESTSIM_EQUIPMENT_H
00275 
00276 // vim:fdm=marker

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