diff options
| -rw-r--r-- | compressor.go (renamed from compressor/compressor.go) | 33 | ||||
| -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-- | util.go | 21 | ||||
| -rw-r--r-- | util/util.go | 31 | ||||
| -rw-r--r-- | volume.go | 33 | ||||
| -rw-r--r-- | volume/volume.go | 44 |
10 files changed, 143 insertions, 209 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 } @@ -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..631fd97 --- /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 { @@ -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)) - } -} |