diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-01-20 17:56:39 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-01-20 17:56:39 -0500 |
| commit | 3aaae870ba76d8f0907b10a61d829ad353936306 (patch) | |
| tree | 8be8cb950d0aa8039cd977c72e2f9438cae4a1e8 | |
| parent | db183cf7570e0f4e448ab5ced0ae41969261a815 (diff) | |
| download | volute-3aaae870ba76d8f0907b10a61d829ad353936306.zip | |
flatten source directory structure by removing modules
| -rw-r--r-- | compressor.go (renamed from compressor/compressor.go) | 33 | ||||
| -rw-r--r-- | main.go | 64 | ||||
| -rw-r--r-- | mass.go | 28 | ||||
| -rw-r--r-- | mass/mass.go | 47 | ||||
| -rw-r--r-- | pressure.go | 40 | ||||
| -rw-r--r-- | pressure/pressure.go | 48 | ||||
| -rw-r--r-- | temperature.go (renamed from temperature/temperature.go) | 27 | ||||
| -rw-r--r-- | ui.go | 124 | ||||
| -rw-r--r-- | util.go | 21 | ||||
| -rw-r--r-- | util/util.go | 31 | ||||
| -rw-r--r-- | volume.go | 33 | ||||
| -rw-r--r-- | volume/volume.go | 44 |
12 files changed, 228 insertions, 312 deletions
diff --git a/compressor/compressor.go b/compressor.go index 9983ecb..a3a16db 100644 --- a/compressor/compressor.go +++ b/compressor.go @@ -1,13 +1,10 @@ -package compressor +package main import ( "github.com/BurntSushi/toml" "io/fs" fp "path/filepath" "strings" - - "volute/mass" - "volute/util" ) const root = "compressor_maps/" @@ -27,17 +24,17 @@ type Compressor struct { // image in pixels. MaxY int // MaxFlow is the mass flow rate at MaxX. - MaxFlow mass.FlowRate + MaxFlow MassFlowRate // MaxPR is the pressure ratio at MaxY. MaxPR float32 } // [manufacturer][series][model] -var compressors = make(map[string]map[string]map[string]Compressor) +var Compressors = make(map[string]map[string]map[string]Compressor) func init() { // Walk root, looking for .toml files describing a compressor. - // Parse these toml files, create a Compressor and add it to compressors. + // Parse these toml files, create a Compressor and add it to Compressors. err := fp.WalkDir(root, func(path string, d fs.DirEntry, err error) error { if err != nil { return err @@ -57,11 +54,11 @@ func init() { man, ser := fp.Split(manSer) man = fp.Clean(man) // Clean trailing slash - if _, ok := compressors[man]; !ok { // Manufacturer does NOT exist - compressors[man] = make(map[string]map[string]Compressor) + if _, ok := Compressors[man]; !ok { // Manufacturer does NOT exist + Compressors[man] = make(map[string]map[string]Compressor) } - if _, ok := compressors[man][ser]; !ok { // Series does NOT exist - compressors[man][ser] = make(map[string]Compressor) + if _, ok := Compressors[man][ser]; !ok { // Series does NOT exist + Compressors[man][ser] = make(map[string]Compressor) } tomlFile := fp.Join(root, path) @@ -74,17 +71,13 @@ func init() { if err != nil { return err } - compressors[man][ser][mod] = c + Compressors[man][ser][mod] = c return nil }) - util.Check(err) -} - -func Compressors() map[string]map[string]map[string]Compressor { - return compressors + Check(err) } -func readMaxFlow(tomlFile string) (mass.FlowRate, error) { +func readMaxFlow(tomlFile string) (MassFlowRate, error) { flow := struct { FlowVal float32 FlowUnit string @@ -92,9 +85,9 @@ func readMaxFlow(tomlFile string) (mass.FlowRate, error) { if _, err := toml.DecodeFile(tomlFile, &flow); err != nil { return -1, err } - unit, err := mass.FlowRateUnitFromString(flow.FlowUnit) + unit, err := ParseMassFlowRateUnit(flow.FlowUnit) if err != nil { return -1, err } - return mass.FlowRate(flow.FlowVal) * unit, nil + return MassFlowRate(flow.FlowVal) * unit, nil } @@ -7,13 +7,6 @@ import ( "image/draw" _ "image/jpeg" "os" - - "volute/compressor" - "volute/mass" - "volute/pressure" - "volute/temperature" - "volute/util" - "volute/volume" ) const ( @@ -21,33 +14,31 @@ const ( airMolarMass = 0.0289647 // kg/mol ) -// numPoints is the number of datapoints on the compressor map. +// Number of data points on the compressor map. var numPoints = 1 var ( - displacement = 2000 * volume.CubicCentimetre - // volumeUnitIndex is used to index volume.UnitStrings(). - volumeUnitIndex = volume.DefaultUnitIndex + displacement = 2000 * CubicCentimetre + volumeUnitIndex int32 - engineSpeed = []int32{2000} + // Angular crankshaft speed in RPM. + speed = []int32{2000} volumetricEfficiency = []int32{80} - intakeAirTemperature = []temperature.Temperature{{25, temperature.Celcius}} - // temperatureUnitIndex is used to index temperature.UnitStrings(). - temperatureUnitIndex = temperature.DefaultUnitIndex + intakeAirTemperature = []Temperature{{25, Celcius}} + temperatureUnitIndex int32 - manifoldPressure = []pressure.Pressure{pressure.Atmospheric()} - // pressureUnitIndex is used to index pressure.UnitStrings(). - pressureUnitIndex = pressure.DefaultUnitIndex + manifoldPressure = []Pressure{AtmosphericPressure()} + pressureUnitIndex int32 ) var pressureRatio []float32 func pressureRatioAt(point int) float32 { - u := pressure.Pascal + u := Pascal m := manifoldPressure[point] / u - a := pressure.Atmospheric() / u + a := AtmosphericPressure() / u return float32(m / a) } func init() { @@ -55,42 +46,41 @@ func init() { } var ( - engineMassFlowRate []mass.FlowRate - // selectedMassFlowRateUnit is used to index mass.FlowRateUnitStrings(). - selectedMassFlowRateUnit = mass.DefaultFlowRateUnitIndex + massFlowRateAir []MassFlowRate + massFlowRateUnitIndex int32 ) -func massFlowRateAt(point int) mass.FlowRate { - rpm := float32(engineSpeed[point]) - disp := float32(displacement / volume.CubicMetre) +func massFlowRateAt(point int) MassFlowRate { + rpm := float32(speed[point]) + disp := float32(displacement / 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] / pressure.Pascal + iat, err := intakeAirTemperature[point].AsUnit(Kelvin) + Check(err) + pres := manifoldPressure[point] / Pascal molsPerMin := (float32(pres) * cubicMetresPerMin) / (gasConstant * iat) kgPerMin := molsPerMin * airMolarMass - mfr := mass.FlowRate(kgPerMin/60.0) * mass.KilogramsPerSecond + mfr := MassFlowRate(kgPerMin/60.0) * KilogramsPerSecond return mfr } func init() { - engineMassFlowRate = append(engineMassFlowRate, massFlowRateAt(0)) + massFlowRateAir = append(massFlowRateAir, massFlowRateAt(0)) } var ( compressorImage *image.RGBA compressorTexture *g.Texture - selectedCompressor compressor.Compressor + selectedCompressor Compressor ) func init() { manufacturer := "garrett" series := "g" model := "25-660" - c, ok := compressor.Compressors()[manufacturer][series][model] + c, ok := Compressors[manufacturer][series][model] if !ok { fmt.Printf("compressor.Compressors()[\"%s\"][\"%s\"][\"%s\"] does not exist.\n", manufacturer, series, model, @@ -113,13 +103,13 @@ func main() { wnd.Run(loop) } -func setCompressor(c compressor.Compressor) { +func setCompressor(c Compressor) { f, err := os.Open(c.FileName) - util.Check(err) + Check(err) defer f.Close() j, _, err := image.Decode(f) - util.Check(err) + Check(err) b := j.Bounds() m := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) @@ -137,7 +127,7 @@ func loop() { g.Table(). Size(g.Auto, 190). Rows( - engineSpeedRow(), + speedRow(), volumetricEfficiencyRow(), intakeAirTemperatureRow(), manifoldPressureRow(), @@ -0,0 +1,28 @@ +package main + +import ( + "errors" + "fmt" +) + +type MassFlowRate float32 + +const ( + KilogramsPerSecond MassFlowRate = 1 + PoundsPerMinute MassFlowRate = 0.007_559_872_833 +) + +var MassFlowRateUnits = []string{"kg/s", "lb/min"} + +func ParseMassFlowRateUnit(s string) (MassFlowRate, error) { + // Each case corresponds to a value in MassFlowRateUnits. + switch s { + case "kg/s": + return KilogramsPerSecond, nil + case "lb/min": + return PoundsPerMinute, nil + default: + return *new(MassFlowRate), errors.New( + fmt.Sprintf("invalid mass flow rate unit: '%s'", s)) + } +} diff --git a/mass/mass.go b/mass/mass.go deleted file mode 100644 index 46ce33e..0000000 --- a/mass/mass.go +++ /dev/null @@ -1,47 +0,0 @@ -package mass - -import ( - "errors" - "fmt" -) - -type Mass float32 - -const ( - Gram Mass = 1 - Kilogram Mass = 1_000 - Pound Mass = 453.5924 -) - -type FlowRate float32 - -const ( - KilogramsPerSecond FlowRate = 1 - PoundsPerMinute FlowRate = 0.007_559_872_833 -) - -// FlowRateUnitStrings returns a slice of strings, each representing a -// flowRate unit. -// This is necessary because giu.Combo only works with strings. -func FlowRateUnitStrings() []string { - return []string{"kg/s", "lb/min"} -} - -const ( - DefaultFlowRateUnit FlowRate = KilogramsPerSecond - // DefaultFlowRateUnitIndex is used to index FlowRateUnitStrings() - DefaultFlowRateUnitIndex int32 = 0 // kg/s -) - -func FlowRateUnitFromString(s string) (FlowRate, error) { - // Each case corresponds to a value in FlowRateUnitStrings(). - switch s { - case "kg/s": - return KilogramsPerSecond, nil - case "lb/min": - return PoundsPerMinute, nil - default: - return *new(FlowRate), errors.New( - fmt.Sprintf("invalid mass flow rate unit: '%s'", s)) - } -} diff --git a/pressure.go b/pressure.go new file mode 100644 index 0000000..cd824c5 --- /dev/null +++ b/pressure.go @@ -0,0 +1,40 @@ +package main + +import ( + "errors" + "fmt" +) + +type Pressure float32 + +const ( + Millibar Pressure = 100 + Pascal Pressure = 1 + Kilopascal Pressure = 1_000 + Bar Pressure = 100_000 + PoundsPerSquareInch Pressure = 6_894.757 +) + +var PressureUnits = []string{"mbar", "Pa", "kPa", "bar", "psi"} + +func ParsePressureUnit(s string) (Pressure, error) { + // Each case corresponds to a value in PressureUnits. + switch s { + case "mbar": + return Millibar, nil + case "Pa": + return Pascal, nil + case "kPa": + return Kilopascal, nil + case "bar": + return Bar, nil + case "psi": + return PoundsPerSquareInch, nil + default: + return *new(Pressure), errors.New(fmt.Sprintf("invalid unit: '%s'", s)) + } +} + +func AtmosphericPressure() Pressure { + return 101.325 * Pascal +} diff --git a/pressure/pressure.go b/pressure/pressure.go deleted file mode 100644 index da1ab1e..0000000 --- a/pressure/pressure.go +++ /dev/null @@ -1,48 +0,0 @@ -package pressure - -import ( - "errors" - "fmt" -) - -type Pressure float32 - -const ( - 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 -// unit. -// This is necessary because giu.Combo only works with strings. -func UnitStrings() []string { - return []string{"Pa", "kPa", "bar", "psi"} -} - -const ( - DefaultUnit Pressure = Kilopascal - // DefaultUnitIndex is used to index UnitStrings(). - DefaultUnitIndex int32 = 1 // kPa -) - -func UnitFromString(s string) (Pressure, error) { - // Each case corresponds to a value in UnitStrings(). - switch s { - case "Pa": - return Pascal, nil - case "kPa": - return Kilopascal, nil - case "bar": - return Bar, nil - case "psi": - return PoundsPerSquareInch, nil - default: - return *new(Pressure), errors.New(fmt.Sprintf("invalid unit: '%s'", s)) - } -} - -func Atmospheric() Pressure { - return 1 * Bar -} diff --git a/temperature/temperature.go b/temperature.go index 2a135df..ac8cae0 100644 --- a/temperature/temperature.go +++ b/temperature.go @@ -1,32 +1,21 @@ -package temperature +package main import ( "errors" "fmt" ) -type unit int +type TemperatureUnit int const ( - Celcius unit = iota + Celcius TemperatureUnit = iota Kelvin Fahrenheit ) -// UnitStrings returns a slice of strings, each representing a -// unit. -// This is necessary because giu.Combo only works with strings. -func UnitStrings() []string { - return []string{"°C", "°K", "°F"} -} - -const ( - DefaultUnit unit = Celcius - // DefaultUnitIndex is used to index UnitStrings(). - DefaultUnitIndex int32 = 0 // celcius -) +var TemperatureUnits = []string{"°C", "°K", "°F"} -func UnitFromString(s string) (unit, error) { +func ParseTemperatureUnit(s string) (TemperatureUnit, error) { // Each case corresponds to a value in UnitStrings(). switch s { case "°C": @@ -36,16 +25,16 @@ func UnitFromString(s string) (unit, error) { case "°F": return Fahrenheit, nil default: - return *new(unit), errors.New(fmt.Sprintf("invalid unit: '%s'", s)) + return *new(TemperatureUnit), errors.New(fmt.Sprintf("invalid unit: '%s'", s)) } } type Temperature struct { Val float32 - Unit unit + Unit TemperatureUnit } -func (t Temperature) AsUnit(u unit) (float32, error) { +func (t Temperature) AsUnit(u TemperatureUnit) (float32, error) { // Convert to celcius var c float32 switch t.Unit { @@ -7,13 +7,6 @@ import ( "image/color" "image/draw" "strconv" - - "volute/compressor" - "volute/mass" - "volute/pressure" - "volute/temperature" - "volute/util" - "volute/volume" ) func red() color.RGBA { @@ -21,34 +14,34 @@ func red() color.RGBA { } func engineDisplacementRow() *g.RowWidget { - s := volume.UnitStrings()[volumeUnitIndex] - unit, err := volume.UnitFromString(s) - util.Check(err) + s := VolumeUnits[volumeUnitIndex] + unit, err := ParseVolumeUnit(s) + Check(err) engDisp := float32(displacement / unit) valWid, _ := g.CalcTextSize("12345.67") - unitWid, _ := g.CalcTextSize(volume.UnitStrings()[volumeUnitIndex]) + unitWid, _ := g.CalcTextSize(VolumeUnits[volumeUnitIndex]) return g.Row( g.Label("Engine Displacement"), g.InputFloat(&engDisp). Format("%.2f"). OnChange(func() { - displacement = volume.Volume(engDisp) * unit + displacement = Volume(engDisp) * unit for i := 0; i < numPoints; i++ { - engineMassFlowRate[i] = massFlowRateAt(i) + massFlowRateAir[i] = massFlowRateAt(i) go updateCompImg() } }). Size(valWid), g.Combo( "", - volume.UnitStrings()[volumeUnitIndex], - volume.UnitStrings(), + VolumeUnits[volumeUnitIndex], + VolumeUnits, &volumeUnitIndex, ).Size(unitWid*2), ) } -func engineSpeedRow() *g.TableRowWidget { +func speedRow() *g.TableRowWidget { widgets := []g.Widget{ g.Label("Engine Speed"), g.Label("rpm"), @@ -57,8 +50,8 @@ func engineSpeedRow() *g.TableRowWidget { i := i widgets = append( widgets, - g.InputInt(&engineSpeed[i]).OnChange(func() { - engineMassFlowRate[i] = massFlowRateAt(i) + g.InputInt(&speed[i]).OnChange(func() { + massFlowRateAir[i] = massFlowRateAt(i) go updateCompImg() }), ) @@ -76,7 +69,7 @@ func volumetricEfficiencyRow() *g.TableRowWidget { widgets = append( widgets, g.InputInt(&volumetricEfficiency[i]).OnChange(func() { - engineMassFlowRate[i] = massFlowRateAt(i) + massFlowRateAir[i] = massFlowRateAt(i) go updateCompImg() }), ) @@ -85,23 +78,23 @@ func volumetricEfficiencyRow() *g.TableRowWidget { } func intakeAirTemperatureRow() *g.TableRowWidget { - wid, _ := g.CalcTextSize(temperature.UnitStrings()[temperatureUnitIndex]) + wid, _ := g.CalcTextSize(TemperatureUnits[temperatureUnitIndex]) widgets := []g.Widget{ g.Label("Intake Air Temperature"), g.Combo( "", - temperature.UnitStrings()[temperatureUnitIndex], - temperature.UnitStrings(), + TemperatureUnits[temperatureUnitIndex], + TemperatureUnits, &temperatureUnitIndex, ).OnChange(func() { - s := temperature.UnitStrings()[temperatureUnitIndex] - u, err := temperature.UnitFromString(s) - util.Check(err) + s := TemperatureUnits[temperatureUnitIndex] + u, err := ParseTemperatureUnit(s) + Check(err) for i := range intakeAirTemperature { t, err := intakeAirTemperature[i].AsUnit(u) - util.Check(err) - intakeAirTemperature[i] = temperature.Temperature{t, u} + Check(err) + intakeAirTemperature[i] = Temperature{t, u} } }).Size(wid * 2), } @@ -112,7 +105,7 @@ func intakeAirTemperatureRow() *g.TableRowWidget { g.InputFloat(&intakeAirTemperature[i].Val). Format("%.2f"). OnChange(func() { - engineMassFlowRate[i] = massFlowRateAt(i) + massFlowRateAir[i] = massFlowRateAt(i) go updateCompImg() }), ) @@ -121,16 +114,16 @@ func intakeAirTemperatureRow() *g.TableRowWidget { } func manifoldPressureRow() *g.TableRowWidget { - s := pressure.UnitStrings()[pressureUnitIndex] - unit, err := pressure.UnitFromString(s) - util.Check(err) - wid, _ := g.CalcTextSize(pressure.UnitStrings()[pressureUnitIndex]) + s := PressureUnits[pressureUnitIndex] + unit, err := ParsePressureUnit(s) + Check(err) + wid, _ := g.CalcTextSize(PressureUnits[pressureUnitIndex]) widgets := []g.Widget{ g.Label("Manifold Absolute Pressure"), g.Combo( "", - pressure.UnitStrings()[pressureUnitIndex], - pressure.UnitStrings(), + PressureUnits[pressureUnitIndex], + PressureUnits, &pressureUnitIndex, ).Size(wid * 2), } @@ -141,9 +134,9 @@ func manifoldPressureRow() *g.TableRowWidget { widgets, g.InputFloat(&manPres).Format("%.2f"). OnChange(func() { - manifoldPressure[i] = pressure.Pressure(manPres * float32(unit)) + manifoldPressure[i] = Pressure(manPres * float32(unit)) pressureRatio[i] = pressureRatioAt(i) - engineMassFlowRate[i] = massFlowRateAt(i) + massFlowRateAir[i] = massFlowRateAt(i) go updateCompImg() }), ) @@ -167,23 +160,23 @@ func pressureRatioRow() *g.TableRowWidget { } func massFlowRateRow() *g.TableRowWidget { - s := mass.FlowRateUnitStrings()[selectedMassFlowRateUnit] - mfrUnit, err := mass.FlowRateUnitFromString(s) - util.Check(err) + s := MassFlowRateUnits[massFlowRateUnitIndex] + mfrUnit, err := ParseMassFlowRateUnit(s) + Check(err) - wid, _ := g.CalcTextSize(mass.FlowRateUnitStrings()[selectedMassFlowRateUnit]) + wid, _ := g.CalcTextSize(MassFlowRateUnits[massFlowRateUnitIndex]) widgets := []g.Widget{ g.Label("Mass Flow Rate"), g.Combo( "", - mass.FlowRateUnitStrings()[selectedMassFlowRateUnit], - mass.FlowRateUnitStrings(), - &selectedMassFlowRateUnit, + MassFlowRateUnits[massFlowRateUnitIndex], + MassFlowRateUnits, + &massFlowRateUnitIndex, ).Size(wid * 2), } for i := 0; i < numPoints; i++ { mfr := strconv.FormatFloat( - float64(engineMassFlowRate[i]/mfrUnit), + float64(massFlowRateAir[i]/mfrUnit), 'f', 3, 32, @@ -203,34 +196,34 @@ func duplicateDeleteRow() *g.TableRowWidget { widgets = append(widgets, g.Row( g.Button("Duplicate").OnClick(func() { numPoints++ - engineSpeed = util.Insert( - engineSpeed, - engineSpeed[i], + speed = Insert( + speed, + speed[i], i, ) - volumetricEfficiency = util.Insert( + volumetricEfficiency = Insert( volumetricEfficiency, volumetricEfficiency[i], i, ) - intakeAirTemperature = util.Insert( + intakeAirTemperature = Insert( intakeAirTemperature, intakeAirTemperature[i], i, ) - manifoldPressure = util.Insert( + manifoldPressure = Insert( manifoldPressure, manifoldPressure[i], i, ) - pressureRatio = util.Insert( + pressureRatio = Insert( pressureRatio, pressureRatio[i], i, ) - engineMassFlowRate = util.Insert( - engineMassFlowRate, - engineMassFlowRate[i], + massFlowRateAir = Insert( + massFlowRateAir, + massFlowRateAir[i], i, ) go updateCompImg() @@ -240,12 +233,12 @@ func duplicateDeleteRow() *g.TableRowWidget { return } numPoints-- - engineSpeed = util.Remove(engineSpeed, i) - volumetricEfficiency = util.Remove(volumetricEfficiency, i) - intakeAirTemperature = util.Remove(intakeAirTemperature, i) - manifoldPressure = util.Remove(manifoldPressure, i) - pressureRatio = util.Remove(pressureRatio, i) - engineMassFlowRate = util.Remove(engineMassFlowRate, i) + speed = Remove(speed, i) + volumetricEfficiency = Remove(volumetricEfficiency, i) + intakeAirTemperature = Remove(intakeAirTemperature, i) + manifoldPressure = Remove(manifoldPressure, i) + pressureRatio = Remove(pressureRatio, i) + massFlowRateAir = Remove(massFlowRateAir, i) go updateCompImg() }), )) @@ -270,14 +263,13 @@ func columns() []*g.TableColumnWidget { var compressorTree []g.Widget func init() { - compressors := compressor.Compressors() - for man := range compressors { + for man := range Compressors { man := man // Manufacturer var serNodes []g.Widget - for ser := range compressors[man] { + for ser := range Compressors[man] { ser := ser // Series var modNodes []g.Widget - for mod, c := range compressors[man][ser] { + for mod, c := range Compressors[man][ser] { mod := mod // Model c := c // Compressor modNodes = append( @@ -365,8 +357,8 @@ func copyImage(old *image.RGBA) *image.RGBA { // The position on the compressor map of an operating point. func pointPos(i int) (pos image.Point) { - const unit = mass.KilogramsPerSecond - mfr := engineMassFlowRate[i] / unit + const unit = KilogramsPerSecond + mfr := massFlowRateAir[i] / unit maxMfr := selectedCompressor.MaxFlow / unit min := selectedCompressor.MinX max := selectedCompressor.MaxX @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "os" +) + +func Check(err error) { + if err != nil { + fmt.Println(err) + os.Exit(1) + } +} + +func Insert[T any](slice []T, elem T, i int) []T { + return append(slice[:i], append([]T{elem}, slice[i:]...)...) +} + +func Remove[T any](slice []T, i int) []T { + return append(slice[:i], slice[i+1:]...) +} diff --git a/util/util.go b/util/util.go deleted file mode 100644 index b0eae51..0000000 --- a/util/util.go +++ /dev/null @@ -1,31 +0,0 @@ -package util - -import ( - "fmt" - "os" - - "volute/mass" - "volute/pressure" - "volute/temperature" -) - -func Check(err error) { - if err != nil { - fmt.Println(err) - os.Exit(1) - } -} - -func Insert[T int32 | float32 | temperature.Temperature | pressure.Pressure | mass.FlowRate](slice []T, elem T, i int) []T { - return append( - slice[:i], - append( - []T{elem}, - slice[i:]..., - )..., - ) -} - -func Remove[T int32 | float32 | temperature.Temperature | pressure.Pressure | mass.FlowRate](slice []T, i int) []T { - return append(slice[:i], slice[i+1:]...) -} diff --git a/volume.go b/volume.go new file mode 100644 index 0000000..6f5fb31 --- /dev/null +++ b/volume.go @@ -0,0 +1,33 @@ +package main + +import ( + "errors" + "fmt" +) + +type Volume float32 + +const ( + CubicCentimetre Volume = 1 + Litre Volume = 1_000 + CubicMetre Volume = 1_000_000 + CubicInch Volume = 16.38706 +) + +var VolumeUnits = []string{"cc", "L", "m³", "in³"} + +func ParseVolumeUnit(s string) (Volume, error) { + // Each case corresponds to a value in VolumeUnits. + switch s { + case "cc": + return CubicCentimetre, nil + case "L": + return Litre, nil + case "m³": + return CubicMetre, nil + case "in³": + return CubicInch, nil + default: + return *new(Volume), errors.New(fmt.Sprintf("invalid volume unit: '%s'", s)) + } +} diff --git a/volume/volume.go b/volume/volume.go deleted file mode 100644 index 48330df..0000000 --- a/volume/volume.go +++ /dev/null @@ -1,44 +0,0 @@ -package volume - -import ( - "errors" - "fmt" -) - -type Volume float32 - -const ( - 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 -// unit. -// This is necessary because giu.Combo only works with strings. -func UnitStrings() []string { - return []string{"cc", "L", "m³", "in³"} -} - -const ( - DefaultUnit Volume = CubicCentimetre - // DefaulUnitIndex is used to index UnitStrings(). - DefaultUnitIndex int32 = 0 // cc -) - -func UnitFromString(s string) (Volume, error) { - // Each case corresponds to a value in UnitStrings(). - switch s { - case "cc": - return CubicCentimetre, nil - case "L": - return Litre, nil - case "m³": - return CubicMetre, nil - case "in³": - return CubicInch, nil - default: - return *new(Volume), errors.New(fmt.Sprintf("invalid volume unit: '%s'", s)) - } -} |