Repository navigation
Expand file tree
/
Copy pathModel.cpp
More file actions
217 lines (178 loc) · 6.24 KB
/
Copy pathModel.cpp
File metadata and controls
217 lines (178 loc) · 6.24 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "Model.hpp"
#include "system/Foundation.hpp"
#include "support/format.hpp"
namespace cpb {
Model::Model(Lattice const& lattice)
: lattice(lattice),
site_registry(lattice.site_registry()),
hopping_registry(lattice.hopping_registry()) {}
void Model::add(Primitive new_primitive) {
primitive = new_primitive;
for (auto i = lattice.ndim(); i < 3; ++i) {
if (primitive.size[i] != 1) {
throw std::logic_error("Primitive shape has more dimensions than the lattice");
}
}
clear_structure();
}
void Model::set_wave_vector(Cartesian const& new_wave_vector) {
if (wave_vector != new_wave_vector) {
wave_vector = new_wave_vector;
clear_hamiltonian();
}
}
void Model::add(Shape const& new_shape) {
shape = new_shape;
lattice.set_offset(lattice.get_offset() + shape.lattice_offset);
clear_structure();
}
void Model::add(TranslationalSymmetry const& translational_symmetry) {
symmetry = translational_symmetry;
clear_structure();
}
void Model::attach_lead(int direction, Shape const& shape) {
if (direction == 0) {
throw std::logic_error("Lead direction must be one of: 1, 2, 3, -1, -2, -3");
} else if (lattice.ndim() == 1) {
throw std::logic_error("Attaching leads to 1D lattices is not supported");
} else if (std::abs(direction) > lattice.ndim()) {
throw std::logic_error(fmt::format("Direction {} is not valid for a {}D lattice",
direction, lattice.ndim()));
}
_leads.add(direction, shape);
clear_structure();
}
void Model::add(SiteStateModifier const& m) {
structure_modifiers.emplace_back(m);
clear_structure();
}
void Model::add(PositionModifier const& m) {
structure_modifiers.emplace_back(m);
clear_structure();
}
void Model::add(OnsiteModifier const& m) {
hamiltonian_modifiers.onsite.push_back(m);
clear_hamiltonian();
}
void Model::add(HoppingModifier const& m) {
hamiltonian_modifiers.hopping.push_back(m);
clear_hamiltonian();
}
void Model::add(SiteGenerator const& g) {
structure_modifiers.emplace_back(g);
site_registry.register_family(g.name, g.energy);
clear_structure();
}
void Model::add(HoppingGenerator const& g) {
structure_modifiers.emplace_back(g);
hopping_registry.register_family(g.name, g.energy);
clear_structure();
}
bool Model::is_multiorbital() const {
return site_registry.has_multiple_orbitals() || hopping_registry.has_multiple_orbitals();
}
bool Model::is_double() const {
return hamiltonian_modifiers.any_double();
}
bool Model::is_complex() const {
return site_registry.any_complex_terms() || hopping_registry.any_complex_terms()
|| hamiltonian_modifiers.any_complex() || symmetry || complex_override;
}
std::shared_ptr<System const> const& Model::system() const {
if (!_system) {
system_build_time.timeit([&]{
_system = make_system();
});
}
return _system;
}
Hamiltonian const& Model::hamiltonian() const {
system();
if (!_hamiltonian) {
hamiltonian_build_time.timeit([&]{
_hamiltonian = make_hamiltonian();
});
}
return _hamiltonian;
}
Leads const& Model::leads() const {
system();
hamiltonian();
_leads.make_hamiltonian(lattice, hamiltonian_modifiers, is_double(), is_complex());
return _leads;
}
Model const& Model::eval() const {
system();
hamiltonian();
leads();
return *this;
}
std::string Model::report() {
auto const num_sites = fmt::with_suffix(static_cast<double>(system()->num_sites()));
auto const nnz = fmt::with_suffix(static_cast<double>(hamiltonian().non_zeros()));
return fmt::format("Built system with {} lattice sites, {}\n"
"The Hamiltonian has {} non-zero values, {}",
num_sites, system_build_time, nnz, hamiltonian_build_time);
}
std::shared_ptr<System> Model::make_system() const {
auto foundation = shape ? Foundation(lattice, shape)
: Foundation(lattice, primitive);
if (symmetry) {
symmetry.apply(foundation);
}
auto const it = std::find_if(structure_modifiers.begin(), structure_modifiers.end(),
[](StructureModifier const& m) { return requires_system(m); });
auto const foundation_modifiers = make_range(structure_modifiers.begin(), it);
auto const system_modifiers = make_range(it, structure_modifiers.end());
for (auto const& modifier : foundation_modifiers) {
apply(modifier, foundation);
}
_leads.create_attachment_area(foundation);
_leads.make_structure(foundation);
auto sys = std::make_shared<System>(site_registry, hopping_registry);
detail::populate_system(*sys, foundation);
if (symmetry) {
detail::populate_boundaries(*sys, foundation, symmetry);
}
for (auto const& modifier : system_modifiers) {
apply(modifier, *sys);
}
detail::remove_invalid(*sys);
if (sys->num_sites() == 0) { throw std::runtime_error{"Impossible system: 0 sites"}; }
return sys;
}
Hamiltonian Model::make_hamiltonian() const {
auto const& built_system = *system();
auto const& modifiers = hamiltonian_modifiers;
auto const& k = wave_vector;
auto const simple_build = std::none_of(
structure_modifiers.begin(), structure_modifiers.end(),
[](StructureModifier const& m) { return is_generator(m); }
);
if (!is_complex()) {
try {
if (!is_double()) {
return ham::make<float>(built_system, lattice, modifiers, k, simple_build);
} else {
return ham::make<double>(built_system, lattice, modifiers, k, simple_build);
}
} catch (ComplexOverride const&) {
complex_override = true;
}
}
if (!is_double()) {
return ham::make<std::complex<float>>(built_system, lattice, modifiers, k, simple_build);
} else {
return ham::make<std::complex<double>>(built_system, lattice, modifiers, k, simple_build);
}
}
void Model::clear_structure() {
_system.reset();
_leads.clear_structure();
clear_hamiltonian();
}
void Model::clear_hamiltonian() {
_hamiltonian.reset();
_leads.clear_hamiltonian();
}
} // namespace cpb