File: physical.cpp

package info (click to toggle)
meep 0.10-2.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,380 kB
  • ctags: 5,469
  • sloc: cpp: 50,653; sh: 8,380; haskell: 744; makefile: 367; perl: 10
file content (108 lines) | stat: -rw-r--r-- 3,468 bytes parent folder | download
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
/* Copyright (C) 2006 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 <meep.hpp>
using namespace meep;

double one(const vec &) { return 1.0; }

double slab(const vec &v) {
  abort("not yet\n");
}

int radiating_2D(const double xmax) {
  const double a = 10.0;
  const double gridpts = a*xmax;
  const double ymax = 3.0;

  volume v = voltwo(xmax,ymax,a);
  structure s(v, one, pml(ymax/3));

  fields f(&s);
  double w = 0.30;
  double dx = 2.0;
  continuous_src_time src(w);
  f.add_point_source(Ez, src, vec(xmax/2 - dx, ymax/2));

  vec p1(xmax/2 + 0*dx, ymax/2);
  vec p2(xmax/2 + 1*dx, ymax/2);

  // let the source reach steady state
  f.solve_cw(1e-3);

  complex<double> amp1 = f.get_field(Ez, p1);
  complex<double> amp2 = f.get_field(Ez, p2);
  double ratio = pow(abs(amp1)/abs(amp2), 2.0) ;
  printf("Ratio is %g from (%g %g) and (%g %g)\n",
         ratio, real(amp1), imag(amp1), real(amp2), imag(amp2));
  if (ratio > 2.12 || ratio < 1.88)
    abort("Failed: amp1 = (%g, %g), amp2 = (%g, %g)\n abs(amp1/amp2)^2 = %g, too far from 2.0\n",
	  real(amp1), imag(amp1), real(amp2), imag(amp2), ratio);
  return 1;
}

int radiating_3D() {
  const double a = 10.0;
  const double pml_thickness = 1.0;
  const double ymax = 0.4 + 2*pml_thickness;
  const double zmax = ymax;
  const double w = 0.30;
  const double xmax = 8.0;
  const double dx = (xmax/2 - pml_thickness)/2 - 2/a;

  volume v = vol3d(xmax,ymax,zmax,a);
  symmetry S = mirror(X,v) + mirror(Y,v) + mirror(Z,v)*(-1.0);
  structure s(v, one, pml(pml_thickness), S);

  fields f(&s);
  continuous_src_time src(w);
  f.add_point_source(Ez, src, vec(xmax/2, ymax/2, zmax/2));

  vec p1(xmax/2 + dx, ymax/2, zmax/2);
  vec p2(xmax/2 + 2*dx, ymax/2, zmax/2);

  // let the source reach steady state
  f.solve_cw(1e-3, 10000, 4);

  complex<double> amp1 = f.get_field(Ez, p1);
  complex<double> amp2 = f.get_field(Ez, p2);
  const double ratio = abs(amp1)/abs(amp2);
  printf("Ratio is %g from (%g %g) and (%g %g)\n",
         ratio, real(amp1), imag(amp1), real(amp2), imag(amp2));
  if (ratio > 2.05 || ratio < 1.87)
    abort("Failed: amp1 = (%g, %g), amp2 = (%g, %g)\n abs(amp1/amp2)^2 = %g, too far from 2.0\n",
	  real(amp1), imag(amp1), real(amp2), imag(amp2), ratio);
  return 1;
}

void attempt(const char *name, int allright) {
  if (allright) master_printf("Passed %s\n", name);
  else abort("Failed %s!\n", name);
}

int main(int argc, char **argv) {
  initialize mpi(argc, argv);
  // quiet = true;
  master_printf("Trying out some physical tests...\n");

  attempt("radiating source should decay spatially as 1/sqrt(r) in 2D.", radiating_2D(8.0));
  attempt("radiating source should decay spatially as 1/r in 3D.", radiating_3D());
  return 0;
}