From 8710ca2b91f0d2095e0448f9cac024f1d266c75d Mon Sep 17 00:00:00 2001 From: tmnj Date: Thu, 29 May 2025 12:58:57 -0400 Subject: [PATCH 1/3] refactored LL Init --- lifeloss/lifeloss.go | 56 +++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/lifeloss/lifeloss.go b/lifeloss/lifeloss.go index 16e2c44..3e99bfe 100644 --- a/lifeloss/lifeloss.go +++ b/lifeloss/lifeloss.go @@ -37,28 +37,42 @@ type LifeLossProcess interface { // Init in the lifeloss package creates a life loss engine with the default settings func Init(seed int64, warningSystem warning.WarningResponseSystem) LifeLossEngine { - //initialize the default high lethality rate relationship - var HighPD paireddata.PairedData - json.Unmarshal(DefaultHighLethalityBytes, &HighPD) - //initialize the default low lethality rate relationships - var LowPD paireddata.PairedData - json.Unmarshal(DefaultLowLethalityBytes, &LowPD) - //create a lethality curve instance for High Lethality - High := LethalityCurve{data: HighPD} - //create a lethality curve instance for Low Lethality - Low := LethalityCurve{data: LowPD} - //create a map of lethality zone to lethality curve - lethalityCurves := make(map[LethalityZone]LethalityCurve) - lethalityCurves[HighLethality] = High - lethalityCurves[LowLethality] = Low - //initalize the stability criteria - stabilityCriteria := make(map[string]StabilityCriteria) - stabilityCriteria["woodunanchored"] = RescDamWoodUnanchored - stabilityCriteria["woodanchored"] = RescDamWoodAnchored - stabilityCriteria["masonryconcretebrick"] = RescDamMasonryConcreteBrick + createLethalityCurve := func(data []byte) (LethalityCurve, error) { + var pd paireddata.PairedData + if err := json.Unmarshal(data, &pd); err != nil { + return LethalityCurve{}, err + } + return LethalityCurve{data: pd}, nil + } + high, err := createLethalityCurve(DefaultHighLethalityBytes) + if err != nil { + log.Fatalf("Failed to initialize high lethality curve: %v", err) + } + low, err := createLethalityCurve(DefaultLowLethalityBytes) + if err != nil { + log.Fatalf("Failed to initialize low lethality curve: %v", err) + } + lethalityCurves := map[LethalityZone]LethalityCurve{ + HighLethality: high, + LowLethality: low, + } + const ( + WoodUnanchoredKey = "woodunanchored" + WoodAnchoredKey = "woodanchored" + MasonryConcreteBrickKey = "masonryconcretebrick" + ) + stabilityCriteria := map[string]StabilityCriteria{ + WoodUnanchoredKey: RescDamWoodUnanchored, + WoodAnchoredKey: RescDamWoodAnchored, + MasonryConcreteBrickKey: RescDamMasonryConcreteBrick, + } rng := rand.New(rand.NewSource(seed)) - - return LifeLossEngine{LethalityCurves: lethalityCurves, StabilityCriteria: stabilityCriteria, WarningSystem: warningSystem, SeedGenerator: rng} + return LifeLossEngine{ + LethalityCurves: lethalityCurves, + StabilityCriteria: stabilityCriteria, + WarningSystem: warningSystem, + SeedGenerator: rng, + } } func LifeLossHeader() []string { From d4481b26e4b7838c8171bb93ae6338fa8d6d7eaa Mon Sep 17 00:00:00 2001 From: tmnj Date: Thu, 29 May 2025 13:02:19 -0400 Subject: [PATCH 2/3] rf LL space b/w functions for easier navigation --- lifeloss/lifeloss.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lifeloss/lifeloss.go b/lifeloss/lifeloss.go index 3e99bfe..2eb93f5 100644 --- a/lifeloss/lifeloss.go +++ b/lifeloss/lifeloss.go @@ -78,6 +78,7 @@ func Init(seed int64, warningSystem warning.WarningResponseSystem) LifeLossEngin func LifeLossHeader() []string { return []string{"ll_u65", "ll_o65", "ll_tot"} //@TODO: Consider adding structure stability state, fatality rates, and sampled hazard parameters } + func LifeLossDefaultResults() []interface{} { var ll_u65, ll_o65, ll_tot int32 ll_u65 = 0 @@ -85,12 +86,14 @@ func LifeLossDefaultResults() []interface{} { ll_tot = 0 return []interface{}{ll_u65, ll_o65, ll_tot} } + func (le LifeLossEngine) RedistributePopulation(e hazards.HazardEvent, s structures.StructureDeterministic) (structures.StructureDeterministic, error) { remainingPop, _ := le.WarningSystem.WarningFunction()(s, e) sout := s.Clone() sout.PopulationSet = remainingPop return sout, nil } + func (le LifeLossEngine) EvaluateStabilityCriteria(e hazards.HazardEvent, s structures.StructureDeterministic) (Stability, error) { if e.Has(hazards.DV) && e.Has(hazards.Depth) || e.Has(hazards.Velocity) && e.Has(hazards.Depth) { sc, err := le.determineStability(s) @@ -126,6 +129,7 @@ func (le LifeLossEngine) ComputeLifeLoss(e hazards.HazardEvent, s structures.Str return le.submergenceCriteria(e, s, remainingPop, rng) } } + func applylethalityRateToPopulation(lethalityrate float64, population int32, rng *rand.Rand) int32 { result := 0 for i := 0; i < int(population); i++ { @@ -135,6 +139,7 @@ func applylethalityRateToPopulation(lethalityrate float64, population int32, rng } return int32(result) } + func (lle LifeLossEngine) submergenceCriteria(e hazards.HazardEvent, s structures.StructureDeterministic, remainingPop structures.PopulationSet, rng *rand.Rand) (consequences.Result, error) { //apply submergence criteria //log.Println("Submergence Based Lifeloss") @@ -209,6 +214,7 @@ func (lle LifeLossEngine) submergenceCriteria(e hazards.HazardEvent, s structure return consequences.Result{Headers: header, Result: []interface{}{llu65, llo65, llu65 + llo65}}, nil } } + func (lle LifeLossEngine) createLifeLossSet(popset structures.PopulationSet, lethalityRate float64, rng *rand.Rand) structures.PopulationSet { result := structures.PopulationSet{0, 0, 0, 0} result.Pop2amo65 = lle.evaluateLifeLoss(popset.Pop2amo65, lethalityRate, rng) @@ -218,6 +224,7 @@ func (lle LifeLossEngine) createLifeLossSet(popset structures.PopulationSet, let //log.Println(result) return result } + func (lle LifeLossEngine) evaluateLifeLoss(populationRemaining int32, lethalityRate float64, rng *rand.Rand) int32 { var result int32 = 0 var i int32 = 0 @@ -228,6 +235,7 @@ func (lle LifeLossEngine) evaluateLifeLoss(populationRemaining int32, lethalityR } return result } + func evaluateMobility(s structures.PopulationSet, rng *rand.Rand) map[Mobility]structures.PopulationSet { //determine based on age and disability result := make(map[Mobility]structures.PopulationSet) From ae94cfc5121c73b5d734ecade7762bbc062d16f9 Mon Sep 17 00:00:00 2001 From: Triet Nguyen <53122983+trietmnj@users.noreply.github.com> Date: Thu, 29 May 2025 14:45:48 -0400 Subject: [PATCH 3/3] Update lifeloss/lifeloss.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- lifeloss/lifeloss.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lifeloss/lifeloss.go b/lifeloss/lifeloss.go index 2eb93f5..2a58ea1 100644 --- a/lifeloss/lifeloss.go +++ b/lifeloss/lifeloss.go @@ -56,11 +56,6 @@ func Init(seed int64, warningSystem warning.WarningResponseSystem) LifeLossEngin HighLethality: high, LowLethality: low, } - const ( - WoodUnanchoredKey = "woodunanchored" - WoodAnchoredKey = "woodanchored" - MasonryConcreteBrickKey = "masonryconcretebrick" - ) stabilityCriteria := map[string]StabilityCriteria{ WoodUnanchoredKey: RescDamWoodUnanchored, WoodAnchoredKey: RescDamWoodAnchored,