Invalid type conversion

I’m trying to solve a pde optimization problem governed by the Navier-Stokes equations. My goal is to minimize the recirculation area in the domain. I would like to define the functional as:

v_x, v_y = u_n.split(deepcopy=True)
c = 1e-3  # Smoothing parameter
threshold = 0.
J_rec = assemble(0.5 * (1 - tanh((v_x - threshold) / c)) * dx)

I’ve also tried the function .project() in order to extract the first component of u_n.
where

V = VectorFunctionSpace(mesh, 'CG', 2)
u_n = Function(V, annotate=annotate)

this gives as error:
ValueError: Invalid type conversion: f_91 / 0.001 can not be converted to any UFL type.

My first idea was to define the functional as the integral of the indicator function on the subdomain where u_n[0]<0., i changed it using tanh to make it differentiable.
Do you have any idea on how i have to treat u_n in order to make it work?the best thing would be to use the indicator function as integrand, but also a solution for the smoothed one would be great.
Thank you!!!

It’s quite hard to help you without a minimal reproducible example.

1 Like

Which fenics version underlies dolfin-adjoint? In 2019.2.0.dev0 there doesn’t seem to be an issue, and this code simply executes:

from dolfin import *
mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, "Lagrange", 1)
u_n = Function(V)
c = 1e-3  # Smoothing parameter
threshold = 0.
J_rec = assemble(0.5 * (1 - tan((u_n - threshold) / c)) * dx)

Note I’ve had to use tan because tanh is not in dolfin. Where did you import it from?

(This is why it is important to create a MWE :wink: )

1 Like

Probably with a direct ufl import, ie
from ufl.operators import tanh

The dolfin_adjoint version is: 2023.3.0

I imported the tanh as:

from ufl import tanh

Thank you for your time!

Could you confirm if @Stein’s example runs for you? And does it run if you replace tan with tanh?

If I were to guess, I’d say that you are affected by Announcement: ufl_legacy and legacy dolfin

Thank you for your help!