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

Strange behaviour of dot product of vector, min. example incl.

+2 votes

Dear all,

I have again a question, probably I missed something crucial. I made a minimal wokring example so it is clearly visible. I just have a vector $\vec{b}=(0,1)$ in $\Omega$ and want to make $\vec{b} \cdot \vec{b}$.

from dolfin import *

# Mesh
mesh = UnitSquareMesh(11,11)
V = FunctionSpace(mesh, "DG", 0)

# Data
b = Constant((1.0, 0.0))

# Trial And Test Functions
v = TestFunction(V)

# Basic Definitions
bb_assemble = assemble(inner(b,b)*v*dx)
bb = Function(V,bb_assemble)
plot(bb)

# Hold plot
interactive()

As you can see, the result is not 1 in $\Omega$ as it should be.

asked Jun 3, 2014 by luk.p FEniCS User (3,470 points)

2 Answers

+3 votes
 
Best answer

This is because in assemble, you will multiply each index in the vector with the volume (area) of each cell (integral of TestFunction).

You can sort this in several ways.

Scale with cell volume:

bb_assemble = assemble(1/CellVolume(mesh)*inner(b,b)*v*dx)

However, this will only work for a DG0-space. Therefore, you might be want to use project:

bb = project(inner(b,b), V)

or projection with written out matrix/vector (equivalent to project, but matrix can be re-used if this is to be done several times):

bb = Function(V)
u = TrialFunction(V)
M = assemble(u*v*dx)
L = assemble(inner(b,b)*v*dx)
solve(M, bb.vector(), L)
answered Jun 3, 2014 by Øyvind Evju FEniCS Expert (17,700 points)
selected Jun 3, 2014 by luk.p

Thank you very much for the reply Øyvind Evju! I realized it from the answer of MiroK.

+1 vote

Hi, $j$-th entry of bb is $\int_{K_j}b \cdot b \phi_j$ where $K_j$ is the support of $j$-th basis function of $V$. Since the space is DG0, the basis functions are constant and the support is just one triangle. So bb values are really just cell volumes. Contrast your plot with

f = project(CellVolume(mesh), V)
plot(f)
answered Jun 3, 2014 by MiroK FEniCS Expert (80,920 points)

Thank you MiroK. What I wanted was project(inner(b,b), V).

I marked the answer from Øyvind Evju as it contains a bit more pieces of information. But thank you very much!

No problem, I was about to suggest you to do that :).

...