summaryrefslogtreecommitdiffstats
path: root/Project/ImportForm.h
blob: 757077bccd4157087f544923a32f0cacb7a78603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 *  Copyright (C) 2018  Thales Lima Oliveira <thales@ufu.br>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program 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
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef IMPORTFORM_H
#define IMPORTFORM_H

#include "PropertiesFormBase.h"

#include <wx/geometry.h>
#include <wx/msgdlg.h>
#include <wx/textfile.h>
#include <wx/tokenzr.h>
#include <bitset>
#include <complex>

class Workspace;
class Bus;
class SyncGenerator;
class SyncMotor;
class Load;
class Capacitor;
class Inductor;
class Capacitor;
class IndMotor;
class Transformer;
class Line;
class PropertiesData;

class GraphAutoLayout;

/**
 * @class ImportForm
 * @author Thales Lima Oliveira <thales@ufu.br>
 * @date 27/03/2018
 * @brief Form to import other programs files to PSP
 * @file ImportForm.h
 */
class ImportForm : public ImportFormBase
{
   public:
    ImportForm(wxWindow* parent, Workspace* workspace);
    virtual ~ImportForm();

    Workspace* GetWorkspace() { return m_workspace; }

   protected:
    virtual void OnButtonCancelClick(wxCommandEvent& event);
    virtual void OnButtonOKClick(wxCommandEvent& event);
    bool ImportSelectedFiles();
    bool ImportCEPELFiles();
    bool ImportMatpowerFiles();
    Bus* GetBusFromID(std::vector<Bus*> busList, int id);

    Workspace* m_workspace = nullptr;
    wxWindow* m_parent;
};

/**
 * @enum ElementType
 * @brief ID of ANAREDE's elements.
 */
enum ElementTypeAnarede {
    ANA_BUS = 1,            /**< Bus */
    ANA_GENERATOR = 4,      /**< Generator */
    ANA_LOAD = 5,           /**< Load */
    ANA_SHUNT = 6,          /**< Shunt element */
    ANA_MIT = 7,            /**< Induction motor */
    ANA_TRANSFORMER = 9,    /**< Transformer */
    ANA_LINE = 14,          /**< Power line */
    ANA_IND_LOAD = 22,      /**< Independent load */
    ANA_IND_SHUNT = 23,     /**< Independent shunt element */
    ANA_IND_GENERATOR = 24, /**< Independent generator */
};

/**
 * @class ParseAnarede
 * @author Thales Lima Oliveira <thales@ufu.br>
 * @date 27/03/2018
 * @brief Class responsible to parse ANAREDE files to import data to PSP.
 * @file ImportForm.h
 */
class ParseAnarede
{
   public:
    // Graphic files data structs
    struct Component {
        int id = 0;                        /**< Graphical ID */
        ElementTypeAnarede type = ANA_BUS; /**< Element type */
        double length = 0.0;               /**< Element lenght (only buses) */
        int rotationID = 0;                /**< Rotation ID (0, 1, 2 or 4) */
        wxPoint2DDouble position;          /**< X and Y coordinates */
        int electricalID = 0;              /**< Bus, Branch or Group electrical IDs */
        std::pair<int, int> busConnectionID[2] = {
            std::make_pair(0, 0), std::make_pair(0, 0)}; /**< In the form <graphicalBusID,electricBusID> */
        std::pair<int, int> busConnectionNode[2] = {std::make_pair(0, 0),
                                                    std::make_pair(0, 0)}; /**< In the form <graphicalBusID,nodeID> */
    };
    struct PowerLine {
        int id = 0;                         /**< Graphical ID */
        ElementTypeAnarede type = ANA_LINE; /**< Element type */
        int electricalID = 0;               /**< Bus, Branch or Group electrical IDs */
        std::pair<int, int> busConnectionID[2] = {
            std::make_pair(0, 0), std::make_pair(0, 0)}; /**< In the form <graphicalBusID,electricBusID> */
        std::pair<int, int> busConnectionNode[2] = {std::make_pair(0, 0),
                                                    std::make_pair(0, 0)}; /**< In the form <graphicalBusID,nodeID> */
        std::vector<wxPoint2DDouble> nodesPosition; /**< Coordinates of the line breaks, if any */
    };
    struct BusData {
        int id = 0;                 /**< Bus electrical ID */
        bool isOnline = true;       /**< Element is online */
        int type = 0;               /**< Bus Type: 0 = PQ; 1 = PV; 2 = Ref.; 3 = PQ with voltage between */
        wxString voltageBase = "0"; /**< Voltage base identifier */
        wxString busName = "Bus";   /**< Bus name */
        double voltage = 1.0;       /**< Bus abs voltage (controlled value for PV and Ref. types) */
        double angle = 0.0;         /**< Angle of voltage */
        std::complex<double> genPower = std::complex<double>(0, 0);  /**< Generated power */
        double minReactivePower = -9999.0;                           /**< Minimal reactive power */
        double maxReactivePower = 99999.0;                           /**< Maximum reactive power */
        int ctrlBusID = 0;                                           /**< Controlled bus ID */
        std::complex<double> loadPower = std::complex<double>(0, 0); /**< Load power */
        double shuntReactive =
            0.0; /**< Reactive power of shunt element. Positive for capacitor, negative for inductor. */
    };
    struct BranchData {
        int id = 0;                                                /**< Branch electrical ID */
        ElementTypeAnarede type = ANA_LINE;                        /**< Element type */
        std::pair<int, int> busConnections = std::make_pair(0, 0); /**< Branch connection IDs */
        bool isOnline = true;                                      /**< Element is online */
        double resistance = 0.0;                                   /**< Branch resistance */
        double indReactance = 0.0;                                 /**< Branch inductive reactance */
        double capSusceptance = 0.0;                               /**< Branch capacitive susceptance */
        double tap = 0.0;                                          /**< Transformer tap */
        double phaseShift = 0.0;                                   /**< Transformer phase shift */
    };
    struct IndElementData {
        int id = 0;                                              /**< Group electrical ID */
        int busConnection = 0;                                   /**< Branch connection ID */
        ElementTypeAnarede type = ANA_IND_GENERATOR;             /**< Element type */
        bool isOnline = true;                                    /**< Element is online */
        std::complex<double> power = std::complex<double>(0, 0); /**< Element power */
        int numUnits = 0;                                        /**< Number of unities */
    };
    struct IndGenData : IndElementData {
        double minReactivePower = -9999.0; /**< Minimal reactive power */
        double maxReactivePower = 99999.0; /**< Maximum reactive power */
        double xt = 1.0;                   /**< Transformer reactance of each generator */
        double xd = 1.0;                   /**< Synchronous direct-axis reactance of each generator */
        double xq = 1.0;                   /**< Synchronous quadrature-axis reactance of each generator */
        double xl = 1.0;                   /**< Leakage reactance of each generator */
        double ratedPower = 100.0;         /**< Rated power of each generator */
    };

    ParseAnarede(wxFileName lstFile, wxFileName pwfFile);
    ~ParseAnarede() { ClearData(); }

    bool Parse();

    std::vector<Component*> GetComponents() const { return m_components; }
    std::vector<PowerLine*> GetLines() const { return m_lines; }
    std::vector<BranchData*> GetBranchData() const { return m_branchData; }
    std::vector<BusData*> GetBusData() const { return m_busData; }
    std::vector<IndElementData*> GetIndElementData() const { return m_indElementData; }
    wxString GetProjectName() const { return m_projectName; }
    double GetMVAPowerBase() const { return m_mvaBase; }

    wxPoint2DDouble GetNodePositionFromID(Bus* bus, double scale, int nodeID);
    BusData* GetBusDataFromID(int id);
    BranchData* GetBranchDataFromID(int id, int fromBus, int toBus, ElementTypeAnarede type);
    IndElementData* GetIndElementDataFromID(int id, int busID, ElementTypeAnarede type);

    void ClearData();

   protected:
    bool GetLenghtAndRotationFromBusCode(wxString code, double& lenght, int& rotationID);
    wxString GetLSTLineNextValue(wxString line, int& currentPos);
    bool StrToElementType(wxString strType, ElementTypeAnarede& type);
    bool ParsePWFExeCode(wxString data, wxString exeCode);
    bool GetPWFStructuredData(wxString data,
                              unsigned int startPos,
                              unsigned int dataLenght,
                              int& value,
                              int decimalPos = -1);
    bool GetPWFStructuredData(wxString data,
                              unsigned int startPos,
                              unsigned int dataLenght,
                              double& value,
                              int decimalPos = -1);

    wxFileName m_lstFile;
    wxFileName m_pwfFile;

    std::vector<Component*> m_components;
    std::vector<PowerLine*> m_lines;
    std::vector<BusData*> m_busData;
    std::vector<BranchData*> m_branchData;
    std::vector<IndElementData*> m_indElementData;

    wxString m_projectName = _("Imported project");
    double m_mvaBase = 100.0;
};

class ParseMatpower
{
   public:
    struct BusData {
        int id = 0;      /**< Bus electrical ID */
        int type = 0;    /**< Bus Type: 1 = PQ; 2 = PV; 3 = Ref.; 4 = isolated */
        double pd = 0.0; /**< Real power demand (MW) */
        double qd = 0.0; /**< Reactive power demand (MVAr) */
        double gs = 0.0; /**< Shunt condutance (MW, V = 1.0 p.u.) */
        double bs = 0.0; /**< Shunt susceptance (MVAr, V = 1.0 p.u.). Positive for capacitor, negative for inductor? */
        int area = 0;    /**< Bus area */
        double voltage = 1.0;     /**< Bus abs voltage (controlled value for PV and Ref. types) */
        double angle = 0.0;       /**< Angle of voltage */
        double baseVoltage = 138; /**< Base voltage (kV)*/
        wxString busName = "Bus"; /**< Bus name */
        wxPoint2DDouble busPosition = wxPoint2DDouble(0,0);  /**< Bus position */
    };
    struct GenData {
        int busID = 0;                     /**< Bus electrical ID */
        double pg = 0.0;                   /**< Real power output (MW) */
        double qg = 0.0;                   /**< Reactive power output (MVAr) */
        double maxReactivePower = 99999.0; /**< Maximum reactive power (MVAr) */
        double minReactivePower = -9999.0; /**< Minimal reactive power (MVAr) */
        double baseMVA = 100;              /**< Generator power base (MVA)*/
        bool isOnline = true; /**< Machine status (> 0 = machine in-service; <= 0 = machine out-of-service) */
    };
    struct BranchData {
        std::pair<int, int> busConnections = std::make_pair(0, 0); /**< Branch connection IDs */
        double resistance = 0.0;                                   /**< Branch resistance */
        double indReactance = 0.0;                                 /**< Branch inductive reactance */
        double capSusceptance = 0.0;                               /**< Branch capacitive susceptance */
        double tap = 0.0;        /**< Transformer tap. If equal zero the branch is a line element */
        double phaseShift = 0.0; /**< Transformer phase shift. Positive represents delay */
        bool isOnline = true;    /**< Element is online */
    };

    ParseMatpower(wxFileName mFile);
    ~ParseMatpower() { ClearData(); }

    void ClearData();

    bool Parse();
    
    std::vector<BranchData*> GetBranchData() const { return m_branchData; }
    std::vector<BusData*> GetBusData() const { return m_busData; }
    std::vector<GenData*> GetGenData() const { return m_genData; }
    double GetMVAPowerBase() const { return m_mvaBase; }
    
    BusData* GetBusDataFromID(int id);
    Bus* GetBusFromID(int id, std::vector<Bus*> busList);

   protected:
    wxStringTokenizer GetMFileTokenData(wxTextFile& mFile, wxString currentLine);
    wxFileName m_mFile;

    std::vector<BusData*> m_busData;
    std::vector<BranchData*> m_branchData;
    std::vector<GenData*> m_genData;

    wxString m_projectName = _("Imported project");
    double m_mvaBase = 100.0;
};

#endif  // IMPORTFORM_H