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

How can I create an expression is dependent on function value?

0 votes

Hello,

I am interested in create a function that is itself dependent on the value of its input.

This is the problem that I am facing.

I have a function, G(T), where the value of G is a polynomial of expression of T with coefficients that are dependent on wether they are for T > Tmid or T<= Tmid, where Tmid is a midpoint value for the range.

T is a Function(V) where V is a FunctionSpace (mesh, 'DG', 1).

Any thoughts on how to proceed?

Thank you for your help on this matter.

Best,
Paul

asked Jul 29, 2016 by paulgarias FEniCS Novice (120 points)

1 Answer

+1 vote

Two methods spring to mind:

Try writing your own custom expression as explained here.

The other is to implement ufl conditionals:

from dolfin import *
import matplotlib.pyplot as plt
import ufl

ufl.algorithms.apply_derivatives.CONDITIONAL_WORKAROUND = True

mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, 'CG', 1)
T, v = Function(V), TestFunction(V)

# kappa = (T + 1)**2 if T > T_mid else 1.0
T_mid = Constant(0.01)
kappa = conditional(gt(T, T_mid), (T + 1)**2, Constant(1.0))

F = dot(kappa*grad(T), grad(v))*dx - Constant(1.0)*v*dx

bcs = [DirichletBC(V, Constant(0.0), 'on_boundary')]
solve(F == 0, T, bcs)

plot(T)
plt.show()
answered Aug 1, 2016 by nate FEniCS Expert (17,050 points)
...