@echo off
echo.

::===================================
::  Directory/file configuration
::===================================
set MPIRUNPath=C:\Program Files\MPICH2\bin\mpiexec.exe 
set nCore=4

set ProgramsDir=d:\Programs
if not exist "%ProgramsDir%" (
	set ProgramsDir=c:\Programs
)
set tee=%ProgramsDir%\bin\ktee.exe -f
set PerlDir=%ProgramsDir%\Perl

set ElasticDir=%PerlDir%\ElasticConstant
set TemplateSource=%ElasticDir%\Template
set script=%ElasticDir%\CalElasticConstant.pl

set QEPerlDir=%ProgramsDir%\Perl\QE
set QERoot=C:\Program Files\Quantum ESPRESSO 64-bit 5.2.1-mpich2\bin
set CMD="%QERoot%\pw.exe"
set MPICMD="%MPIRUNPath%" -n %nCore% "%QERoot%\pw.exe"

set BatchFileDir=%~dp0
set RootDir=%BatchFileDir%
set TemplateDir=%RootDir%Template
set MakeRunScript=CalculateElasticConstant.bat

::===========================
::  Calculation condition
::===========================
set SampleName=%1
if "%SampleName%" == "" (
	set SampleName={SampleName}
)
if "%SampleName%" == "" (
	echo Error: SampleName should be specified.
	goto USAGE
)
::Remove extension
set SampleName=%SampleName:.cif=%

::Engine: gulp, vasp, pwscf
set Engine=%2
if "%Engine%" == "" (
	set Engine={Engine}
)

set RunScript=DoPWSCF.bat
set RunScriptSource=%TemplateDir%\%RunScript%

set CIFFile={CIFFile}
if not exist "%CIFFile%" (
	set CIFFile=%SampleName%.cif
)
set OriginalDir={OriginalDir}
set OriginalCIF={OriginalCIF}
set OriginalFileHeader=%OriginalCIF:.cif=%
set OriginalInput={OriginalInput}


::===============================
::  PWSCF init / update parameters
::===============================
set UseBravaisLattice={UseBravaisLattice}
if "%UseBravaisLattice%" == "" (
	set UseBravaisLattice=0
)
set BurstToP1={BurstToP1}
if "%BurstToP1%" == "" (
	set BurstToP1=1
)

rem USPP NC PAW SL 1/r
set PPType=PAW,USPP,NC
rem LDA SLA PZ PBE PBESOL LYP B88 P86 BLYP PW TPSS GGC GGX NOG NOGC NOGX PBC PBX PSC PSX
set Functional=PBE
rem All, Selected, No
set PPCopyMode=Selected
set aKProduct=1.5
set SpinPolarized=1

::Function: scf nscf relax vc-relax relax bands md phonon
set Function={Function}

::set pressure=0.001 kBar
set pressure={pressure}

set ecutwfc=x1.0,30.0
set ecutrho=x1.0,140.0
set nbnd=x1.2

set nRelaxationStep={nRelaxationStep}
set e_tol={e_tol}
set F_tol={F_tol}
set conv_thr={conv_thr}
set pressure_tol={pressure_tol}


:: Define eArrayF, eArrayR (strain), cifArrayF, cifArrayR (CIF), and dirArrayF, dirArrayR (WorkDir)
:: e.g. set eArrayR[0]=-0.1
::      set cifArrayR[0]=e1111_-0.1.cif
::      set dirArrayR[0]=e1111_-0.1
{ArrayString}

::===============================
::  Print configuration
::===============================
::@echo on
echo RootDir    : %RootDir%
echo TemplateDir: %TemplateDir%
echo SampleName : %SampleName%
echo Mode       : %Mode%
echo.


echo.
:: Run for the reverse direction from the base structure
echo Reverse e1 FileList: [%eArrayR[0]%][%eArrayR[1]%][%eArrayR[2]%][%eArrayR[3]%][%eArrayR[4]%]
setlocal ENABLEDELAYEDEXPANSION
for /l %%n in (0,1,%nR%) do (
	if not "!eArrayR[%%n]!" == "" (
		echo call :RUN !eArrayR[%%n]! !cifArrayR[%%n]! !dirArrayR[%%n]!
		call :RUN !eArrayR[%%n]! !cifArrayR[%%n]! !dirArrayR[%%n]!
	)
)
endlocal

echo.
:: Run for the zero e1 line from the base structure
echo Zero e1 FileList: [%eArrayZ[0]%][%eArrayZ[1]%][%eArrayZ[2]%][%eArrayZ[3]%][%eArrayZ[4]%]
setlocal ENABLEDELAYEDEXPANSION
for /l %%n in (0,1,%nZ%) do (
	if not "!eArrayZ[%%n]!" == "" (
		echo call :RUN !eArrayZ[%%n]! !cifArrayZ[%%n]! !dirArrayZ[%%n]!
		call :RUN !eArrayZ[%%n]! !cifArrayZ[%%n]! !dirArrayZ[%%n]!
	)
)
endlocal

echo.
:: Run for the forward direction from the base structure
echo Forward e1 FileList: [%eArrayF[0]%][%eArrayF[1]%][%eArrayF[2]%][%eArrayF[3]%][%eArrayF[4]%]
setlocal ENABLEDELAYEDEXPANSION
for /l %%n in (0,1,%nF%) do (
	if not "!eArrayF[%%n]!" == "" (
		echo call :RUN !eArrayF[%%n]! !cifArrayF[%%n]! !dirArrayF[%%n]!
		call :RUN !eArrayF[%%n]! !cifArrayF[%%n]! !dirArrayF[%%n]!
	)
)
endlocal

goto END


::===================================
::===================================
:: Subroutines
::===================================
::===================================

:RUN
::=====================================================
:: Run calculation engine in the directory given by %1
::=====================================================
@echo off
if "%Function%" == "vc-relax" (
	set e=0
	set P=%1
) else (
	set e=%1
	set P={pressure}
)
set CIFFile=%2
set WorkDir=%3
::Remove extension
set header=%CIFFile:.cif=%

echo.
:echo h[%1][%2][%3][%CIFFile%][%WorkDir][%e%]

if not exist "%CIFFile%" (
	echo File [%CIFFile%] does not exist.
	echo Skip.
) else if exist "%WorkDir%" (
	echo Directory [%WorkDir%] exists.
	echo Skip.
) else (
	echo mkdir %WorkDir%
	mkdir "%WorkDir%"
	echo copy "%CIFFile%" "%WorkDir%"
	copy "%CIFFile%" "%WorkDir%"
	echo copy "%RunScriptSource%" "%WorkDir%\%RunScript%"
	copy "%RunScriptSource%" "%WorkDir%\%RunScript%"
	echo cd "%WorkDir%"
	cd "%WorkDir%"

	echo Working in:
	cd
	echo ...

	echo *********************************
	echo **  Make PWSCF input files for function "%Function%" **
	echo *********************************
	echo perl %QEPerlDir%\QE.pl --Action=MakeInputFiles --Function=%Function% ^
		--PPType=%PPType% --PPCopyMode=%PPCopyMode% ^
		--SpinPolarized=%SpinPolarized% --UseBravaisLattice=1 --UseCELLDM=1 --aKProduct=%aKProduct% ^
		--ecutrho=%ecutrho% --nbnd=%nbnd% ^
		--etot_conv_thr=%e_tol% --forc_conv_thr=%F_tol% --conv_thr=%conv_thr% ^
		--press=%P% --press_conv_thr=%pressure_tol% --nstep=%nRelaxationStep% --DebugMode=0 ^
		%header%.cif %header%.pwin
	perl %QEPerlDir%\QE.pl --Action=MakeInputFiles --Function=%Function% ^
		--PPType=%PPType% --PPCopyMode=%PPCopyMode% ^
		--SpinPolarized=%SpinPolarized% --UseBravaisLattice=1 --UseCELLDM=1 --aKProduct=%aKProduct% ^
		--ecutrho=%ecutrho% --nbnd=%nbnd% ^
		--etot_conv_thr=%e_tol% --forc_conv_thr=%F_tol% --conv_thr=%conv_thr% ^
		--press=%P% --press_conv_thr=%pressure_tol% --nstep=%nRelaxationStep% --DebugMode=0 ^
		%header%.cif %header%.pwin
	call %RunScript% %header% exec
	call %RunScript% %header% post
	
	rmdir /S /Q *_qe_data
)

echo.
echo cd to RootDir [%RootDir%]
cd %RootDir%
exit /b


:Usage
::============================
::  Show Usage and terminate
::============================
echo Usage: %MakeRunScript% SampleName
goto END


:END
::============================
::  Terminate
::============================
::call GoGULP.bat %SampleName% merge MD-NVT* MD-NPT*
echo .
