Task: Implement Evolve.F77 for CactusExamples/WaveToy1DF77 thorn

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

implements: wavetoy1d

inherits: boundary

private:

cctk_real evolve1d dim=1 type=GF Timelevels=3
{
  phi
} "The evolved scalar field"

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

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

cctk_real coords1d dim=1 type=GF
{
  x
} "The 1D x-coordinate"

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 WaveToy1DF77
# $Header$

STORAGE: evolve1d[3],coords1d

schedule WaveToy1DF77_Startup at STARTUP
{
  LANG: Fortran
} "Register WaveToy1DF77 banner"

schedule WaveToy1DF77_RegisterCoords at WRAGH
{
  LANG: Fortran
} "Register WaveToy1DF77 coordinates"

schedule WaveToy1DF77_Coord as SpatialCoordinates at BASEGRID
{
  LANG: Fortran
} "Set up 1D coordinates"

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

schedule WaveToy1DF77_Evolve at EVOL
{
  LANG: Fortran
  SYNC: evolve1d
} "Evolution of 1D wave equation"

schedule WaveToy1DF77_Boundary at EVOL after WaveToy1DF77_Evolve
{
  LANG: Fortran
} "Apply boundary conditions"

  

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

private:

# Initial data

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

real center "x-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"
  "none"      :: "Don't apply any condition"       
} "zero"
configuration: # Configuration definition for thorn WaveToy1DF77
# $Header$

REQUIRES Boundary

context: Startup.F77:
```
 /*@@
   @file      Startup.F77
   @date      June 13 2000
   @author    Gabrielle Allen
   @desc 
              Register banner for WaveToy1DF77
   @enddesc 
   @version $Header$
 @@*/

#include "cctk.h"

      integer function WaveToy1DF77_Startup()

      implicit none
   
      integer ierr

      call CCTK_RegisterBanner(ierr, 
     &	"WaveToy1DF77: Evolution of a 1D Scalar Field")

      WaveToy1DF77_Startup = ierr

      end 

```

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


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

@@*/

      subroutine WaveToy1DF77_Initial(CCTK_ARGUMENTS)

      implicit none

      DECLARE_CCTK_ARGUMENTS
      DECLARE_CCTK_PARAMETERS 

      INTEGER   i,ierr
      CCTK_REAL dx,dt
      CCTK_REAL zero
      integer   sw(1)

      sw(1) = 1
      zero = 0.0d0

      dx = CCTK_DELTA_SPACE(1)
      dt = CCTK_DELTA_TIME

c     For now only have zero time derivative
      do i = 1, cctk_lsh(1)
	timederiv(i) = 0.0d0
      end do               
    
      do i = 1, cctk_lsh(1)
         phi(i) = amplitude*exp(-(sqrt(x(i)**2)-center)**2/sigma**2)
      end do

c     This is horrible, use a temporary grid variable
      phi_temp(1) = 0.0d0
      do i = 2, cctk_lsh(1)-1
	 phi_temp(i) = dt*timederiv(i) + phi(i) + 0.5d0*dt**2/dx**2*
     &	            (phi(i+1)-2.0d0*phi(i)+phi(i-1))
      end do
      phi_temp(cctk_lsh(1)) = 0.0d0

      call CCTK_SyncGroup(ierr,cctkGH,"wavetoy1df77::dummy")

      do i = 1, cctk_lsh(1)
         phi_p(i) = phi_temp(i)
      end do

      return
      end 



```

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


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

@@*/

      SUBROUTINE WaveToy1DF77_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, 1_ik, "cart1d");

C      call CCTK_CoordRegisterSystem(ierr,1,"cart1d")

c      call CCTK_INFO("Getting System Handle")

      coord_system_handle = Coord_SystemHandle(cctkGH, "cart1d")

c      call CCTK_INFO("Setting Coord Handle")

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

c      call CCTK_INFO("Setting Coord Type")

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

C      call CCTK_CoordRegisterData(ierr,1,"wavetoy1df77::x1d","x","cart1d")

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

c      call CCTK_INFO("Setting Default System")

      ierr = Coord_SetDefaultSystem(cctkGH, "cart1d");

      return
      end


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

@@*/

      subroutine WaveToy1DF77_Coord(CCTK_ARGUMENTS)

      implicit none

      DECLARE_CCTK_ARGUMENTS
      DECLARE_CCTK_FUNCTIONS

      INTEGER   i,ierr
      CCTK_REAL xmin,xmax,x_origin,lowerx,upperx
      CCTK_INT coord_handle
      INTEGER varindex

      xmin=0
      xmax=1

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

      do i=1,cctk_lsh(1)
         x(i) = cctk_delta_space(1)*(i-1+cctk_lbnd(1)) + x_origin
      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", "cart1d")
      if (coord_handle .lt. 0) then
         call CCTK_WARN(0, "Error retrieving coordinate handle for x of cart1d")
      endif

c      call CCTK_INFO("Getting Variable Index")

      call CCTK_VarIndex(varindex, "wavetoy1df77::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")

      return
      end 



```

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

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

# Subdirectories containing source files
SUBDIRS = 


```

Boundary.F77:
```
 /*@@
   @file      Boundary.F77
   @date      July 15th 2000
   @author    Gabrielle Allen
   @desc 
   Apply boundary conditions for 1D Wave Equation
   @enddesc 
 @@*/

#include "cctk.h"
#include "cctk_Arguments.h"
#include "cctk_Functions.h"
#include "cctk_Parameters.h"

      subroutine WaveToy1DF77_Boundary(CCTK_ARGUMENTS)

      implicit none

      DECLARE_CCTK_ARGUMENTS
      DECLARE_CCTK_PARAMETERS
      DECLARE_CCTK_FUNCTIONS

      integer ierr
      integer sw(1)
      CCTK_REAL zero,one
      
      sw(1) = 1
      zero = 0.0d0
      one = 1.0d0

      if (CCTK_EQUALS(bound,"zero")) then
        call BndScalarVN(ierr,cctkGH, sw, zero, "wavetoy1df77::phi")
      else if (CCTK_EQUALS(bound,"radiative")) then
	call BndRadiativeVN(ierr,cctkGH,sw,one,one,"wavetoy1df77::phi",
     &	                    "wavetoy1df77::phi")
      else if (CCTK_EQUALS(bound,"none")) then
	call CCTK_WARN(9,"Hopefully applying periodic boundary condition")
      else 
	call CCTK_INFO("No boundary condition applied")
      end if

      return
      end

```


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

1. Purpose

This thorn illustrates the solution of the scalar wave equation in 1D using
centered explicit finite differencing. Included are routines to

 * Set up and register the 1D 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      June 13 2000
   @author    Gabrielle Allen
   @desc 
              Evolution routines for the 1d wave equation solver
   @enddesc 
 @@*/
  
#include "cctk.h" 
#include "cctk_Arguments.h"


 /*@@
   @routine    WaveToy1DF77_Evolve
   @date       June 13 2000
   @author     Gabrielle Allen
   @desc 
               Evolution for the 1d wave equation
   @enddesc 
   @calls      
   @calledby   
   @history 
 
   @endhistory 

@@*/

      subroutine WaveToy1DF77_Evolve(CCTK_ARGUMENTS)

      implicit none

      DECLARE_CCTK_ARGUMENTS

      INTEGER   i
      integer   istart, iend
      CCTK_REAL dt,dx
      CCTK_REAL fac1,fac2

      dx     = CCTK_DELTA_SPACE(1)
      dt     = CCTK_DELTA_TIME

      istart = 2
      iend   = cctk_lsh(1)-1

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

      do i = istart, iend
         phi(i) = fac1*phi_p(i)-phi_p_p(i)+fac2*(phi_p(i+1)+phi_p(i-1))
      end do

      return
      end