\begin{comment}
/*
\end{comment}
\section{Baby's First Bandstructure}
\begin{comment}
*/
\end{comment}

\begin{comment}
#include <stdio.h>
#include <stdlib.h>
#include <meep.h>
using namespace meep;

double eps(const vec &) {
  return 1.0;
}
const int rad = 10;
const int ttot = 1500*rad;
\end{comment}

In this example we calculate the lowest four TE modes of a simple hollow
metallic waveguide of radius one.

\begin{verbatim}
int main(int argc, char *argv[]) {
  initialize mpi(argc, argv);
  file *ban = everyone_open_write("bands");
  structure s(volcyl(1.0, 0.0, rad), eps);
  for (int m=0;m<3;m++) {
    for (double k=0.0; k<= 1.01; k += 0.25) {
      master_printf("Working on k of %g and m = %d with a=%d...\n",
                    k, m, rad);
      fields f(&s, m);
      f.use_bloch(k);
\end{verbatim}

There are a few tricks you should know before you decide to go about
calculating a band structure.  One of the biggest problems in calculating a
band structure in a time domain code is that of exciting all the modes you
are interested in.  Meep makes this easy with a couple of ``fields''
methods, \verb-initialize_with_n_te-, and \verb-initialize_with_n_tm-.
These initialize the field with the n lowest TE and TM modes respectively.

\begin{verbatim}
      f.initialize_with_n_te(4);
\end{verbatim}

The band structure code itself begins with a call to
\verb-prepare_for_bands-, which allocates space to store the field
data, which is later used for the band structure calculation.  Its third
argument is the maximum frequency you are interested in.

\begin{verbatim}
      double fmax = 1.0, qmin = 200;
      f.prepare_for_bands(0, ttot, fmax, qmin);
      for (int t=0;t<ttot;t++) {
\end{verbatim}

The second band structure function is \verb-record_bands-, which just
copies the fields into the already allocated arrays for future use.

\begin{verbatim}
        f.record_bands();
        f.step();
      }
\end{verbatim}

Finally, the band structure is actually computed and output by the method
\verb-output_bands-.  The key thing to know about
\verb-output_bands- is that its last argument should be something like
twice the number of modes which have a frequency below your maximum.
Rounding this number up slows the code down considerably, but can sometimes
fix problems where harminv (which is used internally) doesn't find the
modes correctly.  Usually, however, when harminv fails it means you are
misunderstanding something (for example, fmax may be less than the lowest
frequency mode).

\begin{verbatim}
      f.output_bands(ban, "band", 35);
    }
  }
  everyone_close(ban);
}
\end{verbatim}