Multiplying a solution vector by a scalar phase factor function in FEnICSx

Hello all.
I tried using the method described in this post to multiply a vector in the Nedelec space with a scalar function interpolated using Lagrange shape functions. Errors indicate that there is a rank mismatch in the “uh.interpolate” procedure multiplying the Nedelec function “E” with the scalar phase function “vv” and placing it back in the Nedelec space “uh” using the “fem.Expression” function (i.e. uh = E \cdot \exp(jkx\cdot a_r) ). What is the correct way to multiply the solution vector (in its Nedelec basis) with a scalar function?

My trial code is here:


import numpy as np
from mpi4py import MPI
from dolfinx import fem
from dolfinx import mesh as msh
import ufl
import basix.ufl

class Hinc:
    def __init__(self, x0, y0, H):
        self.x0 = x0
        self.y0 = y0
        self.H = H
    def eval(self, x):
        hx = -self.H * (x[1] - self.y0) / (2.0 * np.pi * ((x[0] - self.x0)**2 + (x[1] - self.y0)**2))
        hy = self.H * (x[0] - self.x0) / (2.0 * np.pi * ((x[0] - self.x0)**2 + (x[1] - self.y0)**2))
        hz = np.full_like(hy, 0.0+0.0j, dtype=np.complex128)
        return(hx, hy, hz)

class rp:
    def __init__(self, k0, ar):
        self.k0 = k0
        self.ar = ar
    def pp1(self, x):
        phsfac = np.exp(1j * self.k0 * (self.ar[0] * x[0] + self.ar[1] * x[1] + self.ar[2] * x[2]))
        return phsfac

N = 4
mesh = msh.create_unit_cube(MPI.COMM_WORLD, N, N, N, msh.CellType.tetrahedron)

mesh.topology.create_connectivity(mesh.topology.dim-1, mesh.topology.dim)
elem = basix.ufl.element('Nedelec 1st kind H(curl)', mesh.basix_cell(), degree=2)
V = fem.functionspace(mesh, elem)
E = fem.Function(V)
U = fem.Function(V)

Q = fem.functionspace(mesh, ('CG', 2, (1, )))
vv = fem.Function(Q)

f = Hinc(1.5, 1.5, 1.0+0j)
E.interpolate(f.eval)

rr = np.array([1.0, -1.0, 0.5])
p = rp(1.0, rr)
vv.interpolate(p.pp1)

U.interpolate(fem.Expression(E*vv, V.element.interpolation_points()))

sys.exit(0)

The error I get:

Traceback (most recent call last):
  File "/home/bill/Cluster/Fenics2020/PatchAntenna/CircularPatches/RadPatternTest.py", line 288, in <module>
    r = Model([8.806, 3.713])
  File "/home/bill/Cluster/Fenics2020/PatchAntenna/CircularPatches/RadPatternTest.py", line 255, in Model
    uh.interpolate(fem.Expression(vv*E, V.element.interpolation_points()))
  File "/usr/lib/python3/dist-packages/ufl/exproperators.py", line 186, in _mul
    return _mult(self, o)
  File "/usr/lib/python3/dist-packages/ufl/exproperators.py", line 159, in _mult
    raise ValueError(f"Invalid ranks {r1} and {r2} in product.")
ValueError: Invalid ranks 1 and 1 in product.

The code is way too long. We don’t need to see 109 lines about mesh generation to reproduce the issue: just pick the unit cube. Furthermore, we don’t need to see the problem itself either: reduce it to the minimum amount of lines that get you to carrying out the interpolation.

I modified the long code above with a shorter code that reproduces the error. Sorry about the previous big code dump!

Simply remove the last argument:
Q = fem.functionspace(mesh, ('CG', 2))

Thanks for that Dokken. It is always something simple that slips by. The code runs without error. Cheers!