0
$\begingroup$

First, NDSolve is an incredible tool. I've been pushing it hard. Second, this error arises because I don't fully understand how best to handle the singularities. Third, I share this because

  1. This throws a Dot::dotsh error on step 90 and then brings down the kernel. Not a graceful exit
  2. The behavior is similar to that discussed in FEM Mesh related errors that proved to be a bug

Has anyone seen similar behavior? Have a workaround?

Background: Using NDSolve to adjust

  1. the constants cee[i][t] defining the functions g[x,i,t]
  2. the integrals of g[x,i,t]
  3. constants F[i][t] -- the optimality criteria are in terms of F[i][t]+G[x,i,t]
  4. the points s[i,j][t] where the functions g cross

in order to obtain certain optimality criteria.

Singularities: Arise when g[x,i,t] and g[x,i+1,t] are tangent at a single point. To avoid these, when the points s[i,i+t][t] and s[i+1,i][t] get too close, I try to adjust the derivatives to keep them from moving closer using "slacks". This works well when one or two function pairs are getting close to being tangent. As you can see in the figure, this is a case where several curves are becoming tangent. Would welcome any suggestions for managing this sort of phenomenon more elegantly and robustly.

The functions g and the points where they cross

Here's code that produces the issue (on Mathematica 12.2.0.0 on Mac). It works fine up to step 89. Step 90 brings down the kernel. Apologies for all the definitions at the beginning and the awkward translations. They capture the results of several previous steps to get to this point.

With[{params = {sigma->1.`80.,h->1.`80.,p->-1.`80.,U->2.`80.,M->2.`80.,theta->1.`80.,kplus->0,kminus->0,kvplus->0.01`80.,kvminus->0.01`80.},
      rates = {u[1]->-10.`80.,u[2]->-6.`80.,u[3]->-1.`80.,u[4]->1.`80.,u[5]->3.`80.,u[6]->4.`80.,u[7]->5.`80.,u[8]->6.`80.,u[9]->8.`80.,u[10]->10.`80.},
      kays = {k[1,1]->0,k[1,2]->0.000948802000000000000413946654731489616096951067447662353515625`80.,k[1,3]->0.00319882000000000000540512079538757461705245077610015869140625`80.,k[1,4]->0.001861119999999999961859398212027372210286557674407958984375`80.,k[1,5]->0.0043391096400000000071726382298853419428041888750158250331878662109375`80.,k[1,6]->4.8726399999999999885515709896477432039318955503404140472412109375`80.*^-6,k[1,7]->0.0012894856399999999875156399785358640741605995572172105312347412109375`80.,k[1,8]->0.001244516999999999997371380455746248117065988481044769287109375`80.,k[1,9]->0.000494853999999999975313580424796100487583316862583160400390625`80.,k[1,10]->0.0022131436400000000402874824299848111053279353654943406581878662109375`80.,k[2,1]->0.00473396800000000007348222030856277342536486685276031494140625`80.,k[2,2]->0,k[2,3]->0.0036933234000000001741835509161404615952051244676113128662109375`80.,k[2,4]->0.003059352999999999898894653682646094239316880702972412109375`80.,k[2,5]->0.003959572999999999977187303823455977180856280028820037841796875`80.,k[2,6]->0.0000545933999999999980921872033690078751533292233943939208984375`80.,k[2,7]->0.00090994899999999995753030557210649931221269071102142333984375`80.,k[2,8]->0.00029571499999999999695743380101475850096903741359710693359375`80.,k[2,9]->0.0010991830000000001044124786631073220632970333099365234375`80.,k[2,10]->0.001833607000000000010302148023555446343380026519298553466796875`80.,k[3,1]->0.00506821399999999970542585714383676531724631786346435546875`80.,k[3,2]->0.002353580000000000081727957734756273566745221614837646484375`80.,k[3,3]->0,k[3,4]->0.0039197689999999995973112287828143962542526423931121826171875`80.,k[3,5]->0.004931654999999999957997764976624921473558060824871063232421875`80.,k[3,6]->0.00059741799999999995081367831772922727395780384540557861328125`80.,k[3,7]->0.00188203099999999993834076672527544360491447150707244873046875`80.,k[3,8]->0.00264929500000000007868539153577103206771425902843475341796875`80.,k[3,9]->0.0019595989999999998028290537632756240782327950000762939453125`80.,k[3,10]->0.002805688999999999991112609176724390636081807315349578857421875`80.,k[4,1]->0.004469362000000000161026303402422854560427367687225341796875`80.,k[4,2]->0.00183009100000000005277478454246420369599945843219757080078125`80.,k[4,3]->0.00133770000000000004354572258336020240676589310169219970703125`80.,k[4,4]->0,k[4,5]->0.003694967000000000064731386917316058315918780863285064697265625`80.,k[4,6]->0.000191704000000000006342926184288444346748292446136474609375`80.,k[4,7]->0.0006453430000000000450743886659665804472751915454864501953125`80.,k[4,8]->0.002125806000000000049732218343478962196968495845794677734375`80.,k[4,9]->0.002895108999999999876973078016817453317344188690185546875`80.,k[4,10]->0.001569001000000000097846231117415527478442527353763580322265625`80.,k[5,1]->0.004310083000000000143236977834249046281911432743072509765625`80.,k[5,2]->0.00380053999999999990249376080608811889760545454919338226318359375`80.,k[5,3]->0.00353179899999999982718699997885636321370839141309261322021484375`80.,k[5,4]->0.00219409899999999978364127739549616080694249831140041351318359375`80.,k[5,5]->0,k[5,6]->0.00171352699999999992671283199907605876433080993592739105224609375`80.,k[5,7]->0.00283944199999999982871566606146274125421768985688686370849609375`80.,k[5,8]->0.00409625499999999989945119460710287739857449196279048919677734375`80.,k[5,9]->0.00023392899999999998915910237595738863092265091836452484130859375`80.,k[5,10]->0.00376309999999999988148750851291168828538502566516399383544921875`80.,k[6,1]->0.004771982000000000133377309197157956077717244625091552734375`80.,k[6,2]->0.0022137440000000001283364525761498953215777873992919921875`80.,k[6,3]->0.0036387300000000001760913637127714537200517952442169189453125`80.,k[6,4]->0.0046635749999999996651123268520677811466157436370849609375`80.,k[6,5]->0.004334237000000000007184086658895694199600256979465484619140625`80.,k[6,6]->0,k[6,7]->0.0012846129999999999875270884075462163309566676616668701171875`80.,k[6,8]->0.00250945900000000012529388637716465382254682481288909912109375`80.,k[6,9]->0.002703404999999999870630151832529008970595896244049072265625`80.,k[6,10]->0.002208271000000000040298930858995163362124003469944000244140625`80.,k[7,1]->0.0038240190000000001159519147364562741131521761417388916015625`80.,k[7,2]->0.004772821000000000116365861391187763729249127209186553955078125`80.,k[7,3]->0.0064269710000000000726938509387764497660100460052490234375`80.,k[7,4]->0.00524372299999999980329827564684563867558608762919902801513671875`80.,k[7,5]->0.003049624000000000019656998251349477868643589317798614501953125`80.,k[7,6]->0.0038288916400000001159404663074459218563561080372892320156097412109375`80.,k[7,7]->0,k[7,8]->0.005068536000000000113323295192202522230218164622783660888671875`80.,k[7,9]->0.00328355300000000000881610062730686649956624023616313934326171875`80.,k[7,10]->0.000923658000000000052771842451448947031167335808277130126953125`80.,k[8,1]->0.00566556300000000019612134138924375292845070362091064453125`80.,k[8,2]->0.0055891789999999996751967046293430030345916748046875`80.,k[8,3]->0.0065510550000000000725464133211062289774417877197265625`80.,k[8,4]->0.005927627999999999890146096248599860700778663158416748046875`80.,k[8,5]->0.00650640799999999998315391991354772471822798252105712890625`80.,k[8,6]->0.0056437723999999996732888918327120109097450040280818939208984375`80.,k[8,7]->0.0037648000000000000041244785364824565476737916469573974609375`80.,k[8,8]->0,k[8,9]->0.005210860000000000318010062727580589125864207744598388671875`80.,k[8,10]->0.004688458000000000056896320987931403578841127455234527587890625`80.,k[9,1]->0.005312981999999999947748019479831782518886029720306396484375`80.,k[9,2]->0.003566610999999999913334658430130730266682803630828857421875`80.,k[9,3]->0.00329786999999999983802789760289897458278574049472808837890625`80.,k[9,4]->0.001960169999999999794482175019538772176019847393035888671875`80.,k[9,5]->0.005655136999999999859213561936854830491938628256320953369140625`80.,k[9,6]->0.0014795979999999999375537296231186701334081590175628662109375`80.,k[9,7]->0.0026055129999999998395565636855053526232950389385223388671875`80.,k[9,8]->0.00386232599999999991029209223114548876765184104442596435546875`80.,k[9,9]->0,k[9,10]->0.003529170999999999892328406136954299654462374746799468994140625`80.,k[10,1]->0.0037746849999999998459543348872102797031402587890625`80.,k[10,2]->0.004723486999999999846368281541941769319237209856510162353515625`80.,k[10,3]->0.00565776499999999979407215577875689405118464492261409759521484375`80.,k[10,4]->0.00432006499999999975052643319539669164441875182092189788818359375`80.,k[10,5]->0.002125965999999999966885155799900530837476253509521484375`80.,k[10,6]->0.0037795576399999998459428864581999274463441906846128404140472412109375`80.,k[10,7]->0.004285693000000000141280764864859520457684993743896484375`80.,k[10,8]->0.005019201999999999843325715342956527820206247270107269287109375`80.,k[10,9]->0.00235989499999999995604425817585791946839890442788600921630859375`80.,k[10,10]->0},
      imin = 4, imax = 9, 
      beta = <|4->9,5->8,6->8,7->9,8->9,9->9|>, 
      tau = <|4->4,5->4,6->4,7->4,8->4,9->4|>,
      transient = {5,6,7,8}, recurrent = {4,9}, 
      badedge = 4\[DirectedEdge]9, ifix=4,
      sol = {cee[4][t]->-3.302791794819281596993754918490030955226349847121628160661343999062238531610238268496173916025507`78.53558590114208,F[4][t]->0,cee[5][t]->-2.01015278250513775193133051684584753505335423622868961961597414581628527876419169133487159325893`78.0817234313792,F[5][t]->1.343401625712991005406165202250305478094435346189466654599143491341899843261523924856541322142774`78.70546098109291,cee[6][t]->-2.158841423817423445929021697444016615740714802618823197582128636785997746489392370814433861513868`77.95959345812315,F[6][t]->1.427794232532325113002858236024552635056297644849789538816171896112257114351069513634824655343978`78.68984088472966,cee[7][t]->-2.494275023500774311383478819031971217608964889260843826746008836891013231220354764454279694105394`78.63841490082763,F[7][t]->1.469964728191305670104144114920602124613752751536596529895854311670222685243435572198114948124104`78.60625348577585,cee[8][t]->-2.659312902881342888249272958154388089334496177803768287020470186306008806717256525821592811147189`77.78647627276752,F[8][t]->1.497336648672494286294019352619040523337514273218063215390475941700163688623663205919466704233362`78.67386581427606,cee[9][t]->-3.251185532877928483265430755630707901347888595847215401235340823103658834871238823453578979885777`79.46228914704172,F[9][t]->1.521206236469674296705328412178746857709956387567120650707190578072839578102620759279284377440764`78.65105506897532,t->1.946984263023427866123446045045663210783108766777723209882726584829270678969910587628631839086304`80.},
      gamma = 1.946984263023427604549523495604794227616510386383352725147800943701679274630699882935658645126065`80.,
      eps = 0.002467497495021921118119563337708397058411989621746925309331579003232261728963181038287124459513`75.11118243564009,
      svals = {s[4,4][t]->0,s[4,5][t]->0.190087865187306079347774896944024078764595727983799391707412558900002483159768065236175053796497`78.12734067841095,s[4,6][t]->0.190067511329536623998513482119111640681151098400723852917252029540230430519101532677398385832233`78.1291965639342,s[4,7][t]->0.190066961592769588081767703584330154900311951719778210086657593386671735567878944153018479360661`75.58177481037254,s[4,8][t]->0.135521322674871638057978680200200464316895565454473961450419677785130780219380120968639399757355`77.990594202613,s[4,9][t]->0.111897221571789500885116511377590879608647841143903077876360533775999498301945608697126784130255`77.90185067358271,s[5,4][t]->0.190860463182425692050716361677237478423649971832608508871918252181955418107288039741305468646195`78.13562084908294,s[5,5][t]->0,s[5,6][t]->0.190029588032196751065027751763413656389324723815287711272867047478318513834938101347622647467904`78.13203667868355,s[5,7][t]->0.190047079006321721760470939696033101678163552198939570166711545085338396622182597487360025738304`75.30221419722788,s[5,8][t]->0.118826532001887559772871320360363732063964257914701255184653799067801726444121035491640196624339`77.9352850479685,s[5,9][t]->0.096690102007063943634636937686381599194589253454058235270560436777569213758930409799338167637506`77.69491729152642,s[6,4][t]->0.190880546308302371759876712501604430048923251166091491269676563343668167001066455555354740521756`78.13477020581733,s[6,5][t]->0.190917992258709403401558203630710799540871029042871087091734547460147664299037083952524240128583`78.13291770883845,s[6,6][t]->0,s[6,7][t]->0.190065316813216850497599693136618771548435777370231490966921596640344373217447308135324361224472`74.98728997313931,s[6,8][t]->0.101163979611307745143096480101771676336768704042898647500785657574296787146064140507816131423978`77.86728823527966,s[6,9][t]->0.084357124225972292197652946909845137560331165156490839393532658051364362301501622265315934406331`77.54191796970049,s[7,4][t]->0.190880873630853732049595609368503542631507821912291426095320191070317152211301774782582689681218`75.58333963097162,s[7,5][t]->0.190900306767741811327364976559911153438199285533937483735457018189088522169830133739416578180947`75.30459181724046,s[7,6][t]->0.190881854019862246866455459172735379616245945484751774337619204686585105730381173515450490140969`74.98992804400359,s[7,7][t]->0,s[7,8][t]->0.057202224888746466696356719376470990596468816875733018193199701708565877341357433828830192201796`76.67575320659363,s[7,9][t]->0.065065748977569297991850050136490076550902655509932021238590421349533413535631772281482360891249`78.09375472812498,s[8,4][t]->0.229813367114266495299805593208395472535241317355907796274512817116168943978612770077192813349797`78.21025950729067,s[8,5][t]->0.24090633228117474744656112566980464351618817958213614151882204028261460877623733421434729897017`78.22960944629526,s[8,6][t]->0.251490036493382785458846510045026368875483564943286876864350343699768199571436343880867172872953`78.24723110280743,s[8,7][t]->0.273769485604247957881466666203347883422288608489667846407477001937637232057219440990547007188944`77.7253633846222,s[8,8][t]->0,s[8,9][t]->0.06909524812949649757999431953794515900682378063581482068222354272787503695944387942669438761994`77.09301828677526,s[9,4][t]->0.234204493126917749695593110684689437008272030260295522468161718882586177496073148935497067827605`78.02259986803554,s[9,5][t]->0.241969306832140246659991287244717644437497344243378819376393134901921434384444729429533349892336`78.0100343883893,s[9,6][t]->0.247672632096309143554438793521313477237646866623043587583902937535197245063667660871395276150946`78.00222133282291,s[9,7][t]->0.255707787344911888401058806479304981720244155358146106717005312615956426907258864617293381032509`78.66066002903834,s[9,8][t]->0.243517948680056653723346086102258940910739805456367681428613042304298586182208291085550490336996`77.81812573513601,s[9,9][t]->0},
     precision = 80 },
     Off[NDSolve::precw];
     Off[SystemOptions::noset];
  g[x_,i_,t_]:= -p-(h x)/u[i]+(h sigma^2)/(2 u[i]^2)+t/u[i]+E^(-((2 u[i])/sigma^2)x) cee[i][t];
  G[x_,i_,t_]:= -(h /(2 u[i])) x^2+(-p +t/u[i]+(h sigma^2)/(2u[i]^2))x- (sigma^2) /(2 u[i]) E^(-((2 u[i])/sigma^2)x) cee[i][t];
  ss[i_,j_]:=(s[i,j][t]/.svals);
  
           (* The variables *)
           dvars = Join[Flatten[Table[With[{i=i}, {cee[i]'[t],F[i]'[t]}],{i, imin, imax}]],{err'[t]}];
           vars  = Join[Flatten[Table[With[{i=i}, {cee[i],F[i]}],{i, imin, imax}]],{err},
                        Flatten[Table[With[{i=i,j=j}, s[i,j]],{i, imin, imax}, {j, imin, imax}]]];
           (* The equations defining F[\[CenterDot]]'[t], cee[i]'[t] and err'[t] *)
           eqns = Join[
                       Flatten[
                         Table[                        
                          With[{i=i}, 
                               {(* The i \[Rule] beta[i] edge *)
                                If[beta[i]==i, \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(g[0, i, t]\)\)==If[\[Not]MemberQ[recurrent,i], -slack[i,i], 0], (* If beta[i] = i, g[0,i,t] = -U defines cee[i][t] add a possible slack for transient edges *)
                                   F[i]'[t]+\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(G[\(s[i, beta[i]]\)[t], i, t]\)\) - F[beta[i]]'[t] - \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(G[\(s[i, beta[i]]\)[t], beta[i], t]\)\)==If[\[Not]MemberQ[recurrent,i],slack[i,beta[i]], If[i\[DirectedEdge]beta[i] === badedge, err'[t],0]]],
                                (* The i \[Rule] tau[i] edge *)
                                If[tau[i]==i, \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(g[theta, i, t]\)\)==If[\[Not]MemberQ[recurrent,i],  slack[i,i],0], (* If tau[i] = i, g[theta,i,t] = M defines cee[i][t] add a possible slack for transient edges *)
                                   F[i]'[t]+\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(G[\(s[i, tau[i]]\)[t], i, t]\)\) - F[tau[i]]'[t] - \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(G[\(s[i, tau[i]]\)[t], tau[i], t]\)\)== If[\[Not]MemberQ[recurrent,i],slack[i,tau[i]], If[i\[DirectedEdge]tau[i] === badedge, err'[t],0]]]}],
                          {i, imin, imax}]],
                       {F[ifix]'[t]==0}]/.s[__]'[t]->0; (* By Leibnitz rule, the s[\[CenterDot]]'[t] values disappear *)
           (* Fix the initial values of the variables *)
           initconds = Join[Flatten[Table[With[{i=i}, {cee[i][gamma] == (cee[i][t]/.sol/.t->gamma),
                                                         F[i][gamma] == (F[i][t]/.sol/.t->gamma)}],
                                          {i, imin, imax}]],
                            {err[gamma] == eps},
                            Flatten[Table[With[{i=i,j=j}, s[i,j][gamma] == ss[i,j]],{i, imin, imax}, {j, imin, imax}]]];
           (* The equations defining s[i,j]'[t] *)                  
           seqns = Flatten[
                      Table[
                       With[{i=i,j=j}, 
                            If[i==j, s[i,j]'[t]==0, (* s[i,i][t] = 0 *)
                                                                      
                                     s[i,j]'[t]== (-\!\(\*SuperscriptBox[\(g\), 
TagBox[
RowBox[{"(", 
RowBox[{"0", ",", "0", ",", "1"}], ")"}],
Derivative],
MultilineFunction->None]\)[s[i,j][t],i,t]+\!\(\*SuperscriptBox[\(g\), 
TagBox[
RowBox[{"(", 
RowBox[{"0", ",", "0", ",", "1"}], ")"}],
Derivative],
MultilineFunction->None]\)[s[i,j][t],j,t])/(\!\(\*SuperscriptBox[\(g\), 
TagBox[
RowBox[{"(", 
RowBox[{"1", ",", "0", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[s[i,j][t],i,t]-\!\(\*SuperscriptBox[\(g\), 
TagBox[
RowBox[{"(", 
RowBox[{"1", ",", "0", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[s[i,j][t],j,t])+
                                                   If[0 <= ss[i,j] <= theta/.params,0,
                                                      -((-\!\(\*SuperscriptBox[\(g\), 
TagBox[
RowBox[{"(", 
RowBox[{"0", ",", "0", ",", "1"}], ")"}],
Derivative],
MultilineFunction->None]\)[s[i,j][t],i,t]+\!\(\*SuperscriptBox[\(g\), 
TagBox[
RowBox[{"(", 
RowBox[{"0", ",", "0", ",", "1"}], ")"}],
Derivative],
MultilineFunction->None]\)[s[i,j][t],j,t])/(\!\(\*SuperscriptBox[\(g\), 
TagBox[
RowBox[{"(", 
RowBox[{"1", ",", "0", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[s[i,j][t],i,t]-\!\(\*SuperscriptBox[\(g\), 
TagBox[
RowBox[{"(", 
RowBox[{"1", ",", "0", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[s[i,j][t],j,t]))toggless[i,j][t]]]], (* toggless[i,j][t] = 0 if 0 \[LessEqual] s[i,j][t] \[LessEqual] theta and 1 otherwise *)
                            {i, imin, imax}, {j, imin, imax}]]; 
           (* The discrete variables toggless[i,j] *)
           discretevars = Map[toggless@@#&,Cases[Tuples[Range[imin, imax], 2], {i_,j_}/;(\[Not](0 <= ss[i,j] <= theta/.params))]]; (* toggless for each i,j with ss[i,j] < 0 or ss[i,j] > theta *)
           (* Initial values for the discrete variables: The WhenEvent is triggered when the function values are equal. This should be based on the function values:
              If i > j, toggless[i,j] \[Equal] 1 if g[s[i,j],i,t] > g[s[i,j],j,t]
              If i < j, toggless[i,j] \[Equal] 1 if g[s[i,j],i,t] < g[s[i,j],j,t] *)
           initdiscrete = Flatten[Table[With[{i=i,j=j},
                                       If[0 <= ss[i,j] <= theta/.params, {}, 
                                          toggless[i,j][gamma] == If[((i>j) && (g[ss[i,j],i,t]>g[ss[i,j],j,t])/.sol/.rates/.params) || 
                                                                     ((i<j) && (g[ss[i,j],i,t]<g[ss[i,j],j,t])/.sol/.rates/.params), 1, 0]]],
                                       {i, imin, imax}, {j, imin, imax}]];                
           (* Events to watch for *)                                             
           whenevents =  Join[{WhenEvent[Evaluate[err[t]<=0],"StopIntegration"]}, (* Stop when the solution is feasible *)
                               (* Stop when the policy is no longer valid *)
                              Flatten[Table[With[{i=i, betai=beta[i]}, 
                                     If[betai==i, {}, (* Stop if s[i,beta[i]] falls below s[beta[i],beta[beta[i]]].  *)
                                               WhenEvent[Evaluate[\[Not](If[beta[betai]==betai,0, s[betai,beta[betai]][t]]<=s[i,betai][t])],{AppendTo[messages, {i,betai, beta[beta[i]]}],"StopIntegration"}]]],
                                     {i, imin, imax}]],                
                              Flatten[Table[With[{i=i, taui=tau[i]}, 
                                     If[taui==i, {}, (* Stop if s[i,tau[i]] is rises above  s[tau[i],tau[tau[i]]]  *)
                                               WhenEvent[Evaluate[\[Not](s[i,taui][t]<=If[tau[taui]==taui, theta/.params, s[taui,tau[taui]][t]])],{AppendTo[messages,{i,taui,tau[taui]}], "StopIntegration"}]]],
                                     {i, imin, imax}]],
                             (* Change toggless[i,j] to 0 when g[ss[i,j],i,t] = g[ss[i,j],j,t] *)
                               Flatten[Table[With[{i=i,j=j},
                                       If[0 <= ss[i,j] <= theta/.params, {}, 
                                          WhenEvent[Evaluate[g[s[i,j][t],i,t] == g[s[i,j][t],j,t]/.rates/.params], {toggless[i,j][t]->0}]]],
                                       {i, imin, imax}, {j, imin, imax}]],
                              (* When s[i,i+1] is too close to s[i+1,i], stop to recompute the slacks on transient modes *)          
                               Table[With[{i=i}, WhenEvent[Evaluate[s[i+1,i][t]-s[i,i+1][t] <= 10^-3], "StopIntegration", "LocationMethod"->"StepEnd"]],{i, imin, imax-1}]];                                     
                (* The slow step - solve the equations for the dvars to get the dbasis *)
                With[{dbasis = First[Solve[eqns, dvars]]}, 
                     (* Find slack values that work *)
                      With[{slackrules = If[Length[transient] > 0, NMinimize[Join[
                              (* Minimize the sum *)
                              {Plus@@Flatten[Table[With[{i=i}, {slack[i,beta[i]],slack[i,tau[i]]}],{i,transient}]]}, 
                              (* Subject to the conditions: 
                                 If s[i+1,i]-s[i,i+1] is "small", those two will not move closer together  *)
                              Flatten[Table[With[{i=i}, If[Chop[ss[i+1,i]-ss[i,i+1]- 10^-3] <= 0, s[i+1,i]'[t]-s[i,i+1]'[t]<=0/.(seqns/.Equal->Rule)/.dbasis/.svals/.sol/.rates/.params, {}]],{i, imin, imax-1}]],
                               (* and if  beta[i] = i or tau[i] = i, the corresponding slack = 0, otherwise, the slacks are non-negative *)
                              Flatten[Table[With[{i=i}, {slack[i,beta[i]]>= 0, slack[i,tau[i]] >= 0}],{i,transient}]]],
                              (* The variables are the slacks *)
                              Flatten[Table[With[{i=i}, {slack[i,beta[i]],slack[i,tau[i]]}],{i,transient}]]], 
                              {0,{slack[__]->0}}]}, 
                                  
                   step = 0;                         
                     (* Solve the differential equations *) 
                  With[{opts=SystemOptions[]},
                     SetSystemOptions["NDSolveOptions"->"DefaultSolveTimeConstraint"->10.`];                                     
                    
                          ndsol = First[NDSolve[Join[initconds, initdiscrete, dbasis/.Rule->Equal/.SetPrecision[Last[slackrules],\[Infinity]], seqns/.dbasis/.SetPrecision[Last[slackrules],\[Infinity]], whenevents]/.params/.rates, 
                                           Join[vars, discretevars],
                                           {t, -\[Infinity], gamma}, 
                                           DiscreteVariables->discretevars, AccuracyGoal->15, PrecisionGoal->15, 
(*MaxSteps->89,*)
                                           WorkingPrecision->precision, Method -> "StiffnessSwitching",StepMonitor:>(++step;dynrep = {Row[{"step =", step}],Row[{"t = ", t}],Row[{"err[t] = ", err[t]}]})]];
                              SetSystemOptions[opts];]]]]

Modified/Augmented for clarity and to provide answers: Implementing a more robust mechanism for addressing singularities yields a nice answer quickly. Streamlined and more fully explained code that solves and presents that solution is available at MoreDotsh@WolframCloud

$\endgroup$
5
  • 1
    $\begingroup$ If "Step 90 brings down the kernel" means running your code risks crashing the kernel, please add the tag crash. $\endgroup$
    – Michael E2
    Dec 19, 2021 at 19:45
  • $\begingroup$ @JV3 This code is too complicated, also it is not clear what do you try to solve. May by your problem has solution, may be not. In v.13 we have message NDSolve::mxst: Maximum number of 10000 steps reached at the point t == 1.94691171006169305012063940488022896405941766270024998036117764121095239773012521028646760223412280..` . What version do you run? $\endgroup$ Dec 20, 2021 at 4:59
  • $\begingroup$ Alex, yes, the code is complicated. Thanks for exploring. I'm running 12.2.0.0 on a Mac. Regarding the question of whether or not this has a solution: I implemented a more rigorous method for managing the singularities. Rather than use slacks, I insist s[i+1,i]'[t]=s[i,i+1]'[t] when the two are too close. It's more work to set up the problem, but NDSolve finds the answer I'm looking for in 6 steps at t = 1.94204726152797765349223005426381687735756642217697916595651890491415\ 63743742551 and err[t] = 4.665051739796352725978632985646871399975102*10^-37. Glad to know v.13 avoids the crash! $\endgroup$
    – user46831
    Dec 20, 2021 at 18:03
  • $\begingroup$ @JV3 What do you expect to get with this code? $\endgroup$ Dec 21, 2021 at 5:14
  • $\begingroup$ Alex, please see the link to MoreDotsh@WolframCloud. That more clearly explains both the problem and the answer it produces. Thanks! Looking forward to v13! $\endgroup$
    – user46831
    Dec 21, 2021 at 15:31

0

Your Answer

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

Browse other questions tagged or ask your own question.