Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F60489098
generic_pivoted_approximant.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
Tue, Apr 30, 14:42
Size
46 KB
Mime Type
text/x-python
Expires
Thu, May 2, 14:42 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
17359905
Attached To
R6746 RationalROMPy
generic_pivoted_approximant.py
View Options
# Copyright (C) 2018-2020 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
abc
import
abstractmethod
from
os
import
mkdir
,
remove
,
rmdir
import
numpy
as
np
from
collections.abc
import
Iterable
from
copy
import
deepcopy
as
copy
from
rrompy.reduction_methods.base.generic_approximant
import
(
GenericApproximant
)
from
.trained_model.convert_trained_model_pivoted
import
(
convertTrainedModelPivoted
)
from
rrompy.utilities.base.data_structures
import
purgeDict
,
getNewFilename
from
rrompy.utilities.poly_fitting.polynomial
import
polybases
as
ppb
from
rrompy.utilities.poly_fitting.radial_basis
import
polybases
as
rbpb
from
rrompy.utilities.poly_fitting.piecewise_linear
import
sparsekinds
as
sk
from
rrompy.utilities.base.types
import
Np2D
,
paramList
,
List
,
ListAny
from
rrompy.utilities.base
import
verbosityManager
as
vbMng
from
rrompy.utilities.base.decorators
import
get_is_mu_independent
from
rrompy.utilities.numerical.degree
import
reduceDegreeN
from
rrompy.utilities.exception_manager
import
(
RROMPyException
,
RROMPyWarning
,
RROMPy_FRAGILE
)
from
rrompy.parameter
import
emptyParameterList
,
checkParameterList
from
rrompy.utilities.parallel
import
poolRank
,
bcast
__all__
=
[
'GenericPivotedApproximantNoMatch'
,
'GenericPivotedApproximantPolyMatch'
,
'GenericPivotedApproximantPoleMatch'
]
class
GenericPivotedApproximantBase
(
GenericApproximant
):
def
__init__
(
self
,
directionPivot
:
ListAny
,
*
args
,
storeAllSamples
:
bool
=
False
,
**
kwargs
):
self
.
_preInit
()
if
len
(
directionPivot
)
>
1
:
raise
RROMPyException
((
"Exactly 1 pivot parameter allowed in pole "
"matching."
))
from
rrompy.parameter.parameter_sampling
import
(
EmptySampler
as
ES
,
SparseGridSampler
as
SG
)
self
.
_addParametersToList
([
"radialDirectionalWeightsMarginal"
],
[
-
1
],
[
"samplerPivot"
,
"SMarginal"
,
"samplerMarginal"
],
[
ES
(),
1
,
SG
([[
-
1.
],
[
1.
]])],
toBeExcluded
=
[
"sampler"
])
self
.
_directionPivot
=
directionPivot
self
.
storeAllSamples
=
storeAllSamples
if
not
hasattr
(
self
,
"_output_lvl"
):
self
.
_output_lvl
=
[]
self
.
_output_lvl
+=
[
1
/
2
]
super
()
.
__init__
(
*
args
,
**
kwargs
)
self
.
_postInit
()
def
initializeModelData
(
self
,
datadict
):
if
"directionPivot"
in
datadict
.
keys
():
from
.trained_model.trained_model_pivoted_data
import
(
TrainedModelPivotedData
)
data
=
TrainedModelPivotedData
(
datadict
[
"mu0"
],
datadict
[
"mus"
],
datadict
.
pop
(
"projMat"
),
datadict
[
"scaleFactor"
],
datadict
.
pop
(
"parameterMap"
),
datadict
[
"directionPivot"
])
return
(
data
,
[
"mu0"
,
"scaleFactor"
,
"directionPivot"
,
"mus"
])
else
:
return
super
()
.
initializeModelData
(
datadict
)
@property
def
npar
(
self
):
"""Number of parameters."""
if
hasattr
(
self
,
"_temporaryPivot"
):
return
self
.
nparPivot
return
super
()
.
npar
def
checkParameterListPivot
(
self
,
mu
:
paramList
,
check_if_single
:
bool
=
False
)
->
paramList
:
return
checkParameterList
(
mu
,
self
.
nparPivot
,
check_if_single
)
def
checkParameterListMarginal
(
self
,
mu
:
paramList
,
check_if_single
:
bool
=
False
)
->
paramList
:
return
checkParameterList
(
mu
,
self
.
nparMarginal
,
check_if_single
)
def
mapParameterList
(
self
,
*
args
,
**
kwargs
):
if
hasattr
(
self
,
"_temporaryPivot"
):
return
self
.
mapParameterListPivot
(
*
args
,
**
kwargs
)
return
super
()
.
mapParameterList
(
*
args
,
**
kwargs
)
def
mapParameterListPivot
(
self
,
mu
:
paramList
,
direct
:
str
=
"F"
,
idx
:
List
[
int
]
=
None
):
if
idx
is
None
:
idx
=
self
.
directionPivot
else
:
idx
=
[
self
.
directionPivot
[
j
]
for
j
in
idx
]
return
super
()
.
mapParameterList
(
mu
,
direct
,
idx
)
def
mapParameterListMarginal
(
self
,
mu
:
paramList
,
direct
:
str
=
"F"
,
idx
:
List
[
int
]
=
None
):
if
idx
is
None
:
idx
=
self
.
directionMarginal
else
:
idx
=
[
self
.
directionMarginal
[
j
]
for
j
in
idx
]
return
super
()
.
mapParameterList
(
mu
,
direct
,
idx
)
@property
def
mu0
(
self
):
"""Value of mu0."""
if
hasattr
(
self
,
"_temporaryPivot"
):
return
self
.
checkParameterListPivot
(
self
.
_mu0
(
self
.
directionPivot
))
return
self
.
_mu0
@mu0.setter
def
mu0
(
self
,
mu0
):
GenericApproximant
.
mu0
.
fset
(
self
,
mu0
)
@property
def
mus
(
self
):
"""Value of mus."""
return
self
.
_mus
@mus.setter
def
mus
(
self
,
mus
):
self
.
_mus
=
self
.
checkParameterList
(
mus
)
@property
def
musMarginal
(
self
):
"""Value of musMarginal."""
return
self
.
_musMarginal
@musMarginal.setter
def
musMarginal
(
self
,
musMarginal
):
self
.
_musMarginal
=
self
.
checkParameterListMarginal
(
musMarginal
)
@property
def
SMarginal
(
self
):
"""Value of SMarginal."""
return
self
.
_SMarginal
@SMarginal.setter
def
SMarginal
(
self
,
SMarginal
):
if
SMarginal
<=
0
:
raise
RROMPyException
(
"SMarginal must be positive."
)
self
.
_approxParameters
[
"SMarginal"
]
=
self
.
_SMarginal
=
SMarginal
@property
def
radialDirectionalWeightsMarginal
(
self
):
"""Value of radialDirectionalWeightsMarginal."""
return
self
.
_radialDirectionalWeightsMarginal
@radialDirectionalWeightsMarginal.setter
def
radialDirectionalWeightsMarginal
(
self
,
radialDirWeightsMarg
):
if
radialDirWeightsMarg
==
-
1
:
radialDirWeightsMarg
=
[
1.
]
*
self
.
nparMarginal
if
isinstance
(
radialDirWeightsMarg
,
Iterable
):
radialDirWeightsMarg
=
list
(
radialDirWeightsMarg
)
else
:
radialDirWeightsMarg
=
[
radialDirWeightsMarg
]
self
.
_approxParameters
[
"radialDirectionalWeightsMarginal"
]
\
=
self
.
_radialDirectionalWeightsMarginal
\
=
radialDirWeightsMarg
@property
def
directionPivot
(
self
):
"""Value of directionPivot."""
return
self
.
_directionPivot
@directionPivot.setter
def
directionPivot
(
self
,
directionPivot
):
self
.
_directionPivot
=
directionPivot
@property
def
directionMarginal
(
self
):
return
[
x
for
x
in
range
(
self
.
HFEngine
.
npar
)
\
if
x
not
in
self
.
directionPivot
]
@property
def
nparPivot
(
self
):
return
len
(
self
.
directionPivot
)
@property
def
nparMarginal
(
self
):
return
self
.
npar
-
self
.
nparPivot
@property
def
muBounds
(
self
):
"""Value of muBounds."""
return
self
.
samplerPivot
.
lims
@property
def
muBoundsMarginal
(
self
):
"""Value of muBoundsMarginal."""
return
self
.
samplerMarginal
.
lims
@property
def
sampler
(
self
):
"""Proxy of samplerPivot."""
return
self
.
_samplerPivot
@property
def
samplerPivot
(
self
):
"""Value of samplerPivot."""
return
self
.
_samplerPivot
@samplerPivot.setter
def
samplerPivot
(
self
,
samplerPivot
):
if
'generatePoints'
not
in
dir
(
samplerPivot
):
raise
RROMPyException
(
"Pivot sampler type not recognized."
)
self
.
_approxParameters
[
"samplerPivot"
]
=
self
.
_samplerPivot
\
=
copy
(
samplerPivot
)
@property
def
samplerMarginal
(
self
):
"""Value of samplerMarginal."""
return
self
.
_samplerMarginal
@samplerMarginal.setter
def
samplerMarginal
(
self
,
samplerMarginal
):
if
'generatePoints'
not
in
dir
(
samplerMarginal
):
raise
RROMPyException
(
"Marginal sampler type not recognized."
)
self
.
_approxParameters
[
"samplerMarginal"
]
=
self
.
_samplerMarginal
\
=
copy
(
samplerMarginal
)
def
resetSamples
(
self
,
keep_musMarginal
:
bool
=
False
):
"""Reset samples."""
super
()
.
resetSamples
()
self
.
_mus
=
emptyParameterList
()
if
not
keep_musMarginal
:
self
.
_musMarginal
=
emptyParameterList
()
def
setSamples
(
self
,
*
args
,
**
kwargs
):
"""Copy samplingEngine and samples."""
raise
RROMPyException
((
"Cannot transfer samples to pivoted "
"approximant."
))
@property
def
matchState
(
self
):
return
False
def
computeScaleFactor
(
self
):
"""Compute parameter rescaling factor."""
self
.
scaleFactorPivot
=
.
5
*
np
.
abs
((
self
.
mapParameterListPivot
(
self
.
muBounds
[
0
])
-
self
.
mapParameterListPivot
(
self
.
muBounds
[
1
]))[
0
])
self
.
scaleFactorMarginal
=
.
5
*
np
.
abs
((
self
.
mapParameterListMarginal
(
self
.
muBoundsMarginal
[
0
])
-
self
.
mapParameterListMarginal
(
self
.
muBoundsMarginal
[
1
]))[
0
])
self
.
scaleFactor
=
np
.
empty
(
self
.
npar
)
self
.
scaleFactor
[
self
.
directionPivot
]
=
self
.
scaleFactorPivot
self
.
scaleFactor
[
self
.
directionMarginal
]
=
self
.
scaleFactorMarginal
def
_setupTrainedModel
(
self
,
pMat
:
Np2D
,
pMatUpdate
:
bool
=
False
,
collapsed
:
bool
=
False
,
pMatOld
:
Np2D
=
None
,
forceNew
:
bool
=
False
):
if
forceNew
or
self
.
trainedModel
is
None
:
self
.
trainedModel
=
self
.
tModelType
()
self
.
trainedModel
.
verbosity
=
self
.
verbosity
self
.
trainedModel
.
timestamp
=
self
.
timestamp
datadict
=
{
"mu0"
:
self
.
mu0
,
"mus"
:
copy
(
self
.
mus
),
"projMat"
:
pMat
,
"scaleFactor"
:
self
.
scaleFactor
,
"parameterMap"
:
self
.
HFEngine
.
parameterMap
,
"directionPivot"
:
self
.
directionPivot
}
self
.
trainedModel
.
data
=
self
.
initializeModelData
(
datadict
)[
0
]
else
:
self
.
trainedModel
=
self
.
trainedModel
if
pMatUpdate
:
self
.
trainedModel
.
data
.
projMat
=
np
.
hstack
(
(
self
.
trainedModel
.
data
.
projMat
,
pMat
))
else
:
self
.
trainedModel
.
data
.
projMat
=
copy
(
pMat
)
self
.
trainedModel
.
data
.
mus
=
copy
(
self
.
mus
)
if
collapsed
:
self
.
trainedModel
.
data
.
_collapsed
=
True
self
.
trainedModel
.
data
.
musMarginal
=
copy
(
self
.
musMarginal
)
def
addSamplePoints
(
self
,
mus
:
paramList
):
"""Add global sample points to reduced model."""
raise
RROMPyException
((
"Cannot add global samples to pivoted reduced "
"model."
))
def
normApprox
(
self
,
mu
:
paramList
)
->
float
:
_PODOld
,
self
.
_POD
=
self
.
POD
,
0
result
=
super
()
.
normApprox
(
mu
)
self
.
_POD
=
_PODOld
return
result
@property
def
storedSamplesFilenames
(
self
)
->
List
[
str
]:
if
not
hasattr
(
self
,
"_sampleBaseFilename"
):
return
[]
return
[
self
.
_sampleBaseFilename
+
"{}_{}.pkl"
.
format
(
idx
+
1
,
self
.
name
())
for
idx
in
range
(
len
(
self
.
musMarginal
))]
def
purgeStoredSamples
(
self
):
if
not
hasattr
(
self
,
"_sampleBaseFilename"
):
return
for
file
in
self
.
storedSamplesFilenames
:
remove
(
file
)
rmdir
(
self
.
_sampleBaseFilename
[:
-
8
])
def
storeSamples
(
self
,
idx
:
int
=
None
):
"""Store samples to file."""
if
not
hasattr
(
self
,
"_sampleBaseFilename"
):
filenameBase
=
None
if
poolRank
()
==
0
:
foldername
=
getNewFilename
(
self
.
name
(),
"samples"
)
mkdir
(
foldername
)
filenameBase
=
foldername
+
"/sample_"
self
.
_sampleBaseFilename
=
bcast
(
filenameBase
,
force
=
True
)
if
idx
is
not
None
:
super
()
.
storeSamples
(
self
.
_sampleBaseFilename
+
str
(
idx
+
1
),
False
)
def
loadTrainedModel
(
self
,
filename
:
str
):
"""Load trained reduced model from file."""
super
()
.
loadTrainedModel
(
filename
)
self
.
_musMarginal
=
self
.
trainedModel
.
data
.
musMarginal
def
setTrainedModel
(
self
,
model
):
"""Deepcopy approximation from trained model."""
super
()
.
setTrainedModel
(
model
)
self
.
trainedModel
=
convertTrainedModelPivoted
(
self
.
trainedModel
,
self
.
tModelType
,
True
)
self
.
_preliminaryMarginalFinalization
()
self
.
_finalizeMarginalization
()
self
.
trainedModel
.
data
.
approxParameters
=
self
.
approxParameters
class
GenericPivotedApproximantNoMatch
(
GenericPivotedApproximantBase
):
"""
ROM pivoted approximant (without matching) computation for parametric
problems (ABSTRACT).
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': kind of snapshots orthogonalization; allowed values
include 0, 1/2, and 1; defaults to 1, i.e. POD;
- 'scaleFactorDer': scaling factors for derivative computation;
defaults to 'AUTO';
- '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;
- 'radialDirectionalWeightsMarginal': radial basis weights for
marginal interpolant; defaults to 1.
Defaults to empty dict.
verbosity(optional): Verbosity level. Defaults to 10.
Attributes:
HFEngine: HF problem solver.
mu0: Default parameter.
directionPivot: Pivot components.
mus: Array of 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': kind of snapshots orthogonalization;
- 'scaleFactorDer': scaling factors for derivative computation;
- 'radialDirectionalWeightsMarginal': radial basis weights for
marginal interpolant.
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.
verbosity: Verbosity level.
POD: Kind of snapshots orthogonalization.
scaleFactorDer: Scaling factors for derivative computation.
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.
radialDirectionalWeightsMarginal: Radial basis weights for marginal
interpolant.
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.
"""
@property
def
tModelType
(
self
):
from
.trained_model.trained_model_pivoted_rational_nomatch
import
(
TrainedModelPivotedRationalNoMatch
)
return
TrainedModelPivotedRationalNoMatch
def
_finalizeMarginalization
(
self
):
self
.
trainedModel
.
setupMarginalInterp
(
[
self
.
radialDirectionalWeightsMarginal
])
self
.
trainedModel
.
data
.
approxParameters
=
copy
(
self
.
approxParameters
)
def
_preliminaryMarginalFinalization
(
self
):
pass
class
GenericPivotedApproximantPolyMatch
(
GenericPivotedApproximantBase
):
"""
ROM pivoted approximant (with polynomial matching) computation for
parametric problems (ABSTRACT).
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': kind of snapshots orthogonalization; allowed values
include 0, 1/2, and 1; defaults to 1, i.e. POD;
- 'scaleFactorDer': scaling factors for derivative computation;
defaults to 'AUTO';
- 'matchState': whether to match the system state rather than the
system output; defaults to False;
- 'matchingWeight': weight for matching; defaults to 1;
- 'matchingKind': kind of matching; allowed values include 'ROTATE'
and 'PROJECT'; defaults to 'ROTATE';
- '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;
- 'polybasisMarginal': type of polynomial basis for marginal
interpolation; allowed values include 'MONOMIAL_*',
'CHEBYSHEV_*', 'LEGENDRE_*', 'NEARESTNEIGHBOR', and
'PIECEWISE_LINEAR_*'; defaults to 'MONOMIAL';
- 'paramsMarginal': dictionary of parameters for marginal
interpolation; include:
. 'MMarginal': degree of marginal interpolant; defaults to
'AUTO', i.e. maximum allowed; not for 'NEARESTNEIGHBOR' or
'PIECEWISE_LINEAR_*';
. 'nNeighborsMarginal': number of marginal nearest neighbors;
defaults to 1; only for 'NEARESTNEIGHBOR';
. 'polydegreetypeMarginal': type of polynomial degree for
marginal; defaults to 'TOTAL'; not for 'NEARESTNEIGHBOR' or
'PIECEWISE_LINEAR_*';
. 'polyTruncateTolMarginal': tolerance for truncation of
marginal interpolator; defaults to 0;
. 'interpTolMarginal': tolerance for marginal interpolation;
defaults to None; not for 'NEARESTNEIGHBOR' or
'PIECEWISE_LINEAR_*';
. 'radialDirectionalWeightsMarginalAdapt': bounds for adaptive
rescaling of marginal radial basis weights; only for
radial basis.
- 'radialDirectionalWeightsMarginal': radial basis weights for
marginal interpolant; defaults to 1.
Defaults to empty dict.
verbosity(optional): Verbosity level. Defaults to 10.
Attributes:
HFEngine: HF problem solver.
mu0: Default parameter.
directionPivot: Pivot components.
mus: Array of 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': kind of snapshots orthogonalization;
- 'scaleFactorDer': scaling factors for derivative computation;
- 'matchState': whether to match the system state rather than the
system output;
- 'matchingWeight': weight for matching;
- 'matchingKind': kind of matching;
- 'polybasisMarginal': type of polynomial basis for marginal
interpolation;
- 'paramsMarginal': dictionary of parameters for marginal
interpolation; include:
. 'MMarginal': degree of marginal interpolant;
. 'nNeighborsMarginal': number of marginal nearest neighbors;
. 'polydegreetypeMarginal': type of polynomial degree for
marginal;
. 'polyTruncateTolMarginal': tolerance for truncation of
marginal interpolator;
. 'interpTolMarginal': tolerance for marginal interpolation;
. 'radialDirectionalWeightsMarginalAdapt': bounds for adaptive
rescaling of marginal radial basis weights.
- 'radialDirectionalWeightsMarginal': radial basis weights for
marginal interpolant.
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.
verbosity: Verbosity level.
POD: Kind of snapshots orthogonalization.
scaleFactorDer: Scaling factors for derivative computation.
matchState: Whether to match the system state rather than the system
output.
matchingWeight: Weight for matching.
matchingKind: Kind of matching.
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.
polybasisMarginal: Type of polynomial basis for marginal interpolation.
paramsMarginal: Dictionary of parameters for marginal interpolation.
radialDirectionalWeightsMarginal: Radial basis weights for marginal
interpolant.
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.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
_preInit
()
self
.
_addParametersToList
([
"matchState"
,
"matchingWeight"
,
"matchingKind"
,
"polybasisMarginal"
,
"paramsMarginal"
],
[
False
,
1.
,
"ROTATE"
,
"MONOMIAL"
,
{}])
self
.
parameterMarginalList
=
[
"MMarginal"
,
"nNeighborsMarginal"
,
"polydegreetypeMarginal"
,
"polyTruncateTolMarginal"
,
"interpTolMarginal"
,
"radialDirectionalWeightsMarginalAdapt"
]
super
()
.
__init__
(
*
args
,
**
kwargs
)
self
.
_postInit
()
@property
def
tModelType
(
self
):
from
.trained_model.trained_model_pivoted_rational_polymatch
import
(
TrainedModelPivotedRationalPolyMatch
)
return
TrainedModelPivotedRationalPolyMatch
@property
def
matchState
(
self
):
"""Value of matchState."""
return
self
.
_matchState
@matchState.setter
def
matchState
(
self
,
matchState
):
self
.
_approxParameters
[
"matchState"
]
=
self
.
_matchState
=
matchState
@property
def
matchingWeight
(
self
):
"""Value of matchingWeight."""
return
self
.
_matchingWeight
@matchingWeight.setter
def
matchingWeight
(
self
,
matchingWeight
):
self
.
_approxParameters
[
"matchingWeight"
]
=
self
.
_matchingWeight
\
=
matchingWeight
@property
def
matchingKind
(
self
):
"""Value of matchingKind."""
return
self
.
_matchingKind
@matchingKind.setter
def
matchingKind
(
self
,
matchingKind
):
try
:
matchingKind
=
matchingKind
.
upper
()
.
strip
()
.
replace
(
" "
,
""
)
if
matchingKind
not
in
[
"ROTATE"
,
"PROJECT"
]:
raise
RROMPyException
((
"Prescribed matching kind not "
"recognized."
))
except
Exception
as
E
:
RROMPyWarning
(
str
(
E
)
+
" Overriding to 'ROTATE'."
)
matchingKind
=
"ROTATE"
self
.
_approxParameters
[
"matchingKind"
]
=
self
.
_matchingKind
\
=
matchingKind
@property
def
polybasisMarginal
(
self
):
"""Value of polybasisMarginal."""
return
self
.
_polybasisMarginal
@polybasisMarginal.setter
def
polybasisMarginal
(
self
,
polybasisMarginal
):
try
:
polybasisMarginal
=
polybasisMarginal
.
upper
()
.
strip
()
.
replace
(
" "
,
""
)
if
polybasisMarginal
not
in
ppb
+
rbpb
+
[
"NEARESTNEIGHBOR"
]
+
sk
:
raise
RROMPyException
((
"Prescribed marginal polybasis not "
"recognized."
))
except
Exception
as
E
:
RROMPyWarning
(
str
(
E
)
+
" Overriding to 'MONOMIAL'."
)
polybasisMarginal
=
"MONOMIAL"
if
(
polybasisMarginal
in
sk
and
not
hasattr
(
self
.
samplerMarginal
,
"npoints"
)):
raise
RROMPyException
((
"Piecewise linear marginal "
"interpolation requires sparse "
"grid-like marginal sampler."
))
self
.
_approxParameters
[
"polybasisMarginal"
]
=
self
.
_polybasisMarginal
\
=
polybasisMarginal
@property
def
paramsMarginal
(
self
):
"""Value of paramsMarginal."""
return
self
.
_paramsMarginal
@paramsMarginal.setter
def
paramsMarginal
(
self
,
paramsMarginal
):
paramsMarginal
=
purgeDict
(
paramsMarginal
,
self
.
parameterMarginalList
,
dictname
=
self
.
name
()
+
".paramsMarginal"
,
baselevel
=
1
)
keyList
=
list
(
paramsMarginal
.
keys
())
if
not
hasattr
(
self
,
"_paramsMarginal"
):
self
.
_paramsMarginal
=
{}
if
"MMarginal"
in
keyList
:
MMarg
=
paramsMarginal
[
"MMarginal"
]
elif
(
"MMarginal"
in
self
.
paramsMarginal
and
not
hasattr
(
self
,
"_MMarginal_isauto"
)):
MMarg
=
self
.
paramsMarginal
[
"MMarginal"
]
else
:
MMarg
=
"AUTO"
if
isinstance
(
MMarg
,
str
):
MMarg
=
MMarg
.
strip
()
.
replace
(
" "
,
""
)
if
"-"
not
in
MMarg
:
MMarg
=
MMarg
+
"-0"
self
.
_MMarginal_isauto
=
True
self
.
_MMarginal_shift
=
int
(
MMarg
.
split
(
"-"
)[
-
1
])
MMarg
=
0
if
MMarg
<
0
:
raise
RROMPyException
(
"MMarginal must be non-negative."
)
self
.
_paramsMarginal
[
"MMarginal"
]
=
MMarg
if
"nNeighborsMarginal"
in
keyList
:
self
.
_paramsMarginal
[
"nNeighborsMarginal"
]
=
max
(
1
,
paramsMarginal
[
"nNeighborsMarginal"
])
elif
"nNeighborsMarginal"
not
in
self
.
paramsMarginal
:
self
.
_paramsMarginal
[
"nNeighborsMarginal"
]
=
1
if
"polydegreetypeMarginal"
in
keyList
:
try
:
polydegtypeM
=
paramsMarginal
[
"polydegreetypeMarginal"
]
\
.
upper
()
.
strip
()
.
replace
(
" "
,
""
)
if
polydegtypeM
not
in
[
"TOTAL"
,
"TENSOR"
]:
raise
RROMPyException
((
"Prescribed polydegreetypeMarginal "
"not recognized."
))
except
Exception
as
E
:
RROMPyWarning
(
str
(
E
)
+
" Overriding to 'TOTAL'."
)
polydegtypeM
=
"TOTAL"
self
.
_paramsMarginal
[
"polydegreetypeMarginal"
]
=
polydegtypeM
elif
"polydegreetypeMarginal"
not
in
self
.
paramsMarginal
:
self
.
_paramsMarginal
[
"polydegreetypeMarginal"
]
=
"TOTAL"
if
"polyTruncateTolMarginal"
in
keyList
:
self
.
_paramsMarginal
[
"polyTruncateTolMarginal"
]
=
(
paramsMarginal
[
"polyTruncateTolMarginal"
])
elif
"polyTruncateTolMarginal"
not
in
self
.
paramsMarginal
:
self
.
_paramsMarginal
[
"polyTruncateTolMarginal"
]
=
0.
if
"interpTolMarginal"
in
keyList
:
self
.
_paramsMarginal
[
"interpTolMarginal"
]
=
(
paramsMarginal
[
"interpTolMarginal"
])
elif
"interpTolMarginal"
not
in
self
.
paramsMarginal
:
self
.
_paramsMarginal
[
"interpTolMarginal"
]
=
-
1
if
"radialDirectionalWeightsMarginalAdapt"
in
keyList
:
self
.
_paramsMarginal
[
"radialDirectionalWeightsMarginalAdapt"
]
=
(
paramsMarginal
[
"radialDirectionalWeightsMarginalAdapt"
])
elif
"radialDirectionalWeightsMarginalAdapt"
not
in
self
.
paramsMarginal
:
self
.
_paramsMarginal
[
"radialDirectionalWeightsMarginalAdapt"
]
=
[
-
1.
,
-
1.
]
self
.
_approxParameters
[
"paramsMarginal"
]
=
self
.
paramsMarginal
def
_setMMarginalAuto
(
self
):
if
(
self
.
polybasisMarginal
not
in
ppb
+
rbpb
or
"MMarginal"
not
in
self
.
paramsMarginal
or
"polydegreetypeMarginal"
not
in
self
.
paramsMarginal
):
raise
RROMPyException
((
"Cannot set MMarginal if "
"polybasisMarginal does not allow it."
))
self
.
paramsMarginal
[
"MMarginal"
]
=
max
(
0
,
reduceDegreeN
(
len
(
self
.
musMarginal
)
-
1
,
len
(
self
.
musMarginal
),
self
.
nparMarginal
,
self
.
paramsMarginal
[
"polydegreetypeMarginal"
])
-
self
.
_MMarginal_shift
)
vbMng
(
self
,
"MAIN"
,
(
"Automatically setting MMarginal to {}."
)
.
format
(
self
.
paramsMarginal
[
"MMarginal"
]),
25
)
def
purgeparamsMarginal
(
self
):
self
.
paramsMarginal
=
{}
paramsMbadkeys
=
[]
if
self
.
polybasisMarginal
in
ppb
+
rbpb
+
sk
:
paramsMbadkeys
+=
[
"nNeighborsMarginal"
]
if
self
.
polybasisMarginal
not
in
rbpb
:
paramsMbadkeys
+=
[
"radialDirectionalWeightsMarginalAdapt"
]
if
self
.
polybasisMarginal
in
[
"NEARESTNEIGHBOR"
]
+
sk
:
paramsMbadkeys
+=
[
"MMarginal"
,
"polydegreetypeMarginal"
,
"polyTruncateTolMarginal"
,
"interpTolMarginal"
]
if
hasattr
(
self
,
"_MMarginal_isauto"
):
del
self
.
_MMarginal_isauto
if
hasattr
(
self
,
"_MMarginal_shift"
):
del
self
.
_MMarginal_shift
for
key
in
paramsMbadkeys
:
if
key
in
self
.
_paramsMarginal
:
del
self
.
_paramsMarginal
[
key
]
self
.
_approxParameters
[
"paramsMarginal"
]
=
self
.
paramsMarginal
def
_finalizeMarginalization
(
self
):
if
self
.
polybasisMarginal
in
rbpb
+
[
"NEARESTNEIGHBOR"
]:
self
.
computeScaleFactor
()
rDWMEff
=
np
.
array
([
w
*
f
for
w
,
f
in
zip
(
self
.
radialDirectionalWeightsMarginal
,
self
.
scaleFactorMarginal
)])
if
self
.
polybasisMarginal
in
ppb
+
rbpb
+
sk
:
interpPars
=
[
self
.
polybasisMarginal
]
if
self
.
polybasisMarginal
in
ppb
+
rbpb
:
if
self
.
polybasisMarginal
in
rbpb
:
interpPars
+=
[
rDWMEff
]
interpPars
+=
[
self
.
verbosity
>=
5
,
self
.
paramsMarginal
[
"polydegreetypeMarginal"
],
self
.
paramsMarginal
[
"polyTruncateTolMarginal"
]]
if
self
.
polybasisMarginal
in
ppb
:
interpPars
+=
[{}]
else
:
# if self.polybasisMarginal in rbpb:
interpPars
+=
[{
"optimizeScalingBounds"
:
self
.
paramsMarginal
[
"radialDirectionalWeightsMarginalAdapt"
]}]
interpPars
+=
[
{
"rcond"
:
self
.
paramsMarginal
[
"interpTolMarginal"
]}]
extraPar
=
hasattr
(
self
,
"_MMarginal_isauto"
)
else
:
# if self.polybasisMarginal in sk:
idxEff
=
[
x
for
x
in
range
(
self
.
samplerMarginal
.
npoints
)
if
not
hasattr
(
self
.
trainedModel
,
"_idxExcl"
)
or
x
not
in
self
.
trainedModel
.
_idxExcl
]
extraPar
=
self
.
samplerMarginal
.
depth
[
idxEff
]
else
:
# if self.polybasisMarginal == "NEARESTNEIGHBOR":
interpPars
=
[
self
.
paramsMarginal
[
"nNeighborsMarginal"
],
rDWMEff
]
extraPar
=
None
self
.
trainedModel
.
setupMarginalInterp
(
self
,
interpPars
,
extraPar
)
self
.
trainedModel
.
data
.
approxParameters
=
copy
(
self
.
approxParameters
)
def
_preliminaryMarginalFinalization
(
self
):
if
self
.
_mode
==
RROMPy_FRAGILE
:
self
.
_matchState
=
False
vbMng
(
self
,
"INIT"
,
"Matching rational functions."
,
10
)
self
.
trainedModel
.
initializeFromRational
(
self
.
matchingWeight
,
self
.
matchingKind
,
self
.
HFEngine
,
self
.
matchState
)
vbMng
(
self
,
"DEL"
,
"Done matching rational functions."
,
10
)
def
_postApplyC
(
self
):
if
(
get_is_mu_independent
(
self
.
HFEngine
.
C
)
not
in
self
.
_output_lvl
and
self
.
POD
==
1
):
raise
RROMPyException
((
"Cannot apply mu-dependent C to "
"orthonormalized samples."
))
vbMng
(
self
,
"INIT"
,
"Extracting system output from state."
,
35
)
pMat
=
None
for
j
,
mu
in
enumerate
(
self
.
trainedModel
.
data
.
mus
):
pMatj
=
self
.
trainedModel
.
data
.
projMat
[:,
j
]
pMatj
=
np
.
expand_dims
(
self
.
HFEngine
.
applyC
(
pMatj
,
mu
),
-
1
)
if
pMat
is
None
:
pMat
=
np
.
array
(
pMatj
)
else
:
pMat
=
np
.
append
(
pMat
,
pMatj
,
axis
=
1
)
vbMng
(
self
,
"DEL"
,
"Done extracting system output."
,
35
)
self
.
trainedModel
.
data
.
projMat
=
pMat
@abstractmethod
def
setupApprox
(
self
,
*
args
,
**
kwargs
)
->
int
:
if
self
.
checkComputedApprox
():
return
-
1
self
.
purgeparamsMarginal
()
setupOK
=
super
()
.
setupApprox
(
*
args
,
**
kwargs
)
if
self
.
matchState
:
self
.
_postApplyC
()
return
setupOK
class
GenericPivotedApproximantPoleMatch
(
GenericPivotedApproximantPolyMatch
):
"""
ROM pivoted approximant (with pole matching) computation for parametric
problems (ABSTRACT).
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': kind of snapshots orthogonalization; allowed values
include 0, 1/2, and 1; defaults to 1, i.e. POD;
- 'scaleFactorDer': scaling factors for derivative computation;
defaults to 'AUTO';
- 'matchState': whether to match the system state rather than the
system output; defaults to False;
- 'matchingWeight': weight for pole matching optimization; defaults
to 1;
- 'matchingShared': required ratio of marginal points to share
resonance; defaults to 1.;
- 'badPoleCorrection': strategy for correction of bad poles;
available values include 'ERASE', 'RATIONAL', and 'POLYNOMIAL';
defaults to 'ERASE';
- '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;
- 'polybasisMarginal': type of polynomial basis for marginal
interpolation; allowed values include 'MONOMIAL_*',
'CHEBYSHEV_*', 'LEGENDRE_*', 'NEARESTNEIGHBOR', and
'PIECEWISE_LINEAR_*'; defaults to 'MONOMIAL';
- 'paramsMarginal': dictionary of parameters for marginal
interpolation; include:
. 'MMarginal': degree of marginal interpolant; defaults to
'AUTO', i.e. maximum allowed; not for 'NEARESTNEIGHBOR' or
'PIECEWISE_LINEAR_*';
. 'nNeighborsMarginal': number of marginal nearest neighbors;
defaults to 1; only for 'NEARESTNEIGHBOR';
. 'polydegreetypeMarginal': type of polynomial degree for
marginal; defaults to 'TOTAL'; not for 'NEARESTNEIGHBOR' or
'PIECEWISE_LINEAR_*';
. 'polyTruncateTolMarginal': tolerance for truncation of
marginal interpolator; defaults to 0;
. 'interpTolMarginal': tolerance for marginal interpolation;
defaults to None; not for 'NEARESTNEIGHBOR' or
'PIECEWISE_LINEAR_*';
. 'radialDirectionalWeightsMarginalAdapt': bounds for adaptive
rescaling of marginal radial basis weights; only for
radial basis.
- 'radialDirectionalWeightsMarginal': radial basis weights for
marginal interpolant; defaults to 1.
Defaults to empty dict.
verbosity(optional): Verbosity level. Defaults to 10.
Attributes:
HFEngine: HF problem solver.
mu0: Default parameter.
directionPivot: Pivot components.
mus: Array of 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': kind of snapshots orthogonalization;
- 'scaleFactorDer': scaling factors for derivative computation;
- 'matchState': whether to match the system state rather than the
system output;
- 'matchingWeight': weight for pole matching optimization;
- 'matchingShared': required ratio of marginal points to share
resonance;
- 'badPoleCorrection': strategy for correction of bad poles;
- 'polybasisMarginal': type of polynomial basis for marginal
interpolation;
- 'paramsMarginal': dictionary of parameters for marginal
interpolation; include:
. 'MMarginal': degree of marginal interpolant;
. 'nNeighborsMarginal': number of marginal nearest neighbors;
. 'polydegreetypeMarginal': type of polynomial degree for
marginal;
. 'polyTruncateTolMarginal': tolerance for truncation of
marginal interpolator;
. 'interpTolMarginal': tolerance for marginal interpolation;
. 'radialDirectionalWeightsMarginalAdapt': bounds for adaptive
rescaling of marginal radial basis weights.
- 'radialDirectionalWeightsMarginal': radial basis weights for
marginal interpolant.
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.
verbosity: Verbosity level.
POD: Kind of snapshots orthogonalization.
scaleFactorDer: Scaling factors for derivative computation.
matchState: Whether to match the system state rather than the system
output.
matchingWeight: Weight for pole matching optimization.
matchingShared: Required ratio of marginal points to share resonance.
badPoleCorrection: Strategy for correction of bad poles.
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.
polybasisMarginal: Type of polynomial basis for marginal interpolation.
paramsMarginal: Dictionary of parameters for marginal interpolation.
radialDirectionalWeightsMarginal: Radial basis weights for marginal
interpolant.
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.
"""
_allowedBadPoleCorrectionKinds
=
[
"ERASE"
,
"RATIONAL"
,
"POLYNOMIAL"
]
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
_preInit
()
self
.
_addParametersToList
([
"matchingShared"
,
"badPoleCorrection"
],
[
1.
,
"ERASE"
],
toBeExcluded
=
[
"matchingKind"
])
super
()
.
__init__
(
*
args
,
**
kwargs
)
self
.
_postInit
()
@property
def
tModelType
(
self
):
from
.trained_model.trained_model_pivoted_rational_polematch
import
(
TrainedModelPivotedRationalPoleMatch
)
return
TrainedModelPivotedRationalPoleMatch
@property
def
matchingShared
(
self
):
"""Value of matchingShared."""
return
self
.
_matchingShared
@matchingShared.setter
def
matchingShared
(
self
,
matchingShared
):
if
matchingShared
>
1.
:
RROMPyWarning
(
"Shared ratio too large. Clipping to 1."
)
matchingShared
=
1.
elif
matchingShared
<
0.
:
RROMPyWarning
(
"Shared ratio too small. Clipping to 0."
)
matchingShared
=
0.
self
.
_approxParameters
[
"matchingShared"
]
=
self
.
_matchingShared
\
=
matchingShared
@property
def
badPoleCorrection
(
self
):
"""Value of badPoleCorrection."""
return
self
.
_badPoleCorrection
@badPoleCorrection.setter
def
badPoleCorrection
(
self
,
badPoleC
):
try
:
badPoleC
=
badPoleC
.
upper
()
.
strip
()
.
replace
(
" "
,
""
)
if
badPoleC
not
in
self
.
_allowedBadPoleCorrectionKinds
:
raise
RROMPyException
((
"Prescribed badPoleCorrection not "
"recognized."
))
except
Exception
as
E
:
RROMPyWarning
(
str
(
E
)
+
" Overriding to 'ERASE'."
)
badPoleC
=
"ERASE"
self
.
_approxParameters
[
"badPoleCorrection"
]
=
self
.
_badPoleCorrection
\
=
badPoleC
def
_finalizeMarginalization
(
self
):
vbMng
(
self
,
"INIT"
,
"Checking shared ratio."
,
10
)
msg
=
self
.
trainedModel
.
checkShared
(
self
.
matchingShared
,
self
.
badPoleCorrection
)
vbMng
(
self
,
"DEL"
,
"Done checking. "
+
msg
,
10
)
super
()
.
_finalizeMarginalization
()
def
_preliminaryMarginalFinalization
(
self
):
if
self
.
_mode
==
RROMPy_FRAGILE
:
self
.
_matchState
=
False
vbMng
(
self
,
"INIT"
,
"Matching poles and residues."
,
10
)
self
.
trainedModel
.
initializeFromRational
(
self
.
matchingWeight
,
self
.
HFEngine
,
self
.
matchState
)
vbMng
(
self
,
"DEL"
,
"Done matching poles and residues."
,
10
)
Event Timeline
Log In to Comment