File: bands.cpp

package info (click to toggle)
meep-mpich2 1.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 25,824 kB
  • sloc: cpp: 27,370; python: 10,574; lisp: 1,213; makefile: 440; sh: 28
file content (205 lines) | stat: -rw-r--r-- 6,482 bytes parent folder | download | duplicates (5)
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
/* Copyright (C) 2005-2015 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, 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 <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "meep.hpp"
#include "meep_internals.hpp"

#include "config.h"
#ifdef HAVE_HARMINV
#  include <harminv.h>
#endif

using namespace std;

namespace meep {

double fields::last_source_time() {
  double last_time = 0;
    if (sources != NULL)
      last_time = max(last_time, sources->last_time_max());
  for (int i=0;i<num_chunks;i++)
    if (chunks[i]->is_mine())
      last_time = max(last_time,chunks[i]->last_source_time());
  return max_to_all(last_time);
}

double fields_chunk::last_source_time() {
  return 0;
}

/* backwards compatibility with harminv < 1.4 */
#if HARMINV_VERSION_MAJOR < 1 || (HARMINV_VERSION_MAJOR == 1 && HARMINV_VERSION_MINOR < 4)
#  define harminv_get_amplitude(pa, d, k) *(pa) = harminv_get_amplitude(d, k)
#  define harminv_get_omega(pw, d, k) *(pw) = harminv_get_omega(d, k)
#endif

int do_harminv(complex<double> *data, int n, double dt,
	       double fmin, double fmax, int maxbands,
	       complex<double> *amps, double *freq_re, double *freq_im, double *errors,
	       double spectral_density, double Q_thresh, double rel_err_thresh, double err_thresh, double rel_amp_thresh, double amp_thresh) {
#ifndef HAVE_HARMINV
  (void) data; (void) n; (void) dt; (void) fmin; (void) fmax; (void) maxbands;
  (void) amps; (void) freq_re; (void) freq_im; (void) errors;
  (void) spectral_density; (void) Q_thresh; (void) rel_err_thresh;
  (void) err_thresh; (void) rel_amp_thresh; (void) amp_thresh;
  abort("compiled without Harminv library, required for do_harminv");
  return 0;
#else
  int numfreqs = int(fabs(fmax-fmin)*dt*n*spectral_density); // c.f. harminv
  if (numfreqs > 150) numfreqs = 150; // prevent matrices from getting too big
  if (numfreqs < 2) numfreqs = 2;

  if (maxbands > numfreqs)
       numfreqs = maxbands;

  // check for all zeros in input
  // data is a size n array.
  {
    int i;
    for (i = 0; i < n && data[i] == 0.0; i++)
      ;
    if (i == n)
      return 0;
  }

#if 0
  // debugging: save data file and arguments for standalone harminv program
  {
    FILE *f = fopen("harminv.dat", "w");
    fprintf(f, "# -f %d -t %g %g-%g -Q %e -e %e -E %e -a %e -A %e -F\n",
	    numfreqs, dt, fmin, fmax,
	    Q_thresh, rel_err_thresh, err_thresh, rel_amp_thresh, amp_thresh);
    for (int i = 0; i < n; ++i)
      fprintf(f, "%g%+gi\n", real(data[i]), imag(data[i]));
    fclose(f);
  }
#endif

  harminv_data hd = harminv_data_create(n, data, fmin*dt, fmax*dt, numfreqs);
  harminv_solve(hd);

  int nf = harminv_get_num_freqs(hd);
  if (nf == 0) return 0;
  int *fsort = new int[nf]; // indices of frequencies, sorted as needed

  for (int i = 0; i < nf; ++i)
    fsort[i] = i;
  for (int i = 0; i < nf; ++i) // sort in increasing order of error
    for (int j = i + 1; j < nf; ++j)
      if (harminv_get_freq_error(hd, fsort[i]) >
      	  harminv_get_freq_error(hd, fsort[j])) {
      	int k = fsort[i];
      	fsort[i] = fsort[j];
      	fsort[j] = k;
      }

  double min_err = harminv_get_freq_error(hd, fsort[0]);
  complex<double> aa;
  harminv_get_amplitude(&aa, hd, 0);
  double max_amp = abs(aa);
  for (int i = 1; i < nf; ++i) {
    harminv_get_amplitude(&aa, hd, i);
    double amp = abs(aa);
    if (max_amp < amp)
      max_amp = amp;
  }
  { // eliminate modes that fall outside the various thresholds:
    int j = 0;
    for (int i = 0; i < nf; ++i) {
      double f = abs(harminv_get_freq(hd, fsort[i]) / dt);
      double err = harminv_get_freq_error(hd, fsort[i]);
      harminv_get_amplitude(&aa, hd, fsort[i]);
      double amp = abs(aa);
      if (f >= fmin && f <= fmax
      	  && abs(harminv_get_Q(hd, fsort[i])) > Q_thresh
      	  && err < err_thresh
      	  && err < rel_err_thresh * min_err
      	  && amp > amp_thresh
      	  && amp > rel_amp_thresh * max_amp) {
      	fsort[j++] = fsort[i];
      }
    }
    nf = j;
  }
  { // eliminate positive/negative frequency pairs
    // set indices to -1 for frequencies to be eliminated
    for (int i = 0; i < nf; ++i)
      if (fsort[i] != -1) { // i hasn't been eliminated yet
      	double f = harminv_get_freq(hd, fsort[i]);
      	if (f < 0.0) {
      	  double kdiff = -2 * f;
      	  int kpos = i;
      	  for (int k = 0; k < nf; ++k) // search for closest positive freq.
      	    if (fsort[k] != -1) { // k hasn't been eliminated yet
      	      double fdiff = abs(harminv_get_freq(hd, fsort[k]) + f);
      	      if (fdiff < kdiff) {
      		kpos = k;
      		kdiff = fdiff;
	      }
	    }
	  if (kpos != i && kdiff < 2.0 / n) { // consider them the same
	    // pick the one with the smaller error
	    if (harminv_get_freq_error(hd, fsort[i]) <
		harminv_get_freq_error(hd, fsort[kpos]))
	      fsort[kpos] = -1;
	    else
	      fsort[i] = -1;
	  }
	}
      }
    int j = 0;
    for (int i = 0; i < nf; ++i) // remove the eliminated indices
      if (fsort[i] != -1)
      	fsort[j++] = fsort[i];
    nf = j;
  }

  if (nf > maxbands)
    nf = maxbands;

  // sort again, this time in increasing order of freq:
  for (int i = 0; i < nf; ++i) // simple O(nf^2) sort
    for (int j = i + 1; j < nf; ++j)
      if (abs(harminv_get_freq(hd, fsort[i])) >
      	  abs(harminv_get_freq(hd, fsort[j]))) {
      	int k = fsort[i];
      	fsort[i] = fsort[j];
      	fsort[j] = k;
      }

  for (int i = 0; i < nf; ++i) {
    complex<double> freq;
    harminv_get_omega(&freq, hd, fsort[i]);
    freq /= (2*pi*dt);
    freq_re[i] = abs(real(freq));
    freq_im[i] = imag(freq);
    harminv_get_amplitude(&(amps[i]), hd, fsort[i]);
    if (errors)
      errors[i] = harminv_get_freq_error(hd, fsort[i]);
  }

  delete[] fsort;
  harminv_data_destroy(hd);
  return nf;
#endif
}

} // namespace meep