Changeset 91e7658 for src/Fragmentation


Ignore:
Timestamp:
Sep 14, 2016, 6:42:53 PM (8 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_StructOpt_integration_tests, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, GeometryObjects, Gui_displays_atomic_force_velocity, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, RotateToPrincipalAxisSystem_UndoRedo, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, Ubuntu_1604_changes, stable
Children:
d56e21
Parents:
06653a
git-author:
Frederik Heber <heber@…> (05/26/16 09:46:06)
git-committer:
Frederik Heber <heber@…> (09/14/16 18:42:53)
Message:

SamplingGridUnitTest checks downsample on window.

  • FIX: getLengthsOfGrid() asserted that total equals 2level which is only true if the window spans the whole grid. We now assert for equal or less.
  • Renamed getLengthsOfGrid -> getLengthsOfWindow.
  • Extended downsample() tests to check for level difference of one to three.
Location:
src/Fragmentation/Summation/SetValues
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/Fragmentation/Summation/SetValues/SamplingGrid.cpp

    r06653a r91e7658  
    674674};
    675675
    676 static void getLengthsOfGrid(
     676static void getLengthsOfWindow(
    677677    int _total[NDIM],
    678678    const SamplingGrid &_grid)
     
    688688          } else
    689689            _total[index] = 0;
    690           ASSERT (_total[index] == ::pow(2, _grid.level),
     690          // we can only assert that its atmost the maximum number of grid points
     691          ASSERT (_total[index] <= ::pow(2, _grid.level),
    691692              "SamplingGrid::downsample() - total "+toString(_total[index])
    692               +" does not match 2^level: "+toString(_grid.level));
     693              +" is not equal or less than 2^level: "+toString(_grid.level));
    693694        }
    694695}
     
    788789    static_cast<SamplingGridProperties &>(instance) = other;
    789790    instance.setWindowSize(other.begin_window, other.end_window);
    790     if (_level >= other.level) {
     791    ASSERT( _level <= other.level,
     792        "SamplingGrid::downsample() - desired level "+toString(_level)
     793        +" larger than level "+toString(other.level)+" of the given values.");
     794    if (_level == other.level) {
    791795      instance.sampled_grid = other.sampled_grid;
    792796    } else {
     
    801805      int length_d[3];
    802806      int length_s[3];
    803       getLengthsOfGrid(length_s, other);
     807      getLengthsOfWindow(length_s, other);
    804808      for (instance.level = other.level-1; instance.level >= _level; --instance.level) {
    805         getLengthsOfGrid(length_d, instance);
     809        getLengthsOfWindow(length_d, instance);
    806810        // we always have an eighth of the number of sample points as we stop
     811        ASSERT( sourcevalues->size() % 8 == 0,
     812            "SamplingGrid::downsample() - at level "+toString( instance.level)
     813            +" given grid points "+toString(sourcevalues->size())+" are not even numbered per axis anymore.");
    807814        sampledvalues_t downsampled(sourcevalues->size()/(size_t)8, 0.);
    808815        restrictFullWeight(downsampled, length_d, *sourcevalues, length_s);
  • src/Fragmentation/Summation/SetValues/unittests/SamplingGridUnitTest.cpp

    r06653a r91e7658  
    4747
    4848#include "CodePatterns/Assert.hpp"
     49#include "CodePatterns/Log.hpp"
    4950
    5051#include <boost/assign.hpp>
     
    568569      + ::pow(N[2]/(double)_length-.5,2);
    569570}
     571
     572#ifdef HAVE_INLINE
     573inline
     574#endif
     575static double getBoundaryCaseFactor(const int N[NDIM], const int &_length)
     576{
     577  static double third_two=::pow(4., 1./3.);
     578  double returnthreshold = 1.;
     579  for (size_t i=0;i<NDIM;++i)
     580    if ((N[i] == 0) || (N[i] == _length-1))
     581      returnthreshold *= third_two;
     582  return returnthreshold;
     583}
     584
    570585/** UnitTest for downsample()
    571586 */
    572 void SamplingGridTest::downsampleTest()
     587void SamplingGridTest::downsample_gridTest()
    573588{
    574589  const double begin[NDIM] = { 0., 0., 0. };
     
    615630  }
    616631
    617   // same grid, window equals grids, different values
    618   {
    619     const int length_c = ::pow(2,2);
    620     const int length_f = ::pow(2,3);
    621     SamplingGrid::sampledvalues_t checkvalues((int)::pow(length_c, NDIM), 0.);
    622     SamplingGrid::sampledvalues_t othervalues((int)::pow(length_f, NDIM), 0.);
     632  // same grid, window equals grids, ever larger largegrids
     633  const int base_level = 2;
     634  for (int grid_difference = 1; grid_difference <= 3; ++grid_difference) {
     635//    LOG(2, "Checking with difference " << grid_difference);
     636    const int length_c = ::pow(2, base_level);
     637    const int length_f = ::pow(2, base_level+grid_difference);
     638    SamplingGrid::sampledvalues_t checkvalues((int)::pow(length_c, (int)NDIM), 0.);
     639    SamplingGrid::sampledvalues_t othervalues((int)::pow(length_f, (int)NDIM), 0.);
    623640    int N[NDIM];
    624641    for (N[0]=0; N[0]< length_f; ++N[0]) {
     
    641658    }
    642659
    643     SamplingGrid largegrid(begin, end, 3, othervalues);
     660    SamplingGrid largegrid(begin, end, base_level+grid_difference, othervalues);
    644661//    std::cout << " largegrid value " << largegrid.sampled_grid << std::endl;
    645     SamplingGrid smallgrid(begin, end, 2);
    646     SamplingGrid::downsample(smallgrid, largegrid, 2);
    647     SamplingGrid checkgrid(begin, end, 2, checkvalues);
     662    SamplingGrid smallgrid(begin, end, base_level);
     663    SamplingGrid::downsample(smallgrid, largegrid, base_level);
     664    SamplingGrid checkgrid(begin, end, base_level, checkvalues);
    648665//    std::cout << " smallgrid value " << smallgrid.sampled_grid << std::endl;
    649666//    std::cout << " checkgrid value " << checkgrid.sampled_grid << std::endl;
    650     static const double threshold = 1.2e-1;
    651     for (N[0]=0; N[0]< length_c; ++N[0])
     667    const double threshold = 3.2e-2;
     668    // we skip the first value as checkvalues[0] is always zero, hard to get from downsampling
     669    for (N[0]=1; N[0]< length_c; ++N[0])
    652670      for (N[1]=0; N[1]< length_c; ++N[1])
    653671        for (N[2]=0; N[2]< length_c; ++N[2]) {
    654672          const double check_threshold =
    655                     (((N[0] == 0) || (N[0] == length_c-1))
    656                   && ((N[1] == 0) || (N[1] == length_c-1))
    657                   && ((N[2] == 0) || (N[2] == length_c-1)))
    658               ? 2.*threshold : threshold;
     673              threshold*(1.+grid_difference/4.)*getBoundaryCaseFactor(N, length_c);
    659674          const int index = calculateIndex(N, length_c);
    660675//          std::cout << "Comparing "
    661676//              << fabs((smallgrid.sampled_grid[index] - checkgrid.sampled_grid[index]))
    662 //              << " < " << threshold << std::endl;
     677//              << " < " << check_threshold << std::endl;
    663678          CPPUNIT_ASSERT(
    664679              fabs((smallgrid.sampled_grid[index] - checkgrid.sampled_grid[index])) < check_threshold);
     680//          if (fabs(checkgrid.sampled_grid[index]) > 1e-10) {
     681//            std::cout << "Comparing "
     682//                << fabs((smallgrid.sampled_grid[index] - checkgrid.sampled_grid[index])/checkgrid.sampled_grid[index])
     683//                << " < " << check_threshold << std::endl;
     684//            CPPUNIT_ASSERT(
     685//                fabs((smallgrid.sampled_grid[index] - checkgrid.sampled_grid[index])/checkgrid.sampled_grid[index]) < check_threshold);
     686//          }
    665687        }
    666688  }
    667689}
     690
     691/** UnitTest for downsample()
     692 */
     693void SamplingGridTest::downsample_smallerwindowTest()
     694{
     695  const double begin[NDIM] = { 0., 0., 0. };
     696  const double end[NDIM] = { 2., 2., 2. };
     697
     698  // same grid, window is half of grid, different level by one
     699  const int base_level = 3;
     700  for (int grid_difference = 1; grid_difference <= 3; ++grid_difference) {
     701//    LOG(2, "Checking with difference " << grid_difference);
     702    const int window_length_c = ::pow(2, base_level-1);
     703    const int window_length_f = ::pow(2, base_level+grid_difference-1);
     704    SamplingGrid::sampledvalues_t checkvalues((int)::pow(window_length_c, (int)NDIM), 0.);
     705    SamplingGrid::sampledvalues_t othervalues((int)::pow(window_length_f, (int)NDIM), 0.);
     706    int N[NDIM];
     707    for (N[0]=0; N[0]< window_length_f; ++N[0]) {
     708      for (N[1]=0; N[1]< window_length_f; ++N[1]) {
     709        for (N[2]=0; N[2]< window_length_f; ++N[2]) {
     710          const int index = calculateIndex(N, window_length_f);
     711          const double dist = calculateDistanceSquared(N, window_length_f);
     712          othervalues[index] = cos(M_PI*dist/1.5);
     713        }
     714      }
     715    }
     716    for (N[0]=0; N[0]< window_length_c; ++N[0]) {
     717      for (N[1]=0; N[1]< window_length_c; ++N[1]) {
     718        for (N[2]=0; N[2]< window_length_c; ++N[2]) {
     719          const int index = calculateIndex(N, window_length_c);
     720          const double dist = calculateDistanceSquared(N, window_length_c);
     721          checkvalues[index] = cos(M_PI*dist/1.5);
     722        }
     723      }
     724    }
     725
     726    /** Here, we must initialize the larger grid with zero window first and
     727     * then add the values into the smaller window.
     728     */
     729    SamplingGrid largegrid(begin, end, base_level+grid_difference);
     730    const double window_begin[NDIM] = {
     731        largegrid.getNearestHigherGridPoint(0.5, 0),
     732        largegrid.getNearestHigherGridPoint(0.5, 1),
     733        largegrid.getNearestHigherGridPoint(0.5, 2) };
     734    const double window_end[NDIM] = {
     735        largegrid.getNearestLowerGridPoint(1.5, 0),
     736        largegrid.getNearestLowerGridPoint(1.5, 1),
     737        largegrid.getNearestLowerGridPoint(1.5, 2) };
     738    largegrid.setWindow(window_begin, window_end);
     739    largegrid.addOntoWindow(window_begin, window_end, othervalues, +1.);
     740//    std::cout << " largegrid value " << largegrid.sampled_grid << std::endl;
     741
     742    // smallgrid is downsample from full large grid
     743    SamplingGrid smallgrid(begin, end, base_level);
     744    SamplingGrid::downsample(smallgrid, largegrid, base_level);
     745//    std::cout << " smallgrid value " << smallgrid.sampled_grid << std::endl;
     746
     747    // checkgrid is created in the same way as the large grid (only with smaller level)
     748    SamplingGrid checkgrid(begin, end, base_level);
     749    checkgrid.setWindow(window_begin, window_end);
     750    checkgrid.addOntoWindow(window_begin, window_end, checkvalues, +1.);
     751//    std::cout << " checkgrid value " << checkgrid.sampled_grid << std::endl;
     752
     753    // then we compare over the full length
     754    /** Note that the threshold does not get better with increasing grid_difference,
     755     * on the contrary! For a grid_difference of 1, the finer grid is exact as it
     756     * directly sampled from the function. For a larger grid_difference, the finer
     757     * grid (being one level away from the desired coarse grid) is just an
     758     * approximation although obtained from a finer sampling. Hence, the larger the
     759     * grid_difference, the worse is the smallgrid with respect to checkgrid.
     760     */
     761    const double threshold = 3.2e-2;
     762    // we skip the first value as checkvalues[0] is always zero, hard to get from downsampling
     763    for (N[0]=1; N[0]< window_length_c; ++N[0])
     764      for (N[1]=0; N[1]< window_length_c; ++N[1])
     765        for (N[2]=0; N[2]< window_length_c; ++N[2]) {
     766          const double check_threshold =
     767              threshold*(1.+grid_difference/4.)*getBoundaryCaseFactor(N, window_length_c);
     768          const int index = calculateIndex(N, window_length_c);
     769          const double difference = fabs((smallgrid.sampled_grid[index] - checkgrid.sampled_grid[index]));
     770//          std::cout << "Comparing |"
     771//              << scientific
     772//              << smallgrid.sampled_grid[index] << " - "
     773//              << checkgrid.sampled_grid[index] << "| = "
     774//              << fabs((smallgrid.sampled_grid[index] - checkgrid.sampled_grid[index]))
     775//              << " < " << check_threshold << " => "
     776//              << fixed << setprecision(2)
     777//              << 100*difference/threshold
     778//              << " %, compare to " << 100*(1.+grid_difference/4.)*getBoundaryCaseFactor(N, window_length_c)
     779//              << " %" << std::endl;
     780          CPPUNIT_ASSERT( difference < check_threshold );
     781        }
     782  }
     783}
  • src/Fragmentation/Summation/SetValues/unittests/SamplingGridUnitTest.hpp

    r06653a r91e7658  
    3838    CPPUNIT_TEST ( equality_Test );
    3939    CPPUNIT_TEST ( serializeTest );
    40     CPPUNIT_TEST ( downsampleTest );
     40    CPPUNIT_TEST ( downsample_gridTest );
     41    CPPUNIT_TEST ( downsample_smallerwindowTest );
    4142    CPPUNIT_TEST_SUITE_END();
    4243
     
    5859      void equality_Test();
    5960      void serializeTest();
    60       void downsampleTest();
     61      void downsample_gridTest();
     62      void downsample_smallerwindowTest();
    6163
    6264private:
Note: See TracChangeset for help on using the changeset viewer.