Expression in FEniCS

I want to use the expression function to express a complex boundary condition. But I don’t know how to make the expression more concise?
Here is the function I am using, it looks very bulky.

Ux = dl.Expression(‘K1/(2*mu)*sqrt(sqrt(x[0]*x[0]+x[1]x[1])/2/pi)(kappa-x[0]/(sqrt(x[0]*x[0]+x[1]*x[1]))) * (1/2)sqrt(1/2(1+(x[0]-0)/sqrt(x[0]*x[0]+x[1]x[1])))',K1=K1,mu=mu,pi=np.pi,kappa=kappa,degree=2);
Uy = dl.Expression('K1/(2
mu)*sqrt(sqrt(x[0]*x[0]+x[1]x[1])/2/pi)(kappa-x[0]/(sqrt(x[0]*x[0]+x[1]*x[1]))) * (1/2)sqrt(1/2(1-(x[0]-0)/sqrt(x[0]*x[0]+x[1]*x[1])))’,K1=K1,mu=mu,pi=np.pi,kappa=kappa,degree=2);
BC_u = [dl.DirichletBC(V_u.sub(0),Ux, boundaries, 1),
dl.DirichletBC(V_u.sub(0),Ux, boundaries, 2),
dl.DirichletBC(V_u.sub(0),Ux, boundaries, 4),
dl.DirichletBC(V_u.sub(0),Ux, boundaries, 5),
dl.DirichletBC(V_u.sub(1),Uy, boundaries, 1),
dl.DirichletBC(V_u.sub(1),Uy, boundaries, 2),
dl.DirichletBC(V_u.sub(1),Uy, boundaries, 4),
dl.DirichletBC(V_u.sub(1),Uy, boundaries, 5)];

Do you know of any good ways to express this boundary condition more concisely?

1 Like

Please format your code with 3x` such as

```python
Ux = dl.Expression("x[0]", degree=2)
```

etc to make it possible for people to copy your code.
Note that you could do something like

import dolfin
mesh = dolfin.UnitCubeMesh(1,1,1)

r = dolfin.Expression("sqrt(x[0]*x[0]+x[1]*x[1])", degree=2)
p = dolfin.Expression("pow(r, 2)", r=r, degree=2)

# Just to illustrate that it works
V = dolfin.FunctionSpace(mesh, "Lagrange", 2)
u = dolfin.Function(V)
u.interpolate(p)

and similarly define theta.

Oh Thank you! I know!