OpenWAM
TEGRV.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 "TEGRV.h"
32 //#include <cmath>
33 
34 //---------------------------------------------------------------------------
35 //---------------------------------------------------------------------------
36 TEGRV::TEGRV() {
37 
38  FErrorI = 0;
39  FError = 0;
40  FError_ant = 0;
41 
42  FVector_Mf_mapa = NULL;
43  FVector_TipoControl_Regimen_mapa = NULL;
44  FVector_Regimen_mapa = NULL;
45 
46  FMapa_TipoControl = NULL;
47  FMapa_MasaAire = NULL;
48 
49  FKc = 0.01;
50  FKi = 0.008;
51  FKd = 0.015;
52 
53 }
54 
55 //---------------------------------------------------------------------------
56 //---------------------------------------------------------------------------
57 
58 TEGRV::~TEGRV() {
59 
60 }
61 
62 //---------------------------------------------------------------------------
63 //---------------------------------------------------------------------------
64 
65 void TEGRV::LeeDatosEntrada(char *Ruta, FILE *fich) {
66  char *FileEGR;
67  char *DatosEGR;
68 
69  try {
70 
71  for(int i = 0; i <= (int) strlen(Ruta); i++) {
72  DatosEGR[i] = Ruta[i];
73  }
74 
75  fscanf(fich, "%s ", &FileEGR);
76  strcat(DatosEGR, FileEGR);
77 
78  FichEGRV = fopen(DatosEGR, "r");
79  if((FichEGRV = fopen(DatosEGR, "r")) == NULL) {
80  std::cout << "ERROR: Fichero de EGR no cargado";
81  } else {
82  fscanf(FichEGRV, "%lf %lf %lf ", &FKc, &FKi, &FKd);
83  fscanf(FichEGRV, "%d %d %d ", &FNumeroDatos_TipoControl_Regimen, &FNumeroDatos_Mf, &FNumeroDatos_Regimen);
84 
85  FVector_Mf_mapa = new double[FNumeroDatos_Mf];
86  FVector_TipoControl_Regimen_mapa = new double[FNumeroDatos_TipoControl_Regimen];
87  FVector_Regimen_mapa = new double[FNumeroDatos_Regimen];
88 
89  FMapa_TipoControl = new double[FNumeroDatos_TipoControl_Regimen];
90  FMapa_MasaAire = new double*[FNumeroDatos_Mf];
91  for(int i = 0; i < FNumeroDatos_Mf; i++) {
92  FMapa_MasaAire[i] = new double[FNumeroDatos_Regimen];
93  }
94 
95  for(int i = 0; i < FNumeroDatos_TipoControl_Regimen; i++) {
96  fscanf(FichEGRV, "%lf ", &FVector_TipoControl_Regimen_mapa[i]);
97  }
98  for(int i = 0; i < FNumeroDatos_Regimen; i++) {
99  fscanf(FichEGRV, "%lf ", &FVector_Regimen_mapa[i]);
100  }
101  for(int i = 0; i < FNumeroDatos_Mf; i++) {
102  fscanf(FichEGRV, "%lf ", &FVector_Mf_mapa[i]);
103  }
104  for(int i = 0; i < FNumeroDatos_TipoControl_Regimen; i++) {
105  fscanf(FichEGRV, "%lf ", &FMapa_TipoControl[i]);
106  }
107  for(int i = 0; i < FNumeroDatos_Mf; i++) {
108  for(int j = 0; j < FNumeroDatos_Regimen; j++) {
109  fscanf(FichEGRV, "%lf ", &FMapa_MasaAire[i][j]);
110  }
111  }
112  fclose(FichEGRV);
113  }
114 
115  } catch(exception &N) {
116  std::cout << "ERROR: TEGRV::LeeDatosEntrada (DLL)" << std::endl;
117  std::cout << "Tipo de error: " << N.what() << std::endl;
118  throw Exception(N.what());
119  }
120 }
121 
122 //---------------------------------------------------------------------------
123 //---------------------------------------------------------------------------
124 
125 /*void TEGRV::CalculaEGRV(double TiempoActual)
126  {
127  try
128  {
129  // AQUI SE CALCULAN LOS COEFICIENTES DE DESCARGA Y TURBULENCIA
130 
131  int j=0, jmax=FNumeroDatos-1;
132  double EGRVAct,deltaT,t;
133  while(TiempoActual>FTiempo[j] && j<jmax){
134  j++;
135  }
136  if(j==jmax){
137  EGRVAct=FEGRV[jmax];
138  }else{
139  deltaT= FTiempo[j]-FTiempo[j-1];
140  t=TiempoActual-FTiempo[j-1];
141  EGRVAct=xit_(FEGRV[j-1],FEGRV[j],deltaT,t);
142  }
143  if(EGRVAct<=50){
144  FCDEntrante=0.0191*EGRVAct-0.6273;
145  if (FCDEntrante > 1.0){
146  FCDEntrante = 1.;
147  }else if(FCDEntrante < 0.0){
148  FCDEntrante = 0.;
149  }
150  }else{
151  FCDEntrante=0.0015*EGRVAct+0.246;
152  if (FCDEntrante > 1.0){
153  FCDEntrante = 1.;
154  }else if(FCDEntrante < 0.0){
155  FCDEntrante = 0.;
156  }
157  }
158  FCDSaliente=FCDEntrante;
159  FCTorbellino=0;
160 
161 
162 
163  }
164  catch(Exception &N)
165  {
166  std::cout << "ERROR: Calculo de la EGRV (DLL)" << std::endl;
167  std::cout << "Tipo de error: " << N.what() << std::endl;
168  throw Exception(N.what());
169  }
170  } */
171 
172 //---------------------------------------------------------------------------
173 //---------------------------------------------------------------------------
174 double TEGRV::xit_(double vizq, double vder, double axid, double xif) {
175  try {
176  double xx = 0., yy = 0.;
177  double ret_val = 0.;
178 
179  xx = vder - vizq;
180  if(axid != 0.) {
181  yy = xx / axid * xif;
182  ret_val = vizq + yy;
183  } else {
184  printf("ERROR: valores entrada xit\n");
185  throw Exception("");
186  }
187  return ret_val;
188  } catch(exception &N) {
189  std::cout << "ERROR: xit_" << std::endl;
190  std::cout << "Tipo de error: " << N.what() << std::endl;
191  throw Exception(N.what());
192  }
193 }
194 //---------------------------------------------------------------------------
195 //---------------------------------------------------------------------------
196 
197 nmTipoControl TEGRV::DeterminacionTipoControl(double Regimen, double MasaFuel) {
198  try {
199  double MasaFuel_mapadecisor = 0.;
200 
201  MasaFuel_mapadecisor = Interp1(Regimen, FVector_TipoControl_Regimen_mapa, FMapa_TipoControl,
202  FNumeroDatos_TipoControl_Regimen); // en (mg/cc)
203 
204  if(MasaFuel * 1e6 < MasaFuel_mapadecisor) {
205  FTipoControl = nmControlMasaAire;
206  } else {
207  FTipoControl = nmControlPadm;
208  }
209 
210  return FTipoControl;
211 
212  } catch(exception &N) {
213  std::cout << "ERROR: TEGRV::DeterminacionTipoControl (DLL)" << std::endl;
214  std::cout << "Tipo de error: " << N.what() << std::endl;
215  throw Exception(N.what());
216  }
217 }
218 
219 //---------------------------------------------------------------------------
220 //---------------------------------------------------------------------------
221 
222 void TEGRV::CalculaEGRV(double MasaFuel, double Regimen, double MasaAireAdmitida, double TiempoActual) {
223  try {
224 // AQUI SE CALCULAN LOS COEFICIENTES DE DESCARGA Y TURBULENCIA
225 
226  if(FTipoControl == nmControlPadm) {
227  if(TiempoActual < 13 * 120 / Regimen) {
228  FCDEntrante = FCDEntrante / 2;
229  } else {
230  FCDEntrante = 0.;
231  }
232  } else if(FTipoControl == nmControlMasaAire) {
233  MasaFuel = MasaFuel * 1e6; // en (mg/cc)
234  FMasaAireAdmitidaConsigna = Interpolacion_bidimensional(Regimen, MasaFuel, FVector_Mf_mapa, FVector_Regimen_mapa,
235  FMapa_MasaAire, FNumeroDatos_Regimen, FNumeroDatos_Mf); // (mg)
236 
237  FError_ant = FError;
238  FError = (MasaAireAdmitida - FMasaAireAdmitidaConsigna) / FMasaAireAdmitidaConsigna;
239  FErrorI = FErrorI + FError;
240 
241  /* PID */
242  FP = FKc * FError;
243  FI = FKi * (FErrorI);
244  FD = FKd * (FError - FError_ant);
245  FCDEntrante = FCDEntrante + FP + FI + FD;
246  }
247  if(FCDEntrante > 1.0) {
248  FCDEntrante = 1.;
249  } else if(FCDEntrante < 0.0) {
250  FCDEntrante = 0.;
251  FErrorI = 0;
252  }
253  FCDSaliente = FCDEntrante;
254  FCTorbellino = 0.;
255 
256  } catch(exception &N) {
257  std::cout << "ERROR: TEGRV::CalculaEGRV (DLL)" << std::endl;
258  std::cout << "Tipo de error: " << N.what() << std::endl;
259  throw Exception(N.what());
260  }
261 }
262 
263 //---------------------------------------------------------------------------
264 //---------------------------------------------------------------------------
265 
266 void TEGRV::IniciaEGRV(double cdEGR, int CicloCerrado) {
267  try {
268 
269 //FEGRRef=EGRref;
270  FCDEntrante = cdEGR;
271  FCDSaliente = cdEGR;
272  FCicloCerrado = CicloCerrado;
273 
274  } catch(exception &N) {
275  std::cout << "ERROR: IniciaEGRV (DLL)" << std::endl;
276  std::cout << "Tipo de error: " << N.what() << std::endl;
277  throw Exception(N.what());
278  }
279 }
280 
281 //---------------------------------------------------------------------------
282 //---------------------------------------------------------------------------
283 
284 #pragma package(smart_init)
Exception
Custom exception class.
Definition: Exception.hpp:39