#include #include #include "gtest/gtest.h" #include "CLHEP/Matrix/Matrix.h" #include "Config/ModuleConverter.hh" #include "BeamTools/BTFieldConstructor.hh" #include "BeamTools/BTTracker.hh" #include "Utils/Squeak.hh" #include "Interface/Differentiator.hh" #include "Maths/Matrix.hh" #include "Maths/PolynomialMap.hh" #include "Maths/Vector.hh" /// NOTE: tests MAUS::PolynomialMap, Differentiator and PolynomialInterpolator classes /// /// Three classes that are very closely related anyway... /// bool DifferentiatorTest(); bool PolynomialInterpolatorTest(); bool TestF(PolynomialInterpolator* pInt, double tolerance); bool DifferentiatorTest() { bool testpass = true; CLHEP::HepMatrix coeffHep(3,6); for(unsigned int i=0; i<6; i++) coeffHep[0][i] = i+1; for(unsigned int i=0; i<6; i++) coeffHep[1][i] = i*i; for(unsigned int i=0; i<6; i++) coeffHep[2][i] = -int(i)-1; MAUS::Squeak::mout(MAUS::Squeak::debug) << coeffHep << std::endl; MAUS::PolynomialMap* vecP = new MAUS::PolynomialMap(2, MAUS::Matrix(coeffHep)); std::vector delta(2, 0.1); std::vector mag (3, 1); Differentiator* diff = new Differentiator(vecP, delta, mag); Differentiator* clone = diff->Clone(); delete diff; MAUS::Squeak::mout(MAUS::Squeak::debug) << "Diff, Clone ~Diff: " << true << std::endl; testpass &= clone->FunctionMap() == vecP; MAUS::Squeak::mout(MAUS::Squeak::debug) << "FunctionMap: " << testpass << std::endl; testpass &= vecP->PointDimension() == clone->PointDimension(); MAUS::Squeak::mout(MAUS::Squeak::debug) << "PointDimension: " << testpass << std::endl; testpass &= clone->NumberOfDiffRows() == MAUS::PolynomialMap::NumberOfPolynomialCoefficients( delta.size(), mag.size()-1); MAUS::Squeak::mout(MAUS::Squeak::debug) << "NumberOfDiffRows: " << testpass << std::endl; testpass &= clone->ValueDimension() == clone->NumberOfDiffRows()*clone->FunctionMap()->ValueDimension(); MAUS::Squeak::mout(MAUS::Squeak::debug) << "ValueDim: " << testpass << std::endl; double point [2] = {3,-2}; double value [3] = {0,0,0}; double value2[3] = {0,0,0}; ///Y clone->Y(point, value); testpass &= fabs(value[0] - 31)<1e-9 && (value[1]- 80)<1e-9 && (value[2]- (-31))<1e-9; MAUS::Squeak::mout(MAUS::Squeak::debug) << "Y: " << value[0] << " " << value[1] << " " << value[2] << " * " << testpass << std::endl; ///PolyFromDiffs MAUS::PolynomialMap* vecP2 = clone->PolynomialFromDifferentials(point); vecP2->F(point, value2); testpass &= fabs(value[0] - value2[0]) < 1e-5 && fabs(value[1] - value2[1]) < 1e-5 && fabs(value[2] - value2[2]) < 1e-5; MAUS::Squeak::mout(MAUS::Squeak::debug) << "PolyFromDiffs: " << value2[0] << " " << value2[1] << " " << value2[2] << " * " << testpass << std::endl; ///PolynomialMap MAUS::Vector pointHep(2,0.); pointHep(1) = 3; pointHep(2) = -2; MAUS::Matrix valueHep = clone->PolynomialMap(pointHep); for(int i=0; iPolynomialMap(pointHep); for(int i=0; iCentredPolynomialMap(pointHep); for(int i=0; iCentredPolynomialMap(pointHep); MAUS::Squeak::mout(MAUS::Squeak::debug) << valueHep << "CentredPolyMap2: " << testpass << std::endl; ///F std::vector valueVec(clone->ValueDimension(), 0); clone->F(pointHep, valueHep); clone->F(point, &valueVec[0]); int index = -1; for(size_t i=0; i(coeffHep) ); Function* vecF = new Function(TrackingFunction, 2, 2); int size [4] = {9, 9, 12, 16}; double min [4] = {-200, -200, -0.2, -0.1}; double spacing[4] = {5, 5, 0., 0.3}; NDGrid* grid = new NDGrid(2, size, spacing, min); unsigned int diffOrder = 1; unsigned int pointOrder = 0; std::vector delta (4,0.1); std::vector mag (4,1); PolynomialInterpolator* pInt = new PolynomialInterpolator(grid, vecF, diffOrder, pointOrder, delta, mag); PolynomialInterpolator* clone = pInt->Clone(); delete pInt; pInt = clone; MAUS::Squeak::mout(MAUS::Squeak::debug) << "Ctor etc: " << true << " " << std::flush; bool sizepass = pInt->PointDimension() == vecF->PointDimension() && pInt->ValueDimension() == vecF->ValueDimension() && pInt->PolynomialOrder() == diffOrder+pointOrder && pInt->DifferentialOrder() == diffOrder && pInt->PointOrder() == pointOrder; sizepass &= pInt->NumberOfIndices() == MAUS::PolynomialMap::NumberOfPolynomialCoefficients( vecP->PointDimension(), diffOrder+pointOrder); sizepass &= pInt->NumberOfPoints() == ceil(pInt->NumberOfIndices()/pInt->NumberOfDiffIndices()); sizepass &= pInt->NumberOfDiffIndices() == MAUS::PolynomialMap::NumberOfPolynomialCoefficients( vecP->PointDimension(), diffOrder); sizepass &= pInt->GetMesh() == grid; MAUS::Squeak::mout(MAUS::Squeak::debug) << "Bureaucracy: " << sizepass << " " << std::flush; TestF(pInt, 1e-5); delete pInt; if(!sizepass) MAUS::Squeak::mout(MAUS::Squeak::debug) << "FAILED PolynomialInterpolatorTest" << std::endl; return sizepass; } bool TestF(PolynomialInterpolator* pInt, double tolerance) { bool funcpass = true; Mesh* grid = pInt->GetMesh(); VectorMap* vecP = pInt->Function(); std::vector spacing(pInt->PointDimension()); std::vector index1 (pInt->PointDimension(), 1); std::vector index2 (pInt->PointDimension(), 1); for(unsigned int i=0; i point(2,0); std::vector point2(2,1); std::vector value(3,0); Mesh::Iterator it = grid->Nearest(&point[0]); double tol=1e-2; for(Mesh::Iterator it = grid->Begin(); itEnd(); it++) { MAUS::Squeak::mout(MAUS::Squeak::debug) << std::endl << it << std::endl; point = it.Position(); point[0] -= spacing[0]/8.; point[1] -= spacing[1]/8.; vecP->F(&point[0], valueVecP); pInt->F(&point[0], valuePInt); MAUS::Squeak::mout(MAUS::Squeak::debug) << point[0] << " " << point[1] << " ## "; for(unsigned int i=0; iValueDimension(); i++) { bool pass = fabs(valuePInt[i] -valueVecP[i])/2./fabs(valuePInt[i] +valueVecP[i]) < tol; if(!pass) MAUS::Squeak::mout(MAUS::Squeak::debug) << "FAIL " << valuePInt[i] << " " << valueVecP[i] << " " << fabs(valuePInt[i] -valueVecP[i])/2./fabs(valuePInt[i] +valueVecP[i]) << "\n"; funcpass &= pass; } MAUS::Squeak::mout(MAUS::Squeak::debug) << std::endl; point[0] += spacing[0]/4.; vecP->F(&point[0], valueVecP); pInt->F(&point[0], valuePInt); MAUS::Squeak::mout(MAUS::Squeak::debug) << point[0] << " " << point[1] << " ## "; for(unsigned int i=0; iValueDimension(); i++) { bool pass = fabs(valuePInt[i] -valueVecP[i])/2./fabs(valuePInt[i] +valueVecP[i]) < tol; if(!pass) MAUS::Squeak::mout(MAUS::Squeak::debug) << valuePInt[i] << " " << valueVecP[i] << " " << fabs(valuePInt[i] -valueVecP[i])/2./fabs(valuePInt[i] +valueVecP[i]) << " && "; funcpass &= pass; } MAUS::Squeak::mout(MAUS::Squeak::debug) << std::endl; point[1] += spacing[1]/4.; vecP->F(&point[0], valueVecP); pInt->F(&point[0], valuePInt); MAUS::Squeak::mout(MAUS::Squeak::debug) << point[0] << " " << point[1] << " ## "; for(unsigned int i=0; iValueDimension(); i++) { bool pass = fabs(valuePInt[i] -valueVecP[i])/2./fabs(valuePInt[i] +valueVecP[i]) < tol; if(!pass) MAUS::Squeak::mout(MAUS::Squeak::debug) << valuePInt[i] << " " << valueVecP[i] << " " << fabs(valuePInt[i] -valueVecP[i])/2./fabs(valuePInt[i] +valueVecP[i]) << " && "; funcpass &= pass; } MAUS::Squeak::mout(MAUS::Squeak::debug) << std::endl; point[0] -= spacing[0]/4.; vecP->F(&point[0], valueVecP); pInt->F(&point[0], valuePInt); MAUS::Squeak::mout(MAUS::Squeak::debug) << point[0] << " " << point[1] << " ## "; for(unsigned int i=0; iValueDimension(); i++) { bool pass = fabs(valuePInt[i] -valueVecP[i])/2./fabs(valuePInt[i] +valueVecP[i]) < tol; if(!pass) MAUS::Squeak::mout(MAUS::Squeak::debug) << valuePInt[i] << " " << valueVecP[i] << " " << fabs(valuePInt[i] -valueVecP[i])/2./fabs(valuePInt[i] +valueVecP[i]) << " && "; funcpass &= pass; } MAUS::Squeak::mout(MAUS::Squeak::debug) << "\n@@ " << funcpass << std::endl; } return funcpass; } TEST(DifferentiatorTest, old_unit_tests) { EXPECT_TRUE( DifferentiatorTest()); }