Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Jun 28, 2024
1 parent de79fa6 commit 5624194
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@
#include <string>
#include <vector>

#include <antares/study/area/area.h>

namespace Antares
{
class HydroErrorsCollector
{
public:
HydroErrorsCollector() = default;
void IncreaseCounterForArea(const Antares::Data::Area* area);
bool StopExecution() const;
bool ErrorsLimitReached() const;
void FatalErrorHit();
// void RecordFatalErrors(const std::string& msg, uint year);
void Collect(const std::string& area_name, const std::string& message);
void Collect(const std::string& message);
bool CheckForFatalErrors() const;

private:
std::unordered_map<const Antares::Data::Area*, uint> area_errors_counter_;
std::unordered_map<std::string, uint> area_errors_counter_;
bool errors_limit_reached_ = false;
bool stop_ = false;
// std::vector<std::string> fatal_errors_;
};
} // namespace Antares
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class HydroInputsChecker
{
public:
explicit HydroInputsChecker(Antares::Data::Study& study);
bool Execute(uint year);
bool StopExecution() const;
Execute(uint year);
void CheckForFatalErrors() const;
void CheckFinalReservoirLevelsConfiguration(uint year);

private:
Expand Down
65 changes: 30 additions & 35 deletions src/solver/hydro/management/HydroErrorsCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,44 @@

#include <yuni/core/logs.h>

void HydroErrorsCollector::IncreaseCounterForArea(const Antares::Data::Area* area)
#include "antares/antares/fatal-error.h"

namespace Antares
{
void HydroErrorsCollector::Collect(const std::string& area_name, const std::string& message)
{
if (errors_limit_reached_ || !area)
if (errors_limit_reached_)
{
return;
throw FatalError("Hydro validation has failed !")
}
else
{
logs.error() << "In Area " << area_name << " " << message;
area_errors_counter_[area_name]++;
errors_limit_reached_ = area_errors_counter_[area] > 10;
stop_ = true;
}

area_errors_counter_[area]++;
errors_limit_reached_ = area_errors_counter_[area] > 10 || errors_limit_reached_;
stop_ = true;
}

bool HydroErrorsCollector::ErrorsLimitReached() const
{
return errors_limit_reached_;
}

bool HydroErrorsCollector::StopExecution() const
void HydroErrorsCollector::Collect(const std::string& message)
{
return stop_ || errors_limit_reached_;
if (errors_limit_reached_)
{
throw FatalError("Hydro validation has failed !")
}
else
{
logs.error() << message;
stop_ = true;
}
}

void HydroErrorsCollector::FatalErrorHit()
void HydroErrorsCollector::CheckForFatalErrors() const
{
stop_ = true;
if (stop_ || errors_limit_reached_)
{
throw FatalError("Hydro validation has failed !")
}
}

// void HydroErrorsCollector::RecordFatalErrors(const std::string& msg, uint year)
// {
// fatal_errors_.push_back("In year " + std::to_string(year) + " " + msg);
// }

// void HydroErrorsCollector::RecordFatalErrors(const std::string& msg,
// uint year,
// const Antares::Data::Area* area)
// {
// fatal_errors_.push_back("In area " + area.name + " " + std::to_string(year) + " " + msg);
// }

// void HydroErrorsCollector::PrintFatalsErrors() const
// {
// for (const auto& msg: fatal_errors)
// {
// logs.error() << msg;
// }
// }
} // namespace Antares
57 changes: 30 additions & 27 deletions src/solver/hydro/management/HydroInputsChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <yuni/core/logs.h>

#include <antares/utils/utils.h>
#include "antares/antares/fatal-error.h"
#include "antares/solver/hydro/management/finalLevelValidator.h"
#include "antares/solver/hydro/monthly/h2o_m_donnees_annuelles.h"
#include "antares/solver/hydro/monthly/h2o_m_fonctions.h"
Expand All @@ -42,20 +41,20 @@ HydroInputsChecker::HydroInputsChecker(Antares::Data::Study& study):
{
}

bool HydroInputsChecker::Execute(uint year)
HydroInputsChecker::Execute(uint year)
{
prepareInflows_.Run(year);
minGenerationScaling_.Run(year);
if (!checksOnGenerationPowerBounds(year))
{
errorCollector_.FatalErrorHit();
logs.error() << "hydro inputs checks: invalid minimum generation";
std::ostringstream msg;
msg << "hydro inputs checks: invalid minimum generation in year " << year;
errorCollector_.Collect(msg.str());
}
if (parameters_.useCustomScenario)
{
CheckFinalReservoirLevelsConfiguration(year);
}
return !errorCollector_.ErrorsLimitReached();
}

bool HydroInputsChecker::checksOnGenerationPowerBounds(uint year)
Expand Down Expand Up @@ -120,11 +119,12 @@ bool HydroInputsChecker::checkWeeklyMinGeneration(uint year, const Data::Area& a
}
if (totalWeekMingen > totalWeekInflows)
{
errorCollector_.IncreaseCounterForArea(&area);
logs.error() << "In Area " << area.name << " the minimum generation of "
<< totalWeekMingen << " MW in week " << week + 1 << " of TS-"
<< area.hydro.series->mingen.getSeriesIndex(year) + 1
<< " is incompatible with the inflows of " << totalWeekInflows << " MW.";
std::ostringstream msg;
msg << "In Area " << area.name << " the minimum generation of " << totalWeekMingen
<< " MW in week " << week + 1 << " of TS-"
<< area.hydro.series->mingen.getSeriesIndex(year) + 1
<< " is incompatible with the inflows of " << totalWeekInflows << " MW.";
errorCollector_.Collect(area.name, msg);
return false;
}
}
Expand All @@ -136,12 +136,13 @@ bool HydroInputsChecker::checkYearlyMinGeneration(uint year, const Data::Area& a
const auto& data = area.hydro.managementData.at(year);
if (data.totalYearMingen > data.totalYearInflows)
{
errorCollector_.IncreaseCounterForArea(&area);
std::ostringstream msg;

// Yearly minimum generation <= Yearly inflows
logs.error() << "In Area " << area.name << " the minimum generation of "
<< data.totalYearMingen << " MW of TS-"
<< area.hydro.series->mingen.getSeriesIndex(year) + 1
<< " is incompatible with the inflows of " << data.totalYearInflows << " MW.";
msg << "In Area " << area.name << " the minimum generation of " << data.totalYearMingen
<< " MW of TS-" << area.hydro.series->mingen.getSeriesIndex(year) + 1
<< " is incompatible with the inflows of " << data.totalYearInflows << " MW.";
errorCollector_.Collect(area.name, msg);
return false;
}
return true;
Expand All @@ -156,12 +157,13 @@ bool HydroInputsChecker::checkMonthlyMinGeneration(uint year, const Data::Area&
// Monthly minimum generation <= Monthly inflows for each month
if (data.totalMonthMingen[realmonth] > data.totalMonthInflows[realmonth])
{
errorCollector_.IncreaseCounterForArea(&area);
logs.error() << "In Area " << area.name << " the minimum generation of "
<< data.totalMonthMingen[realmonth] << " MW in month " << month + 1
<< " of TS-" << area.hydro.series->mingen.getSeriesIndex(year) + 1
<< " is incompatible with the inflows of "
<< data.totalMonthInflows[realmonth] << " MW.";
std::ostringstream msg;
msg << "In Area " << area.name << " the minimum generation of "
<< data.totalMonthMingen[realmonth] << " MW in month " << month + 1 << " of TS-"
<< area.hydro.series->mingen.getSeriesIndex(year) + 1
<< " is incompatible with the inflows of " << data.totalMonthInflows[realmonth]
<< " MW.";
errorCollector_.Collect(area.name, msg);
return false;
}
}
Expand All @@ -188,12 +190,13 @@ bool HydroInputsChecker::checkGenerationPowerConsistency(uint year)

if (max < min)
{
errorCollector_.IncreaseCounterForArea(&area);
std::ostringstream msg;
logs.error() << "In area: " << area.name << " [hourly] minimum generation of "
<< min << " MW in timestep " << h + 1 << " of TS-" << tsIndexMin + 1
<< " is incompatible with the maximum generation of " << max
<< " MW in timestep " << h + 1 << " of TS-" << tsIndexMax + 1
<< " MW.";
errorCollector_.Collect(area.name, msg);
ret = false;
return;
}
Expand Down Expand Up @@ -227,9 +230,9 @@ void HydroInputsChecker::CheckFinalReservoirLevelsConfiguration(uint year)
errorCollector_);
if (!validator.check())
{
errorCollector_.IncreaseCounterForArea(&area);
errorCollector_.FatalErrorHit();
logs.error() << "hydro final level : infeasibility";
std::ostringstream msg;
msg << "hydro final level : infeasibility";
errorCollector_.Collect(area.name, msg);
}
if (validator.finalLevelFineForUse())
{
Expand All @@ -238,9 +241,9 @@ void HydroInputsChecker::CheckFinalReservoirLevelsConfiguration(uint year)
});
} // End function CheckFinalReservoirLevelsConfiguration

bool HydroInputsChecker::StopExecution() const
bool HydroInputsChecker::CheckForFatalErrors() const
{
return errorCollector_.StopExecution();
errorCollector_.CheckForFatalErrors();
}

} // namespace Antares
29 changes: 18 additions & 11 deletions src/solver/hydro/management/finalLevelValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,12 @@ bool FinalLevelValidator::hydroAllocationStartMatchesSimulation() const
if (lastSimulationDay_ == DAYS_PER_YEAR && initReservoirLvlMonth == firstMonthOfSimulation_)
return true;

logs.error() << "Year " << year_ + 1 << ", area '" << areaName_ << "' : "
<< "Hydro allocation must start on the 1st simulation month and "
<< "simulation last a whole year";
std::ostringstream msg;
msg << "Year " << year_ + 1 << ", area '" << areaName_
<< "' : " << "Hydro allocation must start on the 1st simulation month and "
<< "simulation last a whole year";
errorCollector_.Collect(areaName_, msg);

return false;
}

Expand All @@ -116,10 +119,12 @@ bool FinalLevelValidator::isFinalLevelReachable() const

if ((finalLevel_ - initialLevel_) * reservoirCapacity > totalYearInflows)
{
logs.error() << "Year: " << year_ + 1 << ". Area: " << areaName_
<< ". Incompatible total inflows: " << totalYearInflows
<< " with initial: " << initialLevel_
<< " and final: " << finalLevel_ << " reservoir levels.";
std::ostringstream msg;
msg << "Year: " << year_ + 1 << ". Area: " << areaName_
<< ". Incompatible total inflows: " << totalYearInflows
<< " with initial: " << initialLevel_ << " and final: " << finalLevel_
<< " reservoir levels.";
errorCollector_.Collect(areaName_, msg);
return false;
}
return true;
Expand All @@ -143,10 +148,12 @@ bool FinalLevelValidator::isBetweenRuleCurves() const

if (finalLevel_ < lowLevelLastDay || finalLevel_ > highLevelLastDay)
{
logs.error() << "Year: " << year_ + 1 << ". Area: " << areaName_
<< ". Specifed final reservoir level: " << finalLevel_
<< " is incompatible with reservoir level rule curve [" << lowLevelLastDay
<< " , " << highLevelLastDay << "]";
std::ostringstream msg;
msg << "Year: " << year_ + 1 << ". Area: " << areaName_
<< ". Specifed final reservoir level: " << finalLevel_
<< " is incompatible with reservoir level rule curve [" << lowLevelLastDay << " , "
<< highLevelLastDay << "]";
errorCollector_.Collect(areaName_, msg);
return false;
}
return true;
Expand Down
17 changes: 5 additions & 12 deletions src/solver/simulation/include/antares/solver/simulation/solver.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -971,17 +971,11 @@ void ISimulation<ImplementationType>::loopThroughYears(uint firstYear,
}
for (auto year: batch.yearsIndices)
{
if (!hydroInputsChecker.Execute(year))
{
throw FatalError("Hydro input validation has failed!");
}
hydroInputsChecker.Execute(year);
}
}
hydroInputsChecker.CheckForFatalErrors();

if (hydroInputsChecker.StopExecution())
{
throw FatalError("Hydro input validation has failed!");
}
logs.info() << " Starting the simulation";

// Loop over sets of parallel years to run the simulation
Expand All @@ -1004,10 +998,9 @@ void ISimulation<ImplementationType>::loopThroughYears(uint firstYear,
for (auto y: batch.yearsIndices)
{
// for each year not handled earlier
if (!hydroInputsChecker.Execute(y) || hydroInputsChecker.StopExecution())
{
throw FatalError("Hydro input validation has failed!");
}
hydroInputsChecker.Execute(y);
hydroInputsChecker.CheckForFatalErrors();

bool performCalculations = batch.isYearPerformed[y];
unsigned int numSpace = 999999;
if (performCalculations)
Expand Down

0 comments on commit 5624194

Please sign in to comment.