From cc0d171c5cd1057693960b7f63aee29e7f70ee8e Mon Sep 17 00:00:00 2001 From: sam-anthony Date: Sun, 27 Mar 2022 21:31:39 -0230 Subject: variable number of datapoints --- main.go | 66 +++++++++++-------------------------------------------------- ui.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ util.go | 27 +++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 54 deletions(-) create mode 100644 util.go diff --git a/main.go b/main.go index 516978b..4af9cbb 100644 --- a/main.go +++ b/main.go @@ -1,66 +1,37 @@ package main import ( - "fmt" g "github.com/AllenDang/giu" - "os" "time" ) const ( - // numPoints is the number of datapoints on the compressor map. - numPoints = 6 - gasConstant = 8.314472 airMolarMass = 0.0289647 // kg/mol ) -func check(err error) { - if err != nil { - fmt.Println(err) - os.Exit(1) - } -} +// numPoints is the number of datapoints on the compressor map. +var numPoints = 1 var ( displacement = volume{2000, cubicCentimetre} // selectedVolumeUnit is used to index volumeUnitStrings. selectedVolumeUnit = defaultVolumeUnitIndex -) -var engineSpeed = [numPoints]int32{2000, 3000, 4000, 5000, 6000, 7000} + engineSpeed = []int32{2000} -var volumetricEfficiency = [numPoints]int32{100, 100, 100, 100, 100, 100} - -var ( - intakeAirTemperature = [numPoints]temperature{ - {35, celcius}, - {35, celcius}, - {35, celcius}, - {35, celcius}, - {35, celcius}, - {35, celcius}, - } + volumetricEfficiency = []int32{80} + intakeAirTemperature = []temperature{{25, celcius}} // selectedTemperatureUnit is used to index temperatureUnitStrings. selectedTemperatureUnit = defaultTemperatureUnitIndex -) - -var ( - manifoldPressure = [numPoints]pressure{ - {100, defaultPressureUnit}, - {100, defaultPressureUnit}, - {100, defaultPressureUnit}, - {100, defaultPressureUnit}, - {100, defaultPressureUnit}, - {100, defaultPressureUnit}, - } + manifoldPressure = []pressure{{100, defaultPressureUnit}} // selectedPressureUnit is used to index pressureUnitStrings. selectedPressureUnit = defaultPressureUnitIndex ) -var pressureRatio [numPoints]float32 +var pressureRatio []float32 func pressureRatioAt(point int) float32 { u := pascal @@ -68,16 +39,12 @@ func pressureRatioAt(point int) float32 { a := atmosphericPressure().asUnit(u) return m / a } - func init() { - for i := 0; i < numPoints; i++ { - pressureRatio[i] = pressureRatioAt(i) - } + pressureRatio = append(pressureRatio, pressureRatioAt(0)) } var ( - engineMassFlowRate [numPoints]massFlowRate - + engineMassFlowRate []massFlowRate // selectedMassFlowRateUnit is used to index massFlowRateUnitStrings. selectedMassFlowRateUnit = defaultMassFlowRateUnitIndex ) @@ -104,11 +71,8 @@ func massFlowRateAt(point int) massFlowRate { check(err) return mfr } - func init() { - for i := 0; i < numPoints; i++ { - engineMassFlowRate[i] = massFlowRateAt(i) - } + engineMassFlowRate = append(engineMassFlowRate, massFlowRateAt(0)) } func loop() { @@ -122,16 +86,10 @@ func loop() { manifoldPressureRow(), pressureRatioRow(), massFlowRateRow(), + duplicateDeleteRow(), ). Columns( - g.TableColumn("Parameter"), - g.TableColumn("Unit"), - g.TableColumn("Point 1"), - g.TableColumn("Point 2"), - g.TableColumn("Point 3"), - g.TableColumn("Point 4"), - g.TableColumn("Point 5"), - g.TableColumn("Point 6"), + columns()..., ), ) } diff --git a/ui.go b/ui.go index ec49184..b12ff07 100644 --- a/ui.go +++ b/ui.go @@ -1,6 +1,7 @@ package main import ( + "fmt" g "github.com/AllenDang/giu" "strconv" ) @@ -183,3 +184,72 @@ func massFlowRateRow() *g.TableRowWidget { } return g.TableRow(widgets...) } + +func duplicateDeleteRow() *g.TableRowWidget { + widgets := []g.Widget{g.Label(""), g.Label("")} + for i := 0; i < numPoints; i++ { + i := i + widgets = append(widgets, g.Row( + g.Button("Duplicate").OnClick(func() { + numPoints++ + engineSpeed = insert( + engineSpeed, + engineSpeed[i], + i, + ) + volumetricEfficiency = insert( + volumetricEfficiency, + volumetricEfficiency[i], + i, + ) + intakeAirTemperature = insert( + intakeAirTemperature, + intakeAirTemperature[i], + i, + ) + manifoldPressure = insert( + manifoldPressure, + manifoldPressure[i], + i, + ) + pressureRatio = insert( + pressureRatio, + pressureRatio[i], + i, + ) + engineMassFlowRate = insert( + engineMassFlowRate, + engineMassFlowRate[i], + i, + ) + }), + g.Button("Delete").OnClick(func() { + if numPoints < 2 { + return + } + numPoints-- + engineSpeed = remove(engineSpeed, i) + volumetricEfficiency = remove(volumetricEfficiency, i) + intakeAirTemperature = remove(intakeAirTemperature, i) + manifoldPressure = remove(manifoldPressure, i) + pressureRatio = remove(pressureRatio, i) + engineMassFlowRate = remove(engineMassFlowRate, i) + }), + )) + } + return g.TableRow(widgets...) +} + +func columns() []*g.TableColumnWidget { + widgets := []*g.TableColumnWidget{ + g.TableColumn("Parameter"), + g.TableColumn("Unit"), + } + for i := 0; i < numPoints; i++ { + widgets = append( + widgets, + g.TableColumn(fmt.Sprintf("Point %d", i+1)), + ) + } + return widgets +} diff --git a/util.go b/util.go new file mode 100644 index 0000000..c11ff40 --- /dev/null +++ b/util.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "os" +) + +func check(err error) { + if err != nil { + fmt.Println(err) + os.Exit(1) + } +} + +func insert[T int32 | float32 | temperature | pressure | massFlowRate](slice []T, elem T, i int) []T { + return append( + slice[:i], + append( + []T{elem}, + slice[i:]..., + )..., + ) +} + +func remove[T int32 | float32 | temperature | pressure | massFlowRate](slice []T, i int) []T { + return append(slice[:i], slice[i+1:]...) +} -- cgit v1.2.3