Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F68726471
generic_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
Fri, Jun 28, 16:31
Size
25 KB
Mime Type
text/x-python
Expires
Sun, Jun 30, 16:31 (2 d)
Engine
blob
Format
Raw Data
Handle
18579221
Attached To
R6746 RationalROMPy
generic_approximant.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
abc
import
abstractmethod
import
numpy
as
np
from
itertools
import
product
as
iterprod
from
copy
import
deepcopy
as
copy
from
os
import
remove
as
osrm
from
rrompy.sampling.base.sampling_engine_base
import
SamplingEngineBase
from
rrompy.utilities.base.types
import
Np1D
,
DictAny
,
HFEng
,
sampleEng
,
strLst
from
rrompy.utilities.base
import
purgeDict
,
verbosityDepth
,
getNewFilename
from
rrompy.utilities.exception_manager
import
(
RROMPyException
,
modeAssert
,
RROMPy_READY
,
RROMPy_FRAGILE
)
from
rrompy.utilities.base
import
pickleDump
,
pickleLoad
__all__
=
[
'GenericApproximant'
]
def
addNormFieldToClass
(
self
,
fieldName
):
def
objFunc
(
self
,
mu
:
complex
,
homogeneized
:
bool
=
False
)
->
float
:
getObj
=
getattr
(
self
.
__class__
,
"get"
+
fieldName
)
return
self
.
HFEngine
.
norm
(
getObj
(
self
,
mu
,
homogeneized
))
setattr
(
self
.
__class__
,
"norm"
+
fieldName
,
objFunc
)
def
addPlotFieldToClass
(
self
,
fieldName
):
def
objFunc
(
self
,
mu
:
complex
,
name
:
str
=
fieldName
,
save
:
str
=
None
,
what
:
strLst
=
'all'
,
saveFormat
:
str
=
"eps"
,
saveDPI
:
int
=
100
,
show
:
bool
=
True
,
homogeneized
:
bool
=
False
,
**
figspecs
):
uV
=
getattr
(
self
.
__class__
,
"get"
+
fieldName
)(
self
,
mu
,
homogeneized
)
self
.
HFEngine
.
plot
(
uV
,
name
=
name
,
save
=
save
,
what
=
what
,
saveFormat
=
saveFormat
,
saveDPI
=
saveDPI
,
show
=
show
,
**
figspecs
)
setattr
(
self
.
__class__
,
"plot"
+
fieldName
,
objFunc
)
def
addOutParaviewFieldToClass
(
self
,
fieldName
):
def
objFunc
(
self
,
mu
:
complex
,
name
:
str
=
fieldName
,
filename
:
str
=
"out"
,
time
:
float
=
0.
,
what
:
strLst
=
'all'
,
forceNewFile
:
bool
=
True
,
folder
:
bool
=
False
,
filePW
=
None
,
homogeneized
:
bool
=
False
):
uV
=
getattr
(
self
.
__class__
,
"get"
+
fieldName
)(
self
,
mu
,
homogeneized
)
self
.
HFEngine
.
outParaview
(
uV
,
name
=
name
,
filename
=
filename
,
time
=
time
,
what
=
what
,
forceNewFile
=
forceNewFile
,
folder
=
folder
,
filePW
=
filePW
)
setattr
(
self
.
__class__
,
"outParaview"
+
fieldName
,
objFunc
)
def
addOutParaviewTimeDomainFieldToClass
(
self
,
fieldName
):
def
objFunc
(
self
,
mu
:
complex
,
omega
:
float
=
None
,
timeFinal
:
float
=
None
,
periodResolution
:
int
=
20
,
name
:
str
=
fieldName
,
filename
:
str
=
"out"
,
forceNewFile
:
bool
=
True
,
folder
:
bool
=
False
,
homogeneized
:
bool
=
False
):
uV
=
getattr
(
self
.
__class__
,
"get"
+
fieldName
)(
self
,
mu
,
homogeneized
)
if
omega
is
None
:
omega
=
np
.
real
(
mu
)
self
.
HFEngine
.
outParaviewTimeDomain
(
uV
,
omega
=
omega
,
timeFinal
=
timeFinal
,
periodResolution
=
periodResolution
,
name
=
name
,
filename
=
filename
,
forceNewFile
=
forceNewFile
,
folder
=
folder
)
setattr
(
self
.
__class__
,
"outParaviewTimeDomain"
+
fieldName
,
objFunc
)
class
GenericApproximant
:
"""
ABSTRACT
ROM approximant 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.
Defaults to empty dict.
homogeneized(optional): Whether to homogeneize Dirichlet BCs. Defaults
to False.
verbosity(optional): Verbosity level. Defaults to 10.
Attributes:
HFEngine: HF problem solver.
trainedModel: Trained model evaluator.
mu0: Default parameter.
homogeneized: Whether to homogeneize Dirichlet BCs.
approxParameters: Dictionary containing values for main parameters of
approximant. Recognized keys are in parameterList.
parameterList: Recognized keys of approximant parameters:
- 'POD': whether to compute POD of snapshots.
verbosity: Verbosity level.
POD: Whether to compute POD of snapshots.
samplingEngine: Sampling engine.
uHF: High fidelity solution with wavenumber lastSolvedHF as numpy
complex vector.
lastSolvedHF: Wavenumber corresponding to last computed high fidelity
solution.
uApp: Last evaluated approximant as numpy complex vector.
"""
__all__
+=
[
ftype
+
dtype
for
ftype
,
dtype
in
iterprod
(
[
"norm"
,
"plot"
,
"outParaview"
,
"outParaviewTimeDomain"
],
[
"HF"
,
"RHS"
,
"Approx"
,
"Res"
,
"Err"
])]
def
__init__
(
self
,
HFEngine
:
HFEng
,
mu0
:
complex
=
0
,
approxParameters
:
DictAny
=
{},
homogeneized
:
bool
=
False
,
verbosity
:
int
=
10
,
timestamp
:
bool
=
True
):
self
.
_preInit
()
self
.
_mode
=
RROMPy_READY
self
.
verbosity
=
verbosity
self
.
timestamp
=
timestamp
if
self
.
verbosity
>=
10
:
verbosityDepth
(
"INIT"
,
(
"Initializing approximant engine of "
"type {}."
)
.
format
(
self
.
name
()),
timestamp
=
self
.
timestamp
)
self
.
_HFEngine
=
HFEngine
self
.
_addParametersToList
([
"POD"
])
self
.
mu0
=
mu0
self
.
homogeneized
=
homogeneized
self
.
approxParameters
=
approxParameters
self
.
_postInit
()
### add norm{HF,RHS,Approx,Res,Err} methods
"""
Compute norm of * at arbitrary parameter.
Args:
mu: Target parameter.
homogeneized(optional): Whether to remove Dirichlet BC. Defaults to
False.
Returns:
Target norm of *.
"""
for
objName
in
[
"HF"
,
"RHS"
,
"Approx"
,
"Res"
,
"Err"
]:
addNormFieldToClass
(
self
,
objName
)
### add plot{HF,RHS,Approx,Res,Err} methods
"""
Do some nice plots of * at arbitrary parameter.
Args:
mu: Target parameter.
name(optional): Name to be shown as title of the plots. Defaults to
'u'.
what(optional): Which plots to do. If list, can contain 'ABS',
'PHASE', 'REAL', 'IMAG'. If str, same plus wildcard 'ALL'.
Defaults to 'ALL'.
save(optional): Where to save plot(s). Defaults to None, i.e. no
saving.
saveFormat(optional): Format for saved plot(s). Defaults to "eps".
saveDPI(optional): DPI for saved plot(s). Defaults to 100.
show(optional): Whether to show figure. Defaults to True.
homogeneized(optional): Whether to remove Dirichlet BC. Defaults to
False.
figspecs(optional key args): Optional arguments for matplotlib
figure creation.
"""
for
objName
in
[
"HF"
,
"RHS"
,
"Approx"
,
"Res"
,
"Err"
]:
addPlotFieldToClass
(
self
,
objName
)
### add outParaview{HF,RHS,Approx,Res,Err} methods
"""
Output * to ParaView file.
Args:
mu: Target parameter.
name(optional): Base name to be used for data output.
filename(optional): Name of output file.
time(optional): Timestamp.
what(optional): Which plots to do. If list, can contain 'MESH',
'ABS', 'PHASE', 'REAL', 'IMAG'. If str, same plus wildcard
'ALL'. Defaults to 'ALL'.
forceNewFile(optional): Whether to create new output file.
filePW(optional): Fenics File entity (for time series).
homogeneized(optional): Whether to remove Dirichlet BC. Defaults to
False.
"""
for
objName
in
[
"HF"
,
"RHS"
,
"Approx"
,
"Res"
,
"Err"
]:
addOutParaviewFieldToClass
(
self
,
objName
)
### add outParaviewTimeDomain{HF,RHS,Approx,Res,Err} methods
"""
Output * to ParaView file, converted to time domain.
Args:
mu: Target parameter.
omega(optional): frequency.
timeFinal(optional): final time of simulation.
periodResolution(optional): number of time steps per period.
name(optional): Base name to be used for data output.
filename(optional): Name of output file.
forceNewFile(optional): Whether to create new output file.
homogeneized(optional): Whether to remove Dirichlet BC. Defaults to
False.
"""
for
objName
in
[
"HF"
,
"RHS"
,
"Approx"
,
"Res"
,
"Err"
]:
addOutParaviewTimeDomainFieldToClass
(
self
,
objName
)
def
_preInit
(
self
):
if
not
hasattr
(
self
,
"depth"
):
self
.
depth
=
0
else
:
self
.
depth
+=
1
def
_addParametersToList
(
self
,
what
:
strLst
):
if
not
hasattr
(
self
,
"parameterList"
):
self
.
parameterList
=
[]
self
.
parameterList
+=
what
def
_postInit
(
self
):
if
self
.
depth
==
0
:
if
self
.
verbosity
>=
10
:
verbosityDepth
(
"DEL"
,
"Done initializing."
,
timestamp
=
self
.
timestamp
)
del
self
.
depth
else
:
self
.
depth
-=
1
def
name
(
self
)
->
str
:
return
self
.
__class__
.
__name__
def
__str__
(
self
)
->
str
:
return
self
.
name
()
def
__repr__
(
self
)
->
str
:
return
self
.
__str__
()
+
" at "
+
hex
(
id
(
self
))
def
setupSampling
(
self
,
SamplingEngine
:
sampleEng
=
SamplingEngineBase
):
"""Setup sampling engine."""
modeAssert
(
self
.
_mode
,
message
=
"Cannot setup sampling engine."
)
self
.
samplingEngine
=
SamplingEngine
(
self
.
HFEngine
,
verbosity
=
self
.
verbosity
)
@property
def
HFEngine
(
self
):
"""Value of HFEngine."""
return
self
.
_HFEngine
@HFEngine.setter
def
HFEngine
(
self
,
HFEngine
):
RROMPyException
(
"Cannot change HFEngine."
)
@property
def
mu0
(
self
):
"""Value of mu0."""
return
self
.
_mu0
@mu0.setter
def
mu0
(
self
,
mu0
):
if
not
(
hasattr
(
self
,
"_mu0"
)
and
np
.
isclose
(
mu0
,
self
.
mu0
)):
self
.
resetSamples
()
self
.
_mu0
=
mu0
@property
def
approxParameters
(
self
):
"""Value of approximant parameters."""
return
self
.
_approxParameters
@approxParameters.setter
def
approxParameters
(
self
,
approxParams
):
if
not
hasattr
(
self
,
"approxParameters"
):
self
.
_approxParameters
=
{}
approxParameters
=
purgeDict
(
approxParams
,
self
.
parameterList
,
dictname
=
self
.
name
()
+
".approxParameters"
,
baselevel
=
1
)
keyList
=
list
(
approxParameters
.
keys
())
if
"POD"
in
keyList
:
self
.
POD
=
approxParameters
[
"POD"
]
elif
not
hasattr
(
self
,
"_POD"
)
or
self
.
_POD
is
None
:
self
.
POD
=
True
@property
def
POD
(
self
):
"""Value of POD."""
return
self
.
_POD
@POD.setter
def
POD
(
self
,
POD
):
if
hasattr
(
self
,
"_POD"
):
PODold
=
self
.
POD
else
:
PODold
=
-
1
self
.
_POD
=
POD
self
.
_approxParameters
[
"POD"
]
=
self
.
POD
if
PODold
!=
self
.
POD
:
self
.
samplingEngine
=
None
self
.
resetSamples
()
@property
def
homogeneized
(
self
):
"""Value of homogeneized."""
return
self
.
_homogeneized
@homogeneized.setter
def
homogeneized
(
self
,
homogeneized
):
if
not
hasattr
(
self
,
"_homogeneized"
):
self
.
_homogeneized
=
None
if
homogeneized
!=
self
.
homogeneized
:
self
.
_homogeneized
=
homogeneized
self
.
resetSamples
()
def
setHF
(
self
,
muHF
:
complex
,
uHF
:
Np1D
):
"""Assign high fidelity solution."""
self
.
lastSolvedHF
=
copy
(
muHF
)
self
.
uHF
=
copy
(
uHF
)
def
solveHF
(
self
,
mu
:
complex
=
None
):
"""
Find high fidelity solution with original parameters and arbitrary
parameter.
Args:
mu: Target parameter.
"""
if
mu
is
None
:
mu
=
self
.
mu0
if
not
np
.
isclose
(
self
.
lastSolvedHF
,
mu
):
self
.
setHF
(
mu
,
self
.
samplingEngine
.
solveLS
(
mu
,
homogeneized
=
self
.
homogeneized
))
def
resetSamples
(
self
):
"""Reset samples."""
if
hasattr
(
self
,
"samplingEngine"
)
and
self
.
samplingEngine
is
not
None
:
self
.
samplingEngine
.
resetHistory
()
else
:
self
.
setupSampling
()
self
.
trainedModel
=
None
self
.
setHF
(
np
.
nan
,
None
)
self
.
_mode
=
RROMPy_READY
def
plotSamples
(
self
,
name
:
str
=
"u"
,
save
:
str
=
None
,
what
:
strLst
=
'all'
,
saveFormat
:
str
=
"eps"
,
saveDPI
:
int
=
100
,
**
figspecs
):
"""
Do some nice plots of the samples.
Args:
name(optional): Name to be shown as title of the plots. Defaults to
'u'.
what(optional): Which plots to do. If list, can contain 'ABS',
'PHASE', 'REAL', 'IMAG'. If str, same plus wildcard 'ALL'.
Defaults to 'ALL'.
save(optional): Where to save plot(s). Defaults to None, i.e. no
saving.
saveFormat(optional): Format for saved plot(s). Defaults to "eps".
saveDPI(optional): DPI for saved plot(s). Defaults to 100.
figspecs(optional key args): Optional arguments for matplotlib
figure creation.
"""
modeAssert
(
self
.
_mode
,
message
=
"Cannot plot samples."
)
self
.
samplingEngine
.
plotSamples
(
name
=
name
,
save
=
save
,
what
=
what
,
saveFormat
=
saveFormat
,
saveDPI
=
saveDPI
,
**
figspecs
)
def
outParaviewSamples
(
self
,
name
:
str
=
"u"
,
filename
:
str
=
"out"
,
times
:
Np1D
=
None
,
what
:
strLst
=
'all'
,
forceNewFile
:
bool
=
True
,
folders
:
bool
=
False
,
filePW
=
None
):
"""
Output samples to ParaView file.
Args:
name(optional): Base name to be used for data output.
filename(optional): Name of output file.
times(optional): Timestamps.
what(optional): Which plots to do. If list, can contain 'MESH',
'ABS', 'PHASE', 'REAL', 'IMAG'. If str, same plus wildcard
'ALL'. Defaults to 'ALL'.
forceNewFile(optional): Whether to create new output file.
folders(optional): Whether to split output in folders.
filePW(optional): Fenics File entity (for time series).
"""
modeAssert
(
self
.
_mode
,
message
=
"Cannot output samples."
)
self
.
samplingEngine
.
outParaviewSamples
(
name
=
name
,
filename
=
filename
,
times
=
times
,
what
=
what
,
forceNewFile
=
forceNewFile
,
folders
=
folders
,
filePW
=
filePW
)
def
outParaviewTimeDomainSamples
(
self
,
omegas
:
Np1D
=
None
,
timeFinal
:
Np1D
=
None
,
periodResolution
:
int
=
20
,
name
:
str
=
"u"
,
filename
:
str
=
"out"
,
forceNewFile
:
bool
=
True
,
folders
:
bool
=
False
):
"""
Output samples to ParaView file, converted to time domain.
Args:
omegas(optional): frequencies.
timeFinal(optional): final time of simulation.
periodResolution(optional): number of time steps per period.
name(optional): Base name to be used for data output.
filename(optional): Name of output file.
forceNewFile(optional): Whether to create new output file.
folders(optional): Whether to split output in folders.
"""
modeAssert
(
self
.
_mode
,
message
=
"Cannot output samples."
)
self
.
samplingEngine
.
outParaviewTimeDomainSamples
(
omegas
=
omegas
,
timeFinal
=
timeFinal
,
periodResolution
=
periodResolution
,
name
=
name
,
filename
=
filename
,
forceNewFile
=
forceNewFile
,
folders
=
folders
)
def
setApprox
(
self
,
model
):
"""Deepcopy approximation from trained model."""
if
hasattr
(
model
,
"storeTrainedModel"
):
verb
=
model
.
verbosity
model
.
verbosity
=
0
fileOut
=
model
.
storeTrainedModel
()
model
.
verbosity
=
verb
else
:
try
:
fileOut
=
getNewFilename
(
"trained_model"
,
"pkl"
)
pickleDump
(
model
.
data
.
__dict__
,
fileOut
)
except
:
RROMPyException
((
"Failed to store model data. Parameter model "
"must have either storeTrainedModel or "
"data.__dict__ properties."
))
self
.
loadTrainedModel
(
fileOut
)
osrm
(
fileOut
)
@abstractmethod
def
setupApprox
(
self
):
"""
Setup approximant. (ABSTRACT)
Any specialization should include something like
if self.checkComputedApprox():
return
modeAssert(self._mode, message = "Cannot setup approximant.")
...
self.trainedModel = ...
self.trainedModel.data = ...
self.trainedModel.data.approxParameters = copy(
self.approxParameters)
"""
pass
def
checkComputedApprox
(
self
)
->
bool
:
"""
Check if setup of new approximant is not needed.
Returns:
True if new setup is not needed. False otherwise.
"""
return
self
.
_mode
==
RROMPy_FRAGILE
or
(
self
.
trainedModel
is
not
None
and
self
.
trainedModel
.
data
.
approxParameters
==
self
.
approxParameters
)
def
evalApproxReduced
(
self
,
mu
:
complex
):
"""
Evaluate reduced representation of approximant at arbitrary parameter.
Args:
mu: Target parameter.
"""
self
.
setupApprox
()
self
.
uAppReduced
=
self
.
trainedModel
.
getApproxReduced
(
mu
)
def
evalApprox
(
self
,
mu
:
complex
):
"""
Evaluate approximant at arbitrary parameter.
Args:
mu: Target parameter.
"""
self
.
setupApprox
()
self
.
uApp
=
self
.
trainedModel
.
getApprox
(
mu
)
def
getHF
(
self
,
mu
:
complex
,
homogeneized
:
bool
=
False
)
->
Np1D
:
"""
Get HF solution at arbitrary parameter.
Args:
mu: Target parameter.
homogeneized(optional): Whether to remove Dirichlet BC. Defaults to
False.
Returns:
HFsolution.
"""
self
.
solveHF
(
mu
)
if
self
.
homogeneized
and
not
homogeneized
:
return
self
.
uHF
+
self
.
HFEngine
.
liftDirichletData
(
mu
)
if
not
self
.
homogeneized
and
homogeneized
:
return
self
.
uHF
-
self
.
HFEngine
.
liftDirichletData
(
mu
)
return
self
.
uHF
def
getRHS
(
self
,
mu
:
complex
,
homogeneized
:
bool
=
False
)
->
Np1D
:
"""
Get linear system RHS at arbitrary parameter.
Args:
mu: Target parameter.
homogeneized(optional): Whether to remove Dirichlet BC. Defaults to
False.
Returns:
Linear system RHS.
"""
return
self
.
HFEngine
.
residual
(
None
,
mu
,
homogeneized
=
homogeneized
)
def
getApproxReduced
(
self
,
mu
:
complex
)
->
Np1D
:
"""
Get approximant at arbitrary parameter.
Args:
mu: Target parameter.
Returns:
Reduced approximant.
"""
self
.
evalApproxReduced
(
mu
)
return
self
.
uAppReduced
def
getApprox
(
self
,
mu
:
complex
,
homogeneized
:
bool
=
False
)
->
Np1D
:
"""
Get approximant at arbitrary parameter.
Args:
mu: Target parameter.
homogeneized(optional): Whether to remove Dirichlet BC. Defaults to
False.
Returns:
Approximant.
"""
self
.
evalApprox
(
mu
)
if
self
.
homogeneized
and
not
homogeneized
:
return
self
.
uApp
+
self
.
HFEngine
.
liftDirichletData
(
mu
)
if
not
self
.
homogeneized
and
homogeneized
:
return
self
.
uApp
-
self
.
HFEngine
.
liftDirichletData
(
mu
)
return
self
.
uApp
def
getRes
(
self
,
mu
:
complex
,
homogeneized
:
bool
=
False
)
->
Np1D
:
"""
Get residual at arbitrary parameter.
Args:
mu: Target parameter.
homogeneized(optional): Whether to remove Dirichlet BC. Defaults to
False.
Returns:
Approximant residual.
"""
return
self
.
HFEngine
.
residual
(
self
.
getApprox
(
mu
,
homogeneized
),
mu
,
homogeneized
=
homogeneized
)
def
getErr
(
self
,
mu
:
complex
,
homogeneized
:
bool
=
False
)
->
Np1D
:
"""
Get error at arbitrary parameter.
Args:
mu: Target parameter.
homogeneized(optional): Whether to remove Dirichlet BC. Defaults to
False.
Returns:
Approximant error.
"""
return
self
.
getApprox
(
mu
,
homogeneized
)
-
self
.
getHF
(
mu
,
homogeneized
)
def
getPoles
(
self
)
->
Np1D
:
"""
Obtain approximant poles.
Returns:
Numpy complex vector of poles.
"""
self
.
setupApprox
()
if
self
.
verbosity
>=
20
:
verbosityDepth
(
"INIT"
,
"Computing poles of model."
,
timestamp
=
self
.
timestamp
)
poles
=
self
.
trainedModel
.
getPoles
()
if
self
.
verbosity
>=
20
:
verbosityDepth
(
"DEL"
,
"Done computing poles."
,
timestamp
=
self
.
timestamp
)
return
poles
def
storeTrainedModel
(
self
,
filenameBase
:
str
=
"trained_model"
,
forceNewFile
:
bool
=
True
):
"""Store trained reduced model to file."""
self
.
setupApprox
()
if
self
.
verbosity
>=
20
:
verbosityDepth
(
"INIT"
,
"Storing trained model to file."
,
timestamp
=
self
.
timestamp
)
if
forceNewFile
:
filename
=
getNewFilename
(
filenameBase
,
"pkl"
)
else
:
filename
=
"{}.pkl"
.
format
(
filenameBase
)
pickleDump
(
self
.
trainedModel
.
data
.
__dict__
,
filename
)
if
self
.
verbosity
>=
20
:
verbosityDepth
(
"DEL"
,
"Done storing trained model."
,
timestamp
=
self
.
timestamp
)
return
filename
def
loadTrainedModel
(
self
,
filename
:
str
):
"""Load trained reduced model from file."""
if
self
.
verbosity
>=
20
:
verbosityDepth
(
"INIT"
,
"Loading pre-trained model from file."
,
timestamp
=
self
.
timestamp
)
datadict
=
pickleLoad
(
filename
)
name
=
datadict
.
pop
(
"name"
)
if
name
==
"TrainedModelPade"
:
from
rrompy.reduction_methods.trained_model
import
\
TrainedModelPade
as
tModel
elif
name
==
"TrainedModelRB"
:
from
rrompy.reduction_methods.trained_model
import
\
TrainedModelRB
as
tModel
else
:
raise
RROMPyException
((
"Trained model name not recognized. "
"Loading failed."
))
self
.
mu0
=
datadict
.
pop
(
"mu0"
)
from
rrompy.reduction_methods.trained_model
import
TrainedModelData
trainedModel
=
tModel
()
trainedModel
.
verbosity
=
self
.
verbosity
trainedModel
.
timestamp
=
self
.
timestamp
data
=
TrainedModelData
(
name
,
self
.
mu0
,
datadict
.
pop
(
"projMat"
),
datadict
.
pop
(
"rescalingExp"
))
if
"mus"
in
datadict
:
data
.
mus
=
datadict
.
pop
(
"mus"
)
approxParameters
=
datadict
.
pop
(
"approxParameters"
)
data
.
approxParameters
=
copy
(
approxParameters
)
if
"sampler"
in
approxParameters
:
self
.
_approxParameters
[
"sampler"
]
=
approxParameters
.
pop
(
"sampler"
)
self
.
approxParameters
=
copy
(
approxParameters
)
if
"mus"
in
data
.
__dict__
:
self
.
mus
=
copy
(
data
.
mus
)
if
name
==
"TrainedModelPade"
:
self
.
scaleFactor
=
datadict
.
pop
(
"scaleFactor"
)
data
.
scaleFactor
=
self
.
scaleFactor
for
key
in
datadict
:
setattr
(
data
,
key
,
datadict
[
key
])
trainedModel
.
data
=
data
self
.
trainedModel
=
trainedModel
self
.
_mode
=
RROMPy_FRAGILE
if
self
.
verbosity
>=
20
:
verbosityDepth
(
"DEL"
,
"Done loading pre-trained model."
,
timestamp
=
self
.
timestamp
)
Event Timeline
Log In to Comment