% arara: pdflatex
% arara: pdflatex

% start with vim --server latex %
\documentclass[]{article}

\usepackage{siunitx}
\usepackage{physics}
\usepackage{graphicx}
\usepackage{fullpage}

\DeclareMathOperator\erfc{erfc}

\author{C.D. Clark III}
\title{A Green's Function based model for retinal temperature rise from laser exposure}

\begin{document}
\maketitle

The heat equation describing heat conduction in a material with source term is
\begin{equation*}
    \rho c \pdv{T}{t} = \div{k\grad T} + A(\vec{r},t).
\end{equation*}
Here, $T\qty(\vec{r},t)$ is the temperature field and $A\qty(\vec{r},t)$ is the
source term. The thermal properties, $\rho$, $c$, and $k$ (density, specific
heat, and thermal conductivity) are in general function of space and time, but
for a homogeneous media, this can be rewritten as:
\begin{equation*}
    \pdv{T}{t} = \alpha\laplacian{T} + \frac{A(\vec{r},t)}{\rho c}
\end{equation*}
where $\alpha = \frac{\kappa}{\rho c }$ is the thermal diffusivity.
The Green's function for the heat equation, expressed in Cartesian coordinates, is
\begin{equation*}
    G(x,y,z,t,x',y',z',t') = \qty(\frac{1}{4\pi \alpha (t-t')})^{3/2} e^{-\frac{(x-x')^2+(y-y')^2+(z-z')^2}{4\alpha(t-t')}}.
\end{equation*}
This function describes the thermal response of the media to an instantaneous
point source delivered at the point $(x',y',z')$ and time $t'$, and it can
be used to compute the response of the media to the source term $A$,
\begin{equation*}
  T(x,y,z,t) = \int_0^t \int_{-\infty}^{\infty} \int_{-\infty}^{\infty} \int_{-\infty}^{\infty} G(x,y,z,t;x',y',z',t') \frac{A(x',y',z',t')}{\rho c} \dd x' \dd y' \dd z' \dd t'
\end{equation*}
A laser beam incident on a thin, linear absorbing layer of tissue will produce a spatial source term
\begin{equation*}
    A(x',y',z') = \mu_a E(x',y') e^{-\mu_a z'} = \mu_a E_0 \bar{E}(x',y') e^{-\mu_a z'}
\end{equation*}
while the laser is on,
where $\bar{E}$ denotes the normalized beam profile ($\bar{E}(0,0,) = 0$) and $E_0$ is the irradiance at the center of the beam.
For a circular flat top beam of radius $R$, this is
\begin{equation*}
    \bar{E}(x',y') = \begin{cases}
        1 & x'^2 + y'^2 \le R^2 \\
        0 & x'^2 + y'^2 > R^2
    \end{cases}
\end{equation*}
For a circular Gaussian beam with 1/e radius $\sigma$, this is 
\begin{equation*}
    \bar{E}(x',y') = e^{-\frac{x'^2 + y'^2}{\sigma^2}}
\end{equation*}
Let the absorbing layer have a thickness $d$ and span from $z_0$ to $z_0 + d$. Then the temperature
by the absorbing layer while the laser is on will be
\begin{equation*}
  T(x,y,z,t) = \int_0^t \int_{z_0}^{z_0 + d} \int_{-\infty}^{\infty} \int_{-\infty}^{\infty} \qty(\frac{1}{4\pi \alpha t'})^{3/2} e^{-\frac{(x-x')^2+(y-y')^2+(z-z')^2}{4\alpha t'}} \frac{\mu_a E(x',y') e^{-\mu_a (z'-z_0)}}{\rho c} \dd x' \dd y' \dd z' \dd t',
\end{equation*}
where we have used the fact that $A$ does not depend on time to simplify the time-dependence.
The axial integral can be carried out analytically,
\begin{equation*}
    \int_{z_0}^{z_0 + d} e^{-\frac{(z-z')^2}{4\alpha t'}} e^{-\mu_a (z'-z_0)} \dd z'
\end{equation*}
Let $\beta = \frac{1}{\sqrt{4 \alpha t'}}$. Then,
\begin{equation*}
    \int_{z_0}^{z_0 + d} e^{-\beta^2 (z-z')^2-\mu_a (z'-z_0)} \dd z' =
   e^{\mu_a z_0} \int_{z_0}^{z_0 + d} e^{-\qty(\beta^2 z^2 + \beta^2z'^2 -2\beta^2zz'+\mu_a z')} \dd z'
\end{equation*}
Completing the square for the exponent
\begin{equation*}
  \beta^2 z^2 + \beta^2 z'^2 -2\beta^2 z z' + \mu_a z' = \qty(\beta z' + \frac{\mu_a}{2\beta})^2 - \qty(\frac{\mu_a}{2\beta})^2 + \beta^2z^2 - 2\beta^2 z z'= \qty(\beta z' + \frac{\mu_a}{2\beta} - \beta z)^2 - \qty(\frac{\mu_a}{2\beta})^2  + \mu_a z
\end{equation*}
gives
\begin{align*}
  e^{\mu_a z_0}e^{-\mu_a z} e^{\mu_a^2/2\beta^2}\int_{z_0}^{z_0 + d} e^{-\qty(\beta z' + \frac{\mu_a}{2\beta} - \beta z)^2} \dd z'
= e^{-\mu_a (z-z_0)} e^{\mu_a^2/2\beta^2}\int_{\beta z_0 + \mu_a/2\beta - \beta z}^{\beta(z_0 + d) + \mu_a/2\beta - \beta z} e^{-u^2} \frac{\dd u'}{\beta}
\end{align*}
with $u = \beta z' + \mu_a/2\beta - \beta z$, which can be integrated using definition of the error function
\begin{equation*}
  \erf(x) = \frac{2}{\sqrt{\pi}} \int_0^x e^{-u^2} \dd u,
\end{equation*}
to give
\begin{align*}
  \frac{\sqrt{\pi}}{2\beta} e^{-\mu_a (z-z_0)} e^{\mu_a^2/2\beta^2}\qty[\erf\qty(\beta(z_0 + d - z) + \frac{\mu_a}{2\beta} ) - \erf\qty(\beta(z_0 - z) + \frac{\mu_a}{2\beta}) ] \\
  = \sqrt{\pi\alpha t'} e^{-\mu_a (z-z_0)} e^{\alpha t'\mu_a^2}\qty[\erf\qty(\frac{z_0 + d - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) - \erf\qty(\frac{z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) ]
\end{align*}
To simplify transverse integrals we consider only the temperature along the $z$ axis. Then
we need to evaluate
\begin{equation*}
    \int \int \bar{E}(x',y') e^{-\frac{x'^2 + y'^2}{4\alpha t'}} \dd x' \dd y'
\end{equation*}
For circularly symmetric beams, we can switch to polar coordinates and
integrate the azimuthal angle,
\begin{equation*}
    \int_0^{2\pi} \int_0^\infty \bar{E}(r') e^{-\frac{r'^2}{4\alpha t'}} r'\dd r' \dd \theta'
    = 2\pi \int_0^\infty  \bar{E}(r') e^{-\frac{r'^2}{4\alpha t'}} r'\dd r'
\end{equation*}
To evaluate radial integral, we need to specify the beam profile. For a flat top
beam of radius $R$, we will have
\begin{equation*}
  2\pi \int_0^R e^{-\frac{r'^2}{4\alpha t'}} r'\dd r'
  =  4\pi \alpha t' \qty[1 - e^{-R^2/4\alpha t'}]
\end{equation*}
For a Gaussian beam with 1/e radius $\sigma$ clipped by a circular aperture of radius $R$, we will have
\begin{equation*}
2\pi \int_0^R e^{-\frac{r'^2}{\sigma^2}} e^{-\frac{r'^2}{4\alpha t'}} r'\dd r'
=  \frac{\pi}{1/4\alpha t' + 1/\sigma^2} \qty[1 - e^{-\frac{R^2}{4\alpha t' + \sigma^2}}]
=  \frac{4 \pi \alpha t' \sigma^2}{4\alpha t' + \sigma^2} \qty[1 - e^{-\frac{R^2}{4\alpha t' + \sigma^2}}]
.
\end{equation*}
For an unclipped beam, $R = \infty$.

Putting this all together, for a flat top beam we have
\begin{align}
  \label{eq:flattop_temp}
  T(r=0, z,t) =& \frac{\mu_a}{\rho c} \int_0^t \qty(\frac{1}{4\pi \alpha t'})^{3/2} \qty{\int_{z_0}^{z_0 + d}e^{-\frac{(z-z')^2}{4\alpha t'}} e^{-\mu_a (z'-z_0)} \dd z'} \qty{2\pi\int_{0}^{R} E_0 e^{-\frac{r'^2}{4\alpha t'}}r'\dd r'}\dd t' \nonumber \\
  =& \frac{\mu_a E_0}{\rho c} \int_0^t \qty(\frac{1}{4\pi \alpha t'})^{3/2}  \nonumber \\
  \times & \qty{\sqrt{\pi\alpha t'} e^{-\mu_a (z-z_0)} e^{\alpha t'\mu_a^2}\qty[\erf\qty(\frac{z_0 + d - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) - \erf\qty(\frac{z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) ]} \nonumber \\
  \times & \qty{4\pi \alpha t' \qty[1 - e^{-R^2/4\alpha t'}] } \dd t' \nonumber \\
  =& \frac{\mu_aE_0}{2\rho c}e^{-\mu_a (z-z_0)} \int_0^t e^{\alpha t'\mu_a^2}  \nonumber \\
  \times & \qty[\erf\qty(\frac{z_0 + d - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) - \erf\qty(\frac{z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) ] \nonumber \\
  \times & \qty[1 - e^{-R^2/4\alpha t'} ]
  \dd t'
\end{align}
For a Gaussian beam we have
\begin{align}
  \label{eq:gaussian_temp}
  T(r =0,z,t) =& \frac{\mu_a}{\rho c} \int_0^t \qty(\frac{1}{4\pi \alpha t'})^{3/2} \qty{\int_{z_0}^{z_0 + d}e^{-\frac{(z-z')^2}{4\alpha t'}} e^{-\mu_a (z'-z_0)} \dd z'} \qty{2\pi\int_{0}^{R} E_0 e^{-\frac{r'^2}{\sigma^2}} e^{-\frac{r'^2}{4\alpha t'}}r'\dd r'}\dd t' \nonumber \\
  =& \frac{\mu_aE_0}{2\rho c}e^{-\mu_a (z-z_0)} \int_0^t e^{\alpha t'\mu_a^2}  \nonumber \\
  \times & \qty[\erf\qty(\frac{z_0 + d - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) - \erf\qty(\frac{z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) ] \nonumber \\
  \times & \frac{1}{1 + 4\alpha t'/\sigma^2} \qty[1 - e^{-R^2/4\alpha t'} ]
  \dd t'
\end{align}


\subsection{Approximations}

In theory, Equation \ref{eq:flattop_temp} and \ref{eq:gaussian_temp} can be integrated
numerically to calculate the temperature at $r = 0$ at any time $t$. However,
in practice the calculation is difficult. One of the issues is that the
terms arising from the $z$ integral become very large or small individually and
exceed the precision of standard floating point numbers for long time. This
is especially an issue when the absorption coefficient is large.

\subsubsection{For long time}

The $z$ portion of the integral is
\begin{equation*}
    \int_{z_0}^{z_0 + d} e^{-\frac{(z-z')^2}{4\alpha t'}} e^{-\mu_a (z'-z_0)} \dd z' = e^{\mu_a z_0}\int_{z_0}^{z_0 + d} e^{-\frac{(z-z')^2}{4\alpha t'}} e^{-\mu_a z'} \dd z'
\end{equation*}
The Gaussian term can be written as a power series
\begin{equation*}
  e^{-\frac{(z-z')^2}{4\alpha t'}} = 1 - \frac{(z-z')^2}{4\alpha t'} + \frac{1}{2}\left(\frac{(z-z')^2}{4\alpha t'}\right)^2 + \ldots
\end{equation*}
If $(z-z')^2/4\alpha t'$ is small, which will be the case for positions $z$ close
to the source at long times, then
\begin{equation*}
   e^{\mu_a z_0} \int_{z_0}^{z_0 + d} e^{-\frac{(z-z')^2}{4\alpha t'}} e^{-\mu_a z'} \dd z' \approx
    e^{\mu_a z_0}\int_{z_0}^{z_0 + d} \left[1 - \frac{(z-z')^2}{4\alpha t'}\right] e^{-\mu_a z'} \dd z'
\end{equation*}
\begin{equation*}
    \left[1 - \frac{(z-z')^2}{4\alpha t'}\right] e^{-\mu_a z'}
=   \left[1 - \frac{z^2}{4\alpha t'} + \frac{2zz'}{4\alpha t'} -\frac{z'^2}{4\alpha t'}\right] e^{-\mu_a z'}
\end{equation*}
There are three integrals to evaluate
\begin{equation*}
   e^{\mu_a z_0} \int_{z_0}^{z_0 + d}
   \left(1 -  \frac{z^2}{4\alpha t'}\right)e^{-\mu_a z'}
    \dd z' =
   e^{\mu_a z_0} \left.\left(1 -  \frac{z^2}{4\alpha t'}\right)\frac{e^{-\mu_a z'}}{-\mu_a} \right|_{z_0}^{z_0 + d}
    = \left(1 -  \frac{z^2}{4\alpha t'}\right)\frac{1 - e^{-\mu_a d}}{\mu_a}
\end{equation*}

\begin{align*}
   e^{\mu_a z_0}  \int_{z_0}^{z_0 + d}
   \frac{2zz'}{4\alpha t'} e^{-\mu_a z'}
   \dd z'
   &= e^{\mu_a z_0} \left.\frac{2z}{4\alpha t'} \left(\frac{-\mu_az' - 1}{\mu_a^2} \right)e^{-\mu_a z'} \right|_{z_0}^{z_0 + d} \\
     &= e^{\mu_a z_0} \frac{2z}{4\alpha t'} \left( \frac{\mu_a z_0 + 1}{\mu_a^2}e^{-\mu_a z_0} - \frac{\mu_a (z_0+d) + 1}{\mu_a^2}e^{-\mu_a (z_0+d)}\right) \\
         &=  \frac{2z}{4\alpha t'} \left( \frac{\mu_a z_0 + 1}{\mu_a^2} - \frac{\mu_a (z_0+d) + 1}{\mu_a^2}e^{-\mu_a d}\right)
\end{align*}
\begin{align*}
e^{\mu_a z_0}    \int_{z_0}^{z_0 + d}
   \frac{z'^2}{4\alpha t'} e^{-\mu_a z'}
   \dd z'
   &= e^{\mu_a z_0}\left.\frac{1}{4\alpha t'} \left( \frac{z'^2}{-\mu_a} - \frac{2z'}{\mu_a^2} + \frac{2}{-\mu_a^3} \right)e^{-\mu_a z'} \right|_{z_0}^{z_0 + d} \\
   &= e^{\mu_a z_0}\left.\frac{1}{4\alpha t'} \left( \frac{z'^2}{\mu_a} + \frac{2z'}{\mu_a^2} + \frac{2}{\mu_a^3} \right)e^{-\mu_a z'} \right|_{z_0 + d}^{z_0} \\
   &=
e^{\mu_a z_0}   \frac{1}{4\alpha t'}\left[
   \left( \frac{z_0^2}{\mu_a} + \frac{2z_0}{\mu_a^2} + \frac{2}{\mu_a^3} \right)e^{-\mu_a z_0} -
   \left( \frac{(z_0 + d)^2}{\mu_a} + \frac{2(z_0 + d)}{\mu_a^2} + \frac{2}{\mu_a^3} \right)e^{-\mu_a (z_0+d)}
   \right] \\
    &=
   \frac{1}{4\alpha t'}\left[
   \left( \frac{z_0^2}{\mu_a} + \frac{2z_0}{\mu_a^2} + \frac{2}{\mu_a^3} \right) -
   \left( \frac{(z_0 + d)^2}{\mu_a} + \frac{2(z_0 + d)}{\mu_a^2} + \frac{2}{\mu_a^3} \right)e^{-\mu_a d}
   \right]
\end{align*}


\begin{align*}
  e^{\mu_a z_0}  \int_{z_0}^{z_0 + d} e^{-\frac{(z-z')^2}{4\alpha t'}} e^{-\mu_a z'} \dd z' &\approx
  \left(1 -  \frac{z^2}{4\alpha t'}\right)\frac{1 - e^{-\mu_a d}}{\mu_a} \\
    &+ \frac{2z}{4\alpha t'} \left( \frac{\mu_a z_0 + 1}{\mu_a^2} - \frac{\mu_a (z_0+d) + 1}{\mu_a^2}e^{-\mu_a d}\right) \\
    &- \frac{1}{4\alpha t'}\left[
   \left( \frac{z_0^2}{\mu_a} + \frac{2z_0}{\mu_a^2} + \frac{2}{\mu_a^3} \right) -
   \left( \frac{(z_0 + d)^2}{\mu_a} + \frac{2(z_0 + d)}{\mu_a^2} + \frac{2}{\mu_a^3} \right)e^{-\mu_a d}
   \right]
\end{align*}
For $z=z_0=0$, this simplifies to
\begin{align*}
    e^{\mu_a z_0}\int_{z_0}^{z_0 + d} e^{-\frac{(z-z')^2}{4\alpha t'}} e^{-\mu_a z'} \dd z' &\approx
    \frac{1 - e^{-\mu_a d}}{\mu_a}
    - \frac{1}{4\alpha t'}\left[
   \left( \frac{2}{\mu_a^3} \right) -
   \left( \frac{d^2}{\mu_a} + \frac{2d}{\mu_a^2} + \frac{2}{\mu_a^3} \right)e^{-\mu_a d}
   \right]
\end{align*}
For ``long'' time, $4\alpha t' >> z$, we can keep just the first term,
\begin{align*}
   e^{\mu_a z_0} \int_{z_0}^{z_0 + d} e^{-\frac{(z-z')^2}{4\alpha t'}} e^{-\mu_a z'} \dd z' &\approx
    \frac{1 - e^{-\mu_a d}}{\mu_a}
\end{align*}
Inserting the approximation into Equation \ref{eq:flattop_temp}
\begin{align}
  \label{eq:flattop_temp_approx}
  T(r=0, z,t) =& \frac{\mu_a E_0}{\rho c} \int_0^t \qty(\frac{1}{4\pi \alpha t'})^{3/2}  \nonumber \\
  \times & \left\{\left(1 -  \frac{z^2}{4\alpha t'}\right)\frac{1 - e^{-\mu_a d}}{\mu_a} \right.\nonumber \\
    &+ \frac{2z}{4\alpha t'} \left( \frac{\mu_a z_0 + 1}{\mu_a^2} - \frac{\mu_a (z_0+d) + 1}{\mu_a^2}e^{-\mu_a d}\right) \nonumber \\
    &- \left.\frac{1}{4\alpha t'}\left[
   \left( \frac{z_0^2}{\mu_a} + \frac{2z_0}{\mu_a^2} + \frac{2}{\mu_a^3} \right) -
   \left( \frac{(z_0 + d)^2}{\mu_a} + \frac{2(z_0 + d)}{\mu_a^2} + \frac{2}{\mu_a^3} \right)e^{-\mu_a d}
 \right]
\right\} \nonumber \\
  \times & \qty{4\pi \alpha t' \qty[1 - e^{-R^2/4\alpha t'}] }
  \dd t' \\
  =&
  \frac{E_0}{\rho c} \int_0^t \qty(\frac{1}{4\pi \alpha t'})^{1/2}  \nonumber \nonumber \\
  \times & \left\{\qty[1 - e^{-\mu_a d}]   -  \frac{z^2}{4\alpha t'}\qty[1 - e^{-\mu_a d}]\right. \nonumber \\
    &+ \frac{2z}{4\alpha t'} \left( \frac{\mu_a z_0 + 1}{\mu_a} - \frac{\mu_a (z_0+d) + 1}{\mu_a}e^{-\mu_a d}\right) \nonumber \\
    &- \left.\frac{1}{4\alpha t'}\left[
   \left( z_0^2       + \frac{2z_0}{\mu_a} + \frac{2}{\mu_a^2} \right) -
   \left( (z_0 + d)^2 + \frac{2(z_0 + d)}{\mu_a} + \frac{2}{\mu_a^2} \right)e^{-\mu_a d}
 \right]
\right\} \nonumber \\
  \times & \qty[1 - e^{-R^2/4\alpha t'}]
  \dd t'
\end{align}


\subsection{For large absorption}
The approximation in the previous section for long times has been implemented, and it works, however it does not solve the problem. Evaluating the $z$ integral without approximation leads to
\begin{equation*}
	e^{\alpha t' \mu_a^2} \qty[	
	\erf\qty(\frac{z_0 + d - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) - \erf\qty(\frac{z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a )].
\end{equation*}
The issue is that as $t'$ increases, $e^{\alpha t' \mu_a^2}$ becomes very large. The difference between the two error functions becomes small, so the product of the two remains finite (it actually decreases with time).
However, trying to compute the two terms separately leads to floating point overflow. In Python, both the standard math module and numpy overflow when passing an argument greater than about 700 to the exp function. For
visible light, the absorption coefficient of the RPE can be on the order of \SI{1000}{\per\centi\meter}, which means (assuming the thermal properties of water, $\alpha = \SI{0.0015}{\centi\meter\squared\per\second}$)
that we will get an overflow when $t' > \SI{0.46}{\second}$.This is much too short a time to simulate a laser exposure. But even before an overflow, we will get a loss of precision.

The problem with the long-time approximation is that it is valid when $(z'-z_0)/4\alpha t' \ll 1$, which does not depend on the absorption coefficient at all. So its possible to overflow with a large absorption coefficient
before the approximation can be employed.

Instead, it is possible to approximate the result of the integral using an asymptotic expansion for the error function
\begin{equation}
	\erf(x) = 1 - \erfc(x) = 1 - \frac{e^{-x^2}}{x\sqrt{\pi}} \sum_{n=0}^{\infty} (-1)^n \frac{(2n-1)!!}{(2x^2)^n} =1 - \frac{e^{-x^2}}{x\sqrt{\pi}} +  \frac{e^{-x^2}}{x\sqrt{\pi}} \frac{1}{2 x^2} - \mathcal{O}( 1/x^5).
\end{equation}
This approximation of the error function works for "large" values, however, the argument does not need to be that "large". Keeping only the first two terms gives less than 0.1 error for $x >  0.88$. $x>1.38$ gives an error less
than 0.01, $x>1.86$ gives and error less than 0.001, and $x>2.28$ give an error less than 0.0001. Keeping the third term reduces the values of $x$ to 0.92, 1.30, 1.69, and 2.07 to reach these error threshold. Looking at the arguments
of the two error functions, there are two terms. The first gets smaller with time, the second gets larger. The second term is the only one that depends on $\mu_a$. If we assume that the first term is small, then we can estimate
the time at which the 2 term asymptotic expansion would give less than 0.001 error as $1.86 = \sqrt{\alpha t'} \mu_a$. For $\mu_a = \SI{1000}{\per\centi\meter}$, this gives $t' = \SI{0.001}{\second}$.

To simplify the algebra, let us define the following variables:
\begin{align*}
	A &= \sqrt{\alpha t'} \mu_a \\
	B &= \frac{z_0 - z}{\sqrt{4\alpha t'}} \\
	C &= \frac{d}{\sqrt{4\alpha t'}} 
\end{align*}
Then the product we want to approximate can be written
\begin{align*}
e^{A^2} \qty[
\erf\qty(C + B + A ) - \erf\qty(B + A)
].
\end{align*}
The issue is $e^{A^2}$, it will overflow at short times. If we only had $e^A$, then we would not get an overflow until $t' = \SI{326}{\second}$, which should be sufficient for most laser exposures, sine we would expect to reach steady-state long before that time.

\begin{align*}
	(A+B+C)^2 &= A^2 + B^2 + C^2 + 2AB + 2AC + 2BC \\
	(A+B)^2 &= A^2 + B^2 + 2AB \\
	e^{A^2} \qty[\erf\qty(C + B + A ) - \erf\qty(B + A)] &=
	e^{A^2} \qty[
	\qty(1 - \frac{e^{-(A^2+B^2+C^2+2AB+2AC+2BC)}}{(A+B+C)\sqrt{\pi}}\sum\cdots)
	 - 
	 \qty(1 - \frac{e^{-(A^2+B^2+2AB)}}{(A+B)\sqrt{\pi}}\sum\cdots)
	 	] \\
	 	&= 	 
	 	 \frac{-e^{-(B^2+C^2+2AB+2AC+2BC)}}{(A+B+C)\sqrt{\pi}}\sum\cdots
	 	+ 
		 \frac{e^{-(B^2+2AB)}}{(A+B)\sqrt{\pi}}\sum\cdots
	 	 \\
	 	 	 	&= 	 
	 	  \frac{e^{-(B^2+2AB)}}{(A+B)\sqrt{\pi}}\qty[1 - \frac{1}{2\qty(A+B)^2}+\ldots]
	 	 -
	 	 \frac{e^{-(B^2+C^2+2AB+2AC+2BC)}}{(A+B+C)\sqrt{\pi}}\qty[1 - \frac{1}{2\qty(A+B+C)^2}+\ldots]
	 	 	 	 \\
\end{align*}
This gives us way to calculate the product without having to evaluate $e^{A^2}$ and cause an overflow. We have the following
\begin{align*}
	A^2 &= \alpha t' \mu_a^2 \\
	B^2 &= \frac{(z_0 - z)^2}{4\alpha t'} \\
	C^2 &= \frac{d^2}{4\alpha t'} \\
	2AB&= 2\sqrt{\alpha t'} \mu_a  \frac{z_0 - z}{\sqrt{4\alpha t'}} = \qty(z_0 - z)\mu_a\\
	2AC&= 2\sqrt{\alpha t'} \mu_a  \frac{d}{\sqrt{4\alpha t'}} = d\mu_a\\
	2BC&= 2\frac{z_0 - z}{\sqrt{4\alpha t'}}  \frac{d}{\sqrt{4\alpha t'}} = \frac{d\qty(z_0 - z)}{2\alpha t'}\\
\end{align*}
substituting in
\begin{align*}
		e^{\alpha t' \mu_a^2} \qty[	
	\erf\qty(\frac{z_0 + d - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) - \erf\qty(\frac{z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a )]	&= 	 
	\frac{e^{-\qty(\frac{(z_0 - z)^2}{4\alpha t'}+\frac{d^2}{4\alpha t'}+\qty(z_0 - z)\mu_a+d\mu_a+\frac{d\qty(z_0 - z)}{2\alpha t'})}}{(  \sqrt{\alpha t'} \mu_a+\frac{z_0 - z}{\sqrt{4\alpha t'}}+\frac{d}{\sqrt{4\alpha t'}})\sqrt{\pi}}\sum\cdots\\
	&- 
	\frac{e^{-\qty(\frac{(z_0 - z)^2}{4\alpha t'}+\qty(z_0 - z)\mu_a)}}{(\sqrt{\alpha t'} \mu_a+\frac{z_0 - z}{\sqrt{4\alpha t'}})\sqrt{\pi}}\sum\cdots
	\\
\end{align*}

%\subsubsection{For short time}
%
%
%When $t'$ is small, then $1/t'$ becomes very large. For flat top beams, the temperature rise is given by
%
%\begin{align*}
%	T(r=0, z,t) =& \frac{\mu_aE_0}{2\rho c}e^{-\mu_a (z-z_0)} \int_0^t e^{\alpha t'\mu_a^2}  \nonumber \\
%	\times & \qty[\erf\qty(\frac{z_0 + d - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) - \erf\qty(\frac{z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) ]  \\
%	\times & \qty[1 - e^{-R^2/4\alpha t'} ]
%	\dd t'
%\end{align*}
%The first term in the integral can be expanded in a Taylor series for the exponential function,
%\begin{equation}
%	e^x = \sum_{n=0}^{\infty} \frac{1}{n!} x^n = 1 + x + \frac{1}{2} x^2 + \mathcal{O}(x^n)
%\end{equation}
%where $x = \alpha t' \mu_a^2$.
%
%The time at which we can truncate this series to approximate the exponential depends on $\alpha$ and $\mu_a$. $\alpha$ is the thermal diffusivity. When this parameter is large, heat
%flows through the tissue quickly. When the absorption coefficient $\mu_a$ is large, there will be a steep thermal gradient in the axial direction in the absorbing tissue, which will cause
%a faster heat flow. Physically then, this series can be truncated when heat has not had significant time to conduct out of the absorption volume.
%
%Keeping only the first two terms will give an error of about \SI{1}{\percent} or less when $\alpha t' \mu_a^2 \le 0.15$. Keeping the first three terms will give an error of about \SI{1}{\percent} or less when
%$\alpha t' \mu_a^2 \le 0.44$.
%
%The second term in the integral can be expanded using a Taylor series approximation if the argument to the error function is small, or the asymptotic expansion of $\erfc(x)$ if the argument is large.
%
%The Taylor series expansion of $\erf(x)$ is
%\begin{equation}
%	\erf(x) = \frac{2}{\sqrt{\pi}} \sum_{n=0}^\infty \frac{(-1)^n x^{2n+1}} { n! (2n+1)} = \frac{2}{\sqrt{\pi}} e^{-x^2} \sum_{n=0}^{\infty} \frac{2^n x^{2n+1}}{(2n+1)!!}
%\end{equation}
%where $N!!$ is the double factorial, the product of all odd numbers up to $N$. 
%The first form gives
%\begin{equation}
%	\erf(x) = \frac{2x}{\sqrt{\pi}} - \frac{2x^3}{3\sqrt{\pi}}   + \frac{2x^5}{10\sqrt{\pi}}  + \mathcal{O}(x^7).
%\end{equation}
%The second from gives
%\begin{equation}
%	\erf(x) = \frac{2x}{\sqrt{\pi}} e^{-x^2} + \frac{4x^3}{3\sqrt{\pi}} e^{-x^2}  + \frac{8x^5}{15\sqrt{\pi}} e^{-x^2}+  \mathcal{O}(x^7).
%\end{equation}
%The first form gives a more accurate approximation for larger arguments. Keeping only the first term gives and error of about \SI{1}{\percent} or less for $x \le 0.17$ using the first form and $x \le 0.12$ for the second. Keeping the first two terms give an %error of about \SI{1}{\percent} or less for $x \le 0.56$ for the first form and $x \le 0.45$ for the second.
%
%	
%The asymptotic expansion of $\erfc(x)$ is
%\begin{equation}
%	\erfc(x) = \frac{e^{-x^2}}{x\sqrt{\pi}} \sum_{n=0}^{\infty} (-1)^n \frac{(2n-1)!!}{(2x^2)^n}.
%\end{equation}
%For the error function,
%\begin{equation}
%	\erf(x) = 1 - \erfc(x) = 1 - \frac{e^{-x^2}}{x\sqrt{\pi}} +  \frac{e^{-x^2}}{x\sqrt{\pi}} \frac{1}{2 x^2} - \mathcal{O}( 1/x^5).
%\end{equation}
%Keeping the first two terms in this series gives an error of about \SI{1}{\percent} or less when $x \ge 1.39$. Keeping the first three gives a \SI{1}{\percent} error or less around $x \ge 1.3$.
%
%Using the lowest order approximations for the expon
%
%\begin{equation}
%	(1 + \alpha \mu_a^2 t') \times \qty(\qty( 1 - \frac{e^{-\qty(\frac{d+z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a)^2}}{\qty(\frac{d+z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a)\sqrt{\pi}}) - 
%\frac{2\qty(\frac{z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a)e^{-\qty(\frac{z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a)^2}}{\sqrt{\pi}} )
%\end{equation}

\subsection{Off Axis Temperatures}
The derivation of Equations \ref{eq:flattop_temp} and \ref{eq:gaussian_temp} assumed $x = y = 0$, i.e., it is only valid for the temperature rise on the $z$ axis.
To compute the temperature off axis, we need to evaluate
\begin{equation*}
  \int_{-\infty}^\infty \int_{-\infty}^\infty \bar{E}(x',y') e^{-\frac{(x-x')^2 + (y-y')^2}{4\alpha t'}} \dd x' \dd y'.
\end{equation*}
We can switch to polar coordinates by noting that $(x-x')^2 + (y-y')^2 = |\vec{r}-\vec{r}'|^2 = (\vec{r}-\vec{r}')\cdot(\vec{r}-\vec{r}') = \vec{r}\cdot\vec{r} + \vec{r}'\cdot\vec{r}' - 2r r' \cos(\phi - \phi')$
\begin{equation*}
  \int_0^\infty \int_0^{2\pi} \bar{E}(r',\phi')  e^{-\frac{r^2}{4\alpha t'}}e^{-\frac{r'^2}{4\alpha t'}}e^{\frac{2rr'\cos(\phi-\phi')}{4\alpha t'}}r' \dd \phi' \dd r'.
\end{equation*}
If the source term is symmetric about the $z$ axis, we can evaluate the integral at $\phi=0$ without loss of generality.
\begin{equation*}
\int_0^\infty \bar{E}(r') r'e^{-\frac{r^2}{4\alpha t'}}e^{-\frac{r'^2}{4\alpha t'}}\int_0^{2\pi} e^{2rr'\cos(\phi')/4\alpha t'} \dd \phi' \dd r'.
\end{equation*}
That the zero'th order Modified Bessel Function of the First Kind, $I_0(x)$ has an integral representation
\begin{equation}
  I_0(x) = \frac{1}{\pi} \int_0^\pi e^{x\cos(\phi)} \dd \phi.
\end{equation}
The azimuthal integral then can be carried out by noting that the integrand is symmetric about $\phi' = \pi$
\begin{equation*}
\int_0^{2\pi}  e^{2rr'\cos(\phi')/4\alpha t'}\dd \phi'
=2\int_0^{\pi}  e^{2rr'\cos(\phi')/4\alpha t'}\dd \phi'
=2\pi I_0\qty(\frac{2rr'}{4\alpha t'}).
\end{equation*}
The radian integral is then
\begin{equation*}
  2\pi \int_0^\infty \bar{E}(r')   e^{-\frac{r^2}{4\alpha t'}}e^{-\frac{r'^2}{4\alpha t'}}I_0\qty(\frac{2rr'}{4\alpha t'}) r'\dd r'.
\end{equation*}

\subsubsection{Flat Top Beams}
For a flat top beam with radius $R$, we need to evaluate the integral
\begin{equation*}
  2\pi \int_0^R  e^{-\frac{r^2}{4\alpha t'}}e^{-\frac{r'^2}{4\alpha t'}}I_0\qty(\frac{2rr'}{4\alpha t'}) r'\dd r'.
\end{equation*}
The integral can be carried out using the the Marcum Q-function, which is defined as
\begin{equation*}
Q_\nu(a,b) = 1 - \frac{1}{a^{\nu-1}}\int_0^b x^\nu e^{-\frac{x^2 + a^2}{2}} I_{\nu-1}(ax) \dd x.
\end{equation*}
In our case, $\nu=1$.
To cast our integral into this form, let $x = r'/\sqrt{2\alpha t'}, a = r/\sqrt{2\alpha t'}$, which gives
\begin{equation*}
  2\pi 2\alpha t' \int_0^{R/\sqrt{2\alpha t'}} e^{-a^2/2}e^{-x^2/2}I_0\qty(ax) x\dd x= 4\pi\alpha t' \qty(1 - Q_1(r/\sqrt{2\alpha t'},R/\sqrt{2\alpha t'}))
\end{equation*}
Plugging this into Equation \ref{eq:flattop_temp} gives
\begin{align}
  \label{eq:flattop_off_axis_temp}
  T(r=0, z,t) =& \frac{\mu_aE_0}{2\rho c}e^{-\mu_a (z-z_0)} \int_0^t e^{\alpha t'\mu_a^2}  \nonumber \\
  \times & \qty[\erf\qty(\frac{z_0 + d - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) - \erf\qty(\frac{z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) ] \nonumber \\
  \times & \qty[1 - Q_1\qty(r/\sqrt{2\alpha t'},R/\sqrt{2\alpha t'}) ]
  \dd t'
\end{align}



\subsubsection{Gaussian Beams}
For a Gaussian beam with 1/e radius $\sigma$ we need to evaluate
\begin{equation*}
  2\pi \int_0^\infty
  e^{-\frac{r'^2}{\sigma^2}}
  e^{-\frac{r^2}{4\alpha t'}}e^{-\frac{r'^2}{4\alpha t'}}I_0\qty(\frac{2rr'}{4\alpha t'}) r'\dd r'
  =
  2\pi e^{-\frac{r^2}{4\alpha t'}}\int_0^\infty
  e^{-\qty(\frac{1}{\sigma^2} + \frac{1}{4\alpha t'})r'^2}
  I_0\qty(\frac{2rr'}{4\alpha t'}) r'\dd r'.
\end{equation*}
% The integral can be cast into a standard form and evaluated,
% \begin{equation*}
%   \int_0^\infty t^{\nu + 1} I_\nu\qty(bt) e^{-p^2 t^2} \dd t  = \frac{b^\nu}{\qty(2p^2)^{\nu+1}}  e^{b^2/4p^2}.
% \end{equation*}
% In our case, $\nu = 0$, $b = 2r/4\alpha t'$, and $p^2 = \qty(\frac{1}{\sigma^2} + \frac{r'^2}{4\alpha t'})$, which gives
% \begin{align*}
%   2\pi e^{-\frac{r^2}{4\alpha t'}}\int_0^\infty
%   e^{-\qty(\frac{1}{\sigma^2} + \frac{1}{4\alpha t'})r'^2}
%   I_0\qty(\frac{2rr'}{4\alpha t'}) r'\dd r'
%   &=
%   2\pi e^{-\frac{r^2}{4\alpha t'}}
%   \frac{1}{2\qty(\frac{1}{\sigma^2} + \frac{1}{4\alpha t'})}  e^{\qty(2r/4\alpha t')^2/4\qty(\frac{1}{\sigma^2} + \frac{1}{4\alpha t'})} \\
%   &=
%   4\pi\alpha t' e^{-\frac{r^2}{4\alpha t'}}
%   \frac{1}{1 + \frac{4\alpha t'}{\sigma^2}}
%   e^{\frac{r^2}{4\alpha t'} \frac{1}{ 1 + \frac{4\alpha t'}{\sigma^2}} }
% \end{align*}
The integral can be cast into a standard form and evaluated [``Table of Integrals, Series, and Products'', Gradshteyn and Ryzhik pg. 707],
\begin{equation*}
  \int_0^\infty x e^{-\alpha x^2} I_\nu\qty(\beta x) J_\nu(\gamma x) \dd x = \frac{1}{2\alpha} \exp\qty(\frac{\beta^2 - \gamma^2}{4\alpha}) J_\nu\qty(\frac{\beta\gamma}{2\alpha})
\end{equation*}
In our case, $\nu = \gamma = 0$, $\beta = 2r/4\alpha t'$, and $\alpha = \qty(\frac{1}{\sigma^2} + \frac{1}{4\alpha t'})$, which gives
\begin{align*}
  2\pi e^{-\frac{r^2}{4\alpha t'}}\int_0^\infty
  e^{-\qty(\frac{1}{\sigma^2} + \frac{1}{4\alpha t'})r'^2}
  I_0\qty(\frac{2rr'}{4\alpha t'}) r'\dd r'
  &=
  2\pi e^{-\frac{r^2}{4\alpha t'}}
  \frac{1}{2\qty(\frac{1}{\sigma^2} + \frac{1}{4\alpha t'})}  e^{\qty(2r/4\alpha t')^2/4\qty(\frac{1}{\sigma^2} + \frac{1}{4\alpha t'})} \\
  &=
  4\pi\alpha t' e^{-\frac{r^2}{4\alpha t'}}
  \frac{1}{1 + \frac{4\alpha t'}{\sigma^2}}
  e^{\frac{r^2}{4\alpha t'} \frac{1}{ 1 + \frac{4\alpha t'}{\sigma^2}} } \\
  &=
  4\pi\alpha t'
  \frac{1}{1 + \frac{4\alpha t'}{\sigma^2}}
  e^{\frac{r^2}{4\alpha t'} \qty(\frac{1}{ 1 + \frac{4\alpha t'}{\sigma^2}}-1) } \\
  &=
  4\pi\alpha t'
  \frac{\sigma^2}{\sigma^2 + 4\alpha t'}
  e^{\frac{-r^2}{\sigma^2 + 4\alpha t'}  }
\end{align*}
Plugging this into Equation \ref{eq:gaussian_temp} gives
\begin{align}
  \label{eq:gaussian_off_axis_temp}
  T(r=0, z,t) =& \frac{\mu_aE_0}{2\rho c}e^{-\mu_a (z-z_0)} \int_0^t e^{\alpha t'\mu_a^2}  \nonumber \\
  \times & \qty[\erf\qty(\frac{z_0 + d - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) - \erf\qty(\frac{z_0 - z}{\sqrt{4\alpha t'}} + \sqrt{\alpha t'}\mu_a ) ] \nonumber \\
  \times & \qty[
  \frac{\sigma^2}{\sigma^2 + 4\alpha t'}
  e^{\frac{-r^2}{\sigma^2 + 4\alpha t'}  }
  ]
  \dd t'
\end{align}



\subsection{Pulsed Exposures}

The temperature rises caused by a CW exposure is given by integrating the Green's Function,
\begin{equation}
  \Delta T(t) = \int_{0}^{t} G(t') \dd t'.
\end{equation}
Because the temperature rise is linear, we can use the CW temperature rise to compute
the temperature rise caused by a pulsed exposure,
\begin{equation}
  \Delta T(t) = \begin{cases}
    \int_{0}^{t} G(t') \dd t' & t \le \tau \\
    \int_{0}^{t} G(t') \dd t' - \int_{0}^{t-\tau} G(t') \dd t' & t > \tau.
\end{cases}
\end{equation}
After the pulse, the temperature rise is given by the difference between the CW temperature rise at the times $t$ and $t-\tau$.
We can either calculate $\Delta T(t)$ and $\Delta(t-\tau)$, or evaluate the integral over a different set of limits,
\begin{equation}
    \int_{0}^{t} G(t') \dd t' - \int_{0}^{t-\tau} G(t') \dd t'  = \Delta T(t) - \Delta T(t-\tau) = \int_{t-\tau}^{t} G(t') \dd t'
\end{equation}





\end{document}
