This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to remove DOF of function space on boundary with DirichletBC function?

+2 votes

I am considering an eigenvalue problem like this:

Let $V$ = { $(p_1,p_2) \in (H^1_0(\Omega))^2| d(p_1)/dy-d(p_2)/dx=0$ }.
Find $p=(p_1,p_2)$ from V and $\lambda$ s.t.
$$
(\nabla p_1, \nabla q_1) + (\nabla p_2, \nabla q_2) = \lambda (p,q), \forall q=(q_1,q_2) \in V
$$

I need to export the matrix from DOLFIN to MATLAB since the provided solver of DOLFIN gives error in computing. As the boundary condition removes the DOF of functions space,
when solving the eigenvalue problem Ax=lambda Mx, we can take sub-matrices of A and M to compute the eigenvalues with much smaller matrices.

The question is that how can I find the DOF on boundaries and then create sub-matrices of A and B using only the interior DOFs.

P.S.
In the code below, if the function space W is defined as W = ConfV (Of course, the eig-problem will change), then the columns and rows of matrix A and M corresponding to the DOFs on boundary should be zero. In this case, the sub-matrices are easily obtained by removing these zero columns and zero rows. But as below, W=ConfV*DG, the obtained matrices A and M have no zeros columns or rows. This seems little strange to me.

from dolfin import *
import scipy.io
import numpy as np
import sys


degree= 2

# Define mesh, function space
mesh = Mesh("domain.xml")

ConfV = VectorFunctionSpace(mesh, "CG", degree,2)
DG = FunctionSpace(mesh, "DG", degree-1)
W=ConfV*DG

(p,u) = TrialFunctions(W)
(q,v) = TestFunctions(W)

a = div(p)*div(q)*dx  - (p[0].dx(1)-p[1].dx(0))*v*dx - u*(q[0].dx(1)-q[1].dx(0))*dx
b = inner( p, q )*dx
vec = inner( Constant((0,0)), q)*dx

def bdry(x, on_boundary):   return on_boundary

bc1 = DirichletBC(ConfV.sub(0), Constant(0.0), bdry)
bc2 = DirichletBC(ConfV.sub(1), Constant(0.0), bdry)

A, _ = assemble_system(a,vec,[bc1,bc2])
M, _ = assemble_system(b,vec,[bc1,bc2])

bc1.zero(A); bc2.zero(A)
bc1.zero(M); bc2.zero(M)

A =  as_backend_type(A);
M =  as_backend_type(M);

## ------------------ Export Matrices --------------------------
scipy.io.savemat('matrix.mat', {'A': A.array(), 'M': M.array()  }, oned_as="column" )
asked Sep 20, 2013 by Xuefeng LIU FEniCS Novice (240 points)
edited Sep 21, 2013 by Xuefeng LIU

2 Answers

+1 vote

DOLFIN does not support the removal of constrained/Dirichlet dofs from a system.

answered Sep 21, 2013 by Garth N. Wells FEniCS Expert (35,930 points)
+3 votes

What you can/need do is to extract the submatrices 'manually'.

I have done this for Navier-Stokes Equations in the (python) function condense_sysmatsbybcs (line 171).

answered Sep 21, 2013 by Jan FEniCS User (8,290 points)

Thanks a lot! This is just what I needed.

bc.get_boundary_values()

By using this function, I can get the index for DOF on boundary and then extract submatrix.

...