source: src/Fragmentation/Summation/SetValue.hpp@ 336da8

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
Last change on this file since 336da8 was 58952a, checked in by Frederik Heber <heber@…>, 11 years ago

SetValue treats ZeroInstance as value special.

  • ZeroInstance in value indicates omit contribution in summation.
  • Property mode set to 100644
File size: 4.4 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#include "ZeroInstance.hpp"
23
24#include "CodePatterns/Cacheable.hpp"
25#include "CodePatterns/Log.hpp"
26#include "CodePatterns/Observer/Observable.hpp"
27
28class SetValueTest;
29
30template <class T>
31class SetValue : public Observable
32{
33 //!> grant unit test access to allow setting static function
34 friend class SetValueTest;
35 //!> grant OrthogonalSummation access to static lookup functions
36 template <class TT> friend class OrthogonalSummation;
37 //!> grant Summation access to static lookup functions
38 template <class TT> friend class Summation;
39public:
40 typedef boost::shared_ptr< SetValue<T> > ptr;
41
42 SetValue(const IndexSet_ptr &_indices, const T &_value) :
43 Observable("SubsetValue"),
44 value(_value),
45 indices(_indices),
46 contribution(this,boost::bind(&SetValue<T>::calcSum,this),"contribution")
47 {};
48
49 SetValue & operator+=(const SetValue &other) {
50 if (indices->contains(*other.indices)) {
51 OBSERVE;
52 value += other.value;
53 }
54 return *this;
55 }
56
57 SetValue & operator-=(const SetValue &other) {
58 if (indices->contains(*other.indices)) {
59 OBSERVE;
60 value -= other.value;
61 }
62 return *this;
63 }
64
65 const IndexSet_ptr& getIndexSet() const {
66 return indices;
67 }
68
69 /** Getter for the contribution.
70 *
71 * @return contribution
72 */
73 const T getContribution() const {
74 return *contribution;
75 }
76
77 /** Getter for the value.
78 *
79 * @return value
80 */
81 const T getValue() const {
82 return value;
83 }
84
85 /** Setter for the value.
86 *
87 * This function is observed to notify all sets that contain us of a change.
88 *
89 * @param _value value to set value to
90 */
91 void setValue(const T& _value) {
92 OBSERVE;
93 value = _value;
94 }
95
96private:
97 // prohibit copies
98 SetValue(const SetValue<T> &_value);
99
100private:
101 //!> static lookup function to get to the subsets
102 static boost::function< IndexSetContainer::ptr & (const IndexSet_ptr &)> lookupSubset;
103
104 //!> static lookup function to get the SetValue for a specific IndexSet
105 static boost::function< typename SetValue<T>::ptr & (const IndexSet_ptr &)> lookupValue;
106
107 /** Calculates the contribution for this subset.
108 *
109 * We calculate by value minus the summed contributions(!) from all subsets
110 * of this subset. If the value is ZeroInstance<T> however, we set its
111 * contribution to zero, as it is designated thereby as not contributing.
112 *
113 * \note This is internal function that is bound to the Cacheable contribution.
114 *
115 * @return value minus the summed contribution's from all subsets
116 */
117 const T calcSum() {
118 LOG(2, "DEBUG: Summing up contribution for " << *indices << ".");
119 // we initialize with value
120 T result = getValue();
121 // ZeroInstance<T>() is a specific value, if set, this subset does not contribute
122 if (result != ZeroInstance<T>()) { // compare directly, not numerically!
123 // then subtract contributions from all subsets
124 const IndexSetContainer::ptr &container = lookupSubset(indices);
125 if (container) {
126 const IndexSetContainer::Container_t &subsets = container->getContainer();
127 for (IndexSetContainer::Container_t::const_iterator iter = subsets.begin();
128 iter != subsets.end(); ++iter) {
129 LOG(3, "DEBUG: Current subset is " << **iter << ".");
130 // NOTE: our subset is not contained in the number of subsets
131 typename SetValue<T>::ptr ptr = lookupValue(*iter);
132 if (ptr)
133 result -= ptr->getContribution();
134 else
135 ELOG(1, "Cannot find " << **iter << " via lookup.");
136 }
137 } else {
138 ELOG(1, "Cannot find subsets for " << *indices << " via lookup.");
139 }
140 } // else we return ZeroInstance<T>() as contribution
141 return result;
142 }
143
144private:
145 //!> value associated with this IndexSet
146 T value;
147
148 //!> shared_ptr to the index set
149 IndexSet_ptr indices;
150
151 //!> stores the (orthogonal) contribution for this SubsetValue
152 Cacheable<T> contribution;
153};
154
155template <class T>
156boost::function< IndexSetContainer::ptr & (const IndexSet_ptr &)> SetValue<T>::lookupSubset = NULL;
157
158template <class T>
159boost::function< typename SetValue<T>::ptr & (const IndexSet_ptr &)> SetValue<T>::lookupValue = NULL;
160
161#endif /* SETVALUE_HPP_ */
Note: See TracBrowser for help on using the repository browser.