Exporting and plotting derived quantities in time-dependent problem

Hello,

I am learning this wonderful package and have a question.

I am trying to solve a time-dependent problem. The time-stepping part of my program looks like this:

Time-stepping

t = 0
for n in range(num_steps):

Update current time

t += dt

Solve variational problem for time step

solve(F == 0, f)

Save solution to file (VTK)

_f1, _f2, _f3, _f4 = f.split()
W = FunctionSpace(mesh,‘P’,1)
_fm = project(_f1** 2 + _f2** 2,W)
vtkfile_fm << (_fm, t)

Update previous solution

f_n.assign(f)

As you can see, f is the solution to my variational problem and fm = f1** 2 + f2** 2 is a derived quantity which I want to export and animate with ParaView.

The program ran and exported the .vtu and pvd files.

However, the pvd file didn’t animate: the first frame displayed but subsequent frames just show grey screen.

I traced the issue back to the fact that each .vtu file has a different name. For example:

<DataArray type="Float64" Name="f_58"

for the first .vtu file, and

<DataArray type="Float64" Name="f_107"

for the second file, and so on.

If I manually change the Scalars and Name fields to the same values for all files then the animation work.

How could I enforce the same name for the files automatically, please?

Thank you very much.

There are multiple things you should do to improve your code.
First,

You should use _fm.rename("fm","") after projection to make time dependent visualization work.

Secondly, one should avoid calling

and

Inside loops, as these calls does not cache expensive calls to matrix creation, solver creation, and in the case of projection a reassembly of a time independent matrix (and subsequent LU factorization).

You should instead use
LUSolver
for projections, and
NonlinearVariationalSolver for nonlinear problems.

In general, if you are new to Dolfin, it is recommended to use Dolfinx, see:

Hi Jorgen,

Thank you very much for the solution and suggestions!

Now that I have my very first program working, I will switch to DOLFINx as suggested.

Hi Jorgen,

May I trouble you with another question, please?

I am trying to set up the nonlinear problem outside the loop using NonlinearVariationalSolver as you suggested.

I have:

Define the test functions and trial functions

v1, v2, v3, v4 = TestFunctions(V)
df = TrialFunction(V)
f = Function(V)
f_n = Function(V)
df1, df2, df3, df4 = split(df)
f1, f2, f3, f4 = split(f)
f1_n, f2_n, f3_n, f4_n = split(f_n)

Define the variational problem

def Vpot(f1,f2):
return m0** 2+(m0** 2-omega0** 2)(3.0(f1** 2+f2** 2)-4.0)*(f1** 2+f2** 2)

F = (f3-f3_n)* v1* dx + dt* dot(grad(f1),grad(v1))* dx + dt* Vpot(f1,f2)* f1* v1* dx
+ (f4-f4_n)* v2* dx + dt* dot(grad(f2),grad(v2))* dx + dt* Vpot(f1,f2)* f2* v2* dx
+ (f1-f1_n-dt* f3)* v3* dx + (f2-f2_n-dt* f4)* v4* dx

J = derivative(F, f, df)

problem=NonlinearVariationalProblem(F, f, J)

solver=NonlinearVariationalSolver(problem)

but the step “problem=NonlinearVariationalProblem(F, f, J)” gives me an error with message “TypeError: init(): incompatible constructor arguments. The following argument types are supported:
1. dolfin.cpp.fem.NonlinearVariationalProblem(arg0: dolfin.cpp.fem.Form, arg1: dolfin.cpp.function.Function, arg2: List[dolfin.cpp.fem.DirichletBC], arg3: dolfin.cpp.fem.Form)”.

Could you tell me if I am using the wrong syntax here, please?