Changeset 30abdc
- Timestamp:
- Nov 8, 2012, 1:13:13 PM (12 years ago)
- 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:
- c508fea
- Parents:
- 7bed4e
- git-author:
- Frederik Heber <heber@…> (07/04/12 15:31:42)
- git-committer:
- Frederik Heber <heber@…> (11/08/12 13:13:13)
- Location:
- src/Fragmentation/Summation
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Fragmentation/Summation/SetValue.hpp
r7bed4e r30abdc 15 15 #endif 16 16 17 #include <boost/function.hpp> 18 #include <boost/shared_ptr.hpp> 19 17 20 #include "IndexSet.hpp" 21 #include "IndexSetContainer.hpp" 18 22 23 #include "CodePatterns/Cacheable.hpp" 24 #include "CodePatterns/Log.hpp" 19 25 #include "CodePatterns/Observer/Observable.hpp" 26 27 class SetValueTest; 20 28 21 29 template <class T> 22 30 class SetValue : public Observable 23 31 { 32 //!> grant unit test access to allow setting static function 33 friend class SetValueTest; 24 34 public: 25 SetValue(const IndexSet &_indices, const T &_value) : 35 typedef boost::shared_ptr< SetValue<T> > ptr; 36 37 SetValue(const IndexSet_ptr &_indices, const T &_value) : 26 38 Observable("SubsetValue"), 27 39 value(_value), 28 indices(_indices) 40 indices(_indices), 41 contribution(this,boost::bind(&SetValue<T>::calcSum,this),"contribution") 29 42 {}; 30 43 31 44 SetValue & operator+=(const SetValue &other) { 32 if (indices.contains(other.indices)) 45 if (indices->contains(*other.indices)) { 46 OBSERVE; 33 47 value += other.value; 48 } 34 49 return *this; 35 50 } 36 51 37 52 SetValue & operator-=(const SetValue &other) { 38 if (indices.contains(other.indices)) 53 if (indices->contains(*other.indices)) { 54 OBSERVE; 39 55 value -= other.value; 56 } 40 57 return *this; 41 58 } 42 59 43 const IndexSet & getIndexSet() const {60 const IndexSet_ptr& getIndexSet() const { 44 61 return indices; 45 62 } 46 63 64 /** Getter for the contribution. 65 * 66 * @return contribution 67 */ 68 const T getContribution() const { 69 return *contribution; 70 } 71 72 /** Getter for the value. 73 * 74 * @return value 75 */ 47 76 const T getValue() const { 48 77 return value; 49 78 } 50 79 80 /** Setter for the value. 81 * 82 * This function is observed to notify all sets that contain us of a change. 83 * 84 * @param _value value to set value to 85 */ 51 86 void setValue(const T& _value) { 52 87 OBSERVE; … … 59 94 60 95 private: 96 //!> static lookup function to get to the subsets 97 static boost::function< IndexSetContainer::ptr & (const IndexSet_ptr &)> lookupSubset; 98 99 //!> static lookup function to get the SetValue for a specific IndexSet 100 static boost::function< typename SetValue<T>::ptr & (const IndexSet_ptr &)> lookupValue; 101 102 /** Calculates the contribution for this subset. 103 * 104 * Internal function that is bound to the cacheable 105 * 106 * @return 107 */ 108 const T calcSum() { 109 LOG(1, "INFO: Summing up contribution for " << *indices << "."); 110 // we initialize with value 111 T result = getValue(); 112 // then subtract contributions from all subsets 113 const IndexSetContainer::ptr &container = lookupSubset(indices); 114 if (container) { 115 const IndexSetContainer::Container_t &subsets = container->getContainer(); 116 for (IndexSetContainer::Container_t::const_iterator iter = subsets.begin(); 117 iter != subsets.end(); ++iter) { 118 LOG(2, "INFO: Current subset is " << **iter << "."); 119 // NOTE: our subset is not contained in the number of subsets 120 typename SetValue<T>::ptr ptr = lookupValue(*iter); 121 if (ptr) 122 result -= ptr->getContribution(); 123 else 124 ELOG(1, "Cannot find " << **iter << " via lookup."); 125 } 126 } else { 127 ELOG(1, "Cannot find subsets for " << *indices << " via lookup."); 128 } 129 return result; 130 } 131 132 private: 61 133 //!> value associated with this IndexSet 62 134 T value; 63 135 64 //!> reference to the index set 65 const IndexSet &indices; 136 //!> shared_ptr to the index set 137 IndexSet_ptr indices; 138 139 //!> stores the (orthogonal) contribution for this SubsetValue 140 Cacheable<T> contribution; 66 141 }; 67 142 143 template <class T> 144 boost::function< IndexSetContainer::ptr & (const IndexSet_ptr &)> SetValue<T>::lookupSubset = NULL; 145 146 template <class T> 147 boost::function< typename SetValue<T>::ptr & (const IndexSet_ptr &)> SetValue<T>::lookupValue = NULL; 68 148 69 149 #endif /* SETVALUE_HPP_ */ -
src/Fragmentation/Summation/unittests/Makefile.am
r7bed4e r30abdc 52 52 unittests/SetValueUnitTest.cpp \ 53 53 unittests/SetValueUnitTest.hpp \ 54 unittests/stubs/SetValueMap_Mock.hpp \ 55 unittests/stubs/SubsetMap_Mock.hpp \ 56 IndexSet.cpp \ 57 IndexSet.hpp \ 58 IndexSetContainer.hpp \ 54 59 SetValue.hpp 55 60 SetValueUnitTest_LDADD = ${FRAGMENTATIONSUMMATIONLIBS} -
src/Fragmentation/Summation/unittests/SetValueUnitTest.cpp
r7bed4e r30abdc 39 39 #include <cppunit/ui/text/TestRunner.h> 40 40 41 #include "Fragmentation/Summation/IndexSet.hpp" 42 #include "Fragmentation/Summation/IndexSetContainer.hpp" 41 43 #include "Fragmentation/Summation/SetValue.hpp" 42 44 43 45 #include "SetValueUnitTest.hpp" 46 #include "stubs/SetValueMap_Mock.hpp" 47 #include "stubs/SubsetMap_Mock.hpp" 44 48 45 49 #include <boost/assign.hpp> … … 58 62 CPPUNIT_TEST_SUITE_REGISTRATION( SetValueTest ); 59 63 64 LookupValue LV; 65 LookupSubset LS; 66 60 67 61 68 void SetValueTest::setUp() … … 63 70 IndexSet set; 64 71 set += 1,2,3,4; 65 indices = new IndexSet( set ); 66 value = new SetValue<int>(*indices, VALUE); 72 indices.reset(new IndexSet( set )); 73 value.reset(new SetValue<int>(indices, VALUE)); 74 SetValue<int>::lookupSubset = 75 boost::bind(&LookupSubset::getSubsets, boost::ref(LS), _1); 76 SetValue<int>::lookupValue = 77 boost::bind(&LookupValue::getValue, boost::ref(LV), _1); 78 LV.LookupMap.insert( std::make_pair(indices, value) ); 79 //IndexSet_ptr emptyset(new IndexSet); 80 //LS.LookupMap.insert( std::make_pair(indices, IndexSetContainer::ptr( new IndexSetContainer(emptyset) )) ); 67 81 }; 68 82 … … 70 84 void SetValueTest::tearDown() 71 85 { 72 delete value;73 delete indices;74 86 }; 75 87 … … 87 99 void SetValueTest::operatorTest() 88 100 { 89 SetValue<int> other( *indices, OTHER);101 SetValue<int> other(indices, OTHER); 90 102 91 103 // operator-= … … 99 111 CPPUNIT_ASSERT_EQUAL( (int)VALUE - ((int)OTHER - (int)VALUE), value->getValue() ); 100 112 }; 113 114 void SetValueTest::getContributiontest() 115 { 116 IndexSet set; 117 set += 1,2,3; 118 IndexSet_ptr otherindices(new IndexSet(set)); 119 SetValue<int>::ptr othervalue(new SetValue<int>(otherindices, OTHER)); 120 LV.LookupMap.insert( std::make_pair(otherindices, othervalue) ); 121 122 // connect as subset 123 LS.LookupMap[indices].reset(new IndexSetContainer(otherindices)); 124 125 std::cout << "The following error is intended and does not constitute a failure of the test." << std::endl; 126 CPPUNIT_ASSERT_EQUAL( (int)OTHER, othervalue->getContribution() ); 127 CPPUNIT_ASSERT_EQUAL( 2, value->getContribution() ); 128 129 LS.LookupMap.erase( indices ); 130 } -
src/Fragmentation/Summation/unittests/SetValueUnitTest.hpp
r7bed4e r30abdc 17 17 #include <cppunit/extensions/HelperMacros.h> 18 18 19 #include "Fragmentation/Summation/IndexSet.hpp" 19 20 #include "Fragmentation/Summation/SetValue.hpp" 20 21 class IndexSet;22 21 23 22 /********************************************** Test classes **************************************/ … … 28 27 CPPUNIT_TEST ( constructorTest ); 29 28 CPPUNIT_TEST ( operatorTest ); 29 CPPUNIT_TEST ( getContributiontest ); 30 30 CPPUNIT_TEST_SUITE_END(); 31 31 … … 35 35 void constructorTest(); 36 36 void operatorTest(); 37 void getContributiontest(); 37 38 38 39 private: 39 IndexSet *indices;40 SetValue<int> *value;40 IndexSet::ptr indices; 41 SetValue<int>::ptr value; 41 42 42 43 };
Note:
See TracChangeset
for help on using the changeset viewer.