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

Link TS generation : splitting into multiple files #2171

Merged
merged 31 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f557ada
Remove TS links from solver : extract link names from study.
guilpier-code Jun 11, 2024
0c9ff69
Remove TS links from solver : extract study general parameters relate…
guilpier-code Jun 11, 2024
75476f2
Remove TS links from solver : extract properties from files "properti…
guilpier-code Jun 12, 2024
efa2b35
Remove TS links from solver : adding the reading of prepro TS
guilpier-code Jun 12, 2024
825ca9b
Remove TS links from solver : actually generating TS for links
guilpier-code Jun 13, 2024
a2b3bed
Remove TS links from solver : remove now useless code
guilpier-code Jun 13, 2024
040e83c
Remove TS links from solver : remove more code + correcting reading e…
guilpier-code Jun 13, 2024
3ea7537
Remove TS links from solver : useless code removal
guilpier-code Jun 13, 2024
1999d0c
[skip ci] Remove TS links from solver : code removal
guilpier-code Jun 13, 2024
1862e31
Remove TS links from solver : review corrections + more simplifications
guilpier-code Jun 14, 2024
e284102
Remove TS links from solver : simplifying the data load error handling
guilpier-code Jun 14, 2024
b671e22
Eliminate need for index in loop
flomnes Jun 14, 2024
c844e03
Split TS generation into files : move options handling to separate so…
guilpier-code Jun 14, 2024
fcca483
Split TS generation into files : start moving code for links TS to ne…
guilpier-code Jun 14, 2024
2a1fa53
Split TS generation into files : move code related to TS for links fr…
guilpier-code Jun 14, 2024
46cf379
Split TS generation into files : removing useless header include and …
guilpier-code Jun 17, 2024
8d03e1e
Split TS generation into files : simplifying a loop
guilpier-code Jun 24, 2024
c60b905
Split TS generation into files : move header files to the right place
guilpier-code Jun 25, 2024
ebdef47
Split TS generation into files : taking code review into account
guilpier-code Jun 25, 2024
b2cc58e
Split TS generation into files : remove code duplication about time
guilpier-code Jun 25, 2024
2710aa4
Split TS generation into files : renaming
guilpier-code Jun 25, 2024
f842001
Split TS generation into files : simplifying and renaming function wr…
guilpier-code Jun 25, 2024
82d9bb0
Split TS generation into files : correction for a regression on CI
guilpier-code Jun 26, 2024
4961ceb
TS : generator : make it work for thermal (#2200)
guilpier-code Jun 28, 2024
76a83f9
Merge remote-tracking branch 'remotes/origin/develop' into fix/split-…
guilpier-code Jun 28, 2024
2abab72
Split TS generation into files : correction after merge
guilpier-code Jun 28, 2024
7c094b2
Merge remote-tracking branch 'github/develop' into fix/split-ts-gener…
flomnes Jun 28, 2024
c133e88
TS generator : refactor thermal part (#2202)
guilpier-code Jul 5, 2024
78b0e68
Merge remote-tracking branch 'github/develop' into fix/split-ts-gener…
flomnes Jul 9, 2024
fa17f6e
Formatting
flomnes Jul 9, 2024
e7e7270
Update src/libs/antares/utils/utils.cpp
flomnes Jul 9, 2024
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
6 changes: 2 additions & 4 deletions src/libs/antares/utils/include/antares/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ namespace Antares
*/
template<class StringT>
void TransformNameIntoID(const AnyString& name, StringT& out);

std::string transformNameIntoID(const std::string& name);

std::string FormattedTime(const std::string& format);

/*!
** \brief Beautify a name, for renaming an area for example
*/
Expand All @@ -51,11 +52,8 @@ std::vector<std::pair<std::string, std::string>> splitStringIntoPairs(const std:

namespace Utils
{

bool isZero(double d);

double round(double d, unsigned precision);

} // namespace Utils
} // namespace Antares

Expand Down
14 changes: 14 additions & 0 deletions src/libs/antares/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ void BeautifyName(std::string& out, const std::string& oldname)
out = yuniOut.c_str();
}

std::string FormattedTime(const std::string& format)
{
using namespace std::chrono;
auto time = system_clock::to_time_t(system_clock::now());
std::tm local_time = *std::localtime(&time);

char time_buffer[256];
std::strftime(time_buffer, sizeof(time_buffer), format.c_str(), &local_time);

std::string currentTime = time_buffer;

return currentTime;
flomnes marked this conversation as resolved.
Show resolved Hide resolved
}

std::vector<std::pair<std::string, std::string>> splitStringIntoPairs(const std::string& s,
char delimiter1,
char delimiter2)
Expand Down
16 changes: 1 addition & 15 deletions src/solver/application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,20 +429,6 @@ void Application::runSimulationInAdequacyMode()
observer);
}

static std::string timeToString()
{
using namespace std::chrono;
auto time = system_clock::to_time_t(system_clock::now());
std::tm local_time = *std::localtime(&time);

char time_buffer[256];
std::strftime(time_buffer, sizeof(time_buffer), "%Y%m%d-%H%M%S", &local_time);

std::string currentTime = time_buffer;

return currentTime;
}

void Application::resetLogFilename() const
{
fs::path logfile = fs::path(pSettings.studyFolder.c_str()) / "logs";
Expand All @@ -454,7 +440,7 @@ void Application::resetLogFilename() const
}

logfile /= "solver-"; // append the filename
logfile += timeToString() + ".log"; // complete filename with timestamp and extension
logfile += FormattedTime("%Y%m%d-%H%M%S") + ".log"; // complete filename with timestamp and extension
a-zakir marked this conversation as resolved.
Show resolved Hide resolved

// Assigning the log filename
logs.logfile(logfile.string());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@


#include "hydro-final-reservoir-level-functions.h"
namespace fs = std::filesystem;

namespace Antares::Solver::Simulation
{
Expand Down Expand Up @@ -490,11 +491,9 @@ void ISimulation<ImplementationType>::regenerateTimeSeries(uint year)
if (refreshTSonCurrentYear)
{
auto clusters = getAllClustersToGen(study.areas, pData.haveToRefreshTSThermal);
#define SEP Yuni::IO::Separator
const std::string savePath = std::string("ts-generator") + SEP + "thermal" + SEP + "mc-"
+ std::to_string(year);
#undef SEP
generateThermalTimeSeries(study, clusters, pResultWriter, savePath);
fs::path savePath = fs::path(study.folderOutput.to<std::string>()) / "ts-generator"
/ "thermal" / "mc-" / std::to_string(year);
generateThermalTimeSeries(study, clusters, savePath.string());

// apply the spinning if we generated some in memory clusters
for (auto* cluster: clusters)
Expand Down
106 changes: 52 additions & 54 deletions src/solver/ts-generator/availability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "antares/study/simulation.h"

#define SEP Yuni::IO::Separator
namespace fs = std::filesystem;

constexpr double FAILURE_RATE_EQ_1 = 0.999;

Expand Down Expand Up @@ -70,13 +71,13 @@ AvailabilityTSGeneratorData::AvailabilityTSGeneratorData(LinkTSgenerationParams&

namespace
{
class GeneratorTempData final
class AvailabilityTSgenerator final
{
public:
explicit GeneratorTempData(Data::Study&, unsigned, MersenneTwister&);
explicit GeneratorTempData(bool, unsigned, MersenneTwister&);
explicit AvailabilityTSgenerator(Data::Study&, unsigned, MersenneTwister&);
explicit AvailabilityTSgenerator(bool, unsigned, MersenneTwister&);

void generateTS(AvailabilityTSGeneratorData&) const;
void run(AvailabilityTSGeneratorData&) const;

private:
bool derated;
Expand All @@ -101,26 +102,30 @@ class GeneratorTempData final
const T& duration) const;
};

GeneratorTempData::GeneratorTempData(Data::Study& study, unsigned nbOfSeriesToGen, MersenneTwister& rndGenerator):
AvailabilityTSgenerator::AvailabilityTSgenerator(Data::Study& study,
unsigned nbOfSeriesToGen,
MersenneTwister& rndGenerator):
derated(study.parameters.derated),
nbOfSeriesToGen_(nbOfSeriesToGen),
rndgenerator(rndGenerator)
{
}

GeneratorTempData::GeneratorTempData(bool derated, unsigned int nbOfSeriesToGen, MersenneTwister& rndGenerator):
AvailabilityTSgenerator::AvailabilityTSgenerator(bool derated,
unsigned int nbOfSeriesToGen,
MersenneTwister& rndGenerator):
derated(derated),
nbOfSeriesToGen_(nbOfSeriesToGen),
rndgenerator(rndGenerator)
{
}

template<class T>
void GeneratorTempData::prepareIndispoFromLaw(Data::StatisticalLaw law,
double volatility,
std::array<double, 366>& A,
std::array<double, 366>& B,
const T& duration) const
void AvailabilityTSgenerator::prepareIndispoFromLaw(Data::StatisticalLaw law,
double volatility,
std::array<double, 366>& A,
std::array<double, 366>& B,
const T& duration) const
{
switch (law)
{
Expand Down Expand Up @@ -161,11 +166,11 @@ void GeneratorTempData::prepareIndispoFromLaw(Data::StatisticalLaw law,
}
}

int GeneratorTempData::durationGenerator(Data::StatisticalLaw law,
int expec,
double volat,
double a,
double b) const
int AvailabilityTSgenerator::durationGenerator(Data::StatisticalLaw law,
int expec,
double volat,
double a,
double b) const
{
if (volat == 0 || expec == 1)
{
Expand Down Expand Up @@ -193,7 +198,7 @@ int GeneratorTempData::durationGenerator(Data::StatisticalLaw law,
return 0;
}

void GeneratorTempData::generateTS(AvailabilityTSGeneratorData& tsGenerationData) const
void AvailabilityTSgenerator::run(AvailabilityTSGeneratorData& tsGenerationData) const
{
assert(tsGenerationData.prepro);

Expand Down Expand Up @@ -608,23 +613,8 @@ std::vector<Data::ThermalCluster*> getAllClustersToGen(const Data::AreaList& are
return clusters;
}

void writeResultsToDisk(const Data::Study& study,
Solver::IResultWriter& writer,
const Matrix<>& series,
const std::string& savePath)
{
if (study.parameters.noOutput)
{
return;
}

std::string buffer;
series.saveToBuffer(buffer, 0);
writer.addEntryFromBuffer(savePath, buffer);
}

void writeResultsToDisk(const Matrix<>& series,
const std::filesystem::path savePath)
void writeTStoDisk(const Matrix<>& series,
const std::filesystem::path savePath)
{
std::string buffer;
series.saveToBuffer(buffer, 0);
Expand All @@ -639,29 +629,37 @@ void writeResultsToDisk(const Matrix<>& series,

bool generateThermalTimeSeries(Data::Study& study,
const std::vector<Data::ThermalCluster*>& clusters,
Solver::IResultWriter& writer,
const std::string& savePath)
{
logs.info();
logs.info() << "Generating the thermal time-series";

bool archive = study.parameters.timeSeriesToArchive & Data::timeSeriesThermal;

auto generator = GeneratorTempData(study,
study.parameters.nbTimeSeriesThermal,
study.runtime->random[Data::seedTsGenThermal]);
auto generator = AvailabilityTSgenerator(study,
study.parameters.nbTimeSeriesThermal,
study.runtime->random[Data::seedTsGenThermal]);

for (auto* cluster: clusters)
{
cluster->series.timeSeries.reset(study.parameters.nbTimeSeriesThermal, HOURS_PER_YEAR);
AvailabilityTSGeneratorData tsGenerationData(cluster);
generator.generateTS(tsGenerationData);
generator.run(tsGenerationData);
}

if (archive) // For compatibilty with in memory thermal TS generation
{
std::string filePath = savePath + SEP + cluster->parentArea->id + SEP + cluster->id()
+ ".txt";
writeResultsToDisk(study, writer, cluster->series.timeSeries, filePath);
}
bool archive = study.parameters.timeSeriesToArchive & Data::timeSeriesThermal;
bool doWeWrite = archive && !study.parameters.noOutput;
if (! doWeWrite)
{
logs.info() << "Study parameters forbid writing thermal TS.";
return true;
}

for (auto* cluster: clusters)
{
auto areaName = cluster->parentArea->id.to<std::string>();
auto clusterName = cluster->id();
auto filePath = fs::path(savePath) / areaName / clusterName += ".txt";

writeTStoDisk(cluster->series.timeSeries, filePath.string());
}

return true;
Expand All @@ -675,9 +673,9 @@ bool generateLinkTimeSeries(std::vector<LinkTSgenerationParams>& links,
logs.info();
logs.info() << "Generation of links time-series";

auto generator = GeneratorTempData(generalParams.derated,
generalParams.nbLinkTStoGenerate,
generalParams.random);
auto generator = AvailabilityTSgenerator(generalParams.derated,
generalParams.nbLinkTStoGenerate,
generalParams.random);
for (auto& link: links)
{
if (! link.hasValidData)
Expand All @@ -696,20 +694,20 @@ bool generateLinkTimeSeries(std::vector<LinkTSgenerationParams>& links,
// DIRECT
AvailabilityTSGeneratorData tsConfigDataDirect(link, ts, link.modulationCapacityDirect, link.namesPair.second);

generator.generateTS(tsConfigDataDirect);
generator.run(tsConfigDataDirect);

std::string filePath = savePath + SEP + link.namesPair.first + SEP + link.namesPair.second
+ "_direct.txt";
writeResultsToDisk(ts.timeSeries, filePath);
writeTStoDisk(ts.timeSeries, filePath);

// INDIRECT
AvailabilityTSGeneratorData tsConfigDataIndirect(link, ts, link.modulationCapacityIndirect, link.namesPair.second);

generator.generateTS(tsConfigDataIndirect);
generator.run(tsConfigDataIndirect);

filePath = savePath + SEP + link.namesPair.first + SEP + link.namesPair.second
+ "_indirect.txt";
writeResultsToDisk(ts.timeSeries, filePath);
writeTStoDisk(ts.timeSeries, filePath);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ bool GenerateTimeSeries(Data::Study& study, uint year, IResultWriter& writer);

bool generateThermalTimeSeries(Data::Study& study,
const std::vector<Data::ThermalCluster*>& clusters,
Solver::IResultWriter& writer,
const std::string& savePath);

bool generateLinkTimeSeries(std::vector<LinkTSgenerationParams>& links,
Expand Down
14 changes: 9 additions & 5 deletions src/tools/ts-generator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
set(SRCS
main.cpp
include/antares/tools/ts-generator/linksTSgenerator.h
include/antares/tools/ts-generator/tsGenerationOptions.h
tsGenerationOptions.cpp
linksTSgenerator.cpp
)

set(execname "antares-ts-generator")
Expand All @@ -13,15 +17,15 @@ INSTALL(EXPORT ${execname}

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

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

import_std_libs(${execname})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

#pragma once

#include "antares/tools/ts-generator/tsGenerationOptions.h"
#include <antares/solver/ts-generator/generator.h>

namespace fs = std::filesystem;

namespace Antares::TSGenerator {

class LinksTSgenerator
{
public:
LinksTSgenerator(Settings&);
void extractData();
bool generate();

private:
LinkPairs extractLinkNamesFromStudy();
LinkPairs extractLinkNamesFromCmdLine(const LinkPairs&);
StudyParamsForLinkTS readGeneralParamsForLinksTS();
void extractLinksSpecificTSparameters();

std::string linksFromCmdLineOptions_;
fs::path studyFolder_;
bool generateTSforAllLinks_ = false;
std::vector<LinkTSgenerationParams> linkList_;
StudyParamsForLinkTS generalParams_;
};

} // End Antares::TSGenerator
Loading
Loading