diff options
| author | sam-anthony <samanthony6@protonmail.com> | 2022-03-31 20:27:39 -0230 |
|---|---|---|
| committer | sam-anthony <samanthony6@protonmail.com> | 2022-03-31 20:27:39 -0230 |
| commit | 6634b3ff6bcdffbab38a049460ae6ea3cd68944f (patch) | |
| tree | 4cbcb7829060883afe074854a2ffc82b4ab2e31b /pressure | |
| parent | cc0d171c5cd1057693960b7f63aee29e7f70ee8e (diff) | |
| download | volute-6634b3ff6bcdffbab38a049460ae6ea3cd68944f.zip | |
refactor and compressor map image widget
Diffstat (limited to 'pressure')
| -rw-r--r-- | pressure/pressure.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/pressure/pressure.go b/pressure/pressure.go new file mode 100644 index 0000000..2948c72 --- /dev/null +++ b/pressure/pressure.go @@ -0,0 +1,58 @@ +package pressure + +import ( + "errors" + "fmt" +) + +type unit float32 + +const ( + Pascal unit = 1 + Kilopascal unit = 1_000 + Bar unit = 100_000 + PoundsPerSquareInch unit = 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 unit = Kilopascal + // DefaultUnitIndex is used to index UnitStrings(). + DefaultUnitIndex int32 = 1 // kPa +) + +func UnitFromString(s string) (unit, 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(unit), errors.New(fmt.Sprintf("invalid unit: '%s'", s)) + } +} + +type Pressure struct { + Val float32 + Unit unit +} + +func (p Pressure) AsUnit(u unit) float32 { + pa := p.Val * float32(p.Unit) // Convert to pascals. + return pa / float32(u) // Convert to desired unit. +} + +func Atmospheric() Pressure { + return Pressure{1, Bar} +} |