Harmony
HARMONic stabilitY assessment of PE-penetrated power systems
Loading...
Searching...
No Matches
MMC.h
Go to the documentation of this file.
1#ifndef MMC_H
2#define MMC_H
3
9#include "Converter.h"
10#include "../../Include_control_blocks.h"
11
12// Forward declarations
13class Controller;
14class Filter;
15
21class MMC : public Converter {
22public:
42 MMC(const std::string& symbol, const std::string& location,
43 double omega, double activePower, double reactivePower,
44 double angle, double acVoltage, double Pdc, double dcVoltage,
45 double armInductance, double armResistance, double armCapacitance,
46 int numSubmodules, double reactorInductance, double reactorResistance,
47 double timeDelay);
48
55 MMC(const std::string& symbol, const std::string& location, const std::vector<double>& converter_params);
56
64 MMC(const std::string& symbol, const std::string& location, const std::vector<double>& converter_params, const std::vector<double>& controller_params);
65
74 MMC(const std::string& symbol, const std::string& location, const std::vector<double>& converter_params,
75 const std::vector<double>& controller_params, const std::vector<double>& filter_params);
76
77 // Initialization methods
78 void init_Controller(const std::vector<double>& converter_params);// Method to initialize controllers and Filters in MMC
79 void init_Filter(const std::vector<double>& converter_params);
80 void update_MMC(double Vm, double theta, double Pac, double Qac, double Vdc, double Pdc);
81
83 bool hasGfm() const { return gfm_index_ >= 0 && controls.count("gfm") > 0; }
84
86 std::pair<double, double> getGfmDroops() const;
87
89 void setGfmDroops(double Kdroop_P, double Kdroop_Q);
90
91 // Destructor — control blocks are freed by Converter base class
92 ~MMC() override = default;
93
94
95 // Equilibrium point calculation
96 virtual void solveEquilibrium() override;
97 virtual Eigen::MatrixXd computeStateDerivatives(const Eigen::VectorXd& x, const Eigen::VectorXd& u) override;
98 virtual void computeABCD() override;
100 Eigen::MatrixXd computePlantJacobian(
101 double w,
102 double mDd, double mDq, double mDZd, double mDZq,
103 double mSd, double mSq, double mSz) const;
104
107
108 // Y-parameter computation
109 std::vector<std::vector<complex<double>>> compute_y_parameters(double frequency) override;
110
111 // Override to print MMC-specific parameters
112 virtual void printElementValues() override;
113
114 void computePowerFlow(std::map<std::string, double>& data,
115 std::map<std::string, double>& globalParams) const override
116 {
117 for (auto& [key, value] : element_OPF_info)
118 data[key] = value; // Copy OPF info to branch data
119
120 //data["bf"] = 0.0; // conductance of the filter
121 //data["rf"] = 0.0; // Resistance of the filter
122 //data["xf"] = 0.0; // Reactance of the filter
123 data["xc"] = globalParams["omega"] * L_reactor / globalParams["ACZbase"]; // Base voltage for AC
124 data["rc"] = R_reactor / globalParams["ACZbase"]; // Resistance of the phase reactor
125 data["P_g"] = P / 1e6; // Setting of DC p-control value
126 data["Q_g"] = Q / 1e6; // Setting of AC q-control value
127 data["Vtar"] = V_dc / 1e3 / globalParams["DCbaseKV"]; // Seting of DC v-control value
128
129 data["gridac"] = (int)element_location[2] - '0'; // AC grid number
130
131 // DC side type_dc(1 = constant DC power control (i.e. active power), 2 = constant DC voltage control, 3 = DC droop control)
132 if (controls.count("active_power")) {
133 if (!element_OPF_info.count("type_dc"))
134 data["type_dc"] = 1;
135 }
136 else if (controls.count("dc_voltage")) {
137 if (!element_OPF_info.count("type_dc"))
138 data["type_dc"] = 2;
139 }
140 else if (controls.count("droop")) {
141 if (!element_OPF_info.count("type_dc"))
142 data["type_dc"] = 3;
143 }
144
145 // AC side control type_ac (1 = constant AC voltage control, 2 = constant reactive power control)
146 if (controls.count("ac_voltage")) {
147 if (!element_OPF_info.count("type_ac"))
148 data["type_ac"] = 1;
149 }
150 else if (controls.count("reactive_power")) {
151 if (!element_OPF_info.count("type_ac"))
152 data["type_ac"] = 2;
153 }
154
155 if (element_OPF_info.count("type_dc"))
156 data["type_dc"] = element_OPF_info.at("type_dc");
157 if (element_OPF_info.count("type_ac"))
158 data["type_ac"] = element_OPF_info.at("type_ac");
159 }
160
161 // One MMC arm-voltage time step
162 //vector<MatrixXcd> simulateTimeStep(const vector<MatrixXcd>& input, double Ts, int nKeep1, int nKeep2) override;
163
164 // State-space model manipulation - generic MNA stamping
165 void writeMNAmatrix(SymEngine::DenseMatrix&, std::unordered_map<Bus*, int>&, int,
166 std::map<Element*, std::vector<RCP<const Basic>>>&) override;
167
168 std::vector<RCP<const Basic>> getVirtualInputSymbols() const override;
169
170 std::vector<MatrixXcd> simulateInputStep(
171 const std::vector<MatrixXcd>& states, int nKeep) const override;
172
173 int getNumberOfInternalStates() const override { return number_of_states; }
174
175 //add18/5
176 //
177 // // === BEGIN DQsym: expose plant-only state count ===
178 int getNumberOfPlantStates() const override {
179 /*std::cout << "[MMC::getNumberOfPlantStates] returning " << n_plant_states_ << "\n"; */
180 return n_plant_states_; }
181 // === END DQsym: expose plant-only state count ===
182
183 //add18/5[
184
185 // added18/5=== BEGIN DQsym closed-loop control (public interface) ===
186 void stepControllers(double dt,
187 const std::vector<Eigen::MatrixXcd>& states,
188 const Eigen::Vector2d& Vg_dq);
189 // added18/5]=== END DQsym closed-loop control ===
190
191 // added18/5=== BEGIN DQsym closed-loop control (members) ===
192 Eigen::VectorXd x_ctrl_dqsym_; // persistent controller integrator states
193 Eigen::MatrixXcd mD_dqsym_; // current Δ-modulation (set by stepControllers)
194 Eigen::MatrixXcd mS_dqsym_; // current Σ-modulation
195 bool dqsym_initialized_ = false; // first-call init flag
196
197 // Modulation references exposed by computeStateDerivatives (side-channel output).
198 // Written every call; read only by stepControllers.
199 mutable double last_vMDelta_d_ref_ = 0.0;
200 mutable double last_vMDelta_q_ref_ = 0.0;
201 mutable double last_vMSigma_d_ref_ = 0.0;
202 mutable double last_vMSigma_q_ref_ = 0.0;
203 mutable double last_vMSigma_z_ref_ = 0.0;
204 // added18/5=== END DQsym closed-loop control ===
205
206
207
208 map_basic_basic getParameterSubstitutions() const override;
209
210private:
211 double L_arm; // Arm inductance [H]
212 double R_arm; // Arm resistance
213 double C_arm; // Capacitance per submodule [F]
214 int N; // Number of submodules per arm
215
216 // Helper values
217 double L_eq = 0.0, R_eq = 0.0, m_1 = 1.0;
218
219 // State variables
220 int number_of_states = 12;
221 int vdc_index = 0; // Index for DC voltage in state vector
222 int gfm_index_ = -1; // Start index of GFM states (theta, Pac_f, Qac_f); -1 if disabled
223 double gfm_E_ref_ = 0.0; // GFM internal voltage magnitude reference
225 bool gfm_scale_eq_residual_ = false;
226
227 // add18/5=== BEGIN plant state count (captured at construction, before non-plant states added) ===
228 int n_plant_states_ = 12; // will be overwritten in constructor with actual value
229 // add18/5=== END plant state count ===
230
231 // Open-loop feedforward used when outer controllers (occ/zcc) are absent.
232 bool open_loop_modulation_ = false;
233 double ol_vMDelta_d_ref_ = 0.0;
234 double ol_vMDelta_q_ref_ = 0.0;
235 double ol_vMSigma_z_ref_ = 0.0;
236 Eigen::VectorXd equilibrium_guess_;
237
238 void computeOpenLoopArmRefs(
239 double Id, double Iq, double Vdc, double iSigma_z,
240 double& vMDelta_d, double& vMDelta_q, double& vMSigma_z) const;
241 void initializeDelayStates(
242 Eigen::VectorXd& x0, double Vdc,
243 double vMDelta_d, double vMDelta_q, double vMSigma_z) const;
244 void seedPlantStateGuess(
245 Eigen::VectorXd& x0, double Id, double Iq, double iSigma_z) const;
246
247};
248
249#endif // MMC_H
Base class for power electronic converters with state-space models.
RCP< const Basic > omega
Angular frequency symbol ω.
Definition Symbolic_functions.cpp:10
Abstract controller with per-channel reference values.
Definition Controller.h:17
Abstract power converter with ABCD matrices, controllers, and filters.
Definition Converter.h:17
double R_reactor
Definition Converter.h:129
double Q
Definition Converter.h:119
double L_reactor
Definition Converter.h:128
double theta
Definition Converter.h:125
double P
Definition Converter.h:118
double V_dc
Definition Converter.h:127
std::map< std::string, Controller * > controls
Definition Converter.h:145
Base class for multi-phase network elements with Y-parameters and MNA stamping hooks.
Definition Element.h:29
std::string element_location
Definition Element.h:282
std::map< std::string, double > element_OPF_info
Definition Element.h:286
LTI filter with configurable time constant, damping, and bandwidth.
Definition Filter.h:17
Modular Multilevel Converter with arm dynamics and control loops.
Definition MMC.h:21
double last_vMSigma_z_ref_
Definition MMC.h:203
void stepControllers(double dt, const std::vector< Eigen::MatrixXcd > &states, const Eigen::Vector2d &Vg_dq)
Definition MMC.cpp:1884
Eigen::MatrixXcd mD_dqsym_
Definition MMC.h:193
int getNumberOfInternalStates() const override
Number of internal state variables for dynamic elements.
Definition MMC.h:173
virtual void printElementValues() override
Print MMC element and controller parameter values.
Definition MMC.cpp:1676
Eigen::MatrixXcd mS_dqsym_
Definition MMC.h:194
std::pair< double, double > getGfmDroops() const
Return current GFM droops (Kdroop_P, Kdroop_Q).
Definition MMC.cpp:423
Eigen::VectorXd x_ctrl_dqsym_
Definition MMC.h:192
double last_vMDelta_q_ref_
Definition MMC.h:200
int getNumberOfPlantStates() const override
Number of plant states exposed for simulation (may differ from internal states).
Definition MMC.h:178
virtual void computeABCD() override
Numerically compute the Jacobian matrices A and B using finite differences.
Definition MMC.cpp:968
virtual Eigen::MatrixXd computeStateDerivatives(const Eigen::VectorXd &x, const Eigen::VectorXd &u) override
Compute the state derivatives for the MMC system.
Definition MMC.cpp:455
void init_Filter(const std::vector< double > &converter_params)
Initialize the filter(s) in MMC using provided parameters.
Definition MMC.cpp:319
void update_MMC(double Vm, double theta, double Pac, double Qac, double Vdc, double Pdc)
Update MMC operating point and controller references.
Definition MMC.cpp:357
double last_vMDelta_d_ref_
Definition MMC.h:199
Eigen::MatrixXd computePlantJacobian(double w, double mDd, double mDq, double mDZd, double mDZq, double mSd, double mSq, double mSz) const
Exact 12×12 plant Jacobian (modulation treated as fixed parameters)
Definition MMC.cpp:1024
double last_vMSigma_q_ref_
Definition MMC.h:202
void init_Controller(const std::vector< double > &converter_params)
Initialize the controller(s) in MMC using provided parameters.
Definition MMC.cpp:199
virtual void solveEquilibrium() override
Solve for the steady-state operating point x using Newton-Raphson.
Definition MMC.cpp:1435
std::vector< RCP< const Basic > > getVirtualInputSymbols() const override
Return symbolic names for virtual inputs used in stamping.
Definition MMC.cpp:1712
double last_vMSigma_d_ref_
Definition MMC.h:201
void writeMNAmatrix(SymEngine::DenseMatrix &, std::unordered_map< Bus *, int > &, int, std::map< Element *, std::vector< RCP< const Basic > > > &) override
Stamp this element into the symbolic MNA matrix (override in derived classes).
Definition MMC.cpp:1722
std::vector< std::vector< complex< double > > > compute_y_parameters(double frequency) override
Compute the numerical admittance (Y) parameters for the MMC.
Definition MMC.cpp:1639
std::vector< MatrixXcd > simulateInputStep(const std::vector< MatrixXcd > &states, int nKeep) const override
Simulate a step response given input states (override in dynamic elements).
Definition MMC.cpp:1827
void computeABCD_analytical()
computeABCD variant: exact plant block + numerical controller block
Definition MMC.cpp:1284
void computePowerFlow(std::map< std::string, double > &data, std::map< std::string, double > &globalParams) const override
Compute branch power-flow quantities (override in derived classes).
Definition MMC.h:114
bool hasGfm() const
True when GFM outer-loop states are enabled.
Definition MMC.h:83
void setGfmDroops(double Kdroop_P, double Kdroop_Q)
Set GFM droops (Kdroop_P, Kdroop_Q); no-op if GFM disabled.
Definition MMC.cpp:431
bool dqsym_initialized_
Definition MMC.h:195
~MMC() override=default
map_basic_basic getParameterSubstitutions() const override
Return parameter substitutions for symbolic evaluation.
Definition MMC.cpp:1865