Convert sympy to ufl?

Using strings and eval:

import sympy

x = sympy.Symbol("x")
cos = sympy.cos
sympy_input = cos(x) + x ** 2


import fenics
mesh = fenics.UnitIntervalMesh(10)
cos = fenics.cos
x = fenics.SpatialCoordinate(mesh)[0]
ufl_output = eval(str(sympy_input))
print(fenics.assemble(ufl_output*fenics.dx))

However, is there a specfic reason for using sympy?
UFL-forms can be differentiated etc, so in most use cases there is no need for using sympy.

EDIT: You can also use: https://github.com/MiroK/ulfy

1 Like