# NDSolve throws Dot::dotsh error

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?

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.

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.0180.,kvminus->0.0180.},
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.00094880200000000000041394665473148961609695106744766235351562580.,k[1,3]->0.0031988200000000000054051207953875746170524507761001586914062580.,k[1,4]->0.00186111999999999996185939821202737221028655767440795898437580.,k[1,5]->0.004339109640000000007172638229885341942804188875015825033187866210937580.,k[1,6]->4.872639999999999988551570989647743203931895550340414047241210937580.*^-6,k[1,7]->0.001289485639999999987515639978535864074160599557217210531234741210937580.,k[1,8]->0.00124451699999999999737138045574624811706598848104476928710937580.,k[1,9]->0.00049485399999999997531358042479610048758331686258316040039062580.,k[1,10]->0.002213143640000000040287482429984811105327935365494340658187866210937580.,k[2,1]->0.0047339680000000000734822203085627734253648668527603149414062580.,k[2,2]->0,k[2,3]->0.003693323400000000174183550916140461595205124467611312866210937580.,k[2,4]->0.00305935299999999989889465368264609423931688070297241210937580.,k[2,5]->0.00395957299999999997718730382345597718085628002882003784179687580.,k[2,6]->0.000054593399999999998092187203369007875153329223394393920898437580.,k[2,7]->0.0009099489999999999575303055721064993122126907110214233398437580.,k[2,8]->0.0002957149999999999969574338010147585009690374135971069335937580.,k[2,9]->0.001099183000000000104412478663107322063297033309936523437580.,k[2,10]->0.00183360700000000001030214802355544634338002651929855346679687580.,k[3,1]->0.0050682139999999997054258571438367653172463178634643554687580.,k[3,2]->0.00235358000000000008172795773475627356674522161483764648437580.,k[3,3]->0,k[3,4]->0.003919768999999999597311228782814396254252642393112182617187580.,k[3,5]->0.00493165499999999995799776497662492147355806082487106323242187580.,k[3,6]->0.0005974179999999999508136783177292272739578038454055786132812580.,k[3,7]->0.0018820309999999999383407667252754436049144715070724487304687580.,k[3,8]->0.0026492950000000000786853915357710320677142590284347534179687580.,k[3,9]->0.001959598999999999802829053763275624078232795000076293945312580.,k[3,10]->0.00280568899999999999111260917672439063608180731534957885742187580.,k[4,1]->0.00446936200000000016102630340242285456042736768722534179687580.,k[4,2]->0.0018300910000000000527747845424642036959994584321975708007812580.,k[4,3]->0.0013377000000000000435457225833602024067658931016921997070312580.,k[4,4]->0,k[4,5]->0.00369496700000000006473138691731605831591878086328506469726562580.,k[4,6]->0.00019170400000000000634292618428844434674829244613647460937580.,k[4,7]->0.000645343000000000045074388665966580447275191545486450195312580.,k[4,8]->0.00212580600000000004973221834347896219696849584579467773437580.,k[4,9]->0.00289510899999999987697307801681745331734418869018554687580.,k[4,10]->0.00156900100000000009784623111741552747844252735376358032226562580.,k[5,1]->0.00431008300000000014323697783424904628191143274307250976562580.,k[5,2]->0.0038005399999999999024937608060881188976054545491933822631835937580.,k[5,3]->0.0035317989999999998271869999788563632137083914130926132202148437580.,k[5,4]->0.0021940989999999997836412773954961608069424983114004135131835937580.,k[5,5]->0,k[5,6]->0.0017135269999999999267128319990760587643308099359273910522460937580.,k[5,7]->0.0028394419999999998287156660614627412542176898568868637084960937580.,k[5,8]->0.0040962549999999998994511946071028773985744919627904891967773437580.,k[5,9]->0.0002339289999999999891591023759573886309226509183645248413085937580.,k[5,10]->0.0037630999999999998814875085129116882853850256651639938354492187580.,k[6,1]->0.00477198200000000013337730919715795607771724462509155273437580.,k[6,2]->0.002213744000000000128336452576149895321577787399291992187580.,k[6,3]->0.003638730000000000176091363712771453720051795244216918945312580.,k[6,4]->0.004663574999999999665112326852067781146615743637084960937580.,k[6,5]->0.00433423700000000000718408665889569419960025697946548461914062580.,k[6,6]->0,k[6,7]->0.001284612999999999987527088407546216330956667661666870117187580.,k[6,8]->0.0025094590000000001252938863771646538225468248128890991210937580.,k[6,9]->0.00270340499999999987063015183252900897059589624404907226562580.,k[6,10]->0.00220827100000000004029893085899516336212400346994400024414062580.,k[7,1]->0.003824019000000000115951914736456274113152176141738891601562580.,k[7,2]->0.00477282100000000011636586139118776372924912720918655395507812580.,k[7,3]->0.006426971000000000072693850938776449766010046005249023437580.,k[7,4]->0.0052437229999999998032982756468456386755860876291990280151367187580.,k[7,5]->0.00304962400000000001965699825134947786864358931779861450195312580.,k[7,6]->0.003828891640000000115940466307445921856356108037289232015609741210937580.,k[7,7]->0,k[7,8]->0.00506853600000000011332329519220252223021816462278366088867187580.,k[7,9]->0.0032835530000000000088161006273068664995662402361631393432617187580.,k[7,10]->0.00092365800000000005277184245144894703116733580827713012695312580.,k[8,1]->0.0056655630000000001961213413892437529284507036209106445312580.,k[8,2]->0.005589178999999999675196704629343003034591674804687580.,k[8,3]->0.006551055000000000072546413321106228977441787719726562580.,k[8,4]->0.00592762799999999989014609624859986070077866315841674804687580.,k[8,5]->0.0065064079999999999831539199135477247182279825210571289062580.,k[8,6]->0.005643772399999999673288891832712010909745004028081893920898437580.,k[8,7]->0.003764800000000000004124478536482456547673791646957397460937580.,k[8,8]->0,k[8,9]->0.00521086000000000031801006272758058912586420774459838867187580.,k[8,10]->0.00468845800000000005689632098793140357884112745523452758789062580.,k[9,1]->0.00531298199999999994774801947983178251888602972030639648437580.,k[9,2]->0.00356661099999999991333465843013073026668280363082885742187580.,k[9,3]->0.0032978699999999998380278976028989745827857404947280883789062580.,k[9,4]->0.00196016999999999979448217501953877217601984739303588867187580.,k[9,5]->0.00565513699999999985921356193685483049193862825632095336914062580.,k[9,6]->0.001479597999999999937553729623118670133408159017562866210937580.,k[9,7]->0.002605512999999999839556563685505352623295038938522338867187580.,k[9,8]->0.0038623259999999999102920922311454887676518410444259643554687580.,k[9,9]->0,k[9,10]->0.00352917099999999989232840613695429965446237474679946899414062580.,k[10,1]->0.003774684999999999845954334887210279703140258789062580.,k[10,2]->0.00472348699999999984636828154194176931923720985651016235351562580.,k[10,3]->0.0056577649999999997940721557787568940511846449226140975952148437580.,k[10,4]->0.0043200649999999997505264331953966916444187518209218978881835937580.,k[10,5]->0.00212596599999999996688515579990053083747625350952148437580.,k[10,6]->0.003779557639999999845942886458199927446344190684612840414047241210937580.,k[10,7]->0.00428569300000000014128076486485952045768499374389648437580.,k[10,8]->0.00501920199999999984332571534295652782020624727010726928710937580.,k[10,9]->0.0023598949999999999560442581758579194683989044278860092163085937580.,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},
sol = {cee[4][t]->-3.30279179481928159699375491849003095522634984712162816066134399906223853161023826849617391602550778.53558590114208,F[4][t]->0,cee[5][t]->-2.0101527825051377519313305168458475350533542362286896196159741458162852787641916913348715932589378.0817234313792,F[5][t]->1.34340162571299100540616520225030547809443534618946665459914349134189984326152392485654132214277478.70546098109291,cee[6][t]->-2.15884142381742344592902169744401661574071480261882319758212863678599774648939237081443386151386877.95959345812315,F[6][t]->1.42779423253232511300285823602455263505629764484978953881617189611225711435106951363482465534397878.68984088472966,cee[7][t]->-2.49427502350077431138347881903197121760896488926084382674600883689101323122035476445427969410539478.63841490082763,F[7][t]->1.46996472819130567010414411492060212461375275153659652989585431167022268524343557219811494812410478.60625348577585,cee[8][t]->-2.65931290288134288824927295815438808933449617780376828702047018630600880671725652582159281114718977.78647627276752,F[8][t]->1.49733664867249428629401935261904052333751427321806321539047594170016368862366320591946670423336278.67386581427606,cee[9][t]->-3.25118553287792848326543075563070790134788859584721540123534082310365883487123882345357897988577779.46228914704172,F[9][t]->1.52120623646967429670532841217874685770995638756712065070719057807283957810262075927928437744076478.65105506897532,t->1.94698426302342786612344604504566321078310876677772320988272658482927067896991058762863183908630480.},
gamma = 1.94698426302342760454952349560479422761651038638335272514780094370167927463069988293565864512606580.,
eps = 0.00246749749502192111811956333770839705841198962174692530933157900323226172896318103828712445951375.11118243564009,
svals = {s[4,4][t]->0,s[4,5][t]->0.19008786518730607934777489694402407876459572798379939170741255890000248315976806523617505379649778.12734067841095,s[4,6][t]->0.19006751132953662399851348211911164068115109840072385291725202954023043051910153267739838583223378.1291965639342,s[4,7][t]->0.19006696159276958808176770358433015490031195171977821008665759338667173556787894415301847936066175.58177481037254,s[4,8][t]->0.13552132267487163805797868020020046431689556545447396145041967778513078021938012096863939975735577.990594202613,s[4,9][t]->0.11189722157178950088511651137759087960864784114390307787636053377599949830194560869712678413025577.90185067358271,s[5,4][t]->0.19086046318242569205071636167723747842364997183260850887191825218195541810728803974130546864619578.13562084908294,s[5,5][t]->0,s[5,6][t]->0.19002958803219675106502775176341365638932472381528771127286704747831851383493810134762264746790478.13203667868355,s[5,7][t]->0.19004707900632172176047093969603310167816355219893957016671154508533839662218259748736002573830475.30221419722788,s[5,8][t]->0.11882653200188755977287132036036373206396425791470125518465379906780172644412103549164019662433977.9352850479685,s[5,9][t]->0.09669010200706394363463693768638159919458925345405823527056043677756921375893040979933816763750677.69491729152642,s[6,4][t]->0.19088054630830237175987671250160443004892325116609149126967656334366816700106645555535474052175678.13477020581733,s[6,5][t]->0.19091799225870940340155820363071079954087102904287108709173454746014766429903708395252424012858378.13291770883845,s[6,6][t]->0,s[6,7][t]->0.19006531681321685049759969313661877154843577737023149096692159664034437321744730813532436122447274.98728997313931,s[6,8][t]->0.10116397961130774514309648010177167633676870404289864750078565757429678714606414050781613142397877.86728823527966,s[6,9][t]->0.08435712422597229219765294690984513756033116515649083939353265805136436230150162226531593440633177.54191796970049,s[7,4][t]->0.19088087363085373204959560936850354263150782191229142609532019107031715221130177478258268968121875.58333963097162,s[7,5][t]->0.19090030676774181132736497655991115343819928553393748373545701818908852216983013373941657818094775.30459181724046,s[7,6][t]->0.19088185401986224686645545917273537961624594548475177433761920468658510573038117351545049014096974.98992804400359,s[7,7][t]->0,s[7,8][t]->0.05720222488874646669635671937647099059646881687573301819319970170856587734135743382883019220179676.67575320659363,s[7,9][t]->0.06506574897756929799185005013649007655090265550993202123859042134953341353563177228148236089124978.09375472812498,s[8,4][t]->0.22981336711426649529980559320839547253524131735590779627451281711616894397861277007719281334979778.21025950729067,s[8,5][t]->0.2409063322811747474465611256698046435161881795821361415188220402826146087762373342143472989701778.22960944629526,s[8,6][t]->0.25149003649338278545884651004502636887548356494328687686435034369976819957143634388086717287295378.24723110280743,s[8,7][t]->0.27376948560424795788146666620334788342228860848966784640747700193763723205721944099054700718894477.7253633846222,s[8,8][t]->0,s[8,9][t]->0.0690952481294964975799943195379451590068237806358148206822235427278750369594438794266943876199477.09301828677526,s[9,4][t]->0.23420449312691774969559311068468943700827203026029552246816171888258617749607314893549706782760578.02259986803554,s[9,5][t]->0.24196930683214024665999128724471764443749734424337881937639313490192143438444472942953334989233678.0100343883893,s[9,6][t]->0.24767263209630914355443879352131347723764686662304358758390293753519724506366766087139527615094678.00222133282291,s[9,7][t]->0.25570778734491188840105880647930498172024415535814610671700531261595642690725886461729338103250978.66066002903834,s[9,8][t]->0.24351794868005665372334608610225894091073980545636768142861304230429858618220829108555049033699677.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

• If "Step 90 brings down the kernel" means running your code risks crashing the kernel, please add the tag crash. Dec 19 '21 at 19:45
• @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? Dec 20 '21 at 4:59
• 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!
– JV3
Dec 20 '21 at 18:03
• @JV3 What do you expect to get with this code? Dec 21 '21 at 5:14
• 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!
– JV3
Dec 21 '21 at 15:31