Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Jul 1, 2024
1 parent 18afdbb commit 8885fa7
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,33 @@

namespace Antares
{
using AreaErrorMessages = std::vector<std::string>;

class HydroErrorsCollector
{
public:
class AreaReference
{
public:
AreaReference(HydroErrorsCollector* collector, const std::string& name);
friend void operator<<(const AreaReference& ref, const std::string& msg);

~AreaReference()
{
// std::cout << areasErrorMap_ << std::endl;
}

private:
AreaErrorMessages& areasErrorMessages_;
};

AreaReference operator()(const std::string& name);
HydroErrorsCollector() = default;
void Collect(const std::string& area_name, const std::string& message);
void Collect(const std::string& message);
void CheckForFatalErrors() const;
template<class T>
std::ostream& operator<<(const T& obj);
void CheckForErrors() const;

private:
// for log
constexpr unsigned int TRHESHOLD_NUMBER_OF_ERRORS_FOR_ONE_AREA = 10;
std::map<std::string, unsigned int> area_errors_counter_;
bool errors_limit_reached_ = false;
bool stop_ = false;
// std::vector<std::string> fatal_errors_;
std::map<std::string, AreaErrorMessages> areasErrorMap_;
};

template<class T>
HydroErrorsCollector& HydroErrorsCollector::operator<<(const T& obj)
{
logs.error() << obj;
return *this;
}
} // namespace Antares
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class HydroInputsChecker
public:
explicit HydroInputsChecker(Antares::Data::Study& study);
void Execute(uint year);
void CheckForFatalErrors() const;
void CheckForErrors() const;
void CheckFinalReservoirLevelsConfiguration(uint year);

private:
Expand Down
39 changes: 22 additions & 17 deletions src/solver/hydro/management/HydroErrorsCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,37 @@

namespace Antares
{
void HydroErrorsCollector::Collect(const std::string& area_name, const std::string& message)
{
logs.error() << "In Area " << area_name << " " << message;
auto error_count = area_errors_counter_[area_name]++;
errors_limit_reached_ = error_count > TRHESHOLD_NUMBER_OF_ERRORS_FOR_ONE_AREA;
stop_ = true;

if (errors_limit_reached_)
void HydroErrorsCollector::CheckForErrors() const
{
if (!aresErrorMap_.empty())
{
logs.error() << "Hydro validation has failed !";
logs.error() << error_count << " errors found in Area " << area_name;
for (const auto& [area_name, area_msgs]: areasErrorMap_)
{
logs.error() << "In Area " << area_name;
for (const auto& msg: area_msgs)
{
logs.error() << msg;
}
}
throw FatalError("Hydro validation has failed !");
}
}

void HydroErrorsCollector::Collect(const std::string& message)
HydroErrorsCollector::AreaReference::AreaReference(HydroErrorsCollector* collector,
const std::string& name):
areasErrorMap_(collector->areasErrorMap_[name])
{
logs.error() << message;
stop_ = true;
}

void HydroErrorsCollector::CheckForFatalErrors() const
HydroErrorsCollector::AreaReference HydroErrorsCollector::operator()(const std::string& name)
{
if (stop_)
{
throw FatalError("Hydro validation has failed !");
}
return AreaReference(this, name);
}

void operator<<(const HydroErrorsCollector::AreaReference& ref, const std::string& msg)
{
// TODO what to do with empty msg?
ref.areasErrorMessages_.push_back(msg);
}
} // namespace Antares
57 changes: 23 additions & 34 deletions src/solver/hydro/management/HydroInputsChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ void HydroInputsChecker::Execute(uint year)
minGenerationScaling_.Run(year);
if (!checksOnGenerationPowerBounds(year))
{
std::ostringstream msg;
msg << "hydro inputs checks: invalid minimum generation in year " << year;
errorCollector_.Collect(msg.str());
logs.error() << "hydro inputs checks: invalid minimum generation in year " << year;
}
if (parameters_.useCustomScenario)
{
Expand Down Expand Up @@ -120,12 +118,10 @@ bool HydroInputsChecker::checkWeeklyMinGeneration(uint year, const Data::Area& a
}
if (totalWeekMingen > totalWeekInflows)
{
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.str());
errorCollector_(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.";
return false;
}
}
Expand All @@ -137,13 +133,11 @@ bool HydroInputsChecker::checkYearlyMinGeneration(uint year, const Data::Area& a
const auto& data = area.hydro.managementData.at(year);
if (data.totalYearMingen > data.totalYearInflows)
{
std::ostringstream msg;

// Yearly minimum generation <= Yearly inflows
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.str());
errorCollector_(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.";
return false;
}
return true;
Expand All @@ -158,13 +152,12 @@ bool HydroInputsChecker::checkMonthlyMinGeneration(uint year, const Data::Area&
// Monthly minimum generation <= Monthly inflows for each month
if (data.totalMonthMingen[realmonth] > data.totalMonthInflows[realmonth])
{
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.str());
errorCollector_(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.";
return false;
}
}
Expand All @@ -191,13 +184,11 @@ bool HydroInputsChecker::checkGenerationPowerConsistency(uint year)

if (max < min)
{
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.str());
errorCollector_(area.name)
<< "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.";
ret = false;
return;
}
Expand Down Expand Up @@ -231,9 +222,7 @@ void HydroInputsChecker::CheckFinalReservoirLevelsConfiguration(uint year)
errorCollector_);
if (!validator.check())
{
std::ostringstream msg;
msg << "hydro final level : infeasibility";
errorCollector_.Collect(area.name, msg.str());
logs.error() << "hydro final level : infeasibility";
}
if (validator.finalLevelFineForUse())
{
Expand All @@ -242,9 +231,9 @@ void HydroInputsChecker::CheckFinalReservoirLevelsConfiguration(uint year)
});
} // End function CheckFinalReservoirLevelsConfiguration

void HydroInputsChecker::CheckForFatalErrors() const
void HydroInputsChecker::CheckForErrors() const
{
errorCollector_.CheckForFatalErrors();
errorCollector_.CheckForErrors();
}

} // namespace Antares
28 changes: 11 additions & 17 deletions src/solver/hydro/management/finalLevelValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ bool FinalLevelValidator::hydroAllocationStartMatchesSimulation() const
return true;
}

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.str());
errorCollector_(areaName_) << "Year " << year_ + 1 << ": "
<< "Hydro allocation must start on the 1st simulation month and "
<< "simulation last a whole year";

return false;
}
Expand All @@ -131,12 +129,10 @@ bool FinalLevelValidator::isFinalLevelReachable() const

if ((finalLevel_ - initialLevel_) * reservoirCapacity > totalYearInflows)
{
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.str());
errorCollector_(areaName_)
<< "Year: " << year_ + 1 << " Incompatible total inflows: " << totalYearInflows
<< " with initial: " << initialLevel_ << " and final: " << finalLevel_
<< " reservoir levels.";
return false;
}
return true;
Expand All @@ -162,12 +158,10 @@ bool FinalLevelValidator::isBetweenRuleCurves() const

if (finalLevel_ < lowLevelLastDay || finalLevel_ > 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.str());
errorCollector_(areaName_)
<< "Year: " << year_ + 1 << " Specifed final reservoir level: " << finalLevel_
<< " is incompatible with reservoir level rule curve [" << lowLevelLastDay << " , "
<< highLevelLastDay << "]";
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ void ISimulation<ImplementationType>::loopThroughYears(uint firstYear,
hydroInputsChecker.Execute(year);
}
}
hydroInputsChecker.CheckForFatalErrors();
hydroInputsChecker.CheckForErrors();

logs.info() << " Starting the simulation";

Expand All @@ -998,7 +998,7 @@ void ISimulation<ImplementationType>::loopThroughYears(uint firstYear,
{
// for each year not handled earlier
hydroInputsChecker.Execute(y);
hydroInputsChecker.CheckForFatalErrors();
hydroInputsChecker.CheckForErrors();

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

0 comments on commit 8885fa7

Please sign in to comment.