Skip to content

Commit

Permalink
Refactor link series (#1721)
Browse files Browse the repository at this point in the history
Use generic class TimeSeries for directCapacities and indirectCapacities
  • Loading branch information
payetvin authored Nov 20, 2023
1 parent 1fa900b commit 8fa44ec
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 71 deletions.
25 changes: 13 additions & 12 deletions src/libs/antares/series/include/antares/series/series.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@

namespace Antares::Data
{

/*!
** \class TimeSeries
** \brief This class is used to represent the generic time series
**
** The goal is to handle indexing with the time series numbers: getCoefficient()
** and also providing a wrapper for all the Matrix<> functions such as resize()
*/
class TimeSeries
{
/*!
** \brief This class is used to represent the generic time series
**
** The goal is to handle indexing with the time series numbers: getCoefficient()
** and also providing a wrapper for all the Matrix<> functions such as resize()
*/

public:
using numbers = Matrix<uint32_t>;
using TS = Matrix<double>;
Expand All @@ -50,16 +49,13 @@ class TimeSeries
** \brief Load series from a file
**
** \param path path of the file
** \param dataBuffer yuni dependency to use loadFromCSV
** \param average used to average timeseries
** \return A non-zero value if the operation succeeded, 0 otherwise
*/
bool loadFromFile(const std::string& path,
Matrix<>::BufferType dataBuffer,
const bool average);
/*!
** \brief Save time series to a file
** \ingroup windseries
**
** \param areaID The ID of the area associated to the data series
** \param folder The target folder
Expand All @@ -70,17 +66,22 @@ class TimeSeries
const std::string& folder,
const std::string& prefix) const;

int saveToFile(const std::string& filename, bool saveEvenIfAllZero) const;

double getCoefficient(uint32_t year, uint32_t timestep) const;
const double* getColumn(uint32_t year) const;
uint32_t getSeriesIndex(uint32_t year) const;

/// \brief overload operator to return a column
/// Unlike getColumn() it uses direct indexing and not timeseriesNumbers
double* operator[](uint32_t index);

void reset();
void reset(uint32_t width, uint32_t height);
void unloadFromMemory() const;
void roundAllEntries();
void resize(uint32_t timeSeriesCount, uint32_t timestepCount);
void fill(double value);
void averageTimeseries();

bool forceReload(bool reload = false) const;
Expand All @@ -90,7 +91,7 @@ class TimeSeries
TS timeSeries;
numbers& timeseriesNumbers;

static const double emptyColumn[HOURS_PER_YEAR];
static const std::vector<double> emptyColumn; ///< used in getColumn if timeSeries empty
};

} // namespace Antares::Data
Expand Down
15 changes: 12 additions & 3 deletions src/libs/antares/series/series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ using namespace Yuni;
namespace Antares::Data
{

const double TimeSeries::emptyColumn[] = {0};
const std::vector<double> TimeSeries::emptyColumn(HOURS_PER_YEAR);

TimeSeries::TimeSeries(numbers& tsNumbers) : timeseriesNumbers(tsNumbers)
{}

bool TimeSeries::loadFromFile(const std::string& path,
Matrix<>::BufferType dataBuffer,
const bool average)
{
bool ret = true;
Matrix<>::BufferType dataBuffer;
ret = timeSeries.loadFromCSVFile(path, 1, HOURS_PER_YEAR, &dataBuffer) && ret;

if (average)
Expand All @@ -67,6 +67,10 @@ int TimeSeries::saveToFolder(const AreaName& areaID,
return timeSeries.saveToCSVFile(buffer, 0);
}

int TimeSeries::saveToFile(const std::string& filename, bool saveEvenIfAllZero) const
{
return timeSeries.saveToCSVFile(filename, 6, false, saveEvenIfAllZero);
}

double TimeSeries::getCoefficient(uint32_t year, uint32_t timestep) const
{
Expand All @@ -78,7 +82,7 @@ double TimeSeries::getCoefficient(uint32_t year, uint32_t timestep) const
const double* TimeSeries::getColumn(uint32_t year) const
{
if (timeSeries.width == 0)
return emptyColumn;
return emptyColumn.data();
return timeSeries[getSeriesIndex(year)];
}

Expand Down Expand Up @@ -112,6 +116,11 @@ void TimeSeries::resize(uint32_t timeSeriesCount, uint32_t timestepCount)
timeSeries.resize(timeSeriesCount, timestepCount);
}

void TimeSeries::fill(double value)
{
timeSeries.fill(value);
}

void TimeSeries::roundAllEntries()
{
timeSeries.roundAllEntries();
Expand Down
36 changes: 19 additions & 17 deletions src/libs/antares/study/area/links.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ AreaLink::AreaLink() :
from(nullptr),
with(nullptr),
parameters(fhlMax, HOURS_PER_YEAR),
directCapacities(1, HOURS_PER_YEAR),
indirectCapacities(1, HOURS_PER_YEAR),
directCapacities(timeseriesNumbers),
indirectCapacities(timeseriesNumbers),
useLoopFlow(false),
usePST(false),
useHurdlesCost(false),
Expand All @@ -80,6 +80,8 @@ AreaLink::AreaLink() :
style(stPlain),
linkWidth(1)
{
directCapacities.reset();
indirectCapacities.reset();
}

AreaLink::~AreaLink()
Expand Down Expand Up @@ -131,11 +133,11 @@ bool AreaLink::linkLoadTimeSeries_for_version_820_and_later(const AnyString& fol

// Read link's direct capacities time series
filename.clear() << capacitiesFolder << SEP << with->id << "_direct.txt";
success = directCapacities.loadFromCSVFile(filename, 1, HOURS_PER_YEAR) && success;
success = directCapacities.loadFromFile(filename, false) && success;

// Read link's indirect capacities time series
filename.clear() << capacitiesFolder << SEP << with->id << "_indirect.txt";
success = indirectCapacities.loadFromCSVFile(filename, 1, HOURS_PER_YEAR) && success;
success = indirectCapacities.loadFromFile(filename, false) && success;

return success;
}
Expand Down Expand Up @@ -210,8 +212,8 @@ void AreaLink::detach()
void AreaLink::resetToDefaultValues()
{
parameters.reset(fhlMax, HOURS_PER_YEAR, true);
directCapacities.reset(1, HOURS_PER_YEAR, true);
indirectCapacities.reset(1, HOURS_PER_YEAR, true);
directCapacities.reset();
indirectCapacities.reset();

for (uint i = 0; i != HOURS_PER_YEAR; ++i)
{
Expand Down Expand Up @@ -262,7 +264,7 @@ void AreaLink::reverse()
indirectCapacities.forceReload(true);

// invert NTC values
directCapacities.swap(indirectCapacities);
directCapacities.timeSeries.swap(indirectCapacities.timeSeries);

directCapacities.markAsModified();
indirectCapacities.markAsModified();
Expand Down Expand Up @@ -482,8 +484,8 @@ bool AreaLinksLoadFromFolder(Study& study, AreaList* l, Area* area, const AnyStr
if (study.usedByTheSolver)
{
// Short names for link's properties
const uint nbDirectTS = link.directCapacities.width;
const uint nbIndirectTS = link.indirectCapacities.width;
const uint nbDirectTS = link.directCapacities.timeSeries.width;
const uint nbIndirectTS = link.indirectCapacities.timeSeries.width;
if (nbDirectTS != nbIndirectTS)
{
logLinkDataCheckErrorDirectIndirect(
Expand All @@ -499,8 +501,8 @@ bool AreaLinksLoadFromFolder(Study& study, AreaList* l, Area* area, const AnyStr

for (uint indexTS = 0; indexTS < nbDirectTS; ++indexTS)
{
auto& directCapacities = link.directCapacities[indexTS];
auto& indirectCapacities = link.indirectCapacities[indexTS];
const double* directCapacities = link.directCapacities[indexTS];
const double* indirectCapacities = link.indirectCapacities[indexTS];

// Checks on direct capacities
for (int h = 0; h < HOURS_PER_YEAR; h++)
Expand Down Expand Up @@ -590,8 +592,8 @@ bool AreaLinksLoadFromFolder(Study& study, AreaList* l, Area* area, const AnyStr
case Data::LocalTransmissionCapacities::null:
{
// Ignore transmission capacities
link.directCapacities.zero();
link.indirectCapacities.zero();
link.directCapacities.timeSeries.zero();
link.indirectCapacities.timeSeries.zero();
break;
}
case Data::LocalTransmissionCapacities::infinite:
Expand Down Expand Up @@ -636,12 +638,12 @@ bool saveAreaLinksTimeSeriesToFolder(const Area* area, const char* const folder)

// Save direct capacities time series
filename.clear() << capacitiesFolder << SEP << link.with->id << "_direct.txt";
success = link.directCapacities.saveToCSVFile(filename, 6, false, true) && success;
success = link.directCapacities.saveToFile(filename, true) && success;

// Save indirect capacities time series

filename.clear() << capacitiesFolder << SEP << link.with->id << "_indirect.txt";
success = link.indirectCapacities.saveToCSVFile(filename, 6, false, true) && success;
success = link.indirectCapacities.saveToFile(filename, true) && success;
}

return success;
Expand Down Expand Up @@ -726,8 +728,8 @@ uint64_t AreaLink::memoryUsage() const
{
uint64_t to_return = sizeof(AreaLink);
to_return += parameters.valuesMemoryUsage();
to_return += directCapacities.valuesMemoryUsage();
to_return += indirectCapacities.valuesMemoryUsage();
to_return += directCapacities.memoryUsage();
to_return += indirectCapacities.memoryUsage();

return to_return;
}
Expand Down
5 changes: 3 additions & 2 deletions src/libs/antares/study/area/links.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <yuni/core/string.h>
#include "../fwd.h"
#include <antares/array/matrix.h>
#include <antares/series/series.h>
#include <antares/writer/i_writer.h>
#include <set>

Expand Down Expand Up @@ -144,8 +145,8 @@ class AreaLink final : public Yuni::NonCopyable<AreaLink>
**
*/
Matrix<> parameters;
Matrix<> directCapacities;
Matrix<> indirectCapacities;
TimeSeries directCapacities;
TimeSeries indirectCapacities;

//! Monte-Carlo
Matrix<uint32_t> timeseriesNumbers;
Expand Down
6 changes: 3 additions & 3 deletions src/libs/antares/study/area/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ static bool AreaListLoadFromFolderSingleArea(Study& study,
{
buffer.clear() << study.folderInput << SEP << "load" << SEP << "series" << SEP
<< "load_" << area.id << ".txt";
ret = area.load.series.loadFromFile(buffer.c_str(), study.dataBuffer, averageTs)
ret = area.load.series.loadFromFile(buffer.c_str(), averageTs)
&& ret;
}

Expand All @@ -875,7 +875,7 @@ static bool AreaListLoadFromFolderSingleArea(Study& study,
{
buffer.clear() << study.folderInput << SEP << "solar" << SEP << "series" << SEP
<< "solar_" << area.id << ".txt";
ret = area.solar.series.loadFromFile(buffer.c_str(), study.dataBuffer, averageTs)
ret = area.solar.series.loadFromFile(buffer.c_str(), averageTs)
&& ret;

}
Expand Down Expand Up @@ -920,7 +920,7 @@ static bool AreaListLoadFromFolderSingleArea(Study& study,
{
buffer.clear() << study.folderInput << SEP << "wind" << SEP << "series" << SEP
<< "wind_" << area.id << ".txt";
ret = area.wind.series.loadFromFile(buffer.c_str(), study.dataBuffer, averageTs)
ret = area.wind.series.loadFromFile(buffer.c_str(), averageTs)
&& ret;
}

Expand Down
4 changes: 2 additions & 2 deletions src/libs/antares/study/scenario-builder/applyToMatrix.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ inline bool CheckValidity<Data::AreaLink>(uint value,
{
//Value = index of time series
//Direct Capacities = all time series
//directCapacities.width = Number of time series
return value < data.directCapacities.width;
//directCapacities.timeSeries.width = Number of time series
return value < data.directCapacities.timeSeries.width;
}

template<>
Expand Down
2 changes: 1 addition & 1 deletion src/solver/constraints-builder/cbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ bool CBuilder::checkValidityOfNodalLoopFlow(linkInfo* linkInfo, size_t hour)
{
Data::AreaLink* link = linkInfo->ptr;

for (uint tsIndex = 0; tsIndex < link->indirectCapacities.width; ++tsIndex)
for (uint tsIndex = 0; tsIndex < link->indirectCapacities.timeSeries.width; ++tsIndex)
{
if ((-1.0 * link->indirectCapacities[tsIndex][hour]
> link->parameters[Data::fhlLoopFlow][hour])
Expand Down
4 changes: 2 additions & 2 deletions src/solver/simulation/common-eco-adq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ static void RecalculDesEchangesMoyens(Data::Study& study,
{
auto* link = study.runtime->areaLink[j];
int ret = retrieveAverageNTC(
study, link->directCapacities, link->timeseriesNumbers, avgDirect);
study, link->directCapacities.timeSeries, link->timeseriesNumbers, avgDirect);

ret = retrieveAverageNTC(
study, link->indirectCapacities, link->timeseriesNumbers, avgIndirect)
study, link->indirectCapacities.timeSeries, link->timeseriesNumbers, avgIndirect)
&& ret;
if (!ret)
{
Expand Down
33 changes: 17 additions & 16 deletions src/solver/simulation/sim_calcul_economique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,28 +552,29 @@ void SIM_RenseignementProblemeHebdo(const Study& study,
}
}

int hourInYear = PasDeTempsDebut;
unsigned int year = problem.year;

for (unsigned hourInWeek = 0; hourInWeek < problem.NombreDePasDeTemps; ++hourInWeek, ++hourInYear)
uint linkCount = studyruntime.interconnectionsCount();
for (uint k = 0; k != linkCount; ++k)
{
VALEURS_DE_NTC_ET_RESISTANCES& ntc = problem.ValeursDeNTC[hourInWeek];
int hourInYear = PasDeTempsDebut;
auto& lnk = *(studyruntime.areaLink[k]);
const double* directCapacities = lnk.directCapacities.getColumn(year);
const double* indirectCapacities = lnk.indirectCapacities.getColumn(year);
for (unsigned hourInWeek = 0; hourInWeek < problem.NombreDePasDeTemps; ++hourInWeek, ++hourInYear)
{
uint linkCount = studyruntime.interconnectionsCount();
for (uint k = 0; k != linkCount; ++k)
{
auto& lnk = *(studyruntime.areaLink[k]);
const int tsIndex = (lnk.directCapacities.width != 1) ? lnk.timeseriesNumbers[0][year] : 0;
VALEURS_DE_NTC_ET_RESISTANCES& ntc = problem.ValeursDeNTC[hourInWeek];

assert((uint)hourInYear < lnk.directCapacities.height);
assert((uint)tsIndex < lnk.directCapacities.width);
assert((uint)tsIndex < lnk.indirectCapacities.width);

ntc.ValeurDeNTCOrigineVersExtremite[k] = lnk.directCapacities[tsIndex][hourInYear];
ntc.ValeurDeNTCExtremiteVersOrigine[k] = lnk.indirectCapacities[tsIndex][hourInYear];
ntc.ValeurDeLoopFlowOrigineVersExtremite[k] = lnk.parameters[fhlLoopFlow][hourInYear];
}
ntc.ValeurDeNTCOrigineVersExtremite[k] = directCapacities[hourInYear];
ntc.ValeurDeNTCExtremiteVersOrigine[k] = indirectCapacities[hourInYear];
ntc.ValeurDeLoopFlowOrigineVersExtremite[k] = lnk.parameters[fhlLoopFlow][hourInYear];
}
}

int hourInYear = PasDeTempsDebut;
for (unsigned hourInWeek = 0; hourInWeek < problem.NombreDePasDeTemps; ++hourInWeek, ++hourInYear)
{

preparerBindingConstraint(problem, PasDeTempsDebut,
study.bindingConstraints, study.bindingConstraintsGroups,
weekFirstDay, hourInWeek);
Expand Down
8 changes: 4 additions & 4 deletions src/solver/simulation/timeseries-numbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ class areaLinksTransCapaNumberOfTSretriever : public areaNumberOfTSretriever
for (auto it = area.links.begin(); it != area.links.end(); ++it)
{
const auto& link = *(it->second);
to_return.push_back(link.directCapacities.width);
to_return.push_back(link.indirectCapacities.width);
to_return.push_back(link.directCapacities.timeSeries.width);
to_return.push_back(link.indirectCapacities.timeSeries.width);
}
return to_return;
}
Expand Down Expand Up @@ -702,7 +702,7 @@ void drawAndStoreTSnumbersForNOTintraModal(const array<bool, timeSeriesCount>& i
for (auto it = area.links.begin(); it != area.links.end(); ++it)
{
auto& link = *(it->second);
const uint nbTimeSeries = link.directCapacities.width;
const uint nbTimeSeries = link.directCapacities.timeSeries.width;
if (nbTimeSeries == 1)
{
// Random generator (mersenne-twister) must not be called here
Expand Down Expand Up @@ -866,7 +866,7 @@ static void fixTSNumbersWhenWidthIsOne(Study& study)
[&years](const std::pair<Data::AreaName, Data::AreaLink*>& it) {
auto link = it.second;
fixTSNumbersSingleAreaSingleMode(
link->timeseriesNumbers, link->directCapacities.width, years);
link->timeseriesNumbers, link->directCapacities.timeSeries.width, years);
});
});
study.bindingConstraintsGroups.fixTSNumbersWhenWidthIsOne();
Expand Down
Loading

0 comments on commit 8fa44ec

Please sign in to comment.