I obtained a function T (here, the temperature) from a former finite element computation, and I would like to use f(T(x),x) as a coefficient in a further finite element computation (here, linear elasticity) on the same mesh, where x is the spatial variable and f is some nonlinear function (here, the thermal expansion, given by a material dependent, piecewise linear interpolation of values given in a textbook).
I think I solved this by employing a custom Python class derived from a UserExpression (didn’t validate the results). However, this is extremely slow. Is there a better, possibly canonical solution for this problem?
My function x \mapsto T(x) is an element of FunctionSpace(mesh,‘P’,1).
My function (T,x) \mapsto f(T,x) is currently implemented in the form of two scipy.interpolate.interp1d functions T \mapsto f_1(T), f_2(T), each for one of the materials involved, that is f(T,x)=f_1(T) if x lies in material 1, while f(T,x)=f_2(T) if x lies in material 2.
Now I need to implement the function x \mapsto f(T(x),x) in a way that fenics accepts as a coefficient in a bilinear form, for instance as an element of FunctionSpace(mesh,‘P’,1).
Even without the explicit x dependence I wouldn’t know how to do this. For instance, simply using f_1(T) in the bilinear form with f_1 as above doesn’t work:
However, I would expect this solution to be quite slow. In the meantime I found a different method which is both simple and efficient, namely to work directly with the underlying (numpy) arrays.
Suppose that f is implemented as a function working with numpy arrays and T \in V. Then