# Need help in defining polar solution in FeniCS

**URL:** <https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066>\
**Category:** General\
**Created:** [October 19, 2024, 9:18am UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066 "2024-10-19T09:18:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashini](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Ashini](https://fenicsproject.discourse.group/u/Ashini)\
**Post date:** [October 19, 2024, 9:18am UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/1 "2024-10-19T09:18:10Z")

</div>

Hello everyone

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

 ![Screenshot from 2024-10-19 13-28-59](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/c/cd7ebba19c832dcda7d130bfc95aaa6a058a1096.png)

I defined it as

```auto

        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

---

<div class="post-metadata">

**Author:** ![francesco-ballarin](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@francesco-ballarin](https://fenicsproject.discourse.group/u/francesco-ballarin)\
**Post date:** [October 20, 2024, 8:47am UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/2 "2024-10-20T08:47:57Z")

</div>

Please read [Read before posting: How do I get my question answered?](https://fenicsproject.discourse.group/t/read-before-posting-how-do-i-get-my-question-answered/21)

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

---

<div class="post-metadata">

**Author:** ![Ashini](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Ashini](https://fenicsproject.discourse.group/u/Ashini)\
**Post date:** [October 20, 2024, 12:37pm UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/3 "2024-10-20T12:37:23Z")

</div>

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.

```auto

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)

```

---

<div class="post-metadata">

**Author:** ![francesco-ballarin](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@francesco-ballarin](https://fenicsproject.discourse.group/u/francesco-ballarin)\
**Post date:** [October 20, 2024, 3:41pm UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/4 "2024-10-20T15:41:14Z")

</div>

> [@francesco-ballarin](#):
>
> … and report the actual error, otherwise it’s unlikely anyone will be able to help.

> [@Ashini](#):
>
> … I am not able to find my mistake.

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 😅) , 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.

---

<div class="post-metadata">

**Author:** ![Ashini](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Ashini](https://fenicsproject.discourse.group/u/Ashini)\
**Post date:** [October 20, 2024, 4:56pm UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/5 "2024-10-20T16:56:28Z")

</div>

Error

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

```

---

<div class="post-metadata">

**Author:** ![francesco-ballarin](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@francesco-ballarin](https://fenicsproject.discourse.group/u/francesco-ballarin)\
**Post date:** [October 20, 2024, 4:58pm UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/6 "2024-10-20T16:58:01Z")

</div>

Try `atan2` without the underscore.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 20, 2024, 11:45pm UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/8 "2024-10-20T23:45:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ashini](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Ashini](https://fenicsproject.discourse.group/u/Ashini)\
**Post date:** [October 21, 2024, 5:42am UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/9 "2024-10-21T05:42:14Z")

</div>

The fenics version that I used is

2019.2.0.dev0

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 21, 2024, 9:42am UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/10 "2024-10-21T09:42:33Z")

</div>

The probably try:  
`from ufl_legacy import atan2`

---

<div class="post-metadata">

**Author:** ![Ashini](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Ashini](https://fenicsproject.discourse.group/u/Ashini)\
**Post date:** [October 21, 2024, 11:11am UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/11 "2024-10-21T11:11:08Z")

</div>

It still shows an error

```auto
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)

```auto
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

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

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 21, 2024, 11:14am UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/12 "2024-10-21T11:14:20Z")

</div>

Then try `from ufl import atan2`.

---

<div class="post-metadata">

**Author:** ![Ashini](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Ashini](https://fenicsproject.discourse.group/u/Ashini)\
**Post date:** [October 21, 2024, 4:12pm UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/13 "2024-10-21T16:12:33Z")

</div>

error

```auto
  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.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 21, 2024, 7:14pm UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/14 "2024-10-21T19:14:41Z")

</div>

Looking at the old source code: [Bitbucket](https://bitbucket.org/fenics-project/ufl/src/a6e889ca592d5dd2304ceb6afb82d09e3569968c/ufl/operators.py?at=master#lines-663:674)  
it seems like  
`from ufl.operators import atan_2` is the right thing to do

---

<div class="post-metadata">

**Author:** ![Ashini](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Ashini](https://fenicsproject.discourse.group/u/Ashini)\
**Post date:** [October 22, 2024, 11:43am UTC](https://fenicsproject.discourse.group/t/need-help-in-defining-polar-solution-in-fenics/16066/15 "2024-10-22T11:43:34Z")

</div>

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!
