CloseKernels[];
LaunchKernels[6];
rho1 = 0.;
rho2 = 0.;
sigma1 = 0.;
sigma2 = 0.;
kappa1 = 0.;
kappa2 = 0.;
tc = 0.;
r = 0.1;
theta1 = 0.1;
theta2 = 0.;
terminal = 1.;
deltatime = 0.004;
v10 = 0.1;
v20 = 0.2;
dimension = 4;
n = 1.;
dn = (2 n + 1)^dimension;(*和维数有关*)
dj = 1.;
dq = 1.;
collocation = Tuples[Table[(i)/(2 n), {i, 0, 2 n}], 4];
random1 = RandomReal[{-1, 1}, {dn, dj}];
random2 = RandomReal[{-1, 1}, {dn, dj}];
random3 = RandomReal[{-1, 1}, {dn, dj}];
random4 = RandomReal[{-1, 1}, {dn, dj}];
random5 = RandomReal[{-1, 1}, {dn, dj}];
f[i_, j_, x_] :=
Piecewise[{{(1 + Sin[2 \[Pi] (x - collocation[[i]][[j]]) 2 n])/
2, -5/4 <= (x - collocation[[i]][[j]]) 2 n <= -3/4}, {1, -3/
4 <= (x - collocation[[i]][[j]]) 2 n <=
3/4}, {(1 - Sin[2 \[Pi] (x - collocation[[i]][[j]]) 2 n])/2,
3/4 <= (x - collocation[[i]][[j]]) 2 n <= 5/4}}];
psi[i_, t_, x_, v1_, v2_] :=
f[i, 1, t]*f[i, 2, x]*f[i, 3, v1]*f[i, 4, v2];
li[i_, t_, x_, v1_,
v2_] := {(t - collocation[[i]][[1]]) 2 n, (x -
collocation[[i]][[2]]) 2 n, (v1 -
collocation[[i]][[3]]) 2 n, (v2 - collocation[[i]][[4]]) 2 n};
phi[i_, j_, t_, x_, v1_, v2_] :=
Tanh[{random1[[i, j]], random2[[i, j]], random3[[i, j]],
random4[[i, j]]} . li[i, t, x, v1, v2] + random5[[i, j]]];
df[t_, x_, v1_, v2_] :=
Table[phi[i, j, t, x, v1, v2]*psi[i, t, x, v1, v2], {i, 1, dn}, {j,
1, dj}] // Flatten;
dt[t_, x_, v1_, v2_] = D[df[t, x, v1, v2], t];
dx[t_, x_, v1_, v2_] = D[df[t, x, v1, v2], x];
dv1[t_, x_, v1_, v2_] = D[df[t, x, v1, v2], v1];
dv2[t_, x_, v1_, v2_] = D[df[t, x, v1, v2], v2];
dxx[t_, x_, v1_, v2_] = D[D[df[t, x, v1, v2], x], x];
dxv1[t_, x_, v1_, v2_] = D[D[df[t, x, v1, v2], x], v1];
dxv2[t_, x_, v1_, v2_] = D[D[df[t, x, v1, v2], x], v2];
dv1v1[t_, x_, v1_, v2_] = D[D[df[t, x, v1, v2], v1], v1];
dv2v2[t_, x_, v1_, v2_] = D[D[df[t, x, v1, v2], v2], v2];
ff[t_, x_, v1_, v2_] =
dt[t, x, v1, v2] + 0.5*(v1 + v2)*x^2*dxx[t, x, v1, v2] +
rho1 sigma1 x v1 dxv1[t, x, v1, v2] +
rho2 sigma2 x v2 dxv2[t, x, v1, v2] +
0.5*sigma1^2 v1 dv1v1[t, x, v1, v2] +
0.5*sigma2^2 v2 dv2v2[t, x, v1, v2] - r df[t, x, v1, v2] +
r x dx[t, x, v1, v2] + kappa1 (theta1 - v1) dv1[t, x, v1, v2] +
kappa2 (theta2 - v2) dv2[t, x, v1, v2];
(*xVars=ToExpression[Table["x"<>ToString[i],{i,1,dn dj}]];*)
xdim = IntegerPart[dn dj];
funComp1 =
With[{code = N[ff[t, x, v1, v2]]},
Compile[{{t, _Real}, {x, _Real}, {v1, _Real}, {v2, _Real}}, code,
CompilationTarget -> "C"]]; // AbsoluteTiming
funComp2 =
With[{code = N[df[t, x, v1, v2]]},
Compile[{{t, _Real}, {x, _Real}, {v1, _Real}, {v2, _Real}}, code,
CompilationTarget -> "C"]]; // AbsoluteTiming
funComp3 =
With[{code = N[dx[t, x, v1, v2]]},
Compile[{{t, _Real}, {x, _Real}, {v1, _Real}, {v2, _Real}}, code,
CompilationTarget -> "C"]]; // AbsoluteTiming
This is the code I provided, where $dt, dx, dxv1,$ and so on are partial derivatives of function df. Now, I need to assign a value to function ff, but the current compilation speed is very slow, especially in the second-order partial derivatives part. The figure below illustrates this. How can I improve the compilation speed?
edit: This issue has been resolved. Thank you very much.@xzczd

collocation,random1, etc. arguments of those functions, too. 2. YourfunComp1, etc. involveMainEvaluate. 3. Have you read this?: https://mathematica.stackexchange.com/a/104031/1871 4. TheLaunchKernelsat the beginning doesn't make sense in your code.