aboutsummaryrefslogtreecommitdiff
path: root/src/AHFinder_int.F
blob: b485149f0f57d96f45d899abacc60612fa3cd7a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
/*@@
  @file      AHFinder_int.F
  @date      April 1998
  @author    Miguel Alcubierre
  @desc 
             Find area of surface and integral of expansion.
             This is done in parallel, but the number of
             processors is assumed to be either the square
             of an integer or a power of two (if this is not
             the case, I use less processors).
  @enddesc 
@@*/

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

      subroutine AHFinder_int(CCTK_FARGUMENTS)

      use AHFinder_dat

      implicit none

      DECLARE_CCTK_FARGUMENTS
      DECLARE_CCTK_PARAMETERS
      DECLARE_CCTK_FUNCTIONS

      integer i,j,l,m
      integer npt,npp
      integer l_ntheta,l_nphi,theta0,phi0
      integer npoints
      integer ierror,auxi
      integer handle

      CCTK_REAL LEGEN
      CCTK_REAL theta,phi,xp,yp,zp,rp
      CCTK_REAL xw,yw,zw
      CCTK_REAL cost,sint,cosp,sinp
      CCTK_REAL trr,ttt,tpp,trt,trp,ttp,ft,fp
      CCTK_REAL xmn,ymn,zmn,xmx,ymx,zmx
      CCTK_REAL dtheta,dphi,dtp,idtheta,idphi
      CCTK_REAL det,idet
      CCTK_REAL zero,quarter,half,one,two,three,four,pi
      CCTK_REAL sina,cosa,intw,grad,sigma,h0
      CCTK_REAL aux,aux1,aux2

      CCTK_REAL gupij(3,3)
      CCTK_REAL tempv(0:lmax),tempm(lmax,lmax)
      CCTK_REAL, dimension(3) :: origin,maxval,minval
      CCTK_REAL, allocatable, dimension(:,:) :: rr,xa,ya,za,da,exp,gradn
      CCTK_REAL, allocatable, dimension(:,:) :: txx,tyy,tzz,txy,txz,tyz
      CCTK_REAL, allocatable, dimension(:,:) :: intmask

!     Reduction related things.

      CCTK_INT reduction_handle,red_tmp

!     Variables to be saved for next call.

      save xmn,ymn,zmn,xmx,ymx,zmx
      save dtheta,dphi,dtp,idtheta,idphi
      save npt,npp,l_ntheta,l_nphi,theta0,phi0

!     Description of variables:
!
!     i,j,l,m    Counters.
!
!     theta      Latitude.
!     phi        Longitude.
!
!     npt        number of processors in theta direction.
!     npp        number of processors in phi direction.
!
!     l_ntheta   Local number of grid points in theta direction.
!     l_nphi     Local number of grid points in phi direction.
!
!     theta0     Local origin in theta direction.
!     phi0       Local origin in phi direction.
!
!     npoints    Number of points to interpolate .
!
!     xp         x coordinate from surface centre.
!     yp         y coordinate from surface centre.
!     zp         z coordinate from surface centre.
!
!     rp         Radius.
!     rr         Radius array.
!
!     xa         Array with x coordinates from grid centre.
!     ya         Array with y coordinates from grid centre.
!     za         Array with z coordinates from grid centre.
!
!     exp        Interpolated expansion.
!
!     txx        Array with interpolated gxx metric component.
!     tyy        Array with interpolated gyy metric component.
!     tzz        Array with interpolated gzz metric component.
!     txy        Array with interpolated gxy metric component.
!     txz        Array with interpolated gxz metric component.
!     tyz        Array with interpolated gyz metric component.
!
!     gradn      Array with interpolated norm of gradient of horizon function.
!
!     intmask    Array with interpolated mask.
!
!     cost       cos(theta)
!     sint       sin(theta)
!     cosp       cos(phi)
!     sinp       sin(phi)
!
!     trr        Metric component {r,r}.
!     ttt        Metric component {theta,theta}.
!     tpp        Metric component {phi,phi}.
!     trt        Metric component {r,theta}.
!     trp        Metric component {r,phi}.
!     ttp        Metric component {theta,phi}.
!
!     ft         dr/dtheta
!     fp         dr/dphi
!
!     xmn        Minimum value of x over the grid.
!     xmx        Maximum value of x over the grid.
!
!     ymn        Minimum value of y over the grid.
!     ymx        Maximum value of y over the grid.
!
!     zmn        Minimum value of z over the grid.
!     zmx        Maximum value of z over the grid.
!
!     dtheta     Grid spacing in theta.
!     dphi       Grid spacing in phi.
!     dtp        dtheta*dphi.
!
!     idtheta    1/(2 dtheta)
!     idphi      1/(2 dphi)
!
!     da         Area element.

!     Get the reduction handle for the sum operation.

      call CCTK_ReductionArrayHandle(reduction_handle,"sum")

      if (reduction_handle.lt.0) then 
        call CCTK_WARN(1,"Cannot get reduction handle for SUM operation.")
      endif


!     **************************
!     ***   DEFINE NUMBERS   ***
!     **************************

      zero    = 0.0D0
      quarter = 0.25D0
      half    = 0.5D0
      one     = 1.0D0
      two     = 2.0D0
      three   = 3.0D0
      four    = 4.0D0

      pi = 3.141592654D0

      myproc = CCTK_MyProc(cctkGH)
      nprocs = CCTK_nProcs(cctkGH)


!     *********************************
!     ***   INITIALIZE PARAMETERS   ***
!     *********************************

      if (firstint) then

         firstint = .false.

!        Find grid boundaries.
         
         call CCTK_CoordRange(ierror,cctkGH,xmn,xmx,"x")
         call CCTK_CoordRange(ierror,cctkGH,ymn,ymx,"y")
         call CCTK_CoordRange(ierror,cctkGH,zmn,zmx,"z")

!        Find {dtheta,dphi,dtp}.

         dtheta = pi/dble(ntheta)

         if (cartoon) then

            dphi    = zero
            dtp     = dtheta
            idphi   = one
            idtheta = half/dtheta

         else

            dphi = two*pi/dble(nphi)

            if (refz) dtheta = half*dtheta

            if (refx.and.refy) then
               dphi = quarter*dphi
            else if (refx) then
               call CCTK_INFO('This combination of symmetries has not been properly implemented yet!')
            else if (refy) then
               dphi = half*dphi
            end if

            dtp = dtheta*dphi

            idtheta = half/dtheta
            idphi   = half/dphi

         end if

!        Check the number of processors to figure out
!        how to divide the domain.

!        For cartoon this is trivial.

         if (cartoon) then

            npt = nprocs
            npp = 1

!        Otherwise it is more complicated.  At the moment I
!        only consider numbers of processors that are either
!        the square of an integer, or a power of two.
!        If neither of this is the case, then I use fewer
!        processors to make sure that I always deal with
!        either a square or a power of two.  This can
!        certainly be improved, but it might not be that
!        urgent since in most cases the number of processors
!        will be a power of two.

         else

            aux = sqrt(dble(nprocs))

            if (nprocs.eq.1) then

!              One processor.

               npt = 1
               npp = 1

            else if (aux.eq.int(aux)) then

!              "nprocs" is the square of an integer.

               npt = int(aux)
               npp = int(aux)

            else

!              Check if "nprocs" is a power of two.

               l = 1
               m = 1

               i = 1

               do while (l*m.lt.nprocs)

                  npt = l
                  npp = m

                  if (mod(i,2).ne.0) then
                     m = 2*m
                  else
                     l = 2*l
                  end if

                  i = i + 1

               end do

!              "nprocs" is a power of two.

               if (l*m.eq.nprocs) then

                  npt = l
                  npp = m

!              "nprocs" is not a power of two.  This is where I
!              would need to do something clever.  At the moment,
!              I will just use fewer processors.  The number of
!              processors I use is the power of two or the square
!              of an integer that is closest to "nprocs".

               else

                  if (npt*npp.lt.int(aux)**2) then
                     npt = int(aux)
                     npp = int(aux)
                  end if

               end if

            end if

         end if

!        Figure out the number of grid points per processor,
!        and the "origin" for a given processor.

         if (myproc.ge.npt*npp) then

            l_ntheta = 0
            l_nphi   = 0

            theta0 = 0
            phi0   = 0

         else

!           Take first the number of grid points per processor
!           in a given direction to be equal to the total number
!           of grid points divided by the number of processors
!           in that direction.

            l_ntheta = ntheta/npt
            l_nphi   = nphi/npp

!           And take the "origin" as the corresponding displacement
!           from the real origin.  Notice that mod(myproc,npt)
!           tells my on which "theta" bin I am, while myproc/npt
!           tells me on which "phi" bin I am.

            i = mod(int(myproc),int(npt))
            j = myproc/npt

            theta0 = i*l_ntheta 
            phi0   = j*l_nphi 

!           Now correct all this if the total number of grid points
!           in a given direction was not exactly divisible by the
!           number of processors on that direction.

            if (l_ntheta*npt.ne.ntheta) then

!              Find residue on theta direction.

               l = ntheta - l_ntheta*npt

!              Distribute residue in first few processors, and
!              displace theta0 accordingly.

               if (i.lt.l) then
                  l_ntheta = l_ntheta + 1
                  theta0 = theta0 + i
               else
                  theta0 = theta0 + l
               end if

            end if

            if (l_nphi*npp.ne.nphi) then

!              Find residue on phi direction.

               l = nphi - l_nphi*npp

!              Distribute residue in first few processors, and
!              displace phi0 accordingly.

               if (j.lt.l) then
                  l_nphi = l_nphi + 1
                  phi0 = phi0 + j
               else
                  phi0 = phi0 + l
               end if

            end if

         end if

      end if



!     **************************************
!     ***   ALLOCATE MEMORY FOR ARRAYS   ***
!     **************************************

      allocate(rr(0:l_ntheta,0:l_nphi))

      allocate(xa(0:l_ntheta,0:l_nphi))
      allocate(ya(0:l_ntheta,0:l_nphi))
      allocate(za(0:l_ntheta,0:l_nphi))

      allocate(da(0:l_ntheta,0:l_nphi))
      allocate(exp(0:l_ntheta,0:l_nphi))
      allocate(gradn(0:l_ntheta,0:l_nphi))

      allocate(txx(0:l_ntheta,0:l_nphi))
      allocate(tyy(0:l_ntheta,0:l_nphi))
      allocate(tzz(0:l_ntheta,0:l_nphi))
      allocate(txy(0:l_ntheta,0:l_nphi))
      allocate(txz(0:l_ntheta,0:l_nphi))
      allocate(tyz(0:l_ntheta,0:l_nphi))

      allocate(intmask(0:l_ntheta,0:l_nphi))

!     Initialize.

      rr = zero

      xa = zero; ya = zero; za = zero

      da = zero; exp = zero; gradn = zero

      txx = one;  tyy = one;  tzz = one
      txy = zero; txz = zero; tyz = zero


!     ***************************
!     ***   START MAIN LOOP   ***
!     ***************************

!     Initialize {interror1,interror2}.

      interror1 = 0
      interror2 = 0

!     Initialize {intexp,intexp2,intarea}.

      intexp  = zero
      intexp2 = zero
      intarea = zero
      intexpdel2 = zero

!     For flow algorithm, initialize spectral components.

      if (flow) then

         hflow0 = zero
         hflowc = zero
         hflows = zero

         cflow0 = zero
         cflowc = zero
         cflows = zero

         nflow0 = zero
         nflowc = zero
         nflows = zero

      end if

!     Find interpolating points.

      if (myproc.ge.npt*npp) then

         xa = half*(xmx+xmn)
         ya = half*(ymx+ymn)
         za = half*(zmx+zmn)

      else

         do j=0,l_nphi
            do i=0,l_ntheta

!              Find {theta,phi}.

               theta = dtheta*dble(i+theta0)
               phi   = dphi*dble(j+phi0)

!              Find sines and cosines.

               cost = cos(theta)
               sint = sin(theta)

               cosp = cos(phi)
               sinp = sin(phi)

!              Find radius rp.

               rp = c0(0)

               do l=1+stepz,lmax,1+stepz
                  rp = rp + c0(l)*LEGEN(l,0,cost)
               end do

!              Notice how the sum over m is first.  This will allow
!              me to use the recursion relations to avoid having to
!              start from scratch every time.  Also, I sum over all
!              l s even if I do not want some terms.  This is
!              because in order to use the recursion relations I
!              need all polynomials.

               if (nonaxi) then
                  do m=1,lmax
                     aux = dble(m)*phi
                     sina = sin(aux)
                     cosa = cos(aux)
                     do l=m,lmax
                        aux = LEGEN(l,m,cost)
                        rp = rp + aux*cc(l,m)*cosa
                        if (.not.refy) then
                           rp = rp + aux*cs(l,m)*sina
                        end if
                     end do
                  end do
               end if

!              Check for negative radius.

               if (rp.le.zero) then
                  interror1 = 1
               end if

!              Save radius array.

               rr(i,j) = rp

!              Find {xa,ya,za}

               xa(i,j) = rp*sint*cosp + xc
               ya(i,j) = rp*sint*sinp + yc
               za(i,j) = rp*cost + zc

!              Check if we are within bounds.

               xp = xa(i,j)
               yp = ya(i,j)
               zp = za(i,j)

               if ((xp.gt.xmx).or.(xp.lt.xmn).or.
     .             (yp.gt.ymx).or.(yp.lt.ymn).or.
     .             (zp.gt.zmx).or.(zp.lt.zmn)) then
                  interror2 = 1
               end if

            end do
         end do

      end if

!     Reduce the errors across processors.

      call CCTK_ReduceLocalScalar(ierror,cctkGH,-1,reduction_handle,
     .   interror1,red_tmp,CCTK_VARIABLE_INT)

      if (ierror.ne.0) then
         call CCTK_WARN(1,"Reduction of norm failed!")
      end if

      interror1 = red_tmp

      call CCTK_ReduceLocalScalar(ierror,cctkGH,-1,reduction_handle,
     .   interror2,red_tmp,CCTK_VARIABLE_INT) 

      if (ierror.ne.0) then
         call CCTK_WARN(1,"Reduction of norm failed!")
      end if

      interror2 = red_tmp

!     If there was an error on any processor then assign
!     large values to the integrals and return from the
!     subroutine (but remember to deallocate the arrays
!     first!).

      interror = .false.

      if ((interror1.ne.0).or.(interror2.ne.0)) then
         intexp  = 1.0D10
         intexp2 = 1.0D10
         intarea = 1.0D10
         intexpdel2 = 1.0D10
         interror = .true.
         goto 10
      end if

!     Interpolate expansion and metric.

      npoints = (l_ntheta+1)*(l_nphi+1)
      origin  = cctk_origin_space + cctk_lbnd*cctk_delta_space

      call CCTK_GetInterpHandle(handle,"simple")

      call CCTK_Interp(ierror,cctkGH,handle,npoints,3,9,9,
     .   nx,ny,nz,xa,ya,za,CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL,
     .   CCTK_VARIABLE_REAL,origin(1),origin(2),origin(3),
     .   dx,dy,dz,gxx,gyy,gzz,gxy,gxz,gyz,ahf_exp,ahfgradn,ahmask,
     .   CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL,
     .   CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL,
     .   CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL,
     .   txx,tyy,tzz,txy,txz,tyz,exp,gradn,intmask,
     .   CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL,
     .   CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL,
     .   CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL,CCTK_VARIABLE_REAL)

!     Check if we are either inside mask, or to close for comfort.

      if (.not.CCTK_EQUALS(ahf_mask,'off')) then

         interror3 = 0

         if (myproc.lt.npt*npp) then
            do j=0,l_nphi
               do i=0,l_ntheta
                  if (intmask(i,j).ne.1.0D0) then
                     interror3 = 1
                  end if
               end do
            end do
         end if

         call CCTK_ReduceLocalScalar(ierror,cctkGH,-1,reduction_handle,
     .      interror3,red_tmp,CCTK_VARIABLE_INT)
         interror3 = red_tmp

         if (interror3.ne.0) then
            intexp  = 1.0D10
            intexp2 = 1.0D10
            intarea = 1.0D10
            intexpdel2 = 1.0D10
            interror = .true.
            goto 10
         end if

      end if

!     Find 2D array for area element.

      if (myproc.ge.npt*npp) then

         da = zero

      else

         do j=0,l_nphi
            do i=0,l_ntheta

!              Find {theta,phi}.

               theta = dtheta*dble(i+theta0)
               phi   = dphi*dble(j+phi0)

!              Find {rp}.

               rp = rr(i,j)

!              Find sines and cosines.

               cost = cos(theta)
               sint = sin(theta)

               cosp = cos(phi)
               sinp = sin(phi)

!              Find spherical metric components.

               trr = sint**2*(txx(i,j)*cosp**2
     .             + tyy(i,j)*sinp**2) + tzz(i,j)*cost**2
     .             + two*sint*(txy(i,j)*sint*cosp*sinp
     .             + cost*(txz(i,j)*cosp + tyz(i,j)*sinp))

               ttt = rp**2*(cost**2*(txx(i,j)*cosp**2
     .             + tyy(i,j)*sinp**2) + tzz(i,j)*sint**2
     .             + two*cost*(txy(i,j)*cost*cosp*sinp
     .             - sint*(txz(i,j)*cosp + tyz(i,j)*sinp)))

               tpp = (rp*sint)**2*(txx(i,j)*sinp**2 + tyy(i,j)*cosp**2
     .             - two*txy(i,j)*cosp*sinp)

               trt = rp*(sint*cost*(txx(i,j)*cosp**2 + tyy(i,j)*sinp**2
     .             - tzz(i,j) + two*txy(i,j)*sinp*cosp)
     .             + (cost**2-sint**2)*(txz(i,j)*cosp + tyz(i,j)*sinp))

               trp = rp*sint*(sint*(cosp*sinp*(tyy(i,j) - txx(i,j))
     .             + txy(i,j)*(cosp**2 - sinp**2))
     .             + cost*(cosp*tyz(i,j) - sinp*txz(i,j)))

               ttp = rp**2*sint*(cost*(sinp*cosp*(tyy(i,j) - txx(i,j))
     .             + (cosp**2 - sinp**2)*txy(i,j))
     .             + sint*(sinp*txz(i,j) - cosp*tyz(i,j)))

!              Find derivatives  {ft,fp}.  For interior points I
!              use centered differences, and for the boundary points
!              I use second order one sided differences. Notice that
!              since this is done even in inter-processor boundaries,
!              the final result will depend on the number of processors,
!              but the differences will converge away at high order.

               if ((i.ne.0).and.(i.ne.l_ntheta)) then
                  ft = idtheta*(rr(i+1,j) - rr(i-1,j))
               else if (i.eq.0) then
                  ft = - idtheta*(three*rr(0,j)
     .               - four*rr(1,j) + rr(2,j))
               else
                  ft = + idtheta*(three*rr(l_ntheta,j)
     .               - four*rr(l_ntheta-1,j) + rr(l_ntheta-2,j))
               end if

               if (nonaxi) then
                  if ((j.ne.0).and.(j.ne.l_nphi)) then
                     fp = idphi*(rr(i,j+1) - rr(i,j-1))
                  else if (j.eq.0) then
                     fp = - idphi*(three*rr(i,0)
     .                  - four*rr(i,1) + rr(i,2))
                  else
                     fp = + idphi*(three*rr(i,l_nphi)
     .                  - four*rr(i,l_nphi-1) + rr(i,l_nphi-2))
                  end if
               else
                  fp = zero
               end if

!              Find area element.  For this I use the fact that if
!              we have  r = f(theta,phi), then the area element is:
!
!                     /                  2
!              da  =  | [ gtt + grr (f,t)  +  2 grt f,t ]
!                     \
!                                   2
!                  [ gpp + grr (f,p)  +  2 grp f,p ]  -  [ gtp
!
!                                                       2 \ 1/2
!                  + grr f,t f,p  +  grt f,p + grp f,t ]  |     dtheta dphi
!                                                         /
!
!              where  f,t = df/dtheta  and  f,p = df/dphi.

               aux = (ttt + trr*ft**2 + two*trt*ft)
     .             * (tpp + trr*fp**2 + two*trp*fp)
     .             - (ttp + ft*(trr*fp + trp) + trt*fp)**2

               if (aux.gt.zero) then
                  aux = sqrt(aux)
               else
                  aux = zero
               end if

!              Multiply with dtheta*dphi.

               da(i,j) = aux*dtp

            end do
         end do

!        Find integrals.  To find the integrals I use a second
!        order method.  I approximate the value of the integrand
!        at each cell centre by an average of the values at the
!        four cell corners, and then add up all cells.

         inside_min_count     = 0
         inside_min_neg_count = 0

         do j=0,l_nphi-1
            do i=0,l_ntheta-1

               intarea = intarea + quarter
     .                 *(da(i,j  ) + da(i+1,j  )
     .                 + da(i,j+1) + da(i+1,j+1))

               intexp  = intexp  + quarter
     .                 *(da(i  ,j  )*exp(i  ,j  )
     .                 + da(i+1,j  )*exp(i+1,j  )
     .                 + da(i  ,j+1)*exp(i  ,j+1)
     .                 + da(i+1,j+1)*exp(i+1,j+1))

               intexp2 = intexp2 + quarter
     .                 *(da(i  ,j  )*exp(i  ,j  )**2
     .                 + da(i+1,j  )*exp(i+1,j  )**2
     .                 + da(i  ,j+1)*exp(i  ,j+1)**2
     .                 + da(i+1,j+1)*exp(i+1,j+1)**2)

               intexpdel2 = intexpdel2 + quarter
     .              *(da(i  ,j  )*(exp(i  ,j  )+trapped_surface_delta)**2
     .              + da(i+1,j  )*(exp(i+1,j  )+trapped_surface_delta)**2
     .              + da(i  ,j+1)*(exp(i  ,j+1)+trapped_surface_delta)**2
     .              + da(i+1,j+1)*(exp(i+1,j+1)+trapped_surface_delta)**2)

               inside_min_count = inside_min_count + 1

               if ((exp(i  ,j  ) .le. 0.0d0) .and.
     .             (exp(i+1,j  ) .le. 0.0d0) .and.
     .             (exp(i  ,j+1) .le. 0.0d0) .and.
     .             (exp(i+1,j+1) .le. 0.0d0)) then
                  inside_min_neg_count = inside_min_neg_count + 1
               endif

            end do
         end do

!        If necessary, find spectral components for flow.
!        For this I need to recalculate the Legendre polynomials,
!        but I see no way around it.  Also, to avoid having to
!        define lots of 2D arrays I do the integrals inside
!        the loop.  This means that I have to take care to see
!        if I am on an edge or corner point. Notice also that
!        these integrals do not involve the area element.

         if (flow) then

!           Find h0.

            if (find_trapped_surface) then
               h0 = - trapped_surface_delta
            else
               h0 = zero
            end if

!           Find integrals.

            do j=0,l_nphi
               do i=0,l_ntheta

!                 Find {theta,phi}.

                  theta = dtheta*dble(i+theta0)
                  phi   = dphi*dble(j+phi0)

!                 Find cost,sint.

                  cost = cos(theta)
                  sint = sin(theta)

!                 Find norm of gradient of horizon function.

                  grad = gradn(i,j)

!                 Find weight factor.

                  intw = dtp*(exp(i,j)-h0)*sint**2

                  if (((j.eq.0).or.(j.eq.l_nphi)).and.
     .               ((i.eq.0).or.(i.eq.l_ntheta))) then
                     intw = quarter*intw
                  else if (((j.eq.0).or.(j.eq.l_nphi)).or.
     .               ((i.eq.0).or.(i.eq.l_ntheta))) then
                     intw = half*intw
                  end if

!                 Find "sigma" for Nakamura flow (see Carstens paper).

                  if (nw.eq.0.0) then

                     sigma = zero

                  else

                     xw = xa(i,j) - xc
                     yw = ya(i,j) - yc
                     zw = za(i,j) - zc

!                    Calculate metric on surface.
             
                     det = txx(i,j)*tyy(i,j)*tzz(i,j) 
     .                    + two*txy(i,j)*txz(i,j)*tyz(i,j) 
     .                    - txx(i,j)*tyz(i,j)**2 - tyy(i,j)*txz(i,j)**2
     .                    - tzz(i,j)*txy(i,j)**2

                     idet = one/det

                     gupij(1,1) = idet*(tyy(i,j)*tzz(i,j)-tyz(i,j)**2)
                     gupij(2,2) = idet*(txx(i,j)*tzz(i,j)-txz(i,j)**2)
                     gupij(3,3) = idet*(txx(i,j)*tyy(i,j)-txy(i,j)**2)
                  
                     gupij(1,2) = idet*(txz(i,j)*tyz(i,j)-tzz(i,j)*txy(i,j))
                     gupij(2,1) = gupij(1,2)
                     gupij(1,3) = idet*(txy(i,j)*tyz(i,j)-tyy(i,j)*txz(i,j))
                     gupij(3,1) = gupij(1,3)
                     gupij(2,3) = idet*(txy(i,j)*txz(i,j)-txx(i,j)*tyz(i,j))
                     gupij(3,2) = gupij(2,3)

                     call AHFinder_calcsigma(CCTK_FARGUMENTS,
     .               xw,yw,zw,gupij,sigma)

                  end if

!                 Monopole integrals.

                  aux = intw*grad

                  hflow0(0) = hflow0(0) + intw
                  cflow0(0) = cflow0(0) + aux
                  nflow0(0) = nflow0(0) + aux*sigma

!                 Axisymmetric integrals.

                  do l=1+stepz,lmax,1+stepz

                     aux  = intw*LEGEN(l,0,cost)
                     aux1 = aux*grad

                     hflow0(l) = hflow0(l) + aux
                     cflow0(l) = cflow0(l) + aux1
                     nflow0(l) = nflow0(l) + aux1*sigma

                  end do

!                 Non-axisymmetric integrals.

                  if (nonaxi) then

                     do m=1,lmax

                        aux = dble(m)*phi
                        sina = sin(aux)
                        cosa = cos(aux)

                        do l=m,lmax

                           aux  = intw*LEGEN(l,m,cost)

                           aux1 = aux*cosa
                           aux2 = aux1*grad
                           hflowc(l,m) = hflowc(l,m) + aux1
                           cflowc(l,m) = cflowc(l,m) + aux2
                           nflowc(l,m) = nflowc(l,m) + aux2*sigma

                           if (.not.refy) then
                              aux1 = aux*sina
                              aux2 = aux1*grad
                              hflows(l,m) = hflows(l,m) + aux1
                              cflows(l,m) = cflows(l,m) + aux2
                              nflows(l,m) = nflows(l,m) + aux2*sigma
                           end if

                        end do
                     end do

                  end if

               end do
            end do

         end if

      end if

!     Reduce the integrals across processors.

!     Area and expansion.

      call CCTK_ReduceLocalScalar(ierror,cctkGH,-1,reduction_handle,
     .     intarea,aux,CCTK_VARIABLE_REAL) 
      intarea=aux

      call CCTK_ReduceLocalScalar(ierror,cctkGH,-1,reduction_handle,
     .     intexp,aux,CCTK_VARIABLE_REAL) 
      intexp=aux

      call CCTK_ReduceLocalScalar(ierror,cctkGH,-1,reduction_handle,
     .     intexp2,aux,CCTK_VARIABLE_REAL) 
      intexp2=aux

      call CCTK_ReduceLocalScalar(ierror,cctkGH,-1,reduction_handle,
     .     intexpdel2,aux,CCTK_VARIABLE_REAL) 
      intexpdel2=aux

!     negative expansion elements on surface

      call CCTK_ReduceLocalScalar(ierror,cctkGH,-1,reduction_handle,
     .     inside_min_neg_count,auxi,CCTK_VARIABLE_INT) 
      inside_min_neg_count=auxi

      call CCTK_ReduceLocalScalar(ierror,cctkGH,-1,reduction_handle,
     .     inside_min_count,auxi,CCTK_VARIABLE_INT) 
      inside_min_count=auxi

!     Spectral components for flow.

      if (flow) then

!        Axisymmetric.

         auxi = lmax+1

	 if (hw.ne.zero) then
            call CCTK_ReduceLocalArray1D(ierror,cctkGH,-1,reduction_handle,
     .           hflow0,tempv,auxi,CCTK_VARIABLE_REAL) 
            hflow0 = tempv
	 end if

         if (cw.ne.zero) then      
            call CCTK_ReduceLocalArray1D(ierror,cctkGH,-1,reduction_handle,
     .          cflow0,tempv,auxi,CCTK_VARIABLE_REAL) 
            cflow0 = tempv
         end if

         if (nw.ne.zero) then
            call CCTK_ReduceLocalArray1D(ierror,cctkGH,-1,reduction_handle,
     .           nflow0,tempv,auxi,CCTK_VARIABLE_REAL) 
             nflow0 = tempv
         end if

!        Non-axisymmetric.

         if (nonaxi) then

            auxi = lmax*lmax

	    if (nw.ne.zero) then
               call CCTK_ReduceLocalArray1D(ierror,cctkGH,-1,reduction_handle,
     .              hflowc,tempm,auxi,CCTK_VARIABLE_REAL) 
               hflowc = tempm
	    end if

            if (cw.ne.zero) then
               call CCTK_ReduceLocalArray1D(ierror,cctkGH,-1,reduction_handle,
     .              cflowc,tempm,auxi,CCTK_VARIABLE_REAL) 
               cflowc = tempm
            end if

            if (nw.ne.zero) then
               call CCTK_ReduceLocalArray1D(ierror,cctkGH,-1,reduction_handle,
     .              nflowc,tempm,auxi,CCTK_VARIABLE_REAL) 
               nflowc = tempm
            end if

            if (.not.refy) then

	       if (hw.ne.zero) then
                  call CCTK_ReduceLocalArray1D(ierror,cctkGH,-1,reduction_handle,
     .                 hflows,tempm,auxi,CCTK_VARIABLE_REAL) 
                  hflows = tempm
	       end if

               if (cw.ne.zero) then
                  call CCTK_ReduceLocalArray1D(ierror,cctkGH,-1,reduction_handle,
     .                 cflows,tempm,auxi,CCTK_VARIABLE_REAL) 
                  cflows = tempm
               end if

               if (nw.ne.zero) then
                  call CCTK_ReduceLocalArray1D(ierror,cctkGH,-1,reduction_handle,
     .                 nflows,tempm,auxi,CCTK_VARIABLE_REAL) 
                  nflows = tempm
               end if

            end if

         end if

      end if

!     For cartoon multiply the integrals with 2*pi.

      if (cartoon) then

         intw = 6.283185307D0

         intexp  = intw*intexp
         intexp2 = intw*intexp2
         intarea = intw*intarea
         intexpdel2 = intw*intexpdel2

         if (flow) then
            hflow0 = intw*hflow0
            cflow0 = intw*cflow0
            nflow0 = intw*nflow0
         end if

!     Rescale the integrals according to symmetries.

      else if (refx.or.refy.or.refz) then

         intw = one

         if (refx) intw = two*intw
         if (refy) intw = two*intw
         if (refz) intw = two*intw

         intexp  = intw*intexp
         intexp2 = intw*intexp2
         intarea = intw*intarea

!        Spectral components for flow.

         if (flow) then

!           Axisymmetric.
	
            hflow0 = intw*hflow0
            cflow0 = intw*cflow0
            nflow0 = intw*nflow0

!           Non-axisymmetric.

            if (nonaxi) then

               hflowc = intw*hflowc
               cflowc = intw*cflowc
               nflowc = intw*nflowc

               if (.not.refy) then
                  hflows = intw*hflows
                  cflows = intw*cflows
                  nflows = intw*nflows
               end if

            end if

         end if

      end if


!     *****************************
!     ***   DEALLOCATE MEMORY   ***
!     *****************************

 10   continue

      deallocate(rr,xa,ya,za)
      deallocate(da,exp,gradn)
      deallocate(txx,tyy,tzz,txy,txz,tyz)
      deallocate(intmask)


!     ***************
!     ***   END   ***
!     ***************

      end subroutine AHFinder_int