CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/MagneticField/Interpolation/src/gridTesters.cc

Go to the documentation of this file.
00001 #include "Grid1D.h"
00002 #include "Grid3D.h"
00003 #include <cassert>
00004 
00005 namespace {
00006   bool testGrid1D( Grid1D const & grid)  {
00007     
00008     bool ok=true;
00009     
00010     Grid1D::Scalar f;
00011     int i = grid.index(7.2,f); 
00012     ok &= grid.inRange(i);
00013     
00014     ok &=  (8==i);
00015     ok &= (0.6==f);
00016     
00017     return ok;
00018   }
00019 }
00020 
00021 
00022 #include<iostream>
00023 #include<cstdlib>
00024 #include<cstdio>
00025 
00026 namespace {
00027   void print(Grid1D grid,  Grid1D::Scalar a) {
00028     Grid1D::Scalar f;
00029     int i = grid.index(a,f); 
00030     ::printf("%i %f %a\n",i,f,f);
00031     grid.normalize(i,f);
00032     ::printf("%i %f %a\n",i,f,f);
00033   }
00034 }
00035 
00036 int grid1d_t() {
00037 
00038   bool ok=true;
00039   Grid1D grid(-10.,10.,11);
00040 
00041   print(grid, 7.2);
00042   print(grid, 10.);
00043   print(grid, -10.2);
00044   print(grid, 10.2);
00045 
00046   ok &= testGrid1D(grid);
00047 
00048   assert(ok? 0 : 1);
00049   return ok ? 0 : 1;
00050 
00051 }
00052 
00053 
00054 
00055 namespace {
00056 
00057   Grid3D const  * factory() {
00058 
00059     Grid1D ga(0.,10.,5);
00060     Grid1D gb(-10.,10.,11);
00061     Grid1D gc(-10.,10.,11);
00062 
00063     std::vector< Grid3D::ValueType>  data;
00064     data.reserve(ga.nodes()*gb.nodes()*gc.nodes());
00065     for (int i=0; i<ga.nodes(); ++i) 
00066       for (int j=0; j<gb.nodes(); ++j) 
00067         for (int k=0; k<gc.nodes(); ++k) {
00068           data.push_back(Grid3D::ValueType(10*ga.node(i),10*gb.node(j),10*gc.node(k)));   
00069         }
00070     
00071     return new Grid3D(ga,gb,gc,data);
00072 
00073   }
00074 
00075 }
00076 
00077 
00078 
00079 
00080 #include "LinearGridInterpolator3D.h"
00081 #include <iostream>
00082 
00083 int grid3d_t() {
00084 
00085   Grid3D const  * grid = factory();
00086 
00087   LinearGridInterpolator3D inter(*grid);
00088   
00089   std::cout << inter.interpolate(7.5,7.2,-3.4) << std::endl;
00090   std::cout << inter.interpolate(-0.5,10.2,-3.4) << std::endl;
00091 
00092 
00093   delete grid;
00094   return 0;
00095 }