@echo off

goto START


:Usage
::============================
::  Show Usage and terminate
::============================
echo "Usage: GoGULP.bat SampleName convert"
echo "       GoGULP.bat SampleName super nx ny nz"
echo "       GoGULP.bat SampleName init [optimize|MD-NVT|MD-NPT] (optional:LibraryName)"
echo "       GoGULP.bat SampleName init fit LibraryName CIF_to_be_fitted
echo "       GoGULP.bat SampleName exec"
echo "       GoGULP.bat SampleName post"
echo "       GoGULP.bat SampleName update"
echo "       GoGULP.bat SampleName modify [fit|optimize|MD-NVT|MD-NPT] parameters"
echo "                               parameters: equi_time prod_time timestep Temp0 Temp1 Pressure"
echo "       GoGULP.bat SampleName save DirToBeStored (optional flag for confirmation:no)"
echo "       GoGULP.bat SampleName merge"
echo "       GoGULP.bat SampleName clean"
goto END


:START

::===========================
::  directory configuration
::===========================
set ProgramsDir=c:\Programs
if not exist "%ProgramsDir%" (
	set ProgramsDir=d:\Programs
)

::set GULPDIR=%ProgramsDir%\MD\gulp-5.0
set GULPDIR=%ProgramsDir%\MD\gulp-5.0\x86
set GULPPATH=%GULPDIR%\gulp.exe
::set TEE=%ProgramsDir%\bin\cygwin\tee.exe
set TEE=%ProgramsDir%\bin\ktee.exe -f
set GULPPerlDir=%ProgramsDir%\Perl\GULP
set GULPplPath=%GULPPerlDir%\GULP.pl
set GULPplCmd=perl %GULPplPath%

::=================================
::  Task definition from argments
::=================================

set SampleName=%1
::Remove extension
set SampleName=%SampleName:.cif=%
set SampleName=%SampleName:.glp=%
set SampleName=%SampleName:.gin=%
set SampleName=%SampleName:.out=%
set SampleName=%SampleName:.axsf=%
set SampleName=%SampleName:.arc=%
set SampleName=%SampleName:.trg=%
set SampleName=%SampleName:.pre=%

::Task: convert init exec post update modify (debugexec)
set Task=%2

if "%1" == "clean" (
	set Task=%1
	set DoConfirm=%2
)

if "%Task%" == "save" (
	set TargetDir=%3
	set DoConfirm=%4
)

::=======================
::  convert parameters
::=======================
::No, P1, RhombHex, HexRhomb, HexOrtho, FCCPrim, BCCPrim, ACenterPrim, BCenterPrim, CCenterPrim
set ConvertLattice=
::set ConvertLattice=HexOrtho

::============================
::  init / update parameters
::============================
::fit optimize MD-NVT MD-NPT
set Function=%3
::set Function=fit
::set Function=optimize
::set Function=MD-NVT
::set Function=MD-NPT

set LibraryFile=
::set LibraryFile=IGZO.lib
::set LibraryFile=IGZO-Morse.lib
if "%Task%" == "init" (
	set LibraryFile=%4
	if "%Function%" == "fit" (
		set TargetFiles=%5
	)
)

set UseSpeciesCharges=yes
set CopyScript=0

set temperature=300,3000
set pressure=0.1

set timestep=0.002
set tscale=0.10
set equilibration=0.10
set production=10.0

set ResetTime=1

set sample=%timestep%
set write=0.004

if "%Task%" == "super" (
	set nx=%3
	set ny=%4
	set nz=%5
)

if "%Task%" == "modify" (
	if not "%3" == "" (
		set Function=%3
	)
	if not "%4" == "" (
		set equilibration=%3
	)
	if not "%5" == "" (
		set production=%5
	)
	if not "%6" == "" (
		set timestep=%6
	)
	if not "%8" == "" (
		set temperature=%7,%8
	)
	if not "%9" == "" (
		set pressure=%9
	)
)

::=======================
::  post parameters
::=======================
set nOutputInterval=100
set nMSDOutputInterval=10

:Exec
::=======================
:: Execute tasks
::=======================

echo.
echo *********************************
echo   Execute task [%Task%]
echo *********************************
echo.

if "%Task%" == "super" (
	echo **********************************************************
	echo **  Make supercell  **
	echo **********************************************************
	if not exist "%SampleName%.cif" (
		echo.
		echo "Error: %SampleName%.cif does not exist."
		goto Usage
	)
	if "%nz%" == "" (
		echo.
		echo "Error: nz is not given (nx=%nx% ny=%ny% nz=%nz%)
		goto Usage
	)

	echo perl %ProgramsDir%\Perl\VASP\MakeStructureModel.pl --Action=shift ^
		--Shift=0,0,0 --ChooseRandomly=0 --OutputFile=%SampleName%-%nx%x%ny%x%nz%.cif %SampleName%.cif:%nx%,%ny%,%nz%
	perl %ProgramsDir%\Perl\VASP\MakeStructureModel.pl --Action=shift ^
		--Shift=0,0,0 --ChooseRandomly=0 --OutputFile=%SampleName%-%nx%x%ny%x%nz%.cif %SampleName%.cif:%nx%,%ny%,%nz%
	echo.
	echo ***********************************************************************
	echo   Supercell CIF file would be saved to [%SampleName%-%nx%x%ny%x%nz%.cif]  
	echo ***********************************************************************

	goto Terminate
)

if "%Task%" == "convert" (
	echo **********************************************************
	echo **  Convert lattice with the preset = %ConvertLattice%  **
	echo **********************************************************
	if not exist "%SampleName%.cif" (
		echo.
		echo "Error: %SampleName%.cif does not exist."
		goto Usage
	)
	
	echo .
	echo Remove dir [%Task%}, OK?
	rmdir /S %Task%

@setlocal enabledelayedexpansion
	if "%ConvertLattice%" == "" (
		echo.
		echo Input ConvertLattice mode: Choose from
		echo "  No, P1, RhombHex, HexRhomb, HexOrtho, FCCPrim, BCCPrim"
		echo "  ACenterPrim, BCenterPrim, CCenterPrim"
		set /P ConvertLattice="Input >> "
	)

	echo .
	echo exec [%ProgramsDir%\Perl\Crystal\CIF\ConvertLatticeForCIF.pl %SampleName%.cif !ConvertLattice! ai OriginalToConverted 0.01]
	perl %ProgramsDir%\Perl\Crystal\CIF\ConvertLatticeForCIF.pl %SampleName%.cif !ConvertLattice! ai OriginalToConverted 0.01
@endlocal

	mkdir %Task%
	copy %SampleName%-converted.cif      %Task%
	move %SampleName%.cif                %Task%
	move %SampleName%.struct             %Task%
	move %SampleName%-simple.cif         %Task%
	move %SampleName%-Symmetrized.cif    %Task%
	move %SampleName%-Symmetrized.struct %Task%

	echo.
	echo ***********************************************************************
	echo   Converted CIF file would be saved to [%SampleName%-converted.cif]  
	echo   Related files are moved to [%Task%]
	echo ***********************************************************************

	goto Terminate
)

if "%Task%" == "init" (
	echo *********************************
	echo **  Make GULP input files for function "%Function%" **
	echo *********************************
	if "%Function%" == "" (
		goto Usage
	);
	if not exist "%SampleName%.cif" (
		echo.
		echo "Error: %SampleName%.cif does not exist."
		goto Usage
	)

	del %SampleName%.glp 2> nul

@setlocal enabledelayedexpansion
	if "%LibraryFile%" == "" (
		%GULPplCmd% --Action=FindDatabases --PotentialType=buck %SampleName%.cif

		echo.
		echo Input Library file from above.
		set /P LibraryFile="Input >> "

	)

	if "%TargetFiles%" == "" (
		echo.
	) else (
		set TargetFiles="%TargetFiles%"
	)
	
	%GULPplCmd% --Action=MakeInput --UseShellModelForCation=0 --UseShellModelForAnion=0 ^
		--Function=%Function% --LibraryFile=!LibraryFile! --UseSpeciesCharges=%UseSpeciesCharges% ^
		--temperature=%tempearture% --pressure=%pressure% ^
		--timestep=%timestep% --tscale=%tscale% --equilibration=%equilibration% --production=%production% ^
		--sample=%sample% --write=%write% --CopyScript=%CopyScript% ^
		%SampleName%.cif %SampleName%.glp --TargetFiles=!TargetFiles!
@endlocal

rem	mkdir %Function%
rem	copy %SampleName%.cif %Function%
rem	copy %SampleName%.glp %Function%
rem	copy Potentials.csv   %Function%
rem	del  Potential.csv 2> nul

	echo.
	echo ************************************************************
	echo **  GULP input file would be saved to [%SampleName%.glp]  **
	echo ************************************************************

	goto Terminate
)

if "%Task%" == "exec" (
	echo ********************
	echo **  Execute GULP  **
	echo ********************
	if not exist "%SampleName%.glp" (
		echo.
		echo "Error: %SampleName%.glp does not exist."
		goto Usage
	)

	del %SampleName%.out 2> nul

	echo "Execute [%GULPPATH% < %SampleName%.glp | %TEE% %SampleName%.out]"
	%GULPPATH% < %SampleName%.glp | %TEE% %SampleName%.out
	
	echo.
	echo **************************************************************
	echo **  GULP outtput file would be saved to [%SampleName%.out]  **
	echo **************************************************************

	goto Terminate
)

if "%Task%" == "post" (
	echo **************************************
	echo **  Postprocessing for GULP output  **
	echo **************************************

	del %SampleName%-initial.cif 2> nul
	del %SampleName%-final.cif   2> nul
	del %SampleName%.axsf        2> nul
	del %SampleName%-History-Instantaneous.csv 2> nul
	del %SampleName%-History-Average.csv       2> nul

	%GULPplCmd% --Action=MakeHistoryCSV ^
		--nOutputInterval=%nOutputInterval% ^
		%SampleName%.glp %SampleName%.out %SampleName%.his ^
		%SampleName%-initial.cif %SampleName%-final.cif %SampleName%.axsf ^
		%SampleName%-History-Instantaneous.csv %SampleName%-History-Average.csv

	%GULPplCmd% --Action=CalMSD ^
		--nOutputInterval=%nMSDOutputInterval% ^
		%SampleName%.glp %SampleName%.out %SampleName%.his ^
		%SampleName%-MSD.csv

	echo.
	echo *************************************************************************************
	echo **  GULP output/history files would be converted to [%SampleName%.cif/.asf/.csv]  **
	echo *************************************************************************************

	goto Terminate
)

if "%Task%" == "update" (
	echo ****************************
	echo **  Make next input file  **
	echo ****************************

	del %SampleName%.glp.new 2> nul

	%GULPplCmd% --Action=UpdateInputFile ^
		%SampleName%.glp %SampleName%.out %SampleName%.res %SampleName%.glp.new

	echo.
	echo ********************************************************************
	echo **  New GULP input file would be saved to [%SampleName%.glp.new]  **
	echo ********************************************************************

	goto Terminate
)

if "%Task%" == "modify" (
	echo ****************************
	echo **  Modify input file  **
	echo ****************************

	del %SampleName%.glp.next 2> nul

	%GULPplCmd% --Action=ModifyInput ^
		--Function=%Function% ^
		--temperature=%temperature% --pressure=%pressure% ^
		--timestep=%timestep% --tscale=%tscale% --equilibration=%equilibration% --production=%production% ^
		--ResetTime=%ResetTime% ^
		--sample=%sample% --write=%write% --CopyScript=%CopyScript% ^
		%SampleName%.glp %SampleName%.glp.next

	echo.
	echo ********************************************************************
	echo **  Next GULP input file would be saved to [%SampleName%.glp.next]  **
	echo ********************************************************************

	goto Terminate
)

if "%Task%" == "save" (
	echo *********************************************
	echo   Save calculation results to [%TargetDir%]
	echo *********************************************

	if "%TargetDir%" == "" (
		goto Usage
	)

	if "%DoConfirm%" == "no" (
echo "del /Q %TargetDir%\*.*"
		del /Q %TargetDir%\*.*
	) else (
echo "del %TargetDir%\*.*"
		del %TargetDir%\*.*
	)
	mkdir %TargetDir%

	copy *.* %TargetDir%
	attrib +R *.bat
	attrib +R *.lib
	attrib +R %SampleName%.glp
	attrib +R %SampleName%.cif
	attrib +R %SampleName%.glp.new
	if "%DoConfirm%" == "no" (
		del /Q *.*
	) else (
		del *.*
	)
	attrib -R *.*

	goto Terminate
)

if "%Task%" == "merge" (
	echo ***************
	echo   Merge files
	echo ***************

	del %SampleName%-Merged-Instantaneous.csv
	del %SampleName%-Merged-Average.csv
	%GULPplCmd% --Action=MergeCSV %SampleName%-History-Instantaneous.csv ^
		%SampleName%-Merged-Instantaneous.csv ^
		%3 %4 %5 %6 %7 %8 %9
	%GULPplCmd% --Action=MergeCSV %SampleName%-History-Average.csv ^
		%SampleName%-Merged-Average.csv ^
		%3 %4 %5 %6 %7 %8 %9
	%GULPplCmd% --Action=MergeCSV %SampleName%-MSD.csv ^
		%SampleName%-Merged-MSD.csv ^
		%3 %4 %5 %6 %7 %8 %9
	
	goto Terminate
)

if "%Task%" == "clean" (
	echo *************
	echo   Clean up
	echo *************

	attrib +R *.bat
	attrib +R *.cif
	attrib +R *.lib
	attrib -R *-initial.cif
	attrib -R *-final.cif
	if "%DoConfirm%" == "no" (
		del /Q *.*
	) else (
		del *.*
	)
	attrib -R *.*
	
	goto Terminate
)

if "%Task%" == "debugexec" (
	echo ***********************************************************
	echo   Execute [for debug, results to be copied from template]
	echo ***********************************************************
	
	copy template\*.* .
	
	goto Terminate
)

::=======================
:: Invalid Task cases
::=======================
goto Usage


:Terminate
::=======================
::  Termination routine
::=======================
@echo off
echo ******************
echo **  GULP END    **
echo ******************
::echo Exit in 10 seconds.
::ping -n 10 localhost > nul
goto END

:END
