/
service.py
1360 lines (1047 loc) 路 41 KB
/
service.py
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# [ANDES] (C)2015-2020 Hantao Cui
#
# 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 3 of the License, or
# (at your option) any later version.
#
# File name: service.py
# Last modified: 8/16/20, 7:28 PM
from typing import Optional, Union, Callable, Type
from andes.core.param import BaseParam
from andes.utils.func import list_flatten
from andes.core.common import dummify
from andes.shared import np, ndarray
import logging
from collections import OrderedDict
from andes.utils.tab import Tab
logger = logging.getLogger(__name__)
class BaseService:
"""
Base class for Service.
Service is a v-provider type for holding internal and temporary values. Subclasses need to implement ``v``
as a member attribute or using a property decorator.
Parameters
----------
name : str
Instance name
Attributes
----------
owner : Model
The hosting/owner model instance
"""
def __init__(self, name: str = None, tex_name: str = None, info: str = None, vtype: Type = None):
self.name = name
self.tex_name = tex_name if tex_name else name
self.info = info
self.vtype = vtype if vtype is not None else np.float # type for `v`
self.owner = None
def get_names(self):
"""
Return `name` in a list
Returns
-------
list
A list only containing the name of the service variable
"""
return [self.name]
@property
def n(self):
"""
Return the count of values in ``self.v``.
Needs to be overloaded if ``v`` of subclasses is not a 1-dimensional array.
Returns
-------
int
The count of elements in this variable
"""
if isinstance(self.v, (list, np.ndarray)):
return len(self.v)
else:
return 1
@property
def class_name(self):
"""
Return the class name
"""
return self.__class__.__name__
def __repr__(self):
return f'{self.class_name}: {self.owner.class_name}.{self.name}'
class ConstService(BaseService):
"""
A type of Service that stays constant once initialized.
ConstService are usually constants calculated from parameters. They are only evaluated once in the
initialization phase before variables are initialized. Therefore, uninitialized variables must not be
used in `v_str``.
Parameters
----------
name : str
Name of the ConstService
v_str : str
An equation string to calculate the variable value.
v_numeric : Callable, optional
A callable which returns the value of the ConstService
Attributes
----------
v : array-like or a scalar
ConstService value
"""
def __init__(self,
v_str: Optional[str] = None,
v_numeric: Optional[Callable] = None,
vtype: Optional[type] = None,
name: Optional[str] = None, tex_name=None, info=None):
super().__init__(name=name, vtype=vtype, tex_name=tex_name, info=info)
self.v_str = v_str
self.v_numeric = v_numeric
self.v: Union[float, int, ndarray] = np.array([0.])
def assign_memory(self, n):
"""Assign memory for ``self.v`` and set the array to zero."""
self.v = np.zeros(n, dtype=self.vtype)
class VarService(ConstService):
"""
Variable service that gets updated in each step/loop as variables change.
This class is useful when one has non-differentiable algebraic equations,
which make use of `abs()`, `re` and `im`.
Instead of creating `Algeb`, one can put the equation in `VarService`,
which will be updated before solving algebraic equations.
Examples
--------
In ESST3A model, the voltage and current sensors (vd + jvq), (Id + jIq)
estimate the sensed VE using equation
.. math ::
VE = | K_{PC}*(v_d + 1j v_q) + 1j (K_I + K_{PC}*X_L)*(I_d + 1j I_q)|
One can use `VarService` to implement this equation ::
self.VE = VarService(tex_name='V_E',
info='VE',
v_str='Abs(KPC*(vd + 1j*vq) + 1j*(KI + KPC*XL)*(Id + 1j*Iq))',
)
Warnings
--------
`VarService` is not solved with other algebraic equations, meaning that
there is one step "delay" between the algebraic variables and `VarService`.
Use an algebraic variable whenever possible.
"""
pass
class EventFlag(VarService):
"""
Service to flag events.
`EventFlag.v` stores the values of the input variable from the previous iteration/step.
"""
def __init__(self,
u,
vtype: Optional[type] = None,
name: Optional[str] = None, tex_name=None, info=None):
VarService.__init__(self, v_numeric=self.check,
vtype=vtype, name=name, tex_name=tex_name, info=info)
self.u = dummify(u)
def check(self, **kwargs):
"""
Check status and set event flags.
"""
if not np.all(self.v == self.u.v):
self.owner.system.TDS.custom_event = True
logger.debug(f"Event flag set at t={self.owner.system.dae.t:.6f} sec.")
return self.u.v
class VarHold(VarService):
"""
Service for holding the input when the hold state is on.
"""
def __init__(self, u, hold, vtype=None, name=None, tex_name=None, info=None):
VarService.__init__(self, v_numeric=self.check, vtype=vtype,
name=name, tex_name=tex_name, info=info,
)
self.u = dummify(u)
self.hold = dummify(hold)
self._init = False
def check(self, **kwargs):
if not np.all(self.hold.v == 0.0):
hold_idx = np.where(self.hold.v == 1)
ret = self.u.v.copy()
ret[hold_idx] = self.v[hold_idx]
return ret
else:
return self.u.v
class ExtendedEvent(VarService):
"""
Service to flag events that extends for period of time after event disappears.
`EventFlag.v` stores the flags whether the extended time has completed.
Outputs will become 1 once then event starts until the extended time ends.
Warnings
--------
The performance of this class needs to be optimized.
Parameters
----------
trig : str, rise, fall
Triggering edge for the inception of an event. `rise` by default.
enable : bool or v-provider
If disabled, the output will be `v_disabled`
extend_only : bool
Only output during the extended period, not the event period.
"""
def __init__(self,
u,
t_ext: Union[int, float, BaseParam, BaseService] = 0.0,
trig: str = 'rise',
enable=True,
v_disabled=0,
extend_only=False,
vtype: Optional[type] = None,
name: Optional[str] = None, tex_name=None, info=None):
VarService.__init__(self, v_numeric=self.check,
vtype=vtype, name=name, tex_name=tex_name, info=info)
self.u = dummify(u)
self.t_ext = dummify(t_ext)
self.enable = dummify(enable)
self.v_disabled = v_disabled
self.extend_only = extend_only
self.t_final = None
self.trig = trig
self.v_event = None
self.u_last = None
self.z = None # if is in an extended event (from event start to extension end)
self.n_ext = 0 # number of extended events
def assign_memory(self, n):
"""
Assign memory for internal data.
"""
VarService.assign_memory(self, n)
self.t_final = np.zeros_like(self.v)
self.v_event = np.zeros_like(self.v)
self.u_last = np.zeros_like(self.v)
self.z = np.zeros_like(self.v)
if isinstance(self.t_ext.v, (int, float)):
self.t_ext.v = np.ones_like(self.u.v) * self.t_ext.v
def check(self, **kwargs):
"""
Check if an extended event is in place.
Supplied as a ``v_numeric`` to ``VarService``.
"""
dae_t = self.owner.system.dae.t
if dae_t == 0.0:
self.u_last[:] = self.u.v
self.v_event[:] = self.u.v
# when any input signal changes
if not np.all(self.u.v == self.u_last):
diff = self.u.v - self.u_last
# detect the actual ending of an event
if self.trig == 'rise':
starting = np.where(diff == 1)[0]
ending = np.where(diff == -1)[0]
else:
starting = np.where(diff == -1)[0]
ending = np.where(diff == 1)[0]
if len(starting):
self.z[starting] = 1
if not self.extend_only:
self.v_event[starting] = self.u.v[starting]
if len(ending):
if self.extend_only:
self.v_event[ending] = self.u_last[ending]
final_times = dae_t + self.t_ext.v[ending]
self.t_final[ending] = final_times
self.n_ext += len(ending)
# TODO: insert extended event end times to a model-level list
logger.debug(f"Extended Event ending time set at t={final_times} sec.")
# final time of the extended event
if self.n_ext and np.any(self.t_final <= dae_t):
self.z[np.where(self.t_final <= dae_t)] = 0
self.n_ext = np.count_nonzero(self.z)
self.u_last[:] = self.u.v
return self.enable.v * (self.u.v * (1 - self.z) + self.v_event * self.z) + \
(1-self.enable.v) * self.v_disabled
class PostInitService(ConstService):
"""
Constant service that gets stored once after init.
This service is useful when one need to store initialization
values stored in variables.
Examples
--------
In ESST3A model, the `vf` variable is initialized followed by other
variables. One can store the initial `vf` into `vf0` so that equation
``vf - vf0 = 0`` will hold. ::
self.vref0 = PostInitService(info='Initial reference voltage input',
tex_name='V_{ref0}',
v_str='vref',
)
Since all `ConstService` are evaluated before equation evaluation,
without using PostInitService, one will need to create lots
of `ConstService` to store values in the initialization path
towards `vf0`, in order to correctly initialize `vf`.
"""
pass
class BackRef(BaseService):
"""
A special type of reference collector.
`BackRef` is used for collecting device indices of other models referencing the parent model of the
`BackRef`. The `v``field will be a list of lists, each containing the `idx` of other models
referencing each device of the parent model.
BackRef can be passed as indexer for params and vars, or shape for `NumReduce` and
`NumRepeat`. See examples for illustration.
Examples
--------
A Bus device has an `IdxParam` of `area`, storing the `idx` of area to which the bus device belongs.
In ``Bus.__init__()``, one has ::
self.area = IdxParam(model='Area')
Suppose `Bus` has the following data
==== ==== ====
idx area Vn
---- ---- ----
1 1 110
2 2 220
3 1 345
4 1 500
==== ==== ====
The Area model wants to collect the indices of Bus devices which points to the corresponding Area device.
In ``Area.__init__``, one defines ::
self.Bus = BackRef()
where the member attribute name `Bus` needs to match exactly model name that `Area` wants to collect
`idx` for.
Similarly, one can define ``self.ACTopology = BackRef()`` to collect devices in the `ACTopology` group
that references Area.
The collection of `idx` happens in :py:func:`andes.system.System._collect_ref_param`.
It has to be noted that the specific `Area` entry must exist to collect model idx-dx referencing it.
For example, if `Area` has the following data ::
idx
1
Then, only Bus 1, 3, and 4 will be collected into `self.Bus.v`, namely, ``self.Bus.v == [ [1, 3, 4] ]``.
If `Area` has data ::
idx
1
2
Then, `self.Bus.v` will end up with ``[ [1, 3, 4], [2] ]``.
See Also
--------
andes.core.service.NumReduce : A more complete example using BackRef to build the COI model
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.export = False
self.v = list()
class ExtService(BaseService):
"""
Service constants whose value is from an external model or group.
Parameters
----------
src : str
Variable or parameter name in the source model or group
model : str
A model name or a group name
indexer : IdxParam or BaseParam
An "Indexer" instance whose ``v`` field contains the ``idx`` of devices in the model or group.
Examples
--------
A synchronous generator needs to retrieve the ``p`` and ``q`` values from static generators
for initialization. ``ExtService`` is used for this purpose.
In a synchronous generator, one can define the following to retrieve ``StaticGen.p`` as ``p0``::
class GENCLSModel(Model):
def __init__(...):
...
self.p0 = ExtService(src='p',
model='StaticGen',
indexer=self.gen,
tex_name='P_0')
"""
def __init__(self,
model: str,
src: str,
indexer: Union[BaseParam, BaseService],
attr: str = 'v',
allow_none: bool = False,
default=0,
name: str = None,
tex_name: str = None,
vtype=None,
info: str = None,
):
super().__init__(name=name, tex_name=tex_name, info=info, vtype=vtype)
self.model = model
self.src = src
self.indexer = indexer
self.attr = attr
self.allow_none = allow_none
self.default = default
self.v = np.array([0.])
def assign_memory(self, n):
"""Assign memory for ``self.v`` and set the array to zero."""
self.v = np.zeros(n, dtype=self.vtype)
def link_external(self, ext_model):
"""
Method to be called by ``System`` for getting values from the external model or group.
Parameters
----------
ext_model
An instance of a model or group provided by System
"""
# set initial v values to zero
self.v = np.zeros(self.n)
if self.n == 0:
return
# the same `get` api for Group and Model
self.v = ext_model.get(src=self.src, idx=self.indexer.v, attr=self.attr,
allow_none=self.allow_none,
default=self.default,
)
class DataSelect(BaseService):
"""
Class for selecting values for optional DataParam or NumParam.
This service is a v-provider that uses optional DataParam if available with a fallback.
DataParam will be tested for `None`, and NumParam will be tested with `np.isnan()`.
Notes
-----
An use case of DataSelect is remote bus. One can do ::
self.buss = DataSelect(option=self.busr, fallback=self.bus)
Then, pass ``self.buss`` instead of ``self.bus`` as indexer to retrieve voltages.
Another use case is to allow an optional turbine rating. One can do ::
self.Tn = NumParam(default=None)
self.Sg = ExtParam(...)
self.Sn = DataSelect(Tn, Sg)
"""
def __init__(self,
optional,
fallback,
name: Optional[str] = None,
tex_name: Optional[str] = None,
info: Optional[str] = None,
):
super().__init__(name=name, tex_name=tex_name, info=info, )
self.optional = optional
self.fallback = fallback
self._v = None
@property
def v(self):
if self._v is None:
self._v = [v1 if v1 is not None and not np.isnan(v1)
else v2
for v1, v2 in zip(self.optional.v, self.fallback.v)]
return self._v
class DeviceFinder(BaseService):
"""
Service for finding indices of optionally linked devices.
If not provided, `DeviceFinder` will add devices at the beginning of `System.setup`.
Examples
--------
IEEEST stabilizer takes an optional `busf` (IdxParam) for specifying the connected BusFreq,
which is needed for mode 6. To avoid reimplementing `BusFreq` within IEEEST, one can do
.. code-block :: python
self.busfreq = DeviceFinder(self.busf, link=self.buss, idx_name='bus')
where `self.busf` is the optional input, `self.buss` is the bus indices that `busf` should measure,
and `idx_name` is the name of a BusFreq parameter through which the measured bus indices are specified.
For each `None` values in `self.busf`, a `BusFreq` is created to measure the corresponding bus in `self.buss`.
That is, ``BusFreq.[idx_name].v = [link]``. `DeviceFinder` will find / create `BusFreq` devices so that
the returned list of `BusFreq` indices are connected to `self.buss`, respectively.
"""
def __init__(self, u, link, idx_name, name=None, tex_name=None, info=None):
super().__init__(name=name, tex_name=tex_name, info=info)
self.u = u
self.model = u.model
self.idx_name = idx_name
if self.model is None:
raise ValueError(f'{u.owner.class_name}.{u.name} must contain "model".')
self.link = link
def find_or_add(self, system):
mdl = system.models[self.model]
found_idx = mdl.find_idx((self.idx_name,), (self.link.v,),
allow_none=True, default=None)
action = False
for ii, idx in enumerate(found_idx):
if idx is None:
action = True
new_idx = system.add(self.model, {self.idx_name: self.link.v[ii]})
self.u.v[ii] = new_idx
logger.info(f"{self.owner.class_name} <{self.owner.idx.v[ii]}> "
f"added {self.model} <{new_idx}> "
f"on {self.idx_name} <{self.link.v[ii]}>")
else:
action = True
self.u.v[ii] = idx
if action:
mdl.list2array()
mdl.refresh_inputs()
@property
def v(self):
return self.u.v
class OperationService(BaseService):
"""
Base class for a type of Service which performs specific operations
This class cannot be used by itself.
See Also
--------
NumReduce : Service for Reducing linearly stored 2-D services into 1-D
NumRepeat : Service for repeating 1-D NumParam/ v-array following a sub-pattern
IdxRepeat : Service for repeating 1-D IdxParam/ v-list following a sub-pattern
"""
def __init__(self,
name=None,
tex_name=None,
info=None,
):
self._v = None
super().__init__(name=name, tex_name=tex_name, info=info, )
self.v_str = None
@property
def v(self):
"""
Return values stored in `self._v`. May be overloaded by subclasses.
"""
return self._v
@v.setter
def v(self, value):
self._v = value
class NumReduce(OperationService):
"""
A helper Service type which reduces a linearly stored 2-D ExtParam into 1-D Service.
NumReduce works with ExtParam whose `v` field is a list of lists. A reduce function
which takes an array-like and returns a scalar need to be supplied. NumReduce calls the reduce
function on each of the lists and return all the scalars in an array.
Parameters
----------
u : ExtParam
Input ExtParam whose ``v`` contains linearly stored 2-dimensional values
ref : BackRef
The BackRef whose 2-dimensional shapes are used for indexing
fun : Callable
The callable for converting a 1-D array-like to a scalar
Examples
--------
Suppose one wants to calculate the mean value of the ``Vn`` in one Area. In the ``Area`` class, one defines ::
class AreaModel(...):
def __init__(...):
...
# backward reference from `Bus`
self.Bus = BackRef()
# collect the Vn in an 1-D array
self.Vn = ExtParam(model='Bus',
src='Vn',
indexer=self.Bus)
self.Vn_mean = NumReduce(u=self.Vn,
fun=np.mean,
ref=self.Bus)
Suppose we define two areas, 1 and 2, the Bus data looks like
=== ===== ====
idx area Vn
--- ----- ----
1 1 110
2 2 220
3 1 345
4 1 500
=== ===== ====
Then, `self.Bus.v` is a list of two lists ``[ [1, 3, 4], [2] ]``.
`self.Vn.v` will be retrieved and linearly stored as ``[110, 345, 500, 220]``.
Based on the shape from `self.Bus`, :py:func:`numpy.mean`
will be called on ``[110, 345, 500]`` and ``[220]`` respectively.
Thus, `self.Vn_mean.v` will become ``[318.33, 220]``.
"""
def __init__(self,
u,
ref: BackRef,
fun: Callable,
name=None,
tex_name=None,
info=None,
cache=True,
):
super().__init__(name=name, tex_name=tex_name, info=info)
self.u = u
self.ref = ref
self.fun = fun
self.cache = cache
@property
def v(self):
"""
Return the reduced values from the reduction function in an array
Returns
-------
The array ``self._v`` storing the reduced values
"""
if self._v is not None and self.cache is True:
return self._v
if self._v is None:
self._v = np.zeros(len(self.ref.v))
idx = 0
for i, v in enumerate(self.ref.v):
self._v[i] = self.fun(self.u.v[idx:idx + len(v)])
idx += len(v)
return self._v
class NumRepeat(OperationService):
r"""
A helper Service type which repeats a v-provider's value based on the shape from a BackRef
Examples
--------
NumRepeat was originally designed for computing the inertia-weighted average rotor speed (center of
inertia speed). COI speed is computed with
.. math ::
\omega_{COI} = \frac{ \sum{M_i * \omega_i} } {\sum{M_i}}
The numerator can be calculated with a mix of BackRef, ExtParam and ExtState. The denominator needs to be
calculated with NumReduce and Service Repeat. That is, use NumReduce to calculate the sum,
and use NumRepeat to repeat the summed value for each device.
In the COI class, one would have
.. code-block :: python
class COIModel(...):
def __init__(...):
...
self.SynGen = BackRef()
self.SynGenIdx = RefFlatten(ref=self.SynGen)
self.M = ExtParam(model='SynGen',
src='M',
indexer=self.SynGenIdx)
self.wgen = ExtState(model='SynGen',
src='omega',
indexer=self.SynGenIdx)
self.Mt = NumReduce(u=self.M,
fun=np.sum,
ref=self.SynGen)
self.Mtr = NumRepeat(u=self.Mt,
ref=self.SynGen)
self.pidx = IdxRepeat(u=self.idx,ref=self.SynGen)
Finally, one would define the center of inertia speed as
.. code-block :: python
self.wcoi = Algeb(v_str='1', e_str='-wcoi')
self.wcoi_sub = ExtAlgeb(model='COI',
src='wcoi',
e_str='M * wgen / Mtr',
v_str='M / Mtr',
indexer=self.pidx,
)
It is very worth noting that the implementation uses a trick to separate the average weighted sum into `n`
sub-equations, each calculating the :math:`(M_i * \omega_i) / (\sum{M_i})`. Since all the variables are
preserved in the sub-equation, the derivatives can be calculated correctly.
"""
def __init__(self,
u,
ref,
**kwargs):
super().__init__(**kwargs)
self.u = u
self.ref = ref
@property
def v(self):
"""
Return the values of the repeated values in a sequential 1-D array
Returns
-------
The array, ``self._v`` storing the repeated values
"""
if self._v is None:
self._v = np.zeros(len(list_flatten(self.ref.v)))
idx = 0
for i, v in enumerate(self.ref.v):
self._v[idx:idx + len(v)] = self.u.v[i]
idx += len(v)
return self._v
else:
return self._v
class IdxRepeat(OperationService):
"""
Helper class to repeat IdxParam.
This class has the same functionality as :py:class:`andes.core.service.NumRepeat`
but only operates on IdxParam, DataParam or NumParam.
"""
def __init__(self,
u,
ref,
**kwargs):
super().__init__(**kwargs)
self.u = u
self.ref = ref
@property
def v(self):
if self._v is None:
self._v = [''] * len(list_flatten(self.ref.v))
idx = 0
for i, v in enumerate(self.ref.v):
for jj in range(idx, idx + len(v)):
self._v[jj] = self.u.v[i]
idx += len(v)
return self._v
else:
return self._v
class RefFlatten(OperationService):
"""
A service type for flattening :py:class:`andes.core.service.BackRef` into a 1-D list.
Examples
--------
This class is used when one wants to pass `BackRef` values as indexer.
:py:class:`andes.models.coi.COI` collects referencing
:py:class:`andes.models.group.SynGen` with
.. code-block :: python
self.SynGen = BackRef(info='SynGen idx lists', export=False)
After collecting BackRefs, `self.SynGen.v` will become a two-level list of indices,
where the first level correspond to each COI and the second level correspond to generators
of the COI.
Convert `self.SynGen` into 1-d as `self.SynGenIdx`, which can be passed as indexer for
retrieving other parameters and variables
.. code-block :: python
self.SynGenIdx = RefFlatten(ref=self.SynGen)
self.M = ExtParam(model='SynGen', src='M',
indexer=self.SynGenIdx, export=False,
)
"""
def __init__(self, ref, **kwargs):
super().__init__(**kwargs)
self.ref = ref
@property
def v(self):
return list_flatten(self.ref.v)
class NumSelect(OperationService):
"""
Class for selecting values for optional NumParam.
Notes
-----
One use case is to allow an optional turbine rating. One can do ::
self.Tn = NumParam(default=None)
self.Sg = ExtParam(...)
self.Sn = DataSelect(Tn, Sg)
"""
def __init__(self,
optional,
fallback,
name: Optional[str] = None,
tex_name: Optional[str] = None,
info: Optional[str] = None,
):
super().__init__(name=name, tex_name=tex_name, info=info)
self.optional = optional
self.fallback = fallback
self._v = None
@property
def v(self):
if self._v is None:
self._v = [v1 if not np.isnan(v1)
else v2
for v1, v2 in zip(self.optional.v, self.fallback.v)]
self._v = np.array(self._v)
return self._v
class InitChecker(OperationService):
"""
Class for checking init values against known typical values.
Instances will be stored in `Model.services_post` and
`Model.services_icheck`, which will be checked in
`Model.post_init_check()` after initialization.
Parameters
----------
u
v-provider to be checked
lower : float, BaseParam, BaseVar, BaseService
lower bound
upper : float, BaseParam, BaseVar, BaseService
upper bound
equal : float, BaseParam, BaseVar, BaseService
values that the value from `v_str` should equal
not_equal : float, BaseParam, BaseVar, BaseService
values that should not equal
enable : bool
True to enable checking
Examples
--------
Let's say generator excitation voltages are known to be in
the range of 1.6 - 3.0 per unit. One can add the following
instance to `GENBase` ::
self._vfc = InitChecker(u=self.vf,
info='vf range',
lower=1.8,
upper=3.0,
)
`lower` and `upper` can also take v-providers instead of
float values.
One can also pass float values from Config to make it
adjustable as in our implementation of ``GENBase._vfc``.
"""
def __init__(self, u, lower=None, upper=None, equal=None, not_equal=None,
enable=True, error_out=False, **kwargs):
super().__init__(**kwargs)
self.u = u
self.lower = dummify(lower) if lower is not None else None
self.upper = dummify(upper) if upper is not None else None
self.equal = dummify(equal) if equal is not None else None
self.not_equal = dummify(not_equal) if not_equal is not None else None
self.enable = enable
self.error_out = error_out
def check(self):
"""
Check the bounds and equality conditions.
"""
if not self.enable:
return
def _not_all_close(a, b):
return np.logical_not(np.isclose(a, b))
if self._v is None:
self._v = np.zeros_like(self.u.v)
checks = [(self.lower, np.less_equal, "violation of the lower limit", "limit"),
(self.upper, np.greater_equal, "violation of the upper limit", "limit"),
(self.equal, _not_all_close, 'should be equal', "expected"),