summaryrefslogtreecommitdiff
path: root/src/main/Groups.c
blob: 5c48fe32117f9d9362e73f355eb83f1f49e23429 (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
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
/*@@
  @file      Groups.c
   @date      Mon Feb  1 12:16:28 1999
   @author    Tom Goodale
   @desc 
   Routines to deal with groups.
   @enddesc 
 @@*/

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

#include "flesh.h"
#include "FortranString.h"

#include "Misc.h"
#include "Groups.h"
#include "WarnLevel.h"

/* #define DEBUG_GROUPS */

static char *rcsid = "$Header$";

/* Static variables needed to hold group and variable data. */

static int n_groups = 0;
static cGroupDefinition *groups = NULL;

static int total_variables = 0;

static int *group_of_variable = NULL;

static int maxdim = 0;


/* When passing to fortran, must pass by reference
 * so need to define the odd global variable to pass 8-(
 */

int _cctk_one = 1;


cGroupDefinition *CCTKi_SetupGroup(const char *implementation, const char *group_name, int n_variables);



 /*@@
   @routine    CCTK_GroupIndex
   @date       Fri Jan 29 08:43:48 1999
   @author     Tom Goodale
   @desc 
   Gets the index number for the specified group.
   @enddesc 
   @calls CCTK_Equals   
   @calledby   
   @history 
 
   @endhistory 

@@*/

int CCTK_GroupIndex(const char *fullgroupname)
{
  int group_num;
  int retval=-1;
  char *imp1         = NULL;
  char *group1       = NULL;
  const char *imp2   = NULL;
  const char *group2 = NULL;
  
  switch(CCTK_DecomposeName(fullgroupname,&imp1,&group1))
  {
  case 1:

    CCTK_WARN(2,"Group name not in correct format implementation::group");
    retval = -3;
    break;

  case 2:

    CCTK_WARN(2,"Memory allocation failed");
    retval = -4;
    break;

  default:

    imp2 = (const char *)imp1;
    group2 = (const char *)group1;

    for(group_num = 0; group_num < n_groups; group_num++)
    {
      if(CCTK_Equals(imp2, groups[group_num].implementation) &&
	 CCTK_Equals(group2, groups[group_num].name)) break;
    }

    if (group_num < n_groups)
    {
      retval = group_num;
    }
    else
    {
      char *message;
      message = (char *)malloc( (100+strlen(fullgroupname))*sizeof(char) ); 
      sprintf(message,"No group found with the name %s",fullgroupname);
      CCTK_WARN(2,message);
      if (message) free(message);
      retval = -1;
    }
  }

  /* Free memory from CCTK_DecomposeName */
  if (imp1) free(imp1);
  if (group1) free(group1);
 
  return retval;

}




 /*@@
   @routine    CCTKi_CreateGroup
   @date       Thu Jan 14 15:25:54 1999
   @author     Tom Goodale
   @desc 
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int CCTKi_CreateGroup(const char *gname, const char *thorn, const char *imp,
		     const char *gtype,
		     const char *vtype,
		     const char *gscope,
		     int dimension,
		     int ntimelevels,
		     int n_variables,
		     ...)
{
  int retval;
  int groupscope;
  va_list ap;
  char *variable_name;

  cGroupDefinition *group=NULL;

  int variable;

  retval = 0;

  /* Allocate storage for the group */
  groupscope = CCTKi_GroupScopeNumber(gscope);
  if (groupscope == GROUP_PUBLIC || groupscope == GROUP_PROTECTED)
  {
    group = CCTKi_SetupGroup(imp, gname, n_variables);
  }
  else if (groupscope == GROUP_PRIVATE)
  {
    group = CCTKi_SetupGroup(thorn, gname, n_variables);
  }
  else
  {
    CCTK_WARN(1,"Unrecognised group scope in CCTKi_CreateGroup");
  }

  /* Allocate storage for the group and setup some stuff. */
  if(group)
  {
    group->dim = dimension;
    group->gtype = CCTKi_GroupTypeNumber(gtype);
    group->vtype = CCTKi_VarTypeNumber(vtype);
    group->gscope = groupscope;

    group->n_timelevels = ntimelevels;
    
    /* Extract the variable names from the argument list. */
    va_start(ap, n_variables);

    for(variable = 0; variable < n_variables; variable++)
    {
      variable_name = va_arg(ap, char *);

      group->variables[variable].name = (char *)malloc((strlen(variable_name)+1*sizeof(char)));
      
      if(group->variables[variable].name)
      {
	strcpy(group->variables[variable].name, variable_name);
      }
      else
      {
	break;
      }
    }

    va_end(ap);

    if(variable < n_variables)
    {
      retval = 3;
    }
    else
    {
      if (dimension > maxdim) maxdim=dimension;
    }
  }
  else
  {
    retval = 2;
  }

  if(retval)
  {
    fprintf(stderr, "Error %d in CCTKi_CreateGroup\n", retval);
  }

#ifdef DEBUG_GROUPS
  printf("Created group %s\n",gname);
  printf("  CCTK_GroupIndex(%s) = %d\n",groupname,
	 CCTK_GroupIndex(groupname));
  printf("  CCTK_GroupName(%d) = %s\n",CCTK_GroupIndex(name),
         CCTK_GroupName(CCTK_GroupIndex(groupname)));
#endif

  return retval;

}

 /*@@
   @routine    CCTKi_SetupGroup
   @date       Thu Jan 14 16:38:40 1999
   @author     Tom Goodale
   @desc 
   Stores the data associated with a group.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
cGroupDefinition *CCTKi_SetupGroup(const char *implementation, 
				  const char *name, 
				  int n_variables)
{
  int *temp_int;
  cGroupDefinition *temp;
  cGroupDefinition *returndata;
  int variable;
  int group_num;
  char *fullname1;
  const char *fullname2;

  fullname1 = (char *) malloc( (strlen(implementation)+strlen(name)+2)
				    *sizeof(const char *));
  sprintf(fullname1,"%s::%s",implementation,name); 
  fullname2 = (const char *)fullname1;

  if((group_num = CCTK_GroupIndex(fullname1)) == -1)
  {
    /* Resize the array of groups */
    if(temp = ((cGroupDefinition *)realloc(groups, (n_groups+1)*sizeof(cGroupDefinition))))
    {
      groups = temp;
      
      /* Allocate memory to various fields */
      groups[n_groups].implementation = (char *)malloc((strlen(implementation)+1)*sizeof(char));
      
      groups[n_groups].name = (char *)malloc((strlen(name)+1)*sizeof(char));
      
      groups[n_groups].variables = (cVariableDefinition *)malloc(n_variables*sizeof(cVariableDefinition));
      
      /* Resize the array holding correspondence between variables and groups. */

      temp_int = (int *)realloc(group_of_variable, (total_variables+n_variables)*sizeof(int));

      if(groups[n_groups].implementation && 
	 groups[n_groups].name && 
	 groups[n_groups].variables &&
	 temp_int)
      {
	/* Fill in the data structures. */
	group_of_variable = temp_int;
	
	strcpy(groups[n_groups].implementation, implementation);
	strcpy(groups[n_groups].name, name);
	
	groups[n_groups].number = n_groups;
	
	groups[n_groups].n_variables = n_variables;
	
	/* Fill in global variable numbers. */
	for(variable = 0; variable < n_variables; variable++)
	{
	  groups[n_groups].variables[variable].number = total_variables;
	  
	  group_of_variable[total_variables] = n_groups;
	  
	  total_variables++;
	}
	
	n_groups++;
      }
      else
      {
	/* Memory allocation failed, so free any which may have been allocated. */
	free(groups[n_groups].implementation);
	groups[n_groups].implementation = NULL;

	free(groups[n_groups].name);
	groups[n_groups].name = NULL;
    
	free(groups[n_groups].variables);
	groups[n_groups].variables = NULL;

      }
    }
    
    /* Return the new group definition structure if successful, otherwise NULL.*/
    if(temp && groups[n_groups-1].name)
    {
      returndata =  &(groups[n_groups-1]);
    }
    else
    {
      returndata =  NULL;
    }
  }
  else
  {
    returndata = &(groups[group_num]);
  }

  if (fullname1) free(fullname1);

  return returndata;
}




 /*@@
   @routine    CCTK_VarIndex
   @date       Mon Feb  8 12:03:22 1999
   @author     Tom Goodale
   @desc 
   Gets the global number associated wth a variable.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int CCTK_VarIndex(const char *variable_name)
{
  int retval;
  int gnum;
  int variable;
  int ierr;
  char *message;
  char *realimpname;
  char *realvarname;
  const char *impname;
  const char *varname;

  retval = -1;

  ierr = CCTK_DecomposeName(variable_name,&realimpname,&realvarname);

  if (ierr == 0)
  {

    /* Store the pointers to these strings in const char *s */
    impname = realimpname;
    varname = realvarname;

    for (gnum = 0; gnum < n_groups; gnum++)
    {			
    
      for(variable=0; variable<groups[gnum].n_variables;variable++)
      {
	if(CCTK_Equals(varname, groups[gnum].variables[variable].name)
	   && CCTK_Equals(impname,groups[gnum].implementation))
	{
	  retval  = groups[gnum].variables[variable].number;
	  break;
	}
      }
    }

  }
  else if (ierr == 1)
  {
    message = (char *)malloc( (100+strlen(variable_name))*sizeof(char) );
    sprintf(message,"Full name %s in wrong format in CCTK_VarNum",
	    variable_name);
    CCTK_WARN(2,message);
    if (message) free(message);
    retval = -3; 
  }
  else if (ierr == 2)
  {
    CCTK_WARN(2,"Memory allocation failed");
    retval = -4;
  }
  else
  {
    CCTK_WARN(1,"Error failed to be caught");
  }

#ifdef DEBUG_GROUPS
  printf(" In VarIndex\n"," ------------\n");
  printf("   impname -%s-\n",impname);
  printf("   group_name -%s-\n",group_name);
  printf("   varname -%s-\n",varname);
#endif
    

  if (realimpname) free(realimpname);
  if (realvarname) free(realvarname);
    
  return retval;

}

void  FMODIFIER FORTRAN_NAME(CCTK_VarIndex)(int *index,ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE(name)
  *index = CCTK_VarIndex(name);
  free(name);
}


 /*@@
   @routine    CCTK_MaxDim
   @date       Mon Feb  8 12:04:01 1999
   @author     Tom Goodale
   @desc 
   Gets the maximum dimension of all groups.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int CCTK_MaxDim(void)
{
  return maxdim;
}

void  FMODIFIER FORTRAN_NAME(CCTK_MaxDim)(int *dim)
{
  *dim = CCTK_MaxDim();
}


 /*@@
   @routine    CCTK_NumVars
   @date       Mon Feb  8 12:04:50 1999
   @author     Tom Goodale
   @desc 
   Gets the total number of variables.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int CCTK_NumVars(void)
{
  return total_variables;
}

void  FMODIFIER FORTRAN_NAME(CCTK_NumVars)(int *total_variables)
{
  *total_variables = CCTK_NumVars();
}


 /*@@
   @routine    CCTK_NumGroups
   @date       Mon Feb  8 12:04:50 1999
   @author     Tom Goodale
   @desc 
   Gets the total number of groups.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int CCTK_NumGroups(void)
{
  return n_groups;
}

void  FMODIFIER FORTRAN_NAME(CCTK_NumGroups)(int *n_groups)
{
  *n_groups = CCTK_NumGroups();
}


 /*@@
   @routine    CCTK_GroupNameFromVarI
   @date       Mon Feb 22
   @author     Gabrielle Allen
   @desc 
   Given a variable index return a group name.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

char *CCTK_GroupNameFromVarI(int var)
{
  char *retval;
  int group_num;

  if (var<0 || var>total_variables-1)
  {
    retval = NULL;
  }
  else
  {  
    group_num = group_of_variable[var];
    retval = (char *)malloc( ( strlen(groups[group_num].name) 
			     + strlen(groups[group_num].implementation) + 2)
			     * sizeof(char) );
    sprintf(retval,"%s::%s",groups[group_num].implementation,
	    groups[group_num].name);
  } 

  return retval;
}


 /*@@
   @routine    CCTK_GroupIndexFromVarI
   @date       Mon Feb 22
   @author     Gabrielle Allen
   @desc 
   Given a variable index return a group index.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int CCTK_GroupIndexFromVarI(int var)
{
  int retval;

  if (var<0 || var>total_variables-1)
  {
    retval = -1;
  }
  else
  {  
    retval = group_of_variable[var];
  } 

  return retval;

}

void  FMODIFIER FORTRAN_NAME(CCTK_GroupIndexFromVarI)(int *gindex,int *var)
{
  *gindex = CCTK_GroupIndexFromVarI(*var);
}



 /*@@
   @routine    CCTK_GroupIndexFromVar
   @date       Mon Feb 22
   @author     Gabrielle Allen
   @desc 
   Given a variable name returns a group index.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int CCTK_GroupIndexFromVar(const char *var)
{
  return CCTK_GroupIndexFromVarI(CCTK_VarIndex(var));
}

void  FMODIFIER FORTRAN_NAME(CCTK_GroupIndexFromVar)(int *index,ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE(var)
  *index = CCTK_GroupIndexFromVar(var);
  free(var);
}


 /*@@
   @routine    CCTK_ImpFromVarI
   @date       Mon Feb 22
   @author     Gabrielle Allen
   @desc 
   Given a variable index return a implementation name.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
char *CCTK_ImpFromVarI(int var)
{
  char *retval;
  int group_num;

  if (var<0 || var>total_variables-1)
  {
    retval = NULL;
  }
  else
  {
    group_num = group_of_variable[var];
    retval = groups[group_num].implementation;
  }

  return retval;
}


 /*@@
   @routine    CCTK_FullName
   @date       Mon Feb 22
   @author     Gabrielle Allen
   @desc 
   Given a variable index return the implementation 
   and the variable name together.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

char *CCTK_FullName(int var)
{
  char *impname;
  char *varname;
  int group_num;
  char *fullname=NULL;

  varname = CCTK_VarName(var);
  if (varname)
  {
    group_num = group_of_variable[var];
    impname = groups[group_num].implementation;

    fullname = malloc((strlen(varname)+strlen(impname)+3)*sizeof(char));
    if (fullname)
      sprintf(fullname,"%s::%s",impname,varname);
  }
  else
  {  
    fullname = NULL;
  }

  return fullname;
}


 /*@@
   @routine    CCTKi_GroupTypeNumber
   @date       Mon Feb  8 14:44:45 1999
   @author     Tom Goodale
   @desc 
   Gets the type number associated with a group.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int CCTKi_GroupTypeNumber(const char *type)
{
  int retval=-1;

  if(!strcmp(type, "SCALAR"))
  {
    retval = GROUP_SCALAR;
  }

  if(!strcmp(type, "GF"))
  {
    retval = GROUP_GF;
  }

  if(!strcmp(type, "ARRAY"))
  {
    retval = GROUP_ARRAY;
  }

  return retval;
}

void  FMODIFIER FORTRAN_NAME(CCTKi_GroupTypeNumber)(int *number,ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE(type)
  *number = CCTKi_GroupTypeNumber(type);
  free(type);
}


 /*@@
   @routine    CCTKi_VarTypeNumber
   @date       Mon Feb  8 14:44:45 1999
   @author     Tom Goodale
   @desc 
   Gets the type number associated with a variable.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int CCTKi_VarTypeNumber(const char *type)
{
  int retval=-1;

  if(!strcmp(type, "INT"))
  {
    retval = CCTK_VARIABLE_INT;
  }

  if(!strcmp(type, "REAL"))
  {
    retval = CCTK_VARIABLE_REAL;
  }

  if(!strcmp(type, "COMPLEX"))
  {
    retval = CCTK_VARIABLE_COMPLEX;
  }

  if(!strcmp(type, "CHAR"))
  {
    retval = CCTK_VARIABLE_CHAR;
  }

  return retval;
}

void  FMODIFIER FORTRAN_NAME(CCTKi_VarTypeNumber)(int *number,ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE(type)
  *number = CCTKi_VarTypeNumber(type);
  free(type);
}


 /*@@
   @routine    CCTKi_GroupScopeNumber
   @date       Tuesday June 22 1999
   @author     Gabrielle Allen
   @desc 
   Gets the scope number associated with a group.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int CCTKi_GroupScopeNumber(const char *type)
{
  int retval=-1;

  if(!strcmp(type, "PRIVATE"))
  {
    retval = GROUP_PRIVATE;
  }

  if(!strcmp(type, "PROTECTED"))
  {
    retval = GROUP_PROTECTED;
  }

  if(!strcmp(type, "PUBLIC"))
  {
    retval = GROUP_PUBLIC;
  }

  return retval;
}

void  FMODIFIER FORTRAN_NAME(CCTKi_GroupScopeNumber)(int *number,ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE(type)
  *number = CCTKi_GroupScopeNumber(type);
  free(type);
}


 /*@@
   @routine    CCTK_GroupData
   @date       Mon Feb  8 15:56:01 1999
   @author     Tom Goodale
   @desc 
   Gets the group type, the variable type, and the number of variables
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int CCTK_GroupData(int group, 
		      int *gtype, 
		      int *vtype, 
		      int *dim, 
		      int *n_variables,
		      int *n_timelevels)
{
  int return_code;

  if(group >=0 && group < n_groups)
  {
    *gtype = groups[group].gtype;
    *vtype = groups[group].vtype;
    *dim   = groups[group].dim;
    *n_variables = groups[group].n_variables;
    *n_timelevels = groups[group].n_timelevels;

    return_code = 1;
  }
  else
  {
    return_code = 0;
  }

  return return_code;
}


 /*@@
   @routine    CCTK_VarName
   @date       Tue Feb  9 15:34:56 1999
   @author     Tom Goodale
   @desc 
   Gets the name of a variable.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

char *CCTK_VarName(int varnum)
{
  char *name;
  int group;

  if (varnum<0 || varnum>total_variables-1)
  {
    name = NULL;
  }
  else
  {
    group = group_of_variable[varnum];
    name  = groups[group].variables[varnum-groups[group].variables[0].number].name;
  }

  return name;
}


 /*@@
   @routine    CCTK_DecomposeName
   @date       Tue Feb  9 15:39:14 1999
   @author     Tom Goodale
   @desc 
   Decomposes a group name of the form imp::name
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int CCTK_DecomposeName(const char *fullname, char **implementation, char **name)
{
  return Util_SplitString(implementation, name, fullname, "::");
}


 /*@@
   @routine    CCTK_GroupName
   @date       Tue Apr  9 15:39:14 1999
   @author     Gabrielle Allen
   @desc 
   Given a group index returns the group name
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

char *CCTK_GroupName(int group)
{
  char *name;

  if (group < 0 || group >= n_groups) 
  {
    name = NULL;
  }
  else
  {
    name = (char *)malloc((strlen(groups[group].implementation)
			   +strlen(groups[group].name)+3)*sizeof(char));
    if (name)
    {
      sprintf(name, "%s::%s",groups[group].implementation, groups[group].name);
    }
    else
    {
      name = NULL;
    }
  }

  return name;
}


 /*@@
   @routine    CCTK_FirstVarIndexI
   @date       
   @author     Gabrielle Allen
   @desc 
   Given a group index returns the first variable index in the group
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int CCTK_FirstVarIndexI(int group)
{
  if (0 <= group && group<n_groups)
  {
    return groups[group].variables[0].number;
  }
  else
  {
    return -1;
  }
}

void  FMODIFIER FORTRAN_NAME(CCTK_FirstVarIndexI)(int *first, int *group)
{
  *first = CCTK_FirstVarIndexI(*group);
}


int CCTK_FirstVarIndex(const char *groupname)
{
  return CCTK_FirstVarIndexI(CCTK_GroupIndex(groupname));
}


int CCTK_NumVarsInGroupI(int group)
{
  if (0 <= group && group<n_groups)
  {
    return groups[group].n_variables;
  }
  else
  {
    return -1;
  }
}


 /*@@
   @routine    CCTK_NumVarsInGroup
   @date       
   @author     Gabrielle Allen
   @desc 
   Given a group name returns the number of variables in the group
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int CCTK_NumVarsInGroup(const char *groupname)
{
  return CCTK_NumVarsInGroupI(CCTK_GroupIndex(groupname));
}

void  FMODIFIER FORTRAN_NAME(CCTK_NumVarsInGroup)(int *num,ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE(groupname)
  *num = CCTK_NumVarsInGroup(groupname);
  free(groupname);
}


 /*@@
   @routine    CCTKi_GroupTypeFromVarI
   @date       
   @author     
   @desc 
   Given a variable index return the type of group
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int CCTK_GroupTypeFromVarI(int var)
{
  int gtype;
  int group;

  if (var<0 || var>total_variables-1)
  {
    gtype = -1;
  }
  else
  {
    group = group_of_variable[var];

    gtype = groups[group].gtype;
  }

  return gtype;
}


 /*@@
   @routine    CCTK_VarTypeI
   @date       
   @author     
   @desc 
   Given a variable index return the variable type
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int CCTK_VarTypeI(int var)
{
  int vtype;
  int group;

  if (var<0 || var>total_variables-1)
  {
    vtype = -1;
  }
  else
  {
    group = group_of_variable[var];

    vtype = groups[group].vtype;
  }

  return vtype;
}

void  FMODIFIER FORTRAN_NAME(CCTK_VarTypeI)(int *type,int *var)
{
  *type = CCTK_VarTypeI(*var);
}


 /*@@
   @routine    CCTK_NumTimeLevelsI
   @date       
   @author     
   @desc 
   Given a variable index return the number of timelevels
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int CCTK_NumTimeLevelsFromVarI(int var)
{
  int ntimelevels;
  int group;

  if (var<0 || var>total_variables-1)
  {
    ntimelevels = -1;
  }
  else
  {
    group = group_of_variable[var];
    ntimelevels = groups[group].n_timelevels;
  }
  
  return ntimelevels;
}
  
void  FMODIFIER FORTRAN_NAME(CCTK_NumTimeLevelsFromVarI)(int *num,int *var)
{
  *num = CCTK_NumTimeLevelsFromVarI(*var);
}


 /*@@
   @routine    CCTK_NumTimeLevelsFromVar
   @date       
   @author     
   @desc 
   Given a variable name return the number of timelevels
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int CCTK_NumTimeLevelsFromVar(const char *var)
{
  return CCTK_NumTimeLevelsFromVarI(CCTK_VarIndex(var));
}

void  FMODIFIER FORTRAN_NAME(CCTK_NumTimeLevelsFromVar)(int *num,ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE(var)
  *num = CCTK_NumTimeLevelsFromVar(var);
  free(var);
}


 /*@@
   @routine    CCTK_PrintGroup
   @date       3 July 1999
   @author     Gabrielle Allen
   @desc 
   Given a group index print the group name. This is for debugging
   purposes for Fortran routines.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

void CCTK_PrintGroup(int group)
{
  fprintf(stdout,"Group %d is %s\n",group,CCTK_GroupName(group));
}

void  FMODIFIER FORTRAN_NAME(CCTK_PrintGroup)(int *group)
{
  CCTK_PrintGroup(*group);
}


 /*@@
   @routine    CCTK_PrintVar
   @date       3 July 1999
   @author     Gabrielle Allen
   @desc 
   Given a group index print the variable name. This is for debugging
   purposes for Fortran.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

void CCTK_PrintVar(int var)
{
  fprintf(stdout,"Variable %d is %s\n",var,CCTK_VarName(var));
}

void  FMODIFIER FORTRAN_NAME(CCTK_PrintVar)(int *var)
{
  CCTK_PrintGroup(*var);
}