How to get absolute value of a Function object

Hello everyone,

I define a Function object of c to store the solution after each finite element step. The code is as below

mesh = RectangleMesh(Point(0,0), Point(20,20), 20, 20)
V  = FunctionSpace(mesh, "CG", 1)
c = Function(V, name="Concentration")

After I obtain the solution c, I then want to use abs() command to make sure that all value in c is non-negative. However, when I use the following command to check the minimum value in c, it is negative with very large magnitude.

c_.assign(project(abs(c), V))
print("    min abs(c):", c_.vector().get_local().min())

My question is what is wrong here and what should I do if I want to get non-negative c?

Thanks!

Please make minimal working code example.
For instance:

from dolfin import *
mesh = RectangleMesh(Point(0,0), Point(20,20), 20, 20)
V  = FunctionSpace(mesh, "CG", 1)
c = interpolate(Expression("-x[0]", degree=1), V)

c_ = project(abs(c), V)
print("    min abs(c):", c.vector().get_local().min())
print("    min abs(c_):", c_.vector().get_local().min())

yields:

    min abs(c): -20.0
    min abs(c_): -3.17643549149e-16

where the minimal value of c_ is a very small neagtive number (around machine precision).

1 Like

Thank you for your answer!

1 Like