aboutsummaryrefslogtreecommitdiff
path: root/src/Boundary.c
blob: af9def78797ffc1228397b2ee3dceaa4be3502ba (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
 /*@@
   @file      Boundary.c
   @date      Sat Oct 26 22:39:40 CEST 2002
   @author    David Rideout
   @desc 
              Implements the new boundary specification.
   @enddesc 
   @version   $Header$
 @@*/

#include <string.h>
#include <stdlib.h>

#include "cctk.h"
#include "cctk_Faces.h"
#include "cctk_Arguments.h"
#include "util_Table.h"
#include "util_String.h"
#include "Boundary.h"

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

CCTK_FILEVERSION(CactusBase_Boundary_Boundary_c);

/********************************************************************
 *********************     Local Data Types   ***********************
 ********************************************************************/
/* #define DEBUG 1 */

/* pointer to function which implements a physical boundary condition: */
typedef int (*phys_bc_fn_ptr)(const cGH *, int, int *, int *, int *);

/* Linked list for holding variables selected for a bc: 
 * Entries are sorted in the order they appear in struct BCVAR,
 * i.e. the var_index varies most rapidly, and bc_name least rapidly.
 */
struct BCVAR
{
  struct BCVAR *next_entry;
  char *bc_name;
  /*  CCTK_FPOINTER bc_fn_pointer; // store fn pointers, since they are
      easier to sort? */
  int faces;
  int table;
  int var_index;
};

/*
 * Linked list of pointers to the
 * first entry marked for bc 'A', the first marked for bc 'B', etc.
 * This will speed both adding entries and retreiving them.  One could
 * also provide a pointer to the first entry with bc 'B', table handle
 * 12.  For now, since usually all bcs are marked for the same bc, I
 * won't bother to implement this.  
 *
 * Here is also recorded how many of each bc type have
 * been selected so far, so that the GetSelectedBCs doesn't have to be
 * run through twice; once simply to get the number of selected vars!
 *
 * Could also save a string comparison at each iteration of
 * Boundary_Boundary_SelectedGVs() if we also store and manage bcdata->stop.
 */
struct BCDATA
{
  struct BCDATA *next;
  const char *bc_name;  /* name of bc */
  struct BCVAR *start;  /* start of this bc in BCVAR list */
  int num;              /* number of entries for this bc in BCVAR list */
};

/********************************************************************
 ********************* Local Routine Prototypes *********************
 ********************************************************************/

static int entry_less_than(struct BCVAR *new, struct BCVAR *current);

/********************************************************************
 ***************** Aliased Routine Prototypes ***********************
 ********************************************************************/

CCTK_INT Bdry_Boundary_RegisterPhysicalBC(CCTK_POINTER GH, 
					  CCTK_FPOINTER fn_pointer, 
					  CCTK_STRING bc_name);
CCTK_INT Bdry_Boundary_SelectVarForBC(CCTK_POINTER GH,
				      CCTK_INT faces,
				      CCTK_INT table_handle, 
				      CCTK_STRING var_name, 
				      CCTK_STRING bc_name);
CCTK_INT Bdry_Boundary_SelectVarForBCI(CCTK_POINTER GH, 
				       CCTK_INT faces,
				       CCTK_INT table_handle, 
				       CCTK_INT var_index, 
				       CCTK_STRING bc_name);
CCTK_INT Bdry_Boundary_SelectGroupForBC(CCTK_POINTER GH,
					CCTK_INT faces,
					CCTK_INT table_handle, 
					CCTK_STRING group_name, 
					CCTK_STRING bc_name);
CCTK_INT Bdry_Boundary_SelectGroupForBCI(CCTK_POINTER GH, 
					 CCTK_INT faces,
					 CCTK_INT table_handle, 
					 CCTK_INT group_index, 
					 CCTK_STRING bc_name);
CCTK_INT Bdry_Boundary_SelectedGVs(CCTK_POINTER GH, CCTK_INT array_size,
				   CCTK_POINTER var_indices,
				   CCTK_POINTER faces,
				   CCTK_POINTER table_handles, 
				   CCTK_STRING bc_name);

/********************************************************************
 ***************** Scheduled Routine Prototypes *********************
 ********************************************************************/

void Boundary_ApplyPhysicalBCs(CCTK_ARGUMENTS);
void Boundary_ClearSelection(void);

/********************************************************************
 ********************* Other Routine Prototypes *********************
 ********************************************************************/

/********************************************************************
 *********************     Local Data   *****************************
 ********************************************************************/

/* Table for holding function pointers associated with each boundary condition:
 * This table has
 *	key   = boundary condition name (eg "Radiative")
 *	value = a CCTK_FPOINTER pointing to a function to implement that BC
 */
static int physbc_table_handle = -1;

/* Linked list for storing selected vars data */
static struct BCVAR *selections_list = NULL;

/* Linked list for storing data associated with selections list itself */
static struct BCDATA *bcdata_list = NULL;


/********************************************************************
 *********************     Aliased Routines   **********************
 ********************************************************************/

 /*@@
   @routine    Bdry_Boundary_RegisterPhysicalBC
   @date       Sun Nov  3 19:51:37 CET 2002
   @author     David Rideout
   @desc 
               Used to register physical boundary conditions with the boundary
	       thorn.  
   @enddesc 
   @calls      
   @history 
   @endhistory
   @var        GH
   @vdesc      cctkGH *
   @vtype      CCTK_POINTER
   @vio        in
   @endvar
   @var        fn_pointer
   @vdesc      pointer to function which implements boundary condition bc_name
   @vtype      CCTK_FPOINTER
   @vio        in
   @endvar
   @var        bc_name
   @vdesc      name of boundary condition
   @vtype      CCTK_STRING
   @vio        in
   @endvar
   @returntype CCTK_INT
   @returndesc
               0 success
              -1 error creating table to hold bcs
	      -2 duplicate registration of bc_name
	      -3 error adding bc to table
   @endreturndesc
@@*/
CCTK_INT Bdry_Boundary_RegisterPhysicalBC(CCTK_POINTER GH,
					  CCTK_FPOINTER fn_pointer, 
					  CCTK_STRING bc_name)
{
  int retval;

  /* Ignore GH for now */
  GH = GH;

  /* Check if NULL has been passed for fn_pointer */
  if (!fn_pointer)
  {
    /* Use dummy function if NULL function registered (e.g. for
       non-local physical bcs) */
    fn_pointer = (CCTK_FPOINTER) &BndNone;
  }

  /* Check input arguments */
  if (!fn_pointer)
  {
    CCTK_VWarn(2, __LINE__, __FILE__, CCTK_THORNSTRING,
	       "Null pointer passed to Boundary_RegisterPhysicalBC.  "
	       "Is this intentional?");
  }

  /* Create the registered routines table if necessary */
  if (physbc_table_handle == -1) 
  {
    physbc_table_handle = Util_TableCreate(UTIL_TABLE_FLAGS_DEFAULT);
    if (physbc_table_handle<0) 
    {
      CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
		 "Error creating table to hold registered physical boundary "
		 "conditions");
      retval = -1;
    }
  }

  /* Check if boundary condition has already been registered under this name */
  if (Util_TableGetFnPointer(physbc_table_handle, NULL, bc_name)>=0)
  {
    CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
	       "There already exists a physical boundary condition "
	       "registered under the name \"%s\"", bc_name);
    retval = -2;
  } else 
  {
    /* Add boundary condition to table */
    if (Util_TableSetFnPointer(physbc_table_handle, fn_pointer, bc_name)<0) 
    {
      CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
		 "Error adding boundary condition to table");
      retval = -3;
    }
    retval = 0;
  }

  return retval;
}

 /*@@
   @routine    Bdry_Boundary_SelectVarForBC
   @date       Sun Nov  3 19:51:37 CET 2002
   @author     David Rideout
   @desc 
	       Used to select a Cactus variable to have boundary 
	       conditions applied, using its variable name.
   @enddesc 
   @calls      
   @history 
   @endhistory 
   @var        GH
   @vdesc      cctkGH *
   @vtype      CCTK_POINTER
   @vio        in
   @endvar
   @var        faces
   @vdesc      set of faces to which to apply the boundary condition
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        table_handle
   @vdesc      handle of table which holds arguments to be passed to bc
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        var_name
   @vdesc      name of variable to which to apply bc
   @vtype      CCTK_STRING
   @vio        in
   @endvar   
   @var        bc_name
   @vdesc      name of bc to apply
   @vtype      CCTK_STRING
   @vio        in
   @endvar
   @returntype CCTK_INT
   @returndesc
    0 success
   -3 invalid variable name
   or the returncode of @seeroutine Bdry_Boundary_SelectVarForBCI
   @endreturndesc
@@*/
CCTK_INT Bdry_Boundary_SelectVarForBC(CCTK_POINTER GH, 
				      CCTK_INT faces,
				      CCTK_INT table_handle, 
				      CCTK_STRING var_name, 
				      CCTK_STRING bc_name) 
{
  int retval, var_index;

  retval = 0;

  var_index = CCTK_VarIndex(var_name);
  if (var_index<0) 
  {
    CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
	       "Invalid variable name");
    retval = -3;
  } else 
  {
    retval = Bdry_Boundary_SelectVarForBCI(GH, faces, table_handle, var_index, 
					       bc_name);
  }

  return retval;
}

 /*@@
   @routine    Bdry_Boundary_SelectVarForBCI
   @date       Sun Nov  3 19:51:37 CET 2002
   @author     David Rideout
   @desc 
	       Used to select a Cactus variable to have boundary 
	       conditions applied, using its var index.
   @enddesc 
   @calls      
   @history 
   @endhistory 
   @var        GH
   @vdesc      cctkGH *
   @vtype      CCTK_POINTER
   @vio        in
   @endvar
   @var        faces
   @vdesc      set of faces to which to apply the boundary condition
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        table_handle
   @vdesc      handle of table which holds arguments to be passed to bc
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        var_index
   @vdesc      index of variable to which to apply bc
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        bc_name
   @vdesc      name of bc to apply
   @vtype      CCTK_STRING
   @vio        in
   @endvar
   @returntype CCTK_INT
   @returndesc
    0 success
   -1 error allocating memory for new entry in selected variables database
   -2 no such physical boundary condition registered
   -4 error allocating memory for new entry in 'bcdata' database
   @endreturndesc
@@*/
CCTK_INT Bdry_Boundary_SelectVarForBCI(CCTK_POINTER GH, 
				       CCTK_INT faces,
				       CCTK_INT table_handle, 
				       CCTK_INT var_index, 
				       CCTK_STRING bc_name) 
{
  int retval;
  struct BCVAR *new_entry;
  struct BCVAR *current;
  struct BCVAR *previous;
  struct BCDATA *current_bcdata;
  struct BCDATA *previous_bcdata;

  retval = 0;
  current = NULL;
  previous = NULL;

#ifdef DEBUG
  printf("Boundary_SelectVarForBCI: called with table_handle=%d, var_index=%d, bc_name=%s\n", table_handle, var_index, bc_name);
  printf("                                   vi %d corresponds to %s\n", var_index, CCTK_VarName(var_index));
#endif

  /* Ignore GH */
  GH = GH;

  /* Has some function implementing bc_name been registered? */
  if (!Util_TableQueryValueInfo(physbc_table_handle, NULL, NULL,
				bc_name)) 
  {
    CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
	       "There is no function implementing the physical boundary "
	       "condition %s", bc_name);
    retval = -2;
  }

  /* allocate memory for new entry in database */
  new_entry = (struct BCVAR *) malloc(sizeof(struct BCVAR));
  if (!new_entry)
  {
    CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
	       "Unable to allocate memory for entry into "
               "'selected for bcs' database");
    retval = -1;
  }
#ifdef DEBUG
  printf("Boundary_SelectVarForBCI: new entry in list will be stored at %p\n", new_entry);
#endif

  /* populate new entry with data */
  new_entry -> bc_name = Util_Strdup(bc_name);
  new_entry -> faces = faces;
  new_entry -> table = table_handle;
  new_entry -> var_index = var_index;
  /* new_entry -> next_entry will be filled in later */
#ifdef DEBUG
  printf("Boundary_SelectVarForBCI: sucessfully stored data in new entry\n");
#endif

  /* Use bcdata list to jump to appropriate 'sector' of selections list */
  for (current_bcdata = bcdata_list; 
       current_bcdata; 
       previous_bcdata=current_bcdata, current_bcdata = current_bcdata->next) 
  {
#ifdef DEBUG
    printf("Boundary_SelectVarForBCI: looping through bcdata list, at "
	   "current_bcdata for %s\n",current_bcdata->bc_name);
#endif

    if (CCTK_Equals(current_bcdata->bc_name,bc_name))
    {
      current = current_bcdata->start;
      current_bcdata->num++;
#ifdef DEBUG
      printf("Boundary_SelectVarForBCI: var %s brings bc %s to %d vars\n",
	     CCTK_VarName(var_index),bc_name, current_bcdata->num);
#endif
    }
  }
#ifdef DEBUG
  printf("Boundary_SelectVarForBCI: current starting at %p\n", current);
#endif

  /* If current has not been initialized yet, this is a new bc_name
   * that does not appear in the bcdata list.
   */
  if (!current) /* bc_name was not found in bcdata_list */
  {
#ifdef DEBUG
    printf("Boundary_SelectVarForBCI: adding new entry to bcdata list\n");
#endif

    /* new bc_name.  Add entry to end of bcdata list. */
    current_bcdata = (struct BCDATA *) malloc(sizeof(struct BCDATA));
    if (!current_bcdata) {
      CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
		 "Unable to allocate memory for internal 'bcdata' list");
      retval = -4;
    } else
    {
      current_bcdata->next = NULL;
      current_bcdata->bc_name = Util_Strdup(bc_name);
      current_bcdata->start = NULL; /* this will be filled in below */
      current_bcdata->num = 1;
    }

    /* Place entry into bcdata list */
    if (bcdata_list) /* this is not the first entry */
    {
      previous_bcdata->next = current_bcdata;
    } else 
    {
      bcdata_list = current_bcdata;
    }
#ifdef DEBUG
    printf("bcdata_list is now %p\n", bcdata_list);
    printf("bcdata_list->bc_name is %s\n", bcdata_list->bc_name);
    printf("current_bcdata->bc_name is %s\n", current_bcdata->bc_name);
#endif

    /* bc_name does not appear in bcdata_list, so start at beginning */
    current = selections_list;
  }
  
  /* enter into correct location in linked list */
  /* Note that this loop is skipped if current is NULL. */
  for (; /* starting value for current is selected using the bcdata
	    list, above */
       current && !entry_less_than(new_entry,current); 
       /* continue if not at end of list, and new_entry is greater than 
	  current entry */
       previous = current, current = current->next_entry)
       /* store previous value for later use */
  {
  }

  /* The possibilities:
   * 1 nothing NULL: new_entry goes between previous and current
   * 2 previous NULL, but current non-null : new_entry goes at the start of the 
   *   selections list
   * 3 current NULL, previous non-NULL : new_entry goes at the end of the 
   *   selections list
   * 4 both NULL: selections list is empty
   */  
  if (previous) /* case 1 or 3 */
  {
    if (current) /* case 1 : goes in middle of list */
    {
      previous->next_entry = new_entry;
      new_entry->next_entry = current;
    } else /* case 3 : goes at end of list */ 
    {
      previous->next_entry = new_entry;
      new_entry->next_entry = NULL;
    }
  } else /* case 2 or 4 */ 
  {
    if (current) /* case 2 : goes at start of list*/
    {
      selections_list = new_entry;
      new_entry->next_entry = current;
    } else /* case 4 : starts list */
    {
#ifdef DEBUG
      printf("Boundary_SelectVarForBCI: starting new selections list\n");
#endif
      new_entry->next_entry = NULL;
      selections_list = new_entry;
    }
  }

  /* If new bcdata entry, update current_bcdata->start as promised above */
  if (current_bcdata && !current_bcdata->start)
  {
    current_bcdata->start = new_entry;
  }

#ifdef DEBUG
  printf("\n");
#endif
  return retval;
}

 /*@@
   @routine    Bdry_Boundary_SelectGroupForBC
   @date       Thu Dec 26 21:45:34 CET 2002
   @author     David Rideout
   @desc
	       Used to select a Cactus variable group to have boundary 
	       conditions applied, using the group name.
	       Table handle and faces must be the same for each member of the 
	       group.
   @enddesc 
   @calls      
   @history 
   @endhistory 
   @var        GH
   @vdesc      cctkGH *
   @vtype      CCTK_POINTER
   @vio        in
   @endvar
   @var        faces
   @vdesc      set of faces to which to apply bc
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        table_handle
   @vdesc      handle of table which holds arguments to be passed to bc
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        group_name
   @vdesc      name of group to which to apply bc
   @vtype      CCTK_STRING
   @vio        in
   @endvar
   @var        bc_name
   @vdesc      name of bc to apply
   @vtype      CCTK_STRING
   @vio        in
   @endvar
   @returntype CCTK_INT
   @returndesc
    0 success
   -6 invalid group name
    or the returncode of @seeroutine Bdry_Boundary_SelectGroupForBCI
   @endreturndesc
@@*/
CCTK_INT Bdry_Boundary_SelectGroupForBC(CCTK_POINTER GH,
					CCTK_INT faces,
					CCTK_INT table_handle, 
					CCTK_STRING group_name, 
					CCTK_STRING bc_name)
{
  int retval, gi;

  retval = 0;

#ifdef DEBUG
  printf("Boundary_SelectGroupForBC: called for group %s\n", group_name);
#endif

  /* get group index */
  gi = CCTK_GroupIndex(group_name);
  if (!gi) 
  {
    CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
	       "Invalid group name %s\n", group_name);
    retval = -6;
  } else
  {
    /* call Bdry_Boundary_SelectGroupForBCI() */
    retval = Bdry_Boundary_SelectGroupForBCI(GH, faces, table_handle, gi, 
						 bc_name);
  }

  return retval;
}

 /*@@
   @routine    Bdry_Boundary_SelectGroupForBCI
   @date       Thu Dec 26 21:45:34 CET 2002
   @author     David Rideout
   @desc 
	       Used to select a Cactus variable group to have boundary 
	       conditions applied, using the group index.
   @enddesc 
   @calls      
   @history 
   @endhistory 
   @var        GH
   @vdesc      cctkGH *
   @vtype      CCTK_POINTER
   @vio        in
   @endvar
   @var        faces
   @vdesc      set of faces to which to apply bc
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        table_handle
   @vdesc      handle of table which holds arguments to be passed to bc
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        group_index
   @vdesc      index of group to which to apply bc
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        bc_name
   @vdesc      name of bc to apply
   @vtype      CCTK_STRING
   @vio        in
   @endvar
   @returntype CCTK_INT
   @returndesc
    0 success
   -7 invalid group index
    or the returncode of @seeroutine Bdry_Boundary_SelectVarForBCI
   @endreturndesc
@@*/
CCTK_INT Bdry_Boundary_SelectGroupForBCI(CCTK_POINTER GH,
					 CCTK_INT faces,
					 CCTK_INT table_handle, 
					 CCTK_INT group_index, 
					 CCTK_STRING bc_name) 
{
  int num_vars, vi, max_vi, retval;

  /* Get var indices from group name */
  num_vars = CCTK_NumVarsInGroupI(group_index);
  if (num_vars<0) 
  {
    CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
	       "Invalid variable index");
    retval = -7;
  }
  
  /* loop over variables in group */
  vi = CCTK_FirstVarIndexI(group_index);
  max_vi = vi + num_vars;
  for (; vi<max_vi; ++vi)
  {
    retval = Bdry_Boundary_SelectVarForBCI(GH, faces, table_handle, vi, 
					   bc_name);
  }

  return retval;
}

 /*@@
   @routine    Bdry_Boundary_SelectedGVs
   @date       Sun Nov  3 19:51:37 CET 2002
   @author     David Rideout
   @desc 
               Returns list of variable indices and table handles of 
	       variables selected for boundary conditions.
   @enddesc 
   @calls      
   @history 
   @endhistory
   @var        GH
   @vdesc      cctkGH *
   @vtype      CCTK_POINTER
   @vio        in
   @endvar
   @var        array_size
   @vdesc      size of arrays to which var_indices and table_handles point
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        var_indices
   @vdesc      array into which selected variable indices will be placed
   @vtype      CCTK_INT
   @vio        out
   @endvar
   @var        faces
   @vdesc      array into which a set of selected faces for variables selected
               for bc will be placed
   @vtype      CCTK_INT
   @vio        out
   @endvar
   @var        table_handles
   @vdesc      array into which table_handles for variables selected for bc 
               will be placed
   @vtype      CCTK_INT
   @vio        out
   @endvar
   @var        bc_name
   @vdesc      name of bc for which to get the selected vars, 
               NULL returns all selected vars for all bcs
   @vtype      CCTK_STRING
   @vio        in
   @endvar
   @returntype CCTK_INT
   @returndesc
               number of variables selected for bc_name
   @endreturndesc
@@*/
CCTK_INT Bdry_Boundary_SelectedGVs(CCTK_POINTER GH, 
				   CCTK_INT array_size, 
				   CCTK_POINTER var_indices, 
				   CCTK_POINTER faces,
				   CCTK_POINTER table_handles, 
				   CCTK_STRING bc_name) 
{
  int retval, i;
  struct BCVAR *current;
  struct BCDATA *current_bcdata;

  current = NULL;
  retval = 0;

  /* Ignore GH */
  GH = GH;

#ifdef DEBUG
  printf("Boundary_SelectedGVs: called with bc_name=\"%s\" array_size=%d\n", bc_name, array_size);
#endif

  /* Step through bcdata list to find starting point, and to compute return 
     value */
  for (current_bcdata = bcdata_list;
       current_bcdata;
       current_bcdata = current_bcdata->next) 
  {

#ifdef DEBUG
    printf("Boundary_SelectedGVs: looping through bcdata list\n"
	   "                      current_bcdata->bc_name=\"%s\"\n",
	   current_bcdata->bc_name);
#endif

    if (!bc_name || CCTK_Equals(current_bcdata->bc_name,bc_name))
    {
#ifdef DEBUG
      printf("Boundary_SelectedGVs: strings match, using this start value\n");
#endif
      /* If bc_name matches, use this entry to set current */
      current = current_bcdata->start;
      retval += current_bcdata->num;
      if (bc_name) 
      {
	/* No more needs be added to retval if a specific bc_name has
           been requested. */
	break;
      }
    }
  }
    /* Do we want to return an error code if an 'invalid' bc_name is used?
     * if (!current)
     * {
     *   CCTK_VWarn(2, __LINE__, __FILE__, CCTK_THORNSTRING,
     *   "No variables are marked for boundary condition %s", bc_name); 
     *   retval = -1;
     * }
     */
  if (!bc_name) 
  {
    /* If bc_name is NULL then start from the beginning */
    current = selections_list;
  }

#ifdef DEBUG
  printf("Boundary_SelectedGVs: initializing current to %p\n", current);
#endif
  /* If current is still NULL, bc_name did not match any entry in the
   * bcdata list, i.e. no variables were selected for this bc.
   *
   * If array_size is zero, then just exit immediately, because there
   * is nothing more to do.  
   */
  if (current && array_size) 
  {
  /* loop through selections list, grabbing entries with bc_name, or
   * all if bc_name==NULL
   * 
   * could save a string comparison at each iteration if we also
   * stored bcdata->end
   */
    for (i=0; /* i indexes the location in the returned arrays, 
		 and is incremented after use */
	 current;
	 current = current->next_entry) 
    {
#ifdef DEBUG
      printf("Boundary_SelectedGVs: looping through selected vars, at current->var_index = %d\n",current->var_index);
      printf("                               current->next_entry is %p\n",current->next_entry);
#endif

      if (!bc_name || CCTK_Equals(current->bc_name, bc_name))
      {
	if (i < array_size) 
	{
	  ((int *) faces)[i] = current->faces;
	  ((int *) table_handles)[i] = current->table;
	  ((int *) var_indices)[i] = current->var_index;
	}
	++i;
      } else
      {
	if (i==0)
	{
	  printf("Boundary_SelectedGVs: Internal error:  I am "
		 "past the relevant section of the selections list, yet no "
		 "vars have been 'selected'.\n");
	}

	/* can stop looking if we are past the relevant section of the
	   list */
	break;
      }
    }
  }

  return retval;
}

 
/********************************************************************
 *********************     Scheduled Routines   **********************
 ********************************************************************/

 /*@@
   @routine    Boundary_ApplyPhysicalBCs
   @date       Sun Nov  3 19:51:37 CET 2002
   @author     David Rideout
   @desc  
               This will apply all requested physical boundary conditions.
   @enddesc 
   @calls      
   @history 
   @endhistory 
   @var        CCTK_ARGUMENTS
   @vdesc      Cactus argument list
   @vtype      CCTK_*
   @vio        in
   @endvar
   @returntype void
   @returndesc
   @endreturndesc
@@*/

void Boundary_ApplyPhysicalBCs(CCTK_ARGUMENTS) 
{
  DECLARE_CCTK_ARGUMENTS;
  phys_bc_fn_ptr bc_fn;
  int num_vars, *vars, *faces, *tables, err, max_num_vars;
  struct BCDATA *current_bcdata;

  max_num_vars = 0;
  vars = NULL; /* so that it won't be freed if it was never malloced */

  /* Step through each requested physical boundary condition */
  for (current_bcdata = bcdata_list;
       current_bcdata;
       current_bcdata = current_bcdata->next) 
  {
#ifdef DEBUG
    printf("Boundary_ApplyPhysicalBCs: current_bcdata %p holds: next=%p "
	   "bc_name=%s start=%p num=%d\n",
	   current_bcdata, current_bcdata->next, current_bcdata->bc_name, 
	   current_bcdata->start, current_bcdata->num);
#endif

    /* Allocate memory to hold selected bcs */
    num_vars = current_bcdata->num;
    if (num_vars > max_num_vars) 
    {
      max_num_vars = num_vars; /* store new maximum */
      if (vars) 
      {
	/* reallocate arrays if necessary */
	realloc(vars, num_vars*sizeof(int));
	realloc(faces, num_vars*sizeof(int));
	realloc(tables, num_vars*sizeof(int));
      } else 
      {
	vars = (int *) malloc(num_vars*sizeof(int));
	faces = (int *) malloc(num_vars*sizeof(int));
	tables = (int *) malloc(num_vars*sizeof(int));
      }
    }

    /* get selected vars for this bc_name*/
    err = Bdry_Boundary_SelectedGVs(cctkGH, num_vars, vars, faces, tables, 
					current_bcdata->bc_name);
    if (err<0) /* This is a redundant test for now, 
		  Bdry_Boundary_SelectedGVs never returns <0 */
    {
      CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
		 "Error in Boundary_SelectedGVs for %s boundary condition", 
		 current_bcdata->bc_name);
    } else if (err != num_vars)
    {
      CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
		 "Boundary_SelectedGVs returned %d selected variables for "
		 "\"%s\" boundary condition, but %d expected\n", err, 
		 current_bcdata->bc_name, num_vars);
    }

    /* Get the fn ptr for the bc */
#ifdef DEBUG
    printf("Boundary_ApplyPhysicalBCs: current_bcdata->bc_name=\"%s\"\n",
	   current_bcdata->bc_name);
#endif
    err = Util_TableGetFnPointer(physbc_table_handle, (CCTK_FPOINTER *)&bc_fn,
				 current_bcdata->bc_name);
    if (err<0) 
    {
      CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
		 "Boundary_ApplyPhysicalBCs: Util_TableGetFnPointer "
		 "returned %d", err);
    }

    /* Apply bc to vi */
#ifdef DEBUG
    printf("Boundary_ApplyPhysicalBCs: Attempting to call boundary condition\n"
	   "                           Using function pointer %p with arguments\n"
	   "                           cctkGH %p, num_vars %d, vars, tables\n",
	   (void *) bc_fn, (const void *) cctkGH, num_vars);
#endif
    err = (*bc_fn)(cctkGH, num_vars, vars, faces, tables);
    if (err<0) 
    {
      CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
		 "Function associated with boundary condition %s returned %d",
		 current_bcdata->bc_name, err);
    }
  }

  /* Free data */
  if (vars)
  {
    free(vars);
    free(faces);
    free(tables);
  }
}

 /*@@
   @routine    Boundary_ClearSelection
   @date       Sun Nov  3 19:51:37 CET 2002
   @author     David Rideout
   @desc
	       Clears all boundary condition selections.
   @enddesc
   @calls 
   @history
   @endhistory
@@*/

void Boundary_ClearSelection(void)
{
  struct BCVAR *current, *next;
  struct BCDATA *current_bcdata, *next_bcdata;

  /* Free selections list */
  next = selections_list;
  for (current = selections_list; next; current = next)
  {
    next = current->next_entry;
    free(current->bc_name);
    free(current);
  }
  selections_list = NULL;

  /* Free bcdata list */
#ifdef DEBUG
  printf("Boundary_ClearSelection: looping through bcdata list for freeing\n");
#endif
  next_bcdata = bcdata_list; /* simply so that it will be non-NULL */
  for (current_bcdata = bcdata_list; next_bcdata; current_bcdata = next_bcdata)
  {
    next_bcdata = current_bcdata->next;
    /* free(current_bcdata->bc_name); Must one free this? */
#ifdef DEBUG
    printf("Boundary_ClearSelection: freeing %p\n",current_bcdata);
#endif
    free(current_bcdata);
  }
  bcdata_list = NULL;
}

/********************************************************************
 *********************     Local Routines   *************************
 ********************************************************************/

 /*@@
   @routine    entry_less_than
   @date       Sun Nov  3 19:51:37 CET 2002
   @author     David Rideout
   @desc 
               Sorts entries in selections list
   @enddesc 
   @calls      
   @history 
   @endhistory
   @var        new
   @vdesc      new entry to be inserted into selections list
   @vtype      struct BCVAR *
   @vio        in
   @endvar
   @var        current
   @vdesc      entry in selections list to which to compare new
   @vtype      struct BCVAR *
   @vio        in
   @endvar
   @returntype CCTK_INT
   @returndesc
              -1 new equals current
	       0 new 'greater than' current
               1 new 'less than' current
	       2 new and current have same bc_name, but new has smaller table 
	         handle
	       3 new and current have same bc_name and table handle, but new 
	         has smaller variable index 
   @endreturndesc
@@*/
static int entry_less_than(struct BCVAR *new, struct BCVAR *current) 
{
  int retval;

  /* can assume both arguments are valid (non-null) */
  /* Perhaps this can be done more intelligently... */
  retval = Util_StrCmpi(new->bc_name, current->bc_name);
  if (retval < 0) 
  {
    retval = 1; /* new < current */
  } else if (retval>0) 
  {
    retval = 0;
  } else if (new->table < current->table) 
  {
    retval = 2;
  } else if (new->table > current->table) 
  {
    retval = 0;
  } else if (new->var_index < current->var_index) 
  {
    retval = 3;
  } else if (new->var_index > current->var_index) 
  {
    retval = 0;
  } else 
  {
    CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
	       "Duplicate entry %s marked for boundary condition %s, "
	       "with table handle %d\n", CCTK_VarName(new->var_index), 
	       new->bc_name, new->table);
    retval = -1;
  }

  return retval;
}