results.py 9.82 KB
Newer Older
martin.moraga's avatar
martin.moraga committed
1
import cmath
martin.moraga's avatar
martin.moraga committed
2
import numpy as np
3
from villas.dataprocessing.readtools import read_timeseries_dpsim
4

Jan Dinkelbach's avatar
Jan Dinkelbach committed
5

martin.moraga's avatar
martin.moraga committed
6
class ResultsNode():
Jan Dinkelbach's avatar
Jan Dinkelbach committed
7
8
9
10
11
12
13
14
15
    def __init__(self, topo_node):
        self.topology_node = topo_node
        self.voltage = complex(0, 0)
        self.current = complex(0, 0)
        self.power = complex(0, 0)
        self.voltage_pu = complex(0, 0)
        self.current_pu = complex(0, 0)
        self.power_pu = complex(0, 0)

martin.moraga's avatar
martin.moraga committed
16
17
18
19
20
21
22
    def __str__(self):
        str = 'class=Node\n'
        attributes = self.__dict__
        for key in attributes.keys():
            str = str + key + '={}\n'.format(attributes[key])
        return str

martin.moraga's avatar
martin.moraga committed
23
24

class ResultsBranch():
Jan Dinkelbach's avatar
Jan Dinkelbach committed
25
26
27
28
29
30
31
32
33
    def __init__(self, topo_branch):
        self.topology_branch = topo_branch
        self.current = complex(0, 0)
        self.power = complex(0, 0)  # complex power flow at branch, measured at initial node
        self.power2 = complex(0, 0)  # complex power flow at branch, measured at final node
        self.current_pu = complex(0, 0)
        self.power_pu = complex(0, 0)  # complex power flow at branch, measured at initial node
        self.power2_pu = complex(0, 0)  # complex power flow at branch, measured at final node

martin.moraga's avatar
martin.moraga committed
34
35
36
37
38
39
40
    def __str__(self):
        str = 'class=Branch\n'
        attributes = self.__dict__
        for key in attributes.keys():
            str = str + key + '={}\n'.format(attributes[key])
        return str

Jan Dinkelbach's avatar
Jan Dinkelbach committed
41
42
43
44
45
46
47
48

class Results():
    def __init__(self, system):
        self.nodes = []
        self.branches = []
        self.Ymatrix = system.Ymatrix
        self.Adjacencies = system.Adjacencies
        for node in system.nodes:
49
50
            if node.ideal_connected_with == '':
                self.nodes.append(ResultsNode(topo_node=node))
Jan Dinkelbach's avatar
Jan Dinkelbach committed
51
52
53
        for branch in system.branches:
            self.branches.append(ResultsBranch(topo_branch=branch))

martin.moraga's avatar
martin.moraga committed
54
55
56
57
58
59
60
61
62
63
    def get_node_by_index(self, index):
        """
        return the node with node.index==index
        """
        for node in self.nodes:
            if (node.topology_node.index==index):
                return node
        
        return None
    
Jan Dinkelbach's avatar
Jan Dinkelbach committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    def read_data_dpsim(self, file_name, pu=False):
        """
        read the voltages from a dpsim input file

        @param file_name
        @param pu: - True if voltages are expressed in per unit system
        """
        loadflow_results = read_timeseries_dpsim(file_name, print_status=False)

        if pu == True:
            for node in self.nodes:
                node.voltage_pu = loadflow_results[node.topology_node.uuid].values[0]
                node.voltage = node.voltage_pu * node.topology_node.baseVoltage
        elif pu == False:
            for node in self.nodes:
                node.voltage = loadflow_results[node.topology_node.uuid].values[0]
                node.voltage_pu = node.voltage / node.topology_node.baseVoltage

    def load_voltages(self, V):
        """
        load the voltages of V-array (result of powerflow_cim.solve)
        """
        for index in range(len(V)):
martin.moraga's avatar
martin.moraga committed
87
88
89
            node = self.get_node_by_index(index)
            node.voltage_pu = V[index]
            node.voltage = node.voltage_pu * node.topology_node.baseVoltage
90
    
Jan Dinkelbach's avatar
Jan Dinkelbach committed
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
    def calculate_all(self):
        """
        calculate all quantities of the grid
        """
        self.calculateI()
        self.calculateIinj()
        self.calculateSinj()
        self.calculateIinj()
        self.calculateS1()
        self.calculateS2()

    def calculateI(self):
        """
        To calculate the branch currents
        """
        for branch in self.branches:
            fr = branch.topology_branch.start_node.index
            to = branch.topology_branch.end_node.index
            branch.current_pu = - (self.nodes[fr].voltage_pu - self.nodes[to].voltage_pu) * self.Ymatrix[fr][to]
            branch.current = branch.current_pu * branch.topology_branch.base_current

    def calculateIinj(self):
        """
        calculate current injections at a node
        """
        for node in self.nodes:
            to = complex(0, 0)  # sum of the currents flowing to the node
            fr = complex(0, 0)  # sum of the currents flowing from the node
            for branch in self.branches:
                if node.topology_node.index == branch.topology_branch.start_node.index:
                    fr = fr + branch.current_pu
                if node.topology_node.index == branch.topology_branch.end_node.index:
                    to = to + branch.current_pu
            node.current_pu = to - fr
            node.current = node.current_pu * node.topology_node.base_current

    def calculateSinj(self):
        """
        calculate power injection at a node
        """
        for node in self.nodes:
            node.power_pu = node.voltage_pu * np.conj(node.current_pu)
            node.power = node.power_pu * node.topology_node.base_apparent_power

    def calculateS1(self):
        """
        calculate complex power flow at branch, measured at initial node
        """
        for branch in self.branches:
            branch_index = branch.topology_branch.start_node.index
            for node in self.nodes:
                if branch_index == node.topology_node.index:
                    branch.power_pu = node.voltage_pu * (np.conj(branch.current_pu))
                    branch.power = branch.power_pu * branch.topology_branch.base_apparent_power

    def calculateS2(self):
        """
        calculate complex ower flow at branch, measured at final node
        """
        for branch in self.branches:
            branch_index = branch.topology_branch.end_node.index
            for node in self.nodes:
                if branch_index == node.topology_node.index:
                    branch.power2_pu = -node.voltage_pu * (np.conj(branch.current_pu))
                    branch.power = branch.power2_pu * branch.topology_branch.base_apparent_power

    def get_node(self, index=None, uuid=None):
        """
        returns a PowerflowNode with a certain uuid or a certain index (not both!):
        - if index in not None --> return the PowerflowNode with PowerflowNode.topology_node.index == index
        - if uuid in not None --> return the PowerflowNode with PowerflowNode.topology_node.uuid == uuid
        """
        if index is not None:
            for node in self.nodes:
                if index == node.topology_node.index:
                    return node
        elif uuid is not None:
            for node in self.nodes:
                if uuid == node.topology_node.uuid:
                    return node

    def get_branch(self, uuid):
        """
        returns a PowerflowBranch with a certain uuid
        """
        for branch in self.branches:
            if uuid == branch.topology_branch.uuid:
                return branch

    def get_voltages(self, pu=True):
        """
        get node voltages
        - if pu==True --> voltages are expressed as per-unit
        """
        voltages = np.zeros(len(self.nodes), dtype=np.complex_)
        if pu == True:
            for node in self.nodes:
                voltages[node.topology_node.index] = node.voltage_pu
        elif pu == False:
            for node in self.nodes:
                voltages[node.topology_node.index] = node.voltage

        return voltages

    def get_Iinj(self, pu=True):
        """
        get node currents
        - if pu==True --> voltages are expressed as per-unit
        """
        Iinj = np.zeros(len(self.nodes), dtype=np.complex_)
        if pu == True:
            for node in self.nodes:
                Iinj[node.topology_node.index] = node.current_pu
        elif pu == False:
            for node in self.nodes:
                Iinj[node.topology_node.index] = node.current

        return Iinj

    def get_Sinj(self, pu=True):
        """
        get node power
        - if pu==True --> voltages are expressed as per-unit
        """
        Sinj = np.zeros(len(self.nodes), dtype=np.complex_)
        if pu == True:
            for node in self.nodes:
                Sinj[node.topology_node.index] = node.power_pu
        elif pu == False:
            for node in self.nodes:
                Sinj[node.topology_node.index] = node.power

        return Sinj

    def getI(self, pu=True):
        """
        get branch currents
        - if pu==True --> voltages are expressed as per-unit
        """
        I = np.zeros(len(self.branches), dtype=np.complex_)
        if pu == True:
            for branch_idx in range(len(self.branches)):
                I[branch_idx] = self.branches[branch_idx].current_pu
        elif pu == False:
            for branch_idx in range(len(self.branches)):
236
                I[branch_idx] = self.branches[branch_idx].current
Jan Dinkelbach's avatar
Jan Dinkelbach committed
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

        return I

    def get_S1(self, pu=True):
        """
        get complex Power flow at branch, measured at initial node
        - if pu==True --> voltages are expressed as per-unit
        """
        S1 = np.zeros(len(self.branches), dtype=np.complex_)
        if pu == True:
            for branch_idx in range(len(self.branches)):
                S1[branch_idx] = self.branches[branch_idx].power_pu
        elif pu == False:
            for branch_idx in range(len(self.branches)):
                S1[branch_idx] = self.branches[branch_idx].power

        return S1

    def get_S2(self, pu=True):
        """
        get complex Power flow at branch, measured at final node
        - if pu==True --> voltages are expressed as per-unit
        """
        S2 = np.zeros(len(self.branches), dtype=np.complex_)
        if pu == True:
            for branch_idx in range(len(self.branches)):
                S2[branch_idx] = self.branches[branch_idx].power2_pu
        elif pu == False:
            for branch_idx in range(len(self.branches)):
                S2[branch_idx] = self.branches[branch_idx].power2

        return S2

    def print_voltages_polar(self):
        """
        for test purposes
        """
        for node in self.nodes:
            print(node.topology_node.uuid + " = " + str(cmath.polar(node.voltage)))