Skip to content

Commit

Permalink
Modeler 4.6: system import [ANT-2207] (#2530)
Browse files Browse the repository at this point in the history
This PR add the ability to read a YAML file containing all the infos
about the electric system and which models they're related to,
converting it into system-model structures.

---------

Co-authored-by: Florian Omnès <florian.omnes@rte-france.com>
  • Loading branch information
payetvin and flomnes authored Dec 18, 2024
1 parent 7ead326 commit ce06183
Show file tree
Hide file tree
Showing 16 changed files with 916 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/solver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_subdirectory(lps)
add_subdirectory(misc)
add_subdirectory(modelConverter)
add_subdirectory(modelParser)
add_subdirectory(systemParser)
add_subdirectory(modeler)
add_subdirectory(optimisation)
add_subdirectory(signal-handling)
Expand Down
32 changes: 32 additions & 0 deletions src/solver/systemParser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
find_package(yaml-cpp REQUIRED)

set(SOURCES
parser.cpp
converter.cpp
encoders.hxx
include/antares/solver/systemParser/parser.h
include/antares/solver/systemParser/converter.h
include/antares/solver/systemParser/system.h
)

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

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

# Link dependencies (if any)
target_link_libraries(systemParser
PUBLIC
Antares::antares-study-system-model
PRIVATE
yaml-cpp
)

install(DIRECTORY include/antares
DESTINATION "include"
)
131 changes: 131 additions & 0 deletions src/solver/systemParser/converter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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/solver/systemParser/converter.h"

#include <algorithm>

#include "antares/solver/systemParser/system.h"
#include "antares/study/system-model/system.h"

using namespace Antares::Study;

namespace Antares::Solver::SystemConverter
{

class ErrorWhileSplittingLibraryAndModel: public std::runtime_error
{
public:
explicit ErrorWhileSplittingLibraryAndModel(const std::string& s):
runtime_error("'.' not found while splitting library and model: " + s)
{
}
};

class LibraryNotFound: public std::runtime_error
{
public:
explicit LibraryNotFound(const std::string& s):
runtime_error("No library found with this name: " + s)
{
}
};

class ModelNotFound: public std::runtime_error
{
public:
explicit ModelNotFound(const std::string& s):
runtime_error("No model found with this name: " + s)
{
}
};

static std::pair<std::string, std::string> splitLibraryModelString(const std::string& s)
{
size_t pos = s.find('.');
if (pos == std::string::npos)
{
throw ErrorWhileSplittingLibraryAndModel(s);
}

std::string library = s.substr(0, pos);
std::string model = s.substr(pos + 1);
return {library, model};
}

static const SystemModel::Model& getModel(const std::vector<SystemModel::Library>& libraries,
const std::string& libraryId,
const std::string& modelId)
{
auto lib = std::ranges::find_if(libraries,
[&libraryId](const auto& l) { return l.Id() == libraryId; });
if (lib == libraries.end())
{
throw LibraryNotFound(libraryId);
}

auto search = lib->Models().find(modelId);
if (search == lib->Models().end())
{
throw ModelNotFound(modelId);
}

return search->second;
}

static SystemModel::Component createComponent(const SystemParser::Component& c,
const std::vector<SystemModel::Library>& libraries)
{
const auto [libraryId, modelId] = splitLibraryModelString(c.model);
SystemModel::ModelBuilder model_builder;

const SystemModel::Model& model = getModel(libraries, libraryId, modelId);

SystemModel::ComponentBuilder component_builder;

std::map<std::string, double> parameters;
for (const auto& p: c.parameters)
{
parameters.try_emplace(p.id, p.value);
}

auto component = component_builder.withId(c.id)
.withModel(&model)
.withScenarioGroupId(c.scenarioGroup)
.withParameterValues(parameters)
.build();
return component;
}

SystemModel::System convert(const SystemParser::System& parserSystem,
const std::vector<SystemModel::Library>& libraries)
{
std::vector<SystemModel::Component> components;
for (const auto& c: parserSystem.components)
{
components.push_back(createComponent(c, libraries));
}

SystemModel::SystemBuilder builder;
return builder.withId(parserSystem.id).withComponents(components).build();
}

} // namespace Antares::Solver::SystemConverter
95 changes: 95 additions & 0 deletions src/solver/systemParser/encoders.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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 "antares/solver/systemParser/system.h"

#include "yaml-cpp/yaml.h"

// Implement convert specializations
namespace YAML
{

/**
* @brief shortend to default construct a value when node is null
* @tparam T Type to convert the node to
* @param n node
* @return Object of type T
* It's just to simplify repertitve and verbose lines
* as_fallback_default<std::vector<Antares::Solver::SystemParser::Parameter>>(
node["parameters"]) is equivalent to
node["parameters"].as<std::vector<Antares::Solver::SystemParser::Parameter>>(std::vector<Antares::Solver::SystemParser::Parameter>())
*/
template<typename T>
inline T as_fallback_default(const Node& n)
{
return n.as<T>(T());
}

template<>
struct convert<Antares::Solver::SystemParser::Parameter>
{
static bool decode(const Node& node, Antares::Solver::SystemParser::Parameter& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.id = node["id"].as<std::string>();
rhs.type = node["type"].as<std::string>();
rhs.value = node["value"].as<double>();
return true;
}
};

template<>
struct convert<Antares::Solver::SystemParser::Component>
{
static bool decode(const Node& node, Antares::Solver::SystemParser::Component& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.id = node["id"].as<std::string>();
rhs.model = node["model"].as<std::string>();
rhs.scenarioGroup = node["scenario-group"].as<std::string>();
rhs.parameters = as_fallback_default<std::vector<Antares::Solver::SystemParser::Parameter>>(
node["parameters"]);
return true;
}
};

template<>
struct convert<Antares::Solver::SystemParser::System>
{
static bool decode(const Node& node, Antares::Solver::SystemParser::System& rhs)
{
rhs.id = node["id"].as<std::string>();
rhs.libraries = as_fallback_default<std::vector<std::string>>(node["model-libraries"]);
rhs.components = as_fallback_default<std::vector<Antares::Solver::SystemParser::Component>>(
node["components"]);
return true;
}
};

} // namespace YAML
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 <antares/study/system-model/library.h>
#include <antares/study/system-model/system.h>

#include "parser.h"

namespace Antares::Solver::SystemConverter
{

Study::SystemModel::System convert(const SystemParser::System& parserSystem,
const std::vector<Study::SystemModel::Library>& libraries);

} // namespace Antares::Solver::SystemConverter
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 "antares/solver/systemParser/system.h"

namespace Antares::Solver::SystemParser
{
class Parser
{
public:
System parse(const std::string& content);
};
} // namespace Antares::Solver::SystemParser
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 <string>
#include <vector>

namespace Antares::Solver::SystemParser
{

struct Parameter
{
std::string id;
std::string type;
double value;
};

struct Component
{
std::string id;
std::string model;
std::string scenarioGroup;
std::vector<Parameter> parameters;
};

struct System
{
std::string id;
std::vector<std::string> libraries;
std::vector<Component> components;

// will be implemented later
// std::vector<Connections> connections;
};

} // namespace Antares::Solver::SystemParser
Loading

0 comments on commit ce06183

Please sign in to comment.