atomicrex  0.1
An advanced atomistic model building tool
AtomicStructure.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 "NeighborList.h"
26 #include "../job/FitObject.h"
27 #include "../properties/FitProperty.h"
28 #include "../properties/AtomVectorProperty.h"
29 #include "../dof/AtomCoordinatesDOF.h"
30 
31 namespace atomicrex {
32 
33 class FitJob; // Declared in FitJob.h
34 
42 class AtomicStructure : public FitObject
43 {
44 public:
45 
49  enum DirtyFlags {
51  NEIGHBOR_LISTS = (1<<0),
53  ATOM_POSITIONS = (1<<1),
55  STRUCTURE_DOF = (1<<4),
56  };
57 
61  enum OutputFormat {
62  POSCAR,
63  LAMMPS
64  };
65 
66 
67 public:
68 
69  /*************************************** Constructor / destructor ***********************************/
70 
71  AtomicStructure(const FPString& id, FitJob* job);
72 
73  /*************************************** Simulation cell geometry ************************************/
74 
81  void setupSimulationCell(const Matrix3& cellVectors, const Point3& cellOrigin = Point3::Origin(), const std::array<bool,3>& pbc = std::array<bool,3>{{true,true,true}});
82 
87  void deformSimulationCell(const Matrix3& deformation);
88 
93  const Matrix3& simulationCell() const { return _simulationCell; }
94 
99  const Point3& simulationCellOrigin() const { return _simulationCellOrigin; }
100 
105  const Matrix3& reciprocalSimulationCell() const { return _reciprocalSimulationCell; }
106 
112  bool hasPBC(size_t dimension) const { return _pbc[dimension]; }
113 
118  const std::array<bool,3>& pbc() const { return _pbc; }
119 
120  /*************************************** Coordinate utility functions ************************************/
121 
123  Vector3I periodicImage(const Point3& p) const;
124 
126  Point3 wrapPoint(Point3 p) const;
127 
129  Point3 wrapReducedPoint(Point3 p) const;
130 
132  Point3 reducedToAbsolute(const Point3& reducedPoint) const { return simulationCellOrigin() + (simulationCell() * (reducedPoint - Point3::Origin())); }
133 
135  Point3 absoluteToReduced(const Point3& worldPoint) const { return Point3::Origin() + (reciprocalSimulationCell() * (worldPoint - simulationCellOrigin())); }
136 
138  Vector3 reducedToAbsolute(const Vector3& reducedVec) const { return simulationCell() * reducedVec; }
139 
141  Vector3 absoluteToReduced(const Vector3& worldVec) const { return reciprocalSimulationCell() * worldVec; }
142 
143  /************************************************ Atoms ***********************************************/
144 
149  void setAtomCount(int numLocalAtoms);
150 
152  int numLocalAtoms() const { return _numLocalAtoms; }
153 
155  int numGhostAtoms() const { return _numGhostAtoms; }
156 
158  int numAtoms() const { return _numAtoms; }
159 
161  const std::vector<Point3>& atomPositions() const { return _atomPositions; }
162 
164  std::vector<Point3>& atomPositions() { return _atomPositions; }
165 
170  void setAtomPositions(const std::vector<Point3>& newPositions) {
171  BOOST_ASSERT(newPositions.size() == numLocalAtoms());
172  std::copy(newPositions.begin(), newPositions.end(), _atomPositions.begin());
174  }
175 
177  const std::vector<Point3>& atomDisplacements() const { return _atomDisplacements; }
178 
180  std::vector<Point3>& atomDisplacements() { return _atomDisplacements; }
181 
183  const std::vector<Point3>& atomInitialPositions() const { return _atomInitialPositions; }
184 
186  int atomType(int i) const { return _atomTypes[i]; }
187 
189  const std::vector<int>& atomTypes() const { return _atomTypes; }
190 
192  std::vector<int>& atomTypes() { return _atomTypes; }
193 
195  const std::vector<int>& atomTags() const { return _atomTags; }
196 
198  std::vector<int>& atomTags() { return _atomTags; }
199 
201  const std::vector<Vector3>& atomForces() const { return _atomForcesProperty.values(); }
202 
204  std::vector<Vector3>& atomForces() { return _atomForcesProperty.values(); }
205 
208 
210  const std::vector< std::pair<int,int> >& forwardMapping() const { return _forwardMapping; }
211 
213  const std::vector<int>& reverseMapping() const { return _reverseMapping; }
214 
216  template<typename T>
217  T* perAtomData() { return _perAtomPotentialData.empty() ? nullptr : reinterpret_cast<T*>(&_perAtomPotentialData.front()); }
218 
219  // Returns the neighbor list built for this structure and the given potential.
220  NeighborList& neighborList(const Potential* potential) {
221  auto nl = _neighborLists.find(potential);
222  BOOST_ASSERT(nl != _neighborLists.end());
223  return nl->second;
224  }
225 
226  /************************************ Updating and modifying the structure ***************************************/
227 
229  void setDirty(DirtyFlags flags) { _dirtyFlags |= flags; }
230 
232  bool isDirty(DirtyFlags parts) const { return (_dirtyFlags & parts) != 0; }
233 
235  void clearDirty(DirtyFlags parts) { _dirtyFlags &= ~parts; }
236 
241  virtual void dofValueChanged(DegreeOfFreedom& dof) override {
244  }
245 
249  virtual void updateStructure();
250 
251  /******************************************** Property calculation ***********************************************/
252 
257  virtual bool computeProperties(bool isFitting) override;
258 
267  double computeEnergy(bool computeForces, bool isFitting, bool suppressRelaxation = false);
268 
273  bool relax(bool isFitting);
274 
275  /******************************************** Input/output ***********************************************/
276 
282  void writeToDumpFile(const FPString& filename, bool includeGhostAtoms = false) const;
283 
289  void writeToPoscarFile(const FPString& filename, bool includeGhostAtoms = false) const;
290 
294  void writeToFile();
295 
296  /************************************* Access to calculated quantities *****************************************/
297 
299  double totalEnergy() const { return _totalEnergy; }
300 
302  const std::array<double,6>& virial() const { return _virial; }
303 
308  std::array<double,6>& virial() { return _virial; }
309 
314  double pressureTensor(int voigtIndex) const { BOOST_ASSERT(voigtIndex >= 0 && voigtIndex < 6); return _pressureTensorProperty[voigtIndex]; }
315 
320  double pressure() const { return _pressureProperty; }
321 
322 public:
323 
325  virtual void parse(XML::Element structureElement) override;
326 
327 private:
328 
333  void computeElasticConstants(bool isFitting);
334 
336  int _dirtyFlags;
337 
339  Matrix3 _simulationCell;
341  Point3 _simulationCellOrigin;
343  Matrix3 _reciprocalSimulationCell;
345  std::array<bool,3> _pbc;
346 
348  int _numLocalAtoms;
350  int _numGhostAtoms;
352  int _numAtoms;
354  double _skinThickness;
356  std::vector<Point3> _atomPositions;
358  std::vector<Point3> _lastNeighborUpdatePositions;
360  Matrix3 _lastSimulationCell;
362  Vector3I _ghostAtomImages;
364  std::vector<Point3> _atomDisplacements;
366  std::vector<Point3> _atomInitialPositions;
368  std::vector<int> _atomTypes;
370  std::vector<int> _atomTags;
372  std::vector<std::pair<int,int>> _forwardMapping;
374  std::vector<int> _reverseMapping;
376  std::vector<unsigned char> _perAtomPotentialData;
378  std::vector<Vector3> _ghostAtomPBCImages;
379 
381  std::map<const Potential*, NeighborList> _neighborLists;
382 
384  double _totalEnergy;
385 
387  int _lastUpdate = 0;
388 
390  FPString _outputFile = "";
391  OutputFormat _outputFormat;
392 
394  std::array<double,6> _virial;
395 
396 protected:
397 
402 
407 
412 
417 
420 
423 
424 };
425 
426 } // End of namespace
const std::vector< int > & atomTypes() const
Returns the array of types of all atoms in the cell (const version).
Definition: AtomicStructure.h:189
int atomType(int i) const
Returns the type of a i-th atom.
Definition: AtomicStructure.h:186
Base class for maintaining structures.
Definition: AtomicStructure.h:42
double pressureTensor(int voigtIndex) const
Returns a component of the pressure tensor.
Definition: AtomicStructure.h:314
const Matrix3 & reciprocalSimulationCell() const
The inverse of the simulation cell matrix used to transform absolute coordinates to reduced coordinat...
Definition: AtomicStructure.h:105
neighbor lists require updating
Definition: AtomicStructure.h:51
void setAtomPositions(const std::vector< Point3 > &newPositions)
Sets the array of positions of all atoms in the structure cell.
Definition: AtomicStructure.h:170
Point3 reducedToAbsolute(const Point3 &reducedPoint) const
Converts a point given in reduced cell coordinates to a point in absolute coordinates.
Definition: AtomicStructure.h:132
void deformSimulationCell(const Matrix3 &deformation)
Applies an affine transformation to the cell and the atoms.
Definition: AtomicStructure.cpp:118
void setDirty(DirtyFlags flags)
Marks parts of the structure that must be updated before the next energy calculation.
Definition: AtomicStructure.h:229
double computeEnergy(bool computeForces, bool isFitting, bool suppressRelaxation=false)
Computes the total energy and optionally the forces for this structure.
Definition: AtomicStructure.cpp:528
void writeToPoscarFile(const FPString &filename, bool includeGhostAtoms=false) const
Exports the structure to a POSCAR file.
Definition: AtomicStructure.cpp:796
virtual bool computeProperties(bool isFitting) override
Computes all enabled properties of the structures.
Definition: AtomicStructure.cpp:400
Definition: NeighborList.h:58
Point3 wrapReducedPoint(Point3 p) const
Wraps a point given in reduced coordinated to be inside the simulation cell.
Definition: AtomicStructure.cpp:162
bool isDirty(DirtyFlags parts) const
Tests whether certain parts of the structure must be updated before the next energy calculation...
Definition: AtomicStructure.h:232
std::vector< int > & atomTypes()
Returns the array of types of all atoms in the cell (non-const version).
Definition: AtomicStructure.h:192
Vector3 reducedToAbsolute(const Vector3 &reducedVec) const
Converts a vector given in reduced cell coordinates to a vector in absolute coordinates.
Definition: AtomicStructure.h:138
const std::vector< Point3 > & atomInitialPositions() const
Returns the initial positions of the all real atoms they had at the beginning of the job...
Definition: AtomicStructure.h:183
const std::vector< Point3 > & atomPositions() const
Returns the array of positions of all atoms (including ghosts) in the structure (const version)...
Definition: AtomicStructure.h:161
const std::array< double, 6 > & virial() const
Returns the virial tensor computed by the force routine.
Definition: AtomicStructure.h:302
const std::vector< Vector3 > & values() const
Returns the current computed values of the vector property.
Definition: AtomVectorProperty.h:72
std::vector< Point3 > & atomPositions()
Returns the array of positions of all atoms (including ghosts) in the structure cell (non-const versi...
Definition: AtomicStructure.h:164
const std::array< bool, 3 > & pbc() const
Returns the periodic boundary condition flags.
Definition: AtomicStructure.h:118
int numLocalAtoms() const
Returns the number of (real) atoms in the structure cell.
Definition: AtomicStructure.h:152
AtomCoordinatesDOF _atomCoordinatesDOF
This DOF encapsulates the atomic degrees of freedom.
Definition: AtomicStructure.h:422
Vector3I periodicImage(const Point3 &p) const
Determines the periodic image of the cell the given point is located in.
Definition: AtomicStructure.cpp:135
T * perAtomData()
Returns a pointer to the memory buffer that stores temporary per-atom data used by the potential rout...
Definition: AtomicStructure.h:217
virtual void dofValueChanged(DegreeOfFreedom &dof) override
This callback function is called by the DOFs of the structure each time when their values changes...
Definition: AtomicStructure.h:241
DirtyFlags
Flags that indicate which parts of the structure need to be updated prior to the next energy calculat...
Definition: AtomicStructure.h:49
bool relax(bool isFitting)
Relaxes the structural degrees of freedom such that the total energy is minimized.
Definition: AtomicStructure.cpp:440
AtomVectorProperty & atomForcesProperty()
Returns the property for the atomic force vectors.
Definition: AtomicStructure.h:207
const std::vector< int > & reverseMapping() const
Returns an array of integers that maps the ghost atoms to the real atoms.
Definition: AtomicStructure.h:213
virtual void dofValueChanged(DegreeOfFreedom &dof)
This callback function is called by the DOFs of this fit object each time when their values changes...
Definition: FitObject.h:95
virtual void updateStructure()
Updates the structure, creates ghost atoms and builds neighbor lists.
Definition: AtomicStructure.cpp:200
ScalarFitProperty _pressureTensorProperty[6]
The components of the cell&#39;s pressure tensor.
Definition: AtomicStructure.h:409
Vector3 absoluteToReduced(const Vector3 &worldVec) const
Converts a vector given in absolute coordinates to a point in vector cell coordinates.
Definition: AtomicStructure.h:141
This DOF manages the atomic positions in a structure.
Definition: AtomCoordinatesDOF.h:37
std::vector< Vector3 > & atomForces()
Returns the array of force vectors (non-const version).
Definition: AtomicStructure.h:204
std::array< double, 6 > & virial()
Returns a reference to the virial tensor.
Definition: AtomicStructure.h:308
double pressure() const
Returns the hydrostatic part of the pressure tensor.
Definition: AtomicStructure.h:320
const std::vector< Vector3 > & atomForces() const
Returns the array of force vectors (const version).
Definition: AtomicStructure.h:201
Point3 absoluteToReduced(const Point3 &worldPoint) const
Converts a point given in absolute coordinates to a point in reduced cell coordinates.
Definition: AtomicStructure.h:135
AtomVectorProperty _atomForcesProperty
The force vector.
Definition: AtomicStructure.h:419
ScalarFitProperty _pressureProperty
One third of the trace of the cell&#39;s pressure tensor.
Definition: AtomicStructure.h:411
void clearDirty(DirtyFlags parts)
Resets the dirty flags.
Definition: AtomicStructure.h:235
This file collects the definition of classes that define various simple crystal structures.
Definition: Atomicrex.h:67
ScalarFitProperty _atomicVolumeProperty
The average volume per atom in the structure cell.
Definition: AtomicStructure.h:406
double totalEnergy() const
Returns the total potential energy of this structure after computeEnergy() has been called...
Definition: AtomicStructure.h:299
Point3 wrapPoint(Point3 p) const
Wraps a point to be inside the simulation cell if periodic boundary conditions are enabled...
Definition: AtomicStructure.cpp:146
void setAtomCount(int numLocalAtoms)
Resizes the atoms array.
Definition: AtomicStructure.cpp:177
void writeToFile()
Exports the structure to file if a filename has been provided.
Definition: AtomicStructure.cpp:780
ghost atom positions require updating
Definition: AtomicStructure.h:53
ScalarFitProperty _totalVolumeProperty
The total volume of the structure cell.
Definition: AtomicStructure.h:404
const std::vector< Point3 > & atomDisplacements() const
Returns the array of displacements of all atoms in the structure cell wrt a reference structure (cons...
Definition: AtomicStructure.h:177
bool hasPBC(size_t dimension) const
Returns whether periodic boundary conditions are enabled in the given spatial dimension.
Definition: AtomicStructure.h:112
std::string FPString
The default string type used throughout the code:
Definition: Atomicrex.h:70
int numGhostAtoms() const
Returns the number of ghost atoms in the structure cell.
Definition: AtomicStructure.h:155
void setupSimulationCell(const Matrix3 &cellVectors, const Point3 &cellOrigin=Point3::Origin(), const std::array< bool, 3 > &pbc=std::array< bool, 3 >{{true, true, true}})
Set up the simulation cell.
Definition: AtomicStructure.cpp:97
std::vector< int > & atomTags()
Returns the array of IDs of the atoms in the cell (non-const version).
Definition: AtomicStructure.h:198
Definition: XMLUtilities.h:69
Definition: FitObject.h:42
Definition: FitJob.h:37
OutputFormat
Possible formats for writing the structure to file.
Definition: AtomicStructure.h:61
virtual void parse(XML::Element structureElement) override
Parses the structure-specific parameters in the XML element in the job file.
Definition: AtomicStructure.cpp:910
ScalarFitProperty _elasticConstantProperties[21]
The elastic constants.
Definition: AtomicStructure.h:416
const Matrix3 & simulationCell() const
Returns global simulation cell matrix.
Definition: AtomicStructure.h:93
FitJob * job() const
Returns a pointer to the job to which this object belongs.
Definition: FitObject.h:150
const Point3 & simulationCellOrigin() const
The origin point of the global simulation cell in world coordinates.
Definition: AtomicStructure.h:99
Base class for all degrees of freedom a potential or a structure may have.
Definition: DegreeOfFreedom.h:46
std::vector< Point3 > & atomDisplacements()
Returns the array of displacements of all atoms in the structure cell wrt a reference structure (non-...
Definition: AtomicStructure.h:180
const std::vector< int > & atomTags() const
Returns the array of IDs of the atoms in the cell (const version).
Definition: AtomicStructure.h:195
ScalarFitProperty _atomicEnergyProperty
Output of the per atom energy calculation that can be used for fitting.
Definition: AtomicStructure.h:401
degree(s) of freedom requires updating
Definition: AtomicStructure.h:55
const std::vector< std::pair< int, int > > & forwardMapping() const
Returns an array of integer pairs that map the real atoms to the ghost atoms.
Definition: AtomicStructure.h:210
Definition: FitProperty.h:126
void writeToDumpFile(const FPString &filename, bool includeGhostAtoms=false) const
Exports the structure to a LAMMPS dump file.
Definition: AtomicStructure.cpp:848
ScalarFitProperty _totalEnergyProperty
Output of the energy calculation that can be used for fitting.
Definition: AtomicStructure.h:399
ScalarFitProperty _bulkModulusProperty
The bulk modulus of the lattice.
Definition: AtomicStructure.h:414
Definition: AtomVectorProperty.h:32
int numAtoms() const
Returns the total number of atoms in the structure cell including real and ghost atoms.
Definition: AtomicStructure.h:158
Base class for potential.
Definition: Potential.h:41