create Function from MeshFunction / CellFunction ?

Asked by Paul Constantine

Is it possible to create a Function from a MeshFunction or CellFunction? I have a function defined by points in a text file. I can create a CellFunction following the example on page 189 of the FEniCS book. But I want to use this like a Function. Is such a thing possible? Can I go directly from the text file to a Function via something like project()? I couldn't find anything like this in the demos or the examples in the book.

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Paul Constantine
Solved:
Last query:
Last reply:
Revision history for this message
Paul Constantine (paul-g-constantine) said :
#1

Okay, something like this works, but it seems a little clunky. Let me know if you know anything slicker.

====
from dolfin import *
from numpy import loadtxt,interp

class MySine(Expression):

    def __init__(self,filename):
        self.data = loadtxt(open(filename,"rb"),delimiter=",",skiprows=1)

    def eval(self,values,x):
        values[0] = interp(x[0],self.data[:,0],self.data[:,1])

if __name__ == "__main__":

    f = MySine("sine_file.txt")

    mesh = UnitIntervalMesh(10)
    V = FunctionSpace(mesh,"CG",1)
    fV = project(f,V)

    print fV(0.33)
    print f(0.33)

====

Revision history for this message
Garth Wells (garth-wells) said :
#2

Take a look at

    demo/undocumented/tensor-weighted-poisson/

for an example.

Revision history for this message
Paul Constantine (paul-g-constantine) said :
#3

Looks great! Thanks!