spectralDNS / shenfun

High performance computational platform in Python for the spectral Galerkin method

Home Page:http://shenfun.readthedocs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError when passing a lambdified function as a buffer

thoeschler opened this issue · comments

Hi Mikael,

I am reporting another issue which is related to the evaluation of a scalar function on a 2D grid at the quadrature points. I used the same code as in the 3D Poisson's equation demo. However, in the case that the function doesn't depend on the entire set of spatial variables, I get a TypeError if I am using the lambdified version. I found a way to circumvent the error by passing the symbolic expression. I hope that the code below shows what I mean. However, I want to let you know that the problem might cause some headaches for inexperienced users like me.

Best regards,
Thilo

from sympy import symbols, lambdify
from mpi4py import MPI
from shenfun import Array, Basis, TensorProductSpace
from mpi4py_fft.pencil import Subcomm

comm = MPI.COMM_WORLD
family = 'legendre'

# Use sympy
x, y = symbols("x,y")
# symbolic expression
fe_working = x*(x+y)
fe_non_working = x
# lambdify
fl_working = lambdify((x, y), fe_working, 'numpy')
fl_non_working = lambdify((x, y), fe_non_working, 'numpy')

# Size of discretization
n = 25
N = [n, n+1]

BX = Basis(N[0], family=family, bc=(0, 0))
BY = Basis(N[1], family=family, bc=(0, 0))
subcomms = Subcomm(MPI.COMM_WORLD, [0, 1])
T = TensorProductSpace(subcomms, (BX, BY), axes=(1, 0))
X = T.local_mesh()

# Get f on quad points

# working case with expression
fj = Array(T, buffer=fe_working)
fj = Array(T, buffer=fe_non_working)

# working case with lambdify
fj = Array(T, buffer=fl_working(*X))

# non working case with lambdify
fj = Array(T, buffer=fl_non_working(*X)) # TypeError: buffer is too small for requested array

Hi Thilo
In the newest version of shenfun, as you seem to have noticed, there is no need to lambdify your expression before sending it to Array. So I'm not worried about the non-working case. Also, the error there, I think, is simply in the sympy call fl_non_working(*X), because fe_non_working does not depend on y. Just use the working cases and you should be fine.