# -*- 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                abinit
version             9.2.2
revision            7
categories          science
platforms           darwin
license             GPL-3
maintainers         {gmail.com:cram5431 @mtorrent} \
                    openmaintainer

description         Full-featured atomic-scale first-principles simulation software

long_description    ABINIT is a package whose main program allows one to find the total energy, \
                    charge density and electronic structure of systems made of electrons and nuclei \
                    (molecules and periodic solids) within Density Functional Theory (DFT), \
                    using pseudopotentials and a planewave or wavelet basis. \
                    ABINIT also includes options to optimize the geometry according to the DFT forces \
                    and stresses, or to perform molecular dynamics simulations using these forces, \
                    or to generate dynamical matrices, Born effective charges, and dielectric tensors, \
                    based on Density-Functional Perturbation Theory, and many more properties. \
                    Excited states can be computed within the Many-Body Perturbation Theory \
                    (the GW approximation and the Bethe-Salpeter equation), and \
                    Time-Dependent Density Functional Theory (for molecules). \
                    In addition to the main ABINIT code, different utility programs are provided. \
                    ABINIT is a project that favours development and collaboration.

homepage            https://www.abinit.org
master_sites        ${homepage}/sites/default/files/packages

checksums           rmd160  675cf2df645175b0b054bde9ab030b038d526f9c \
                    sha256  4999d48ed20655af7ee4015dc79fe5363db494eb330e71aeacb11870135fa95d \
                    size    96751216

compilers.choose    cc cxx fc
compilers.allow_arguments_mismatch yes
# g95 is no longer supported by abinit
mpi.setup           default require_fortran -g95

configure.args-append  --enable-gw-dpc
configure.fcflags-append  -ffree-line-length-none
configure.optflags  -O3

depends_lib-append      port:libxc4 port:hdf5
compilers.enforce_fortran libxc4 hdf5
configure.args-append   --with-libxc="${prefix}" --with-hdf5="${prefix}"
mpi.enforce_variant hdf5

depends_lib-append      port:netcdf-fortran
compilers.enforce_fortran netcdf-fortran
configure.args-append   --with-netcdf="${prefix}" --with-netcdf-fortran="${prefix}"
# check compatibility with MPI? not needed

use_parallel_build  yes

# We do not need to use ABINIT's own approach to parallel builds, which only is relevant if
# the "fallbacks" are used, namely if we asked for libxc, wannier90, etc. support
# but did not provide the libraries. That situation should never happen for this port.
#build.target        multi multi_nprocs=4

pre-configure {
# enable LibPAW, PFFT?

    if { [variant_isset atlas] } {
        set linalg-flavor atlas
    } elseif { [variant_isset openblas] } {
        set linalg-flavor openblas
    } else {
        set linalg-flavor netlib
    }
    if { [variant_isset scalapack] } {
        set linalglib     "${linalglib} -lscalapack"
    }
    configure.args-append   --with-linalg-flavor="${linalg-flavor}" LINALG_LIBS="${linalglib}"
}

# install binaries and pkgconfig but not the very large number of test files
destroot.target   install-exec install-data-local

#universal variant not allowed for libxc
universal_variant   no

test.run            yes
test.cmd            tests/runtests.py -j ${build.jobs}

# args -t0 for no timeout since it cannot be found anyway; or: set depends_test-append port:timeout
test.target         libxc built-in fast tutorespfn tutorial unitary
#test.target         built-in fast tutorespfn tutorial unitary v1 v2 v3 v4 v5 v6 v67mbpt v7
depends_test-append port:py27-pandas

pre-test {
    if {[mpi_variant_isset]} {
        test.target-append paral mpiio tutoparal
        reinplace "s|mpirun -np|${mpi.exec} -np|" ${worksrcpath}/tests/pymods/jobrunner.py
        test.args-append -n 1
    } else {
        test.target-append seq
        test.args-append -j ${build.jobs}
    }
}

pre-configure {
    configure.args-append  FCCPP="${configure.cc} -E -ansi"
    if {[mpi_variant_isset]} {
        configure.args-append  --with-mpi --enable-mpi-io="yes" --enable-mpi-inplace

        # causes compilation error in m_profiling_abi.F90 related to MPI_COMM_WORLD
        # This looks like a possible mistake in not including the mpi module
        # configure.args-append --with-mpi-level=3
    } else {
        configure.args-append  --without-mpi
    }
}

variant fftw3 description {Build with support for fftw-3 FFT library} {
    depends_lib-append      port:fftw-3 port:fftw-3-single
    compilers.enforce_some_fortran fftw-3 fftw-3-single
    
    configure.args-append   --with-fft-flavor="fftw3"
    configure.args-append    FFTW3_LIBS="-L${prefix} -lfftw3 -lfftw3f"
}

# the fftw-3 port always provides the threaded library
variant fftw3 description {Build with support for (threaded) fftw-3 FFT library} {
    depends_lib-append      port:fftw-3 port:fftw-3-single
    compilers.enforce_some_fortran fftw-3 fftw-3-single

    configure.args-append   --with-fft-flavor="fftw3-threads"
    configure.args-append    FFTW3_LIBS="-L${prefix} -lfftw3 -lfftw3f -lfftw3_threads"
}

variant threads description {Build with support for OpenMP threads} {
    configure.args-append   --enable-openmp
    configure.fcflags-append -fopenmp
}

# add support for gsl, yaml?

variant scalapack description {Build with ScaLAPACK for parallel linear algebra} {
    depends_lib-append      port:scalapack

    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
}

variant wannier90 description {Build with support for Wannier90} {
    configure.args-append   --with-wannier90 WANNIER90_LIBS="-lwannier"
    depends_lib-append      port:wannier90
    test.target-append      wannier90 tutoplugs vdwxc
}

# FIXME: the code's build system will download BigDFT itself, which is contrary to the
# way MacPorts should work. Make a bigdft port to support this.
# There is an error compiling anyway, which could be fixed with a patch,
# but it is unpatchable since the download occurs after the patch phase.

#variant bigdft description {Build with support for the wavelet BigDFT library} {
    # avoid this error (and equivalent with OpenCL)
    # :info:build ar cru libCUDA.a
    # :info:build ar: no archive members specified
#    patchfiles-append       patch-fallbacks-sources-bigdft-1.7.0.93-src-Makefile.in.diff
#}

# FIXME: same problem as for BigDFT, it gets downloaded. Make a separate port.
#variant atompaw description {Build including AtomPAW atomic dataset generator} {
#    depends_lib-append       port:atompaw
#    configure.args-append    --enable-atompaw --with-atompaw-bins=${prefix} --with-atompaw-incs=-I${prefix} --with-atompaw-libs=-L${prefix}
#    test.target-append       atompaw
#}
 
livecheck.type      regex
livecheck.url       ${homepage}/packages
livecheck.regex     ABINIT v (\[0-9.\]+)
