The UFZ services GitLab and Mattermost will be unavailable on Monday, October 25 from 06:00 AM to 08:00 AM due to maintenance work.

Commit edca17ab authored by Johannes Leins's avatar Johannes Leins
Browse files

refurbished distScheme handling

parent 6b3e1e7a
Pipeline #29829 passed with stage
in 53 seconds
......@@ -46,7 +46,7 @@ namespace Constants
extern const int BASE_YEAR(1970);
extern const int DAYS_PER_YEAR(364);
extern const std::string NAME_DISTURBANCE_NONE("none");
extern const unsigned int ID_DISTURBANCE_NONE(0);
extern const unsigned int ID_DISTURBANCE_NONE(1);
extern const std::unordered_map<GlobalModel,std::string> GLOBAL_CLIMATE_MODEL_NAMES_BY_ENUM({
{CCCma, "CCCma-CanESM2"},
{CNRM, "CNRM-CERFACS-CNRM-CM5"},
......
......@@ -190,6 +190,7 @@ void Environment::setDisturbanceAt(Point coord, unsigned int disturbanceID) {
if(coord == Point()) {
uniformDisturbances = true;
uniformDisturbanceID = disturbanceID;
fixedDisturbanceCoords.clear();
return;
}
if(uniformDisturbances) {
......
......@@ -33,6 +33,7 @@ LifeStage::LifeStage(Environment* environment, string name, PopulationAtts* atts
this->gaining = false;
this->cohortCreationConstraint = new ConstraintDefault(!uniqueCohort);
nReset = -1; // signaling that reset will be called from constructor
migratory = false;
reset(environment, atts);
}
......@@ -48,7 +49,6 @@ LifeStage::LifeStage(const LifeStage& templStage)
}
void LifeStage::reset(Environment* environment, PopulationAtts* atts) {
migratory = false;
density = 0;
grossDiff = 0;
meanDensity = 0.0;
......
......@@ -60,8 +60,8 @@ const vector<ClimateScenario*> climateScenarios = {
bool memorySavingMode = false; // year climate data preload only
// input file handler
CSVHandlerDisturbance* handlerDisturbances;
CSVHandlerDisturbanceScheme* handlerScheme;
CSVHandlerDisturbance* handlerDisturbances = NULL;
CSVHandlerDisturbanceScheme* handlerScheme = NULL;
CSVHandlerLifeCycle* handlerLifeCylce;
CSVHandlerDensities* handlerDensities;
CSVHandlerInfluences* handlerInfluences;
......@@ -438,126 +438,99 @@ bool updateDisturbanceSchemes() {
if(disturbanceAppType == 0) {
return false;
}
if(uniformDisturbances) {
unsigned int iDist = 0;
unordered_set<Point> fixedCells;
for(auto coord : grasslandCoords) {
if((!soloPops || currSoloCoord == coord || handlerScheme->isFixedAt(Point())) && handlerScheme->isFixedAt(coord)) {
fixedCells.insert(coord);
}
unordered_set<Point> fixedCells;
unordered_map<string, unsigned int> nFixedName;
string maxNFixedName;
unordered_set<Point> unfixedCells;
unordered_map<string, unsigned int> nUnfixedName;
string maxNUnfixedName;
unordered_map<string, unsigned int> nDistName;
string maxNDistName;
for(auto coord : grasslandCoords) {
string distName = handlerScheme->getDisturbanceNameAt(coord);
if(nDistName.find(distName) == nDistName.end()) {
nDistName[distName] = 1;
} else {
nDistName[distName]++;
}
set<unsigned int> uniformDistIDs;
if(grasslandCells.size() == fixedCells.size()) { //if all cells are fixed, only create one scheme
if(handlerScheme->isFixedAt(Point())) { //if the wildcard is fixed, all not explicitly fixed cells get its disturbance
uniformDistIDs.insert(tools::strToHash(handlerScheme->getDisturbanceNameAt(Point())));
}else {
uniformDistIDs.insert(*(disturbanceIDs.begin())); // value here is basically arbitrary, because each cell has its explicit fix
}
}else{
uniformDistIDs.insert(disturbanceIDs.begin(), disturbanceIDs.end()); // set all disturbances to be used in unfixed cells
if(nDistName[distName] > nDistName[maxNDistName]) { //update disturbance with maximum occurrence
maxNDistName = distName;
}
for(auto distID : uniformDistIDs) {
if(distID == Constants::ID_DISTURBANCE_NONE) {
continue;
if((!soloPops || currSoloCoord == coord || handlerScheme->isFixedAt(Point())) && handlerScheme->isFixedAt(coord)) {
fixedCells.insert(coord);
if(nFixedName.find(distName) == nFixedName.end()) {
nFixedName[distName] = 1;
} else {
nFixedName[distName]++;
}
if(disturbanceAppType == 1 && iDist == 0) {
disturbanceSchemes.clear();
disturbanceSchemeNameByID.clear();
baseSchemeID = distID;
for(auto coord : grasslandCoords) {
if((!soloPops || currSoloCoord == coord) && fixedCells.find(coord) != fixedCells.end()) {
baseDisturbanceScheme[coord] = tools::strToHash(handlerScheme->getDisturbanceNameAt(coord));
} else {
baseDisturbanceScheme[coord] = distID;
}
}
if(nFixedName[distName] > nFixedName[maxNFixedName]) { //update fixed disturbance with maximum occurrence
maxNFixedName = distName;
}
map<Point, unsigned int> distByCoord({{Point(), distID}});
unordered_map<unsigned int, unsigned int> nDistID({{distID, grasslandCoords.size()}});
unsigned int maxNDistID = distID;
for(auto coord : fixedCells) { //count disturbance occurrences in fixed cells and add them to scheme
nDistID[distID]--;
unsigned int fixedDistedID = tools::strToHash(handlerScheme->getDisturbanceNameAt(coord));
if(nDistID.find(fixedDistedID) == nDistID.end()) {
nDistID[fixedDistedID] = 1;
} else {
nDistID[fixedDistedID]++;
}
if(nDistID[fixedDistedID] > nDistID[maxNDistID]) { //update disturbance with maximum occurrence
maxNDistID = fixedDistedID;
}
distByCoord[coord] = fixedDistedID;
}else {
unfixedCells.insert(coord);
if(nUnfixedName.find(distName) == nUnfixedName.end()) {
nUnfixedName[distName] = 1;
} else {
nUnfixedName[distName]++;
}
if(grasslandCells.size() == fixedCells.size()) { //if all cells are fixed, only create one scheme
// BEGIN set disturbance with maximum occurrence as wildcard by removing explicit occurrences and current wildcard first
for(auto coord : grasslandCoords) {
if(distByCoord[coord] == maxNDistID) {
distByCoord.erase(coord);
}
}
distByCoord.erase(Point());
distByCoord[Point()] = maxNDistID;
// END
string baseSchemeName = disturbanceNameByID[maxNDistID];
baseSchemeID = tools::strToHash(baseSchemeName);
disturbanceSchemes[baseSchemeID] = distByCoord;
disturbanceSchemeNameByID[baseSchemeID] = baseSchemeName;
break;
if(nUnfixedName[distName] > nUnfixedName[maxNUnfixedName]) { //update unfixed disturbance with maximum occurrence
maxNUnfixedName = distName;
}
disturbanceSchemes[distID] = distByCoord;
disturbanceSchemeNameByID[distID] = disturbanceNameByID.at(distID);
iDist++;
}
return true;
}
if(handlerScheme->isEmpty()) {
cout << "INFO: No disturbance scheme loaded from file. Skipping disturbances" << endl;
cout << handlerScheme->getMessage() << endl;
return true;
}
unordered_set<Point> unfixedCells;
for(auto coord : grasslandCoords) {
int distID = tools::strToHash(handlerScheme->getDisturbanceNameAt(coord));
baseDisturbanceScheme[coord] = distID;
if(prognosis && !handlerScheme->isFixedAt(coord)) {
unfixedCells.emplace(coord);
}
}
if(unfixedCells.size() == 0) {
if(disturbanceAppType == 1) {
disturbanceSchemes.clear();
disturbanceSchemeNameByID.clear();
string baseSchemeName = "fixed";
string baseSchemeName;
if(uniformDisturbances && fixedCells.size() < grasslandCoords.size()) {
baseSchemeName = maxNUnfixedName;
} else {
string distNameWildcard = handlerScheme->getDisturbanceNameAt(Point());
if(distNameWildcard != Constants::NAME_DISTURBANCE_NONE) {
baseSchemeName = distNameWildcard;
} else {
baseSchemeName = maxNDistName;
}
}
baseSchemeID = tools::strToHash(baseSchemeName);
disturbanceSchemes[baseSchemeID] = baseDisturbanceScheme;
unsigned int baseDistID = baseSchemeID;
map<Point, unsigned int> distByCoord({{Point(), baseDistID}});
for(auto coord : fixedCells) {
unsigned int fixedDistID = tools::strToHash(handlerScheme->getDisturbanceNameAt(coord));
baseDisturbanceScheme[coord] = fixedDistID;
distByCoord[coord] = fixedDistID;
}
for(auto coord : unfixedCells) {
baseDisturbanceScheme[coord] = baseDistID;
}
disturbanceSchemes[baseSchemeID] = distByCoord;
disturbanceSchemeNameByID[baseSchemeID] = baseSchemeName;
return true;
}
for(auto distID : disturbanceIDs) {
for(auto coord : unfixedCells) {
if(distID == baseDisturbanceScheme[coord]) {
if(uniformDisturbances) {
set<unsigned int> uniformDistIDs;
if(grasslandCells.size() == fixedCells.size()) { // If all cells are fixed
if(disturbanceAppType == 2) { // if clim only AND dist was applied (appType ==2) create ONE additional dist scheme
uniformDistIDs.insert(tools::strToHash(maxNDistName)); // set dist with max occurrence as key
}
}else{
uniformDistIDs.insert(disturbanceIDs.begin(), disturbanceIDs.end()); // set all disturbances to be used in unfixed cells
}
for(auto distID : uniformDistIDs) {
if(distID == Constants::ID_DISTURBANCE_NONE || distID == baseSchemeID) {
continue;
}
string key = "";
if(disturbanceAppType != BASE_IMMIGRATION && disturbanceAppType != IMMOBILE) {
key.append(std::to_string(coord.getX() + 1));
key.append("_");
key.append(std::to_string(coord.getY() + 1));
key.append("_");
}
key.append(disturbanceNameByID[distID]);
unsigned int keyID = tools::strToHash(key);
map<Point, unsigned int> disturbanceScheme;
if(disturbanceSchemes.find(keyID) != disturbanceSchemes.end()) {
disturbanceScheme = disturbanceSchemes.at(keyID);
}
disturbanceScheme[coord] = distID;
disturbanceSchemes[keyID] = disturbanceScheme;
disturbanceSchemeNameByID[keyID] = key;
map<Point, unsigned int> distByCoord({{Point(), distID}});
for(auto coord : fixedCells) {
distByCoord[coord] = tools::strToHash(handlerScheme->getDisturbanceNameAt(coord));
}
disturbanceSchemes[distID] = distByCoord;
disturbanceSchemeNameByID[distID] = disturbanceNameByID.at(distID);
}
}
// TODO in the end, this means, an initial scheme and a list of cells that change in a next scheme
return true;
}
......@@ -565,7 +538,7 @@ bool handleDisturbances() {
baseDisturbanceScheme.clear();
string baseDistName = "";
string baseSchemeName = "base";
int baseDistID = 1;
unsigned int baseDistID = 1;
baseSchemeID = tools::strToHash(baseSchemeName);
// TODO M66 as base disturbance ?
if(uniformDisturbances || disturbanceAppType == 0) {
......@@ -1443,7 +1416,7 @@ bool handleFileInput() {
return false;
}
// order of call to handler's isValid() method important
if(!handleDisturbances() && (!handlerDisturbances->isValid() || !handlerScheme->isValid())) {
if(!handleDisturbances() && ((handlerDisturbances && !handlerDisturbances->isValid()) || (handlerScheme && !handlerScheme->isValid()))) {
return false;
}
cout << endl << "Loading life cycle from file " << filenameStages << endl;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment