I am not sure about a simple way. I have done it like this:
def condense_sysmatsbybcs(Ma,Aa,BTa,Ba,fv,fp,velbcs):
"""resolve the Dirichlet BCs and condense the sysmats
to the inner nodes"""
#auxiliary vector containing the dirichlet data
auxu = np.zeros((len(fv),1))
bcinds = []
for bc in velbcs:
bcdict = bc.get_boundary_values()
auxu[bcdict.keys(),0] = bcdict.values()
bcinds.extend(bcdict.keys())
# putting the bcs into the right hand sides
fvbc = - Aa*auxu # '*' is np.dot for csr matrices
fpbc = - Ba*auxu
# indices of the innernodes
innerinds = np.setdiff1d(range(len(fv)),bcinds).astype(np.int32)
# extract the inner nodes equation coefficients
Mc = Ma[innerinds,:][:,innerinds]
Ac = Aa[innerinds,:][:,innerinds]
fvbc= fvbc[innerinds,:]
Bc = Ba[:,innerinds]
BTc = BTa[innerinds,:]
bcvals = auxu[bcinds]
return Mc, Ac, BTc, Bc, fvbc, fpbc, bcinds, bcvals, innerinds
where Ma,Aa,BTa,Ba
are the unreduced scipy matrices representing mass, stiffness, gradient, divergence
with respect to mixed finite elements, where fv,fp
are the rhs of the momentum and the continuity equation, and where velbcs
is a list of the dolfin's DirichletBC
s. See this python module and the other files in this github repository for related functions.