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 4.6: system import [ANT-2207] #2530

Merged
merged 53 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
57c3088
Base for system parser
payetvin Dec 10, 2024
99026ca
library.h
payetvin Dec 10, 2024
27fc0fc
format
payetvin Dec 10, 2024
493a629
fix some compile err
payetvin Dec 10, 2024
4b8cd4b
compile ok, start encoder
payetvin Dec 11, 2024
f440fac
encoder ok
payetvin Dec 11, 2024
9a478db
system in parser
payetvin Dec 11, 2024
0f8fb8a
system converter
payetvin Dec 11, 2024
3df0a31
format
payetvin Dec 11, 2024
963cbea
rename library.h into system.h
payetvin Dec 11, 2024
168eb54
remove systemConverter
payetvin Dec 11, 2024
0c073d8
add test system parser
payetvin Dec 11, 2024
7380bbb
test empty ok
payetvin Dec 12, 2024
5757d2c
more tests
payetvin Dec 12, 2024
1507574
c
payetvin Dec 12, 2024
3289a8d
test components
payetvin Dec 12, 2024
c884e8f
test parameters
payetvin Dec 12, 2024
4d7767f
double for param value
payetvin Dec 12, 2024
9f56e65
format
payetvin Dec 12, 2024
38524e0
Create components model with a simple vector, add tests
payetvin Dec 12, 2024
1942d6e
Add parameters to createComponent
payetvin Dec 12, 2024
533f0d5
fix tests
payetvin Dec 12, 2024
2e5a875
format
payetvin Dec 12, 2024
b58f119
add converter.cpp
payetvin Dec 12, 2024
41cbd04
fix test system study
payetvin Dec 12, 2024
e9baf5e
add converter header
payetvin Dec 12, 2024
c410da8
format
payetvin Dec 12, 2024
4654260
use convert function in unit tests
payetvin Dec 12, 2024
af95bc3
format
payetvin Dec 12, 2024
f70e7b6
split library and model
payetvin Dec 13, 2024
157c83a
shared ptr for model
payetvin Dec 13, 2024
feed197
add test converter
payetvin Dec 13, 2024
9cd5c38
pass libraries
payetvin Dec 13, 2024
ab7c2b0
add get model
payetvin Dec 13, 2024
55fc259
fix test
payetvin Dec 13, 2024
9ce46f8
test param not in model
payetvin Dec 13, 2024
a03866d
format
payetvin Dec 16, 2024
d976130
custom exceptions
payetvin Dec 16, 2024
c83e3e7
test those exceptions
payetvin Dec 16, 2024
62c2b1c
no model found
payetvin Dec 16, 2024
2456f69
error no model found
payetvin Dec 16, 2024
1c9f0c6
Full unit test example
payetvin Dec 16, 2024
3529109
Add component id in logs
payetvin Dec 16, 2024
81771fb
logs and format
payetvin Dec 16, 2024
3e55a6e
rename fixture
payetvin Dec 16, 2024
39a4d40
simplify
payetvin Dec 16, 2024
a33570e
remove espace insecables
payetvin Dec 16, 2024
7058d96
format
payetvin Dec 16, 2024
6c14f79
add checks
payetvin Dec 17, 2024
e41a9a7
Merge branch 'develop' into feature/4.6-system-import
flomnes Dec 17, 2024
efb0bbb
correct log with id
payetvin Dec 17, 2024
d6fb19b
Merge branch 'feature/4.6-system-import' of https://github.com/Antare…
payetvin Dec 17, 2024
5954300
comments
payetvin Dec 17, 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
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"
)
133 changes: 133 additions & 0 deletions src/solver/systemParser/converter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a dedicated struct for the return type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to use a pair and get var like this in this context const auto [libraryId, modelId] = getSplit...

{
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,
payetvin marked this conversation as resolved.
Show resolved Hide resolved
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);
}

try
{
auto& model = lib->Models().at(modelId);
return &model;
}
catch (const std::out_of_range&)
{
throw ModelNotFound(modelId);
}
payetvin marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading