Changeset c6355f


Ignore:
Timestamp:
Feb 15, 2013, 9:51:49 AM (12 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, 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_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
64bafe0
Parents:
de6dfb
git-author:
Frederik Heber <heber@…> (12/15/12 08:33:06)
git-committer:
Frederik Heber <heber@…> (02/15/13 09:51:49)
Message:

FIX: New non-zero window operations of SamplingGrid are now fully working.

Location:
src/Jobs/Grid
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/Jobs/Grid/SamplingGrid.cpp

    rde6dfb rc6355f  
    6060SamplingGrid::SamplingGrid(const double _begin[3],
    6161    const double _end[3],
     62    const int _level) :
     63  SamplingGridProperties(_begin, _end, _level)
     64{
     65  setWindowSize(zeroOffset, zeroOffset);
     66  ASSERT( getWindowGridPoints() == (size_t)0,
     67      "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
     68}
     69
     70SamplingGrid::SamplingGrid(const double _begin[3],
     71    const double _end[3],
    6272    const int _level,
    6373    const sampledvalues_t &_sampled_grid) :
     
    7484  sampled_grid(_grid.sampled_grid)
    7585{
    76   setWindowSize(_grid.begin, _grid.end);
     86  setWindowSize(_grid.begin_window, _grid.end_window);
     87  ASSERT( getWindowGridPoints() == _grid.getWindowGridPoints(),
     88      "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
    7789}
    7890
     
    8092  SamplingGridProperties(_props)
    8193{
    82   setWindowSize(_props.begin, _props.end);
     94  setWindowSize(zeroOffset, zeroOffset);
     95  ASSERT( getWindowGridPoints() == (size_t)0,
     96      "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
    8397}
    8498
     
    241255  setWindowSize(_begin_window, _end_window);
    242256  const size_t gridpoints_window = getWindowGridPoints();
     257  sampled_grid.clear();
    243258  sampled_grid.resize(gridpoints_window, 0.);
    244259}
     
    268283    const double _end_window[3])
    269284{
    270   // check that we truly have to extend the window
    271285#ifndef NDEBUG
    272286  for(size_t index=0;index < 3; ++index) {
     287    // check that we truly have to extend the window
    273288    ASSERT ( begin_window[index] >= _begin_window[index],
    274289        "SamplingGrid::extendWindow() - component "+toString(index)+
     
    277292        "SamplingGrid::extendWindow() - component "+toString(index)+
    278293        " of window end is less than old value.");
     294
     295    // check that we are still less than domain
     296    ASSERT ( _begin_window[index] >= begin[index],
     297        "SamplingGrid::extendWindow() - component "+toString(index)+
     298        " of window start is less than domain start.");
     299    ASSERT ( _end_window[index] <= end[index],
     300        "SamplingGrid::extendWindow() - component "+toString(index)+
     301        " of window end is greater than domain end.");
    279302  }
    280303#endif
     
    311334#endif
    312335  // the only issue are indices
    313   const size_t gridpoints_axis = pow(2, level);
     336  const size_t gridpoints_axis = getGridPointsPerAxis();
    314337  size_t pre_offset[3];
    315338  size_t post_offset[3];
     
    318341  for(size_t index=0;index<3;++index) {
    319342    pre_offset[index] = (_begin_window[index] - begin_window[index])*gridpoints_axis;
     343    length[index] = (_end_window[index] - _begin_window[index])*gridpoints_axis;
    320344    post_offset[index] = (end_window[index] - _end_window[index])*gridpoints_axis;
    321     length[index] = (_end_window[index] - _begin_window[index])*gridpoints_axis;
    322345    total[index] = (end_window[index] - begin_window[index])*gridpoints_axis;
    323346    ASSERT( pre_offset[index]+post_offset[index]+length[index] == total[index],
    324         "SamplingGrid::addOntoWindow() - pre, past, and length don't sum up to total for "
     347        "SamplingGrid::addOntoWindow() - pre, post, and length don't sum up to total for "
    325348        +toString(index)+"th component.");
    326349  }
    327   ASSERT( length[0]*length[1]*length[2] ==  _sampled_grid.size(),
    328       "SamplingGrid::addOntoWindow() - not enough sampled values given.");
    329   ASSERT( length[0]*length[1]*length[2] <=  sampled_grid.size(),
    330       "SamplingGrid::addOntoWindow() - not enough sampled values available.");
     350#ifndef NDEBUG
     351  const size_t calculated_size = length[0]*length[1]*length[2];
     352  ASSERT( calculated_size == _sampled_grid.size(),
     353      "SamplingGrid::addOntoWindow() - not enough sampled values given: "
     354      +toString(calculated_size)+" != "+toString(_sampled_grid.size())+".");
     355  ASSERT( calculated_size <=  sampled_grid.size(),
     356      "SamplingGrid::addOntoWindow() - not enough sampled values available: "
     357      +toString(calculated_size)+" <= "+toString(sampled_grid.size())+".");
     358  const size_t total_size = total[0]*total[1]*total[2];
     359  ASSERT( total_size == sampled_grid.size(),
     360      "SamplingGrid::addOntoWindow() - total size is not equal to number of present points: "
     361      +toString(total_size)+" != "+toString(sampled_grid.size())+".");
     362#endif
    331363  size_t N[3];
     364  size_t counter = 0;
    332365  sampledvalues_t::iterator griditer = sampled_grid.begin();
    333366  std::advance(griditer, pre_offset[0]*total[1]*total[2]);
     
    338371      std::advance(griditer, pre_offset[2]);
    339372      for(N[2]=0; N[2] < length[2]; ++N[2]) {
     373        ASSERT( griditer != sampled_grid.end(),
     374            "SamplingGrid::addOntoWindow() - griditer is already at end of window.");
     375        ASSERT( copyiter != _sampled_grid.end(),
     376            "SamplingGrid::addOntoWindow() - griditer is already at end of window.");
    340377        *griditer++ += prefactor*(*copyiter++);
    341378      }
     
    344381    std::advance(griditer, post_offset[1]*total[2]);
    345382  }
    346   //std::advance(griditer, post_offset[0]*total[1]*total[2]);
     383#ifndef NDEBUG
     384  std::advance(griditer, post_offset[0]*total[1]*total[2]);
     385  ASSERT( griditer == sampled_grid.end(),
     386      "SamplingGrid::addOntoWindow() - griditer is not at end of window.");
     387  ASSERT( copyiter == _sampled_grid.end(),
     388      "SamplingGrid::addOntoWindow() - copyiter is not at end of window.");
     389#endif
    347390  LOG(2, "DEBUG: Grid after adding other is " << sampled_grid << ".");
    348391}
  • TabularUnified src/Jobs/Grid/SamplingGrid.hpp

    rde6dfb rc6355f  
    4040  typedef std::vector< double > sampledvalues_t;
    4141
    42   /** Constructor for class SamplingGrid.
     42  /** Constructor for class SamplingGrid for full window.
     43   *
     44   * Here, the window of sampled values spans the given domain.
    4345   *
    4446   * \param _begin offset for grid per axis
     
    5254      const sampledvalues_t &_sampled_grid);
    5355
    54   /** Copy constructor for class SamplingGrid.
    55    *
    56    * \param _grid grid to copy
    57    */
    58   SamplingGrid(const SamplingGrid &_grid);
    59 
    60   /** Copy constructor for class SamplingGrid from SamplingGridProperties.
     56  /** Constructor for class SamplingGrid for empty window.
     57   *
     58   * Here, the window is initially of size zero.
     59   *
     60   * \param _begin offset for grid per axis
     61   * \param _end edge length of grid per axis
     62   * \param _level number of grid points in \f$2^{\text{level}}\f$
     63   */
     64  SamplingGrid(const double _begin[3],
     65      const double _end[3],
     66      const int _level);
     67
     68  /** Copy constructor for class SamplingGrid with full window from SamplingGridProperties.
     69   *
     70   * Here, the window is initially empty.
    6171   *
    6272   * \param _props properties to copy
     
    6474  SamplingGrid(const SamplingGridProperties &_props);
    6575
    66   /** Copy constructor for class SamplingGrid from SamplingGridProperties.
     76  /** Copy constructor for class SamplingGrid with empty window from SamplingGridProperties.
     77   *
     78   * Here, the window must span the whole domain
    6779   *
    6880   * \param _props properties to copy
     
    7284      const SamplingGridProperties &_props,
    7385      const sampledvalues_t &_sampled_grid);
     86
     87  /** Copy constructor for class SamplingGrid.
     88   *
     89   * The window of sampled values corresponds to the one on \a _grid.
     90   *
     91   * \param _grid grid to copy
     92   */
     93  SamplingGrid(const SamplingGrid &_grid);
    7494
    7595  /** default cstor.
  • TabularUnified src/Jobs/Grid/unittests/SamplingGridUnitTest.cpp

    rde6dfb rc6355f  
    4343#include "CodePatterns/Assert.hpp"
    4444
     45#include <boost/assign.hpp>
    4546#include <cmath>
    46 #include <boost/assign.hpp>
     47#include <numeric>
    4748
    4849#ifdef HAVE_TESTRUNNER
     
    5758
    5859#define NUMBEROFSAMPLES(n) (size_t)(pow(pow(2,n),3))
     60#define DOMAINVOLUME(l) (size_t)pow(l,3)
    5961
    6062// Registers the fixture into the 'registry'
     
    7072  const double begin[3] = { 0., 0., 0. };
    7173  const double end[3] = { 1., 1., 1. };
    72   for (size_t i=0; i< pow(1,3)*NUMBEROFSAMPLES(2); ++i)
     74  for (size_t i=0; i< DOMAINVOLUME(1)*NUMBEROFSAMPLES(2); ++i)
    7375    values += grid_value;
    7476  grid = new SamplingGrid(begin, end, 2, values);
     
    8991  const double begin[3] = { 0., 0., 0. };
    9092  const double end[3] = { 2., 2., 2. };
    91   SamplingGridProperties illegal_props(begin, end, 1);
     93  SamplingGridProperties illegal_props(begin, end, 5);
    9294  SamplingGridProperties legal_props(begin, end, 2);
    9395  CPPUNIT_ASSERT( !grid->isCompatible(illegal_props) );
     
    98100  SamplingGrid::sampledvalues_t legal_values;
    99101  for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
    100     illegal_values += 1.5;
     102    legal_values += 1.5;
    101103#ifndef NDEBUG
    102104  // throws because props and size of illegal_values don't match
     
    114116}
    115117
    116 /** UnitTest for getVolume_Test
     118/** UnitTest for integral()
     119 */
     120void SamplingGridTest::integral_Test()
     121{
     122  double sum = 0.;
     123  sum = std::accumulate( grid->sampled_grid.begin(), grid->sampled_grid.end(), sum );
     124  CPPUNIT_ASSERT_EQUAL( sum*grid->getVolume()/grid->getWindowGridPoints(), grid->integral() );
     125}
     126
     127/** UnitTest for getVolume()
    117128 */
    118129void SamplingGridTest::getVolume_Test()
     
    121132}
    122133
    123 /** UnitTest for getWindowSize_Test
     134/** UnitTest for getWindowSize()
    124135 */
    125136void SamplingGridTest::getWindowSize_Test()
     
    147158void SamplingGridTest::extendWindow_Test()
    148159{
    149   // we have a grid with size of two, extend to twice the size and check
     160  // we have a grid with size of one, extend to twice the size and check
    150161  const double begin[3] = { 0., 0., 0. };
    151162  const double size = 2.;
    152163  const double end[3] = { size, size, size };
    153   grid->extendWindow(begin, end);
    154 
    155   // check integral
    156   CPPUNIT_ASSERT_EQUAL( grid_value/(NUMBEROFSAMPLES(grid->level)/NUMBEROFSAMPLES(grid->level)), grid->integral() );
    157 
    158   // check number of points
    159   CPPUNIT_ASSERT_EQUAL( (size_t)NUMBEROFSAMPLES(grid->level), grid->getWindowGridPoints() );
     164  double offset[3];
     165  for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
     166    for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
     167      for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
     168        const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
     169        const double window_end[3] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
     170        SamplingGrid newgrid(begin, end, 2);
     171        newgrid.setWindowSize(window_begin, window_end);
     172        // resize values by hand to new window size. Otherwise they get zero'd.
     173        newgrid.sampled_grid = values;
     174        newgrid.sampled_grid.resize(NUMBEROFSAMPLES(1));
     175        newgrid.extendWindow(begin, end);
     176
     177        // check integral
     178        CPPUNIT_ASSERT_EQUAL(
     179            grid_value/(NUMBEROFSAMPLES(grid->level)/NUMBEROFSAMPLES(grid->level)),
     180            grid->integral()
     181            );
     182
     183        // check number of points
     184        CPPUNIT_ASSERT_EQUAL(
     185            (size_t)NUMBEROFSAMPLES(grid->level),
     186            grid->getWindowGridPoints()
     187            );
     188      }
    160189}
    161190
     
    164193void SamplingGridTest::extendWindow_asymmetric_Test()
    165194{
     195  std::cout << "SamplingGridTest::extendWindow_asymmetric_Test()" << std::endl;
    166196  const double begin[3] = { 0., 0., 0. };
    167197  const double end[3] = { 2., 2., 2. };
    168   grid->sampled_grid.clear();
    169   SamplingGrid::sampledvalues_t::iterator griditer = grid->sampled_grid.begin();
    170   for (size_t i=0; i< NUMBEROFSAMPLES(grid->level); ++i)
    171      *griditer++ += grid_value*i;
    172   grid->extendWindow(begin, end);
    173 
    174   CPPUNIT_ASSERT_EQUAL( grid_value, grid->integral() );
     198  double offset[3];
     199  for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
     200    for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
     201      for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
     202        const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
     203        const double window_end[3] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
     204        SamplingGrid newgrid(begin, end, 2);
     205        CPPUNIT_ASSERT_EQUAL( (size_t)0, newgrid.getWindowGridPoints() );
     206        newgrid.setWindowSize(window_begin, window_end);
     207        // window size is only half of domain size
     208        const size_t max_samples = NUMBEROFSAMPLES(newgrid.level)*pow(0.5,3);
     209        for (size_t i=0; i< max_samples; ++i)
     210          newgrid.sampled_grid += grid_value*i;
     211        const size_t sum_weight = (max_samples)*(max_samples-1)/2;
     212        const double integral = newgrid.integral();
     213        newgrid.extendWindow(begin, end);
     214
     215        // check that integral has remained the same
     216        CPPUNIT_ASSERT_EQUAL( integral, newgrid.integral() );
     217        CPPUNIT_ASSERT_EQUAL( grid_value*sum_weight/DOMAINVOLUME(2), newgrid.integral() );
     218      }
    175219}
    176220
     
    182226  CPPUNIT_ASSERT_EQUAL( 1.*grid_value, grid->integral() );
    183227
    184   // create another window from (.5,.5,.5) to (1., 1., 1.)
    185   const double begin[3] = { .5, .5, .5 };
    186   const double end[3] = { 1., 1., 1. };
     228  // create values for half-sized window
    187229  values.clear();
    188   for (size_t i=0; i< NUMBEROFSAMPLES(2)/pow(2,3); ++i) // cut in half
     230  for (size_t i=0; i< (size_t)pow(.5*pow(2,2),3); ++i)
    189231    values += grid_value;
     232
    190233  // check that too large a window throws
    191234#ifndef NDEBUG
     235  const double begin[3] = { .5, .5, .5 };
    192236  const double wrongend[3] = { 1.5, 1.5, 1.5 };
    193237  std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
    194238  CPPUNIT_ASSERT_THROW( grid->addOntoWindow(begin, wrongend, values, +1.), Assert::AssertionFailure );
    195239#endif
    196   // now perform working operation
    197   grid->addOntoWindow(begin, end, values, +1.);
    198 
    199   // check integral to be one and half times the old value
    200   CPPUNIT_ASSERT_EQUAL( (1.+pow(.5,3))*grid_value, grid->integral() );
     240
     241  // create another window from (.5,.5,.5) to (1., 1., 1.)
     242  double offset[3];
     243  for (offset[0] = 0.; offset[0] <= .5; offset[0] += .5)
     244    for (offset[1] = 0.; offset[1] <= .5; offset[1] += .5)
     245      for (offset[2] = 0.; offset[2] <= .5; offset[2] += .5) {
     246        const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
     247        const double window_end[3] = { .5+offset[0], .5+offset[1], .5+offset[2]};
     248
     249        SamplingGrid newgrid(*grid);
     250        // now perform working operation
     251        newgrid.addOntoWindow(window_begin, window_end, values, +1.);
     252
     253        // check integral to be one and one eighth times the old value
     254        CPPUNIT_ASSERT_EQUAL( (1.+pow(.5,3))*grid_value, newgrid.integral() );
     255      }
    201256}
    202257
     
    205260void SamplingGridTest::addOntoWindow_asymmetric_Test()
    206261{
     262  const size_t size = grid->end[0]-grid->begin[0];
    207263  // check with asymmetric values
    208264  grid->sampled_grid.clear();
    209   grid->sampled_grid.resize(NUMBEROFSAMPLES(2), 0.);
     265  grid->sampled_grid.resize(DOMAINVOLUME(size)*NUMBEROFSAMPLES(grid->level), 0.);
    210266
    211267  for (size_t i=0;i<grid->level*(grid->end[0]-grid->begin[0]);++i)
     
    217273
    218274  const double integral = grid->integral();
    219   const double begin[3] = { 0., 0., 0. };
    220   for(double size=2.; size<=5.; size += 1.) {
    221     const double newend[3] = {size, size, size};
    222     grid->extendWindow(begin, newend);
    223     CPPUNIT_ASSERT_EQUAL( pow(size,3)*integral, grid->integral() );
    224   }
     275
     276  // now perform working operation
     277  grid->addOntoWindow(grid->begin, grid->end, values, +1.);
     278  // values is equal to integral of 1.
     279  CPPUNIT_ASSERT_EQUAL( 1.+integral, grid->integral() );
    225280}
    226281
  • TabularUnified src/Jobs/Grid/unittests/SamplingGridUnitTest.hpp

    rde6dfb rc6355f  
    2727    CPPUNIT_TEST ( operatorPlusEqual_Test );
    2828    CPPUNIT_TEST ( operatorMinusEqual_Test );
     29    CPPUNIT_TEST ( integral_Test );
    2930    CPPUNIT_TEST ( getVolume_Test );
    3031    CPPUNIT_TEST ( getWindowSize_Test );
     
    3940      void tearDown();
    4041      void compatibleGrids_Test();
     42      void integral_Test();
    4143      void getVolume_Test();
    4244      void getWindowSize_Test();
Note: See TracChangeset for help on using the changeset viewer.