Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F85139724
laplace_base_problem_engine.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, Sep 27, 01:24
Size
10 KB
Mime Type
text/x-python
Expires
Sun, Sep 29, 01:24 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
21133876
Attached To
R6746 RationalROMPy
laplace_base_problem_engine.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/>.
#
import
numpy
as
np
import
fenics
as
fen
from
rrompy.hfengines.base.linear_affine_engine
import
LinearAffineEngine
from
rrompy.hfengines.base.fenics_engine_base
import
FenicsEngineBase
from
rrompy.utilities.base.types
import
paramVal
from
rrompy.solver.fenics
import
(
fenZERO
,
fenONE
,
H1NormMatrix
,
Hminus1NormMatrix
)
from
rrompy.utilities.base
import
verbosityManager
as
vbMng
from
rrompy.parameter
import
checkParameter
from
rrompy.solver.fenics
import
fenics2Sparse
,
fenics2Vector
__all__
=
[
'LaplaceBaseProblemEngine'
]
class
LaplaceBaseProblemEngine
(
LinearAffineEngine
,
FenicsEngineBase
):
"""
Solver for generic Laplace problems.
- \nabla \cdot (a \nabla u) = f in \Omega
u = u0 on \Gamma_D
\partial_nu = g1 on \Gamma_N
\partial_nu + h u = g2 on \Gamma_R
Attributes:
verbosity: Verbosity level.
BCManager: Boundary condition manager.
V: Real FE space.
u: Generic trial functions for variational form evaluation.
v: Generic test functions for variational form evaluation.
As: Scipy sparse array representation (in CSC format) of As.
bs: Numpy array representation of bs.
cs: Numpy array representation of cs.
energyNormMatrix: Scipy sparse matrix representing inner product over
V.
energyNormDualMatrix: Scipy sparse matrix representing dual inner
product between Riesz representers V-V.
degree_threshold: Threshold for ufl expression interpolation degree.
omega: Value of omega.
diffusivity: Value of a.
forcingTerm: Value of f.
DirichletDatum: Value of u0.
NeumannDatum: Value of g1.
RobinDatumG: Value of g2.
RobinDatumH: Value of h.
DirichletBoundary: Function handle to \Gamma_D.
NeumannBoundary: Function handle to \Gamma_N.
RobinBoundary: Function handle to \Gamma_R.
ds: Boundary measure 2-tuple (resp. for Neumann and Robin boundaries).
dsToBeSet: Whether ds needs to be set.
"""
_energyDualNormCompress
=
None
def
__init__
(
self
,
mu0
:
paramVal
=
[],
degree_threshold
:
int
=
np
.
inf
,
verbosity
:
int
=
10
,
timestamp
:
bool
=
True
):
super
()
.
__init__
(
degree_threshold
=
degree_threshold
,
verbosity
=
verbosity
,
timestamp
=
timestamp
)
self
.
_affinePoly
=
True
self
.
mu0
=
checkParameter
(
mu0
)
self
.
npar
=
self
.
mu0
.
shape
[
1
]
self
.
omega
=
np
.
abs
(
self
.
mu0
(
0
,
0
))
if
self
.
npar
>
0
else
0.
self
.
diffusivity
=
fenONE
self
.
forcingTerm
=
fenZERO
self
.
DirichletDatum
=
fenZERO
self
.
NeumannDatum
=
fenZERO
self
.
RobinDatumG
=
fenZERO
self
.
RobinDatumH
=
fenZERO
@property
def
diffusivity
(
self
):
"""Value of a."""
return
self
.
_diffusivity
@diffusivity.setter
def
diffusivity
(
self
,
diffusivity
):
self
.
resetAs
()
if
not
isinstance
(
diffusivity
,
(
list
,
tuple
,)):
diffusivity
=
[
diffusivity
,
fenZERO
]
self
.
_diffusivity
=
diffusivity
@property
def
forcingTerm
(
self
):
"""Value of f."""
return
self
.
_forcingTerm
@forcingTerm.setter
def
forcingTerm
(
self
,
forcingTerm
):
self
.
resetbs
()
if
not
isinstance
(
forcingTerm
,
(
list
,
tuple
,)):
forcingTerm
=
[
forcingTerm
,
fenZERO
]
self
.
_forcingTerm
=
forcingTerm
@property
def
DirichletDatum
(
self
):
"""Value of u0."""
return
self
.
_DirichletDatum
@DirichletDatum.setter
def
DirichletDatum
(
self
,
DirichletDatum
):
self
.
resetbs
()
if
not
isinstance
(
DirichletDatum
,
(
list
,
tuple
,)):
DirichletDatum
=
[
DirichletDatum
,
fenZERO
]
self
.
_DirichletDatum
=
DirichletDatum
@property
def
NeumannDatum
(
self
):
"""Value of g1."""
return
self
.
_NeumannDatum
@NeumannDatum.setter
def
NeumannDatum
(
self
,
NeumannDatum
):
self
.
resetbs
()
if
not
isinstance
(
NeumannDatum
,
(
list
,
tuple
,)):
NeumannDatum
=
[
NeumannDatum
,
fenZERO
]
self
.
_NeumannDatum
=
NeumannDatum
@property
def
RobinDatumG
(
self
):
"""Value of g2."""
return
self
.
_RobinDatumG
@RobinDatumG.setter
def
RobinDatumG
(
self
,
RobinDatumG
):
self
.
resetbs
()
if
not
isinstance
(
RobinDatumG
,
(
list
,
tuple
,)):
RobinDatumG
=
[
RobinDatumG
,
fenZERO
]
self
.
_RobinDatumG
=
RobinDatumG
@property
def
RobinDatumH
(
self
):
"""Value of h."""
return
self
.
_RobinDatumH
@RobinDatumH.setter
def
RobinDatumH
(
self
,
RobinDatumH
):
self
.
resetAs
()
if
not
isinstance
(
RobinDatumH
,
(
list
,
tuple
,)):
RobinDatumH
=
[
RobinDatumH
,
fenZERO
]
self
.
_RobinDatumH
=
RobinDatumH
@property
def
DirichletBoundary
(
self
):
"""Function handle to DirichletBoundary."""
return
self
.
BCManager
.
DirichletBoundary
@DirichletBoundary.setter
def
DirichletBoundary
(
self
,
DirichletBoundary
):
self
.
resetAs
()
self
.
resetbs
()
self
.
BCManager
.
DirichletBoundary
=
DirichletBoundary
@property
def
NeumannBoundary
(
self
):
"""Function handle to NeumannBoundary."""
return
self
.
BCManager
.
NeumannBoundary
@NeumannBoundary.setter
def
NeumannBoundary
(
self
,
NeumannBoundary
):
self
.
resetAs
()
self
.
resetbs
()
self
.
dsToBeSet
=
True
self
.
BCManager
.
NeumannBoundary
=
NeumannBoundary
@property
def
RobinBoundary
(
self
):
"""Function handle to RobinBoundary."""
return
self
.
BCManager
.
RobinBoundary
@RobinBoundary.setter
def
RobinBoundary
(
self
,
RobinBoundary
):
self
.
resetAs
()
self
.
resetbs
()
self
.
dsToBeSet
=
True
self
.
BCManager
.
RobinBoundary
=
RobinBoundary
def
buildEnergyNormForm
(
self
):
"""
Build sparse matrix (in CSR format) representative of scalar product.
"""
vbMng
(
self
,
"INIT"
,
"Assembling energy matrix."
,
20
)
self
.
_energyNormMatrix
=
H1NormMatrix
(
self
.
V
,
np
.
abs
(
self
.
omega
)
**
2
)
vbMng
(
self
,
"DEL"
,
"Done assembling energy matrix."
,
20
)
def
buildEnergyNormDualForm
(
self
):
"""
Build sparse matrix (in CSR format) representative of dual scalar
product without duality.
"""
vbMng
(
self
,
"INIT"
,
"Assembling energy dual matrix."
,
20
)
self
.
_energyNormDualMatrix
=
Hminus1NormMatrix
(
self
.
V
,
np
.
abs
(
self
.
omega
)
**
2
,
compressRank
=
self
.
_energyDualNormCompress
)
vbMng
(
self
,
"DEL"
,
"Done assembling energy dual matrix."
,
20
)
def
buildA
(
self
):
"""Build terms of operator of linear system."""
if
self
.
thAs
[
0
]
is
None
:
self
.
thAs
=
self
.
getMonomialWeights
(
self
.
nAs
)
if
self
.
As
[
0
]
is
None
:
self
.
autoSetDS
()
vbMng
(
self
,
"INIT"
,
"Assembling operator term A0."
,
20
)
DirichletBC0
=
fen
.
DirichletBC
(
self
.
V
,
fenZERO
,
self
.
DirichletBoundary
)
aRe
,
aIm
=
self
.
diffusivity
hRe
,
hIm
=
self
.
RobinDatumH
termNames
=
[
"diffusivity"
,
"RobinDatumH"
]
parsRe
=
self
.
iterReduceQuadratureDegree
(
zip
([
aRe
,
hRe
],
[
x
+
"Real"
for
x
in
termNames
]))
parsIm
=
self
.
iterReduceQuadratureDegree
(
zip
([
aIm
,
hIm
],
[
x
+
"Imag"
for
x
in
termNames
]))
a0Re
=
(
aRe
*
fen
.
inner
(
fen
.
grad
(
self
.
u
),
fen
.
grad
(
self
.
v
))
*
fen
.
dx
+
hRe
*
self
.
u
*
self
.
v
*
self
.
ds
(
1
))
a0Im
=
(
aIm
*
fen
.
inner
(
fen
.
grad
(
self
.
u
),
fen
.
grad
(
self
.
v
))
*
fen
.
dx
+
hIm
*
self
.
u
*
self
.
v
*
self
.
ds
(
1
))
self
.
As
[
0
]
=
(
fenics2Sparse
(
a0Re
,
parsRe
,
DirichletBC0
,
1
)
+
1.j
*
fenics2Sparse
(
a0Im
,
parsIm
,
DirichletBC0
,
0
))
vbMng
(
self
,
"DEL"
,
"Done assembling operator term."
,
20
)
def
buildb
(
self
):
"""Build terms of operator of linear system."""
if
self
.
thbs
[
0
]
is
None
:
self
.
thbs
=
self
.
getMonomialWeights
(
self
.
nbs
)
if
self
.
bs
[
0
]
is
None
:
self
.
autoSetDS
()
vbMng
(
self
,
"INIT"
,
"Assembling forcing term b0."
,
20
)
u0Re
,
u0Im
=
self
.
DirichletDatum
fRe
,
fIm
=
self
.
forcingTerm
g1Re
,
g1Im
=
self
.
NeumannDatum
g2Re
,
g2Im
=
self
.
RobinDatumG
termNames
=
[
"forcingTerm"
,
"NeumannDatum"
,
"RobinDatumG"
]
parsRe
=
self
.
iterReduceQuadratureDegree
(
zip
([
fRe
,
g1Re
,
g2Re
],
[
x
+
"Real"
for
x
in
termNames
]))
parsIm
=
self
.
iterReduceQuadratureDegree
(
zip
([
fIm
,
g1Im
,
g2Im
],
[
x
+
"Imag"
for
x
in
termNames
]))
L0Re
=
(
fRe
*
self
.
v
*
fen
.
dx
+
g1Re
*
self
.
v
*
self
.
ds
(
0
)
+
g2Re
*
self
.
v
*
self
.
ds
(
1
))
L0Im
=
(
fIm
*
self
.
v
*
fen
.
dx
+
g1Im
*
self
.
v
*
self
.
ds
(
0
)
+
g2Im
*
self
.
v
*
self
.
ds
(
1
))
DBCR
=
fen
.
DirichletBC
(
self
.
V
,
u0Re
,
self
.
DirichletBoundary
)
DBCI
=
fen
.
DirichletBC
(
self
.
V
,
u0Im
,
self
.
DirichletBoundary
)
self
.
bs
[
0
]
=
(
fenics2Vector
(
L0Re
,
parsRe
,
DBCR
,
1
)
+
1.j
*
fenics2Vector
(
L0Im
,
parsIm
,
DBCI
,
1
))
vbMng
(
self
,
"DEL"
,
"Done assembling forcing term."
,
20
)
Event Timeline
Log In to Comment