# IT4Innovations 2022
# JK LK

easyblock = 'MakeCp'

name = 'VASP'
version = '6.3.2'
versionsuffix = '-SCPC'

homepage = 'http://www.vasp.at'
docurls = 'https://www.vasp.at/wiki/index.php/The_VASP_Manual'
description = """The Vienna Ab initio Simulation Package (VASP) is a local computer program for atomic scale
materials modelling, e.g. electronic structure calculations and quantum-mechanical molecular dynamics,
from first principles.

This module has been modified to include the SCPC method. Please note that vasp_ncl is unstable and therefore not included.

To use VASP, you need an academic license from University of Vienna. Follow the instructions at https://www.vasp.at/index.php/faqs.

Please send us a list of authorized users and their IDs for which you need access (use only http://support.it4i.cz/rt). We are responsible for verifying your licenses."""

toolchain = {'name': 'intel', 'version': '2021b'}
toolchainopts = {'pic': True, 'usempi': True}

# Vasp is proprietary software, see http://www.vasp.at/index.php/faqs on
# how to get access to the code
sources = ['%(namelower)s.%(version)s.tgz']
checksums = ['f7595221b0f9236a324ea8afe170637a578cdd5a837cc7679e7f7812f6edf25a']

# use serial compilation of W90, see https://www.vasp.at/wiki/index.php/Makefile.include#Wannier90_.28optional.29
# Important: In case of Wannier90 3.x, you should compile a serial version by removing COMMS=mpi in the make.inc of Wannier90. 
dependencies = [
    ('HDF5', '1.12.1', '-parallel'),
    ('Wannier90', '3.1.0', '-serial'),
    ('DL_MG', '2.1.4'), # use v2.1.4 because of segfaults with newer versions
    ('PSPFFT', '1.0'),
]

prebuildopts = 'cp arch/makefile.include.intel ./makefile.include && '

# OFLAG to -O3 -march
prebuildopts += 'sed -i "s|\(OFLAG\s\+=\) -O2|\\1 -O3 -ip -march=xHost|" makefile.include && '

# OBJECTS, FFLAGS and MKLROOT
prebuildopts += 'sed -i "s|\(OBJECTS\s\+=\) fftmpiw.o fftmpi_map.o fftw3d.o fft3dlib.o|\\1 fftmpiw.o fftmpi_map.o fftw3d.o fft3dlib.o ${EBROOTIMKLMINFFTW}/lib/libfftw3xf_intel.a|" makefile.include && '
prebuildopts += 'sed -i "s|\(FFLAGS\s\+=\) -assume byterecl -w|\\1 -FR -assume byterecl|" makefile.include && '
prebuildopts += 'sed -i "s|\(MKLROOT\s\+?=\) /path/to/your/mkl/installation|\\1 ${MKLROOT}|" makefile.include && '

# HDF5 support
prebuildopts += 'sed -i "/DVASP_HDF5/,+s|^#||" makefile.include && '
prebuildopts += 'sed -i "s|#\(HDF5_ROOT\s\+?=\) /path/to/your/hdf5/installation|\\1 ${EBROOTHDF5}|" makefile.include && '
prebuildopts += 'sed -i "/L\$(HDF5_ROOT)\/lib -lhdf5_fortran/,+s|^#||" makefile.include && '
prebuildopts += 'sed -i "/I\$(HDF5_ROOT)\/include/,+s|^#||" makefile.include && '

# Wannier90 interface
prebuildopts += 'sed -i "/DVASP2WANNIER90/,+s|^#||" makefile.include && '
prebuildopts += 'sed -i "s|#\(WANNIER90_ROOT\s\+?=\) /path/to/your/wannier90/installation|\\1 ${EBROOTWANNIER90}|" makefile.include && '
prebuildopts += 'sed -i "/L\$(WANNIER90_ROOT)\/lib -lwannier/,+s|^#||" makefile.include && '

# SCPC method
prebuildopts += 'sed -i "s|\(CPP_OPTIONS\s\+=\)|\\1 -DSCPC|" makefile.include && '
prebuildopts += 'echo "" >> makefile.include && '
prebuildopts += 'echo "#SCPC METHOD" >> makefile.include && '
#prebuildopts += 'echo "LIBEXT     = /apps/all" >> makefile.include && '
prebuildopts += 'echo "DLMGROOT   = ${EBROOTDL_MG}" >> makefile.include && '
prebuildopts += 'echo "PSPFFTROOT = ${EBROOTPSPFFT}" >> makefile.include && '
prebuildopts += 'echo "INCS      +=-I\$(DLMGROOT)/lib" >> makefile.include && '
prebuildopts += 'echo "INCS      +=-I\$(PSPFFTROOT)/include" >> makefile.include && '
prebuildopts += 'echo "LLIBS     +=-Wl,--start-group \$(DLMGROOT)/lib/libdlmg.a -Wl,--end-group" >> makefile.include && '
prebuildopts += 'echo "LLIBS     +=-Wl,--start-group \$(PSPFFTROOT)/lib/libpspfft.a -Wl,--end-group" >> makefile.include && '
prebuildopts += 'echo "#SCPC METHOD" >> makefile.include && '

# AMD/intel cpu
import os
if os.environ.get("CLUSTERNAME") in ["KAROLINA"]:
    prebuildopts += 'sed -i "s|-xHOST|-march=core-avx2|" makefile.include && '
    prebuildopts += 'sed -i "s|-march=xHost|-march=core-avx2|" makefile.include && '


# VASP uses LIBS as a list of folders
prebuildopts += 'unset LIBS && '

buildopts = 'std gam '

parallel = 1

files_to_copy = [(['bin/vasp_std', 'bin/vasp_gam'], 'bin')]

sanity_check_paths = {
    'files': ['bin/vasp_std', 'bin/vasp_gam'],
    'dirs': []
}

modluafooter = 'add_property("state","license")'

moduleclass = 'chem'
