How to make a video animation of two big matrices?

Here is a part of my code:

tmax = 0.1;
tmin = 0.;
nsteps = 10000.;
nparticles = 10000;
dt = (tmax - tmin)/nsteps;

xpa = Table[0, {ti, nsteps}, {i, nparticles}];
vpa = Table[0, {ti, nsteps}, {i, nparticles}];

Afterwards I have a loop in time that has nsteps in time and save the xpa and vpa in each iteration

Do[
  v = v + dt/2.*elp;
  x = x + dt*v;
  
  x = xpcp[x, xgrid, nparticles, ngrid, dx];(*x=x1;*)
  x = xpcm[x, xgrid, nparticles, ngrid, dx];(*x=x1;*)
  \[Rho] = 
   rho2[x, xgrid, q, nparticles, ngrid, dx];(*\[Rho]=\[Rho];*)
  \[Phi] = phi[\[Rho], ngrid, eps, dx]; \[Phi] = 1/(dx*dx)*\[Phi];
  el = elg[\[Phi], ngrid, dx]; el = el*(eps*dx);
  (*elpart[x,xgrid,nparticles,ngrid,dx,el];elp=elp;*)
  elp = elpart2[x, xgrid, nparticles, ngrid, dx, el];
  epa[[t]] = elp;
  xpa[[t]] = x;
  vpa[[t]] = v;
  
  v = v + dt/2.*elp,
  
  {t, 1, nsteps}] // AbsoluteTiming

Now I want to do a video animation where xpa will be the x coordinates, vpa will be the y coordinates (I need the something like this {xpa[[t]],vpa[[t]]} for all steps in time. As a trial I am trying to do this only for 65 iterations in time.

fplot = Compile[{{xp, _Real, 10000}, {vp, _Real, 
     10000}, {np, _Integer}, {ns, _Integer}, {temp, _Integer}},
   Block[{data, plot},
    data = Table[0, {ti, ns}, {i, np}];
    Do[data[[ti]] = MapThread[{#1, #2} &, {xp[[ti]], vp[[ti]]}], {ti, 
      ns}];
    
    plot = Table[ListPlot[data[[t]]], {t, temp}]; plot], 
   CompilationTarget -> "C", RuntimeOptions -> "Speed"];
plotf = fplot[xpa, vpa, nparticles, nsteps, 65]; // AbsoluteTiming

And when I run this it is extremely slow and gives this error

CompiledFunction::cfta: Argument {<<802080128 bytes>>} at position 1 should be a rank 10000 tensor of machine-size real numbers.

How can I correct this?

Try changing 10000 to 1 in

{xp, _Real, 10000}, {vp, _Real, 10000}

Hi Diogo,

for Compile you need to specify the number of dimensions of your parameters, not their length.
In your case, this should be 2 (not 10000)
Inside Compile you can check the length/dimensions of the passes arguments using

Dimensions[xp]
Dimensions[vp]

Compile allows Arrays of any length, only the dimensionality must be specified/fixed.

this is wrong:

Compile[{{xp, _Real, 10000}, {vp, _Real, 
     10000}, {np, _Integer}, {ns, _Integer}, {temp, _Integer}},

correct it to:

Compile[{{xp, _Real, 2}, {vp, _Real, 
     2}, {np, _Integer}, {ns, _Integer}, {temp, _Integer}},

Cheers, Robert

No, it has to be 2.

Yeah, I thought that the dimension was the number of columns.

However can I make a Compile that returns a Table of ListPlots?
I don’t know how to make a huge number of ListPlots with a big number of points without it becoming too slow or crashing…

You can’t handle or produce ListPlot in Compile.
Only basic type Reals, Integers, and 1D, 2D, 3D, 4D, nD Lists of this basic types are supported.
For a video you could n frames, each frame a 2D List aka Matrix.
Outside Compile you then can generate a animation, video, etc. from frames generated by Compile.

Diogo,

As a general comment, your posted code examples rely too heavily on looping (Do[]). This is generally bad practice in Mathematica. There is (almost) always a better, faster way to program using functions such as Map, Nest, etc. If you follow this approach, you will also have little need for compiling functions because the uncompiled version will be very efficient.

Regards,

Neil