# Interpolate into complex-valued vector function

**URL:** <https://fenicsproject.discourse.group/t/interpolate-into-complex-valued-vector-function/9178>\
**Category:** dolfinx\
**Created:** [August 31, 2022, 4:01pm UTC](https://fenicsproject.discourse.group/t/interpolate-into-complex-valued-vector-function/9178 "2022-08-31T16:01:47Z")\
**Posts on this page:** 4\
**Page:** 1

<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 31, 2022, 4:01pm UTC](https://fenicsproject.discourse.group/t/interpolate-into-complex-valued-vector-function/9178/1 "2022-08-31T16:01:47Z")

</div>

I was trying to test a complex-valued PDE on a standard rectangular domain and noticed some issues with the way I was interpolating and expression into a function.

Here is a MWE to get a function \boldsymbol{u} = \exp(-\mathrm{i} \boldsymbol{k} \cdot \boldsymbol{x}) \boldsymbol{k} for a given plane-wave direction vector, \boldsymbol{k}.

```python
import numpy as np
from mpi4py import MPI
from dolfinx.mesh import create_rectangle
from dolfinx.fem import FunctionSpace, Function

msh = create_rectangle(MPI.COMM_WORLD, ((0.0, 0.0), (1.0, 1.0)), (20, 20))
msh.name = 'dolfinx_mesh'

def foo(x):
    k = [1.0, 0.0]
    return np.vstack((
        np.exp(-1j*(k[0]*x[0]+k[1]*x[1])) * k[0],
        np.exp(-1j*(k[0]*x[0]+k[1]*x[1])) * k[1]
    ))

V = FunctionSpace(mesh, ('Raviart-Thomas', 1))

u = Function(V, dtype=np.complex128)
u.interpolate(foo)

```

Results in some issues along the Y-coordinate (which is supposed to be 0. since it is multiplied explicitly with it)

 ![Paraview-vis](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/5/53c234ea1f44306615d1b04821aee7db1493b544.png)

This bar on the left-hand side that you notice is observed also at other boundaries (with more complex meshes). **The width of this bar does reduce with increased RT-order and mesh refinement, but not entirely and artifacts do remain in a few cells**.

Could someone point me to something I am doing wrong in interpolating a vector-valued expression to a Raviart-Thomas function space? I borrowed this interpolation template from [test\_interpolation.py](https://github.com/FEniCS/dolfinx/blob/v0.5.0/python/test/unit/fem/test_interpolation.py).

---

<div class="post-metadata">

**Author:** ![conpierce8](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/conpierce8/32/2464_2.png) [@conpierce8](https://fenicsproject.discourse.group/u/conpierce8)\
**Post date:** [August 31, 2022, 10:04pm UTC](https://fenicsproject.discourse.group/t/interpolate-into-complex-valued-vector-function/9178/2 "2022-08-31T22:04:42Z")

</div>

I don’t think you’re doing anything wrong. You should, however, consider interpolating to a DG-1 function space and using `VTXWriter` for exporting results, as noted here:

> [@Maxwell solver example does not work after 0.5 update](https://fenicsproject.discourse.group/t/maxwell-solver-example-does-not-work-after-0-5-update/9100/2):
>
> Please note that you should **never** use `XDMFFile` for post-processing `Nedelec-kind` elements (as it interpolates into CG-1, which is not well defined).
> 
> You should use `VTXWriter` after interpolating the solution into DG-1.

Below, I compare the real part of the y-component, exported with interpolation into a DG-1 space (plots labeled “VTK”) and without interpolation into a DG-1 space (plots labeled “XDMF”). You can see that the error depends on the type of discretization (left, right, or crossed).

 ![screenshot](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/7/7bf5291d25c5d8c25ded94dfd2191b2111de988a.png)

In general, the erroneous nonzero y-component is very small, as you can see by plotting your interpolated field with a “Glyph” filter, so I would guess that the interpolation is giving you the best representation of the plane wave that you can achieve with RT elements.

 ![screenshot2](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/3/37275858402e9bc458ed34cfd21d13418472de27.png)

---

<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:** [September 1, 2022, 9:19am UTC](https://fenicsproject.discourse.group/t/interpolate-into-complex-valued-vector-function/9178/3 "2022-09-01T09:19:18Z")

</div>

I see, thank you! I could reproduce the plots that you got once I projected the function into DG space.

Just as a remark, this discretization error (although a couple orders small) still seems significant since they appear when complex valued functions are utilized. I had not encountered these earlier when I used two-part real RT space to represent the complex functions (in `dolfin`, although I believe it did some post-processing while writing RT into XDMF).

---

<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:** [September 2, 2022, 3:13pm UTC](https://fenicsproject.discourse.group/t/interpolate-into-complex-valued-vector-function/9178/4 "2022-09-02T15:13:01Z")

</div>

Depending on what function you used with XDMFFile,

- `write(u)` (interpolates into a CG-1 space)
- `write_checkpoint(u)` (for some orders represented with: [XDMF Model and Format - XdmfWeb](https://xdmf.org/index.php/XDMF_Model_and_Format#Attribute) section finite element function).
