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

Calculate a Function

0 votes

Hi,

For example, I have following functions:

mesh = ...
V = FunctionSpace(mesh,"Quadrature",4)

f1 = Function(V)
f2 = Function(V)
f3 = Function(V)

and I know that:

if f1 >= f2:
   f4 = f3
else:
   f4 = f3 - 1

How can I calculate f4?

Thanks!!!

asked Oct 3, 2016 by lulio FEniCS Novice (450 points)

1 Answer

+2 votes
 
Best answer

Hi, consider

from dolfin import *
import numpy as np

#        /  f3 if f1 > f2
# f_4 =  |
#        \  f1 otherwise

mesh = IntervalMesh(1000, -1, 1)
V = FunctionSpace(mesh, 'CG', 1)
f1 = interpolate(Expression('1-x[0]*x[0]', degree=2), V)
f2 = interpolate(Constant(3/4.), V)
f3 = interpolate(Expression('3*x[0]*x[0]', degree=2), V)

# By Projection
f4 = project(conditional(gt(f1, f2), f3, f1), V)

# By manipulating vectors
f5 = Function(V, f1.vector())

F5 = f5.vector().get_local()
F2 = f2.vector().get_local()
F3 = f3.vector().get_local()

gt = np.where(F5 > F2)[0]
F5[gt] = F3[gt]

f5.vector().set_local(F5)
f5.vector().apply('insert')

print '|f1-f2|_2 =', sqrt(assemble(inner(f1-f5, f1-f5)*dx))

plot(f4)
interactive() 
answered Oct 4, 2016 by MiroK FEniCS Expert (80,920 points)
selected Oct 8, 2016 by lulio
...