atomicrex  0.1
An advanced atomistic model building tool
FitJob.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 "../properties/FitProperty.h"
26 #include "../properties/DerivedProperty.h"
27 #include "FitGroup.h"
28 #include "../util/xml/XMLUtilities.h"
29 
30 namespace atomicrex {
31 
32 class Potential; // Defined in Potential.h
33 class AtomicStructure; // Defined in AtomicStructure.h
34 class DegreeOfFreedom; // Defined in DegreeOfFreedom.h
35 class Minimizer; // Defined in Minimizer.h
36 
37 class FitJob
38 {
39 public:
40 
46  };
47 
48 public:
49 
51  FitJob();
52 
54  ~FitJob();
55 
57  const FPString& name() const { return _name; }
58 
60  double getDefaultPropertyTolerance(FitProperty* property);
61 
63  int numAtomTypes() const { return _natomtypes; }
64 
66  bool addAtomType(const FPString& name, double mass = 0.0, int atomicNumber = 0) {
67  if(name.empty() || find(_atomTypeNames.begin(), _atomTypeNames.end(), name) != _atomTypeNames.end())
68  return false;
69  _atomTypeNames.push_back(name);
70  _atomTypeMasses.push_back(mass);
71  _atomTypeAtomicNumbers.push_back(atomicNumber);
72  _natomtypes = (int)_atomTypeNames.size();
73  return true;
74  }
75 
77  const FPString& atomTypeName(int i) const {
78  BOOST_ASSERT(_natomtypes == _atomTypeNames.size());
79  BOOST_ASSERT(i >= 0 && i < _atomTypeNames.size());
80  return _atomTypeNames[i];
81  }
82 
85  double atomTypeMass(int i) const {
86  BOOST_ASSERT(_natomtypes == _atomTypeMasses.size());
87  BOOST_ASSERT(i >= 0 && i < _atomTypeMasses.size());
88  return _atomTypeMasses[i];
89  }
90 
93  int atomTypeAtomicNumber(int i) const {
94  BOOST_ASSERT(_natomtypes == _atomTypeAtomicNumbers.size());
95  BOOST_ASSERT(i >= 0 && i < _atomTypeAtomicNumbers.size());
96  return _atomTypeAtomicNumbers[i];
97  }
98 
100  int atomTypeIndex(const FPString& s) const {
101  auto iter = std::find(_atomTypeNames.begin(), _atomTypeNames.end(), s);
102  BOOST_ASSERT(iter != _atomTypeNames.end());
103  return iter - _atomTypeNames.begin();
104  }
105 
107  double maximumCutoff() const { return _maximumCutoff; }
108 
110  const std::vector<Potential*>& potentials() const { return _potentials; }
111 
113  const std::vector<AtomicStructure*>& structures() const { return _structures; }
114 
117  const std::vector<AtomicStructure*>& referencedStructures() const { return _referencedStructures; }
118 
120  void addReferencedStructure(AtomicStructure* structure);
121 
124  const std::vector<AtomicStructure*>& unreferencedStructures() const { return _unreferencedStructures; }
125 
127  void addUnreferencedStructure(AtomicStructure* structure) { _unreferencedStructures.push_back(structure); }
128 
130  const std::vector<DerivedProperty*>& derivedProperties() { return _derivedProperties; }
131 
133  const std::vector<FitProperty*>& fitProperties() { return _propertyList; }
134 
136  FitGroup& rootGroup() { return _rootGroup; }
137 
141 
145 
149 
151  const Matrix3* lookupDeformation(const FPString& deformationId) const {
152  auto d = _deformations.find(deformationId);
153  if(d != _deformations.end()) return &d->second;
154  else return nullptr;
155  }
156 
158  bool fittingEnabled() const { return (bool)_fitMinimizer; }
159 
161  std::shared_ptr<Minimizer> fitMinimizer() { return _fitMinimizer; }
162 
164  void performFitting();
165 
170  void printPotentialDOFList(bool printFullDOFList);
171 
175  void printFittingProperties();
176 
178  bool potentialValidationEnabled() const { return _potentialValidationEnabled; }
179 
181  void validatePotentials();
182 
184  int numActiveDOF() const { return _numActiveDOF; }
185 
190  void packDOF(double* destination);
191 
196  void packDOF(std::vector<double>& destination) {
197  BOOST_ASSERT(destination.size() == numActiveDOF());
198  packDOF(destination.data());
199  }
200 
205  void unpackDOF(const double* source);
206 
211  void unpackDOF(const std::vector<double>& source) {
212  BOOST_ASSERT(source.size() == numActiveDOF());
213  unpackDOF(source.data());
214  }
215 
216 public:
217 
222  void parse(const FPString& file);
223 
231  int parseAtomTypeAttribute(XML::Element element, const char* attributeName);
232 
240  int parseAtomTypeElement(XML::Element element, const char* elementName);
241 
249  std::vector<int> parseAtomTypesAttribute(XML::Element element, const char* attributeName);
250 
258  ScalarFitProperty* parseScalarFitPropertyAttribute(XML::Element element, const char* attributeName);
259 
265 
267  void prepareFitting();
268 
269 
270 private:
271 
272 
274  void assignAbsoluteWeights();
275 
281  void addStructure(AtomicStructure* structure);
282 
289  void addDerivedProperty(DerivedProperty* derivedProperty);
290 
291 
292 
293 private:
294 
299  void parseAtomTypesElement(XML::Element typesElement);
300 
305  void parsePotentialsElement(XML::Element potentialsElement);
306 
311  void parseFittingElement(XML::Element fittingElement);
312 
317  void parseDeformationsElement(XML::Element deformationsElement);
318 
319 private:
320 
322  FPString _name;
323 
325  int _natomtypes = 0;
326 
329  std::vector<FPString> _atomTypeNames;
330 
333  std::vector<double> _atomTypeMasses;
334 
337  std::vector<int> _atomTypeAtomicNumbers;
338 
340  double _maximumCutoff = 0.0;
341 
343  std::vector<Potential*> _potentials;
344 
346  FitGroup _rootGroup;
347 
349  std::vector<AtomicStructure*> _structures;
350 
353  std::vector<AtomicStructure*> _referencedStructures;
354 
357  std::vector<AtomicStructure*> _unreferencedStructures;
358 
360  std::vector<DerivedProperty*> _derivedProperties;
361 
363  std::vector<DegreeOfFreedom*> _completePotentialDOFList;
364 
366  std::vector<DegreeOfFreedom*> _activePotentialDOFList;
367 
370  size_t _numActiveDOF = 0;
371 
373  std::vector<double> _currentState;
374 
376  int _intervalForPrinting;
377 
379  std::vector<FitProperty*> _propertyList;
380 
382  std::shared_ptr<Minimizer> _fitMinimizer;
383 
385  std::map<FPString, Matrix3> _deformations;
386 
389  bool _potentialValidationEnabled = false;
390 
392  TolerancePreset _tolerancePreset = Balanced;
393 
394  friend class FitGroup;
395  friend class AtomicStructure;
396  friend class DerivedProperty;
397 };
398 
399 } // End of namespace
400 
401 #include "../structures/AtomicStructure.h"
402 #include "../potentials/Potential.h"
Base class for maintaining structures.
Definition: AtomicStructure.h:42
void parse(const FPString &file)
Parses the job description from the XML file.
Definition: FitJob.cpp:86
Definition: FitGroup.h:36
void addUnreferencedStructure(AtomicStructure *structure)
Add a structure to the list of unreferenced structures.
Definition: FitJob.h:127
bool addAtomType(const FPString &name, double mass=0.0, int atomicNumber=0)
Adds a new atom type to the job.
Definition: FitJob.h:66
double getDefaultPropertyTolerance(FitProperty *property)
Returns the default tolerance for the given property based on the selected tolerance preset...
Definition: FitJob.cpp:371
All tolerances are 1.0.
Definition: FitJob.h:43
int numAtomTypes() const
Returns the number of atom types used in this job.
Definition: FitJob.h:63
Indicates that the norm selected by the user should be used.
Definition: FitProperty.h:44
const std::vector< Potential * > & potentials() const
Returns the list of all potentials used in this job.
Definition: FitJob.h:110
void unpackDOF(const std::vector< double > &source)
Unpacks the values of all degrees of freedom from a linear vector.
Definition: FitJob.h:211
double maximumCutoff() const
Returns the maximum cutoff radius of all potentials used by this job.
Definition: FitJob.h:107
int atomTypeIndex(const FPString &s) const
Returns the index of the atom type.
Definition: FitJob.h:100
DerivedProperty * getDerivedProperty(const FPString &id)
Definition: FitJob.cpp:463
Specifies a balanced unit based assignment of tolerance parameters.
Definition: FitJob.h:44
FitJob()
Constructor.
Definition: FitJob.cpp:49
void prepareFitting()
Prepares a list of all degrees of freedom and properties that are to be fitted.
Definition: Fitting.cpp:37
ScalarFitProperty * parseScalarFitPropertyAttribute(XML::Element element, const char *attributeName)
Returns the ScalarFitProperty specified in the given element attribute.
Definition: FitJob.cpp:401
Definition: DerivedProperty.h:38
void performFitting()
Performs the actual fitting by minimizing the residual.
Definition: Fitting.cpp:95
Definition: FitProperty.h:38
std::shared_ptr< Minimizer > fitMinimizer()
Returns the minimizer.
Definition: FitJob.h:161
void addReferencedStructure(AtomicStructure *structure)
Add a structure to the list of referenced structures.
Definition: FitJob.cpp:495
const std::vector< FitProperty * > & fitProperties()
Returns the global list of active properties which are included in the fit and which need to be compu...
Definition: FitJob.h:133
int parseAtomTypeElement(XML::Element element, const char *elementName)
Returns the atom type index of the species specified in the given element.
Definition: FitJob.cpp:326
double calculateResidual(FitProperty::ResidualNorm norm=FitProperty::UserDefined)
Calculates the total residual, i.e. the objective function to be minimized during fitting...
Definition: Fitting.cpp:248
ScalarFitProperty * getScalarFitProperty(const FPString &id)
Definition: FitJob.cpp:426
void printPotentialDOFList(bool printFullDOFList)
Outputs the current values of all DOFs of the potentials.
Definition: Fitting.cpp:170
Specifies a unit based assignment that emphasizes energies.
Definition: FitJob.h:45
This file collects the definition of classes that define various simple crystal structures.
Definition: Atomicrex.h:67
TolerancePreset
Specifies presets for tolerance parameters.
Definition: FitJob.h:42
std::vector< int > parseAtomTypesAttribute(XML::Element element, const char *attributeName)
Returns the list of atom types corresponding to the species specified in the given element attribute...
Definition: FitJob.cpp:340
FitGroup & rootGroup()
Returns the root node of the property group tree.
Definition: FitJob.h:136
std::string FPString
The default string type used throughout the code:
Definition: Atomicrex.h:70
const Matrix3 * lookupDeformation(const FPString &deformationId) const
Returns the deformation with the given id. Returns NULL if no such deformation has been defined...
Definition: FitJob.h:151
void printFittingProperties()
Outputs the list of properties being fitted.
Definition: Fitting.cpp:206
const std::vector< AtomicStructure * > & structures() const
Returns the list of all atomic structures used for fitting.
Definition: FitJob.h:113
bool fittingEnabled() const
Returns whether fitting will be performed.
Definition: FitJob.h:158
Definition: XMLUtilities.h:69
int atomTypeAtomicNumber(int i) const
Definition: FitJob.h:93
Definition: FitJob.h:37
~FitJob()
Destructor.
Definition: FitJob.cpp:56
const FPString & atomTypeName(int i) const
Returns the name of the i-th atom type.
Definition: FitJob.h:77
const std::vector< AtomicStructure * > & unreferencedStructures() const
Definition: FitJob.h:124
void validatePotentials()
Performs a check of the energy/force calculation routines using numerical differentiation.
Definition: Validation.cpp:97
const std::vector< DerivedProperty * > & derivedProperties()
Returns the list of all derived properties.
Definition: FitJob.h:130
void packDOF(std::vector< double > &destination)
Packs the values of all degrees of freedom into a linear vector, which can be passed to the minimizer...
Definition: FitJob.h:196
const FPString & name() const
Returns the user-defined name of this job.
Definition: FitJob.h:57
AtomicStructure * getAtomicStructure(const FPString &id)
Definition: FitJob.cpp:451
Definition: FitProperty.h:126
int numActiveDOF() const
Returns the total number of active degrees of freedom.
Definition: FitJob.h:184
bool potentialValidationEnabled() const
Returns whether validation of the force/energy calculation routines has been requested by the user...
Definition: FitJob.h:178
int parseAtomTypeAttribute(XML::Element element, const char *attributeName)
Returns the atom type index of the species specified in the given element attribute.
Definition: FitJob.cpp:312
ResidualNorm
Specifies how residual contributions are calculated.
Definition: FitProperty.h:43
void unpackDOF(const double *source)
Unpacks the values of all degrees of freedom from a linear vector.
Definition: Fitting.cpp:236
double atomTypeMass(int i) const
Definition: FitJob.h:85
void packDOF(double *destination)
Packs the values of all degrees of freedom into a linear vector, which can be passed to the minimizer...
Definition: Fitting.cpp:226
const std::vector< AtomicStructure * > & referencedStructures() const
Definition: FitJob.h:117