extern field_t* field_read_gfs(const char* file) { int argc = 0; gfs_init(&argc,NULL); FILE *st = fopen(file,"r"); if (!st) { fprintf(stderr,"failed to open %s\n",file); return NULL; } GtsFile *fp = gts_file_new(st); GfsSimulation *sim = gfs_simulation_read(fp); if (!sim) { fprintf(stderr, "file %s not a valid simulation file\n" "line %d:%d: %s\n", file, fp->line, fp->pos, fp->error); return NULL; } gts_file_destroy(fp); fclose(st); GfsDomain *gdom = GFS_DOMAIN(sim); /* find the bounding box */ bbox_t *bb = NULL; gfs_domain_cell_traverse(gdom, FTT_PRE_ORDER, FTT_TRAVERSE_NON_LEAFS, 0, (FttCellTraverseFunc)ftt_bbox, &bb); if (!bb) { fprintf(stderr,"failed to determine bounding box\n"); return NULL; } #ifdef FRG_DEBUG fprintf(stdout, "%g %g %g %g\n", bb->x.min, bb->x.max, bb->y.min, bb->y.max); #endif /* tree depth and discretisation size */ int depth = gfs_domain_depth(gdom), nw = (int)(POW2(depth)*bbox_width(*bb)), nh = (int)(POW2(depth)*bbox_height(*bb)); /* shave bbox for node-aligned rather than pixel */ double shave = bbox_width(*bb)/(2.0*nw); bb->x.min += shave; bb->x.max -= shave; bb->y.min += shave; bb->y.max -= shave; /* create & intialise meshes */ bilinear_t* B[2]; int i; for (i=0 ; i<2 ; i++) { if ((B[i] = bilinear_new()) == NULL) return NULL; if (bilinear_dimension(nw,nh,*bb,B[i]) != ERROR_OK) return NULL; } ftts_t ftts; ftts.B = B; ftts.depth = depth; if ((ftts.u = gfs_variable_from_name(gdom->variables,"U")) == NULL) { fprintf(stderr,"no variable U\n"); return NULL; } if ((ftts.v = gfs_variable_from_name(gdom->variables,"V")) == NULL) { fprintf(stderr,"no variable V\n"); return NULL; } gfs_domain_cell_traverse(gdom, FTT_PRE_ORDER, FTT_TRAVERSE_LEAFS, -1, (FttCellTraverseFunc)ftt_sample, &ftts); /* clean up */ free(bb); gts_object_destroy(GTS_OBJECT(sim)); /* results */ field_t* F = malloc(sizeof(field_t)); if (!F) return NULL; F->u = B[0]; F->v = B[1]; F->k = NULL; return F; }
static void output_spectra_read (GtsObject ** o, GtsFile * fp) { (* GTS_OBJECT_CLASS (gfs_output_spectra_class ())->parent_class->read) (o, fp); if (fp->type == GTS_ERROR) return; GfsOutputSpectra * v = GFS_OUTPUT_SPECTRA (*o); if (fp->type != GTS_STRING) { gts_file_error (fp, "expecting a string (v)"); return; } GfsDomain * domain = GFS_DOMAIN (gfs_object_simulation (*o)); if (!(v->v = gfs_variable_from_name (domain->variables, fp->token->str))) { gts_file_error (fp, "unknown variable `%s'", fp->token->str); return; } gts_file_next_token (fp); GtsFileVariable var[] = { {GTS_DOUBLE, "x", TRUE, &v->pos.x}, {GTS_DOUBLE, "y", TRUE, &v->pos.y}, {GTS_DOUBLE, "z", TRUE, &v->pos.z}, {GTS_DOUBLE, "Lx", TRUE, &v->L.x}, {GTS_DOUBLE, "Ly", TRUE, &v->L.y}, {GTS_DOUBLE, "Lz", TRUE, &v->L.z}, {GTS_NONE} }; gts_file_assign_variables (fp, var); if (fp->type == GTS_ERROR) return; if (fp->type == GTS_INT) { v->level = atoi (fp->token->str); gts_file_next_token (fp); } else v->level = gfs_domain_depth (domain); guint i, j, k, size = 1; v->cgd = gfs_cartesian_grid_new (gfs_cartesian_grid_class ()); /* number of dims of the fft */ v->cgd->N = 3; v->Ndim = 0; k = 0; for (i = 0; i < 3; i++) { if ((&(v->L.x))[i] != 0) { v->Ndim++; v->dir[k] = i; k++; } } if (v->Ndim == 0) { gts_file_error (fp, "There must be at least one L component larger than 0"); return; } /* number of points in each direction */ v->cgd->n = g_malloc (3*sizeof (guint)); for (i = 0; i < 3; i++) { if ((&(v->L.x))[i] == 0 ) v->cgd->n[i] = 1; else v->cgd->n[i] = pow(2,v->level); size *= v->cgd->n[i]; } /* mesh coordinates */ v->cgd->x = g_malloc0 (3*sizeof (gdouble *)); for (i = 0; i < 3; i++) { v->cgd->x[i] = g_malloc (v->cgd->n[i]*sizeof (gdouble)); if (v->cgd->n[i] != 1) for (j = 0; j < v->cgd->n[i]; j++) v->cgd->x[i][j] = (&(v->pos.x))[i] + (&(v->L.x))[i]*(gdouble)j/((gdouble)(v->cgd->n[i]-1)) - 0.5; else v->cgd->x[i][0] = (&(v->pos.x))[i]; } /* memory data allocation */ v->cgd->v = g_malloc0( sizeof ( gdouble ) * 2*(size/2+1) ); }