From d382c2e0faab2520526632512e5c90b9a07be1e6 Mon Sep 17 00:00:00 2001 From: troyfrever Date: Fri, 6 Dec 2024 14:58:51 -0800 Subject: [PATCH] make const for MASS_MIN; refactor to remove various warnings --- src/fish.cpp | 33 ++++++++++++++++----------------- src/fish.h | 2 +- src/model.cpp | 19 ++++++++++--------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/fish.cpp b/src/fish.cpp index 16f4173..6d00608 100644 --- a/src/fish.cpp +++ b/src/fish.cpp @@ -45,10 +45,6 @@ const float CONS_Y = log(CQ) * (CTM - CTO + 2.0); const float CONS_X = (pow(CONS_Z, 2.0) * pow(1.0 + sqrt(1.0 + 40/CONS_Y), 2.0))/400.0; const float AVG_LOCAL_ABUNDANCE = 7.5839; -//const float MORT_CONST_C = 0.01939742; // old constant -// reparameterized: -const float MORT_CONST_C = 0.03096; -const float MORT_CONST_A = -0.42; #define DEPTH_CUTOFF 0.2f @@ -540,19 +536,20 @@ void Fish::dieStarvation(Model &model) { this->exitTime = model.time; } -float Fish::getPmax(const MapNode &loc) { +// ReSharper disable once CppMemberFunctionMayBeStatic +float Fish::getPmax(const MapNode &loc) { // NOLINT(*-convert-member-functions-to-static) constexpr float SQ_METER_TO_HECTARE_CONVERSION = 10000.0; + constexpr float GROWTH_FACTOR = 0.0007; - constexpr float growth_factor = 0.0007; - constexpr float PMAX_MIN = 0.2; - constexpr float PMAX_MAX = 1.0; + float Pmax = 0.8f - ((loc.popDensity * SQ_METER_TO_HECTARE_CONVERSION) * GROWTH_FACTOR); - float Pmax = 0.8f - ((loc.popDensity * SQ_METER_TO_HECTARE_CONVERSION) * growth_factor); + constexpr float PMAX_MIN = 0.2; if (Pmax < PMAX_MIN) { Pmax = PMAX_MIN; } if (isNearshore(loc.type)) { - Pmax = PMAX_MAX; + constexpr float PMAX_NEARSHORE = 1.0; + Pmax = PMAX_NEARSHORE; } return Pmax; } @@ -635,19 +632,21 @@ bool Fish::growAndDie(Model &model) { this->mortalityHistory->push_back(m); } this->mass = this->mass + g; + // Sample from bernoulli(m) to check if fish should die from mortality risk, // and check to make sure fish hasn't reached a critically low mass - if (unit_rand() > m && this->mass > 0.381f) { + constexpr float MASS_MIN = 0.381; + if (unit_rand() > m && this->mass > MASS_MIN) { this->forkLength = forkLengthFromMass(this->mass); return true; + } + + if (this->mass <= MASS_MIN) { + this->dieStarvation(model); } else { - if (this->mass <= 0.381f) { - this->dieStarvation(model); - } else { - this->dieMortality(model); - } - return false; + this->dieMortality(model); } + return false; } void Fish::addHistoryBuffers() { diff --git a/src/fish.h b/src/fish.h index 601f82d..31c27f1 100644 --- a/src/fish.h +++ b/src/fish.h @@ -105,7 +105,7 @@ class Fish { // Register this fish as dead due to starvation void dieStarvation(Model &model); - static float getPmax(const MapNode &loc); + float getPmax(const MapNode &loc); // Compute the growth (g) for a given location and movement cost (meters swum) and pmax float getGrowth(Model &model, MapNode &loc, float cost, float pmax); // compute growth, pmax calculated internally diff --git a/src/model.cpp b/src/model.cpp index 5889eed..517336c 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -16,8 +16,8 @@ #include // default mortality constants -const float MORT_CONST_C = 0.03096; -const float MORT_CONST_A = -0.42; +constexpr float MORT_CONST_C = 0.03096; +constexpr float MORT_CONST_A = -0.42; /* * Constructs a model instance from parameters and data filenames. @@ -72,11 +72,11 @@ Model::Model( time(0UL), deadCount(0), exitedCount(0), + mortConstA(MORT_CONST_A), + mortConstC(MORT_CONST_C), nextFishID(0UL), maxThreads(maxThreads), - recruitTagRate(0.5f), - mortConstA(MORT_CONST_A), - mortConstC(MORT_CONST_C) + recruitTagRate(0.5f) { // Load the map loadMap( @@ -116,7 +116,8 @@ Model::Model( float distFlow ) : map(map), hydroModel(map, depths, temps, distFlow), - recCounts(recCounts), recSizeDists(recSizeDists), + recCounts(recCounts), + recSizeDists(recSizeDists), recPoints(recPoints), recTimeIntercept(0), globalTimeIntercept(0), @@ -124,11 +125,11 @@ Model::Model( time(0UL), deadCount(0), exitedCount(0), + mortConstA(MORT_CONST_A), + mortConstC(MORT_CONST_C), nextFishID(0UL), maxThreads(maxThreads), - recruitTagRate(0.5f), - mortConstA(MORT_CONST_A), - mortConstC(MORT_CONST_C) + recruitTagRate(0.5f) { // Make room in the recruit plan vector (per-timestep recruit counts for the current day) this->recDayPlan.resize(24, 0UL);