# IT4Innovations
# LK 2023
# BS edit

name = 'OpenMX'
version = '3.9'

easyblock = 'MakeCp'

homepage = 'http://www.openmx-square.org/index.html'
description = """OpenMX (Open source package for Material eXplorer) is a software
       package for nano-scale material simulations based on density functional
       theories (DFT), norm-conserving pseudopotentials, and pseudo-atomic
       localized basis functions test.
"""

toolchain = {'name': 'foss', 'version': '2022b'}
toolchainopts = {'usempi': True, 'openmp': True}

source_urls = [
    'https://www.openmx-square.org/',
    'http://www.openmx-square.org/bugfixed/21Oct17/'
]
sources = [
    {
        'filename': 'openmx3.9.tar.gz',
        'extract_cmd': 'mkdir -p %(builddir)s/openmx3.9 && tar xzvf %s --strip-components=1 -C $_',
    },
    {
        'filename': 'patch3.9.9.tar.gz',
        'download_filename': 'patch%(version).9.tar.gz',
        'source_urls': ['http://www.openmx-square.org/bugfixed/21Oct17/']
    },
]
checksums = [
    '27bb56bd4d1582d33ad32108fb239b546bdd1bdffd6f5b739b4423da1ab93ae2',  # openmx3.9.tar.gz
    '20cccc4e3412a814a53568f400260e90f79f0bfb7e2bed84447fe071b26edd38',  # patch3.9.9.tar.gz
]

dependencies = [
    ('LAPACK', '3.10.0'),
    ('ELPA', '2022.05.001'),
    ('FFTW', '3.3.10'),
    ('OpenMPI', '4.1.4'),
    ('OpenBLAS', '0.3.21'),
]


files_to_copy = [
    ('%(sourcepath)s/*', '%(builddir)s/openmx3.9/source'),
    ('%(sourcepath)s/patch3.9.9.tar.gz', '%(builddir)s/openmx3.9/source'),
    ('%(sourcepath)s/kpoint.in', '%(sourcepath)s/openmx3.9/work')
]

#build_cmd = 'cd %(builddir)s/openmx3.9/source && sed -i "s/-xHOST//g" makefile && sed -i "s/-ip//g" makefile && sed -i "s/-qopenmp/-fopenmp/g" makefile && sed -i "s/-no-prec-div//g" makefile && make CFLAGS="-O3 -fopenmp"'


#build_cmd = 'cd %(builddir)s/openmx3.9/source && sed -i "s/-xHOST//g" makefile && sed -i "s/-ip//g" makefile && sed -i "s/-qopenmp/-fopenmp/g" makefile && sed -i "s/-no-prec-div//g" makefile && sed -i "s|-I/opt/intel/mkl/include/fftw|-I/apps/all/FFTW/3.3.10-GCC-12.2.0/include|g" makefile && sed -i "s|-I./elpa-2018.05.001|-I/apps/all/ELPA/2022.05.001-foss-2022b/include|g" makefile && make CFLAGS=\"-O3 -fopenmp\"'

# The third party packages should be installed separately and added as
# dependencies.  The exception is w90, which is force built

#buildopts = 'all'

build_cmd = 'cd %(builddir)s/openmx3.9/source && make'
buildopts = 'CFLAGS="-O3 -fopenmp" \
             FCFLAGS="-O3 -fopenmp -fno-range-check  -fdefault-real-8 -fdefault-double-8 -fallow-argument-mismatch" \
             CC="mpicc -O3 -fopenmp -I/apps/all/FFTW/3.3.10-GCC-12.2.0/include" \
             FC="mpif90 -O3 -fopenmp" \
             LIBS="-L/apps/all/OpenBLAS/0.3.21-GCC-12.2.0/lib -lopenblas -L/apps/all/FFTW/3.3.10-GCC-12.2.0/lib -lfftw3 -L/apps/all/ELPA/2022.05.001-foss-2022b/lib -lelpa -lpthread -lm -ldl" \
             MPIFC=mpif90 \
             MPICC=mpicc'

parallel = 0

moduleclass = 'chem'
