From 1c9af6e74a243cc82561f5124741e016244f0275 Mon Sep 17 00:00:00 2001 From: Christof Metzger-Kraus <christof.j.kraus@gmail.com> Date: Tue, 21 Apr 2015 11:06:50 +0200 Subject: [PATCH] unfinished unit test for gauss distribution --- src/Distribution/Distribution.h | 6 + tests/CMakeLists.txt | 2 +- tests/opal_src/CMakeLists.txt | 1 + tests/opal_src/Distribution/CMakeLists.txt | 9 ++ tests/opal_src/Distribution/GaussTest.cpp | 131 +++++++++++++++++++++ 5 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 tests/opal_src/Distribution/CMakeLists.txt create mode 100644 tests/opal_src/Distribution/GaussTest.cpp diff --git a/src/Distribution/Distribution.h b/src/Distribution/Distribution.h index 66ef84ca6..70f7809bc 100644 --- a/src/Distribution/Distribution.h +++ b/src/Distribution/Distribution.h @@ -37,6 +37,9 @@ #include <gsl/gsl_histogram.h> #include <gsl/gsl_qrng.h> +#ifdef WITH_UNIT_TESTS +#include <gtest/gtest_prod.h> +#endif class Beam; class PartBunch; @@ -195,6 +198,9 @@ public: void setNumberOfDistributions(unsigned int n) { numberOfDistributions_m = n; } private: +#ifdef WITH_UNIT_TESTS + FRIEND_TEST(GaussTest, FullSigmaTest); +#endif Distribution(const std::string &name, Distribution *parent); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3c58dc000..7614d7211 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,7 +4,7 @@ set (TEST_EXE opal_unit_tests) set (SRC_DIRS classic_src opal_src) set (CMAKE_CXX_FLAGS - "${OPAL_CXX_FLAGS}" + "${OPAL_CXX_FLAGS} -DWITH_UNIT_TESTS" ) set (TEST_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/tests/opal_src/CMakeLists.txt b/tests/opal_src/CMakeLists.txt index 8b33deaae..3b1974afe 100644 --- a/tests/opal_src/CMakeLists.txt +++ b/tests/opal_src/CMakeLists.txt @@ -1,4 +1,5 @@ add_subdirectory (Elements) add_subdirectory (Utilities) +add_subdirectory (Distribution) set (TEST_SRCS_LOCAL ${TEST_SRCS_LOCAL} PARENT_SCOPE) \ No newline at end of file diff --git a/tests/opal_src/Distribution/CMakeLists.txt b/tests/opal_src/Distribution/CMakeLists.txt new file mode 100644 index 000000000..207afad7f --- /dev/null +++ b/tests/opal_src/Distribution/CMakeLists.txt @@ -0,0 +1,9 @@ +set (_SRCS + GaussTest.cpp +) + +include_directories ( + ${CMAKE_CURRENT_SOURCE_DIR} +) + +add_sources(${_SRCS}) \ No newline at end of file diff --git a/tests/opal_src/Distribution/GaussTest.cpp b/tests/opal_src/Distribution/GaussTest.cpp new file mode 100644 index 000000000..46a627e18 --- /dev/null +++ b/tests/opal_src/Distribution/GaussTest.cpp @@ -0,0 +1,131 @@ +#include "gtest/gtest.h" + +#include "opal.h" + +Ippl *ippl; +Inform *gmsg; + +#include "AbstractObjects/OpalData.h" +#include "OpalConfigure/Configure.h" +#include "Utilities/OpalException.h" +#include "Distribution/Distribution.h" +#include "OpalParser/OpalParser.h" + +#include "Parser/FileStream.h" +#include "Physics/Physics.h" + +#include "gsl/gsl_statistics_double.h" + +#include <iostream> +#include <fstream> +#include <string> +#include <cstring> + +std::string input = "OPTION, ECHO=TRUE;\n" + "OPTION, CZERO=FALSE;\n" + "TITLE, STRING=\"gauss distribution unit test\";\n" + "DIST1: DISTRIBUTION, DISTRIBUTION = \"GAUSS\", \n" + "SIGMAX = 3.914e-6, SIGMAY = 6.239e-6, SIGMAZ = 2.363e-6, \n" + "SIGMAPX = 0.6396, SIGMAPY = 0.3859, SIGMAPZ = 0.8944, \n" + "R = {-0.6486e-3, 0, 0, 0.4542e-6, 1.362e-3, 0, 0, 0.7265e-3, -0.2685, 1.198e-3, 0, 0, 0, 0, 0.1752e-3}, \n" + "EKIN = 0.63, \n" + "EMITTED = FALSE;\n"; + +TEST(GaussTest, FullSigmaTest) { + + char inputFileName[] = "GaussDistributionTest.in"; + + int narg = 8; + char exe_name[] = "opal_unit_tests"; + char commlib[] = "--commlib"; + char mpicomm[] = "mpi"; + char info[] = "--info"; + char info0[] = "0"; + char warn[] = "--warn"; + char warn0[] = "0"; + + char **arg = new char*[8]; + arg[0] = exe_name; + arg[1] = inputFileName; + arg[2] = commlib; + arg[3] = mpicomm; + arg[4] = info; + arg[5] = info0; + arg[6] = warn; + arg[7] = warn0; + + ippl = new Ippl(narg, arg, Ippl::KEEP, MPI_COMM_WORLD); + gmsg = new Inform("OPAL "); + + std::ofstream inputFile(inputFileName); + inputFile << input << std::endl; + inputFile.close(); + + OpalData *OPAL = OpalData::getInstance(); + Configure::configure(); + OPAL->storeInputFn(inputFileName); + + FileStream *is = 0; + try { + is = new FileStream(inputFileName); + } catch(...) { + is = 0; + throw new OpalException("FullSigmaTest", "Could not read string"); + } + + OpalParser *parser = new OpalParser(); + if (is) { + try { + parser->run(is); + } catch (...) { + throw new OpalException("FullSigmaTest", "Could not parse input"); + } + } + + Object *distObj; + try { + distObj = OPAL->find("DIST1"); + } catch(...) { + distObj = 0; + throw new OpalException("FullSigmaTest", "Could not find distribution"); + } + + if (distObj) { + Distribution *dist = dynamic_cast<Distribution*>(distObj); + + dist->SetDistType(); + dist->CheckIfEmitted(); + + size_t numParticles = 1000000; + dist->Create(numParticles, Physics::m_p); + + + double R11 = gsl_stats_variance(&(dist->xDist_m[0]), 1, dist->xDist_m.size()) * 1e6; + double R21 = gsl_stats_covariance(&(dist->xDist_m[0]), 1, &(dist->pxDist_m[0]), 1, dist->xDist_m.size()) * 1e3; + double R22 = gsl_stats_variance(&(dist->pxDist_m[0]), 1, dist->pxDist_m.size()); + + double R51 = gsl_stats_covariance(&(dist->xDist_m[0]), 1, &(dist->tOrZDist_m[0]), 1, dist->xDist_m.size()) * 1e6; + double R52 = gsl_stats_covariance(&(dist->pxDist_m[0]), 1, &(dist->tOrZDist_m[0]), 1, dist->pxDist_m.size()) * 1e3; + double R61 = gsl_stats_covariance(&(dist->xDist_m[0]), 1, &(dist->pzDist_m[0]), 1, dist->xDist_m.size()) * 1e3; + double R62 = gsl_stats_covariance(&(dist->pxDist_m[0]), 1, &(dist->pzDist_m[0]), 1, dist->pxDist_m.size()); + + std::cout << std::setprecision(4) + << std::setw(11) << R11 + << std::setw(11) << R21 + << std::setw(11) << R22 + << std::endl + << std::setw(11) << R51 + << std::setw(11) << R52 + << std::setw(11) << R61 + << std::setw(11) << R62 + << std::endl; + + //EXPECT_NEAR(); + } + + OpalData::deleteInstance(); + delete parser; + delete gmsg; + delete ippl; + delete[] arg; +} \ No newline at end of file -- GitLab