This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Examine complex-valued function at a point x

+2 votes

I solve a complex-valued problem with FeNiCs. This works quite well. Now I want to examine a function which consists of the real-valued parts of the solutions of the problem (phi_r and u_r).

The problem is: I would like to examine this new function at one point x. My problem is: I can calculate the function for the whole domain and I can even plot it, but I can't filter out the value of it at one point.

phi_r , phi_i = split(phi)
u_r, u_i = split(u)

d=Function(V)
d=inner(grad(phi_r),grad(u_r))

plot(d, title='d')

print d(0.0,0.0)

The last line does not work. I have found out, that even this one does not work:

print phi_r(0.0,0.0)

But when I write it the following way, it works:

print phi(0.0,0.0)[0]

So, why is this? Thank you very much for your help!

asked Oct 18, 2013 by Lollwies FEniCS Novice (230 points)

This is not a well-conditioned question. Good one should include at least one of the following (but ideally both)

  • short but complete code
  • error message

and also

  • version of used FEniCS

Ok, I'm sorry. Here is a complete code, which solves the Laplace-equation:

from dolfin import *
import numpy

mesh=UnitSquareMesh(6,4)

# calculate u

f_r=Constant(-6.0)
f_i=Constant(-6.0)

p=1
V = FunctionSpace(mesh,'CG',p)
W = V*V

u0_r = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]')
u0_i = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]')

def u0_boundary(x, on_boundary):
    return on_boundary

bcs = [DirichletBC(W.sub(0), u0_r, u0_boundary), DirichletBC(W.sub(1), u0_i, u0_boundary)]

(u_r, u_i) = TrialFunctions(W)
(v_r, v_i) = TestFunctions(W)

a_r = inner(grad(u_r),grad(v_r))*dx - inner(grad(u_i),grad(v_i))*dx
a_i = inner(grad(u_r),grad(v_i))*dx + inner(grad(u_i),grad(v_r))*dx

L_r = inner(f_r,v_r)*dx - inner(f_i,v_i)*dx
L_i = inner(f_r, v_i)*dx + inner(f_i,v_r)*dx

a = a_r+a_i
L = L_r+L_i

A = assemble(a)
b = assemble(L)
for bc in bcs: bc.apply(A, b)

u = Function(W)
solve(A, u.vector(), b, 'lu')

u_r, u_i = split(u)

plot(u_r)

# calculate phi

g_r=Constant(10.0)
g_i=Constant(10.0)

(phi_r, phi_i) = TrialFunctions(W)
(vp_r, vp_i) = TestFunctions(W)

ap_r = inner(grad(phi_r),grad(vp_r))*dx - inner(grad(phi_i),grad(vp_i))*dx
ap_i = inner(grad(phi_r),grad(vp_i))*dx + inner(grad(phi_i),grad(vp_r))*dx

Lp_r = inner(g_r, vp_r)*dx - inner(g_i,vp_i)*dx
Lp_i = inner(g_r, vp_i)*dx + inner(g_i,vp_r)*dx

ap = ap_r+ap_i
Lp = Lp_r+Lp_i

Ap = assemble(ap)
bp = assemble(Lp)
for bc in bcs: bc.apply(Ap, bp)

phi = Function(W)
solve(Ap, phi.vector(), bp, 'lu')

phi_r, phi_i = split(phi)

plot(phi_r)

# function d

d=Function(V)
d=inner(grad(phi_r),grad(u_r))

plot(d, title='d')

print d(0.0,0.0)
print phi_r(0.0,0.0)
print phi(0.0,0.0)[0]

interactive()

The error-message that occurs is:

ufl.log.UFLException: Expecting dim to match the geometric dimension, got dim=1 and gdim=2

My FEniCS-version is for Ubuntu and with Python (is that enough information on the version?).

BTW you might be interested in this: https://bitbucket.org/tuomas2/complexdolfin/

1 Answer

+3 votes
 
Best answer

There are two issues.

d=Function(V)
d=inner(grad(phi_r),grad(u_r))

is not a correct python code. Do rather

d = inner(grad(phi_r),grad(u_r))
d = project(d, V)

Also use rather

phi_r, phi_i = phi.split()

instead of

phi_r, phi_i = split(phi)

The latter makes merely UFL expression (which can be used in forms) while former creates DOLFIN subfunction (which can be evaluated, plotted, ...).

answered Oct 18, 2013 by Jan Blechta FEniCS Expert (51,420 points)
selected Oct 18, 2013 by Lollwies

Thank you very much! My code works now. Interesting, that there are different ways to split a function.

...