#!/bin/bash
#############################
# SPO-DVR BEAM SIMULATOR 
#############################
#
# This script wil setup a file tree to propagate many types of wavepackets for each specified state in the ranges set in this script
# --Script will create PBS job Script
# --Script will create DVR.inp
# --Script will rename all output files appropriately, and copy them to a log folder
#
# This script will also create a BACKUP in your /home/{USER}/
#  -when this script runs it create the file structure where you run it.
#  -It will back-up all input files to your /home/{USER}/
#  -Jobs read their input from the /home/{USER}/ backup folder
#  -Jobs will only copy the reaction probability back to /home/{USER}/
#  ----> So if glusterfs crashes the Reaction Probability of the calculation is still saved, just not the S-matrix,wavefunction,and balin-kurti coefficients
#
# The script will perform sanity checks on all input WPs
#
# Before submitting a summary is printed in the terminal
#

#########################################################################
#	INPUT WAVEPACKETS
#
# -Four (4) WavePackets can be submitted per state, namely: A, B, C, and/or D. To select a particular WP add it to the string in the UseWP=" " below.
#
# -A WP is an energy range, and you can specify three angular grids per energy range
#	- v0j0mj0 up till v0jXmjX 		X = HIGHLOWswitch
#	- v0jXmjX up till v0jMAXmjMAX		MAX = v0qjmax / v1qjmax
#	- v1					Since v1 state only go up to j=8 --> there is only one type of angular grid.
#
#########################################################################

#########################################################################
# 	FILE TREE
# Workdirectory 	(where you run this script)
# --State		(e.g. v0j0mj0)
# -- -- WPname          (more of a selector... choose some string and script creates a folder with that name in which to start the calculation -> you need multiple wave packets per state)
#
# 	FILE TREE LOG/BACKUP
# /home/{USER}/{LOGFOLDER}
# -- ReacProb		(This will store the reaction probabilities.) 	Naming= ${state}.${UseWP}.${WPname}.ReacProb 
# -- DVRinputs		(This will store the DVR.inp files.)		Naming= ${state}.${UseWP}.${WPname}.DVR
# -- out		(This will store the spo-dvr.out files.)	Naming= ${state}.${UseWP}.${WPname}.out
#
#########################################################################

#########################################################################
########	INPUT PARAMETERS 			#################
#########################################################################
UseWP="A B"
#SystemID="H2Cu211SRPPES"
#SystemID="H2Pt211PES"
#SystemID="HardWallPES"
SystemID="CRPlibrary"
POTNAME="Cu111_B86SRP68-DF2_Z8A_C3v"
#########################################################################
#fftnumbers="2 4 6 8 10 12 14 16 18 20 22 24 28 30 32 36 40 42 44 48 56 60 64 66 70 72 80 84 88 90 96 110 112 120 126 128 132 140 144 154 160 168 176 180 192 198 210 220 224 240 252 256 264 280 288 308 320 330 336 352 360 384 396 420 440 448 462 480 504 512 528 560 576 616 630 640 660 672 704 720 726 768 770 784 792 800 840 880 882 896 900 924 960 968 980 990 1008 1024 1050 1056 1078"
# WAVE PACKETS GO HERE
# WP = A		        WP = B				WP = C				WP = D				WP = E
AWPname="0.5eV-1.4eV"   ;	BWPname="0.15eV-0.55eV"   ;	CWPname="0.2eV-0.75eV";	DWPname="Lo.0.1eV-0.4eV"; 	EWPname="Lo.0.1ev-0.4eV"; #Name of Wave Packet --> /{state}/{WPname} 
AEminWP=0.5		;	BEminWP=0.15		;	CEminWP=0.2		;	DEminWP=0.1		; 	EEminWP=0.1		; #lowest  energy of wave packet
AEmaxWP=1.4		;	BEmaxWP=0.55		;	CEmaxWP=0.75		;	DEmaxWP=0.4		; 	EEmaxWP=0.4		; #highest energy of wave packet
AZ0WP=16.80		;	BZ0WP=17.40		;	CZ0WP=14.20		;	DZ0WP=21.68		; 	EZ0WP=21.68		; #center of wave packet
		
AnZ=192			;	BnZ=198			;	CnZ=192			;	DnZ=320			; 	EnZ=320			; #scattering grid Z
AZstart=-1.0		;	BZstart=-1.0		;	CZstart=-2.00		;	DZstart=-2.00		; 	EZstart=-2.0		;
AdZ=0.10 		;	BdZ=0.10 		;	CdZ=0.10		;	DdZ=0.08		;	EdZ=0.08		;
			
AnZspec=224		;	BnZspec=252		;	CnZspec=220		;	DnZspec=384		; 	EnZspec=384		; #specular grid Z

AnR=48			;	BnR=56			;	CnR=48			;	DnR=80			; 	EnR=80			; #scattering grid R
ARstart=0.8		;	BRstart=0.8		;	CRstart=0.8		;	DRstart=0.8		;	ERstart=0.8		;
AdR=0.15		;	BdR=0.15		;	CdR=0.15		;	DdR=0.1			;	EdR=0.1			;
	
AnX=20			;	BnX=20			;	CnX=2			;	DnX=16			; 	EnX=18			; #scattering grid X
AnY=20			;	BnY=20			;	CnY=2			;	DnY=16			; 	EnY=18			; #scattering grid Y
	
Av0nJLOW=36		;	Bv0nJLOW=26		;	Cv0nJLOW=2		;	Dv0nJLOW=32		; 	Ev0nJLOW=36		;
Av0nMJLOW=28		;	Bv0nMJLOW=26		;	Cv0nMJLOW=2		;	Dv0nMJLOW=28		;	Ev0nMJLOW=32		;
			
Av0nJHIGH=42		;	Bv0nJHIGH=32		;	Cv0nJHIGH=2		;	Dv0nJHIGH=36		;	Ev0nJHIGH=40		;
Av0nMJHIGH=40		;	Bv0nMJHIGH=32		;	Cv0nMJHIGH=2		;	Dv0nMJHIGH=32		;	Ev0nMJHIGH=36		;
				
Av1nJ=40		;	Bv1nJ=36		;	Cv1nJ=2 		;	Dv1nJ=32		;	Ev1nJ=36		;
Av1nMJ=32		;	Bv1nMJ=28		;	Cv1nMJ=2		;	Dv1nMJ=32		;	Ev1nMJ=36		;

#optical potentials
AZCAPstart=15.20	;	BZCAPstart=15.20	;	CZCAPstart=12.50	;	DZCAPstart=19.04	; 	EZCAPstart=19.04	; #Z scattering CAP
AZCAPend=18.10		;	BZCAPend=18.70		;	CZCAPend=16.90		;	DZCAPend=23.52		;	EZCAPend=23.52		;
AZCAPtargetE=0.30	;	BZCAPtargetE=0.1	;	CZCAPtargetE=0.08	;	DZCAPtargetE=0.08	;	EZCAPtargetE=0.08	;
	
AZspecCAPstart=18.80	;	BZspecCAPstart=19.80	;	CZspecCAPstart=16.10	;	DZspecCAPstart=24.24	; 	EZspecCAPstart=24.24	; #Z specular CAP
AZspecCAPend=21.30	;	BZspecCAPend=24.10	;	CZspecCAPend=19.90	;	DZspecCAPend=28.64	;	EZspecCAPend=28.64	;
AZspecCAPtargetE=0.25	;	BZspecCAPtargetE=0.08	;	CZspecCAPtargetE=0.08	;	DZspecCAPtargetE=0.08	;	EZspecCAPtargetE=0.08	;
	
ARCAPstart=4.55		;	BRCAPstart=4.55		;	CRCAPstart=4.55		;	DRCAPstart=4.6		; 	ERCAPstart=4.6		; #R CAP 
ARCAPend=7.85		;	BRCAPend=9.05		;	CRCAPend=7.85		;	DRCAPend=8.7		;	ERCAPend=8.7		;
ARCAPtargetE=0.25	;	BRCAPtargetE=0.1	;	CRCAPtargetE=0.2	;	DRCAPtargetE=0.3	;	ERCAPtargetE=0.3	;

#time + steps
Adt=2			;	Bdt=2			;	Cdt=2			;	Ddt=2			; 	Edt=2			; #size of time step
Ant=6500 		;	Bnt=10000		;	Cnt=100		        ;	Dnt=12000		; 	Ent=12000		; #number of timesteps
#number of needed nodes
#these are not used --> submitter will calculate the number of nodes it will use
ANODEv0LOW=4		;	BNODEv0LOW=1		;	CNODEv0LOW=1		;	DNODEv0LOW=		;	ENODEv0LOW=		; #NOT USED
ANODEv0HIGH=1		;	BNODEv0HIGH=1		;	CNODEv0HIGH=1		;	DNODEv0HIGH=		;	ENODEv0HIGH=		; #NOT USED
ANODEv1=1		;	BNODEv1=1		;	CNODEv1=1		;	DNODEv1=1		;	ENODEv1=1		; #NOT USED
#add a number of nodes to a calculation, if NODEOFFS=0 the bare minimum is used
ANODEOFFS=0		;	BNODEOFFS=0		;	CNODEOFFS=1		;	DNODEOFFS=0		;	ENODEOFFS=0		; #USED -- add arbitrary nodes to calc

#swith to high at J=
AHIGHLOWswitch=8	;	BHIGHLOWswitch=8	;	CHIGHLOWswitch=8	;	DHIGHLOWswitch=8	;	EHIGHLOWswitch=8	;
#########################################################################
#########################################################################

#maximum of queued Jobs
maxQ=2

#Masses
Mass1=1.0078
Mass2=1.0078

#Mass1=2.0141
#Mass2=2.0141

#The script wil run over the following states:
#  -- from nu = qnumin to nu = qnumax
#  -- -- then from J = qjstart to J = v*qjmax
#  -- -- -- mJ always runs from 0 to the mJ = J
#Vmax vibrational quantum number
qnumin=0  	#start at v = $qnumin
qnumax=0	#end with v = $qnumax


##script has been modified to run only these J states 
#whichj="0"


#initial qj quantum number
qjstart=9

#Jmax 
v0qjmax=11 	#Jmax for v = 0
v1qjmax=7	#Jmax for v = 1
v2qjmax=5       #Jmax for v = 2

qmjstart=0

#GB RAM per node
RAMPERNODE=346

#additional timesteps
dtAna=50
dtAve=50
NrOfEs=8
#########################################################################
#########################################################################

#----- MY version
#path to SPO-DVR
#SPODVR="/home/guido/SPO-DVR/EXECUTABLE.DIR/"
#BINARY="${SPODVR}SPO-DVR_5.09_O3_IFORT_MPI_OMP_FFTW.x"
#BINARY="${SPODVR}SPO-DVR_5.14_O3_IFORT_MPI_OMP_FFTW.x" #had to switch --> dummy crp initialization + potential.f90 revert to original

#---- svn version
SPODVR="/home/guido/ORG_SPO-DVR/EXECUTABLE.DIR/"
BINARY="${SPODVR}SPO-DVR_5.16_O3_IFORT_MPI_OMP_FFTW.x"
BINARY_ALT="/home/guido/ORG_SPO-DVR/EXECUTABLE.DIR/SPO-DVR_5.16_O3_IFORT_OMP_FFTW.x"
# Log folder in home
HOME="/home/guido/"
LOGfolder="${HOME}LOG-QD-Cu111_B86SRP68-DF2_H2/"
LOGReacProb="${LOGfolder}ReacProb"
LOGinputs="${LOGfolder}DVRinputs"
LOGout="${LOGfolder}out"
LOGinelastic="${LOGfolder}inelastic"

#Keep resubmitting till converged
autosub="TRUE"
#HEADNODE="Z5R6U1"
HEADNODE="Z5R1U1"
#########################################################################
#####	END OF USER INPUT				#################
#########################################################################



#########################################################################
#if log folders do not exist; then make them
if [ ! -d "${LOGfolder}" ]; then
  mkdir ${LOGfolder}
fi
if [ ! -d "${LOGReacProb}" ]; then
  mkdir ${LOGReacProb}
fi
if [ ! -d "${LOGinputs}" ]; then
  mkdir ${LOGinputs}
fi
if [ ! -d "${LOGout}" ]; then
  mkdir ${LOGout}
fi
if [ ! -d "${LOGinelastic}" ]; then
  mkdir ${LOGinelastic}
fi
#########################################################################
#########################################################################
#	now we're going to check the input wave packets to make sure everthing goes well

#loop over the wave packets we want to use to perform sanity checks on the inputs
for WP in $UseWP
do
  #Now copy the values for each WP into the working variables
  if [ $WP == "A" ]; then
    echo "-------------------------------------------------------------"
    echo " ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP}" 
    echo "  SANITY CHECKS FOR WP=${WP} $AWPname"
    WPname=$AWPname
    EminWP=$AEminWP
    EmaxWP=$AEmaxWP
    Z0WP=$AZ0WP
    nZ=$AnZ
    Zstart=$AZstart
    dZ=$AdZ
    nZspec=$AnZspec
    nR=$AnR
    Rstart=$ARstart
    dR=$AdR
    nX=$AnX
    nY=$AnY
    v0nJLOW=$Av0nJLOW
    v0nMJLOW=$Av0nMJLOW
    v0nJHIGH=$Av0nJHIGH
    v0nMJHIGH=$Av0nMJHIGH
    v1nJ=$Av1nJ
    v1nMJ=$Av1nMJ
    ZCAPstart=$AZCAPstart
    ZCAPend=$AZCAPend
    ZCAPtargetE=$AZCAPtargetE
    ZspecCAPstart=$AZspecCAPstart
    ZspecCAPend=$AZspecCAPend
    ZspecCAPtargetE=$AZspecCAPtargetE
    RCAPstart=$ARCAPstart
    RCAPend=$ARCAPend
    RCAPtargetE=$ARCAPtargetE
    dt=$Adt
    nt=$Ant
    NODEv0LOW=$ANODEv0LOW
    NODEv0HIGH=$ANODEv0HIGH
    NODEv1=$ANODEv1
    HIGHLOWswitch=$AHIGHLOWswitch
  elif [ $WP == "B" ]; then
    echo "-------------------------------------------------------------"
    echo " ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP}" 
    echo "  SANITY CHECKS FOR WP=${WP} $BWPname"

    WPname=$BWPname
    EminWP=$BEminWP
    EmaxWP=$BEmaxWP
    Z0WP=$BZ0WP
    nZ=$BnZ
    Zstart=$BZstart
    dZ=$BdZ
    nZspec=$BnZspec
    nR=$BnR
    Rstart=$BRstart
    dR=$BdR
    nX=$BnX
    nY=$BnY
    v0nJLOW=$Bv0nJLOW
    v0nMJLOW=$Bv0nMJLOW
    v0nJHIGH=$Bv0nJHIGH
    v0nMJHIGH=$Bv0nMJHIGH
    v1nJ=$Bv1nJ
    v1nMJ=$Bv1nMJ
    ZCAPstart=$BZCAPstart
    ZCAPend=$BZCAPend
    ZCAPtargetE=$BZCAPtargetE
    ZspecCAPstart=$BZspecCAPstart
    ZspecCAPend=$BZspecCAPend
    ZspecCAPtargetE=$BZspecCAPtargetE
    RCAPstart=$BRCAPstart
    RCAPend=$BRCAPend
    RCAPtargetE=$BRCAPtargetE
    dt=$Bdt
    nt=$Bnt
    NODEv0LOW=$BNODEv0LOW
    NODEv0HIGH=$BNODEv0HIGH
    NODEv1=$BNODEv1
    HIGHLOWswitch=$BHIGHLOWswitch
  elif [ $WP == "C" ]; then 
    echo "-------------------------------------------------------------"
    echo " ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP}" 
    echo "  SANITY CHECKS FOR WP=${WP} $CWPname"
    WPname=$CWPname
    EminWP=$CEminWP
    EmaxWP=$CEmaxWP
    Z0WP=$CZ0WP
    nZ=$CnZ
    Zstart=$CZstart
    dZ=$CdZ
    nZspec=$CnZspec
    nR=$CnR
    Rstart=$CRstart
    dR=$CdR
    nX=$CnX
    nY=$CnY
    v0nJLOW=$Cv0nJLOW
    v0nMJLOW=$Cv0nMJLOW
    v0nJHIGH=$Cv0nJHIGH
    v0nMJHIGH=$Cv0nMJHIGH
    v1nJ=$Cv1nJ
    v1nMJ=$Cv1nMJ
    ZCAPstart=$CZCAPstart
    ZCAPend=$CZCAPend
    ZCAPtargetE=$CZCAPtargetE
    ZspecCAPstart=$CZspecCAPstart
    ZspecCAPend=$CZspecCAPend
    ZspecCAPtargetE=$CZspecCAPtargetE
    RCAPstart=$CRCAPstart
    RCAPend=$CRCAPend
    RCAPtargetE=$CRCAPtargetE
    dt=$Cdt
    nt=$Cnt
    NODEv0LOW=$CNODEv0LOW
    NODEv0HIGH=$CNODEv0HIGH
    NODEv1=$CNODEv1
    HIGHLOWswitch=$CHIGHLOWswitch
  elif [ $WP == "D" ]; then
    echo "-------------------------------------------------------------"
    echo " ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP}" 
    echo "  SANITY CHECKS FOR WP=${WP} $DWPname"
    WPname=$DWPname
    EminWP=$DEminWP
    EmaxWP=$DEmaxWP
    Z0WP=$DZ0WP
    nZ=$DnZ
    Zstart=$DZstart
    dZ=$DdZ
    nZspec=$DnZspec
    nR=$DnR
    Rstart=$DRstart
    dR=$DdR
    nX=$DnX
    nY=$DnY
    v0nJLOW=$Dv0nJLOW
    v0nMJLOW=$Dv0nMJLOW
    v0nJHIGH=$Dv0nJHIGH
    v0nMJHIGH=$Dv0nMJHIGH
    v1nJ=$Dv1nJ
    v1nMJ=$Dv1nMJ
    ZCAPstart=$DZCAPstart
    ZCAPend=$DZCAPend
    ZCAPtargetE=$DZCAPtargetE
    ZspecCAPstart=$DZspecCAPstart
    ZspecCAPend=$DZspecCAPend
    ZspecCAPtargetE=$DZspecCAPtargetE
    RCAPstart=$DRCAPstart
    RCAPend=$DRCAPend
    RCAPtargetE=$DRCAPtargetE
    dt=$Ddt
    nt=$Dnt
    NODEv0LOW=$DNODEv0LOW
    NODEv0HIGH=$DNODEv0HIGH
    NODEv1=$DNODEv1
    HIGHLOWswitch=$DHIGHLOWswitch
  elif [ $WP == "E" ]; then
    echo "-------------------------------------------------------------"
    echo " ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP} ${WP}" 
    echo "  SANITY CHECKS FOR WP=${WP} $EWPname"
    WPname=$EWPname
    EminWP=$EEminWP
    EmaxWP=$EEmaxWP
    Z0WP=$EZ0WP
    nZ=$EnZ
    Zstart=$EZstart
    dZ=$EdZ
    nZspec=$EnZspec
    nR=$EnR
    Rstart=$ERstart
    dR=$EdR
    nX=$EnX
    nY=$EnY
    v0nJLOW=$Ev0nJLOW
    v0nMJLOW=$Ev0nMJLOW
    v0nJHIGH=$Ev0nJHIGH
    v0nMJHIGH=$Ev0nMJHIGH
    v1nJ=$Ev1nJ
    v1nMJ=$Ev1nMJ
    ZCAPstart=$EZCAPstart
    ZCAPend=$EZCAPend
    ZCAPtargetE=$EZCAPtargetE
    ZspecCAPstart=$EZspecCAPstart
    ZspecCAPend=$EZspecCAPend
    ZspecCAPtargetE=$EZspecCAPtargetE
    RCAPstart=$ERCAPstart
    RCAPend=$ERCAPend
    RCAPtargetE=$ERCAPtargetE
    dt=$Edt
    nt=$Ent
    NODEv0LOW=$ENODEv0LOW
    NODEv0HIGH=$ENODEv0HIGH
    NODEv1=$ENODEv1
    HIGHLOWswitch=$EHIGHLOWswitch
  else 
    echo "this wave packet --> $WP <-- is not yet implemented!!!!"
    exit 1
  fi
  #run tests per wave packet. 

  #fist check that nX nY nZ nZspec nR are valid fft numbers
  #list from powers of 2 3 5 7 11 --> this is much faster then calculating them
  fftnumbers="2 4 6 8 10 12 14 16 18 20 22 24 28 30 32 36 40 42 44 48 56 60 64 66 70 72 80 84 88 90 96 110 112 120 126 128 132 140 144 154 160 168 176 180 192 198 210 220 224 240 252 256 264 280 288 308 320 330 336 352 360 384 396 420 440 448 462 480 504 512 528 560 576 616 630 640 660 672 704 720 726 768 770 784 792 800 840 880 882 896 900 924 960 968 980 990 1008 1024 1050 1056 1078"
  fftcheck=0
  #  echo "For WP=${WP} --> checking if nX, nY, nZ, nZspec and nR are valid fftnumbers..."
  for i in ${fftnumbers}
  do
    if [ "$nX" -eq "$i" ] || [ "$nR" -eq "$i" ] || [ "$nZ" -eq "$i" ] || [ "$nZspec" -eq "$i" ]; then
      ((fftcheck++))
    fi
    if [ "$nY" -eq "$i" ]; then
      ((fftcheck++))
    fi
  done #fftcheck  
  if [ "$fftcheck" -ne "5" ]; then
    echo "ERROR: for WP=${WP} --> either nX, nY, nZ, nZspec and/or nR is not a valid fft number!!!!"
  fi

#--GRID POINT CHECKS-------------------------
#Check if WPcenter, ZScatOptStart, ZSpecOptStart and ZANA lie on a gridpoint
i=0
WPcentercheck="FALSE"
ZScatStartcheck="FALSE"
ZSpecStartcheck="FALSE"
ZScatEndCheck="FALSE"
ZSpecEndCheck="FALSE"
while [ $i -lt $nZspec ]
do
  gridpoint=`echo "$Zstart + ( $i * $dZ )" | bc `
  #echo "gridpoint = $gridpoint "
  if [ "$gridpoint" == "$Z0WP" ]
    then WPcentercheck="TRUE"
  fi
  if [ "$gridpoint" == "$ZCAPstart" ]
    then ZScatStartcheck="TRUE"
  fi
  if [ "$gridpoint" == "$ZspecCAPstart" ]
    then ZSpecStartcheck="TRUE" 
  fi
  if [ "$gridpoint" == "$ZCAPend" ]
    then ZScatEndCheck="TRUE"
  fi
  if [ "$gridpoint" == "$ZspecCAPend" ]
    then ZSpecEndCheck="TRUE"
  fi
  let i++
done 
if [ "$WPcentercheck" == "FALSE" ]
then
  echo "ERROR WP=${WP}: Z0WP is not on a gridpoint! "
  exit 1
fi
if [ "$ZScatStartcheck" == "FALSE" ]
then
  echo "ERROR WP=${WP}: ZScatOptStart is not on a gridpoint! "
  exit 1
fi
if [ "$ZSpecStartcheck" == "FALSE" ]
then
  echo "ERROR WP=${WP}: ZSpecOptStart is not on a gridpoint! $ZspecCAPstart"
  exit 1
fi 
if [ "$ZScatEndCheck" == "FALSE" ]
then 
  echo "ERROR WP=${WP}: ZCAPEnd is not on a gridpoint!  $ZCAPend"
  exit 1
fi
if [ "$ZspecCAPend" == "FALSE" ]
then 
  echo "ERROR WP=${WP}: ZspecCAPend is not on a gridpoint!"
  exit 1
fi

#check if ROptStart lies on a grid point
i=0
ROptStartcheck="FALSE"
ROptEndcheck="FALSE"
while [ $i -lt $nR ]
do
  gridpoint=`echo "$Rstart + ( $i * $dR )" | bc `
  #echo "gridpointR = $gridpoint "
  if [ "$gridpoint" == "$RCAPstart" ]
    then ROptStartcheck="TRUE"
  fi
  if [ "$gridpoint" == "$RCAPend" ]
    then ROptEndcheck="TRUE"
  fi
  let i++
done
if [ "$ROptStartcheck" == "FALSE" ]
then
  echo "ERROR WP=${WP}: RCAPstart is not on a gridpoint! $RCAPstart"
  exit 1
fi
if [ "$ROptEndcheck" == "FALSE" ]
then
  echo "ERROR WP=${WP}: RCAPend is not on a gridpoint! $RCAPend"
  exit 1
fi

#convert to atomic units
camau=1822.89
chaev=27.2114
pi=3.141592654

EMinWPau=`echo "scale=8; $EminWP / $chaev" | bc `
EMaxWPau=`echo "scale=8; $EmaxWP / $chaev" | bc `

ZspecCAPtargetEau=`echo "scale=8; $ZspecCAPtargetE / $chaev" |bc `
ZCAPtargetEau=`echo "scale=8; $ZCAPtargetE / $chaev" | bc `
RCAPtargetEau=`echo "scale=8; $RCAPtargetE / $chaev" | bc`

Mass1au=`echo "scale=8; $camau * $Mass1" | bc `
Mass2au=`echo "scale=8; $camau * $Mass2" | bc `

TotalMassau=`echo "scale=8; $Mass1au + $Mass2au" |bc `
ReducedMassau=`echo "scale=8; ($Mass1au * $Mass2au) / $TotalMassau " | bc `

#calculate length of CAPS
ZScatOptLength=`echo "$ZCAPend - $ZCAPstart" | bc ` 
ZSpecOptLength=`echo "$ZspecCAPend - $ZspecCAPstart" | bc `
ROptLength=`echo "$RCAPend - $RCAPstart" | bc `

#calculate reduced wavelengths
ZScatReducedLength=`echo "scale=2; $ZScatOptLength / ( (2.0 * $pi) / sqrt( 2.0 * $TotalMassau * $ZCAPtargetEau ) )"| bc `
ZScatReducedLengthMinWPE=`echo "scale=2; $ZScatOptLength / ( (2.0 * $pi) / sqrt( 2.0 * $TotalMassau * $EMinWPau ) )" | bc `
ZScatReducedLengthMaxWPE=`echo "scale=2; $ZScatOptLength / ( (2.0 * $pi) / sqrt( 2.0 * $TotalMassau * $EMaxWPau ) )" | bc `

ZSpecReducedLength=`echo "scale=2; $ZSpecOptLength / ( (2.0 * $pi) / sqrt( 2.0 * $TotalMassau * $ZspecCAPtargetEau ))" | bc `
ZSpecReducedLengthMinWPE=`echo "scale=2; $ZSpecOptLength / ( (2.0 * $pi) / sqrt( 2.0 * $TotalMassau * $EMinWPau) )" | bc `
ZSpecReducedLengthMaxWPE=`echo "scale=2; $ZSpecOptLength / ( (2.0 * $pi) / sqrt( 2.0 * $TotalMassau * $EMaxWPau) )" | bc `

RReducedLength=`echo "scale=2; $ROptLength / ( (2.0 * $pi) / sqrt( 2.0 * $TotalMassau * $RCAPtargetEau))" | bc `
RReducedLengthMinWPE=`echo "scale=2; $ROptLength / ( (2.0 * $pi) / sqrt( 2.0 * $TotalMassau * $EMinWPau))" | bc`
RReducedLengthMaxWPE=`echo "scale=2; $ROptLength / ( (2.0 * $pi) / sqrt( 2.0 * $TotalMassau * $EMaxWPau))" | bc`

echo "  Specular Optical potential in Z Reduced Length at TargetE:   $ZSpecReducedLength"
echo "  Specular Optical potential in Z Reduced Length for EminWp:   $ZSpecReducedLengthMinWPE"
echo "  Specular Optical potential in Z Reduced Length for EmaxWp:   $ZSpecReducedLengthMaxWPE"
echo
echo "  Scattering Optical Potential in Z Reduced Length at TargetE: $ZScatReducedLength"
echo "  Scattering Optical Potential in Z Reduced Length for EminWP: $ZScatReducedLengthMinWPE"
echo "  Scattering Optical Potential in Z Reduced Length for EmaxWP: $ZScatReducedLengthMaxWPE"
echo
echo "  R grid Optical potential Reduced Length at TargetE:           $RReducedLength"
echo "  R grid Optical potential Reduced Length for EminWP:           $RReducedLengthMinWPE"
echo "  R grid Optical potential Reduced Length for EmaxWP:           $RReducedLengthMaxWPE"
echo

#info on wave packet
#python WPcheck.py $dvrZstart $ZSpecOptEnd $dvrZspec $Mass1 $Mass2 ${EminWP} ${EmaxWP} $WPcenter $ZANA $ZSpecOptStart ${dvrdZ} $dvrZ $ZScatOptEnd $dvrRstart $ROptEnd $dvrR
python WPcheck-MolBeam.py $Zstart $ZspecCAPend $nZspec $Mass1 $Mass2 ${EminWP} ${EmaxWP} $Z0WP $ZCAPstart $ZspecCAPstart ${dZ} $nZ $ZCAPend $Rstart $RCAPend $nR

#RAM estimate
#RAMestimate=`echo "scale=2; ($nZ * $nR * $nX * $nY * ( $v1nJ + 1 ) * ( (2 * $v1nMJ) + 1 ) * 16 * 2.45) / 1000000000 " | bc `

#end sanity checks
  echo "-------------------------------------------------------------"
  echo
done #WP



#########################################################################
#########################################################################
# THESE ARE THE NESTED STATE LOOPS
# This is so we can see which jobs will be submitted
echo "-------------------------------------------------------------------"
echo "  The following Jobs will be submitted to the que:"
#exit 1
#job counter
count=0

for (( qnu=$qnumin; qnu <= $qnumax; qnu++ ))  #qnu is vibrational quantum number
do
  #maximum J state selector -> qjmax is different per vibrational state
  if [ $qnu -eq 0 ]
  then
    qjmax=$v0qjmax
  fi
  if [ $qnu -eq 1 ]
  then 
    qjmax=$v1qjmax
  fi
  if [ $qnu -eq 2 ]
  then
    qjmax=$v2qjmax
  fi
  #######
  let qj=$qjstart		#qj is rotational quantum number
  while [ $qj -le $qjmax ]
#for qj in $whichj
  do
    let qmj=$qmjstart            #qmj loop
    while [ $qmj -le $qj ] 
    do
 
      for WP in $UseWP 	#loop over multiple WavePackets --> i.e. energies
      do
      #now put everything in the working variables
      if [ $WP == "A" ]; then
        WPname=$AWPname
        EminWP=$AEminWP
    	EmaxWP=$AEmaxWP
	Z0WP=$AZ0WP
    	nZ=$AnZ
    	Zstart=$AZstart
    	dZ=$AdZ
    	nZspec=$AnZspec
    	nR=$AnR
	Rstart=$ARstart
    	dR=$AdR
    	nX=$AnX
    	nY=$AnY
    	v0nJLOW=$Av0nJLOW
    	v0nMJLOW=$Av0nMJLOW
    	v0nJHIGH=$Av0nJHIGH
    	v0nMJHIGH=$Av0nMJHIGH
    	v1nJ=$Av1nJ
    	v1nMJ=$Av1nMJ
    	ZCAPstart=$AZCAPstart
    	ZCAPend=$AZCAPend
    	ZCAPtargetE=$AZCAPtargetE
    	ZspecCAPstart=$AZspecCAPstart
    	ZspecCAPend=$AZspecCAPend
    	ZspecCAPtargetE=$AZspecCAPtargetE
    	RCAPstart=$ARCAPstart
    	RCAPend=$ARCAPend
    	RCAPtargetE=$ARCAPtargetE
    	dt=$Adt
    	nt=$Ant
    	NODEv0LOW=$ANODEv0LOW
    	NODEv0HIGH=$ANODEv0HIGH
    	NODEv1=$ANODEv1
        HIGHLOWswitch=$AHIGHLOWswitch
        NodeOFFSET=$ANODEOFFS
      elif [ $WP == "B" ]; then
    	WPname=$BWPname
    	EminWP=$BEminWP
    	EmaxWP=$BEmaxWP
    	Z0WP=$BZ0WP
    	nZ=$BnZ
    	Zstart=$BZstart
    	dZ=$BdZ
    	nZspec=$BnZspec
    	nR=$BnR
    	Rstart=$BRstart
    	dR=$BdR
    	nX=$BnX
    	nY=$BnY
    	v0nJLOW=$Bv0nJLOW
   	v0nMJLOW=$Bv0nMJLOW
    	v0nJHIGH=$Bv0nJHIGH
    	v0nMJHIGH=$Bv0nMJHIGH
    	v1nJ=$Bv1nJ
    	v1nMJ=$Bv1nMJ
    	ZCAPstart=$BZCAPstart
    	ZCAPend=$BZCAPend
    	ZCAPtargetE=$BZCAPtargetE
    	ZspecCAPstart=$BZspecCAPstart
    	ZspecCAPend=$BZspecCAPend
    	ZspecCAPtargetE=$BZspecCAPtargetE
    	RCAPstart=$BRCAPstart
    	RCAPend=$BRCAPend
    	RCAPtargetE=$BRCAPtargetE
    	dt=$Bdt
    	nt=$Bnt
    	NODEv0LOW=$BNODEv0LOW
    	NODEv0HIGH=$BNODEv0HIGH
    	NODEv1=$BNODEv1
        HIGHLOWswitch=$BHIGHLOWswitch
        NodeOFFSET=$BNODEOFFS
      elif [ $WP == "C" ]; then 
        WPname=$CWPname
    	EminWP=$CEminWP
    	EmaxWP=$CEmaxWP
    	Z0WP=$CZ0WP
    	nZ=$CnZ
    	Zstart=$CZstart
    	dZ=$CdZ
    	nZspec=$CnZspec
    	nR=$CnR
    	Rstart=$CRstart
    	dR=$CdR
    	nX=$CnX
    	nY=$CnY
    	v0nJLOW=$Cv0nJLOW
    	v0nMJLOW=$Cv0nMJLOW
    	v0nJHIGH=$Cv0nJHIGH
    	v0nMJHIGH=$Cv0nMJHIGH
    	v1nJ=$Cv1nJ
    	v1nMJ=$Cv1nMJ
    	ZCAPstart=$CZCAPstart
    	ZCAPend=$CZCAPend
    	ZCAPtargetE=$CZCAPtargetE
    	ZspecCAPstart=$CZspecCAPstart
    	ZspecCAPend=$CZspecCAPend
    	ZspecCAPtargetE=$CZspecCAPtargetE
    	RCAPstart=$CRCAPstart
    	RCAPend=$CRCAPend
    	RCAPtargetE=$CRCAPtargetE
    	dt=$Cdt
    	nt=$Cnt
    	NODEv0LOW=$CNODEv0LOW
    	NODEv0HIGH=$CNODEv0HIGH
    	NODEv1=$CNODEv1
        HIGHLOWswitch=$CHIGHLOWswitch
        NodeOFFSET=$CNODEOFFS
      elif [ $WP == "D" ]; then
        WPname=$DWPname
    	EminWP=$DEminWP
    	EmaxWP=$DEmaxWP
    	Z0WP=$DZ0WP
    	nZ=$DnZ
    	Zstart=$DZstart
    	dZ=$DdZ
    	nZspec=$DnZspec
    	nR=$DnR
    	Rstart=$DRstart
    	dR=$DdR
    	nX=$DnX
    	nY=$DnY
    	v0nJLOW=$Dv0nJLOW
    	v0nMJLOW=$Dv0nMJLOW
    	v0nJHIGH=$Dv0nJHIGH
    	v0nMJHIGH=$Dv0nMJHIGH
    	v1nJ=$Dv1nJ
    	v1nMJ=$Dv1nMJ
    	ZCAPstart=$DZCAPstart
    	ZCAPend=$DZCAPend
    	ZCAPtargetE=$DZCAPtargetE
    	ZspecCAPstart=$DZspecCAPstart
    	ZspecCAPend=$DZspecCAPend
    	ZspecCAPtargetE=$DZspecCAPtargetE
    	RCAPstart=$DRCAPstart
    	RCAPend=$DRCAPend
    	RCAPtargetE=$DRCAPtargetE
    	dt=$Ddt
    	nt=$Dnt
    	NODEv0LOW=$DNODEv0LOW
    	NODEv0HIGH=$DNODEv0HIGH
    	NODEv1=$DNODEv1
        HIGHLOWswitch=$DHIGHLOWswitch
        NodeOFFSET=$DNODEOFFS
      elif [ $WP == "E" ]; then
        WPname=$EWPname
        EminWP=$EEminWP
        EmaxWP=$EEmaxWP
        Z0WP=$EZ0WP
        nZ=$EnZ
        Zstart=$EZstart
        dZ=$EdZ
        nZspec=$EnZspec
        nR=$EnR
        Rstart=$ERstart
        dR=$EdR
        nX=$EnX
        nY=$EnY
        v0nJLOW=$Ev0nJLOW
        v0nMJLOW=$Ev0nMJLOW
        v0nJHIGH=$Ev0nJHIGH
        v0nMJHIGH=$Ev0nMJHIGH
        v1nJ=$Ev1nJ
        v1nMJ=$Ev1nMJ
        ZCAPstart=$EZCAPstart
        ZCAPend=$EZCAPend
        ZCAPtargetE=$EZCAPtargetE
        ZspecCAPstart=$EZspecCAPstart
        ZspecCAPend=$EZspecCAPend
        ZspecCAPtargetE=$EZspecCAPtargetE
        RCAPstart=$ERCAPstart
        RCAPend=$ERCAPend
        RCAPtargetE=$ERCAPtargetE
        dt=$Edt
        nt=$Ent
        NODEv0LOW=$ENODEv0LOW
        NODEv0HIGH=$ENODEv0HIGH
        NODEv1=$ENODEv1
        HIGHLOWswitch=$EHIGHLOWswitch
        NodeOFFSET=$ENODEOFFS
      fi	

      #now we need to figure out which flavor of the wave packet we want
      #either v0LOW, v0HIGH, or v1
      if [ "$qnu" == "0" ] && [ "$qj" -lt "$HIGHLOWswitch" ]; then
        dvrJ=$v0nJLOW
        dvrMJ=$v0nMJLOW
        NODE=$NODEv0LOW
      elif [ "$qnu" == "0" ] && [ "$qj" -ge "$HIGHLOWswitch" ]; then
        dvrJ=$v0nJHIGH
        dvrMJ=$v0nMJHIGH
        NODE=$NODEv0HIGH
      elif [ "$qnu" == "1" ]; then
        dvrJ=$v1nJ
        dvrMJ=$v1nMJ
        NODE=$NODEv1
      fi

      #lastly we need to subtract 1 from dvrJ and dvrMJ if J is uneven
      if [ $(( $qj % 2 )) != 0 ]; then
        ((dvrJ--))
        ((dvrMJ--))
      fi
      #RAM estimate
      Vpoints=`echo "($ZCAPstart - $Zstart) / $dZ " | bc `
      RAMestimate=`echo "scale=2; ( ($nZ * $nR * $nX * $nY * ( $dvrJ + 1 ) * ( (2 * $dvrMJ) + 1 ) * 16) + ( $Vpoints * $nR * $nX * $nY * ( $dvrJ + 1 ) * ( (2 * $dvrMJ) + 1 ) * 8 ) + ( $nX * $nY * ( $dvrJ + 1 ) * ( (2 * $dvrMJ) + 1 ) * 16  ) ) / 1000000000 " | bc -l`
      NodeEST=`echo "scale=2; $RAMestimate / ${RAMPERNODE} " | bc -l`
      NodeESTint=`python -c "from math import ceil; print ceil($NodeEST)"`
      NodeESTint=`echo "scale=0; ($NodeESTint + $NodeOFFSET )/ 1 " |bc `
#########################################################################
###########STATE LOOP -- FOR SHOWING JOBS################################
#now I can walk over the selected states 
state=v${qnu}j${qj}mj${qmj}
echo " STATE= $state  WP= $WP $WPname  dvrJ= $dvrJ   dvrMJ= $dvrMJ  NodeEST= $NodeEST  Nodes used= $NodeESTint"


    

        #increment job counter
        let count++
      done #for WP
      echo
      #increment qmj number
      let qmj=qmj+1
    done #while qmj
    echo
    echo
    echo
    #increment qj number
    let qj=qj+1
  done #while qj
  echo
done #for qnu

echo "Total amount of Jobs that will be submitted: $count "
echo "-------------------------------------------------------------------"
echo
echo "Is the above information correct? (Y/N)"
WhereYouAnIdiot=oops
read WhereYouAnIdiot
#echo " WhereYouAnIdiot = $WhereYouAnIdiot "
if [ "$WhereYouAnIdiot" != "Y" ]
then 
  echo "exiting script"
  exit 1
fi

# The state loop works, as well as the switch from low to high for v0 and
# subtracting 1 from dvrJ and dvrMJ for uneven qj
#########################################################################
#########################################################################

#########################################################################
#########################################################################
# NOW THE SUBMIT LOOP
count=0

for (( qnu=$qnumin; qnu <= $qnumax; qnu++ ))  #qnu is vibrational quantum number
do
  #maximum J state selector -> qjmax is different per vibrational state
  if [ $qnu -eq 0 ]
  then
    qjmax=$v0qjmax
  fi
  if [ $qnu -eq 1 ]
  then 
    qjmax=$v1qjmax
  fi
  if [ $qnu -eq 2 ]
  then
    qjmax=$v2qjmax
  fi
  #######
  let qj=$qjstart		#qj is rotational quantum number
  while [ $qj -le $qjmax ]
#for qj in $whichj
  do
    let qmj=$qmjstart            #qmj loop
    while [ $qmj -le $qj ] 
    do
 
      for WP in $UseWP 	#loop over multiple WavePackets --> i.e. energies
      do
      #now put everything in the working variables
      if [ $WP == "A" ]; then
        WPname=$AWPname
        EminWP=$AEminWP
    	EmaxWP=$AEmaxWP
	Z0WP=$AZ0WP
    	nZ=$AnZ
    	Zstart=$AZstart
    	dZ=$AdZ
    	nZspec=$AnZspec
    	nR=$AnR
	Rstart=$ARstart
    	dR=$AdR
    	nX=$AnX
    	nY=$AnY
    	v0nJLOW=$Av0nJLOW
    	v0nMJLOW=$Av0nMJLOW
    	v0nJHIGH=$Av0nJHIGH
    	v0nMJHIGH=$Av0nMJHIGH
    	v1nJ=$Av1nJ
    	v1nMJ=$Av1nMJ
    	ZCAPstart=$AZCAPstart
    	ZCAPend=$AZCAPend
    	ZCAPtargetE=$AZCAPtargetE
    	ZspecCAPstart=$AZspecCAPstart
    	ZspecCAPend=$AZspecCAPend
    	ZspecCAPtargetE=$AZspecCAPtargetE
    	RCAPstart=$ARCAPstart
    	RCAPend=$ARCAPend
    	RCAPtargetE=$ARCAPtargetE
    	dt=$Adt
    	nt=$Ant
    	NODEv0LOW=$ANODEv0LOW
    	NODEv0HIGH=$ANODEv0HIGH
    	NODEv1=$ANODEv1
        HIGHLOWswitch=$AHIGHLOWswitch
        NodeOFFSET=$ANODEOFFS
      elif [ $WP == "B" ]; then
    	WPname=$BWPname
    	EminWP=$BEminWP
    	EmaxWP=$BEmaxWP
    	Z0WP=$BZ0WP
    	nZ=$BnZ
    	Zstart=$BZstart
    	dZ=$BdZ
    	nZspec=$BnZspec
    	nR=$BnR
    	Rstart=$BRstart
    	dR=$BdR
    	nX=$BnX
    	nY=$BnY
    	v0nJLOW=$Bv0nJLOW
   	v0nMJLOW=$Bv0nMJLOW
    	v0nJHIGH=$Bv0nJHIGH
    	v0nMJHIGH=$Bv0nMJHIGH
    	v1nJ=$Bv1nJ
    	v1nMJ=$Bv1nMJ
    	ZCAPstart=$BZCAPstart
    	ZCAPend=$BZCAPend
    	ZCAPtargetE=$BZCAPtargetE
    	ZspecCAPstart=$BZspecCAPstart
    	ZspecCAPend=$BZspecCAPend
    	ZspecCAPtargetE=$BZspecCAPtargetE
    	RCAPstart=$BRCAPstart
    	RCAPend=$BRCAPend
    	RCAPtargetE=$BRCAPtargetE
    	dt=$Bdt
    	nt=$Bnt
    	NODEv0LOW=$BNODEv0LOW
    	NODEv0HIGH=$BNODEv0HIGH
    	NODEv1=$BNODEv1
        HIGHLOWswitch=$BHIGHLOWswitch
        NodeOFFSET=$BNODEOFFS
      elif [ $WP == "C" ]; then 
        WPname=$CWPname
    	EminWP=$CEminWP
    	EmaxWP=$CEmaxWP
    	Z0WP=$CZ0WP
    	nZ=$CnZ
    	Zstart=$CZstart
    	dZ=$CdZ
    	nZspec=$CnZspec
    	nR=$CnR
    	Rstart=$CRstart
    	dR=$CdR
    	nX=$CnX
    	nY=$CnY
    	v0nJLOW=$Cv0nJLOW
    	v0nMJLOW=$Cv0nMJLOW
    	v0nJHIGH=$Cv0nJHIGH
    	v0nMJHIGH=$Cv0nMJHIGH
    	v1nJ=$Cv1nJ
    	v1nMJ=$Cv1nMJ
    	ZCAPstart=$CZCAPstart
    	ZCAPend=$CZCAPend
    	ZCAPtargetE=$CZCAPtargetE
    	ZspecCAPstart=$CZspecCAPstart
    	ZspecCAPend=$CZspecCAPend
    	ZspecCAPtargetE=$CZspecCAPtargetE
    	RCAPstart=$CRCAPstart
    	RCAPend=$CRCAPend
    	RCAPtargetE=$CRCAPtargetE
    	dt=$Cdt
    	nt=$Cnt
    	NODEv0LOW=$CNODEv0LOW
    	NODEv0HIGH=$CNODEv0HIGH
    	NODEv1=$CNODEv1
        HIGHLOWswitch=$CHIGHLOWswitch
        NodeOFFSET=$CNODEOFFS
      elif [ $WP == "D" ]; then
        WPname=$DWPname
    	EminWP=$DEminWP
    	EmaxWP=$DEmaxWP
    	Z0WP=$DZ0WP
    	nZ=$DnZ
    	Zstart=$DZstart
    	dZ=$DdZ
    	nZspec=$DnZspec
    	nR=$DnR
    	Rstart=$DRstart
    	dR=$DdR
    	nX=$DnX
    	nY=$DnY
    	v0nJLOW=$Dv0nJLOW
    	v0nMJLOW=$Dv0nMJLOW
    	v0nJHIGH=$Dv0nJHIGH
    	v0nMJHIGH=$Dv0nMJHIGH
    	v1nJ=$Dv1nJ
    	v1nMJ=$Dv1nMJ
    	ZCAPstart=$DZCAPstart
    	ZCAPend=$DZCAPend
    	ZCAPtargetE=$DZCAPtargetE
    	ZspecCAPstart=$DZspecCAPstart
    	ZspecCAPend=$DZspecCAPend
    	ZspecCAPtargetE=$DZspecCAPtargetE
    	RCAPstart=$DRCAPstart
    	RCAPend=$DRCAPend
    	RCAPtargetE=$DRCAPtargetE
    	dt=$Ddt
    	nt=$Dnt
    	NODEv0LOW=$DNODEv0LOW
    	NODEv0HIGH=$DNODEv0HIGH
    	NODEv1=$DNODEv1
        HIGHLOWswitch=$DHIGHLOWswitch
        NodeOFFSET=$DNODEOFFS
      elif [ $WP == "E" ]; then
        WPname=$EWPname
        EminWP=$EEminWP
        EmaxWP=$EEmaxWP
        Z0WP=$EZ0WP
        nZ=$EnZ
        Zstart=$EZstart
        dZ=$EdZ
        nZspec=$EnZspec
        nR=$EnR
        Rstart=$ERstart
        dR=$EdR
        nX=$EnX
        nY=$EnY
        v0nJLOW=$Ev0nJLOW
        v0nMJLOW=$Ev0nMJLOW
        v0nJHIGH=$Ev0nJHIGH
        v0nMJHIGH=$Ev0nMJHIGH
        v1nJ=$Ev1nJ
        v1nMJ=$Ev1nMJ
        ZCAPstart=$EZCAPstart
        ZCAPend=$EZCAPend
        ZCAPtargetE=$EZCAPtargetE
        ZspecCAPstart=$EZspecCAPstart
        ZspecCAPend=$EZspecCAPend
        ZspecCAPtargetE=$EZspecCAPtargetE
        RCAPstart=$ERCAPstart
        RCAPend=$ERCAPend
        RCAPtargetE=$ERCAPtargetE
        dt=$Edt
        nt=$Ent
        NODEv0LOW=$ENODEv0LOW
        NODEv0HIGH=$ENODEv0HIGH
        NODEv1=$ENODEv1
        HIGHLOWswitch=$EHIGHLOWswitch
        NodeOFFSET=$ENODEOFFS
      fi	

      #now we need to figure out which flavor of the wave packet we want
      #either v0LOW, v0HIGH, or v1
      if [ "$qnu" == "0" ] && [ "$qj" -lt "$HIGHLOWswitch" ]; then
        dvrJ=$v0nJLOW
        dvrMJ=$v0nMJLOW
        NODE=$NODEv0LOW
      elif [ "$qnu" == "0" ] && [ "$qj" -ge "$HIGHLOWswitch" ]; then
        dvrJ=$v0nJHIGH
        dvrMJ=$v0nMJHIGH
        NODE=$NODEv0HIGH
      elif [ "$qnu" == "1" ]; then
        dvrJ=$v1nJ
        dvrMJ=$v1nMJ
        NODE=$NODEv1
      fi

      #lastly we need to subtract 1 from dvrJ and dvrMJ if J is uneven
      if [ $(( $qj % 2 )) != 0 ]; then
        ((dvrJ--))
        ((dvrMJ--))
      fi

      #calculate tend
      tend=`echo "$nt * $dt" | bc `
      #RAM estimate
      Vpoints=`echo "($ZCAPstart - $Zstart) / $dZ " | bc `
      RAMestimate=`echo "scale=2; ( ($nZ * $nR * $nX * $nY * ( $dvrJ + 1 ) * ( (2 * $dvrMJ) + 1 ) * 16) + ( $Vpoints * $nR * $nX * $nY * ( $dvrJ + 1 ) * ( (2 * $dvrMJ) + 1 ) * 8 ) + ( $nX * $nY * ( $dvrJ + 1 ) * ( (2 * $dvrMJ) + 1 ) * 16  ) ) / 1000000000 " | bc -l`
      NodeEST=`echo "scale=2; $RAMestimate / ${RAMPERNODE} " | bc -l`
      NodeESTint=`python -c "from math import ceil; print ceil($NodeEST)"`
      #NodeESTint=`echo "scale=0; $NodeESTint / 1 " |bc `
      NodeESTint=`echo "scale=0; ($NodeESTint + $NodeOFFSET )/ 1 " |bc `

      NODE=$NodeESTint
      
#########################################################################
###########STATE LOOP -- FOR submitting JOBS################################
#now I can walk over the selected states 
state=v${qnu}j${qj}mj${qmj}
WPfolder="${WP}.${WPname}"
echo "submitting: STATE= $state  WP= $WP $WPname dvrJ= $dvrJ   dvrMJ= $dvrMJ  NODE=$NODE"

#create folder structure
if [ ! -d "${state}" ]; then #if state folder does not exist; make state folder
  mkdir ${state}
fi
cd ${state}
if [ ! -d "${WPfolder}" ]; then
  mkdir ${WPfolder}
fi 
cd ${WPfolder}

cat >run.info<<EOF
0		#runnumber
0		#tstart
${tend}		#tend
${nt}		#nt
${dt}		#dt
${state}	#state
${WP}		#WP
${WPname}	#WPname
EOF

#create DVR input
#create DVR.inp
cat >DVR.inf<<EOF
# Add a title for this input.
Title          :STRING:     "${state}-${WPname}";     # the title of this run...

# System and new calculation or restart.
SystemID       :STRING:     "${SystemID}";            # the system for which to do a calculation...
CRPlibDataSet  :STRING:     "${POTNAME}";
NewCalc        :LOGICAL:    BOOLnewcalc;                       # this is a new calculation...
ZDoF           :LOGICAl:    TRUE;                       # include Z degree of freedom...
RDoF           :LOGICAl:    TRUE;                       # include r degree of freedom...
XDof           :LOGICAl:    TRUE;                       # include X degree of freedom...
YDof           :LOGICAl:    TRUE;                       # include Y degree of freedom...
ThetaDof       :LOGICAl:    TRUE;                       # include Theta degree of freedom...
PhiDof         :LOGICAl:    TRUE;                       # include phi degree of freedom...

# Atom masses and hom-nuclear or hetero-nuclear.
HomoNuc        :LOGICAL:    TRUE;                       # H2 is a homonuclear system...
Mass1          :REAL:       ${Mass1};                     # the mass of atom 1 in u...
Mass2          :REAL:       ${Mass2};                     # the mass of atom 2 in u...

# The scattering grid and specular grid in Z.
nZ             :INTEGER:    ${nZ};                         # nr points in z...
nZSpecular     :INTEGER:    ${nZspec};                         # size of specular grid...
Zstart         :REAL:       ${Zstart};                       # start value in z in bohr...
dZ             :REAL:       ${dZ};                       # step size in z in bohr...

# The scattering grid and specular grid in r, which are identical.
nR             :INTEGER:    ${nR};                         # nr points in r...
Rstart         :REAL:       ${Rstart};                        # start value in r in bohr...
dR             :REAL:       ${dR};                       # step size in r in bohr...

# The scattering grids in X and Y.
nX             :INTEGER:    ${nX};                          # nx points in X...
nY             :INTEGER:    ${nY};                          # ny points in Y...

# The rotational basis to use.
JMax           :INTEGER:    ${dvrJ};                          # the maximum j in the grid rep....
MjMax          :INTEGER:    ${dvrMJ};                          # the maximum mj in the grid rep ( 0 <= MjMax <= JMax )...

# The projection operator type to use.
ProjOp         :INTEGER:    2;                          # the projection operator type to use (0, 1 or 2)...

# The propagation parameters for split operator.
tStart         :REAL:       INTtstart;                        # starting time of this run...
tEnd           :REAL:       INTtend;                     # ending time of this run...
dtStep         :REAL:       DOUBLEdt;                       # timestep of split operator propagator...
nStep          :INTEGER:    INTnt;                        # number of timesteps to take...

# The analysis parameters.
onlyAnalysis   :LOGICAL:    FALSE;                      # only do analysis run...
alsoAnalysis   :LOGICAL:    TRUE;                       # also perform analysis (after propagation)...
dtAna          :REAL:       ${dtAna};                       # the timestep during analysis in aut...
DynamZSpAna    :LOGICAL:    TRUE;                       # initially using a dynamical analysis surface on specular grid...
dtAve          :REAL:       ${dtAve};                         # print norm and expectation values every dtAverage
ZAna           :REAL:       ${ZCAPstart};                        # asymptotic analysis point in z in bohr...
NrOfEs         :INTEGER:    ${NrOfEs};                         # calculate on-the-fly analysis at given nr of energies within
                                                          #+ wavepacket...
# The flux analysis parameters.
doFluxAnalysis :LOGICAL:    FALSE;                       # If true, a flux analysis in r is performed.
rFluxAnalysis  :REAL:       ${RCAPstart};                        # Flux analysis surface in r is placed at this coordinate.
EMinFluxAna    :REAL:       ${EminWP};                       # Lowest energy in the flux analysis.
EMaxFluxAna    :REAL:       ${EmaxWP};                       # Highest energy in the flux analysis.
DeltaEFluxAna  :REAL:       0.005;                       # Energy separation in the flux analysis.

# The initial wavepacket.
EMinWP         :REAL:       ${EminWP};                       # minimum normal incidence energy in eV...
EMaxWP         :REAL:       ${EmaxWP};                       # maximum normal incidence energy in eV...
Z0             :REAL:       ${Z0WP};                       # initial location of the center of the wavepacket...

# The initial quantum state of the incident molecule.
VIn            :INTEGER:    ${qnu};                          # v in quantum number...
JIn            :INTEGER:    ${qj};                          # j in quantum number...
MjIn           :INTEGER:    ${qmj};                          # mj in quantum number...

# Concerning the potential.
potential      :LOGICAL:    FALSE;                      # only compute the potential...
imposeVMin     :LOGICAL:    FALSE;                      # impose a minimum on the potential...
VMin           :REAL:       -5.50;                      # minimum imposed on the potential
imposeVMax     :LOGICAL:    TRUE;                       # impose a maximum on the potential...
VMax           :REAL:       10.0;                       # maximum imposed on the potential...
UseSymmetry    :LOGICAL:    FALSE;                       # Exploit symmetry in setting up the potential...
nPhisOptimal   :LOGICAL:    FALSE;                      # Chooses nPhis to fully exploit potential symmetry...

# The optical potential on the scattering grid in Z.
ZOptStart      :REAL:       ${ZCAPstart};                        # start of optical potential in z...
ZOptEnd        :REAL:       ${ZCAPend};                      # end of optical potential in z...
ZOptEnergy     :REAL:       ${ZCAPtargetE};                       # the energy for which to absorb optimally...

# The optical potential on the scattering grid in r.
ROptStart      :REAL:       ${RCAPstart};                       # start of optical potential in r...
ROptEnd        :REAL:       ${RCAPend};                       # end of optical potential in r...
ROptEnergy     :REAL:       ${RCAPtargetE};                       # the energy for which to absorb optimally...

# The optical potential on the specular grid in Z.
ZSpOptStart    :REAL:       ${ZspecCAPstart};                       # start of optical potential on specular grid...
ZSpOptEnd      :REAL:       ${ZspecCAPend};                       # end of optical potential on specular grid...
ZSpOptEnergy   :REAL:       ${ZspecCAPtargetE};                       # the energy for which to absorb optimally...

# Initial motion parallel to the surface.
PhiParallel    :REAL:       0.0;                        # azimuthal angle of incidence, wrt the x-axis, in degrees...
EParallel      :REAL:       0.0;                        # initial energy in motion parallel to the surface

# Switching function for faster decay of the potential for large Z.
UseSwitching   :LOGICAL:    FALSE;                      # if true, the potential is switched to the gas phase quicker...
ZMinSwitch     :REAL:       6.5;                        # switching the potential starts here...
ZMaxSwitch     :REAL:       7.0;                        # switching the potential ends here...

# Writing results to file.
writeSMatrix   :LOGICAL:    TRUE;                      # S-matrix elements are written to unit 3...
writeProbs     :LOGICAL:    TRUE;                       # S-matrix elements at the analysis energies written to unit 25...
writeRDProbs   :LOGICAL:    TRUE;                       # rotational-diffractional probabilities written to output fil10...

# Tolerances
TolSp          :REAL:       1.0E-9;                     # Tolerances for transferring specular grid to scattering grid...

# Writing 2D PES to file for the coordinates given.
write2DPES     :LOGICAL:    FALSE;                       # It true, only compute 2D PES, write to file and quit program.
#write2DPES     :LOGICAL:    FALSE;                       # It true, only compute 2D PES, write to file and quit program.
x2DPES         :REAL:       2.0;                        # x coordinate for the 2D PES.
y2DPES         :REAL:       1.0;                        # y coordinate for the 2D PES.
theta2DPES     :REAL:       0.0;                       # theta coordinate for the 2D PES (degrees).
phi2DPES       :REAL:       0.0;                        # phi coordinate for the 2D PES (degrees).
EOF

#now copy DVR.inp to input log foler
#in the job file we are going to use the input from the logfolder
cp DVR.inf ${LOGinputs}/${state}.${WP}.${WPname}.DVR

#make Job script
cat >spodvrjob_inf<<EOF
#!/bin/bash
#PBS -l walltime=168:00:00
#PBS -S /bin/bash
#PBS -lnodes=${NODE}:ppn=32
#PBS -N ${state}.${WP}0.${WPname}


#workdirectory (this is on glusterfs)
#WORKDIR=\$PBS_O_WORKDIR
WORKDIR=\${SLURM_SUBMIT_DIR}

#read info from run.info
cd \${WORKDIR}
runversion=\`awk "NR == 1 " run.info | awk '{print \$1 }' \`
tstart=\`awk "NR == 2 " run.info | awk '{print \$1 }' \`
tend=\`awk "NR == 3 " run.info | awk '{print \$1 }' \`
nt=\`awk "NR == 4 " run.info | awk '{print \$1 }' \`
dt=\`awk "NR == 5 " run.info | awk '{print \$1 }' \`
state=\`awk "NR == 6 " run.info | awk '{print \$1 }' \`
WP=\`awk "NR == 7 " run.info | awk '{print \$1 }' \`
WPname=\`awk "NR == 8 " run.info | awk '{print \$1 }' \`

previousrun=\${runversion}
((previousrun--))
nextrun=\${runversion}
((nextrun++))

STATE="\${state}.\${WP}\${runversion}.\${WPname}" 

STORAGE="run_\${runversion}"
mkdir \${STORAGE}

#creat DVR
if [ "\${runversion}" == "0" ]; then
  cat DVR.inf | awk '{gsub("BOOLnewcalc","TRUE"); print }' | awk '{gsub("INTtstart","'\${tstart}'"); print}' | awk '{gsub("INTtend","'\${tend}'"); print}' | awk '{gsub("DOUBLEdt","'\${dt}'"); print }' | awk '{gsub("INTnt","'\${nt}'"); print}' > DVR.\${runversion}.inp
else
  cat DVR.inf | awk '{gsub("BOOLnewcalc","FALSE"); print }' | awk '{gsub("INTtstart","'\${tstart}'"); print}' | awk '{gsub("INTtend","'\${tend}'"); print}' | awk '{gsub("DOUBLEdt","'\${dt}'"); print }' | awk '{gsub("INTnt","'\${nt}'"); print}' > DVR.\${runversion}.inp
fi


#scratch (this local on compute node)
#SCRATCH=/scratch/\${USER}/\${PBS_JOBID}
SCRATCH=/scratch/\${USER}/\${SLURM_JOBID}

# Set OMP environmental variables. For a serial calculation, these have no effect.
OMP_NUM_THREADS=32
export OMP_NUM_THREADS

# Set the stack size to unlimited.
ulimit -s unlimited

#go to scratch
cd \${SCRATCH}

if [ "\${runversion}" == "0" ]; then
  cp \${WORKDIR}/DVR.\${runversion}.inp DVR.inp
else
  cp \${WORKDIR}/DVR.\${runversion}.inp DVR.inp
  cp \${WORKDIR}/run_\${previousrun}/fort.9 fort.8
  cp \${WORKDIR}/run_\${previousrun}/fort.21 fort.22
  cp \${WORKDIR}/*.pes .
fi

#copy executable to folder--> No, we'll run by reference
#cp $BINARY spo-dvr.x

#CRPlibrary
cp -r ${LOGfolder}${POTNAME} .


# Copy start date and time to output file.
echo "Starting date and time: "\`date\` > spo-dvr.out
echo
echo "Calculation run on nodes/processors:" >> spo-dvr.out
cat \${PBS_NODEFILE} >> spo-dvr.out
echo

#run program
if [ "${NODE}" == "1" ]; then
${BINARY_ALT} >> spo-dvr.out
else
mpiexec -pernode -x OMP_NUM_THREADS ${BINARY} >> spo-dvr.out
fi
rm -r ${POTNAME}

# Copy end date and time to output file.
echo "Ending date and time: "\`date\` >> spo-dvr.out

#extract data from spo-dvr.out
starttable=\`awk '/info from probaf/{print NR}' spo-dvr.out\`
let starttable=starttable+8

endtable=\`awk '/PRINTING SCATTERING AND/{print NR}' spo-dvr.out\`
let endtable=endtable-6

variable="NR>=\$starttable && NR<=\$endtable"

awk  "\$variable"  spo-dvr.out > \${STATE}.ReacProb

#copy the reaction probability to safe /home/
scp \${STATE}.ReacProb ${LOGReacProb}/.

#copy output to safe /home/
scp spo-dvr.out ${LOGout}/\${STATE}.out

#--------------- CALC NORM
#MY VERSION
#Check remaining norm
#linenr=\`awk '/Main.f90 deleteWaveFunction/{print NR}' spo-dvr.out \`
#((linenr--))
#((linenr--))
#norm=\`awk ' NR == '\$linenr' {print}' spo-dvr.out | awk '{print \$2}'\`

#SVN version
linenr=\`awk '/info from reading or writing asymptotic info/{print NR}' spo-dvr.out \`
((linenr--))
((linenr--))
((linenr--))
((linenr--))
((linenr--))
norm=\`awk ' NR == '\$linenr' {print}' spo-dvr.out | awk '{print \$2}'\`

#copy back
scp -r \${SCRATCH}/* \${WORKDIR}/\${STORAGE}/.
rm \${WORKDIR}/\${STORAGE}/*pes
if [ "\${runversion}" == "0" ]; then
  scp -r \${SCRATCH}/*pes \${WORKDIR}/.
fi
rm -r \${SCRATCH}/*


#go to workdir 
cd \${WORKDIR}

#check if copy succesfull, delete previous
#if [ -f \${WORKDIR}/\${STORAGE}/fort.9 ] && [ -f \${WORKDIR}/\${STORAGE}/fort.21 ] && [ -f \${WORKDIR}/\${STORAGE}/spo-dvr.out ]; then
#  if [ -d \${WORKDIR}/run_\${previousrun}
#    rm -r \${WORKDIR}/run_\${previousrun}
#  fi
#fi

#update jobfile name
nextSTATE="\${state}.\${WP}\${nextrun}.\${WPname}"
cat spodvrjob_inf | awk '{gsub("#PBS -N '\${STATE}'","#PBS -N '\${nextSTATE}'"); print}' > bla ; mv bla spodvrjob_inf

#generate new run.info
mv run.info run.info_\${runversion}

newtstart=\${tend}
newtend=\`echo " \${tend} + ( \${nt} * \${dt} ) " | bc -l \`

cat >run.info<<blaat
\${nextrun} 	#runversion
\${newtstart}               #tstart
\${newtend}         #tend
\${nt}           #nt
\${dt}           #dt
\${state}        #state
\${WP}           #WP
\${WPname}       #WPname
blaat

echo
echo "runinfo: "
cat run.info
echo 

autosub="$autosub"

#if not converged, continue 
#else calculate inelastic scattering probs
if (( \$( echo "\${norm} > 0.01 " | bc -l) )); then
  echo "bla1"
  if [ "\${autosub}" == "TRUE" ]; then
    echo "bla2"
    ssh ${HEADNODE} "cd \${WORKDIR} ; qsub spodvrjob_inf "
    echo "bla3"
  fi
else

  #goto place with fort.10
  #cd \${WORKDIR}/\${STORAGE}

  #cp /home/guido/scripts/SPO-DVR/extract-state-to-state .
  #bash extract-state-to-state

  #cp \${state}-to-v1j3.dat ../.
  #cp \${state}-to-v1j3.dat ${LOGinelastic}/.


fi #autosub

EOF


continuevar=0
while [ "$continuevar" != "1" ]
do
  runningjobs=`qstat -u guido | grep v | wc -l`
  maxQ=`head -1 ${LOGfolder}/STOPCAR | awk '{print $2}'`
  if [ "$runningjobs" -le "$maxQ" ]; then
    continuevar=1
  else
    echo "sleepy time $WP $WPname " `date`  
    sleep 60
  fi
done
continuevar=0
while [ "$continuevar" != "1" ]
do
  STOPCAR=`head -1 ${LOGfolder}/STOPCAR | awk '{print $1}'`
  maxQ=`head -1 ${LOGfolder}/STOPCAR | awk '{print $2}'`
  if [ "$STOPCAR" == "STOP" ];  then
    continuevar=0
    echo "STOPCAR sleepy time $WP $WPname " `date`
    sleep 60
  else 
    continuevar=1
  fi
done
#now submit the job
if [ ! -f DVR.0.inp ]; then
  JobID=$( qsub spodvrjob_inf )
  echo "${JobID} STATE = $state  WPname= $WP $WPname"  >> ${LOGfolder}/SubmittedJobs.txt
fi

cd ..  #go back to state folder
cd ..  #go back to run directory

###########END STATE LOOP###############################################
#########################################################################    

        #increment job counter
        let count++
      done #for WP
      echo
      #increment qmj number
      let qmj=qmj+1
    done #while qmj
    echo
    echo
    echo
    #increment qj number
    let qj=qj+1
  done #while qj
  echo
done #for qnu

