Average calculation discrepancies between two different approach

Hi all,

I tried to calculate the volume average but the results are different for the two methods.

assemble(sol[1]*dx)/assemble(Constant(1)*dx)
sol[1].vector().sum()/mesh.num_cells()

From what I understand, these two should yield the same results.

Thanks.

In your second average, you are using number of cells, where you should use the length of the vector you are summing over, i.e.

from dolfin import *


def average(N):
    mesh = UnitIntervalMesh(N)
    V = FunctionSpace(mesh, "CG", 1)
    x = SpatialCoordinate(mesh)
    u = project(x[0], V)
    dx = Measure("dx", domain=mesh)
    print(N, assemble(u*dx)/ assemble(Constant(1)*dx), u.vector().sum()/len(u.vector()))

for n in [3, 10, 20, 40, 80]:
    average(n)

yielding:

3 0.5 0.5
10 0.5 0.5000000000000001
20 0.5000000000000001 0.5000000000000002
40 0.5000000000000001 0.5
80 0.5000000000000001 0.5000000000000001