From c18598af667bc2e9005dbd9023d502016fd72faf Mon Sep 17 00:00:00 2001 From: Johann Pototschnig Date: Tue, 13 May 2025 19:51:39 +0200 Subject: [PATCH] add fixes and ind toolchain instalation for tblite --- CMakeLists.txt | 1 + cmake/cmake_cp2k.sh | 1 + src/qs_energy_init.F | 3 +- src/qs_scf.F | 2 +- src/tblite_interface.F | 16 +- tools/toolchain/install_cp2k_toolchain.sh | 22 ++- .../toolchain/scripts/stage8/install_dftd4.sh | 30 ++-- .../scripts/stage8/install_stage8.sh | 1 + .../scripts/stage8/install_tblite.sh | 159 ++++++++++++++++++ 9 files changed, 207 insertions(+), 28 deletions(-) create mode 100755 tools/toolchain/scripts/stage8/install_tblite.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index acb580472d..1bdf860346 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,6 +126,7 @@ option(CP2K_USE_EVERYTHING option(CP2K_USE_ACE "Enable ACE support" ${CP2K_USE_EVERYTHING}) option(CP2K_USE_DEEPMD "Enable DeePMD support" ${CP2K_USE_EVERYTHING}) option(CP2K_USE_DFTD4 "Enable DFTD4 support" ${CP2K_USE_EVERYTHING}) +option(CP2K_USE_TBLITE "Enable TBLITE support" ${CP2K_USE_EVERYTHING}) option(CP2K_USE_FFTW3 "Enable FFTW3 support" ${CP2K_USE_EVERYTHING}) option(CP2K_USE_GREENX "Enable GreenX support" ${CP2K_USE_EVERYTHING}) option(CP2K_USE_GRPP "Enable GRPP support" ${CP2K_USE_EVERYTHING}) diff --git a/cmake/cmake_cp2k.sh b/cmake/cmake_cp2k.sh index a3bbf0728f..8c19b4d845 100755 --- a/cmake/cmake_cp2k.sh +++ b/cmake/cmake_cp2k.sh @@ -161,6 +161,7 @@ elif [[ "${PROFILE}" == "ubuntu" ]] && [[ "${VERSION}" == "ssmp" ]]; then -DCP2K_USE_ACE=OFF \ -DCP2K_USE_DEEPMD=OFF \ -DCP2K_USE_DFTD4=OFF \ + -DCP2K_USE_TBLITE=OFF \ -DCP2K_USE_GREENX=OFF \ -DCP2K_USE_LIBTORCH=OFF \ -DCP2K_USE_LIBXC=OFF \ diff --git a/src/qs_energy_init.F b/src/qs_energy_init.F index 85a9826fdc..7e8c0cf0cb 100644 --- a/src/qs_energy_init.F +++ b/src/qs_energy_init.F @@ -308,8 +308,7 @@ CONTAINS CALL qs_env_update_s_mstruct(qs_env) ELSEIF (dft_control%qs_control%xtb) THEN IF (dft_control%qs_control%xtb_control%do_tblite) THEN - CALL build_tblite_matrices(qs_env=qs_env, para_env=para_env, & - calculate_forces=.FALSE.) + CALL build_tblite_matrices(qs_env=qs_env, calculate_forces=.FALSE.) ELSE CALL build_xtb_matrices(qs_env=qs_env, calculate_forces=.FALSE.) END IF diff --git a/src/qs_scf.F b/src/qs_scf.F index 7d2fa565a0..ee8a70a9dc 100644 --- a/src/qs_scf.F +++ b/src/qs_scf.F @@ -524,7 +524,7 @@ CONTAINS IF (dft_control%qs_control%xtb_control%do_tblite) THEN CALL tb_update_charges(qs_env, dft_control, qs_env%tb_tblite, .FALSE., .FALSE.) CALL evaluate_core_matrix_p_mix_new(qs_env) - CALL tb_get_energy(qs_env, dft_control, qs_env%tb_tblite, energy) + CALL tb_get_energy(qs_env, qs_env%tb_tblite, energy) END IF CALL qs_scf_density_mixing(scf_env, rho, para_env, diis_step) diff --git a/src/tblite_interface.F b/src/tblite_interface.F index 1e6d0f347a..c6c3940c2d 100644 --- a/src/tblite_interface.F +++ b/src/tblite_interface.F @@ -184,7 +184,8 @@ CONTAINS info = tb%calc%variable_info() IF (info%charge > shell_resolved) CPABORT("tblite: no support for orbital resolved charge") IF (info%dipole > atom_resolved) CPABORT("tblite: no support for shell resolved dipole moment") - IF (info%quadrupole > atom_resolved) CPABORT("tblite: no support shell resolved quadrup[ole moment") + IF (info%quadrupole > atom_resolved) & + CPABORT("tblite: no support shell resolved quadrupole moment") CALL new_wavefunction(tb%wfn, tb%mol%nat, tb%calc%bas%nsh, tb%calc%bas%nao, nSpin, 0.0_dp*K2Eh) @@ -326,14 +327,12 @@ CONTAINS ! ************************************************************************************************** !> \brief ... !> \param qs_env ... -!> \param dft_control ... !> \param tb ... !> \param energy ... ! ************************************************************************************************** - SUBROUTINE tb_get_energy(qs_env, dft_control, tb, energy) + SUBROUTINE tb_get_energy(qs_env, tb, energy) TYPE(qs_environment_type), POINTER :: qs_env - TYPE(dft_control_type), POINTER :: dft_control TYPE(tblite_type), POINTER :: tb TYPE(qs_energy_type), POINTER :: energy @@ -385,7 +384,6 @@ CONTAINS #else MARK_USED(qs_env) - MARK_USED(dft_control) MARK_USED(tb) MARK_USED(energy) CPABORT("Built without TBLITE") @@ -509,13 +507,11 @@ CONTAINS ! ************************************************************************************************** !> \brief ... !> \param qs_env ... -!> \param para_env ... !> \param calculate_forces ... ! ************************************************************************************************** - SUBROUTINE build_tblite_matrices(qs_env, para_env, calculate_forces) + SUBROUTINE build_tblite_matrices(qs_env, calculate_forces) TYPE(qs_environment_type), POINTER :: qs_env - TYPE(mp_para_env_type), POINTER :: para_env LOGICAL, INTENT(IN) :: calculate_forces #if defined(__TBLITE) @@ -868,6 +864,8 @@ CONTAINS TYPE(qs_scf_env_type), POINTER :: scf_env TYPE(xtb_atom_type), POINTER :: xtb_kind + IF (calculate_forces) CPABORT("tblite: forces not yet available") + ! also compute multipoles needed by GFN2 do_dipole = .FALSE. do_quadrupole = .FALSE. @@ -1058,6 +1056,8 @@ CONTAINS TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set TYPE(xtb_atom_type), POINTER :: xtb_atom_a, xtb_atom_b + IF (calculate_forces) CPABORT("tblite: forces not yet available") + nimg = dft_control%nimages NULLIFY (matrix_s, ks_matrix, n_list, qs_kind_set) diff --git a/tools/toolchain/install_cp2k_toolchain.sh b/tools/toolchain/install_cp2k_toolchain.sh index 0eebdc1d63..e41d336ba5 100755 --- a/tools/toolchain/install_cp2k_toolchain.sh +++ b/tools/toolchain/install_cp2k_toolchain.sh @@ -235,6 +235,9 @@ The --with-PKG options follow the rules: --with-dftd4 Enable the DFTD4 package by Grimme. This package requires CMake and Ninja. Default = install + --with-tblite Enable the tblite package by Grimme. + This package requires CMake and Ninja. + Default = no --with-trexio Enable the trexio library (read/write TREXIO files). Default = no --with-greenx Enable GreenX library for Minimax grids and Padé analytic continuation in RT-BSE @@ -281,7 +284,7 @@ mpi_list="mpich openmpi intelmpi" math_list="mkl acml openblas" lib_list="fftw libint libxc libgrpp libxsmm cosma scalapack elpa dbcsr cusolvermp plumed spfft spla gsl spglib hdf5 libvdwxc sirius - libvori libtorch deepmd ace dftd4 pugixml libsmeagol trexio greenx gmp" + libvori libtorch deepmd ace dftd4 tblite pugixml libsmeagol trexio greenx gmp" package_list="${tool_list} ${mpi_list} ${math_list} ${lib_list}" # ------------------------------------------------------------------------ @@ -334,6 +337,7 @@ with_libvori="__INSTALL__" with_libtorch="__DONTUSE__" with_ninja="__DONTUSE__" with_dftd4="__DONTUSE__" +with_tblite="__DONTUSE__" with_libsmeagol="__DONTUSE__" # for MPI, we try to detect system MPI variant @@ -682,6 +686,9 @@ while [ $# -ge 1 ]; do --with-dftd4*) with_dftd4=$(read_with "${1}") ;; + --with-tblite*) + with_tblite=$(read_with "${1}") + ;; --with-libsmeagol*) with_libsmeagol=$(read_with "${1}") ;; @@ -821,11 +828,17 @@ if [ "${ENABLE_OPENCL}" = "__TRUE__" ]; then fi fi -#dftd4 installation requires ninja -if [ "${with_dftd4}" = "__INSTALL__" ]; then +#dftd4 / tblite installation requires ninja +if [ "${with_dftd4}" = "__INSTALL__" ] || + [ "${with_tblite}" = "__INSTALL__" ]; then [ "${with_ninja}" = "__DONTUSE__" ] && with_ninja="__INSTALL__" fi +#tblite includes dftd4 - deactivate it +if [ "${with_tblite}" != "__DONTUSE__" ]; then + with_dftd4="__DONTUSE__" +fi + # several packages require cmake. if [ "${with_spglib}" = "__INSTALL__" ] || [ "${with_libvori}" = "__INSTALL__" ] || @@ -837,7 +850,8 @@ if [ "${with_spglib}" = "__INSTALL__" ] || [ "${with_spla}" = "__INSTALL__" ] || [ "${with_ninja}" = "__INSTALL__" ] || [ "${with_greenx}" = "__INSTALL__" ] || - [ "${with_dftd4}" = "__INSTALL__" ]; then + [ "${with_dftd4}" = "__INSTALL__" ] || + [ "${with_tblite}" = "__INSTALL__" ]; then [ "${with_cmake}" = "__DONTUSE__" ] && with_cmake="__INSTALL__" fi diff --git a/tools/toolchain/scripts/stage8/install_dftd4.sh b/tools/toolchain/scripts/stage8/install_dftd4.sh index 32d1970c1e..0edee75d87 100755 --- a/tools/toolchain/scripts/stage8/install_dftd4.sh +++ b/tools/toolchain/scripts/stage8/install_dftd4.sh @@ -97,36 +97,40 @@ export DFTD4_VER="${dftd4_ver}" EOF if [ "$with_dftd4" != "__SYSTEM__" ]; then - DFTD4_LOC=$(find ${pkg_install_dir}/include -name "multicharge.mod") - DFTD4_MCHARGE=${DFTD4_LOC%/*} - DFTD4_LOC=$(find ${pkg_install_dir}/include -name "mstore.mod") - DFTD4_STORE=${DFTD4_LOC%/*} - DFTD4_LOC=$(find ${pkg_install_dir}/include -name "mctc_io.mod") - DFTD4_MCTC=${DFTD4_LOC%/*} - DFTD4_LOC=$(find ${pkg_install_dir}/include -name "dftd4.mod") - DFTD4_DFTD4=${DFTD4_LOC%/*} + TEMP_LOC=$(find ${pkg_install_dir}/include -name "multicharge.mod") + MCHARGE=${DFTD4_LOC%/*} + TEMP_LOC=$(find ${pkg_install_dir}/include -name "mstore.mod") + MSTORE=${DFTD4_LOC%/*} + TEMP_LOC=$(find ${pkg_install_dir}/include -name "mctc_io.mod") + MCTC=${DFTD4_LOC%/*} + TEMP_LOC=$(find ${pkg_install_dir}/include -name "dftd4.mod") + DFTD4=${DFTD4_LOC%/*} + + DFTD4_INCLUDE_DIRS="$pkg_install_dir/include" # use the lib64 directory if present DFTD4_LINK_LIBRARIES="${pkg_install_dir}/lib" [ -d "${pkg_install_dir}/lib64" ] && DFTD4_LINK_LIBRARIES="${pkg_install_dir}/lib64" - DFTD4_CFLAGS="-I'${pkg_install_dir}/include' -I'${DFTD4_DFTD4}' -I'${DFTD4_MCTC}'" + DFTD4_CFLAGS="-I'${MCHARGE}' -I'${MCTC}' -I'${DFTD4}'" DFTD4_LDFLAGS="-L'${DFTD4_LINK_LIBRARIES}' -Wl,-rpath,'${DFTD4_LINK_LIBRARIES}'" cat << EOF >> "${BUILDDIR}/setup_dftd4" prepend_path LD_LIBRARY_PATH "${DFTD4_LINK_LIBRARIES}" prepend_path LD_RUN_PATH "${DFTD4_LINK_LIBRARIES}" prepend_path LIBRARY_PATH "${DFTD4_LINK_LIBRARIES}" -prepend_path CPATH "$pkg_install_dir/include" +prepend_path CPATH "${DFTD4_INCLUDE_DIRS}" prepend_path PKG_CONFIG_PATH "${DFTD4_LINK_LIBRARIES}/pkgconfig" prepend_path CMAKE_PREFIX_PATH "${pkg_install_dir}" EOF fi cat << EOF >> "${BUILDDIR}/setup_dftd4" -export DFTD4_DFTD4="${DFTD4_DFTD4}" -export DFTD4_MCTC="${DFTD4_MCTC}" +export MCHARGE="${MCHARGE}" +export MSTORE="${MSTORE}" +export MCTC="${MCTC}" +export DFTD4="${DFTD4}" +export DFTD4_INCLUDE_DIRS="${DFTD4_INCLUDE_DIRS}" export DFTD4_LINK_LIBRARIES="${DFTD4_LINK_LIBRARIES}" -export DFTD4_INCLUDE_DIRS="$pkg_install_dir/include" export DFTD4_ROOT="${pkg_install_dir}" export DFTD4_DFLAGS="${DFTD4_DFLAGS}" export DFTD4_CFLAGS="${DFTD4_CFLAGS}" diff --git a/tools/toolchain/scripts/stage8/install_stage8.sh b/tools/toolchain/scripts/stage8/install_stage8.sh index c51a696828..42dcc1d2c6 100755 --- a/tools/toolchain/scripts/stage8/install_stage8.sh +++ b/tools/toolchain/scripts/stage8/install_stage8.sh @@ -9,4 +9,5 @@ ./scripts/stage8/install_sirius.sh ./scripts/stage8/install_dftd4.sh ./scripts/stage8/install_trexio.sh +./scripts/stage8/install_tblite.sh #EOF diff --git a/tools/toolchain/scripts/stage8/install_tblite.sh b/tools/toolchain/scripts/stage8/install_tblite.sh new file mode 100755 index 0000000000..48e87b04a1 --- /dev/null +++ b/tools/toolchain/scripts/stage8/install_tblite.sh @@ -0,0 +1,159 @@ +#!/bin/bash -e + +# TODO: Review and if possible fix shellcheck errors. +# shellcheck disable=all + +[ "${BASH_SOURCE[0]}" ] && SCRIPT_NAME="${BASH_SOURCE[0]}" || SCRIPT_NAME=$0 +SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_NAME")/.." && pwd -P)" + +tblite_ver="0.4.0" +tblite_sha256="c4a67dfbe04827095fd7598183e076fa3017a5a475c4f90fd28e78992dc19ea7" + +source "${SCRIPT_DIR}"/common_vars.sh +source "${SCRIPT_DIR}"/tool_kit.sh +source "${SCRIPT_DIR}"/signal_trap.sh +source "${INSTALLDIR}"/toolchain.conf +source "${INSTALLDIR}"/toolchain.env + +[ -f "${BUILDDIR}/setup_tblite" ] && rm "${BUILDDIR}/setup_tblite" + +TBLITE_DFLAGS='' +TBLITE_CFLAGS='' +TBLITE_LDFLAGS='' +TBLITE_LIBS='' +! [ -d "${BUILDDIR}" ] && mkdir -p "${BUILDDIR}" +cd "${BUILDDIR}" + +case "$with_tblite" in + __DONTUSE__) ;; + + __INSTALL__) + echo "==================== Installing tblite ====================" + require_env OPENBLAS_ROOT + require_env MATH_LIBS + + pkg_install_dir="${INSTALLDIR}/tblite-${tblite_ver}" + install_lock_file="${pkg_install_dir}/install_successful" + + if verify_checksums "${install_lock_file}"; then + echo " tblite-${tblite_ver} is already installed, skipping it." + else + if [ -f tblite-${tblite_ver}.tar.gz ]; then + echo " tblite-${tblite_ver}.tar.gz is found" + else + #download_pkg_from_cp2k_org "${tblite_sha256}" "tblite-${tblite_ver}.tar.gz" + wget https://github.com/tblite/tblite/archive/refs/tags/v${tblite_ver}.tar.gz -O "tblite-${tblite_ver}.tar.gz" + fi + + echo "Installing from scratch into ${pkg_install_dir}" + [ -d tblite-${tblite_ver} ] && rm -rf tblite-${tblite_ver} + tar -xzf tblite-${tblite_ver}.tar.gz + cd tblite-${tblite_ver} + + rm -Rf build + mkdir build + cd build + + CMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}:${OPENBLAS_ROOT}" cmake \ + -B . -G Ninja \ + -DCMAKE_INSTALL_PREFIX="${pkg_install_dir}" \ + -DCMAKE_C_COMPILER="${MPICC}" \ + -DCMAKE_Fortran_COMPILER="${MPIFC}" \ + -DCMAKE_VERBOSE_MAKEFILE=ON \ + .. \ + > cmake.log 2>&1 || tail -n ${LOG_LINES} cmake.log + cmake --build . -j $(get_nprocs) >> build.log 2>&1 || tail -n ${LOG_LINES} build.log + cmake --install . >> install.log 2>&1 || tail -n ${LOG_LINES} install.log + + cd .. + fi + write_checksums "${install_lock_file}" "${SCRIPT_DIR}/stage8/$(basename ${SCRIPT_NAME})" + ;; + + __SYSTEM__) + echo "==================== Finding tblite from system paths ====================" + check_command pkg-config --modversion tblite + add_include_from_paths TBLITE_CFLAGS "tblite.h" $INCLUDE_PATHS + add_include_from_paths TBLITE_CFLAGS "tblite.mod" $INCLUDE_PATHS + add_include_from_paths TBLITE_CFLAGS "mctc_io.mod" $INCLUDE_PATHS + add_lib_from_paths TBLITE_LDFLAGS "libtblite.*" $LIB_PATHS + ;; + + *) + echo "==================== Linking TBLITE to user paths ====================" + pkg_install_dir="$with_tblite" + check_dir "${pkg_install_dir}/include" + ;; + +esac + +if [ "$with_tblite" != "__DONTUSE__" ]; then + + TBLITE_DFLAGS="-D__TBLITE -D__DFTD4" + TBLITE_LIBS="-ltblite -ldftd4 -ls-dftd3 -lmulticharge -lmctc-lib -ltoml-f" + + cat << EOF > "${BUILDDIR}/setup_tblite" +export TBLITE_VER="${tblite_ver}" +EOF + + if [ "$with_tblite" != "__SYSTEM__" ]; then + TEMP_LOC=$(find ${pkg_install_dir}/include -name "tomlf.mod") + TOMLF=${TEMP_LOC%/*} + TEMP_LOC=$(find ${pkg_install_dir}/include -name "multicharge.mod") + MCHARGE=${TEMP_LOC%/*} + TEMP_LOC=$(find ${pkg_install_dir}/include -name "mstore.mod") + MSTORE=${TEMP_LOC%/*} + TEMP_LOC=$(find ${pkg_install_dir}/include -name "mctc_io.mod") + MCTC=${TEMP_LOC%/*} + TEMP_LOC=$(find ${pkg_install_dir}/include -name "dftd3.mod") + SDFTD3=${TEMP_LOC%/*} + TEMP_LOC=$(find ${pkg_install_dir}/include -name "dftd4.mod") + DFTD4=${TEMP_LOC%/*} + TEMP_LOC=$(find ${pkg_install_dir}/include -name "tblite_xtb.mod") + TBLITE=${TEMP_LOC%/*} + + TBLITE_INCLUDE_DIRS="${pkg_install_dir}/include" + # use the lib64 directory if present + TBLITE_LINK_LIBRARIES="${pkg_install_dir}/lib" + [ -d "${pkg_install_dir}/lib64" ] && TBLITE_LINK_LIBRARIES="${pkg_install_dir}/lib64" + TBLITE_CFLAGS="-I'${TOMLF}' -I'${MCTC}' -I'${SDFTD3}' -I'${DFTD4}' -I'${TBLITE}'" + TBLITE_LDFLAGS="-L'${TBLITE_LINK_LIBRARIES}' -Wl,-rpath,'${TBLITE_LINK_LIBRARIES}'" + + cat << EOF >> "${BUILDDIR}/setup_tblite" +prepend_path LD_LIBRARY_PATH "${TBLITE_LINK_LIBRARIES}" +prepend_path LD_RUN_PATH "${TBLITE_LINK_LIBRARIES}" +prepend_path LIBRARY_PATH "${TBLITE_LINK_LIBRARIES}" +prepend_path CPATH "${TBLITE_INCLUDE_DIRS}" +prepend_path PKG_CONFIG_PATH "${TBLITE_LINK_LIBRARIES}/pkgconfig" +prepend_path CMAKE_PREFIX_PATH "${pkg_install_dir}" +EOF + fi + + cat << EOF >> "${BUILDDIR}/setup_tblite" +export TOMLF="${TOMLF}" +export MCHARGE="${MCHARGE}" +export MSTORE="${MSTORE}" +export MCTC="${MCTC}" +export SDFTD3="${SDFTD3}" +export DFTD4="${DFTD4}" +export TBLITE="${TBLITE}" +export TBLITE_INCLUDE_DIRS="${TBLITE_INCLUDE_DIRS}" +export TBLITE_LINK_LIBRARIES="${TBLITE_LINK_LIBRARIES}" +export TBLITE_ROOT="${pkg_install_dir}" +export TBLITE_DFLAGS="${TBLITE_DFLAGS}" +export TBLITE_CFLAGS="${TBLITE_CFLAGS}" +export TBLITE_LDFLAGS="${TBLITE_LDFLAGS}" +export TBLITE_LIBS="${TBLITE_LIBS}" +export CP_DFLAGS="\${CP_DFLAGS} \${TBLITE_DFLAGS}" +export CP_CFLAGS="\${CP_CFLAGS} \${TBLITE_CFLAGS}" +export CP_LDFLAGS="\${CP_LDFLAGS} \${TBLITE_LDFLAGS}" +export CP_LIBS="\${TBLITE_LIBS} \${CP_LIBS}" +EOF + cat "${BUILDDIR}/setup_tblite" >> $SETUPFILE +fi + +load "${BUILDDIR}/setup_tblite" +write_toolchain_env "${INSTALLDIR}" + +cd "${ROOTDIR}" +report_timing "tblite"