Dear all,
I’m trying to use the conditional Module but, obviously, I’m doing something wrong.
My problem could be summarised as follow: given a certain condition, I would like to get the value 1 if the condition is respected, 0 otherwise.
I tried to do so with the minimal code posted below:
from dolfin import *
from mshr import *
import numpy as np
mesh = UnitSquareMesh(1, 1)
V = FunctionSpace(mesh, "DG", 1)
rv = Function(V)
# conditon to be checked: rv >= 2
cond = ge(rv-2,DOLFIN_EPS)
# imposing the values above the cond
rv.vector()[:] = 2.1
# setting only the frist DoF below the cond
rv.vector()[0] = 1.9
# 1.9 gives 1 in position 0
# 1.8 gives 0 in positions 0, 1 and 2
# conditional statement
a = conditional(cond,1,0)
# output
np.savetxt("rv.csv", project(rv,V).vector().get_local(), delimiter=",")
np.savetxt("a.csv", project(a,V).vector().get_local(), delimiter=",")
I was expecting to get the value 0 in the first position of a. Instead, in the case (rv.vector()[0] = 1.9 ) I obtain 1 and in the other case (rv.vector()[0] = 1.8 ) the value 0 appears in the first three positions of a.
Any hint?
Thank you very much.
This is due to the fact that a projection does not evaluate the function at the degrees of freedom, but at the quadrature points. You observe this when simply assembling your conditional with various quadrature degrees:
from dolfin import *
import numpy as np
mesh = UnitSquareMesh(1, 1)
V = FunctionSpace(mesh, "DG", 1)
rv = Function(V)
# conditon to be checked: rv >= 2
cond = ge(rv-2,DOLFIN_EPS)
# imposing the values above the cond
rv.vector()[:] = 2.1
# setting only the frist DoF below the cond
rv.vector()[0] = 1.9
# 1.9 gives 1 in position 0
# 1.8 gives 0 in positions 0, 1 and 2
# conditional statement
a = conditional(cond,1,0)
for degree in range(15):
print(degree, assemble(a*dx(domain=mesh, metadata={"quadrature_degree": degree})))