source: src/Parameters/ContinuousValue_impl.hpp@ c68409

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 c68409 was c68409, checked in by Michael Ankele <ankele@…>, 13 years ago

copied Parameters and template-ized

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/*
2 * ContinuousValue_impl.hpp
3 *
4 * Created on: Sep 29, 2011
5 * Author: heber
6 */
7
8#ifndef CONTINUOUSVALUE_IMPL_HPP_
9#define CONTINUOUSVALUE_IMPL_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include <string>
17
18#include <boost/any.hpp>
19
20#include "CodePatterns/Assert.hpp"
21#include "CodePatterns/Log.hpp"
22#include "CodePatterns/Range.hpp"
23
24#include "ContinuousValue.hpp"
25
26// static member
27template <class T> ConvertTo<T> ContinuousValue<T>::Converter;
28
29/** Constructor of class DiscreteValue.
30 */
31template <class T>
32ContinuousValue<T>::ContinuousValue() :
33 ValueSet(false),
34 ValidRangeSet(range<bool>(false, false))
35{}
36
37/** Constructor of class DiscreteValue with set of valid values.
38 *
39 * @param _ValidValues vector with all valid values
40 */
41template <class T>
42ContinuousValue<T>::ContinuousValue(const range<T> &_ValidRange) :
43 ValueSet(false),
44 ValidRangeSet(range<bool>(true, true)),
45 ValidRange(_ValidRange)
46{
47// LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
48}
49
50/** Destructor of class DiscreteValue.
51 */
52template <class T>
53ContinuousValue<T>::~ContinuousValue()
54{}
55
56/** Checks whether \a _value is a valid value.
57 * \param _value value to check for validity.
58 * \return true - \a _value is valid, false - is not
59 */
60template <class T>
61bool ContinuousValue<T>::isValid(const T & _value) const
62{
63 return isValidValue(_value);
64}
65
66/** Compares this continuous value against another \a _instance.
67 *
68 * @param _instance other value to compare to
69 * @return true - if value and valid ranges are the same, false - else
70 */
71template <class T>
72bool ContinuousValue<T>::operator==(const ContinuousValue<T> &_instance) const
73{
74 bool status = true;
75 status = status && (ValidRange == _instance.ValidRange);
76 status = status && (ValueSet == _instance.ValueSet);
77 if (ValueSet && _instance.ValueSet)
78 status = status && (value == _instance.value);
79 return status;
80}
81
82/** Getter of value, returning string.
83 *
84 * @return string value
85 */
86template <class T>
87const std::string ContinuousValue<T>::get() const
88{
89 ASSERT(ValueSet,
90 "ContinuousValue<T>::get() - requesting unset value.");
91 return toString(value);
92}
93
94/** Setter of value for string
95 *
96 * @param _value string containing new value
97 */
98template <class T>
99void ContinuousValue<T>::set(const std::string _value)
100{
101 const T castvalue = Converter(_value);
102// LOG(0, "Converted value reads " << castvalue <<".");
103 setValue(castvalue);
104// LOG(0, "STATUS: Value is now set to " << value << ".");
105}
106
107/** Setter for the valid range.
108 *
109 * If value is invalid in new range, we throw AssertFailure and set ValueSet to false.
110 *
111 * @param _range range (pair of values)
112 */
113template <class T>
114void ContinuousValue<T>::setValidRange(const range<T> &_range)
115{
116
117 ValidRangeSet = range<bool>(true, true);
118 ValidRange = _range;
119 if (ValueSet) {
120 //std::cout << "Checking whether " << value << " is in range " << ValidRange << "." << std::endl;
121 if (!((ValidRange.isInRange(value)) || (value == ValidRange.last))) {
122 //std::cout << "ValueSet to false." << std::endl;
123 ValueSet = false;
124 // have full check again in assert such that it appears in output, too
125 ASSERT(ValidRange.isInRange(value) || (value == ValidRange.last),
126 "ContinuousValue<T>::setValidRange() - new range "
127 +toString(_range)+" invalidates current value "+toString(value)+".");
128 }
129 }
130// LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
131}
132
133/** Getter for the valid range.
134 *
135 * @return _range range (pair of values)
136 */
137template <class T>
138const range<T> & ContinuousValue<T>::getValidRange() const
139{
140 ASSERT(ValidRangeSet.first && ValidRangeSet.last,
141 "ContinuousValue<T>::getValidRange() called though no valid range set so far.");
142 return ValidRange;
143}
144
145/** Checks whether \a _value is a valid value.
146 * \param _value value to check for validity.
147 * \return true - \a _value is valid, false - is not
148 */
149template <class T>
150bool ContinuousValue<T>::isValidValue(const T &_value) const
151{
152 bool isBefore = true;
153 bool isBeyond = true;
154 // check left boundary
155 isBefore = !((!ValidRangeSet.first) || (!ValidRange.isBefore(_value)));
156// if (isBefore)
157// LOG(0, "INFO: " << _value << " is before " << ValidRange.first << ".");
158 // check right boundary
159 isBeyond = !((!ValidRangeSet.last) || (!ValidRange.isBeyond(_value)) || (_value == ValidRange.last));
160// if (isBeyond)
161// LOG(0, "INFO: " << _value << " is beyond " << ValidRange.last << ".");
162 return (!isBefore) && (!isBeyond);
163}
164
165
166/** Sets the value.
167 *
168 * We check for its validity, otherwise we throw an Assert::AssertionFailure.
169 *
170 * @param _value const reference of value to set
171 */
172template <class T>
173void ContinuousValue<T>::setValue(const T &_value)
174{
175 ASSERT(isValidValue(_value),
176 "ContinuousValue<T>::setValue() - trying to set invalid value "+toString(_value)+".");
177 if (!ValueSet)
178 ValueSet = true;
179 value = _value;
180}
181
182/** Getter fot the set value.
183 *
184 * We check whether it has been set, otherwise we throw an Assert::AssertionFailure.
185 *
186 * @return set value
187 */
188template <class T>
189const T &ContinuousValue<T>::getValue() const
190{
191 ASSERT(ValueSet,
192 "ContinuousValue<T>::get() - value has never been set.");
193 return value;
194}
195
196#endif /* CONTINUOUSVALUE_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.