-
Notifications
You must be signed in to change notification settings - Fork 782
Expand file tree
/
Copy pathdft_ldos.cpp
More file actions
178 lines (158 loc) · 6.41 KB
/
Copy pathdft_ldos.cpp
File metadata and controls
178 lines (158 loc) · 6.41 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
/* Copyright (C) 2005-2026 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "meep.hpp"
#include "meep/backend_hooks.hpp"
#include "meep_internals.hpp"
using namespace std;
namespace meep {
dft_ldos::dft_ldos(double freq_min, double freq_max, int Nfreq) {
freq = meep::linspace(freq_min, freq_max, Nfreq);
Fdft = new complex<double>[Nfreq];
Jdft = new complex<double>[Nfreq];
for (int i = 0; i < Nfreq; ++i)
Fdft[i] = Jdft[i] = 0.0;
Jsum = 1.0;
saved_overall_scale = 1.0;
}
dft_ldos::dft_ldos(const std::vector<double> freq_) {
const size_t Nfreq = freq_.size();
freq = freq_;
Fdft = new complex<double>[Nfreq];
Jdft = new complex<double>[Nfreq];
for (size_t i = 0; i < Nfreq; ++i)
Fdft[i] = Jdft[i] = 0.0;
Jsum = 1.0;
saved_overall_scale = 1.0;
}
dft_ldos::dft_ldos(const double *freq_, size_t Nfreq) : freq(Nfreq) {
for (size_t i = 0; i < Nfreq; ++i)
freq[i] = freq_[i];
Fdft = new complex<double>[Nfreq];
Jdft = new complex<double>[Nfreq];
for (size_t i = 0; i < Nfreq; ++i)
Fdft[i] = Jdft[i] = 0.0;
Jsum = 1.0;
saved_overall_scale = 1.0;
}
// |c|^2
static double abs2(complex<double> c) { return real(c) * real(c) + imag(c) * imag(c); }
double *dft_ldos::ldos() {
// we try to get the overall scale factor right (at least for a point source)
// so that we can compare against the analytical formula for testing
// ... in most practical cases, the scale factor won't matter because
// the user will compute the relative LDOS of 2 cases (e.g. LDOS/vacuum)
// overall scale factor
double Jsum_all = sum_to_all(Jsum);
saved_overall_scale = 4.0 / pi // from definition of LDOS comparison to power
* -0.5 // power = -1/2 Re[E* J]
/ (Jsum_all * Jsum_all); // normalize to unit-integral current
const size_t Nfreq = freq.size();
double *sum = new double[Nfreq];
for (size_t i = 0; i < Nfreq; ++i) /* 4/pi * work done by unit dipole */
sum[i] = saved_overall_scale * real(Fdft[i] * conj(Jdft[i])) / abs2(Jdft[i]);
double *out = new double[Nfreq];
sum_to_all(sum, out, Nfreq);
delete[] sum;
return out;
}
complex<double> *dft_ldos::F() const {
const size_t Nfreq = freq.size();
complex<double> *out = new complex<double>[Nfreq];
sum_to_all(Fdft, out, Nfreq);
return out;
}
complex<double> *dft_ldos::J() const {
const size_t Nfreq = freq.size();
complex<double> *out = new complex<double>[Nfreq];
// note: Jdft is the same on all processes, so no sum_to_all
memcpy(out, Jdft, Nfreq * sizeof(complex<double>));
return out;
}
void dft_ldos::update(fields &f) {
complex<double> EJ = 0.0; // integral E * J*
complex<double> HJ = 0.0; // integral H * J* for magnetic currents
double scale = (f.dt / sqrt(2 * pi));
// compute Jsum for LDOS normalization purposes
// ...don't worry about the tiny inefficiency of recomputing this repeatedly
Jsum = 0.0;
/* If no fast point-read is available, fall back to a single up-front
* sync so the direct array reads (via read_field_at) see fresh data. */
if (!meep_backend.read_point) sync_host_if_needed(&f);
for (int ic = 0; ic < f.num_chunks; ic++)
if (f.chunks[ic]->is_mine()) {
fields_chunk *fc = f.chunks[ic];
for (const src_vol &sv : fc->get_sources(D_stuff)) {
component c = direction_component(Ex, component_direction(sv.c));
realnum *fr = fc->f[c][0];
realnum *fi = fc->f[c][1];
if (fr && fi) // complex E
for (size_t j = 0; j < sv.num_points(); j++) {
const ptrdiff_t idx = sv.index_at(j);
const complex<double> &A = sv.amplitude_at(j);
EJ += complex<double>(read_field_at(&f, fc, c, 0, idx),
read_field_at(&f, fc, c, 1, idx)) *
conj(A);
Jsum += abs(A);
}
else if (fr) { // E is purely real
for (size_t j = 0; j < sv.num_points(); j++) {
const ptrdiff_t idx = sv.index_at(j);
const complex<double> &A = sv.amplitude_at(j);
EJ += double(read_field_at(&f, fc, c, 0, idx)) * conj(A);
Jsum += abs(A);
}
}
}
for (const src_vol &sv : fc->get_sources(B_stuff)) {
component c = direction_component(Hx, component_direction(sv.c));
realnum *fr = fc->f[c][0];
realnum *fi = fc->f[c][1];
if (fr && fi) // complex H
for (size_t j = 0; j < sv.num_points(); j++) {
const ptrdiff_t idx = sv.index_at(j);
const complex<double> &A = sv.amplitude_at(j);
HJ += complex<double>(read_field_at(&f, fc, c, 0, idx),
read_field_at(&f, fc, c, 1, idx)) *
conj(A);
Jsum += abs(A);
}
else if (fr) { // H is purely real
for (size_t j = 0; j < sv.num_points(); j++) {
const ptrdiff_t idx = sv.index_at(j);
const complex<double> &A = sv.amplitude_at(j);
HJ += double(read_field_at(&f, fc, c, 0, idx)) * conj(A);
Jsum += abs(A);
}
}
}
}
for (size_t i = 0; i < freq.size(); ++i) {
complex<double> Ephase = polar(1.0, 2 * pi * freq[i] * f.time()) * scale;
complex<double> Hphase = polar(1.0, 2 * pi * freq[i] * (f.time() - f.dt / 2)) * scale;
Fdft[i] += Ephase * EJ + Hphase * HJ;
// NOTE: take only 1st time dependence: assumes all sources have same J(t)
if (f.sources) {
if (f.is_real) // todo: not quite right if A is complex
Jdft[i] += Ephase * real(f.sources->current());
else
Jdft[i] += Ephase * f.sources->current();
}
}
// correct for dV factors
Jsum *= sqrt(f.gv.dV(f.gv.icenter(), 1).computational_volume());
}
} // namespace meep