diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2022-05-27 22:39:03 -0230 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2022-05-27 22:39:03 -0230 |
| commit | 31605150d3e10b08dad2086005c64664f5648a51 (patch) | |
| tree | 7419f90e7b30d3de52595f25852e9ef87094718c /app.go | |
| parent | 585fbf852c1e76470df42ebe99ede62440ce19d9 (diff) | |
| download | volute-31605150d3e10b08dad2086005c64664f5648a51.zip | |
prepare to switch ui library
Diffstat (limited to 'app.go')
| -rw-r--r-- | app.go | 128 |
1 files changed, 128 insertions, 0 deletions
@@ -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 +} |