During Monte-Carlo (MC) simulations on a latice of dimensions $(L_1,L_2,\ldots,L_D)$, all the site varibales are often stored as a $D$ dimensional array array
. To lookup a site varibale, one just needs the $D$ dim. coord coord
.
For e.g. on a 2 D lattice, a 2 D array may be made where each element is some site variable.(the dimensionalty or nature of the site variable itself is irrelevant). To lookup a var at coord={x,y}
one uses, Extract[array,coord]
Often the simulation is performed in sweeps. Each sweep comprise of $N=L_1L_2\ldots L_D$ random lookups on the array.
This is a lot of lookups, especially when a simulation may have a large no of sweeps.
Since memory is ultimately stored as a sequential flat array, it may be better to perform lookups on a flat array instead.
flatArray=Flatten@array
Now a lookup is simpler
flatArray[position]
As before $N$ random positions are generated per sweep.
- Is this correct? Are flat array lookups faster than nested ones? (for the test case take $D=3$ on a $48\times 48\times48$ lattice)
However, for the flatened array, the lattice structure is lost. To perform calculations which need that structure, one needs the site's coordinate in the lattice. Even though this can be computed every time, one might as well do a lookup instead.
allCords=Flatten[Array[{##}&,{L1,L2,...,LD}],D-1]
toCoord[pos_]:=allCoords[[pos]]
Is it correct to assume that coord compute is slower than coord lookup? Keep in mind that the compute is for a general $D$.
With this requirement of needing the lattice coord too (via compute or lookup), is the answer to 1. still the same?
Assume in the above that memory is cheap but time expensive.