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


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

using built in compiler

Started byrajesh7796gm@gmail.com
First post2012-09-22 07:07 +0000
Last post2012-10-08 06:51 +0000
Articles 2 — 2 participants

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


Contents

  using built in compiler rajesh7796gm@gmail.com - 2012-09-22 07:07 +0000
    Re: using built in compiler Chris <kristoph.steikert@gmail.com> - 2012-10-08 06:51 +0000

#11848 — using built in compiler

Fromrajesh7796gm@gmail.com
Date2012-09-22 07:07 +0000
Subjectusing built in compiler
Message-ID<k3jo30$2cj$1@smc.vnet.net>
dear esteemed mathematica experts,
hello there & greetings from malaysia.
 i have been trying to use the compiler on a module to see whether it speeds up the execution or not. kindly see below.
unfortunately, mathematica returns  a couple of error messages. 
can anybody help ? will appreciate any assistance from this forum. 

cheers
rajesh

fC := Compile[{{l, _Real}, {nsteps, _Integer}, {En, _Real} },
  Module[{meV, \[Angstrom], \[Sigma], \[Mu], \[Lambda], k, s, 
    scat, \[Delta]},
   meV = 1./(27.3*1000.); 
   en = En*meV; \[Angstrom] = 1./0.52; \[Sigma] = 
    3.57 \[Angstrom]; \[Epsilon] = 5.9 meV;
   V[r_] = \[Epsilon] ((\[Sigma]/r)^12 - 2 (\[Sigma]/r)^6);
   
   \[Mu] = (mH mKr)/(mH + mKr) /. {mH -> 1836.0, mKr -> 1836.0*136.0};
   (*Print[\[Mu]];*)
   \[Mu] // N;
   \[Lambda] = 2 \[Pi]/Sqrt[2.0 \[Mu] en] // N; k = 2 \[Pi]/\[Lambda];
   (*Print["\[Lambda]= ",\[Lambda]]*);
   s = 5. \[Lambda]/nsteps; 
   x = {0.5 \[Sigma], 0.5 \[Sigma] + s}; \[Psi] = {0.0, 0.0001};
   F[r_] = 2. \[Mu]1 (V[r] + 1/(2. \[Mu]1 r^2) l (l + 1) - en);
   Do[\[Psi]m = \[Psi][[n - 1]]; \[Psi]n = \[Psi][[n]]; 
    xm = x[[n - 1]]; xn = x[[n]];
    xp = xn + s; x = Append[x, xp]; Fn = F[xn]; Fp = F[xp]; Fm = F[xm];
    \[Psi]p = (2. \[Psi]n - \[Psi]m + 
        s^2/12. (10. Fn \[Psi]n + Fm \[Psi]m))/(1. - s^2/12. Fp);
    \[Psi] = Append[\[Psi], \[Psi]p], {n, 2, nsteps}];
   \[Psi] = \[Psi]/Max[\[Psi]]; scat = Transpose[{x, \[Psi]}];
   nmax = Length[scat]; {r1, u1} = scat[[nmax]]; {r2, u2} = 
    scat[[nmax - 1]];
   K = (r1 u2)/(r2 u1); j1 = j[l, k r1]; j2 = j[l, k r2]; 
   n1 = n[l, k r1]; n2 = n[l, k r2];
   \[Delta] = ArcTan[(K j1 - j2)/(K n1 - n2)]]]

fC[2.0, 1000, 2.0] // AbsoluteTiming
CompiledFunction::cfse: Compiled expression 1822.5985401459855` should be a machine-size integer. >>

CompiledFunction::cfex: Could not complete external evaluation at instruction 7; proceeding with uncompiled evaluation. >>

[toc] | [next] | [standalone]


#11997

FromChris <kristoph.steikert@gmail.com>
Date2012-10-08 06:51 +0000
Message-ID<k4tt62$sen$1@smc.vnet.net>
In reply to#11848
Am Samstag, 22. September 2012 09:08:33 UTC+2 schrieb (unbekannt):
> dear esteemed mathematica experts,
>
> hello there & greetings from malaysia.
>
>  i have been trying to use the compiler on a module to see whether it speeds up the execution or not. kindly see below.
>
> unfortunately, mathematica returns  a couple of error messages.
>
> can anybody help ? will appreciate any assistance from this forum.
>
>
>
> cheers
>
> rajesh
>
>
>
> fC := Compile[{{l, _Real}, {nsteps, _Integer}, {En, _Real} },
>
>   Module[{meV, \[Angstrom], \[Sigma], \[Mu], \[Lambda], k, s,
>
>     scat, \[Delta]},
>
>    meV = 1./(27.3*1000.);
>
>    en = En*meV; \[Angstrom] = 1./0.52; \[Sigma] =
>
>     3.57 \[Angstrom]; \[Epsilon] = 5.9 meV;
>
>    V[r_] = \[Epsilon] ((\[Sigma]/r)^12 - 2 (\[Sigma]/r)^6);
>
>   
>
>    \[Mu] = (mH mKr)/(mH + mKr) /. {mH -> 1836.0, mKr -> 1836.0*136.0};
>
>    (*Print[\[Mu]];*)
>
>    \[Mu] // N;
>
>    \[Lambda] = 2 \[Pi]/Sqrt[2.0 \[Mu] en] // N; k = 2 \[Pi]/\[Lambda];
>
>    (*Print["\[Lambda]= ",\[Lambda]]*);
>
>    s = 5. \[Lambda]/nsteps;
>
>    x = {0.5 \[Sigma], 0.5 \[Sigma] + s}; \[Psi] = {0.0, 0.0001};
>
>    F[r_] = 2. \[Mu]1 (V[r] + 1/(2. \[Mu]1 r^2) l (l + 1) - en);
>
>    Do[\[Psi]m = \[Psi][[n - 1]]; \[Psi]n = \[Psi][[n]];
>
>     xm = x[[n - 1]]; xn = x[[n]];
>
>     xp = xn + s; x = Append[x, xp]; Fn = F[xn]; Fp = F[xp]; Fm = F[xm];
>
>     \[Psi]p = (2. \[Psi]n - \[Psi]m +
>
>         s^2/12. (10. Fn \[Psi]n + Fm \[Psi]m))/(1. - s^2/12. Fp);
>
>     \[Psi] = Append[\[Psi], \[Psi]p], {n, 2, nsteps}];
>
>    \[Psi] = \[Psi]/Max[\[Psi]]; scat = Transpose[{x, \[Psi]}];
>
>    nmax = Length[scat]; {r1, u1} = scat[[nmax]]; {r2, u2} =
>
>     scat[[nmax - 1]];
>
>    K = (r1 u2)/(r2 u1); j1 = j[l, k r1]; j2 = j[l, k r2];
>
>    n1 = n[l, k r1]; n2 = n[l, k r2];
>
>    \[Delta] = ArcTan[(K j1 - j2)/(K n1 - n2)]]]
>
>
>
> fC[2.0, 1000, 2.0] // AbsoluteTiming
>
> CompiledFunction::cfse: Compiled expression 1822.5985401459855` should be a machine-size integer. >>
>
>
>
> CompiledFunction::cfex: Could not complete external evaluation at instruction 7; proceeding with uncompiled evaluation. >>

Hi

Here is some code that works. There are still some lines that are unnecessary but I think it will do. Note, that the code is not complete because it is not clear what you mean after your function K. It seems that you are new 
to the topic, thus I advise to debug your code one line at a tim. Cheers

fC:=Compile[{{l,_Real},{nsteps,_Integer},{En,_Real}},Block[{meV=1./(27. 3*1000.),en,\[Angstrom]=1./0.52,\[Sigma],\[Epsilon],mH=1836.,mKr=249696.,\[Mu],\[Lambda],k,s,x,\[Psi]={0.0,0.0001},scat,vec1,vec2,K},
en=En*meV;
\[Sigma]=3.57 \[Angstrom];
\[Epsilon]=5.9 meV;
\[Mu]=(mH mKr)/(mH+mKr);
\[Lambda]=2 \[Pi]/Sqrt[2.* \[Mu]* en];
k=2 \[Pi]/\[Lambda];
s=5.*\[Lambda]/nsteps;
x={0.5 \[Sigma],0.5 \[Sigma]+s};

Block[{n=2,xn,xm,xp,\[Psi]n,\[Psi]m,\[Psi]p,Fn,Fp,Fm},
Do[
xn=x[[n]];
xp=xn+s;
AppendTo[x,xp];

\[Psi]n=\[Psi][[n]];
\[Psi]m=\[Psi][[n-1]];
xm=x[[n-1]];
Fn=2.*\[Mu]*(\[Epsilon] *((\[Sigma]/xn)^12-2.* (\[Sigma]/xn)^6)+1./(2. \[Mu]* xn^2) l (l+1.)-en);
Fp=2.*\[Mu]*(\[Epsilon] *((\[Sigma]/xp)^12-2.* (\[Sigma]/xp)^6)+1./(2. \[Mu]* xp^2) l (l+1.)-en);
Fm=2.*\[Mu]*(\[Epsilon] *((\[Sigma]/xm)^12-2.* (\[Sigma]/xm)^6)+1./(2. \[Mu]* xm^2) l (l+1.)-en);
\[Psi]p=(2.*\[Psi]n-\[Psi]m+s^2/12. *(10. Fn* \[Psi]n+Fm* \[Psi]m))/(1.-s^2/12.*Fp);
AppendTo[\[Psi],\[Psi]p];

n++
,{nsteps-2}]
];
\[Psi]=\[Psi]/Max[\[Psi]];
scat=Transpose[{x,\[Psi]}];
vec1=scat[[-1]];
vec2=scat[[-2]];
K=(vec1[[1]]* vec2[[2]])/(vec2[[1]]* vec1[[2]])
]
]

[toc] | [prev] | [standalone]


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


csiph-web