Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Jan 6, 2025
2 parents 87d36c6 + d1dd983 commit 2eb45b6
Show file tree
Hide file tree
Showing 36 changed files with 904 additions and 121 deletions.
21 changes: 15 additions & 6 deletions src/api/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Antares::API
{
SimulationResults APIInternal::run(
const IStudyLoader& study_loader,
const std::filesystem::path& output,
const Antares::Solver::Optimization::OptimizationOptions& optOptions)
{
try
Expand All @@ -43,9 +44,9 @@ SimulationResults APIInternal::run(
catch (const ::Antares::Error::StudyFolderDoesNotExist& e)
{
Antares::API::Error err{.reason = e.what()};
return {.simulationPath = "", .antares_problems = {}, .error = err};
return {.antares_problems = {}, .error = err};
}
return execute(optOptions);
return execute(output, optOptions);
}

/**
Expand All @@ -56,14 +57,15 @@ SimulationResults APIInternal::run(
* dupllication
*/
SimulationResults APIInternal::execute(
const std::filesystem::path& output,
const Antares::Solver::Optimization::OptimizationOptions& optOptions) const
{
// study_ == nullptr e.g when the -h flag is given
if (!study_)
{
using namespace std::string_literals;
Antares::API::Error err{.reason = "Couldn't create study"s};
return {.simulationPath{}, .antares_problems{}, .error = err};
return {.antares_problems{}, .error = err};
}

Settings settings;
Expand All @@ -75,10 +77,19 @@ SimulationResults APIInternal::execute(
auto ioQueueService = std::make_shared<Yuni::Job::QueueService>();
ioQueueService->maximumThreadCount(1);
ioQueueService->start();

study_->folderOutput = output;
auto resultWriter = Solver::resultWriterFactory(parameters.resultFormat,
study_->folderOutput,
ioQueueService,
durationCollector);

// In some cases (e.g tests) we don't want to write anything
if (!output.empty())
{
study_->saveAboutTheStudy(*resultWriter);
}

SimulationObserver simulationObserver;

optimizationInfo = simulationRun(*study_,
Expand All @@ -90,8 +101,6 @@ SimulationResults APIInternal::execute(
// Importing Time-Series if asked
study_->importTimeseriesIntoInput();

return {.simulationPath = study_->folderOutput,
.antares_problems = simulationObserver.acquireLps(),
.error{}};
return {.antares_problems = simulationObserver.acquireLps(), .error{}};
}
} // namespace Antares::API
10 changes: 4 additions & 6 deletions src/api/include/antares/api/SimulationResults.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <filesystem>
#include <optional>
#include <string>

#include "antares/solver/lps/LpsFromAntares.h"

namespace Antares::API
Expand All @@ -31,7 +32,8 @@ namespace Antares::API
* @struct Error
* @brief The Error structure is used to represent an error that occurred during the simulation.
*/
struct Error {
struct Error
{
/**
* @brief The reason for the error.
*/
Expand All @@ -45,10 +47,6 @@ struct Error {
*/
struct [[nodiscard("Contains results and potential error")]] SimulationResults
{
/**
* @brief The path to the simulation (output).
*/
std::filesystem::path simulationPath;
/**
* @brief weekly problems
*/
Expand All @@ -59,4 +57,4 @@ struct [[nodiscard("Contains results and potential error")]] SimulationResults
std::optional<Error> error;
};

}
} // namespace Antares::API
1 change: 1 addition & 0 deletions src/api/include/antares/api/solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ namespace Antares::API
*/
SimulationResults PerformSimulation(
const std::filesystem::path& study_path,
const std::filesystem::path& output,
const Antares::Solver::Optimization::OptimizationOptions& optOptions) noexcept;
} // namespace Antares::API
2 changes: 2 additions & 0 deletions src/api/private/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ class APIInternal
* @return SimulationResults object which contains the results of the simulation.
*/
SimulationResults run(const IStudyLoader& study_loader,
const std::filesystem::path& output,
const Antares::Solver::Optimization::OptimizationOptions& optOptions);

private:
std::shared_ptr<Antares::Data::Study> study_;
SimulationResults execute(
const std::filesystem::path& output,
const Antares::Solver::Optimization::OptimizationOptions& optOptions) const;
};

Expand Down
5 changes: 3 additions & 2 deletions src/api/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ namespace Antares::API

SimulationResults PerformSimulation(
const std::filesystem::path& study_path,
const std::filesystem::path& output,
const Antares::Solver::Optimization::OptimizationOptions& optOptions) noexcept
{
try
{
APIInternal api;
FileTreeStudyLoader study_loader(study_path);
return api.run(study_loader, optOptions);
return api.run(study_loader, output, optOptions);
}
catch (const std::exception& e)
{
Antares::API::Error err{.reason = e.what()};
return SimulationResults{.simulationPath = study_path, .antares_problems{}, .error = err};
return SimulationResults{.antares_problems{}, .error = err};
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/api_client_example/src/API_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

#include <utility>

Antares::API::SimulationResults solve(std::filesystem::path study_path)
Antares::API::SimulationResults solve(std::filesystem::path study_path,
std::filesystem::path output)
{
return Antares::API::PerformSimulation(std::move(study_path), {});
return Antares::API::PerformSimulation(std::move(study_path), std::move(output), {});
}
5 changes: 3 additions & 2 deletions src/api_client_example/src/API_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

#pragma once

#include <antares/api/solver.h>
#include <antares/api/SimulationResults.h>
#include <antares/api/solver.h>

Antares::API::SimulationResults solve(std::filesystem::path study_path);
Antares::API::SimulationResults solve(std::filesystem::path study_path,
std::filesystem::path output);
8 changes: 5 additions & 3 deletions src/api_client_example/tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
#define BOOST_TEST_MODULE test_client_api

#include <boost/test/unit_test.hpp>

#include "API_client.h"

BOOST_AUTO_TEST_CASE(test_run) {
auto results = solve("dummy_study_test_client_api");
BOOST_AUTO_TEST_CASE(test_run)
{
auto results = solve("dummy_study_test_client_api", {});
BOOST_CHECK(results.error);
BOOST_CHECK(!results.error->reason.empty());
auto c = results.error->reason;
Expand All @@ -34,4 +36,4 @@ BOOST_AUTO_TEST_CASE(test_run) {
BOOST_CHECK(results.error->reason.find("folder") != std::string::npos);
BOOST_CHECK(results.error->reason.find("not") != std::string::npos);
BOOST_CHECK(results.error->reason.find("exist") != std::string::npos);
}
}
2 changes: 1 addition & 1 deletion src/format-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
if [ $# -eq 0 ]
then
# No arguments: format all
SOURCE_DIRS="analyzer/ libs/ solver/ tools/ config/ tests/ packaging/"
SOURCE_DIRS="analyzer/ libs/ solver/ tools/ config/ tests/ packaging/ api/"
SOURCE_FILES=$(find $SOURCE_DIRS -regextype egrep -regex ".*/*\.(c|cxx|cpp|cc|h|hxx|hpp)$" ! -path '*/antlr-interface/*')
else
# Format files provided as arguments
Expand Down
14 changes: 7 additions & 7 deletions src/libs/antares/study/parts/short-term-storage/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static bool loadHours(const std::string& hoursStr, AdditionalConstraints& additi
fullFormatRegex))
{
logs.error() << "In constraint " << additionalConstraints.name
<< ": Input string does not match the required format: " << hoursStr << '\n';
<< ": Input string does not match the required format: " << hoursStr << '\n';
return false;
}
// Split the `hours` field into multiple groups
Expand All @@ -111,17 +111,17 @@ static bool loadHours(const std::string& hoursStr, AdditionalConstraints& additi

catch (const std::invalid_argument& ex)
{
logs.error() << "In constraint " << additionalConstraints.name <<
" Hours sets contains invalid values: " << hour <<
"\n exception thrown: " << ex.what() << '\n';
logs.error() << "In constraint " << additionalConstraints.name
<< " Hours sets contains invalid values: " << hour
<< "\n exception thrown: " << ex.what() << '\n';

return false;
}
catch (const std::out_of_range& ex)
{
logs.error() << "In constraint " << additionalConstraints.name <<
" Hours sets contains out of range values: " << hour <<
"\n exception thrown: " << ex.what() << '\n';
logs.error() << "In constraint " << additionalConstraints.name
<< " Hours sets contains out of range values: " << hour
<< "\n exception thrown: " << ex.what() << '\n';
return false;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/solver/application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,6 @@ void Application::readDataForTheStudy(Data::StudyLoadOptions& options)

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())
{
Expand Down Expand Up @@ -375,6 +372,9 @@ void Application::execute()
return;
}

// Save about-the-study files (comments, notes, etc.)
pStudy->saveAboutTheStudy(*resultWriter);

SystemMemoryLogger memoryReport;
memoryReport.interval(1000 * 60 * 5); // 5 minutes
memoryReport.start();
Expand Down
39 changes: 39 additions & 0 deletions src/solver/modeler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
add_subdirectory(api)
add_subdirectory(ortoolsImpl)
add_subdirectory(loadFiles)
add_subdirectory(parameters)

OMESSAGE(" :: modeler")

set(exec_name "antares-modeler")

add_library(modeler-lib INTERFACE
${SRCS}
)

add_executable(antares-modeler
main.cpp
${SRCS}
)

set_target_properties(antares-modeler PROPERTIES OUTPUT_NAME ${exec_name})

target_link_libraries(modeler-lib
INTERFACE
Antares::loadModelerFiles
Antares::modelerParameters
)

target_link_libraries(antares-modeler
PRIVATE
modeler-lib
)

import_std_libs(antares-modeler)
executable_strip(antares-modeler)

copy_dependency(sirius_solver antares-modeler)

install(TARGETS antares-modeler EXPORT antares-modeler DESTINATION bin)

INSTALL(EXPORT antares-modeler
FILE antares-modelerConfig.cmake
DESTINATION cmake
)
34 changes: 34 additions & 0 deletions src/solver/modeler/loadFiles/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
set(SOURCES
readSystem.cpp
readLibraries.cpp
readParameters.cpp
handleErrors.cpp

include/antares/solver/modeler/loadFiles/loadFiles.h
)

# Create the library
add_library(loadModelerFiles STATIC ${SOURCES})
add_library(Antares::loadModelerFiles ALIAS loadModelerFiles)

# Specify include directories
target_include_directories(loadModelerFiles
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

# Link dependencies (if any)
target_link_libraries(loadModelerFiles
PUBLIC
Antares::antares-study-system-model
PRIVATE
Antares::io
Antares::systemParser
Antares::modelParser
Antares::modelConverter
Antares::modelerParameters
)

install(DIRECTORY include/antares
DESTINATION "include"
)
38 changes: 38 additions & 0 deletions src/solver/modeler/loadFiles/handleErrors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2007-2024, RTE (https://www.rte-france.com)
* See AUTHORS.txt
* SPDX-License-Identifier: MPL-2.0
* This file is part of Antares-Simulator,
* Adequacy and Performance assessment for interconnected energy networks.
*
* Antares_Simulator is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public Licence 2.0 as published by
* the Mozilla Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 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
* Mozilla Public Licence 2.0 for more details.
*
* You should have received a copy of the Mozilla Public Licence 2.0
* along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/

#include <antares/logs/logs.h>
#include "antares/solver/modeler/loadFiles/loadFiles.h"

namespace Antares::Solver::LoadFiles
{

void handleYamlError(const YAML::Exception& e, const std::string& context)
{
logs.error() << "Error while parsing the yaml file: " << context;
if (!e.mark.is_null())
{
logs.error() << "Line " << e.mark.line << " column " << e.mark.column;
}
logs.error() << e.what();
}

} // namespace Antares::Solver::LoadFiles
Loading

0 comments on commit 2eb45b6

Please sign in to comment.