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

Run all hydro checks outside simulation's loop #2159

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
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace Antares
class HydroInputsChecker
{
public:
HydroInputsChecker(Antares::Data::Study& study, Solver::IResultWriter& resultWriter);
void Execute();
explicit HydroInputsChecker(Antares::Data::Study& study);
void Execute(uint year);

private:
Data::AreaList& areas_;
Expand All @@ -42,7 +42,6 @@ class HydroInputsChecker
const uint endYear_;
PrepareInflows prepareInflows_;
MinGenerationScaling minGenerationScaling_;
Solver::IResultWriter& resultWriter_;

//! return false if checkGenerationPowerConsistency or checkMinGeneration returns false
bool checkMonthlyMinGeneration(uint year, const Data::Area& area) const;
Expand Down
21 changes: 8 additions & 13 deletions src/solver/hydro/management/HydroInputsChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,27 @@
namespace Antares
{

HydroInputsChecker::HydroInputsChecker(Antares::Data::Study& study,
Solver::IResultWriter& resultWriter):
HydroInputsChecker::HydroInputsChecker(Antares::Data::Study& study):
areas_(study.areas),
parameters_(study.parameters),
calendar_(study.calendar),
simulationMode_(study.runtime->mode),
firstYear_(0),
endYear_(1 + study.runtime->rangeLimits.year[Data::rangeEnd]),
prepareInflows_(study.areas, study.calendar),
minGenerationScaling_(study.areas, study.calendar),
resultWriter_(resultWriter)
minGenerationScaling_(study.areas, study.calendar)
{
}

void HydroInputsChecker::Execute()
void HydroInputsChecker::Execute(uint year)
{
for (auto year = firstYear_; year < endYear_; ++year)
if (parameters_.yearsFilter[year])
{
if (parameters_.yearsFilter[year])
prepareInflows_.Run(year);
minGenerationScaling_.Run(year);
if (!checksOnGenerationPowerBounds(year))
{
prepareInflows_.Run(year);
minGenerationScaling_.Run(year);
if (!checksOnGenerationPowerBounds(year))
{
throw FatalError("hydro inputs checks: invalid minimum generation");
}
throw FatalError("hydro inputs checks: invalid minimum generation");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ class ISimulation: public Impl
/*!
** \brief Regenerate time-series if required for a given year
*/
void regenerateTimeSeries(uint year);
void regenerateTimeSeries(uint year, bool include_hydro = true);

void regenerateHydroTimeSeries(uint year);
/*!
** \brief Builds sets of parallel years
**
Expand Down Expand Up @@ -126,7 +127,7 @@ class ISimulation: public Impl
** Storing these costs to compute std deviation later.
*/
void computeAnnualCostsStatistics(std::vector<Variable::State>& state,
std::vector<setOfParallelYears>::iterator& set_it);
setOfParallelYears& batch);

/*!
** \brief Iterate through all MC years
Expand Down
94 changes: 55 additions & 39 deletions src/solver/simulation/include/antares/solver/simulation/solver.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,18 @@ void ISimulation<ImplementationType>::writeResults(bool synthesis, uint year, ui
}

template<class ImplementationType>
void ISimulation<ImplementationType>::regenerateTimeSeries(uint year)
void ISimulation<ImplementationType>::regenerateHydroTimeSeries(uint year)
{
using namespace TSGenerator;
if (pData.haveToRefreshTSHydro && (year % pData.refreshIntervalHydro == 0))
{
pDurationCollector("tsgen_hydro") << [year, this]
{ GenerateTimeSeries<Data::timeSeriesHydro>(study, year, pResultWriter); };
}
}

template<class ImplementationType>
void ISimulation<ImplementationType>::regenerateTimeSeries(uint year, bool include_hydro)
{
// A preprocessor can be launched for several reasons:
// * The option "Preprocessor" is checked in the interface _and_ year == 0
Expand All @@ -460,10 +471,9 @@ void ISimulation<ImplementationType>::regenerateTimeSeries(uint year)
<< [year, this] { GenerateTimeSeries<Data::timeSeriesWind>(study, year, pResultWriter); };
}
// Hydro
if (pData.haveToRefreshTSHydro && (year % pData.refreshIntervalHydro == 0))
if (include_hydro)
{
pDurationCollector("tsgen_hydro") << [year, this]
{ GenerateTimeSeries<Data::timeSeriesHydro>(study, year, pResultWriter); };
regenerateHydroTimeSeries(year);
}

// Thermal
Expand Down Expand Up @@ -915,23 +925,20 @@ void ISimulation<ImplementationType>::computeRandomNumbers(
}

} // End loop over years
} // End function
} // End function

template<class ImplementationType>
void ISimulation<ImplementationType>::computeAnnualCostsStatistics(
std::vector<Variable::State>& state,
std::vector<setOfParallelYears>::iterator& set_it)
setOfParallelYears& batch)
{
// Loop over years contained in the set
std::vector<unsigned int>::iterator year_it;
for (year_it = set_it->yearsIndices.begin(); year_it != set_it->yearsIndices.end(); ++year_it)
for (auto y: batch.yearsIndices)
{
// Get the index of the year
unsigned int y = *year_it;
if (set_it->isYearPerformed[y])
if (batch.isYearPerformed[y])
{
// Get space number associated to the performed year
uint numSpace = set_it->performedYearToSpace[y];
uint numSpace = batch.performedYearToSpace[y];
const Variable::State& s = state[numSpace];
pAnnualStatistics.systemCost.addCost(s.annualSystemCost);
pAnnualStatistics.criterionCost1.addCost(s.optimalSolutionCost1);
Expand Down Expand Up @@ -994,53 +1001,62 @@ void ISimulation<ImplementationType>::loopThroughYears(uint firstYear,

// Number of threads to perform the jobs waiting in the queue
pQueueService->maximumThreadCount(pNbMaxPerformedYearsInParallel);
HydroInputsChecker hydroInputsChecker(study);

// Loop over sets of parallel years
std::vector<setOfParallelYears>::iterator set_it;
for (set_it = setsOfParallelYears.begin(); set_it != setsOfParallelYears.end(); ++set_it)
// Loop over sets of parallel years to check hydro inputs
for (auto batch: setsOfParallelYears)
{
// 1 - We may want to regenerate the time-series this year.
// This is the case when the preprocessors are enabled from the
// interface and/or the refresh is enabled.
if (set_it->regenerateTS)
if (batch.regenerateTS)
{
regenerateTimeSeries(set_it->yearForTSgeneration);
regenerateHydroTimeSeries(batch.yearForTSgeneration);
}

for (auto year: batch.yearsIndices)
{
hydroInputsChecker.Execute(year);
}
}

// Loop over sets of parallel years to run the simulation
for (auto batch: setsOfParallelYears)
{
// 1 - We may want to regenerate the time-series this year.
// This is the case when the preprocessors are enabled from the
// interface and/or the refresh is enabled.
if (batch.regenerateTS)
{
regenerateTimeSeries(batch.yearForTSgeneration, false);
}
computeRandomNumbers(randomForParallelYears,
set_it->yearsIndices,
set_it->isYearPerformed,
batch.yearsIndices,
batch.isYearPerformed,
randomHydroGenerator);
HydroInputsChecker(study, pResultWriter).Execute();

std::vector<unsigned int>::iterator year_it;

bool yearPerformed = false;
Concurrency::FutureSet results;
for (year_it = set_it->yearsIndices.begin(); year_it != set_it->yearsIndices.end();
++year_it)
for (auto y: batch.yearsIndices)
{
// Get the index of the year
unsigned int y = *year_it;

bool performCalculations = set_it->isYearPerformed[y];
bool performCalculations = batch.isYearPerformed[y];
unsigned int numSpace = 999999;
if (performCalculations)
{
yearPerformed = true;
numSpace = set_it->performedYearToSpace[y];
numSpace = batch.performedYearToSpace[y];
}

// If the year has not to be rerun, we skip the computation of the year.
// Note that, when we enter for the first time in the "for" loop, all years of the set
// have to be rerun (meaning : they must be run once). if(!set_it->yearFailed[y])
// have to be rerun (meaning : they must be run once). if(!batch.yearFailed[y])
// continue;

auto task = std::make_shared<yearJob<ImplementationType>>(
this,
y,
set_it->yearFailed,
set_it->isFirstPerformedYearOfASet,
batch.yearFailed,
batch.isFirstPerformedYearOfASet,
pFirstSetParallelWithAPerformedYearWasRun,
numSpace,
randomForParallelYears,
Expand All @@ -1053,7 +1069,7 @@ void ISimulation<ImplementationType>::loopThroughYears(uint firstYear,
results.add(Concurrency::AddTask(*pQueueService, task));
} // End loop over years of the current set of parallel years

logPerformedYearsInAset(*set_it);
logPerformedYearsInAset(batch);

pQueueService->start();

Expand All @@ -1069,7 +1085,7 @@ void ISimulation<ImplementationType>::loopThroughYears(uint firstYear,
}

// On regarde si au moins une année du lot n'a pas trouvé de solution
for (auto& [year, failed]: set_it->yearFailed)
for (auto& [year, failed]: batch.yearFailed)
{
// Si une année du lot d'années n'a pas trouvé de solution, on arrête tout
if (failed)
Expand All @@ -1081,17 +1097,17 @@ void ISimulation<ImplementationType>::loopThroughYears(uint firstYear,
}
// Computing the summary : adding the contribution of MC years
// previously computed in parallel
ImplementationType::variables.computeSummary(set_it->spaceToPerformedYear,
set_it->nbPerformedYears);
ImplementationType::variables.computeSummary(batch.spaceToPerformedYear,
batch.nbPerformedYears);

// Computing summary of spatial aggregations
ImplementationType::variables.computeSpatialAggregatesSummary(ImplementationType::variables,
set_it->spaceToPerformedYear,
set_it->nbPerformedYears);
batch.spaceToPerformedYear,
batch.nbPerformedYears);

// Computes statistics on annual (system and solution) costs, to be printed in output into
// separate files
computeAnnualCostsStatistics(state, set_it);
computeAnnualCostsStatistics(state, batch);

// Set to zero the random numbers of all parallel years
randomForParallelYears.reset();
Expand Down