source: src/Fragmentation/Summation/SetValue.hpp@ 1e7dd4

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
Last change on this file since 1e7dd4 was 1e7dd4, checked in by Frederik Heber <heber@…>, 12 years ago

Added OrthogonalSummation and unit test.

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 * SetValue.hpp
3 *
4 * Created on: Jun 25, 2012
5 * Author: heber
6 */
7
8#ifndef SETVALUE_HPP_
9#define SETVALUE_HPP_
10
11
12// include config.h
13#ifdef HAVE_CONFIG_H
14#include <config.h>
15#endif
16
17#include <boost/function.hpp>
18#include <boost/shared_ptr.hpp>
19
20#include "IndexSet.hpp"
21#include "IndexSetContainer.hpp"
22
23#include "CodePatterns/Cacheable.hpp"
24#include "CodePatterns/Log.hpp"
25#include "CodePatterns/Observer/Observable.hpp"
26
27class SetValueTest;
28
29template <class T>
30class SetValue : public Observable
31{
32 //!> grant unit test access to allow setting static function
33 friend class SetValueTest;
34 //!> grant OrthogonalSummation access to static lookup functions
35 template <class TT> friend class OrthogonalSummation;
36public:
37 typedef boost::shared_ptr< SetValue<T> > ptr;
38
39 SetValue(const IndexSet_ptr &_indices, const T &_value) :
40 Observable("SubsetValue"),
41 value(_value),
42 indices(_indices),
43 contribution(this,boost::bind(&SetValue<T>::calcSum,this),"contribution")
44 {};
45
46 SetValue & operator+=(const SetValue &other) {
47 if (indices->contains(*other.indices)) {
48 OBSERVE;
49 value += other.value;
50 }
51 return *this;
52 }
53
54 SetValue & operator-=(const SetValue &other) {
55 if (indices->contains(*other.indices)) {
56 OBSERVE;
57 value -= other.value;
58 }
59 return *this;
60 }
61
62 const IndexSet_ptr& getIndexSet() const {
63 return indices;
64 }
65
66 /** Getter for the contribution.
67 *
68 * @return contribution
69 */
70 const T getContribution() const {
71 return *contribution;
72 }
73
74 /** Getter for the value.
75 *
76 * @return value
77 */
78 const T getValue() const {
79 return value;
80 }
81
82 /** Setter for the value.
83 *
84 * This function is observed to notify all sets that contain us of a change.
85 *
86 * @param _value value to set value to
87 */
88 void setValue(const T& _value) {
89 OBSERVE;
90 value = _value;
91 }
92
93private:
94 // prohibit copies
95 SetValue(const SetValue<T> &_value);
96
97private:
98 //!> static lookup function to get to the subsets
99 static boost::function< IndexSetContainer::ptr & (const IndexSet_ptr &)> lookupSubset;
100
101 //!> static lookup function to get the SetValue for a specific IndexSet
102 static boost::function< typename SetValue<T>::ptr & (const IndexSet_ptr &)> lookupValue;
103
104 /** Calculates the contribution for this subset.
105 *
106 * Internal function that is bound to the cacheable
107 *
108 * @return
109 */
110 const T calcSum() {
111 LOG(1, "INFO: Summing up contribution for " << *indices << ".");
112 // we initialize with value
113 T result = getValue();
114 // then subtract contributions from all subsets
115 const IndexSetContainer::ptr &container = lookupSubset(indices);
116 if (container) {
117 const IndexSetContainer::Container_t &subsets = container->getContainer();
118 for (IndexSetContainer::Container_t::const_iterator iter = subsets.begin();
119 iter != subsets.end(); ++iter) {
120 LOG(2, "INFO: Current subset is " << **iter << ".");
121 // NOTE: our subset is not contained in the number of subsets
122 typename SetValue<T>::ptr ptr = lookupValue(*iter);
123 if (ptr)
124 result -= ptr->getContribution();
125 else
126 ELOG(1, "Cannot find " << **iter << " via lookup.");
127 }
128 } else {
129 ELOG(1, "Cannot find subsets for " << *indices << " via lookup.");
130 }
131 return result;
132 }
133
134private:
135 //!> value associated with this IndexSet
136 T value;
137
138 //!> shared_ptr to the index set
139 IndexSet_ptr indices;
140
141 //!> stores the (orthogonal) contribution for this SubsetValue
142 Cacheable<T> contribution;
143};
144
145template <class T>
146boost::function< IndexSetContainer::ptr & (const IndexSet_ptr &)> SetValue<T>::lookupSubset = NULL;
147
148template <class T>
149boost::function< typename SetValue<T>::ptr & (const IndexSet_ptr &)> SetValue<T>::lookupValue = NULL;
150
151#endif /* SETVALUE_HPP_ */
Note: See TracBrowser for help on using the repository browser.