aboutsummaryrefslogtreecommitdiff
path: root/src/EHFinder_Integrate2.F90
blob: 6433533b6ab561b455df7bb56acaefcd18a5fa0c (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
! Integrating over the surface(s). Right now only in full mode.
! $Header$

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

subroutine EHFinder_FindSurfaceElement(CCTK_ARGUMENTS)

  use EHFinder_mod

  implicit none

  DECLARE_CCTK_PARAMETERS
  DECLARE_CCTK_ARGUMENTS
  DECLARE_CCTK_FUNCTIONS

  CCTK_INT :: i, j, k, im, jm
  CCTK_INT :: interp_handle, table_handle, coord_system_handle

  character(len=200) :: area_interp
  CCTK_INT :: area_interp_len
  character(len=7) :: area_order

  CCTK_INT, dimension(4) :: bbox
  CCTK_INT, dimension(2) :: gsh, lsh, lbnd, ubnd, nghost

  CCTK_REAL :: dtheta, dphi, dthetainv, dphiinv
  CCTK_REAL :: dxdth, dxdph, dydth, dydph, dzdth, dzdph
!  CCTK_REAL :: gtt, gtp, gpp
  CCTK_POINTER, dimension(3) :: interp_coords
  CCTK_POINTER, dimension(7) :: out_array
  CCTK_INT, dimension(7) :: in_array
  CCTK_INT, dimension(7), parameter :: out_types = (/ CCTK_VARIABLE_REAL, &
                                                      CCTK_VARIABLE_REAL, &
                                                      CCTK_VARIABLE_REAL, &
                                                      CCTK_VARIABLE_REAL, &
                                                      CCTK_VARIABLE_REAL, &
                                                      CCTK_VARIABLE_REAL, &
                                                      CCTK_VARIABLE_REAL /)
  
! If finding of surface failed do not try to integrate but exit.
  if ( find_surface_status .lt. 0 ) then
    return
  endif

  call CCTK_INFO ( 'Finding surface element' )
  call CCTK_GroupbboxGN ( ierr, cctkGH, 4, bbox, "ehfinder::surface_arrays" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get bounding box for surface arrays" )
  end if
!  print*,bbox
  call CCTK_GroupgshGN ( ierr, cctkGH, 2, gsh, "ehfinder::surface_arrays" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get global size for surface arrays" )
  end if
!  print*,gsh
  call CCTK_GrouplbndGN ( ierr, cctkGH, 2, lbnd, "ehfinder::surface_arrays" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get lower bounds for surface arrays" )
  end if
!  print*,lbnd
  call CCTK_GroupubndGN ( ierr, cctkGH, 2, ubnd, "ehfinder::surface_arrays" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get upper bounds for surface arrays" )
  end if
!  print*,ubnd
  call CCTK_GrouplshGN ( ierr, cctkGH, 2, lsh, "ehfinder::surface_arrays" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get local size for surface arrays" )
  end if
!  print*,lsh
  call CCTK_GroupnghostzonesGN ( ierr, cctkGH, 2, nghost, "ehfinder::surface_arrays" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get local size for surface arrays" )
  end if

  dtheta = ctheta(2,1) - ctheta(1,1)
  dphi = cphi(1,2) - cphi(1,1)
  dthetainv = one / dtheta
  dphiinv = one / dphi

  call CCTK_FortranString ( area_interp_len, area_interpolator, &
                                             area_interp )

  call CCTK_InterpHandle ( interp_handle, area_interp(1:area_interp_len) )

  if ( interp_handle .lt. 0 ) then
    call CCTK_WARN( 0, "Cannot get handle for interpolation. Forgot to activate an implementation providing interpolation operators??" )
  end if

  write(area_order,'(a6,i1)') 'order=',area_interpolation_order

  call Util_TableCreateFromString ( table_handle, area_order )
  if ( table_handle .lt. 0 ) then
    call CCTK_WARN( 0, "Cannot create parameter table for interpolator" )
  end if

  call CCTK_CoordSystemHandle ( coord_system_handle, "cart3d" )
  if ( coord_system_handle .lt. 0) then
    call CCTK_WARN( 0, "Cannot get handle for cart3d coordinate system. Forgot to activate an implementation providing coordinates ??" )
  endif

  interp_coords(1) = CCTK_PointerTo(interp_x)
  interp_coords(2) = CCTK_PointerTo(interp_y)
  interp_coords(3) = CCTK_PointerTo(interp_z)

  out_array(1) = CCTK_PointerTo(gxxi)
  out_array(2) = CCTK_PointerTo(gxyi)
  out_array(3) = CCTK_PointerTo(gxzi)
  out_array(4) = CCTK_PointerTo(gyyi)
  out_array(5) = CCTK_PointerTo(gyzi)
  out_array(6) = CCTK_PointerTo(gzzi)
  out_array(7) = CCTK_PointerTo(psii)

! find the cartesian coordinates for the interpolation points

!  print*,center
  do j = 1, lsh(2)
    do i = 1, lsh(1)
      interp_x(i,j) = center(1) + rsurf(i,j) * sintheta(i,j) * cosphi(i,j)
      interp_y(i,j) = center(2) + rsurf(i,j) * sintheta(i,j) * sinphi(i,j)
      interp_z(i,j) = center(3) + rsurf(i,j) * costheta(i,j)
    end do
  end do

  call CCTK_VarIndex ( in_array(1), "admbase::gxx" )
  call CCTK_VarIndex ( in_array(2), "admbase::gxy" )
  call CCTK_VarIndex ( in_array(3), "admbase::gxz" )
  call CCTK_VarIndex ( in_array(4), "admbase::gyy" )
  call CCTK_VarIndex ( in_array(5), "admbase::gyz" )
  call CCTK_VarIndex ( in_array(6), "admbase::gzz" )

  if ( CCTK_EQUALS ( metric_type, "static conformal" ) ) then
    call CCTK_VarIndex ( in_array(7), "staticconformal::psi" )

    call CCTK_InterpGridArrays ( ierr, cctkGH, 3, interp_handle, &
                                 table_handle, coord_system_handle, &
                                 lsh(1) * lsh(2), CCTK_VARIABLE_REAL, &
                                 interp_coords, 7, in_array, &
                                 7, out_types, out_array )
    gxxi = psii**4 * gxxi
    gxyi = psii**4 * gxyi
    gxzi = psii**4 * gxzi
    gyyi = psii**4 * gyyi
    gyzi = psii**4 * gyzi
    gzzi = psii**4 * gzzi
  else
    call CCTK_InterpGridArrays ( ierr, cctkGH, 3, interp_handle, &
                                 table_handle, coord_system_handle, &
                                 lsh(1) * lsh(2), CCTK_VARIABLE_REAL, &
                                 interp_coords, 6, in_array(1:6), &
                                 6, out_types(1:6), out_array(1:6) )
  end if

  do j = 1, lsh(2)
    do i = 1, lsh(1)
!      print*,i,j,ctheta(i,j),cphi(i,j)
      dxdth = ( drdtheta(i,j) * sintheta(i,j) + &
                rsurf(i,j) * costheta(i,j) ) * cosphi(i,j)
!      print*,dxdth,rsurf(i,j)*costheta(i,j)*cosphi(i,j)
      dydth = ( drdtheta(i,j) * sintheta(i,j) + &
                rsurf(i,j) * costheta(i,j) ) * sinphi(i,j)
!      print*,dydth,rsurf(i,j)*costheta(i,j)*sinphi(i,j)
      dzdth = drdtheta(i,j) * costheta(i,j) - rsurf(i,j) * sintheta(i,j)
!      print*,dzdth,-rsurf(i,j)*sintheta(i,j)
      dxdph = ( drdphi(i,j) * cosphi(i,j) - &
                rsurf(i,j) * sinphi(i,j) ) * sintheta(i,j)
!      print*,dxdph,-rsurf(i,j)*sintheta(i,j)*sinphi(i,j)
      dydph = ( drdphi(i,j) * sinphi(i,j) + &
                rsurf(i,j) * cosphi(i,j) ) * sintheta(i,j)
!      print*,dydph,rsurf(i,j)*sintheta(i,j)*cosphi(i,j)
      dzdph = drdphi(i,j) * costheta(i,j)
!      print*,dzdph,0
!      print*

      gtt(i,j) = dxdth**2 * gxxi(i,j) + dydth**2 * gyyi(i,j) + &
            dzdth**2 * gzzi(i,j) + &
            two * ( dxdth * dydth * gxyi(i,j) + &
                    dxdth * dzdth * gxzi(i,j) + &
                    dydth * dzdth * gyzi(i,j) )
      gtp(i,j) = dxdth * dxdph * gxxi(i,j) + &
            ( dxdth * dydph + dydth * dxdph ) * gxyi(i,j) + &
            ( dxdth * dzdph + dzdth * dxdph ) * gxzi(i,j) + &
            dydth * dydph * gyyi(i,j) + &
            ( dydth * dzdph + dzdth * dydph ) * gyzi(i,j) + &
            dzdth * dzdph * gzzi(i,j)
      gpp(i,j) = dxdph**2 * gxxi(i,j) + dydph**2 * gyyi(i,j) + &
            dzdph**2 * gzzi(i,j) + &
            two * ( dxdph * dydph * gxyi(i,j) + &
                    dxdph * dzdph * gxzi(i,j) + &
                    dydph * dzdph * gyzi(i,j) )
      da(i,j) = dtheta * dphi * sqrt ( gtt(i,j) * gpp(i,j) - gtp(i,j)**2 )
      dltheta(i,j) = dtheta * sqrt ( gtt(i,j) )
      dlphi(i,j) = dphi * sqrt ( gpp(i,j) )
    end do
  end do
!  print*,da
!  do j = 1, lsh(2)
!    print*,'phi = ',cphi(1,j)
!    print*,da(:,j)
!    print*,gtt(:,j)
!    print*,gtp(:,j)
!    print*,gpp(:,j)
!  end do
 
end subroutine EHFinder_FindSurfaceElement


subroutine EHFinder_IntegrateArea(CCTK_ARGUMENTS)

  use EHFinder_mod

  implicit none

  DECLARE_CCTK_PARAMETERS
  DECLARE_CCTK_ARGUMENTS
  DECLARE_CCTK_FUNCTIONS

  CCTK_INT :: int_var, sn
  CCTK_REAL :: eh_area_tmp
  character(len=30) :: info_message
  CCTK_INT, dimension(1) :: lbnd, ubnd, lsh

! If finding of surface failed do not try to integrate but exit.
  if ( find_surface_status .lt. 0 ) then
    return
  endif

  call CCTK_INFO ( 'Integrating area' )

  sn = surface_counter - integrate_counter
  int_tmp = sym_factor * weights * da

  call CCTK_VarIndex ( int_var, "ehfinder::int_tmp" )
  if ( int_var .lt. 0 ) then
    call CCTK_WARN ( 0, 'Could not get index to grid array int_tmp' )
  end if
!  call CCTK_Reduce ( ierr, cctkGH, -1, sum_handle, 1, CCTK_VARIABLE_REAL, &
!                     eh_area2(sn), 1, int_var )
!  write(info_message,'(a15,f10.5)') 'Horizon area = ',eh_area2(sn)

  call CCTK_Reduce ( ierr, cctkGH, -1, sum_handle, 1, CCTK_VARIABLE_REAL, &
                     eh_area_tmp, 1, int_var )
  write(info_message,'(a15,f10.5)') 'Horizon area = ',eh_area_tmp

  call CCTK_INFO(info_message)

  call CCTK_GrouplbndGN ( ierr, cctkGH, 1, lbnd, "ehfinder::eh_area2" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get lower bounds for area array" )
  end if
  call CCTK_GroupubndGN ( ierr, cctkGH, 1, ubnd, "ehfinder::eh_area2" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get upper bounds for area array" )
  end if
  call CCTK_GrouplshGN ( ierr, cctkGH, 1, lsh, "ehfinder::eh_area2" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get local size for area array" )
  end if

!  if ( ( sn .ge. lbnd(1) + lsh(1) ) .and. ( sn .le. ubnd(1) + lsh(1) ) ) then
  if ( ( sn .ge. lbnd(1) + 1 ) .and. ( sn .le. ubnd(1) + 1 ) ) then
    eh_area2(sn-lbnd(1)) = eh_area_tmp
  end if
  print*,'Debug information : ',sn,lbnd,ubnd,lsh
!  print*,eh_area2

end subroutine EHFinder_IntegrateArea


subroutine EHFinder_IntegrateCentroid(CCTK_ARGUMENTS)

  use EHFinder_mod

  implicit none

  DECLARE_CCTK_PARAMETERS
  DECLARE_CCTK_ARGUMENTS
  DECLARE_CCTK_FUNCTIONS

  CCTK_INT :: int_var, sn, k
  character(len=50) :: info_message
  CCTK_INT, dimension(1) :: lbnd, ubnd, lsh
  CCTK_REAL :: centroid_x, centroid_y, centroid_z

! If finding of surface failed do not try to integrate but exit.
  if ( find_surface_status .lt. 0 ) then
    return
  endif

  call CCTK_INFO ( 'Integrating centroid' )

  sn = surface_counter - integrate_counter

  interp_x = center(1) + rsurf * sintheta * cosphi
  interp_y = center(2) + rsurf * sintheta * sinphi
  interp_z = center(3) + rsurf * costheta

  call CCTK_VarIndex ( int_var, "ehfinder::int_tmp" )
  if ( int_var .lt. 0 ) then
    call CCTK_WARN ( 0, 'Could not get index to grid array int_tmp' )
  end if

!  int_tmp = sym_factor * interp_x * weights * da
!  call CCTK_Reduce ( ierr, cctkGH, -1, sum_handle, 1, CCTK_VARIABLE_REAL, &
!                     eh_centroid2_x(sn), 1, int_var )
!
!  int_tmp = sym_factor * interp_y * weights * da
!  call CCTK_Reduce ( ierr, cctkGH, -1, sum_handle, 1, CCTK_VARIABLE_REAL, &
!                     eh_centroid2_y(sn), 1, int_var )
!
!  int_tmp = sym_factor * interp_z * weights * da
!  call CCTK_Reduce ( ierr, cctkGH, -1, sum_handle, 1, CCTK_VARIABLE_REAL, &
!                     eh_centroid2_z(sn), 1, int_var )

!  if ( s_symx ) eh_centroid2_x(sn) = zero
!  if ( s_symy ) eh_centroid2_y(sn) = zero
!  if ( s_symz ) eh_centroid2_z(sn) = zero

  int_tmp = sym_factor * interp_x * weights * da
  call CCTK_Reduce ( ierr, cctkGH, -1, sum_handle, 1, CCTK_VARIABLE_REAL, &
                     centroid_x, 1, int_var )

  int_tmp = sym_factor * interp_y * weights * da
  call CCTK_Reduce ( ierr, cctkGH, -1, sum_handle, 1, CCTK_VARIABLE_REAL, &
                     centroid_y, 1, int_var )

  int_tmp = sym_factor * interp_z * weights * da
  call CCTK_Reduce ( ierr, cctkGH, -1, sum_handle, 1, CCTK_VARIABLE_REAL, &
                     centroid_z, 1, int_var )

  if ( s_symx ) centroid_x = zero
  if ( s_symy ) centroid_y = zero
  if ( s_symz ) centroid_z = zero

  call CCTK_GrouplbndGN ( ierr, cctkGH, 1, lbnd, "ehfinder::eh_centroid2" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get lower bounds for centroid array" )
  end if
  call CCTK_GroupubndGN ( ierr, cctkGH, 1, ubnd, "ehfinder::eh_centroid2" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get upper bounds for centroid array" )
  end if
  call CCTK_GrouplshGN ( ierr, cctkGH, 1, lsh, "ehfinder::eh_centroid2" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get local size for centroid array" )
  end if

!    eh_centroid2_x(sn) = eh_centroid2_x(sn) / eh_area2(sn)
!    eh_centroid2_y(sn) = eh_centroid2_y(sn) / eh_area2(sn)
!    eh_centroid2_z(sn) = eh_centroid2_z(sn) / eh_area2(sn)

  if ( ( sn .ge. lbnd(1) + 1 ) .and. ( sn .le. ubnd(1) + 1 ) ) then
    k = sn - lbnd(1)
    eh_centroid2_x(k) = centroid_x / eh_area2(k)
    eh_centroid2_y(k) = centroid_y / eh_area2(k)
    eh_centroid2_z(k) = centroid_z / eh_area2(k)
!    print*,centroid_x,centroid_y,centroid_z
!    print*,eh_area2(k)
  end if

!  write(info_message,'(a19,3(f10.5))') 'Horizon centroid = ', &
!                                       eh_centroid2_x(sn), &
!                                       eh_centroid2_y(sn), &
!                                       eh_centroid2_z(sn)

  write(info_message,'(a19,3(f10.5))') 'Horizon centroid = ', &
                                       eh_centroid2_x(sn), &
                                       eh_centroid2_y(sn), &
                                       eh_centroid2_z(sn)
  call CCTK_INFO(info_message)

end subroutine EHFinder_IntegrateCentroid


subroutine EHFinder_IntegrateEquatorial(CCTK_ARGUMENTS)

  use EHFinder_mod

  implicit none

  DECLARE_CCTK_PARAMETERS
  DECLARE_CCTK_ARGUMENTS
  DECLARE_CCTK_FUNCTIONS

  CCTK_INT :: int_var, sn, k
  character(len=50) :: info_message
  CCTK_INT, dimension(1) :: lbnd1, ubnd1, lsh1
  CCTK_INT, dimension(2) :: lbnd, lsh, nghost
  CCTK_INT, dimension(4) :: bbox
  CCTK_INT :: ithl, ithr, jphl, jphr, itha, jpha
  CCTK_REAL :: eq_circ_loc, eq_circ, pol_circ_loc, pol_circ

! If finding of surface failed do not try to integrate but exit.
  if ( find_surface_status .lt. 0 ) then
    return
  endif

  sn = surface_counter - integrate_counter

  ! Find out the lower bounds of the distributed integration grid arrays.
  call CCTK_GrouplbndGN ( ierr, cctkGH, 2, lbnd, "ehfinder::surface_arrays" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get lower bounds for surface arrays" )
  end if

  ! Find out the local size of the distributed integration grid arrays
  call CCTK_GrouplshGN ( ierr, cctkGH, 2, lsh, "ehfinder::surface_arrays" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get local size for surface arrays" )
  end if

  ! Find out the bounding box of the distributed integration grid arrays
  call CCTK_GroupbboxGN ( ierr, cctkGH, 4, bbox, "ehfinder::surface_arrays" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get bounding box for surface arrays" )
  end if

  ! Find out the ghost size of the distributed integration grid arrays
  call CCTK_GroupnghostzonesGN ( ierr, cctkGH, 2, nghost, "ehfinder::surface_arrays" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get ghost zones for surface arrays" )
  end if
 
!  print*,lbnd
!  print*,lsh
!  print*,bbox
!  print*,nghost

  ithl = 1; ithr = lsh(1)
  jphl = 1; jphr = lsh(2)
  
  if ( bbox(1) .eq. 0 ) ithl = ithl + nghost(1)
  if ( bbox(2) .eq. 0 ) ithr = ithr - nghost(1)
  if ( bbox(3) .eq. 0 ) jphl = jphl + nghost(2)
  if ( bbox(4) .eq. 0 ) jphr = jphr - nghost(2)

!  print*,ithl,ithr
!  print*,jphl,jphr

  call CCTK_INFO ( 'Integrating equatorial circumference' )

  int_tmp = phi_sym_factor * phiweights * dlphi

  if ( ( ithl+lbnd(1) .le. githeta ) .and. ( githeta .le. ithr+lbnd(1) ) ) then
    itha = githeta - lbnd(1)
    eq_circ_loc = sum ( int_tmp(itha,jphl:jphr) )
  else
    eq_circ_loc = zero
  end if

!  print*,itha,githeta,eq_circ_loc
!  print*
!  print*,int_tmp(itha,jphl:jphr)
!  print*
!  print*,phi_sym_factor
!  print*
!  print*,phiweights(itha,jphl:jphr)
!  print*
!  print*,dlphi(itha,jphl:jphr)

  call CCTK_ReduceLocScalar ( ierr, cctkGH, -1, sum_handle, &
                              eq_circ_loc, eq_circ, CCTK_VARIABLE_REAL )

  call CCTK_GrouplbndGN ( ierr, cctkGH, 1, lbnd1, "ehfinder::eh_circumference2" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get lower bounds for area array" )
  end if
  call CCTK_GroupubndGN ( ierr, cctkGH, 1, ubnd1, "ehfinder::eh_circumference2" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get upper bounds for area array" )
  end if
  call CCTK_GrouplshGN ( ierr, cctkGH, 1, lsh1, "ehfinder::eh_circumference2" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get local size for area array" )
  end if

  if ( ( sn .ge. lbnd1(1) + 1 ) .and. ( sn .le. ubnd1(1) + 1 ) ) then
    k = sn - lbnd1(1)
    eh_circ_eq2(k) = eq_circ
  end if
!  eh_circ_eq2(sn) = eq_circ

  write(info_message,'(a35,f10.5)') 'Horizon equatorial circumference = ',eq_circ
  call CCTK_INFO(info_message)

  call CCTK_INFO ( 'Integrating polar circumference' )

  int_tmp = theta_sym_factor * thetaweights * dltheta

!  print*,jphl+lbnd(2),jphr+lbnd(2),gjphi
  if ( ( jphl+lbnd(2) .le. gjphi ) .and. ( gjphi .le. jphr+lbnd(2) ) ) then
    jpha = gjphi - lbnd(2)
    pol_circ_loc = sum ( int_tmp(ithl:ithr,jpha) )
  else
    pol_circ_loc = zero
  end if

!  print*,jpha,gjphi,pol_circ_loc
!  print*
!  print*,int_tmp(ithl:ithr,jpha)
!  print*
!  print*,theta_sym_factor
!  print*,thetaweights(ithl:ithr,jpha)
!  print*
!  print*,dltheta(ithl:ithr,jpha)
  call CCTK_ReduceLocScalar ( ierr, cctkGH, -1, sum_handle, &
                              pol_circ_loc, pol_circ, CCTK_VARIABLE_REAL )


  if ( ( sn .ge. lbnd1(1) + 1 ) .and. ( sn .le. ubnd1(1) + 1 ) ) then
    k = sn - lbnd1(1)
    eh_circ_pol2(k) = pol_circ
  end if
!  eh_circ_pol2(sn) = pol_circ

  write(info_message,'(a30,f10.5)') 'Horizon polar circumference = ',pol_circ
  call CCTK_INFO(info_message)
  
end subroutine EHFinder_IntegrateEquatorial


! This routine sets up the weights for the Simpsons rule integration
! over the surface.
subroutine EHFinder_InitWeights(CCTK_ARGUMENTS)

  use EHFinder_mod

  implicit none

  DECLARE_CCTK_PARAMETERS
  DECLARE_CCTK_ARGUMENTS
  DECLARE_CCTK_FUNCTIONS

  CCTK_INT :: i, j, k, im, jm

  CCTK_INT, dimension(4) :: bbox
  CCTK_INT, dimension(2) :: gsh, lsh, lbnd, ubnd, nghost

  ! Find out the lower bounds of the distributed integration grid arrays.
  call CCTK_GrouplbndGN ( ierr, cctkGH, 2, lbnd, "ehfinder::surface_arrays" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get lower bounds for surface arrays" )
  end if

  ! Find out the local size of the distributed integration grid arrays
  call CCTK_GrouplshGN ( ierr, cctkGH, 2, lsh, "ehfinder::surface_arrays" )
  if ( ierr .lt. 0 ) then
    call CCTK_WARN ( 0, "cannot get local size for surface arrays" )
  end if

  weights = one

  ! Initialise the weight grid array for the 2D Simpsons rule integration.
  ! To do this I need to figure out the global location of the given point.
  ! There are 3 cases in the one dimensional case. If the point is on the
  !  boundary the weight is 1/3. If it is at an even position the weight
  ! is 4/3 and if it is at an odd position the weight is 2/3. 

  do j = 1, lsh(2)

    ! This is first done in the phi direction. Meaning that all points with
    ! the same theta coordinate are set to the same weight.
    if ( ( lbnd(2)+j .eq. 1 ) .or. ( lbnd(2)+j .eq. nphi ) ) then
      weights(:,j) = onethird
      phiweights(:,j) = onethird
    else if ( mod(lbnd(2)+j,2) .eq. 0 ) then
      weights(:,j) = fourthirds
      phiweights(:,j) = fourthirds
    else
      weights(:,j) = twothirds
      phiweights(:,j) = twothirds
    end if

    ! Then it is done in the theta direction with the one-dimensional
    ! weights beeing multiplied.
    do i = 1, lsh(1)
      if ( ( lbnd(1)+i .eq. 1 ) .or. ( lbnd(1)+i .eq. ntheta ) ) then
        weights(i,j) = onethird * weights(i,j)
        thetaweights(i,j) = onethird
      else if ( mod(lbnd(1)+i,2) .eq. 0 ) then
        weights(i,j) = fourthirds * weights(i,j)
        thetaweights(i,j) = fourthirds
      else
        weights(i,j) = twothirds * weights(i,j)
        thetaweights(i,j) = twothirds
      end if
    end do
  end do

  ! The end result is a 2D array with the weights in the following pattern.

  !  1/9   4/9  2/9   4/9  2/9   4/9  1/9
  !  4/9  16/9  8/9  16/9  8/9  16/9  4/9
  !  2/9   8/9  4/9   8/9  4/9   8/9  2/9
  !  4/9  16/9  8/9  16/9  8/9  16/9  4/9
  !  2/9   8/9  4/9   8/9  4/9   8/9  2/9
  !  4/9  16/9  8/9  16/9  8/9  16/9  4/9
  !  1/9   4/9  2/9   4/9  2/9   4/9  1/9
end subroutine EHFinder_InitWeights


subroutine EHFinder_CopyArea(CCTK_ARGUMENTS)

  use EHFinder_mod

  implicit none

  DECLARE_CCTK_PARAMETERS
  DECLARE_CCTK_ARGUMENTS
  DECLARE_CCTK_FUNCTIONS

!  print*,Xeh_area0
!  print*,Xeh_area20

! If finding of surface failed set area to zero.
  print*,'Debug2 : ',find_surface_status
  if ( find_surface_status .lt. 0 ) then
    eh_area = zero
    return
  else
    call CCTK_INFO('Copying area data')
    eh_area = eh_area2
  endif
end subroutine EHFinder_CopyArea


subroutine EHFinder_CopyCentroid(CCTK_ARGUMENTS)

  use EHFinder_mod

  implicit none

  DECLARE_CCTK_PARAMETERS
  DECLARE_CCTK_ARGUMENTS
  DECLARE_CCTK_FUNCTIONS

!  print*,Xeh_centroid0
!  print*,Xeh_centroid20

! If finding of surface failed set centroids to zero.
  if ( find_surface_status .lt. 0 ) then
    eh_centroid_x = zero
    eh_centroid_y = zero
    eh_centroid_z = zero
    return
  else
    call CCTK_INFO('Copying centroid data')
    eh_centroid_x = eh_centroid2_x
    eh_centroid_y = eh_centroid2_y
    eh_centroid_z = eh_centroid2_z
  end if

end subroutine EHFinder_CopyCentroid


subroutine EHFinder_CopyCircumference(CCTK_ARGUMENTS)

  use EHFinder_mod

  implicit none

  DECLARE_CCTK_PARAMETERS
  DECLARE_CCTK_ARGUMENTS
  DECLARE_CCTK_FUNCTIONS

  if ( find_surface_status .lt. 0 ) then
    eh_circ_eq = zero
    eh_circ_pol = zero
    return
  else
    call CCTK_INFO('Copying circumference data')
    eh_circ_eq = eh_circ_eq2
    eh_circ_pol = eh_circ_pol2
  end if

end subroutine EHFinder_CopyCircumference