Task: Implement Complex.c for CactusTest/TestComplex thorn

thorn_name: CactusTest/TestComplex
src_filename: Complex.c
interface: # Interface definition for thorn TestComplex
# $Header$

implements: testcomplex

complex cvars3d dim=3 type=gf timelevels=2
{
  comp3d
} "3D complex grid functions"

#complex cvars2d dim=2 type=gf
#{
#  comp2d
#} "2D complex grid functions"

#complex cvars1d dim=1 type=gf
#{
#  comp1d
#} "1D complex grid functions"

complex carrays3d dim=3 size=10,10,10 type=array
{
  acomp3d
} "3D complex grid arrays"

complex carrays2d dim=2 size=10,10 type=array
{
  acomp2d
} "2D complex grid arrays"

complex carrays1d dim=1 size=10 type=array
{
  acomp1d
} "1D complex grid arrays"

complex cscalars type=scalar
{
  scomp
} "complex grid scalars"

real cmplx_output type=scalar
{
  real_part
  imaginary_part
}

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

STORAGE: cvars3d[2]
STORAGE: carrays3d carrays2d carrays1d cscalars
STORAGE: cmplx_output

schedule InitComplex3D at INITIAL
{
  LANG: Fortran
} "Initialise 3D complex grid functions"

schedule EvolveComplex3D at EVOL
{
  LANG: Fortran
  SYNC: cvars3d
} "Evolve 3D complex grid functions"

schedule TestComplexPower at CCTK_POSTINITIAL
{
  LANG: C
} "Do a simple complex power operation"

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


REAL power_value "power of the complex number to take" STEERABLE = ALWAYS
{
 0.0: :: "Some positive value"
} 0.5

REAL real_value "power of the complex number to take" STEERABLE = ALWAYS
{
 *:* :: "real part"
} 3

REAL imaginary_value "power of the complex number to take" STEERABLE = ALWAYS
{
 *:* :: "imaginary part"
} -2
context: make.code.defn:
```
# Main make.code.defn file for thorn TestComplex
# $Header$

# Source files in this directory
SRCS = Complex3D.F77 Complex.c

# Subdirectories containing source files
SUBDIRS = 


```

Complex3D.F77:
```
c     $Header$

#include "cctk.h"
#include "cctk_Arguments.h"

      subroutine InitComplex3D(CCTK_ARGUMENTS)

      implicit none

      DECLARE_CCTK_ARGUMENTS

      integer grouplbnd(3), grouplsh(3)
      integer i0,j0,k0
      integer nx,ny,nz
      integer i,j,k
      integer ierr

      i0 = cctk_lbnd(1)
      j0 = cctk_lbnd(2)
      k0 = cctk_lbnd(3)
      nx = cctk_lsh(1)
      ny = cctk_lsh(2)
      nz = cctk_lsh(3)

      do k=1,nz
        do j=1,ny
          do i=1, nx

            comp3d_p(i,j,k) = dcmplx(sqrt(dble(i0+i)**2+
     &                       	          dble(j0+j)**2+
     &	                     	          dble(k0+k)**2
     &                                   )
     &                              ,
     &                               dble(i0+i)**2+
     &                               dble(j0+j)**2+
     &	                     	     dble(k0+k)**2
     &                              )

	    comp3d(i,j,k) = comp3d_p(i,j,k)

          end do
        end do
      end do

      call CCTK_GrouplbndGN (ierr, cctkGH, 3, grouplbnd, "TestComplex::carrays3d")
      if (ierr .ne. 0) call CCTK_ERROR ("Group not found")
      i0 = grouplbnd(1)-1
      j0 = grouplbnd(2)-1
      k0 = grouplbnd(3)-1

      call CCTK_GrouplshGN (ierr, cctkGH, 3, grouplsh, "TestComplex::carrays3d")
      if (ierr .ne. 0) call CCTK_ERROR ("Group not found")
      nx = grouplsh(1)
      ny = grouplsh(2)
      nz = grouplsh(3)

      do k=1,nz
         do j=1,ny
            do i=1,nx
               acomp3d(i,j,k) = dcmplx(i0+i+j0+j,j0+j+k0+k)
            end do
         end do
      end do

      call CCTK_GrouplbndGN (ierr, cctkGH, 2, grouplbnd, "TestComplex::carrays2d")
      if (ierr .ne. 0) call CCTK_ERROR ("Group not found")
      i0 = grouplbnd(1)-1
      j0 = grouplbnd(2)-1

      call CCTK_GrouplshGN (ierr, cctkGH, 2, grouplsh, "TestComplex::carrays2d")
      if (ierr .ne. 0) call CCTK_ERROR ("Group not found")
      nx = grouplsh(1)
      ny = grouplsh(2)

      do j=1,ny
         do i=1,nx
            acomp2d(i,j) = dcmplx(i0+i,j0+j)
         end do
      end do

      call CCTK_GrouplbndGN (ierr, cctkGH, 1, grouplbnd, "TestComplex::carrays1d")
      if (ierr .ne. 0) call CCTK_ERROR ("Group not found")
      i0 = grouplbnd(1)-1

      call CCTK_GrouplshGN (ierr, cctkGH, 1, grouplsh, "TestComplex::carrays1d")
      if (ierr .ne. 0) call CCTK_ERROR ("Group not found")
      nx = grouplsh(1)

      do i=1,nx
         acomp1d(i) = dcmplx(1,i0+i)
      end do

      scomp = dcmplx(1,2)

      end


      subroutine EvolveComplex3D(CCTK_ARGUMENTS)

      implicit none

      DECLARE_CCTK_ARGUMENTS

      integer nx,ny,nz
      integer i,j,k

      nx = cctk_lsh(1)
      ny = cctk_lsh(2)
      nz = cctk_lsh(3)

c     provide boundary data
      comp3d = comp3d_p

      do k=2,nz-1
        do j=2,ny-1
          do i=2,nx-1

            comp3d(i,j,k) = 1.0/6.0*(
     &          comp3d_p(i-1,j,k)+comp3d_p(i+1,j,k)+
     &          comp3d_p(i,j-1,k)+comp3d_p(i,j+1,k)+
     &          comp3d_p(i,j,k-1)+comp3d_p(i,j,k+1)
     &      )

          end do
        end do
      end do

      end

```


combined_doc_context: ## README (README):
```
Cactus Code Thorn TestComplex
Author(s)    : unknown
Maintainer(s): Cactus team
Licence      : LGPL
--------------------------------------------------------------------------

1. Purpose

not documented

```

## 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: #include <math.h>

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

static const char *rcsid = "$Id$";

CCTK_FILEVERSION(CactusTest_TestComplex_Complex_c);

void TestComplexPower(CCTK_ARGUMENTS)
{
  DECLARE_CCTK_ARGUMENTS;
  DECLARE_CCTK_PARAMETERS;
  
  CCTK_COMPLEX num, tmp;
  
  num = CCTK_Cmplx(real_value, imaginary_value);
  
  tmp = CCTK_CmplxPow(num, power_value);
  
  *real_part      = CCTK_CmplxReal(tmp);
  *imaginary_part = CCTK_CmplxImag(tmp); 
}