# Mathematica clears all definitions or crashes with the following code, can anyone reproduce the bug? [closed]

I am working on a simple code, however Mathematica crashes or clears all definitions each time I will try to do anything with matrix B. Even when I just print it out. The code producing the problem is:

ClearAll["Global*"]
Cs = With[{aT = {0., 98., 201., 316., 428., 571.},
Cp = {917., 978., 1028., 1078., 1133., 1230.}},
Interpolation[Transpose@{aT, Cp}]];

Capp[T_] :=
With[{T1 = 582., T2 = 652., Tm = 617., A = 11371.42},
A Cos[\[Pi] (T - Tm)/(T2 - T1)]^2]

Cn[T_] :=
With[{C1 = 1180., T1 = 582., T2 = 652., Tm = 617.},
Cs[T] + 0.5 (C1 - Cs[T]) (1. + Tanh[8. (T - Tm)/(T2 - T1)])]
Ct[T_] :=
With[{T1 = 582., T2 = 652.},
Piecewise[{{Cn[#], # <= T1}, {Cn[#] +
Capp[#], # >= T1 && # <= T2}, {Cn[#], True}}]] & /@ T

\[Rho] = With[{T = {0., 98., 201., 316., 428., 571., 600., 610.,
720.}, \[Rho] = {2705., 2685., 2670., 2640., 2620., 2575.,
2550., 2375., 2300.}},
Interpolation[Transpose@{T, \[Rho]}, InterpolationOrder -> 1]];

k = With[{T = {0., 98., 201., 316., 428., 571., 600., 700., 800.},
k = {162., 177., 192., 207., 223., 253., 210., 90., 100.}},
Interpolation[Transpose@{T, k}, InterpolationOrder -> 1]];

ra[T_] := 1./(\[Rho][T] Ct[T])

L = 2.; (*domain length*)
Ts = 50.; (*simulation time*)
Tm = 700.; (*max temperature*)
a = 3.;
T[t_, x_] := 0.5 (Tm Exp[-a x] Cos[t - a x] + Tm)
Plot[Evaluate@Table[T[t, x], {t, 0, Ts, Ts/10}], {x, 0, L},
PlotRange -> All, AxesOrigin -> {0, 0}]
(*
Plot[T[t,0],{t,0,Ts},PlotRange\[Rule]All,AxesOrigin\[Rule]{0,0}]
Plot[T[t,L],{t,0,Ts},PlotRange\[Rule]All,AxesOrigin\[Rule]{0,0}]
*)
Plot[{T[t, 0], T[t, L]}, {t, 0, Ts}, PlotRange -> All,
AxesOrigin -> {0, 0}]
Plot[T[0, x], {x, 0, L}, PlotRange -> All, AxesOrigin -> {0, 0}]

kxx[x_] := k[x]

\[Phi][t_, x_] =
Simplify[D[T[t, x], t] -
ra[T[t, x]] D[kxx[T[t, x]] D[T[t, x], x], x]];
eq = D[u[t, x], t] -
ra[u[t, x]] D[kxx[u[t, x]] D[u[t, x], x], x] == \[Phi][t, x];

maxIter = 50;

Nx = 21;
Nt = 100;
dt = Ts/Nt;
dx = L/(Nx - 1);
\[Omega] = 0.7;
\[Theta] = 0.50;
\[Epsilon]T = 0.001;

X = Join[Range[0, L/2, L/2/IntegerPart@(4/5 Nx)],
2^Table[i, {i, Log[2., L/2 + dx],
1, (1 - Log[2., L/2 + dx])/(IntegerPart@(Nx/5) - 1)}]];
DX = Differences[X];
T0 = T[0, X];

A = ConstantArray[0, {Nx, Nx}];
A[[1, 1]] = 0.5 \[Omega] DX[[1]];
A[[1, 2]] = 0.5 (1. - \[Omega]) DX[[1]];
For[i = 2, i <= Nx - 1, i++,
A[[i, i - 1]] = 0.5 (1. - \[Omega]) DX[[i]];
A[[i, i]] = \[Omega] DX[[i]];
A[[i, i + 1]] = 0.5 (1. - \[Omega]) DX[[i]];
];
A[[Nx, Nx - 1]] = 0.5 (1. - \[Omega]) DX[[-1]];
A[[Nx, Nx]] = 0.5 \[Omega] DX[[-1]];
A = SparseArray[A];

Kxx = kxx[T0]*ra[T0];
avgK = 0.5 Table[Kxx[[i]] + Kxx[[i + 1]], {i, Nx - 1}];

B = ConstantArray[0, {Nx, Nx}];
B[[1, 1]] = avgK[[1]]/DX[[1]]; B[[1, 2]] = -avgK[[1]]/DX[[1]];
For[i = 2, i <= Nx - 1, i++,
B[[i, i - 1]] = -avgK[[i - 1]]/DX[[1]];
B[[i, i]] = (avgK[[i - 1]] + avgK[[i]])/DX[[i]];
B[[i, i + 1]] = -avgK[[i]]/DX[[i]];];
B[[Nx, Nx - 1]] = -avgK[[Nx - 1]]/DX[[-1]];
B[[Nx, Nx]] = avgK[[Nx - 1]]/DX[[-1]];
B = SparseArray[B];


Now it is enough to write B in the cell and hit Shift+Enter to get a crash or clear all the variables. Could anyone tell me if the same happens in case of your environment? My is Windows 10, Mathematica 12.

When the code is run as a WLS it does not happen.

• Is every single line of your code necessary to reproduce the behiavour? If not, could you simplify it as much as possible (without changing the behaviour of course)? – anderstood Jul 24 '19 at 9:42
• @anderstood unfortunately yes, all the code causes such behavior. If I remove some parts, it does not happen. – Misery Jul 24 '19 at 9:44
• Unix and version 11.3 here, code runs without crashing the kernel and manipulating matrix B` does not seem to cause problems. – Kiro Jul 24 '19 at 10:04
• Works for me in version 12.0 on Windows 10 32-bit, producing B and several errors, e.g. "InterpolatingFunction::dmval: Input value {700.} lies outside the range of data in the interpolating function. Extrapolation will be used.". – user64494 Jul 24 '19 at 12:40
• Maybe a memory issue? – Daniel Lichtblau Jul 30 '19 at 16:25