How to Import csv data file to FEniCS?

Hi everyone,
I am looking for a way to import data from a .csv file to interpolate it over a mesh, so
I tried to run the code in this link https://groups.google.com/forum/#!topic/fenics-support/Bs4p6D0G4dM (using UserExpression instead of Expression), but I got the following error: AttributeError: ‘ExpressionFromScipyFunction’ object has no attribute ‘_ufl_function_space’
I am using fenics 2018.1, with fenics 2017.2.0 it used to work,
Does anyone know what am I missing?
thanks,

Hi, consider

from dolfin import *
import numpy as np
from scipy.interpolate import interp2d

data = np.loadtxt('dato.xyz')
x, y, values = data[:,0], data[:,1], data[:,2]
interpolant = interp2d(x, y, values, kind='linear', copy=False, bounds_error=True)

class ExpressionFromScipyFunction(UserExpression):
    def __init__(self, f, **kwargs):
        self._f = f
        UserExpression.__init__(self, **kwargs)
    def eval(self, values, x):
        values[:] = self._f(*x)

mesh = UnitSquareMesh(3, 3)
V = FunctionSpace(mesh, 'CG', 1)

expression = ExpressionFromScipyFunction(interpolant, element=V.ufl_element())
expression = interpolate(expression, V) 
3 Likes

@MiroK
Thank you, it worked!!!