Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F65796017
rational_interpolant_pivoted.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
Thu, Jun 6, 07:12
Size
18 KB
Mime Type
text/x-python
Expires
Sat, Jun 8, 07:12 (2 d)
Engine
blob
Format
Raw Data
Handle
18128450
Attached To
R6746 RationalROMPy
rational_interpolant_pivoted.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
.generic_pivoted_approximant
import
GenericPivotedApproximant
,
PODGlobal
from
rrompy.reduction_methods.standard.rational_interpolant
import
(
RationalInterpolant
)
from
rrompy.utilities.base
import
verbosityManager
as
vbMng
from
rrompy.utilities.numerical
import
dot
from
rrompy.utilities.numerical.hash_derivative
import
nextDerivativeIndices
from
rrompy.utilities.exception_manager
import
RROMPyAssert
,
RROMPyWarning
from
rrompy.parameter
import
emptyParameterList
__all__
=
[
'RationalInterpolantPivoted'
]
class
RationalInterpolantPivoted
(
GenericPivotedApproximant
,
RationalInterpolant
):
"""
ROM pivoted rational interpolant (with pole matching) computation for
parametric problems.
Args:
HFEngine: HF problem solver.
mu0(optional): Default parameter. Defaults to 0.
directionPivot(optional): Pivot components. 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;
- 'scaleFactorDer': scaling factors for derivative computation;
defaults to 'AUTO';
- 'matchingWeight': weight for pole matching optimization; defaults
to 1;
- 'cutOffTolerance': tolerance for ignoring parasitic poles;
defaults to np.inf;
- 'cutOffSharedRatio': required ratio of marginal points to share
resonance in cut off strategy; defaults to 1.;
- 'S': total number of pivot samples current approximant relies
upon;
- 'samplerPivot': pivot sample point generator;
- 'SMarginal': total number of marginal samples current approximant
relies upon;
- 'samplerMarginal': marginal sample point generator;
- 'polybasis': type of polynomial basis for pivot
interpolation; defaults to 'MONOMIAL';
- 'polybasisMarginal': type of polynomial basis for marginal
interpolation; allowed values include 'MONOMIAL', 'CHEBYSHEV'
and 'LEGENDRE'; defaults to 'MONOMIAL';
- 'M': degree of rational interpolant numerator; defaults to
'AUTO', i.e. maximum allowed;
- 'N': degree of rational interpolant denominator; defaults to
'AUTO', i.e. maximum allowed;
- 'MMarginal': degree of marginal interpolant; defaults to 'AUTO',
i.e. maximum allowed;
- 'polydegreetypeMarginal': type of polynomial degree for marginal;
defaults to 'TOTAL';
- 'radialDirectionalWeights': radial basis weights for pivot
numerator; defaults to 1;
- 'radialDirectionalWeightsMarginal': radial basis weights for
marginal interpolant; defaults to 1;
- 'interpRcond': tolerance for pivot interpolation; defaults to
None;
- 'interpRcondMarginal': tolerance for marginal 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.
directionPivot: Pivot components.
mus: Array of snapshot parameters.
musPivot: Array of pivot snapshot parameters.
musMarginal: Array of marginal 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;
- 'scaleFactorDer': scaling factors for derivative computation;
- 'matchingWeight': weight for pole matching optimization;
- 'cutOffTolerance': tolerance for ignoring parasitic poles;
- 'cutOffSharedRatio': required ratio of marginal points to share
resonance in cut off strategy;
- 'polybasis': type of polynomial basis for pivot
interpolation;
- 'polybasisMarginal': type of polynomial basis for marginal
interpolation;
- 'M': degree of rational interpolant numerator;
- 'N': degree of rational interpolant denominator;
- 'MMarginal': degree of marginal interpolant;
- 'polydegreetypeMarginal': type of polynomial degree for marginal;
- 'radialDirectionalWeights': radial basis weights for pivot
numerator;
- 'radialDirectionalWeightsMarginal': radial basis weights for
marginal interpolant;
- 'interpRcond': tolerance for pivot interpolation;
- 'interpRcondMarginal': tolerance for marginal interpolation;
- '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 pivot samples current approximant relies
upon;
- 'samplerPivot': pivot sample point generator;
- 'SMarginal': total number of marginal samples current approximant
relies upon;
- 'samplerMarginal': marginal sample point generator.
approx_state: Whether to approximate state.
verbosity: Verbosity level.
POD: Whether to compute POD of snapshots.
scaleFactorDer: Scaling factors for derivative computation.
matchingWeight: Weight for pole matching optimization.
cutOffTolerance: Tolerance for ignoring parasitic poles.
cutOffSharedRatio: Required ratio of marginal points to share resonance
in cut off strategy.
S: Total number of pivot samples current approximant relies upon.
samplerPivot: Pivot sample point generator.
SMarginal: Total number of marginal samples current approximant relies
upon.
samplerMarginal: Marginal sample point generator.
polybasis: Type of polynomial basis for pivot interpolation.
polybasisMarginal: Type of polynomial basis for marginal interpolation.
M: Numerator degree of approximant.
N: Denominator degree of approximant.
MMarginal: Degree of marginal interpolant.
polydegreetypeMarginal: Type of polynomial degree for marginal.
radialDirectionalWeights: Radial basis weights for pivot numerator.
radialDirectionalWeightsMarginal: Radial basis weights for marginal
interpolant.
interpRcond: Tolerance for pivot interpolation.
interpRcondMarginal: Tolerance for marginal interpolation.
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 pivot parameter values.
muBoundsMarginal: list of bounds for marginal 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
(
toBeExcluded
=
[
"polydegreetype"
,
"sampler"
])
super
()
.
__init__
(
*
args
,
**
kwargs
)
self
.
_postInit
()
@property
def
scaleFactorDer
(
self
):
"""Value of scaleFactorDer."""
if
self
.
_scaleFactorDer
==
"NONE"
:
return
1.
if
self
.
_scaleFactorDer
==
"AUTO"
:
return
self
.
scaleFactorPivot
return
self
.
_scaleFactorDer
@scaleFactorDer.setter
def
scaleFactorDer
(
self
,
scaleFactorDer
):
if
hasattr
(
self
,
"_scaleFactorDer"
):
scaleFactorDerold
=
self
.
scaleFactorDer
else
:
scaleFactorDerold
=
-
1
if
isinstance
(
scaleFactorDer
,
(
str
,)):
scaleFactorDer
=
scaleFactorDer
.
upper
()
self
.
_scaleFactorDer
=
scaleFactorDer
self
.
_approxParameters
[
"scaleFactorDer"
]
=
self
.
_scaleFactorDer
if
scaleFactorDerold
!=
self
.
_scaleFactorDer
:
self
.
resetSamples
()
@property
def
polydegreetype
(
self
):
"""Value of polydegreetype."""
return
"TOTAL"
@polydegreetype.setter
def
polydegreetype
(
self
,
polydegreetype
):
RROMPyWarning
((
"polydegreetype is used just to simplify inheritance, "
"and its value cannot be changed from 'TOTAL'."
))
@property
def
polybasis0
(
self
):
if
"_"
in
self
.
polybasis
:
return
self
.
polybasis
.
split
(
"_"
)[
0
]
return
self
.
polybasis
@property
def
correctorTol
(
self
):
"""Value of correctorTol."""
return
self
.
_correctorTol
@correctorTol.setter
def
correctorTol
(
self
,
correctorTol
):
if
correctorTol
<
0.
or
(
correctorTol
>
0.
and
self
.
nparPivot
>
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
.
nparPivot
>
1
):
RROMPyWarning
((
"Overriding prescribed max number of corrector "
"iterations to 1."
))
correctorMaxIter
=
1
self
.
_correctorMaxIter
=
correctorMaxIter
self
.
_approxParameters
[
"correctorMaxIter"
]
=
self
.
correctorMaxIter
def
_setupInterpolationIndices
(
self
):
"""Setup parameters for polyvander."""
RROMPyAssert
(
self
.
_mode
,
message
=
"Cannot setup interpolation indices."
)
if
(
self
.
_musUniqueCN
is
None
or
len
(
self
.
_reorder
)
!=
len
(
self
.
musPivot
)):
try
:
muPC
=
self
.
trainedModel
.
centerNormalizePivot
(
self
.
musPivot
)
except
:
muPC
=
self
.
trainedModel
.
centerNormalize
(
self
.
musPivot
)
self
.
_musUniqueCN
,
musIdxsTo
,
musIdxs
,
musCount
=
(
muPC
.
unique
(
return_index
=
True
,
return_inverse
=
True
,
return_counts
=
True
))
self
.
_musUnique
=
self
.
musPivot
[
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
.
nparPivot
,
cnt
)
jIdx
=
np
.
nonzero
(
musIdxs
==
j
)[
0
]
self
.
_reorder
[
jIdx
]
=
np
.
arange
(
filled
,
filled
+
cnt
)
filled
+=
cnt
def
computeSnapshots
(
self
):
"""Compute snapshots of solution map."""
RROMPyAssert
(
self
.
_mode
,
message
=
"Cannot start snapshot computation."
)
self
.
computeScaleFactor
()
if
self
.
samplingEngine
.
nsamplesTot
!=
self
.
S
*
self
.
SMarginal
:
self
.
resetSamples
()
self
.
samplingEngine
.
scaleFactor
=
self
.
scaleFactorDer
vbMng
(
self
,
"INIT"
,
"Starting computation of snapshots."
,
5
)
self
.
musPivot
=
self
.
samplerPivot
.
generatePoints
(
self
.
S
)
while
len
(
self
.
musPivot
)
>
self
.
S
:
self
.
musPivot
.
pop
()
self
.
musMarginal
=
self
.
samplerMarginal
.
generatePoints
(
self
.
SMarginal
)
while
len
(
self
.
musMarginal
)
>
self
.
SMarginal
:
self
.
musMarginal
.
pop
()
self
.
mus
=
emptyParameterList
()
self
.
mus
.
reset
((
self
.
S
*
self
.
SMarginal
,
self
.
HFEngine
.
npar
))
self
.
samplingEngine
.
resetHistory
(
self
.
SMarginal
)
for
j
,
muMarg
in
enumerate
(
self
.
musMarginal
):
for
k
in
range
(
j
*
self
.
S
,
(
j
+
1
)
*
self
.
S
):
self
.
mus
.
data
[
k
,
self
.
directionPivot
]
=
(
self
.
musPivot
[
k
-
j
*
self
.
S
]
.
data
)
self
.
mus
.
data
[
k
,
self
.
directionMarginal
]
=
muMarg
.
data
self
.
samplingEngine
.
iterSample
(
self
.
musPivot
,
self
.
musMarginal
)
self
.
_finalizeSnapshots
()
vbMng
(
self
,
"DEL"
,
"Done computing snapshots."
,
5
)
def
_finalizeSnapshots
(
self
):
if
self
.
POD
==
PODGlobal
:
self
.
samplingEngine
.
coalesceSamples
(
self
.
interpRcondMarginal
)
else
:
self
.
samplingEngine
.
coalesceSamples
()
def
setupApprox
(
self
)
->
int
:
"""Compute rational interpolant."""
if
self
.
checkComputedApprox
():
return
-
1
RROMPyAssert
(
self
.
_mode
,
message
=
"Cannot setup approximant."
)
vbMng
(
self
,
"INIT"
,
"Setting up {}."
.
format
(
self
.
name
()),
5
)
self
.
computeSnapshots
()
pMat
=
self
.
samplingEngine
.
samplesCoalesced
.
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
,
"directionPivot"
:
self
.
directionPivot
}
self
.
trainedModel
.
data
=
self
.
initializeModelData
(
datadict
)[
0
]
else
:
self
.
trainedModel
=
self
.
trainedModel
self
.
trainedModel
.
data
.
projMat
=
copy
(
pMatEff
)
N0
=
copy
(
self
.
N
)
Qs
,
Ps
=
[
None
]
*
len
(
self
.
musMarginal
),
[
None
]
*
len
(
self
.
musMarginal
)
self
.
_temporaryPivot
=
1
padLeft
=
0
if
self
.
POD
:
self
.
_RPODOldPivot
=
copy
(
self
.
samplingEngine
.
RPODCoalesced
)
else
:
self
.
_samplesOldPivot
=
copy
(
self
.
samplingEngine
.
samples
)
padRight
=
self
.
samplingEngine
.
nsamplesTot
self
.
_scaleFactorOldPivot
=
copy
(
self
.
scaleFactor
)
self
.
scaleFactor
=
self
.
scaleFactorPivot
for
j
in
range
(
len
(
self
.
musMarginal
)):
vbMng
(
self
,
"MAIN"
,
"Building marginal model no. {} at {}."
.
format
(
j
+
1
,
self
.
musMarginal
[
j
]),
5
)
self
.
N
=
N0
if
self
.
POD
:
self
.
samplingEngine
.
RPOD
=
(
self
.
_RPODOldPivot
[:,
padLeft
:
padLeft
+
self
.
S
])
else
:
self
.
samplingEngine
.
samples
=
self
.
_samplesOldPivot
[
j
]
padRight
-=
self
.
S
self
.
verbosity
-=
5
self
.
_iterCorrector
()
self
.
verbosity
+=
5
Qs
[
j
]
=
copy
(
self
.
trainedModel
.
data
.
Q
)
Ps
[
j
]
=
copy
(
self
.
trainedModel
.
data
.
P
)
del
self
.
trainedModel
.
data
.
Q
,
self
.
trainedModel
.
data
.
P
if
not
self
.
POD
:
Ps
[
j
]
.
pad
(
padLeft
,
padRight
)
padLeft
+=
self
.
S
if
self
.
POD
:
self
.
samplingEngine
.
RPODCoalesced
=
copy
(
self
.
_RPODOldPivot
)
del
self
.
_RPODOldPivot
else
:
self
.
samplingEngine
.
samples
=
copy
(
self
.
_samplesOldPivot
)
del
self
.
_samplesOldPivot
self
.
scaleFactor
=
self
.
_scaleFactorOldPivot
del
self
.
_temporaryPivot
,
self
.
_scaleFactorOldPivot
self
.
trainedModel
.
data
.
mus
=
copy
(
self
.
mus
)
self
.
trainedModel
.
data
.
musPivot
=
copy
(
self
.
musPivot
)
self
.
trainedModel
.
data
.
musMarginal
=
copy
(
self
.
musMarginal
)
self
.
trainedModel
.
data
.
Qs
,
self
.
trainedModel
.
data
.
Ps
=
Qs
,
Ps
vbMng
(
self
,
"INIT"
,
"Matching poles."
,
10
)
self
.
trainedModel
.
initializeFromRational
(
self
.
HFEngine
,
self
.
matchingWeight
,
self
.
POD
==
PODGlobal
,
False
)
vbMng
(
self
,
"DEL"
,
"Done matching poles."
,
10
)
self
.
_finalizeMarginalization
()
vbMng
(
self
,
"DEL"
,
"Done setting up approximant."
,
5
)
return
0
Event Timeline
Log In to Comment