diff --git a/CMakeLists.txt b/CMakeLists.txt index 78b48c86e4e8c3ba85f0f2a5cba8e812bc7badd7..b15db11f2e9b4193118e724f9c61d12a2d7316b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,9 +58,14 @@ FIND_PACKAGE (HDF5 REQUIRED) FIND_PACKAGE (GSL REQUIRED) set (BOOSTROOT $ENV{BOOST_DIR}) -find_package (Boost 1.55.0 REQUIRED) +set(Boost_USE_STATIC_LIBS ON) +set(Boost_USE_MULTITHREADED OFF) +set(Boost_USE_STATIC_RUNTIME OFF) +find_package (Boost 1.55.0 COMPONENTS regex filesystem system) if (Boost_INCLUDE_DIRS) message (STATUS "Found boost include dir: ${Boost_INCLUDE_DIR}") + message (STATUS "Found boost library dir: ${Boost_LIBRARY_DIR}") + message (STATUS "Found boost libraries: ${Boost_LIBRARIES}") include_directories (${Boost_INCLUDE_DIRS}) endif () diff --git a/src/Distribution/Distribution.h b/src/Distribution/Distribution.h index 682c2119ab7c7b980f088ada1ff9dd7403284f07..1ad0de7cb76dd04a104a7b04521baa5523670e63 100644 --- a/src/Distribution/Distribution.h +++ b/src/Distribution/Distribution.h @@ -199,7 +199,8 @@ public: void setNumberOfDistributions(unsigned int n) { numberOfDistributions_m = n; } private: #ifdef WITH_UNIT_TESTS - FRIEND_TEST(GaussTest, FullSigmaTest); + FRIEND_TEST(GaussTest, FullSigmaTest1); + FRIEND_TEST(GaussTest, FullSigmaTest2); #endif Distribution(const std::string &name, Distribution *parent); @@ -448,4 +449,4 @@ inline Inform &operator<<(Inform &os, const Distribution &d) { return d.printInfo(os); } -#endif // OPAL_Distribution_HH +#endif // OPAL_Distribution_HH \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7614d72118c27fc56acfbb5a6e26eebbcb578f1e..41c109fd82774fcb882cbcf32ee99bd294118e46 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -36,6 +36,7 @@ LINK_DIRECTORIES( ${GTEST_LIB_DIR} ${Trilinos_LIBRARY_DIRS} ${Trilinos_TPL_LIBRARY_DIRS} ${CCSE_LIBRARY_DIR} + ${Boost_LIBRARIES} ) INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIR} diff --git a/tests/Main.cpp b/tests/Main.cpp index fc0b08acf54cb1c6938d5ae2071635c351cf2f6e..b265ef6ac70f01b26b627c0dc8f8dd97b29390e7 100644 --- a/tests/Main.cpp +++ b/tests/Main.cpp @@ -1,8 +1,12 @@ #include "gtest/gtest.h" +#include "mpi.h" + int main(int argc, char **argv) { - ::testing::InitGoogleTest(&argc, argv); - int test_out = RUN_ALL_TESTS(); - return test_out; -} + ::testing::InitGoogleTest(&argc, argv); + MPI_Init(&argc, &argv); + int test_out = RUN_ALL_TESTS(); + MPI_Finalize(); + return test_out; +} \ No newline at end of file diff --git a/tests/opal_src/Distribution/GaussTest.cpp b/tests/opal_src/Distribution/GaussTest.cpp index 46a627e18135250e1ee7baf054b587c20589eba6..907c22dc4e51514ed21362f9b42808a3d4a606aa 100644 --- a/tests/opal_src/Distribution/GaussTest.cpp +++ b/tests/opal_src/Distribution/GaussTest.cpp @@ -16,45 +16,159 @@ Inform *gmsg; #include "gsl/gsl_statistics_double.h" +#include <boost/filesystem.hpp> + #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) { +TEST(GaussTest, FullSigmaTest1) { char inputFileName[] = "GaussDistributionTest.in"; + std::string input = "OPTION, ECHO=TRUE;\n" + "OPTION, CZERO=FALSE;\n" + "TITLE, STRING=\"gauss distribution unit test\";\n" + "DIST1: DISTRIBUTION, DISTRIBUTION = \"GAUSS\", \n" + "SIGMAX = 1.978e-3, SIGMAY = 2.498e-3, SIGMAZ = 1.537e-3, \n" + "SIGMAPX = 0.7998, SIGMAPY = 0.6212, SIGMAPZ = 0.9457, \n" + "R = {-0.40993, 0, 0, 0.14935, 0.72795, 0, 0, 0.59095, -0.3550, 0.77208, 0, 0, 0, 0, 0.12051}, \n" + "EKIN = 0.63, \n" + "EMITTED = FALSE;\n"; + + int narg = 7; + char exe_name[] = "opal_unit_tests"; + char commlib[] = "--nocomminit"; + char info[] = "--info"; + char info0[] = "0"; + char warn[] = "--warn"; + char warn0[] = "0"; + + char **arg = new char*[7]; + arg[0] = exe_name; + arg[1] = inputFileName; + arg[2] = commlib; + arg[3] = info; + arg[4] = info0; + arg[5] = warn; + arg[6] = warn0; + + if (!ippl) + 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"); + } + } - int narg = 8; + 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()); + + const double expectedR11 = 3.914; + const double expectedR21 = -0.6486; + const double expectedR22 = 0.6396; + const double expectedR51 = 0.4542; + const double expectedR52 = 0.7265; + const double expectedR61 = 1.362; + const double expectedR62 = -0.2685; + + EXPECT_LT(std::abs(expectedR11 - R11), 0.03 * expectedR11); + EXPECT_LT(std::abs(expectedR21 - R21), -0.03 * expectedR21); + EXPECT_LT(std::abs(expectedR22 - R22), 0.03 * expectedR22); + EXPECT_LT(std::abs(expectedR51 - R51), 0.03 * expectedR51); + EXPECT_LT(std::abs(expectedR52 - R52), 0.03 * expectedR52); + EXPECT_LT(std::abs(expectedR61 - R61), 0.03 * expectedR61); + EXPECT_LT(std::abs(expectedR62 - R62), -0.03 * expectedR62); + } + + OpalData::deleteInstance(); + delete parser; + delete gmsg; + // delete ippl; + delete[] arg; + + boost::filesystem::remove(inputFileName); +} + +TEST(GaussTest, FullSigmaTest2) { + + char inputFileName[] = "GaussDistributionTest.in"; + std::string input = "OPTION, ECHO=TRUE;\n" + "OPTION, CZERO=FALSE;\n" + "TITLE, STRING=\"gauss distribution unit test\";\n" + "DIST1: DISTRIBUTION, DISTRIBUTION = \"GAUSS\", \n" + "SIGMAX = 1.978e-3, SIGMAY = 2.498e-3, SIGMAZ = 1.537e-3, \n" + "SIGMAPX = 0.7998, SIGMAPY = 0.6212, SIGMAPZ = 0.9457, \n" + "CORRX= -0.40993, CORRY=0.77208, CORRZ=0.12051, \n" + "R51=0.14935, R52=0.59095, R61=0.72795, R62=-0.3550}, \n" + "EKIN = 0.63, \n" + "EMITTED = FALSE;\n"; + + + int narg = 7; char exe_name[] = "opal_unit_tests"; - char commlib[] = "--commlib"; - char mpicomm[] = "mpi"; + char commlib[] = "--nocomminit"; char info[] = "--info"; char info0[] = "0"; char warn[] = "--warn"; char warn0[] = "0"; - char **arg = new char*[8]; + char **arg = new char*[7]; 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; + arg[3] = info; + arg[4] = info0; + arg[5] = warn; + arg[6] = warn0; - ippl = new Ippl(narg, arg, Ippl::KEEP, MPI_COMM_WORLD); + if (!ippl) + ippl = new Ippl(narg, arg, Ippl::KEEP, MPI_COMM_WORLD); gmsg = new Inform("OPAL "); std::ofstream inputFile(inputFileName); @@ -109,23 +223,28 @@ TEST(GaussTest, FullSigmaTest) { 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(); + const double expectedR11 = 3.914; + const double expectedR21 = -0.6486; + const double expectedR22 = 0.6396; + const double expectedR51 = 0.4542; + const double expectedR52 = 0.7265; + const double expectedR61 = 1.362; + const double expectedR62 = -0.2685; + + EXPECT_LT(std::abs(expectedR11 - R11), 0.03 * expectedR11); + EXPECT_LT(std::abs(expectedR21 - R21), -0.03 * expectedR21); + EXPECT_LT(std::abs(expectedR22 - R22), 0.03 * expectedR22); + EXPECT_LT(std::abs(expectedR51 - R51), 0.03 * expectedR51); + EXPECT_LT(std::abs(expectedR52 - R52), 0.03 * expectedR52); + EXPECT_LT(std::abs(expectedR61 - R61), 0.03 * expectedR61); + EXPECT_LT(std::abs(expectedR62 - R62), -0.03 * expectedR62); } OpalData::deleteInstance(); delete parser; delete gmsg; - delete ippl; + // delete ippl; delete[] arg; + + boost::filesystem::remove(inputFileName); } \ No newline at end of file