Skip to content

Commit

Permalink
Modeler 2.5a: Read models and system yaml files [ANT-2306] (#2540)
Browse files Browse the repository at this point in the history
Add the main modeler as a new binary
Directory loadFiles added, used to call previous work to read yaml files
by handling I/O and errors related to yaml parsing
  • Loading branch information
payetvin authored Dec 31, 2024
1 parent f5f634c commit 832e89b
Show file tree
Hide file tree
Showing 20 changed files with 838 additions and 56 deletions.
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

0 comments on commit 832e89b

Please sign in to comment.