3

I know this error is very common. I checked out a good number of proposed solutions to this problem, but unfortunately, none of them helped in my case.

I would appreciate any help or hints on how to overcome this issue.

My program is quite simple. It is supposed to solve two coupled time-dependent partial differential equations on a sphere. I don't know if it's relevant, but from the physical point of view, it's actually the semi-classical approximation of a modified Schrodinger's equation on a sphere, with the Newtonian gravitational potential made scale-invariant.

The program is supposed to solve the PDE up to t=100, but it breaks down even at a value as low as 0.01. Here's the code with some comments for further information:

  (* defining the potential that will be used in the equations. *)

V[ϕ, θ] := 0.045360921162651446/Sqrt[1 + Cos[ϕ] Sin[θ]] + 0.045360921162651446/Sqrt[ 1 - Sin[θ] Sin[π/6 - ϕ]] + 0.045360921162651446/ Sqrt[1 - Sin[θ] Sin[π/6 + ϕ]];

(* defining the region in which the differential equation is to be solved. θ = π/2 is avoided on purpose because of the singularities of V *)

Π1 = ImplicitRegion[ 0 <= ϕ <= 2 π && 0 <= θ <= π/2 - 0.001, {ϕ, θ}]; Π2 = ImplicitRegion[ 0 <= ϕ <= 2 π && π/2 + 0.001 <= θ <= π, {ϕ, θ}]; Π = RegionUnion[Π1, Π2];

(* defining the two coupled PDEs. *)

firsteq = {-D[Subscript[S, 0][ϕ, θ, t], t] == -V[ϕ, θ] + (Derivative[0, 1, 0][Subscript[S, 0]][ϕ, θ, t]^2 + Sec[θ]^2Derivative[1, 0, 0][Subscript[S, 0]][ϕ, θ, t]^2)}; secondeq = {-D[Subscript[S, 1][ϕ, θ, t], t] == -((-Tan[θ])Derivative[0, 1, 0][Subscript[S, 0]][ϕ, θ, t] + Derivative[0, 2, 0][Subscript[S, 0]][ϕ, θ, t] + Sec[θ]^2Derivative[2, 0, 0][Subscript[S, 0]][ϕ, θ, t]) + 2(Derivative[0, 1, 0][Subscript[S, 0]][ϕ, θ, t]Derivative[0, 1, 0][Subscript[S, 1]][ϕ, θ, t] + Sec[θ]^2Derivative[1, 0, 0][Subscript[S, 0]][ϕ, θ, t]*Derivative[1, 0, 0][Subscript[S, 1]][ϕ, θ, t])};

(* defining the initial conditions *)

initial = {Subscript[S, 0][ϕ, θ, 0] == Sin[θ], Subscript[S, 1][ϕ, θ, 0] == Sin[θ]^2};

Equations = Join[firsteq, secondeq, initial];

(* Solving the equations ... *)

Solution = NDSolve[Equations, {Subscript[S, 0], Subscript[S, 1]}, {t, 0, 100}, {ϕ, θ} ∈ Π, Method -> "Automatic"];

I get the error:

NDSolve::ndsz: At t == 0.011684614824200409`, step size is effectively zero; singularity or stiff system suspected.

The system is indeed singular. I plotted the incomplete solution and it appears that very soon the solution diverges over some regions near the poles and equator. Is there some way to overcome this problem?

Thank you very much.

Edit:

I modified the potential to remove its singularities:

V[ϕ_, θ_] := 
      0.045360921162651446/Sqrt[1.00001 + Cos[ϕ] Sin[θ]] + 
       0.045360921162651446/Sqrt[
       1.00001 - Sin[θ] Sin[π/6 - ϕ]] + 0.045360921162651446/
       Sqrt[1.00001 - Sin[θ] Sin[π/6 + ϕ]];

Following the suggestions in the comments, I tried to correct another issue in the code and define periodic boundary conditions for ϕ. Here's the code I added. I'm grateful to @xzczd for this.

  δ = 10^-5;
periodicity = {PeriodicBoundaryCondition[
    Subscript[S, 0][ϕ, θ, t], ϕ == 0 && 
     0 <= θ <= π , Function[x, x + {2 π, 0}]], 
   PeriodicBoundaryCondition[
    Subscript[S, 0][ϕ, θ, 
     t], ϕ == 2 π + δ && 0 <= θ <= π , 
    Function[x, x + {-2 π, 0}]], 
   PeriodicBoundaryCondition[
    Subscript[S, 1][ϕ, θ, t], ϕ == 0 && 
     0 <= θ <= π , Function[x, x + {2 π, 0}]], 
   PeriodicBoundaryCondition[
    Subscript[S, 1][ϕ, θ, 
     t], ϕ == 2 π + δ && 0 <= θ <= π , 
    Function[x, x + {-2 π, 0}]]};

But this time, I got this error:

NDSolve::ndcf: Repeated convergence test failure at t == 0.`; unable to continue.

Following @xzczd's helpful suggestion, I decided to forget about FiniteElements and use the TensorProduct method instead. Here's the final code. I also changed the initial conditions.

    firsteq = {-D[Subscript[S, 0][ϕ, θ, t], t] == -V[ϕ, θ] + (Derivative[0, 1, 0][Subscript[S, 0]][ϕ, θ, t]^2 + Csc[θ]^2*Derivative[1, 0, 0][Subscript[S, 0]][ϕ, θ, t]^2)}; 
    secondeq = {-D[Subscript[S, 1][ϕ, θ, t], t] == -((-Cot[θ])*Derivative[0, 1, 0][Subscript[S, 0]][ϕ, θ, t] + Derivative[0, 2, 0][Subscript[S, 0]][ϕ, θ, t] + Csc[θ]^2*Derivative[2, 0, 0][Subscript[S, 0]][ϕ, θ, t]) + 
          2*(Derivative[0, 1, 0][Subscript[S, 0]][ϕ, θ, t]*Derivative[0, 1, 0][Subscript[S, 1]][ϕ, θ, t] + Csc[θ]^2*Derivative[1, 0, 0][Subscript[S, 0]][ϕ, θ, t]*Derivative[1, 0, 0][Subscript[S, 1]][ϕ, θ, t])};
initial = {Subscript[S, 0][ϕ, θ, 0] == Sin[θ]^2, 
           Subscript[S, 1][ϕ, θ, 0] == Sin[θ]^2};


Periodicity = {Subscript[S, 0][0, θ, t] == Subscript[S, 0][2 π, θ, t], Subscript[S, 1][0, θ, t] == Subscript[S, 1][2 π, θ, t]};

        Equations = Join[firsteq, secondeq, initial, periodicity];

        Solution = 
              NDSolve[Equations, {Subscript[S, 0], Subscript[S, 1]}, {t, 0, 
                10}, {ϕ, 0, 2 π}, {θ, 0, π}, 
               Method -&gt; {&quot;PDEDiscretization&quot; -&gt; {&quot;MethodOfLines&quot;, 
                   &quot;SpatialDiscretization&quot; -&gt; {&quot;TensorProductGrid&quot;, 
                     &quot;MaxPoints&quot; -&gt; 500, &quot;MinPoints&quot; -&gt; 500}}}];

However, after about half an hour, I got this error:

NDSolve::ndsz: At t == 0.00003716693557750961`, step size is effectively zero; singularity or stiff system suspected.

NDSolve::eerr: Warning: scaled local spatial error estimate of 1.0182820651808292*^12 at t = 0.00003716693557750961 in the direction of independent variable ϕ is much greater than the prescribed error tolerance. Grid spacing with 500 points may be too large to achieve the desired accuracy or precision. A singularity may have formed or a smaller grid spacing can be specified using the MaxStepSize or MinPoints method options.

I tried to make the discretization finer, but the kernel crashed for MaxPoints, MinPoints = 1000.

xzczd
  • 65,995
  • 9
  • 163
  • 468
Po_oya
  • 33
  • 5
  • 1
    Hello! There are several problems. You specify $0\leq\phi\leq 2\pi$ in the domain and surely are thinking that angle $\phi = 0$ and angle $\phi = 2\pi$ must be identified. No, for Mathematica \[Phi] is just another symbol. You seem to think that you get away with simply cutting out a $0.001$-neighborhood near some singularity. No, you are creating a new boundary. Also, spherical coordinates are themselves singular at the north and south poles. – user293787 Jul 24 '22 at 14:11
  • I do not want discourage you. With the search phrase "[differential-equations] spherical coordinates" here on Mathematics SE I found this and this and this and this. Perhaps you can find some interesting hints or ideas there. – user293787 Jul 24 '22 at 14:23
  • @user293787 Singularities at north and south poles won't be a problem, FEM is able to handle the removable singularities. Example: https://mathematica.stackexchange.com/questions/245195/laplaces-equation-in-spherical-coordinates-with-neumann-b-c/245309#comment617526_245309 – xzczd Jul 24 '22 at 14:28
  • 1
    But the missing boundary condition in $\phi$ direction is indeed a problem. See e.g. this and this. Also, since the problem is defined in a regular domain, I'd say the old-good TensorProductGrid is a better choice, see e.g. discussions here and here. And, I believe you need to re-consider the definition of the potential, can you turn to a potential free of singularity? – xzczd Jul 24 '22 at 14:42
  • Oh I forgot to emphasize: "Singularities at north and south poles won't be a problem, FEM is able to handle the removable singularities." As long as the PDEs are correct. I'm not familiar with the physics background so cannot check, but please make sure the PDE system itself is correct. (If it's convenient for you, consider adding a bit more background info, that'll help us analyzing the problem. ) – xzczd Jul 24 '22 at 14:51
  • Thanks @xzczd. A math hack to remove the singularity for something of the form $1/f$, where $f \geq 0$ can vanish in some places, is to replace it by $1/\sqrt{f^2+a^2}$ for some small $a>0$. Depends on the problem if that will help. Concerning $\phi$ there is PeriodicBoundaryCondition but not sure if that will help. – user293787 Jul 24 '22 at 15:04
  • @user293787, Hello. Thank you very much for the help. Yes, I was aware of that issue with the \phi variable, but thought it was not so important as I'm only interested in the solution over some small neighborhood for calculating some trajectories. Anyway, I think I'm wrong and it does affect the final result. I tried to correct it but failed. I put the code in the question.

    Yes, I actually wanted to avoid the singularities by modifying the region. So I have to modify the potential then. Thanks for the warning.

    – Po_oya Jul 24 '22 at 20:21
  • @xzczd, Thank you for this. I added the code for the periodic boundary conditions, but it doesn't work. It gives me a long list of errors, I put it in the question. I would appreciate it if you could have a look at it. I tried to use the structure of the code you wrote in one of the questions you sent. Have I made a mistake somewhere? – Po_oya Jul 24 '22 at 20:22
  • In light of your suggestions, I changed the potential (please see the edited code above). One can simply add a small number to the expression under the square root to make it singularity free.

    Thank you for the suggestion. I ignored the periodic boundary conditions to see what happens, but even with the singularity-free potential, got another error.

    – Po_oya Jul 24 '22 at 20:23
  • @xzczd, I would use TensorProductGrid if you suggest it resolves the problem, but couldn't quite follow the two discussions you referred me to. Is there some pedagogical introduction on how to implement the technique? – Po_oya Jul 24 '22 at 20:24
  • @xzczd, I'm afraid I don't know what you mean by "correct PDEs". The physical background of this work is very unconventional. It's part of the research I'm doing. To my knowledge, it's not in any literature, and there is no similar numerical work, I suppose. – Po_oya Jul 24 '22 at 20:30
  • The equation above is the semi-classical approximation of a modified Schrodinger's equation on a sphere, with the potential defined in the code, which is actually the Newtonian gravitational potential made scale-invariant. I decided to skip all of these details lest I confuse and distract the readers. But I'm confident that the equations are correct, meaning that they follow from the model we're considering. I used Mathematica for it. – Po_oya Jul 24 '22 at 20:39
  • 1
    Currently FEM is automatically chosen because you've used Element and PeriodicBoundaryCondition in your code. (See this post for more info. ) Anyway, if you want to use PeriodicBoundaryCondition: t should not be part of the domain, because FiniteElement method is only used for spatial discretization, something like PeriodicBoundaryCondition[ Subscript[S, 0][\[Phi], \[Theta], t], \[Phi] == 0 && 0 <= \[Theta] <= \[Pi], Function[x, x + {2 \[Pi], 0}]] (You need to modify the other 3 accordingly, of course) should work. – xzczd Jul 25 '22 at 01:58
  • "but thought it was not so important as I'm only interested in the solution over some small neighborhood for calculating some trajectories. " Do you mean the the solution is supposed to be very localized e.g. non-zero solution only exists in a very small region of the domain? Also, is the i.c. Subscript[S, 0][ϕ, θ, 0] == Sin[θ] necessary? Derivative in $\theta$ direction at the poles are supposed to be zero to make the solution smooth there, but the i.c. doesn't satisfy it, which may be a problem. – xzczd Jul 25 '22 at 02:22
  • @xzczd, Thanks for the clear explanation and the post you suggested. Yes, I corrected the boundary conditions and it now works, but this time I got another error for NDSolve (please see the edit above). – Po_oya Jul 25 '22 at 12:36
  • "Do you mean the solution is supposed to be very localized e.g. non-zero solution only exists in a very small region of the domain?"

    No, but as the trajectories will be limited to a small neighborhood, I thought what happens near the boundaries wouldn't affect the central regions, provided that the evolution is sufficiently slow. But I'm now quite sure that I was wrong, and it does affect the overall solution in that time interval.

    – Po_oya Jul 25 '22 at 12:37
  • @xzczd, About the initial condition, I actually want the gradient of S_0 to be in the direction of the equator and away from the poles, that's why I used Sin[theta]. But I see your point now and changed it to Sin[theta]^2. This has vanishing derivatives at the poles and is good for my work. Thanks!

    Anyway, I don't actually know the initial conditions myself. This work is part of a new research and I have to probably work with so many different initial conditions to find the best one.

    – Po_oya Jul 25 '22 at 12:40
  • I also read a bit more about the TensorProductGrid method and looked again at the posts you sent. Yes, I understand now. I decided to use it. This time, the program actually worked. But it's highly inefficient. I set the values for MaxPoints and MinPoints equal to 500, but it broke down again at about t = 0.00003. I have included the details in the edit above.

    What do you suggest I do? The kernel crashes when I try to use a larger value.

    Thank you very much for your help!

    – Po_oya Jul 25 '22 at 12:48
  • 1
    The Sec[θ] term is undoubtedly hard to handle. There might exist certain clever variable transformation that can avoid this singularity (an example is this post) but I can't think out one. Anyway, by adding a relatively large artificial viscosity term (μ=10^-1) and choosing a relatively coarse grid (100 points for each dimension) I obtain the following $S_0$ (firsteq only involves $S_0$, so I solve it separately): https://i.stack.imgur.com/EhS5J.png Is this solution expected? – xzczd Jul 25 '22 at 16:05
  • @xzczd, Actually I just noticed I had made a mistake in defining the metric and therefore, got the incorrect equations. All Sec[theta] and Tan[theta] terms must be changed into Csc[theta] and Cot[theta] respectively. In fact, the spatial derivatives in the equations follow from covariant derivatives w.r.t the natural metric defined on the sphere. I put the correct ones above. – Po_oya Jul 25 '22 at 21:49
  • With this correction, now the equations are singular at the poles. I changed the θ interval to `{[Theta], 0.01, [Pi]-0.01}, but got a similar error with the 500 points. For 1000 points, my computer crashed because of the heavy computation load! I don't know... – Po_oya Jul 25 '22 at 21:49
  • I would be very grateful if you could send me the modified code you used. I don't know if that viscosity term affects the physics behind the solutions or not, but it is helpful.

    Thank you for your efforts and help.

    – Po_oya Jul 25 '22 at 21:49

1 Answers1

5

First of all, I'd like to emphasize again that it's important to make sure the equation system itself is correct. Anyway, after the correction the system is less demanding, so let me post an answer.

The idea is simple: the system is nonlinear, derivative of $S_0$ in $\theta$ direction of firsteq is of 1st order, derivative of $S_1$ in $\theta$ direction of secondeq is of 1st order, the solution of the system seems to be stiff, so the experience obtained in e.g. this post may help. Let's try artificial viscosity.

After adding artificial viscosity, something related to the issue discussed in this post seems to show up, so I use pdetoode to discretize the system.

V[ϕ_, θ_] := 
  0.045360921162651446/Sqrt[1.00001 + Cos[ϕ] Sin[θ]] + 
   0.045360921162651446/Sqrt[1.00001 - Sin[θ] Sin[π/6 - ϕ]] + 
   0.045360921162651446/Sqrt[1.00001 - Sin[θ] Sin[π/6 + ϕ]];

Clear[points, grid, domain] points@ϕ = points@θ = 200; eps = 0(Pi/10^2); domain@ϕ = {0, 2 Pi}; domain@θ = {eps, Pi - eps}; (grid@# = Array[# &, points@#, domain@#]) & /@ {ϕ, θ};

difforder = 2; (* Definition of pdetoode isn't included in this post, please find it in the link above. *) ptoofunc = pdetoode[{Subscript[S, 0], Subscript[S, 1]}[ϕ, θ, t], t, grid /@ {ϕ, θ}, difforder, {True, False}]; μ0 = 10^-2; μ1 = 10^-2; Unevaluated[ eq = {μ0 (D[Subscript[S, 0], {θ, 2}] + D[Subscript[S, 0], {ϕ, 2}]) - D[Subscript[S, 0], t] == -V[ϕ, θ] + (D[Subscript[S, 0], θ]^2 + Csc[θ]^2 D[Subscript[S, 0], ϕ]^2), μ1 (D[Subscript[S, 1], {θ, 2}] + D[Subscript[S, 1], {ϕ, 2}]) - D[Subscript[S, 1], t] == -(-Cot[θ] D[Subscript[S, 0], θ] + D[Subscript[S, 0], {θ, 2}] + Csc[θ]^2 D[Subscript[S, 0], {ϕ, 2}]) + 2 (D[Subscript[S, 0], θ] D[Subscript[S, 1], θ] + Csc[θ]^2 D[Subscript[S, 0], ϕ] D[Subscript[S, 1], ϕ])}; ic = {Subscript[S, 0] == Sin[θ]^2, Subscript[S, 1] == Sin[θ]^2} /. t -> 0; bc = {D[Subscript[S, 0], θ] == 0, D[Subscript[S, 1], θ] == 0} /. {{θ -> domain[θ][[1]]}, {θ -> domain[θ][[-1]]}}] /. {Subscript[S, 0] -> Subscript[S, 0][ϕ, θ, t], Subscript[S, 1] -> Subscript[S, 1][ϕ, θ, t]}; del = #[[2 ;; -2]] &; ode = Map[del, ptoofunc@eq, {2}] // Quiet;

odebc = ptoofunc@diffbc[t][bc]; odeic = ptoofunc@ic;

varlst = Outer[#@##2 &, {Subscript[S, 0], Subscript[S, 1]}, grid@ϕ, grid@θ];

tend = 10;

showStatus[status_]:=LinkWrite[$ParentLink, SetNotebookStatusLine[FrontEnd`EvaluationNotebook[],ToString[status]]]; clearStatus[]:=showStatus[""]; clearStatus[] jianshi[t_]:=EvaluationMonitor:>showStatus["t = "<>ToString[CForm[t]]] sollst = NDSolveValue[{ode, odeic, odebc}, varlst, {t, 0, tend}, jianshi[t], Method -> {"EquationSimplification" -> "MassMatrix"}(,SolveDelayed-> True)]; // AbsoluteTiming (* {1984.24, Null} *)

{sols0, sols1} = rebuild[#, grid /@ {ϕ, θ}, -1] & /@ sollst;

Remark

Adding PrecisionGoal -> 3, AccuracyGoal -> 3 to NDSolveValue will reduce the timing to about 277.04 seconds! But the beginning part of $S_1$ (for about $t<0.2$) changes. (Not widely different, but quite observable. ) Anyway, if the accuracy of transient behavior isn't important for you, consider adding these options.

The showStatus is from this post.

I've solved $S_0$ and $S_1$ all in one, but since firsteq only involves $S_0$ (and further trial suggests it's easier to solve than secondeq), you may consider solve firsteq first.

Let's check the solution. $S_0$:

piclst0 = Table[
   Plot3D[sols0[f, th, t], {th, #3, #4}, {f, #1, #2}, PlotPoints -> 50] & @@ 
    Flatten[domain /@ {ϕ, θ}], {t, 0, tend, tend/25}];

ListAnimate[piclst0]

enter image description here

$S_1$:

piclst1 = Table[
   SphericalPlot3D[sols1[f, th, t], 
     {th, #3, #4}, {f, #1, #2}, PlotPoints -> 50] & @@
       Flatten[domain /@ {ϕ, θ}], {t, 0, tend, tend/25}];

ListAnimate[ Show[#, PlotRange -> {{-1.2, 1.2}, {-1.2, 1.2}, {-3.2, 3.2}}] & /@ piclst1]

enter image description here

The solution around $\theta=\pi/2$ looks a bit unusual, I'm not sure if it's the nature of the solution or numeric error. You may adjust points@ϕ, points@θ, μ0, μ1 further to see how the solution changes.

xzczd
  • 65,995
  • 9
  • 163
  • 468
  • It is a very nice solution (+1). Please note, first equation (the Hamilton-Jacobi equation) can be solved separately on Rectangle[{0,0}, {2 Pi, Pi}] without artificial viscosity. – Alex Trounev Jul 26 '22 at 10:10
  • @AlexTrounev Er… what setting do you choose? For points@ϕ = points@θ = 100;, NDSolve gets stuck around t==1.6 when solving firsteq; for points@ϕ = points@θ = 64; it gets stuck around t==2.6. (The steep potential function seems to be a problem. ) – xzczd Jul 26 '22 at 13:14
  • Thank you very much for your answer. It helped a lot. I had been working on the program with your code. The viscosity term doesn't affect the dynamics very considerably but comes in handy especially for the first equation, as it tames the singular nodes that form.

    The only issue I face for some initial conditions is the computation time. Unless I run the code on a supercomputer or leave mine running for days (if not weeks!), the computation time can be an issue.

    Is there some way to increase the proficiency?

    – Po_oya Jul 30 '22 at 20:08
  • I also tried to use this code for the full Schrodinger equation (without this approximation), but again faced the same error ("step size is effectively zero") which couldn't be solved by modifying the equations and adding the viscosity terms. Increasing the number of points was not a viable option either due to the huge computational load that would have been entailed. Anyway, that's a question I need to post separately. Thank you so much for your help. – Po_oya Jul 30 '22 at 20:11
  • 1
    @Po_oya As to performance tuning, see the new-added Remark. – xzczd Jul 31 '22 at 03:25