Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests & Validations #35

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
build/
build*/
5 changes: 5 additions & 0 deletions CIValidations/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ foreach(app
MaCh3ModeValidations
SamplePDFValidations
NuMCMCconversion
NuOscillatorInterfaceValidations
)
add_executable(${app} ${app}.cpp)
target_link_libraries(${app} MaCh3Tutorial::ValidationsUtils MaCh3Tutorial::samplePDFTutorial)
Expand All @@ -25,3 +26,7 @@ install(DIRECTORY
Scripts

DESTINATION CIValidations)

if(MaCh3Tutorial_UNITTESTS_ENABLED)
add_subdirectory(UnitTests)
endif()
66 changes: 66 additions & 0 deletions CIValidations/NuOscillatorInterfaceValidations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "Utils/Comparison.h"

#include "Oscillator/OscillatorFactory.h"

#include "spdlog/fmt/bundled/core.h"

#include <cmath>
#include <fstream>

int main() {

std::ofstream configf("NuOscillatorInterfaceValidations.yml");
configf << "General:\n"
" Verbosity: \"NONE\"\n"
" CosineZIgnored: true\n"
" CalculationType: \"Unbinned\"\n"
"\n"
"OscProbCalcerSetup:\n"
" ImplementationName: \"CUDAProb3Linear\"\n"
" OscChannelMapping:\n"
" - Entry: \"Muon:Electron\"\n"
" - Entry: \"Muon:Muon\"\n";
configf.close();

auto osc = OscillatorFactory().CreateOscillator(
"NuOscillatorInterfaceValidations.yml");

std::vector<double> Energies;
double start = -3;
double end = 3;
size_t nbins = 100;
double lbw = (end - start) / double(nbins);
for (size_t i = 0; i < nbins; i++) {
double epower = start + i * lbw;
Energies.push_back(std::pow(10.0, epower));
}
osc->SetEnergyArrayInCalcer(Energies);
osc->Setup();

std::vector<double> OscParProp = {0.3, 0.5, 0.020, 7.53e-5,
2.494e-3, 0.0, 1300, 2.6};
osc->CalculateProbabilities(OscParProp);

std::ofstream outFile("NewNuOscOut.txt");

outFile << fmt::format("{:14}, {:14}, {:14}", "E (GeV)", "p(numu->numu)",
"p(numu->nue)\n");
for (auto E : Energies) {
double pdisp = osc->ReturnOscillationProbability(NuOscillator::kMuon,
NuOscillator::kMuon, E);
double papp = osc->ReturnOscillationProbability(NuOscillator::kMuon,
NuOscillator::kElectron, E);
outFile << fmt::format("{:14.12f}, {:14.12f}, {:14.12f}\n", E, pdisp, papp);
}
outFile.close();

bool TheSame = CompareTwoFiles("CIValidations/TestOutputs/NuOscOut.txt",
"NewNuOscOut.txt");

if (!TheSame) {
MACH3LOG_CRITICAL("Different osc mate");
throw MaCh3Exception(__FILE__, __LINE__);
} else {
MACH3LOG_INFO("Everything is correct");
}
}
101 changes: 101 additions & 0 deletions CIValidations/TestOutputs/NuOscOut.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
E (GeV) , p(numu->numu) , p(numu->nue)
0.001000000000, 0.200768832585, 0.325308707412
0.001148153621, 0.533692785686, 0.359677704372
0.001318256739, 0.704585547933, 0.083906274495
0.001513561248, 0.889317164744, 0.001413872374
0.001737800829, 0.277598696137, 0.232732303437
0.001995262315, 0.468008922528, 0.159260782677
0.002290867653, 0.618569034373, 0.094410354340
0.002630267992, 0.927777866537, 0.000463778042
0.003019951720, 0.982316372139, 0.002323159215
0.003467368505, 0.349646486188, 0.496237959366
0.003981071706, 0.156619946160, 0.056465144339
0.004570881896, 0.392437213249, 0.543349860355
0.005248074602, 0.269986166059, 0.577299610686
0.006025595861, 0.299071472546, 0.611211644952
0.006918309709, 0.440250612555, 0.470368220253
0.007943282347, 0.027912568891, 0.068671931760
0.009120108394, 0.036923527513, 0.325154897868
0.010471285481, 0.308860334171, 0.379465920692
0.012022644346, 0.563586682124, 0.157933556616
0.013803842646, 0.111408365344, 0.209470936275
0.015848931925, 0.181308229321, 0.345541539234
0.018197008586, 0.805760753271, 0.045626852104
0.020892961309, 0.002720226364, 0.124180013699
0.023988329190, 0.202832349161, 0.310613868123
0.027542287033, 0.144470532967, 0.342779274501
0.031622776602, 0.058942767468, 0.251179969422
0.036307805477, 0.986606701634, 0.013362706372
0.041686938347, 0.353916984812, 0.023865566706
0.047863009232, 0.561114532723, 0.115926798458
0.054954087386, 0.099225722610, 0.436792799445
0.063095734448, 0.120050353372, 0.459120093663
0.072443596007, 0.217335215052, 0.643618550294
0.083176377110, 0.345695883449, 0.415110066420
0.095499258602, 0.424629226074, 0.336611779089
0.109647819614, 0.522063285016, 0.419294686812
0.125892541179, 0.034546716731, 0.338459048920
0.144543977075, 0.546675747419, 0.327213918530
0.165958690744, 0.804148269686, 0.167715063200
0.190546071796, 0.839523505956, 0.113733248151
0.218776162395, 0.863461135229, 0.122264199166
0.251188643151, 0.384982802960, 0.161521162926
0.288403150313, 0.082433227331, 0.083599097912
0.331131121483, 0.961280570207, 0.036877860133
0.380189396321, 0.002033691716, 0.085705098797
0.436515832240, 0.969726414574, 0.023407230972
0.501187233627, 0.189024766264, 0.038186750299
0.575439937337, 0.334933525006, 0.057210932585
0.660693448008, 0.995371604077, 0.004579626576
0.758577575029, 0.489120144661, 0.018003522600
0.870963589956, 0.005008776491, 0.048642671461
1.000000000000, 0.272931984128, 0.038833215080
1.148153621497, 0.783316261270, 0.011675517008
1.318256738556, 0.999719405635, 0.000001174755
1.513561248436, 0.854703592511, 0.009012941084
1.737800828749, 0.539502030259, 0.027480882200
1.995262314969, 0.243547995654, 0.044194828960
2.290867652768, 0.060153005538, 0.053912190875
2.630267991895, 0.000995607309, 0.056311597545
3.019951720402, 0.037499032522, 0.053342101220
3.467368504525, 0.131890454005, 0.047331911581
3.981071705535, 0.252268425188, 0.040139097852
4.570881896149, 0.376825696276, 0.032965312481
5.248074602498, 0.492928403949, 0.026458289517
6.025595860744, 0.594606702661, 0.020885868202
6.918309709189, 0.680103425317, 0.016289716604
7.943282347243, 0.750016909035, 0.012594524547
9.120108393559, 0.806071361933, 0.009676223159
10.471285480509, 0.850379284842, 0.007400384725
12.022644346174, 0.885039662841, 0.005641457091
13.803842646029, 0.911945534269, 0.004290729003
15.848931924611, 0.932712744120, 0.003258196027
18.197008586100, 0.948673503881, 0.002471446673
20.892961308540, 0.960900981338, 0.001873329035
23.988329190195, 0.970245846519, 0.001419324124
27.542287033382, 0.977374704764, 0.001075069330
31.622776601684, 0.982805597146, 0.000814210841
36.307805477010, 0.986938662442, 0.000616627603
41.686938347034, 0.990081579463, 0.000467005204
47.863009232264, 0.992470138454, 0.000353712681
54.954087385762, 0.994284584385, 0.000267929365
63.095734448019, 0.995662442299, 0.000202972191
72.443596007499, 0.996708493662, 0.000153780643
83.176377110267, 0.997502487043, 0.000116524102
95.499258602144, 0.998105069651, 0.000088303381
109.647819614319, 0.998562334382, 0.000066924311
125.892541179417, 0.998909296460, 0.000050726218
144.543977074593, 0.999172546496, 0.000038452071
165.958690743756, 0.999372272107, 0.000029150251
190.546071796325, 0.999523796692, 0.000022100231
218.776162394955, 0.999638749703, 0.000016756376
251.188643150958, 0.999725956122, 0.000012705424
288.403150312661, 0.999792112182, 0.000009634322
331.131121482591, 0.999842298528, 0.000007305897
380.189396320561, 0.999880369816, 0.000005540437
436.515832240166, 0.999909250441, 0.000004201753
501.187233627272, 0.999931158976, 0.000003186626
575.439937337157, 0.999947778492, 0.000002416819
660.693448007596, 0.999960385795, 0.000001833023
758.577575029184, 0.999969949473, 0.000001390277
870.963589956081, 0.999977204301, 0.000001054492
4 changes: 4 additions & 0 deletions CIValidations/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_executable(manager_tests manager_tests.cpp)
target_link_libraries(manager_tests PRIVATE Catch2::Catch2WithMain Manager)

catch_discover_tests(manager_tests)
18 changes: 18 additions & 0 deletions CIValidations/UnitTests/manager_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "catch2/catch_test_macros.hpp"

#include "manager/YamlHelper.h"

TEST_CASE("OverrideConfig", "[Yamlhelper]") {
YAML::Node lineup = YAML::Load("{1B: Prince Fielder, 2B: Rickie Weeks, LF: Ryan Braun}");

REQUIRE(lineup.size() == 3);
REQUIRE(lineup["1B"].as<std::string>() == "Prince Fielder");

OverrideConfig(lineup, "1B", "Fielder formerly know as Prince");

REQUIRE(lineup["1B"].as<std::string>() == "Fielder formerly know as Prince");

OverrideConfig(lineup, "1B", 123);

REQUIRE(lineup["1B"].as<unsigned>() == 123u);
}
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ SET(MaCh3Tutorial_VERSION 1.2.0)
project(MaCh3Tutorial VERSION ${MaCh3Tutorial_VERSION} LANGUAGES CXX)
set(CMAKE_CXX_STANDARD_REQUIRED True)

option(MaCh3Tutorial_UNITTESTS_ENABLED "Whether to build MaCh3 Unit Tests" OFF)

# Changes default install path to be a subdirectory of the build dir.
# Can set build dir at configure time with -DCMAKE_INSTALL_PREFIX=/install/path
if(CMAKE_INSTALL_PREFIX STREQUAL "" OR CMAKE_INSTALL_PREFIX STREQUAL
Expand Down Expand Up @@ -91,6 +93,17 @@ else()
endif()
cmessage(STATUS "CMAKE CXX Standard: ${CMAKE_CXX_STANDARD}")

############################ Catch2/CTest ####################################
if(MaCh3Tutorial_UNITTESTS_ENABLED)
find_package(Catch2 QUIET)
if(NOT Catch2_FOUND)
CPMAddPackage("gh:catchorg/Catch2@3.5.2")
LIST(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
endif()

include(CTest)
include(Catch)
endif()
################################# Build MaCh3 ##################################
add_subdirectory(Utils)
add_subdirectory(CIValidations)
Expand Down