# -*- 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           legacysupport 1.1
PortGroup           muniversal 1.0
PortGroup           cmake 1.1
PortGroup           mpi 1.0
PortGroup           linear_algebra 1.0

name                gromacs
# not all versions have patches available for plumed; subport's patch will fail if not there
# find list at https://www.plumed.org/doc-v2.5/user-doc/html/_code_specific_notes.html
version             2021.3
revision            2
categories          science math
platforms           darwin
license             LGPL-2.1
maintainers         {dstrubbe @dstrubbe} openmaintainer
description         Molecular dynamics package designed for simulations of proteins, lipids, and nucleic acids.
long_description    GROMACS is a versatile package to perform molecular \
                    dynamics, i.e. simulate the Newtonian equations of motion for \
                    systems with hundreds to millions of particles. It is primarily \
                    designed for biochemical molecules like proteins and lipids that \
                    have a lot of complicated bonded interactions, but since GROMACS is \
                    extremely fast at calculating the nonbonded interactions (that \
                    usually dominate simulations) many groups are also using it for \
                    research on non-biological systems, e.g. polymers.
platforms           darwin

homepage            https://www.gromacs.org/
master_sites        https://ftp.gromacs.org/pub/gromacs

# md5 as on http://manual.gromacs.org/documentation/2021.3/download.html
checksums           rmd160  74adb5fc9e2fa461d4d7ff603d0fe7e08476da20 \
                    sha256  e109856ec444768dfbde41f3059e3123abdb8fe56ca33b1a83f31ed4575a1cc6 \
                    md5     1850870fbbfb228051ee0afc1c5ddeff \
                    size    37987972

# Enable use of 'macports-libcxx' for macOS 10.12 and earlier, as port uses
# libcxx features normally only available on 10.13 and later.
legacysupport.use_mp_libcxx \
                        yes

depends_build-append \
                    port:pkgconfig

depends_lib-append  port:fftw-3-single port:hwloc port:zlib

compiler.cxx_standard 2017

# FIXME: enable use of avx when appropriate, instead of just SSE
configure.args-append  -DGMX_SIMD:STRING="SSE4.1" -DBUILD_TESTING:BOOL=ON -DGMX_X11:BOOL=OFF -DGMX_HWLOC:BOOL=ON -DGMX_OPENMP:BOOL=OFF
# Ensure that GROMACS log files reflect that the build was
# done via macports, to help users troubleshoot issues
configure.args-append  -DGMX_VERSION_STRING_OF_FORK="macports"
# Always use libc++
configure.args-append  -DGMX_STDLIB_CXX_FLAGS=-stdlib=libc++ -DGMX_STDLIB_LIBRARIES='-lc++abi -lc++'

variant x11 description {Enable GMX view via X11} {
    configure.args-replace  -DGMX_X11:BOOL=OFF -DGMX_X11:BOOL=ON
    depends_lib-append      port:xorg-libX11 port:xorg-libXext
}

variant threads description {Enable usage of OpenMP} {
    configure.args-replace  -DGMX_OPENMP:BOOL=OFF -DGMX_OPENMP:BOOL=ON
}

compilers.choose    cc cxx
mpi.setup

test.run     yes
test.target  check

if {[mpi_variant_isset]} {
    set suffix _mpi
} else {
    set suffix ""
}

pre-test {
    system -W ${worksrcpath} "install_name_tool -change ${prefix}/lib/libgromacs${suffix}.dylib @executable_path/../lib/libgromacs${suffix}.dylib bin/gmx${suffix}"
    # reset name for new executables that will be built in this phase
    system -W ${worksrcpath} "install_name_tool -id @executable_path/../lib/libgromacs${suffix}.dylib lib/libgromacs${suffix}.dylib"
}
post-test {
    # undo changes, in case 'install' is done afterward
    system -W ${worksrcpath} "install_name_tool -change @executable_path/../lib/libgromacs${suffix}.dylib ${prefix}/lib/libgromacs.dylib bin/gmx${suffix}"
    system -W ${worksrcpath} "install_name_tool -id ${prefix}/lib/libgromacs${suffix}.dylib lib/libgromacs${suffix}.dylib"
}
# look into this:
#NOTE: Regression tests have not been run. If you want to run them from the build system, get the correct version of the regression tests package and set REGRESSIONTEST_PATH in CMake to point to it, or set REGRESSIONTEST_DOWNLOAD=ON.

subport gromacs-plumed {
    description    ${description}: \
      (THIS PORT INSTALLS A VERSION OF GROMACS PATCHED WITH PLUMED)
    long_description    ${long_description}: \
      (THIS PORT INSTALLS A VERSION OF GROMACS PATCHED WITH PLUMED)
    conflicts gromacs

# in case gromacs is compiled with MPI, also enforce that plumed is compiled with the same MPI variant:
    if {[mpi_variant_isset]} {
        mpi.enforce_variant path:${prefix}/lib/libplumedKernel.dylib:plumed
    }
# make sure that thread MPI is disabled, since it does not work with plumed
    configure.args-append -DGMX_THREAD_MPI=OFF

    depends_build-append   path:${prefix}/bin/plumed:plumed
    depends_lib-append     path:${prefix}/lib/libplumedKernel.dylib:plumed
    post-patch {
# I need to execute with full path since PATH variable is not properly set here
# Also notice that I am patching with runtime. Notice that
# plumed compiled for MacPorts has the hardcoded path also in the runtime patch.
# This allows the user to work with MacPorts plumed and possibly
# override the choice setting the PLUMED_KERNEL environment variable.
# Also notice that gromacs version is hardcoded here. Plumed patch is not always
# updated when gromacs is.
        exec ${prefix}/bin/plumed patch --mdroot=${worksrcpath} -e gromacs-${version} --runtime -p
    }
    notes "
PLUMED is linked with runtime binding. By setting the environment variable PLUMED_KERNEL\
to the path of libplumedKernel.dylib you can replace your own PLUMED library at runtime.\
By default, ${prefix}/lib/libplumedKernel.dylib is linked.
"
}

linalg.setup  noveclibfort
pre-configure {
    if {[mpi_variant_isset]} {
        configure.args-append  -DGMX_MPI:BOOL=ON -DMPIEXEC:STRING="${mpi.exec}"
    }
    configure.args-append      -DGMX_BLAS_USER="${linalglib}" -DGMX_LAPACK_USER="${linalglib}"
}

variant double description "Build in double precision (much slower, use only if you really need it)" {
    depends_lib-delete      port:fftw-3-single
    depends_lib-append      port:fftw-3
    configure.args-append   -DGMX_DOUBLE:BOOL=ON
}

livecheck.type          regex
livecheck.url           https://ftp.gromacs.org/pub/gromacs/
livecheck.regex         ${name}-(\[0-9.\]+)${extract.suffix}
