Loops Not Working- Kernel Crashes and Values Incorrectly Updated

I haven't used Mathematica in a while so any help is very greatly appreciated. I am trying to run a program with nested for loops that runs two function evaluations in the inner most part of the loops.

I have two main problems:

1. When I try to run the loops it will start, but then there is a little pop noise a few seconds into running and it looks as if the kernel quits because all "ln[#]"s on the side revert to their pre-run values, ie. "ln[]" and all values calculated are deleted. If I remove the two functions (CalcPoints and ElectricIntensity)the loops can finish and all values in the resulting table are correct (it's just missing the last column).
2. The values for the fourth column are not correct when the two functions (CalcPoints and ElectricIntensity) are included in the innermost part of the loops. I'm not sure why this happens because the fourth column is updated by the "sio2" value which is not updated in the functions. I believe the CalcPoints function is the culprit here but I cannot see how to fix it. The value for the fourth column should be .5 for the first 121 iterations. (Note I had to abort the execution before the problem outlined in #1 happened in order to see this).

Below is my full code:

Functions:

CalcPoints[topgap_, basegap_, electrode_, sio2_] := {a1 = topgap/2,
a2 = basegap/2,
b1 = 0.25,
h = electrode,
b2 = sio2 - h + b1}

ElectricIntensity[a1_, a2_, a3_, b1_, b2_, h_] :=
Module[{}, \[CapitalOmega] =
RegionDifference[
RegionDifference[Rectangle[{-a3 - R, -R}, {a3 + R, b2 + R}],
Polygon[{{a1, b2}, {a2, b2}, {a2, b1}, {a3, b1}, {a3,
b1 + h}, {a2 + h, b1 + h}, {a2 + h, b2 + h}, {a1, b2 + h}}]],
Polygon[{{-a1, b2}, {-a2, b2}, {-a2, b1}, {-a3, b1}, {-a3,
b1 + h}, {-a2 - h, b1 + h}, {-a2 - h, b2 + h}, {-a1, b2 + h}}]];
Potential = NDSolveValue[{\!$$\*SubsuperscriptBox[\(\[Del]$$, $${x, y}$$, $$2$$]$$u[x, y]$$\) == 0,
DirichletCondition[u[x, y] == 0.5,
a1 <= x <= a2 && y == b2 || x == a2 && b1 <= y <= b2 ||
a2 <= x <= a3 && y == b1 || a1 <= x <= a2 + h && y == b2 + h ||
x == a2 + h && b1 + h <= y <= b2 + h ||
a2 + h <= x <= a3 && y == b1 + h ||
x == a1 && b2 <= y <= b2 + h || x == a3 && b1 <= y <= b1 + h],
DirichletCondition[
u[x, y] == -0.5, -a2 <= x <= -a1 && y == b2 ||
x == -a2 && b1 <= y <= b2 || -a3 <= x <= a2 - 3 &&
y == b1 || -a2 - h <= x <= -a1 &&
y == b2 + h || -a2 - h == x &&
b1 + h <= y <= b2 + h || -a2 - h <= x <= -a3 && y == b1 + h ||
x == -a1 && b2 <= y <= b2 + h ||
x == -a3 && b1 <= y <= b1 + h]},
u, {x, y} \[Element] \[CapitalOmega]];
Derivative[1, 0][Potential][0, 0.25]]

Loops:

results = Table[Table[{}, 6], 2662];

baserange = {3, 8};
basemax = baserange[];
basemin = baserange[];
basediff = basemax - basemin;

iter = 1;
For [ g = 0; electrode = 0; sio2min = 0, g < 2, g++,
electrode = If [g == 0, 0.1, If[g == 1, 0.29, -1000000]];
sio2min =
If[electrode == 0.1, 0.5, If[electrode == 0.29, 0.7, -10000000]];
For[h = 0; sio2max = 0; sio2minval = 0; sio2diff = 0; sio2 = 0,
h < 11, h++,
For[k = 0; basegap = 0, k < 11, k++,
For[j = 0; topmax = 0; topmin = 0; topdiff = 0; topgap = 0, j < 11,
j++,
results[[iter, 1]] = iter;
results[[iter, 5]] = electrode;

sio2range = {sio2min, 2};
sio2max = sio2range[];
sio2minval = sio2range[];
sio2diff = sio2max - sio2minval;
sio2 = sio2minval + 0.1*h*sio2diff;
results[[iter, 4]] = sio2;

basegap = basemin + 0.1*k*basediff;
results[[iter, 2]] = basegap;

toprange = {0.5, basegap - 1};
topmax = toprange[];
topmin = toprange[];
topdiff = topmax - topmin;
topgap = topmin + 0.1*j*topdiff;
results[[iter, 3]] = topgap;

R = 10;
a3 = 3;
{a1, a2, b1, h, b2} = CalcPoints[topgap, basegap, electrode, sio2];

results[[iter, 6]] = ElectricIntensity[a1, a2, a3, b1, b2, h];

iter = iter + 1;

]
]
]
]
Export["myresults.csv", results];

Thank you!

• Kernel crash reproduced on 12.3.1 for Mac OS X ARM (64-bit) (July 8, 2021). Nov 17 '21 at 18:27
• @RohitNamjoshi Thanks for your response! Do you have any idea how to fix it? Nov 17 '21 at 18:36
• Unfortunately, I do not. Code that crashes the kernel is a bug. You should report it to Wolfram Support. If you have a service subscription then here, otherwise here. Nov 17 '21 at 19:49