This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
emgr.py
712 lines (543 loc) · 25.9 KB
/
emgr.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
"""
emgr - EMpirical GRamian Framework
==================================
project: emgr ( https://gramian.de )
version: 5.99.py (2022-04-13)
authors: Christian Himpe (0000-0003-2194-6754)
license: BSD-2-Clause License (opensource.org/licenses/BSD-2-Clause)
summary: Empirical system Gramians for (nonlinear) input-output systems.
DESCRIPTION:
------------
Empirical gramian matrix and empirical covariance matrix computation
for model reduction, decentralized control, nonlinearity quantification,
sensitivity analysis, parameter identification, uncertainty quantification &
combined state and parameter reduction of large-scale input-output systems.
Data-driven analysis of input-output coherence and system-gramian-based
nonlinear model order reduction. Compatible with PYTHON3.
BRIEF:
------
Unsupervised learning of I/O system properties for data-driven control.
ALGORITHM:
----------
C. Himpe (2018). emgr - The Empirical Gramian Framework. Algorithms 11(7):91
doi:10.3390/a11070091
USAGE:
------
W = emgr(f,g,s,t,w,[pr],[nf],[ut],[us],[xs],[um],[xm],[dp])
MANDATORY ARGUMENTS:
--------------------
f {function} vector field handle: x' = f(x,u,p,t)
g {function} output function handle: y = g(x,u,p,t)
s {tuple} system dimensions: [inputs, states, outputs]
t {tuple} time discretization: [time-step, time-horizon]
w {string} single character encoding gramian type:
* "c" empirical controllability gramian (Wc)
* "o" empirical observability gramian (Wo)
* "x" empirical cross gramian (Wx aka Wco)
* "y" empirical linear cross gramian (Wy)
* "s" empirical sensitivity gramian (Ws)
* "i" empirical identifiability gramian (Wi)
* "j" empirical joint gramian (Wj)
OPTIONAL ARGUMENTS:
-------------------
pr {matrix|0} parameter vector(s), each column is one parameter sample
nf {tuple|0} option flags, thirteen component vector, default all zero:
* centering: none(0), steady(1), last(2), mean(3), rms(4), midrange(5)
* input scales: single(0), linear(1), geometric(2), log(3), sparse(4)
* state scales: single(0), linear(1), geometric(2), log(3), sparse(4)
* input rotations: unit(0), single(1)
* state rotations: unit(0), single(1)
* normalization (only: Wc, Wo, Wx, Wy): none(0), steady(1), Jacobi(2)
* state gramian variant:
* controllability gramian type (only: Wc, Ws): regular(0), output(1)
* observability gramian type (only: Wo, Wi): regular(0), averaged(1)
* cross gramian type (only: Wx, Wy, Wj): regular(0), non-symmetric(1)
* extra input (only: Wo, Wx, Ws, Wi, Wj): no(0), yes(1)
* parameter centering (only: Ws, Wi, Wj): none(0), lin(1), log(2), nom(3)
* parameter gramian variant:
* averaging type (only: Ws): input-state(0), input-output(1)
* Schur-complement (only: Wi, Wj): approx(0), coarse(1)
* cross gramian partition size (only: Wx, Wj): full(0), partitioned(<N)
* cross gramian partition index (only: Wx, Wj): partition(>0)
* weighting: none(0), linear(1), squared(2), state(3), scale(4), rsqrt(5)
ut {handle|'i'} input function: u_t = ut(t) or single character string:
* "i" delta impulse input
* "s" step input / load vector / source term
* "h" havercosine decaying exponential chirp input
* "a" sinc (cardinal sine) input
* "r" pseudo-random binary input
us {vector|0} steady-state input (1 or #inputs rows)
xs {vector|0} steady-state and nominal initial state x_0 (1 or #states rows)
um {matrix|1} input scales (1 or #inputs rows)
xm {matrix|1} initial-state scales (1 or #states rows)
dp {handle|@mtimes} inner product or kernel: xy = dp(x,y)
RETURNS:
--------
W {matrix} State-space system Gramian Matrix (for: Wc, Wo, Wx, Wy)
W {tuple} (State, Parameter)-space system Gramian (for: Ws, Wi, Wj)
CITE AS:
--------
C. Himpe (2022). emgr - EMpirical GRamian Framework (Version 5.99)
[Software]. Available from https://gramian.de . doi:10.5281/zenodo.6457616
KEYWORDS:
---------
model reduction, system gramians, empirical gramians, cross gramian, MOR
SEE ALSO:
---------
gram (Python Control Systems Library)
COPYRIGHT: Christian Himpe
---------
For more information, see: https://gramian.de
"""
import math
import numpy as np
__version__ = "5.99"
__date__ = "2022-04-13"
__copyright__ = "Copyright (C) 2022 Christian Himpe"
__author__ = "Christian Himpe"
__license__ = "BSD 2-Clause"
ODE = lambda f, g, t, x0, u, p: ssp2(f, g, t, x0, u, p) # Preset default integrator
def emgr(f, g=None, s=None, t=None, w=None, pr=0, nf=0, ut="i", us=0.0, xs=0.0, um=1.0, xm=1.0, dp=np.dot):
""" Compute empirical system Gramian matrix """
# Version Info
if f == "version":
return __version__
fState = f
# Default Arguments
if isinstance(pr, (int, float)) or np.ndim(pr) == 1:
pr = np.reshape(pr, (-1, 1))
###############################################################################
# SETUP
###############################################################################
# System Dimensions
nInputs = int(s[0]) # Number of inputs
nStates = int(s[1]) # Number of states
nOutputs = int(s[2]) # Number of outputs
# Parameter Dimensions
nParams = pr.shape[0] # Dimension of parameter
nParamSamples = pr.shape[1] # Number of parameter-sets
# Time Discretization
tStep = t[0] # Time-step width
tFinal = t[1] # Time horizon
nSteps = int(math.floor(tFinal / tStep) + 1) # Number of time-steps
# Gramian Type
gramianType = w[0].lower()
# Flag Vector
if nf == 0:
flags = [0]
else:
flags = nf
if len(flags) < 13:
flags = flags + [0] * (13 - len(flags))
# Built-in Input Functions
if isinstance(ut, str):
a0 = math.pi / (2.0 * tStep) * tFinal / math.log(4.0 * (tStep / tFinal))
b0 = (4.0 * (tStep / tFinal)) ** (1.0 / tFinal)
if ut.lower() == "i": # Delta Impulse Input
def fExcite(t):
return float(t <= tStep) / tStep
elif ut.lower() == "s": # Step Input
def fExcite(t):
return 1.0
elif ut.lower() == "h": # Havercosine Chirp Input
def fExcite(t):
return 0.5 * math.cos(a0 * (b0 ** t - 1.0)) + 0.5
elif ut.lower() == "a": # Sinc Input
def fExcite(t):
return math.sin(t / tStep) / ((t / tStep) + float(t == 0))
elif ut.lower() == "r": # Pseudo-Random Binary Input
def fExcite(t):
return np.random.randint(0, 1, 1)
else:
assert False, "emgr: unknown input ut!"
else:
fExcite = ut
###############################################################################
# CONFIGURATION
###############################################################################
# Output Function
if (isinstance(g, int) and g == 1) or ((gramianType == "c") and not flags[6]) or (gramianType == "y"):
fOutput = ident
fAdjoint = g
else:
fOutput = g
# Trajectory Weighting
tInstances = np.linspace(0, tFinal, nSteps)[np.newaxis, :]
tInstances[0, 0] = 0.5 * tStep
if flags[12] == 1: # Linear Time-Weighting
def fWeight(traj):
return traj * np.sqrt(tInstances)
elif flags[12] == 2: # Quadratic Time-Weighting
def fWeight(traj):
return traj * (tInstances / math.sqrt(2.0))
elif flags[12] == 3: # State-Weighting
def fWeight(traj):
return traj / np.maximum(math.sqrt(np.spacing(1)), np.linalg.norm(traj, 2, axis=0))
elif flags[12] == 4: # Scale-Weighting
def fWeight(traj):
return traj / np.maximum(math.sqrt(np.spacing(1)), np.linalg.norm(traj, np.inf, axis=1)[:, np.newaxis])
elif flags[12] == 5: # Reciprocal Square-Root Time-Weighting
def fWeight(traj):
return traj / (math.pi * tInstances) ** 0.25
else: # None
def fWeight(traj):
return traj
# Trajectory Centering
if flags[0] == 1: # Steady-State / Output
def fCenter(traj, xs):
return traj - xs.reshape(-1, 1)
elif flags[0] == 2: # Final State / Output
def fCenter(traj, xs):
return traj - traj[:, -1].reshape(-1, 1)
elif flags[0] == 3: # Temporal Mean State / Output
def fCenter(traj, xs):
return traj - np.mean(traj, axis=1).reshape(-1, 1)
elif flags[0] == 4: # Temporal Root-Mean-Square / Output
def fCenter(traj, xs):
return traj - np.sqrt(np.mean(traj * traj, axis=1)).reshape(-1, 1)
elif flags[0] == 5: # Temporal Mid-range of State / Output
def fCenter(traj, xs):
return traj - 0.5 * (np.amax(traj, axis=1) + np.amin(traj, axis=1)).reshape(-1, 1)
else: # None
def fCenter(traj, xs):
return traj
# Steady State
vSteadyInput = np.full((nInputs, 1), us) if np.isscalar(us) else us
vSteadyState = np.full((nStates, 1), xs) if np.isscalar(xs) else xs
# Gramian Normalization
if flags[5] in {1, 2} and w in {"c", "o", "x", "y"}:
if flags[5] == 2: # Jacobi-type preconditioner
NF = list(flags)
NF[5] = 0
if w == "c":
NF[6] = 0
def DP(x, y):
return np.sum(x[:nStates, :] * y[:, :nStates].T, 1) # Diagonal-only kernel
TX = np.sqrt(np.abs(emgr(f, g, s, t, w, np.mean(pr, axis=1), NF, ut, us, xs, um, xm, DP)))[:, np.newaxis]
if flags[5] == 1: # Steady-state preconditioner
TX = vSteadyState
TX[np.fabs(TX) < np.sqrt(np.spacing(1))] = 1.0
vSteadyState = vSteadyState / TX
def fState(x, u, p, t):
return f(TX * x, u, p, t) / TX
def fAdjoint(x, u, p, t):
return g(TX * x, u, p, t) / TX
if fOutput == ident:
def fOutput(x, u, p, t):
return ident(TX * x, u, p, t)
else:
def fOutput(x, u, p, t):
return g(TX * x, u, p, t)
# Output Averaging
nPages = 1 if flags[6] != 0 else nOutputs
# Extra Input (for control explicit observability)
if flags[7] != 0:
def fSteady(t):
return vSteadyInput + fExcite(t)
else:
def fSteady(t):
return vSteadyInput
# Perturbation Scales
vInputMax = np.full((nInputs, 1), um) if np.isscalar(um) else um
vStateMax = np.full((nStates, 1), xm) if np.isscalar(xm) else xm
vOutputMax = np.full((nOutputs, 1), xm) if np.isscalar(xm) else xm
mInputScales = (vInputMax * scales(flags[1], flags[3])) if vInputMax.shape[1] == 1 else vInputMax
mStateScales = (vStateMax * scales(flags[2], flags[4])) if vStateMax.shape[1] == 1 else vStateMax
mOutputScales = (vOutputMax * scales(flags[1], flags[3])) if vOutputMax.shape[1] == 1 else vOutputMax
nTotalStates = mStateScales.shape[0]
nInputScales = mInputScales.shape[1]
nStateScales = mStateScales.shape[1]
nOutputScales = mOutputScales.shape[1]
###############################################################################
# EMPIRICAL SYSTEM GRAMIAN COMPUTATION
###############################################################################
W = 0.0 # Initialize gramian variable
# Common Layout:
# For each {parameter, scale, input/state/parameter component}:
# Perturb, simulate, weight, center, normalize, accumulate
# Output and adjoint trajectories are cached to prevent recomputation
# Parameter gramians "s", "i", "j" call state gramians "c", "o", "x"
###############################################################################
# EMPIRICAL CONTROLLABILITY GRAMIAN
###############################################################################
if w == "c":
for k in range(nParamSamples):
vParam = pr[:, [k]]
vSteadyOutput = fOutput(vSteadyState, vSteadyInput, vParam, 0)
for c in range(nInputScales):
for m in range(nInputs):
sPerturb = mInputScales[m, c]
if sPerturb != 0.0:
vUnit = np.zeros(nInputs)
vUnit[m] = sPerturb
def fInput(t):
return vSteadyInput + vUnit * fExcite(t)
mTraj = fWeight(fCenter(ODE(fState, fOutput, t, vSteadyState, fInput, vParam), vSteadyOutput)) / sPerturb
W += dp(mTraj, mTraj.T)
W *= tStep / (nInputScales * nParamSamples)
return W
###############################################################################
# EMPIRICAL OBSERVABILITY GRAMIAN
###############################################################################
if w == "o":
obsCache = np.zeros((nPages * nSteps, nTotalStates))
for k in range(nParamSamples):
vParam = pr[:, [k]]
for d in range(nStateScales):
for n in range(nTotalStates):
sPerturb = mStateScales[n, d]
if sPerturb != 0.0:
vUnit = np.zeros((nTotalStates, 1))
vUnit[n] = sPerturb
vInit = vSteadyState + vUnit[:nStates]
vParamInit = np.copy(vParam)
if nTotalStates > nStates:
vParamInit += vUnit[nStates:]
vSteadyOutput = fOutput(vSteadyState, vSteadyInput, vParamInit, 0)
mTraj = fWeight(fCenter(ODE(fState, fOutput, t, vInit, fSteady, vParamInit), vSteadyOutput)) / sPerturb
if flags[6] != 0:
obsCache[:, n] = np.sum(mTraj, axis=0)
else:
obsCache[:, n] = mTraj.flatten(order='F')
W += dp(obsCache.T, obsCache)
W *= tStep / (nStateScales * nParamSamples)
return W
###############################################################################
# EMPIRICAL CROSS GRAMIAN
###############################################################################
if w == "x":
assert nInputs == nOutputs or nf[6], "emgr: non-square system!"
colFirst = 0 # Start partition column index
colLast = nTotalStates # Final partition column index
# Partitioned cross gramian
if flags[10] > 0:
parSize = int(round(nf[10])) # Partition size
parIndex = int(round(nf[11])) # Partition index
colFirst += (parIndex - 1) * parSize # Start index
colLast = min(colFirst + (parSize - 1), nStates)
if colFirst > nStates:
colFirst -= (math.ceil(nStates / parSize) * parSize - nStates)
colLast = min(colFirst + parSize - 1, nTotalStates)
if parIndex < 0 or colFirst >= colLast or colFirst < 0:
return 0
obsCache = np.zeros((nSteps * nPages, colLast - colFirst))
for k in range(nParamSamples):
vParam = pr[:, [k]]
for d in range(nStateScales):
for n in range(colLast - colFirst):
sPerturb = mStateScales[colFirst + n, d]
if sPerturb != 0.0:
vUnit = np.zeros((nTotalStates, 1))
vUnit[colFirst + n] = sPerturb
vInit = vSteadyState + vUnit[:nStates]
vParamInit = np.copy(vParam)
if nTotalStates > nStates:
vParamInit += vUnit[nStates:]
vSteadyOutput = fOutput(vSteadyState, vSteadyInput, vParamInit, 0)
mTraj = fWeight(fCenter(ODE(fState, fOutput, t, vInit, fSteady, vParamInit), vSteadyInput)) / sPerturb
if flags[6] != 0:
obsCache[:, n] = np.sum(mTraj, axis=0).T
else:
obsCache[:, n] = mTraj.T.flatten(0)
for c in range(nInputScales):
for m in range(nInputs):
sPerturb = mInputScales[m, c]
if sPerturb != 0.0:
vUnit = np.zeros((nInputs, 1))
vUnit[m] = sPerturb
def fInput(t):
return vSteadyInput + vUnit * fExcite(t)
mTraj = fWeight(fCenter(ODE(fState, ident, t, vSteadyState, fInput, vParam), vSteadyInput)) / sPerturb
nBlock = 0 if flags[6] else m * nSteps
W += dp(mTraj, obsCache[nBlock:nBlock + nSteps, :])
W *= tStep / (nInputScales * nStateScales * nParamSamples)
return W
###############################################################################
# EMPIRICAL LINEAR CROSS GRAMIAN
###############################################################################
if w == "y":
assert nInputs == nOutputs or nf[6], "emgr: non-square system!"
assert nInputScales == nOutputScales, "emgr: scale count mismatch!"
adjCache = np.zeros((nSteps, nStates, nPages))
for k in range(nParamSamples):
vParam = pr[:, [k]]
for c in range(nInputScales):
for q in range(nOutputs):
sPerturb = mOutputScales[q, c]
if sPerturb != 0.0:
vUnit = np.zeros((nOutputs, 1))
vUnit[q] = sPerturb
def fInput(t):
return vSteadyInput + vUnit * fExcite(t)
mTraj = fWeight(fCenter(ODE(fAdjoint, ident, t, vSteadyState, fInput, vParam), vSteadyInput)) / sPerturb
adjCache[:, :, q] = mTraj.T
if flags[6] != 0:
adjCache[:, :, 0] = np.sum(adjCache, axis=2)
for m in range(nInputs):
sPerturb = mInputScales[m, c]
if sPerturb != 0.0:
vUnit = np.zeros((nInputs, 1))
vUnit[m] = sPerturb
def fInput(t):
return vSteadyInput + vUnit * fExcite(t)
mTraj = fWeight(fCenter(ODE(fState, ident, t, vSteadyState, fInput, vParam), vSteadyInput)) / sPerturb
W += dp(mTraj, adjCache[:, :, 0 if flags[6] != 0 else m])
W *= tStep / (nInputScales * nParamSamples)
return W
###############################################################################
# EMPIRICAL SENSITIVITY GRAMIAN
###############################################################################
if w == "s":
# Controllability Gramian
pr, mParamScales = paramScales(pr, flags[8], nInputScales)
WC = emgr(f, g, s, t, "c", pr, flags, ut, us, xs, um, xm, dp)
if not flags[9]: # Input-state sensitivity gramian
def DP(x, y):
return np.sum(x * y.T) # Trace pseudo-kernel
else: # Input-output sensitivity gramian
def DP(x, y):
return y # Custom pseudo-kernel
flags[6] = 1
Y = emgr(f, g, s, t, "o", pr, flags, ut, us, xs, um, xm, DP)
flags[6] = 0
def DP(x, y):
return np.abs(np.sum(y * Y)) # Custom pseudo-kernel
# (Diagonal) Sensitivity Gramian
WS = np.zeros((nParams, 1))
for p in range(nParams):
paramSamples = np.tile(pr, (1, mParamScales.shape[1]))
paramSamples[p, :] += mParamScales[p, :]
WS[p] = emgr(f, g, s, t, "c", paramSamples, flags, ut, us, xs, um, xm, DP)
return WC, WS
###############################################################################
# EMPIRICAL IDENTIFIABILTY GRAMIAN
###############################################################################
if w == "i":
# Augmented Observability Gramian
pr, mParamScales = paramScales(pr, flags[8], nStateScales)
V = emgr(f, g, s, t, "o", pr, flags, ut, us, xs, um, np.vstack((mStateScales, mParamScales)), dp)
# Return augmented observability gramian
if flags[10] != 0:
return V
WO = V[:nStates, :nStates] # Observability Gramian
WM = V[:nStates, nStates:] # Mixed Block
WI = V[nStates:, nStates:] # Parameter Gramian
# Identifiability Gramian
if flags[9] == 2: # Exact Schur-complement via pseudo-inverse
WI -= (WM.T).dot(np.linalg.pinv(WO)).dot(WM)
elif flags[9] == 0: # Approximate Schur-complement via approximate inverse
WI -= (WM.T).dot(ainv(WO)).dot(WM)
return WO, WI
###############################################################################
# EMPIRICAL JOINT GRAMIAN
###############################################################################
if w == "j":
# Empirical Joint Gramian
pr, mParamScales = paramScales(pr, flags[8], nStateScales)
V = emgr(f, g, s, t, "x", pr, flags, ut, us, xs, um, np.vstack((mStateScales, mParamScales)), dp)
# Return joint gramian (partition)
if flags[10] != 0:
return V
WX = V[:nStates, :nStates] # Cross gramian
WM = V[:nStates, nStates:] # Mixed Block
# Cross-identifiability Gramian via Schur Complement
if flags[9] == 1: # Coarse Schur-complement via identity
WI = 0.5 * (WM.T).dot(WM)
elif flags[9] == 2: # Exact Schur-complement via pseudo-inverse
WI = 0.5 * (WM.T).dot(np.linalg.pinv(WX + WX.T)).dot(WM)
else: # Approximate Schur-complement via approximate inverse
WI = 0.5 * (WM.T).dot(ainv(WX + WX.T)).dot(WM)
return WX, WI
assert False, "emgr: unknown gramian type!"
###############################################################################
# LOCAL FUNCTION: ident
###############################################################################
def ident(x, u, p, t):
""" (Output) identity function """
return x
###############################################################################
# LOCAL FUNCTION: scales
###############################################################################
def scales(flScales, flRot):
""" Input and initial state perturbation scales """
if flScales == 1: # Linear
mScales = np.array([0.25, 0.50, 0.75, 1.0], ndmin=1)
elif flScales == 2: # Geometric
mScales = np.array([0.125, 0.25, 0.5, 1.0], ndmin=1)
elif flScales == 3: # Logarithmic
mScales = np.array([0.001, 0.01, 0.1, 1.0], ndmin=1)
elif flScales == 4: # Sparse
mScales = np.array([0.01, 0.50, 0.99, 1.0], ndmin=1)
else:
mScales = np.array([1.0], ndmin=1)
if flRot == 0:
mScales = np.concatenate((-mScales, mScales))
return mScales
###############################################################################
# LOCAL FUNCTION: pscales
###############################################################################
def paramScales(p, flScales, nParamScales):
""" Parameter perturbation scales """
vParamMin = np.amin(p, axis=1)
vParamMax = np.amax(p, axis=1)
if flScales == 1: # Linear centering and scales
assert p.shape[1] >= 2, "emgr: min and max parameter requires!"
vParamSteady = 0.5 * (vParamMax + vParamMin)
vScales = np.linspace(0.0, 1.0, nParamScales)
elif flScales == 2: # Logarithmic centering and scales
assert p.shape[1] >= 2, "emgr: min and max parameter requires!"
vParamSteady = np.sqrt(vParamMax * vParamMin)
vParamMin = np.log(vParamMin)
vParamMax = np.log(vParamMax)
vScales = np.linspace(0.0, 1.0, nParamScales)
elif flScales == 3: # Nominal centering and scaling
assert p.shape[1] >= 3, "emgr: min, nom, max parameter requires!"
vParamSteady = p[:, 1]
vParamMin = p[:, 0]
vParamMax = p[:, 2]
vScales = np.linspace(0.0, 1.0, nParamScales)
else: # No centering and linear scales
assert p.shape[1] >= 2, "emgr: min and max parameter requires!"
vParamSteady = np.copy(vParamMin)
vParamMin = np.full(p.shape[0], 1.0 / nParamScales)
vScales = np.linspace(1.0 / nParamScales, 1.0, nParamScales)
mParamScales = np.outer(vParamMax - vParamMin, vScales.T) + np.expand_dims(vParamMin, -1)
if flScales == 2:
mParamScales = np.exp(mParamScales)
vParamSteady = np.expand_dims(vParamSteady, -1)
mParamScales -= vParamSteady
return vParamSteady, mParamScales
###############################################################################
# LOCAL FUNCTION: ainv
###############################################################################
def ainv(m):
""" Quadratic complexity approximate inverse matrix """
# Based on truncated Neumann series: X = D^-1 - D^-1 (M - D) D^-1
d = np.copy(np.diag(m))[:, np.newaxis]
k = np.nonzero(np.abs(d) > np.sqrt(np.spacing(1)))
d[k] = 1.0 / d[k]
x = (m * (-d)) * d.T
x.flat[::np.size(d) + 1] = d
return x
###############################################################################
# LOCAL FUNCTION: ssp2
###############################################################################
STAGES = 3 # Configurable number of stages for enhanced stability
def ssp2(f, g, t, x0, u, p):
""" Low-Storage Strong-Stability-Preserving Second-Order Runge-Kutta """
nStages = STAGES if isinstance(STAGES, int) else 3
tStep = t[0]
nSteps = int(math.floor(t[1] / tStep) + 1)
y0 = g(x0, u(0), p, 0)
y = np.empty((y0.shape[0], nSteps)) # Pre-allocate trajectory
y[:, 0] = y0.T
xk1 = np.copy(x0)
for k in range(1, nSteps):
xk2 = np.copy(xk1)
tCurr = (k - 0.5) * tStep
uCurr = u(tCurr)
for _ in range(1, nStages):
xk1 += (tStep / (STAGES - 1.0)) * f(xk1, uCurr, p, tCurr)
xk1 = (xk1 * (nStages - 1) + xk2 + tStep * f(xk1, uCurr, p, tCurr)) / nStages
y[:, k] = g(xk1, uCurr, p, tCurr).flatten(0)
return y