@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=%

::Engine: gulp, vasp, pwscf
set Engine=%2
if "%Engine%" == "" (
	set Engine=pwscf
)

set RunScript=%TemplateDir%\DoPWSCF.bat

set OriginalCIF=original.cif
set OriginalFileHeader=%OriginalCIF:.cif=%

:: Pressure list in kBar
set PListReverse={PListReverse}
set PListForward={PListForward}


::===============================
::  PWSCF init / update parameters
::===============================
set CIFFile=%SampleName%.cif
::set CIFFile=auto

::Function: scf nscf relax vc-relax relax bands md phonon
set Function=vc-relax
::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


::===============================
::  Print configuration
::===============================
::@echo on
echo RootDir    : %RootDir%
echo TemplateDir: %TemplateDir%
echo SampleName : %SampleName%
echo FileList for reverse scan: [%PListReverse%]
echo FileList for forward scan: [%PListForward%]
echo.

:: Run for the reverse direction from the base structure
for %%a in ( %PListReverse% ) do (
@echo on
	call :RUN original P%%a %%a
@echo off
)

:: Run for the forward direction from the base structure
echo FileList: [%PListForward%]
for %%a in ( %PListForward% ) do (
@echo on
	call :RUN original P%%a %%a
@echo off
)

goto END


::===================================
::===================================
:: Subroutines
::===================================
::===================================


:END
::===================================
:: Finalize
::===================================
::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
::=====================================================
:: Run calculation engine in the directory given by %1
::=====================================================
@echo off
set header=%1
set CIFFile=%1.cif
set WorkDir=%2
set P=%3
echo.
::echo h[%1][%2][%3][%CIFFile%][%WorkDir][%P%]

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

