aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2022-05-15 16:51:41 -0230
committerSam Anthony <sam@samanthony.xyz>2022-05-15 16:51:41 -0230
commit585fbf852c1e76470df42ebe99ede62440ce19d9 (patch)
tree4bb5406eb0e1d1a978e21de1e6c4291c22510d39
parent98a5ded8daccbb576b2928359112c6454fd0c5b3 (diff)
downloadvolute-585fbf852c1e76470df42ebe99ede62440ce19d9.zip
simplify unit conversion
-rw-r--r--compressor/compressor.go2
-rw-r--r--main.go16
-rw-r--r--mass/mass.go46
-rw-r--r--pressure/pressure.go30
-rw-r--r--ui.go41
-rw-r--r--volume/volume.go28
6 files changed, 48 insertions, 115 deletions
diff --git a/compressor/compressor.go b/compressor/compressor.go
index 9e03cbb..efe792e 100644
--- a/compressor/compressor.go
+++ b/compressor/compressor.go
@@ -93,7 +93,7 @@ func init() {
if err != nil {
return err
}
- c.MaxFlow = mass.NewFlowRate(flow.FlowVal, u)
+ c.MaxFlow = mass.FlowRate(flow.FlowVal) * u
compressors[man][ser][mod] = c
diff --git a/main.go b/main.go
index 83368dc..377d4ac 100644
--- a/main.go
+++ b/main.go
@@ -25,7 +25,7 @@ const (
var numPoints = 1
var (
- displacement = volume.New(2000, volume.CubicCentimetre)
+ displacement = 2000 * volume.CubicCentimetre
// volumeUnitIndex is used to index volume.UnitStrings().
volumeUnitIndex = volume.DefaultUnitIndex
@@ -46,9 +46,9 @@ var pressureRatio []float32
func pressureRatioAt(point int) float32 {
u := pressure.Pascal
- m := manifoldPressure[point].AsUnit(u)
- a := pressure.Atmospheric().AsUnit(u)
- return m / a
+ m := manifoldPressure[point] / u
+ a := pressure.Atmospheric() / u
+ return float32(m / a)
}
func init() {
pressureRatio = append(pressureRatio, pressureRatioAt(0))
@@ -62,18 +62,18 @@ var (
func massFlowRateAt(point int) mass.FlowRate {
rpm := float32(engineSpeed[point])
- disp := displacement.AsUnit(volume.CubicMetre)
+ disp := float32(displacement / volume.CubicMetre)
ve := float32(volumetricEfficiency[point]) / 100.0
cubicMetresPerMin := (rpm / 2.0) * disp * ve
iat, err := intakeAirTemperature[point].AsUnit(temperature.Kelvin)
util.Check(err)
- pres := manifoldPressure[point].AsUnit(pressure.Pascal)
- molsPerMin := (pres * cubicMetresPerMin) / (gasConstant * iat)
+ pres := manifoldPressure[point] / pressure.Pascal
+ molsPerMin := (float32(pres) * cubicMetresPerMin) / (gasConstant * iat)
kgPerMin := molsPerMin * airMolarMass
- mfr := mass.NewFlowRate(kgPerMin/60.0, mass.KilogramsPerSecond)
+ mfr := mass.FlowRate(kgPerMin/60.0) * mass.KilogramsPerSecond
return mfr
}
func init() {
diff --git a/mass/mass.go b/mass/mass.go
index 6e9f670..46ce33e 100644
--- a/mass/mass.go
+++ b/mass/mass.go
@@ -5,47 +5,35 @@ import (
"fmt"
)
-type unit float32
+type Mass float32
const (
- Gram unit = 1
- Kilogram unit = 1_000
- Pound unit = 453.5924
+ Gram Mass = 1
+ Kilogram Mass = 1_000
+ Pound Mass = 453.5924
)
-type Mass struct {
- val float32
-}
-
-func New(i float32, u unit) Mass {
- return Mass{i * float32(u)}
-}
-
-func (m Mass) AsUnit(u unit) float32 {
- return m.val / float32(u)
-}
-
-type flowRateUnit float32
+type FlowRate float32
const (
- KilogramsPerSecond flowRateUnit = 1
- PoundsPerMinute flowRateUnit = 0.007_559_872_833
+ KilogramsPerSecond FlowRate = 1
+ PoundsPerMinute FlowRate = 0.007_559_872_833
)
// FlowRateUnitStrings returns a slice of strings, each representing a
-// flowRateUnit.
+// flowRate unit.
// This is necessary because giu.Combo only works with strings.
func FlowRateUnitStrings() []string {
return []string{"kg/s", "lb/min"}
}
const (
- DefaultFlowRateUnit flowRateUnit = KilogramsPerSecond
+ DefaultFlowRateUnit FlowRate = KilogramsPerSecond
// DefaultFlowRateUnitIndex is used to index FlowRateUnitStrings()
DefaultFlowRateUnitIndex int32 = 0 // kg/s
)
-func FlowRateUnitFromString(s string) (flowRateUnit, error) {
+func FlowRateUnitFromString(s string) (FlowRate, error) {
// Each case corresponds to a value in FlowRateUnitStrings().
switch s {
case "kg/s":
@@ -53,19 +41,7 @@ func FlowRateUnitFromString(s string) (flowRateUnit, error) {
case "lb/min":
return PoundsPerMinute, nil
default:
- return *new(flowRateUnit), errors.New(
+ return *new(FlowRate), errors.New(
fmt.Sprintf("invalid mass flow rate unit: '%s'", s))
}
}
-
-type FlowRate struct {
- val float32
-}
-
-func NewFlowRate(i float32, u flowRateUnit) FlowRate {
- return FlowRate{i * float32(u)}
-}
-
-func (fr FlowRate) AsUnit(u flowRateUnit) float32 {
- return fr.val / float32(u)
-}
diff --git a/pressure/pressure.go b/pressure/pressure.go
index b3063cc..da1ab1e 100644
--- a/pressure/pressure.go
+++ b/pressure/pressure.go
@@ -5,13 +5,13 @@ import (
"fmt"
)
-type unit float32
+type Pressure float32
const (
- Pascal unit = 1
- Kilopascal unit = 1_000
- Bar unit = 100_000
- PoundsPerSquareInch unit = 6_894.757
+ Pascal Pressure = 1
+ Kilopascal Pressure = 1_000
+ Bar Pressure = 100_000
+ PoundsPerSquareInch Pressure = 6_894.757
)
// UnitStrings returns a slice of strings, each representing a
@@ -22,12 +22,12 @@ func UnitStrings() []string {
}
const (
- DefaultUnit unit = Kilopascal
+ DefaultUnit Pressure = Kilopascal
// DefaultUnitIndex is used to index UnitStrings().
DefaultUnitIndex int32 = 1 // kPa
)
-func UnitFromString(s string) (unit, error) {
+func UnitFromString(s string) (Pressure, error) {
// Each case corresponds to a value in UnitStrings().
switch s {
case "Pa":
@@ -39,22 +39,10 @@ func UnitFromString(s string) (unit, error) {
case "psi":
return PoundsPerSquareInch, nil
default:
- return *new(unit), errors.New(fmt.Sprintf("invalid unit: '%s'", s))
+ return *new(Pressure), errors.New(fmt.Sprintf("invalid unit: '%s'", s))
}
}
-type Pressure struct {
- val float32
-}
-
-func New(i float32, u unit) Pressure {
- return Pressure{i * float32(u)}
-}
-
-func (p Pressure) AsUnit(u unit) float32 {
- return p.val / float32(u)
-}
-
func Atmospheric() Pressure {
- return Pressure{float32(1 * Bar)}
+ return 1 * Bar
}
diff --git a/ui.go b/ui.go
index f4b0a0d..c116b8e 100644
--- a/ui.go
+++ b/ui.go
@@ -24,11 +24,11 @@ func engineDisplacementRow() *g.RowWidget {
s := volume.UnitStrings()[volumeUnitIndex]
unit, err := volume.UnitFromString(s)
util.Check(err)
- engDisp := displacement.AsUnit(unit)
+ engDisp := float32(displacement / unit)
return g.Row(
g.Label("Engine Displacement"),
g.InputFloat(&engDisp).Format("%.2f").OnChange(func() {
- displacement = volume.New(engDisp, unit)
+ displacement = volume.Volume(engDisp) * unit
for i := 0; i < numPoints; i++ {
engineMassFlowRate[i] = massFlowRateAt(i)
go updateCompImg()
@@ -39,12 +39,7 @@ func engineDisplacementRow() *g.RowWidget {
volume.UnitStrings()[volumeUnitIndex],
volume.UnitStrings(),
&volumeUnitIndex,
- ).OnChange(func() {
- displacement = volume.New(
- displacement.AsUnit(unit),
- unit,
- )
- }),
+ ),
)
}
@@ -131,23 +126,16 @@ func manifoldPressureRow() *g.TableRowWidget {
pressure.UnitStrings()[pressureUnitIndex],
pressure.UnitStrings(),
&pressureUnitIndex,
- ).OnChange(func() {
- for i := 0; i < numPoints; i++ {
- manifoldPressure[i] = pressure.New(
- manifoldPressure[i].AsUnit(unit),
- unit,
- )
- }
- }),
+ ),
}
for i := 0; i < numPoints; i++ {
i := i
- manPres := manifoldPressure[i].AsUnit(unit)
+ manPres := float32(manifoldPressure[i] / unit)
widgets = append(
widgets,
g.InputFloat(&manPres).Format("%.2f").
OnChange(func() {
- manifoldPressure[i] = pressure.New(manPres, unit)
+ manifoldPressure[i] = pressure.Pressure(manPres * float32(unit))
pressureRatio[i] = pressureRatioAt(i)
engineMassFlowRate[i] = massFlowRateAt(i)
go updateCompImg()
@@ -184,18 +172,11 @@ func massFlowRateRow() *g.TableRowWidget {
mass.FlowRateUnitStrings()[selectedMassFlowRateUnit],
mass.FlowRateUnitStrings(),
&selectedMassFlowRateUnit,
- ).OnChange(func() {
- for i := 0; i < numPoints; i++ {
- engineMassFlowRate[i] = mass.NewFlowRate(
- engineMassFlowRate[i].AsUnit(mfrUnit),
- mfrUnit,
- )
- }
- }),
+ ),
}
for i := 0; i < numPoints; i++ {
mfr := strconv.FormatFloat(
- float64(engineMassFlowRate[i].AsUnit(mfrUnit)),
+ float64(engineMassFlowRate[i]/mfrUnit),
'f',
3,
32,
@@ -327,10 +308,10 @@ func updateCompImg() {
max := selectedCompressor.MaxX
unit := mass.KilogramsPerSecond
- mfr := engineMassFlowRate[i].AsUnit(unit)
- maxMfr := selectedCompressor.MaxFlow.AsUnit(unit)
+ mfr := engineMassFlowRate[i] / unit
+ maxMfr := selectedCompressor.MaxFlow / unit
- x := min + int(float32(max-min)*(mfr/maxMfr))
+ x := min + int(float32(max-min)*float32(mfr/maxMfr))
min = selectedCompressor.MinY
max = selectedCompressor.MaxY
diff --git a/volume/volume.go b/volume/volume.go
index acec040..48330df 100644
--- a/volume/volume.go
+++ b/volume/volume.go
@@ -5,13 +5,13 @@ import (
"fmt"
)
-type unit float32
+type Volume float32
const (
- CubicCentimetre unit = 1
- Litre unit = 1_000
- CubicMetre unit = 1_000_000
- CubicInch unit = 16.38706
+ CubicCentimetre Volume = 1
+ Litre Volume = 1_000
+ CubicMetre Volume = 1_000_000
+ CubicInch Volume = 16.38706
)
// UnitStrings returns a slice of strings, each representing a
@@ -22,12 +22,12 @@ func UnitStrings() []string {
}
const (
- DefaultUnit unit = CubicCentimetre
+ DefaultUnit Volume = CubicCentimetre
// DefaulUnitIndex is used to index UnitStrings().
DefaultUnitIndex int32 = 0 // cc
)
-func UnitFromString(s string) (unit, error) {
+func UnitFromString(s string) (Volume, error) {
// Each case corresponds to a value in UnitStrings().
switch s {
case "cc":
@@ -39,18 +39,6 @@ func UnitFromString(s string) (unit, error) {
case "in³":
return CubicInch, nil
default:
- return *new(unit), errors.New(fmt.Sprintf("invalid volume unit: '%s'", s))
+ return *new(Volume), errors.New(fmt.Sprintf("invalid volume unit: '%s'", s))
}
}
-
-type Volume struct {
- val float32
-}
-
-func New(i float32, u unit) Volume {
- return Volume{i * float32(u)}
-}
-
-func (v Volume) AsUnit(u unit) float32 {
- return v.val / float32(u)
-}