<commit_before># Copyright 2013-2019 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import os
import os.path
import copy

import spack.util.environment


class Cp2k(MakefilePackage):
    """CP2K is a quantum chemistry and solid state physics software package
    that can perform atomistic simulations of solid state, liquid, molecular,
    periodic, material, crystal, and biological systems
    """
    homepage = 'https://www.cp2k.org'
    url = 'https://github.com/cp2k/cp2k/releases/download/v3.0.0/cp2k-3.0.tar.bz2'
    git = 'https://github.com/cp2k/cp2k.git'
    list_url = 'https://github.com/cp2k/cp2k/releases'

    version('6.1', sha256='af803558e0a6b9e9d9ce8a3ab955ba32bacd179922455424e061c82c9fefa34b')
    version('5.1', sha256='e23613b593354fa82e0b8410e17d94c607a0b8c6d9b5d843528403ab09904412')
    version('4.1', sha256='4a3e4a101d8a35ebd80a9e9ecb02697fb8256364f1eccdbe4e5a85d31fe21343')
    version('3.0', sha256='1acfacef643141045b7cbade7006f9b7538476d861eeecd9658c9e468dc61151')
    version('develop', branch='master', submodules="True")

    variant('mpi', default=True, description='Enable MPI support')
    variant('blas', default='openblas', values=('openblas', 'mkl', 'accelerate'),
            description='Enable the use of OpenBlas/MKL/Accelerate')
    variant('openmp', default=False, description='Enable OpenMP support')
    variant('smm', default='libxsmm', values=('libxsmm', 'libsmm', 'blas'),
            description='Library for small matrix multiplications')
    variant('plumed', default=False, description='Enable PLUMED support')
    variant('libxc', default=True,
            description='Support additional functionals via libxc')
    variant('pexsi', default=False,
            description=('Enable the alternative PEXSI method'
                         'for density matrix evaluation'))
    variant('elpa', default=False,
            description='Enable optimised diagonalisation routines from ELPA')
    variant('sirius', default=False,
            description=('Enable planewave electronic structure'
                         ' calculations via SIRIUS'))

    depends_on('python', type='build')

    depends_on('fftw@3:', when='~openmp')
    depends_on('fftw@3:+openmp', when='+openmp')

    # see #1712 for the reason to enumerate BLAS libraries here
    depends_on('openblas threads=none', when='blas=openblas ~openmp')
    depends_on('openblas threads=openmp', when='blas=openblas +openmp')
    depends_on('lapack', when='blas=openblas ~openmp')

    depends_on('intel-mkl', when="blas=mkl ~openmp")
    depends_on('intel-mkl threads=openmp', when='blas=mkl +openmp')

    conflicts('blas=accelerate', '+openmp')  # there is no Accelerate with OpenMP support

    depends_on('libxsmm@1.10:~header-only', when='smm=libxsmm')
    # use pkg-config (support added in libxsmm-1.10) to link to libxsmm
    depends_on('pkgconfig', type='build', when='smm=libxsmm')

    # libint & libxc are always statically linked
    depends_on('libint@1.1.4:1.2', when='@3.0:', type='build')
    depends_on('libxc@2.2.2:', when='+libxc@:5.5999', type='build')
    depends_on('libxc@4.0.3:', when='+libxc@6.0:', type='build')

    depends_on('mpi@2:', when='+mpi')
    depends_on('scalapack', when='+mpi')
    depends_on('elpa@2011.12:2016.13+openmp', when='+openmp+elpa@:5.999')
    depends_on('elpa@2011.12:2017.11+openmp', when='+openmp+elpa@6.0:')
    depends_on('elpa@2011.12:2016.13~openmp', when='~openmp+elpa@:5.999')
    depends_on('elpa@2011.12:2017.11~openmp', when='~openmp+elpa@6.0:')
    depends_on('plumed+shared+mpi', when='+plumed+mpi')
    depends_on('plumed+shared~mpi', when='+plumed~mpi')

    # while we link statically against PEXSI, its own deps may be linked in
    # dynamically, therefore can't set this as pure build-type dependency.
    depends_on('pexsi+fortran@0.9.0:0.9.999', when='+pexsi@:4.999')
    depends_on('pexsi+fortran@0.10.0:', when='+pexsi@5.0:')

    # only OpenMP should be consistenly used, all other common things
    # like ELPA, SCALAPACK are independent and Spack will ensure that
    # a consistent/compat. combination is pulled in to the dependency graph.
    depends_on('sirius+fortran+vdwxc+shared+openmp', when='+sirius+openmp')
    depends_on('sirius+fortran+vdwxc+shared~openmp', when='+sirius~openmp')
    # to get JSON-based UPF format support used in combination with SIRIUS
    depends_on('json-fortran', when='+sirius')

    # PEXSI, ELPA and SIRIUS need MPI in CP2K
    conflicts('~mpi', '+pexsi')
    conflicts('~mpi', '+elpa')
    conflicts('~mpi', '+sirius')
    conflicts('+sirius', '@:6.999')  # sirius support was introduced in 7+

    # Apparently cp2k@4.1 needs an "experimental" version of libwannier.a
    # which is only available contacting the developer directly. See INSTALL
    # in the stage of cp2k@4.1
    depends_on('wannier90', when='@3.0+mpi', type='build')

    # TODO : add dependency on CUDA

    # CP2K needs compiler specific compilation flags, e.g. optflags
    conflicts('%clang')
    conflicts('%cray')
    conflicts('%nag')
    conflicts('%xl')

    @property
    def makefile_architecture(self):
        return '{0.architecture}-{0.compiler.name}'.format(self.spec)

    @property
    def makefile_version(self):
        return '{prefix}{suffix}'.format(
            prefix='p' if '+mpi' in self.spec else 's',
            suffix='smp' if '+openmp' in self.spec else 'opt'
        )

    @property
    def makefile(self):
        makefile_basename = '.'.join([
            self.makefile_architecture, self.makefile_version
        ])
        return os.path.join('arch', makefile_basename)

    @property
    def archive_files(self):
        return [os.path.join(self.stage.source_path, self.makefile)]

    def edit(self, spec, prefix):

        fftw = spec['fftw:openmp' if '+openmp' in spec else 'fftw']

        optimization_flags = {
            'gcc': [
                '-O2',
                '-mtune=native',
                '-funroll-loops',
                '-ftree-vectorize',
            ],
            'intel': ['-O2', '-pc64', '-unroll'],
            'pgi': ['-fast'],
        }

        dflags = ['-DNDEBUG']
        cppflags = [
            '-D__FFTW3',
            '-D__LIBINT',
            '-D__LIBINT_MAX_AM=6',
            '-D__LIBDERIV_MAX_AM1=5',
            fftw.headers.cpp_flags,
        ]

        if '^mpi@3:' in spec:
            cppflags.append('-D__MPI_VERSION=3')
        elif '^mpi@2:' in spec:
            cppflags.append('-D__MPI_VERSION=2')

        if '^intel-mkl' in spec:
            cppflags.append('-D__FFTSG')

        cflags = optimization_flags[self.spec.compiler.name][:]
        cxxflags = optimization_flags[self.spec.compiler.name][:]
        fcflags = optimization_flags[self.spec.compiler.name][:]
        ldflags = []
        libs = []

        if '%intel' in spec:
            cflags.append('-fp-model precise')
            cxxflags.append('-fp-model precise')
            fcflags.extend(['-fp-model source', '-heap-arrays 64'])
        elif '%gcc' in spec:
            fcflags.extend(['-ffree-form', '-ffree-line-length-none'])
        elif '%pgi' in spec:
            fcflags.extend(['-Mfreeform', '-Mextend'])

        if '+openmp' in spec:
            fcflags.append(self.compiler.openmp_flag)
            ldflags.append(self.compiler.openmp_flag)

        ldflags.append(fftw.libs.search_flags)

        if 'superlu-dist@4.3' in spec:
            ldflags.insert(0, '-Wl,--allow-multiple-definition')

        # libint-1.x.y has to be linked statically to work around<commit_msg>packages/cp2k: build with -ggdb with GNU for backtraces<commit_after># Copyright 2013-2019 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import os
import os.path
import copy

import spack.util.environment


class Cp2k(MakefilePackage):
    """CP2K is a quantum chemistry and solid state physics software package
    that can perform atomistic simulations of solid state, liquid, molecular,
    periodic, material, crystal, and biological systems
    """
    homepage = 'https://www.cp2k.org'
    url = 'https://github.com/cp2k/cp2k/releases/download/v3.0.0/cp2k-3.0.tar.bz2'
    git = 'https://github.com/cp2k/cp2k.git'
    list_url = 'https://github.com/cp2k/cp2k/releases'

    version('6.1', sha256='af803558e0a6b9e9d9ce8a3ab955ba32bacd179922455424e061c82c9fefa34b')
    version('5.1', sha256='e23613b593354fa82e0b8410e17d94c607a0b8c6d9b5d843528403ab09904412')
    version('4.1', sha256='4a3e4a101d8a35ebd80a9e9ecb02697fb8256364f1eccdbe4e5a85d31fe21343')
    version('3.0', sha256='1acfacef643141045b7cbade7006f9b7538476d861eeecd9658c9e468dc61151')
    version('develop', branch='master', submodules="True")

    variant('mpi', default=True, description='Enable MPI support')
    variant('blas', default='openblas', values=('openblas', 'mkl', 'accelerate'),
            description='Enable the use of OpenBlas/MKL/Accelerate')
    variant('openmp', default=False, description='Enable OpenMP support')
    variant('smm', default='libxsmm', values=('libxsmm', 'libsmm', 'blas'),
            description='Library for small matrix multiplications')
    variant('plumed', default=False, description='Enable PLUMED support')
    variant('libxc', default=True,
            description='Support additional functionals via libxc')
    variant('pexsi', default=False,
            description=('Enable the alternative PEXSI method'
                         'for density matrix evaluation'))
    variant('elpa', default=False,
            description='Enable optimised diagonalisation routines from ELPA')
    variant('sirius', default=False,
            description=('Enable planewave electronic structure'
                         ' calculations via SIRIUS'))

    depends_on('python', type='build')

    depends_on('fftw@3:', when='~openmp')
    depends_on('fftw@3:+openmp', when='+openmp')

    # see #1712 for the reason to enumerate BLAS libraries here
    depends_on('openblas threads=none', when='blas=openblas ~openmp')
    depends_on('openblas threads=openmp', when='blas=openblas +openmp')
    depends_on('lapack', when='blas=openblas ~openmp')

    depends_on('intel-mkl', when="blas=mkl ~openmp")
    depends_on('intel-mkl threads=openmp', when='blas=mkl +openmp')

    conflicts('blas=accelerate', '+openmp')  # there is no Accelerate with OpenMP support

    depends_on('libxsmm@1.10:~header-only', when='smm=libxsmm')
    # use pkg-config (support added in libxsmm-1.10) to link to libxsmm
    depends_on('pkgconfig', type='build', when='smm=libxsmm')

    # libint & libxc are always statically linked
    depends_on('libint@1.1.4:1.2', when='@3.0:', type='build')
    depends_on('libxc@2.2.2:', when='+libxc@:5.5999', type='build')
    depends_on('libxc@4.0.3:', when='+libxc@6.0:', type='build')

    depends_on('mpi@2:', when='+mpi')
    depends_on('scalapack', when='+mpi')
    depends_on('elpa@2011.12:2016.13+openmp', when='+openmp+elpa@:5.999')
    depends_on('elpa@2011.12:2017.11+openmp', when='+openmp+elpa@6.0:')
    depends_on('elpa@2011.12:2016.13~openmp', when='~openmp+elpa@:5.999')
    depends_on('elpa@2011.12:2017.11~openmp', when='~openmp+elpa@6.0:')
    depends_on('plumed+shared+mpi', when='+plumed+mpi')
    depends_on('plumed+shared~mpi', when='+plumed~mpi')

    # while we link statically against PEXSI, its own deps may be linked in
    # dynamically, therefore can't set this as pure build-type dependency.
    depends_on('pexsi+fortran@0.9.0:0.9.999', when='+pexsi@:4.999')
    depends_on('pexsi+fortran@0.10.0:', when='+pexsi@5.0:')

    # only OpenMP should be consistenly used, all other common things
    # like ELPA, SCALAPACK are independent and Spack will ensure that
    # a consistent/compat. combination is pulled in to the dependency graph.
    depends_on('sirius+fortran+vdwxc+shared+openmp', when='+sirius+openmp')
    depends_on('sirius+fortran+vdwxc+shared~openmp', when='+sirius~openmp')
    # to get JSON-based UPF format support used in combination with SIRIUS
    depends_on('json-fortran', when='+sirius')

    # PEXSI, ELPA and SIRIUS need MPI in CP2K
    conflicts('~mpi', '+pexsi')
    conflicts('~mpi', '+elpa')
    conflicts('~mpi', '+sirius')
    conflicts('+sirius', '@:6.999')  # sirius support was introduced in 7+

    # Apparently cp2k@4.1 needs an "experimental" version of libwannier.a
    # which is only available contacting the developer directly. See INSTALL
    # in the stage of cp2k@4.1
    depends_on('wannier90', when='@3.0+mpi', type='build')

    # TODO : add dependency on CUDA

    # CP2K needs compiler specific compilation flags, e.g. optflags
    conflicts('%clang')
    conflicts('%cray')
    conflicts('%nag')
    conflicts('%xl')

    @property
    def makefile_architecture(self):
        return '{0.architecture}-{0.compiler.name}'.format(self.spec)

    @property
    def makefile_version(self):
        return '{prefix}{suffix}'.format(
            prefix='p' if '+mpi' in self.spec else 's',
            suffix='smp' if '+openmp' in self.spec else 'opt'
        )

    @property
    def makefile(self):
        makefile_basename = '.'.join([
            self.makefile_architecture, self.makefile_version
        ])
        return os.path.join('arch', makefile_basename)

    @property
    def archive_files(self):
        return [os.path.join(self.stage.source_path, self.makefile)]

    def edit(self, spec, prefix):

        fftw = spec['fftw:openmp' if '+openmp' in spec else 'fftw']

        optimization_flags = {
            'gcc': [
                '-O2',
                '-mtune=native',
                '-funroll-loops',
                '-ftree-vectorize',
            ],
            'intel': ['-O2', '-pc64', '-unroll'],
            'pgi': ['-fast'],
        }

        dflags = ['-DNDEBUG']
        cppflags = [
            '-D__FFTW3',
            '-D__LIBINT',
            '-D__LIBINT_MAX_AM=6',
            '-D__LIBDERIV_MAX_AM1=5',
            fftw.headers.cpp_flags,
        ]

        if '^mpi@3:' in spec:
            cppflags.append('-D__MPI_VERSION=3')
        elif '^mpi@2:' in spec:
            cppflags.append('-D__MPI_VERSION=2')

        if '^intel-mkl' in spec:
            cppflags.append('-D__FFTSG')

        cflags = optimization_flags[self.spec.compiler.name][:]
        cxxflags = optimization_flags[self.spec.compiler.name][:]
        fcflags = optimization_flags[self.spec.compiler.name][:]
        ldflags = []
        libs = []

        if '%intel' in spec:
            cflags.append('-fp-model precise')
            cxxflags.append('-fp-model precise')
            fcflags.extend(['-fp-model source', '-heap-arrays 64'])
        elif '%gcc' in spec:
            fcflags.extend([
                '-ffree-form',
                '-ffree-line-length-none',
                '-ggdb',  # make sure we get proper Fortran backtraces
            ])
        elif '%pgi' in spec:
            fcflags.extend(['-Mfreeform', '-Mextend'])

        if '+openmp' in spec:
            fcflags.append(self.compiler.openmp_flag)
            ldflags.append(self.compiler.openmp_flag)

        ldflags.append(fftw.libs.search_flags)

        if 'superlu-dist@4.3' in spec:
            ldflags.insert(0, '-Wl,--allow-multiple-definition')

        # libint-1.x.y has to be linked statically to work around
{'fraction_non_alphanumeric': 0.14489112227805695, 'fraction_numerical': 0.04580595284112872, 'mean_word_length': 3.187483140005395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6980119', 'n_tokens_mistral': 5713, 'n_tokens_neox': 5241, 'n_words': 1051}