1d plot

Asked by Jacopo Lanzoni

Dear all,

I wrote a script for a 1-dimensional Dirichlet problem for Helmholtz equation but, when I try to plot the solution simply with plot(u), I get the following error message

Solving linear variational problem.
Object cannot be plotted directly, projecting to piecewise linears.
Traceback (most recent call last):
  File "Helmholtz1D_PML.py", line 84, in <module>
    plot(ur)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/common/plot.py", line 70, in dolfin_plot
    return viper_dolfin.plot(make_viper_object(object), *args, **kwargs)
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper_dolfin.py", line 431, in plot
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper_dolfin.py", line 380, in plot
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper_dolfin.py", line 394, in autoplot
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper_dolfin.py", line 47, in __init__
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper_dolfin.py", line 72, in plot
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper_dolfin.py", line 150, in plot_genericfunction
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper.py", line 1261, in plot_xy
TypeError: function takes exactly 2 arguments (1 given)

The same script rewritten for a 2-dimensional case works perfectly fine. What should I add to the plot command?

Best wishes
Jacopo

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Johan Hake
Solved:
Last query:
Last reply:
Revision history for this message
Johan Hake (johan-hake) said :
#1

Can you provide a minimal runnable example that reproduces the error.

Johan

On Friday December 2 2011 11:01:00 Jacopo Lanzoni wrote:
> New question #180690 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/180690
>
> Dear all,
>
> I wrote a script for a 1-dimensional Dirichlet problem for Helmholtz
> equation but, when I try to plot the solution simply with plot(u), I get
> the following error message
>
> Solving linear variational problem.
> Object cannot be plotted directly, projecting to piecewise linears.
> Traceback (most recent call last):
> File "Helmholtz1D_PML.py", line 84, in <module>
> plot(ur)
> File
> "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/d
> olfin/common/plot.py", line 70, in dolfin_plot return
> viper_dolfin.plot(make_viper_object(object), *args, **kwargs) File
> "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper
> _dolfin.py", line 431, in plot File
> "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper
> _dolfin.py", line 380, in plot File
> "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper
> _dolfin.py", line 394, in autoplot File
> "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper
> _dolfin.py", line 47, in __init__ File
> "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper
> _dolfin.py", line 72, in plot File
> "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper
> _dolfin.py", line 150, in plot_genericfunction File
> "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/viper/viper
> .py", line 1261, in plot_xy TypeError: function takes exactly 2 arguments
> (1 given)
>
> The same script rewritten for a 2-dimensional case works perfectly fine.
> What should I add to the plot command?
>
> Best wishes
> Jacopo

Revision history for this message
Jacopo Lanzoni (lanzoni) said :
#2

Dear Johan,

Here is my code.
--------------------------------------------------------------------------------------------------------
from dolfin import *

# Input data
b = 10.0
mesh = Interval(100, -b, b) # domain [-10,10] with 100 elements (h=.2)
p = 1
a = 8 # PML INTERNAL boundary radius
sigmaval = 1
n = 1 # Power order of the PML function
k = 5
potval = 3*k**2 # k_1 = 2*k_0
l = 3 # Obstacle length
f_r = Expression('+cos(k*x[0])', {'k': k}) # Plane wave coming from right
f_i = Expression('-sin(k*x[0])', {'k': k})

# Definition of SubDomainS
class Domain(SubDomain):
    def inside(self, x, on_boundary):
        return abs(x[0]) <= a
class PML(SubDomain):
    def inside(self, x, on_boundary):
        return abs(x[0]) > a
class Obstacle(SubDomain):
    def inside(self, x, on_boundary):
 return abs(x[0]) < l/2

# Function 'pml' is 1 on PML, 0 elsewhere
pml = MeshFunction('uint', mesh, 1)
pml.set_all(0)
pml0 = PML()
pml0.mark(pml, 1)

# Function 'obstacle' is 1 on Obstacle, 0 elsewhere
obstacle = MeshFunction('uint', mesh, 1)
obstacle.set_all(0)
obstacle0 = Obstacle()
obstacle0.mark(obstacle, 1)

# Definition of boundaries
def PML_boundary(x, on_boundary):
    return on_boundary

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

# Definition of boundary conditions
bcr = DirichletBC(W.sub(0), Constant(0.0), PML_boundary) # Dirichlet condition
bci = DirichletBC(W.sub(1), Constant(0.0), PML_boundary)
bc = [bcr, bci]

V0 = FunctionSpace(mesh, 'DG', n)

sigma = Function(V0)
sigma_values = [0, sigmaval]
for cell_no in range(len(pml.array())):
    subdomain_no = pml.array()[cell_no]
    sigma.vector()[cell_no] = sigma_values[subdomain_no]

Vpot = Function(V0)
V_values = [0, potval] # values of V in the two subdomains
for cell_no in range(len(obstacle.array())):
    obstacle_no = obstacle.array()[cell_no]
    Vpot.vector()[cell_no] = V_values[obstacle_no]

x = Expression('x[0]')
sigma = sigma*(x-a)**n
Ar = 1/(1+sigma**2)
Ai = sigma/(1+sigma**2)
C = Vpot + k**2
(u_r, u_i) = TrialFunctions(W)
(v_r, v_i) = TestFunctions(W)

ar = inner( Ar*grad(u_r) +Ai*grad(u_i), grad(v_r))*dx - inner(C*(u_r -sigma*u_i), v_r)*dx
ai = inner(-Ai*grad(u_r) +Ar*grad(u_i), grad(v_i))*dx - inner(C*(sigma*u_r +u_i), v_i)*dx
Lr = Vpot*(f_r -sigma*f_i)*v_r*dx
Li = Vpot*(sigma*f_r +f_i)*v_i*dx

problem = VariationalProblem(ar+ai, Lr+Li, bc)
u = problem.solve()

ur, ui = split(u)

plot(ur)
---------------------------------------------------------------------------------

Revision history for this message
Best Johan Hake (johan-hake) said :
#3

Works fine here!

There have been some changes to a lot of stuff in DOLFIN. I suggest you
upgrade to latest version. 1.0 is soon out!

Johan

On Friday December 2 2011 23:55:53 Jacopo Lanzoni wrote:
> Question #180690 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/180690
>
> Jacopo Lanzoni posted a new comment:
> Dear Johan,
>
> Here is my code.
> ---------------------------------------------------------------------------
> ----------------------------- from dolfin import *
>
> # Input data
> b = 10.0
> mesh = Interval(100, -b, b) # domain [-10,10] with 100 elements (h=.2)
> p = 1
> a = 8 # PML INTERNAL boundary radius
> sigmaval = 1
> n = 1 # Power order of the PML function
> k = 5
> potval = 3*k**2 # k_1 = 2*k_0
> l = 3 # Obstacle length
> f_r = Expression('+cos(k*x[0])', {'k': k}) # Plane wave coming from right
> f_i = Expression('-sin(k*x[0])', {'k': k})
>
> # Definition of SubDomainS
> class Domain(SubDomain):
> def inside(self, x, on_boundary):
> return abs(x[0]) <= a
> class PML(SubDomain):
> def inside(self, x, on_boundary):
> return abs(x[0]) > a
> class Obstacle(SubDomain):
> def inside(self, x, on_boundary):
> return abs(x[0]) < l/2
>
> # Function 'pml' is 1 on PML, 0 elsewhere
> pml = MeshFunction('uint', mesh, 1)
> pml.set_all(0)
> pml0 = PML()
> pml0.mark(pml, 1)
>
> # Function 'obstacle' is 1 on Obstacle, 0 elsewhere
> obstacle = MeshFunction('uint', mesh, 1)
> obstacle.set_all(0)
> obstacle0 = Obstacle()
> obstacle0.mark(obstacle, 1)
>
> # Definition of boundaries
> def PML_boundary(x, on_boundary):
> return on_boundary
>
> V = FunctionSpace(mesh,'CG',p)
> W = V*V
>
> # Definition of boundary conditions
> bcr = DirichletBC(W.sub(0), Constant(0.0), PML_boundary) # Dirichlet
> condition bci = DirichletBC(W.sub(1), Constant(0.0), PML_boundary)
> bc = [bcr, bci]
>
> V0 = FunctionSpace(mesh, 'DG', n)
>
> sigma = Function(V0)
> sigma_values = [0, sigmaval]
> for cell_no in range(len(pml.array())):
> subdomain_no = pml.array()[cell_no]
> sigma.vector()[cell_no] = sigma_values[subdomain_no]
>
> Vpot = Function(V0)
> V_values = [0, potval] # values of V in the two subdomains
> for cell_no in range(len(obstacle.array())):
> obstacle_no = obstacle.array()[cell_no]
> Vpot.vector()[cell_no] = V_values[obstacle_no]
>
> x = Expression('x[0]')
> sigma = sigma*(x-a)**n
> Ar = 1/(1+sigma**2)
> Ai = sigma/(1+sigma**2)
> C = Vpot + k**2
> (u_r, u_i) = TrialFunctions(W)
> (v_r, v_i) = TestFunctions(W)
>
> ar = inner( Ar*grad(u_r) +Ai*grad(u_i), grad(v_r))*dx - inner(C*(u_r
> -sigma*u_i), v_r)*dx ai = inner(-Ai*grad(u_r) +Ar*grad(u_i), grad(v_i))*dx
> - inner(C*(sigma*u_r +u_i), v_i)*dx Lr = Vpot*(f_r -sigma*f_i)*v_r*dx
> Li = Vpot*(sigma*f_r +f_i)*v_i*dx
>
> problem = VariationalProblem(ar+ai, Lr+Li, bc)
> u = problem.solve()
>
> ur, ui = split(u)
>
> plot(ur)
> ---------------------------------------------------------------------------
> ------

Revision history for this message
Jacopo Lanzoni (lanzoni) said :
#4

Thanks Johan Hake, that solved my question.