Need help in defining polar solution in FeniCS

Hello everyone

I want to polar solution on a Lshaped domain but it shows error.

I defined it as


        x = SpatialCoordinate(mesh)
        r = sqrt(x[0]**2 + x[1]**2)
        theta = atan_2(x[1], x[0])  
 
        chi = Constant(0.54448373)  
        lamda = Constant(10)        
        nu = Constant(1.0)          
        M1 = Expression("-cos((chi + 1) * 3*pi / 4) / cos((chi - 1) * 3*pi / 4)", chi=chi, degree=3)
        M2 = Expression("2 * (lamda + 2*nu) / (nu + lamda)", lamda=lamda, nu=nu, degree=3)
        u_exact = Expression(("pow(r, chi) / (2 * nu) * (-(chi + 1) * cos((chi + 1) * theta) + (M2 - chi - 1) * M1 * cos((chi - 1) * theta))",  "pow(r, chi) / (2 * nu) * ((chi + 1) * sin((chi + 1) * theta) + (M2 + chi - 1) * M1 * sin((chi - 1) * theta))"), r=r, theta=theta, chi=chi, M1=M1, M2=M2, nu=nu, domain=mesh, degree=3)
        p_exact = Expression("pow(r, 1.0/3.0) * sin(1.0/3.0 * (pi/2 + theta))", r=r, theta=theta, domain=mesh, degree=3)
       

but it shows error. Please help me in this regard.

Thanks

Please read Read before posting: How do I get my question answered?

In particular, provide a minimal code we can run, and report the actual error, otherwise it’s unlikely anyone will be able to help.

The minimal example is attached below. I just want this polar exact solution works. I am not able to find my mistake. Thank you in advance.


from dolfin import *
import numpy as np
import matplotlib.pyplot as plt
from mshr import Rectangle, generate_mesh


rect1 = Rectangle(Point(0, 0), Point(0.5, 1))
rect2 = Rectangle(Point(0, 0), Point(1, 0.5))
mesh = generate_mesh(rect1 + rect2, 50)

plot(mesh)
plt.show()

x = SpatialCoordinate(mesh)
r = sqrt(x[0]**2 + x[1]**2)
theta = atan_2(x[1], x[0])

# Constants
chi = Constant(0.54448373)
lamda = Constant(10)
nu = Constant(1.0)

M1 = Expression("-cos((chi + 1) * 3*pi / 4) / cos((chi - 1) * 3*pi / 4)", chi=chi, degree=3)
M2 = Expression("2 * (lamda + 2*nu) / (nu + lamda)", lamda=lamda, nu=nu, degree=3)

u_exact = Expression((
    "pow(r, chi) / (2 * nu) * (-(chi + 1) * cos((chi + 1) * theta) + (M2 - chi - 1) * M1 * cos((chi - 1) * theta))",
    "pow(r, chi) / (2 * nu) * ((chi + 1) * sin((chi + 1) * theta) + (M2 + chi - 1) * M1 * sin((chi - 1) * theta))"
), r=r, theta=theta, chi=chi, M1=M1, M2=M2, nu=nu, domain=mesh, degree=3)

p_exact = Expression("pow(r, 1.0/3.0) * sin(1.0/3.0 * (pi/2 + theta))", r=r, theta=theta, domain=mesh, degree=3)


I did not mean to ask you to report your mistake (otherwise you would have spotted it yourself, so there were no point in you asking the question in this forum :sweat_smile:) , I did mean to post the error message you get from the code.

I know that I could run the code and get it myself, but it’s good for you to learn the best practices on how to interact with the community.

Error

   theta = atan_2(x[1], x[0])
NameError: name 'atan_2' is not defined. Did you mean: 'atan'?

Try atan2 without the underscore.

Try importing either of them directly from ufl/ufl_legacy depending on what version of fenics you are running

The fenics version that I used is

2019.2.0.dev0

The probably try:
from ufl_legacy import atan2

It still shows an error

Inspiron-3501:~/Downloads/aposteriori/L-Shaped$ pip show ufl
Name: UFL
Version: 2017.1.0
Summary: Unified Form Language
Home-page: https://bitbucket.org/fenics-project/ufl/
Author: Martin Sandve Alnæs, Anders Logg
Author-email: fenics-dev@googlegroups.com
License: UNKNOWN
Location: /home/ashin/.local/lib/python3.10/site-packages
Requires: numpy, six
Required-by: 

FeniCS version : 2019.2.0.dev0

The code I used is ( from ufl_legacy import atan2 also shows error)

from dolfin import *
import numpy as np
import matplotlib.pyplot as plt
from mshr import Rectangle, generate_mesh
from ufl import atan

rect1 = Rectangle(Point(0, 0), Point(0.5, 1))
rect2 = Rectangle(Point(0, 0), Point(1, 0.5))
mesh = generate_mesh(rect1 + rect2, 50)

plot(mesh)
plt.show()

x = SpatialCoordinate(mesh)
r = sqrt(x[0]**2 + x[1]**2)
theta = atan2(x[1], x[0])

# Constants
chi = Constant(0.54448373)
lamda = Constant(10)
nu = Constant(1.0)

M1 = Expression("-cos((chi + 1) * 3*pi / 4) / cos((chi - 1) * 3*pi / 4)", chi=chi, degree=3)
M2 = Expression("2 * (lamda + 2*nu) / (nu + lamda)", lamda=lamda, nu=nu, degree=3)

u_exact = Expression((
    "pow(r, chi) / (2 * nu) * (-(chi + 1) * cos((chi + 1) * theta) + (M2 - chi - 1) * M1 * cos((chi - 1) * theta))",
    "pow(r, chi) / (2 * nu) * ((chi + 1) * sin((chi + 1) * theta) + (M2 + chi - 1) * M1 * sin((chi - 1) * theta))"
), r=r, theta=theta, chi=chi, M1=M1, M2=M2, nu=nu, domain=mesh, degree=3)

p_exact = Expression("pow(r, 1.0/3.0) * sin(1.0/3.0 * (pi/2 + theta))", r=r, theta=theta, domain=mesh, degree=3)

output

   theta = atan2(x[1], x[0])
NameError: name 'atan2' is not defined. Did you mean: 'atan'?

Then try from ufl import atan2.

error

  File "/home/ashini/Downloads/aposteriori/L-Shaped/lshaped.py", line 5, in <module>
    from ufl import atan2
ImportError: cannot import name 'atan2' from 'ufl' (/home/ashini/.local/lib/python3.10/site-packages/ufl/__init__.py)

Please tell me some alternative way.

Looking at the old source code: Bitbucket
it seems like
from ufl.operators import atan_2 is the right thing to do

Sir, the problem is not related to importing the library. The main issue is with the variables r and theta. We define M1, M2, and chi using expressions, while r and theta are defined without expressions. If we define r and theta using expressions, the problem is resolved. Thanks!