diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c5c414..1d18dc8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,12 +60,16 @@ if(OPENTRIM_BUILD_GUI) find_package(QtDataBrowser REQUIRED) endif () ## BUILD GUI +include(CTest) + add_subdirectory(source) if (OPENTRIM_BUILD_TESTS) add_subdirectory(test/scattering_calc) endif () ## Tests +add_subdirectory(test/post-build) + include(CMakeSummary) diff --git a/dist/OBS/opentrim.spec b/dist/OBS/opentrim.spec index 9929dca..e7f12d7 100644 --- a/dist/OBS/opentrim.spec +++ b/dist/OBS/opentrim.spec @@ -1,5 +1,4 @@ %global debug_package %{nil} -%global __strip /bin/true Name: opentrim Version: 0 @@ -43,6 +42,7 @@ BuildRequires: libqwt-qt5-dev BuildRequires: libqt5svg5-dev BuildRequires: libeigen3-dev >= 3.4 BuildRequires: libhdf5-dev +BuildRequires: hdf5-tools %else @@ -135,6 +135,9 @@ install -d %{buildroot}/%{_datadir}/%{name}/tests cp -r test/%{name}/* %{buildroot}/%{_datadir}/%{name}/tests/ +%check +%ctest "-V" "-j1" + %post libs -p /sbin/ldconfig %postun libs -p /sbin/ldconfig diff --git a/test/post-build/CMakeLists.txt b/test/post-build/CMakeLists.txt new file mode 100644 index 0000000..0cef3d5 --- /dev/null +++ b/test/post-build/CMakeLists.txt @@ -0,0 +1,41 @@ +find_program(H5DIFF_EXECUTABLE h5diff REQUIRED) + +# loop over the test cases +foreach(N IN ITEMS 1 2 3) + + # Do it with 1 or 2 threads + foreach(J IN ITEMS 1 2) + + # Test 1 (fixture setup): run opentrim with J threads, produce out${N}j${J}.h5 + add_test(NAME PostBuild_RunSim_${N}_${J} + COMMAND "$" + -f "${CMAKE_CURRENT_SOURCE_DIR}/in${N}.json" + -j ${J} -o out${N}j${J} + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + ) + set_tests_properties(PostBuild_RunSim_${N}_${J} PROPERTIES + FIXTURES_SETUP PostBuildFixture_${N}_${J} + ) + + # Test 2: compare produced out${N}j${J}.h5 against template using h5diff + add_test(NAME PostBuild_Compare_${N}_${J} + COMMAND "${CMAKE_COMMAND}" + "-DH5DIFF=${H5DIFF_EXECUTABLE}" + "-DPRODUCED=${CMAKE_CURRENT_BINARY_DIR}/out${N}j${J}.h5" + "-DTEMPLATE=${CMAKE_CURRENT_SOURCE_DIR}/out${N}j${J}.h5" + -P "${CMAKE_CURRENT_SOURCE_DIR}/compare.cmake" + ) + set_tests_properties(PostBuild_Compare_${N}_${J} PROPERTIES + FIXTURES_REQUIRED PostBuildFixture_${N}_${J} + ) + + # Fixture cleanup + add_test(NAME PostBuild_Cleanup_${N}_${J} + COMMAND "${CMAKE_COMMAND}" -E rm -f "${CMAKE_CURRENT_BINARY_DIR}/out${N}j${J}.h5" + ) + set_tests_properties(PostBuild_Cleanup_${N}_${J} PROPERTIES + FIXTURES_CLEANUP PostBuildFixture_${N}_${J} + ) + + endforeach() +endforeach() diff --git a/test/post-build/compare.cmake b/test/post-build/compare.cmake new file mode 100644 index 0000000..2a1ae0f --- /dev/null +++ b/test/post-build/compare.cmake @@ -0,0 +1,64 @@ +set(physics_datasets + /tally/damage_events/Implantations + /tally/damage_events/Recombinations + /tally/damage_events/Replacements + /tally/damage_events/Vacancies + /tally/energy_deposition/Ionization + /tally/energy_deposition/Lattice + /tally/energy_deposition/Lost + /tally/energy_deposition/Stored + /tally/ion_stat/Collisions + /tally/ion_stat/Flight_path + /tally/ion_stat/Lost + /tally/pka_damage/Pka + /tally/pka_damage/Pka_energy + /tally/pka_damage/Tdam + /tally/pka_damage/Tdam_LSS + /tally/pka_damage/Vnrt + /tally/pka_damage/Vnrt_LSS + /tally/totals/data +) + +set(error_datasets + /tally/damage_events/Implantations_sem + /tally/damage_events/Recombinations_sem + /tally/damage_events/Replacements_sem + /tally/damage_events/Vacancies_sem + /tally/energy_deposition/Ionization_sem + /tally/energy_deposition/Lattice_sem + /tally/energy_deposition/Lost_sem + /tally/energy_deposition/Stored_sem + /tally/ion_stat/Collisions_sem + /tally/ion_stat/Flight_path_sem + /tally/ion_stat/Lost_sem + /tally/pka_damage/Pka_sem + /tally/pka_damage/Pka_energy_sem + /tally/pka_damage/Tdam_sem + /tally/pka_damage/Tdam_LSS_sem + /tally/pka_damage/Vnrt_sem + /tally/pka_damage/Vnrt_LSS_sem + /tally/totals/data_sem +) + +foreach(target IN LISTS physics_datasets) + execute_process( + COMMAND "${H5DIFF}" -v --relative=1e-9 "${PRODUCED}" "${TEMPLATE}" "${target}" + RESULT_VARIABLE result + OUTPUT_VARIABLE output + ERROR_VARIABLE error + ) + if(result) + message(FATAL_ERROR "h5diff mismatch in ${target}:\n${output}${error}") + endif() +endforeach() +foreach(target IN LISTS error_datasets) + execute_process( + COMMAND "${H5DIFF}" -v --relative=1e-6 "${PRODUCED}" "${TEMPLATE}" "${target}" + RESULT_VARIABLE result + OUTPUT_VARIABLE output + ERROR_VARIABLE error + ) + if(result) + message(FATAL_ERROR "h5diff mismatch in ${target}:\n${output}${error}") + endif() +endforeach() diff --git a/test/post-build/in1.json b/test/post-build/in1.json new file mode 100644 index 0000000..da738ea --- /dev/null +++ b/test/post-build/in1.json @@ -0,0 +1,76 @@ +{ + "Run": { + "max_no_ions": 100, + "threads": 0 + }, + "Transport": { + "flight_path_type": "Variable" + }, + "IonBeam": { + "ion": { + "symbol": "Fe", + "atomic_mass": 55.935 + }, + "energy_distribution": { + "center": 2e6 + }, + "spatial_distribution": { + "center": [ + 0, + 600, + 600 + ] + } + }, + "Target": { + "size": [ + 1200, + 1200, + 1200 + ], + "cell_count": [ + 100, + 1, + 1 + ], + "periodic_bc": [ + 0, + 1, + 1 + ], + "materials": [ + { + "id": "Fe", + "density": 7.8658, + "composition": [ + { + "element": { + "symbol": "Fe" + }, + "X": 1, + "Ed": 40, + "El": 3, + "Es": 3, + "Er": 40, + "Rc": 0.8 + } + ] + } + ], + "regions": [ + { + "id": "R1", + "material_id": "Fe", + "size": [ + 1200, + 1200, + 1200 + ] + } + ] + }, + "Output": { + "title": "2MeV Fe on Fe", + "outfilename": "out1" + } +} \ No newline at end of file diff --git a/test/post-build/in2.json b/test/post-build/in2.json new file mode 100644 index 0000000..29c3675 --- /dev/null +++ b/test/post-build/in2.json @@ -0,0 +1,85 @@ +{ + "Run": { + "max_no_ions": 50, + "threads": 0 + }, + "Simulation": { + "nrt_calculation": "NRT_average" + }, + "IonBeam": { + "ion": { + "symbol": "Xe", + "atomic_mass": 131.904 + }, + "energy_distribution": { + "center": 3e6 + }, + "spatial_distribution": { + "center": [ + 0, + 600, + 600 + ] + } + }, + "Target": { + "size": [ + 1200, + 1200, + 1200 + ], + "cell_count": [ + 100, + 1, + 1 + ], + "periodic_bc": [ + 0, + 1, + 1 + ], + "materials": [ + { + "id": "Uranium oxide", + "density": 10.97, + "composition": [ + { + "element": { + "symbol": "U" + }, + "X": 1, + "Ed": 40, + "El": 4, + "Es": 4, + "Er": 20 + }, + { + "element": { + "symbol": "O" + }, + "X": 2, + "Ed": 20, + "El": 2, + "Es": 2, + "Er": 20 + } + ] + } + ], + "regions": [ + { + "id": "R1", + "material_id": "Uranium oxide", + "size": [ + 1200, + 1200, + 1200 + ] + } + ] + }, + "Output": { + "title": "3MeV Xe on UO2", + "outfilename": "out2" + } +} \ No newline at end of file diff --git a/test/post-build/in3.json b/test/post-build/in3.json new file mode 100644 index 0000000..208fbee --- /dev/null +++ b/test/post-build/in3.json @@ -0,0 +1,75 @@ +{ + "Run": { + "max_no_ions": 10000, + "threads": 0 + }, + "Transport": { + "flight_path_type": "Variable" + }, + "IonBeam": { + "ion": { + "symbol": "H", + "atomic_mass": 1.00784 + }, + "energy_distribution": { + "center": 3e6 + }, + "spatial_distribution": { + "center": [ + 0, + 5000, + 5000 + ] + } + }, + "Target": { + "size": [ + 50000, + 10000, + 10000 + ], + "cell_count": [ + 100, + 1, + 1 + ], + "periodic_bc": [ + 0, + 1, + 1 + ], + "materials": [ + { + "id": "Fe", + "density": 7.8658, // g/cm^3 + "composition": [ + { + "element": { + "symbol": "Fe" + }, + "X": 1, + "Ed": 40, + "El": 3, + "Es": 3, + "Er": 40 + } + ] + } + ], + "regions": [ + { + "id": "R1", + "material_id": "Fe", + "size": [ + 50000, + 10000, + 10000 + ] + } + ] + }, + "Output": { + "title": "3 MeV H on Fe", + "outfilename": "out3" + } +} \ No newline at end of file diff --git a/test/post-build/make_templates.sh b/test/post-build/make_templates.sh new file mode 100755 index 0000000..7743b12 --- /dev/null +++ b/test/post-build/make_templates.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# Usage: ./make_templates.sh +# Creates template output files out{N}j{J}.h5 for all test cases. + +set -e + +OPENTRIM=${1:?Usage: $0 } +SCRIPT_DIR=$(dirname "$(realpath "$0")") + +cd "$SCRIPT_DIR" + +for N in 1 2 3; do + for J in 1 2; do + echo "Running: in${N}.json -j ${J} -> out${N}j${J}.h5" + "$OPENTRIM" -f "in${N}.json" -j "$J" -o "out${N}j${J}" + done +done + +echo "Done. Template files created in $SCRIPT_DIR" diff --git a/test/post-build/out1j1.h5 b/test/post-build/out1j1.h5 new file mode 100644 index 0000000..5f06074 Binary files /dev/null and b/test/post-build/out1j1.h5 differ diff --git a/test/post-build/out1j2.h5 b/test/post-build/out1j2.h5 new file mode 100644 index 0000000..b16ec0e Binary files /dev/null and b/test/post-build/out1j2.h5 differ diff --git a/test/post-build/out2j1.h5 b/test/post-build/out2j1.h5 new file mode 100644 index 0000000..a4a3d09 Binary files /dev/null and b/test/post-build/out2j1.h5 differ diff --git a/test/post-build/out2j2.h5 b/test/post-build/out2j2.h5 new file mode 100644 index 0000000..f605886 Binary files /dev/null and b/test/post-build/out2j2.h5 differ diff --git a/test/post-build/out3j1.h5 b/test/post-build/out3j1.h5 new file mode 100644 index 0000000..767e359 Binary files /dev/null and b/test/post-build/out3j1.h5 differ diff --git a/test/post-build/out3j2.h5 b/test/post-build/out3j2.h5 new file mode 100644 index 0000000..f937a30 Binary files /dev/null and b/test/post-build/out3j2.h5 differ