# JK 2022
# to get the source code, clone
# https://github.com/QMCPACK/qmcpack.git
# and run the appropriate download and patch script from
# qmcpack/external_codes/quantum_espresso/
#
# CHANGELOG
# see https://github.com/QMCPACK/qmcpack/issues/3967
# CMake is necessary for the pw2qmcpack
# w90 is downloaded automatically using git remote+https when using CMake
# gipaw does not build using CMake

easyblock = 'CMakeMake'

name = 'QuantumESPRESSO'
version = '7.1'
versionsuffix = '-forQMCPACK'

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).
"""

toolchain = {'name': 'intel', 'version': '2022a'}

import os
if os.environ.get("CLUSTERNAME") in ["KAROLINA"]:
   toolchainopts = {'usempi': True, 'openmp': True,  'optarch': 'march=core-avx2'}
else:
   toolchainopts = {'usempi': True, 'openmp': 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://github.com/dceresoli/qe-gipaw/archive/',
]
sources = [
    'qe-%(version)s.tar.gz',
    {'filename': 'qe-gipaw-%(version)s.tar.gz', 'download_filename': '%(version)s.tar.gz'},
]
checksums = [
    {'qe-7.1.tar.gz': 'feacdbc67d084d55df464f989a916f20dfe11a50ccfda782573cdeed4fab3d3a'},
    {'qe-gipaw-7.1.tar.gz': '486b60f38fad7363f81d346adc69de004692f50c9f6be59eee5152a717ca1513'},
]

#patches = ['QuantumESPRESSO-intel-2020b-forQMCPACK-fix-UtilXlib-Makefile-tab.patch']

builddependencies = [
    ('git', '2.36.0', '-nodocs', ('GCCcore', '11.3.0')),
    ('CMake', '3.23.1'),
]

dependencies = [
    ('HDF5', '1.12.2'),
    ('ELPA', '2021.11.001'),
    ('libxc', '5.2.3'),
]

# pw2qmcpack has native support since 7.1
configopts = '-DQE_ENABLE_PLUGINS=pw2qmcpack '

# The third party packages should be installed separately and added as
# dependencies.  The exception is w90, which is force built, and gipaw
# which depends on qe source
buildopts = 'all gwl xspectra couple epw' # gipaw

# parallel build tends to fail
parallel = 1

moduleclass = 'chem'
