source: src/Fragmentation/Summation/SetValue.hpp@ 8eafd6

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests 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_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 FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes 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 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 8eafd6 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
RevLine 
[d699de]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
[30abdc]17#include <boost/function.hpp>
18#include <boost/shared_ptr.hpp>
19
[d699de]20#include "IndexSet.hpp"
[30abdc]21#include "IndexSetContainer.hpp"
[58952a]22#include "ZeroInstance.hpp"
[d699de]23
[30abdc]24#include "CodePatterns/Cacheable.hpp"
25#include "CodePatterns/Log.hpp"
[7bed4e]26#include "CodePatterns/Observer/Observable.hpp"
27
[30abdc]28class SetValueTest;
29
[d699de]30template <class T>
[7bed4e]31class SetValue : public Observable
[d699de]32{
[30abdc]33 //!> grant unit test access to allow setting static function
34 friend class SetValueTest;
[1e7dd4]35 //!> grant OrthogonalSummation access to static lookup functions
36 template <class TT> friend class OrthogonalSummation;
[c40e15d]37 //!> grant Summation access to static lookup functions
38 template <class TT> friend class Summation;
[d699de]39public:
[30abdc]40 typedef boost::shared_ptr< SetValue<T> > ptr;
41
42 SetValue(const IndexSet_ptr &_indices, const T &_value) :
[7bed4e]43 Observable("SubsetValue"),
[d699de]44 value(_value),
[30abdc]45 indices(_indices),
46 contribution(this,boost::bind(&SetValue<T>::calcSum,this),"contribution")
[d699de]47 {};
48
49 SetValue & operator+=(const SetValue &other) {
[30abdc]50 if (indices->contains(*other.indices)) {
51 OBSERVE;
[d699de]52 value += other.value;
[30abdc]53 }
[d699de]54 return *this;
55 }
56
57 SetValue & operator-=(const SetValue &other) {
[30abdc]58 if (indices->contains(*other.indices)) {
59 OBSERVE;
[d699de]60 value -= other.value;
[30abdc]61 }
[d699de]62 return *this;
63 }
64
[30abdc]65 const IndexSet_ptr& getIndexSet() const {
[d699de]66 return indices;
67 }
68
[30abdc]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 */
[7bed4e]81 const T getValue() const {
82 return value;
83 }
84
[30abdc]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 */
[d699de]91 void setValue(const T& _value) {
[7bed4e]92 OBSERVE;
[d699de]93 value = _value;
94 }
95
[7bed4e]96private:
97 // prohibit copies
98 SetValue(const SetValue<T> &_value);
99
[30abdc]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 *
[58952a]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.
[30abdc]112 *
[58952a]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
[30abdc]116 */
117 const T calcSum() {
[1b5a40]118 LOG(2, "DEBUG: Summing up contribution for " << *indices << ".");
[30abdc]119 // we initialize with value
120 T result = getValue();
[58952a]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.");
[30abdc]139 }
[58952a]140 } // else we return ZeroInstance<T>() as contribution
[30abdc]141 return result;
142 }
143
[7bed4e]144private:
[d699de]145 //!> value associated with this IndexSet
146 T value;
[7bed4e]147
[30abdc]148 //!> shared_ptr to the index set
149 IndexSet_ptr indices;
150
151 //!> stores the (orthogonal) contribution for this SubsetValue
152 Cacheable<T> contribution;
[d699de]153};
154
[30abdc]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;
[d699de]160
161#endif /* SETVALUE_HPP_ */
Note: See TracBrowser for help on using the repository browser.