Defining real part of complex equation using Expression() in FEniCS

Legacy dolfin doesn’t really have complex support.
You can use sympy for this though:

from dolfin import *


mesh = UnitSquareMesh(10, 10)

import sympy

x, y, t = sympy.symbols("x[0], x[1] t", real=True)

v_in = y * (1 - y) * sympy.exp(1j * t)
v_real, _ = v_in.as_real_imag()


expr = Expression(str(v_real), degree=2, t=0.0)

print(assemble(expr * dx(domain=mesh)))

expr.t = 2
print(assemble(expr * dx(domain=mesh)))
1 Like