tops 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. tops-0.2.0/LICENSE +9 -0
  2. tops-0.2.0/PKG-INFO +50 -0
  3. tops-0.2.0/README.md +23 -0
  4. tops-0.2.0/pyproject.toml +29 -0
  5. tops-0.2.0/setup.cfg +4 -0
  6. tops-0.2.0/src/tops/dyn_models/__init__.py +11 -0
  7. tops-0.2.0/src/tops/dyn_models/avr.py +174 -0
  8. tops-0.2.0/src/tops/dyn_models/blocks.py +368 -0
  9. tops-0.2.0/src/tops/dyn_models/gen.py +290 -0
  10. tops-0.2.0/src/tops/dyn_models/gov.py +149 -0
  11. tops-0.2.0/src/tops/dyn_models/lines.py +158 -0
  12. tops-0.2.0/src/tops/dyn_models/loads.py +134 -0
  13. tops-0.2.0/src/tops/dyn_models/pll.py +66 -0
  14. tops-0.2.0/src/tops/dyn_models/pss.py +43 -0
  15. tops-0.2.0/src/tops/dyn_models/shunts.py +29 -0
  16. tops-0.2.0/src/tops/dyn_models/trafos.py +203 -0
  17. tops-0.2.0/src/tops/dyn_models/utils.py +355 -0
  18. tops-0.2.0/src/tops/dyn_models/vsc.py +73 -0
  19. tops-0.2.0/src/tops/dynamic.py +514 -0
  20. tops-0.2.0/src/tops/modal_analysis.py +272 -0
  21. tops-0.2.0/src/tops/plotting.py +85 -0
  22. tops-0.2.0/src/tops/ps_models/__init__.py +0 -0
  23. tops-0.2.0/src/tops/ps_models/ieee39.py +198 -0
  24. tops-0.2.0/src/tops/ps_models/ieee39_all_ctrl.py +201 -0
  25. tops-0.2.0/src/tops/ps_models/ieee68.py +318 -0
  26. tops-0.2.0/src/tops/ps_models/k2a.py +93 -0
  27. tops-0.2.0/src/tops/ps_models/k2a_val.py +93 -0
  28. tops-0.2.0/src/tops/ps_models/n44.py +260 -0
  29. tops-0.2.0/src/tops/ps_models/sm_ib.py +41 -0
  30. tops-0.2.0/src/tops/ps_models/sm_ib_kundur.py +41 -0
  31. tops-0.2.0/src/tops/ps_models/sm_load.py +30 -0
  32. tops-0.2.0/src/tops/solvers.py +112 -0
  33. tops-0.2.0/src/tops/utility_functions.py +224 -0
  34. tops-0.2.0/src/tops.egg-info/PKG-INFO +50 -0
  35. tops-0.2.0/src/tops.egg-info/SOURCES.txt +36 -0
  36. tops-0.2.0/src/tops.egg-info/dependency_links.txt +1 -0
  37. tops-0.2.0/src/tops.egg-info/requires.txt +4 -0
  38. tops-0.2.0/src/tops.egg-info/top_level.txt +1 -0
tops-0.2.0/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022-present Hallvar Haugdal <hallvar.haugdal@sintef.no>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
tops-0.2.0/PKG-INFO ADDED
@@ -0,0 +1,50 @@
1
+ Metadata-Version: 2.1
2
+ Name: tops
3
+ Version: 0.2.0
4
+ Summary: Tiny open power system simulator
5
+ Author-email: Hallvar Haugdal <hallvar.haugdal@sintef.no>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2022-present Hallvar Haugdal <hallvar.haugdal@sintef.no>
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+
16
+ Project-URL: Homepage, https://github.com/hallvar-h/TOPS
17
+ Project-URL: Bug Tracker, https://github.com/hallvar-h/TOPS/issues
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Operating System :: OS Independent
20
+ Requires-Python: >=3.7
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: numpy
24
+ Requires-Dist: scipy
25
+ Requires-Dist: matplotlib
26
+ Requires-Dist: pandas
27
+
28
+ # TOPS (Tiny open power system simulator)
29
+ This is a package for performing dynamic power system simulations in Python. The aim is to provide a simple and lightweight tool which is easy to install, run and modify, to be used by researchers and in education. Performance is not the main priority. The only dependencies are numpy, scipy, pandas and matplotlib (the core functionality only uses numpy and scipy).
30
+
31
+ The package is being developed as part of ongoing research, and thus contains experimental features. Use at your own risk!
32
+
33
+ Some features:
34
+ - Newton-Rhapson power flow
35
+ - Dynamic time domain simulation (RMS/phasor approximation)
36
+ - Linearization, eigenvalue analysis/modal analysis
37
+
38
+ # Installation
39
+ The package can be installed using pip, as follows:
40
+
41
+ `pip install git+https://github.com/hallvar-h/tops`
42
+
43
+ # Citing
44
+ If you use this code for your research, please cite [this paper](https://arxiv.org/abs/2101.02937).
45
+
46
+ # Example notebooks
47
+ [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/hallvar-h/TOPS/HEAD?filepath=examples%2Fnotebooks)
48
+
49
+ # Contact
50
+ [Hallvar Haugdal](mailto:hallvhau@gmail.com)
tops-0.2.0/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # TOPS (Tiny open power system simulator)
2
+ This is a package for performing dynamic power system simulations in Python. The aim is to provide a simple and lightweight tool which is easy to install, run and modify, to be used by researchers and in education. Performance is not the main priority. The only dependencies are numpy, scipy, pandas and matplotlib (the core functionality only uses numpy and scipy).
3
+
4
+ The package is being developed as part of ongoing research, and thus contains experimental features. Use at your own risk!
5
+
6
+ Some features:
7
+ - Newton-Rhapson power flow
8
+ - Dynamic time domain simulation (RMS/phasor approximation)
9
+ - Linearization, eigenvalue analysis/modal analysis
10
+
11
+ # Installation
12
+ The package can be installed using pip, as follows:
13
+
14
+ `pip install git+https://github.com/hallvar-h/tops`
15
+
16
+ # Citing
17
+ If you use this code for your research, please cite [this paper](https://arxiv.org/abs/2101.02937).
18
+
19
+ # Example notebooks
20
+ [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/hallvar-h/TOPS/HEAD?filepath=examples%2Fnotebooks)
21
+
22
+ # Contact
23
+ [Hallvar Haugdal](mailto:hallvhau@gmail.com)
@@ -0,0 +1,29 @@
1
+ [build-system]
2
+ requires = ["setuptools"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "tops"
7
+ description = 'Tiny open power system simulator'
8
+ readme = "README.md"
9
+ requires-python = ">=3.7"
10
+ license = {file="LICENSE"}
11
+ version = "0.2.0"
12
+ keywords = []
13
+ authors = [
14
+ { name = "Hallvar Haugdal", email = "hallvar.haugdal@sintef.no" },
15
+ ]
16
+ classifiers = [
17
+ "Programming Language :: Python :: 3",
18
+ "Operating System :: OS Independent",
19
+ ]
20
+ dependencies = [
21
+ "numpy",
22
+ "scipy",
23
+ "matplotlib",
24
+ "pandas"
25
+ ]
26
+
27
+ [project.urls]
28
+ "Homepage" = "https://github.com/hallvar-h/TOPS"
29
+ "Bug Tracker" = "https://github.com/hallvar-h/TOPS/issues"
tops-0.2.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,11 @@
1
+ from . import gen
2
+ from . import loads
3
+ from . import lines
4
+ from . import trafos
5
+ from . import shunts
6
+ from . import avr
7
+ from . import pss
8
+ from . import gov
9
+ from . import utils
10
+ from . import pll
11
+ from . import vsc
@@ -0,0 +1,174 @@
1
+ from tops.dyn_models.blocks import *
2
+ from tops.dyn_models.utils import auto_init
3
+
4
+
5
+ class AVR:
6
+ def connections(self):
7
+ return [
8
+ {
9
+ 'input': 'v_t',
10
+ 'source': {
11
+ 'container': 'gen',
12
+ 'mdl': '*',
13
+ 'id': self.par['gen'],
14
+ },
15
+ 'output': 'v_t_abs',
16
+ },
17
+ {
18
+ 'input': 'v_setp',
19
+ 'source': {
20
+ 'container': 'gen',
21
+ 'mdl': '*',
22
+ 'id': self.par['gen'],
23
+ },
24
+ 'output': 'v_setp',
25
+ },
26
+ {
27
+ 'input': 'v_pss',
28
+ 'source': {
29
+ 'container': 'gen',
30
+ 'mdl': '*',
31
+ 'id': self.par['gen'],
32
+ },
33
+ 'output': 'v_pss',
34
+ },
35
+ {
36
+ 'output': 'output',
37
+ 'destination': {
38
+ 'container': 'gen',
39
+ 'mdl': '*',
40
+ 'id': self.par['gen'],
41
+ },
42
+ 'input': 'E_f',
43
+ }
44
+ ]
45
+
46
+
47
+ class SEXS(DAEModel, AVR):
48
+ def input_list(self):
49
+ return ['v_setp', 'v_t', 'v_pss']
50
+
51
+ def add_blocks(self):
52
+ p = self.par
53
+ self.tg_red = LeadLag(T_1=p['T_a'], T_2=p['T_b'])
54
+ self.gain = Gain(K=p['K'])
55
+ self.time_constant_lim = TimeConstantLims(T=p['T_e'], V_min=p['E_min'], V_max=p['E_max'])
56
+ self.tg_red.input = lambda x, v: self.v_setp(x, v) - self.v_t(x, v) + self.v_pss(x, v) + self.int_par['bias']
57
+ self.gain.input = lambda x, v: self.tg_red.output(x, v)
58
+ self.time_constant_lim.input = lambda x, v: self.gain.output(x, v)
59
+ self.output = lambda x, v: self.time_constant_lim.output(x, v)
60
+
61
+ def int_par_list(self):
62
+ return ['bias']
63
+
64
+ def init_from_connections(self, x0, v0, output_0):
65
+ # auto_init(self, x0, v0, output_0['output'])
66
+ self.int_par['bias'] = \
67
+ self.tg_red.initialize(
68
+ x0, v0, self.gain.initialize(
69
+ x0, v0, self.time_constant_lim.initialize(
70
+ x0, v0, output_0['output'])
71
+ )
72
+ )
73
+
74
+
75
+
76
+ class SEXS_PI(DAEModel, AVR):
77
+ def input_list(self):
78
+ return ['v_setp', 'v_t', 'v_pss']
79
+
80
+ def add_blocks(self):
81
+ p = self.par
82
+ self.pi_regulator = PIRegulator(K_p=p['K_p'], K_i=p['K_i'])
83
+ self.tg_red = LeadLag(T_1=p['T_a'], T_2=p['T_b'])
84
+ self.gain = Gain(K=p['K'])
85
+ self.time_constant_lim = TimeConstantLims(T=p['T_e'], V_min=p['E_min'], V_max=p['E_max'])
86
+
87
+ self.v_setp_lag = TimeConstant(T=p['T_ext'])
88
+ self.v_setp_lag.input = lambda x, v: self.v_setp(x, v)
89
+
90
+ self.pi_regulator.input = lambda x, v: self.v_setp_lag.output(x, v) - self.v_t(x, v) + self.v_pss(x, v) # + self.int_par['bias']
91
+ self.tg_red.input = lambda x, v: self.pi_regulator.output(x, v)
92
+ self.gain.input = lambda x, v: self.tg_red.output(x, v)
93
+ self.time_constant_lim.input = lambda x, v: self.gain.output(x, v)
94
+ self.output = lambda x, v: self.time_constant_lim.output(x, v)
95
+
96
+ # def int_par_list(self):
97
+ # return ['bias']
98
+
99
+ def init_from_connections(self, x0, v0, output_0):
100
+ # v_0 = np.ones(self.n_units)
101
+ v_0 = self.v_setp(x0, v0)
102
+ self.v_setp_lag.initialize(x0, v0, v_0)
103
+ self.pi_regulator.initialize(
104
+ x0, v0, self.tg_red.initialize(
105
+ x0, v0, self.gain.initialize(
106
+ x0, v0, self.time_constant_lim.initialize(
107
+ x0, v0, output_0['output']
108
+ )
109
+ )
110
+ )
111
+ )
112
+
113
+
114
+ class IEEET1(DAEModel, AVR):
115
+ '''
116
+ Saturation not yet included!
117
+ '''
118
+ def input_list(self):
119
+ return ['v_setp', 'v_t', 'v_pss']
120
+
121
+ def add_blocks(self):
122
+ p = self.par
123
+ self.time_constant_Tr = TimeConstant(T=p['T_r'])
124
+ self.time_constant_gain_Ka_Ta = TimeConstantGainLims(K=p['K_a'], T=p['T_a'], V_min=p['V_rmin'], V_max=p['V_rmax'])
125
+ self.time_constant_gain_Ke_Te = TimeConstantVar(K=p['K_e'], T=p['T_e'])
126
+ # self.saturation = Saturation(E_1=p['E_1'], S_e1=p['S_e1'], E_2=p['E_2'], S_e2=p['S_e2'])
127
+ self.diff_Kf_Tf = WashoutGain(K=p['K_f'], T_w=p['T_f'])
128
+
129
+ self.time_constant_Tr.input = lambda x, v: self.v_t(x, v)
130
+ self.v_error = lambda x, v: self.v_setp(x, v) - self.time_constant_Tr.output(x, v) + self.v_pss(x, v) + self.int_par['bias']
131
+ self.time_constant_gain_Ka_Ta.input = lambda x, v: self.v_error(x, v) - self.diff_Kf_Tf.output(x, v)
132
+ self.time_constant_gain_Ke_Te.input = lambda x, v: self.time_constant_gain_Ka_Ta.output(x, v) # + self.saturation.output(x, v)
133
+ self.diff_Kf_Tf.input = self.time_constant_gain_Ke_Te.output
134
+ # self.saturation.input = lambda x, v: self.time_constant_gain_Ke_Te.output(x, v)*0
135
+
136
+ self.output = self.time_constant_gain_Ke_Te.output
137
+
138
+ def int_par_list(self):
139
+ return ['bias']
140
+
141
+ def init_from_connections(self, x0, v0, output_0):
142
+ auto_init(self, x0, v0, output_0['output'])
143
+
144
+
145
+ class SCRX(DAEModel, AVR):
146
+ '''
147
+ Missing negative current logic!
148
+ '''
149
+ def input_list(self):
150
+ return ['v_setp', 'v_t', 'v_pss']
151
+
152
+ def add_blocks(self):
153
+ p = self.par
154
+
155
+ self.lead_lag_Tb_Ta = LeadLag(T_1=p['T_a'], T_2=p['T_b'])
156
+ self.time_constant_gain_K_Te = TimeConstantGainLims(K=p['K'], T=p['T_e'], V_min=p['E_min'], V_max=p['E_max'])
157
+
158
+ self.v_error = lambda x, v: self.v_setp(x, v) - self.v_t(x, v) + self.v_pss(x, v) + self.int_par['bias']
159
+ self.lead_lag_Tb_Ta.input = self.v_error
160
+ self.time_constant_gain_K_Te.input = self.lead_lag_Tb_Ta.output
161
+
162
+ def current_logic_block(x, v):
163
+ bus_fed_idx = p['C_switch'] == 0
164
+ out_signal = self.time_constant_gain_K_Te.output(x, v)
165
+ out_signal[bus_fed_idx] *= self.v_t(x, v)[bus_fed_idx]
166
+ return out_signal
167
+
168
+ self.output = current_logic_block
169
+
170
+ def int_par_list(self):
171
+ return ['bias']
172
+
173
+ def init_from_connections(self, x0, v0, output_0):
174
+ auto_init(self, x0, v0, output_0['output'])
@@ -0,0 +1,368 @@
1
+ from tops.dyn_models.utils import DAEModel, output
2
+ import numpy as np
3
+
4
+
5
+ class PIRegulator(DAEModel):
6
+
7
+ def add_blocks(self):
8
+ self.integrator = Integrator(n_units=self.n_units)
9
+ self.integrator.input = lambda x, v: self.input(x, v)
10
+
11
+ @output
12
+ def output(self, x, v):
13
+ # X = self.local_view(x)
14
+ return self.par['K_p']*self.input(x, v) + self.par['K_i']*self.integrator.output(x, v)
15
+
16
+ def initialize(self, x0, v0, output_value):
17
+ input_value = self.integrator.initialize(x0, v0, output_value/self.par['K_i'])
18
+ return input_value
19
+
20
+
21
+ class Integrator(DAEModel):
22
+ def state_list(self):
23
+ return ['x_i']
24
+
25
+ @output
26
+ def output(self, x, v):
27
+ X = self.local_view(x)
28
+ return X['x_i']
29
+
30
+ def state_derivatives(self, dx, x, v):
31
+ dX = self.local_view(dx)
32
+ X = self.local_view(x)
33
+
34
+ dX['x_i'][:] = self.input(x, v)
35
+
36
+ def initialize(self, x0, v0, output_value):
37
+ X0 = self.local_view(x0)
38
+ X0['x_i'][:] = output_value
39
+ input_value = output_value
40
+ return input_value*0
41
+
42
+
43
+ class Integrator2(Integrator):
44
+ """
45
+ Same as Integrator, but with time constant.
46
+ """
47
+ @output
48
+ def output(self, x, v):
49
+ X = self.local_view(x)
50
+ return X['x_i']/self.par['T']
51
+
52
+ def initialize(self, x0, v0, output_value):
53
+ X0 = self.local_view(x0)
54
+ X0['x_i'][:] = output_value*self.par['T']
55
+ input_value = output_value*self.par['T']
56
+ return input_value*0
57
+
58
+
59
+ class Gain(DAEModel):
60
+ @output
61
+ def output(self, x, v):
62
+ # X = self.local_view(x)
63
+ return self.par['K']*self.input(x, v)
64
+
65
+ def initialize(self, x0, v0,output_value):
66
+ return output_value/self.par['K']
67
+
68
+
69
+ class Limiter(DAEModel):
70
+ @output
71
+ def output(self, x, v):
72
+ # X = self.local_view(x)
73
+ return np.minimum(np.maximum(self.input(x, v), self.par['Min']), self.par['Max'])
74
+
75
+
76
+ class Washout(DAEModel):
77
+ def state_list(self):
78
+ return ['x']
79
+
80
+ @output
81
+ def output(self, x, v):
82
+ X = self.local_view(x)
83
+ return (self.input(x, v) - X['x'])/self.par['T_w']
84
+
85
+ def state_derivatives(self, dx, x, v):
86
+ dX = self.local_view(dx)
87
+ X = self.local_view(x)
88
+
89
+ dX['x'][:] = self.output(x, v)
90
+
91
+
92
+ class TimeConstant(DAEModel):
93
+ '''
94
+ ______________
95
+ | |
96
+ | 1 |
97
+ u ---->| ----------- |----> y
98
+ | 1 + s*T |
99
+ |_______________|
100
+
101
+
102
+ '''
103
+ def __init__(self, *args, **kwargs):
104
+ super().__init__(*args, **kwargs)
105
+ self.zero_idx = self.par['T']==0
106
+
107
+ def state_list(self):
108
+ return ['x']
109
+
110
+ @output
111
+ def output(self, x, v):
112
+ X = self.local_view(x)
113
+ out = X['x']
114
+ if np.any(self.zero_idx):
115
+ out[self.zero_idx] = self.input(x, v)[self.zero_idx]
116
+ return out
117
+
118
+ def initialize(self, x0, v0, output_value):
119
+ X0 = self.local_view(x0)
120
+ X0['x'][:] = output_value
121
+ return output_value
122
+
123
+ def state_derivatives(self, dx, x, v):
124
+ dX = self.local_view(dx)
125
+ X = self.local_view(x)
126
+
127
+ coeff = ~self.zero_idx/(self.par['T']+self.zero_idx) # 1/T if T is not zero, 0 otherw
128
+ dX['x'][:] = coeff*(self.input(x, v) - X['x'])
129
+
130
+
131
+ class TimeConstantVar(TimeConstant):
132
+ '''
133
+ ______________
134
+ | |
135
+ | 1 |
136
+ u ---->| ----------- |----> y
137
+ | K + s*T |
138
+ |_______________|
139
+
140
+
141
+ '''
142
+ def initialize(self, x0, v0, output_value):
143
+ X0 = self.local_view(x0)
144
+ X0['x'][:] = output_value
145
+ return self.par['K']*output_value
146
+
147
+ def state_derivatives(self, dx, x, v):
148
+ dX = self.local_view(dx)
149
+ X = self.local_view(x)
150
+ # Check if T=0?
151
+ dX['x'][:] = 1/self.par['T']*(self.input(x, v) - self.par['K']*X['x'])
152
+
153
+
154
+ class TimeConstantGain(TimeConstant):
155
+ def output(self, x, v):
156
+ return self.par['K']*super().output(x, v)
157
+
158
+ def initialize(self, x0, v0, output_value):
159
+ return super().initialize(x0, v0, output_value/self.par['K'])
160
+
161
+
162
+ class TimeConstantLims(DAEModel):
163
+ '''
164
+ ___ V_max
165
+ ________/_____
166
+ | |
167
+ | 1 |
168
+ u ---->| ----------- |----> y
169
+ | 1 + s*T_2 |
170
+ |_______________|
171
+ ___/
172
+ V_min
173
+
174
+ '''
175
+ def state_list(self):
176
+ return ['x']
177
+
178
+ @output
179
+ def output(self, x, v):
180
+ X = self.local_view(x)
181
+ return np.minimum(np.maximum(X['x'], self.par['V_min']), self.par['V_max'])
182
+
183
+ def initialize(self, x0, v0,output_value):
184
+ X0 = self.local_view(x0)
185
+ X0['x'][:] = np.minimum(np.maximum(output_value, self.par['V_min']), self.par['V_max'])
186
+ return output_value
187
+
188
+ def state_derivatives(self, dx, x, v):
189
+ dX = self.local_view(dx)
190
+ X = self.local_view(x)
191
+
192
+ dX['x'][:] = 1/self.par['T']*(self.input(x, v) - X['x'])
193
+
194
+ # Lims on state variable (clamping)
195
+ lower_lim_idx = (X['x'] <= self.par['V_min']) & (dX['x'] < 0)
196
+ dX['x'][lower_lim_idx] *= 0
197
+
198
+ upper_lim_idx = (X['x'] >= self.par['V_max']) & (dX['x'] > 0)
199
+ dX['x'][upper_lim_idx] *= 0
200
+
201
+ class TimeConstantGainLims(TimeConstantLims):
202
+ '''
203
+ ___ V_max
204
+ ________/_____
205
+ | |
206
+ | K |
207
+ u ---->| ----------- |----> y
208
+ | 1 + s*T_2 |
209
+ |_______________|
210
+ ___/
211
+ V_min
212
+
213
+ '''
214
+ def state_derivatives(self, dx, x, v):
215
+ dX = self.local_view(dx)
216
+ X = self.local_view(x)
217
+
218
+ dX['x'][:] = 1/self.par['T']*(self.par['K']*self.input(x, v) - X['x'])
219
+
220
+ # Lims on state variable (clamping)
221
+ lower_lim_idx = (X['x'] <= self.par['V_min']) & (dX['x'] < 0)
222
+ dX['x'][lower_lim_idx] *= 0
223
+
224
+ upper_lim_idx = (X['x'] >= self.par['V_max']) & (dX['x'] > 0)
225
+ dX['x'][upper_lim_idx] *= 0
226
+
227
+ def initialize(self, x0, v0, output_value):
228
+ return super().initialize(x0, v0, output_value/self.par['K'])
229
+
230
+ class LeadLag(DAEModel):
231
+ '''
232
+ _______________
233
+ | |
234
+ | 1 + s*T_1 |
235
+ u ---->| ----------- |----> y
236
+ | 1 + s*T_2 |
237
+ |_______________|
238
+
239
+ '''
240
+ def state_list(self):
241
+ return ['x']
242
+
243
+ @output
244
+ def output(self, x, v):
245
+ X = self.local_view(x)
246
+ return 1/self.par['T_2']*(self.par['T_1']*self.input(x, v) - X['x'])
247
+
248
+ def initialize(self, x0, v0,output_value):
249
+ X0 = self.local_view(x0)
250
+ p = self.par
251
+
252
+ X0['x'][:] = p['T_1']*output_value - p['T_2']*output_value
253
+ return output_value
254
+
255
+ def state_derivatives(self, dx, x, v):
256
+ dX = self.local_view(dx)
257
+ X = self.local_view(x)
258
+
259
+ dX['x'][:] = (self.par['T_1']/self.par['T_2'] - 1)*self.input(x, v) - 1/self.par['T_2']*X['x']
260
+
261
+
262
+ class PIRegulator2(DAEModel):
263
+ def state_list(self):
264
+ return ['x']
265
+
266
+ @output
267
+ def output(self, x, v):
268
+ X = self.local_view(x)
269
+ return 1/self.par['T_2']*(self.par['T_1']*self.input(x, v) + X['x'])
270
+
271
+ def state_derivatives(self, dx, x, v):
272
+ dX = self.local_view(dx)
273
+ X = self.local_view(x)
274
+ dX['x'][:] = self.input(x, v)
275
+
276
+ def initialize(self, x0, v0, output_value):
277
+ X = self.local_view(x0)
278
+ X['x'] = self.par['T_2']/self.par['T_1']*output_value
279
+ return np.zeros(self.n_units)
280
+
281
+
282
+ class WashoutGain(DAEModel):
283
+ '''
284
+ _______________
285
+ | |
286
+ | s*K |
287
+ u ---->| ----------- |----> y
288
+ | 1 + s*T |
289
+ |_______________|
290
+
291
+ sK/(1+sT) = y/u
292
+ u*sK = y + sTy
293
+ s(Ku - Ty) = y
294
+ x = Ku - Ty
295
+ sx = y
296
+ y = 1/T(Ku - x)
297
+ '''
298
+ def state_list(self):
299
+ return ['x']
300
+
301
+ @output
302
+ def output(self, x, v):
303
+ X = self.local_view(x)
304
+ return 1/self.par['T_w']*(self.par['K']*self.input(x, v) - X['x'])
305
+
306
+ def state_derivatives(self, dx, x, v):
307
+ dX = self.local_view(dx)
308
+ dX['x'][:] = self.output(x, v)
309
+
310
+ # def initialize(self, x0, v0, output_value):
311
+ # dx = 0 => x = Ku
312
+ # pass
313
+
314
+
315
+ class Saturation(DAEModel):
316
+ '''
317
+ Not verified!!
318
+ '''
319
+ @output
320
+ def output(self, x, v):
321
+ U = self.input(x, v)
322
+
323
+ E1 = self.par['E_1']
324
+ SE1 = self.par['S_e1']
325
+ E2 = self.par['E_2']
326
+ SE2 = self.par['S_e2']
327
+
328
+ sqrt = np.sqrt
329
+ K = SE1/SE2
330
+ A = sqrt(E1*E2)*(sqrt(E1) - sqrt(E2*K))/(sqrt(E2) - sqrt(E1*K))
331
+ B = SE2*(sqrt(E2) - sqrt(E1*K))**2/(E1 - E2)**2
332
+
333
+
334
+ with np.errstate(divide='ignore'):
335
+ SE = B*(U - A)**2/U
336
+ SE[U <= 0] = 0
337
+
338
+ return SE
339
+
340
+
341
+ class Backlash(DAEModel):
342
+ """Based on PowerFactory implementation"""
343
+ def state_list(self):
344
+ return ['x']
345
+
346
+ def state_derivatives(self, dx, x, v):
347
+ dX = self.local_view(dx)
348
+ X = self.local_view(x)
349
+ db = self.par['db']
350
+ if db <= 0:
351
+ return db*0
352
+
353
+ y = self.input(x, v)
354
+ x = X['x']
355
+
356
+ if (y - x) >= db:
357
+ d = (y - x) - db
358
+ elif (y - x) <= -db:
359
+ d = (y - x) + db
360
+ else:
361
+ d = 0
362
+
363
+ dX['x'][:] = d/0.01
364
+
365
+ @output
366
+ def output(self, x, v):
367
+ X = self.local_view(x)
368
+ return X['x']