I have a 1d periodic space and I just interpolate some function and plot it. But this gives strange answer, please see
https://github.com/cpraveen/fenics/blob/master/1d/cosmic_ray/periodic_test.ipynb
What am I doing wrong ?
It looks like you plot the solution using matplotlib instead of the dolfin built-in visualization tool, see the output in Cell 5. So I guess commenting the '%matplotlib inline' line does solve your problem. As an alternative approach, you can dump the solution to a .vtk file and check the results using e.g. ParaView.
dolfin has limited support for matplotlib as the plotting backend via plot(). E.g: plot(u, backend='matplotlib').
plot()
plot(u, backend='matplotlib')
Using vtk as the plotting backend, I see no problems.
Furthermore, when I output the data, e.g: XDMFFile("u.xdmf").write(u), the results look fine.
XDMFFile("u.xdmf").write(u)
It looks like this may be an issue with the matplotlib plotting backend and constrained domains.
I cannot get vtk plots to show when I run fenics in docker. I have to save to file and then open with paraview/visit, which is too much work to see a simple 1d plot. Hence I was trying with notebook.
Is there an easier way to plot 1d solutions ? Gnuplot output would have been ideal.
I could plot with matplotlib in notebook mode
Here is the example
%matplotlib inline %config InlineBackend.figure_format='svg' import numpy as np import matplotlib.pyplot as plt from dolfin import * # number of points nc = 100 # Sub domain with periodicity class PeriodicBoundary(SubDomain): def inside(self, x, on_boundary): return (near(x[0],0.0) and on_boundary) def map(self, x, y): y[0] = x[0] - 1.0 u0 = Expression("1.0+0.5*sin(2*pi*x[0])",degree=1) mesh = UnitIntervalMesh(nc) V = FunctionSpace(mesh, "CG", 1, constrained_domain=PeriodicBoundary()) u = Function(V) u.interpolate(u0) ua=u.vector().array() x = V.tabulate_dof_coordinates() i = np.argsort(x) plt.plot(x[i],ua[i]);