@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%" "%QERoot%\pw.exe" -n %nCore%

set BatchFileDir=%~dp0
set RootDir=%BatchFileDir%
set TemplateDir=%RootDir%Template

::===========================
::  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=%

set RunScript=%TemplateDir%\DoPWSCF.bat


::===============================
::  PWSCF init / update parameters
::===============================
set CIFFile=%SampleName%.cif
::set CIFFile=auto

::Function: scf nscf relax vc-relax relax bands md phonon
set Function=scf
::set pressure=0.001 kBar
set pressure={pressure}
set pressure_tol={pressure_tol}

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
set ecutwfc=x1.0,30.0
set ecutrho=x1.0,140.0
set nbnd=x1.2

set OriginalCIF=original.cif
set OriginalFileHeader=%OriginalCIF:.cif=%


:: 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.


:: Create base input files
::if exist "%OriginalCIF%" (
:::	echo File [%OriginalCIF%] exists.
::	echo Skip.
::) else (
::	echo call %RunScript% 0.0 %OriginalCIF% %OriginalFileHeader init
::	call %RunScript% 0.0 %OriginalCIF% %OriginalFileHeader init
::)

:: Run for the base structure
echo call :RUN 0.0 %OriginalCIF% %OriginalFileHeader%
call :RUN 0.0 %OriginalCIF% %OriginalFileHeader%

echo.
:: Run for the reverse direction from the base structure
echo Forward FileList: [%eArrayR[0]%][%eArrayR[1]%][%eArrayR[2]%][%eArrayR[3]%][%eArrayR[4]%]
setlocal ENABLEDELAYEDEXPANSION
for /l %%n in (0,1,%nR%) do (
	echo call :RUN !eArrayR[%%n]! !cifArrayR[%%n]! !dirArrayR[%%n]!
	call :RUN !eArrayR[%%n]! !cifArrayR[%%n]! !dirArrayR[%%n]!
)
endlocal

echo.
:: Run for the zero e1 from the base structure
echo Zero FileList: [%eArrayZ[0]%][%eArrayZ[1]%][%eArrayZ[2]%][%eArrayZ[3]%][%eArrayZ[4]%]
setlocal ENABLEDELAYEDEXPANSION
for /l %%n in (0,1,%nZ%) do (
	echo call :RUN !eArrayZ[%%n]! !cifArrayZ[%%n]! !dirArrayZ[%%n]!
	call :RUN !eArrayZ[%%n]! !cifArrayZ[%%n]! !dirArrayZ[%%n]!
)

echo.
:: Run for the forward direction from the base structure
echo Forward FileList: [%eArrayF[0]%][%eArrayF[1]%][%eArrayF[2]%][%eArrayF[3]%][%eArrayF[4]%]
setlocal ENABLEDELAYEDEXPANSION
for /l %%n in (0,1,%nF%) do (
	echo call :RUN !eArrayF[%%n]! !cifArrayF[%%n]! !dirArrayF[%%n]!
	call :RUN !eArrayF[%%n]! !cifArrayF[%%n]! !dirArrayF[%%n]!
)
endlocal

goto END


::===================================
:: Subroutines
::===================================

:END
::call GoGULP.bat %SampleName% merge MD-NVT* MD-NPT*
echo .
exit /b



:INITRUN
::===================================
:: Make initial input files and run
::===================================
set header=%1
if not exist "%header%.cif" (
	echo File [%header%.cif] does not exist.
	echo Skip.
) else if exist "%header%" (
	echo Directory [%header%] exists.
	echo Skip.
) else (
	echo Make [%header%]
	mkdir %header%
	echo copy [%header%.cif] to [%header%]
	copy %header%.cif %header%

	echo chdir to [%header%]
	cd %header%
	echo Working in:
	cd
	echo...

	echo *********************************
	echo **  Make PWSCF input files for function "%Function%" **
	echo *********************************
@echo on
	perl %QEPerlDir%\QE.pl --Action=MakeInputFiles --PPType=%PPType% --Function=%Function% --PPCopyMode=%PPCopyMode% ^
		--SpinPolarized=%SpinPolarized% --UseBravaisLattice=1 --UseCELLDM=1 --aKProduct=%aKProduct% ^
		--ecutrho=%ecutrho% --nbnd=%nbnd% ^
		--press=%P% --press_conv_thr=%pressure_tol% --DebugMode=0 ^
		%header%.cif %header%.pwin
@echo off

	echo call %RunScript% %header% exec...
	call %RunScript% %header% exec
	echo call %RunScript% %header% post...
	call %RunScript% %header% post
	echo call %RunScript% %header% update...
	call %RunScript% %header% update
)

echo.
echo cd to RootDir [%RootDir%]
cd %RootDir%
exit /b


::===================================
:: Run calculation engine in the directory given by %1
::===================================
:RUN
set e=%1
set cif=%2
set WorkDir=%3
if not exist "%cif%" (
	echo File [%cif%] does not exist.
	echo Skip.
) else if exist "%WorkDir%" (
	echo Directory [%WorkDir%] exists.
	echo Skip.
) else (
	echo Make [%WorkDir%]
	mkdir %WorkDir%
	echo copy [%cif%\] to [%WorkDir%]
	copy %cif% %WorkDir%

	echo chdir to [%WorkDir%]
	cd %WorkDir%
	echo Working in:
	cd
	echo ...

	echo call %RunScript% %cif% init...
	call %RunScript% %cif% init
	echo call %RunScript% %cif% exec...
	call %RunScript% %cif% exec
	echo call %RunScript% %cif% post...
	call %RunScript% %cif% post
)

echo .
echo cd to RootDir [%RootDir%]
cd %RootDir%
exit /b

:RUN
::=====================================================
:: Run calculation engine in the directory given by %1
::=====================================================
@echo off
set e=%1
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 on
	mkdir %WorkDir%
	copy %CIFFile% %WorkDir%
	cd %WorkDir%
@echo off
	echo Working in:
	cd
	echo ...

	echo *********************************
	echo **  Make PWSCF input files for function "%Function%" **
	echo *********************************
@echo on
	perl %QEPerlDir%\QE.pl --Action=MakeInputFiles --PPType=%PPType% --Function=%Function% --PPCopyMode=%PPCopyMode% ^
		--SpinPolarized=%SpinPolarized% --UseBravaisLattice=1 --UseCELLDM=1 --aKProduct=%aKProduct% ^
		--ecutrho=%ecutrho% --nbnd=%nbnd% ^
		--press=%P% --press_conv_thr=%pressure_tol% --DebugMode=0 ^
		%header%.cif %header%.pwin
	call %RunScript% %header% exec
	call %RunScript% %header% post
@echo off
)

echo.
echo cd to RootDir [%RootDir%]
cd %RootDir%
exit /b


:Usage
::============================
::  Show Usage and terminate
::============================
echo Usage: CalculateElasticConstant.bat SampleName
goto END

