File: energy_and_flux.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 (333 lines) | stat: -rw-r--r-- 10,640 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* 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 <string.h>

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

using namespace std;

namespace meep {

/* Energy calculation */

double fields::count_volume(component c) {
  double vol = 0;
  for (int i=0;i<num_chunks;i++)
    if (chunks[i]->is_mine())
      vol += chunks[i]->count_volume(c);
  return sum_to_all(vol);
}

double fields_chunk::count_volume(component c) {
  double vol = 0;
  for (size_t i=0;i<gv.ntot();i++)
    vol += gv.dV(c,i).full_volume();
  return vol;
}

double fields::total_energy() {
  return energy_in_box(user_volume.surroundings());
}

double fields::field_energy() {
  return field_energy_in_box(user_volume.surroundings());
}

double fields::energy_in_box(const volume &where) {
  return thermo_energy_in_box(where) + field_energy_in_box(where);
}

double fields::field_energy_in_box(const volume &where) {
  synchronize_magnetic_fields();
  double cur_step_magnetic_energy = magnetic_energy_in_box(where);
  restore_magnetic_fields();
  return electric_energy_in_box(where) + cur_step_magnetic_energy;
}

static complex<double> dot_integrand(const complex<double> *fields,
				     const vec &loc, void *data_)
{
  (void) loc; (void) data_; // unused;
  return real(conj(fields[0]) * fields[1]);
}

double fields::field_energy_in_box(component c,
				   const volume &where) {
  if (coordinate_mismatch(gv.dim, c))
    return 0.0;

  component cs[2];
  if (is_electric(c) || is_D(c)) {
    cs[0] = direction_component(Ex, component_direction(c));
    cs[1] = direction_component(Dx, component_direction(c));
  }
  else if (is_magnetic(c) || is_B(c)) {
    cs[0] = direction_component(Hx, component_direction(c));
    cs[1] = direction_component(Bx, component_direction(c));
  }
  else
    abort("invalid field component in field_energy_in_box");

  return real(integrate(2, cs, dot_integrand, 0, where)) * 0.5;
}

double fields::electric_energy_in_box(const volume &where) {
  long double sum = 0.0;
  FOR_ELECTRIC_COMPONENTS(c)
    sum += field_energy_in_box(c, where);
  return sum;
}

double fields::magnetic_energy_in_box(const volume &where) {
  long double sum = 0.0;
  FOR_MAGNETIC_COMPONENTS(c)
    sum += field_energy_in_box(c, where);
  return sum;
}

void fields_chunk::backup_component(component c) {
  DOCMP {
    if (c < NUM_FIELD_COMPONENTS && f[c][cmp] &&
	// in mu=1 regions where H==B, don't bother to backup H
	!(is_magnetic(c) && f[c][cmp]
	  == f[direction_component(Bx, component_direction(c))][cmp])) {

#define BACKUP(f) if (f[c][cmp]) {					\
      if (!f##_backup[c][cmp])						\
	f##_backup[c][cmp] = new realnum[gv.ntot()];			\
      memcpy(f##_backup[c][cmp], f[c][cmp], gv.ntot()*sizeof(realnum)); }

      BACKUP(f);
      BACKUP(f_u);
      BACKUP(f_w);
      BACKUP(f_cond);

#undef BACKUP
    }
  }
}

void fields_chunk::restore_component(component c) {
  DOCMP  {
#define RESTORE(f)                                                      \
    if (f##_backup[c][cmp] && f[c][cmp])                                \
      memcpy(f[c][cmp], f##_backup[c][cmp], gv.ntot()*sizeof(realnum));

    RESTORE(f);
    RESTORE(f_u);
    RESTORE(f_w);
    RESTORE(f_cond);

#undef RESTORE
  }
}

void fields_chunk::average_with_backup(component c) {
  DOCMP {
    realnum *fc = f[c][cmp];
    realnum *backup = f_backup[c][cmp];
    if (fc && backup)
      for (size_t i = 0; i < gv.ntot(); i++)
	fc[i] = 0.5 * (fc[i] + backup[i]);
  }
}

void fields::synchronize_magnetic_fields() {
  if (synchronized_magnetic_fields++) return; // already synched
  for (int i=0;i<num_chunks;i++)
    if (chunks[i]->is_mine()) {
      FOR_B_COMPONENTS(c) chunks[i]->backup_component(c);
      FOR_MAGNETIC_COMPONENTS(c) chunks[i]->backup_component(c);
    }
  am_now_working_on(Stepping);
  calc_sources(time()); // for B sources
  step_db(B_stuff);
  step_source(B_stuff);
  step_boundaries(B_stuff);
  calc_sources(time() + 0.5*dt); // for integrated H sources
  update_eh(H_stuff);
  step_boundaries(H_stuff);
  finished_working();
  for (int i=0;i<num_chunks;i++)
    if (chunks[i]->is_mine()) {
      FOR_B_COMPONENTS(c) chunks[i]->average_with_backup(c);
      FOR_MAGNETIC_COMPONENTS(c) chunks[i]->average_with_backup(c);
    }
}

void fields::restore_magnetic_fields() {
  if (!synchronized_magnetic_fields // already restored
      || --synchronized_magnetic_fields) // not ready to restore yet
    return;
  for (int i=0;i<num_chunks;i++)
    if (chunks[i]->is_mine()) {
      FOR_B_COMPONENTS(c) chunks[i]->restore_component(c);
      FOR_MAGNETIC_COMPONENTS(c) chunks[i]->restore_component(c);
    }
}

double fields::thermo_energy_in_box(const volume &where) {
  long double sum = 0.0;
  (void) where; // unused
  abort("thermo_energy_in_box no longer supported");
  return sum_to_all(sum);
}

/* Compute ExH integral in box using current fields, ignoring fact
   that this E and H correspond to different times. */
double fields::flux_in_box_wrongH(direction d, const volume &where) {
  if (coordinate_mismatch(gv.dim, d))
    return 0.0;

  component cE[2], cH[2];
  switch (d) {
  case X: cE[0] = Ey, cE[1] = Ez, cH[0] = Hz, cH[1] = Hy; break;
  case Y: cE[0] = Ez, cE[1] = Ex, cH[0] = Hx, cH[1] = Hz; break;
  case R: cE[0] = Ep, cE[1] = Ez, cH[0] = Hz, cH[1] = Hp; break;
  case P: cE[0] = Ez, cE[1] = Er, cH[0] = Hr, cH[1] = Hz; break;
  case Z:
    if (gv.dim == Dcyl)
      cE[0] = Er, cE[1] = Ep, cH[0] = Hp, cH[1] = Hr;
    else
      cE[0] = Ex, cE[1] = Ey, cH[0] = Hy, cH[1] = Hx;
    break;
  case NO_DIRECTION: abort("cannot get flux in NO_DIRECTION");
  }

  long double sum = 0.0;
  for (int i = 0; i < 2; ++i) {
    component cs[2];
    cs[0] = cE[i]; cs[1] = cH[i];
    sum += real(integrate(2, cs, dot_integrand, 0, where)) * (1 - 2*i);
  }
  return sum;
}

double fields::flux_in_box(direction d, const volume &where) {
  synchronize_magnetic_fields();
  double cur_step_flux = flux_in_box_wrongH(d, where);
  restore_magnetic_fields();
  return cur_step_flux;
}

flux_vol *fields::add_flux_vol(direction d, const volume &where) {
  if (where.dim != gv.dim) abort("invalid dimensionality in add_flux_vol");
  if (d == NO_DIRECTION || coordinate_mismatch(gv.dim, d))
    abort("invalid direction in add_flux_vol");
 return new flux_vol(this, d, where);
}

// As add_flux_vol, but infer direction from where (if possible)
flux_vol *fields::add_flux_plane(const volume &where) {
  return add_flux_vol(where.normal_direction(), where);
}

flux_vol *fields::add_flux_plane(const vec &p1, const vec &p2) {
  return add_flux_plane(volume(p1, p2));
}

/************************************************************************/

/* Note that computation of modal grid_volume by this definition is
   somewhat problematic computationally, because we need to compute
   max|D*E|, which requires averaging discontinuous functions.  Hence,
   except for the special case of 2d TM polarization, the computed
   value tends to have a large error bar if the maximum lies on a
   dielectric boundary as it commonly does.

   A better method would be to average only continuous quantities in
   order to compute the fields on the Centered grid, but this
   is more expensive and requires us to know the boundary orientation, and
   does not seem worth the trouble at this point. */

static complex<double> dot3_max_integrand(const complex<double> *fields,
				      const vec &loc, void *data_)
{
  (void) loc; (void) data_; // unused;
  return (real(conj(fields[0]) * fields[3]) +
	  real(conj(fields[1]) * fields[4]) +
	  real(conj(fields[2]) * fields[5]));
}

double fields::electric_energy_max_in_box(const volume &where) {
  component cs[6];
  if (gv.dim == Dcyl) {
    cs[0] = Er; cs[1] = Ep; cs[2] = Ez;
    cs[3+0] = Dr; cs[3+1] = Dp; cs[3+2] = Dz;
  }
  else {
    cs[0] = Ex; cs[1] = Ey; cs[2] = Ez;
    cs[3+0] = Dx; cs[3+1] = Dy; cs[3+2] = Dz;
  }

  return max_abs(6, cs, dot3_max_integrand, 0, where) * 0.5;
}

/* "modal" grid_volume according to definition in:
      E. M. Purcell, Phys. Rev. B 69, 681 (1946).
    (based on spontaneous emission enhancement). */
double fields::modal_volume_in_box(const volume &where) {
  return electric_energy_in_box(where) / electric_energy_max_in_box(where);
}

/************************************************************************/

  /* compute integral f(x) * Re[conj(f1)*f2] * 0.5, which is useful for
     perturbation theory, etcetera, where f1 and f2 are two field components
     on the same Yee lattice (e.g. Hx and Hx or Ex and Dx). */

typedef double (*fx_func)(const vec &);

static complex<double> dot_fx_integrand(const complex<double> *fields,
					const vec &loc, void *data_) {
  fx_func fx = (fx_func) data_;
  return (real(conj(fields[0]) * fields[1]) * fx(loc));
}

/* computes integral of f(x) * |E|^2 / integral epsilon*|E|^2 */
double fields::electric_sqr_weighted_integral(double (*f)(const vec &),
					     const volume &where) {
  double sum = 0.0;
  FOR_ELECTRIC_COMPONENTS(c)
    if (!coordinate_mismatch(gv.dim, component_direction(c))) {
      component cs[2];
      cs[0] = cs[1] = direction_component(Ex, component_direction(c));
      sum += real(integrate(2, cs, dot_fx_integrand, (void *) f, where));
    }
  return sum * 0.5 / electric_energy_in_box(where);
}

/* computes integral of f(x) * epsilon*|E|^2 / integral epsilon*|E|^2 */
double fields::electric_energy_weighted_integral(double (*f)(const vec &),
					     const volume &where) {
  double sum = 0.0;
  FOR_ELECTRIC_COMPONENTS(c)
    if (!coordinate_mismatch(gv.dim, component_direction(c))) {
      component cs[2];
      cs[0] = direction_component(Ex, component_direction(c));
      cs[1] = direction_component(Dx, component_direction(c));
      sum += real(integrate(2, cs, dot_fx_integrand, (void *) f, where));
    }
  return sum * 0.5 / electric_energy_in_box(where);
}

} // namespace meep