# External verification runs only native PIMD derivatives.
if(NOT DEEPMD OR NOT DEEPMD_TEST_MODEL)
  return()
endif()
if(NOT EXISTS "${DEEPMD_TEST_MODEL}" OR IS_DIRECTORY "${DEEPMD_TEST_MODEL}")
  message(FATAL_ERROR "DEEPMD_TEST_MODEL must name an existing frozen model")
endif()

foreach(case IN ITEMS h2o water64)
  set(work "${CMAKE_CURRENT_BINARY_DIR}/${case}/fixture")
  file(MAKE_DIRECTORY "${work}")
  configure_file("${case}/pimd.toml" "${work}/pimd.toml" COPYONLY)
  configure_file("${case}/structure.dat" "${work}/structure.dat" COPYONLY)
  configure_file("${DEEPMD_TEST_MODEL}" "${work}/frozen_model.pb" COPYONLY)
  set(check force)
  if(case STREQUAL "water64")
    set(check virial)
  endif()
  string(TOUPPER "${check}" method)
  foreach(bead_count IN ITEMS 1 3)
    set(name deepmd-${check}-${case})
    set(processes 1)
    if(bead_count GREATER 1)
      string(APPEND name "-${bead_count}beads")
      set(processes 2)
    endif()
    add_derivative_test(${name} "${work}"
      "${CMAKE_CURRENT_BINARY_DIR}/${case}/${check}-${bead_count}beads"
      MODE mpi MPI_RANKS ${processes} THREADS 1
      RUNTIME_TIMEOUT 800 TIMEOUT 900
      WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${case}"
      ENVIRONMENT "OMP_NUM_THREADS=1;OPENBLAS_NUM_THREADS=1;PYTHONDONTWRITEBYTECODE=1;TF_INTRA_OP_PARALLELISM_THREADS=1;TF_INTER_OP_PARALLELISM_THREADS=1"
      LABELS validation deepmd external derivatives deepmd-derivatives
      OPTIONS "--setup=${CMAKE_CURRENT_SOURCE_DIR}/setup.py"
        "--method=TEST${method}" "--beads=${bead_count}")
  endforeach()
endforeach()
