OpenWAM
INJECTION_RATE.cpp
1 #pragma hdrstop
2 // #include "simstruc.h"
3 #pragma hdrstop
4 #include<cstdio>
5 #include<cmath>
6 #include<string.h>
7 #include<stdlib.h>
8 #include<malloc.h>
9 // #include<conio.h>
10 
11 using namespace std;
12 
15 extern "C" void CALCULUS_OF_INJECTION_RATE(int INITIAL, double*SOP, double*MFI, double*SOI, double*EOI, double speed,
16  int CAI, double PCR, double*A, double*B, double*C, double*D, double*injection_rate, double*CAD_injection_rate);
17 
20 #pragma argsused
21 // int main(int argc,char **argv)
22 // {
23 //
24 // int counter,test_number,z,p,size_inlet_inj;
25 // int aux = 0;
26 // int CAI = 0; /* vectors point number according to crank angle degree */
27 // FILE *fich;
28 //
29 // char title[3000];
30 //
31 //
32 //
33 // int *INITIAL;
34 // double *A;
35 // double *B;
36 // double *C;
37 // double *D;
38 // double *PCR;
39 // double *speed;
40 // double **SOP;
41 // double **SOI;
42 // double **EOI;
43 // double **MFI;
44 // double PMI_hp = 0.;
45 //
46 // double **test_variables;
47 //
48 // double **injection_rate;
49 // double **CAD_injection_rate;
50 //
51 //
52 //
53 // CAI=1801;
54 // size_inlet_inj=CAI;
55 // test_number=1;
56 // z=0;
57 //
59 //
60 //
61 // PCR=(double *)malloc(test_number*sizeof(double ));
62 // speed=(double *)malloc(test_number*sizeof(double ));
63 // INITIAL=(int *)malloc(test_number*sizeof(int ));
64 //
65 // A=(double *)malloc(2*sizeof(double ));
66 // B=(double *)malloc(2*sizeof(double ));
67 // C=(double *)malloc(2*sizeof(double ));
68 // D=(double *)malloc(2*sizeof(double ));
69 //
70 // SOP=(double **)malloc(test_number*sizeof(double ));
71 // for (z=0;z<test_number;z++){
72 // SOP[z]=(double *)malloc(8*sizeof(double ));
73 // }
74 //
75 // SOI=(double **)malloc(test_number*sizeof(double ));
76 // for (z=0;z<test_number;z++){
77 // SOI[z]=(double *)malloc(8*sizeof(double ));
78 // }
79 // EOI=(double **)malloc(test_number*sizeof(double ));
80 // for (z=0;z<test_number;z++){
81 // EOI[z]=(double *)malloc(8*sizeof(double ));
82 // }
83 // MFI=(double **)malloc(test_number*sizeof(double ));
84 // for (z=0;z<test_number;z++){
85 // MFI[z]=(double *)malloc(8*sizeof(double ));
86 // }
87 //
88 // test_variables=(double **)malloc(test_number*sizeof(double ));
89 // for (z=0;z<test_number;z++){
90 // test_variables[z]=(double *)malloc(18*sizeof(double ));
91 // }
92 //
93 //
94 //
95 // //the injecition rate law is created with a function
96 //
97 // if((fich=fopen("injection_system_parameters.dat","r"))==NULL){
98 // printf("The file of average variables could not be open.\n");
99 // }
100 //
101 // fscanf(fich, "%s",title);
102 // fscanf(fich, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",&A[0],&A[1],&B[0],&B[1],&C[0],&C[1],&D[0],&D[1]);
103 // fclose(fich);
104 //
105 //
106 //
107 // INITIAL[0]=0;
108 //
109 // if((fich=fopen("injection_parameters.dat","r"))==NULL){
110 // printf("The file of average variables could not be open.\n");
111 // }
112 //
113 // fscanf(fich, "%s",title);
114 // for (p=0;p<8;p++){
115 // fscanf(fich, "%d,%lf,%lf,%lf",aux,&SOP[0][p],&MFI[0][p],&PMI_hp);
116 //
117 // if(MFI[0][p]>0){
118 // INITIAL[0]=INITIAL[0]+1;
119 // }
120 //
121 // }
122 // fclose(fich);
123 //
124 //
125 // if((fich=fopen("test_variables.dat","r"))==NULL){
126 // printf("The file of average variables could not be open.\n");
127 // return 0;
128 // }
129 // fscanf(fich, "%s",title);
130 //
131 // for(p=0;p<18;p++){
132 // fscanf(fich, "%lf,",&test_variables[0][p]);
133 // }
134 //
135 // fclose(fich);
136 //
137 // speed[0]=test_variables[0][0];
138 // PCR[0]=test_variables[0][5];
139 //
140 //
141 // injection_rate=(double **)malloc(test_number*sizeof(double ));
142 // for (z=0;z<test_number;z++){
143 // injection_rate[z]=(double *)malloc(CAI*sizeof(double ));
144 // }
145 //
146 // CAD_injection_rate=(double **)malloc(test_number*sizeof(double ));
147 // for (z=0;z<test_number;z++){
148 // CAD_injection_rate[z]=(double *)malloc(CAI*sizeof(double ));
149 // }
150 //
151 // //initializes injection rate angle
152 // for (z=0;z<test_number;z++){
153 // CAD_injection_rate[z][0]=-180;
154 // for(int counter=1;counter<CAI;counter++){
155 // CAD_injection_rate[z][counter]=CAD_injection_rate[z][counter-1]+((360./(CAI-1)));
156 // }
157 // }
158 //
159 // for (z=0;z<test_number;z++){
160 //
161 // CALCULUS_OF_INJECTION_RATE(INITIAL[z],SOP[z],MFI[z],SOI[z],EOI[z],speed[z],CAI,
162 // PCR[z],A,B,C,D,injection_rate[z],CAD_injection_rate[z]);
163 //
164 // }
165 //
166 //
167 //
168 // if((fich=fopen("injection_rate.dat","w"))==NULL){
169 // printf("The element file results could not be opened");
170 // exit(1);
171 // }
172 // strcpy(title,"CAD,IR");
173 //
174 // fprintf(fich, "%s", title);
175 // for(p=0;p<CAI;p++){
176 // fprintf(fich, "\n%lf,",CAD_injection_rate[0][p]);
177 // for(z=0;z<test_number;z++){
178 // fprintf(fich, "%lf",injection_rate[z][p]);
179 // }
180 // }
181 // fclose(fich);
182 //
183 // if((fich=fopen("injection_rate_parameters.dat","w"))==NULL){
184 // printf("The element file results could not be opened");
185 // exit(1);
186 // }
187 // strcpy(title,"INITIAL,SOI1,EOI1,SOI2,EOI2,SOI3,EOI3,SOI4,EOI4,SOI5,EOI5,SOI6,EOI6,SOI7,EOI7,SOI8,EOI8");
188 //
189 // fprintf(fich, "%s", title);
190 //
191 // fprintf(fich, "\n%d",INITIAL[0]);
192 // for(p=0;p<8;p++){
193 // fprintf(fich, ",%lf,%lf",SOI[0][p],EOI[0][p]);
194 // }
195 //
196 // fclose(fich);
197 //
199 // free(PCR);
200 // free(INITIAL);
201 // free(A);
202 // free(B);
203 // free(C);
204 // free(D);
205 //
206 //
207 // for(z=0;z<test_number;z++)free(SOP[z]);
208 // free(SOP);
209 // for(z=0;z<test_number;z++)free(SOI[z]);
210 // free(SOI);
211 // for(z=0;z<test_number;z++)free(EOI[z]);
212 // free(EOI);
213 // for(z=0;z<test_number;z++)free(MFI[z]);
214 // free(MFI);
215 //
216 // for(z=0;z<test_number;z++)free(test_variables[z]);
217 // free(test_variables);
218 //
219 // for (z=0;z<test_number;z++)free(injection_rate[z]);
220 // free(injection_rate);
221 // for (z=0;z<test_number;z++)free(CAD_injection_rate[z]);
222 // free(CAD_injection_rate);
223 //
224 //
225 // }
226 
227 inline void CALCULUS_OF_INJECTION_RATE(int INITIAL, double *SOP, double *MFI, double *SOI, double *EOI, double speed,
228  int CAI, double PCR, double *A, double *B, double *C, double *D, double *injection_rate, double *CAD_injection_rate)
229 
230 {
231 
232  int k = 0;
233  // initialize injection rate
234  injection_rate[0] = 0.;
235  for(k = 1; k < CAI; k++) {
236  injection_rate[k] = 0.;
237  }
238 
239  double a = (A[1] * PCR + A[0]) * 1e3;
240  double b = (B[1] * PCR + B[0]) * 1e3;
241  double c = (C[1] * PCR + C[0]) * 1e3;
242 
243  // definition of injection rate for each injection
244 
245  double mftopemini = 1. / 2. * ((b / a) + (b / -c)) * b;
246  double time = 0.;
247 
248  for(int j = 0; j < INITIAL; j++) {
249 
250  SOI[j] = SOP[j] + (D[1] * PCR + D[0]) * (6 * speed);
251 
252  // calculates total fuel mass injected
253 
254  if(MFI[j] >= mftopemini) {
255  double DLM = (MFI[j] - mftopemini) / b;
256  for(int k = 0; k < CAI; k++) {
257  // time between SOI and actual point
258  time = (CAD_injection_rate[k] - SOI[j]) / (6 * speed);
259  // calculates injection rate
260  if(time > 0. && time <= b / a)
261  injection_rate[k] = time * a;
262  else if(time > b / a && time <= b / a + DLM)
263  injection_rate[k] = b;
264  else if(time > b / a + DLM && time <= b / a + DLM + b / -c)
265  injection_rate[k] = b + ((time - b / a - DLM) * c);
266  }
267  // calculates end of injection
268  EOI[j] = SOI[j] + (b / a + DLM + b / -c) * 6 * speed;
269  } else {
270  double levmaxi = sqrt((2. * MFI[j]) / (1. / a + 1. / -c));
271  double DTL = levmaxi / a;
272  double DTB = levmaxi / -c;
273  for(int k = 0; k < CAI; k++) {
274  // time between SOI and actual point
275  time = (CAD_injection_rate[k] - SOI[j]) / (6 * speed);
276  if(time > 0. && time <= DTL)
277  injection_rate[k] = time * a;
278  else if(time > DTL && time <= DTL + DTB)
279  injection_rate[k] = levmaxi + (time - DTL) * c;
280  }
281  // calculates end of injection
282  EOI[j] = SOI[j] + (DTL + DTB) * 6 * speed;
283  }
284  }
285 
286  // converts mg/s to g/s
287  for(int k = 0; k < CAI; k++) {
288  injection_rate[k] /= 1000.;
289  }
290 
291 }