Commit 85bc1058 authored by Christof Metzger-Kraus's avatar Christof Metzger-Kraus

cleaning up Gauss distribution unit test; improve SilenceTest class to print output if tests fail

parent b8847846
......@@ -5,13 +5,13 @@
# It can be found at:
# http://amas.web.psi.ch/tools/GSL/index.html
#
# GTEST_INCLUDE_DIR - where to find gtest/gtest.h
# GTEST_INCLUDE_DIRS - where to find gtest/gtest.h
# GTEST_LIBRARY - libgtest.a path
# GTEST_MAIN_LIBRARY - libgtest_main.a path
# GTEST_FOUND - do not attempt to use if "no" or undefined.
FIND_PATH(GTEST_INCLUDE_DIR gtest/gtest.h
HINTS $ENV{GTEST_INCLUDE_PATH} $ENV{GTEST_INCLUDE_DIR} $ENV{GTEST_PREFIX}/include $ENV{GTEST_ROOT}/include ${PROJECT_SOURCE_DIR}/tests/tools/gtest/include
FIND_PATH(GTEST_INCLUDE_DIRS gtest/gtest.h
HINTS $ENV{GTEST_INCLUDE_PATH} $ENV{GTEST_INCLUDE_DIRS} $ENV{GTEST_PREFIX}/include $ENV{GTEST_ROOT}/include ${PROJECT_SOURCE_DIR}/tests/tools/gtest/include
PATHS ENV CPP_INCLUDE_PATH
)
......@@ -30,19 +30,17 @@ set( GTEST_BOTH_LIBRARIES
${GTEST_LIBRARY_MAIN}
)
IF(GTEST_INCLUDE_DIR AND GTEST_LIBRARY)
IF(GTEST_INCLUDE_DIRS AND GTEST_LIBRARY)
SET( GTEST_FOUND "YES" )
ENDIF(GTEST_INCLUDE_DIR AND GTEST_LIBRARY)
ENDIF(GTEST_INCLUDE_DIRS AND GTEST_LIBRARY)
IF (GTEST_FOUND)
IF (NOT GTEST_FIND_QUIETLY)
MESSAGE(STATUS "Found gtest libraries: ${GTEST_BOTH_LIBRARIES}")
MESSAGE(STATUS "Found gtest include dir: ${GTEST_INCLUDE_DIR}")
MESSAGE(STATUS "Found gtest include dir: ${GTEST_INCLUDE_DIRS}")
ENDIF (NOT GTEST_FIND_QUIETLY)
ELSE (GTEST_FOUND)
IF (GTest_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find GTEST!")
ENDIF (GTest_FIND_REQUIRED)
ENDIF (GTEST_FOUND)
ENDIF (GTEST_FOUND)
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -79,6 +79,154 @@ namespace InputMomentumUnitsT
};
}
namespace Attrib
{
namespace Distribution
{
enum AttributesT {
TYPE,
DISTRIBUTION,
FNAME,
WRITETOFILE,
WEIGHT,
INPUTMOUNITS,
EMITTED,
EMISSIONSTEPS,
EMISSIONMODEL,
EKIN,
ELASER,
W,
FE,
CATHTEMP,
NBIN,
XMULT,
YMULT,
ZMULT,
TMULT,
PXMULT,
PYMULT,
PZMULT,
OFFSETX,
OFFSETY,
OFFSETZ,
OFFSETT,
OFFSETPX,
OFFSETPY,
OFFSETPZ,
SIGMAX,
SIGMAY,
SIGMAR,
SIGMAZ,
SIGMAT,
TPULSEFWHM,
TRISE,
TFALL,
SIGMAPX,
SIGMAPY,
SIGMAPZ,
MX,
MY,
MZ,
MT,
CUTOFFX,
CUTOFFY,
CUTOFFR,
CUTOFFLONG,
CUTOFFPX,
CUTOFFPY,
CUTOFFPZ,
FTOSCAMPLITUDE,
FTOSCPERIODS,
R, // the correlation matrix (a la transport)
CORRX,
CORRY,
CORRZ,
CORRT,
R51,
R52,
R61,
R62,
LASERPROFFN,
IMAGENAME,
INTENSITYCUT,
FLIPX,
FLIPY,
ROTATE90,
ROTATE180,
ROTATE270,
NPDARKCUR,
INWARDMARGIN,
EINITHR,
FNA,
FNB,
FNY,
FNVYZERO,
FNVYSECOND,
FNPHIW,
FNBETA,
FNFIELDTHR,
FNMAXEMI,
SECONDARYFLAG,
NEMISSIONMODE,
VSEYZERO, // sey_0 in Vaughn's model.
VEZERO, // Energy related to sey_0 in Vaughan's model.
VSEYMAX, // sey max in Vaughan's model.
VEMAX, // Emax in Vaughan's model.
VKENERGY, // Fitting parameter denotes the roughness of
// surface for impact energy in Vaughn's model.
VKTHETA, // Fitting parameter denotes the roughness of
// surface for impact angle in Vaughn's model.
VVTHERMAL, // Thermal velocity of Maxwellian distribution
// of secondaries in Vaughan's model.
VW,
SURFMATERIAL, // Add material type, currently 0 for copper
// and 1 for stainless steel.
EX, // below is for the matched distribution
EY,
ET,
MAGSYM, // number of sector magnets
LINE,
FMAPFN,
FMTYPE, // field map type used in matched gauss distribution
RESIDUUM,
MAXSTEPSCO,
MAXSTEPSSI,
ORDERMAPS,
E2,
RGUESS,
ID1, // special particle that the user can set
ID2, // special particle that the user can set
SCALABLE,
SIZE
};
}
namespace Legacy
{
namespace Distribution
{
enum LegacyAttributesT {
// DESCRIPTION OF THE DISTRIBUTION:
DEBIN = Attrib::Distribution::SIZE,
SBIN,
SIGMAPT,
CUTOFF,
T,
PT,
// ALPHAX,
// ALPHAY,
// BETAX,
// BETAY,
// DX,
// DDX,
// DY,
// DDY,
SIZE
};
}
}
}
/*
* Class Distribution
*
......@@ -209,6 +357,8 @@ private:
#ifdef WITH_UNIT_TESTS
FRIEND_TEST(GaussTest, FullSigmaTest1);
FRIEND_TEST(GaussTest, FullSigmaTest2);
FRIEND_TEST(BinomialTest, FullSigmaTest1);
FRIEND_TEST(BinomialTest, FullSigmaTest2);
#endif
Distribution(const std::string &name, Distribution *parent);
......@@ -217,9 +367,7 @@ private:
Distribution(const Distribution &);
void operator=(const Distribution &);
// void printSigma(SigmaGenerator<double,unsigned int>::matrix_type& M, Inform& out);
void addDistributions();
void applyEmissionModel(double lowEnergyLimit, double &px, double &py, double &pz, std::vector<double> &additionalRNs);
void applyEmissModelAstra(double &px, double &py, double &pz, std::vector<double> &additionalRNs);
......
......@@ -41,7 +41,7 @@ LINK_DIRECTORIES( ${GTEST_LIB_DIR}
${Boost_LIBRARIES}
)
INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIR}
INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}
${IPPL_INCLUDE_DIR}
${H5Hut_INCLUDE_DIR}
......@@ -53,7 +53,7 @@ INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIR}
${Trilinos_TPL_INCLUDE_DIRS}
${IPPL_SOURCE_DIRS}
${CCSE_INCLUDE_DIRS}
${GTEST_INCLUDE_DIR}
${GTEST_INCLUDE_DIRS}
)
# Check to see if cmake finds the test files
......@@ -62,4 +62,4 @@ INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIR}
# Build the test exe. We don't do an install on the unit test exe as it is
# assumed that this is internal to opal
ADD_EXECUTABLE(${TEST_EXE} ${TEST_SRCS}) # the opal and classic sources are not needed again if we link agains libOPAL and libCLASSIC!
TARGET_LINK_LIBRARIES(${TEST_EXE} OPALib ${OPAL_LIBS} ${Trilinos_LIBRARIES} ${Trilinos_TPL_LIBRARIES} ${CCSE_LIBRARIES} -lgfortran ${OTHER_CMAKE_EXE_LINKER_FLAGS} ${GTEST_BOTH_LIBRARIES} -lpthread)
TARGET_LINK_LIBRARIES(${TEST_EXE} OPALib ${OPAL_LIBS} ${Trilinos_LIBRARIES} ${Trilinos_TPL_LIBRARIES} ${CCSE_LIBRARIES} -lgfortran ${OTHER_CMAKE_EXE_LINKER_FLAGS} ${GTEST_BOTH_LIBRARIES} -lpthread)
\ No newline at end of file
......@@ -4,7 +4,14 @@
#include "Utility/Inform.h" // ippl
extern Inform* gmsg;
Inform* gmsg;
class NewLineAdder: public ::testing::EmptyTestEventListener {
virtual void OnTestPartResult(const ::testing::TestPartResult &test_part_result) {
if (test_part_result.failed())
printf("\n");
}
};
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
......@@ -13,8 +20,13 @@ int main(int argc, char **argv) {
return 1;
}
MPI_Init(&argc, &argv);
::testing::TestEventListeners &listeners =
::testing::UnitTest::GetInstance()->listeners();
listeners.Append(new NewLineAdder);
int test_out = RUN_ALL_TESTS();
MPI_Finalize();
return test_out;
}
}
\ No newline at end of file
#include "gtest/gtest.h"
#include "Distribution/Distribution.h"
#include "Attributes/Attributes.h"
#include "Physics/Physics.h"
#include "opal_test_utilities/SilenceTest.h"
#include "gsl/gsl_statistics_double.h"
TEST(BinomialTest, FullSigmaTest1) {
OpalTestUtilities::SilenceTest silencer(true);
const double expectedR11 = 1.978;
const double expectedR22 = 0.7998;
const double expectedR33 = 2.498;
const double expectedR44 = 0.6212;
const double expectedR55 = 1.537;
const double expectedR66 = 0.9457;
const double expectedR21 = -0.40993;
const double expectedR43 = 0.77208;
const double expectedR65 = 0.12051;
const double expectedR51 = 0.14935;
const double expectedR52 = 0.59095;
const double expectedR61 = 0.72795;
const double expectedR62 = -0.3550;
std::vector<double> expectedR({expectedR21, 0, 0, expectedR51, expectedR61, \
/* */ 0, 0, expectedR52, expectedR62, \
/* */ expectedR43, 0, 0, \
/* */0, 0, \
/* */expectedR65});
Distribution dist;
Attributes::setString(dist.itsAttr[Attrib::Distribution::TYPE], "BINOMIAL");
Attributes::setReal(dist.itsAttr[Attrib::Distribution::SIGMAX], expectedR11 * 1e-3);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::SIGMAPX], expectedR22);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::SIGMAY], expectedR33 * 1e-3);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::SIGMAPY], expectedR44);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::SIGMAZ], expectedR55 * 1e-3);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::SIGMAPZ], expectedR66);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::MX], 999999999.9);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::MY], 999999999.9);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::MZ], 999999999.9);
Attributes::setRealArray(dist.itsAttr[Attrib::Distribution::R], expectedR);
Attributes::setBool(dist.itsAttr[Attrib::Distribution::EMITTED], false);
dist.setDistType();
dist.checkIfEmitted();
size_t numParticles = 1000000;
dist.create(numParticles, Physics::m_p);
double R11 = sqrt(gsl_stats_variance(&(dist.xDist_m[0]), 1, dist.xDist_m.size())) * 1e3;
double R22 = sqrt(gsl_stats_variance(&(dist.pxDist_m[0]), 1, dist.pxDist_m.size()));
double R33 = sqrt(gsl_stats_variance(&(dist.yDist_m[0]), 1, dist.yDist_m.size())) * 1e3;
double R44 = sqrt(gsl_stats_variance(&(dist.pyDist_m[0]), 1, dist.pyDist_m.size()));
double R55 = sqrt(gsl_stats_variance(&(dist.tOrZDist_m[0]), 1, dist.tOrZDist_m.size())) * 1e3;
double R66 = sqrt(gsl_stats_variance(&(dist.pzDist_m[0]), 1, dist.pzDist_m.size()));
double R21 = (gsl_stats_covariance(&(dist.xDist_m[0]), 1, &(dist.pxDist_m[0]), 1, dist.xDist_m.size()) * 1e3 /
(expectedR11 * expectedR22));
double R51 = (gsl_stats_covariance(&(dist.xDist_m[0]), 1, &(dist.tOrZDist_m[0]), 1, dist.xDist_m.size()) * 1e6 /
(expectedR11 * expectedR55));
double R52 = (gsl_stats_covariance(&(dist.pxDist_m[0]), 1, &(dist.tOrZDist_m[0]), 1, dist.pxDist_m.size()) * 1e3 /
(expectedR22 * expectedR55));
double R61 = (gsl_stats_covariance(&(dist.xDist_m[0]), 1, &(dist.pzDist_m[0]), 1, dist.xDist_m.size()) * 1e3 /
(expectedR11 * expectedR66));
double R62 = (gsl_stats_covariance(&(dist.pxDist_m[0]), 1, &(dist.pzDist_m[0]), 1, dist.pxDist_m.size()) /
(expectedR22 * expectedR66));
EXPECT_NEAR(expectedR11, R11, 0.05 * std::abs(expectedR11));
EXPECT_NEAR(expectedR22, R22, 0.05 * std::abs(expectedR22));
EXPECT_NEAR(expectedR33, R33, 0.05 * std::abs(expectedR33));
EXPECT_NEAR(expectedR44, R44, 0.05 * std::abs(expectedR44));
EXPECT_NEAR(expectedR55, R55, 0.05 * std::abs(expectedR55));
EXPECT_NEAR(expectedR66, R66, 0.05 * std::abs(expectedR66));
EXPECT_NEAR(expectedR21, R21, 0.1 * std::abs(expectedR21));
EXPECT_NEAR(expectedR51, R51, 0.1 * std::abs(expectedR51));
EXPECT_NEAR(expectedR52, R52, 0.1 * std::abs(expectedR52));
EXPECT_NEAR(expectedR61, R61, 0.1 * std::abs(expectedR61));
EXPECT_NEAR(expectedR62, R62, 0.1 * std::abs(expectedR62));
}
TEST(BinomialTest, FullSigmaTest2) {
OpalTestUtilities::SilenceTest silencer(true);
const double expectedR11 = 1.978;
const double expectedR22 = 0.7998;
const double expectedR33 = 2.498;
const double expectedR44 = 0.6212;
const double expectedR55 = 1.537;
const double expectedR66 = 0.9457;
const double expectedR21 = -0.40993;
const double expectedR43 = 0.77208;
const double expectedR65 = 0.12051;
const double expectedR51 = 0.14935;
const double expectedR52 = 0.59095;
const double expectedR61 = 0.72795;
const double expectedR62 = -0.3550;
Distribution dist;
Attributes::setString(dist.itsAttr[Attrib::Distribution::TYPE], "GAUSS");
Attributes::setReal(dist.itsAttr[Attrib::Distribution::SIGMAX], expectedR11 * 1e-3);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::SIGMAPX], expectedR22);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::SIGMAY], expectedR33 * 1e-3);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::SIGMAPY], expectedR44);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::SIGMAZ], expectedR55 * 1e-3);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::SIGMAPZ], expectedR66);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::CORRX], expectedR21);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::CORRY], expectedR43);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::CORRZ], expectedR65);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::R51], expectedR51);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::R61], expectedR61);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::R52], expectedR52);
Attributes::setReal(dist.itsAttr[Attrib::Distribution::R62], expectedR62);
Attributes::setBool(dist.itsAttr[Attrib::Distribution::EMITTED], false);
dist.setDistType();
dist.checkIfEmitted();
size_t numParticles = 1000000;
dist.create(numParticles, Physics::m_p);
double R11 = sqrt(gsl_stats_variance(&(dist.xDist_m[0]), 1, dist.xDist_m.size())) * 1e3;
double R22 = sqrt(gsl_stats_variance(&(dist.pxDist_m[0]), 1, dist.pxDist_m.size()));
double R33 = sqrt(gsl_stats_variance(&(dist.yDist_m[0]), 1, dist.yDist_m.size())) * 1e3;
double R44 = sqrt(gsl_stats_variance(&(dist.pyDist_m[0]), 1, dist.pyDist_m.size()));
double R55 = sqrt(gsl_stats_variance(&(dist.tOrZDist_m[0]), 1, dist.tOrZDist_m.size())) * 1e3;
double R66 = sqrt(gsl_stats_variance(&(dist.pzDist_m[0]), 1, dist.pzDist_m.size()));
double R21 = (gsl_stats_covariance(&(dist.xDist_m[0]), 1, &(dist.pxDist_m[0]), 1, dist.xDist_m.size()) * 1e3 /
(expectedR11 * expectedR22));
double R51 = (gsl_stats_covariance(&(dist.xDist_m[0]), 1, &(dist.tOrZDist_m[0]), 1, dist.xDist_m.size()) * 1e6 /
(expectedR11 * expectedR55));
double R52 = (gsl_stats_covariance(&(dist.pxDist_m[0]), 1, &(dist.tOrZDist_m[0]), 1, dist.pxDist_m.size()) * 1e3 /
(expectedR22 * expectedR55));
double R61 = (gsl_stats_covariance(&(dist.xDist_m[0]), 1, &(dist.pzDist_m[0]), 1, dist.xDist_m.size()) * 1e3 /
(expectedR11 * expectedR66));
double R62 = (gsl_stats_covariance(&(dist.pxDist_m[0]), 1, &(dist.pzDist_m[0]), 1, dist.pxDist_m.size()) /
(expectedR22 * expectedR66));
EXPECT_NEAR(expectedR11, R11, 0.05 * std::abs(expectedR11));
EXPECT_NEAR(expectedR22, R22, 0.05 * std::abs(expectedR22));
EXPECT_NEAR(expectedR33, R33, 0.05 * std::abs(expectedR33));
EXPECT_NEAR(expectedR44, R44, 0.05 * std::abs(expectedR44));
EXPECT_NEAR(expectedR55, R55, 0.05 * std::abs(expectedR55));
EXPECT_NEAR(expectedR66, R66, 0.05 * std::abs(expectedR66));
EXPECT_NEAR(expectedR21, R21, 0.1 * std::abs(expectedR21));
EXPECT_NEAR(expectedR51, R51, 0.1 * std::abs(expectedR51));
EXPECT_NEAR(expectedR52, R52, 0.1 * std::abs(expectedR52));
EXPECT_NEAR(expectedR61, R61, 0.1 * std::abs(expectedR61));
EXPECT_NEAR(expectedR62, R62, 0.1 * std::abs(expectedR62));
}
\ No newline at end of file
set (_SRCS
BinomialTest.cpp
GaussTest.cpp
)
......
This diff is collapsed.
#include "SilenceTest.h"
#include "opal_test_utilities/SilenceTest.h"
std::streambuf *OpalTestUtilities::SilenceTest::_defaultCout = NULL;
std::streambuf *OpalTestUtilities::SilenceTest::_defaultCerr = NULL;
\ No newline at end of file
std::streambuf *OpalTestUtilities::SilenceTest::_defaultCerr = NULL;
OpalTestUtilities::SilenceTest::SilenceTest(bool willSilence):
_failed(false) {
if (willSilence && _defaultCout == NULL ) {
_defaultCout = std::cout.rdbuf();
_defaultCerr = std::cerr.rdbuf();
std::cout.rdbuf(_debugOutput.rdbuf());
std::cerr.rdbuf(_debugOutput.rdbuf());
::testing::TestEventListeners& listeners =
::testing::UnitTest::GetInstance()->listeners();
_failureTest = new FailureTester(this);
listeners.Append(_failureTest);
}
}
OpalTestUtilities::SilenceTest::~SilenceTest() { // return buffer to normal on delete
if (_defaultCout != NULL) {
std::cout.rdbuf(_defaultCout);
std::cerr.rdbuf(_defaultCerr);
_defaultCout = NULL;
_defaultCerr = NULL;
::testing::TestEventListeners& listeners =
::testing::UnitTest::GetInstance()->listeners();
listeners.Release(_failureTest);
delete _failureTest;
if (_failed) {
std::cerr << _debugOutput.str() << std::endl;
}
}
}
void OpalTestUtilities::SilenceTest::setFailed() {
_failed = true;
}
\ No newline at end of file
......@@ -29,39 +29,51 @@
#include <sstream>
#include <iostream>
#include "gtest/gtest.h"
namespace OpalTestUtilities {
/** Shutup test output
*
* If more than one is called, will shutup output on any alloc if it is loud
* and will make loud on any dealloc if it is quiet.
*/
class SilenceTest {
public:
SilenceTest(bool willSilence) {
if (willSilence && _defaultCout == NULL ) {
_defaultCout = std::cout.rdbuf(_debugOutput.rdbuf());
_defaultCerr = std::cerr.rdbuf(_debugOutput.rdbuf());
}
}
/** Shutup test output
*
* If more than one is called, will shutup output on any alloc if it is loud
* and will make loud on any dealloc if it is quiet.
*/
class FailureTester;
~SilenceTest() { // return buffer to normal on delete
if (_defaultCout != NULL) {
std::cout.rdbuf(_defaultCout);
std::cerr.rdbuf(_defaultCerr);
_defaultCout = NULL;
_defaultCerr = NULL;
}
}
class SilenceTest {
public:
SilenceTest(bool willSilence);
~SilenceTest();
void setFailed();
private:
SilenceTest(); // disable default ctor
SilenceTest(const SilenceTest& test); // disable default copy ctor
private:
SilenceTest(); // disable default ctor
SilenceTest(const SilenceTest& test); // disable default copy ctor
std::ostringstream _debugOutput;
static std::streambuf *_defaultCout;
static std::streambuf *_defaultCerr;
bool _failed;
FailureTester *_failureTest;
};
class FailureTester: public ::testing::EmptyTestEventListener {
public:
FailureTester(SilenceTest *st):
EmptyTestEventListener(),
_test(st) { }
~FailureTester() { }
private:
FailureTester();
virtual void OnTestPartResult(const ::testing::TestPartResult &test_part_result) {
if (test_part_result.failed())
_test->setFailed();
}
std::ostringstream _debugOutput;
static std::streambuf *_defaultCout;
static std::streambuf *_defaultCerr;
};
SilenceTest *_test;
};
}
#endif //OPALTESTUTILITIES_SILENCETEST_H_
#endif //OPALTESTUTILITIES_SILENCETEST_H_
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment