% -*- mode: tex; mode: fold -*-
\documentclass{article}
\newcommand{\HRMA}{{\bf HRMA}}
\newcommand{\AXAF}{{\bf AXAF}}
\newcommand{\eq}[1]{(\ref{#1})}
\newcommand{\p}{\hat{p}}
\newcommand{\x}{\vec{x}}
\newcommand{\pperp}{\vec{p}_{\perp}}
\newcommand{\pperpp}{\pperp\,\!\!\!\!'}
\newcommand{\marx}{{\bf marx}}
\newcommand{\marxcat}{{\bf marxcat}}
\renewcommand{\d}[1]{\mbox{d}#1\;}

\usepackage{amsmath}
\usepackage{epsfig}

\begin{document}

\section{Technical Overview}
\subsection{Introduction}
 The purpose of this section is to provide technical information about
 the implementation of the various subsystems of \marx{}.

\subsection{Source Models} %{{{
\marx{} provides support for both extended and point sources, which can
be either on or off-axis.  The sources can have a flat energy spectrum
or they can use an energy spectrum supplied by the user.  For even
more flexibility, \marx{} has the ability to dynamically link to
user-defined source models.  Finally, the \marxcat{} program may be used
to merge simulations to form a composite simulation of multiple sources.

\marx{} supports the following source types: 
\begin{itemize}
  \item {\tt POINT}\\
     This is simply a point source.

  \item {\tt LINE}\\
     This source is mainly used for diagnostic purposes.

  \item {\tt GAUSS}\\
     This is an extended source that has a Gaussian spatial profile.
     
  \item {\tt BETA}\\
     This source represents the so-called beta model that is used for
     modeling galaxy clusters.

  \item {\tt RAYFILE}\\
     This is a special source that represents partially processed rays.

  \item {\tt DISK}\\
     This source is useful for modeling extended objects such as
     a supernova.

  \item {\tt USER}\\
     This source refers to a user-defined source that is dynamically
     loaded.
\end{itemize}

  These sources will be discussed in detail later.  First, it is
  important to have an understanding of what a source means in the
  context of \marx{}.  It is particularly important to understand how
  \marx{} interacts with a source model for the proper implementation of
  a user-defined source.

  As far as \marx{} is concerned, a source is characterized by the
  distribution of rays in energy, time, and direction that it produces
  at the entrance aperture of the telescope.  Implicit in this
  characterization is the assumption that the source is sufficiently
  far away that the flux does not vary with position across the
  telescope.  

  To be more precise, let
\begin{equation}
     F(t,E,\p,\x) (\p\cdot\d{\vec{A}}) \d{\Omega} \d{E} \d{t}
\end{equation}
  denote the number of rays with energy between $E$ and $E + dE$, and
  whose directions lie in a solid angle $d\Omega$ about the direction
  $\p$, crossing an area element $\d{\vec{A}}$ at $\x$ during the time
  $dt$. That is, $F(t,E,\p,\x)$ is a space-time dependent flux
  density.  If the source is very far away, as is the case for objects
  of astrophysical interest, the spatial dependence of the flux
  density may be ignored when $\x$ refers to points near the
  telescope.  For this reason, $F$ will be assumed to be independent
  of $\x$. Similarly, since the angular acceptance of the telescope is
  very small (on the order of arc-minutes), to first order we need
  only consider area elements $\d{\vec{A}}$ whose normals are in the
  direction of the source.  Hence, $\p\cdot\d{\vec{A}}$ will simply be
  written as $\d{A}$.

  We can consider various classes of source models according to
  whether or not $F$ factors.  For example, all \marx{} sources with
  the exception of the \verb|USER| source produce fluxes that are
  time-independent with an energy spectrum that is uncorrelated with
  the direction of the flux (see section \ref{usersource} for more
  information about the \verb|USER| source). For the rest of this
  section, we will only consider the class of models for which $F$ can
  be written
\begin{equation}
     F(t,E,\p) = f(t) \cdot f_E(E) \cdot f_p(\p).
\end{equation}
  When such a factorization is possible, it will be assumed that $f_E(E)$
  and $f_p(\p)$ are normalized, i.e.,
\begin{equation}
     1 = \int_{\Omega} \d{\Omega} f_p(\p),
\end{equation}
  and 
\begin{equation}
     1 = \int\d{E} f_E(E) .
\end{equation} 
  With this normalization,
\begin{equation} 
    f(t) = \int \d{E} \int_{\Omega} \d{\Omega} F(t, E, \p)
\end{equation}
 is simply the total flux, which can depend upon time.
 In the following, we shall call $f_E$ the {\em energy spectrum}, and
 $f_p$ the {\em angular distribution} of the source.

 In the \marx{} parameter file, \verb|marx.par|, the parameter
 \verb|SpectrumType| is used to specify the function $f_E$. Currently,
 $f_E$ can only be a \verb|FLAT| spectrum or a \verb|FILE| spectrum.
 Similarly, when $f(t)$ is time-independent, as it for all \marx{}
 sources in this class, then its value is specified by the
 \verb|SourceFlux| parameter.  See section \ref{spectrum} for more
 information about these parameters.

 The distribution function $f_p(\p)$ characterizes the angular
 distribution of the flux and, hence, the angular distribution of the
 source.  \marx{} assumes that this distribution function specifies an
 on-axis source and that the source can be moved off-axis via the
 \verb|SourceElevation| and \verb|SourceAzimuth| parameters.

 By convention, $f_p(\p)$ is assumed to be normalized to unity, i.e.,
\begin{equation}
  1 = \int_{0}^{\pi} \sin\theta \d{\theta}
      \int_0^{2\pi} d{\phi} f_p(\theta, \phi) ,
\end{equation}
 where $\p$ has been expressed in spherical coordinates.  For an
 azimuthally symmetric source, $f_p$ is independent of $\phi$ and 
 the normalization condition reduces to
\begin{equation}
  1 = 2\pi \int_{0}^{\pi} \d{\theta} \sin\theta  f_p(\theta) .
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{POINT Source}

 The \verb|POINT| source corresponds to an angular distribution
 function given by
\begin{equation} 
    f_p(\theta, \phi) = \frac{1}{2\pi} \delta (\phi) 
      \delta(1 - \cos \theta)
\end{equation}
 
\subsection{LINE Source}
 The \verb|LINE| source corresponds to a to an angular distribution
 function given by
\begin{equation}
  f_p(\theta, \phi) = \frac{1}{\theta_0\theta}\cdot
     \frac{1}{2} \big[\delta(\phi - \phi_0) 
          + \delta(\phi - \phi_0 - \pi) \big]
\end{equation}
 for $\theta < \theta_0$ and zero otherwise.  Here $\theta_0$ may be
 set via the \verb|S-LineTheta| parameter and the \verb|S-LinePhi|
 controls the value of $\phi_0$.

\subsection{GAUSS Source}
 The \verb|GAUSS| source corresponds to a to an angular distribution
 function given by
\begin{equation}
  f_p(\theta, \phi) = \frac{1}{\pi} e^{-\theta^2/\theta_0^2}
\end{equation}
 where $\theta_0$ determines the width of the Gaussian distribution.
 This value may be set via the \verb|S-GaussSigma| parameter.

\subsection{BETA Source}
 The \verb|| source corresponds to a to an angular distribution
 function given by
\begin{equation}
  f_p(\theta, \phi) = \frac{1}{2\pi}
    \cdot 
      \frac{6}{\theta_c}(\beta - \frac{1}{2})
      \big[ 1 + (\frac{\theta}{\theta_c})^2 \big]^{-3\beta + \frac{1}{2}}.
\end{equation}
 The core radius, $\theta_c$ is specified by the
 \verb|S-BetaCoreRadius| parameter, while \verb|S-BetaBeta| controls
 the value of $\beta$.
 This distribution is used to model galaxy clusters.

\subsection{DISK Source}
 The \verb|DISK| source corresponds to a to an angular distribution
 function given by
\begin{equation}
  f_p(\theta, \phi) = \frac{1}{2\pi}
       \cdot \frac{2}{\theta_1^2 - \theta_0^2}
\end{equation}
 for $\theta_0 <= \theta < \theta_1$.  Outside this region, it is zero.
 The values $\theta_0$ and $\theta_1$ correspond to the parameter file
 parameters \verb|S-DiskTheta0| and \verb|S-DiskTheta1|, respectively.
 This source actually generates a ring structure and is useful for
 modeling a supernova remnant.

\subsection{USER Source}

 The \verb|USER| source is the most versatile of the \marx{}
 sources. This is because it is a user-defined source where each ray
 may be given an independent energy, time, and direction.  This means
 that one does not need to demand that the flux density factorize as
 was assumed for the other \marx{} sources.  This permits one to
 simulate sources whose spectrum changes with time, complex extended
 objects, etc.

 A user-defined source model must be created by the user using a
 language such as C and then compiled as a shared object.  During
 run-time, \marx{} will dynamically link to this shared object and use
 it to generate rays.  To use this source, first and foremost, the
 underlying operating system must support dynamic linking.  Operating
 systems such as Linux and Solaris support dynamic linking while
 others such as NeXT do not.  It is important to understand that
 creating a user-defined source does not mean that \marx{} must be
 recompiled.  If that were the case, then there would be no value to a
 user-defined source.

 Creating a such a source is relatively simple and is best
 accomplished using the C programming language.  The C source file must
 define three functions that \marx{} will call during run-time: 
\begin{verbatim} 
   user_open_source
   user_close_source
   user_create_ray
\end{verbatim}
 The \verb|user_open_source| function will be called by \marx{} before
 any rays are generated.  The purpose of this function is to
 initialize any data structures required by the \verb|user_create_ray|
 function.  The \verb|user_create_ray| function will be called one
 time for each ray generated.  The purpose of this routine is to
 assign an energy, time, and direction to a ray.  Finally, the
 \verb|user_close_source| function will be called when \marx{} has
 finished processing rays.  Each of these functions are described
 in more detail below.

\subsubsection{user\_open\_source}

 The \verb|user_open_source| function has the prototype:
\begin{verbatim}
  int user_open_source (char **argv, int argc,
                        double area,
                        double cosx, 
                        double cosy, 
                        double cosz);
\end{verbatim}
 The value of the \verb|marx.par| parameter \verb|UserSourceArgs| will
 be broken into an array of whitespace separated strings and passed to
 \verb|user_open_source| via the \verb|argv| parameter.  The parameter
 \verb|argc| indicates the number of such strings.  The actual meaning
 of these strings will depend upon the details of the user-defined
 source.  For example, if the user-defined source needs to read an
 external data file, the parameter can represent the name of the data
 file.

 The \verb|area| parameter specifies the area in cm$^2$ of the entrance
 aperture of the mirror.  Knowledge of this value is necessary to
 compute the time interval between rays since the incoming flux must
 be multiplied by this value to generate the total incoming photon rate.

 The other three parameters \verb|cosx|, \verb|cosy|, and \verb|cosz|
 are the direction cosines of a ray from a reference point on the
 source to the origin of the \marx{} coordinate system.  These numbers
 are derived from the \marx{} parameter file \verb|SourceElevation|
 and \verb|SourceAzimuth| parameters.  For an on axis source,
 \verb|cosy| and \verb|cosz| will be set to zero, but \verb|cosx| will
 be set to \verb|-1|. If the reference point of the user defined
 source is always on axis, these parameters may be ignored and the
 actual parameter values for \verb|SourceAzimuth| and
 \verb|SourceElevation| will have no affect on the rays generated by
 source.  However, if one would like to position the source off-axis
 via the SourceElevation and SourceAzimuth parameters, the values of
 the direction cosines will need to be taken into account.  An example
 of this is presented below.

 Upon success, \verb|user_open_source| must return \verb|0|.  If for any
 reason it fails, e.g, unable to open a file, it must return \verb|-1|.

 The simplest example of \verb|user_open_source| is one which does nothing:
\begin{verbatim} 
  int user_open_source (char **argv, int argc,
                        double cosx,
                        double cosy, 
                        double cosz)
  {
     return 0;   /* Success */
  }
\end{verbatim}   

\subsubsection{user\_close\_source}

 The \verb|user_close_source| function has the prototype:
\begin{verbatim} 
   void user_close_source (void);
\end{verbatim} 
 Its purpose is to free up any resources acquired by the source.  For
 example, if the source dynamically allocated memory,
\verb|user_close_source| should deallocate it.

\subsubsection{user\_create\_ray}

 The \verb|user_create_ray| function is the function that actually
 defines the source by endowing each ray with a direction, energy, and
 time.  It has the following prototype:
\begin{verbatim} 
   int user_create_ray (double *delta_t, double *energy,
                        double *cosx, double *cosy, double *cosz);
\end{verbatim} 
 Since the purpose of this routine is to assign a ray an energy, time,
 and direction, the parameters are actually pointer types and the
 requested information is passed back to the calling routine via the
 parameter list.  It is important to note that the ray is completely
 undefined prior to calling this function.

 The \verb|delta_t| parameter is used to give the ray a time-stamp.
 Actually it does not refer directly to the absolute time of the ray;
 rather, its value should refer to the time since the last ray was
 generated.  For example, if a ray is generated every second,
\begin{verbatim} 
   *delta_t = 1.0;
\end{verbatim}
 should be used.  If \verb|*delta_t| is set to \verb|-1.0|, then
 \marx{} will generate the time based on the \verb|SourceFlux|
 parameter. Otherwise, the value should be set in a manner consistent
 with the flux and the geometric area of the mirror.

 The meaning of the other parameters that specify the energy and
 direction cosines should be rather clear.  If \verb|energy| is set to
 \verb|-1.0|, then \marx{} will use the setting of the
 \verb|SpectrumType| parameter to assign an energy to the ray.

\subsubsection{Compiling a User-Defined Source}
 The procedure for compiling a user-defined source as a shared object
 will depend upon the operating system.  For details, consult you
 compiler and linker manual.  For the purposes of this section, it
 is assumed that the file containing the code for the
 user-defined source is called \verb|mysource.c|.  This may be
 compiled as a shared object under {\bf Linux} using \verb|gcc| via
 the command:
\begin{verbatim} 
    gcc -shared mysource.c -o mysource.so
\end{verbatim}
 If \verb|mysource.c| requires other libraries, they should also be
 included on the command line.  The syntax is slightly different under
 {\bf Solaris}:
\begin{verbatim} 
    cc -G mysource.c -o mysource.so
\end{verbatim}
 
 To actually use the source in \marx{}, set the \verb|marx.par|
 parameter \verb|SourceType| to \verb|USER| and also set the parameter
 \verb|UserSourceFile| to point to the full absolute filename for
 \verb|mysource.so|.  It is usually necessary to use an absolute
 filename because of the way the dynamic linker searches for shared
 objects.  Finally, set the parameter \verb|UserSourceArgs| to a value
 that is appropriate to your source.
 
 If running \verb|marx| using your dynamically linked source causes it
 to crash, do not assume that the bug is in \marx{}.  Rather, it is
 most likely a bug in your code.  Make sure that the interface
 routines are properly prototyped and that the routines return the
 proper values to \marx{}.  If you use dynamic memory allocation, check
 the return status of routines such as \verb|malloc|.  Finally, look
 at the examples provided with the \marx{} distribution and try to run
 those.

\subsubsection{Examples of User-Defined Sources}
 The simplest source is that of a point source.  Although \marx{}
 already provides built-in support for this source, it is instructive
 to write it as a user-defined source.  Here is the complete C code
 for such a source:

\begin{verbatim} 
#include <stdio.h>

static double Source_CosX;
static double Source_CosY;
static double Source_CosZ;

int user_open_source (char **argv, int argc, double area,
                      double cosx, double cosy, double cosz)
{
   Source_CosX = cosx;
   Source_CosY = cosy;
   Source_CosZ = cosz;
   return 0;
}

void user_close_source (void)
{
}


int user_create_ray (double *delta_t, double *energy,
                     double *cosx, double *cosy, double *cosz)
{
   *cosx = Source_CosX;
   *cosy = Source_CosY;
   *cosz = Source_CosZ;

   *delta_t = -1.0;
   *energy = -1.0;
   
   return 0;
}
\end{verbatim}

 First of all, note that \verb|energy| and \verb|delta_t| have been
 set equal to \verb|-1.0| in \verb|user_create_ray|.  This indicates
 to \marx{} that it should compute the time and energy of the ray via
 the \verb|SpectrumType| and \verb|SourceFlux| parameters.  For this
 reason, the \verb|area| parameter was not used by
 \verb|user_open_source|.  Since the direction cosines passed to
 \verb|user_open_source| refers to the vector from the position of the
 source to the origin where the telescope is located, those values
 were saved and used in \verb|user_create_ray|.

 For more complex examples, look at the files under
 \verb|marx/doc/examples| in the \marx{} distribution.


%}}}

\subsection{Mirror Modules}

\subsection{Grating Modules} %{{{

 The \AXAF{} instrument contains two distinct grating assemblies
 called the HETG and the LETG.  The HETG is meant to be used for high
 energy xrays and the LETG is optimized for low energy xrays.
 Actually, the HETG consists of two types of gratings: MEG for medium
 energy rays, and HEG for high energy rays.  The LETG consists entirely
 of LEG type gratings.  Each grating facet is arranged such that its
 geometric center lies on a rowland torus.  The MEG torus is rotated
 by +5 degrees with respect to the LEG torus, and the HEG torus is
 rotated by -5 degrees with respect to the LEG torus.
 
 After a ray leaves the mirror it travels towards the detector.
 If the gratings are being used, the ray will intersect the grating
 and undergo a diffraction process.  Actually, a certain percentage of
 the rays will not strike a grating facet; instead some will be
 absorbed by the grating assembly.  This percentage that intersect
 with a facet is specified by the appropriate vignetting parameter,
 \verb|LETGVig| if the LETG is being used, or \verb|HETGVig| if the
 HETG is used.
 
 \marx{} currently knows very little about the actual location of
 individual grating facets.  The assumption is that the HRMA and the
 grating assembly is aligned such that the probability of a ray
 striking a facet is maximized, and the percentage that miss is
 controlled by the vignetting factor.
 
\subsubsection{Intersection with the Rowland Torus} %{{{
 
 The {\bf Rowland Torus} is defined by the equation
\begin{equation} 
  (x^2 + y^2 + z^2)^2 = 4 R^2 (x^2 + z^2) \label{eq:torus}
\end{equation}
 where $R$ is the Rowland radius.  To determine the intersection of a ray
 with the torus, \eq{ray} is substituted into the equation for the torus.
 This yields the fourth order equation for $t$
\begin{equation}
\begin{split}
 0 = t^4 &+ 4 (\p\cdot\x_0)t^3  \\
         &+ 2t^2 \big(|\x|^2 + 2(\p\cdot\x_0)^2 - 2R^2(p_x^2 + p_z^2)\big) \\
         &+ 4t \big(|\x_0|^2 (\p\cdot\x_0) - 2R^2(p_x x_0 + p_z z_0)\big)  \\
         &+ |\x_0|^4 - 4R^2 (x_0^2 + z_0^2)  \label{quartic}
\end{split}
\end{equation}
 where the vector $\x_0$ has components $(x_0, y_0, z_0)$.
 The four roots of this equation are a manifestation of the fact that a line
 can intersect the torus at four different places.

 An important case is when $\x_0 = \vec{0}$ where an enormous simplification
 occurs and the equation reduces to
\begin{equation} 
   0 = t^4 - 4R^2t^2(p_x^2 + p_z^2).
\end{equation}
 This equation has a double root at $0$ and non-zero roots at
\begin{equation} 
   t = \pm 2R\sqrt{p_x^2 + p_z^2}.  \label{t0}
\end{equation}
 In the coordinate system employed in this work, rays travel in the negative
 $x$ direction from the \HRMA\ to the torus.  This means that the solution
 of interest is the {\em most negative} root of \eq{quartic}.  Such a root
 corresponds to the first intersection point of the ray with the torus.

 Even if $\x_0$ is non-zero, one can always project the ray to the $x = 0$
 plane to make the component $x_0 = 0$.  One can then argue that for \AXAF\
 the remaining two components $z_0$ and $y_0$ will be small (i.e., $z_0<<R$)
 since the rays from the \HRMA\ will be converging to the focal point
 located at the center of the torus.  The upshot is that \eq{t0} is a good
 zeroth order approximation to the exact solution and that one can use this
 value as the starting point in an iterative solution\footnote{Although a
 closed form solution exists for the quartic equation, Newton's method is
 used here.} to \eq{quartic}.

 Let $t$ be the solution to the equation $0 = f(t)$ and let $t_0$ represent
 an approximate root.  If $\delta t = t - t_0$, then a taylor expansion yields
\begin{equation*}
\begin{split}
   0 &= f(t) \\
     &= f(t_0 + \delta t) \\
     &= f(t_0) + \delta t f'(t_0) + \cdots 
\end{split}
\end{equation*}
 or
\begin{equation} 
   t = t_0 - \frac{f(t_0)}{f'(t_0)} + \cdots.
\end{equation} 
 Newton's method follows from the last expression as an iterative solution
 of the form
\begin{equation} 
   t_{k+1} = t_k - \frac{f(t_k)}{f'(t_k)}. \label{newton}
\end{equation}
 For the quartic equation 
\begin{equation} 
  0 = t^4 + at^3 + bt^2 + ct + d,
\end{equation} 
 Newton's method yields the iterative scheme
\begin{equation} 
 t_{k+1} = \frac{(3t_k^2 + 2at_k + b)t_k^2 - d}%
                {(4t_k + 3a)t_k^2 + 2bt_k + c}  \label{iterat}
\end{equation}
 From \eq{quartic}, it follows that
\begin{equation}
\begin{split}
   a &= 4\p\cdot\x_0 \\
   b &= 2|\x|^2 + 4(\p\cdot\x_0)^2 - 4R^2(p_x^2 + p_z^2) \\
   c &= 4|\x_0|^2 \p\cdot\x_0 - 8R^2 p_z z_0 \\
   d &=  |\x_0|^4 - 4R^2 z_0^2  
\end{split}
\end{equation} 
 where $x_0$ has been set to zero in accordance with the understanding that
 {\bf the ray has been projected to the $x = 0$ plane}.  This means that
\begin{equation} 
   t_0 = -2R\sqrt{p_x^2 + p_z^2}
\end{equation} 
 should be used to seed \eq{iterat}.

 The previous analysis is fine for the LETG.  However the HETG consists of
 an MEG torus {\em and} an HEG torus.  The MEG torus differs from the LEG
 torus by a rotation of $-5$ degrees about the $x$ axis.  Similarly, the HEG
 torus is rotated in $5$ degrees the other direction.  In the following, we
 consider the more general case of a torus that is rotated by an angle
 $\theta$ about the $x$ axis.  

 Let ${\cal R}(\theta)$ represent a rotation about the $x$ axis by an angle
 theta.  It takes a vector $\vec{v}$ and transforms it into a new vector
 $\vec{v'}$ via 
\begin{equation}
   \vec{v}' = {\cal R}(\theta) \vec{v} \label{rotation}
\end{equation}
 where the components of $\vec{v}'$ satisfy
\begin{align}
   v_x' &= v_x \nonumber \\
   v_y' &= v_y \cos\theta + v_z \sin\theta \\
   v_z' &= -v_y \sin\theta + v_z\cos\theta.
\end{align}

 At this point \eq{rotation} could be applied to points on the torus to
 obtain a rotated version of \eq{eq:torus} and the preceeding analysis be
 repeated with the new, more complicated, equation.  However, it is easier
 to work in a rotated coordinate system where the equation of the torus
 retains its form given in \eq{eq:torus}.  So, the prescription for
 computing the intersection with a rotated torus looks like this:
%\begin{itemize}
\begin{enumerate}
  \item  After projecting $\x_0$ to the $x = 0$ plane, rotate $\x_0$ and $\p$
  via ${\cal R}(-\theta)$.
  \item  Perform the intersection calculation outlined above using the
  rotated values of $\x_0$ and $\p$.  This calculation will result in the
  intersection point $\x$ with components expressed in the rotated frame.
  \item Rotate all vectors back using ${\cal R}(\theta)$.  The result will
  be that the intersection point $\x$ will be expressed in the unrotated
  frame. 
\end{enumerate}
%\end{itemize}

 To illustrate this procedure, consider the special case of $\x_0 = 0$.  In
 the unrotated case, we found \eq{t0} as the solution.  For a rotation by an
 angle $\theta$, the solution in the rotated frame will be
\begin{align}
    \x' &= \p' t_0  \nonumber \\
                 &= -2R \p' \sqrt{p_x^2 + (p_z\cos\theta + p_y\sin\theta)^2}
                     \nonumber \\
\end{align}
which when rotated back to the original frame yields
\begin{equation} 
    \x = -2R \p\sqrt{p_x^2 + (p_z\cos\theta - p_y\sin\theta)^2}
                     \nonumber.
\end{equation}
 
%}}}

\subsubsection{Diffraction of the Ray} %{{{
 Consider a ray with wavelength $\lambda$ and direction $\p$ incident upon a
 diffraction grating of period $d$ located at position $\x$ and normal
 $\hat{n}$.  The grating lines are assumed to oriented in the direction
 $\hat{l}$.  See Figure \ref{fig:grating}.  It will be shown in another work
 that a ray diffracting into order $m$ will move in a direction $\p'$
 determined by the conditions%
\footnote{These equations are consistent with the vector equation
  $\p'\times\hat{n}=\p\times\hat{n} + (m\lambda/d)\hat{l}$.  The first
  equation is a simple result of taking the cross product of this equation
  with $\hat{l}$.  The second one follows from taking the dot product of
  the equation with $\hat{l}$.}
\begin{align}
    \label{diffract0}
    \p'\cdot\hat{l} &= \p\cdot\hat{l} \\
    \p'\cdot\hat{d} &= \p\cdot\hat{d} + \frac{m\lambda}{d}
\end{align}
 where 
\begin{equation} 
    \hat{d} = \hat{n} \times \hat{l}.
\end{equation}

\begin{figure}
\begin{center}
\epsfig{file=grating.eps,height=4cm}
\caption{Figure showing the orthogonal coordinate system local to an
individual gratig facet.  The vector $\hat{n}$ is normal to the facet and
$\hat{l}$ is in the direction of the grating lines.  The vector $\hat{d}$ is
in the dispersion direction.  The incident ray is given by $\p$ and
the diffracted ray is $\p'$.}
 \label{fig:grating}
\end{center}
\end{figure}

 Since $\hat{n}$, $\hat{l}$, and $\hat{d}$ form a right-handed orthonormal
 coordinate system, it trivially follows that
\begin{equation} 
  \p' = (\p\cdot\hat{l})\hat{l}
        + (\p\cdot\hat{d} + \frac{m\lambda}{d})\hat{d}
        + \hat{n} \sqrt{1  
                       - (\p\cdot\hat{l})^2 
                       - (\p\cdot\hat{d} + \frac{m\lambda}{d})^2}.
                   \label{diffracted}
\end{equation}
 After diffraction, the ray will travel along the trajectory
\begin{equation} 
   \x(t) = \x + \p't.
\end{equation}
 Note that \eq{diffracted} may be put into a more familiar form as
 follows.  Since the component of the ray in the $\hat{l}$ direction is not
 changed by the grating, the effect of the diffraction is simply a
 rotation of $\p$ about the $\hat{l}$ axis by some angle.  Let
 $\pperp$ denote the projection of $\p$ onto the
 $(\hat{d},\hat{n})$ plane, and let $\theta$ be the angle between
 $\pperp$ and $\hat{n}$.  Define $\pperpp$ and $\theta'$ in a similar
 fashion (see Figure \ref{fig:simplediffraction}).
\begin{figure}
\begin{center}
\epsfig{file=diffract.eps,height=4cm}
\caption{Diffraction in the $(n,d)$ plane.  Here $\theta$ is the
angle the projection of the incoming ray onto the $(\hat{d}\hat{n}$
makes with respect to the normal, and $\theta'$ is the angle between
the normal and the projection of the outgoing ray.}
\label{fig:simplediffraction}
\end{center}
\end{figure}
 It follows from 
 \eq{diffract0} that
\begin{equation}
   p_{\perp} \sin \theta' = p_{\perp} \sin \theta - \frac{m\lambda}{d},
\end{equation}
 where $p_{\perp} = |\pperp| = |\pperpp|$.  In fact, the previous
 equation is reduces to the well known diffraction equation when $\p$
 has no component in the $\hat{l}$ direction.  Using these
 definitions, one can write \eq{diffracted} in the form
\begin{equation} 
  \p' = (\p\cdot\hat{l})\hat{l}
        - (p_{\perp} \sin{\theta'}) \hat{d} 
        + (p_{\perp} \cos{\theta'}) \hat{n}.
\end{equation} 

 In general, $\hat{n}$ and $\hat{l}$ are complicated functions of the
 position of the grating.  However, for gratings of infinitesimal
 size\footnote{For finite size facets, the grating normal will have to be
 looked up in a facet database.} positioned on the surface of the Rowland
 torus, $\hat{n}$ will be directed towards the origin, i.e.,
\begin{equation}
    \hat{n} = -\frac{\x}{|\x|}
\end{equation}
 Similarly, $\hat{l}$ may be determined from the condition that the facets
 are arranged such that $\hat{l}$ has no $y$ component\footnote{We are
 working in the natural coordinate system of the torus.  Thus these
 equations hold for the LETG and the HETG.} and that it is normal to
 $\hat{n}$.  That is,
\begin{equation*}
\begin{split}
   0 &= \hat{l}\cdot\hat{y} \\
   0 &= \hat{l}\cdot\hat{n} \\
   1 &= |\hat{l}|
\end{split}
\end{equation*} 
 from which it follows that
\begin{equation} 
  \hat{l} = \frac{1}{\sqrt{n_x^2 + n_z^2}} 
              \begin{pmatrix} 
                 n_z\\
                 0\\
                 -n_x
              \end{pmatrix}.
\end{equation} 

 Since the LETG gratings have a support structure that also acts as a
 diffraction grating, we need to consider a more general orientation of the
 $\hat{l}$ axis that consists of a rotation about the $\hat{n}$ axis by some
 angle $\theta$.  This means that the rotated vectors,
\begin{align}
  \hat{l}_{\theta} &= \hat{l} \cos\theta + \hat{d}\sin\theta \\
  \hat{d}_{\theta} &= -\hat{l} \sin\theta + \hat{d}\cos\theta,
\end{align}
 should be used in \eq{diffracted} to yield
\begin{equation} 
  \p' = (\p\cdot\hat{l}_{\theta})\hat{l}_{\theta} 
        + (\p\cdot\hat{d}_{\theta} + \frac{m\lambda}{d})\hat{d}_{\theta}
        + \hat{n} \sqrt{1  
                       - (\p\cdot\hat{l}_{\theta})^2 
                       - (\p\cdot\hat{d}_{\theta} + \frac{m\lambda}{d})^2}.
\end{equation}


%}}}


%}}}

\subsection{Detector Modules}

\end{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\subsection{\verb|POINT| Source}
 The \marx{} \verb|POINT| source is characterized by the angular flux
 function
\begin{equation} 
    f_p^(\mbox{point}) = 

%%%%%%%%%%%%%%%%%%%

 \marx{} uses a {\em Monte Carlo} procedure for generating rays
 whose direction is consistent with the spatial distribution of the
 source.   Let $\rho(\theta, \phi)$ be the joint {\em probability
 density} for the distribution of rays.  This means that probability of a
 ray emerging from a region whose polar angle lies between $\theta$ and
 $\theta + d\theta$, and whose azimuth is between $\phi$ and
 $\phi + d\phi$, is $\rho(\theta, \phi) d\theta\ d\phi$.  Since the
 probability is unity for a ray to come from some direction, the
 distribution is normalized
 \begin{equation} 
    1 = \int_{0}^{\pi} d\theta \int_0^{2\pi} d\phi\; \rho(\theta, \phi) .
 \end{equation}
 It must be emphasized that the probability density $\rho(\theta,\phi)$
 is {\em not} the same as the angular distribution $f_p(\p)$.
 In fact, it is easy to see that they are related by the expression
\begin{equation}
   \rho(\theta, \phi) = \sin(\theta) f_p(\theta, \phi).
\end{equation}
 where $\p$ has been expressed in spherical coordinates.

 Let the random variables $\Theta$ and $\Phi$ be distributed according
 to the probability density $\rho(\theta, \phi)$ and denote the
 probability that $\Theta$ is less than $\theta$, while $\Phi$ is less
 than $\phi$, by
\begin{equation}
  {\cal P}(\Theta < \theta, \Phi < \phi) =
    \int_{0}^{\theta} d\theta' \int_0^{\phi} d\phi'\; \rho(\theta', \phi')
\end{equation}
 The quantity ${\cal P}$ is called the {\em probability distribution}.

 If one only wants the probability distribution for the random variable
 $\Theta$ {\em independent} of the value of $\Phi$, then the appropriate
 distribution is
\begin{equation}
  {\cal P}(\Theta < \theta) = C(\theta) =
    \int_{0}^{\theta} d\theta' \int_0^{2\pi} d\phi'\; \rho(\theta', \phi')
\end{equation}
 where $C(\theta)$ is the value of the integral (this is sometimes
 called the {\em cumulative distribution}).

 Since the density $\rho(\theta, \phi) \ge 0$, it follows that the
 previous equation may be inverted; this is represented symbolically by
\begin{equation}
   \theta = C^{-1}({\cal P}).
\end{equation}
 It is well known that if ${\cal U}$ is a random variable distributed
 according to a {\em uniform} probability distribution on the unit
 interval, then the random variable $\Theta$ defined by
\begin{equation}
   \Theta = C^{-1}({\cal U})
\end{equation}
 will be distributed with the probability distribution $C(\theta)$.  In
 fact, this basic result is the basis for numerically computing
 random numbers distributed according to some distribution, from a
 uniform distribution of random numbers.

