00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef MECHSYS_LABTESTSIM_LABTESTSIM_H
00023 #define MECHSYS_LABTESTSIM_LABTESTSIM_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 "util/string.h"
00034 #include "util/array.h"
00035 #include "labtestsim/inputdata.h"
00036 #include "labtestsim/equipment.h"
00037 #include "labtestsim/ctx.h"
00038 #include "labtestsim/ttx.h"
00039 #include "models/coupled/coupledmodel.h"
00040 #include "models/equilibmodel.h"
00041
00042 namespace LabTestSim
00043 {
00044
00064 template<typename T_Model>
00065 class LTS
00066 {
00067 public:
00068
00069 LTS (InputData const & ID);
00070 ~LTS ();
00071
00072
00073 void ReInitModel ();
00074 void ReInitModel (Array<REAL> const & ModelPrms);
00075 void Run (size_t iEquip);
00076 void WriteFile (size_t iEquip);
00077 void RunAll ();
00078 void WriteAll ();
00079 Equipment<T_Model> const * Equip (int iEquip) const;
00080
00081 private:
00082
00083 InputData const & _idata;
00084 bool _is_initialized;
00085 Array< String > _equipments;
00086 Array< Array<State> > _a_states;
00087 Array< Array<REAL> > _a_ini_data;
00088 Array< T_Model * > _a_p_model;
00089 Array< Equipment<T_Model>* > _a_p_equip;
00090
00091
00092 void _re_initialize(Array<REAL> const & ModelPrms);
00093 };
00094
00095
00097
00098
00099 template<typename T_Model>
00100 inline LTS<T_Model>::LTS(InputData const & ID)
00101 : _idata (ID) ,
00102 _is_initialized (false)
00103 {
00104
00105 CoupledModel::SetIntegSchemesCtes(ID.intSC);
00106 EquilibModel::SetIntegSchemesCtes(ID.intSC);
00107
00108
00109 size_t n_files = _idata.a_data.size();
00110
00111
00112 _equipments.resize(n_files);
00113 _a_states .resize(n_files);
00114 _a_ini_data.resize(n_files);
00115 _a_p_model .resize(n_files, NULL);
00116 _a_p_equip .resize(n_files, NULL);
00117
00118
00119 for (size_t i_file=0; i_file<n_files; ++i_file)
00120 {
00121
00122 _equipments[i_file] = _idata.a_EQUIPS[i_file];
00123
00124
00125 FileParser::Table::const_iterator Sa_it = _idata.a_data[i_file].find("Sa");
00126 if (Sa_it==_idata.a_data[i_file].end())
00127 throw new Fatal(_("LTS::LTS File < %s >. Could not find tag <Sa> inside data"), _idata.a_fnDAT[i_file].c_str());
00128
00129
00130 FileParser::Table::const_iterator Sr_it = _idata.a_data[i_file].find("Sr");
00131 if (Sr_it==_idata.a_data[i_file].end())
00132 throw new Fatal(_("LTS::LTS File < %s >. Could not find tag <Sr> inside data"), _idata.a_fnDAT[i_file].c_str());
00133
00134
00135 FileParser::Table::const_iterator St_it = _idata.a_data[i_file].find("St");
00136 if (St_it==_idata.a_data[i_file].end())
00137 throw new Fatal(_("LTS::LTS File < %s >. Could not find tag <St> inside data"), _idata.a_fnDAT[i_file].c_str());
00138
00139 #ifdef USE_COUPLEDMODEL
00140
00141 FileParser::Table::const_iterator Pp_it = _idata.a_data[i_file].find("Pp");
00142 if (Pp_it==_idata.a_data[i_file].end())
00143 throw new Fatal(_("LTS::LTS File < %s >. Could not find tag <Pp> inside data"), _idata.a_fnDAT[i_file].c_str());
00144 #endif
00145
00146
00147 FileParser::Table::const_iterator v_it = _idata.a_data[i_file].find("v");
00148 if (v_it==_idata.a_data[i_file].end())
00149 throw new Fatal(_("LTS::LTS File < %s >. Could not find tag <v> inside data"), _idata.a_fnDAT[i_file].c_str());
00150
00151
00152
00153
00154 int n_states = Sa_it->second.size() - _idata.a_SKIPLAST[i_file] - _idata.a_SKIPFIRST[i_file];
00155 int skip = _idata.a_SKIPFIRST[i_file];
00156 Array<State> tmp_states;
00157 tmp_states.resize(n_states);
00158 for (int j_state=0; j_state<n_states; ++j_state)
00159 {
00160 tmp_states[j_state].Sa = Sa_it->second[skip+j_state];
00161 tmp_states[j_state].Sr = Sr_it->second[skip+j_state];
00162 tmp_states[j_state].St = St_it->second[skip+j_state];
00163 #ifdef USE_COUPLEDMODEL
00164 tmp_states[j_state].Pp = Pp_it->second[skip+j_state];
00165 #endif
00166 }
00167 _a_states[i_file] = tmp_states;
00168
00169
00170 if (v_it->second[skip+0]==0.0)
00171 throw new Fatal(_("LTS::LTS File < %s >. v_ini must be different than zero"), _idata.a_fnDAT[i_file].c_str());
00172 Array<REAL> tmp_ini_data;
00173 #ifdef USE_COUPLEDMODEL
00174 tmp_ini_data.resize(7);
00175 #else
00176 tmp_ini_data.resize(5);
00177 #endif
00178 tmp_ini_data[0] = _a_states[i_file][0].Sa;
00179 tmp_ini_data[1] = _a_states[i_file][0].Sr;
00180 tmp_ini_data[2] = _a_states[i_file][0].St;
00181 tmp_ini_data[3] = v_it->second[skip+0];
00182 tmp_ini_data[4] = ID.a_OCR[i_file];
00183 #ifdef USE_COUPLEDMODEL
00184 tmp_ini_data[5] = ID.a_OSI[i_file];
00185 tmp_ini_data[6] = _a_states[i_file][0].Pp;
00186 #endif
00187 _a_ini_data[i_file] = tmp_ini_data;
00188 }
00189 }
00190
00191 template<typename T_Model>
00192 inline LTS<T_Model>::~LTS()
00193 {
00194 for (size_t i=0; i<_equipments.size(); ++i)
00195 {
00196 if (_a_p_model[i]!=NULL) delete _a_p_model[i];
00197 if (_a_p_equip[i]!=NULL) delete _a_p_equip[i];
00198 }
00199 }
00200
00201 template<typename T_Model>
00202 inline void LTS<T_Model>::ReInitModel()
00203 {
00204
00205 Array<REAL> model_prms;
00206 FileParser FP(_idata.fnMAT);
00207 FP.FindKeyAndFillArray(_idata.MODEL, model_prms);
00208
00209
00210 _re_initialize(model_prms);
00211
00212
00213 _is_initialized = true;
00214 }
00215
00216 template<typename T_Model>
00217 inline void LTS<T_Model>::ReInitModel(Array<REAL> const & ModelPrms)
00218 {
00219
00220 _re_initialize(ModelPrms);
00221
00222
00223 _is_initialized = true;
00224 }
00225
00226 template<typename T_Model>
00227 inline void LTS<T_Model>::Run(size_t iEquip)
00228 {
00229
00230 if (!_is_initialized) throw new Fatal(_("LTS::Run: LTS was not initialized"));
00231
00232
00233 _a_p_equip[iEquip]->Run(_idata.NDIV, _a_states[iEquip]);
00234 }
00235
00236 template<typename T_Model>
00237 inline void LTS<T_Model>::WriteFile(size_t iEquip)
00238 {
00239
00240 if (!_is_initialized) throw new Fatal(_("LTS::WriteFile: LTS was not initialized"));
00241
00242
00243 _a_p_equip[iEquip]->WriteFile(_idata.a_fnCAL[iEquip], _idata.PRINCVALS);
00244 }
00245
00246 template<typename T_Model>
00247 inline void LTS<T_Model>::RunAll()
00248 {
00249 for (size_t i_equip=0; i_equip<_a_p_equip.size(); ++i_equip)
00250 Run (i_equip);
00251 }
00252
00253 template<typename T_Model>
00254 inline void LTS<T_Model>::WriteAll()
00255 {
00256 for (size_t i_equip=0; i_equip<_a_p_equip.size(); ++i_equip)
00257 WriteFile (i_equip);
00258 }
00259
00260 template<typename T_Model>
00261 inline Equipment<T_Model> const * LTS<T_Model>::Equip(int iEquip) const
00262 {
00263
00264 if (!_is_initialized) throw new Fatal(_("LTS::WriteFile: LTS was not initialized"));
00265
00266 return _a_p_equip[iEquip];
00267 }
00268
00269 template<typename T_Model>
00270 inline void LTS<T_Model>::_re_initialize(Array<REAL> const & ModelPrms)
00271 {
00272
00273 size_t n_files = _equipments.size();
00274
00275
00276 for (size_t i=0; i<n_files; ++i)
00277 {
00278
00279 if (_a_p_model[i]!=NULL) delete _a_p_model[i];
00280
00281
00282 if (_a_p_equip[i]!=NULL) delete _a_p_equip[i];
00283
00284
00285 ALLOCMODELFUNC(_idata.MODEL,ModelPrms,_a_ini_data[i], _a_p_model[i]);
00286
00287
00288 if (_equipments[i]=="CTX")
00289 _a_p_equip[i] = new CTX<T_Model> (_a_p_model[i], _equipments[i], _idata.StopOnFailure, _idata.SAVEINTSTATES, _idata.Silent);
00290
00291 else if (_equipments[i]=="TTX")
00292 _a_p_equip[i] = new TTX<T_Model> (_a_p_model[i], _equipments[i], _idata.StopOnFailure, _idata.SAVEINTSTATES, _idata.Silent);
00293
00294 else
00295 {
00296 delete _a_p_model[i];
00297 throw new Fatal(_("LTS::_re_initialize: Invalid equipment key < %s >"), _equipments[i].c_str());
00298 }
00299 }
00300 }
00301
00302 };
00303
00304 #endif // MECHSYS_LABTESTSIM_LABTESTSIM_H
00305
00306