aboutsummaryrefslogtreecommitdiffstats
path: root/app.go
diff options
context:
space:
mode:
Diffstat (limited to 'app.go')
-rw-r--r--app.go128
1 files changed, 128 insertions, 0 deletions
diff --git a/app.go b/app.go
new file mode 100644
index 0000000..f0f5068
--- /dev/null
+++ b/app.go
@@ -0,0 +1,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
+}