==============================================================================
         ----------------------------------
         Demonstration code using Panphasia
         ----------------------------------

Consisting of:

This REAMDE file, makefile, main.f ../src/panphasia_routines.f  ../src/generic_lecuyer.f90

See arXiv:1306.5771 for the documentation about the subroutines and an
overview of the demonstrations.


Demo subroutines
-------------------  

 There are 5 demo subroutines which can be called from main.f. They need
to be uncommented individually to run and the executable remade. They
are provided purely for illustration.

(0)  make_descriptor

 The software requires the user to provide a descriptor, which specifies the
location within Panphasia that the phases are taken from.  The desciptors
have an error checking digit within them, which is non-trivial to compute
by hand. The subroutine generates new descriptors including the check digit.
We recommend using this generator to choose the phases for new volumes.
 
 These descriptors are selected `randomly' from Panphasia volume.  This
routine generates descriptors only for cubic volumes.  It selects an
appropriate level in the octree based on user supplied information.
We recommend using this routine as it makes it unlikely that someone
else will be running a simulation from the same region in Panphasia.
The generator assumes Panphasia is 25000 Gpc/h on a side, which is
big enough to make this kind of coincidence rare. 


(1)  Subroutine check_moments
-------------------------

 As explained in Jenkins 2013 (arXiv:1306.5968) the octree basis functions
have zero first and second moments. It follows that if the first and second
moments of a region of Panphasia made of whole octree cells is computed, then
the value of the moments is independent of the level of the octree at which
Panphasia is evaluated.   This demo code checks the is actually true for
regions given by the user.

 The demo code check_moments shows this for particular cases, for a user defined
cuboidal domain.

The subroutine requires command line arguments:
./demo_code.x
  
Usage: test_code.x lev xorigin yorigin zorigin
 xperiod yperiod,zperiod xrel yrel zrel nx, ny, nz

The code calls the set_local_box subroutine.

 'lev' is the level of the octree. 'xorigin','yorigin','zorigin'
define the origin of the domain of interest. This domain is periodic
in the x,y,z directions with period given by
'xperiod','yperiod','zperiod'.  Within the domain the origin of a
subdomain is defined by the coordinates (xrel,yrel,zrel).  The moments
are calculated by summing the cuboid of side lengths nx,ny,nz, with
(xrel,yrel,zrel) being the lowest corner of the cuboid. The sizes and
variance of the cells are scaled appropriately with level in the
octree when computing the moments so that one can make valid
comparisons between levels.


 For example:

./demo_code.x 40 200 400 800 1 1 1 0 0 0 1 1 1
----------------------------------------------------------
 Successfully initialised Panphasia box at level           40
 Abs origin: (         200,         400,         800)
 Rel origin: (           0,           0,           0)
 Periods   : (           1,           1,           1)
 Phases used: [Panph1,L40,(200,400,800),S1,CH-999,XXXXXXXX]
 ----------------------------------------------------------
 -----------------------------------------------------
Moment: I(000) =     0.40263830399469E-18
Moment: I(001) =    -0.21622221261036E-30
Moment: I(010) =    -0.29391437181154E-31
Moment: I(011) =     0.91428635240100E-43
Moment: I(100) =    -0.23078445689585E-30
Moment: I(101) =     0.83196847010331E-43
Moment: I(110) =    -0.36947830720766E-43
Moment: I(111) =    -0.57205869835501E-57
 -----------------------------------------------------
 RMS averages =   0.4642104       1.013437      0.1290658      0.9494902    
                  0.6179731       1.529195       1.391514      3.6442794E-02
                     1  cells in total.
 Time for summation:   9.9899992E-04 seconds.


In this example the eight moments are given for a single cell
at level 40 in the octree.


 The moments for the same physical volume can instead by evaluated
at level 44 by summing over the (2**4)**3 = 4096 cells which comprise
the single cell at level 40 - as follows:

./demo_code.x 44 3200 6400 12800 16 16 16 0 0 0 16 16 16
 ----------------------------------------------------------
 Successfully initialised Panphasia box at level           44
 Abs origin: (        3200,        6400,       12800)
 Rel origin: (           0,           0,           0)
 Periods   : (          16,          16,          16)
 Phases used: [Panph1,L40,(200,400,800),S1,CH-999,XXXXXXXX]
 ----------------------------------------------------------
 -----------------------------------------------------
Moment: I(000) =     0.40263830399469E-18
Moment: I(001) =    -0.21622221261036E-30
Moment: I(010) =    -0.29391437181154E-31
Moment: I(011) =     0.91428635240100E-43
Moment: I(100) =    -0.23078445689585E-30
Moment: I(101) =     0.83196847010331E-43
Moment: I(110) =    -0.36947830720766E-43
Moment: I(111) =    -0.57205869835500E-57
 -----------------------------------------------------
 RMS averages =   0.9888939       1.011291       1.002234       1.008872    
                  0.9895510      0.9902741      0.9950475      0.9929609    
                  4096  cells in total.
 Time for summation:   1.7997000E-02 seconds.

 As required all eight moments are virtually identical. The small
differences are just caused by rounding error.  The 'RMS' averages of
the eight moments taken over all the cells.  The expectation value for
a white noise field is unity.

 Note that the descriptor ('Phases used:') is the same.  The descriptor 
uses the lowest possible level alway to describe the region of interest.


Evaluating the same region at level 48:

./demo_code.x 48 51200 102400 204800 256 256 256 0 0 0 256 256 256
 ----------------------------------------------------------
 Successfully initialised Panphasia box at level           48
 Abs origin: (       51200,      102400,      204800)
 Rel origin: (           0,           0,           0)
 Periods   : (         256,         256,         256)
 Phases used: [Panph1,L40,(200,400,800),S1,CH-999,XXXXXXXX]
 ----------------------------------------------------------
 -----------------------------------------------------
Moment: I(000) =     0.40263830399466E-18
Moment: I(001) =    -0.21622221261040E-30
Moment: I(010) =    -0.29391437181182E-31
Moment: I(011) =     0.91428635240108E-43
Moment: I(100) =    -0.23078445689587E-30
Moment: I(101) =     0.83196847010315E-43
Moment: I(110) =    -0.36947830720774E-43
Moment: I(111) =    -0.57205869835435E-57
 -----------------------------------------------------
 RMS averages =   0.9998612      0.9998406      0.9999232      0.9998716    
                   1.000265      0.9999132      0.9998236       1.000037    
              16777216  cells in total.
 Time for summation:    42.60552     seconds.

 The same region can be evaluated starting with different values to the global
origin (xorigin,yorigin,zorigin) and the starting point of the summation
(ixmin,iymin,izmin) (and also boxsize) by shifting the global and
local coordinates in equal and opposite directions.

E.g. a shift of 701 cells in all 3 coordinate directions:


./demo_code.x 48 50499 101699 204099 957 957 957 701 701 701 256 256 256
 ----------------------------------------------------------
 Successfully initialised Panphasia box at level           48
 Abs origin: (       50499,      101699,      204099)
 Rel origin: (         701,         701,         701)
 Periods   : (         957,         957,         957)
 Phases used: [Panph1,L48,(50499,101699,204099),S957,CH-999,XXXXXXXX]
 ----------------------------------------------------------
 -----------------------------------------------------
Moment: I(000) =     0.40263830399466E-18
Moment: I(001) =    -0.21622221261040E-30
Moment: I(010) =    -0.29391437181182E-31
Moment: I(011) =     0.91428635240108E-43
Moment: I(100) =    -0.23078445689587E-30
Moment: I(101) =     0.83196847010315E-43
Moment: I(110) =    -0.36947830720774E-43
Moment: I(111) =    -0.57205869835435E-57
 -----------------------------------------------------
 RMS averages =   0.9998612      0.9998406      0.9999232      0.9998716    
                   1.000265      0.9999132      0.9998236       1.000037    
              16777216  cells in total.
 Time for summation:    42.60852     seconds.

 This gives exactly the same moments (and RMS averages) as directly
above, as the order of summation is preserved (provided the periodicity
is increased to at least the value given here to prevent wrap around).
In this case the descriptor is different as a different (but overlapping)
region of Panphasia has been chosen.


 In the previous examples the subdomain did not abut with the domain
boundary. In this example the subdomain is placed close to the upper corner
so that it has to be wrapped across the boundary. This is seemless from
the user point of view.

./demo_code.x 21 200000 400002 600014 2000 2000 2000 1990 1994 1998 16 16 16
 ----------------------------------------------------------
 Successfully initialised Panphasia box at level           21
 Abs origin: (      200000,      400002,      600014)
 Rel origin: (        1990,        1994,        1998)
 Periods   : (        2000,        2000,        2000)
 Phases used: [Panph1,L20,(100000,200001,300007),S1000,CH-999,XXXXXXXX]
 ----------------------------------------------------------
 -----------------------------------------------------
Moment: I(000) =     0.15523103711209E-07
Moment: I(001) =     0.16734041104396E-13
Moment: I(010) =     0.50524664145685E-13
Moment: I(011) =     0.14647218897821E-18
Moment: I(100) =    -0.82127043873677E-13
Moment: I(101) =     0.12220354286167E-18
Moment: I(110) =     0.16536597926443E-18
Moment: I(111) =     0.33927733984333E-24
 -----------------------------------------------------
 RMS averages =   0.9995553      0.9967698       1.010076       1.000050    
                  0.9852598       1.009849      0.9981809      0.9925951    
                  4096  cells in total.
 Time for summation:   9.9985003E-02 seconds.

 similarly further down the octree - for the same region:

./demo_code.x 25 3200000 6400032 9600224 32000 32000 32000 31840 31904 31968 256 256 256
 ----------------------------------------------------------
 Successfully initialised Panphasia box at level           25
 Abs origin: (     3200000,     6400032,     9600224)
 Rel origin: (       31840,       31904,       31968)
 Periods   : (       32000,       32000,       32000)
 Phases used: [Panph1,L20,(100000,200001,300007),S1000,CH-999,XXXXXXXX]
 ----------------------------------------------------------
 -----------------------------------------------------
Moment: I(000) =     0.15523103711208E-07
Moment: I(001) =     0.16734041104397E-13
Moment: I(010) =     0.50524664145682E-13
Moment: I(011) =     0.14647218897820E-18
Moment: I(100) =    -0.82127043873675E-13
Moment: I(101) =     0.12220354286167E-18
Moment: I(110) =     0.16536597926441E-18
Moment: I(111) =     0.33927733984333E-24
 -----------------------------------------------------
 RMS averages =    1.000022       1.000140       1.000052       1.000056    
                  0.9998846       1.000000       1.000268       1.000191    
              16777216  cells in total.
 Time for summation:    74.36469     seconds.

The underlying more complex access pattern is reflected in the run time.


The speed of evaluating Panphasia depends strongly on the access pattern
---------------------------------=---------------------------------------

  The cost of evaluating the properties of a single cell deep down in
the tree are high, but the cost of making successive calls to
neighbouring cells can be made low by retaining the results of
previous calls and only doing new evaluations when they are really
needed.  In real applications the CPU cost per white noise cell
averages to around 2 micro-second per call for a 2.6GHz Sandybridge
(over tens to hundreds of millions of cells). Aggressive compiler
optimisation introduces only very small changes in the values
returned and is therefore recommended provided the code can still
accurately reproduce the results above. The INTEL compiler was
used for the results shown here. 

The execution time is sufficiently fast that generating the white
noise field is not particularly limiting for making initial conditions
- at least when compared to some other methods commonly used in
setting the phases.  There defintely is room to improve the performance of the
routines further. There is a weak dependence of speed with the depth
of the tree in the current routines that could probably be largely
avoided. There is a difference of about 50% between level
49 compared to level 10. The code is also slower when the subdomain
crosses one or more boundaries of the domain as shown above.
 
  There are three demo subroutines to illustrate the dependence of
speed on the access pattern. They all evaluate Panphasia
over the same arbitrarly chosen small cube with 64**3
cells in the 49th (bottom) level of the octree.  All demos
give exactly the same answers cell by cell (as evidenced by a set of eight check
sums) but differ significantly in execution time because they adopt
different access patterns. The real*8 sum over the zeroth moments of
the cells comes out nearly the same, but not exactly the same because
of the dependence of the round-off error on the order of summation.

(2) random_summation_demo  employs a pseudo-random access pattern to the 64**3
cells, which is very inefficient and close to a worst case:

 Running this subroutine gives:

./demo_code.x
 ----------------------------------------------------------
 Successfully initialised Panphasia box at level           49
 Abs origin: (559234782084,409495239424, 99335354324)
 Rel origin: (           0,           0,           0)
 Periods   : (  2000000072,   547435176,  1000000068)
 Phases used: 
 [Panph1,L47,(139808695521,102373809856,24833838581),S500000018,CH-999,XXXXXXXX]
 ----------------------------------------------------------
 Sum of zero moments =   -147.932115765520     
 Check-sum no.=           1   -568320532359462070
 Check-sum no.=           2   1408588989405454447
 Check-sum no.=           3  -4742847346318282848
 Check-sum no.=           4   1548221454173201162
 Check-sum no.=           5    658194772941294313
 Check-sum no.=           6  -1248375398177650819
 Check-sum no.=           7  -5912618053914587316
 Check-sum no.=           8   7240174312988716418
 Check-sum no.=           9   -899422301238017455
 Pseudo-random access summation
 Time:    56.51341    

(3) raster_summation_demo 

 A raster can is more efficient.  It does depend on which index
is used as the inner loop.  The routines are designed so that
z direction should be the inner index.

The raster_summation_demo shows this. In this code we compute
the zeroth moment of the cells, and integer check sum for
the other moments.  Because the order of addition for integers
does not affect the total, we expect the summations over a
particular set of cells to give the same answer independent
of the ordering.  This is the case. 

The precise value of the check sum depends on the compiler and flags used as each
individual value is derived from a floating point number,
and these floating point values are sensitive to these
options, but for a given compiler and options the check sums should be
independent of the order of summation.


./demo_code.x
 ----------------------------------------------------------
 Successfully initialised Panphasia box at level           49
 Abs origin: (559234782084,409495239424, 99335354324)
 Rel origin: (           0,           0,           0)
 Periods   : (  2000000072,   547435176,  1000000068)
 Phases used: 
 [Panph1,L47,(139808695521,102373809856,24833838581),S500000018,CH-999,XXXXXXXX]
 ----------------------------------------------------------
 Sum of zero moments =   -147.932115765516     
 Check-sum no.=           1   -568320532359462070
 Check-sum no.=           2   1408588989405454447
 Check-sum no.=           3  -4742847346318282848
 Check-sum no.=           4   1548221454173201162
 Check-sum no.=           5    658194772941294313
 Check-sum no.=           6  -1248375398177650819
 Check-sum no.=           7  -5912618053914587316
 Check-sum no.=           8   7240174312988716418
 Check-sum no.=           9   -899422301238017455
 Raster scan with x as inner index:
 Time:    11.94718    
 Sum of zero moments =   -147.932115765505     
 Check-sum no.=           1   -568320532359462070
 Check-sum no.=           2   1408588989405454447
 Check-sum no.=           3  -4742847346318282848
 Check-sum no.=           4   1548221454173201162
 Check-sum no.=           5    658194772941294313
 Check-sum no.=           6  -1248375398177650819
 Check-sum no.=           7  -5912618053914587316
 Check-sum no.=           8   7240174312988716418
 Check-sum no.=           9   -899422301238017455
 Raster scan with z as inner index:
 Time:    2.093681    
 
 Using z as the inner loop the time per cell evaluation to about
8 microseconds.  This can be improved further by exploiting
the fact that the code actually computes 8 cells at a time.

(4) fast_summation_demo

 Quicker still is: `fast_summation_demo': which takes advantage of the
fact that the cell properties are determined in groups of eight (as
the octree basis functions span eight cells at a minimum).  The
subroutine has an inner loop which sums over clusters of eight cells
(as written this assumes these eight cells share a common parent cell
in the level of the octree above which is not necessarily true, but is
for the current example).

./demo_code.x
 ----------------------------------------------------------
 Successfully initialised Panphasia box at level           49
 Abs origin: (559234782084,409495239424, 99335354324)
 Rel origin: (           0,           0,           0)
 Periods   : (  2000000072,   547435176,  1000000068)
 Phases used: 
 [Panph1,L47,(139808695521,102373809856,24833838581),S500000018,CH-999,XXXXXXXX]
 ----------------------------------------------------------
 Sum of zero moments =   -147.932115765511     
 Check-sum no.=           1   -568320532359462070
 Check-sum no.=           2   1408588989405454447
 Check-sum no.=           3  -4742847346318282848
 Check-sum no.=           4   1548221454173201162
 Check-sum no.=           5    658194772941294313
 Check-sum no.=           6  -1248375398177650819
 Check-sum no.=           7  -5912618053914587316
 Check-sum no.=           8   7240174312988716418
 Check-sum no.=           9   -899422301238017455
 CPU efficient ordering of the summation
 Time:   0.5539160    

 The time per cell evaluation is now 2.3 microseconds. For shallower
levels in the tree the evaluation time will be shorter still. 
 
---------------------------------------------------------------

Finally: summing over the same 64**3 cells above but with the check_moments
subroutine gives:

./demo_code.x 49 561234782092 410042674536 100335354328 64 64 64 0 0 0 64 64 64
 ----------------------------------------------------------
 Successfully initialised Panphasia box at level           49
 Abs origin: (561234782092,410042674536,100335354328)
 Rel origin: (           0,           0,           0)
 Periods   : (          64,          64,          64)
 Phases used: 
 [Panph1,L47,(140308695523,102510668634,25083838582),S16,CH-999,XXXXXXXX]
 ----------------------------------------------------------
 -----------------------------------------------------
Moment: I(000) =    -0.11075358360317E-19
Moment: I(001) =    -0.11405510733182E-33
Moment: I(010) =     0.17573900581185E-32
Moment: I(011) =    -0.53961246327836E-46
Moment: I(100) =     0.85879044990399E-33
Moment: I(101) =    -0.57530626721687E-47
Moment: I(110) =     0.81328064632391E-46
Moment: I(111) =     0.26181821286200E-60
 -----------------------------------------------------
 RMS averages =   0.9989292      0.9991783       1.001735       1.001985    
                  0.9996984       1.000668       1.000727      0.9967595    
                262144  cells in total.
 Time for summation:    2.181669     seconds.

 'check_moments' give a zeroth moment of  -0.11075358360317E-19
Comparing this with the zeroth moment value of -147.9321157 found by
the three subroutines above leads to a ratio =
( -147.9321157/-0.11075358222045E-19) which very close to 2**(3*49/2)
as it should be.


==================================================================================