@echo off

goto START

:Usage
::============================
::  Show Usage and terminate
::============================
:Usage
echo "Usage: GoLAMMPS.bat SampleName convert"
echo "       GoLAMMPS.bat SampleName super nx ny nz"
echo "       GoLAMMPS.bat SampleName init [relax|MD-NVT|MD-NPT|Relax,MD-NVT|Relax,MD-NPT] (optional:LibraryName)"
echo "       GoLAMMPS.bat SampleName exec"
echo "       GoLAMMPS.bat SampleName post"
:echo "       GoLAMMPS.bat SampleName update"
echo "       GoLAMMPS.bat SampleName save DirToBeStored (optional flag for confirmation:no)"
:echo "       GoLAMMPS.bat SampleName merge"
echo "       GoLAMMPS.bat SampleName clean"
goto END


:START

::===========================
::  directory configuration
::===========================
set ProgramsDir=c:\Programs
if not exist "%ProgramsDir%" (
	set ProgramsDir=d:\Programs
)
set WINMOSRoot=C:\winmos8
set LAMMPSDIR=C:\Program Files\LAMMPS 64-bit 16Mar2018-MPI
set LAMMPSPATH=%LAMMPSDIR%\bin\lmp_mpi.exe
set TEE=%ProgramsDir%\bin\ktee.exe
set LAMMPSPerlDir=%ProgramsDir%\Perl\LAMMPS
set LAMMPSplPath=%LAMMPSPerlDir%\LAMMPS.pl

set CYGWINROOT=C:\cygwin_wm
set BASHPATH=%CYGWINROOT%\bin\bash
set DOS2UNIX4BINPath=%WINMOSRoot%\bin\dos2unix4bin.exe

::=================================
::  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: init exec post clean save
if "%1" == "clean" (
	set Task=%1
	set DoConfirm=%2
) else (
	set Task=%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
::=======================
:EWALD parameter: NULL=automatically determined by LAMMPS
set gewald=0.3
:set gewald=

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

if "%Task%" == "init" (
::Function: relax MD-NVT MD-NPT relax,[MD-NVT|MD-NPT]
	if "%3" == "" (
:		set Function=relax,MD-NPT
		set Function=MD-NPT
	) else (
		set Function=%3
	)

	if "%4" == "" (
:		set LibraryPath=IGZO-Morse.lib
:		set LibraryPath=IGZO-Buckingham.lib
		set LibraryPath=Potential.table
	) else (
		set LibraryPath=%4
	)
)

if "%Task%" == "update" (
::Function: Relax MD Relax,MD
	if "%3" == "" (
:		set Function=Relax,MD
		set Function=MD
	) else (
		set Function=%3
	)
)

::=======================
::  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   Convert CIF file would be saved to [%SampleName%-converted.cif]  
	echo   Related files are moved to [%Task%]
	echo ***********************************************************************

	goto Terminate
)

if "%2" == "init" (
	echo *********************************
	echo **  Make LAMMPS input files for function "%Function%" **
	echo *********************************
	perl %ProgramsDir%\Perl\LAMMPS\LAMMPS.pl --Action=MakeInput --Function=%Function% ^
		--UseShellModelForCation=0 --UseShellModelForAnion=0 ^
		--LibraryFile=%LibraryPath% --gewald=%gewald% ^
		%SampleName%.cif %SampleName%.in %SampleName%.data

	echo.
	echo ************************************************************
	echo **  LAMMPS input file would be saved to [%SampleName%.in] and [%SampleName%.data]  **
	echo ************************************************************

	goto Terminate
)

if "%Task%" == "exec" (
	echo **********************
	echo **  Execute LAMMPS  **
	echo **********************

	if not exist "%SampleName%.in" (
		echo "Error: %SampleName%.in does not exist."
		goto Usage
	)
	if not exist "%SampleName%.data" (
		echo "Error: %SampleName%.data does not exist."
		goto Usage
	)

@echo on
	md "%SampleName%_lmp_tmp"
	cd "%SampleName%_lmp_tmp"
	del /f /q "lmp_tmp.log"
	copy /Y ..\%SampleName%.in   lmp_tmp.in
	copy /Y ..\%SampleName%.data lmp_tmp.data
	copy /Y ..\*.table .

	set OMP_NUM_THREADS=1
	if exist lmp_tmp.restart (
		del /f /q "lmp_tmp.restart"
	)
	set LAMMPS_POTENTIALS=%LAMMPSDIR%\potentials\

	del lmp_tmp.log %SampleName%.log
	"%LAMMPSPATH%" < lmp_tmp.in | "%TEE%" lmp_tmp.log

@echo off
	if not exist lmp_tmp.restart (
		echo "ERROR: Failed to execute LAMMPS"
		cd ..
		goto Terminate
	)

	copy /Y lmp_tmp.log %SampleName%.log

	"%LAMMPSPATH%" -restart lmp_tmp.restart remap lmp_tmp_final.data

	echo.
	echo **************************************************************
	echo **  LAMMPS outtput file would be saved to [lmp_tmp_final.data]  **
	echo **************************************************************

	cd ..
	
	goto Terminate
)

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

	cd "%SampleName%_lmp_tmp"

echo.
echo Execute [%LAMMPSplPath% --Action=ConvCustomDump]
	perl %LAMMPSplPath% --Action=ConvCustomDump lmp_tmp.in lmp_tmp.data lmp_tmp.log lmp_tmp.dump ^
		%SampleName%-initial.cif %SampleName%-final.cif %SampleName%.axsf ^
		%SampleName%-LogHistory.csv %SampleName%-CrystalStructureHistory.csv

	cd ..

	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%.in
	attrib +R %SampleName%.data
	if "%DoConfirm%" == "no" (
		del /Q *.*
	) else (
		del *.*
	)
	attrib -R *.*

	goto Terminate
)

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

	attrib +R *.bat
	attrib +R *.pl
	attrib +R *.cif
	attrib +R *.lib
	attrib +R *.table
	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
:cd ..
echo ********************
echo **  LAMMPS END    **
echo ********************
::echo Exit in 10 seconds.
::ping -n 10 localhost > nul
goto END


:END
