0f0200396f0519087be651958a4bd5e373541ee6
[alexxy/gromacs.git] / tests / CMakeLists.txt
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012,2013,2014,2015,2016,2017,2018,2019,2020, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34
35 set(REGRESSIONTEST_PATH "" CACHE PATH "Directory containing regressiontests")
36 mark_as_advanced(REGRESSIONTEST_PATH)
37 option(REGRESSIONTEST_DOWNLOAD
38     "Automatically download regressiontests. Tests can be run with ctest." OFF)
39
40 if(REGRESSIONTEST_DOWNLOAD)
41     if (NOT SOURCE_IS_SOURCE_DISTRIBUTION)
42         set(REGRESSIONTEST_URL http://gerrit.gromacs.org/snapshot/${REGRESSIONTEST_BRANCH})
43         # REGRESSIONTEST_PATH for dev trees is set later based on the dirname found in the tar
44     else()
45         set(REGRESSIONTEST_URL http://gerrit.gromacs.org/download/regressiontests-${REGRESSIONTEST_VERSION}.tar.gz)
46         set(REGRESSIONTEST_PATH
47             "${CMAKE_CURRENT_BINARY_DIR}/regressiontests-${REGRESSIONTEST_VERSION}"
48             CACHE PATH "Path to auto-downloaded regressiontests" FORCE)
49     endif()
50     set(REGRESSIONTEST_FILE "${CMAKE_CURRENT_BINARY_DIR}/regressiontests.tgz")
51     message("Downloading: ${REGRESSIONTEST_URL}")
52     file(DOWNLOAD ${REGRESSIONTEST_URL} "${REGRESSIONTEST_FILE}" SHOW_PROGRESS STATUS status LOG log)
53     list(GET status 0 status_code)
54     list(GET status 1 status_string)
55
56     if(NOT status_code EQUAL 0)
57         message(FATAL_ERROR "error: downloading '${REGRESSIONTEST_URL}' failed
58 status_code: ${status_code}
59 status_string: ${status_string}
60 log: ${log}")
61     endif()
62     if (SOURCE_IS_SOURCE_DISTRIBUTION)
63         file(MD5 ${REGRESSIONTEST_FILE} COMPUTED_MD5SUM)
64         if(NOT ${REGRESSIONTEST_MD5SUM} STREQUAL ${COMPUTED_MD5SUM})
65             message(FATAL_ERROR "Download of regressiontests failed. Expected MD5 of ${REGRESSIONTEST_MD5SUM} but download has ${COMPUTED_MD5SUM}")
66         endif()
67     else()
68         # Extract the REGRESSIONTEST_PATH from the tar when using dev tree.
69         execute_process(COMMAND ${CMAKE_COMMAND} -E tar tf "${REGRESSIONTEST_FILE}"
70                 RESULT_VARIABLE _tar_tf_res
71                 OUTPUT_VARIABLE _tar_tf_out)
72         if (${_tar_tf_res} EQUAL 0)
73             string(REGEX REPLACE "/\n.*$" "" _regressiontest_dir "${_tar_tf_out}")
74             set(REGRESSIONTEST_PATH "${CMAKE_CURRENT_BINARY_DIR}/${_regressiontest_dir}"
75                 CACHE PATH "Path to auto-downloaded regressiontests" FORCE)
76         else()
77             message(FATAL_ERROR "Failed to list the contents of the downloaded tarball: ${REGRESSIONTEST_FILE}")
78         endif()
79     endif()
80
81     file(REMOVE_RECURSE "${REGRESSIONTEST_PATH}") #delete potential prior folder
82     execute_process(COMMAND ${CMAKE_COMMAND} -E tar xf "${REGRESSIONTEST_FILE}"
83         WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
84     if(NOT EXISTS ${REGRESSIONTEST_PATH}/gmxtest.pl)
85         message(FATAL_ERROR "Download incorrect. Doesn't contain required gmxtest.pl")
86     endif()
87     set(REGRESSIONTEST_DOWNLOAD OFF CACHE BOOL "Tests already downloaded. Set to yes to download again" FORCE)
88 endif()
89
90 if(REGRESSIONTEST_PATH AND (CMAKE_CROSSCOMPILING OR CMAKE_CONFIGURATION_TYPES OR GMX_BUILD_MDRUN_ONLY))
91     # TODO: It would be nicer to do these checks before potentially downloading the tests.
92     # Cross-compiling toolchains require us to compile both front-end and
93     # back-end binaries to run gmxtest.pl.
94     # Testing an mdrun-only builds require supporting binaries from a full build
95     message(WARNING
96         "With cross-compiling, multi-configuration generators (e.g. Visual Studio), or with mdrun-only builds, running regressiontests from build system is not supported. Please run gmxtest.pl directly.")
97     set(REGRESSIONTEST_PATH OFF CACHE BOOL
98         "With cross-compiling or multi-configuration generators, running regressiontests from build system is not supported." FORCE)
99 endif()
100
101 if(REGRESSIONTEST_PATH)
102     if(NOT EXISTS ${REGRESSIONTEST_PATH}/gmxtest.pl)
103         message(FATAL_ERROR
104             "REGRESSIONTEST_PATH invalid. The path needs to contain gmxtest.pl.")
105     endif()
106
107     # gmxtests target builds all binaries required for running gmxtest
108     add_custom_target(gmxtests DEPENDS gmx)
109     add_dependencies(run-ctest gmxtests)
110     add_dependencies(run-ctest-nophys gmxtests)
111
112     if(GMX_DOUBLE)
113         list(APPEND ARGS -double)
114     endif()
115     if(GMX_LIB_MPI AND NOT MPIEXEC) # find_program failed
116         message(WARNING
117             "Please set MPIEXEC. Otherwise mpirun is assumed for running tests.")
118     endif()
119     if(DEFINED ENV{CI_JOB_ID} AND GMX_OPENMP)
120         set(GMX_REGRESSIONTEST_OMP_THREADS 2 CACHE STRING
121             "Number of OpenMP threads to use with the regression tests")
122         mark_as_advanced(GMX_REGRESSIONTEST_OMP_THREADS)
123         list(APPEND ARGS -ntomp ${GMX_REGRESSIONTEST_OMP_THREADS})
124     endif()
125     if(GMX_LIB_MPI)
126         set(GMX_TEST_NUMBER_PROCS 8 CACHE STRING "Number of processors used for testing")
127         mark_as_advanced(GMX_TEST_NUMBER_PROCS)
128         list(APPEND ARGS -np ${GMX_TEST_NUMBER_PROCS})
129         if(MPIEXEC)
130             list(APPEND ARGS -mpirun ${MPIEXEC})
131         endif()
132         #We should use MPIEXEC_NUMPROC_FLAG but gmxtest.pl doesn't let us pass it
133     endif()
134     if(GMX_BINARY_SUFFIX)
135         list(APPEND ARGS -suffix ${GMX_BINARY_SUFFIX})
136     endif()
137     #crosscompile is only used to disable checking whether binaries work
138     #given that we know they are there and that mdrun might not be exectuable
139     #(e.g. Cray) we enable it.
140     list(APPEND ARGS -crosscompile)
141
142     set(REGRESSIONTEST_EXTRA_ARGS "" CACHE STRING 
143         "Extra arguments passed to gmxtest")
144     mark_as_advanced(REGRESSIONTEST_EXTRA_ARGS)
145     list(APPEND ARGS ${REGRESSIONTEST_EXTRA_ARGS})
146
147     if(DEFINED ENV{CI_JOB_ID} AND GMX_THREAD_MPI)
148         set(GMX_REGRESSIONTEST_THREAD_MPI_THREADS 2 CACHE STRING
149         "Number of thread MPI threads to use for testing")
150         mark_as_advanced(GMX_REGRESSIONTEST_THREAD_MPI_THREADS)
151         list(APPEND ARGS -nt ${GMX_REGRESSIONTEST_THREAD_MPI_THREADS})
152     endif()
153     list(APPEND ARGS -noverbose -nosuffix)
154
155     if(GMX_NATIVE_WINDOWS)
156         set(PATH_SEPARATOR "\\;")
157         #replacing \ with / shouldn't be neccessary. But otherwise "..\bin\;c:\.."
158         #gets turned into "...\bin\\c:\.." don't know why and don't have a better
159         #workaround. This workaround doesn't hurt.
160         string(REPLACE "\\" "/" PATH "$ENV{PATH}")
161         #protect ; (don't treat as list)
162         string(REPLACE ";" "\\;" PATH "${PATH}")
163     else()
164         set(PATH_SEPARATOR ":")
165         set(PATH "$ENV{PATH}")
166     endif()
167
168     foreach(FOLDER bin lib) #lib folders might be needed for
169         #e.g. DLLs. For GMX paths native ("\") is needed for GMXLIB detection
170         file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/${FOLDER}" DIR)
171         set(PATH "${DIR}${PATH_SEPARATOR}${PATH}")
172     endforeach()
173
174     find_program(PERL_EXECUTABLE NAMES "perl")
175     mark_as_advanced(PERL_EXECUTABLE)
176
177     if (NOT PERL_EXECUTABLE)
178         message(FATAL_ERROR "Perl not found. Install perl, set PERL_EXECUTABLE to the perl location, or unset REGRESSIONTEST_PATH to disable testing.")
179     endif()
180
181     #currently not testing tools because they don't contain any useful tests
182     foreach(subtest complex freeenergy rotation essentialdynamics)
183         add_test(NAME regressiontests/${subtest}
184             #windows requires the command to be perl and not the script
185             COMMAND perl "${REGRESSIONTEST_PATH}/gmxtest.pl" ${subtest} ${ARGS})
186         set_tests_properties(regressiontests/${subtest} PROPERTIES
187             ENVIRONMENT "PATH=${PATH}")
188     endforeach()
189 else()
190     gmx_add_missing_tests_notice("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.")
191 endif()
192
193
194 #
195 # Physical validation tests are opt-in via -DGMX_PHYSICAL_VALIDATION=ON
196 #
197 if(GMX_PHYSICAL_VALIDATION)
198     include(FindPythonModule)
199     # physical validation suite is distributed with the source
200     set(PHYSVALTEST_SOURCE_PATH
201             "${CMAKE_CURRENT_SOURCE_DIR}/physicalvalidation")
202     #       CACHE PATH "Source directory containing physical validation tests.")
203     if(NOT EXISTS ${PHYSVALTEST_SOURCE_PATH}/gmx_physicalvalidation.py)
204         message(FATAL_ERROR
205             "GMX_PHYSICAL_VALIDATION set, but physical validation script not found in ${PHYSVALTEST_SOURCE_PATH}.")
206     endif()
207
208     if(CMAKE_CROSSCOMPILING OR CMAKE_CONFIGURATION_TYPES OR GMX_BUILD_MDRUN_ONLY)
209         # The following comment is copied from regression tests:
210         #     Cross-compiling toolchains require us to compile both front-end and
211         #     back-end binaries to run gmxtest.pl.
212         #     Testing an mdrun-only builds require supporting binaries from a full build
213         # TODO: Look into the details of this.
214         # For now, turn it off - our python-gmx interface is probably not that stable for special cases anyway
215         message(WARNING
216                 "With cross-compiling, multi-configuration generators (e.g. Visual Studio), or with mdrun-only builds,\
217                 running physical validation tests from build system is not supported.\
218                 Please run physicalvalidation.py directly.")
219         set(GMX_PHYSICAL_VALIDATION OFF CACHE BOOL
220                 "With cross-compiling or multi-configuration generators, running physical validation tests from build\
221                 system is not supported." FORCE)
222
223     else()
224         #
225         # Making sure gmx is built before running physical validation tests
226         #
227         add_dependencies(run-ctest-phys gmx)
228         add_dependencies(run-ctest gmx)
229
230         #
231         # Determine arguments passed to physicalvalidation.py
232         #
233         set(PARGS "")
234         list(APPEND PARGS --wd ${CMAKE_CURRENT_BINARY_DIR}/physicalvalidation)
235         list(APPEND PARGS --bindir ${CMAKE_BINARY_DIR}/bin)
236
237         if(GMX_LIB_MPI AND NOT MPIEXEC) # find_program failed
238             message(WARNING
239                 "Please set MPIEXEC. Otherwise mpirun is assumed for running tests.")
240         endif()
241         if(GMX_LIB_MPI)
242             # define number of processors in analogy to regression tests?
243             if(MPIEXEC)
244                 list(APPEND ARGS --mpicmd ${MPIEXEC})
245             else()
246                 list(APPEND ARGS --mpicmd mpirun)
247             endif()
248         endif()
249         if(GMX_LIB_MPI)
250             message(FATAL_ERROR
251                 "Physical validation using MPI not supported.")
252         endif()
253
254         if(GMX_BINARY_SUFFIX)
255             list(APPEND PARGS --suffix ${GMX_BINARY_SUFFIX})
256         endif()
257
258         #
259         # Give possibility to add args via cache
260         #
261         set(PHYSVALTEST_EXTRA_ARGS "" CACHE STRING
262             "Extra arguments passed to phystest")
263         mark_as_advanced(PHYSVALTEST_EXTRA_ARGS)
264         list(APPEND PARGS ${PHYSVALTEST_EXTRA_ARGS})
265
266         #
267         # The following lines are directly copied from regression tests.
268         # They seem to be applicable to physical validation tests as well.
269         #
270         if(GMX_NATIVE_WINDOWS)
271             set(PATH_SEPARATOR "\\;")
272             #replacing \ with / shouldn't be neccessary. But otherwise "..\bin\;c:\.."
273             #gets turned into "...\bin\\c:\.." don't know why and don't have a better
274             #workaround. This workaround doesn't hurt.
275             string(REPLACE "\\" "/" PATH "$ENV{PATH}")
276             #protect ; (don't treat as list)
277             string(REPLACE ";" "\\;" PATH "${PATH}")
278         else()
279             set(PATH_SEPARATOR ":")
280             set(PATH "$ENV{PATH}")
281         endif()
282
283         foreach(FOLDER bin lib) #lib folders might be needed for
284                                 #e.g. DLLs. For GMX paths native ("\") is needed for GMXLIB detection
285             file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/${FOLDER}" DIR)
286             set(PATH "${DIR}${PATH_SEPARATOR}${PATH}")
287         endforeach()
288         #
289         # End copied from regression tests.
290         #
291
292         if (NOT PYTHONINTERP_FOUND)
293             message(FATAL_ERROR
294                     "Python not found. Physical validation requires python. \
295                      Install python, set PYTHON_EXECUTABLE to a valid python location, \
296                      or set GMX_PHYSICAL_VALIDATION=OFF to disable the physical validation tests.")
297         endif()
298         #
299         foreach(module numpy scipy pymbar) # add further modules if necessary
300             find_python_module(${module})
301             string(TOUPPER ${module} module_upper)
302             if(NOT PYTHONMODULE_${module_upper})
303                 message(WARNING
304                         "Python module ${module} not found. Physical validation relies on ${module}. Make sure\
305                         ${module} can be found by PYTHON_EXECUTABLE, or set GMX_PHYSICAL_VALIDATION=OFF to disable\
306                         the physical validation tests.")
307             endif()
308         endforeach()
309
310         #
311         # Hook in our own tests
312         # Read them from json file to make every system a separate test
313         #
314         if (GMX_DOUBLE)
315             set(PHYSVALTEST_JSON "${PHYSVALTEST_SOURCE_PATH}/systems_d.json")
316         else()
317             set(PHYSVALTEST_JSON "${PHYSVALTEST_SOURCE_PATH}/systems.json")
318         endif()
319         file(STRINGS "${PHYSVALTEST_JSON}" json)
320         string(REPLACE "\"" "" json ${json})
321         string(REPLACE "," "" json ${json})
322         string(REPLACE " " "" json ${json})
323         string(REPLACE ";" "<<>>" json ${json})
324         string(REPLACE "[" "" json ${json})
325         string(REPLACE "]" "" json ${json})
326         string(REPLACE "{" "" json ${json})
327         string(REPLACE "}" "" json ${json})
328         string(REPLACE "<<>>" ";" json ${json})
329         foreach(line ${json})
330             if("${line}" MATCHES "name:")
331                 string(REPLACE "name:" "" testname ${line})
332                 add_test(NAME physicalvalidationtests/${testname}
333                         COMMAND ${PYTHON_EXECUTABLE} "${PHYSVALTEST_SOURCE_PATH}/gmx_physicalvalidation.py" "${PHYSVALTEST_JSON}" -s ${testname} -a ${PARGS})
334                 set_tests_properties(physicalvalidationtests/${testname} PROPERTIES
335                         ENVIRONMENT "PATH=${PATH}"
336                         LABELS "PhysicalValidationTest")
337             endif()
338         endforeach()
339
340         #
341         # Create prepare and run targets while all variables are set
342         # Will be referenced in CheckTarget.cmake
343         #
344         # "check-phys-prepare" prepares the systems needed for physical validation for external running
345         add_custom_target(check-phys-prepare
346                           COMMAND ${PYTHON_EXECUTABLE} "${PHYSVALTEST_SOURCE_PATH}/gmx_physicalvalidation.py" "${PHYSVALTEST_JSON}" -p ${PARGS}
347                           COMMENT "Preparing systems for physical validation"
348                           DEPENDS gmx)
349         # "run-physval-sims" prepares and runs the systems needed for physical validation
350         add_custom_target(run-physval-sims
351                           COMMAND ${PYTHON_EXECUTABLE} "${PHYSVALTEST_SOURCE_PATH}/gmx_physicalvalidation.py" "${PHYSVALTEST_JSON}" -r ${PARGS}
352                           COMMENT "Preparing and running systems for physical validation"
353                           DEPENDS gmx)
354     endif()
355 else()
356     #
357     # Create dummy prepare and run targets
358     # Will be referenced in CheckTarget.cmake
359     #
360     # "check-phys-prepare" prepares the systems needed for physical validation for external running
361     add_custom_target(check-phys-prepare
362                       COMMAND ${CMAKE_COMMAND} -E echo "NOTE: You called the target `check-phys-prepare`, but ran cmake with\
363  `-DGMX_PHYSICAL_VALIDATION=OFF`. The physical validation tests are therefore unavailable,\
364  and this target is not doing anything."
365                       COMMENT "No physical validation" VERBATIM)
366     # "run-physval-sims" prepares and runs the systems needed for physical validation
367     add_custom_target(run-physval-sims
368                       COMMAND ${CMAKE_COMMAND} -E echo "NOTE: You called the target `run-physval-sims`, but ran cmake with\
369  `-DGMX_PHYSICAL_VALIDATION=OFF`. The physical validation tests are therefore unavailable,\
370  and this target is not doing anything."
371                       COMMENT "No physical validation" VERBATIM)
372 endif()
373
374 gmx_create_missing_tests_notice_target()
375