Skip to content

Commit

Permalink
useful shorthands
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoPannetier committed Dec 2, 2024
1 parent 48ec522 commit 4a57e65
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 45 deletions.
17 changes: 7 additions & 10 deletions src/RScore/Community.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void Community::initialise(Species* pSpecies, int year) {
}
}
else { // cell-based model - is cell(patch) suitable
if (pch.pPatch->getK() > 0.0) {
if (pch.pPatch->isSuitable()) {
suitablePatches.insert(patchnum);
}
}
Expand Down Expand Up @@ -119,8 +119,7 @@ void Community::initialise(Species* pSpecies, int year) {
pch = pLandscape->getPatchData(i);
if (pch.pPatch->withinLimits(limits)) {
patchnum = pch.pPatch->getPatchNum();
if (patchnum != 0 && pch.pPatch->getK() > 0.0) {
// patch is suitable
if (!pch.pPatch->isMatrix() && pch.pPatch->isSuitable()) {
selectedPatches.insert(patchnum);
}
}
Expand Down Expand Up @@ -170,7 +169,7 @@ void Community::initialise(Species* pSpecies, int year) {
if (pCell != nullptr) { // not a no-data cell
pPatch = pCell->getPatch();
if (pPatch != nullptr) {
if (pPatch->getPatchNum() != 0) { // not the matrix patch
if (!pPatch->isMatrix()) { // not the matrix patch
selectedPatches.insert(pPatch->getPatchNum());
}
}
Expand Down Expand Up @@ -212,7 +211,7 @@ void Community::initialise(Species* pSpecies, int year) {
if (pLandscape->existsPatch(iind.patchID)) {
pPatch = pLandscape->findPatch(iind.patchID);
Species* pSpecies = findSpecies(iind.speciesID);
if (pPatch->getK() > 0.0) { // patch is suitable
if (pPatch->isSuitable()) {
initialInd(pLandscape, pSpecies, pPatch, pPatch->getRandomCell(), indIx);
}
}
Expand All @@ -222,7 +221,7 @@ void Community::initialise(Species* pSpecies, int year) {
if (pCell != nullptr) {
pPatch = pCell->getPatch();
if (pPatch != nullptr) {
if (pPatch->getK() > 0.0) { // patch is suitable
if (pPatch->isSuitable()) {
Species* pSpecies = findSpecies(iind.speciesID);
initialInd(pLandscape, pSpecies, pPatch, pCell, indIx);
}
Expand Down Expand Up @@ -536,8 +535,7 @@ commStats Community::getStats()

if (patchPop.pPatch != nullptr) {

localK = patchPop.pPatch->getK();
if (localK > 0.0) s.suitable++;
if (patchPop.pPatch->isSuitable() > 0.0) s.suitable++;
if (patchPop.nInds > 0 && patchPop.breeding) {
s.occupied++;
patchLimits pchlim = patchPop.pPatch->getLimits();
Expand Down Expand Up @@ -627,8 +625,7 @@ void Community::outPop(int rep, int yr, int gen)
}
}
for (auto pop : popns) {
float localK = pop->getPatch()->getK();
if (localK > 0.0 || pop->totalPop() > 0) {
if (pop->getPatch()->isSuitable() || pop->totalPop() > 0) {
pop->outPopulation(outPopOfs, rep, yr, gen, env.local, eps, land.patchModel, writeEnv, gradK);
}
}
Expand Down
18 changes: 5 additions & 13 deletions src/RScore/Individual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,6 @@ bool Individual::moveKernel(Landscape* pLandscape, const bool absorbing)
int newX = 0, newY = 0;
bool isDispersing = true;
double xrand, yrand, meandist, dist, r1, rndangle, nx, ny;
float localK;
trfrKernelParams kern;
Cell* pCell;
Patch* pPatch;
Expand Down Expand Up @@ -835,9 +834,8 @@ bool Individual::moveKernel(Landscape* pLandscape, const bool absorbing)
}
else {
pCurrCell = pCell;
if (pPatch == 0) localK = 0.0; // matrix
else localK = pPatch->getK();
if (patchNum > 0 && localK > 0.0) { // found a new patch
bool suitable = pPatch == nullptr ? false : pPatch->isSuitable();
if (patchNum > 0 && suitable) { // found a new patch
status = waitSettlement; // record as potential settler
}
else {
Expand Down Expand Up @@ -889,7 +887,6 @@ bool Individual::moveStep(Landscape* pLandscape,
{
if (status != dispersing) return false; // not currently dispersing

int patchNum;
int newX, newY;
locn loc;
bool isDispersing = true;
Expand All @@ -907,11 +904,6 @@ bool Individual::moveStep(Landscape* pLandscape,
settleSteps settsteps = pSpecies->getSteps(stage, sex);

pPatch = pCurrCell->getPatch();
if (pPatch == nullptr) { // no data
patchNum = 0;
} else {
patchNum = pPatch->getPatchNum();
}

// Apply step-dependent mortality risk
if (pPatch == pNatalPatch
Expand All @@ -935,7 +927,7 @@ bool Individual::moveStep(Landscape* pLandscape,
(path->year)++;
(path->total)++;

if (pPatch == nullptr || patchNum == 0) { // not in a patch
if (pPatch == nullptr || pPatch->isMatrix()) { // not in a patch
// Reset path settlement status
if (path != nullptr) path->settleStatus = 0;
(path->out)++;
Expand Down Expand Up @@ -1045,8 +1037,8 @@ bool Individual::moveStep(Landscape* pLandscape,
if (pPatch != nullptr // not no-data area or matrix
&& path->total >= settsteps.minSteps) {
if (pPatch != pNatalPatch
&& pPatch->getK() > 0.0) {
status = waitSettlement; // new patch is suitable
&& pPatch->isSuitable()) {
status = waitSettlement;
}
}
if (status != waitSettlement
Expand Down
6 changes: 3 additions & 3 deletions src/RScore/Landscape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ set<int> Landscape::samplePatches(const string& samplingOption, int nbToSample,

// Get list of viable patches where the species is present
for (auto p : patches) {
if (p->getPatchNum() == 0) continue; // skip the matrix
if (p->isMatrix()) continue; // skip
if (samplingOption == "random") { // then all patches are eligible
eligiblePatches.push_back(p->getPatchNum());
}
Expand Down Expand Up @@ -968,7 +968,7 @@ void Landscape::updateCarryingCapacity(Species* pSpecies, int yr, short landIx)
landlimits.yMax = maxY;
int npatches = static_cast<int>(patches.size());
for (int i = 0; i < npatches; i++) {
if (patches[i]->getPatchNum() != 0) { // not matrix patch
if (!patches[i]->isMatrix()) {
patches[i]->setCarryingCapacity(pSpecies, landlimits, getGlobalStoch(yr),
nHab, rasterType, landIx, gradK);
}
Expand Down Expand Up @@ -2669,7 +2669,7 @@ void Landscape::outConnect(int rep, int yr) {
for (int i = 0; i < npatches; i++) {
patchnum0 = patches[i]->getPatchNum();
if (patchnum0 != 0) {
if (patches[i]->getK() > 0.0) { // suitable patch
if (patches[i]->isSuitable()) {
outConnMat << rep << "\t" << yr
<< "\t" << patchnum0 << "\t-999\t" << emigrants[i] << endl;
outConnMat << rep << "\t" << yr
Expand Down
2 changes: 2 additions & 0 deletions src/RScore/Patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ void Patch::setCarryingCapacity(Species* pSpecies, patchLimits landlimits, float

float Patch::getK() { return localK; }

bool Patch::isSuitable() { return localK > 0.0f; }

int Patch::getInitNbInds(const bool& isPatchModel, const int& landResol) const {

initParams init = paramsInit->getInit();
Expand Down
5 changes: 5 additions & 0 deletions src/RScore/Patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Patch {

int getSeqNum();
int getPatchNum();
bool isMatrix() { return patchNum == 0; };
int getNCells();

patchLimits getLimits(); // Returns the minimum and maximum co-ordinates of the patch
Expand All @@ -98,10 +99,13 @@ class Patch {
void resetLimits(); // Reset minimum and maximum co-ordinates of the patch

void addCell(Cell* pCell, int x, int y);

// Return co-ordinates of a specified cell
locn getCellLocn(int ix);

// Return pointer to a specified cell
Cell* getCell(int ix);

// Return co-ordinates of patch centroid
locn getCentroid();
void removeCell(Cell* pCell);
Expand All @@ -121,6 +125,7 @@ class Patch {
void setCarryingCapacity(Species* pSpecies, patchLimits landlimits, float epsGlobal,
short nHab, short rasterType, short landIx, bool gradK);
float getK();
bool isSuitable();

int getInitNbInds(const bool& isPatchModel, const int& landResol) const;

Expand Down
36 changes: 17 additions & 19 deletions src/RScore/Population.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1162,9 +1162,9 @@ int Population::transfer(Landscape* pLandscape, short landIx, short nextseason)
if (pPatch != nullptr) { // not no-data area

// Check whether patch is suitable
if (pPatch->getPatchNum() > 0 // not the matrix
if (!pPatch->isMatrix()
&& pPatch != inds[i]->getNatalPatch()// or natal patch
&& pPatch->getK() > 0.0) { // suitable
&& pPatch->isSuitable()) {

// Check mate reqt if applicable
if (sett.findMate) {
Expand Down Expand Up @@ -1200,19 +1200,18 @@ bool Population::isMatePresent(Cell* pCell, short othersex)

pPatch = pCell->getPatch();
if (pPatch != nullptr) {
if (pPatch->getPatchNum() > 0 // not the matrix patch
&& pPatch->getK() > 0.0) { // suitable

pNewPopn = pPatch->getPop();
if (pNewPopn != nullptr) {
for (int stg = 0; stg < nStages; stg++) {
if (pNewPopn->nInds[stg][othersex] > 0)
return true; // one is enough
}
if (!pPatch->isMatrix() && pPatch->isSuitable()) {

pNewPopn = pPatch->getPop();
if (pNewPopn != nullptr) {
for (int stg = 0; stg < nStages; stg++) {
if (pNewPopn->nInds[stg][othersex] > 0)
return true; // one is enough
}
// If empty, check for incoming settlers
if (pPatch->getPossSettlers(othersex) > 0)
return true;
}
// If empty, check for incoming settlers
if (pPatch->getPossSettlers(othersex) > 0)
return true;
}
}
return false; // no mates? :(
Expand Down Expand Up @@ -1443,14 +1442,14 @@ void Population::outPopulation(ofstream& outPopOfs, int rep, int yr, int gen, bo
Cell* pCell = pPatch->getRandomCell();
if (pCell != 0) eps = pCell->getEps();
}
if (pPatch->getPatchNum() == 0) { // matrix
if (pPatch->isMatrix()) {
outPopOfs << "\t0\t0\t0";
}
else {
float k = pPatch->getK();
float envval = 0.0;
pCell = pPatch->getRandomCell();
if (pCell != 0) envval = pCell->getEnvVal();
if (pCell != nullptr) envval = pCell->getEnvVal();
outPopOfs << "\t" << eps << "\t" << envval << "\t" << k;
}
}
Expand Down Expand Up @@ -1603,7 +1602,7 @@ void Population::outIndividual(ofstream& outIndsOfs, Landscape* pLandscape, int

void Population::outputTraitPatchInfo(ofstream& outtraits, int rep, int yr, int gen, bool patchModel)
{
if (pPatch->getK() > 0.0 && this->getNInds() > 0) {
if (pPatch->isSuitable() && this->getNInds() > 0) {
outtraits << rep << "\t" << yr << "\t" << gen;
if (patchModel) {
outtraits << "\t" << pPatch->getPatchNum();
Expand All @@ -1626,8 +1625,7 @@ traitsums Population::outTraits(ofstream& outtraits, const bool& writefile)
// provided that the patch is suitable (i.e. non-zero carrying capacity)

Species* pSpecies;
float localK = pPatch->getK();
if (localK > 0.0 && this->getNInds() > 0) {
if (pPatch->isSuitable() && this->getNInds() > 0) {

pSpecies = this->getSpecies();
demogrParams dem = pSpecies->getDemogrParams();
Expand Down

0 comments on commit 4a57e65

Please sign in to comment.