# External verification runs only native PIMD derivatives.
if(NOT DFTB+ OR NOT DFTBPLUS_TEST_SK_DIR)
  return()
endif()
if(NOT EXISTS "${DFTBPLUS_TEST_SK_DIR}/mio-1-1/O-H.skf")
  message(FATAL_ERROR "DFTBPLUS_TEST_SK_DIR must contain mio-1-1/O-H.skf")
endif()
set(mode serial)
set(processes 1)
if(DftbPlus_WITH_MPI)
  set(mode mpi)
  set(processes 4)
  if(VALIDATION_MPI_RANKS GREATER 0)
    set(processes ${VALIDATION_MPI_RANKS})
  endif()
endif()
set(threads 1)
if(DftbPlus_WITH_OMP)
  set(threads 2)
endif()
foreach(case IN ITEMS h2o water32)
  set(work "${CMAKE_CURRENT_BINARY_DIR}/${case}/fixture")
  file(MAKE_DIRECTORY "${work}")
  configure_file("${case}/dftb.dat.in" "${work}/dftb.dat" @ONLY)
  configure_file("${case}/pimd.toml" "${work}/pimd.toml" COPYONLY)
  configure_file("${case}/structure.dat" "${work}/structure.dat" COPYONLY)
  set(checks force)
  if(case STREQUAL "water32")
    list(APPEND checks virial)
  endif()
  foreach(check IN LISTS checks)
    set(bead_counts 1)
    if((check STREQUAL "force" AND case STREQUAL "h2o") OR check STREQUAL "virial")
      list(APPEND bead_counts 3)
    endif()
    foreach(bead_count IN LISTS bead_counts)
      set(name dftbplus-${check}-${case})
      if(bead_count GREATER 1)
        string(APPEND name "-${bead_count}beads")
      endif()
      string(TOUPPER "${check}" method)
      add_derivative_test(${name} "${work}"
        "${CMAKE_CURRENT_BINARY_DIR}/${case}/${check}-${bead_count}beads"
        MODE ${mode} MPI_RANKS ${processes} THREADS ${threads}
        RUNTIME_TIMEOUT 600 TIMEOUT 660
        WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${case}"
        LABELS validation dftbplus external derivatives dftbplus-derivatives
        OPTIONS "--setup=${CMAKE_CURRENT_SOURCE_DIR}/setup.py"
          "--method=TEST${method}" "--beads=${bead_count}")
    endforeach()
  endforeach()
endforeach()
