Hi lmd141,
I don't see an immediate answer, so I will provide these trivial comments instead:
int new_atom=n_atoms+1;
This nearly confused me, because the variables names are so close. Try to avoid doing this, it will save you one day.
nreject = nreject + 1;
can be written
nreject++;
because it is an int. If it were a double you can do this
MyDouble += 1.0;
or with another variable
MyDouble += AnotherDbl;
I tend to name my variables like this
ParticleEnergy
- no underscores and capitalise the first letter of each word. That is my preference, just putting the idea out there.
for (int i = 0; i <= n_atoms-1; i++)
can be written:
for (int i = 0; i < n_atoms; i++)
With these parts:
if (random1 < acceptance_insert)
if (r>0)
Doubles are stored as binary fractions, and cannot represent al real numbers, so comparisons like that often fail. Consider this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <cmath>
#include <limits>
float a = 0.1; //a == 0.0999997
float b = 10 * a; //b == 0.9999997
if ( b == 1.0){} //false
if ((1.0 - b) == 0.0){} //false
if((1.0 -b) < 0.0){} //false
if((1.0 - b) > 0.0){} //true but not what you want
MyEpsilon = std::numeric_limits<float>epsilon;
if(abs(1.0 - b) < MyEpsilon){} // effective equality comparison with zero
double c = 1000.0;
//scale epsilon up to the number
MyDblEpsilon = c* std::numeric_limits<double>epsilon; //only good for values from 1.0e3 to < 1.0e4
double d = 2 * 500.0;
if (c == d){} //fail
if(abs(c-d )< MyDblEpsilon){} // effective equality comparison betwen variables c and d
|
The value of epsilon needs to be scaled up because you need to deal with the 'distance' between representable numbers which varies across the range of floating point values.
Changing the type to double doesn't fix the problem.
You can do something similar for < and > comparisons - I am sure you can figure that out.
Hopefully this won't be confused with your global variable epsilon.
This whole thing could be the cause of your problem in the if statement in the MCinsert and LJ_pot functions. If it isn't, then what I have mentioned is better practice anyway.
Finally, the way to solve these problems properly is to use the debugger. If you have an IDE it should be easy to use. You can set break points, have a watch list of variable values,step through the code 1 line at a time. See how the values change, and how that affects the logic.
Hope all this helps. Good luck !!