# -*- 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                wannier90
version             3.1.0
revision            1
categories          science
platforms           darwin
license             GPL-2+
maintainers         {dstrubbe @dstrubbe} openmaintainer

description         A Tool for Obtaining Maximally-Localised Wannier Functions

long_description    Wannier90 uses inputs from an electronic-structure code \
                    to transform the Bloch orbitals of a crystal into \
                    maximally-localised Wannier functions. Interfaces exist \
                    for various density-functional theory codes.

homepage            http://www.wannier.org
master_sites        https://github.com/wannier-developers/wannier90/archive

checksums           rmd160  d9d1117c3ccc4dde4e5b4181ef230e84edd8e18c \
                    sha1    1d4447775e245f43967ab8a37ba475c2f1547936 \
                    sha256  40651a9832eb93dec20a8360dd535262c261c34e13c41b6755fa6915c936b254 \
                    size    101211573

distname            v${version}
worksrcdir          ${name}-${version}

configure {
    file copy ${worksrcpath}/config/make.inc.macosx.macports ${worksrcpath}/make.inc
}

pre-build {
    build.args      F90=${configure.f90} LDOPTS="-O3" CC=${configure.cc} CFLAGS="${configure.cflags}" \
            LIBS="-L${prefix}/lib ${linalglib}"
    if {[mpi_variant_isset]} {
        build.args-append  COMMS="mpi" MPIF90=${configure.f90}
    } else {
        build.args-append  COMMS="" MPIF90=""
    }
    if {[variant_isset g95]} {
        build.args-append FCOPTS="${configure.fcflags} -fno-second-underscore"
    } else {
        build.args-append FCOPTS="${configure.fcflags}"
    }
}

use_parallel_build  yes

destroot {
    xinstall ${worksrcpath}/wannier90.x              ${destroot}${prefix}/bin/
    xinstall ${worksrcpath}/postw90.x                ${destroot}${prefix}/bin/
    xinstall ${worksrcpath}/w90chk2chk.x             ${destroot}${prefix}/bin/
    xinstall ${worksrcpath}/utility/w90pov/w90pov    ${destroot}${prefix}/bin/
    xinstall ${worksrcpath}/utility/w90vdw/w90vdw.x  ${destroot}${prefix}/bin/
    xinstall ${worksrcpath}/libwannier.a             ${destroot}${prefix}/lib/
}

compilers.choose    f90
mpi.setup           require_fortran
compilers.allow_arguments_mismatch yes

variant threads requires atlas description {Build with threaded ATLAS} {}
# test fails with small discrepancies:
#tests/testpostw90_fe_kpathcurv - Fe.win: **FAILED**.
#bandcurvz
#    ERROR: relative error 1.24e+02 greater than 1.00e+02. (Test: 5.9210191e-20.  Benchmark: 4.7482879e-22.)
#tests/testpostw90_fe_kpathmorbcurv - Fe.win: Passed.
#tests/testpostw90_fe_kslicecurv - Fe.win: **FAILED**.
#bandcurvz
#    ERROR: relative error 1.24e+02 greater than 1.00e+02. (Test: 5.9210191e-20.  Benchmark: 4.7482879e-22.)
#bandcurvz
#    ERROR: relative error 1.18e+02 greater than 1.00e+02. (Test: 5.8410051e-20.  Benchmark: 4.914996e-22.)
#tests/testw90_example26 - gaas.win: **FAILED**.
#final_spreads
#    ERROR: absolute error 3.53e-06 greater than 3.00e-06. (Test: 2.7477301.  Benchmark: 2.74772657.)
#final_spreads
#    ERROR: absolute error 3.53e-06 greater than 3.00e-06. (Test: 2.7477301.  Benchmark: 2.74772657.)
#final_spreads
#    ERROR: absolute error 3.53e-06 greater than 3.00e-06. (Test: 2.7477301.  Benchmark: 2.74772657.)
#omegaRest
#    ERROR: absolute error 1.06e-05 greater than 1.00e-05. (Test: 11.511365424.  Benchmark: 11.511354835.)

test.run            yes
pre-test {
    if {[mpi_variant_isset]} {
        test.target    test-parallel
        # TODO: patch numprocs to appropriate number, not just 4
        reinplace "s|mpirun -np tc\.nprocs|${mpi.exec} -np ${build.jobs}|" ${worksrcpath}/test-suite/testcode/lib/testcode2/__init__.py
    } else {
        test.target    test-serial
    }
}

# with mpich +gcc9, just one failure:
#tests/testpostw90_pt_kpathbandsshc - Pt.win: **FAILED**.
#shc
#    ERROR: absolute error 1.11e-02 greater than 1.00e-02. (Test: -2.415645.  Benchmark: -2.4267646.)
#shc
#    ERROR: absolute error 1.11e-02 greater than 1.00e-02. (Test: -2.9992466.  Benchmark: -2.9881258.)

# without threads small discrepancies for
#tests/testw90_example03 - silicon.win: **FAILED**.
#final_spreads
#    ERROR: absolute error 3.37e-06 greater than 3.00e-06. (Test: 1.81247943.  Benchmark: 1.81247606.)
#tests/testpostw90_pt_kpathbandsshc - Pt.win: **FAILED**.
#shc
#    ERROR: absolute error 1.11e-02 greater than 1.00e-02. (Test: -2.415645.  Benchmark: -2.4267646.)
#shc
#    ERROR: absolute error 1.11e-02 greater than 1.00e-02. (Test: -2.9992466.  Benchmark: -2.9881258.)

livecheck.type      regex
livecheck.url       ${homepage}/download
livecheck.regex     Wannier90 \\(v(\[0-9.\]+)\\)
