forked from AntaresSimulatorTeam/Antares_Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.cpp
491 lines (399 loc) · 14.4 KB
/
application.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include "application.h"
#include <antares/sys/policy.h>
#include <antares/resources/resources.h>
#include <antares/logs/hostinfo.h>
#include <antares/fatal-error.h>
#include <antares/benchmarking/timer.h>
#include <antares/exception/InitializationError.hpp>
#include <antares/exception/LoadingError.hpp>
#include <antares/checks/checkLoadedInputData.h>
#include <antares/version.h>
#include <antares/writer/writer_factory.h>
#include "signal-handling/public.h"
#include "misc/system-memory.h"
#include "misc/write-command-line.h"
#include "utils/ortools_utils.h"
#include "../config.h"
#include <antares/infoCollection/StudyInfoCollector.h>
#include <yuni/io/io.h>
#include <yuni/datetime/timestamp.h>
#include <yuni/core/process/rename.h>
#include <algorithm>
using namespace Antares::Check;
namespace
{
void printSolvers()
{
std::cout << "Available solvers :" << std::endl;
for (const auto& solver : getAvailableOrtoolsSolverName())
{
std::cout << solver << std::endl;
}
}
} // namespace
namespace Antares::Solver
{
Application::Application()
{
resetProcessPriority();
}
void Application::prepare(int argc, char* argv[])
{
pArgc = argc;
pArgv = argv;
// Load the local policy settings
LocalPolicy::Open();
LocalPolicy::CheckRootPrefix(argv[0]);
Resources::Initialize(argc, argv);
// Options
Data::StudyLoadOptions options;
options.usedByTheSolver = true;
// Bind pSettings / options members to command line arguments
// Something like bind("--foo", options.foo);
// So that option.foo will be assigned <value>
// if the user provides --foo <value>.
// CAUTION
// The parser contains references to members of pSettings and options,
// don't de-allocate these.
auto parser = CreateParser(pSettings, options);
// Parse the command line arguments
switch (auto ret = parser->operator()(argc, argv); ret)
{
using namespace Yuni::GetOpt;
case ReturnCode::error:
throw Error::CommandLineArguments(parser->errors());
break;
case ReturnCode::help:
// End the program
pStudy = nullptr;
return;
default:
break;
}
if (options.displayVersion)
{
PrintVersionToStdCout();
pStudy = nullptr;
return;
}
if (options.listSolvers)
{
printSolvers();
pStudy = nullptr;
return;
}
// Perform some checks
checkAndCorrectSettingsAndOptions(pSettings, options);
pSettings.checkAndSetStudyFolder(options.studyFolder);
checkStudyVersion(pSettings.studyFolder);
// Determine the log filename to use for this simulation
resetLogFilename();
// Starting !
#ifdef GIT_SHA1_SHORT_STRING
logs.checkpoint() << "Antares Solver v" << ANTARES_VERSION_STR << " (" << GIT_SHA1_SHORT_STRING
<< ")";
#else
logs.checkpoint() << "Antares Solver v" << ANTARES_VERSION_STR;
#endif
WriteHostInfoIntoLogs();
// Write command-line options into logs
// Incidentally, it also seems to contain the full path to the executable
logs.info();
WriteCommandLineIntoLogs(argc, argv);
logs.info() << " :: log filename: " << logs.logfile();
// Temporary use a callback to count the number of errors and warnings
logs.callback.connect(this, &Application::onLogMessage);
// Allocate a study
pStudy = std::make_shared<Antares::Data::Study>(true /* for the solver */);
// Setting global variables for backward compatibility
pParameters = &(pStudy->parameters);
// Loading the study
readDataForTheStudy(options);
// Some more checks require the existence of pParameters, hence of a study.
// Their execution is delayed up to this point.
checkOrtoolsUsage(
pParameters->unitCommitment.ucMode, pParameters->ortoolsUsed, pParameters->ortoolsSolver);
checkSimplexRangeHydroPricing(pParameters->simplexOptimizationRange,
pParameters->hydroPricing.hpMode);
checkSimplexRangeUnitCommitmentMode(pParameters->simplexOptimizationRange,
pParameters->unitCommitment.ucMode);
checkSimplexRangeHydroHeuristic(pParameters->simplexOptimizationRange, pStudy->areas);
if (pParameters->adqPatchParams.enabled)
pParameters->adqPatchParams.checkAdqPatchParams(pParameters->mode,
pStudy->areas,
pParameters->include.hurdleCosts);
bool tsGenThermal
= (0 != (pParameters->timeSeriesToGenerate & Antares::Data::TimeSeriesType::timeSeriesThermal));
checkMinStablePower(tsGenThermal, pStudy->areas);
checkFuelCostColumnNumber(pStudy->areas);
checkCO2CostColumnNumber(pStudy->areas);
// Start the progress meter
pStudy->initializeProgressMeter(pSettings.tsGeneratorsOnly);
if (pSettings.noOutput)
pSettings.displayProgression = false;
if (pSettings.displayProgression)
{
auto& filename = pStudy->buffer;
filename.clear() << "about-the-study" << Yuni::IO::Separator << "map";
pStudy->progression.saveToFile(filename, *resultWriter);
pStudy->progression.start();
}
else
logs.info() << " The progression is disabled";
}
void Application::initializeRandomNumberGenerators()
{
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)
{
case Yuni::Logs::Verbosity::Warning::level:
++pWarningCount;
break;
case Yuni::Logs::Verbosity::Error::level:
++pErrorCount;
break;
case Yuni::Logs::Verbosity::Fatal::level:
++pErrorCount;
break;
default:
break;
}
}
void Application::execute()
{
// pStudy == nullptr e.g when the -h flag is given
if (!pStudy)
return;
processCaption(Yuni::String() << "antares: running \"" << pStudy->header.caption << "\"");
SystemMemoryLogger memoryReport;
memoryReport.interval(1000 * 60 * 5); // 5 minutes
memoryReport.start();
pStudy->computePThetaInfForThermalClusters();
// Run the simulation
switch (pStudy->runtime->mode)
{
case Data::SimulationMode::Economy:
case Data::SimulationMode::Expansion:
runSimulationInEconomicMode();
break;
case Data::SimulationMode::Adequacy:
runSimulationInAdequacyMode();
break;
default:
break;
}
// TODO : make an interface class for ISimulation, check writer & queue before
// runSimulationIn<XXX>Mode()
// Importing Time-Series if asked
pStudy->importTimeseriesIntoInput();
// Stop the display of the progression
pStudy->progression.stop();
}
void Application::resetLogFilename() const
{
// Assigning the log file
Yuni::String logfile;
logfile << pSettings.studyFolder << Yuni::IO::Separator << "logs";
// Making sure that the folder
if (!Yuni::IO::Directory::Create(logfile))
{
throw FatalError(std::string("Impossible to create the log folder at ") + logfile.c_str() + ". Aborting now.");
}
// Date/time
logfile << Yuni::IO::Separator << "solver-";
Yuni::DateTime::TimestampToString(logfile, "%Y%m%d-%H%M%S", 0, false);
logfile << ".log";
// Assigning the log filename
logs.logfile(logfile);
if (!logs.logfileIsOpened())
{
throw FatalError(std::string("Impossible to create the log file at ") + logfile.c_str());
}
}
void Application::processCaption(const Yuni::String& caption)
{
pArgv = Yuni::Process::Rename(pArgc, pArgv, caption);
}
void Application::prepareWriter(Antares::Data::Study& study,
Benchmarking::IDurationCollector& duration_collector)
{
ioQueueService = std::make_shared<Yuni::Job::QueueService>();
ioQueueService->maximumThreadCount(1);
ioQueueService->start();
resultWriter = resultWriterFactory(
study.parameters.resultFormat, study.folderOutput, ioQueueService, duration_collector);
}
void Application::readDataForTheStudy(Data::StudyLoadOptions& options)
{
processCaption(Yuni::String() << "antares: loading \"" << pSettings.studyFolder << "\"");
auto& study = *pStudy;
// Name of the simulation
if (!pSettings.simulationName.empty())
study.simulationComments.name = pSettings.simulationName;
// Force some options
options.prepareOutput = !pSettings.noOutput;
options.ignoreConstraints = pSettings.ignoreConstraints;
options.loadOnlyNeeded = true;
// Load the study from a folder
Benchmarking::Timer timer;
std::exception_ptr loadingException;
try
{
if (study.loadFromFolder(pSettings.studyFolder, options) && !study.gotFatalError)
{
logs.info() << "The study is loaded.";
logs.info() << LOG_UI_DISPLAY_MESSAGES_OFF;
}
timer.stop();
pDurationCollector.addDuration("study_loading", timer.get_duration());
if (study.gotFatalError)
throw Error::ReadingStudy();
if (study.areas.empty())
{
throw Error::NoAreas();
}
// no output ?
study.parameters.noOutput = pSettings.noOutput;
if (pSettings.forceZipOutput)
{
pParameters->resultFormat = Antares::Data::zipArchive;
}
}
catch (...)
{
loadingException = std::current_exception();
}
// This settings can only be enabled from the solver
// Prepare the output for the study
study.prepareOutput();
// Initialize the result writer
prepareWriter(study, pDurationCollector);
// Some checks may have failed, but we need a writer to copy the logs
// to the output directory
// So we wait until we have initialized the writer to rethrow
if (loadingException)
{
std::rethrow_exception(loadingException);
}
Antares::Solver::initializeSignalHandlers(resultWriter);
// Save about-the-study files (comments, notes, etc.)
study.saveAboutTheStudy(*resultWriter);
// Name of the simulation (again, if the value has been overwritten)
if (!pSettings.simulationName.empty())
study.simulationComments.name = pSettings.simulationName;
// Removing all callbacks, which are no longer needed
logs.callback.clear();
logs.info();
if (pSettings.noOutput)
{
logs.info() << "The output has been disabled.";
logs.info();
}
// Errors
if (pErrorCount || pWarningCount || study.gotFatalError)
{
if (pErrorCount || !pSettings.ignoreWarningsErrors)
{
// The loading of the study produces warnings and/or errors
// As the option '--force' is not given, we can not continue
LogDisplayErrorInfos(pErrorCount, pWarningCount, "The simulation must stop.");
throw FatalError("The simulation must stop.");
}
else
{
LogDisplayErrorInfos(
0,
pWarningCount,
"As requested, the warnings can be ignored and the simulation will continue",
false /* not an error */);
// Actually importing the log file is useless here.
// However, since we have warnings/errors, it allows to have a piece of
// log when the unexpected happens.
if (!study.parameters.noOutput)
study.importLogsToOutputFolder(*resultWriter);
// empty line
logs.info();
}
}
// Checking for filename length limits
if (!pSettings.noOutput)
{
if (!study.checkForFilenameLimits(true))
throw Error::InvalidFileName();
// comments
{
study.buffer.clear() << "simulation-comments.txt";
if (!pSettings.commentFile.empty())
{
resultWriter->addEntryFromFile(study.buffer.c_str(), pSettings.commentFile.c_str());
pSettings.commentFile.clear();
pSettings.commentFile.shrink();
}
}
}
// Runtime data dedicated for the solver
if (!study.initializeRuntimeInfos())
throw Error::RuntimeInfoInitialization();
// Apply transformations needed by the solver only (and not the interface for example)
study.performTransformationsBeforeLaunchingSimulation();
// alloc global vectors
SIM_AllocationTableaux(study);
// Random-numbers generators
initializeRandomNumberGenerators();
}
void Application::writeExectutionInfo()
{
if (!pStudy)
return;
// Last missing duration to get : measure of total simulation duration
pTotalTimer.stop();
pDurationCollector.addDuration("total", pTotalTimer.get_duration());
// If no writer is available, we can't write
if (!resultWriter)
return;
// Info collectors : they retrieve data from study and simulation
Benchmarking::StudyInfoCollector study_info_collector(*pStudy);
Benchmarking::SimulationInfoCollector simulation_info_collector(pOptimizationInfo);
// Fill file content with data retrieved by collectors
Benchmarking::FileContent file_content;
pDurationCollector.toFileContent(file_content);
study_info_collector.toFileContent(file_content);
simulation_info_collector.toFileContent(file_content);
// Flush previous info into a record file
const std::string exec_info_path = "execution_info.ini";
std::string content = file_content.saveToBufferAsIni();
resultWriter->addEntryFromBuffer(exec_info_path, content);
}
Application::~Application()
{
// Destroy all remaining bouns (callbacks)
destroyBoundEvents();
// Release all allocated data
if (!(!pStudy))
{
logs.info() << LOG_UI_SOLVER_DONE;
// Copy the log file if a result writer is available
if (!pStudy->parameters.noOutput && resultWriter)
{
pStudy->importLogsToOutputFolder(*resultWriter);
}
// release all reference to the current study held by this class
pStudy->clear();
pStudy = nullptr;
LocalPolicy::Close();
}
}
} // namespace Antares::Solver