OpenWAM
TValvulaContr.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 "TValvulaContr.h"
32 #include "TBloqueMotor.h"
33 
34 //---------------------------------------------------------------------------
35 //---------------------------------------------------------------------------
36 TValvulaContr::TValvulaContr() :
37  TTipoValvula(nmValvulaContr) {
38 
39  FAngle0 = 0;
40 }
41 
42 //---------------------------------------------------------------------------
43 //---------------------------------------------------------------------------
44 
45 TValvulaContr::~TValvulaContr() {
46 
47 }
48 
49 //---------------------------------------------------------------------------
50 //---------------------------------------------------------------------------
51 
52 TValvulaContr::TValvulaContr(TValvulaContr *Origen, int Valvula) :
53  TTipoValvula(nmValvulaContr) {
54 
55  FTipoContr = Origen->FTipoContr;
56  FLimiteInf1 = Origen->FLimiteInf1;
57  FLimiteInf2 = Origen->FLimiteInf2;
58  FLimiteSup1 = Origen->FLimiteSup1;
59  FLimiteSup2 = Origen->FLimiteSup2;
60  FCDInicial = Origen->FCDInicial;
61  FCDFinal = Origen->FCDFinal;
62  FAngle0 = Origen->FAngle0;
63 
64  FValvula = Valvula;
65 
66  FDiamRef = -1;
67 
68  FNumeroOrden = Origen->FNumeroOrden;
69 
70 }
71 
72 //---------------------------------------------------------------------------
73 //---------------------------------------------------------------------------
74 
75 void TValvulaContr::LeeDatosIniciales(const char *FileWAM, fpos_t &filepos, int norden, bool HayMotor,
76  TBloqueMotor *Engine) {
77  try {
78  int Ctrl = 0;
79 
80  FILE *fich = fopen(FileWAM, "r");
81  fsetpos(fich, &filepos);
82 
83  FNumeroOrden = norden;
84 
85  FEngine = Engine;
86 
87  fscanf(fich, "%d %lf %lf %lf %lf %lf %lf ", &Ctrl, &FLimiteInf1, &FLimiteInf2, &FLimiteSup1, &FLimiteSup2, &FCDInicial,
88  &FCDFinal);
89 
90  switch(Ctrl) {
91  case 0:
92  FTipoContr = nmContrAngulo;
93  break;
94  case 1:
95  FTipoContr = nmContrFuel;
96  break;
97  }
98 
99  fgetpos(fich, &filepos);
100  fclose(fich);
101 
102  } catch(exception &N) {
103  std::cout << "ERROR: LeeDatosIniciales ValvulaContr" << std::endl;
104  //std::cout << "Tipo de error: " << N.what().scr() << std::endl;
105  throw Exception(N.what());
106 
107  }
108 }
109 
110 //---------------------------------------------------------------------------
111 //---------------------------------------------------------------------------
112 
113 void TValvulaContr::CalculaCD(double AnguloActual, double Mf) {
114  try {
115  double xx = 0., acoef = 0., bcoef = 0.;
116 
117  if(FTipoContr == nmContrAngulo)
118  xx = AnguloActual;
119  else if(FTipoContr == nmContrFuel)
120  xx = Mf;
121  else {
122  printf("ERROR: This type of controlled valve is not implemented");
123  throw Exception("ERROR: This type of controlled valve is not implemented");
124  }
125  if(FLimiteSup1 == FLimiteSup2) {
126  if(xx >= FLimiteSup1) {
127  FCDTubVol = FCDFinal;
128  FCDVolTub = FCDFinal;
129  }
130  } else if(FLimiteSup1 < FLimiteSup2) {
131  if(xx >= FLimiteSup1 && xx < FLimiteSup2) {
132  acoef = (FCDFinal - FCDInicial) / (FLimiteSup2 - FLimiteSup1);
133  bcoef = FCDInicial - acoef * FLimiteSup1;
134  FCDTubVol = acoef * xx + bcoef;
135  FCDVolTub = acoef * xx + bcoef;
136  }
137  if(xx >= FLimiteSup2) {
138  FCDTubVol = FCDFinal;
139  FCDVolTub = FCDFinal;
140  }
141  } else {
142  printf("ERROR: in hysteresis valve (EGR,Swirl...");
143  throw Exception("ERROR: in hysteresis valve (EGR,Swirl...)");
144  }
145  if(FLimiteInf1 == FLimiteInf2) {
146  if(xx <= FLimiteInf1) {
147  FCDTubVol = FCDInicial;
148  FCDVolTub = FCDInicial;
149  }
150  } else if(FLimiteInf1 > FLimiteInf2) {
151  if(xx <= FLimiteInf1 && xx > FLimiteInf2) {
152  acoef = (FCDInicial - FCDFinal) / (FLimiteInf2 - FLimiteInf1);
153  bcoef = FCDInicial - acoef * FLimiteInf2;
154  FCDTubVol = acoef * xx + bcoef;
155  FCDVolTub = acoef * xx + bcoef;
156  }
157  if(xx <= FLimiteInf2) {
158  FCDTubVol = FCDInicial;
159  FCDVolTub = FCDFinal;
160  }
161  } else {
162  printf("ERROR: in hysteresis valve (EGR,Swirl...");
163  throw Exception("ERROR: in hysteresis valve (EGR,Swirl...)");
164  }
165  } catch(exception &N) {
166  std::cout << "ERROR: CalculaCD ValvulaContr" << std::endl;
167  //std::cout << "Tipo de error: " << N.what().scr() << std::endl;
168  throw Exception(N.what());
169 
170  }
171 }
172 
173 void TValvulaContr::GetCDin(double Time) {
174 
175  double xx = 0., acoef = 0., bcoef = 0.;
176 
177  if(FTipoContr == nmContrAngulo) {
178  double DeltaT = Time - FTime0;
179  FTime0 = Time;
180  double DeltaA = 6 * FEngine->getRegimen() * DeltaT;
181  xx = DeltaA + FAngle0;
182  FAngle0 = xx;
183  } else if(FTipoContr == nmContrFuel) {
184  xx = FEngine->getMasaFuel();
185  } else {
186  printf("ERROR: This type of controlled valve is not implemented");
187  throw Exception("ERROR: This type of controlled valve is not implemented");
188  }
189  if(FLimiteSup1 == FLimiteSup2) {
190  if(xx >= FLimiteSup1) {
191  FCDTubVol = FCDFinal;
192  }
193  } else if(FLimiteSup1 < FLimiteSup2) {
194  if(xx >= FLimiteSup1 && xx < FLimiteSup2) {
195  acoef = (FCDFinal - FCDInicial) / (FLimiteSup2 - FLimiteSup1);
196  bcoef = FCDInicial - acoef * FLimiteSup1;
197  FCDTubVol = acoef * xx + bcoef;
198  }
199  if(xx >= FLimiteSup2) {
200  FCDTubVol = FCDFinal;
201  }
202  } else {
203  printf("ERROR: in hysteresis valve (EGR,Swirl...");
204  throw Exception("ERROR: in hysteresis valve (EGR,Swirl...)");
205  }
206  if(FLimiteInf1 == FLimiteInf2) {
207  if(xx <= FLimiteInf1) {
208  FCDTubVol = FCDInicial;
209  }
210  } else if(FLimiteInf1 > FLimiteInf2) {
211  if(xx <= FLimiteInf1 && xx > FLimiteInf2) {
212  acoef = (FCDInicial - FCDFinal) / (FLimiteInf2 - FLimiteInf1);
213  bcoef = FCDInicial - acoef * FLimiteInf2;
214  FCDTubVol = acoef * xx + bcoef;
215  }
216  if(xx <= FLimiteInf2) {
217  FCDTubVol = FCDInicial;
218  }
219  } else {
220  printf("ERROR: in hysteresis valve (EGR,Swirl...");
221  throw Exception("ERROR: in hysteresis valve (EGR,Swirl...)");
222  }
223 }
224 
225 void TValvulaContr::GetCDout(double Time) {
226  double xx = 0., acoef = 0., bcoef = 0.;
227 
228  if(FTipoContr == nmContrAngulo) {
229  double DeltaT = Time - FTime0;
230  FTime0 = Time;
231  double DeltaA = 6 * FEngine->getRegimen() * DeltaT;
232  xx = DeltaA + FAngle0;
233  FAngle0 = xx;
234  } else if(FTipoContr == nmContrFuel) {
235  xx = FEngine->getMasaFuel();
236  } else {
237  printf("ERROR: This type of controlled valve is not implemented");
238  throw Exception("ERROR: This type of controlled valve is not implemented");
239  }
240  if(FLimiteSup1 == FLimiteSup2) {
241  if(xx >= FLimiteSup1) {
242  FCDVolTub = FCDFinal;
243  }
244  } else if(FLimiteSup1 < FLimiteSup2) {
245  if(xx >= FLimiteSup1 && xx < FLimiteSup2) {
246  acoef = (FCDFinal - FCDInicial) / (FLimiteSup2 - FLimiteSup1);
247  bcoef = FCDInicial - acoef * FLimiteSup1;
248  FCDVolTub = acoef * xx + bcoef;
249  }
250  if(xx >= FLimiteSup2) {
251  FCDVolTub = FCDFinal;
252  }
253  } else {
254  printf("ERROR: in hysteresis valve (EGR,Swirl...");
255  throw Exception("ERROR: in hysteresis valve (EGR,Swirl...)");
256  }
257  if(FLimiteInf1 == FLimiteInf2) {
258  if(xx <= FLimiteInf1) {
259  FCDVolTub = FCDInicial;
260  }
261  } else if(FLimiteInf1 > FLimiteInf2) {
262  if(xx <= FLimiteInf1 && xx > FLimiteInf2) {
263  acoef = (FCDInicial - FCDFinal) / (FLimiteInf2 - FLimiteInf1);
264  bcoef = FCDInicial - acoef * FLimiteInf2;
265  FCDVolTub = acoef * xx + bcoef;
266  }
267  if(xx <= FLimiteInf2) {
268  FCDVolTub = FCDFinal;
269  }
270  } else {
271  printf("ERROR: in hysteresis valve (EGR,Swirl...");
272  throw Exception("ERROR: in hysteresis valve (EGR,Swirl...)");
273  }
274 }
275 
276 #pragma package(smart_init)
TTipoValvula
Definition: TTipoValvula.h:53
TValvulaContr
Definition: TValvulaContr.h:46
Exception
Custom exception class.
Definition: Exception.hpp:39
TBloqueMotor
Definition: TBloqueMotor.h:43