File: cube.c

package info (click to toggle)
plotmtv 1.4.1-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 4,024 kB
  • ctags: 5,006
  • sloc: ansic: 51,179; makefile: 1,976; fortran: 1,277; sh: 510; csh: 439
file content (460 lines) | stat: -rw-r--r-- 9,802 bytes parent folder | download | duplicates (6)
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
/*
 * cube.c - linked list procedures building and maintaining lists
 *          of cubes 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include "CNplot.h"

static void print_cube();
static void print_block();

/*
 * Query functions
 */

/*
 * Is the cube divided into prisms?
 */
int CNcube_is_divided(cube)
CNcubeptr cube;
{
   int divided = CN_FALSE;

   /* Check 1st 3 bits */
   if ( ((cube->pcode & CN_XPRISM) != 0) ||
        ((cube->pcode & CN_YPRISM) != 0) ||
        ((cube->pcode & CN_ZPRISM) != 0) )
      divided = CN_TRUE;

   /* Check the materials just to make sure */
   if (divided)
      if (cube->mat1 == cube->mat2) divided = CN_FALSE;

   /* Return */
   return(divided);
}

/*
 * Is the xmin face of the cube an electrode?
 */
int CNcube_xmin_is_electrode(cube)
CNcubeptr cube;
{
   int electrode = CN_FALSE;

   /* Check the relevant bit */
   if ((cube->pcode & CN_XMINELECT) != 0) 
      electrode = CN_TRUE;

   /* Return */
   return(electrode);
}

/*
 * Is the xmax face of the cube an electrode?
 */
int CNcube_xmax_is_electrode(cube)
CNcubeptr cube;
{
   int electrode = CN_FALSE;

   /* Check the relevant bit */
   if ((cube->pcode & CN_XMAXELECT) != 0) 
      electrode = CN_TRUE;

   /* Return */
   return(electrode);
}

/*
 * Is the ymin face of the cube an electrode?
 */
int CNcube_ymin_is_electrode(cube)
CNcubeptr cube;
{
   int electrode = CN_FALSE;

   /* Check the relevant bit */
   if ((cube->pcode & CN_YMINELECT) != 0) 
      electrode = CN_TRUE;

   /* Return */
   return(electrode);
}

/*
 * Is the ymax face of the cube an electrode?
 */
int CNcube_ymax_is_electrode(cube)
CNcubeptr cube;
{
   int electrode = CN_FALSE;

   /* Check the relevant bit */
   if ((cube->pcode & CN_YMAXELECT) != 0) 
      electrode = CN_TRUE;

   /* Return */
   return(electrode);
}

/*
 * Is the zmin face of the cube an electrode?
 */
int CNcube_zmin_is_electrode(cube)
CNcubeptr cube;
{
   int electrode = CN_FALSE;

   /* Check the relevant bit */
   if ((cube->pcode & CN_ZMINELECT) != 0) 
      electrode = CN_TRUE;

   /* Return */
   return(electrode);
}

/*
 * Is the zmax face of the cube an electrode?
 */
int CNcube_zmax_is_electrode(cube)
CNcubeptr cube;
{
   int electrode = CN_FALSE;

   /* Check the relevant bit */
   if ((cube->pcode & CN_ZMAXELECT) != 0) 
      electrode = CN_TRUE;

   /* Return */
   return(electrode);
}

/*
 * Is the diag face of the cube an electrode?
 */
int CNcube_diag_is_electrode(cube)
CNcubeptr cube;
{
   int electrode = CN_FALSE;

   /* Check the relevant bit */
   if ((cube->pcode & CN_DIAGELECT) != 0) 
      electrode = CN_TRUE;

   /* Return */
   return(electrode);
}


/*
 * CUBE DATA STRUCTURE
 */

/*
 * A cube is used to represent a geometrical block structure.
 * Internally, the cube could be divided into 2 prisms where
 * the cut is parallel to the x, y, or z axis.
 */

/*
 * Allocate room for a cube
 */
CNcubeptr CNmake_cube(x0, x1, y0, y1, z0, z1,
                      mat1,mat2,prism_code,ID)
double x0, x1, y0, y1, z0, z1;
short  mat1,mat2;
int    prism_code;
short  ID;
{
   CNcubeptr newptr;
   unsigned int size = sizeof(CNcube);

   if ((newptr = (CNcubeptr)malloc(size))!=NULL) {
      newptr->ID      = ID;
      newptr->mat1    = mat1;
      newptr->mat2    = mat2;
      newptr->pcode   = prism_code;
      newptr->zave    = 0.0;
      newptr->x0      = x0;
      newptr->x1      = x1;
      newptr->y0      = y0;
      newptr->y1      = y1;
      newptr->z0      = z0;
      newptr->z1      = z1;
      newptr->x0_o    = x0;
      newptr->x1_o    = x1;
      newptr->y0_o    = y0;
      newptr->y1_o    = y1;
      newptr->z0_o    = z0;
      newptr->z1_o    = z1;
      newptr->next    = NULL;
      newptr->prev    = NULL;
   }
   return(newptr);
}

/*  
 * Insert a cube at the tail of the current cube list
 */
CNcubeptr CNinsert_cube(cube_listhead, cube_listtail, 
                        x0, x1, y0, y1, z0, z1,
                        mat1,mat2,prism_code,ID)
CNcubeptr *cube_listhead, *cube_listtail;
double    x0, x1, y0, y1, z0, z1;
short     mat1,mat2;
int       prism_code;
short     ID;
{
   CNcubeptr CNmake_cube();
   CNcubeptr next,A,B;

   A = *cube_listtail;
   if ((B=CNmake_cube(x0, x1, y0, y1, z0, z1,
                      mat1,mat2,prism_code,ID))!=NULL) {
      if (A==NULL) {
         *cube_listhead = B;
         *cube_listtail = B;
      } else {
         next = A->next;
         B->next = next;
         B->prev = A;
         A->next = B;
         if (next != NULL) next->prev = B;
         if (B->next == NULL) *cube_listtail = B;
      }
   }
   return(B);
}


/*
 * Delete cube at address L 
 */
void CNdelete_cube(cube_listhead, cube_listtail, L)
CNcubeptr *cube_listhead, *cube_listtail;
CNcubeptr L;
{
   CNcubeptr prev,next;

   prev = L->prev;
   next = L->next;
   if (prev!=NULL) prev->next = next;
   if (next!=NULL) next->prev = prev;
   if (L== *cube_listhead) *cube_listhead = next;
   if (L== *cube_listtail) *cube_listtail = prev;
   free ((char*)L);
   L = NULL;
}


/* 
 * Delete all the cubes in the list
 */
void CNdelete_cube_list(cube_listhead, cube_listtail)
CNcubeptr *cube_listhead, *cube_listtail;
{
   CNcubeptr C;

   while ((C = *cube_listhead) != NULL)
      CNdelete_cube(cube_listhead, cube_listtail, C);
}


/* 
 * Print out the list of cubes 
 */
/*ARGSUSED*/
void CNprint_cube_list(cube_listhead, cube_listtail)
CNcubeptr cube_listhead, cube_listtail;
{
   CNcubeptr C;

   for (C=cube_listhead; C!=NULL; C=C->next) 
      print_cube(C);
}


/* 
 * print the coordinates of a cube
 */
static void print_cube(C)
CNcubeptr C;
{
   (void) fprintf(stdout,"CubeID=%d\n",C->ID);
   (void) fprintf(stdout,"   x0=%8.5g x1=%8.5g\n",C->x0, C->x1);
   (void) fprintf(stdout,"   y0=%8.5g y1=%8.5g\n",C->y0, C->y1);
   (void) fprintf(stdout,"   z0=%8.5g z1=%8.5g\n",C->z0, C->z1);
}


/*
 * Count the number of cubes in the list 
 */
/*ARGSUSED*/
int CNcount_cubes(cube_listhead, cube_listtail)
CNcubeptr cube_listhead, cube_listtail;
{
   CNcubeptr C;
   int       count = 0;

   for (C=cube_listhead; C!=NULL; C=C->next) count++;

   return(count);
}


/*
 * BLOCK DATA STRUCTURE
 */

/*
 * A block is used to represent 4D data, that is, values in 3D space
 * on each of the vertices of the cube.  Internally, the block could
 * be divided into 2 prisms where the cut is parallel to the x, y, or z axis.
 * The block refers to the cube for its geometry.
 * Multiple blocks could point to the same cube.
 * Thus the block-cube relation is analogous to the node-point relationship.
 */

/*
 * Allocate room for a block
 */
CNblockptr CNmake_block(cube,t000,t001,t010,t011,t100,t101,t110,t111,ID)
CNcubeptr cube;
double    t000,t001,t010,t011,t100,t101,t110,t111;
int       ID;
{
   CNblockptr newptr;
   unsigned int size = sizeof(CNblock);

   if ((newptr = (CNblockptr)malloc(size))!=NULL) {
      newptr->ID     = ID;
      newptr->cube   = cube;
      newptr->zave   = 0.0;
      newptr->t000   = t000;
      newptr->t001   = t001;
      newptr->t010   = t010;
      newptr->t011   = t011;
      newptr->t100   = t100;
      newptr->t101   = t101;
      newptr->t110   = t110;
      newptr->t111   = t111;
      newptr->next   = NULL;
      newptr->prev   = NULL;
   }
   return(newptr);
}

/*  
 * Insert a block at the tail of the current block list
 */
CNblockptr CNinsert_block(block_listhead, block_listtail, 
                          cube,t000,t001,t010,t011,t100,t101,t110,t111,ID)
CNblockptr *block_listhead, *block_listtail;
CNcubeptr  cube;
double     t000,t001,t010,t011,t100,t101,t110,t111;
int        ID;
{
   CNblockptr CNmake_block();
   CNblockptr next,A,B;

   A = *block_listtail;
   if ((B=CNmake_block(cube,t000,t001,t010,t011,t100,t101,t110,t111,ID))!=NULL){
      if (A==NULL) {
         *block_listhead = B;
         *block_listtail = B;
      } else {
         next = A->next;
         B->next = next;
         B->prev = A;
         A->next = B;
         if (next != NULL) next->prev = B;
         if (B->next == NULL) *block_listtail = B;
      }
   }
   return(B);
}


/*
 * Delete block at address L 
 */
void CNdelete_block(block_listhead, block_listtail, L)
CNblockptr *block_listhead, *block_listtail;
CNblockptr L;
{
   CNblockptr prev,next;

   prev = L->prev;
   next = L->next;
   if (prev!=NULL) prev->next = next;
   if (next!=NULL) next->prev = prev;
   if (L== *block_listhead) *block_listhead = next;
   if (L== *block_listtail) *block_listtail = prev;
   free ((char*)L);
   L = NULL;
}


/* 
 * Delete all the blocks in the list
 */
void CNdelete_block_list(block_listhead, block_listtail)
CNblockptr *block_listhead, *block_listtail;
{
   CNblockptr B;

   while ((B = *block_listhead) != NULL)
      CNdelete_block(block_listhead, block_listtail, B);
}


/* 
 * Print out the list of blocks 
 */
/*ARGSUSED*/
void CNprint_block_list(block_listhead, block_listtail)
CNblockptr block_listhead, block_listtail;
{
   CNblockptr B;

   for (B=block_listhead; B!=NULL; B=B->next) 
      print_block(B);
}


/* 
 * print the coordinates of a block
 */
static void print_block(B)
CNblockptr B;
{
   (void) fprintf(stdout,"blockID=%d cubeID=%d\n",B->ID,B->cube->ID);
   (void) fprintf(stdout,"   t000=%8.5g t001=%8.5g\n",B->t000, B->t001);
   (void) fprintf(stdout,"   t010=%8.5g t011=%8.5g\n",B->t010, B->t011);
   (void) fprintf(stdout,"   t100=%8.5g t101=%8.5g\n",B->t100, B->t101);
   (void) fprintf(stdout,"   t110=%8.5g t111=%8.5g\n",B->t110, B->t111);
}


/*
 * Count the number of blocks in the list 
 */
/*ARGSUSED*/
int CNcount_blocks(block_listhead, block_listtail)
CNblockptr block_listhead, block_listtail;
{
   CNblockptr B;
   int       count = 0;

   for (B=block_listhead; B!=NULL; B=B->next) count++;

   return(count);
}