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

Modeler 2.5a: Read models and system yaml files [ANT-2306] #2540

Merged
merged 47 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
96c13d7
Add dir loadfile/ , add readSystem
payetvin Dec 19, 2024
ffe1554
format
payetvin Dec 19, 2024
e8da375
add readLibraries.cpp
payetvin Dec 19, 2024
b4a2a74
add function to read one lib file
payetvin Dec 19, 2024
478b087
add a loadLibraries func
payetvin Dec 19, 2024
67c3754
format
payetvin Dec 19, 2024
2fb43d6
Add unit test folder
payetvin Dec 19, 2024
ad4ca2d
First test case
payetvin Dec 19, 2024
cba7b31
several libs
payetvin Dec 19, 2024
78e083f
comments
payetvin Dec 19, 2024
4978046
merge develop, conflicts
payetvin Dec 19, 2024
385f53a
test load system
payetvin Dec 19, 2024
6f89696
comments
payetvin Dec 19, 2024
67cd455
add headers
payetvin Dec 19, 2024
a807358
add a main
payetvin Dec 19, 2024
0b3794a
fill main
payetvin Dec 19, 2024
b5a44c7
parameters.yml in class, correct extension: yml and not yaml
payetvin Dec 20, 2024
beb7339
test file not yml
payetvin Dec 20, 2024
869a67a
handle invalid directory for the study
payetvin Dec 20, 2024
7d8c306
fix test
payetvin Dec 20, 2024
02f366f
use namespace loadFiles for parameters
payetvin Dec 20, 2024
d429081
error handling for libraries loading
payetvin Dec 20, 2024
f8c3d70
error handling for system
payetvin Dec 20, 2024
9d93c9d
catch yaml exception, use mark if available
payetvin Dec 23, 2024
557edaf
Merge branch 'develop' into feature/2.5-read-input-files
payetvin Dec 24, 2024
5bda772
Add loadParameters.cpp
payetvin Dec 24, 2024
0dc1f63
Remove windows related lines in modeler main cmake
payetvin Dec 24, 2024
20df9eb
format
payetvin Dec 24, 2024
d74f595
fix tests
payetvin Dec 24, 2024
732b0ef
try fix windows unit tests
payetvin Dec 24, 2024
b39407f
try fix windows unit tests 2
payetvin Dec 24, 2024
663b786
catch specific error in main
payetvin Dec 24, 2024
4c42da5
catch generic exception in main
payetvin Dec 24, 2024
4dd06fa
change tests layout
payetvin Dec 24, 2024
58e1148
test parameter missing
payetvin Dec 24, 2024
511dc7e
format
payetvin Dec 24, 2024
00183c4
add error handling functions to avoid duplication
payetvin Dec 30, 2024
559f739
add error handling for read system and libraries
payetvin Dec 30, 2024
bf08250
format
payetvin Dec 30, 2024
a230b55
rename file to handleErrors.cpp
payetvin Dec 30, 2024
e1679bf
format
payetvin Dec 30, 2024
abb4e33
path to string conversion fir windows
payetvin Dec 30, 2024
6df9b65
split try catch for parameters
payetvin Dec 31, 2024
9341362
split try catch for system and libraries loading
payetvin Dec 31, 2024
e80708b
format
payetvin Dec 31, 2024
cbe01b2
test yaml error with mark
payetvin Dec 31, 2024
267d5fa
Add info logs when files are loaded
payetvin Dec 31, 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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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/>.
*/

#pragma once

#include <filesystem>
#include <vector>
#include <yaml-cpp/yaml.h>

#include <antares/solver/modeler/parameters/modelerParameters.h>
#include <antares/study/system-model/library.h>
#include <antares/study/system-model/system.h>

namespace Antares::Solver::LoadFiles
{

ModelerParameters loadParameters(const std::filesystem::path& studyPath);

std::vector<Study::SystemModel::Library> loadLibraries(const std::filesystem::path& studyPath);

Study::SystemModel::System loadSystem(const std::filesystem::path& studyPath,
const std::vector<Study::SystemModel::Library>& libraries);

void handleYamlError(const YAML::Exception& e, const std::string& context);

/// Generic error class for all loading errors to catch in the main
class ErrorLoadingYaml: public std::runtime_error
{
public:
explicit ErrorLoadingYaml(const std::string& s):
runtime_error(s)
{
}
};

} // namespace Antares::Solver::LoadFiles
92 changes: 92 additions & 0 deletions src/solver/modeler/loadFiles/readLibraries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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 <yaml-cpp/yaml.h>

#include <antares/io/file.h>
#include <antares/logs/logs.h>
#include <antares/solver/modelConverter/modelConverter.h>
#include <antares/solver/modelParser/parser.h>
#include "antares/solver/modeler/loadFiles/loadFiles.h"

namespace fs = std::filesystem;

namespace Antares::Solver::LoadFiles
{

static Study::SystemModel::Library loadSingleLibrary(const fs::path& filePath)
{
std::string libraryStr;
try
{
libraryStr = IO::readFile(filePath);
}
catch (const std::runtime_error& e)
{
logs.error() << "Error while trying to read this library file: " << filePath;
throw ErrorLoadingYaml(e.what());
}

ModelParser::Parser parser;
ModelParser::Library libraryObj;

try
{
libraryObj = parser.parse(libraryStr);
}
catch (const YAML::Exception& e)
{
handleYamlError(e, filePath.string());
throw ErrorLoadingYaml(e.what());
}

try
{
return ModelConverter::convert(libraryObj);
}
catch (const std::runtime_error& e)
{
logs.error() << "Error while converting this library yaml: " << filePath;
throw ErrorLoadingYaml(e.what());
}
}

std::vector<Study::SystemModel::Library> loadLibraries(const fs::path& studyPath)
{
std::vector<Study::SystemModel::Library> libraries;

const fs::path directoryPath = studyPath / "input" / "model-libraries";
for (const auto& entry: fs::directory_iterator(directoryPath))
{
if (entry.path().extension() != ".yml")
{
logs.info() << entry.path()
<< " ignored, only files having the `.yml` extension are loaded";
continue;
}

libraries.push_back(loadSingleLibrary(entry.path()));
logs.info() << "Library loaded: " << libraries.back().Id();
}

return libraries;
}
} // namespace Antares::Solver::LoadFiles
59 changes: 59 additions & 0 deletions src/solver/modeler/loadFiles/readParameters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 <yaml-cpp/yaml.h>

#include <antares/io/file.h>
#include <antares/logs/logs.h>
#include "antares/solver/modeler/loadFiles/loadFiles.h"
#include "antares/solver/modeler/parameters/parseModelerParameters.h"

namespace fs = std::filesystem;

namespace Antares::Solver::LoadFiles
{

ModelerParameters loadParameters(const fs::path& studyPath)
{
std::string filename = "parameters.yml";
std::string paramStr;
try
{
paramStr = IO::readFile(studyPath / filename);
}
catch (const std::runtime_error& e)
{
logs.error() << "Error while trying to read file parameters.yml";
throw ErrorLoadingYaml(e.what());
}

try
{
return parseModelerParameters(paramStr);
}
catch (const YAML::Exception& e)
{
handleYamlError(e, filename);
throw ErrorLoadingYaml(e.what());
}
}

} // namespace Antares::Solver::LoadFiles
73 changes: 73 additions & 0 deletions src/solver/modeler/loadFiles/readSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 <yaml-cpp/yaml.h>

#include <antares/io/file.h>
#include <antares/logs/logs.h>
#include <antares/solver/systemParser/converter.h>
#include <antares/solver/systemParser/parser.h>
#include "antares/solver/modeler/loadFiles/loadFiles.h"

namespace fs = std::filesystem;

namespace Antares::Solver::LoadFiles
{

Study::SystemModel::System loadSystem(const fs::path& studyPath,
const std::vector<Study::SystemModel::Library>& libraries)
{
std::string filename = "system.yml";
std::string systemStr;
try
{
systemStr = IO::readFile(studyPath / "input" / filename);
}
catch (const std::runtime_error& e)
{
logs.error() << "Error while trying to read file system.yml";
throw ErrorLoadingYaml(e.what());
}

SystemParser::Parser parser;
SystemParser::System systemObj;
try
{
systemObj = parser.parse(systemStr);
}
catch (const YAML::Exception& e)
{
handleYamlError(e, filename);
throw ErrorLoadingYaml(e.what());
}

try
{
return SystemConverter::convert(systemObj, libraries);
}
catch (const std::runtime_error& e)
{
logs.error() << "Error while converting the system yaml to components";
throw ErrorLoadingYaml(e.what());
}
}

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