aboutsummaryrefslogtreecommitdiff
path: root/archive/Comparison.c
blob: 5add331cbce0668eb29e8f3ecc0650f159fb8a10 (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
/* Please god comment me ... */
/* $Header$ */

#include "cctk.h"

static int comp_initialized = 0;
static int comp_active = -1;

static int comp_metric = 0;
static int comp_curv   = 0;
static int comp_gauge  = 0;

#define f_comparisonmetric FORTRAN_NAME(comparisonmetric_,COMPARISONMETRIC,comparisonmetric)
void FMODIFIER f_comparisonmetric(FORT_ARGS_PROTO, PROTO_FORT_FIELDS,
				  double *,
				  double *,
				  double *,
				  double *,
				  double *,
				  double *);
#define f_comparisoncurvature FORTRAN_NAME(comparisoncurvature_,COMPARISONCURVATURE,comparisoncurvature)
void FMODIFIER f_comparisoncurvature(FORT_ARGS_PROTO, PROTO_FORT_FIELDS,
				  double *,
				  double *,
				  double *,
				  double *,
				  double *,
				  double *);
#define f_comparisongauge FORTRAN_NAME(comparisongauge_,COMPARISONGAUGE,comparisongauge)
void FMODIFIER f_comparisongauge(FORT_ARGS_PROTO, PROTO_FORT_FIELDS,
				  double *,
				  double *,
				  double *,
				  double *);

static int comptrips=1;

void Comparison(pGH *in) {
  void DoComparison(pGH *high, pGH *med, pGH *low,
		    int gfi, double *he, double *me, double *le);

  int i,j,k;
  double *h[6];
  double *m[6];
  double *l[6];

  int compevery = 1;
  
  pGH *high, *med, *low;

  /* Grab the approriate grid heirarchies */
  if (in->convlevel != 0) return;

  high = in;
  med = GetGHbyLevel(1,in->level, in->mglevel);
  low = GetGHbyLevel(2,in->level, in->mglevel);

  /* Only compare when grids are aligned. */
  if (med) compevery = compevery * 2;
  if (low) compevery = compevery * 2;

  if (iteration % compevery != 0) return;

  /* See if I'm active */
  if (comp_active < 0) {
    comp_active = Contains("comparison","yes");
  }
  if (!comp_active) return;

  if (!comp_initialized) {
    pGF *tGF;
    comp_initialized =1;
    for (i=0; i<high->ngridFuncs;i++) {
      tGF = high->gridFuncs[i];
      if (Contains_bounded("compfields",tGF->name)) {
	tGF->do_comparison = 1;
	if (tGF->gfno >= GFI_GXX &&
	    tGF->gfno <= GFI_GZZ)
	  comp_metric = 1;

	if (tGF->gfno >= GFI_HXX &&
	    tGF->gfno <= GFI_HZZ)
	  comp_curv = 1;

	if (tGF->gfno == GFI_ALP   ||
	    tGF->gfno == GFI_BETAX ||
	    tGF->gfno == GFI_BETAY ||
	    tGF->gfno == GFI_BETAZ)
	  comp_gauge = 1;
      }
    }
  }
  if (!(comp_metric || comp_gauge || comp_curv)) {
    comp_active = 0;
    printf ("You must compare gauge, curvature, or metric");
    return;
  }

  /* OK at this point we know we are going to do something, so
     allocate the memory for the comparisons */
  for (i=0;i<6;i++) {
    h[i] = (double *)malloc(high->npoints*sizeof(double));
    if (med)
      m[i] = (double *)malloc( med->npoints*sizeof(double));
    else
      m[i] = NULL;
    if (low) 
      l[i] = (double *)malloc( low->npoints*sizeof(double));
    else
      l[i] = NULL;
  }

  EnableGFDataStorage(high, high->gridFuncs[high->ngridFuncs-1]);
  if (med) 
    EnableGFDataStorage( med,  med->gridFuncs[med->ngridFuncs-1]);
  if (low) 
    EnableGFDataStorage( low,  low->gridFuncs[low->ngridFuncs-1]);

  if (comp_metric) {
    SetupFortranArrays(high);
    f_comparisonmetric(FORT_ARGS(high), PASS_FORT_FIELDS(high),
		       h[0],h[1],h[2],h[3],h[4],h[5]);

    if (med) {
      SetupFortranArrays(med);
      f_comparisonmetric(FORT_ARGS(med), PASS_FORT_FIELDS(med),
			 m[0],m[1],m[2],m[3],m[4],m[5]);
    }
    
    if (low) {
      SetupFortranArrays(low);
      f_comparisonmetric(FORT_ARGS(low), PASS_FORT_FIELDS(low),
			 l[0],l[1],l[2],l[3],l[4],l[5]);
    }

    for (i=GFI_GXX; i<=GFI_GZZ;i++) {
      if (high->gridFuncs[i]->do_comparison) {
	DoComparison(high,med,low,i,
		     h[i-GFI_GXX],m[i-GFI_GXX],l[i-GFI_GXX]);
      }
    }
  }

  if (comp_curv) {
    SetupFortranArrays(high);
    f_comparisoncurvature(FORT_ARGS(high), PASS_FORT_FIELDS(high),
		       h[0],h[1],h[2],h[3],h[4],h[5]);

    if (med) {
      SetupFortranArrays(med);
      f_comparisoncurvature(FORT_ARGS(med), PASS_FORT_FIELDS(med),
			    m[0],m[1],m[2],m[3],m[4],m[5]);
    } 

    if (low) {
      SetupFortranArrays(low);
      f_comparisoncurvature(FORT_ARGS(low), PASS_FORT_FIELDS(low),
			    l[0],l[1],l[2],l[3],l[4],l[5]);
    }

    for (i=GFI_HXX; i<=GFI_HZZ;i++) {
      if (high->gridFuncs[i]->do_comparison) {
	DoComparison(high,med,low,i,
		     h[i-GFI_HXX],m[i-GFI_HXX],l[i-GFI_HXX]);
      }
    }
  }

  if (comp_gauge) {
    SetupFortranArrays(high);
    f_comparisongauge(FORT_ARGS(high), PASS_FORT_FIELDS(high),
		       h[0],h[1],h[2],h[3]);

    if (med) {
      SetupFortranArrays(med);
      f_comparisongauge(FORT_ARGS(med), PASS_FORT_FIELDS(med),
			m[0],m[1],m[2],m[3]);
    }

    if (low) {
      SetupFortranArrays(low);
      f_comparisongauge(FORT_ARGS(low), PASS_FORT_FIELDS(low),
			l[0],l[1],l[2],l[3]);
    }

    if (high->gridFuncs[GFI_ALP]->do_comparison) {
      DoComparison(high,med,low,GFI_ALP,h[0],m[0],l[0]);
    }

    if (high->gridFuncs[GFI_BETAX]->do_comparison) {
      DoComparison(high,med,low,GFI_BETAX,h[1],m[1],l[1]);
    }

    if (high->gridFuncs[GFI_BETAY]->do_comparison) {
      DoComparison(high,med,low,GFI_BETAY,h[2],m[2],l[2]);
    }

    if (high->gridFuncs[GFI_BETAZ]->do_comparison) {
      DoComparison(high,med,low,GFI_BETAZ,h[3],m[3],l[3]);
    }
  }

  for (i=0;i<6;i++) {
    free(h[i]); if (m[i]) free(m[i]); if (l[i]) free(l[i]);
  }
  DisableGFDataStorage(high, high->gridFuncs[high->ngridFuncs-1]);
  if (med)
    DisableGFDataStorage( med,  med->gridFuncs[med->ngridFuncs-1]);
  if (low)
    DisableGFDataStorage( low,  low->gridFuncs[low->ngridFuncs-1]);
  comptrips ++ ;
}

void DoComparison(pGH *high, pGH *med, pGH *low, int gfi,
		  double *he, double *me, double *le) {

  pGF *ws, *cur;
  double max[3], nm1[3], nm2[3];
  double sigtop, sigbot, sig3w, sig10, sig21;

  int i;

  printf ("Comparing %s\n",high->gridFuncs[gfi]->name);

  /* Output the analytic solution if we need it */
  cur = high->gridFuncs[gfi]; ws = high->gridFuncs[high->ngridFuncs-1];
  if (cur->do_1dio) {
    ws->do_1dio = comptrips;
    for (i=0;i<high->npoints;i++) 
      ws->data[i] = he[i];
    sprintf(ws->name,"%s_exact",cur->name);
    IO_Write1D(high,ws);
    ws->do_1dio = 0;
    ws->lastio_it[1]--;
  }

  /* Diffs go into workspace */
  for (i=0;i<high->npoints;i++) 
    ws->data[i] = cur->data[i] - he[i];

  if (cur->do_1dio) {
    ws->do_1dio = comptrips;
    sprintf(ws->name,"%s_diff",cur->name);
    IO_Write1D(high,ws);
    ws->do_1dio = 0;
    ws->lastio_it[1]--;
  }

  max[0] = pGF_MaxVal(high,ws);
  nm1[0] = pGF_Norm1 (high,ws);
  nm2[0] = pGF_Norm2 (high,ws);

  if (med) {
    cur = med->gridFuncs[gfi]; ws = med->gridFuncs[med->ngridFuncs-1];
    for (i=0;i<med->npoints;i++)
      ws->data[i] = cur->data[i] - me[i];
    if (cur->do_1dio) {
      ws->do_1dio = comptrips;
      sprintf(ws->name,"%s_diff",cur->name);
      IO_Write1D(med,ws);
      ws->do_1dio = 0;    
      ws->lastio_it[1]--;
    }

    max[1] = pGF_MaxVal(med,ws);
    nm1[1] = pGF_Norm1 (med,ws);
    nm2[1] = pGF_Norm2 (med,ws);
  }

  if (low) {
    cur = low->gridFuncs[gfi]; ws = low->gridFuncs[low->ngridFuncs-1];
    for (i=0;i<low->npoints;i++)
      ws->data[i] = cur->data[i] - le[i];
    if (cur->do_1dio) {
      ws->do_1dio = comptrips;
      sprintf(ws->name,"%s_diff",cur->name);
      IO_Write1D(low,ws);
      ws->do_1dio = 0;
      ws->lastio_it[1]--;
    }
    
    max[2] = pGF_MaxVal(low,ws);
    nm1[2] = pGF_Norm1 (low,ws);
    nm2[2] = pGF_Norm2 (low,ws);
  } else {
    max[2] = 0.0; nm1[2] = 0.0; nm2[2] = 0.0;
  }

  if (low) {
    printf (" ------ :   High     Med       Low\n");
    printf ("Max Diff: %lf %lf %lf\n",
	    max[0],max[1],max[2]);
    printf ("NM1 Diff: %lf %lf %lf\n",
	  nm1[0],nm1[1],nm1[2]);
    printf ("NM2 Diff: %lf %lf %lf\n",
	    nm2[0],nm2[1],nm2[2]);
  } else {
    if (med) {
      printf (" ------ :   High     Med      \n");
      printf ("Max Diff: %lf %lf\n",
	      max[0],max[1]);
      printf ("NM1 Diff: %lf %lf\n",
	      nm1[0],nm1[1]);
      printf ("NM2 Diff: %lf %lf\n",
	      nm2[0],nm2[1]);
    }
  }


  if (low) {
    sigbot = nm2[0] - nm2[1];
    if (sigbot == 0) sig3w = 0.0;
    else sig3w = log(fabs((nm2[1]-nm2[2])/(nm2[0]-nm2[1])))/log(2.0);

    if (nm2[1] == 0) sig21 = 0.0;
    else sig21 = log(fabs(nm2[2]/nm2[1]))/log(2.0);
  } else {
    sig3w = 0.0; sig21 = 0;
  }
    
  if (nm2[0] == 0) sig10 = 0.0;
  else sig10 = log(fabs(nm2[1]/nm2[0]))/log(2.0);

  if (low)
    printf ("sigma: 3way %lf  hm %lf  ml %lf\n",
	    sig3w, sig10, sig21);
  else
    if (med)
      printf ("sigma: hm %lf\n",sig10);
  
}