# Solving complex-valued PDE

**URL:** <https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806>\
**Category:** Uncategorized\
**Created:** [May 13, 2019, 8:24am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806 "2019-05-13T08:24:44Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![JulianDwain](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@JulianDwain](https://fenicsproject.discourse.group/u/JulianDwain)\
**Post date:** [May 13, 2019, 8:24am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/1 "2019-05-13T08:24:44Z")

</div>

Hi,  
I’m trying to solve the helmholtz equation in its form Laplace§+k^2\*p = 0, where p is a complex-valued quantity. I have a 3D Box mesh and following boundary conditions.  
boundary\_conditions = {  
0: {“Neumann”: a\_val \* v\_s[0]}, # 0 left boundary  
1: {“Neumann”: a\_val \* v\_s[1]}, # 1 lower boundary  
2: {“Robin”: (a\_val \* Y\_right, -1 \* Z\_right \* v\_s[2])}, # 2 right boundary  
3: {“Neumann”: a\_val \* v\_s[3]}, # 3 upper boundary  
4: {“Neumann”: a\_val \* v\_s[4]}, # 4 front boundary  
5: {“Neumann”: a\_val \* v\_s[5]}, # 5 back boundary  
}  
where Y\_right and Z\_right are real valued quantities, a\_val and the list v\_s has a complex values. I’m then collecting my Neumann and Robin boundary conditions in lists and evalaute them.  
bc = []  
integrals\_n = [] # list for NBC  
integrals\_ra = [] # list for RBC  
integrals\_rl = [] # list for RBC  
j = complex(0, 1)  
for i in boundary\_conditions:  
# collect Neumann integrals  
if “Neumann” in boundary\_conditions[i]:  
if boundary\_conditions[i][“Neumann”] != 0:  
print(“Neumann Integral added”)  
g = boundary\_conditions[i][“Neumann”]  
integrals\_n.append(df.inner(g, v) \* df.ds(i))  
# Collect Robin integrals, can be split up in two lists  
if “Robin” in boundary\_conditions[i]:  
if boundary\_conditions[i][“Robin”]:  
print(“Robin Integral added”)  
r, s = boundary\_conditions[i][“Robin”]  
integrals\_ra.append(r \* p \* v \* df.ds(i))  
integrals\_rl.append(r \* s \* v \* df.ds(i))  
Afterwards i’m building my left and right side and solve it.  
left\_side = -1 \* (df.inner(df.grad§, df.grad(v)) - (k \*\* 2) \* v \* p) \* df.dx - sum(integrals\_ra)  
right\_side = df.Constant(0) \* v \* df.dx + sum(integrals\_n) - sum(integrals\_rl)

u = df.Function(function\_space)

df.solve(left\_side == right\_side, u, bc)  
Since p is a complex valued quantity, i’m expecting a complex solution, but i’m only getting the following error message:  
ufl.log.UFLException: Unexpected complex value in real expression.  
I’m not able to figure out why this is happening. I was trying to do it like in here:

> **[Solving the Exterior Helmholtz Problem in DOLFIN-X](https://igorbaratta.github.io/gsoc_blog/jekyll/update/2018/07/03/Helmholtz.html)**
>
> Here you will find some information about the GSoC project: ‘Complex Support in Fenics’. During this summer (or winter here in the southern hemisphere), weekly blog posts are planned to keep the interested guest up-to-date on what’s happening...

  
But none of this is working and i wasn’t able to find anything on this forum, so i’m hoping anyone of you can help me.  
There are also only Neumann and Robin boundary conditions, therefore i’m not sure how to handle it properly.  
Any little help is highly appreciated!

---

<div class="post-metadata">

**Author:** ![Julius](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Julius](https://fenicsproject.discourse.group/u/Julius)\
**Post date:** [May 13, 2019, 8:44am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/2 "2019-05-13T08:44:57Z")

</div>

Which version of fenics are you using?

---

<div class="post-metadata">

**Author:** ![JulianDwain](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@JulianDwain](https://fenicsproject.discourse.group/u/JulianDwain)\
**Post date:** [May 13, 2019, 9:03am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/3 "2019-05-13T09:03:12Z")

</div>

I’m using FEniCS version 2019.1.0, sorry I forgot to mention it.

---

<div class="post-metadata">

**Author:** ![dparsons](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dparsons](https://fenicsproject.discourse.group/u/dparsons)\
**Post date:** [May 14, 2019, 5:46am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/4 "2019-05-14T05:46:20Z")

</div>

The example you’re following comes from dolfin-x, the development version of dolfin. dolfin 2019.1 does not support complex numbers. You’ll need to build dolfin-x from source.

(an alternative of course is to split the real and imaginary parts and solve the simultaneous system of 2 real equations using a MixedFunctionSpace)

---

<div class="post-metadata">

**Author:** ![JulianDwain](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@JulianDwain](https://fenicsproject.discourse.group/u/JulianDwain)\
**Post date:** [May 14, 2019, 9:45am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/5 "2019-05-14T09:45:26Z")

</div>

Thank you very much, is there an example of how to calculate the real and imaginary parts using a MixedFunctionSpace?

---

<div class="post-metadata">

**Author:** ![dparsons](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dparsons](https://fenicsproject.discourse.group/u/dparsons)\
**Post date:** [May 14, 2019, 10:20am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/6 "2019-05-14T10:20:31Z")

</div>

You’ll need to use your own maths to split the equations. But there are examples of MixedFunctionSpaces

e.g. Stokes equations (solving for u and p),  
[https://fenicsproject.org/docs/dolfin/2019.1.0/python/demos/stokes-iterative/demo\_stokes-iterative.py.html](https://fenicsproject.org/docs/dolfin/2019.1.0/python/demos/stokes-iterative/demo_stokes-iterative.py.html)  
/usr/share/dolfin/demo-python/documented/stokes-iterative/

Mixed Poisson  
[https://fenicsproject.org/docs/dolfin/2019.1.0/python/demos/mixed-poisson/demo\_mixed-poisson.py.html](https://fenicsproject.org/docs/dolfin/2019.1.0/python/demos/mixed-poisson/demo_mixed-poisson.py.html)

also  
/usr/share/dolfin/demo-python/undocumented/mixed-poisson-sphere/demo\_mixed-poisson-sphere.py

---

<div class="post-metadata">

**Author:** ![JulianDwain](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@JulianDwain](https://fenicsproject.discourse.group/u/JulianDwain)\
**Post date:** [May 14, 2019, 11:01am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/7 "2019-05-14T11:01:33Z")

</div>

Thank you very much that really helped me out a lot, i’m currently trying to figure out how to build dolfin-x from source, but i will try it with the MixedFunctionSpace.

---

<div class="post-metadata">

**Author:** ![4shsn](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/4shsn/32/268_2.png) [@4shsn](https://fenicsproject.discourse.group/u/4shsn)\
**Post date:** [May 29, 2019, 3:17pm UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/8 "2019-05-29T15:17:16Z")

</div>

Hey Julian,

I work with the same PDE and I use Fenics 2019.1.0 as well.

We use the `FunctionSpace` this way

```python
P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)

# # Define 2D-Real function space to substitute for Complex space
V = FunctionSpace(mesh, P1 * P1) # NOTE: V.sub(0) and V.sub(1) are the subspaces 

```

and take the Trial and test functions as tuples from V i.e

```python
(u_re, u_im) = TrialFunctions(V)
(v_re, v_im) = TestFunctions(V)

```

All the operations over ufl forms can then be simplified by defining a Complex class to handle complex objects.

Cheers, Hope this helps!

---

<div class="post-metadata">

**Author:** ![JulianDwain](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@JulianDwain](https://fenicsproject.discourse.group/u/JulianDwain)\
**Post date:** [May 31, 2019, 8:23am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/9 "2019-05-31T08:23:08Z")

</div>

Thank you very much, I really appreciate it.  
How have you implemented your boundary conditions on this problem?

---

<div class="post-metadata">

**Author:** ![4shsn](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/4shsn/32/268_2.png) [@4shsn](https://fenicsproject.discourse.group/u/4shsn)\
**Post date:** [May 31, 2019, 9:23am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/10 "2019-05-31T09:23:33Z")

</div>

Dirichlet boundary conditions are imposed on each subspace :

```python
bcs = [DirichletBC(V.sub(0), u0_re, boundary_markers, 1),
       DirichletBC(V.sub(1), u0_im, boundary_markers, 1)]

```

The Neumann conditions can be easily applied by dividing the integrals for real and imaginary parts.

```python
g_re * v_re * ds(1) + g_im * v_im * ds(1)

```

PS: This is a based on a simple (yet, neat) trick of representing the complex space of test functions by `v_re + i * 0` and `0 + i * v_im` functions.

---

<div class="post-metadata">

**Author:** ![JulianDwain](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@JulianDwain](https://fenicsproject.discourse.group/u/JulianDwain)\
**Post date:** [May 31, 2019, 4:44pm UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/11 "2019-05-31T16:44:44Z")

</div>

Wow thank you very very much, it really helped me out a lot!  
Can you post your weak formulation so I can compare it?  
I’m not sure if I have done it right.

---

<div class="post-metadata">

**Author:** ![cweickhmann](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@cweickhmann](https://fenicsproject.discourse.group/u/cweickhmann)\
**Post date:** [August 2, 2019, 4:44pm UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/12 "2019-08-02T16:44:53Z")

</div>

Maybe one question here:  
I am having a similar problem and have my FunctionSpace set up like in your examples above.  
However, I am using SLEPcEigenSolver and it looks like the complex part of the eigenvalue is _always_ zero. (I’ll have to sit for an MWE…)

Has anyone here ever successfully obtained complex eigenvalues from a real-valued problem with `dolfin.SLEPcEigenSolver`?

---

<div class="post-metadata">

**Author:** ![JakobWagner](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jakobwagner/32/2248_2.png) [@JakobWagner](https://fenicsproject.discourse.group/u/JakobWagner)\
**Post date:** [January 5, 2021, 4:52pm UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/13 "2021-01-05T16:52:49Z")

</div>

> [@JulianDwain](#):
>
> [Igor A. Baratta – 3 Jul 18](https://igorbaratta.github.io/gsoc_blog/jekyll/update/2018/07/03/Helmholtz.html)
> 
> ### [Solving the Exterior Helmholtz Problem in DOLFIN-X](https://igorbaratta.github.io/gsoc_blog/jekyll/update/2018/07/03/Helmholtz.html)
> 
> Here you will find some information about the GSoC project: ‘Complex Support in Fenics’. During this summer (or winter here in the southern hemisphere), weekly blog posts are planned to keep the interested guest up-to-date on what’s happening…

Is it possible to find this example? When i klick the link i only get a 404 response code. It would really help me a lot to see other peoples solutions to this.  
RN i assemble all of the boundary conditions myself. I would love to know another way of doing it 🙂

---

<div class="post-metadata">

**Author:** ![4shsn](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/4shsn/32/268_2.png) [@4shsn](https://fenicsproject.discourse.group/u/4shsn)\
**Post date:** [August 10, 2021, 12:04pm UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/14 "2021-08-10T12:04:54Z")

</div>

Dear all,

For reference, here’s an involved example of solving a complex-valued Helmholtz scattering problem with Perfectly Matched Layers for reference. All the complex-arithmetic in the variational forms (UFL forms) is handled by way of a complex class.

[Github Repo](https://github.com/ROMSOC/benchmarks-acoustic-propagation/)

---

<div class="post-metadata">

**Author:** ![andcic](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/andcic/32/2890_2.png) [@andcic](https://fenicsproject.discourse.group/u/andcic)\
**Post date:** [April 1, 2022, 6:07pm UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/16 "2022-04-01T18:07:21Z")

</div>

Dear all,

Is there a way to ‘activate’ the complex solver for Fenicsx after instlalling as Debian/Ubuntu package? (see [here](https://docs.fenicsproject.org/dolfinx/main/python/installation.html#debian-ubuntu-packages))

I see that the package `python3-dolfinx-complex` gets installed.

Many thanks in advance.  
Andrea

---

<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:** [April 2, 2022, 12:35am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/17 "2022-04-02T00:35:52Z")

</div>

`source dolfinx-complex-mode ` should do the trick.

---

<div class="post-metadata">

**Author:** ![dparsons](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dparsons](https://fenicsproject.discourse.group/u/dparsons)\
**Post date:** [April 2, 2022, 8:44am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/18 "2022-04-02T08:44:46Z")

</div>

I don’t think the Debian package has `dolfinx-complex-mode` as such, at least I’m not aware of it.

I’ve set up the debian packages to work off PETSC\_DIR, so

```auto
PETSC_DIR=/usr/lib/petscdir/petsc-complex python3 job.py

```

should get it working for you (define `SLEPC_DIR` likewise if needed).

If you need more fine control (e.g. different petsc versions) you can point PETSC\_DIR at other locations below /usr/lib/petscdir/.

The FEniCS build (python modules) are placed in `${PETSC_DIR}/lib/python3/dist-packages/`. If dolfinx is not located there, then it hasn’t been built for that version of PETSc.

---

<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:** [April 2, 2022, 8:47am UTC](https://fenicsproject.discourse.group/t/solving-complex-valued-pde/806/19 "2022-04-02T08:47:52Z")

</div>

You are right @dparsons. Thanks for correcting me and posting an actual solution.
