### saving time-varying Expressions

283
views
0
10 months ago by
Hi,
I'm trying to save my exact solution u which was defined as an Expression with varying parameter t; unfortunately, only the first step is saved. Below is the basic code.
from fenics import *

num_steps = 20
dt = 0.1

# Create mesh and define function space
nx = ny = 30
mesh = RectangleMesh(Point(-2, -2), Point(2, 2), nx, ny)
V = FunctionSpace(mesh, 'P', 1)

import sympy as sym
x, y, t = sym.symbols('x[0], x[1], t')
u_e = x*x*y + t
#f_e = -(sym.diff(sym.diff(u_e,x),x) + sym.diff(sym.diff(u_e,y),y)) + sym.diff(u_e,t)
u_code = sym.printing.ccode(u_e)
#f_code = sym.printing.ccode(f_e)

u_0 = Expression(u_code, degree = 2, t = 0)
t = 0

vtkfile = File('save_f_results/u_exact.pvd')
for n in range(num_steps):
u_0.t = t
u_exact = interpolate(u_0, V)

# Update current time
t += dt
# Save to file and plot solution
vtkfile << (u_exact, float(t))

​

My goal is to be able to save the exact u and exact f as they vary in time. This has troubled me for some time so if anyone can offer suggestions, I appreciate any help.

Community: FEniCS Project

3
10 months ago by
All the steps are saved but under different f_<something> names. To 'unite' them, add
u_exact.rename('u', 'u')​

after you interpolate u_0.

Thank you so much!! Now my solution is saving properly.
written 10 months ago by K Bicol