Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
Cell.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include "core/Atom.hpp"
12#include "math/Constants.hpp"
14#include "math/Precision.hpp"
15
16#include <array>
17#include <optional>
18#include <span>
19#include <string_view>
20#include <vector>
21
22namespace correlation::core {
23
30class alignas(64) Cell {
31
32public:
35 explicit Cell() = default;
36
43 explicit Cell(const math::Vector3<real_t> &vec_a, const math::Vector3<real_t> &vec_b,
44 const math::Vector3<real_t> &vec_c);
45
57 explicit Cell(const std::array<real_t, 6> &params);
58
64 Cell(Cell &&other) noexcept = default;
65
71 Cell &operator=(Cell &&other) noexcept = default;
72
77 Cell(const Cell &other) = default;
78
84 Cell &operator=(const Cell &other) = default;
85
89 ~Cell() = default;
90
92
95
96 // Lattice Parameters
101 [[nodiscard]] const std::array<real_t, 6> &latticeParameters() const noexcept {
102 return lattice_parameters_;
103 }
104
105 void setLatticeParameters(const std::array<real_t, 6> &params);
106
107 // Lattice Vectors
112 [[nodiscard]] const math::Matrix3<real_t> &latticeVectors() const noexcept {
113 return lattice_vectors_;
114 }
115
121 [[nodiscard]] const math::Matrix3<real_t> &inverseLatticeVectors() const noexcept {
123 }
124
125 // Volume
130 [[nodiscard]] const real_t &volume() const noexcept { return volume_; }
131
132 // Atoms
137 [[nodiscard]] const std::vector<Atom> &atoms() const noexcept { return atoms_; }
138
143 [[nodiscard]] std::vector<Atom> &atoms() noexcept { return atoms_; }
144
149 [[nodiscard]] std::span<const Atom> atomSpan() const noexcept { return atoms_; }
150
155 [[nodiscard]] std::span<Atom> atomSpan() noexcept { return atoms_; }
156
157 // Lazy per-atom velocities / forces (allocated only when populated)
162 [[nodiscard]] bool hasVelocities() const noexcept { return !velocities_.empty(); }
163
168 [[nodiscard]] const std::vector<math::Vector3<real_t>> &velocities() const noexcept {
169 return velocities_;
170 }
171
177 [[nodiscard]] math::Vector3<real_t> atomVelocity(size_t atom_idx) const noexcept {
178 return (atom_idx < velocities_.size()) ? velocities_[atom_idx]
179 : math::Vector3<real_t>{0.0, 0.0, 0.0};
180 }
181
187 void setAtomVelocity(size_t atom_idx, const math::Vector3<real_t> &vel) {
188 if (atom_idx >= atoms_.size()) {
189 return;
190 }
191 if (velocities_.size() < atoms_.size()) {
192 velocities_.resize(atoms_.size(), math::Vector3<real_t>{0.0, 0.0, 0.0});
193 }
194 velocities_[atom_idx] = vel;
195 }
196
201 if (velocities_.size() < atoms_.size()) {
202 velocities_.resize(atoms_.size(), math::Vector3<real_t>{0.0, 0.0, 0.0});
203 }
204 }
205
206 // Elements
211 [[nodiscard]] const std::vector<Element> &elements() const noexcept { return elements_; }
212
217 [[nodiscard]] std::span<const Element> elementSpan() const noexcept { return elements_; }
218
223 [[nodiscard]] size_t atomCount() const noexcept { return atoms_.size(); }
224
229 [[nodiscard]] bool isEmpty() const noexcept { return atoms_.empty(); }
230
237 [[nodiscard]] std::optional<Element> findElement(std::string_view symbol) const;
238
245 bool renameElement(std::string_view old_symbol, std::string_view new_symbol);
246
248
251
262
271 Atom &addAtom(std::string_view symbol, const math::Vector3<real_t> &position);
272
278 void wrapPositions();
279
288 [[nodiscard]] std::array<real_t, 3> perpendicularWidths() const noexcept;
289
299 [[nodiscard]] Cell replicate(int rep_a, int rep_b, int rep_c) const;
300
315 [[nodiscard]] Cell autoSupercell(real_t r_cut, int max_replication = 10,
316 real_t max_radius = correlation::math::MAX_CUTOFF_RADIUS) const;
317
322 void setEnergy(real_t energy) noexcept { energy_ = energy; }
323
328 [[nodiscard]] real_t getEnergy() const noexcept { return energy_; }
329
334 void reserveAtoms(std::size_t count) { atoms_.reserve(count); }
335
340 void updateLattice(const math::Matrix3<real_t> &new_lattice);
341
343
344private:
349
355 const Element &getOrRegisterElement(std::string_view symbol);
356
359 std::array<real_t, 6> lattice_parameters_{};
362 std::vector<Atom> atoms_;
363 std::vector<Element> elements_;
364 std::vector<math::Vector3<real_t>>
366};
367
368} // namespace correlation::core
Atom data structure and AtomID type definitions.
Mathematical and physical constants for the Correlation tool.
Lightweight 3D vector and matrix classes for geometric calculations.
Floating point precision type definitions for Correlation.
Represents an atom in the simulation cell.
Definition Atom.hpp:183
Represents the simulation cell (periodic box).
Definition Cell.hpp:30
bool hasVelocities() const noexcept
Checks whether lazy per-atom velocities have been allocated for this cell.
Definition Cell.hpp:162
std::vector< Atom > atoms_
Collection of atoms in the cell.
Definition Cell.hpp:362
real_t energy_
Potential energy of this specific coordinate set.
Definition Cell.hpp:361
void wrapPositions()
Applies periodic boundary conditions to all atom positions. Wraps all atoms back into the primary sim...
Definition Cell.cpp:240
std::vector< Element > elements_
Unique elements present in the system.
Definition Cell.hpp:363
Cell(const Cell &other)=default
Copy constructor.
std::array< real_t, 6 > lattice_parameters_
{a, b, c, alpha, beta, gamma}.
Definition Cell.hpp:359
void setAtomVelocity(size_t atom_idx, const math::Vector3< real_t > &vel)
Sets the velocity of a specific atom, lazily allocating velocities_ on first write.
Definition Cell.hpp:187
Cell & operator=(Cell &&other) noexcept=default
Move assignment operator.
std::span< Atom > atomSpan() noexcept
Gets a mutable span view over the atoms in the cell.
Definition Cell.hpp:155
std::vector< Atom > & atoms() noexcept
Gets a mutable list of atoms in the cell.
Definition Cell.hpp:143
std::array< real_t, 3 > perpendicularWidths() const noexcept
Computes the perpendicular widths (interplanar spacing) between opposing cell faces.
Definition Cell.cpp:250
void setLatticeParameters(const std::array< real_t, 6 > &params)
Definition Cell.cpp:57
std::span< const Atom > atomSpan() const noexcept
Gets an immutable span view over the atoms in the cell.
Definition Cell.hpp:149
void reserveAtoms(std::size_t count)
Reserves capacity for atom storage to minimize reallocations.
Definition Cell.hpp:334
Cell autoSupercell(real_t r_cut, int max_replication=10, real_t max_radius=correlation::math::MAX_CUTOFF_RADIUS) const
Automatically expands the cell to satisfy the Minimum Image Convention for cutoff r_cut.
Definition Cell.cpp:319
std::vector< math::Vector3< real_t > > velocities_
Lazy per-atom velocities/forces (0 B when unused).
Definition Cell.hpp:365
const math::Matrix3< real_t > & inverseLatticeVectors() const noexcept
Gets the inverse lattice vectors as a 3x3 matrix. Useful for converting Cartesian coordinates to frac...
Definition Cell.hpp:121
real_t volume_
Cached volume in Angstroms^3.
Definition Cell.hpp:360
const std::vector< Atom > & atoms() const noexcept
Gets the list of atoms in the cell.
Definition Cell.hpp:137
std::optional< Element > findElement(std::string_view symbol) const
Finds the Element properties for a given element symbol.
Definition Cell.cpp:144
size_t atomCount() const noexcept
Gets the total number of atoms in the cell.
Definition Cell.hpp:223
bool isEmpty() const noexcept
Checks whether the cell contains no atoms.
Definition Cell.hpp:229
math::Vector3< real_t > atomVelocity(size_t atom_idx) const noexcept
Gets the velocity of a specific atom, returning {0, 0, 0} if unallocated.
Definition Cell.hpp:177
std::span< const Element > elementSpan() const noexcept
Gets an immutable span view over unique chemical elements in the cell.
Definition Cell.hpp:217
const std::vector< math::Vector3< real_t > > & velocities() const noexcept
Gets the contiguous lazy velocity buffer (empty if velocities were never set).
Definition Cell.hpp:168
void updateLattice(const math::Matrix3< real_t > &new_lattice)
Updates lattice vectors and recomputes volume, inverse matrix, and scalar parameters.
Definition Cell.cpp:93
Cell(Cell &&other) noexcept=default
Move constructor. Transfers ownership of lattice vectors and atom data.
const std::vector< Element > & elements() const noexcept
Gets the list of unique chemical elements in the cell.
Definition Cell.hpp:211
Cell replicate(int rep_a, int rep_b, int rep_c) const
Replicates the simulation cell na x nb x nc times along lattice vectors.
Definition Cell.cpp:270
void ensureVelocities()
Ensures the lazy velocity buffer is sized to match atomCount().
Definition Cell.hpp:200
Cell & operator=(const Cell &other)=default
Copy assignment operator.
const Element & getOrRegisterElement(std::string_view symbol)
Registers an element symbol if not already present and returns its Element reference.
Definition Cell.cpp:205
const real_t & volume() const noexcept
Gets the volume of the simulation cell.
Definition Cell.hpp:130
~Cell()=default
Destructor.
math::Matrix3< real_t > inverse_lattice_vectors_
Inverse matrix for fractional mapping.
Definition Cell.hpp:358
void setEnergy(real_t energy) noexcept
Sets the energy of the cell frame.
Definition Cell.hpp:322
const std::array< real_t, 6 > & latticeParameters() const noexcept
Gets the lattice parameters (a, b, c, alpha, beta, gamma).
Definition Cell.hpp:101
const math::Matrix3< real_t > & latticeVectors() const noexcept
Gets the lattice vectors as a 3x3 matrix.
Definition Cell.hpp:112
Atom & addAtom(std::string_view symbol, const math::Vector3< real_t > &position)
Adds a new atom to the cell. The atom's element type is automatically registered if it's the first me...
Definition Cell.cpp:220
bool renameElement(std::string_view old_symbol, std::string_view new_symbol)
Renames an existing element species in-place across all atoms in the cell.
Definition Cell.cpp:153
void updateLatticeParametersFromVectors()
Internal helper to synchronize scalar parameters with vector matrix.
Definition Cell.cpp:103
math::Vector3< real_t > minimumImage(const math::Vector3< real_t > &distance) const
Applies the minimum image convention to a distance vector.
Definition Cell.cpp:227
real_t getEnergy() const noexcept
Gets the energy of the cell frame.
Definition Cell.hpp:328
math::Matrix3< real_t > lattice_vectors_
Basis vectors of the box.
Definition Cell.hpp:357
A lightweight, constexpr, stack-based 3x3 matrix.
Definition LinearAlgebra.hpp:261
A lightweight, constexpr, stack-based 3D vector.
Definition LinearAlgebra.hpp:32
Definition ITrajectoryCalculator.hpp:13
real_t distance(const Atom &atom_a, const Atom &atom_b) noexcept
Calculates the Euclidean distance between two atoms.
Definition Atom.hpp:280
Defines universal mathematical and physical constants.
Definition AnalysisTypes.hpp:11
double real_t
Definition Precision.hpp:18
Represents a chemical element with its symbol and unique ID.
Definition Atom.hpp:52