Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F63010389
sampling_engine_base.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, May 17, 03:08
Size
8 KB
Mime Type
text/x-python
Expires
Sun, May 19, 03:08 (2 d)
Engine
blob
Format
Raw Data
Handle
17720314
Attached To
R6746 RationalROMPy
sampling_engine_base.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/>.
#
import
numpy
as
np
from
rrompy.utilities.base.types
import
(
Np1D
,
HFEng
,
strLst
,
paramVal
,
paramList
,
sampList
)
from
rrompy.utilities.base
import
verbosityDepth
from
rrompy.utilities.exception_manager
import
RROMPyWarning
from
rrompy.parameter
import
(
emptyParameterList
,
checkParameter
,
checkParameterList
)
from
rrompy.sampling
import
emptySampleList
__all__
=
[
'SamplingEngineBase'
]
class
SamplingEngineBase
:
"""HERE"""
def
__init__
(
self
,
HFEngine
:
HFEng
,
verbosity
:
int
=
10
,
timestamp
:
bool
=
True
,
allowRepeatedSamples
:
bool
=
True
):
self
.
verbosity
=
verbosity
self
.
timestamp
=
timestamp
self
.
allowRepeatedSamples
=
allowRepeatedSamples
if
self
.
verbosity
>=
10
:
verbosityDepth
(
"INIT"
,
"Initializing sampling engine of type {}."
.
format
(
self
.
name
()),
timestamp
=
self
.
timestamp
)
self
.
HFEngine
=
HFEngine
if
self
.
verbosity
>=
10
:
verbosityDepth
(
"DEL"
,
"Done initializing sampling engine."
,
timestamp
=
self
.
timestamp
)
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
resetHistory
(
self
):
self
.
samples
=
emptySampleList
()
self
.
nsamples
=
0
self
.
mus
=
emptyParameterList
()
self
.
_derIdxs
=
[]
def
popSample
(
self
):
if
hasattr
(
self
,
"nsamples"
)
and
self
.
nsamples
>
1
:
if
self
.
samples
.
shape
[
1
]
>
self
.
nsamples
:
RROMPyWarning
((
"More than 'nsamples' memory allocated for "
"samples. Popping empty sample column."
))
self
.
nsamples
+=
1
self
.
nsamples
-=
1
self
.
samples
.
pop
()
self
.
mus
.
pop
()
else
:
self
.
resetHistory
()
def
preallocateSamples
(
self
,
u
:
sampList
,
mu
:
paramVal
,
n
:
int
):
self
.
samples
.
reset
((
u
.
shape
[
0
],
n
),
u
.
dtype
)
self
.
samples
[
0
]
=
u
mu
=
checkParameter
(
mu
,
self
.
HFEngine
.
npar
)
self
.
mus
.
reset
((
n
,
self
.
HFEngine
.
npar
))
self
.
mus
[
0
]
=
mu
[
0
]
@property
def
HFEngine
(
self
):
"""Value of HFEngine. Its assignment resets history."""
return
self
.
_HFEngine
@HFEngine.setter
def
HFEngine
(
self
,
HFEngine
):
self
.
_HFEngine
=
HFEngine
self
.
resetHistory
()
def
solveLS
(
self
,
mu
:
paramList
=
[],
RHS
:
sampList
=
None
,
homogeneized
:
bool
=
False
)
->
sampList
:
"""
Solve linear system.
Args:
mu: Parameter value.
Returns:
Solution of system.
"""
mu
,
_
=
checkParameterList
(
mu
,
self
.
HFEngine
.
npar
)
if
self
.
verbosity
>=
15
:
verbosityDepth
(
"INIT"
,
"Solving HF model for mu = {}."
.
format
(
mu
),
timestamp
=
self
.
timestamp
)
u
=
self
.
HFEngine
.
solve
(
mu
,
RHS
,
homogeneized
)
if
self
.
verbosity
>=
15
:
verbosityDepth
(
"DEL"
,
"Done solving HF model."
,
timestamp
=
self
.
timestamp
)
return
u
def
plotSamples
(
self
,
name
:
str
=
"u"
,
save
:
str
=
None
,
what
:
strLst
=
'all'
,
saveFormat
:
str
=
"eps"
,
saveDPI
:
int
=
100
,
show
:
bool
=
True
,
**
figspecs
):
"""
Do some nice plots of the samples.
Args:
name(optional): Name to be shown as title of the plots. Defaults to
'u'.
save(optional): Where to save plot(s). Defaults to None, i.e. no
saving.
what(optional): Which plots to do. If list, can contain 'ABS',
'PHASE', 'REAL', 'IMAG'. If str, same plus wildcard 'ALL'.
Defaults to 'ALL'.
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.
figspecs(optional key args): Optional arguments for matplotlib
figure creation.
"""
for
j
in
range
(
self
.
nsamples
):
self
.
HFEngine
.
plot
(
self
.
samples
[
j
],
name
=
"{}_{}"
.
format
(
name
,
j
),
save
=
save
,
what
=
what
,
saveFormat
=
saveFormat
,
saveDPI
=
saveDPI
,
show
=
show
,
**
figspecs
)
def
outParaviewSamples
(
self
,
name
:
str
=
"u"
,
folders
:
bool
=
True
,
filename
:
str
=
"out"
,
times
:
Np1D
=
None
,
what
:
strLst
=
'all'
,
forceNewFile
:
bool
=
True
,
filePW
=
None
):
"""
Output samples to ParaView file.
Args:
name(optional): Base name to be used for data output.
folders(optional): Whether to split output in folders.
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.
filePW(optional): Fenics File entity (for time series).
"""
if
times
is
None
:
times
=
[
0.
]
*
self
.
nsamples
for
j
in
range
(
self
.
nsamples
):
self
.
HFEngine
.
outParaview
(
self
.
samples
[
j
],
name
=
"{}_{}"
.
format
(
name
,
j
),
filename
=
"{}_{}"
.
format
(
filename
,
j
),
time
=
times
[
j
],
what
=
what
,
forceNewFile
=
forceNewFile
,
folder
=
folders
,
filePW
=
filePW
)
def
outParaviewTimeDomainSamples
(
self
,
omegas
:
Np1D
=
None
,
timeFinal
:
Np1D
=
None
,
periodResolution
:
int
=
20
,
name
:
str
=
"u"
,
folders
:
bool
=
True
,
filename
:
str
=
"out"
,
forceNewFile
:
bool
=
True
):
"""
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.
folders(optional): Whether to split output in folders.
filename(optional): Name of output file.
forceNewFile(optional): Whether to create new output file.
"""
if
omegas
is
None
:
omegas
=
np
.
real
(
self
.
mus
)
if
not
isinstance
(
timeFinal
,
(
list
,
tuple
,)):
timeFinal
=
[
timeFinal
]
*
self
.
nsamples
for
j
in
range
(
self
.
nsamples
):
self
.
HFEngine
.
outParaviewTimeDomain
(
self
.
samples
[
j
],
omega
=
omegas
[
j
],
timeFinal
=
timeFinal
[
j
],
periodResolution
=
periodResolution
,
name
=
"{}_{}"
.
format
(
name
,
j
),
filename
=
"{}_{}"
.
format
(
filename
,
j
),
forceNewFile
=
forceNewFile
,
folder
=
folders
)
Event Timeline
Log In to Comment