Problem to define a function

Hello !
I’m new to fenics and I don’t really understand how to define “complex” function.
I’m solving the Poisson equation for my project
My function f in python is :

def f(x,y):
n=len(xcenter)
for i in range(n):
if xcenter[i]-radius[i]<x<xcenter[i]+radius[i] and ycenter[i]-radius[i]<y<ycenter[i]+radius[i]:
return 1
return 0

It’s pretty simple but I don’t know how to convert it to fenics.
I’ve tried class, Expression but it doesn’t work.

Thank you in advance for your help :slight_smile:

Please format your code using 3x` encapsulation such that it is formatted appropriately.

In general, consider the following code using Expression and the C+±conditional.
In this case, the function is one if it is with the circle with radius 0.2, with center at (0.3,0.4)

from dolfin import *

mesh = UnitSquareMesh(30,30)
expr = Expression("pow(x[0]-0.3, 2)+pow(x[1]-0.4, 2)<pow(0.2, 2)? 1 : 0", degree=0)
V = FunctionSpace(mesh, "DG", 0)
c = project(expr, V)
plot(c)
import matplotlib.pyplot as plt
plt.savefig("expr.png")

Thanks dokken !
I’ll try that =)

I’ve tried but I have a compilation error :

Blockquote

Moving new file over differing existing file:
src: /home/jovyan/jitfailure-dolfin_expression_527155762140775dd8756af1a7967936/error.log.d28b07c11ae043e58ea52ab980f579ec
dst: /home/jovyan/jitfailure-dolfin_expression_527155762140775dd8756af1a7967936/error.log
backup: /home/jovyan/jitfailure-dolfin_expression_527155762140775dd8756af1a7967936/error.log.old
Backup file exists, overwriting.
------------------- Start compiler output ------------------------
/tmp/tmplxktn8lf/dolfin_expression_527155762140775dd8756af1a7967936.cpp: In member function ‘virtual void dolfin::dolfin_expression_527155762140775dd8756af1a7967936::eval(Eigen::Ref<Eigen::Matrix<double, -1, 1> >, Eigen::Ref<const Eigen::Matrix<double, -1, 1> >) const’:
/tmp/tmplxktn8lf/dolfin_expression_527155762140775dd8756af1a7967936.cpp:61:32: error: ‘xcenter’ was not declared in this scope
61 | values[0] = pow(x[0]-xcenter, 2)+pow(x[1]-ycenter, 2)<pow(radius, 2)? 1 : 0;
| ^~~~~~~
/tmp/tmplxktn8lf/dolfin_expression_527155762140775dd8756af1a7967936.cpp:61:53: error: ‘ycenter’ was not declared in this scope
61 | values[0] = pow(x[0]-xcenter, 2)+pow(x[1]-ycenter, 2)<pow(radius, 2)? 1 : 0;
| ^~~~~~~
/tmp/tmplxktn8lf/dolfin_expression_527155762140775dd8756af1a7967936.cpp:61:69: error: ‘radius’ was not declared in this scope
61 | values[0] = pow(x[0]-xcenter, 2)+pow(x[1]-ycenter, 2)<pow(radius, 2)? 1 : 0;
| ^~~~~~

------------------- End compiler output ------------------------
Compilation failed! Sources, command, and errors have been written to: /home/jovyan/jitfailure-dolfin_expression_527155762140775dd8756af1a7967936

Blockquote

I’ve tried with using value in place of my values decenter, radius and ycenter.
Do you have an idea ?

Could you please supply what expression you are obtaining this error with?
Is it the one I supplied above?

Yes it’s the one above. I’ve tried implementing just this function and I had the same error.
I’ve seen on the forum that creating a class could resolve the problem but my tries were insuccessful.

Could you please supply the exact code you are running to get this error?

I was using the wrong mesh I think but now the problem is resolved !
Thank you very much for your help :grin: