The situation is that I have a loop that generates a sequence of numbers. But the numbers are odd, likely wrong. But what really boggles the mind is if I remove the loop and just run the body of the loop once, it's all fine! This happens both inside a Do and inside a Table.
I tried to reduce the problem as much as possible into a working example but couldn't do much. Honestly I'm not hoping for someone to help me with my specific code but how to deal with these problems in general. If I had some way to know what the code is doing that would be nice. Trace and TracePrint generate so much output that it usually freezes and crashes before it's done. Anyway, here's the bit of code:
therm = 50;
iters = 100;
AllEs = Table[
conf = Hotconf;
Do[conf = MCStep[conf, b], {therm}];
es = Table[conf = MCStep[conf, b]; CalcE[conf], {iters}];
Total[es]/(iters*L*L)
, {b, 0, 1, 0.1}]
Not shown here are the functions Hotconf, MCStep and CalcE. But they were thoroughly tested and work as intended. Hotconf and MCStep returns an LxL matrix, while CalcE returns a number. The output of the code above is something like {1., 1., 1., 1., 1., 1., 1., 0.6, 0.6, 1., 1.}. There is a randomness involved (this is monte carlo) but it's always very near one. So, what happens if you run the body of the loop separately? For example
conf = Hotconf;
Do[conf = MCStep[conf, 0.1], {50}];
es = Table[conf = MCStep[conf, 0.1]; CalcE[conf], {100}];
Total[es]/(100*L*L)
This is equivalent to b=0.1 from the previous code, the second number from the previous output. The output now is 0.0784 (it oscillates randomly but it's always below 0.1).
My gut tells me this is a problem of evaluation so I sprinkled "Evaluate" all over the place, but that made no difference.
I could really use some help. Not because of this code specifically, but I have a lot of work relying on thousands of Mathematica lines of code running complex algorithms. Now I just noticed how easy it is for something wrong to go under the radar with no error messages or anything, and it's quite scary! I'm glad I didn't publish anything based on that yet because I am having difficulty trusting the code now.