Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfixes from Develop #25

Merged
merged 8 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions Landscape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,9 @@ int Landscape::readLandChange(int filenum, bool costs)
}
if (costs) {
if (c < 1) { // invalid cost
#if RS_RCPP
Rcpp::Rcout << "Found invalid cost value of " << c << " in cell x " << x << " and y " << y << std::endl;
#endif
hfile.close(); hfile.clear();
if (pfile.is_open()) {
pfile.close(); pfile.clear();
Expand All @@ -1294,8 +1297,8 @@ int Landscape::readLandChange(int filenum, bool costs)
}
}
}
}
}
} // for x
} // for y
#if RS_RCPP
hfile >> hfloat;
if (!hfile.eof()) EOFerrorR("habitatchgfile");
Expand Down Expand Up @@ -1421,6 +1424,9 @@ int Landscape::readLandChange(int filenum, bool costs)
}
if (costs) {
if (c < 1) { // invalid cost
#if RS_RCPP
Rcpp::Rcout << "Found invalid cost value of " << c << "in cell x " << x << " and y " << y << std::endl;
#endif
hfile.close(); hfile.clear();
if (pfile.is_open()) {
pfile.close(); pfile.clear();
Expand All @@ -1432,8 +1438,8 @@ int Landscape::readLandChange(int filenum, bool costs)
}
}
}
}
}
} // end x
} // end y
#if RS_RCPP
hfile >> hfloat;
if (!hfile.eof()) EOFerrorR("habitatchgfile");
Expand Down Expand Up @@ -1658,7 +1664,7 @@ bool Landscape::inInitialDist(Species* pSpecies, locn loc) {
void Landscape::deleteDistribution(Species* pSpecies) {
// WILL NEED TO SELECT DISTRIBUTION FOR CORRECT SPECIES ...
// ... CURRENTLY IT IS THE ONLY ONE ...
// ... FOR MULTIPLE SPECIES IT MAY BE BETTER TO USE A DYNAMIC ARRAY FOR
// ... FOR MULTIPLE SPECIES IT MAY BE BETTER TO USE A DYNAMIC ARRAY FOR
// SPECIES DISTRIBUTIONS INDEXED BY SPECIES NUMBER, RATHER THAN A VECTOR
if (distns[0] != 0) delete distns[0]; distns.clear();
}
Expand Down Expand Up @@ -1938,7 +1944,7 @@ int Landscape::readLandscape(int fileNum, string habfile, string pchfile, string
if (existsPatch(p)) {
pPatch = findPatch(p);
addNewCellToPatch(pPatch, x, y, h);
// addNewCellToPatch(findPatch(p),x,y,h);
// addNewCellToPatch(findPatch(p),x,y,h);
}
else {
pPatch = newPatch(seq++, p);
Expand Down Expand Up @@ -2288,12 +2294,11 @@ int Landscape::readCosts(string fname)
costs.close(); costs.clear();
return -1;
}
double tmpresolCost;
double tmpresolCost;
costs >> maxXcost >> header >> maxYcost >> header >> minLongCost;
costs >> header >> minLatCost >> header >> tmpresolCost >> header >> NODATACost;
resolCost = (int) tmpresolCost;


#if !RS_RCPP
MemoLine("Loading costs map. Please wait...");
#endif
Expand Down Expand Up @@ -2321,17 +2326,26 @@ resolCost = (int) tmpresolCost;
#endif
if (hc < 1 && hc != NODATACost) {
#if RS_RCPP && !R_CMD
Rcpp::Rcout << "Cost map my only contain values of 1 or higher, but found " << fcost << "." << endl;
Rcpp::Rcout << "Cost map may only contain values of 1 or higher, but found " << fcost << "." << endl;
#endif
// error - zero / negative cost not allowed
costs.close(); costs.clear();
return -999;
}
pCell = findCell(x, y);
if (pCell != 0) { // not no-data cell
pCell->setCost(hc);
if (hc > maxcost) maxcost = hc;
}
if (hc > 0){ // only if cost value is above 0 in a data cell
pCell->setCost(hc);
if (hc > maxcost) maxcost = hc;
} else { // if cost value is below 0
#if RS_RCPP && !R_CMD
Rcpp::Rcout << "Cost map may only contain values of 1 or higher in habiat cells, but found " << hc << " in cell x: " << x << " y: " << y << "." << endl;
#endif
// costs.close(); costs.clear(); // not sure if it should stop at this point
// return -999;
}

} // end not no data vell
}
}
#if RS_RCPP
Expand Down
9 changes: 8 additions & 1 deletion Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ int _tmain(int argc, _TCHAR* argv[])
return 1;
# else
assert(0.1 > 0.0); // assert does run correctly
run_unit_tests();
try
{
run_unit_tests();
}
catch (const std::exception& e)
{
cerr << endl << "Error: " << e.what() << endl;
}
cout << "All tests have completed." << endl;
return 0; // if tests succeed, we are happy
# endif // NDEBUG
Expand Down
46 changes: 39 additions & 7 deletions Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ int RunModel(Landscape* pLandscape, int seqsim)

}

#if RS_EMBARCADERO || LINUX_CLUSTER || RS_RCPP
#if LINUX_CLUSTER || RS_RCPP
// Check whether a specified directory path exists
bool is_directory(const char* pathname) {
struct stat info;
Expand Down Expand Up @@ -1687,23 +1687,40 @@ void OutParameters(Landscape* pLandscape)

if (trfr.moveModel) {
string plusmating = "+ mating requirements";
ssteps = pSpecies->getSteps(0, 0);

outPar << "MIN. No. OF STEPS:\t " << ssteps.minSteps << endl;
outPar << "MAX. No. OF STEPS:\t ";
if (ssteps.maxSteps == 99999999) outPar << "not applied" << endl;
else outPar << ssteps.maxSteps << endl;

if (sett.sexDep) {
nsexes = 2;
outPar << sexdept << "yes" << endl;
if (sett.stgDep) {
nstages = sstruct.nStages;
outPar << stgdept << "yes" << endl;
for (int i = 0; i < nstages; i++) {
if (dem.stageStruct && nstages > 1) outPar << "stage " << i << ": " << endl;
for (int sx = 0; sx < nsexes; sx++) {
if (sx == 0) outPar << "FEMALES:" << endl;
else outPar << "MALES:" << endl;
ssteps = pSpecies->getSteps(i, sx);

outPar << "MIN. No. OF STEPS:\t " << ssteps.minSteps << endl;
outPar << "MAX. No. OF STEPS:\t ";
if (ssteps.maxSteps == 99999999) outPar << "not applied" << endl;
else outPar << ssteps.maxSteps << endl;
}
}
}
else { // !sett.stgDep
nstages = 1;
outPar << stgdept << "no" << endl;
for (int sx = 0; sx < nsexes; sx++) {
if (sx == 0) outPar << "FEMALES:" << endl;
else outPar << "MALES:" << endl;
ssteps = pSpecies->getSteps(0, sx);

outPar << "MIN. No. OF STEPS:\t " << ssteps.minSteps << endl;
outPar << "MAX. No. OF STEPS:\t ";
if (ssteps.maxSteps == 99999999) outPar << "not applied" << endl;
else outPar << ssteps.maxSteps << endl;
}
}
}
else { // !sett.sexDep
Expand All @@ -1712,10 +1729,25 @@ void OutParameters(Landscape* pLandscape)
if (sett.stgDep) {
nstages = sstruct.nStages;
outPar << stgdept << "yes" << endl;
for (int i = 0; i < nstages; i++) {
if (dem.stageStruct && nstages > 1) outPar << "stage " << i << ": " << endl;
ssteps = pSpecies->getSteps(i, 0);

outPar << "MIN. No. OF STEPS:\t " << ssteps.minSteps << endl;
outPar << "MAX. No. OF STEPS:\t ";
if (ssteps.maxSteps == 99999999) outPar << "not applied" << endl;
else outPar << ssteps.maxSteps << endl;
}
}
else { // !sett.stgDep
nstages = 1;
outPar << stgdept << "no" << endl;
ssteps = pSpecies->getSteps(0, 0);

outPar << "MIN. No. OF STEPS:\t " << ssteps.minSteps << endl;
outPar << "MAX. No. OF STEPS:\t ";
if (ssteps.maxSteps == 99999999) outPar << "not applied" << endl;
else outPar << ssteps.maxSteps << endl;
}
}
for (int sx = 0; sx < nsexes; sx++) {
Expand Down
24 changes: 12 additions & 12 deletions RSrandom.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
/*----------------------------------------------------------------------------
*
* Copyright (C) 2020 Greta Bocedi, Stephen C.F. Palmer, Justin M.J. Travis, Anne-Kathleen Malchow, Damaris Zurell
*
*
* Copyright (C) 2020 Greta Bocedi, Stephen C.F. Palmer, Justin M.J. Travis, Anne-Kathleen Malchow, Damaris Zurell
*
* This file is part of RangeShifter.
*
*
* RangeShifter is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* RangeShifter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with RangeShifter. If not, see <https://www.gnu.org/licenses/>.
*
*
--------------------------------------------------------------------------*/


#include "RSrandom.h"

//--------------- 2.) New version of RSrandom.cpp

#if !RS_RCPP
#if !RS_RCPP

#if RSDEBUG
#include "Parameters.h"
Expand Down Expand Up @@ -113,7 +113,7 @@ int RSrandom::Poisson(double mean)
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------

#else // if RS_RCPP
#else // if RS_RCPP

//--------------- 3.) R package version of RSrandom.cpp

Expand Down Expand Up @@ -257,11 +257,11 @@ int RSrandom::Poisson(double mean)
#endif // RS_RCPP


#if RSDEBUG
#if RSDEBUG && !RS_RCPP
void testRSrandom() {

{
// Bernoulli distribution
// Bernoulli distribution
// Abuse cases
assert_error("Bernoulli's p cannot be negative.\n", []{
RSrandom rsr;
Expand Down
28 changes: 16 additions & 12 deletions Species.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ Species::Species(void)

// initialise settlement parameters
stgDepSett = false; sexDepSett = false; indVarSett = false;
minSteps = 0; maxSteps = 99999999;

for (int i = 0; i < NSTAGES; i++) {
for (int j = 0; j < NSEXES; j++) {
densDepSett[i][j] = false; wait[i][j] = false; go2nbrLocn[i][j] = false; findMate[i][j] = false;
maxStepsYr[i][j] = 99999999;
maxStepsYr[i][j] = 99999999; minSteps[i][j] = 0; maxSteps[i][j] = 99999999;
s0[i][j] = 1.0; alphaS[i][j] = 0.0; betaS[i][j] = 1.0;
}
}
Expand Down Expand Up @@ -1276,24 +1276,28 @@ settleRules Species::getSettRules(short stg, short sex) {
}

void Species::setSteps(const short stg, const short sex, const settleSteps s) {
if (stg == 0 && sex == 0) {
if (s.minSteps >= 0) minSteps = s.minSteps;
else minSteps = 0;
if (s.maxSteps >= 1) maxSteps = s.maxSteps;
else maxSteps = 99999999;
}
if (stg >= 0 && stg < NSTAGES && sex >= 0 && sex < NSEXES) {
if (s.maxStepsYr >= 1) maxStepsYr[stg][sex] = s.maxStepsYr;
else maxStepsYr[stg][sex] = 99999999;
if (s.minSteps >= 0) minSteps[stg][sex] = s.minSteps;
else minSteps[stg][sex] = 0;
if (s.maxSteps >= 1) maxSteps[stg][sex] = s.maxSteps;
else maxSteps[stg][sex] = 99999999;
}
}

settleSteps Species::getSteps(short stg, short sex) {
settleSteps s;
s.minSteps = minSteps;
s.maxSteps = maxSteps;
if (stg >= 0 && stg < NSTAGES && sex >= 0 && sex < NSEXES) s.maxStepsYr = maxStepsYr[stg][sex];
else s.maxStepsYr = 99999999;
if (stg >= 0 && stg < NSTAGES && sex >= 0 && sex < NSEXES) {
s.maxStepsYr = maxStepsYr[stg][sex];
s.minSteps = minSteps[stg][sex];
s.maxSteps = maxSteps[stg][sex];
}
else {
s.maxStepsYr = 99999999;
s.minSteps = 0;
s.maxSteps = 99999999;
}
return s;
}

Expand Down
4 changes: 2 additions & 2 deletions Species.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,8 @@ class Species {
bool wait[NSTAGES][NSEXES]; // wait to continue moving next season (stage-structured model only)
bool go2nbrLocn[NSTAGES][NSEXES]; // settle in neighbouring cell/patch if available (ditto)
bool findMate[NSTAGES][NSEXES];
int minSteps; // minimum no. of steps
int maxSteps; // maximum total no. of steps
int minSteps[NSTAGES][NSEXES]; // minimum no. of steps
int maxSteps[NSTAGES][NSEXES]; // maximum total no. of steps
int maxStepsYr[NSTAGES][NSEXES]; // maximum no. of steps in any one dispersal period
float s0[NSTAGES][NSEXES]; // maximum settlement probability
float alphaS[NSTAGES][NSEXES]; // slope of the settlement reaction norm to density
Expand Down
Loading