Skip to content

Commit

Permalink
Remove duplication in simulation run (#2274)
Browse files Browse the repository at this point in the history
This work just started...

**Situation** : 
There are code duplications :
- When running the simulation, functions we use to do it
(**runSimulationInEconomicMode()** and
**runSimulationInAdequacyMode()**) are very much the same, the only
difference is the C++ type of simulation (**economy** vs **adequacy**).
- The way simulation is run from **API** or from classic **Application**
look pretty close as well : see methods **APIInternal::execute()** and
**Application::execute()**.

**Problem** : 
Difficult to remove the first duplication without removing the second
one.

**Purpose** : 
Remove them both.
  • Loading branch information
guilpier-code authored Aug 23, 2024
1 parent f157f0b commit f206d45
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 251 deletions.
79 changes: 29 additions & 50 deletions src/api/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@
*/

#include "API.h"
#include "antares/solver/simulation/economy_mode.h"
#include "antares/solver/simulation/adequacy_mode.h"
#include "antares/solver/misc/options.h"
#include "antares/infoCollection/StudyInfoCollector.h"

#include <SimulationObserver.h>

#include <antares/writer/writer_factory.h>
#include "antares/benchmarking/DurationCollector.h"
#include "antares/exception/LoadingError.hpp"
#include <antares/writer/writer_factory.h>
#include <SimulationObserver.h>
#include "antares/infoCollection/StudyInfoCollector.h"
#include "antares/solver/misc/options.h"
#include "antares/solver/simulation/simulation-run.h"

namespace Antares::API
{
SimulationResults APIInternal::run(const IStudyLoader& study_loader)
{
try {
try
{
study_ = study_loader.load();
} catch (const ::Antares::Error::StudyFolderDoesNotExist& e) {
}
catch (const ::Antares::Error::StudyFolderDoesNotExist& e)
{
Antares::API::Error err{.reason = e.what()};
return {
.simulationPath = "",
.antares_problems = {},
.error = err
};
return {.simulationPath = "", .antares_problems = {}, .error = err};
}
return execute();
}
Expand All @@ -50,7 +50,8 @@ SimulationResults APIInternal::run(const IStudyLoader& study_loader)
* @brief The execute method is used to execute the simulation.
* @return SimulationResults object which contains the results of the simulation.
*
* This method is initialy a copy of Application::execute with some modifications hence the apparent dupllication
* This method is initialy a copy of Application::execute with some modifications hence the apparent
* dupllication
*/
SimulationResults APIInternal::execute() const
{
Expand All @@ -62,8 +63,6 @@ SimulationResults APIInternal::execute() const
return {.simulationPath{}, .antares_problems{}, .error = err};
}

study_->computePThetaInfForThermalClusters();

// Only those two fields are used un simulation
Settings settings;
settings.tsGeneratorsOnly = false;
Expand All @@ -74,43 +73,23 @@ SimulationResults APIInternal::execute() const
auto ioQueueService = std::make_shared<Yuni::Job::QueueService>();
ioQueueService->maximumThreadCount(1);
ioQueueService->start();
auto resultWriter = Solver::resultWriterFactory(
study_->parameters.resultFormat, study_->folderOutput, ioQueueService, durationCollector);
auto resultWriter = Solver::resultWriterFactory(study_->parameters.resultFormat,
study_->folderOutput,
ioQueueService,
durationCollector);
SimulationObserver simulationObserver;
// Run the simulation
switch (study_->runtime.mode)
{
case Data::SimulationMode::Economy:
case Data::SimulationMode::Expansion:
Solver::runSimulationInEconomicMode(*study_,
settings,
durationCollector,
*resultWriter,
optimizationInfo,
simulationObserver);
break;
case Data::SimulationMode::Adequacy:
Solver::runSimulationInAdequacyMode(*study_,
settings,
durationCollector,
*resultWriter,
optimizationInfo,
simulationObserver);
break;
default:
break;
}

optimizationInfo = simulationRun(*study_,
settings,
durationCollector,
*resultWriter,
simulationObserver);

// Importing Time-Series if asked
study_->importTimeseriesIntoInput();

// Stop the display of the progression
study_->progression.stop();
return
{
.simulationPath = study_->folderOutput.c_str(),
.antares_problems = simulationObserver.acquireLps(),
.error{}
};
return {.simulationPath = study_->folderOutput.c_str(),
.antares_problems = simulationObserver.acquireLps(),
.error{}};
}
} // namespace Antares::API
} // namespace Antares::API
53 changes: 8 additions & 45 deletions src/solver/application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
#include "antares/application/application.h"

#include <yuni/datetime/timestamp.h>

#include <antares/antares/fatal-error.h>
#include <antares/application/ScenarioBuilderOwner.h>
#include <antares/benchmarking/timer.h>
Expand All @@ -30,19 +28,17 @@
#include <antares/infoCollection/StudyInfoCollector.h>
#include <antares/logs/hostinfo.h>
#include <antares/resources/resources.h>
#include <antares/study/version.h>
#include <antares/sys/policy.h>
#include <antares/writer/writer_factory.h>
#include "antares/antares/version.h"
#include "antares/config/config.h"
#include "antares/signal-handling/public.h"
#include "antares/solver/misc/system-memory.h"
#include "antares/solver/misc/write-command-line.h"
#include "antares/solver/simulation/adequacy_mode.h"
#include "antares/solver/simulation/economy_mode.h"
#include "antares/solver/simulation/simulation-run.h"
#include "antares/solver/simulation/simulation.h"
#include "antares/solver/simulation/solver.h"
#include "antares/solver/utils/ortools_utils.h"
#include "antares/study/simulation.h"

using namespace Antares::Check;

Expand Down Expand Up @@ -387,23 +383,12 @@ void Application::execute()
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()
Simulation::NullSimulationObserver observer;
pOptimizationInfo = simulationRun(*pStudy,
pSettings,
pDurationCollector,
*resultWriter,
observer);

// Importing Time-Series if asked
pStudy->importTimeseriesIntoInput();
Expand All @@ -412,28 +397,6 @@ void Application::execute()
pStudy->progression.stop();
}

void Application::runSimulationInEconomicMode()
{
Simulation::NullSimulationObserver observer;
Solver::runSimulationInEconomicMode(*pStudy,
pSettings,
pDurationCollector,
*resultWriter,
pOptimizationInfo,
observer);
}

void Application::runSimulationInAdequacyMode()
{
Simulation::NullSimulationObserver observer;
Solver::runSimulationInAdequacyMode(*pStudy,
pSettings,
pDurationCollector,
*resultWriter,
pOptimizationInfo,
observer);
}

void Application::resetLogFilename() const
{
fs::path logfile = fs::path(pSettings.studyFolder.c_str()) / "logs";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <antares/writer/i_writer.h>
#include "antares/infoCollection/StudyInfoCollector.h"
#include "antares/solver/misc/options.h"
#include "antares/solver/simulation/ISimulationObserver.h"

namespace Antares::Solver
{
Expand Down Expand Up @@ -92,9 +93,6 @@ class Application final: public Yuni::IEventObserver<Application, Yuni::Policy::
*/
void readDataForTheStudy(Antares::Data::StudyLoadOptions& options);

void runSimulationInAdequacyMode();
void runSimulationInEconomicMode();

void onLogMessage(int level, const std::string& message);

//! The settings given from the command line
Expand Down Expand Up @@ -132,4 +130,5 @@ class Application final: public Yuni::IEventObserver<Application, Yuni::Policy::
void postParametersChecks() const;

}; // class Application

} // namespace Antares::Solver
6 changes: 2 additions & 4 deletions src/solver/simulation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ set(SRC_SIMULATION
include/antares/solver/simulation/solver.hxx
include/antares/solver/simulation/solver.data.h
solver.data.cpp
include/antares/solver/simulation/simulation-run.h
simulation-run.cpp
include/antares/solver/simulation/common-eco-adq.h
common-eco-adq.cpp
common-hydro-remix.cpp
Expand All @@ -45,10 +47,6 @@ set(SRC_SIMULATION
TimeSeriesNumbersWriter.cpp
include/antares/solver/simulation/BindingConstraintsTimeSeriesNumbersWriter.h
include/antares/solver/simulation/ISimulationObserver.h
economy_mode.cpp
adequacy_mode.cpp
include/antares/solver/simulation/economy_mode.h
include/antares/solver/simulation/adequacy_mode.h
)
source_group("simulation" FILES ${SRC_SIMULATION})

Expand Down
50 changes: 0 additions & 50 deletions src/solver/simulation/adequacy_mode.cpp

This file was deleted.

50 changes: 0 additions & 50 deletions src/solver/simulation/economy_mode.cpp

This file was deleted.

This file was deleted.

Loading

0 comments on commit f206d45

Please sign in to comment.