Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive

MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Vector Runge-Kutta ODE solver with compilation?

  • To: mathgroup at
  • Subject: [mg116786] Re: Vector Runge-Kutta ODE solver with compilation?
  • From: DmitryG <einschlag at>
  • Date: Mon, 28 Feb 2011 04:59:43 -0500 (EST)
  • References: <ikd6c8$pvr$>

On 27 Feb., 04:43, Oliver Ruebenkoenig <ruebe... at> wrote:

> You could use CompilePrint to see that for NN=11 this generates a some 900
> line so code - quite a lot of which are Part[]. Make sure you understand
> what you are doing here - you in-line this sum into the code.
> Weird code? - No, not really.
> ...

Hi Oliver,

In this short program I mimick my real-life program that solves a
system of NN ODE's with the maximal coupling: the RHS of each of NN
equations contains all NN variables. Any molecular dynamics (MD)
simulation is of this kind: Acceleration of any particle depends on
positions of all particles.

This application of Mathematica's compilation is crucial but there are
two problems that I have encountered:

1) Compilation in C is prohibitively slow above NN=50 or so. In this
case Mathematica's own compiler is still doing a good job.

2) For NN=1000 or above Mathematica's compiler consumes all RAM (my
laptop has 3 GB RAM) and quits. This is very disappointing because
generating the list of RHS does not take much memory and the RK 4
routine does not consume much memory as well. Thus it would be only a
matter of computer time to solve a problem for any NN. However,
compiler generates a blown-up code (component by component, with the
vector structure abandoned) that does not fit into memory.

Thank you for suggestion to look into the compiled code with
CompilePrint. Yes, the code is very long, and its structure is the
same for the initial problem we considered, the problem with


Also in this case the compiled code contains Part[].

Is there any way to optimize this calculation or we have an intrinsic
limitation of Mathematica here? Below is the code again:

(* Definition *)
Do[t=t0+k h;
K1=h f[t,x];
K2=h f[t+(1/2) h,x+(1/2) K1];
K3=h f[t+(1/2) h,x+(1/2) K2];
K4=h f[t+h,x+K3];
x=x+(1/6) K1+(1/3) K2+(1/3) K3+(1/6) K4;


(* Calculation *)
1,NN}]];] (* Quiet to suppress complaints *)
F=Function[{t,x},Evaluate[RHS]];  (* Evaluate to use actual form of
RHS *)


x0=Table[RandomReal[{0,1}],{i,1,NN}];   t0=0;    tMax=50;   n=500;

Print["Compilation: ",Developer`PackedArrayQ@Sol]

(* Plotting time dependences *)



Thank you for your help,


  • Prev by Date: Re: How to local files on ParallelKernels
  • Next by Date: Re: Numerical Convolution
  • Previous by thread: Re: Vector Runge-Kutta ODE solver with compilation?
  • Next by thread: Re: Vector Runge-Kutta ODE solver with compilation?