1
$\begingroup$

I'm writing a Runge-Kutta algorithm for solving a system of coupled differential equations. My code works fine when defined as a module, but when I try to compile it, I get the following error:

CompiledFunction::cfte: Compiled expression {{0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.},{-0.00157061-0.49998 I,-0.00141364+5.92154*10^-6 I,1.25575*10^-8+2.66469*10^-6 I,3.76726*10^-9+0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I,0. +0. I},{0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.},<<45>>,{0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.},{0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.},<<951>>} should be a rank 2 tensor of machine-size real numbers.
CompiledFunction::cfexe: Could not complete external evaluation; proceeding with uncompiled evaluation.

I would appreciate help identifying where I'm going wrong in trying to compile the code. I specify that the output should be complex, but I still get the error and can't get it to compile. Here is my code:

(*Define Parameters*)
Nsim = 20;
\[Epsilon] = N[50.0];
g = N[0.4];
\[Omega]c = N[2 \[Pi] * 7.6];
\[Omega]d = N[2 \[Pi]*7.5];
\[Kappa] = N[2 \[Pi]*1.6/1000];
J = N[2 \[Pi]*0.09];
\[CapitalOmega] = N[2 \[Pi]*13];
\[CapitalGamma] = N[2 \[Pi]*10/1000];
AMat = SparseArray[{{1, 1} -> \[Omega]c - \[Omega]d - I \[Kappa]/2, {Nsim, Nsim} -> \[Omega]c - \[Omega]d - I \[Kappa]/2, Band[{1, 1}] -> \[Omega]c - \[Omega]d - I \[Kappa]/2, Band[{1, 2}] -> J, Band[{2, 1}] -> J}, {Nsim, Nsim}];
BMat = SparseArray[{Band[{1, 1}] -> \[CapitalOmega] - \[Omega]d - I \[CapitalGamma]/2}, {Nsim, Nsim}];
DriveArray = -I \[Epsilon]*UnitVector[Nsim, 1];
U[\[Beta]_] = -0.35 Abs[\[Beta]]^2 \[Beta];
F\[Alpha][x_] := -I AMat.x 
F\[Beta][x_] := -I BMat.x - I U[x]

(*Define Compiler*)

RK4[fn\[Alpha]_, fn\[Beta]_] := With[{f\[Alpha] = fn\[Alpha], f\[Beta] = fn\[Beta]},

            Compile[{{\[Alpha]0, _Complex, 1}, {\[Beta]0, _Complex, 1}, {tf, _Real}, {n, _Integer}},

            Module[{dt, \[Alpha], \[Beta], Nsim, k1\[Alpha], k2\[Alpha], k3\[Alpha], k4\[Alpha], k1\[Beta], k2\[Beta], k3\[Beta], k4\[Beta]},

                Nsim = Length@\[Alpha]0;
                dt = N[tf/(n - 1)];
                \[Alpha] = ConstantArray[0.0, {n, Nsim}];
                \[Beta] = ConstantArray[0.0, {n, Nsim}];
                \[Alpha][[1]] = \[Alpha]0;
                \[Beta][[1]] = \[Beta]0;

                Do[{
  k1\[Alpha] = f\[Alpha]@\[Alpha][[nc]] - I g \[Beta][[nc]] + DriveArray,
  k1\[Beta] = f\[Beta]@\[Beta][[nc]] - I g \[Alpha][[nc]],

  k2\[Alpha] = f\[Alpha]@(\[Alpha][[nc]] + 0.5*dt*k1\[Alpha]) - I g (\[Beta][[nc]] + 0.5*dt*k1\[Beta]) + DriveArray,
  k2\[Beta] = f\[Beta]@(\[Beta][[nc]] + 0.5*dt*k1\[Beta]) - I g (\[Alpha][[nc]] + 0.5*dt*k1\[Alpha]),

  k3\[Alpha] = f\[Alpha]@(\[Alpha][[nc]] + 0.5*dt*k2\[Alpha]) - I g (\[Beta][[nc]] + 0.5*dt*k2\[Beta]) + DriveArray,
  k3\[Beta] = f\[Beta]@(\[Beta][[nc]] + 0.5*dt*k2\[Beta]) - I g (\[Alpha][[nc]] + 0.5*dt*k2\[Alpha]),

  k4\[Alpha] = f\[Alpha]@(\[Alpha][[nc]] + dt*k3\[Alpha]) - I g (\[Beta][[nc]] + dt*k3\[Beta]) + DriveArray,
  k4\[Beta] = f\[Beta]@(\[Beta][[nc]] + dt*k3\[Beta]) - I g (\[Alpha][[nc]] + dt*k3\[Alpha]),

  \[Alpha][[nc + 1]] = \[Alpha][[nc]] + dt*1/6*(k1\[Alpha] + 2 k2\[Alpha] + 2 k3\[Alpha] + k4\[Alpha]),
  \[Beta][[nc + 1]] = \[Beta][[nc]] + dt*1/6*(k1\[Beta] + 2 k2\[Beta] + 2 k3\[Beta] + k4\[Beta])
                   }, {nc, 1, n - 1}];

{\[Alpha], \[Beta]}],
{{\[Alpha], _Complex, 2}, {\[Beta], _Complex, 2}, {k1\[Alpha], _Complex, 1}, {k1\[Beta], _Complex, 1}, {k2\[Alpha], _Complex, 1}, {k2\[Beta], _Complex, 1}, {k3\[Alpha], _Complex, 1}, {k3\[Beta], _Complex, 1}, {k4\[Alpha], _Complex, 1}, {k4\[Beta], _Complex, 1}}, 
CompilationTarget -> "C"]]

(*Initialize System*)
runRK4 = RK4[F\[Alpha], F\[Beta]];
\[Alpha]0 = ConstantArray[0.0 , Nsim];
\[Beta]0 = ConstantArray[0.0, Nsim];
Tsim = 10.0; 
\[CapitalDelta]t = 0.01;
n = Tsim/\[CapitalDelta]t + 1 // Round;

(*Run*)
{\[Alpha]temp, \[Beta]temp} = runRK4[\[Alpha]0, \[Beta]0, Tsim, n];
$\endgroup$

1 Answer 1

3
$\begingroup$

You initialized \[Alpha] and \[Beta] as real arrays. This is how you can make them complex:

\[Alpha] = Table[0. I, {n}, {Nsim}];
\[Beta] = Table[0. I, {n}, {Nsim}];

Also notice that I used Table instead of ConstantArray because the latter ist not compilable. However, several other parts of your code are not compilable, e.g. the SparseArrays.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.