OpenWAM
TTable1D.cpp
1 /* --------------------------------------------------------------------------------*\
2 |==========================|
3  |\\ /\ /\ // O pen | OpenWAM: The Open Source 1D Gas-Dynamic Code
4  | \\ | X | // W ave |
5  | \\ \/_\/ // A ction | CMT-Motores Termicos / Universidad Politecnica Valencia
6  | \\/ \// M odel |
7  |----------------------------------------------------------------------------------
8  License
9 
10  This file is part of OpenWAM.
11 
12  OpenWAM is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  OpenWAM is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with OpenWAM. If not, see <http://www.gnu.org/licenses/>.
24 
25 
26  \*-------------------------------------------------------------------------------- */
27 
28 // ---------------------------------------------------------------------------
29 #pragma hdrstop
30 
31 #include "TTable1D.h"
32 
33 // ---------------------------------------------------------------------------
34 
36  TController(nmCtlTable, i) {
37  fID = i + 1;
38  /* fError_ant=0;
39  fTime_ant=0;
40  fInicio=true; */
41 
42  fDimensiones = 1;
43  fDatos = NULL;
44 }
45 
47  delete fDatos;
48 }
49 
50 double TTable1D::Output(double Time) {
51 
52  double X = FSensor[0]->Output();
53 
54  fOutput = fDatos->interp(X);
55 
57 
58  return fOutput;
59 }
60 
61 void TTable1D::LeeController(const char *FileWAM, fpos_t &filepos) {
62 
63  int xnum = 0, tip = 0, fromfile = 0;
64 
65  FILE *fich = fopen(FileWAM, "r");
66  fsetpos(fich, &filepos);
67 
68  fscanf(fich, "%d ", &fromfile);
69 
70  if(fromfile == 1) {
71  char *InputFile;
72  fscanf(fich, "%s ", &InputFile);
73 
74  FILE *fichdata = fopen(InputFile, "r");
75 
76  double X_tmp = 0., Y_tmp = 0.;
77  dVector X_vec, Y_vec;
78  while(!feof(fichdata)) {
79  fscanf(fichdata, "%lf %lf ", &X_tmp, &Y_tmp);
80  X_vec.push_back(X_tmp);
81  Y_vec.push_back(Y_tmp);
82  }
83  fX_map = X_vec;
84  fY_map = Y_vec;
85  } else if(fromfile == 0) {
86  // fscanf(fich,"%d ",&fDimensiones);
87  fscanf(fich, "%d ", &xnum);
88  fX_map.resize(xnum);
89  fY_map.resize(xnum);
90  for(int i = 0; i < xnum; i++) {
91  fscanf(fich, "%lf %lf ", &fX_map[i], &fY_map[i]);
92  }
93  } else {
94 
95  }
96 
97  fscanf(fich, "%lf ", &fPeriod);
98 
99  fscanf(fich, "%d ", &tip);
100  switch(tip) {
101  case 0:
102  fTipo = nmLineal;
103  fDatos = new Linear_interp(fX_map, fY_map);
104  break;
105  case 1:
106  fTipo = nmHermite;
107  fDatos = new Hermite_interp(fX_map, fY_map);
108  break;
109  case 2:
110  fTipo = nmSteps;
111  fDatos = new Step_interp(fX_map, fY_map);
112  break;
113  }
114 
115  FSensorID.resize(1);
116  fscanf(fich, "%d ", &FSensorID[0]);
117 
118  fgetpos(fich, &filepos);
119  fclose(fich);
120 
121 }
122 
123 void TTable1D::AsignaObjetos(TSensor **Sensor, TController **Controller) {
124 
125  FSensor.push_back(Sensor[FSensorID[0] - 1]);
126 
127  // fSetPointController=Controller[fSetPointControllerID-1];
128 
129 }
130 
131 void TTable1D::LeeResultadosMedControlador(const char *FileWAM, fpos_t &filepos) {
132  try {
133  int nvars = 0, var = 0;
134 
135  FILE *fich = fopen(FileWAM, "r");
136  fsetpos(fich, &filepos);
137 
138  fscanf(fich, "%d ", &nvars);
139  for(int i = 0; i < nvars; i++) {
140  fscanf(fich, "%d ", &var);
141  switch(var) {
142  case 0:
143  FResMediosCtrl.Output = true;
144  break;
145  default:
146  std::cout << "Resultados medios en Controlador " << fID << " no implementados " << std::endl;
147  }
148  }
149 
150  fgetpos(fich, &filepos);
151  fclose(fich);
152  } catch(exception & N) {
153  std::cout << "ERROR: TTable::LeeResultadosControlador en el controlador " << fID << std::endl;
154  std::cout << "Tipo de error: " << N.what() << std::endl;
155  throw Exception(N.what());
156  }
157 }
158 
159 void TTable1D::LeeResultadosInsControlador(const char *FileWAM, fpos_t &filepos) {
160  try {
161  int nvars = 0, var = 0;
162 
163  FILE *fich = fopen(FileWAM, "r");
164  fsetpos(fich, &filepos);
165 
166  fscanf(fich, "%d ", &nvars);
167  for(int i = 0; i < nvars; i++) {
168  fscanf(fich, "%d ", &var);
169  switch(var) {
170  case 0:
171  FResInstantCtrl.Output = true;
172  break;
173  default:
174  std::cout << "Resultados instantaneos en Controlador " << fID << " no implementados " << std::endl;
175  }
176  }
177 
178  fgetpos(fich, &filepos);
179  fclose(fich);
180  } catch(exception & N) {
181  std::cout << "ERROR: TTable::LeeResultadosInsControlador en el controlador " << fID << std::endl;
182  std::cout << "Tipo de error: " << N.what() << std::endl;
183  throw Exception(N.what());
184  }
185 }
186 
187 void TTable1D::CabeceraResultadosMedControlador(stringstream& medoutput) {
188  try {
189  std::string Label;
190 
191  if(FResMediosCtrl.Output) {
192  Label = "\t" + PutLabel(705) + std::to_string(fID) + PutLabel(901);
193  medoutput << Label.c_str();
194  }
195 
196  } catch(exception & N) {
197  std::cout << "ERROR: TTable::CabeceraResultadosMedControlador en el controlador " << fID << std::endl;
198  std::cout << "Tipo de error: " << N.what() << std::endl;
199  throw Exception(N.what());
200  }
201 }
202 
203 void TTable1D::CabeceraResultadosInsControlador(stringstream& insoutput) {
204  try {
205  std::string Label;
206 
207  if(FResInstantCtrl.Output) {
208  Label = "\t" + PutLabel(705) + std::to_string(fID) + PutLabel(901);
209  insoutput << Label.c_str();
210  }
211 
212  } catch(exception & N) {
213  std::cout << "ERROR: TTable::CabeceraResultadosInsControlador en el controlador " << fID << std::endl;
214  std::cout << "Tipo de error: " << N.what() << std::endl;
215  throw Exception(N.what());
216  }
217 }
218 
219 void TTable1D::ImprimeResultadosMedControlador(stringstream& medoutput) {
220  try {
221  std::string Label;
222 
223  if(FResMediosCtrl.Output) {
224  medoutput << "\t" << FResMediosCtrl.OutputMED;
225  }
226 
227  } catch(exception & N) {
228  std::cout << "ERROR: TTable::ImprimeResultadosMedControlador en el controlador " << fID << std::endl;
229  std::cout << "Tipo de error: " << N.what() << std::endl;
230  throw Exception(N.what());
231  }
232 }
233 
234 void TTable1D::ImprimeResultadosInsControlador(stringstream& insoutput) {
235  try {
236  std::string Label;
237 
238  if(FResInstantCtrl.Output) {
239  insoutput << "\t" << FResInstantCtrl.OutputINS;
240  }
241 
242  } catch(exception & N) {
243  std::cout << "ERROR: TTable::CabeceraResultadosInsControlador en el controlador " << fID << std::endl;
244  std::cout << "Tipo de error: " << N.what() << std::endl;
245  throw Exception(N.what());
246  }
247 }
248 
250  try {
251 
252  FResMediosCtrl.OutputSUM = 0.;
253  FResMediosCtrl.TiempoSUM = 0.;
254  FResMediosCtrl.Tiempo0 = 0.;
255 
256  } catch(exception & N) {
257  std::cout << "ERROR: TTable::IniciaMedias en el controlador: " << fID << std::endl;
258  // std::cout << "Tipo de error: " << N.what() << std::endl;
259  throw Exception(N.what());
260  }
261 }
262 
264  try {
265 
266  if(FResMediosCtrl.Output && FResMediosCtrl.TiempoSUM > 0) {
267  FResMediosCtrl.OutputMED = FResMediosCtrl.OutputSUM / FResMediosCtrl.TiempoSUM;
268  FResMediosCtrl.OutputSUM = 0.;
269  } else {
270  FResMediosCtrl.OutputMED = 0.;
271  }
272 
273  FResMediosCtrl.TiempoSUM = 0;
274 
275  } catch(exception & N) {
276  std::cout << "ERROR: TTable::ResultadosMediosController en el eje: " << fID << std::endl;
277  // std::cout << "Tipo de error: " << N.what() << std::endl;
278  throw Exception(N.what());
279  }
280 }
281 
283  try {
284  /* Lo que se hace en esta funcion se realiza dentro del calculo del eje, para asi poder
285  llevar a cabo la salida de resultados medios por pantalla. */
286  double Delta = Actual - FResMediosCtrl.Tiempo0;
287 
288  if(FResMediosCtrl.Output) {
289  FResMediosCtrl.OutputSUM += fOutput * Delta;
290  }
291 
292  FResMediosCtrl.TiempoSUM += Delta;
293  FResMediosCtrl.Tiempo0 = Actual;
294 
295  } catch(exception & N) {
296  std::cout << "ERROR: TTable::AcumulaResultadosMediosController en el eje: " << fID << std::endl;
297  // std::cout << "Tipo de error: " << N.what() << std::endl;
298  throw Exception(N.what());
299  }
300 }
301 
303  try {
304  if(FResInstantCtrl.Output)
305  FResInstantCtrl.OutputINS = fOutput;
306 
307  } catch(exception & N) {
308  std::cout << "ERROR: TTable::ResultadosInstantController en el eje " << fID << std::endl;
309  std::cout << "Tipo de error: " << N.what() << std::endl;
310  throw Exception(N.what());
311  }
312 }
313 
314 #pragma package(smart_init)
TTable1D::LeeResultadosMedControlador
void LeeResultadosMedControlador(const char *FileWAM, fpos_t &filepos)
Definition: TTable1D.cpp:131
TTable1D::~TTable1D
~TTable1D()
Definition: TTable1D.cpp:46
TController
Definition: TController.h:37
TTable1D::LeeResultadosInsControlador
void LeeResultadosInsControlador(const char *FileWAM, fpos_t &filepos)
Definition: TTable1D.cpp:159
Step_interp
Definition: Math_wam.h:325
TTable1D::AsignaObjetos
void AsignaObjetos(TSensor **Sensor, TController **Controller)
Definition: TTable1D.cpp:123
TTable1D::ResultadosMediosController
void ResultadosMediosController()
Definition: TTable1D.cpp:263
TTable1D::AcumulaResultadosMediosController
void AcumulaResultadosMediosController(double Actual)
Definition: TTable1D.cpp:282
TTable1D::ImprimeResultadosMedControlador
void ImprimeResultadosMedControlador(stringstream &medoutput)
Definition: TTable1D.cpp:219
TTable1D::CabeceraResultadosInsControlador
void CabeceraResultadosInsControlador(stringstream &insoutput)
Definition: TTable1D.cpp:203
Hermite_interp
Definition: Math_wam.h:311
TController::FSensorID
iVector FSensorID
Array with the ID of the sensor inputs.
Definition: TController.h:43
TController::FResMediosCtrl
stResMediosCtrl FResMediosCtrl
Struct with the average results of the controllers.
Definition: TController.h:49
TTable1D::ResultadosInstantController
void ResultadosInstantController()
Definition: TTable1D.cpp:302
Linear_interp
Definition: Math_wam.h:301
TTable1D::IniciaMedias
void IniciaMedias()
Definition: TTable1D.cpp:249
TTable1D::LeeController
void LeeController(const char *FileWAM, fpos_t &filepos)
Definition: TTable1D.cpp:61
PutLabel
std::string PutLabel(int idx)
Returns an integer.
Definition: labels.cpp:475
Exception
Custom exception class.
Definition: Exception.hpp:39
TTable1D::Output
double Output(double Time)
Definition: TTable1D.cpp:50
TTable1D::CabeceraResultadosMedControlador
void CabeceraResultadosMedControlador(stringstream &medoutput)
Definition: TTable1D.cpp:187
TTable1D::ImprimeResultadosInsControlador
void ImprimeResultadosInsControlador(stringstream &insoutput)
Definition: TTable1D.cpp:234
TController::FResInstantCtrl
stResInstantCtrl FResInstantCtrl
Struct with the average results of the controllers.
Definition: TController.h:50
TSensor
Definition: TSensor.h:42
dVector
std::vector< double > dVector
Double vector.
Definition: Math_wam.h:70
TController::FSensor
std::vector< TSensor * > FSensor
Array with the pointers of the sensor inputs.
Definition: TController.h:40
TTable1D::TTable1D
TTable1D(int i)
Definition: TTable1D.cpp:35