# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           mpi 1.0
PortGroup           linear_algebra 1.0

name                quantum-espresso
version             6.6
revision            2
categories          science
platforms           darwin
license             GPL-2
maintainers         {dstrubbe @dstrubbe} openmaintainer

description         Plane-wave density-functional theory code

long_description    An integrated suite of open-source computer codes for electronic-structure \
                    calculations and materials modeling at the nanoscale. It is based on density-functional \
                    theory, plane waves, and pseudopotentials. \
                    This package will build pw, pp, ld1, xspectra, cp, and ph.

homepage            http://www.quantum-espresso.org
master_sites        https://gitlab.com/QEF/q-e/-/archive/qe-${version}
checksums           rmd160  823001cbaff351a2473cec92b08a01bf34ddb17b \
                    sha256  924656cb083f52e5d2fe71ade05881389dac64b45316f1bdd6dee1c6170a672c \
                    size    77129279

depends_lib-append  port:fftw-3

distname            q-e-qe-${version}
use_autoconf        yes
autoconf.args       -i
autoconf.dir        ${worksrcpath}/install

patchfiles          \
    patch-install-m4-x_ac_qe_f90.m4.diff \
    patch-install-m4-x_ac_qe_mpif90.m4.diff \
    patch-Makefile.diff

compilers.choose    cc fc
# g95 is not compatible with OpenMP
mpi.setup           default require_fortran -g95
compilers.allow_arguments_mismatch yes

configure.optflags  -O3

pre-test {
    if {[mpi_variant_isset]} {
        reinplace -W ${worksrcpath} "s|PARA_PREFIX=\" \"||" environment_variables
        reinplace -W ${worksrcpath} "s|PARA_PREFIX=\"mpirun -np 4\"|PARA_PREFIX=\"${mpi.exec} -n ${build.jobs}\"|" environment_variables
        reinplace -W ${worksrcpath} "s|PARA_IMAGE_PREFIX=\"mpirun -np 4\"|PARA_IMAGE_PREFIX=\"${mpi.exec} -n [expr ${build.jobs} * 2]\"|" environment_variables
    }
}

# there is now a testsuite, 'make test-suite'
test.run            yes

# note: this will download some pseudopotentials and maybe other stuff
test {
    ui_msg "Running PW examples"
    system -W ${worksrcpath}/PW/examples "./run_all_examples"

    # some PHonon tests will use gnuplot if available
    ui_msg "Running PHonon examples"
    system -W ${worksrcpath}/PHonon/examples "./run_all_examples"

    # WAN90_example will not run since wannier90.x (from Wannier90 port) would be needed.
    # example02 was found to fail in parallel with MPI_Bcast error
    ui_msg "Running PP examples"
    system -W ${worksrcpath}/PP/examples "./run_all_examples"

    # add examples for atomic
    ui_msg "Inspect logfile (and results directories) to see if tests passed."
}

configure.args-append   --with-scalapack=no

pre-configure {
    # using cpp separately does not work because it tries to output .f90 to .F90, which is the same file in OSX
    configure.args-append  FC=${configure.fc} F90FLAGS="${configure.fcflags} -x f95-cpp-input -D__FFTW3" \
        F77="${configure.fc}" FFLAGS="${configure.fflags}" F90="${configure.fc}" \
        CC="${configure.cc}" CFLAGS="${configure.cflags}" CPP="${configure.cc} -E" \
        BLAS_LIBS="${linalglib}" LAPACK_LIBS="${linalglib}" FFT_LIBS="${prefix}/lib/libfftw3.dylib"
    # install anything else? e.g. pseudos?
    if {[mpi_variant_isset]} {
        configure.args-append  --enable-parallel MPIF90="${configure.fc}"
    } else {
        configure.args-append  --disable-parallel
    }
}

# libxc support appears to be very experimental for now, so we will not enable it
#post-configure {
#    reinplace -W ${worksrcpath} "s|DFLAGS         =|DFLAGS         = -D__LIBXC|" make.inc
#    reinplace -W ${worksrcpath} "s|LIBS           =|LIBS           = -L${prefix} -lxc -lxcf90|" make.inc
#}

variant scalapack description {Build with ScaLAPACK for parallel linear algebra} {
    depends_lib-append     port:scalapack
    configure.args-delete  --with-scalapack=no
    configure.args-append  SCALAPACK_LIBS="${prefix}/lib/libscalapack.dylib"
    if {![mpi_variant_isset]} {
        ui_error "+scalapack requires an MPI variant. Choose +mpich, +mpich_devel, +openmpi, or +openmpi_devel."
        return -code error "+scalapack requires an MPI variant."
    }
    mpi.enforce_variant     scalapack
}

if {[mpi_variant_isset]} {
    default_variants-append +scalapack
}

use_parallel_build yes
build.target  all-j
# as patched, builds pw pp ld1 upf ph cp

# threads (including fftw/blas)

#BEWARE: you are not using multi-threaded FFTs!

#variant threads description {Build with support for OpenMP threads} {
    # not compatible with g95. What about threaded FFTW3?
#    configure.args-append   --enable-openmp
#    configure.fcflags-append -fopenmp
#}

livecheck.type      regex
livecheck.url       ${homepage}/news-events/news/
livecheck.regex     QUANTUM ESPRESSO V.(\[0-9.\]+)
