Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10553 > unrolled thread

NDSolve profiling

Started byBen <Benjamin.R.Lewis@gmail.com>
First post2012-06-20 07:53 +0000
Last post2012-06-24 08:23 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  NDSolve profiling Ben <Benjamin.R.Lewis@gmail.com> - 2012-06-20 07:53 +0000
    Re: NDSolve profiling Narasimham <mathma18@hotmail.com> - 2012-06-21 09:24 +0000
      Re: NDSolve profiling Ben <benjamin.r.lewis@gmail.com> - 2012-06-24 08:23 +0000

#10553 — NDSolve profiling

FromBen <Benjamin.R.Lewis@gmail.com>
Date2012-06-20 07:53 +0000
SubjectNDSolve profiling
Message-ID<jrrvgu$4sd$1@smc.vnet.net>
Are there any tricks to profiling/optimising NDSolve?

A computationally expensive subexpression (involving ProductLog) is repeated many times in my ODE, so I'd like to check whether it is only computed once per evaluation point (and would also like to try other possible optimisations like using compile unless that is already redundant).

My ODE has multiple coupled-components so I've also experimented with converting it into vector form. With simpler ODEs I've found this has facilitated using compiled functions in NDSolve (and gave an overall speed boost), but with my current ODE I find that the vector form causes failure. I'm wondering whether switching from a coupled set of equations to vector form of the identical ODE/IVP may cause the algorithm pick a different step sequences, or may be more likely to produce catastrophic-cancellation inaccuracies?

Can anybody share relevant insight into the evaluation that goes on behind the scenes of ndsolve's equation-processing and iteration, and how to debug or ways to optimise it for speed?

Thanks,
Ben

[toc] | [next] | [standalone]


#10578

FromNarasimham <mathma18@hotmail.com>
Date2012-06-21 09:24 +0000
Message-ID<jrup92$fnq$1@smc.vnet.net>
In reply to#10553
On Jun 20, 12:53 pm, Ben <Benjamin.R.Le...@gmail.com> wrote:
> Are there any tricks to profiling/optimising NDSolve?
>
> A computationally expensive subexpression (involving ProductLog) is repea=
ted many times in my ODE, so I'd like to check whether it is only computed =
once per evaluation point (and would also like to try other possible optimi=
sations like using compile unless that is already redundant).
>
> My ODE has multiple coupled-components so I've also experimented with con=
verting it into vector form. With simpler ODEs I've found this has facilita=
ted using compiled functions in NDSolve (and gave an overall speed boost), =
but with my current ODE I find that the vector form causes failure. I'm won=
dering whether switching from a coupled set of equations to vector form of =
the identical ODE/IVP may cause the algorithm pick a different step sequenc=
es, or may be more likely to produce catastrophic-cancellation inaccuracies=
?
>
> Can anybody share relevant insight into the evaluation that goes on behin=
d the scenes of ndsolve's equation-processing and iteration, and how to deb=
ug or ways to optimise it for speed?
>
> Thanks,
> Ben

If the relations are explicitly and logically defined there may be no
problems.Sometimes what appears as a narrow deviation may be
surprising; but in any case at least a representative code should be
posted here to help avoid vagueness,to pinpoint the problem.

Narasimham

[toc] | [prev] | [next] | [standalone]


#10621

FromBen <benjamin.r.lewis@gmail.com>
Date2012-06-24 08:23 +0000
Message-ID<js6iqn$857$1@smc.vnet.net>
In reply to#10578
Narasimham, let me give you one specific example, hopefully someone
can help me to pinpoint my problem.

Here is a two-dimensional ODE defined by some complicated functions f1
and f2 (reproduced below). The outcome of NDSolve can be utterly
different depending on whether this problem is expressed in component
form or vector form:

laws={u''[t]==f1[u[t],v[t],u'[t],v'[t]],
v''[t]==f2[u[t],v[t],u'[t],v'[t]]};

Timing[NDSolve[Join[{u[0]==v[0]==0},
{u'[0]==10000,v'[0]==10000.0001},laws],{u,v},{t,-10,10},StoppingTest-
>v[t]^2<0.9` +u[t]^2]]

>> {8.003,{{u->InterpolatingFunction[{{-0.804964,0.804964}...

f3[x__List]:=({f1@@#1,f2@@#1}&)[Join[x]]; st[{u_,v_}]:=v^2<0.9` +u^2;

Timing[NDSolve[{y[0]=={0,0},y'[0]=={10000,
10000.0001},y''[t]==f3[y[t],y'[t]]},{y},{t,-10,10},StoppingTest-
>st[y[t]]]]

>> NDSolve::mxst: Maximum number of 10000 steps reached at the point t == 0.328...
>> {291.753,{{y->InterpolatingFunction[{{-0.827141,0.328212}...


Notice that the same functions are used in both forms. One form fails
early (at +0.3 instead of +0.8), and obviously uses far too many steps
even to get that far. It doesn't always have to be this way, for
example a different choice of initial conditions (like, y'=(0,1))
appears to produce the expected symmetry of behaviour between both
approaches. Any idea what's going on??

By the way, another difference is that in the component form, I
suspect f1 & f2 are only explicitly evaluated in the equation
processing stage, unlike the vector form. Since I wanted to exercise
finer control over how the guts of f1&f2 are evaluated during the
iterate stage, it is troublesome for me that NDSolve becomes
unreliable in that particular form.

Note in f1 & f2 below, that ProductLog appears repeatedly with the
same argument. In my real equations this occurs more than forty times,
so I want to make sure that it isn't being recalculated each time
(since NDSolve of this kind of equation is the main bottleneck in what
I'm doing).

-Ben

f2 = Function[{u, v, pu,
   pv}, -pu ((
      pv (-E^(1 + ProductLog[(u^2 - v^2)/E]) - u^2 + v^2) (2 u + (
         2 u)/(1 + ProductLog[(u^2 - v^2)/E])))/(
      2 (E^(1 + ProductLog[(u^2 - v^2)/E]) + u^2 - v^2)^2) + (
      pu (-E^(1 + ProductLog[(u^2 - v^2)/E]) - u^2 + v^2) (-2 v - (
         2 v)/(1 + ProductLog[(u^2 - v^2)/E])))/(
      2 (E^(1 + ProductLog[(u^2 - v^2)/E]) + u^2 - v^2)^2)) -
   pv ((pu (-E^(1 + ProductLog[(u^2 - v^2)/E]) - u^2 + v^2) (2 u + (
         2 u)/(1 + ProductLog[(u^2 - v^2)/E])))/(
      2 (E^(1 + ProductLog[(u^2 - v^2)/E]) + u^2 - v^2)^2) + (
      pv (-E^(1 + ProductLog[(u^2 - v^2)/E]) - u^2 + v^2) (-2 v - (
         2 v)/(1 + ProductLog[(u^2 - v^2)/E])))/(
      2 (E^(1 + ProductLog[(u^2 - v^2)/E]) + u^2 - v^2)^2))]; f1 =
 Function[{u, v, pu,
   pv}, -pv (-((pv (2 u + (2 u)/(1 + ProductLog[(u^2 - v^2)/E])))/(
       2 (E^(1 + ProductLog[(u^2 - v^2)/E]) + u^2 - v^2))) - (
      pu (-2 v - (2 v)/(1 + ProductLog[(u^2 - v^2)/E])))/(
      2 (E^(1 + ProductLog[(u^2 - v^2)/E]) + u^2 - v^2))) -
   pu (-((pu (2 u + (2 u)/(1 + ProductLog[(u^2 - v^2)/E])))/(
       2 (E^(1 + ProductLog[(u^2 - v^2)/E]) + u^2 - v^2))) - (
      pv (-2 v - (2 v)/(1 + ProductLog[(u^2 - v^2)/E])))/(
      2 (E^(1 + ProductLog[(u^2 - v^2)/E]) + u^2 - v^2)))];

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web