Skip to content

Commit

Permalink
Refactor hydro series (#1705)
Browse files Browse the repository at this point in the history
* [DEV] Removed hydro/series.hxx

* [DEV] Use a ref for TSNumbers

* [DEV] Use a ref for TSNumbers (#1706)

* [DEV] Use TimeSeries for ror

* [DEV] Modify operator[] behavior

* [DEV] storage

* [CI] Upgrade python to 3.12

* [DEV] mingen

* [DEV] replace tsindex with year in hydro/management

* [DEV] Remove getIndex from hydro series

* [DEV] code smells

* Make hydro ts count a private member (#1712)

* Refactor max power TS checks : internalize count of generation time series (ror, storage, mingen)

We should not be able to change a TS count without resizing the TS.
This was allowed when because this count was a public data member.
So we make it a private data member, and change it only when resizing the associated data.

* Make hydro time series cont a private member

---------

Co-authored-by: guilpier-code <[email protected]>
  • Loading branch information
payetvin and guilpier-code authored Oct 19, 2023
1 parent 9067974 commit 356ef42
Show file tree
Hide file tree
Showing 29 changed files with 190 additions and 190 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/windows-vcpkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ jobs:
ortools-url: ${{env.ORTOOLS_URL}}
ortools-dir: ${{env.ORTOOLS_DIR}}

- name: Setup Python 3.11
- name: Setup Python 3.12
uses: actions/setup-python@v4
with:
architecture: 'x64'
python-version: '3.11'
python-version: '3.12'

- name: Install pip dependencies if necessary
run: pip install -r src/tests/examples/requirements.txt
Expand Down
1 change: 0 additions & 1 deletion src/libs/antares/study/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ set(SRC_STUDY_PART_HYDRO
parts/hydro/container.h
parts/hydro/container.cpp
parts/hydro/series.h
parts/hydro/series.hxx
parts/hydro/series.cpp
parts/hydro/prepro.h
parts/hydro/prepro.cpp
Expand Down
7 changes: 2 additions & 5 deletions src/libs/antares/study/area/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1556,11 +1556,8 @@ void AreaList::removeLoadTimeseries()

void AreaList::removeHydroTimeseries()
{
each([](Data::Area& area) {
area.hydro.series->ror.reset(1, HOURS_PER_YEAR);
area.hydro.series->storage.reset(1, DAYS_PER_YEAR);
area.hydro.series->mingen.reset(1, HOURS_PER_YEAR);
area.hydro.series->count = 1;
each([&](Data::Area& area) {
area.hydro.series->reset();
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/antares/study/area/scratchpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ AreaScratchpad::AreaScratchpad(const StudyRuntimeInfos& rinfos, Area& area)
if (!area.hydro.prepro) // not in prepro mode
{
assert(area.hydro.series);
hydroHasInflows = MatrixTestForAtLeastOnePositiveValue(area.hydro.series->storage);
hydroHasInflows = MatrixTestForAtLeastOnePositiveValue(area.hydro.series->storage.timeSeries);
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion src/libs/antares/study/parts/common/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Cluster::Cluster(Area* parent) :
parentArea(parent),
index(0),
nominalCapacity(0.),
areaWideIndex((uint)-1)
areaWideIndex((uint)-1),
series(tsNumbers)
{
}

Expand Down
2 changes: 2 additions & 0 deletions src/libs/antares/study/parts/common/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ class Cluster
//! Series
TimeSeries series;

TimeSeries::TSNumbers tsNumbers;

/*!
** \brief Modulation matrix
**
Expand Down
92 changes: 59 additions & 33 deletions src/libs/antares/study/parts/hydro/series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,29 @@ namespace Antares
{
namespace Data
{
DataSeriesHydro::DataSeriesHydro() : count(0)
DataSeriesHydro::DataSeriesHydro() :
ror(timeseriesNumbers),
storage(timeseriesNumbers),
mingen(timeseriesNumbers)
{
// Pmin was introduced in v8.6
// The previous behavior was Pmin=0
// For compatibility reasons with existing studies, mingen is set to one column of zeros
// by default
mingen.reset(1, HOURS_PER_YEAR);
mingen.reset();
}

void DataSeriesHydro::copyGenerationTS(DataSeriesHydro& source)
{
ror.timeSeries = source.ror.timeSeries;
storage.timeSeries = source.storage.timeSeries;
mingen.timeSeries = source.mingen.timeSeries;

count = source.count;

source.ror.unloadFromMemory();
source.storage.unloadFromMemory();
source.mingen.unloadFromMemory();
}

bool DataSeriesHydro::saveToFolder(const AreaName& areaID, const AnyString& folder) const
Expand All @@ -61,11 +77,11 @@ bool DataSeriesHydro::saveToFolder(const AreaName& areaID, const AnyString& fold

// Saving data
buffer.clear() << folder << SEP << areaID << SEP << "ror.txt";
ret = ror.saveToCSVFile(buffer, 0) && ret;
ret = ror.timeSeries.saveToCSVFile(buffer, 0) && ret;
buffer.clear() << folder << SEP << areaID << SEP << "mod.txt";
ret = storage.saveToCSVFile(buffer, 0) && ret;
ret = storage.timeSeries.saveToCSVFile(buffer, 0) && ret;
buffer.clear() << folder << SEP << areaID << SEP << "mingen.txt";
ret = mingen.saveToCSVFile(buffer, 0) && ret;
ret = mingen.timeSeries.saveToCSVFile(buffer, 0) && ret;
return ret;
}
return false;
Expand All @@ -78,21 +94,21 @@ bool DataSeriesHydro::loadFromFolder(Study& study, const AreaName& areaID, const

buffer.clear() << folder << SEP << areaID << SEP << "ror." << study.inputExtension;

ret = ror.loadFromCSVFile(buffer, 1, HOURS_PER_YEAR, &study.dataBuffer) && ret;
ret = ror.timeSeries.loadFromCSVFile(buffer, 1, HOURS_PER_YEAR, &study.dataBuffer) && ret;

buffer.clear() << folder << SEP << areaID << SEP << "mod." << study.inputExtension;
ret = storage.loadFromCSVFile(buffer, 1, DAYS_PER_YEAR, &study.dataBuffer) && ret;
ret = storage.timeSeries.loadFromCSVFile(buffer, 1, DAYS_PER_YEAR, &study.dataBuffer) && ret;

// The number of time-series
count = storage.width;
count = storage.timeSeries.width;

if (ror.width > count)
count = ror.width;
if (ror.timeSeries.width > count)
count = ror.timeSeries.width;

if (study.header.version >= 860)
{
buffer.clear() << folder << SEP << areaID << SEP << "mingen." << study.inputExtension;
ret = mingen.loadFromCSVFile(buffer, 1, HOURS_PER_YEAR, &study.dataBuffer) && ret;
ret = mingen.timeSeries.loadFromCSVFile(buffer, 1, HOURS_PER_YEAR, &study.dataBuffer) && ret;
}

if (study.usedByTheSolver)
Expand All @@ -101,15 +117,15 @@ bool DataSeriesHydro::loadFromFolder(Study& study, const AreaName& areaID, const
{
logs.error() << "Hydro: `" << areaID
<< "`: empty matrix detected. Fixing it with default values";
ror.reset(1, HOURS_PER_YEAR);
storage.reset(1, DAYS_PER_YEAR);
mingen.reset(1, HOURS_PER_YEAR);
ror.reset();
storage.reset();
mingen.reset();
}
else
{
if (count > 1 && storage.width != ror.width)
if (count > 1 && storage.timeSeries.width != ror.timeSeries.width)
{
if (ror.width != 1 && storage.width != 1)
if (ror.timeSeries.width != 1 && storage.timeSeries.width != 1)
{
logs.fatal() << "Hydro: `" << areaID
<< "`: The matrices ROR (run-of-the-river) and hydro-storage must "
Expand All @@ -118,19 +134,19 @@ bool DataSeriesHydro::loadFromFolder(Study& study, const AreaName& areaID, const
}
else
{
if (ror.width == 1)
if (ror.timeSeries.width == 1)
{
ror.resizeWithoutDataLost(count, ror.height);
ror.timeSeries.resizeWithoutDataLost(count, ror.timeSeries.height);
for (uint x = 1; x < count; ++x)
ror.pasteToColumn(x, ror[0]);
ror.timeSeries.pasteToColumn(x, ror[0]);
}
else
{
if (storage.width == 1)
if (storage.timeSeries.width == 1)
{
storage.resizeWithoutDataLost(count, storage.height);
storage.timeSeries.resizeWithoutDataLost(count, storage.timeSeries.height);
for (uint x = 1; x < count; ++x)
storage.pasteToColumn(x, storage[0]);
storage.timeSeries.pasteToColumn(x, storage[0]);
}
}
Area* areaToInvalidate = study.areas.find(areaID);
Expand Down Expand Up @@ -165,9 +181,9 @@ bool DataSeriesHydro::loadFromFolder(Study& study, const AreaName& areaID, const

void DataSeriesHydro::checkMinGenTsNumber(Study& study, const AreaName& areaID)
{
if (mingen.width != storage.width)
if (mingen.timeSeries.width != storage.timeSeries.width)
{
if (mingen.width > 1)
if (mingen.timeSeries.width > 1)
{
logs.fatal() << "Hydro: `" << areaID
<< "`: The matrices Minimum Generation must "
Expand All @@ -176,9 +192,9 @@ void DataSeriesHydro::checkMinGenTsNumber(Study& study, const AreaName& areaID)
}
else
{
mingen.resizeWithoutDataLost(count, mingen.height);
mingen.timeSeries.resizeWithoutDataLost(count, mingen.timeSeries.height);
for (uint x = 1; x < count; ++x)
mingen.pasteToColumn(x, mingen[0]);
mingen.timeSeries.pasteToColumn(x, mingen[0]);
Area* areaToInvalidate = study.areas.find(areaID);
if (areaToInvalidate)
{
Expand Down Expand Up @@ -211,20 +227,30 @@ void DataSeriesHydro::markAsModified() const

void DataSeriesHydro::reset()
{
ror.reset(1, HOURS_PER_YEAR);
storage.reset(1, DAYS_PER_YEAR);
mingen.reset(1, HOURS_PER_YEAR);
ror.reset();
storage.reset();
mingen.reset();
count = 1;
}

uint64_t DataSeriesHydro::memoryUsage() const
void DataSeriesHydro::resizeRORandSTORAGE(unsigned int width)
{
return sizeof(double) + ror.memoryUsage() + storage.memoryUsage() + mingen.memoryUsage();
ror.resize(width, HOURS_PER_YEAR);
storage.resize(width, DAYS_PER_YEAR);
count = width;
}

void DataSeriesHydro::resizeGenerationTS(unsigned int w, unsigned int h)
{
ror.resize(w, h);
storage.resize(w, h);
mingen.resize(w, h);
count = w;
}

unsigned int DataSeriesHydro::getIndex(unsigned int year) const
uint64_t DataSeriesHydro::memoryUsage() const
{
return (count != 1) ? timeseriesNumbers[0][year] : 0;
return sizeof(double) + ror.memoryUsage() + storage.memoryUsage() + mingen.memoryUsage();
}

} // namespace Data
Expand Down
31 changes: 18 additions & 13 deletions src/libs/antares/study/parts/hydro/series.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifndef __ANTARES_LIBS_STUDY_PARTS_HYDRO_TIMESERIES_H__
#define __ANTARES_LIBS_STUDY_PARTS_HYDRO_TIMESERIES_H__

#include <include/antares/series/series.h>
#include <antares/array/matrix.h>
#include "../../fwd.h"

Expand All @@ -48,13 +49,18 @@ class DataSeriesHydro
DataSeriesHydro();
//@}

void copyGenerationTS(DataSeriesHydro& source);

//! \name Data
//@{
/*!
** \brief Reset all data, as if it were a new area
*/
void reset();

void resizeRORandSTORAGE(unsigned int width);
void resizeGenerationTS(unsigned int w, unsigned int h);

/*!
** \brief Load all data not already loaded
**
Expand Down Expand Up @@ -105,31 +111,36 @@ class DataSeriesHydro
*/
void checkMinGenTsNumber(Study& s, const AreaName& areaID);

unsigned int getIndex(unsigned int year) const;

public:
/*!
** \brief Run-of-the-river - ROR (MW)
**
** (it was DAYS_PER_YEAR before 3.9)
*/
Matrix<double> ror;
TimeSeries ror;

/*!
** \brief Mod (MW)
**
** Merely a matrix of TimeSeriesCount * 365 values
** This matrix is not used in `adequation` mode.
*/
Matrix<double> storage;
TimeSeries storage;

/*!
** \brief Minimum Generation (MW)
**
** Merely a matrix of TimeSeriesCount * HOURS_PER_YEAR values
*/
Matrix<double> mingen;
TimeSeries mingen;

unsigned int TScount() const { return count; };

/*!
** \brief Monte-Carlo
*/
Matrix<uint32_t> timeseriesNumbers;

/*!
** \brief The number of time-series
Expand All @@ -139,18 +150,12 @@ class DataSeriesHydro
** (for example using `fatal.width` and `mod.width` in the same routine, it might
** indicate that the two values are not strictly equal)
*/
uint count;

/*!
** \brief Monte-Carlo
*/
Matrix<uint32_t> timeseriesNumbers;
private:
uint count = 0;

}; // class DataSeriesHydro

} // namespace Data
} // namespace Antares

#include "series.hxx"

#endif /* __ANTARES_LIBS_STUDY_PARTS_HYDRO_TIMESERIES_H__ */
39 changes: 0 additions & 39 deletions src/libs/antares/study/parts/hydro/series.hxx

This file was deleted.

2 changes: 1 addition & 1 deletion src/libs/antares/study/parts/load/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ using namespace Yuni;

namespace Antares::Data::Load
{
Container::Container() : prepro(nullptr)
Container::Container() : prepro(nullptr), series(tsNumbers)
{
}

Expand Down
2 changes: 2 additions & 0 deletions src/libs/antares/study/parts/load/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class Container final : private Yuni::NonCopyable<Container>
/*! Data for time-series */
TimeSeries series;

TimeSeries::TSNumbers tsNumbers;

}; // class Container

} // namespace Load
Expand Down
Loading

0 comments on commit 356ef42

Please sign in to comment.