aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2022-05-04 22:52:06 -0230
committerSam Anthony <sam@samanthony.xyz>2022-05-04 22:52:06 -0230
commitb94fef3b581133ddf6807d113422062acafde717 (patch)
tree52ff762e5af9a59d7f836a4c9c47d568e735bf8b
parentbb8d0c77cbd620ccc7521961df59c1f2f812125b (diff)
downloadvolute-b94fef3b581133ddf6807d113422062acafde717.zip
improve unit conversions
-rw-r--r--compressor/compressor.go2
-rw-r--r--main.go25
-rw-r--r--mass/mass.go37
-rw-r--r--pressure/pressure.go12
-rw-r--r--ui.go76
-rw-r--r--volume/volume.go10
6 files changed, 76 insertions, 86 deletions
diff --git a/compressor/compressor.go b/compressor/compressor.go
index ca5a0af..9e03cbb 100644
--- a/compressor/compressor.go
+++ b/compressor/compressor.go
@@ -93,7 +93,7 @@ func init() {
if err != nil {
return err
}
- c.MaxFlow = mass.FlowRate{flow.FlowVal, u}
+ c.MaxFlow = mass.NewFlowRate(flow.FlowVal, u)
compressors[man][ser][mod] = c
diff --git a/main.go b/main.go
index b7d3206..83368dc 100644
--- a/main.go
+++ b/main.go
@@ -7,7 +7,6 @@ import (
"image/draw"
_ "image/jpeg"
"os"
- "time"
"github.com/sam-anthony/volute/compressor"
"github.com/sam-anthony/volute/mass"
@@ -26,21 +25,21 @@ const (
var numPoints = 1
var (
- displacement = volume.Volume{2000, volume.CubicCentimetre}
- // selectedVolumeUnit is used to index volume.UnitStrings().
- selectedVolumeUnit = volume.DefaultUnitIndex
+ displacement = volume.New(2000, volume.CubicCentimetre)
+ // volumeUnitIndex is used to index volume.UnitStrings().
+ volumeUnitIndex = volume.DefaultUnitIndex
engineSpeed = []int32{2000}
volumetricEfficiency = []int32{80}
intakeAirTemperature = []temperature.Temperature{{25, temperature.Celcius}}
- // selectedTemperatureUnit is used to index temperature.UnitStrings().
- selectedTemperatureUnit = temperature.DefaultUnitIndex
+ // temperatureUnitIndex is used to index temperature.UnitStrings().
+ temperatureUnitIndex = temperature.DefaultUnitIndex
- manifoldPressure = []pressure.Pressure{{100, pressure.DefaultUnit}}
- // selectedPressureUnit is used to index pressure.UnitStrings().
- selectedPressureUnit = pressure.DefaultUnitIndex
+ manifoldPressure = []pressure.Pressure{pressure.Atmospheric()}
+ // pressureUnitIndex is used to index pressure.UnitStrings().
+ pressureUnitIndex = pressure.DefaultUnitIndex
)
var pressureRatio []float32
@@ -74,13 +73,7 @@ func massFlowRateAt(point int) mass.FlowRate {
kgPerMin := molsPerMin * airMolarMass
- massPerMin := mass.Mass{kgPerMin, mass.Kilogram}
-
- u, err := mass.FlowRateUnitFromString(mass.FlowRateUnitStrings()[selectedMassFlowRateUnit])
- util.Check(err)
-
- mfr, err := mass.NewFlowRate(massPerMin, time.Minute, u)
- util.Check(err)
+ mfr := mass.NewFlowRate(kgPerMin/60.0, mass.KilogramsPerSecond)
return mfr
}
func init() {
diff --git a/mass/mass.go b/mass/mass.go
index 29ecbd5..6e9f670 100644
--- a/mass/mass.go
+++ b/mass/mass.go
@@ -3,7 +3,6 @@ package mass
import (
"errors"
"fmt"
- "time"
)
type unit float32
@@ -15,13 +14,15 @@ const (
)
type Mass struct {
- Val float32
- Unit unit
+ val float32
+}
+
+func New(i float32, u unit) Mass {
+ return Mass{i * float32(u)}
}
func (m Mass) AsUnit(u unit) float32 {
- g := m.Val * float32(m.Unit) // Convert to grams.
- return g / float32(u) // Convert to desired unit.
+ return m.val / float32(u)
}
type flowRateUnit float32
@@ -53,34 +54,18 @@ func FlowRateUnitFromString(s string) (flowRateUnit, error) {
return PoundsPerMinute, nil
default:
return *new(flowRateUnit), errors.New(
- fmt.Sprintf("invalid massFlowRateUnit: '%s'", s))
+ fmt.Sprintf("invalid mass flow rate unit: '%s'", s))
}
}
type FlowRate struct {
- Val float32
- Unit flowRateUnit
+ val float32
}
-func NewFlowRate(m Mass, t time.Duration, u flowRateUnit) (FlowRate, error) {
- switch u {
- case KilogramsPerSecond:
- return FlowRate{
- m.AsUnit(Kilogram) / float32(t.Seconds()),
- u,
- }, nil
- case PoundsPerMinute:
- return FlowRate{
- m.AsUnit(Pound) / float32(t.Minutes()),
- u,
- }, nil
- default:
- return *new(FlowRate), errors.New(
- fmt.Sprintf("invalid massFlowRateUnit: '%v'", u))
- }
+func NewFlowRate(i float32, u flowRateUnit) FlowRate {
+ return FlowRate{i * float32(u)}
}
func (fr FlowRate) AsUnit(u flowRateUnit) float32 {
- kgps := fr.Val * float32(fr.Unit) // Convert to kilogramsPerSecond.
- return kgps / float32(u) // Convert to desired unit.
+ return fr.val / float32(u)
}
diff --git a/pressure/pressure.go b/pressure/pressure.go
index 2948c72..b3063cc 100644
--- a/pressure/pressure.go
+++ b/pressure/pressure.go
@@ -44,15 +44,17 @@ func UnitFromString(s string) (unit, error) {
}
type Pressure struct {
- Val float32
- Unit unit
+ val float32
+}
+
+func New(i float32, u unit) Pressure {
+ return Pressure{i * float32(u)}
}
func (p Pressure) AsUnit(u unit) float32 {
- pa := p.Val * float32(p.Unit) // Convert to pascals.
- return pa / float32(u) // Convert to desired unit.
+ return p.val / float32(u)
}
func Atmospheric() Pressure {
- return Pressure{1, Bar}
+ return Pressure{float32(1 * Bar)}
}
diff --git a/ui.go b/ui.go
index 1ee281d..f4b0a0d 100644
--- a/ui.go
+++ b/ui.go
@@ -21,9 +21,14 @@ func red() color.RGBA {
}
func engineDisplacementRow() *g.RowWidget {
+ s := volume.UnitStrings()[volumeUnitIndex]
+ unit, err := volume.UnitFromString(s)
+ util.Check(err)
+ engDisp := displacement.AsUnit(unit)
return g.Row(
g.Label("Engine Displacement"),
- g.InputFloat(&displacement.Val).Format("%.2f").OnChange(func() {
+ g.InputFloat(&engDisp).Format("%.2f").OnChange(func() {
+ displacement = volume.New(engDisp, unit)
for i := 0; i < numPoints; i++ {
engineMassFlowRate[i] = massFlowRateAt(i)
go updateCompImg()
@@ -31,17 +36,14 @@ func engineDisplacementRow() *g.RowWidget {
}),
g.Combo(
"",
- volume.UnitStrings()[selectedVolumeUnit],
+ volume.UnitStrings()[volumeUnitIndex],
volume.UnitStrings(),
- &selectedVolumeUnit,
+ &volumeUnitIndex,
).OnChange(func() {
- s := volume.UnitStrings()[selectedVolumeUnit]
- u, err := volume.UnitFromString(s)
- util.Check(err)
- displacement = volume.Volume{
- displacement.AsUnit(u),
- u,
- }
+ displacement = volume.New(
+ displacement.AsUnit(unit),
+ unit,
+ )
}),
)
}
@@ -87,11 +89,11 @@ func intakeAirTemperatureRow() *g.TableRowWidget {
g.Label("Intake Air Temperature"),
g.Combo(
"",
- temperature.UnitStrings()[selectedTemperatureUnit],
+ temperature.UnitStrings()[temperatureUnitIndex],
temperature.UnitStrings(),
- &selectedTemperatureUnit,
+ &temperatureUnitIndex,
).OnChange(func() {
- s := temperature.UnitStrings()[selectedTemperatureUnit]
+ s := temperature.UnitStrings()[temperatureUnitIndex]
u, err := temperature.UnitFromString(s)
util.Check(err)
@@ -118,32 +120,34 @@ func intakeAirTemperatureRow() *g.TableRowWidget {
}
func manifoldPressureRow() *g.TableRowWidget {
+ s := pressure.UnitStrings()[pressureUnitIndex]
+ unit, err := pressure.UnitFromString(s)
+ util.Check(err)
+
widgets := []g.Widget{
g.Label("Manifold Absolute Pressure"),
g.Combo(
"",
- pressure.UnitStrings()[selectedPressureUnit],
+ pressure.UnitStrings()[pressureUnitIndex],
pressure.UnitStrings(),
- &selectedPressureUnit,
+ &pressureUnitIndex,
).OnChange(func() {
- s := pressure.UnitStrings()[selectedPressureUnit]
- u, err := pressure.UnitFromString(s)
- util.Check(err)
-
for i := 0; i < numPoints; i++ {
- manifoldPressure[i] = pressure.Pressure{
- manifoldPressure[i].AsUnit(u),
- u,
- }
+ manifoldPressure[i] = pressure.New(
+ manifoldPressure[i].AsUnit(unit),
+ unit,
+ )
}
}),
}
for i := 0; i < numPoints; i++ {
i := i
+ manPres := manifoldPressure[i].AsUnit(unit)
widgets = append(
widgets,
- g.InputFloat(&manifoldPressure[i].Val).Format("%.2f").
+ g.InputFloat(&manPres).Format("%.2f").
OnChange(func() {
+ manifoldPressure[i] = pressure.New(manPres, unit)
pressureRatio[i] = pressureRatioAt(i)
engineMassFlowRate[i] = massFlowRateAt(i)
go updateCompImg()
@@ -169,6 +173,10 @@ func pressureRatioRow() *g.TableRowWidget {
}
func massFlowRateRow() *g.TableRowWidget {
+ s := mass.FlowRateUnitStrings()[selectedMassFlowRateUnit]
+ mfrUnit, err := mass.FlowRateUnitFromString(s)
+ util.Check(err)
+
widgets := []g.Widget{
g.Label("Mass Flow Rate"),
g.Combo(
@@ -177,21 +185,17 @@ func massFlowRateRow() *g.TableRowWidget {
mass.FlowRateUnitStrings(),
&selectedMassFlowRateUnit,
).OnChange(func() {
- s := mass.FlowRateUnitStrings()[selectedMassFlowRateUnit]
- u, err := mass.FlowRateUnitFromString(s)
- util.Check(err)
-
for i := 0; i < numPoints; i++ {
- engineMassFlowRate[i] = mass.FlowRate{
- engineMassFlowRate[i].AsUnit(u),
- u,
- }
+ engineMassFlowRate[i] = mass.NewFlowRate(
+ engineMassFlowRate[i].AsUnit(mfrUnit),
+ mfrUnit,
+ )
}
}),
}
for i := 0; i < numPoints; i++ {
mfr := strconv.FormatFloat(
- float64(engineMassFlowRate[i].Val),
+ float64(engineMassFlowRate[i].AsUnit(mfrUnit)),
'f',
3,
32,
@@ -321,15 +325,19 @@ func updateCompImg() {
for i := 0; i < numPoints; i++ {
min := selectedCompressor.MinX
max := selectedCompressor.MaxX
- unit := selectedCompressor.MaxFlow.Unit
+
+ unit := mass.KilogramsPerSecond
mfr := engineMassFlowRate[i].AsUnit(unit)
maxMfr := selectedCompressor.MaxFlow.AsUnit(unit)
+
x := min + int(float32(max-min)*(mfr/maxMfr))
min = selectedCompressor.MinY
max = selectedCompressor.MaxY
+
pr := pressureRatio[i]
maxPr := selectedCompressor.MaxPR
+
y := min - int(float32((min-max))*((pr-1.0)/(maxPr-1.0)))
ps := m.Bounds().Dx() / 100 // Point size
diff --git a/volume/volume.go b/volume/volume.go
index 020d1a6..acec040 100644
--- a/volume/volume.go
+++ b/volume/volume.go
@@ -44,11 +44,13 @@ func UnitFromString(s string) (unit, error) {
}
type Volume struct {
- Val float32
- Unit unit
+ val float32
+}
+
+func New(i float32, u unit) Volume {
+ return Volume{i * float32(u)}
}
func (v Volume) AsUnit(u unit) float32 {
- cc := v.Val * float32(v.Unit) // Convert to cubic centimetres.
- return cc / float32(u) // Convert to desired unit.
+ return v.val / float32(u)
}