OpenWAM
TValvula4T.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 "TValvula4T.h"
32 #include "TCilindro.h"
33 #include "TTubo.h"
34 #include "TBloqueMotor.h"
35 
36 // ---------------------------------------------------------------------------
37 // ---------------------------------------------------------------------------
38 
39 TValvula4T::TValvula4T() :
40  TTipoValvula(nmValvula4T) {
41 
42  fun_FLift = NULL;
43  fun_CDin = NULL;
44  fun_CDout = NULL;
45  fun_Torb = NULL;
46  FVVTLift = false;
47  FVVTTiming = false;
48  FVVTDuration = false;
49  FVVT = false;
50  FVVTLiftMultiplier = 1.;
51  FVVTTimigGap = 0.;
52  FVVTDurationMultiplier = 1.;
53  FAngle0 = 0.;
54 }
55 // ---------------------------------------------------------------------------
56 // ---------------------------------------------------------------------------
57 
58 TValvula4T::~TValvula4T() {
59 
60  delete fun_Torb;
61  delete fun_FLift;
62  delete fun_CDin;
63  delete fun_CDout;
64 }
65 
66 // ---------------------------------------------------------------------------
67 // ---------------------------------------------------------------------------
68 
69 TValvula4T::TValvula4T(TValvula4T *Origen, int Valvula) :
70  TTipoValvula(nmValvula4T) {
71 
72  FCDEntrada = Origen->FCDEntrada;
73  FCDSalida = Origen->FCDSalida;
74  FDiametro = Origen->FDiametro;
75  FDiametroRef = Origen->FDiametroRef;
76  FAnguloApertura = Origen->FAnguloApertura;
77  FAnguloApertura0 = Origen->FAnguloApertura0;
78  FAnguloCierre = Origen->FAnguloCierre;
79  FCoefTorbMedio = Origen->FCoefTorbMedio;
80  FIncrLev = Origen->FIncrLev;
81  FIncrAng = Origen->FIncrAng;
82  FNumeroOrden = Origen->FNumeroOrden;
83  FAngle0 = Origen->FAngle0;
84 
85  FValvula = Valvula;
86 
87  FDiamRef = FDiametroRef;
88 
89  FEngine = Origen->FEngine;
90 
91  FAngle.resize(Origen->FAngle.size());
92  FLevantamiento.resize(Origen->FLevantamiento.size());
93  FDatosCDEntrada.resize(Origen->FDatosCDEntrada.size());
94  FDatosCDSalida.resize(Origen->FDatosCDSalida.size());
95  FDatosTorbellino.resize(Origen->FDatosTorbellino.size());
96  FLiftCD.resize(Origen->FLiftCD.size());
97 
98  for(dVector::size_type i = 0; i < Origen->FAngle.size(); i++) {
99  FAngle[i] = Origen->FAngle[i];
100  FLevantamiento[i] = Origen->FLevantamiento[i];
101  }
102  fun_FLift = new Hermite_interp(FAngle, FLevantamiento);
103 
104  for(dVector::size_type i = 0; i < Origen->FLiftCD.size(); i++) {
105  FLiftCD[i] = Origen->FLiftCD[i];
106  FDatosCDEntrada[i] = Origen->FDatosCDEntrada[i];
107  FDatosCDSalida[i] = Origen->FDatosCDSalida[i];
108  FDatosTorbellino[i] = Origen->FDatosTorbellino[i];
109  }
110 
111  fun_CDin = new Hermite_interp(FLiftCD, FDatosCDEntrada);
112  fun_CDout = new Hermite_interp(FLiftCD, FDatosCDSalida);
113  fun_Torb = new Hermite_interp(FLiftCD, FDatosTorbellino);
114 
115  FRegimen = Origen->getRegimen();
116  FRelacionVelocidades = Origen->getRelacionVelocidades();
117  FControlRegimen = Origen->getControlRegimen();
118 
119  FVVTLift = Origen->FVVTLift;
120  FVVTTiming = Origen->FVVTTiming;
121  FVVTDuration = Origen->FVVTDuration;
122 
123  FVVT = Origen->FVVT;
124 
125  FVVTLiftMultiplier = Origen->FVVTLiftMultiplier;
126  FVVTTimigGap = Origen->FVVTTimigGap;
127  FVVTDurationMultiplier = Origen->FVVTDurationMultiplier;
128 
129  FVVTLiftCtrlID = Origen->FVVTLiftCtrlID;
130  FVVTTimingCtrlID = Origen->FVVTTimingCtrlID;
131  FVVTDurationCtrlID = Origen->FVVTDurationCtrlID;
132 }
133 
134 // ---------------------------------------------------------------------------
135 // ---------------------------------------------------------------------------
136 
137 void TValvula4T::LeeDatosIniciales(const char *FileWAM, fpos_t &filepos, int norden, bool HayMotor,
138  TBloqueMotor *Engine) {
139  try {
140  int ControlRegimen, NumLev = 0, NumCD = 0;
141 
142  FEngine = Engine;
143 
144  FILE *fich = fopen(FileWAM, "r");
145  fsetpos(fich, &filepos);
146 
147  FNumeroOrden = norden;
148 
149  fscanf(fich, "%lf %d %lf %lf %lf %lf ", &FDiametro, &NumLev, &FIncrAng, &FAnguloApertura, &FDiametroRef,
150  &FCoefTorbMedio);
151 
152  FAnguloApertura0 = FAnguloApertura;
153 
154  FAnguloCierre = FAnguloApertura + (double)(NumLev - 1) * FIncrAng;
155 
156  // FLevantamiento=new double[NumLev];
157  FLevantamiento.resize(NumLev);
158  FAngle.resize(NumLev);
159 
160  for(int i = 0; i < NumLev; i++) {
161  FAngle[i] = (double) i * FIncrAng;
162  fscanf(fich, " %lf", &FLevantamiento[i]);
163  }
164 
165  fscanf(fich, "%d %lf ", &NumCD, &FIncrLev);
166 
167  FLiftCD.resize(NumCD);
168  FDatosCDEntrada.resize(NumCD);
169  FDatosCDSalida.resize(NumCD);
170  FDatosTorbellino.resize(NumCD);
171 
172  for(int i = 0; i < NumCD; i++) {
173  FLiftCD[i] = (double) i * FIncrLev;
174  fscanf(fich, " %lf", &FDatosCDEntrada[i]);
175  }
176  for(int i = 0; i < NumCD; i++) {
177  fscanf(fich, " %lf", &FDatosCDSalida[i]);
178  }
179  for(int i = 0; i < NumCD; i++) {
180  fscanf(fich, " %lf", &FDatosTorbellino[i]);
181  }
182 
183  fscanf(fich, "%d ", &ControlRegimen);
184 
185  switch(ControlRegimen) {
186  case 0:
187  FControlRegimen = nmPropio;
188  break;
189  case 1:
190  FControlRegimen = nmMotor;
191  break;
192  }
193  if(FControlRegimen == nmPropio) {
194  fscanf(fich, "%lf ", &FRegimen);
195  FRelacionVelocidades = 1.;
196  } else if(FControlRegimen == nmMotor && HayMotor) {
197  fscanf(fich, "%lf ", &FRelacionVelocidades);
198  } else {
199  std::cout << "ERROR: TValvula4T::LeeDatosIniciales Lectura del Control del Regimen erronea " << std::endl;
200  throw Exception(" ");
201  }
202  int controllers = 0;
203  int param = 0;
204  fscanf(fich, "%d ", &controllers);
205  for(int i = 0; i < controllers; i++) {
206  fscanf(fich, "%d ", &param);
207  switch(param) {
208  case 0:
209  fscanf(fich, "%d ", &FVVTTimingCtrlID);
210  FVVTLift = true;
211  break;
212  case 1:
213  fscanf(fich, "%d ", &FVVTLiftCtrlID);
214  FVVTTiming = true;
215  break;
216  case 2:
217  fscanf(fich, "%d", &FVVTDurationCtrlID);
218  FVVTDuration = true;
219  break;
220  }
221  if(FVVTLift || FVVTTiming || FVVTDuration)
222  FVVT = true;
223  }
224 
225  fgetpos(fich, &filepos);
226  fclose(fich);
227 
228  } catch(exception & N) {
229  std::cout << "ERROR: LeeDatosIniciales Valvula4T" << std::endl;
230  // std::cout << "Tipo de error: " << N.what().scr() << std::endl;
231  throw Exception(N.what());
232 
233  }
234 }
235 
236 // ---------------------------------------------------------------------------
237 // ---------------------------------------------------------------------------
238 
239 void TValvula4T::CalculaCD(double Angulo) {
240  try {
241  double X = 0., XLv = 0., XCd = 0.;
242 
243  if(Angulo <= FAnguloApertura || Angulo >= FAnguloCierre) {
244  FCDTubVol = 0.;
245  FCDVolTub = 0.;
246  } else {
247  X = Angulo - FAnguloApertura;
248  XLv = X / (FIncrAng * FVVTDurationMultiplier);
249 
250  FApertura = fun_FLift->interp(XLv) * FVVTLiftMultiplier;
251 
252  if(FApertura < 0)
253  FApertura = 0;
254 
255  FCDTubVol = fun_CDin->interp(FApertura) * FSectionRatio;
256  FCDVolTub = fun_CDout->interp(FApertura) * FSectionRatio;
257  FCTorb = fun_Torb->interp(FApertura);
258 
259  }
260  } catch(exception & N) {
261  std::cout << "ERROR: LeeDatosIniciales Valvula4T" << std::endl;
262  // std::cout << "Tipo de error: " << N.what().scr() << std::endl;
263  throw Exception(N.what());
264 
265  }
266 }
267 
268 void TValvula4T::GetCDin(double Time) {
269 
270  double X = 0., XLv = 0., XCd = 0., Angulo = 0.;
271 
272  if(FControlRegimen == nmPropio) {
273  Angulo = 6. * FRegimen * Time; // It's correct if FRegimen is constant.
274  } else {
275  if(FToCylinder) {
276  Angulo = FCylinder->getAnguloActual();
277  } else {
278  double DeltaT = Time - FTime0;
279  FTime0 = Time;
280  double DeltaA = 6 * FEngine->getRegimen() * DeltaT * FRelacionVelocidades;
281  Angulo = DeltaA + FAngle0;
282  if(Angulo > 360)
283  Angulo -= 360.;
284  FAngle0 = Angulo;
285  }
286  }
287 
288  if(Angulo <= FAnguloApertura || Angulo >= FAnguloCierre) {
289  FCDTubVol = 0.;
290  FCDVolTub = 0.;
291  } else {
292  X = Angulo - FAnguloApertura;
293  XLv = X / FVVTDurationMultiplier;
294  if(XLv > 720)
295  XLv -= 720.;
296 
297  FApertura = fun_FLift->interp(XLv) * FVVTLiftMultiplier;
298 
299  if(FApertura < 0)
300  FApertura = 0;
301 
302  FCDTubVol = fun_CDin->interp(FApertura) * FSectionRatio;
303  FCTorb = fun_Torb->interp(FApertura);
304  }
305 }
306 
307 void TValvula4T::GetCDout(double Time) {
308  double X = 0., XLv = 0., XCd = 0., Angulo = 0.;
309 
310  if(FControlRegimen == nmPropio) {
311  Angulo = 360. * FRegimen / 60. * Time; // It's correct if FRegimen is constant.
312  } else {
313  if(FToCylinder) {
314  Angulo = FCylinder->getAnguloActual();
315  } else {
316  double DeltaT = Time - FTime0;
317  FTime0 = Time;
318  double DeltaA = 6 * FEngine->getRegimen() * DeltaT * FRelacionVelocidades;
319  Angulo = DeltaA + FAngle0;
320  if(Angulo > 360)
321  Angulo -= 360.;
322  FAngle0 = Angulo;
323  }
324  }
325 
326  if(Angulo <= FAnguloApertura || Angulo >= FAnguloCierre) {
327  FCDTubVol = 0.;
328  FCDVolTub = 0.;
329  } else {
330  X = Angulo - FAnguloApertura;
331  XLv = X / FVVTDurationMultiplier;
332  if(XLv > 720)
333  XLv -= 720.;
334 
335  FApertura = fun_FLift->interp(XLv) * FVVTLiftMultiplier;
336 
337  if(FApertura < 0)
338  FApertura = 0;
339 
340  FCDVolTub = fun_CDout->interp(FApertura) * FSectionRatio;
341  FCTorb = fun_Torb->interp(FApertura);
342  }
343 }
344 
345 void TValvula4T::VVTControl(double Time) {
346  if(FVVT) {
347  if(FVVTLift) {
348  FVVTLiftMultiplier = FVVTLiftCtrl->Output(Time);
349  }
350  if(FVVTTiming) {
351  FVVTTimigGap = FVVTTimingCtrl->Output(Time);
352  }
353  if(FVVTDuration) {
354  FVVTDurationMultiplier = FVVTDurationCtrl->Output(Time);
355  }
356  FAnguloApertura = FAnguloApertura0 + FVVTTimigGap;
357  FAnguloCierre = FAnguloApertura + (double)(FAngle.size() - 1) * FIncrAng * FVVTDurationMultiplier;
358  }
359 }
360 
361 void TValvula4T::AsignaLevController(TController **Controller) {
362  if(FVVT) {
363  if(FVVTLift) {
364  FVVTLiftCtrl = Controller[FVVTLiftCtrlID - 1];
365  }
366  if(FVVTTiming) {
367  FVVTTimingCtrl = Controller[FVVTTimingCtrlID - 1];
368  }
369  if(FVVTDuration) {
370  FVVTDurationCtrl = Controller[FVVTDurationCtrlID - 1];
371  }
372  }
373 }
374 
375 #pragma package(smart_init)
TController
Definition: TController.h:37
TTipoValvula
Definition: TTipoValvula.h:53
Hermite_interp
Definition: Math_wam.h:311
TValvula4T
Definition: TValvula4T.h:45
Exception
Custom exception class.
Definition: Exception.hpp:39
TTubo.h
TBloqueMotor
Definition: TBloqueMotor.h:43
TController::Output
virtual double Output(double Time)=0