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

Assembling a matrix when terms are from different function spaces

0 votes
from dolfin import *
mesh = UnitIntervalMesh(2)

V = FunctionSpace(mesh, 'CG', 1)
u = TrialFunction(V)
v = TestFunction(V)

k1 = Expression('sin(x[0])*exp(x[0])', degree=2)
a1 = k1*u*v*dx
A1 = as_backend_type(assemble(a1)).array()

k2 = Expression('sin(x[0])*exp(x[0])', degree=3)
a2 = k2*u*v*dx
A2 = as_backend_type(assemble(a2)).array()

print A1
print A2

I can see the entries of A1 and A2 are different, and I'm curious how exactly the degree of k1/k2 makes different here. What happens when a matrix is assembled? I thought k1/k2 would be projected to CG1 first, but apparently, I was wrong.

In [63]: A1
Out[63]: 
array([[ 0.30926758,  0.12185024,  0.        ],
       [ 0.12185024,  0.27557479,  0.02776519],
       [ 0.        ,  0.02776519,  0.0251803 ]])

In [64]: A2
Out[64]: 
array([[ 0.30926328,  0.12185723,  0.        ],
       [ 0.12185723,  0.27544346,  0.02776675],
       [ 0.        ,  0.02776675,  0.0253416 ]])
asked May 24, 2017 by Chao Zhang FEniCS User (1,180 points)

1 Answer

0 votes
 
Best answer

Hi, the forms will differ in the polynomial degree of the integrand. Since FEniCS defaults to quadrature rule which is exact for the integrand the forms will be integrated differently. Consider

from ufl.algorithms import estimate_total_polynomial_degree as get_degree 
from dolfin import *
mesh = UnitIntervalMesh(2)

V = FunctionSpace(mesh, 'CG', 1)
u = TrialFunction(V)
v = TestFunction(V)

k1 = Expression('sin(x[0])*exp(x[0])', degree=2)
a1 = k1*u*v*dx
A1 = as_backend_type(assemble(a1)).array()
print 'a1 degree', get_degree(a1)

k2 = Expression('sin(x[0])*exp(x[0])', degree=3)
a2 = k2*u*v*dx
A2 = as_backend_type(assemble(a2)).array()
print 'a2 degree', get_degree(a2)

b = k2*u*v*dx(metadata={'quadrature_degree': 4})
B = as_backend_type(assemble(b)).array()

print (A2-B)
answered May 24, 2017 by MiroK FEniCS Expert (80,920 points)
selected May 24, 2017 by Chao Zhang
...