Re: Do I need MathLink to run finite-difference fast enough

• To: mathgroup at smc.vnet.net
• Subject: [mg115783] Re: Do I need MathLink to run finite-difference fast enough
• From: Daniel Lichtblau <danl at wolfram.com>
• Date: Thu, 20 Jan 2011 06:32:58 -0500 (EST)

```James wrote:
> Hello guys,
>
> I would like to create a Manipulate for a finite-difference on a system
> of PDEs using a 100x100 array that is processed about 10,000 times.  Is
> it possible to code this in native Mathematica, for example using
> Compile, fast enough to be reasonably used by Manipulate (under 3 or 4
> seconds)?  The C++ code is below and with the Compiler set to optimize
> speed, it runs in approximately 1.5 seconds.  However, if I attempt to
> just code it in Mathematica using arrays, it takes much too long to
> execute.
>
> Can someone help me decide if the only reasonable option is to use
>
> Thanks Guys.  Here's the C++ code.  The variables u, v, and lap
> are all 100x100 arrays.
>
> startt=clock();
>
>   for(p=1;p<=10000;p++){
>
>
>      lap[0][0]=(u[0][1]-4*u[0][0]+u[0][M-1]+u[1][0]+u[N-1][0]);
>
>     for(j=1;j<=M-2;j++)
>     {
>         lap[0][j]=(u[0][j+1]-4*u[0][j]+u[0][j-1]+u[1][j]+u[N-1][j]);
>     }
>
>     lap[0][M-1]=(u[0][0]-4*u[0][M-1]+u[0][M-2]+u[1][M-1]+u[N-1][M-1]);
>
>     for(i=1;i<=N-2;i++)
>     {
>         lap[i][0]=(u[i][1]-4*u[i][0]+u[i][M-1]+u[i+1][0]+u[i-1][0]);
>     }
>
>     for(i=1;i<=N-2;i++)
>     {
>
> lap[i][M-1]=(u[i][0]-4*u[i][M-1]+u[i][M-2]+u[i+1][M-1]+u[i-1][M-1]);
>     }
>
>     lap[N-1][0]=(u[N-1][1]-4*u[N-1][0]+u[N-1][M-1]+u[0][0]+u[N-2][0]);
>
>
> lap[N-1][M-1]=(u[N-1][0]-4*u[N-1][M-1]+u[N-1][M-2]+u[0][M-1]+u[N-2][M-1]);
>
>     for(j=1;j<=M-2;j++)
>     {
>
> lap[N-1][j]=(u[N-1][j+1]-4*u[N-1][j]+u[N-1][j-1]+u[0][j]+u[N-2][j]);
>     }
>
>     for(i=1;i<=N-2;i++)
>         for(j=1;j<=M-2;j++)
>     {
>         lap[i][j]=(u[i][j+1]-4*u[i][j]+u[i][j-1]+u[i+1][j]+u[i-1][j]);
>     }
>
>     for(i=0;i<M;i++)
>       for(j=0;j<N;j++){
>
>         u[i][j] =
> =u[i][j]+dt*(D_u*lap[i][j]+(a-(b+1)*u[i][j]+v[i][j]*u[i][j]*u[i][j]));
>
>       }
>
>
>       lap[0][0]=(v[0][1]-4*v[0][0]+v[0][M-1]+v[1][0]+v[N-1][0]);
>
>     for(j=1;j<=M-2;j++)
>     {
>         lap[0][j]=(v[0][j+1]-4*v[0][j]+v[0][j-1]+v[1][j]+v[N-1][j]);
>     }
>
>     lap[0][M-1]=(v[0][0]-4*v[0][M-1]+v[0][M-2]+v[1][M-1]+v[N-1][M-1]);
>
>     for(i=1;i<=N-2;i++)
>     {
>         lap[i][0]=(v[i][1]-4*v[i][0]+v[i][M-1]+v[i+1][0]+v[i-1][0]);
>     }
>
>     for(i=1;i<=N-2;i++)
>     {
>
> lap[i][M-1]=(v[i][0]-4*v[i][M-1]+v[i][M-2]+v[i+1][M-1]+v[i-1][M-1]);
>     }
>
>     lap[N-1][0]=(v[N-1][1]-4*v[N-1][0]+v[N-1][M-1]+v[0][0]+v[N-2][0]);
>
>
> lap[N-1][M-1]=(v[N-1][0]-4*v[N-1][M-1]+v[N-1][M-2]+v[0][M-1]+v[N-2][M-1]);
>
>     for(j=1;j<=M-2;j++)
>     {
>
> lap[N-1][j]=(v[N-1][j+1]-4*v[N-1][j]+v[N-1][j-1]+v[0][j]+v[N-2][j]);
>     }
>
>     for(i=2;i<=N-2;i++)
>         for(j=2;j<=M-2;j++)
>     {
>         lap[i][j]=(v[i][j+1]-4*v[i][j]+v[i][j-1]+v[i+1][j]+v[i-1][j]);
>     }
>
>       for(i=0;i<M;i++)
>       for(j=0;j<N;j++){
>          v[i][j] =
> =v[i][j]+dt*(D_v*lap[i][j]+(b*u[i][j]-v[i][j]*u[i][j]*u[i][j]));
>     }
>
>   endt=clock();
>
>   fprintf(stderr,"%d\n",endt-startt);

Not certain, but these operations look like the sort of thing that might
be accomplished with ListConvolve. If so, that would probably be
comparable to a C/C++ coding.

Daniel Lichtblau
Wolfram Research

```

• Prev by Date: Re: Using FindRoot on an equation involving Log terms
• Next by Date: Re: avoiding non-machine numbers
• Previous by thread: Re: The new (v.8.0) distribution plots using a numerical
• Next by thread: problem with LessEqual::nord: