diff options
| author | sam-anthony <samanthony6@protonmail.com> | 2022-03-26 13:35:58 -0230 |
|---|---|---|
| committer | sam-anthony <samanthony6@protonmail.com> | 2022-03-26 13:35:58 -0230 |
| commit | e8c878f4236c056b1c0c9308e2d49c5b23833963 (patch) | |
| tree | 01f53ee620e256fa7b87ea43656b9315735e9c2a /pressure.go | |
| parent | 90f42ad66feb6b77474d0da267d572eca16e2826 (diff) | |
| download | volute-e8c878f4236c056b1c0c9308e2d49c5b23833963.zip | |
pressure and rough layout
Diffstat (limited to 'pressure.go')
| -rw-r--r-- | pressure.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/pressure.go b/pressure.go new file mode 100644 index 0000000..f486552 --- /dev/null +++ b/pressure.go @@ -0,0 +1,54 @@ +package main + +import ( + "errors" + "fmt" +) + +type pressureUnit float32 + +const ( + pascal pressureUnit = 1 + kilopascal pressureUnit = 1_000 + bar pressureUnit = 100_000 + poundsPerSquareInch pressureUnit = 6_894.757 +) + +// pressureUnitStrings returns a slice of strings, each representing a +// pressureUnit. +// This is necessary because giu.Combo only works with strings. +func pressureUnitStrings() []string { + return []string{"Pa", "kPa", "bar", "psi"} +} + +const ( + defaultPressureUnit pressureUnit = kilopascal + // Used to index pressureUnitStrings + defaultPressureUnitIndex int32 = 1 // kPa +) + +func pressureUnitFromString(s string) (pressureUnit, error) { + // Each case corresponds to a value in pressureUnitStrings + 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(pressureUnit), errors.New(fmt.Sprintf("invalid pressureUnit: '%s'", s)) + } +} + +type pressure struct { + val float32 + unit pressureUnit +} + +func (p pressure) asUnit(u pressureUnit) float32 { + pa := p.val * float32(p.unit) // Convert to pascals. + return pa / float32(u) // Convert to desired unit. +} |