Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F62499175
rational_interpolant.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, May 13, 15:26
Size
28 KB
Mime Type
text/x-python
Expires
Wed, May 15, 15:26 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
17654817
Attached To
R6746 RationalROMPy
rational_interpolant.py
View Options
# Copyright (C) 2018 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
from
copy
import
deepcopy
as
copy
import
numpy
as
np
from
rrompy.reduction_methods.base
import
checkRobustTolerance
from
.generic_standard_approximant
import
GenericStandardApproximant
from
rrompy.utilities.poly_fitting.polynomial
import
(
polybases
as
ppb
,
polyfitname
,
polyvander
as
pvP
,
polyvanderTotal
as
pvTP
,
polyTimes
,
polyTimesTable
,
vanderInvTable
,
PolynomialInterpolator
as
PI
)
from
rrompy.utilities.poly_fitting.heaviside
import
rational2heaviside
from
rrompy.utilities.poly_fitting.radial_basis
import
(
polybases
as
rbpb
,
RadialBasisInterpolator
as
RBI
)
from
rrompy.utilities.poly_fitting.moving_least_squares
import
(
polybases
as
mlspb
,
MovingLeastSquaresInterpolator
as
MLSI
)
from
rrompy.utilities.base.types
import
Np1D
,
Np2D
,
Tuple
,
List
,
sampList
from
rrompy.utilities.base
import
verbosityManager
as
vbMng
from
rrompy.utilities.numerical
import
(
customPInv
,
dot
,
fullDegreeN
,
totalDegreeN
,
degreeTotalToFull
,
fullDegreeMaxMask
,
totalDegreeMaxMask
,
nextDerivativeIndices
)
from
rrompy.utilities.exception_manager
import
(
RROMPyException
,
RROMPyAssert
,
RROMPyWarning
)
__all__
=
[
'RationalInterpolant'
]
class
RationalInterpolant
(
GenericStandardApproximant
):
"""
ROM rational interpolant computation for parametric problems.
Args:
HFEngine: HF problem solver.
mu0(optional): Default parameter. Defaults to 0.
approxParameters(optional): Dictionary containing values for main
parameters of approximant. Recognized keys are:
- 'POD': whether to compute POD of snapshots; defaults to True;
- 'S': total number of samples current approximant relies upon;
- 'sampler': sample point generator;
- 'polybasis': type of polynomial basis for interpolation; defaults
to 'MONOMIAL';
- 'M': degree of rational interpolant numerator; defaults to 0;
- 'N': degree of rational interpolant denominator; defaults to 0;
- 'polydegreetype': type of polynomial degree; defaults to 'TOTAL';
- 'radialDirectionalWeights': radial basis weights for interpolant
numerator; defaults to 0, i.e. identity;
- 'nNearestNeighbor': mumber of nearest neighbors considered in
numerator if polybasis allows; defaults to -1;
- 'interpRcond': tolerance for interpolation; defaults to None;
- 'robustTol': tolerance for robust rational denominator
management; defaults to 0;
- 'correctorForce': whether corrector should forcefully delete bad
poles; defaults to False;
- 'correctorTol': tolerance for corrector step; defaults to 0.,
i.e. no bad poles;
- 'correctorMaxIter': maximum number of corrector iterations;
defaults to 1.
Defaults to empty dict.
approx_state(optional): Whether to approximate state. Defaults to
False.
verbosity(optional): Verbosity level. Defaults to 10.
Attributes:
HFEngine: HF problem solver.
mu0: Default parameter.
mus: Array of snapshot parameters.
approxParameters: Dictionary containing values for main parameters of
approximant. Recognized keys are in parameterList.
parameterListSoft: Recognized keys of soft approximant parameters:
- 'POD': whether to compute POD of snapshots;
- 'polybasis': type of polynomial basis for interpolation;
- 'M': degree of rational interpolant numerator;
- 'N': degree of rational interpolant denominator;
- 'polydegreetype': type of polynomial degree;
- 'radialDirectionalWeights': radial basis weights for interpolant
numerator;
- 'nNearestNeighbor': mumber of nearest neighbors considered in
numerator if polybasis allows;
- 'interpRcond': tolerance for interpolation via numpy.polyfit;
- 'robustTol': tolerance for robust rational denominator
management;
- 'correctorForce': whether corrector should forcefully delete bad
poles;
- 'correctorTol': tolerance for corrector step;
- 'correctorMaxIter': maximum number of corrector iterations.
parameterListCritical: Recognized keys of critical approximant
parameters:
- 'S': total number of samples current approximant relies upon;
- 'sampler': sample point generator.
approx_state: Whether to approximate state.
verbosity: Verbosity level.
POD: Whether to compute POD of snapshots.
S: Number of solution snapshots over which current approximant is
based upon.
sampler: Sample point generator.
polybasis: type of polynomial basis for interpolation.
M: Numerator degree of approximant.
N: Denominator degree of approximant.
polydegreetype: Type of polynomial degree.
radialDirectionalWeights: Radial basis weights for interpolant
numerator.
nNearestNeighbor: Number of nearest neighbors considered in numerator
if polybasis allows.
interpRcond: Tolerance for interpolation via numpy.polyfit.
robustTol: Tolerance for robust rational denominator management.
correctorForce: Whether corrector should forcefully delete bad poles.
correctorTol: Tolerance for corrector step.
correctorMaxIter: Maximum number of corrector iterations.
muBounds: list of bounds for parameter values.
samplingEngine: Sampling engine.
uHF: High fidelity solution(s) with parameter(s) lastSolvedHF as
sampleList.
lastSolvedHF: Parameter(s) corresponding to last computed high fidelity
solution(s) as parameterList.
uApproxReduced: Reduced approximate solution(s) with parameter(s)
lastSolvedApprox as sampleList.
lastSolvedApproxReduced: Parameter(s) corresponding to last computed
reduced approximate solution(s) as parameterList.
uApprox: Approximate solution(s) with parameter(s) lastSolvedApprox as
sampleList.
lastSolvedApprox: Parameter(s) corresponding to last computed
approximate solution(s) as parameterList.
Q: Numpy 1D vector containing complex coefficients of approximant
denominator.
P: Numpy 2D vector whose columns are FE dofs of coefficients of
approximant numerator.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
_preInit
()
self
.
_addParametersToList
([
"polybasis"
,
"M"
,
"N"
,
"polydegreetype"
,
"radialDirectionalWeights"
,
"nNearestNeighbor"
,
"interpRcond"
,
"robustTol"
,
"correctorForce"
,
"correctorTol"
,
"correctorMaxIter"
],
[
"MONOMIAL"
,
0
,
0
,
"TOTAL"
,
1
,
-
1
,
-
1
,
0
,
False
,
0.
,
1
])
super
()
.
__init__
(
*
args
,
**
kwargs
)
self
.
catchInstability
=
0
self
.
_postInit
()
@property
def
tModelType
(
self
):
from
rrompy.reduction_methods.trained_model
import
TrainedModelRational
return
TrainedModelRational
@property
def
polybasis
(
self
):
"""Value of polybasis."""
return
self
.
_polybasis
@polybasis.setter
def
polybasis
(
self
,
polybasis
):
try
:
polybasis
=
polybasis
.
upper
()
.
strip
()
.
replace
(
" "
,
""
)
if
polybasis
not
in
ppb
+
rbpb
+
mlspb
:
raise
RROMPyException
(
"Prescribed polybasis not recognized."
)
self
.
_polybasis
=
polybasis
except
:
RROMPyWarning
((
"Prescribed polybasis not recognized. Overriding "
"to 'MONOMIAL'."
))
self
.
_polybasis
=
"MONOMIAL"
self
.
_approxParameters
[
"polybasis"
]
=
self
.
polybasis
@property
def
polybasis0
(
self
):
if
"_"
in
self
.
polybasis
:
return
self
.
polybasis
.
split
(
"_"
)[
0
]
return
self
.
polybasis
@property
def
interpRcond
(
self
):
"""Value of interpRcond."""
return
self
.
_interpRcond
@interpRcond.setter
def
interpRcond
(
self
,
interpRcond
):
self
.
_interpRcond
=
interpRcond
self
.
_approxParameters
[
"interpRcond"
]
=
self
.
interpRcond
@property
def
radialDirectionalWeights
(
self
):
"""Value of radialDirectionalWeights."""
return
self
.
_radialDirectionalWeights
@radialDirectionalWeights.setter
def
radialDirectionalWeights
(
self
,
radialDirectionalWeights
):
self
.
_radialDirectionalWeights
=
radialDirectionalWeights
self
.
_approxParameters
[
"radialDirectionalWeights"
]
=
(
self
.
radialDirectionalWeights
)
@property
def
nNearestNeighbor
(
self
):
"""Value of nNearestNeighbor."""
return
self
.
_nNearestNeighbor
@nNearestNeighbor.setter
def
nNearestNeighbor
(
self
,
nNearestNeighbor
):
self
.
_nNearestNeighbor
=
nNearestNeighbor
self
.
_approxParameters
[
"nNearestNeighbor"
]
=
self
.
nNearestNeighbor
@property
def
M
(
self
):
"""Value of M."""
return
self
.
_M
@M.setter
def
M
(
self
,
M
):
if
M
<
0
:
raise
RROMPyException
(
"M must be non-negative."
)
self
.
_M
=
M
self
.
_approxParameters
[
"M"
]
=
self
.
M
@property
def
N
(
self
):
"""Value of N."""
return
self
.
_N
@N.setter
def
N
(
self
,
N
):
if
N
<
0
:
raise
RROMPyException
(
"N must be non-negative."
)
self
.
_N
=
N
self
.
_approxParameters
[
"N"
]
=
self
.
N
@property
def
polydegreetype
(
self
):
"""Value of polydegreetype."""
return
self
.
_polydegreetype
@polydegreetype.setter
def
polydegreetype
(
self
,
polydegreetype
):
try
:
polydegreetype
=
polydegreetype
.
upper
()
.
strip
()
.
replace
(
" "
,
""
)
if
polydegreetype
not
in
[
"TOTAL"
,
"FULL"
]:
raise
RROMPyException
((
"Prescribed polydegreetype not "
"recognized."
))
self
.
_polydegreetype
=
polydegreetype
except
:
RROMPyWarning
((
"Prescribed polydegreetype not recognized. "
"Overriding to 'TOTAL'."
))
self
.
_polydegreetype
=
"TOTAL"
self
.
_approxParameters
[
"polydegreetype"
]
=
self
.
polydegreetype
@property
def
robustTol
(
self
):
"""Value of tolerance for robust rational denominator management."""
return
self
.
_robustTol
@robustTol.setter
def
robustTol
(
self
,
robustTol
):
if
robustTol
<
0.
:
RROMPyWarning
((
"Overriding prescribed negative robustness "
"tolerance to 0."
))
robustTol
=
0.
self
.
_robustTol
=
robustTol
self
.
_approxParameters
[
"robustTol"
]
=
self
.
robustTol
@property
def
correctorForce
(
self
):
"""Value of correctorForce."""
return
self
.
_correctorForce
@correctorForce.setter
def
correctorForce
(
self
,
correctorForce
):
self
.
_correctorForce
=
correctorForce
self
.
_approxParameters
[
"correctorForce"
]
=
self
.
correctorForce
@property
def
correctorTol
(
self
):
"""Value of correctorTol."""
return
self
.
_correctorTol
@correctorTol.setter
def
correctorTol
(
self
,
correctorTol
):
if
correctorTol
<
0.
or
(
correctorTol
>
0.
and
self
.
npar
>
1
):
RROMPyWarning
((
"Overriding prescribed corrector tolerance "
"to 0."
))
correctorTol
=
0.
self
.
_correctorTol
=
correctorTol
self
.
_approxParameters
[
"correctorTol"
]
=
self
.
correctorTol
@property
def
correctorMaxIter
(
self
):
"""Value of correctorMaxIter."""
return
self
.
_correctorMaxIter
@correctorMaxIter.setter
def
correctorMaxIter
(
self
,
correctorMaxIter
):
if
correctorMaxIter
<
1
or
(
correctorMaxIter
>
1
and
self
.
npar
>
1
):
RROMPyWarning
((
"Overriding prescribed max number of corrector "
"iterations to 1."
))
correctorMaxIter
=
1
self
.
_correctorMaxIter
=
correctorMaxIter
self
.
_approxParameters
[
"correctorMaxIter"
]
=
self
.
correctorMaxIter
def
resetSamples
(
self
):
"""Reset samples."""
super
()
.
resetSamples
()
self
.
_musUniqueCN
=
None
self
.
_derIdxs
=
None
self
.
_reorder
=
None
def
_setupInterpolationIndices
(
self
):
"""Setup parameters for polyvander."""
if
self
.
_musUniqueCN
is
None
or
len
(
self
.
_reorder
)
!=
len
(
self
.
mus
):
self
.
_musUniqueCN
,
musIdxsTo
,
musIdxs
,
musCount
=
(
self
.
trainedModel
.
centerNormalize
(
self
.
mus
)
.
unique
(
return_index
=
True
,
return_inverse
=
True
,
return_counts
=
True
))
self
.
_musUnique
=
self
.
mus
[
musIdxsTo
]
self
.
_derIdxs
=
[
None
]
*
len
(
self
.
_musUniqueCN
)
self
.
_reorder
=
np
.
empty
(
len
(
musIdxs
),
dtype
=
int
)
filled
=
0
for
j
,
cnt
in
enumerate
(
musCount
):
self
.
_derIdxs
[
j
]
=
nextDerivativeIndices
([],
self
.
mus
.
shape
[
1
],
cnt
)
jIdx
=
np
.
nonzero
(
musIdxs
==
j
)[
0
]
self
.
_reorder
[
jIdx
]
=
np
.
arange
(
filled
,
filled
+
cnt
)
filled
+=
cnt
def
_setupDenominator
(
self
):
"""Compute rational denominator."""
RROMPyAssert
(
self
.
_mode
,
message
=
"Cannot setup denominator."
)
vbMng
(
self
,
"INIT"
,
"Starting computation of denominator."
,
7
)
while
self
.
N
>
0
:
invD
,
fitinv
=
self
.
_computeInterpolantInverseBlocks
()
idxSamplesEff
=
list
(
range
(
self
.
S
))
if
self
.
POD
:
ev
,
eV
=
self
.
findeveVGQR
(
self
.
samplingEngine
.
RPOD
[:,
idxSamplesEff
],
invD
)
else
:
ev
,
eV
=
self
.
findeveVGExplicit
(
self
.
samplingEngine
.
samples
(
idxSamplesEff
),
invD
)
nevBad
=
checkRobustTolerance
(
ev
,
self
.
robustTol
)
if
nevBad
<=
1
:
break
if
self
.
catchInstability
>
0
:
raise
RROMPyException
((
"Instability in denominator "
"computation: eigenproblem is poorly "
"conditioned."
),
self
.
catchInstability
==
1
)
RROMPyWarning
((
"Smallest {} eigenvalues below tolerance. Reducing "
"N by 1."
)
.
format
(
nevBad
))
self
.
N
=
self
.
N
-
1
if
self
.
N
<=
0
:
self
.
_N
=
0
eV
=
np
.
ones
((
1
,
1
))
q
=
PI
()
q
.
npar
=
self
.
npar
q
.
polybasis
=
self
.
polybasis0
if
self
.
polydegreetype
==
"TOTAL"
:
q
.
coeffs
=
degreeTotalToFull
(
tuple
([
self
.
N
+
1
]
*
self
.
npar
),
self
.
npar
,
eV
[:,
0
])
else
:
q
.
coeffs
=
eV
[:,
0
]
.
reshape
([
self
.
N
+
1
]
*
self
.
npar
)
vbMng
(
self
,
"DEL"
,
"Done computing denominator."
,
7
)
return
q
,
fitinv
def
_setupNumerator
(
self
):
"""Compute rational numerator."""
RROMPyAssert
(
self
.
_mode
,
message
=
"Cannot setup numerator."
)
vbMng
(
self
,
"INIT"
,
"Starting computation of numerator."
,
7
)
self
.
_setupInterpolationIndices
()
Qevaldiag
=
polyTimesTable
(
self
.
trainedModel
.
data
.
Q
,
self
.
_musUniqueCN
,
self
.
_reorder
,
self
.
_derIdxs
,
np
.
power
(
self
.
scaleFactor
,
-
1.
))
if
self
.
POD
:
Qevaldiag
=
Qevaldiag
.
dot
(
self
.
samplingEngine
.
RPOD
.
T
)
cfun
=
totalDegreeN
if
self
.
polydegreetype
==
"TOTAL"
else
fullDegreeN
M
=
copy
(
self
.
M
)
while
self
.
S
<
cfun
(
M
,
self
.
npar
):
M
-=
1
if
M
<
self
.
M
:
RROMPyWarning
((
"M too large compared to S. Reducing M by "
"{}"
)
.
format
(
self
.
M
-
M
))
self
.
M
=
M
while
self
.
M
>=
0
:
pParRest
=
[
self
.
verbosity
>=
5
,
self
.
polydegreetype
==
"TOTAL"
,
{
"derIdxs"
:
self
.
_derIdxs
,
"reorder"
:
self
.
_reorder
,
"scl"
:
np
.
power
(
self
.
scaleFactor
,
-
1.
)}]
if
self
.
polybasis
in
ppb
:
p
=
PI
()
else
:
pParRest
=
[
self
.
radialDirectionalWeights
]
+
pParRest
pParRest
[
-
1
][
"nNearestNeighbor"
]
=
self
.
nNearestNeighbor
p
=
RBI
()
if
self
.
polybasis
in
rbpb
else
MLSI
()
if
self
.
polybasis
in
ppb
+
rbpb
:
pParRest
+=
[{
"rcond"
:
self
.
interpRcond
}]
wellCond
,
msg
=
p
.
setupByInterpolation
(
self
.
_musUniqueCN
,
Qevaldiag
,
self
.
M
,
self
.
polybasis
,
*
pParRest
)
vbMng
(
self
,
"MAIN"
,
msg
,
5
)
if
wellCond
:
break
if
self
.
catchInstability
>
0
:
raise
RROMPyException
((
"Instability in numerator computation: "
"polyfit is poorly conditioned."
),
self
.
catchInstability
==
1
)
RROMPyWarning
(
"Polyfit is poorly conditioned. Reducing M by 1."
)
self
.
M
=
self
.
M
-
1
if
self
.
M
<
0
:
raise
RROMPyException
((
"Instability in computation of numerator. "
"Aborting."
))
vbMng
(
self
,
"DEL"
,
"Done computing numerator."
,
7
)
return
p
def
setupApprox
(
self
):
"""Compute rational interpolant."""
if
self
.
checkComputedApprox
():
return
RROMPyAssert
(
self
.
_mode
,
message
=
"Cannot setup approximant."
)
vbMng
(
self
,
"INIT"
,
"Setting up {}."
.
format
(
self
.
name
()),
5
)
self
.
computeSnapshots
()
pMat
=
self
.
samplingEngine
.
samples
.
data
pMatEff
=
dot
(
self
.
HFEngine
.
C
,
pMat
)
if
self
.
approx_state
else
pMat
if
self
.
trainedModel
is
None
:
self
.
trainedModel
=
self
.
tModelType
()
self
.
trainedModel
.
verbosity
=
self
.
verbosity
self
.
trainedModel
.
timestamp
=
self
.
timestamp
datadict
=
{
"mu0"
:
self
.
mu0
,
"projMat"
:
pMatEff
,
"scaleFactor"
:
self
.
scaleFactor
,
"rescalingExp"
:
self
.
HFEngine
.
rescalingExp
}
self
.
trainedModel
.
data
=
self
.
initializeModelData
(
datadict
)[
0
]
else
:
self
.
trainedModel
=
self
.
trainedModel
self
.
trainedModel
.
data
.
projMat
=
copy
(
pMatEff
)
self
.
trainedModel
.
data
.
mus
=
copy
(
self
.
mus
)
self
.
_iterCorrector
()
self
.
trainedModel
.
data
.
approxParameters
=
copy
(
self
.
approxParameters
)
vbMng
(
self
,
"DEL"
,
"Done setting up approximant."
,
5
)
def
_iterCorrector
(
self
):
self
.
_N0
=
self
.
N
if
self
.
correctorTol
>
0.
and
(
self
.
correctorMaxIter
>
1
or
self
.
correctorForce
):
vbMng
(
self
,
"INIT"
,
"Starting correction iterations."
,
7
)
self
.
_Qhat
=
PI
()
self
.
_Qhat
.
npar
=
self
.
npar
self
.
_Qhat
.
polybasis
=
"MONOMIAL"
self
.
_Qhat
.
coeffs
=
np
.
ones
(
1
,
dtype
=
np
.
complex
)
if
self
.
POD
:
self
.
_RPODOld
=
copy
(
self
.
samplingEngine
.
RPOD
)
else
:
self
.
_samplesOld
=
copy
(
self
.
samplingEngine
.
samples
)
for
j
in
range
(
self
.
correctorMaxIter
):
if
self
.
N
>
0
:
Q
=
self
.
_setupDenominator
()[
0
]
else
:
Q
=
PI
()
Q
.
coeffs
=
np
.
ones
((
1
,)
*
self
.
npar
,
dtype
=
np
.
complex
)
Q
.
npar
=
self
.
npar
Q
.
polybasis
=
self
.
polybasis
self
.
trainedModel
.
data
.
Q
=
Q
self
.
trainedModel
.
data
.
P
=
self
.
_setupNumerator
()
self
.
_applyCorrector
(
j
)
if
self
.
N
<=
0
:
break
self
.
_N
=
self
.
_N0
del
self
.
_N0
if
self
.
correctorTol
<=
0.
or
(
self
.
correctorMaxIter
<=
1
and
not
self
.
correctorForce
):
return
if
self
.
POD
:
self
.
samplingEngine
.
RPOD
=
self
.
_RPODOld
del
self
.
_RPODOld
else
:
self
.
samplingEngine
.
samples
=
self
.
_samplesOld
del
self
.
_samplesOld
if
self
.
correctorForce
:
QOld
,
QOldBasis
=
[
1.
],
"MONOMIAL"
else
:
QOld
,
QOldBasis
=
Q
.
coeffs
,
self
.
polybasis
Q
=
polyTimes
(
self
.
_Qhat
.
coeffs
,
QOld
,
Pbasis
=
self
.
_Qhat
.
polybasis
,
Qbasis
=
QOldBasis
,
Rbasis
=
self
.
polybasis
)
del
self
.
_Qhat
gamma
=
np
.
linalg
.
norm
(
Q
)
self
.
trainedModel
.
data
.
Q
.
coeffs
=
np
.
pad
(
Q
,
(
0
,
self
.
_N
-
len
(
Q
)
+
1
),
"constant"
)
/
gamma
if
self
.
correctorForce
:
self
.
trainedModel
.
data
.
P
=
self
.
_setupNumerator
()
else
:
self
.
trainedModel
.
data
.
P
.
coeffs
/=
gamma
vbMng
(
self
,
"DEL"
,
"Terminated correction iterations."
,
7
)
def
_applyCorrector
(
self
,
j
:
int
):
if
self
.
correctorTol
<=
0.
or
(
j
>=
self
.
correctorMaxIter
-
1
and
not
self
.
correctorForce
):
self
.
_N
=
0
return
cfs
,
pls
,
_
=
rational2heaviside
(
self
.
trainedModel
.
data
.
P
,
self
.
trainedModel
.
data
.
Q
)
cfs
=
cfs
[:
self
.
N
]
if
self
.
POD
:
resEff
=
np
.
linalg
.
norm
(
cfs
,
axis
=
1
)
else
:
resEff
=
self
.
HFEngine
.
norm
(
self
.
samplingEngine
.
samples
.
dot
(
cfs
.
T
),
is_state
=
self
.
approx_state
)
resEff
/=
np
.
max
(
np
.
hstack
((
np
.
ones
((
self
.
N
,
1
)),
np
.
abs
(
pls
)
.
reshape
(
-
1
,
1
))),
axis
=
1
)
resEff
/=
np
.
mean
(
resEff
)
idxKeep
=
np
.
where
(
resEff
>=
self
.
correctorTol
)[
0
]
vbMng
(
self
,
"MAIN"
,
(
"Correction iteration no. {}: {} out of {} residuals satisfy "
"tolerance."
)
.
format
(
j
+
1
,
len
(
idxKeep
),
self
.
N
),
10
)
self
.
_N
-=
len
(
idxKeep
)
if
self
.
N
<=
0
and
not
self
.
correctorForce
:
return
for
i
in
idxKeep
:
self
.
_Qhat
.
coeffs
=
polyTimes
(
self
.
_Qhat
.
coeffs
,
[
-
pls
[
i
],
1.
],
Pbasis
=
self
.
_Qhat
.
polybasis
,
Rbasis
=
self
.
_Qhat
.
polybasis
)
self
.
_Qhat
.
coeffs
/=
np
.
linalg
.
norm
(
self
.
_Qhat
.
coeffs
)
if
self
.
N
<=
0
:
return
vbMng
(
self
,
"MAIN"
,
(
"Removing poles at (normalized positions): "
"{}."
)
.
format
(
pls
[
resEff
<
self
.
correctorTol
]),
65
)
That
=
polyTimesTable
(
self
.
_Qhat
,
self
.
_musUniqueCN
,
self
.
_reorder
,
self
.
_derIdxs
,
np
.
power
(
self
.
scaleFactor
,
-
1.
))
.
T
if
self
.
POD
:
self
.
samplingEngine
.
RPOD
=
self
.
_RPODOld
.
dot
(
That
)
else
:
self
.
samplingEngine
.
samples
=
self
.
_samplesOld
.
dot
(
That
)
def
_computeInterpolantInverseBlocks
(
self
)
->
Tuple
[
List
[
Np2D
],
Np2D
]:
"""
Compute inverse factors for minimal interpolant target functional.
"""
RROMPyAssert
(
self
.
_mode
,
message
=
"Cannot solve eigenvalue problem."
)
self
.
_setupInterpolationIndices
()
cfun
=
totalDegreeN
if
self
.
polydegreetype
==
"TOTAL"
else
fullDegreeN
N
=
copy
(
self
.
N
)
while
self
.
S
<
cfun
(
N
,
self
.
npar
):
N
-=
1
if
N
<
self
.
N
:
RROMPyWarning
((
"N too large compared to S. Reducing N by "
"{}"
)
.
format
(
self
.
N
-
N
))
self
.
N
=
N
TEGen
=
pvTP
if
self
.
polydegreetype
==
"TOTAL"
else
pvP
TEGenPar
=
[
self
.
polybasis0
,
self
.
_derIdxs
,
self
.
_reorder
,
np
.
power
(
self
.
scaleFactor
,
-
1.
)]
while
self
.
N
>=
0
:
if
self
.
polydegreetype
==
"TOTAL"
:
Neff
=
self
.
N
idxsB
=
totalDegreeMaxMask
(
self
.
N
,
self
.
npar
)
else
:
#if self.polydegreetype == "FULL":
Neff
=
[
self
.
N
]
*
self
.
npar
idxsB
=
fullDegreeMaxMask
(
self
.
N
,
self
.
npar
)
TE
=
TEGen
(
self
.
_musUniqueCN
,
Neff
,
*
TEGenPar
)
fitOut
=
customPInv
(
TE
,
rcond
=
self
.
interpRcond
,
full
=
True
)
vbMng
(
self
,
"MAIN"
,
(
"Fitting {} samples with degree {} through {}... "
"Conditioning of pseudoinverse system: {:.4e}."
)
.
format
(
TE
.
shape
[
0
],
self
.
N
,
polyfitname
(
self
.
polybasis0
),
fitOut
[
1
][
1
][
0
]
/
fitOut
[
1
][
1
][
-
1
]),
5
)
if
fitOut
[
1
][
0
]
==
TE
.
shape
[
1
]:
fitinv
=
fitOut
[
0
][
idxsB
,
:]
break
if
self
.
catchInstability
>
0
:
raise
RROMPyException
((
"Instability in denominator "
"computation: polyfit is poorly "
"conditioned."
),
self
.
catchInstability
==
1
)
RROMPyWarning
(
"Polyfit is poorly conditioned. Reducing N by 1."
)
self
.
N
=
self
.
N
-
1
invD
=
vanderInvTable
(
fitinv
,
idxsB
,
self
.
_reorder
,
self
.
_derIdxs
)
for
k
in
range
(
len
(
invD
)):
invD
[
k
]
=
dot
(
invD
[
k
],
TE
)
return
invD
,
fitinv
def
findeveVGExplicit
(
self
,
sampleE
:
sampList
,
invD
:
List
[
Np2D
])
->
Tuple
[
Np1D
,
Np2D
]:
"""
Compute explicitly eigenvalues and eigenvectors of rational denominator
matrix.
"""
RROMPyAssert
(
self
.
_mode
,
message
=
"Cannot solve eigenvalue problem."
)
nEnd
=
invD
[
0
]
.
shape
[
1
]
eWidth
=
len
(
invD
)
vbMng
(
self
,
"INIT"
,
"Building gramian matrix."
,
10
)
gramian
=
self
.
HFEngine
.
innerProduct
(
sampleE
,
sampleE
,
is_state
=
self
.
approx_state
)
G
=
np
.
zeros
((
nEnd
,
nEnd
),
dtype
=
np
.
complex
)
for
k
in
range
(
eWidth
):
G
+=
dot
(
dot
(
gramian
,
invD
[
k
])
.
T
,
invD
[
k
]
.
conj
())
.
T
vbMng
(
self
,
"DEL"
,
"Done building gramian."
,
10
)
vbMng
(
self
,
"INIT"
,
"Solving eigenvalue problem for gramian matrix."
,
7
)
ev
,
eV
=
np
.
linalg
.
eigh
(
G
)
vbMng
(
self
,
"MAIN"
,
(
"Solved eigenvalue problem of size {} with condition number "
"{:.4e}."
)
.
format
(
nEnd
,
ev
[
-
1
]
/
ev
[
0
]),
5
)
vbMng
(
self
,
"DEL"
,
"Done solving eigenvalue problem."
,
7
)
return
ev
,
eV
def
findeveVGQR
(
self
,
RPODE
:
Np2D
,
invD
:
List
[
Np2D
])
->
Tuple
[
Np1D
,
Np2D
]:
"""
Compute eigenvalues and eigenvectors of rational denominator matrix
through SVD of R factor.
"""
RROMPyAssert
(
self
.
_mode
,
message
=
"Cannot solve eigenvalue problem."
)
nEnd
=
invD
[
0
]
.
shape
[
1
]
S
=
RPODE
.
shape
[
0
]
eWidth
=
len
(
invD
)
vbMng
(
self
,
"INIT"
,
"Building half-gramian matrix stack."
,
10
)
Rstack
=
np
.
zeros
((
S
*
eWidth
,
nEnd
),
dtype
=
np
.
complex
)
for
k
in
range
(
eWidth
):
Rstack
[
k
*
S
:
(
k
+
1
)
*
S
,
:]
=
dot
(
RPODE
,
invD
[
k
])
vbMng
(
self
,
"DEL"
,
"Done building half-gramian."
,
10
)
vbMng
(
self
,
"INIT"
,
"Solving svd for square root of gramian matrix."
,
7
)
_
,
s
,
eV
=
np
.
linalg
.
svd
(
Rstack
,
full_matrices
=
False
)
ev
=
s
[::
-
1
]
eV
=
eV
[::
-
1
,
:]
.
T
.
conj
()
vbMng
(
self
,
"MAIN"
,
(
"Solved svd problem of size {} x {} with condition number "
"{:.4e}."
)
.
format
(
*
Rstack
.
shape
,
s
[
0
]
/
s
[
-
1
]),
5
)
vbMng
(
self
,
"DEL"
,
"Done solving svd."
,
7
)
return
ev
,
eV
def
getResidues
(
self
,
*
args
,
**
kwargs
)
->
Np1D
:
"""
Obtain approximant residues.
Returns:
Matrix with residues as columns.
"""
return
self
.
trainedModel
.
getResidues
(
*
args
,
**
kwargs
)
Event Timeline
Log In to Comment