aboutsummaryrefslogtreecommitdiffstats
path: root/engine.c
blob: d0a1b38b481def1e914464bf0e70982138c469ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <math.h>
#include <string.h>

#include "unit.h"
#include "engine.h"


/* A four-stroke piston engine takes two revolutions per cycle. */
static const double REV_PER_CYCLE = 2.0;

/* Specific heat of dry air at constant pressure at T=300K [J/(kg*K)]. */
static const double C_P_AIR = 1005.0;

/* Specific heat of dry air at constant volume at T=300K [J/(kg*K)]. */
static const double C_V_AIR = 718.0;

/* Heat capacity ratio of dry air at T=300K [J/(kg*K)]. */
static const double GAMMA_AIR = C_P_AIR / C_V_AIR;

/* Gas constant of air [J/(kg*K)]. */
static const double R_AIR = 287.05;


static VolumeFlowRate port_volume_flow_rate(const Engine *e);
static double density_ratio(const Engine *e);


void
init_engine(Engine *e) {
	memset(e, 0, sizeof(*e));
}

Pressure
comp_outlet_pressure(const Engine *e) {
	return e->map + e->intercooler_deltap;
}

/* Pressure ratio across the compressor. */
double
pressure_ratio(const Engine *e) {
	Pressure p1, p2;

	p1 = e->ambient_pressure;
	p2 = comp_outlet_pressure(e);
	return p2 / p1;
}

Temperature
comp_outlet_temperature(const Engine *e) {
	Temperature t1, dt;
	Pressure p1, p2;
	double exp;

	t1 = e->ambient_temperature;
	p1 = e->ambient_pressure;
	p2 = comp_outlet_pressure(e);
	exp = (GAMMA_AIR - 1.0) / GAMMA_AIR;
	dt = t1 * (pow(p2/p1, exp) - 1.0) / e->comp_efficiency;

	return  t1 + dt;
}

Temperature
manifold_temperature(const Engine *e) {
	Temperature t1, t2;

	t1 = e->ambient_temperature;
	t2 = comp_outlet_temperature(e);
	return t2 - (t2 - t1)*e->intercooler_efficiency;
}

/* Volume flow rate throught the compressor inlet. */
VolumeFlowRate
volume_flow_rate(const Engine *e) {
	VolumeFlowRate v3;
	double r;

	v3 = port_volume_flow_rate(e);
	r = density_ratio(e);
	return v3 * r;
}

/* Volume flow rate through the intake ports. */
static VolumeFlowRate
port_volume_flow_rate(const Engine *e) {
	double n, d, ve;

	n = as_rpm(e->rpm);
	d = as_cubic_metre(e->displacement);
	ve = e->ve;
	return cubic_metre_per_min(n * d * ve / REV_PER_CYCLE);
}

/* Density ratio between the ports and the compressor inlet. */
static double
density_ratio(const Engine *e) {
	Pressure p1, p3;
	Temperature t1, t3;

	p1 = e->ambient_pressure;
	p3 = e->map;
	t1 = e->ambient_temperature;
	t3 = manifold_temperature(e);
	return (p3 * t1) / (p1 * t3);
}

/* Mass flow rate through the engine (corrected to ambient conditions). */
MassFlowRate
mass_flow_rate(const Engine *e) {
	Pressure p;
	VolumeFlowRate v;
	Temperature t;

	p = e->ambient_pressure;
	v = volume_flow_rate(e);
	t = e->ambient_temperature;
	return (p * v) / (R_AIR * t);
}