# -*- 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
# $Id: Portfile 118091 2014-03-21 19:53:05Z macsforever2000@macports.org $

PortSystem          1.0
PortGroup           active_variants 1.1

name                abinit
version             7.4.3
categories          science
platforms           darwin
license             GPL-3
maintainers         gmail.com:cram5431 \
                    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            http://www.abinit.org
master_sites        http://ftp.abinit.org/

checksums           rmd160  6e3629a94721440f8afacbcaf2b046b5c6e9b23d \
                    sha256  3593dc96c124c0e1ad29a5ec13e9e8f8dc391b328cf06ed461e34f5a9298b246

depends_lib         port:atlas

configure.args      --with-linalg-flavor="atlas" --enable-gw-dpc
configure.optflags  -O3

build.cmd           make
build.target        multi multi_nprocs=4
use_parallel_build  no

default_variants +etsf_io +libxc +wannier90
if { ![variant_isset gcc46] && ![variant_isset gcc47] && ![variant_isset gcc48] } {
    if { ![variant_isset openmpi] } {
        default_variants    +mpich
    } elseif { ![variant_isset mpich] } {
        default_variants    +openmpi
    } else {
        default_variants    +gcc48
    }
}

pre-fetch {
    set fortran unknown
    set fortrans { gcc42 gcc43 gcc44 gcc45 gcc46 gcc47 gcc48 gcc49 g95 }

    foreach fc_name ${fortrans} {
        if { [variant_isset ${fc_name}] } {
            set fortran ${fc_name}
        } elseif { [variant_isset openmpi] } {
            if { [_portnameactive openmpi-default] } {
                if { [active_variants openmpi-default ${fc_name}] } { set fortran ${fc_name} }
            } elseif { [_portnameactive openmpi-devel-default] } {
                if { [active_variants openmpi-devel-default ${fc_name}] } { set fortran ${fc_name} }
            }
        } elseif { [variant_isset mpich] } {
            if { [_portnameactive mpich-default] } {
                if { [active_variants mpich-default ${fc_name}] } { set fortran ${fc_name} }
            } elseif { [_portnameactive mpich-devel-default] } {
                if { [active_variants mpich-devel-default ${fc_name}] } { set fortran ${fc_name} }
            }
        }
    }

    if { ${fortran} eq "unknown" } {
        if { [variant_isset openmpi] } {
            ui_error "Variant +openmpi requires openmpi to have been built with Fortran support."
            return -code error "Variant +openmpi requires openmpi to have been built with Fortran support."
        } elseif { [variant_isset mpich] } {
            ui_error "Variant +mpich requires mpich-default/mpich-devel-default to have been built with Fortran support."
            return -code error "Variant +mpich requires mpich-default/mpich-devel-default to have been built with Fortran support."
        } else {
            ui_error "Internal error: cannot determine Fortran compiler."
            return -code error "Internal error: cannot determine Fortran compiler."
        }
    }

    if { [variant_isset libxc] } {
        if { ![active_variants libxc ${fortran}] } {
            ui_error "libxc must have been built with +${fortran}."
            return -code error "libxc must have been built with +${fortran}."
        }
    }

    if { [variant_isset netcdf] } {
        if {![active_variants netcdf-fortran ${fortran}] } {
            ui_error "netcdf-fortran must have been built with +${fortran}."
            return -code error "netcdf-fortran must have been built with +${fortran}."
        }
    }

    if { [variant_isset etsf_io] } {
        if {![active_variants etsf_io ${fortran}] } {
            ui_error "etsf_io must have been built with +${fortran}."
            return -code error "etsf_io must have been built with +${fortran}."
        }
    }

#   No need to check compiler for wannier90
#   No need to check compiler for fftw-3
}

pre-configure {
    if { [variant_isset etsf_io] } {
            configure.args-append  --with-trio-flavor="netcdf+etsf_io"
        } elseif { [variant_isset netcdf] } {
            configure.args-append  --with-trio-flavor="netcdf"
        } else {
            configure.args-append  --with-trio-flavor="none"
    }

    set dft-flavor "none"
    if { [variant_isset libxc] } {
        if { ${dft-flavor} eq "none" } {
            set dft-flavor "libxc"
        } else {
            set dft-flavor ${dft-flavor}"+libxc"
        }
    }
    if { [variant_isset wannier90] } {
        if { ${dft-flavor} eq "none" } {
            set dft-flavor "wannier90"
        } else {
            set dft-flavor ${dft-flavor}"+wannier90"
        }
    }
    if { [variant_isset bigdft] } {
        if { ${dft-flavor} eq "none" } {
            set dft-flavor "bigdft"
        } else {
            set dft-flavor ${dft-flavor}"+bigdft"
        }
    }
    if { [variant_isset atompaw] } {
        if { ${dft-flavor} eq "none" } {
            set dft-flavor "atompaw"
        } else {
            set dft-flavor ${dft-flavor}"+atompaw"
        }
    }
    configure.args-append  --with-dft-flavor="${dft-flavor}"
}

destroot {
    xinstall -W ${worksrcpath}/src/98_main \
     abinit aim anaddb band2eps bsepostproc conducti cut3d \
     fftprof ioprof kss2wfk lapackprof lwf macroave mrgddb \
     mrggkk mrgscr optic ujdet vdw_kernelgen \
     ${destroot}${prefix}/bin
    if { [variant_isset atompaw] } {
        xinstall -W ${worksrcpath}/fallbacks/exports/bin \
         atompaw-abinit ${destroot}${prefix}/bin
    }
}

#universal variant not allowed for libxc/etsf_io
universal_variant   no

test.run            yes
test.target         check-full


variant gcc46 conflicts gcc47 gcc48 openmpi mpich description {Build with GCC 4.6} {
    configure.compiler  macports-gcc-4.6
    configure.args-append  FCCPP="${configure.cpp} -ansi"
}

variant gcc47 conflicts gcc46 gcc48 openmpi mpich description {Build with GCC 4.7} {
    configure.compiler  macports-gcc-4.7
    configure.args-append  FCCPP="${configure.cpp} -ansi"
}

variant gcc48 conflicts gcc46 gcc47 openmpi mpich description {Build with GCC 4.8} {
    configure.compiler  macports-gcc-4.8
    configure.args-append  FCCPP="${configure.cpp} -ansi"
}

variant openmpi conflicts gcc46 gcc47 gcc48 mpich description {Build parallel version with OpenMPI} {
    depends_lib-append     path:bin/mpif90-openmpi-mp:openmpi-default
    configure.fc           mpif90-openmpi-mp
    configure.cc           mpicc-openmpi-mp
    configure.cxx          mpicxx-openmpi-mp
    configure.args-append  FCCPP="${configure.cc} -E -ansi"
    configure.args-append  --enable-mpi="yes" --enable-mpi-io="yes" --with-mpi-level="1"
    configure.args-append  MPI_RUNNER=${prefix}/bin/mpiexec-openmpi-mp
    test.env-append        MPIEXEC=${prefix}/bin/mpiexec-openmpi-mp
}

variant mpich conflicts gcc46 gcc47 gcc48 openmpi description {Build parallel version with MPICH} {
    depends_lib-append     path:bin/mpif90-mpich-mp:mpich-default
    configure.fc           mpif90-mpich-mp
    configure.cc           mpicc-mpich-mp
    configure.cxx          mpicxx-mpich-mp
    configure.args-append  FCCPP="${configure.cc} -E -ansi"
    configure.args-append  --enable-mpi="yes" --enable-mpi-io="yes"
    configure.args-append  MPI_RUNNER=${prefix}/bin/mpiexec-mpich-mp
    test.env-append        MPIEXEC=${prefix}/bin/mpiexec-mpich-mp
}

variant fftw3 description {Build with support for fftw-3 FFT library} {
    depends_lib-append      port:fftw-3
    if { [variant_isset threads] } {
        configure.args-append   --with-fft-flavor="fftw3-threads"
        configure.args-append   --with-fft-libs="-lfftw3_threads -lfftw3"
    } else {
        configure.args-append   --with-fft-flavor="fftw3"
        configure.args-append   --with-fft-libs="-lfftw3"
    }
}

variant threads description {Build with support for multi-thread support (openMP)} {
    configure.args-append   --enable-openmp
    configure.fcflags-append -fopenmp
    configure.args-append   --with-linalg-libs="-L"${prefix}"/lib -ltatlas"
    configure.args-append   --with-linalg-incs="-I"${prefix}"/include"
}

variant netcdf description {Build with support for NetCDF transferable I/O} {
    depends_lib-append      port:netcdf-fortran
    configure.args-append   --with-netcdf-incs="-I"${prefix}"/include"
    configure.args-append   --with-netcdf-libs="-L"${prefix}"/lib -lnetcdf -lnetcdff"
}

variant etsf_io description {Build with support for ETSF_IO transferable I/O} {
    depends_lib-append      port:etsf_io
    configure.args-append   --with-etsf-io-incs="-I"${prefix}"/include/gcc"
    configure.args-append   --with-netcdf-incs="-I"${prefix}"/include"
    configure.args-append   --with-netcdf-libs="-L"${prefix}"/lib -lnetcdf -lnetcdff"
}

variant libxc description {Build with support for libXC exchange-correlation library} {
    depends_lib-append      port:libxc
    configure.args-append   --with-libxc-incs="-I"${prefix}"/include"
}

variant wannier90 description {Build with support for Wannier90} {
    depends_lib-append      port:wannier90
}

variant bigdft description {Build with support for the wavelet BigDFT library} {
#   Patch bigdft-abi-1.0.4 (missing config.Sub and config.guess)
    patchfiles-append       patch-bigdft-config.diff
}

variant atompaw description {Build including AtomPAW atomic dataset generator} {
}
 
livecheck.type      regex
livecheck.url       ${homepage}/downloads/source-packages/abinit-1/releases/index.html
livecheck.regex     ABINIT (\[0-9.\]+)

