Skip to content

Commit

Permalink
Refactor time series (#1677)
Browse files Browse the repository at this point in the history
* [DEV] Add classes TimeSeriesNumbers TimeSeries

* [DEV] Add classes to cpp

* [DEV] getTSNumber

* [DEV] getCoeff getColumn

* [FIX] Windows CI

* [DEV] Use matrix<> instead of vector

* [DEV] Add timeSeriesLoadFromFolder

* [DEV]
removed pointer from windseries
add savetofolder
add reset
add typedef TSNumbers

* [DEV] Wind compiling

* [DEV] remove old wind series files

* [FIX] crash

* [FIX] getSeriesIndex

* [DEV] load series

* [DEV] remove load series files

* [FIX] compile

* [DEV] Comments

* [DEV] revert reset()

* [DEV] solar series

* [DEV] remove solar series files

* [FIX] Tests crashes

* [DEV] Add static member emptyColumn

* [DEV] remove useless if

* Use generic class for load series (#1686)

* [DEV] load series

* [DEV] remove load series files

* [FIX] compile

* [DEV] Comments

* [DEV] revert reset()

* [DEV] Comments

* [DEV] using TScoefficients

* Refactor thermal & renewable series (#1693)

* [DEV] Thermal series start

* [FIX] getCoefficient() arg order

* [DEV] Use Matrix<double> only

* [DEV] Remove class DataSeriesCommon

* [DEV] Add using TS

* [skip ci] Add library series, move files to part/series

* [DEV] Use library series

* [DEV] Replace matrix<double> with TimeSeries::TS

* [DEV] Remove template from class TimeSeriesConfigurer

* [DEV] Clean valueAtTimeStep

* [DEV] Comments and use series.reset()

* Refactor hydro series (#1705)

* [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]>

* [FIX] Compile

* [FIX] Code smells

* [FIX] Code smells

* [DEV] valueAtTimeStep uses year hour now

* Unit tests for timeseries (#1713)

* [DEV] Fix include path

* [DEV] move folder series

* [DEV] Use timestep instead of hour

* [FIX] several comments for class series

* [FIX] more comments

* [FIX] try fix windows CI

---------

Co-authored-by: guilpier-code <[email protected]>
  • Loading branch information
payetvin and guilpier-code authored Oct 26, 2023
1 parent 4e90b33 commit fb8b725
Show file tree
Hide file tree
Showing 84 changed files with 994 additions and 1,706 deletions.
1 change: 1 addition & 0 deletions src/libs/antares/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ add_subdirectory(stdcxx)
add_subdirectory(utils)
add_subdirectory(paths)
add_subdirectory(mersenne-twister)
add_subdirectory(series)

add_library(antares-core
antares.h
Expand Down
5 changes: 3 additions & 2 deletions src/libs/antares/checks/checkLoadedInputData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <antares/exception/LoadingError.hpp>
#include <antares/study/version.h>

#include <antares/series/series.h>
#include <antares/checks/checkLoadedInputData.h>
#include <antares/study/area/area.h>

Expand Down Expand Up @@ -161,7 +162,7 @@ void checkMinStablePower(bool tsGenThermal, const Antares::Data::AreaList& areas
// TS
template<class ExceptionT>
static void checkThermalColumnNumber(const Antares::Data::AreaList& areas,
Matrix<double> Antares::Data::EconomicInputData::*matrix)
Antares::Data::TimeSeries::TS Antares::Data::EconomicInputData::*matrix)
{
ExceptionT exception;
bool error = false;
Expand All @@ -174,7 +175,7 @@ static void checkThermalColumnNumber(const Antares::Data::AreaList& areas,
if (cluster.costgeneration == Antares::Data::setManually)
continue;
const uint otherMatrixWidth = (cluster.ecoInput.*matrix).width;
uint tsWidth = cluster.series->timeSeries.width;
uint tsWidth = cluster.series.timeSeries.width;
if (otherMatrixWidth != 1 && otherMatrixWidth != tsWidth)
{
logs.warning() << "Area: " << area.name << ". Cluster name: " << cluster.name()
Expand Down
15 changes: 15 additions & 0 deletions src/libs/antares/series/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
add_library(series)
add_library(Antares::series ALIAS series)

set(SRC_STUDY_PART_SERIES
include/antares/series/series.h
series.cpp
)
target_sources(series PUBLIC ${SRC_STUDY_PART_SERIES})

target_link_libraries(series PUBLIC Antares::array)

target_include_directories(series
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
96 changes: 96 additions & 0 deletions src/libs/antares/series/include/antares/series/series.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
** Copyright 2007-2023 RTE
** Authors: Antares_Simulator Team
**
** This file is part of Antares_Simulator.
**
** Antares_Simulator is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** There are special exceptions to the terms and conditions of the
** license as they are applied to this software. View the full text of
** the exceptions in file COPYING.txt in the directory of this software
** distribution
**
** Antares_Simulator is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Antares_Simulator. If not, see <http://www.gnu.org/licenses/>.
**
** SPDX-License-Identifier: licenceRef-GPL3_WITH_RTE-Exceptions
*/
#ifndef __ANTARES_LIBS_STUDY_PARTS_COMMON_TIMESERIES_H__
#define __ANTARES_LIBS_STUDY_PARTS_COMMON_TIMESERIES_H__

#include <antares/array/matrix.h>

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()
*/

public:
using numbers = Matrix<uint32_t>;
using TS = Matrix<double>;

explicit TimeSeries(numbers& tsNumbers);
/*!
** \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
** \param prefix the prefix for the filename
** \return A non-zero value if the operation succeeded, 0 otherwise
*/
int saveToFolder(const AreaName& areaID,
const std::string& folder,
const std::string& prefix) const;

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

double* operator[](uint32_t index);

void reset();
void unloadFromMemory() const;
void roundAllEntries();
void resize(uint32_t timeSeriesCount, uint32_t timestepCount);
void averageTimeseries();

bool forceReload(bool reload = false) const;
void markAsModified() const;
uint64_t memoryUsage() const;

TS timeSeries;
numbers& timeseriesNumbers;

static const double emptyColumn[HOURS_PER_YEAR];
};

} // namespace Antares::Data
#endif /* __ANTARES_LIBS_STUDY_PARTS_COMMON_TIMESERIES_H__ */
140 changes: 140 additions & 0 deletions src/libs/antares/series/series.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
** Copyright 2007-2023 RTE
** Authors: Antares_Simulator Team
**
** This file is part of Antares_Simulator.
**
** Antares_Simulator is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** There are special exceptions to the terms and conditions of the
** license as they are applied to this software. View the full text of
** the exceptions in file COPYING.txt in the directory of this software
** distribution
**
** Antares_Simulator is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Antares_Simulator. If not, see <http://www.gnu.org/licenses/>.
**
** SPDX-License-Identifier: licenceRef-GPL3_WITH_RTE-Exceptions
*/

#include <yuni/yuni.h>
#include <yuni/io/file.h>
#include <yuni/io/directory.h>
#include "antares/series/series.h"

using namespace Yuni;

#define SEP IO::Separator


namespace Antares::Data
{

const double TimeSeries::emptyColumn[] = {0};

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

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

if (average)
timeSeries.averageTimeseries();

timeseriesNumbers.clear();

return ret;
}

int TimeSeries::saveToFolder(const AreaName& areaID,
const std::string& folder,
const std::string& prefix) const
{
Clob buffer;
buffer.clear() << folder << SEP << prefix << areaID << ".txt";
return timeSeries.saveToCSVFile(buffer, 0);
}


double TimeSeries::getCoefficient(uint32_t year, uint32_t timestep) const
{
if (timeSeries.width == 0)
return 0;
return timeSeries[getSeriesIndex(year)][timestep];
}

const double* TimeSeries::getColumn(uint32_t year) const
{
if (timeSeries.width == 0)
return emptyColumn;
return timeSeries[getSeriesIndex(year)];
}

uint32_t TimeSeries::getSeriesIndex(uint32_t year) const
{
if (timeSeries.width == 1)
return 0;
else
return timeseriesNumbers[0][year];
}

double* TimeSeries::operator[](uint32_t index)
{
if (timeSeries.width <= index)
return nullptr;
return timeSeries[index];
}

void TimeSeries::reset()
{
timeSeries.reset(1, HOURS_PER_YEAR);
}

void TimeSeries::resize(uint32_t timeSeriesCount, uint32_t timestepCount)
{
timeSeries.resize(timeSeriesCount, timestepCount);
}

void TimeSeries::roundAllEntries()
{
timeSeries.roundAllEntries();
}

void TimeSeries::averageTimeseries()
{
timeSeries.averageTimeseries();
}

void TimeSeries::unloadFromMemory() const
{
timeSeries.unloadFromMemory();
}

bool TimeSeries::forceReload(bool reload) const
{
return timeSeries.forceReload(reload);
}

void TimeSeries::markAsModified() const
{
timeSeries.markAsModified();
}

uint64_t TimeSeries::memoryUsage() const
{
return timeSeries.memoryUsage();
}

} // namespace Antares::Data
13 changes: 1 addition & 12 deletions src/libs/antares/study/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ source_group("study\\area\\ui" FILES ${SRC_STUDY_AREA_UI})


set(SRC_STUDY_PART_COMMON
parts/common/series.h
parts/common/series.cpp
parts/common/cluster.cpp
parts/common/cluster.h
parts/common/cluster_list.h
Expand Down Expand Up @@ -114,9 +112,6 @@ source_group("study\\part\\short-term-storage" FILES ${SRC_STUDY_PART_SHORT_TERM
set(SRC_STUDY_PART_WIND
parts/wind/container.h
parts/wind/container.cpp
parts/wind/series.h
parts/wind/series.hxx
parts/wind/series.cpp
parts/wind/prepro.h
parts/wind/prepro.hxx
parts/wind/prepro.cpp
Expand All @@ -127,7 +122,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 Expand Up @@ -202,19 +196,13 @@ set(SRC_STUDY
# Load
parts/load/container.h
parts/load/container.cpp
parts/load/series.h
parts/load/series.hxx
parts/load/series.cpp
parts/load/prepro.cpp
parts/load/prepro.h
parts/load/prepro.hxx

# Solar
parts/solar/container.h
parts/solar/container.cpp
parts/solar/series.h
parts/solar/series.hxx
parts/solar/series.cpp
parts/solar/prepro.cpp
parts/solar/prepro.h
parts/solar/prepro.hxx
Expand Down Expand Up @@ -312,6 +300,7 @@ target_link_libraries(study
Antares::mersenne
Antares::result_writer #study.h
Antares::object
Antares::series
PRIVATE
Antares::exception
Antares::benchmarking
Expand Down
23 changes: 6 additions & 17 deletions src/libs/antares/study/area/area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,8 @@ void Area::createMissingData()

void Area::createMissingTimeSeries()
{
if (!load.series)
load.series = new DataSeriesLoad();
if (!solar.series)
solar.series = new DataSeriesSolar();
if (!wind.series)
wind.series = new DataSeriesWind();
if (!hydro.series)
hydro.series = new DataSeriesHydro();
thermal.list.ensureDataTimeSeries();
renewable.list.ensureDataTimeSeries();
}
void Area::createMissingPrepros()
{
Expand Down Expand Up @@ -269,16 +261,13 @@ void Area::resizeAllTimeseriesNumbers(uint n)
assert(n < 200000); // arbitrary number

// asserts
assert(load.series and "load.series must not be nullptr !");
assert(solar.series and "solar.series must not be nullptr !");
assert(wind.series and "wind.series must not be nullptr !");
assert(hydro.series and "series must not be nullptr !");

if (!n)
{
load.series->timeseriesNumbers.clear();
solar.series->timeseriesNumbers.clear();
wind.series->timeseriesNumbers.clear();
load.series.timeseriesNumbers.clear();
solar.series.timeseriesNumbers.clear();
wind.series.timeseriesNumbers.clear();
hydro.series->timeseriesNumbers.clear();
for (auto& namedLink : links)
{
Expand All @@ -288,9 +277,9 @@ void Area::resizeAllTimeseriesNumbers(uint n)
}
else
{
load.series->timeseriesNumbers.resize(1, n);
solar.series->timeseriesNumbers.resize(1, n);
wind.series->timeseriesNumbers.resize(1, n);
load.series.timeseriesNumbers.resize(1, n);
solar.series.timeseriesNumbers.resize(1, n);
wind.series.timeseriesNumbers.resize(1, n);
hydro.series->timeseriesNumbers.resize(1, n);
for (auto& namedLink : links)
{
Expand Down
Loading

0 comments on commit fb8b725

Please sign in to comment.