-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_cubic_lattice.cpp
48 lines (41 loc) · 1.5 KB
/
set_cubic_lattice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// Created by Nicolas von Trott on 01.06.22.
//
#include "set_cubic_lattice.h"
void set_cubic_lattice(Atoms &atoms, double lattice_constant) {
// Get number of atoms in the Atoms structure.
long nb_atoms = atoms.nb_atoms();
// Get the size of the next smallest cubic lattice in which all atoms will fit.
// Not every grid point must be occupied by an atom.
int grid_size = ceil(cbrt(nb_atoms) / lattice_constant);
// Variable to count the placed atoms.
int i = 0;
// Variable for breaking the loops when all atoms are positioned.
int flag = 1;
// Loop over the cartesian coordinates x, y, and z.
for (int x = 0; x < grid_size; ++x) {
for (int y = 0; y < grid_size; ++y) {
for (int z = 0; z < grid_size; ++z) {
// Break loop if all atoms are positioned.
if (i == nb_atoms) {
flag = 0;
break;
}
// Get and set the x, y, and z coordinates of the atom.
atoms.positions(0, i) = x * lattice_constant;
atoms.positions(1, i) = y * lattice_constant;
atoms.positions(2, i) = z * lattice_constant;
// Count the placed atom.
i++;
}
// Break loop if all atoms are positioned.
if (flag == 0) {
break;
}
}
// Break loop if all atoms are positioned.
if (flag == 0) {
break;
}
}
}