# Mathematica crash every time when I run this loop [closed]

I have written a simulation for a percolation test. Then, I am using another notebook to control this simulation to generate 1000 results with a different random seed for each one of them.

The control notebook:

λ = 0.15;
shh = {};
nC := Ceiling[(nA + nB)/2]
v21 := NotebookEvaluate[$UserDocumentsDirectory <> "/V21.nb"]; lengtshh := Total[Map[ArcLength, Map[Line, dum]]] Do[nA = Round[5.7/λ^2*0.5]; nB = Round[5.7/λ^2*1.5]; Do[n := nC; n3 = NotebookEvaluate[$UserDocumentsDirectory <> "/V21.nb"];
If[n3 == "Percolated", nB = nC, nA = nC];
If[nB - nA == 1 && n3 == "Percolated",
shh = AppendTo[shh, {n, lengtshh}] && Break[]];
If[nB - nA == 1 && n3 == "not Percolated",
n = n + 1 && v21 && shh =
AppendTo[shh, {n, lengtshh}] &&
Break[]], ∞], {α, 1, 1000}] // AbsoluteTiming

The structure of control notebook is a loop inside a loop. The inside loop is called binary chop. The outside loop is for repeat v21.nb simulation 1000 times with different seed random.

I am using Mathematica 11.3 on windows 10. This code works very well on my Mac with Mathematica 11.0.

## closed as off-topic by Daniel Lichtblau, Henrik Schumacher, m_goldberg, MarcoB, xzczdApr 7 at 7:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Daniel Lichtblau, Henrik Schumacher, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.

• You seem to use && both as a logical And and as a statement combinator. The latter use is incorrect though, and may be leading to some strange behavior. In those cases, you should use ; instead. Furthermore, AppendTo already has the side effect of modifying its first argument; you seem to use it with syntax more appropriate to Append instead. – MarcoB Apr 6 at 4:16
• Also, don't use x == "somestring" as it may not evaluate (== is for constructing equations). Use x === "somestring"]. Finally, it is not good practice to programmatically evaluate notebooks like this, and it very well might be the cause of the crash. Notebooks are for interactive work. You should be defining a function here and calling it. If you want to programmatically evaluate code you depend on, put the code in an .m file, not a notebook. Even then, don't keep loading it in a loop, instead wrap it up in a function and call the function. – Szabolcs Apr 6 at 7:07
• In addition to the code issues raised by others, this appears not to be the complete code (which was also noted). It is not possible to address this in absence of complete code. – Daniel Lichtblau Apr 6 at 13:55
• I'm voting to close this question as off-topic because it is too localized; i.e, the issues it raises are limited to the situation and needs of its poster. Answers will not benefit others. – m_goldberg Apr 6 at 23:26
• Based on all suggestion and answers. I have solved the problem. I think the problem is caused by NotebookEvaluate[]. Now I am using m.file. You guys are amazing! – Yubao Deng Apr 7 at 1:13

This is not a complete answer. There is no way I can debug the code you post which is incomplete. But I can point out some problems I see in your code.

• correct what to appear to obvious bugs
λ = 0.15;
shh = {};
nC := Ceiling[(nA + nB)/2] (* how will na and nb get values? *)
v21 := NotebookEvaluate[$UserDocumentsDirectory <> "/V21.nb"]; lengtshh := Total[Map[ArcLength, Map[Line, dum]]] (* how will dum get a value? *) With[{max = 10, αmax = 2}, (* work with small nubers for debugging *) Module[{nA, nB, n3, n}, (* localize auxiliary variables *) Do[ nA = Round[5.7/λ^2*0.5]; nB = Round[5.7/λ^2*1.5]; Do[ n := nC; (* this looks it might be a bug; maybe = intended?*) n3 = NotebookEvaluate[$UserDocumentsDirectory <> "/V21.nb"];
If[n3 == "Percolated", nB = nC, nA = nC];
If[nB - nA == 1 && n3 == "Percolated",
shh = AppendTo[shh, {n, lengtshh}]; Break[]];
If[nB - nA == 1 && n3 == "not Percolated",
n = n + 1; v21; shh = AppendTo[shh, {n, lengtshh}]; Break[]],
i],
{α, αmax}]]]

The is no guarantee that this will fix all your problems. I suspect it won't. but I hope this helps you to move forward with your code.

Caution — this code has not been tested.