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
|
/*
* mesh4D.c - procedures to build and maintain a volumetric grid
* data structure used by the mesh4D-type dataset
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "CNplot.h"
static double get_1D_double_array_value();
/*
* MESH4D DATA STRUCTURE
* A mesh4D is used to store a volumetric grid and single-valued
* data (quantities) stored on that grid.
* The data is represented in the form of arrays.
* The mesh4D_parent has the volumetric grid, while the mesh4D_child
* only contains the qarray.
*/
/*
* Allocate room for a mesh4D data structure
*/
CNmesh4Dptr CNmake_mesh4D(ID)
int ID;
{
CNmesh4Dptr newptr;
unsigned int size = sizeof(CNmesh4D);
if ((newptr = (CNmesh4Dptr)malloc(size))!=NULL) {
newptr->ID = ID;
newptr->flag = 0;
newptr->xarray = NULL;
newptr->yarray = NULL;
newptr->zarray = NULL;
newptr->qarray = NULL;
newptr->mat1_array = NULL;
newptr->mat2_array = NULL;
newptr->prism_array= NULL;
newptr->nx = 0;
newptr->ny = 0;
newptr->nz = 0;
newptr->nq = 0;
newptr->xmin = 0.0;
newptr->xmax = 0.0;
newptr->ymin = 0.0;
newptr->ymax = 0.0;
newptr->zmin = 0.0;
newptr->zmax = 0.0;
newptr->qmin = 0.0;
newptr->qmax = 0.0;
newptr->regionhead = NULL;
newptr->regiontail = NULL;
newptr->quant4Dhead= NULL;
newptr->quant4Dtail= NULL;
}
return(newptr);
}
/*
* Delete mesh4D
*/
void CNdelete_mesh4D(Gptr)
CNmesh4Dptr Gptr;
{
/* Free the arrays */
if (Gptr->xarray) free((char *)Gptr->xarray);
if (Gptr->yarray) free((char *)Gptr->yarray);
if (Gptr->zarray) free((char *)Gptr->zarray);
if (Gptr->qarray) free((char *)Gptr->qarray);
if (Gptr->mat1_array ) free((char *)Gptr->mat1_array);
if (Gptr->mat2_array ) free((char *)Gptr->mat2_array);
if (Gptr->prism_array) free((char *)Gptr->prism_array);
/* Free the regions */
CNdelete_region_list(&(Gptr->regionhead), &(Gptr->regiontail));
/* Free the quantities */
CNdelete_quant4D_list(&(Gptr->quant4Dhead), &(Gptr->quant4Dtail));
/* Reset the array counts */
Gptr->nx = 0;
Gptr->ny = 0;
Gptr->nz = 0;
Gptr->nq = 0;
/* Now delete Gptr */
free ((char*)Gptr);
}
/*
* Retrieve data from the grid
*/
/* x-value */
double CNmesh4D_x(Gptr,i)
CNmesh4Dptr Gptr;
int i;
{
double val;
val = get_1D_double_array_value(Gptr->xarray,i,Gptr->nx);
return(val);
}
/* y-value */
double CNmesh4D_y(Gptr,i)
CNmesh4Dptr Gptr;
int i;
{
double val;
val = get_1D_double_array_value(Gptr->yarray,i,Gptr->ny);
return(val);
}
/* z-value */
double CNmesh4D_z(Gptr,i)
CNmesh4Dptr Gptr;
int i;
{
double val;
val = get_1D_double_array_value(Gptr->zarray,i,Gptr->nz);
return(val);
}
/* q-value */
double CNmesh4D_q(Gptr,i,j,k)
CNmesh4Dptr Gptr;
int i,j,k;
{
double val;
int t;
t = i + j*Gptr->nx + k*Gptr->nx*Gptr->ny;
val = get_1D_double_array_value(Gptr->qarray,t,Gptr->nq);
return(val);
}
/* mat1-value */
double CNmesh4D_m1(Gptr,i,j,k)
CNmesh4Dptr Gptr;
int i,j,k;
{
double val;
int t, nc;
nc = (Gptr->nx - 1)*(Gptr->ny - 1)*(Gptr->nz - 1);
t = i + j*(Gptr->nx -1) + k*(Gptr->nx - 1)*(Gptr->ny - 1);
val = get_1D_double_array_value(Gptr->mat1_array,t,nc);
return(val);
}
/* mat2-value */
double CNmesh4D_m2(Gptr,i,j,k)
CNmesh4Dptr Gptr;
int i,j,k;
{
double val;
int t, nc;
nc = (Gptr->nx - 1)*(Gptr->ny - 1)*(Gptr->nz - 1);
t = i + j*(Gptr->nx -1) + k*(Gptr->nx - 1)*(Gptr->ny - 1);
val = get_1D_double_array_value(Gptr->mat2_array,t,nc);
return(val);
}
/* mat1-value */
double CNmesh4D_mp(Gptr,i,j,k)
CNmesh4Dptr Gptr;
int i,j,k;
{
double val;
int t, nc;
nc = (Gptr->nx - 1)*(Gptr->ny - 1)*(Gptr->nz - 1);
t = i + j*(Gptr->nx -1) + k*(Gptr->nx - 1)*(Gptr->ny - 1);
val = get_1D_double_array_value(Gptr->prism_array,t,nc);
return(val);
}
/*
* Print info on the mesh4D
*/
void CNprint_mesh4D(Gptr)
CNmesh4Dptr Gptr;
{
int i,j,k,i0,j0,k0;
(void) fprintf(stdout,"Mesh4D Grid %d:\n",Gptr->ID);
(void) fprintf(stdout," x[0] = %8.3g x[%5d] = %8.3g\n",
CNmesh4D_x(Gptr,0),
Gptr->nx-1,
CNmesh4D_x(Gptr,Gptr->nx-1));
(void) fprintf(stdout," y[0] = %8.3g y[%5d] = %8.3g\n",
CNmesh4D_y(Gptr,0),
Gptr->ny-1,
CNmesh4D_y(Gptr,Gptr->ny-1));
(void) fprintf(stdout," z[0] = %8.3g z[%5d] = %8.3g\n",
CNmesh4D_z(Gptr,0),
Gptr->nz-1,
CNmesh4D_z(Gptr,Gptr->nz-1));
for (k=0; k<2; k++)
for (i=0; i<2; i++)
for (j=0; j<2; j++) {
i0 = i*(Gptr->nx-1);
j0 = j*(Gptr->ny-1);
k0 = k*(Gptr->nz-1);
(void) fprintf(stdout," t[%2d][%2d][%2d] = %8.3g\n",
i0, j0, k0, CNmesh4D_q(Gptr,i0, j0, k0));
}
}
/*
* QUANT 4D - this is used only on a temporary basis to store
* quantities based on mesh4D structures
*/
/*
* quantities are used to store field information at nodes
*/
CNquant4Dptr CNmake_quant4D(label, qarray, npts, ID)
char *label;
double *qarray;
int npts;
int ID;
{
CNquant4Dptr newptr;
unsigned int size = sizeof(CNquant4D);
if ((newptr = (CNquant4Dptr)malloc(size))!=NULL) {
newptr->ID = ID;
newptr->name = CNcreate_string(label);
newptr->qarray = qarray;
newptr->npts = npts;
newptr->next = NULL;
newptr->prev = NULL;
}
return(newptr);
}
/*
* Insert a quantity at the tail of the current quantity list
*/
CNquant4Dptr CNinsert_quant4D(quant4D_listhead,quant4D_listtail,
label, qarray, npts, ID)
CNquant4Dptr *quant4D_listhead, *quant4D_listtail;
char *label;
double *qarray;
int npts;
int ID;
{
CNquant4Dptr next,A,B;
A = *quant4D_listtail;
if ((B=CNmake_quant4D(label, qarray, npts, ID))!=NULL) {
if (A==NULL) {
*quant4D_listhead = B;
*quant4D_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) *quant4D_listtail = B;
}
}
return(B);
}
/*
* Insert a quantity at the tail of the current quantity list
*/
void CNadd_quant4D(quant4D_listhead,quant4D_listtail,quant4D)
CNquant4Dptr *quant4D_listhead, *quant4D_listtail, quant4D;
{
CNquant4Dptr next,A,B;
A = *quant4D_listtail;
if ((B=quant4D)!=NULL) {
if (A==NULL) {
*quant4D_listhead = B;
*quant4D_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) *quant4D_listtail = B;
}
}
}
/*
* Delete quant
*/
void CNdelete_quant4D(quant4D_listhead, quant4D_listtail, Q)
CNquant4Dptr *quant4D_listhead, *quant4D_listtail;
CNquant4Dptr Q;
{
CNquant4Dptr prev,next;
/* Delete the label */
CNdestroy_string(Q->name);
/* Delete the array associated with the quantity */
if (Q->qarray) free((char *)(Q->qarray));
Q->qarray = NULL;
prev = Q->prev;
next = Q->next;
if (prev!=NULL) prev->next = next;
if (next!=NULL) next->prev = prev;
if (Q==*quant4D_listhead) *quant4D_listhead = next;
if (Q==*quant4D_listtail) *quant4D_listtail = prev;
/* Now delete Q */
free ((char*)Q);
}
/*
* Delete all the quantities in the list
*/
void CNdelete_quant4D_list(quant4D_listhead, quant4D_listtail)
CNquant4Dptr *quant4D_listhead, *quant4D_listtail;
{
CNquant4Dptr Q;
while ((Q = *quant4D_listhead) != NULL)
CNdelete_quant4D(quant4D_listhead, quant4D_listtail, Q);
}
/*
* print out the list of quantities
*/
/*ARGSUSED*/
void CNprint_quant4D_list(quant4D_listhead, quant4D_listtail)
CNquant4Dptr quant4D_listhead, quant4D_listtail;
{
CNquant4Dptr Q;
for (Q=quant4D_listhead; Q!=NULL; Q=Q->next)
CNprint_quant4D(Q);
}
/*
* print a quantity
*/
void CNprint_quant4D(Q)
CNquant4Dptr Q;
{
(void) fprintf(stdout,
" QuantID# %3d Label=\"%s\" Array=%d\n",
Q->ID, Q->name, Q->qarray);
(void) fflush(stdout);
}
/*
* Count the number of quants in the list
*/
/*ARGSUSED*/
int CNcount_quant4Ds(quant4D_listhead, quant4D_listtail)
CNquant4Dptr quant4D_listhead, quant4D_listtail;
{
CNquant4Dptr P;
int count = 0;
for (P=quant4D_listhead; P!=NULL; P=P->next) count++;
return(count);
}
/*
* Array routines
*/
/*
* get the value of an array element
*/
static double get_1D_double_array_value(arrptr,i,isize)
double *arrptr;
int i,isize;
{
double val;
if (i<0 || i>=isize) {
(void) fprintf(stderr,"Element [%d] is out of bounds!\n",i);
return(0.0);
}
val = *(arrptr + i);
return(val);
}
|