atomicrex  0.1
An advanced atomistic model building tool
DegreeOfFreedom.h
1 //
3 // Copyright (C) 2017, Alexander Stukowski and Paul Erhart
4 //
5 // This file is part of atomicrex.
6 //
7 // Atomicrex is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Atomicrex is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //
21 
22 #pragma once
23 
24 #include "../Atomicrex.h"
25 #include "../minimizers/Minimizer.h"
26 #include "../util/xml/XMLUtilities.h"
27 
28 namespace atomicrex {
29 
30 class FitObject;
31 class AtomicStructure;
32 class Potential;
33 
47 {
48 public:
49 
55  DegreeOfFreedom(const FPString& id = FPString(), const FPString& tag = FPString()) : _id(id), _tag(tag), _fitEnabled(false),
56  _relax(false), _object(nullptr), _resetBeforeRelax(true) {}
57 
59  virtual ~DegreeOfFreedom() {}
60 
62  const FPString& id() const { return _id; }
63 
65  void setId(const FPString& id) { _id = id; }
66 
68  const FPString& tag() const { return _tag; }
69 
71  void setTag(const FPString& tag) { _tag = tag; }
72 
74  bool fitEnabled() const { return _fitEnabled; }
75 
77  void setFitEnabled(bool enable) { _fitEnabled = enable; }
78 
80  bool relax() const { return _relax; }
81 
83  void setRelax(bool enable) { _relax = enable; }
84 
86  FitObject* object() const { return _object; }
87 
92  virtual int numScalars() = 0;
93 
95  virtual void exportValue(double*& dst) = 0;
96 
98  virtual void importValue(const double*& src) = 0;
99 
101  virtual bool hasBoundConstraints() const { return false; }
102 
104  virtual bool satisfiesBoundConstraints() const { return true; }
105 
107  virtual void getBoundConstraints(std::vector<Minimizer::BoundConstraints>::iterator& types, std::vector<double>::iterator& lowerBounds, std::vector<double>::iterator& upperBounds) {
108  std::fill(types, types + numScalars(), Minimizer::NO_BOUNDS);
109  types += numScalars();
110  lowerBounds += numScalars();
111  upperBounds += numScalars();
112  }
113 
115  virtual void print(MsgLogger& stream) {}
116 
119  bool resetBeforeRelax() const { return _resetBeforeRelax; }
120 
123  void setResetBeforeRelax(bool enableReset) { _resetBeforeRelax = enableReset; }
124 
126  virtual void reset() {}
127 
128 public:
129 
131  virtual void parseFit(XML::Element fitElement);
132 
134  virtual void parseRelax(XML::Element relaxElement);
135 
138 
140  virtual XML::OElement generateXMLFitDefinition() const;
141 
142 private:
143 
145  FPString _id;
146 
148  FPString _tag;
149 
151  bool _fitEnabled;
152 
154  bool _relax;
155 
158  bool _resetBeforeRelax;
159 
161  FitObject* _object;
162 
163  friend class FitObject;
164 };
165 
170 {
171 public:
172 
174  ScalarDOF(const FPString& id = FPString(), double defaultValue = 0,
175  double minValue = -std::numeric_limits<double>::infinity(),
176  double maxValue = std::numeric_limits<double>::infinity()) : DegreeOfFreedom(id),
177  _value(defaultValue),
178  _initialValue(defaultValue),
179  _minValue(minValue),
180  _maxValue(maxValue),
181  _masterDOF(nullptr) {}
182 
184  operator double() const { return _value; }
185 
187  double getValue() { return _value; }
188 
190  ScalarDOF& operator=(double value);
191 
193  void setValue(double value) { _value = value; };
194 
196  double* getValuePointer() { return &_value; }
197 
199  void setInitialValue(double value) { _initialValue = value; *this = value; }
200 
202  double initialValue() const { return _initialValue; }
203 
205  virtual void reset() override { DegreeOfFreedom::reset(); *this = _initialValue; }
206 
208  virtual int numScalars() override { return 1; }
209 
211  virtual void exportValue(double*& dst) override;
212 
214  virtual void importValue(const double*& src) override;
215 
217  virtual void print(MsgLogger& stream) override;
218 
220  double lowerBound() const { return _minValue; }
221 
223  bool hasLowerBound() const { return _minValue != -std::numeric_limits<double>::infinity(); }
224 
226  void setLowerBound(double lowerBound) { _minValue = lowerBound; }
227 
229  double upperBound() const { return _maxValue; }
230 
232  bool hasUpperBound() const { return _maxValue != std::numeric_limits<double>::infinity(); }
233 
235  void setUpperBound(double upperBound) { _maxValue = upperBound; }
236 
238  virtual bool hasBoundConstraints() const override { return hasLowerBound() || hasUpperBound(); }
239 
241  virtual bool satisfiesBoundConstraints() const override {
242  if(hasLowerBound() && _value < lowerBound()) return false;
243  if(hasUpperBound() && _value > upperBound()) return false;
244  return true;
245  };
246 
248  virtual void getBoundConstraints(std::vector<Minimizer::BoundConstraints>::iterator& types, std::vector<double>::iterator& lowerBounds, std::vector<double>::iterator& upperBounds) override {
249  if(hasLowerBound()) {
250  if(hasUpperBound())
251  *types++ = Minimizer::BOTH_BOUNDS;
252  else
253  *types++ = Minimizer::LOWER_BOUNDS;
254  }
255  else if(hasUpperBound())
256  *types++ = Minimizer::UPPER_BOUNDS;
257  else
258  *types++ = Minimizer::NO_BOUNDS;
259  *lowerBounds++ = _minValue;
260  *upperBounds++ = _maxValue;
261  }
262 
268  bool setEqualTo(ScalarDOF* masterDOF);
269 
270 public:
271 
273  virtual void parseFit(XML::Element fitElement) override;
274 
276  virtual void parseRelax(XML::Element relaxElement) override;
277 
279  void parseSetEqualToAttribute(XML::Element element);
280 
282  virtual XML::OElement generateXMLValueDefinition() const override;
283 
285  virtual XML::OElement generateXMLFitDefinition() const override;
286 
287 private:
288 
290  double _value;
291 
293  double _initialValue;
294 
296  double _minValue;
297 
299  double _maxValue;
300 
302  ScalarDOF* _masterDOF;
303 
305  std::vector<std::function<void(ScalarDOF&)>> _changeListeners;
306 };
307 
308 } // End of namespace
void setResetBeforeRelax(bool enableReset)
Definition: DegreeOfFreedom.h:123
Definition: Logger.h:37
double upperBound() const
Returns the upper bound if this DOF has a constraint applied to it.
Definition: DegreeOfFreedom.h:229
void setId(const FPString &id)
Sets the identifier of this degree of freedom.
Definition: DegreeOfFreedom.h:65
virtual bool satisfiesBoundConstraints() const
Returns whether this DOF satisfies the bound constraints applied to it.
Definition: DegreeOfFreedom.h:104
double getValue()
Returns the current value of the DoF.
Definition: DegreeOfFreedom.h:187
virtual void reset()
Resets the value of the DOF to what was specified in the job file.
Definition: DegreeOfFreedom.h:126
virtual XML::OElement generateXMLFitDefinition() const
Produces an XML representation of the DOF&#39;s fit settings.
Definition: DegreeOfFreedom.cpp:158
virtual int numScalars() override
Returns the number of scalar degrees of freedom this DOF is composed of.
Definition: DegreeOfFreedom.h:208
void setFitEnabled(bool enable)
Sets whether this DOF should be varied during the fitting process.
Definition: DegreeOfFreedom.h:77
const FPString & id() const
Return the identifier of this degree of freedom.
Definition: DegreeOfFreedom.h:62
virtual bool hasBoundConstraints() const override
Returns whether this DOF has any bound constraints applied to it.
Definition: DegreeOfFreedom.h:238
virtual void print(MsgLogger &stream)
Outputs the current value of the DOF.
Definition: DegreeOfFreedom.h:115
virtual void getBoundConstraints(std::vector< Minimizer::BoundConstraints >::iterator &types, std::vector< double >::iterator &lowerBounds, std::vector< double >::iterator &upperBounds) override
Lets the DOF write its constraints information to the given linear arrays.
Definition: DegreeOfFreedom.h:248
virtual void parseRelax(XML::Element relaxElement)
Parse the contents of the <relax> element in the job file.
Definition: DegreeOfFreedom.cpp:139
void setLowerBound(double lowerBound)
Sets a lower bound constraint on this DOF.
Definition: DegreeOfFreedom.h:226
virtual void exportValue(double *&dst)=0
Lets the DOF export its current value(s) into the given value array.
bool hasUpperBound() const
Returns whether this DOF has an upper bound constraint applied to it.
Definition: DegreeOfFreedom.h:232
virtual bool hasBoundConstraints() const
Returns whether this DOF has any bound constraints applied to it.
Definition: DegreeOfFreedom.h:101
virtual bool satisfiesBoundConstraints() const override
Returns whether this DOF satisfies the bound constraints applied to it.
Definition: DegreeOfFreedom.h:241
virtual int numScalars()=0
Returns the number of scalar degrees of freedom this DOF is composed of.
void setInitialValue(double value)
Sets the initial value of this DOF. This also changes the current value to the same value...
Definition: DegreeOfFreedom.h:199
virtual void getBoundConstraints(std::vector< Minimizer::BoundConstraints >::iterator &types, std::vector< double >::iterator &lowerBounds, std::vector< double >::iterator &upperBounds)
Lets the DOF write its constraints information to the given linear arrays.
Definition: DegreeOfFreedom.h:107
virtual XML::OElement generateXMLValueDefinition() const
Produces an XML representation of the DOF&#39;s current value.
Definition: DegreeOfFreedom.cpp:150
ScalarDOF(const FPString &id=FPString(), double defaultValue=0, double minValue=-std::numeric_limits< double >::infinity(), double maxValue=std::numeric_limits< double >::infinity())
Constructor.
Definition: DegreeOfFreedom.h:174
bool relax() const
Returns whether this DOF should be relaxed during each iteration when fitting.
Definition: DegreeOfFreedom.h:80
virtual void parseFit(XML::Element fitElement)
Parse the contents of the <fit> element in the job file.
Definition: DegreeOfFreedom.cpp:127
void setValue(double value)
Assigns a new value to the DOF.
Definition: DegreeOfFreedom.h:193
This file collects the definition of classes that define various simple crystal structures.
Definition: Atomicrex.h:67
A simple scalar degree of freedom that consists of a single value.
Definition: DegreeOfFreedom.h:169
DegreeOfFreedom(const FPString &id=FPString(), const FPString &tag=FPString())
Constructor.
Definition: DegreeOfFreedom.h:55
virtual ~DegreeOfFreedom()
Virtual destructor.
Definition: DegreeOfFreedom.h:59
bool hasLowerBound() const
Returns whether this DOF has a lower bound constraint applied to it.
Definition: DegreeOfFreedom.h:223
virtual void importValue(const double *&src)=0
Lets the DOF import its value(s) from the given value array.
void setTag(const FPString &tag)
Sets the tag on this DOF.
Definition: DegreeOfFreedom.h:71
std::string FPString
The default string type used throughout the code:
Definition: Atomicrex.h:70
Definition: XMLUtilities.h:226
Definition: XMLUtilities.h:69
double initialValue() const
Returns the initial value of this DOF.
Definition: DegreeOfFreedom.h:202
Definition: FitObject.h:42
double * getValuePointer()
Returns a pointer to the internal storage field of this DoF.
Definition: DegreeOfFreedom.h:196
FitObject * object() const
Returns a pointer to the potential or structure to which this DOF belongs.
Definition: DegreeOfFreedom.h:86
double lowerBound() const
Returns the lower bound if this DOF has a constraint applied to it.
Definition: DegreeOfFreedom.h:220
Base class for all degrees of freedom a potential or a structure may have.
Definition: DegreeOfFreedom.h:46
void setUpperBound(double upperBound)
Sets an upper bound constraint on this DOF.
Definition: DegreeOfFreedom.h:235
const FPString & tag() const
Returns the tag on this DOF.
Definition: DegreeOfFreedom.h:68
virtual void reset() override
Resets the value of the DOF to what was given in the job file.
Definition: DegreeOfFreedom.h:205
bool resetBeforeRelax() const
Definition: DegreeOfFreedom.h:119
bool fitEnabled() const
Returns whether this DOF should be varied during the fitting process.
Definition: DegreeOfFreedom.h:74
void setRelax(bool enable)
Sets whether this DOF is being relaxed during each iteration when fitting.
Definition: DegreeOfFreedom.h:83