-- This script runs FPLO to create an LDA calculation of CrF4 

-- The name of the program and version
FPLO  = "fplo14.00-49-x86_64"
FEdit = "fedit14.00-49-x86_64"

directoryname = "DFT"

-- create the directory where the calculation should be stored
os.execute("if [ ! -d \""..directoryname.."\" ]; then mkdir "..directoryname.."; fi")

-- we use the -p option of FPLO to run it in a scripting mode
-- here the script to setup the calculation
FEditMenuOptions = [[
# go to symmetry menu
@+@
# title
@c@FeF4
# enter spacegroup select box - 215
@s@
@215@
@x@
# structure type - molecule
@t@
@m@
@x@
# lenth units - angstroem
@u@
@a@
@x@
# setup Wyckoff positions
@n@2
# Now, give list of all Wyckoff positions.
@1@ Cr @ 0. 0. 0.
@2@ F @ 1. 1. 1.
#
# NOW CALL UPDATE, NEVER FORGET THIS!!!
#
@+@
# leave symmetry menu
@x@
# back in main menu
# This was the symmetry setup, and now we follow our advise to create
# the default =.in input by using the REBUILD-action.
# (The space before the 'e' opens the alternative menu bar.)
@ e@
# now we have the default input, and are still in the main menu

# Set the number of k-points
@k@ 10 10 10
# Set the xc-potential version
# first enter select box
@v@
# select via search
!Perdew Wang 92!
# leave select box, go back to main menu
@x@

# Set maximum number of iterations to 200
@n@200

# Set convergence criterion to Density OR energy
@g@
@1@
@x@

# activate structure optimization
# (The space before the 'f' opens the alternative menu bar.)
@ f@
@f@
@2@
@x@
# leave forces menu
@x@

# last action must be
@q@
]]

-- The first run to selfconsistency.
-- Copy the string above to an input file
file = io.open(directoryname.."/=.pipe", "w")
file:write(FEditMenuOptions)
file:close()
-- run FEdit
os.execute("cd "..directoryname.."; "..FEdit.." -p "..FPLO.." -pipe <=.pipe 2>+log 1> out.fedit")
-- run FPLO


os.execute("cd "..directoryname.."; "..FPLO.." 2>>+log 1 | tee out.scflow")

-- Next we increase the number of k-points and numerical accuracy
FEditMenuOptionsMorekPoints = [[
# Set the number of k-points
@k@ 40 40 40
# Set the accuracy of density
@a@ 1.e-9
# Set the accuracy of Etot
@a@ 1.e-11

# deactivate site geometry optimization again
# (The space before the 'f' opens the alternative menu bar.)
@ f@
@f@
@1@
@x@
# leave forces menu
@x@

# Set options:
@-@
# save the basis orbitals to disk
!PLOT_BASIS!+
#leave menu
@x@

# last action must be
@q@
]]

-- write the above string to a file
file = io.open(directoryname.."/=.pipekmesh", "w")
file:write(FEditMenuOptionsMorekPoints)
file:close()
-- run FEdit
os.execute("cd "..directoryname.."; "..FEdit.." -p "..FPLO.." -pipe <=.pipekmesh 2>+log 1> out.feditkmesh")
-- run FPLO
os.execute("cd "..directoryname.."; "..FPLO.." 2>>+log 1 | tee out.scf")

-- calculate bands
FEditMenuOptionsBand = [[
@ b@
  @b@+
@x@
@q@";
]]

-- copy the string to a file
file = io.open(directoryname.."/=.pipeband", "w")
file:write(FEditMenuOptionsBand)
file:close()
-- run FEDIT
os.execute("cd "..directoryname.."; "..FEdit.." -p "..FPLO.." -pipe <=.pipeband 2>+log 1> out.feditband")
-- run FPLO
os.execute("cd "..directoryname.."; "..FPLO.." 2>>+log 1 | tee out.band")

