aboutsummaryrefslogtreecommitdiffstats
path: root/app.go
blob: f0f506892f8b3279a4acf6c3a7d1c2b27601f08c (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
119
120
121
122
123
124
125
126
127
128
package main

import (
	"errors"
	"fmt"
	"image"
	"image/draw"
	_ "image/jpeg"
	"os"

	"github.com/sam-anthony/volute/compressor"
	"github.com/sam-anthony/volute/mass"
	"github.com/sam-anthony/volute/pressure"
	"github.com/sam-anthony/volute/temperature"
	"github.com/sam-anthony/volute/volume"
)

type App struct {
	Displacement volume.Volume
	VolumeUnit   volume.Volume

	Rpm []int

	Ve []float32

	Iat             []temperature.Temperature
	TemperatureUnit temperature.Unit

	Map          []pressure.Pressure
	PressureUnit pressure.Pressure

	Pr []float32

	Flow     []mass.FlowRate
	FlowUnit mass.FlowRate

	Comp    compressor.Compressor
	CompImg image.Image
}

func NewApp() (App, error) {
	a := App{
		Displacement: 2 * volume.Litre,
		VolumeUnit:   volume.Litre,

		Rpm: []int{2000},

		Ve: []float32{0.80},

		Iat: []temperature.Temperature{temperature.New(
			30.0, temperature.Celcius)},
		TemperatureUnit: temperature.Celcius,

		Map:          []pressure.Pressure{pressure.Bar},
		PressureUnit: pressure.Kilopascal,

		Pr: []float32{0.0},

		Flow:     []mass.FlowRate{mass.FlowRate(0.0)},
		FlowUnit: mass.KilogramPerSecond,
	}

	manufacturer, series, model := "borgwarner", "efr", "6258"
	c, ok := compressor.Compressors[manufacturer][series][model]
	if !ok {
		return App{}, errors.New(fmt.Sprintf(
			"Invalid compressor: %s %s %s",
			manufacturer, series, model,
		))
	}
	if err := a.SetCompressor(c); err != nil {
		return App{}, err
	}

	a.SetPr()
	a.SetFlow()

	return a, nil
}

func (a *App) SetCompressor(c compressor.Compressor) error {
	f, err := os.Open(c.FileName)
	if err != nil {
		return err
	}
	defer f.Close()

	j, _, err := image.Decode(f)
	if err != nil {
		return err
	}

	b := j.Bounds()
	m := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
	draw.Draw(m, m.Bounds(), j, b.Min, draw.Src)

	a.Comp = c
	a.CompImg = m

	return nil
}

func (a *App) SetPr() {
	for i := 0; i < len(a.Rpm); i++ {
		a.Pr[i] = float32(a.Map[i] / pressure.Atmospheric)
	}
}

func (a *App) SetFlow() error {
	for i := 0; i < len(a.Rpm); i++ {
		rpm := a.Rpm[i]
		disp := a.Displacement.As(volume.CubicMetre)
		ve := a.Ve[i]
		cubicMetresPerMin := float32(rpm/2) * disp * ve

		iat, err := a.Iat[i].As(temperature.Kelvin)
		if err != nil {
			return err
		}
		manPres := a.Map[i].As(pressure.Pascal)
		molsPerMin := (manPres * cubicMetresPerMin) / (gasConstant * iat)

		kgPerMin := molsPerMin * airMolarMass

		a.Flow[i] = mass.FlowRate(kgPerMin/60.0) * mass.KilogramPerSecond
	}
	return nil
}