Code Reference¶
dolfin_to_sparrays¶
-
dolfin_to_sparrays.
ass_convmat_asmatquad
(W=None, invindsw=None)[source]¶ assemble the convection matrix H, so that N(v)v = H[v.v]
for the inner nodes.
Notes
Implemented only for 2D problems
-
dolfin_to_sparrays.
get_stokessysmats
(V, Q, nu=None, bccontrol=False, cbclist=None, cbshapefuns=None)[source]¶ Assembles the system matrices for Stokes equation
in mixed FEM formulation, namely
\[\begin{split}\begin{bmatrix} A & -J' \\ J & 0 \end{bmatrix} \colon V \times Q \to V' \times Q'\end{split}\]as the discrete representation of
\[\begin{split}\begin{bmatrix} -\Delta & \text{grad} \\ \text{div} & 0 \end{bmatrix}\end{split}\]plus the velocity and pressure mass matrices
for a given trial and test space W = V * Q not considering boundary conditions.
Parameters: V : dolfin.VectorFunctionSpace
Fenics VectorFunctionSpace for the velocity
Q : dolfin.FunctionSpace
Fenics FunctionSpace for the pressure
nu : float, optional
viscosity parameter - defaults to 1
bccontrol : boolean, optional
whether boundary control (via penalized Robin conditions) is applied, defaults to False
cbclist : list, optional
list of dolfin’s Subdomain classes describing the control boundaries
cbshapefuns : list, optional
list of spatial shape functions of the control boundaries
Returns: stokesmats, dictionary :
- a dictionary with the following keys:
M
: the mass matrix of the velocity space,A
: the stiffness matrix \(\nu \int_\Omega (\nabla \phi_i, \nabla \phi_j)\)JT
: the gradient matrix,J
: the divergence matrix, andMP
: the mass matrix of the pressure spaceApbc
: (N, N) sparse matrix, the contribution of the Robin conditions to A \(\nu \int_\Gamma (\phi_i, \phi_j)\)Bpbc
: (N, k) sparse matrix, the input matrix of the Robin conditions \(\nu \int_\Gamma (\phi_i, g_k)\), where \(g_k\) is the shape function associated with the j-th control boundary segment
-
dolfin_to_sparrays.
get_convmats
(u0_dolfun=None, u0_vec=None, V=None, invinds=None, diribcs=None)[source]¶ returns the matrices related to the linearized convection
where u_0 is the linearization point
Returns: N1 : (N,N) sparse matrix
representing \((u_0 \cdot \nabla )u\)
N2 : (N,N) sparse matrix
representing \((u \cdot \nabla )u_0\)
fv : (N,1) array
representing \((u_0 \cdot \nabla )u_0\)
See also
stokes_navier_utils.get_v_conv_conts
- the convection contributions reduced to the inner nodes
-
dolfin_to_sparrays.
get_convvec
(u0_dolfun=None, V=None, u0_vec=None, femp=None, diribcs=None, invinds=None)[source]¶ return the convection vector e.g. for explicit schemes
given a dolfin function or the coefficient vector
-
dolfin_to_sparrays.
condense_sysmatsbybcs
(stms, velbcs)[source]¶ resolve the Dirichlet BCs and condense the system matrices
to the inner nodes
Parameters: stms: dict :
- of the stokes matrices with the keys
M
: the mass matrix of the velocity space,A
: the stiffness matrix,JT
: the gradient matrix,J
: the divergence matrix, andMP
: the mass matrix of the pressure space
velbcs : list
of dolfin Dirichlet boundary conditions for the velocity
Returns: stokesmatsc : dict
- a dictionary of the condensed matrices:
M
: the mass matrix of the velocity space,A
: the stiffness matrix,JT
: the gradient matrix, andJ
: the divergence matrixMP
: the mass matrix of the pressure space
rhsvecsb : dict
- a dictionary of the contributions of the boundary data to the rhs:
fv
: contribution to momentum equation,fp
: contribution to continuity equation
invinds : (N,) array
vector of indices of the inner nodes
bcinds : (K,) array
vector of indices of the boundary nodes
bcvals : (K,) array
vector of the values of the boundary nodes
-
dolfin_to_sparrays.
condense_velmatsbybcs
(A, velbcs, return_bcinfo=False)[source]¶ resolve the Dirichlet BCs, condense velocity related matrices
to the inner nodes, and compute the rhs contribution This is necessary when, e.g., the convection matrix changes with time
Parameters: A : (N,N) sparse matrix
coefficient matrix for the velocity
velbcs : list
of dolfin dolfin Dirichlet boundary conditions for the velocity
return_bcinfo : boolean, optional
if True a dict with the inner and the boundary indices is returned, defaults to False
Returns: Ac : (K, K) sparse matrix
the condensed velocity matrix
fvbc : (K, 1) array
the contribution to the rhs of the momentum equation
dict, on demand :
- with the keys
ininds
: indices of the inner nodesbcinds
: indices of the boundary nodes
-
dolfin_to_sparrays.
expand_vp_dolfunc
(V=None, Q=None, invinds=None, diribcs=None, vp=None, vc=None, pc=None, ppin=-1, **kwargs)[source]¶ expand v [and p] to the dolfin function representation
Parameters: V : dolfin.VectorFunctionSpace
FEM space of the velocity
Q : dolfin.FunctionSpace
FEM space of the pressure
invinds : (N,) array
vector of indices of the velocity nodes
diribcs : list, optional
of the (Dirichlet) velocity boundary conditions, if None it is assumed that vc already contains the bc, defaults to None
vp : (N+M,1) array, optional
solution vector of velocity and pressure
vc : (N,1) array, optional
solution vector of velocity
pc : (M,1) array, optional
solution vector of pressure
ppin : {int, None}, optional
which dof of p is used to pin the pressure, defaults to -1
Returns: v : dolfin.Function(V)
velocity as function
p : dolfin.Function(Q), optional
pressure as function
See also
expand_vecnbc_dolfunc
- for a scalar function with multiple bcs
-
dolfin_to_sparrays.
expand_vecnbc_dolfunc
(V=None, vec=None, bcindsl=None, bcvalsl=None, diribcs=None, bcsfaclist=None, invinds=None)[source]¶ expand a function vector with changing boundary conditions
the boundary conditions may not be disjoint, what is used to model spatial dependencies of a control at the boundary.
Parameters: V : dolfin.FunctionSpace
FEM space of the scalar
invinds : (N,) array
vector of indices of the velocity nodes
vec : (N,1) array
solution vector
diribcs : list
of boundary conditions
bcsfaclist : list, optional
of factors for the boundary conditions
Returns: dolfin.function :
of the vector values and the bcs
problem_setups¶
-
problem_setups.
get_sysmats
(problem='drivencavity', N=10, scheme=None, ppin=None, Re=None, nu=None, bccontrol=False, mergerhs=False, onlymesh=False)[source]¶ retrieve the system matrices for stokes flow
Parameters: problem : {‘drivencavity’, ‘cylinderwake’}
problem class
N : int
mesh parameter
nu : real, optional
kinematic viscosity, is set to L/Re if Re is provided
Re : real, optional
Reynoldsnumber, is set to L/nu if nu is provided
bccontrol : boolean, optional
whether to consider boundary control via penalized Robin defaults to False
mergerhs : boolean, optional
whether to merge the actual rhs and the contribution from the boundary conditions into one rhs
onlymesh : boolean, optional
whether to only return femp, containing the mesh and FEM spaces, defaults to False
Returns: femp : dict
- with the keys:
- V: FEM space of the velocity
- Q: FEM space of the pressure
- diribcs: list of the (Dirichlet) boundary conditions
- bcinds: indices of the boundary nodes
- bcvals: values of the boundary nodes
- invinds: indices of the inner nodes
- fv: right hand side of the momentum equation
- fp: right hand side of the continuity equation
- charlen: characteristic length of the setup
- nu: the kinematic viscosity
- Re: the Reynolds number
- odcoo: dictionary with the coordinates of the domain of observation
- cdcoo: dictionary with the coordinates of the domain of * ppin : {int, None}
- which dof of p is used to pin the pressure, typically -1 for internal flows, and None for flows with outflow
- control
stokesmatsc : dict
- a dictionary of the condensed matrices:
- M: the mass matrix of the velocity space,
- A: the stiffness matrix,
- JT: the gradient matrix, and
- J: the divergence matrix
- Jfull: the uncondensed divergence matrix
- and, if bccontrol=True, the boundary control matrices that weakly impose Arob*v = Brob*u, where
- Arob: contribution to A
- Brob: input operator
`if mergerhs` :
rhsd : dict
rhsd_vfrc and rhsd_stbc merged
`else` :
rhsd_vfrc : dict
of the dirichlet and pressure fix reduced right hand sides
rhsd_stbc : dict
- of the contributions of the boundary data to the rhs:
- fv: contribution to momentum equation,
- fp: contribution to continuity equation
Examples
femp, stokesmatsc, rhsd_vfrc, rhsd_stbc = get_sysmats(problem=’drivencavity’, N=10, nu=1e-2)
-
problem_setups.
drivcav_fems
(N, vdgree=2, pdgree=1, scheme=None, bccontrol=None)[source]¶ dictionary for the fem items of the (unit) driven cavity
Parameters: N : int
mesh parameter for the unitsquare (N gives 2*N*N triangles)
vdgree : int, optional
polynomial degree of the velocity basis functions, defaults to 2
pdgree : int, optional
polynomial degree of the pressure basis functions, defaults to 1
scheme : {None, ‘CR’, ‘TH’}
the finite element scheme to be applied, ‘CR’ for Crouzieux-Raviart, ‘TH’ for Taylor-Hood, overrides pdgree, vdgree, defaults to None
bccontrol : boolean, optional
whether to consider boundary control via penalized Robin defaults to false. TODO: not implemented yet but we need it here for consistency
Returns: femp : a dict
- of problem FEM description with the keys:
- V: FEM space of the velocity
- Q: FEM space of the pressure
- diribcs: list of the (Dirichlet) boundary conditions
- fv: right hand side of the momentum equation
- fp: right hand side of the continuity equation
- charlen: characteristic length of the setup
- odcoo: dictionary with the coordinates of the domain of observation
- cdcoo: dictionary with the coordinates of the domain of control
-
problem_setups.
cyl_fems
(refinement_level=2, vdgree=2, pdgree=1, scheme=None, bccontrol=False, verbose=False)[source]¶ dictionary for the fem items for the cylinder wake
Parameters: N : mesh parameter for the unitsquare (N gives 2*N*N triangles)
vdgree : polynomial degree of the velocity basis functions,
defaults to 2
pdgree : polynomial degree of the pressure basis functions,
defaults to 1
scheme : {None, ‘CR’, ‘TH’}
the finite element scheme to be applied, ‘CR’ for Crouzieux-Raviart, ‘TH’ for Taylor-Hood, overrides pdgree, vdgree, defaults to None
bccontrol : boolean, optional
whether to consider boundary control via penalized Robin defaults to False
Returns: femp : a dictionary with the keys:
- V: FEM space of the velocity
- Q: FEM space of the pressure
- diribcs: list of the (Dirichlet) boundary conditions
- dirip: list of the (Dirichlet) boundary conditions for the pressure
- fv: right hand side of the momentum equation
- fp: right hand side of the continuity equation
- charlen: characteristic length of the setup
- odcoo: dictionary with the coordinates of the domain of observation
- cdcoo: dictionary with the coordinates of the domain of control
- uspacedep: int that specifies in what spatial direction Bu changes. The remaining is constant
- bcsubdoms: list of subdomains that define the segments where the boundary control is applied
Notes
parts of the code were taken from the NSbench collection https://launchpad.net/nsbench
__author__ = “Kristian Valen-Sendstad <kvs@simula.no>”__date__ = “2009-10-01”__copyright__ = “Copyright (C) 2009-2010 ” + __author____license__ = “GNU GPL version 3 or any later version”
data_output_utils¶
-
data_output_utils.
output_paraview
(V=None, Q=None, fstring='nn', invinds=None, diribcs=None, vp=None, vc=None, pc=None, ppin=-1, t=None, writeoutput=True, vfile=None, pfile=None)[source]¶ write the paraview output for a solution vector vp
-
data_output_utils.
load_or_comp
(filestr=None, comprtn=None, comprtnargs={}, arraytype=None, debug=False, loadrtn=None, loadmsg='loaded ', savertn=None, savemsg='saved ', itsadict=False, numthings=1)[source]¶ routine for caching computation results on disc
Parameters: filestr: {string, list of strings, `None`} :
where to load/store the computed things, if None nothing is loaded or stored
arraytype: {`None`, ‘sparse’, ‘dense’} :
if not None, then it sets the default routines to save/load dense or sparse arrays
itsadict: boolean, optional :
whether it is python dictionary that can be JSON serialized, overrides all other options concerning arrays
savertn: fun(), optional :
routine for saving the computed results, defaults to None, i.e. no saving here
debug: boolean, optional :
no saving or loading, defaults to False