Task: Implement Evolve.F77 for CactusExamples/WaveToy2DF77 thorn

thorn_name: CactusExamples/WaveToy2DF77
src_filename: Evolve.F77
interface: # Interface definition for thorn WaveToy2DF77
# $Header$

implements: wavetoy2d
inherits: boundary 

private:

cctk_real evolve2d dim=2 type=GF Timelevels=3
{
  phi
} "The evolved scalar field"

cctk_real initial dim=2 type=GF
{
  timederiv
} "For initial data"

cctk_real coords2d dim=2 type=GF
{
  x,
  y
} "The 2D x and y coordinates"

cctk_real dummy dim=2 type=GF
{
  phi_temp
} "Just for creating initial data"

CCTK_INT FUNCTION Coord_SystemRegister   \
  (CCTK_POINTER_TO_CONST IN GH,          \
   CCTK_INT IN dim,                      \
   CCTK_STRING IN systemname)

REQUIRES FUNCTION Coord_SystemRegister

CCTK_INT FUNCTION Coord_SystemHandle  \
  (CCTK_POINTER_TO_CONST IN GH,       \
   CCTK_STRING IN systemname)

REQUIRES FUNCTION Coord_SystemHandle

CCTK_INT FUNCTION Coord_CoordRegister  \
  (CCTK_POINTER_TO_CONST IN GH,        \
   CCTK_INT IN systemhandle,           \
   CCTK_INT IN direction,              \
   CCTK_STRING IN coordname)

REQUIRES FUNCTION Coord_CoordRegister

CCTK_INT FUNCTION Coord_CoordHandle    \
  (CCTK_POINTER_TO_CONST IN GH,        \
   CCTK_STRING IN coordname,           \
   CCTK_STRING IN systemname)

REQUIRES FUNCTION Coord_CoordHandle

CCTK_INT FUNCTION Coord_GroupSystem  \
  (CCTK_POINTER_TO_CONST IN GH,      \
   CCTK_STRING IN groupname)

REQUIRES FUNCTION Coord_GroupSystem

CCTK_INT FUNCTION Coord_SetDefaultSystem  \
  (CCTK_POINTER_TO_CONST IN GH,           \
   CCTK_STRING IN systemname)

REQUIRES FUNCTION Coord_SetDefaultSystem

schedule: # Schedule definitions for thorn WaveToy2DF77
# $Header$

STORAGE: evolve2d[3],coords2d

schedule WaveToy2DF77_Startup at STARTUP
{
  LANG: Fortran
} "Register WaveToy2DF77 banner"

schedule WaveToy2DF77_RegisterCoords at WRAGH
{
  LANG: Fortran
} "Register WaveToy2DF77 coordinates"

schedule WaveToy2DF77_Coord as SpatialCoordinates at BASEGRID
{
  LANG: Fortran
} "Set up 2D coordinates"

schedule WaveToy2DF77_Initial at INITIAL
{
  STORAGE: initial,dummy
  LANG: Fortran
} "Initial data for 2D wave equation"

schedule WaveToy2DF77_Evolve at EVOL
{
  LANG: Fortran
  SYNC: evolve2d
} "Evolution of 2D wave equation"

schedule WaveToy2DF77_Boundary at EVOL after WaveToy2DF77_Evolve
{
  LANG: Fortran
} "Apply boundary conditions"

param: # Parameter definitions for thorn WaveToy2DF77
# $Header$

private:

# Initial data

real amplitude "Amplitude of initial Gaussian wave"
{
  0:* :: ""
} 1.0

real xcenter "x-position of center of Gaussian wave"
{
  *:* :: "Anywhere"
} 0.5

real ycenter "y-position of center of Gaussian wave"
{
  *:* :: "Anywhere"
} 0.5

real sigma "Width of Gaussian wave"
{
  *:* :: "Anything"
} 0.02


# Boundary

keyword bound "Which boundary condition to apply"
{
  "zero"      :: "Set boundary values to zero"
  "radiation" :: "Radiation/wave boundary conditions"
} "zero"
configuration: # Configuration definition for thorn WaveToy2DF77
# $Header$

REQUIRES Boundary

context: Startup.F77:
```
 /*@@
   @file      Startup.F77
   @date      July 2 2000
   @author    Gabrielle Allen
   @desc 
              Register banner for WaveToy2DF77
   @enddesc 
   @version $Header$
 @@*/

#include "cctk.h"

      integer function WaveToy2DF77_Startup()

      implicit none
   
      integer ierr

      call CCTK_RegisterBanner(ierr, 
     &	"WaveToy2DF77: Evolution of a 2D Scalar Field")

      WaveToy2DF77_Startup = ierr

      end 

```

Initial.F77:
```
 /*@@
   @file      Initial.F77
   @date      June 13 2000
   @author    Gabrielle Allen
   @desc 
              Initial data for the 2d wave equation solver
   @enddesc 
 @@*/
  
#include "cctk.h" 
#include "cctk_Parameters.h"
#include "cctk_Arguments.h"


 /*@@
   @routine    WaveToy2DF77_Initial
   @date       June 13 2000
   @author     Gabrielle Allen
   @desc 
               Initial data for the 2d wave equation. Assumes first
	       time derivative is zero.
   @enddesc 
   @calls      
   @calledby   
   @history 
 
   @endhistory 

@@*/

      subroutine WaveToy2DF77_Initial(CCTK_ARGUMENTS)

      implicit none

      DECLARE_CCTK_ARGUMENTS
      DECLARE_CCTK_PARAMETERS 

      INTEGER   i,j,ierr
      CCTK_REAL dx,dy,dt	
      CCTK_REAL rad
      CCTK_REAL zero
      integer   sw(2)

      zero = 0.0
      sw(1) = 1
      sw(2) = 1

      dx = CCTK_DELTA_SPACE(1)
      dy = CCTK_DELTA_SPACE(2)
      dt = CCTK_DELTA_TIME

c     For now only have zero time derivative
      do j = 1, cctk_lsh(2)
        do i = 1, cctk_lsh(1)
       	  timederiv(i,j) = 0.0
        end do
      end do               

      do j = 1, cctk_lsh(2)
        do i = 1, cctk_lsh(1)
	   rad = sqrt((x(i,j)-xcenter)**2+(y(i,j)-ycenter)**2)
           phi(i,j) = amplitude*exp(-(rad)**2/sigma**2)  
        end do
      end do

c      call BndScalarVN(ierr,cctkGH, sw, zero, "wavetoy2df77::phi")

      do j = 2, cctk_lsh(2)-1
        do i = 2, cctk_lsh(1)-1
  	   phi_temp(i,j) = dt*timederiv(i,j) + 0.5*phi(i,j)+0.5*dt**2/dx**2*
     &	                (phi(i+1,j)-2.0*phi(i,j)+phi(i-1,j))
     &                  +0.5*dt**2/dy**2*(phi(i,j+1)-2.0*phi(i,j)+
     &                  phi(i,j-1))
        end do
      end do
 
      call CCTK_SyncGroup(ierr,cctkGH,"wavetoy2df77::dummy");
	
      do j = 1, cctk_lsh(2)
        do i = 1, cctk_lsh(1)
  	   phi_p(i,j) = phi_temp(i,j)
        end do
      end do


      return
      end 



```

Coord.F77:
```
 /*@@
   @file      Coord.F77
   @date      July 2 2000
   @author    Gabrielle Allen
   @desc 
              Coordinates for the 2d wave equation solver
   @enddesc 
 @@*/
  
#include "cctk.h" 
#include "cctk_Parameters.h"
#include "cctk_Arguments.h"
#include "cctk_Functions.h"


 /*@@
   @routine    WaveToy2DF77_RegisterCoords
   @date       June 13 2000
   @author     Gabrielle Allen
   @desc 
     Routine registers the coordinates for WaveToy2Df77
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

      subroutine WaveToy2DF77_RegisterCoords(CCTK_ARGUMENTS)

      implicit none
  
      DECLARE_CCTK_ARGUMENTS
      DECLARE_CCTK_PARAMETERS
      DECLARE_CCTK_FUNCTIONS

      CCTK_INT, parameter :: izero = 0
      integer, parameter :: ik = kind(izero)

      integer retval,ierr
      CCTK_INT coord_system_handle

      retval = 0

      ierr= Coord_SystemRegister(cctkGH, 2_ik, "cart2d");

c      call CCTK_INFO("Getting System Handle")

      coord_system_handle = Coord_SystemHandle(cctkGH, "cart2d")

c      call CCTK_INFO("Setting Coord Handle")

      ierr =  Coord_CoordRegister(cctkGH, coord_system_handle, 1_ik, "x")

      if (ierr .lt. 0) then
         call CCTK_WARN(1,"Problem with registering coordinate x")
         retval = -1
      end if

      ierr =  Coord_CoordRegister(cctkGH, coord_system_handle, 2_ik, "y")

      if (ierr .lt. 0) then
         call CCTK_WARN(1,"Problem with registering coordinate y")
         retval = -1
      end if

c      call CCTK_INFO("Setting Coord Type")

      call Util_TableSetString(ierr, coord_system_handle, "uniform", "TYPE")

c      call CCTK_INFO("Setting Default System")

      ierr = Coord_SetDefaultSystem(cctkGH, "cart2d");

      return
      end


 /*@@
   @routine    WaveToy2DF77_Coord
   @date       June 13 2000
   @author     Gabrielle Allen
   @desc 
               Coordinates for the 2d wave equation
   @enddesc 
   @calls      
   @calledby   
   @history 
 
   @endhistory 

@@*/

      subroutine WaveToy2DF77_Coord(CCTK_ARGUMENTS)

      implicit none

      DECLARE_CCTK_ARGUMENTS
      DECLARE_CCTK_FUNCTIONS

      INTEGER   i,j,ierr
      CCTK_REAL xmin,xmax,x_origin,lowerx,upperx
      CCTK_REAL ymin,ymax,y_origin,lowery,uppery
      CCTK_INT coord_handle
      INTEGER varindex

      xmin=0
      xmax=1

      ymin=0
      ymax=1


c     Grid spacing on coarsest grid 
      cctk_delta_space(1) = (xmax - xmin) / max(cctk_gsh(1) - 1, 1)
      cctk_delta_space(2) = (ymax - ymin) / max(cctk_gsh(2) - 1, 1)

      x_origin = xmin
      y_origin = ymin

      do j=1,cctk_lsh(2)
        do i=1,cctk_lsh(1)
          x(i,j) = cctk_delta_space(1)*(i-1+cctk_lbnd(1)) + x_origin
          y(i,j) = cctk_delta_space(2)*(j-1+cctk_lbnd(2)) + y_origin
        end do
      end do
  
      lowerx = x_origin
      upperx = x_origin+cctk_delta_space(1)*(cctk_gsh(1)-1)

c      call CCTK_INFO("Getting Coord Handle")

      coord_handle = Coord_CoordHandle(cctkGH, "x", "cart2d")
      if (coord_handle .lt. 0) then
         call CCTK_WARN(0, "Error retrieving coordinate handle for x of cart2d")
      endif

c      call CCTK_INFO("Getting Variable Index")

      call CCTK_VarIndex(varindex, "wavetoy2df77::x")

c      call CCTK_INFO("Setting Physical Minimum")

      call Util_TableSetReal   (ierr,coord_handle, lowerx, "PHYSICALMIN")

c      call CCTK_INFO("Setting Computational Minimum")

      call Util_TableSetReal   (ierr,coord_handle, lowerx, "COMPMIN")

c      call CCTK_INFO("Setting Physical Maximum")

      call Util_TableSetReal   (ierr,coord_handle, upperx, "PHYSICALMAX")

c      call CCTK_INFO("Setting Computational Maximum")

      call Util_TableSetReal   (ierr,coord_handle, upperx, "COMPMAX")

c      call CCTK_INFO("Setting Coordinate Type")

      call Util_TableSetString (ierr,coord_handle, "uniform", "TYPE")

c      call CCTK_INFO("Setting Time dependency")

      call Util_TableSetString (ierr,coord_handle, "no", "TIMEDEPENDENT")

c      call CCTK_INFO("Setting datatype")

      call Util_TableSetString (ierr,coord_handle, "CCTK_REAL", "DATATYPE")

c      call CCTK_INFO("Setting index")

      call Util_TableSetInt    (ierr,coord_handle, varindex, "GAINDEX")

c      call CCTK_INFO("Setting delta")

      call Util_TableSetReal   (ierr,coord_handle, cctk_delta_space(1), "DELTA")

c      call CCTK_INFO("Finished Setting up Coordinates")

      lowery = y_origin
      uppery = y_origin+cctk_delta_space(2)*(cctk_gsh(2)-1)

c      call CCTK_INFO("Getting Coord Handle")

      coord_handle = Coord_CoordHandle(cctkGH, "y", "cart2d")
      if (coord_handle .lt. 0) then
         call CCTK_WARN(0, "Error retrieving coordinate handle for y of cart2d")
      endif

c      call CCTK_INFO("Getting Variable Index")

      call CCTK_VarIndex(varindex, "wavetoy2df77::y")

c      call CCTK_INFO("Setting Physical Minimum")

      call Util_TableSetReal   (ierr,coord_handle, lowery, "PHYSICALMIN")

c      call CCTK_INFO("Setting Computational Minimum")

      call Util_TableSetReal   (ierr,coord_handle, lowery, "COMPMIN")

c      call CCTK_INFO("Setting Physical Maximum")

      call Util_TableSetReal   (ierr,coord_handle, uppery, "PHYSICALMAX")

c      call CCTK_INFO("Setting Computational Maximum")

      call Util_TableSetReal   (ierr,coord_handle, uppery, "COMPMAX")

c      call CCTK_INFO("Setting Coordinate Type")

      call Util_TableSetString (ierr,coord_handle, "uniform", "TYPE")

c      call CCTK_INFO("Setting Time dependency")

      call Util_TableSetString (ierr,coord_handle, "no", "TIMEDEPENDENT")

c      call CCTK_INFO("Setting datatype")

      call Util_TableSetString (ierr,coord_handle, "CCTK_REAL", "DATATYPE")

c      call CCTK_INFO("Setting index")

      call Util_TableSetInt    (ierr,coord_handle, varindex, "GAINDEX")

c      call CCTK_INFO("Setting delta")

      call Util_TableSetReal   (ierr,coord_handle, cctk_delta_space(2), "DELTA")

c      call CCTK_INFO("Finished Setting up Coordinates")

      return
      end 



```

make.code.defn:
```
# Main make.code.defn file for thorn WaveToy2DF77
# $Header$

# Source files in this directory
SRCS = Evolve.F77\
       Startup.F77\
       Initial.F77\
       Coord.F77\
       Boundary.F77

# Subdirectories containing source files
SUBDIRS = 


```

Boundary.F77:
```
#include "cctk.h"
#include "cctk_Arguments.h"
#include "cctk_Functions.h"
#include "cctk_Parameters.h"

      subroutine WaveToy2DF77_Boundary(CCTK_ARGUMENTS)

      implicit none

      DECLARE_CCTK_ARGUMENTS
      DECLARE_CCTK_PARAMETERS
      DECLARE_CCTK_FUNCTIONS

      integer ierr
      integer sw(2)
      CCTK_REAL zero
      
      sw(1) = 1
      sw(2) = 1

      zero = 0.0d0 

      if (CCTK_EQUALS(bound,"zero")) then
        call BndScalarVN(ierr,cctkGH, sw, zero,"wavetoy2df77::phi")
      end if

      return
      end

```


combined_doc_context: ## README (README):
```
Cactus Code Thorn WaveToy2DF77
Author(s)    : Gabrielle Allen
Maintainer(s): Cactus team
Licence      : LGPL
--------------------------------------------------------------------------

1. Purpose

This thorn illustrates the solution of the scalar wave equation in 2D using
centered explicit finite differencing. Included are routines to
NOTE: This thorn isn't working properly yet for multiple processors, it 
      will be fixed very soon in the development version.

 * Set up and register the 2D spatial coordinate system
 * Calculate initial data
 * Evolve using a standard explicit method

The Cactus infrastructure used includes

 * Boundary conditions  (CactusBase/Boundary)
 * Timestep calculation (CactusBase/Time)
 * Parallel driver      (CactusPUGH/PUGH)
 * IO			(Standard IO thorns)


```

## Documentation (.tex files):
Files: documentation.tex

```tex
=== documentation.tex ===
% *======================================================================*
%  Cactus Thorn template for ThornGuide documentation
%  Author: Ian Kelley
%  Date: Sun Jun 02, 2002
%  $Header$                                                             
%
%  Thorn documentation in the latex file doc/documentation.tex 
%  will be included in ThornGuides built with the Cactus make system.
%  The scripts employed by the make system automatically include 
%  pages about variables, parameters and scheduling parsed from the 
%  relevent thorn CCL files.
%  
%  This template contains guidelines which help to assure that your     
%  documentation will be correctly added to ThornGuides. More 
%  information is available in the Cactus UsersGuide.
%                                                    
%  Guidelines:
%   - Do not change anything before the line
%       % BEGIN CACTUS THORNGUIDE",
%     except for filling in the title, author, date etc. fields.
%   - You can define your own macros are OK, but they must appear after
%     the BEGIN CACTUS THORNGUIDE line, and do not redefine standard 
%     latex commands.
%   - To avoid name clashes with other thorns, 'labels', 'citations', 
%     'references', and 'image' names should conform to the following 
%     convention:          
%       ARRANGEMENT_THORN_LABEL
%     For example, an image wave.eps in the arrangement CactusWave and 
%     thorn WaveToyC should be renamed to CactusWave_WaveToyC_wave.eps
%   - Graphics should only be included using the graphix package. 
%     More specifically, with the "includegraphics" command. Do
%     not specify any graphic file extensions in your .tex file. This 
%     will allow us (later) to create a PDF version of the ThornGuide
%     via pdflatex. |
%   - References should be included with the latex "bibitem" command.  
%   - For the benefit of our Perl scripts, and for future extensions, 
%     please use simple latex.     
%
% *======================================================================* 
% 
% Example of including a graphic image:
%    \begin{figure}[ht]
% 	\begin{center}
%    	   \includegraphics[width=6cm]{MyArrangement_MyThorn_MyFigure}
% 	\end{center}
% 	\caption{Illustration of this and that}
% 	\label{MyArrangement_MyThorn_MyLabel}
%    \end{figure}
%
% Example of using a label:
%   \label{MyArrangement_MyThorn_MyLabel}
%
% Example of a citation:
%    \cite{MyArrangement_MyThorn_Author99}
%
% Example of including a reference
%   \bibitem{MyArrangement_MyThorn_Author99}
%   {J. Author, {\em The Title of the Book, Journal, or periodical}, 1 (1999), 
%   1--16. {\tt http://www.nowhere.com/}}
%
% *======================================================================* 

% If you are using CVS use this line to give version information
% $Header$

\documentclass{article}

% Use the Cactus ThornGuide style file
% (Automatically used from Cactus distribution, if you have a 
%  thorn without the Cactus Flesh download this from the Cactus
%  homepage at www.cactuscode.org)
\usepackage{../../../../doc/latex/cactus}

\begin{document}

% The author of the documentation
\author{} 

% The title of the document (not necessarily the name of the Thorn)
\title{}

% the date your document was last changed, if your document is in CVS, 
% please us:
%    \date{$ $Date$ $}
\date{}

\maketitle

% Do not delete next line
% START CACTUS THORNGUIDE

% Add all definitions used in this documentation here 
%   \def\mydef etc

% Add an abstract for this thorn's documentation
\begin{abstract}

\end{abstract}

% The following sections are suggestive only.
% Remove them or add your own.

\section{Introduction}

\section{Physical System}

\section{Numerical Implementation}

\section{Using This Thorn}

\subsection{Obtaining This Thorn}

\subsection{Basic Usage}

\subsection{Special Behaviour}

\subsection{Interaction With Other Thorns}

\subsection{Support and Feedback}

\section{History}

\subsection{Thorn Source Code}

\subsection{Thorn Documentation}

\subsection{Acknowledgements}


\begin{thebibliography}{9}

\end{thebibliography}

% Do not delete next line
% END CACTUS THORNGUIDE

\end{document}

```



Reference answer:
src_code:  /*@@
   @file      Evolve.F77
   @date      July 2 2000
   @author    Gabrielle Allen
   @desc 
              Evolution routines for the 2d wave equation solver
   @enddesc 
 @@*/
  
#include "cctk.h" 
#include "cctk_Arguments.h"


 /*@@
   @routine    WaveToy2DF77_Evolve
   @date       July 2 2000
   @author     Gabrielle Allen
   @desc 
               Evolution for the 2d wave equation
   @enddesc 
   @calls      
   @calledby   
   @history 
 
   @endhistory 

@@*/

      subroutine WaveToy2DF77_Evolve(CCTK_ARGUMENTS)

      implicit none

      DECLARE_CCTK_ARGUMENTS

      INTEGER   i,j
      CCTK_REAL dt,dx,dy
      CCTK_REAL fac1,fac2,fac3

      dx     = CCTK_DELTA_SPACE(1)
      dy     = CCTK_DELTA_SPACE(2)
      dt     = CCTK_DELTA_TIME

      fac3   = (dt*dt)/(dy*dy)
      fac2   = (dt*dt)/(dx*dx)
      fac1   = 2.0d0*(1.0d0 - fac2 - fac3)

      do j = 2, cctk_lsh(2)-1
        do i = 2, cctk_lsh(1)-1
          phi(i,j) = fac1*phi_p(i,j)-phi_p_p(i,j)+
     &                 fac2*(phi_p(i+1,j)+phi_p(i-1,j))
     &                +fac3*(phi_p(i,j+1)+phi_p(i,j-1))
        end do
      end do

      return
      end