Apply an expression on a fonction

Dear Fenics users,
I’d like to apply an expression on a function defined on a Function space V, in order to have polar coordinates as unknown in Fenics but classic coordinates in the variational formulation of my problem.

class MyRotation(UserExpression):
    def eval(self,values,x):
        r=10
        x0=0
        y0=-10
        values[0]=x0+(x[1]-y0)*sin(x[0]/r) -x[1]
        values[1]=y0+(x[1]-y0)*cos(x[0]/r) -x[2]
    def value_shape(self):
        return (2,)

Ve = VectorElement("Lagrange", mesh.ufl_cell(), 1)
v=Function(V)
f=MyRotation(v) #doesn't work!!!

There’s typos in the invocations of x[1] and x[2]. And I think you want to create a VectorFunctionSpace for v. The following code works for me:

import dolfin as dlf
import mshr

class MyRotation(dlf.UserExpression):
    def eval(self, values, x):
        r = 2
        x0 = 0
        y0 = -10
        values[0] = x0 + (x[1]-y0)*dlf.sin(x[0]/r) - x[0]
        values[1] = y0 + (x[1]-y0)*dlf.cos(x[0]/r) - x[1]
    def value_shape(self):
        return (2,)

domain = mshr.Circle(dlf.Point(0,-10), 2)
mesh = mshr.generate_mesh(domain, 10)
    
V = dlf.VectorFunctionSpace(mesh, "Lagrange", 1)
v = dlf.Function(V)
f = MyRotation(v)

# Test operation
F = dlf.interpolate(f, V).vector()
F.norm("l2")

It does work well to compute something but I’m searching for a symbolic operation on v=(r,\theta) to transform it into v_{c}=(r\cos(\theta),r\sin(\theta)). Then I keep a variational formulation in Cartesian coordinates and the operation derivative(Potential_Energy) is the function I minimize with it’s Jacobian to do it faster.

An other way of doing it could be to always stay in polar coordinates but in the potential energy formula I will have some \frac{1}{r} that will appear and it’s the same problem How can I write an symbolic expression on a function?

the perfect example is the \det(v^T.v) operation working on ufl level and apply the determinant symbolic operation, then the symbolic calcul ‘derivative’ does work.