# IT4Innovations 2022
# JK

easyblock = 'CMakeMake'
name = 'QuantumESPRESSO'
version = '7.1'

homepage = 'https://www.quantum-espresso.org'
description = """Quantum ESPRESSO  is an integrated suite of computer codes
for electronic-structure calculations and materials modeling at the nanoscale.
It is based on density-functional theory, plane waves, and pseudopotentials
(both norm-conserving and ultrasoft).
"""

# NVHPC 21.11-22.2 have a severe bug causing hanging in runs when QE is
# compiled with both OpenMP and OpenACC.  Use a different compiler release or
# dislable OpenMP with potential performance loss.
toolchain = {'name': 'NVHPC', 'version': '21.9'}

import os
if os.environ.get("CLUSTERNAME") in ["KAROLINA"]:
   toolchainopts = {'pic': True,  'optarch': 'march=core-avx2'}
else:
   toolchainopts = {'pic': True}

if os.environ.get("CLUSTERNAME") in ["KAROLINA"]:
   prebuildopts = "echo %(builddir)s && while read i; do echo $i; sed 's|-xHost|-march=core-avx2|g' -i $i; done < <(grep xHost %(builddir)s -R | cut -d ':' -f 1 | sort -u) && "


source_urls = [
    'https://www.quantum-espresso.org/rdm-download/488/v7-1/3adf3b73b84938d19f4b7ac856795acb/',
#    'https://github.com/dceresoli/qe-gipaw/releases/download/%(version)s/',
    'https://github.com/wannier-developers/wannier90/archive/',
]
sources = [
    'qe-%(version)s-ReleasePack.tar.gz',
#    'qe-gipaw-%(version)s.tar.gz',
    {'filename': 'wannier90-3.1.0.tar.gz', 'download_filename': 'v3.1.0.tar.gz'},
]
checksums = [
    {'qe-7.1-ReleasePack.tar.gz': 'feacdbc67d084d55df464f989a916f20dfe11a50ccfda782573cdeed4fab3d3a'},
#    {'qe-gipaw-7.1.tar.gz': '486b60f38fad7363f81d346adc69de004692f50c9f6be59eee5152a717ca1513'},
    {'wannier90-3.1.0.tar.gz': '40651a9832eb93dec20a8360dd535262c261c34e13c41b6755fa6915c936b254'},
]

builddependencies = [
    ('CMake', '3.20.1', '', ('GCCcore', '10.2.0')),
    ('git', '2.28.0', '-nodocs', ('GCCcore', '10.2.0')),
]

dependencies = [
#    ('ELPA', '2021.05.001', '', ('intel', '2021a')),
    ('libxc', '6.0.0'),
    ('HDF5', '1.12.1'),
    ('FFTW', '3.3.10'),
    ('OpenMPI', '4.0.7', '-CUDA-11.4.1'),
]

configopts = 'FC=pgfortran F77=pgfortran F90=pgfortran CC=pgcc CXX=pgc++ '
configopts += '-DQE_ENABLE_CUDA=ON -DQE_ENABLE_MPI_GPU_AWARE=ON '
configopts += '-DNVFORTRAN_CUDA_VERSION=11.4 '

import os
if os.environ.get("CLUSTERNAME") in ["KAROLINA"]:
    configopts += '-DNVFORTRAN_CUDA_CC=80 '
elif os.environ.get("CLUSTERNAME") in ["BARBORA"]:
    configopts += '-DNVFORTRAN_CUDA_CC=70 '

# gipaw zatim neni portle na GPU
buildopts = 'all gwl xspectra couple epw w90'
#buildopts = 'all gwl xspectra couple epw w90' # no rule to make target 'gipaw'
#preinstallopts = ' FC=pgfortran F77=pgfortran F90=pgfortran CC=pgcc CXX=pgc++ ctest -j4 --output-on-failure -L unit && '

# parallel build tends to fail
parallel = 1

modluafooter = """
add_property('arch', 'gpu')
"""

moduleclass = 'chem'
