#!/bin/bash
#
# This script generates typical FFT sizes that might be encountered running CP2K
# it can be used to generated FFTW wisdom with cp2k using the command
#
# fftw-wisdom `./cp2k_wisdom` > wisdom.dat
#
# where fftw-wisdom is a program that is part of the fftw installation  (assumed to be in your path)
#
# in order to limit the number of transforms that is planned, we only do 3d transforms of cubic cells.
# this might still require fftw-wisdom to run for hours (but you only need to do this once).
# However, you might extend the range of transforms e.g. by setting minrat / maxrat non-equal
# or just generate wisdom for the cells that you expect for your simulation
#
# it seems a good idea to always plan the 1D transforms below.
#

# these are the type of transforms we might do
prefixes="cif cib rif rib cof cob rof rob"

# these are the fftsg sizes that might be executed by default
sizes="2    4    6    8    9   12   15   16   18   20   24   25  27   30   32   36   40   45   48   54   60   64   72   75   80   81   90  96  100  108  120  125  128  135  144  150  160  162  180  192  200  216  225  240  243  256  270  288  300  320  324  360  375  384  400  405  432  450  480  486  500  512  540  576  600  625  640  648  675  720  729  750  768  800  810  864  900  960  972  1000  1024"

# all 1D transforms
for size in $sizes
do
  for pre in $prefixes
  do
      printf "%s " "${pre}${size}"
  done
done

# all 3d transofrms subject to some restrictions on the sizes, i.e. the cell is cubic, the grids not too large
# only two different axis allowed
minrat=1.0
maxrat=1.0
min2d=2
max2d=300

for s1 in $sizes
do
  for s2 in $sizes
  do
    ratio_ok=`echo "$s1 $s2" | awk -vminrat=$minrat -vmaxrat=$maxrat -vmin2d=$min2d -vmax2d=$max2d '{if ($1/$2>=minrat && $1/$2<=maxrat && $1<max2d && $2<max2d && $1>min2d && $2>min2d) {print 1} else {print 0}}'`
    if [ "$ratio_ok" == "1" ]; then
       for pre in $prefixes
       do
              printf "%s " "${pre}${s1}x${s1}x${s2}"
              printf "%s " "${pre}${s1}x${s2}x${s1}"
              printf "%s " "${pre}${s2}x${s1}x${s1}"
       done
    fi
  done
done
