Hi, everyone. I’m new here and I have a quik question. I’m trying to export a function of solution in time stepping by using the Function project, and following is a simple example:
from dolfin import *
# Time step
dt = 0.005
# Define mesh
mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, "Lagrange", 1)
# Define variational problem and initial condition
u = TrialFunction(V)
v = TestFunction(V)
u0 = Expression('x[0]+x[1]',degree=1)
u0 = project(u0,V)
a = u/dt*v*dx+inner(grad(u), grad(v))*dx
L = u0/dt*v*dx
A = assemble(a)
# Compute solution and time stepping
file1 = File("Poisson/poisson1.pvd")
file2 = File("Poisson/poisson2.pvd")
ux = Function(V)
t=0
for t in range(0,30):
b=assemble(L)
solve(A,ux.vector(),b)
file1 << ux
uux=project(ux**2+1,V)
file2 << uux
u0.assign(ux)
t+=1
This form finally leads to unupdating of uux while ux can update normally. So what should I do to get the uxx updating normally with the update of ux. Thanks!
A MWE is the simplest piece of code that reproduces or explains the problem you have.
It must be possible to take the MWE, paste it into a file and run it, without any extra modifications. Good MWEs can be as little as 10 lines of code.
If you can remove a line of code from your MWE and still show your problem, then you don’t have an MWE!
Based on your MWE, it seems that for every time step you create new uux variable.
You can resolve this by defining uux outside of the loop and assigning the projection in every iteration,
or, alternatively, by renaming the variable (see code snippet below).
# uux = Function(V) # Define variable once...
t=0
for t in range(0,30):
b=assemble(L)
solve(A,ux.vector(),b)
file1 << ux
# uux.assign(project(ux**2+1,V)) # ...and then update it in every iteration...
uux=project(ux**2+1,V) # This creates new variable with different id in each iteration
uux.rename('uxx', '0') # ...or just rename it for the output
file2 << uux
u0.assign(ux)
t+=1
My preferred way is to define the variable before the loop and just update the value with assign.
If you have to output lots of variables for post-processing (in Paraview for example), renaming them could be useful as well.