Skip to content

Commit

Permalink
Tool for time series generation (#1967)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Maréchal <[email protected]>
Co-authored-by: Florian OMNES <[email protected]>
Co-authored-by: Guillaume PIERRE <[email protected]>
Co-authored-by: OMNES Florian <[email protected]>
  • Loading branch information
5 people authored Mar 1, 2024
1 parent 0a41c41 commit a7670bb
Show file tree
Hide file tree
Showing 19 changed files with 318 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/centos7.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jobs:
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_TESTING=ON \
-DBUILD_not_system=OFF \
-DBUILD_TOOLS=OFF \
-DBUILD_TOOLS=ON \
-DBUILD_UI=OFF \
-DCMAKE_PREFIX_PATH=${{ env.ORTOOLSDIR }}/install \
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/oracle8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
cmake -B _build -S src \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_TESTING=ON \
-DBUILD_TOOLS=OFF \
-DBUILD_TOOLS=ON \
-DBUILD_UI=OFF \
-DCMAKE_PREFIX_PATH=${{ env.ORTOOLS_DIR }}/install
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class StudyRuntimeInfos
*/
bool loadFromStudy(Study& study);

void initializeRandomNumberGenerators(const Parameters& parameters);

public:
//! The number of years to process
uint nbYears;
Expand Down
15 changes: 15 additions & 0 deletions src/libs/antares/study/runtime/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,19 @@ void StudyRuntimeInfos::checkThermalTSGeneration(Study& study)
});
}

void StudyRuntimeInfos::initializeRandomNumberGenerators(const Parameters& parameters)
{
logs.info() << "Initializing random number generators...";
for (uint i = 0; i != Data::seedMax; ++i)
{
#ifndef NDEBUG
logs.debug() << " random number generator: " << Data::SeedToCString((Data::SeedIndex)i)
<< ", seed: " << parameters.seed[i];
#endif
random[i].reset(parameters.seed[i]);
}
}

bool StudyRuntimeInfos::loadFromStudy(Study& study)
{
auto& gd = study.parameters;
Expand Down Expand Up @@ -309,6 +322,8 @@ bool StudyRuntimeInfos::loadFromStudy(Study& study)
if (not gd.geographicTrimming)
disableAllFilters(study);

initializeRandomNumberGenerators(study.parameters);

logs.info();
logs.info() << "Summary";
logs.info() << " areas: " << study.areas.size();
Expand Down
3 changes: 2 additions & 1 deletion src/libs/antares/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_library(Antares::utils ALIAS ${PROJ})
target_link_libraries(${PROJ}
PRIVATE
yuni-static-core
Antares::logs
)

target_include_directories(${PROJ}
Expand All @@ -21,4 +22,4 @@ target_include_directories(${PROJ}

install(DIRECTORY include/antares
DESTINATION "include"
)
)
7 changes: 7 additions & 0 deletions src/libs/antares/utils/include/antares/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include <yuni/yuni.h>
#include <yuni/core/string.h>

#include <string>
#include <vector>

namespace Antares
{
/*!
Expand All @@ -40,6 +43,10 @@ void TransformNameIntoID(const AnyString& name, StringT& out);
void BeautifyName(YString& out, AnyString oldname);
void BeautifyName(std::string& out, const std::string& oldname);

std::vector<std::pair<std::string, std::string>> splitStringIntoPairs(const std::string& s,
char delimiter1,
char delimiter2);

} // namespace Antares

#include "utils.hxx"
Expand Down
28 changes: 28 additions & 0 deletions src/libs/antares/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
*/

#include "antares/utils/utils.h"
#include <antares/logs/logs.h>


#include <sstream>

using namespace Yuni;

Expand Down Expand Up @@ -79,4 +83,28 @@ void BeautifyName(std::string& out, const std::string& oldname)
out = yuniOut.c_str();
}

std::vector<std::pair<std::string, std::string>> splitStringIntoPairs(const std::string& s,
char delimiter1,
char delimiter2)
{
std::vector<std::pair<std::string, std::string>> pairs;
std::stringstream ss(s);
std::string token;

while (std::getline(ss, token, delimiter1))
{
size_t pos = token.find(delimiter2);
if (pos != std::string::npos)
{
std::string begin = token.substr(0, pos);
std::string end = token.substr(pos + 1);
pairs.push_back({begin, end});
}
else
logs.warning() << "Error while parsing: " << token;
}

return pairs;
}

} // namespace Antares
19 changes: 0 additions & 19 deletions src/solver/application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,6 @@ void Application::prepare(int argc, char* argv[])
logs.info() << " The progression is disabled";
}

void Application::initializeRandomNumberGenerators() const
{
logs.info() << "Initializing random number generators...";
const auto& parameters = pStudy->parameters;
auto& runtime = *pStudy->runtime;

for (uint i = 0; i != Data::seedMax; ++i)
{
#ifndef NDEBUG
logs.debug() << " random number generator: " << Data::SeedToCString((Data::SeedIndex)i)
<< ", seed: " << parameters.seed[i];
#endif
runtime.random[i].reset(parameters.seed[i]);
}
}

void Application::onLogMessage(int level, const Yuni::String& /*message*/)
{
switch (level)
Expand Down Expand Up @@ -432,9 +416,6 @@ void Application::readDataForTheStudy(Data::StudyLoadOptions& options)

// alloc global vectors
SIM_AllocationTableaux(study);

// Random-numbers generators
initializeRandomNumberGenerators();
}
void Application::writeComment(Data::Study& study)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ class Application final : public Yuni::IEventObserver<Application, Yuni::Policy:
void runSimulationInAdequacyMode();
void runSimulationInEconomicMode();

void initializeRandomNumberGenerators() const;

void onLogMessage(int level, const YString& message);

void processCaption(const Yuni::String& caption);
Expand Down
4 changes: 3 additions & 1 deletion src/solver/misc/include/antares/solver/misc/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ class Settings
int ignoreWarningsErrors = 0;
//! Ignore constraints
bool ignoreConstraints = false;
//!

//! Run the TS generator only
bool tsGeneratorsOnly = false;

//! True to disable the writing in the output folder
bool noOutput = false;
//! Progression
Expand Down
2 changes: 1 addition & 1 deletion src/solver/misc/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ std::unique_ptr<GetOpt::Parser> CreateParser(Settings& settings,
settings.simulationName, 'n', "name", "Set the name of the new simulation to VALUE");
// --generators-only
parser->addFlag(
settings.tsGeneratorsOnly, 'g', "generators-only", "Run the time-series generators only");
settings.tsGeneratorsOnly, 'g', "generators-only", "Run the time-series generators only");

// --comment-file
parser->add(settings.commentFile,
Expand Down
15 changes: 10 additions & 5 deletions src/solver/simulation/include/antares/solver/simulation/solver.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,20 @@ void ISimulation<ImplementationType>::regenerateTimeSeries(uint year)
timer.stop();
pDurationCollector.addDuration("tsgen_hydro", timer.get_duration());
}

// Thermal
const bool refreshTSonCurrentYear = (year % pData.refreshIntervalThermal == 0);
Benchmarking::Timer timer;

if (refreshTSonCurrentYear)
{
Benchmarking::Timer timer;
GenerateThermalTimeSeries(
study, year, pData.haveToRefreshTSThermal, refreshTSonCurrentYear, pResultWriter);
timer.stop();
pDurationCollector.addDuration("tsgen_thermal", timer.get_duration());
auto clusters = getAllClustersToGen(study.areas, pData.haveToRefreshTSThermal);

GenerateThermalTimeSeries(study, clusters, year, pResultWriter);
}

timer.stop();
pDurationCollector.addDuration("tsgen_thermal", timer.get_duration());
}

template<class ImplementationType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ template<enum Data::TimeSeriesType T>
bool GenerateTimeSeries(Data::Study& study, uint year, IResultWriter& writer);

bool GenerateThermalTimeSeries(Data::Study& study,
std::vector<Data::ThermalCluster*> clusters,
uint year,
bool globalThermalTSgeneration,
bool refresh,
IResultWriter& writer);
Solver::IResultWriter& writer);

std::vector<Data::ThermalCluster*> getAllClustersToGen(Data::AreaList& areas,
bool globalThermalTSgeneration);
/*!
** \brief Destroy all TS Generators
*/
Expand Down
35 changes: 20 additions & 15 deletions src/solver/ts-generator/thermal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/

#include <sstream>
#include <string>

#include <yuni/yuni.h>
Expand Down Expand Up @@ -596,11 +597,24 @@ void GeneratorTempData::operator()(Data::Area& area, Data::ThermalCluster& clust
}
} // namespace

std::vector<Data::ThermalCluster*> getAllClustersToGen(Data::AreaList& areas,
bool globalThermalTSgeneration)
{
std::vector<Data::ThermalCluster*> clusters;

areas.each([&clusters, &globalThermalTSgeneration](Data::Area& area) {
for (auto cluster : area.thermal.list.all())
if (cluster->doWeGenerateTS(globalThermalTSgeneration))
clusters.push_back(cluster.get());
});

return clusters;
}

bool GenerateThermalTimeSeries(Data::Study& study,
std::vector<Data::ThermalCluster*> clusters,
uint year,
bool globalThermalTSgeneration,
bool refreshTSonCurrentYear,
Antares::Solver::IResultWriter& writer)
Solver::IResultWriter& writer)
{
logs.info();
logs.info() << "Generating the thermal time-series";
Expand All @@ -610,22 +624,13 @@ bool GenerateThermalTimeSeries(Data::Study& study,

generator->currentYear = year;

study.areas.each([&](Data::Area& area) {
for (auto cluster : area.thermal.list.all())
{
if (cluster->doWeGenerateTS(globalThermalTSgeneration) && refreshTSonCurrentYear)
{
(*generator)(area, *cluster);
}
++progression;
}
});
// TODO VP: parallel
for (auto* cluster : clusters)
(*generator)(*cluster->parentArea, *cluster);

delete generator;

return true;
}

} // namespace Antares::TSGenerator


1 change: 1 addition & 0 deletions src/tests/src/solver/simulation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ target_include_directories(tests-ts-numbers
)
target_link_libraries(tests-ts-numbers
PRIVATE
Antares::utils
Boost::unit_test_framework
model_antares
antares-solver-simulation
Expand Down
37 changes: 37 additions & 0 deletions src/tests/src/solver/simulation/tests-ts-numbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <antares/solver/simulation/timeseries-numbers.h>
#include "antares/solver/ts-generator/generator.h"
#include <antares/utils/utils.h>

#include <algorithm> // std::adjacent_find

Expand Down Expand Up @@ -751,3 +752,39 @@ BOOST_AUTO_TEST_CASE(check_all_drawn_ts_numbers_are_bounded_between_0_and_nb_of_
BOOST_CHECK(thermalTsNumber < thermalNumberOfTs);
BOOST_CHECK_LT(binding_constraints_TS_number, binding_constraints_number_of_TS);
}

BOOST_AUTO_TEST_CASE(split_string_ts_cluster_gen)
{
char delimiter1 = ';';
char delimiter2 = '.';

using stringPair = std::pair<std::string, std::string>;
std::vector<stringPair> v;

// only one pair of area cluster
v = splitStringIntoPairs("abc.def", delimiter1, delimiter2);
BOOST_CHECK(v[0] == stringPair("abc", "def"));

// two pairs
v = splitStringIntoPairs("abc.def;ghi.jkl", delimiter1, delimiter2);
BOOST_CHECK(v[0] == stringPair("abc", "def"));
BOOST_CHECK(v[1] == stringPair("ghi", "jkl"));

// first pair isn't valid
v = splitStringIntoPairs("abcdef;ghi.jkl", delimiter1, delimiter2);
BOOST_CHECK(v[0] == stringPair("ghi", "jkl"));

// second pair isn't valid
v = splitStringIntoPairs("abc.def;ghijkl", delimiter1, delimiter2);
BOOST_CHECK(v[0] == stringPair("abc", "def"));

// no semi colon
v = splitStringIntoPairs("abc.def.ghi.jkl", delimiter1, delimiter2);
BOOST_CHECK(v[0] == stringPair("abc", "def.ghi.jkl"));

// no separator
v.clear();
v = splitStringIntoPairs("abcdef", delimiter1, delimiter2);
BOOST_CHECK(v.empty());
}

2 changes: 1 addition & 1 deletion src/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ add_subdirectory(cleaner)
add_subdirectory(yby-aggregator)
add_subdirectory(config)
add_subdirectory(vacuum)

add_subdirectory(kirchhoff-cbuilder)
add_subdirectory(ts-generator)
28 changes: 28 additions & 0 deletions src/tools/ts-generator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
set(SRCS
main.cpp
)

set(execname "antares-ts-generator")
add_executable(${execname} ${SRCS})
install(TARGETS ${execname} EXPORT antares-ts-generator DESTINATION bin)

INSTALL(EXPORT ${execname}
FILE antares-ts-generatorConfig.cmake
DESTINATION cmake
)

target_link_libraries(${execname}
PRIVATE
Antares::utils
antares-solver-ts-generator
Antares::study
Antares::checks
)

target_include_directories(${execname}
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/include"
)

import_std_libs(${execname})
executable_strip(${execname})
Loading

0 comments on commit a7670bb

Please sign in to comment.