Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F63749239
reduced_basis.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
Wed, May 22, 06:11
Size
9 KB
Mime Type
text/x-python
Expires
Fri, May 24, 06:11 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
17814124
Attached To
R6746 RationalROMPy
reduced_basis.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_standard_approximant
import
GenericStandardApproximant
from
rrompy.hfengines.base.linear_affine_engine
import
checkIfAffine
from
rrompy.reduction_methods.base.reduced_basis_utils
import
\
projectAffineDecomposition
from
rrompy.utilities.base.types
import
Np1D
,
Np2D
,
List
,
Tuple
,
sampList
from
rrompy.utilities.base
import
verbosityManager
as
vbMng
from
rrompy.utilities.numerical
import
dot
from
rrompy.utilities.exception_manager
import
(
RROMPyWarning
,
RROMPyException
,
RROMPyAssert
)
__all__
=
[
'ReducedBasis'
]
class
ReducedBasis
(
GenericStandardApproximant
):
"""
ROM RB 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;
- 'scaleFactorDer': scaling factors for derivative computation;
defaults to 'AUTO';
- 'S': total number of samples current approximant relies upon;
- 'sampler': sample point generator;
- 'R': rank for Galerkin projection; defaults to 'AUTO', i.e.
maximum allowed;
- 'PODTolerance': tolerance for snapshots POD; defaults to -1.
Defaults to empty dict.
approx_state(optional): Whether to approximate state. Defaults and must
be True.
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;
- 'scaleFactorDer': scaling factors for derivative computation;
- 'R': rank for Galerkin projection;
- 'PODTolerance': tolerance for snapshots POD.
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.
scaleFactorDer: Scaling factors for derivative computation.
S: Number of solution snapshots over which current approximant is
based upon.
sampler: Sample point generator.
R: Rank for Galerkin projection.
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.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
_preInit
()
self
.
_addParametersToList
([
"R"
,
"PODTolerance"
],
[
"AUTO"
,
-
1
])
super
()
.
__init__
(
*
args
,
**
kwargs
)
checkIfAffine
(
self
.
HFEngine
,
"apply RB method"
)
if
not
self
.
approx_state
:
raise
RROMPyException
(
"Must compute RB approximation of state."
)
self
.
_postInit
()
@property
def
tModelType
(
self
):
from
.trained_model.trained_model_reduced_basis
import
(
TrainedModelReducedBasis
)
return
TrainedModelReducedBasis
@property
def
R
(
self
):
"""Value of R. Its assignment may change S."""
return
self
.
_R
@R.setter
def
R
(
self
,
R
):
if
isinstance
(
R
,
str
):
R
=
R
.
strip
()
.
replace
(
" "
,
""
)
if
"-"
not
in
R
:
R
=
R
+
"-0"
self
.
_R_isauto
,
self
.
_R_shift
=
True
,
int
(
R
.
split
(
"-"
)[
-
1
])
R
=
0
if
R
<
0
:
raise
RROMPyException
(
"R must be non-negative."
)
self
.
_R
=
R
self
.
_approxParameters
[
"R"
]
=
self
.
R
def
_setRAuto
(
self
):
self
.
R
=
max
(
0
,
self
.
S
-
self
.
_R_shift
)
vbMng
(
self
,
"MAIN"
,
"Automatically setting R to {}."
.
format
(
self
.
R
),
25
)
@property
def
PODTolerance
(
self
):
"""Value of PODTolerance."""
return
self
.
_PODTolerance
@PODTolerance.setter
def
PODTolerance
(
self
,
PODTolerance
):
self
.
_PODTolerance
=
PODTolerance
self
.
_approxParameters
[
"PODTolerance"
]
=
self
.
PODTolerance
def
_setupProjectionMatrix
(
self
):
"""Compute projection matrix."""
RROMPyAssert
(
self
.
_mode
,
message
=
"Cannot setup numerator."
)
vbMng
(
self
,
"INIT"
,
"Starting computation of projection matrix."
,
7
)
if
hasattr
(
self
,
"_R_isauto"
):
self
.
_setRAuto
()
else
:
if
self
.
S
<
self
.
R
:
RROMPyWarning
((
"R too large compared to S. Reducing R by "
"{}"
)
.
format
(
self
.
R
-
self
.
S
))
self
.
S
=
self
.
S
try
:
if
self
.
POD
:
U
,
s
,
_
=
np
.
linalg
.
svd
(
self
.
samplingEngine
.
RPOD
)
s
=
s
**
2.
else
:
Gramian
=
self
.
HFEngine
.
innerProduct
(
self
.
samplingEngine
.
samples
,
self
.
samplingEngine
.
samples
,
is_state
=
True
)
U
,
s
,
_
=
np
.
linalg
.
svd
(
Gramian
)
except
np
.
linalg
.
LinAlgError
as
e
:
raise
RROMPyException
(
e
)
snorm
=
np
.
cumsum
(
s
[::
-
1
])
/
np
.
sum
(
s
)
nPODTrunc
=
min
(
self
.
S
-
np
.
argmax
(
snorm
>
self
.
PODTolerance
),
self
.
R
)
pMat
=
dot
(
self
.
samplingEngine
.
samples
,
U
[:,
:
nPODTrunc
])
vbMng
(
self
,
"MAIN"
,
(
"Assembling {}x{} projection matrix from {} "
"samples."
)
.
format
(
*
(
pMat
.
shape
),
self
.
S
),
5
)
vbMng
(
self
,
"DEL"
,
"Done computing projection matrix."
,
7
)
return
pMat
def
setupApprox
(
self
)
->
int
:
"""Compute RB projection matrix."""
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
.
_setupProjectionMatrix
()
.
data
pMatEff
=
dot
(
self
.
HFEngine
.
C
,
pMat
)
if
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"
:
pMatEff
,
"scaleFactor"
:
self
.
scaleFactor
,
"rescalingExp"
:
self
.
HFEngine
.
rescalingExp
}
data
=
self
.
initializeModelData
(
datadict
)[
0
]
data
.
affinePoly
=
self
.
HFEngine
.
affinePoly
data
.
thAs
,
data
.
thbs
=
self
.
HFEngine
.
thAs
,
self
.
HFEngine
.
thbs
self
.
trainedModel
.
data
=
data
else
:
self
.
trainedModel
=
self
.
trainedModel
self
.
trainedModel
.
data
.
projMat
=
copy
(
pMatEff
)
self
.
trainedModel
.
data
.
mus
=
copy
(
self
.
mus
)
ARBs
,
bRBs
=
self
.
assembleReducedSystem
(
pMat
)
self
.
trainedModel
.
data
.
ARBs
=
ARBs
self
.
trainedModel
.
data
.
bRBs
=
bRBs
self
.
trainedModel
.
data
.
approxParameters
=
copy
(
self
.
approxParameters
)
vbMng
(
self
,
"DEL"
,
"Done setting up approximant."
,
5
)
return
0
def
assembleReducedSystem
(
self
,
pMat
:
sampList
=
None
,
pMatOld
:
sampList
=
None
)
\
->
Tuple
[
List
[
Np2D
],
List
[
Np1D
]]:
"""Build affine blocks of RB linear system through projections."""
if
pMat
is
None
:
self
.
setupApprox
()
ARBs
=
self
.
trainedModel
.
data
.
ARBs
bRBs
=
self
.
trainedModel
.
data
.
bRBs
else
:
self
.
HFEngine
.
buildA
()
self
.
HFEngine
.
buildb
()
vbMng
(
self
,
"INIT"
,
"Projecting affine terms of HF model."
,
10
)
ARBsOld
=
None
if
pMatOld
is
None
else
self
.
trainedModel
.
data
.
ARBs
bRBsOld
=
None
if
pMatOld
is
None
else
self
.
trainedModel
.
data
.
bRBs
ARBs
,
bRBs
=
projectAffineDecomposition
(
self
.
HFEngine
.
As
,
self
.
HFEngine
.
bs
,
pMat
,
ARBsOld
,
bRBsOld
,
pMatOld
)
vbMng
(
self
,
"DEL"
,
"Done projecting affine terms."
,
10
)
return
ARBs
,
bRBs
Event Timeline
Log In to Comment