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

How to assemble matrix on subspace spanned by custom basis?

+5 votes

Hello,

I have a function space $X$ on a mesh and a basis $v_1,\dots,v_n$ for a subspace $V\subseteq X$. Furthermore I have a second function space $U$ with basis $u_1,\dots,u_m$ and an operator $T:U \rightarrow X$.
I now want to assemble the matrix $L_{ij} := \langle T u_i, v_j \rangle$.

As a concrete example consider the following code snippet:

...
mesh = Mesh(...)
U = FunctionSpace(mesh, 'Lagrange', 1)
X = VectorFunctionSpace(mesh, 'DG', 0)
T = grad

u = TestFunction(U)
v = Function(X)

for j in range(n):
    v.vector()[:] = V_basis[j]
    a = inner(T(u),v)*dx
    L[:,j] = assemble(a)

where V_basis is a numpy array of length $n$, entry $j$ being an array of length X.dim(), representing the DoF vector for the basis vector $v_j$.
This code seems to work but is really slow because of the many calls to assemble.

Q: How can this be efficiently done in Fenics?

My hope is that I can somehow define a new VectorFunctionSpace $V$ by providing my basis. Then the assembly would reduce to a simple

u = TestFunction(U)
v = TestFunction(V)
a = inner(T(u),v)*dx
L = assemble(a)

But I haven't figured out how to do this. What's the best way?

Thanks

asked Feb 8, 2015 by konstantin FEniCS Novice (900 points)

1 Answer

+2 votes

On the matrix level, what you are actually doing, is a basis transformation.

Say v is a vector in the standard basis, and v_new = V*v is its representation in a different basis. (Here V is your V_basis, reshaped to a matrix).

The value of the form inner(T(u), v)*dx is independent of the basis used to express v. Thus, if A_new is the matrix you assembled for the new basis and A is as assembled in the standard basis, then it must hold that

A*v = A_new*v_new = A_new*V*v.

Accordingly, you can assemble A_new = A*inv(V) via

  1. assembling the form in the standard basis
  2. postmultiply the matrix by the inverse of the basis transformation

This, however, does not answer the interesting question, how one can use 'custom bases' in fenics.

answered Feb 9, 2015 by Jan FEniCS User (8,290 points)
edited Feb 9, 2015 by Jan

Thanks, Jan! So you suggest to assemble the "big matrix" of dimension $dim(U) \cdot dim(X)$ and then pick out the combinations I need for my basis $V$ - good idea and probably in many cases even faster until you hit the break-even point - I'll try it. Morally it contains many wasted computations though, in particular if $V$ is small compared to $X$, so it's still interesting if it's possible to define custom FE-subspaces.

If your bases have different sizes, i.e. they span different spaces, then inv(V) will not work. You will have to use projections and extensions instead.

My guess is, that you won't earn much, since I am sure that the assemble works best for the standard nodal bases that have local support.

Yes, please, let the question open. Maybe someone with deeper insight in UFL can comment on this.

Still your idea works: if $x_1,\dots,x_k$ is the standard basis for $X$ and $v_j = \sum_i a_{ij} x_i$ I get my matrix $L$ from above by just multiplying the big matrix with the $(k\times n)$-matrix $(a_{ij})$.

...