FEniCS.jl icon indicating copy to clipboard operation
FEniCS.jl copied to clipboard

Using DirichletBC

Open barche opened this issue 8 years ago • 1 comments

Hi,

I'm trying to use the DirichletBC, translating the following Python script:

from dolfin import *

n = 64

# Create mesh and define function space
mesh = UnitSquareMesh(n, n)
V = FunctionSpace(mesh, 'Lagrange', 1)

# Define boundary conditions
u0 = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)

def u0_boundary(x, on_boundary):
    return on_boundary

bc = DirichletBC(V, u0, u0_boundary)

into Julia:

using FEniCS

n = 64

# Create mesh and define function space
mesh = UnitSquareMesh(n, n)
V = FunctionSpace(mesh, "Lagrange", 1)

# Define boundary conditions
u0 = Expression("1 + x[0]*x[0] + 2*x[1]*x[1]", degree=2)

function bc_func(x,  on_boundary)
    return on_boundary
end

bc = DirichletBC(V, u0, bc_func)

This gives the error:

ERROR: LoadError: PyError (ccall(@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, arg, C_NULL)) <class 'TypeError'>
TypeError("float() argument must be a string or a number, not 'PyCall.jlwrap'",)
  File "/usr/lib/python3.6/site-packages/dolfin/fem/bcs.py", line 97, in __init__
    expr = Constant(args[1])  # let Constant handle all problems
  File "/usr/lib/python3.6/site-packages/dolfin/functions/constant.py", line 75, in __init__
    floats = list(map(float, array.flat))

Stacktrace:
 [1] pyerr_check at /home/username/.julia/v0.6/PyCall/src/exception.jl:56 [inlined]
 [2] pyerr_check at /home/username/.julia/v0.6/PyCall/src/exception.jl:61 [inlined]
 [3] macro expansion at /home/username/.julia/v0.6/PyCall/src/exception.jl:81 [inlined]
 [4] #_pycall#66(::Array{Any,1}, ::Function, ::PyCall.PyObject, ::PyCall.PyObject, ::Vararg{Any,N} where N) at /home/username/.julia/v0.6/PyCall/src/PyCall.jl:596
 [5] _pycall(::PyCall.PyObject, ::PyCall.PyObject, ::Vararg{Any,N} where N) at /home/username/.julia/v0.6/PyCall/src/PyCall.jl:584
 [6] #pycall#70(::Array{Any,1}, ::Function, ::PyCall.PyObject, ::Type{PyCall.PyAny}, ::PyCall.PyObject, ::Vararg{Any,N} where N) at /home/username/.julia/v0.6/PyCall/src/PyCall.jl:618
 [7] pycall(::PyCall.PyObject, ::Type{PyCall.PyAny}, ::PyCall.PyObject, ::Vararg{Any,N} where N) at /home/username/.julia/v0.6/PyCall/src/PyCall.jl:618
 [8] #call#71(::Array{Any,1}, ::PyCall.PyObject, ::PyCall.PyObject, ::Vararg{Any,N} where N) at /home/username/.julia/v0.6/PyCall/src/PyCall.jl:621
 [9] DirichletBC(::FEniCS.FunctionSpaceImpl, ::FEniCS.ExpressionImpl, ::Function) at /home/username/.julia/v0.6/FEniCS/src/jfem.jl:68
while loading /home/username/CloudStation/projects/julia/gsoc/2017/poisson.jl, in expression starting on line 16

Any ideas?

barche avatar Jul 27 '17 19:07 barche

Hi!

We've had a very brief discussion on this in Gitter with Chris. https://fenicsproject.org/pub/tutorial/html/._ftut1004.html gives a slightly more detailed explanation of what each function does. To get it to work like that in Julia, I believe we need to change the u0_boundary function slightly (I'm not quite sure how this modification will be , but I'd assume it will resemble the direct implementation (calculating the distance between point and boundary?)


From: Bart Janssens [email protected] Sent: 27 July 2017 20:46:48 To: JuliaDiffEq/FEniCS.jl Cc: Subscribed Subject: [JuliaDiffEq/FEniCS.jl] Using DirichletBC (#20)

Hi,

I'm trying to use the DirichletBC, translating the following Python script:

from dolfin import *

n = 64

Create mesh and define function space

mesh = UnitSquareMesh(n, n) V = FunctionSpace(mesh, 'Lagrange', 1)

Define boundary conditions

u0 = Expression('1 + x[0]x[0] + 2x[1]*x[1]', degree=2)

def u0_boundary(x, on_boundary): return on_boundary

bc = DirichletBC(V, u0, u0_boundary)

into Julia:

using FEniCS

n = 64

Create mesh and define function space

mesh = UnitSquareMesh(n, n) V = FunctionSpace(mesh, "Lagrange", 1)

Define boundary conditions

u0 = Expression("1 + x[0]x[0] + 2x[1]*x[1]", degree=2)

function bc_func(x, on_boundary) return on_boundary end

bc = DirichletBC(V, u0, bc_func)

This gives the error:

ERROR: LoadError: PyError (ccall(@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, arg, C_NULL)) <class 'TypeError'> TypeError("float() argument must be a string or a number, not 'PyCall.jlwrap'",) File "/usr/lib/python3.6/site-packages/dolfin/fem/bcs.py", line 97, in init expr = Constant(args[1]) # let Constant handle all problems File "/usr/lib/python3.6/site-packages/dolfin/functions/constant.py", line 75, in init floats = list(map(float, array.flat))

Stacktrace: [1] pyerr_check at /home/username/.julia/v0.6/PyCall/src/exception.jl:56 [inlined] [2] pyerr_check at /home/username/.julia/v0.6/PyCall/src/exception.jl:61 [inlined] [3] macro expansion at /home/username/.julia/v0.6/PyCall/src/exception.jl:81 [inlined] [4] #_pycall#66(::Array{Any,1}, ::Function, ::PyCall.PyObject, ::PyCall.PyObject, ::Vararg{Any,N} where N) at /home/username/.julia/v0.6/PyCall/src/PyCall.jl:596 [5] _pycall(::PyCall.PyObject, ::PyCall.PyObject, ::Vararg{Any,N} where N) at /home/username/.julia/v0.6/PyCall/src/PyCall.jl:584 [6] #pycall#70(::Array{Any,1}, ::Function, ::PyCall.PyObject, ::Type{PyCall.PyAny}, ::PyCall.PyObject, ::Vararg{Any,N} where N) at /home/username/.julia/v0.6/PyCall/src/PyCall.jl:618 [7] pycall(::PyCall.PyObject, ::Type{PyCall.PyAny}, ::PyCall.PyObject, ::Vararg{Any,N} where N) at /home/username/.julia/v0.6/PyCall/src/PyCall.jl:618 [8] #call#71(::Array{Any,1}, ::PyCall.PyObject, ::PyCall.PyObject, ::Vararg{Any,N} where N) at /home/username/.julia/v0.6/PyCall/src/PyCall.jl:621 [9] DirichletBC(::FEniCS.FunctionSpaceImpl, ::FEniCS.ExpressionImpl, ::Function) at /home/username/.julia/v0.6/FEniCS/src/jfem.jl:68 while loading /home/username/CloudStation/projects/julia/gsoc/2017/poisson.jl, in expression starting on line 16

Any ideas?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/JuliaDiffEq/FEniCS.jl/issues/20, or mute the threadhttps://github.com/notifications/unsubscribe-auth/APV6MX70pb1_IhEpob_iCXpclO54Ofoxks5sSOkngaJpZM4OlwHt.

ysimillides avatar Jul 27 '17 21:07 ysimillides