From 5bda7723ab605a6962491a4220dd9741c55ca1d8 Mon Sep 17 00:00:00 2001 From: Vincent Payet Date: Tue, 24 Dec 2024 11:59:17 +0100 Subject: [PATCH] Add loadParameters.cpp --- src/solver/modeler/CMakeLists.txt | 2 +- src/solver/modeler/loadFiles/CMakeLists.txt | 1 + .../solver/modeler/loadFiles/loadFiles.h | 7 ++- .../modeler/loadFiles/readParameters.cpp | 61 +++++++++++++++++++ src/solver/modeler/parameters/CMakeLists.txt | 18 +++--- .../parameters/parseModelerParameters.cpp | 7 ++- .../solver/modeler/loadFiles/CMakeLists.txt | 1 + .../solver/modeler/parameters/CMakeLists.txt | 2 +- 8 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 src/solver/modeler/loadFiles/readParameters.cpp diff --git a/src/solver/modeler/CMakeLists.txt b/src/solver/modeler/CMakeLists.txt index 44178780e0..d3d8953f01 100644 --- a/src/solver/modeler/CMakeLists.txt +++ b/src/solver/modeler/CMakeLists.txt @@ -43,7 +43,7 @@ set_target_properties(antares-modeler PROPERTIES OUTPUT_NAME ${exec_name}) target_link_libraries(modeler-lib INTERFACE Antares::loadModelerFiles - modeler-parameters + Antares::modelerParameters ) target_link_libraries(antares-modeler diff --git a/src/solver/modeler/loadFiles/CMakeLists.txt b/src/solver/modeler/loadFiles/CMakeLists.txt index d7a1c0118c..e332714917 100644 --- a/src/solver/modeler/loadFiles/CMakeLists.txt +++ b/src/solver/modeler/loadFiles/CMakeLists.txt @@ -24,6 +24,7 @@ target_link_libraries(loadModelerFiles Antares::systemParser Antares::modelParser Antares::modelConverter + Antares::modelerParameters ) install(DIRECTORY include/antares diff --git a/src/solver/modeler/loadFiles/include/antares/solver/modeler/loadFiles/loadFiles.h b/src/solver/modeler/loadFiles/include/antares/solver/modeler/loadFiles/loadFiles.h index 63ab4bb732..9fb01d9453 100644 --- a/src/solver/modeler/loadFiles/include/antares/solver/modeler/loadFiles/loadFiles.h +++ b/src/solver/modeler/loadFiles/include/antares/solver/modeler/loadFiles/loadFiles.h @@ -24,15 +24,18 @@ #include #include +#include #include #include namespace Antares::Solver::LoadFiles { -Study::SystemModel::System loadSystem(const std::filesystem::path& studyPath, - const std::vector& libraries); +ModelerParameters loadParameters(const std::filesystem::path& studyPath); std::vector loadLibraries(const std::filesystem::path& studyPath); +Study::SystemModel::System loadSystem(const std::filesystem::path& studyPath, + const std::vector& libraries); + } // namespace Antares::Solver::LoadFiles diff --git a/src/solver/modeler/loadFiles/readParameters.cpp b/src/solver/modeler/loadFiles/readParameters.cpp new file mode 100644 index 0000000000..77882131cd --- /dev/null +++ b/src/solver/modeler/loadFiles/readParameters.cpp @@ -0,0 +1,61 @@ +/* + * 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 . + */ + +#include + +#include +#include +#include "antares/solver/modeler/parameters/parseModelerParameters.h" +#include "antares/solver/modeler/loadFiles/loadFiles.h" + +namespace fs = std::filesystem; + +namespace Antares::Solver::LoadFiles +{ + +ModelerParameters loadParameters(const fs::path& studyPath) +{ + try + { + const std::string paramStr = IO::readFile(studyPath / "parameters.yml"); + return parseModelerParameters(paramStr); + } + catch (const YAML::Exception& e) + { + logs.error() << "Error while parsing the yaml parameters file"; + if (!e.mark.is_null()) + { + logs.error() << "Line " << e.mark.line << " column " << e.mark.column; + } + logs.error() << e.what(); + + throw std::runtime_error(e.what()); + } + catch (const std::runtime_error& e) + { + logs.error() << "Error while parsing the yaml parameters file:"; + logs.error() << e.what(); + + throw std::runtime_error(e.what()); + } +} + +} // namespace Antares::Solver::LoadFiles diff --git a/src/solver/modeler/parameters/CMakeLists.txt b/src/solver/modeler/parameters/CMakeLists.txt index 33cfda091e..7ab35d5002 100644 --- a/src/solver/modeler/parameters/CMakeLists.txt +++ b/src/solver/modeler/parameters/CMakeLists.txt @@ -1,14 +1,16 @@ -add_library(modeler-parameters - include/antares/solver/modeler/parameters/modelerParameters.h - include/antares/solver/modeler/parameters/parseModelerParameters.h - parseModelerParameters.cpp - encoder.hxx) +add_library(modelerParameters + include/antares/solver/modeler/parameters/modelerParameters.h + include/antares/solver/modeler/parameters/parseModelerParameters.h + parseModelerParameters.cpp + encoder.hxx) -target_link_libraries(modeler-parameters +add_library(Antares::modelerParameters ALIAS modelerParameters) + +target_link_libraries(modelerParameters PRIVATE yaml-cpp - Antares::io) + Antares::io) -target_include_directories(modeler-parameters +target_include_directories(modelerParameters PUBLIC $) diff --git a/src/solver/modeler/parameters/parseModelerParameters.cpp b/src/solver/modeler/parameters/parseModelerParameters.cpp index c41ca4c81a..46782344e4 100644 --- a/src/solver/modeler/parameters/parseModelerParameters.cpp +++ b/src/solver/modeler/parameters/parseModelerParameters.cpp @@ -20,7 +20,6 @@ */ #include -#include #include #include @@ -29,10 +28,12 @@ namespace Antares::Solver::LoadFiles { -ModelerParameters parseModelerParameters(const std::filesystem::path& studyPath) + +ModelerParameters parseModelerParameters(const std::filesystem::path& filePath) { - const auto contents = Antares::IO::readFile(studyPath / "parameters.yml"); + const auto contents = Antares::IO::readFile(filePath); YAML::Node root = YAML::Load(contents); return root.as(); } + } // namespace Antares::Solver::LoadFiles diff --git a/src/tests/src/solver/modeler/loadFiles/CMakeLists.txt b/src/tests/src/solver/modeler/loadFiles/CMakeLists.txt index 81bbf085cc..816e2d54f3 100644 --- a/src/tests/src/solver/modeler/loadFiles/CMakeLists.txt +++ b/src/tests/src/solver/modeler/loadFiles/CMakeLists.txt @@ -5,6 +5,7 @@ target_link_libraries(load-modeler-files PRIVATE Boost::unit_test_framework Antares::loadModelerFiles + Antares::modelerParameters test_utils_unit ) diff --git a/src/tests/src/solver/modeler/parameters/CMakeLists.txt b/src/tests/src/solver/modeler/parameters/CMakeLists.txt index 85c27e2969..3870a14e1f 100644 --- a/src/tests/src/solver/modeler/parameters/CMakeLists.txt +++ b/src/tests/src/solver/modeler/parameters/CMakeLists.txt @@ -3,5 +3,5 @@ include(${CMAKE_SOURCE_DIR}/tests/macros.cmake) add_boost_test(parse-parameters SRC testParametersParsing.cpp LIBS - modeler-parameters + Antares::modelerParameters test_utils_unit)