Skip to main content

differential equations - How to speed up the integral in NDSolve?


On the MMA.SE, there have been several question on how to solve equations including integral using NDSolve, see example 1, example 2, and example 3. Many people, like me, have a hard time with this kind of problem. I had raised the last one 5 months ago and obtained a promising answer, since then I have been tried to solve the following equation for a spatially periodic function u(x,t) on [−L,L] with 2L periodicity:


∂tu+u∂xu+∂2xu+∂4xu+aint[∂3xu]+bu3int[(∂2xu)int[∂xu]]=0,


where a and b are constants, and int[f] is a spatial integral for a periodic function f(x,t)


int[f](x,t)=12LPV∫L−Lf(x′,t)cot[π(x−x′)2L]dx′,


which should be understood in the sense of principal value (PV) since there is a singularity at x=x′.


This equation is subjected to periodic boundary conditions and an initial condition. In my real problem, it has several int terms and its nest, like int[(∂2xu)int[⋯]], which will be solved over a large domain and a long time, say, L=30 and tmax=200 (much larger than those in my original post). I tried Michael's answer but found that even for that simplified version of Eq.(1) (without the last nested term) the code is extremely slow, though it works. Actually, I also posted my answer based on the finite difference method, but the accuracy of my code is not as good as Michael's. (That is why I didn't accept any answer.) Also, the problem has stiffness due to the high-order derivatives and nonlinear terms. So I have added some Method options to NDSolve ProcessEquations.


The Mathematica code



L = 30; tmax = 30; a = 1; b = 1/100; c = 1/(2 L); e = 1/10; nGrid = 91;
ic[x_] = e*Cos[\[Pi]*x/L];

sys = {D[u[x, t], t] + u[x, t]*D[u[x, t], x] + D[u[x, t], {x, 2}] +
D[u[x, t], {x, 4}] + a*int[D[u[x, t], {x, 3}], x, t] +
b*u[x, t]^3*intnest[D[u[x, t], {x, 2}]*int[D[u[x, t], x], x, t], x, t] == 0, u[-L, t] == u[L, t], u[x, 0] == ic[x]};

periodize[data_] := Append[data, {N@L, data[[1, 2]]}];(*for periodic interpolation*)

Block[{int, intnest},

(* IC fools ProcessEquations to consider int[] as a good num.fn.*)
int[uppp_, x_?NumericQ, t_ /; t == 0] := (cnt++;
c*NIntegrate[D[ic[xp], {xp, 3}]*Cot[\[Pi] (x - xp)/(2*L)], {xp, x - L, x, x + L},
Method -> {"InterpolationPointsSubdivision", Method -> {"PrincipalValue", "SymbolicProcessing" -> 0}},
PrecisionGoal -> 8, AccuracyGoal -> 8, MaxRecursion -> 10]);
int[uppp_?VectorQ, xv_?VectorQ, t_?NumericQ] := Function[x, cnt++;
c*NIntegrate[Interpolation[periodize@Transpose@{xv, uppp}, xp,
PeriodicInterpolation -> True]*Cot[\[Pi] (x - xp)/(2*L)], {xp, x - L, x, x + L},
Method -> {"InterpolationPointsSubdivision", Method -> {"PrincipalValue", "SymbolicProcessing" -> 0}},
PrecisionGoal -> 8, AccuracyGoal -> 8, MaxRecursion -> 10]] /@xv;

intnest[upp_, x_?NumericQ, t_ /; t == 0] := (cnt2++;
c*NIntegrate[D[ic[xp], {xp, 2}]*int[D[ic[xp], xp], x, t]*Cot[\[Pi] (x - xp)/(2*L)], {xp, x - L, x, x + L},
Method -> {"InterpolationPointsSubdivision", Method -> {"PrincipalValue", "SymbolicProcessing" -> 0}},
PrecisionGoal -> 8, AccuracyGoal -> 8, MaxRecursion -> 10]);
intnest[upp_?VectorQ, xv_?VectorQ, t_?NumericQ] := Function[x, cnt2++;
c*NIntegrate[Interpolation[periodize@Transpose@{xv, upp}, xp,
PeriodicInterpolation -> True]*Cot[\[Pi] (x - xp)/(2*L)], {xp, x - L, x, x + L},
Method -> {"InterpolationPointsSubdivision", Method -> {"PrincipalValue", "SymbolicProcessing" -> 0}},
PrecisionGoal -> 8, AccuracyGoal -> 8, MaxRecursion -> 10]] /@xv;
(*monitor while integrating pde*)

Clear[foo];
cnt = 0; cnt2 = 0;
PrintTemporary@Dynamic@{foo, cnt, cnt2, Clock[Infinity]};
(*broken down NDSolve call*)
Internal`InheritedBlock[{MapThread},
{state} = NDSolve`ProcessEquations[sys, u, {x, -L, L}, {t, 0, tmax},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid",
"MinPoints" -> nGrid, "MaxPoints" -> nGrid, "DifferenceOrder" -> "Pseudospectral"},
Method -> {"StiffnessSwitching", "NonstiffTest" -> Automatic}},

AccuracyGoal -> Infinity, WorkingPrecision -> 20,
MaxSteps -> \[Infinity], StepMonitor :> (foo = t)];
Unprotect[MapThread];
MapThread[f_, data_, 1] /; ! FreeQ[f, int] := f @@ data;
Protect[MapThread];
NDSolve`Iterate[state, {0, tmax}];
sol = NDSolve`ProcessSolutions[state]]] // AbsoluteTiming

My problem


As mentioned above, the code is very slow. An estimation: >2 hrs may be required to obtain convergence with tmax = 1. Btw, the "slwcon" and "ncvb" warning could be ignored (see Michael's comments following his answer there). Is there any approach that would help speed up the code? Thank you very much.



Some ideas


As suggested by Henrik Schumacher, the combination of NIntegrate and Interpolation limits the speed of this code. Maybe it could be better to use a fixed quadrature rule and implement the integration with ListConvolve. But I need help with implementing this idea in my problem, so I bring this problem here in the hope that someone could help.


I am thinking that can we divide the interval into a uniform grid with 2M(=nGrid−1) mesh points defined by xm=(m−M)h, where h=L/M. Please see also my answer to a similar problem. Then the integral term (2) could be evaluate at the midpoints xi+1/2=(xi+xi+1)/2, for i=0,1,…,2M−1, (note the periodicity demands u0=u2M) using a certain integration rule, e.g., trapezoidal rule, with xi as integration nodes. In this way, the principal value integral could be efficiently computed, as if it were simply an ordinary integral.



Answer



It is possible to solve this problem using the decomposition of the solution in a Fourier series. Then it is possible to replace the integrals with the coefficients of the Fourier series using the following obvious property
12π∫π−πe−inx′cot((x−x′)/2)dx′=ie−inx(1+sign(n))

It is exactly equal to FourierCoefficient[Cot[(x - xp)/2], xp, n] The result is a fairly simple code. But the calculation does not coincide with that obtained using the author's code. It means that we have to write another third code to test these two. For ease of use of Fourier series, we convert the coordinate and time according to x->k0 x, t->k0 t, k0=Pi/L.


L = 30; tmax = 30; a = 1; b = 1/100; c = 1/(2 L); e = 1/10; nn = 10; k0 = Pi/L; tm = tmax*k0;
a1 = a/k0;
uf[x_, t_] := Sum[f[k][t] Exp[I k x], {k, -nn, nn}]
eq = Table[

f[m]'[t] +
I Sum[ If[Abs[m - k] <= nn, f[m - k][t], 0] k f[k][t], {k, -nn,
nn, 1}] - k0*m^2 f[m][t] + k0^3 m^4 f[m][t] -
a k0^2 (I m)^3 I (1 - Sign[m]) f[m][t] +
b k0^2 Sum[
If[Abs[m - k - s1 - s2 - s3] <= nn,
f[s1][t] f[s2][t] f[s3][t] f[k][t] f[m - k - s1 - s2 - s3][
t] I (1 - Sign[k]) I (1 - Sign[m - k - s1 - s2 - s3]),
0], {s1, -nn, nn}, {s2, -nn, nn}, {s3, -nn, nn}, {k, -nn,
nn}] == 0, {m, -nn, nn, 1}];


ic = Table[
f[m][0] ==
e (KroneckerDelta[m, 1] + KroneckerDelta[m, -1])/2, {m, -nn, nn,
1}];
var = Table[f[i], {i, -nn, nn, 1}];
soli = NDSolve[{eq, ic}, var, {t, 0, tm}]; // AbsoluteTiming

Here is a comparison with the calculation using code @user55777 (left), Fourier (top right) and both codes at time t = 30 (bottom left). The bottom right shows the calculation without integrals (green curve) and with integrals (red curve) by the Fourier method.


Plot3D[Evaluate[Re[uf[x, t] /. soli]], {x, -Pi, Pi}, {t, 0, tm}, 

Mesh -> None, ColorFunction -> "Rainbow"]

Figure 1


Comments

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]