FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
CIEngine.h
1#pragma once
2
86#include <string>
87#include <fhsim/simobject/SimObject.h>
88#include <fhsim/PrintDuringExec.h>
89
90
92static const double gd_PI = 3.14159265358979323846;
93
95//==================================================================================================
96// Structs for model parameters that are read from xml file
97//==================================================================================================
98
99// GP
100typedef struct
101{
102 double cp_air; // Specific heat of air
103 double cv_air; // Specific heat of air
104 double cp_ex; // Specific heat of exhaust
105 double cv_ex; // Specific heat of exhaust
106 double pAmb; // Ambient pressure
107 double TAmb; // Ambient temperature
109
110// CC
111typedef struct
112{
113 double V_d; // Displaced volume 1 cylinder
114 double n_cyl; // Number of cylinders
115 double AFRStoich; // Stoichiometric air to fuel ratio
116 double Q_LHV; // Fuel lower heating value
117 double VolEff; // Volumetric efficiency
118 double CCEff; // Combustion chamber efficiency
119 double CR; // Compression ratio
121
122// InM
123typedef struct
124{
125 double Vol; // manifold volume
127
128// ExM
129typedef struct
130{
131 double Vol; // manifold volume
133
134// CS
135typedef struct
136{
137 double I_eng; // Inertia of crankshaft and flywheel
138 double M_fric_rps; // Friction torque pr rps
140
141// TC
142typedef struct
143{
144 double Inertia; // Inertia of TC and shaft
145 double TrbEff; // Turbine efficiency, should be implemented as map
146 double CmpEff; // Compressor efficiency, should be implemented as map
147 double WGRef; // Wastegate reference pressure
149
150// IC
151typedef struct
152{
153 double Eff; // Intercooler efficiency
155
156// Model parameters
157typedef struct
158{
159 GPModParType m_GP; // Global Parameters
160 CCModParType m_CC; // Combustion Chamber parameters
161 InMModParType m_InM; // Intake manifold parameters
162 ExMModParType m_ExM; // Exhaust manifold parameters
163 CSModParType m_CS; // Crank shaft parameters
164 TCModParType m_TC; // Turbo Charger parameters
165 ICModParType m_IC; // Intercooler parameters
167
169//==================================================================================================
170// Model input structs
171//==================================================================================================
172
173
174typedef struct
175{
176 ISignalPort* m_M_load; // Torque load from prop shaft
177 ISignalPort* m_N_eng_SP; // Engine speed set-point
178 ISignalPort* m_pInM_SP; // Intake manifold pressure setpoint
179 ISignalPort* m_MinLambda_SP;// Minimum allowed lambda setpoint
181
183//==================================================================================================
184// Model variables structs
185//==================================================================================================
186
187// CC
188typedef struct CCMVT
189{
190 double IMEP; // Indicated Mean Effective Pressure
191 double IP; // Indicated Power
192 double M_CC; // Torque to CS
193 double W_InM; // Mass flow intake manifold
194 double T_ExP; // Temperature flow exhaust port
195 double X; // Amount of exhaust gas
196 double W_ExP; // Mass flow exhaust port
197 double W_fuel; // Fuel mass flow
198 double Lambda; // Lambda
199
200 CCMVT()
201 {
202 IMEP = IP = M_CC = W_InM = T_ExP = X = W_ExP = W_fuel = Lambda = 0;
203 }
205
206// InM
207typedef struct InMMVT
208{
209 double p; // Pressure
210 double T; // Temperature
211
212 InMMVT()
213 {
214 p = T = 0;
215 }
217
218// ExM
219typedef struct ExMMVT
220{
221 double p; // Pressure
222 double T; // Temperature
223
224 ExMMVT()
225 {
226 p = T = 0;
227 }
229
230// CS
231typedef struct CSMVT
232{
233 double M_eng; // Engine torque
234 double N_eng; // Engine speed
235 double P_eng; // Engine power
236
237 CSMVT()
238 {
239 M_eng = N_eng = P_eng = 0;
240 }
242
243// TC
244typedef struct TCMVT
245{
246 double W_trb; // Turbine mass flow
247 double T_cmp; // Compressor temperature outlet flow
248 double W_cmp; // Mass flow compressor
249 double M_trb; // Turbine torque
250 double M_cmp; // Compressor torque
251 double N_TC; // TC speed
252 double k; // k in compressor map calc
253 double PR_trb; // Pressure ratio turbine
254 double PR_cmp; // Pressure ratio compressor
255
256 TCMVT()
257 {
258 W_trb = T_cmp = W_cmp = M_trb = M_cmp = N_TC = k = PR_trb = PR_cmp = 1;
259 }
261
262// IC
263typedef struct ICMVT
264{
265 double T_IC; // Temperature IC outlet
266
267 ICMVT()
268 {
269 T_IC = 1;
270 }
272
273// ECU
274typedef struct ECUMVT
275{
276 double FuelPrCycle; // Injected fuel pr combustion cycle
277 double WGOpening; // Waste gate opening setpoint
278 double MaxFuelPrCycle; // Max allowed fuel pr cycle
279
280 ECUMVT()
281 {
282 WGOpening = 1;
283 MaxFuelPrCycle = FuelPrCycle = 1e-3;
284 }
286
287// Model variables
288typedef struct
289{
290 CCModVarType m_CC;
291 InMModVarType m_InM;
292 ExMModVarType m_ExM;
293 CSModVarType m_CS;
294 TCModVarType m_TC;
295 ICModVarType m_IC;
296 ECUModVarType m_ECU;
298
300//==================================================================================================
301// Integrator anti windup type
302//==================================================================================================
303typedef struct
304{
305 double Lower; // Lower saturation limit
306 double Upper; // Upper saturation limit
308
310//==================================================================================================
311// State definition
312//==================================================================================================
313typedef struct
314{
315 int mI_SIndex; // The index of the state
316 IAWSatLimitType m_AWL; // Integrator anti windup saturation limit
317 //double *pd_VarPtr; // Pointer to model variable for this state
318}StateType;
319
321//==================================================================================================
322// PI controller definition
323//==================================================================================================
324typedef struct
325{
326 double e; // Deviation
327 double Kp; // Proortional gain
328 double Ti; // Integral time
329 IAWSatLimitType PIOutSatLim;// PI controller output saturation limits
330}PIType;
331
333//==================================================================================================
334// Class definition
335//==================================================================================================
336
337class CCIEngine : public SimObject
338{
339public:
341 CCIEngine(const std::string& simobjectname, ISimObjectCreator* const creator);
342
344 // CC
345 const double* out_CC_IMEP(const double T, const double* const X);
346 const double* out_CC_IP(const double T, const double* const X);
347 const double* out_CC_Lambda(const double T, const double* const X);
348 const double* out_CC_M_CC(const double T, const double* const X);
349 const double* out_CC_T_ExP(const double T, const double* const X);
350 const double* out_CC_W_ExP(const double T, const double* const X);
351 const double* out_CC_W_fuel(const double T, const double* const X);
352 const double* out_CC_W_InM(const double T, const double* const X);
353 const double* out_CC_X(const double T, const double* const X);
354
355 // InM
356 const double* out_InM_p(const double T, const double* const X);
357
358 // ExM
359 const double* out_ExM_p(const double T, const double* const X);
360
361 // CS
362 const double* out_CS_M_eng(const double T, const double* const X);
363 const double* out_CS_N_eng(const double T, const double* const X);
364 const double* out_CS_P_eng(const double T, const double* const X);
365
366 // TC
367 const double* out_TC_K_cmp(const double T, const double* const X);
368 const double* out_TC_M_trb(const double T, const double* const X);
369 const double* out_TC_M_cmp(const double T, const double* const X);
370 const double* out_TC_PR_cmp(const double T, const double* const X);
371 const double* out_TC_PR_trb(const double T, const double* const X);
372 const double* out_TC_T_cmp(const double T, const double* const X);
373 const double* out_TC_W_cmp(const double T, const double* const X);
374 const double* out_TC_W_trb(const double T, const double* const X);
375 const double* out_TC_N_TC(const double T, const double* const X);
376
377 // IC
378 const double* out_IC_T_IC(const double T, const double* const X);
379
380 // ECU
381 const double* out_ECU_FuelPrCyc(const double T, const double* const X);
382 const double* out_ECU_MaxFuelPrCyc(const double T, const double* const X);
383 const double* out_ECU_WGOpening(const double T, const double* const X);
384
386 void OdeFcn(const double T, const double *const X, double *const XDot) const;
387
388#ifdef FH_VISUALIZATION
389
391 virtual void RenderInit(Ogre::Root* const ogreRoot, ISimObjectCreator* const creator);
392
394 virtual void RenderUpdate(const double T, const double* const X);
395#endif
396
397protected:
400 {
401 double p_InM = 0, p_ExM = 0, N_eng = 0, N_TC = 0, PI_N_eng = 0, PI_Lambda = 0, PI_WG = 0;
402 };
403
405 void EvaluateModel(const double T, const double* const X, StateDerivatives& derivatives) const;
406
408 void Compute(const double T, const double* const X) const { EvaluateModel(T,X,m_Derivatives); }
409
411 void EvaluateModelAt(const double T, const double* const X) const { m_compute->ComputeFunction(T,X); }
412
414 bool LoadParameterFile(ISimObjectCreator* creator);
415
416 // Integrator anti windup
417 double IntegratorAntiWindup(const double* const adX, const StateType *p_StateInfo, double Dot) const;
418
419 double CalcInM() const; // Inlet manifold calculations
420 void CalcCC() const; // Combustion chamber calculations
421 double CalcExM() const; // Exhaust manifold calculations
422 void CalcCompressor() const; // Turbo charger compressor calculations
423 void CalcTurbine() const; // Turbo charger turbine calculations
424 double CalcTC() const; // Turbo charger shaft: N_TC_dot
425 double CalcCS(const double T, const double* const X) const; // Crank shaft calculations
426 void CalcIC() const; // Intercooler calculations
427 double CalcECUWG(const double T, const double* const X) const; // ECU wastegate control
428 double CalcECUMaxFuelPrCycle(const double T, const double* const X) const; // ECU Max allowed fuel pr cycle control
429 double CalcECUFuelPrCycle(const double T, const double* const X) const; // ECU fuel pr cycle controller
430
431 // PI controller
432 double PIController(PIType *p_PI, StateType *p_State, const double* const adX) const;
433
436
438 StateType mS_p_InM; // Pressure intake manifold
439 StateType mS_p_ExM; // Pressure Exhaust manifold
440 StateType mS_N_eng; // Engine speed
441 StateType mS_N_TC; // Turbo charger speed
442
443 mutable StateType mS_PI_N_eng; // Integrator for ECU PI speed controller
444 mutable StateType mS_PI_Lambda; // Integrator for ECU Lambda controller
445 mutable StateType mS_PI_WG; // Integrator for ECU Wastegate opening controller
446
449
451 mutable ModVarType m_Var{}; // intermediate values of the last Compute
452 mutable StateDerivatives m_Derivatives{}; // state derivatives of the last Compute
453
454 ICommonComputation* m_compute = nullptr; // runs Compute once per (T, X)
455
456 PrintDuringExec* m_Print; // Pointer to PrintDuringExec
457
458#ifdef FH_VISUALIZATION
459 Ogre::SceneNode* m_RenderNode;
460#endif
461};
Definition CIEngine.h:338
ModelInType m_InMod
Model input indexes.
Definition CIEngine.h:448
const double * out_CC_IMEP(const double T, const double *const X)
Output functions.
bool LoadParameterFile(ISimObjectCreator *creator)
Loads engine parameters from xml file.
void EvaluateModelAt(const double T, const double *const X) const
Sets m_Var at (T, X) for an output port, which has no use for the derivatives.
Definition CIEngine.h:411
CCIEngine(const std::string &simobjectname, ISimObjectCreator *const creator)
The constructor sets the pointer to the output object and the parser object.
ModParType m_ModPar
Model parameters.
Definition CIEngine.h:435
ModVarType m_Var
Model variables used in the different submodels.
Definition CIEngine.h:451
void OdeFcn(const double T, const double *const X, double *const XDot) const
Calculates the state derivatives.
StateType mS_p_InM
States.
Definition CIEngine.h:438
void Compute(const double T, const double *const X) const
The common computation: sets m_Var and m_Derivatives at (T, X). The engine runs it once after each Re...
Definition CIEngine.h:408
void EvaluateModel(const double T, const double *const X, StateDerivatives &derivatives) const
Runs the submodels at (T, X): sets m_Var and the derivatives.
The derivatives of the states that the submodels calculate.
Definition CIEngine.h:400
Definition CIEngine.h:189
Definition CIEngine.h:112
Definition CIEngine.h:232
Definition CIEngine.h:136
Definition CIEngine.h:275
Definition CIEngine.h:220
Definition CIEngine.h:130
Definition CIEngine.h:101
Definition CIEngine.h:304
Definition CIEngine.h:264
Definition CIEngine.h:152
Definition CIEngine.h:208
Definition CIEngine.h:124
Definition CIEngine.h:158
Definition CIEngine.h:289
Definition CIEngine.h:175
Definition CIEngine.h:325
Definition CIEngine.h:314
Definition CIEngine.h:245
Definition CIEngine.h:143