Saving complicated functions to Paraview as a movie

Hi there. How do I write complicated time dependent mathematical expressions to file and have them play in paraview as a movie? In my attached example I’m calculating the spectral decomposition of a tensor and then performing some algebra before writing to file. The problem is that each time iteration is saved indepently and doesn’t play as a movie. Additionally the whole process is quite slow because I’m using project. Any suggestions would be very welcome?

from fenics import *

def e1(A): 
     return (tr(A) - sqrt((A[1,1]-A[0,0])**2+4*A[1,0]**2))/2

def e2(A): 
     return (tr(A) + sqrt((A[1,1]-A[0,0])**2+4*A[1,0]**2))/2


def vmat(A):
     lambdan = e1(A)
     c = A[1,0]
     d = A[1,1]
     v11 = lambdan - d
     nv11 = sqrt(v11**2 + c**2)
     a1 = v11/nv11
     c1 = c/nv11
     tol = 10e-12
     return conditional(gt(abs(c), tol),as_matrix(((a1,-c1),(c1,a1))),as_matrix(((1,0),(0,1)))) 


file_tau = XDMFFile('tau.xdmf')
file_tau.parameters['flush_output'] = True

STRESS = TensorFunctionSpace(mesh, 'CG', 2)

tau0 = Function(STRESS)

beta_ve=0.5
Wi_out=2

c = vmat(tau0)*as_matrix([[exp(e1(tau0)),0],[0,exp(e2(tau0))]])*vmat(tau0).T
tauff = ((1-beta_ve)/Wi_out)*(c-Identity(2))

t=0

tauf = project(tauff, STRESS)
file_tau.write(tauf, t)

You should make a projector, that re-uses the matrix at every time step.
See for instance:
https://simula-sscp.github.io/SSCP_2023_lectures/L12%20(FEniCS%20Intro)/L03_FEniCS_Darcy.html#advanced-reusable-projection
which would fix your paraview issue as well (as you are currently creating a new function with a new name every time you call project)

1 Like

Thanks a lot for your help Dokken, this was the solution I was looking for.