aboutsummaryrefslogtreecommitdiff
path: root/src/Utils.c
blob: 12d0af8cee0d566d2ae711d70852c8745fb4775e (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
 /*@@
   @file      Utils.c
   @date      Tue 4th July 2000
   @author    Gabrielle Allen
   @desc 
   Utility routines which may be called by other IO thorns
   @enddesc 
   @history
   @endhistory
   @version $Header$
 @@*/

/*#define DEBUG_IOUTIL*/

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

static char *rcsid = "$Header$";

CCTK_FILEVERSION(CactusBase_IOUtils_Utils_c)

/********************************************************************
 *********************     External Routines   **********************
 ********************************************************************/

 /*@@
   @routine    IOUtil_1DLines
   @date       July 4 2000
   @author     Gabrielle Allen, Gerd Lanfermann, Thomas Radke
   @desc 
   Fills out an array determining where to position 1D lines for output 
   on a multidimensional regular Cartesian unigrid.
   The first slot of the array specifies the 1D line direction, the
   second slot fixes the indices of the starting point of that line on the
   grid.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int IOUtil_1DLines (cGH *GH, 
		    int dimension, 
		    int **index, 
		    CCTK_REAL **coord, 
		    int **spxyz)
{
  DECLARE_CCTK_PARAMETERS

  int ierr=0;
  int dim, dir;
  CCTK_REAL lower,upper;
  const char *name=NULL;
  CCTK_REAL lowercoords[3]={0,0,0};
  CCTK_REAL uppercoords[3]={0,0,0};

  /* Get the appropriate coordinate system */
  switch (dimension)
  {
    case 1:
      name = "cart1d";
      ierr = CCTK_CoordRange(GH, &lower, &upper, 1, NULL, name);
      if (ierr>-1)
      {
	lowercoords[0] = lower;
	uppercoords[0] = upper;
      }
      break;
    case 2:
      name = "cart2d";
      ierr = CCTK_CoordRange(GH, &lower, &upper, 1, NULL, name);
      if (ierr>-1)
      {
	lowercoords[0] = lower;
	uppercoords[0] = upper;
      }
      ierr = CCTK_CoordRange(GH, &lower, &upper, 2, NULL, name);
      if (ierr>-1)
      {
	lowercoords[1] = lower;
	uppercoords[1] = upper;
      }
      break;
    case 3:
      name = "cart3d";
      ierr = CCTK_CoordRange(GH, &lower, &upper, 1, NULL, name);
      if (ierr>-1)
      {
	lowercoords[0] = lower;
	uppercoords[0] = upper;
      }
      ierr = CCTK_CoordRange(GH, &lower, &upper, 2, NULL, name);
      if (ierr>-1)
      {
	lowercoords[1] = lower;
	uppercoords[1] = upper;
      }
      ierr = CCTK_CoordRange(GH, &lower, &upper, 3, NULL, name);
      if (ierr>-1)
      {
	lowercoords[2] = lower;
	uppercoords[2] = upper;
      }
      break;
    default:
      CCTK_VWarn(4,__LINE__,__FILE__,"IOASCII",
		 "IOUtil_1DLines: Support only for dim<=3");
      ierr = -1;
      break;
  }

  for (dir = 0; dir < dimension; dir++)
  {
    for (dim = 0; dim < dimension; dim++)
    {
      if (dim == dir)
      {
	spxyz[dir][dim] = 0;
      }
      else if (index && index[dir][dim]>-1)
      {
	spxyz[dir][dim] = index[dir][dim];
      }
      else if (ierr < 0)
      {
	CCTK_VWarn(4, __LINE__,__FILE__,"IOASCII",
		   "IOUtil_1DLines: Cartesian coordinate system %s not found",
		   name);
	spxyz[dir][dim] = 0;
      }
      else if (lowercoords[dim]>coord[dir][dim] || 
	       uppercoords[dim]<coord[dir][dim])
      {
	CCTK_VWarn(2,__LINE__,__FILE__,"IOUtil",
		   "IOUtil_1DLines: Coordinate in direction %d (%f,%f)"
		   " doesn't contain %f",
		   dim,lowercoords[dim],uppercoords[dim],coord[dir][dim]);
	spxyz[dir][dim] = 0;
      }
      else 
      {
	/* Find index for first point above the chosen coordinate */
	spxyz[dir][dim] = 0.5-(lowercoords[dim]-coord[dir][dim])/
	  (GH->cctk_delta_space[dim]);
	
#ifdef DEBUG_IOUTIL
	printf("spxyz for %d-coord of lines in %d-direction is %d\n",
	       dim,dir,spxyz[dir][dim]);
#endif
      }
    }
  }

  return 0;
}



int IOUtil_2DPlanes (cGH *GH, 
		     int dimension, 
		     int *index, 
		     CCTK_REAL *coord, 
		     int *sp2xyz)
{
  DECLARE_CCTK_PARAMETERS

  int ierr=0;
  int dir;
  CCTK_REAL lower,upper;
  const char *name=NULL;
  CCTK_REAL lowercoords[3]={0,0,0};
  CCTK_REAL uppercoords[3]={0,0,0};

  /* Get the appropriate coordinate system */
  /* Get the appropriate coordinate system */
  switch (dimension)
  {
    case 1:
      name = "cart1d";
      ierr = CCTK_CoordRange(GH, &lower, &upper, 1, NULL, name);
      if (ierr>-1)
      {
	lowercoords[0] = lower;
	uppercoords[0] = upper;
      }
      break;
    case 2:
      name = "cart2d";
      ierr = CCTK_CoordRange(GH, &lower, &upper, 1, NULL, name);
      if (ierr>-1)
      {
	lowercoords[0] = lower;
	uppercoords[0] = upper;
      }
      ierr = CCTK_CoordRange(GH, &lower, &upper, 2, NULL, name);
      if (ierr>-1)
      {
	lowercoords[1] = lower;
	uppercoords[1] = upper;
      }
      break;
    case 3:
      name = "cart3d";
      ierr = CCTK_CoordRange(GH, &lower, &upper, 1, NULL, name);
      if (ierr>-1)
      {
	lowercoords[0] = lower;
	uppercoords[0] = upper;
      }
      ierr = CCTK_CoordRange(GH, &lower, &upper, 2, NULL, name);
      if (ierr>-1)
      {
	lowercoords[1] = lower;
	uppercoords[1] = upper;
      }
      ierr = CCTK_CoordRange(GH, &lower, &upper, 3, NULL, name);
      if (ierr>-1)
      {
	lowercoords[2] = lower;
	uppercoords[2] = upper;
      }
      break;
    default:
      CCTK_VWarn(4,__LINE__,__FILE__,"IOASCII",
		 "IOUtil_2DPlanes: Support only for dim<=3");
      ierr = -1;
      break;
  }


  for (dir = 0; dir < dimension; dir++)
  {
    if (index && index[dir]>-1)
    {
      sp2xyz[dir] = index[dir];
    }
    else if (ierr < 0) 
    {
      CCTK_VWarn(4, __LINE__,__FILE__,"IOUtil",
		 "IOUtil_2DPlanes: Cartesian coordinate system %s not found",
		 name);
      sp2xyz[dir] = 0;
    }
    else if (lowercoords[dir]>coord[dir] || 
	     uppercoords[dir]<coord[dir])
    {
      CCTK_VWarn(2,__LINE__,__FILE__,"IOUtil",
		 "IOUtil_2DPlanes: Coordinate in direction %d (%lf,%lf)"
		 " doesn't contain %f",
		 dir,lowercoords[dir],uppercoords[dir],coord[dir]);
      sp2xyz[dir] = 0;
    }
    else
    {
      /* Find index for first point above the chosen coordinate */
      sp2xyz[dir] = 0.5-(lowercoords[dir]-coord[dir])/
	(GH->cctk_delta_space[dir]);
      
#ifdef DEBUG_IOUTIL
      printf("sp2xyz for planes perpendicular to"
	     " %d-direction is %d\n",
	     dir,sp2xyz[dir]);
#endif
    }
  }

  return 0;

}