Antares Simulator
Power System Simulator
model.h
1 /*
2 ** Copyright 2007-2025, RTE (https://www.rte-france.com)
3 ** See AUTHORS.txt
4 ** SPDX-License-Identifier: MPL-2.0
5 ** This file is part of Antares-Simulator,
6 ** Adequacy and Performance assessment for interconnected energy networks.
7 **
8 ** Antares_Simulator is free software: you can redistribute it and/or modify
9 ** it under the terms of the Mozilla Public Licence 2.0 as published by
10 ** the Mozilla Foundation, either version 2 of the License, or
11 ** (at your option) any later version.
12 **
13 ** Antares_Simulator is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** Mozilla Public Licence 2.0 for more details.
17 **
18 ** You should have received a copy of the Mozilla Public Licence 2.0
19 ** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20 */
21 #pragma once
22 
23 #include <map>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include <antares/expressions/expression.h>
28 
29 #include "connection.h"
30 #include "constraint.h"
31 #include "extraOutput.h"
32 #include "objective.h"
33 #include "parameter.h"
34 #include "port.h"
35 #include "portFieldDefinition.h"
36 #include "variable.h"
37 
38 namespace Antares::ModelerStudy::SystemModel
39 {
41 {
42  std::string portId;
43  std::string fieldId;
44  auto operator<=>(const PortFieldKey&) const = default;
45 };
46 
47 class PortFieldKeyHash final
48 {
49 public:
50  std::size_t operator()(const PortFieldKey& input) const;
51 };
52 
53 using PortFieldMap = std::unordered_map<PortFieldKey, PortFieldDefinition, PortFieldKeyHash>;
54 
59 // TODO: add unit tests for this class
60 class Model final
61 {
62 public:
63  Model() = default;
64  Model(Model&&) = default;
65  Model(const Model&) = delete;
66  ~Model() = default;
67 
68  Model& operator=(Model&&) = default;
69  Model& operator=(const Model&) = delete;
70 
71  const std::string& Id() const
72  {
73  return id_;
74  }
75 
76  const std::vector<Objective>& Objectives() const
77  {
78  return objectives_;
79  }
80 
81  const std::vector<Constraint>& Constraints() const
82  {
83  return constraints_;
84  }
85 
86  const std::map<std::string, Parameter>& Parameters() const
87  {
88  return parameters_;
89  }
90 
91  const std::vector<Variable>& Variables() const
92  {
93  return variables_;
94  }
95 
96  const std::map<std::string, Port>& Ports() const
97  {
98  return ports_;
99  }
100 
101  const PortFieldMap& PortFieldDefinitions() const
102  {
103  return portFieldDefinitions_;
104  }
105 
106  const std::map<std::string, ExtraOutput>& ExtraOutputs() const
107  {
108  return extraOutputs_;
109  }
110 
111 private:
112  friend class ModelBuilder;
113  std::string id_;
114 
115  std::map<std::string, Parameter> parameters_;
116  std::vector<Variable> variables_;
117  std::vector<Constraint> constraints_;
118  std::map<std::string, Port> ports_;
119  std::map<std::string, ExtraOutput> extraOutputs_;
120  std::vector<Objective> objectives_;
121 
122  PortFieldMap portFieldDefinitions_;
123 };
124 
125 // List of IDs used internally to check for uniqueness of IDs at component level
126 class UniqueIDChecker final
127 {
128 public:
129  void add(const std::string& id);
130  void check(const std::string& modelId) const;
131  void clear();
132 
133 private:
134  std::unordered_map<std::string, int> attribute_ids_;
135 };
136 
137 class ModelBuilder final
138 {
139 public:
140  ModelBuilder& withId(std::string_view id);
141  ModelBuilder& withObjectives(std::vector<Objective>&& objectives);
142  ModelBuilder& withParameters(std::vector<Parameter>&& parameters);
143  ModelBuilder& withVariables(std::vector<Variable>&& variables);
144  ModelBuilder& withPorts(std::vector<Port>&& ports);
145  ModelBuilder& withConstraints(std::vector<Constraint>&& constraints);
146  ModelBuilder& withPortFieldDefinitions(std::vector<PortFieldDefinition>&& portFieldDefinitions);
147  ModelBuilder& withExtraOutputs(std::vector<ExtraOutput>&& extraOutputs);
148  Model build();
149 
150 private:
151  Model model_;
152  UniqueIDChecker uniqueIdChecker_;
153  void reset();
154 };
155 
156 } // namespace Antares::ModelerStudy::SystemModel
ModelBuilder & withParameters(std::vector< Parameter > &&parameters)
Sets the parameters of the model.
Definition: model.cpp:152
ModelBuilder & withObjectives(std::vector< Objective > &&objectives)
Sets the objectives of the model.
Definition: model.cpp:134
ModelBuilder & withExtraOutputs(std::vector< ExtraOutput > &&extraOutputs)
Sets the extra outputs of the model.
Definition: model.cpp:241
ModelBuilder & withConstraints(std::vector< Constraint > &&constraints)
Sets the constraints of the model.
Definition: model.cpp:199
Model build()
Builds and returns the Model object.
Definition: model.cpp:94
ModelBuilder & withId(std::string_view id)
Sets the ID of the model.
Definition: model.cpp:122
ModelBuilder & withPorts(std::vector< Port > &&ports)
Sets the ports of the model.
Definition: model.cpp:185
ModelBuilder & withVariables(std::vector< Variable > &&variables)
Sets the variables of the model.
Definition: model.cpp:166
ModelBuilder & withPortFieldDefinitions(std::vector< PortFieldDefinition > &&portFieldDefinitions)
Sets the port-field definitions of the model.
Definition: model.cpp:217