plot solution of 2d problem only in one row of the mesh


47
views
0
9 days ago by
Hello,

I'm fairly new to fenics (one week) and I would like to do the following, if possible.

Let say I have a 2d mesh of [0,1]x[0,1] with 10 nodes in each axis, and the solution of, a Poisson equation for instance, is stored un variable u.
How do I plot solution u only in y=k? (k=0 for now is sufficient). I would expect a x vs u plot.

Following an example, i tried this (but didin't worked):

y = 0.0
meshx = IntervalMesh(nx, A, B)
W = FunctionSpace(meshx,"Lagrange",1)
w = Function(W)
coor = meshx.coordinates()
w_array = w.vector().array()
if meshx.num_vertices() == len(w_array):
for i in range(meshx.num_vertices()):
w_array[i] = u(coor[i][0],y)

w.vector().set_local(w_array)
plot(w, interactive=True)








Community: FEniCS Project

2 Answers


2
9 days ago by
Finally, I got it working like this:

#plot y=k
y = 0.0
meshx = IntervalMesh(nx, x0, xN)
W = FunctionSpace(meshx,"Lagrange",1)
w = Function(W)
coor = meshx.coordinates()
w_array = w.vector().array()

if meshx.num_vertices() == len(w_array):
for i in range(meshx.num_vertices()):
w_array[i] = u(coor[i][0],y)

w.vector().set_local(w_array)
plot(w)
plt.show()
I think there is a simpler alternative using matplotlib, since u can be evaluated at arbitrary points (x,y) by u(x,y).

import numpy as np
import dolfin as df
import matplotlib.pyplot as plt

mesh = df.UnitSquareMesh(10,10)
CG1 = df.FunctionSpace(mesh,"CG",1)

u = df.interpolate(df.Expression("sin(pi*x[0])*cos(pi*x[1])",degree=1),CG1)

xx = np.linspace(0,1,100)
yy = np.empty_like(xx)

for i in range(100):
yy[i] = u(xx[i],0.3)


plt.plot(xx,yy)
plt.show()
written 9 days ago by Lukas O.  
0
9 days ago by
The plot functionality of fenics is rather simple. I would rely on external tools like paraview.

However, you can e.g. use fenicstools (https://github.com/mikaem/fenicstools).
Please login to add an answer/comment or follow this question.

Similar posts:
Search »