• Main Page
  • Classes
  • Files
  • File List
  • File Members

output_tab.c

Go to the documentation of this file.
00001 #include "copyright.h"
00002 /*============================================================================*/
00003 /*! \file output_tab.c
00004  *  \brief Functions for writing output in tabular format.
00005  *
00006  * PURPOSE: Functions for writing output in tabular format.  With SMR,
00007  *   dumps are made for all levels and domains, unless nlevel and ndomain are
00008  *   specified in <output> blocks.
00009  *
00010  * CONTAINS PUBLIC FUNCTIONS: 
00011  * - output_tab() - opens file and calls appropriate 1D/2D/3D output function
00012  *     Uses OutData1,2,3() to extract appropriate section to be output.
00013  *
00014  * PRIVATE FUNCTION PROTOTYPES:
00015  * - output_tab_1d() - write tab file for 1D slice of data
00016  * - output_tab_2d() - write tab file for 2D plane of data
00017  * - output_tab_3d() - write tab file for 3D section of data                  */
00018 /*============================================================================*/
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include "defs.h"
00024 #include "athena.h"
00025 #include "prototypes.h"
00026 
00027 /*==============================================================================
00028  * PRIVATE FUNCTION PROTOTYPES:
00029  *   output_tab_1d() - write tab file for 1D slice of data
00030  *   output_tab_2d() - write tab file for 2D plane of data
00031  *   output_tab_3d() - write tab file for 3D section of data
00032  *============================================================================*/
00033 
00034 void output_tab_1d(MeshS *pM, OutputS *pOut, int nl, int nd);
00035 void output_tab_2d(MeshS *pM, OutputS *pOut, int nl, int nd);
00036 void output_tab_3d(MeshS *pM, OutputS *pOut, int nl, int nd);
00037 
00038 /*=========================== PUBLIC FUNCTIONS ===============================*/
00039 /*----------------------------------------------------------------------------*/
00040 /*! \fn void output_tab(MeshS *pM, OutputS *pOut)
00041  *  \brief Open file, call 1D/2D/3D writer; called by data_ouput  */
00042 
00043 void output_tab(MeshS *pM, OutputS *pOut)
00044 {
00045   int nl,nd;
00046 
00047 /* Loop over all Domains in Mesh, and output Grid data */
00048 
00049   for (nl=0; nl<(pM->NLevels); nl++){
00050     for (nd=0; nd<(pM->DomainsPerLevel[nl]); nd++){
00051       if (pM->Domain[nl][nd].Grid != NULL){
00052 
00053 /* write files if domain and level match input, or are not specified (-1) */
00054       if ((pOut->nlevel == -1 || pOut->nlevel == nl) &&
00055           (pOut->ndomain == -1 || pOut->ndomain == nd)){
00056 
00057         if (pOut->ndim == 3) {
00058           output_tab_3d(pM,pOut,nl,nd);
00059         } else if (pOut->ndim == 2) {
00060           output_tab_2d(pM,pOut,nl,nd);
00061         } else if (pOut->ndim == 1) {
00062           output_tab_1d(pM,pOut,nl,nd);
00063         }
00064       }}
00065     }
00066   }
00067 
00068   return;
00069 }
00070 
00071 /*----------------------------------------------------------------------------*/
00072 /*! \fn void output_tab_1d(MeshS *pM, OutputS *pOut, int nl, int nd) 
00073  *  \brief Writes 1D data.  Note x-coordinate is just i-index.  */
00074 
00075 void output_tab_1d(MeshS *pM, OutputS *pOut, int nl, int nd)
00076 {
00077   GridS *pGrid=pM->Domain[nl][nd].Grid;
00078   int i,nx1;
00079   FILE *pFile;
00080   char fmt[80],*fname,*plev=NULL,*pdom=NULL;
00081   char levstr[8],domstr[8];
00082   Real *data=NULL;
00083   Real dmin, dmax, xworld;
00084 
00085 /* Add a white space to the format, setup format for integer zone columns */
00086   if(pOut->dat_fmt == NULL){
00087      sprintf(fmt," %%12.8e"); /* Use a default format */
00088   }
00089   else{
00090     sprintf(fmt," %s",pOut->dat_fmt);
00091   }
00092 
00093 /* compute 1D array of data */
00094   data = OutData1(pGrid,pOut,&nx1);
00095   if (data == NULL) return;  /* slice not in range of Grid */
00096 
00097   minmax1(data,nx1,&dmin,&dmax);
00098 
00099 /* construct output filename */
00100   if (nl>0) {
00101     plev = &levstr[0];
00102     sprintf(plev,"lev%d",nl);
00103   }
00104   if (nd>0) {
00105     pdom = &domstr[0];
00106     sprintf(pdom,"dom%d",nd);
00107   }
00108 
00109   if((fname = ath_fname(plev,pM->outfilename,plev,pdom,num_digit,pOut->num,
00110       pOut->id,"tab")) == NULL){
00111     ath_error("[output_tab]: Error constructing filename\n");
00112   }
00113 
00114 /* open filename */
00115   pFile = fopen(fname,"w");
00116   if (pFile == NULL) {
00117     ath_error("[output_tab]: Unable to open tab file %s\n",fname);
00118   }
00119 
00120 /* write data */
00121   for (i=0; i<nx1; i++) {
00122     xworld = (float)(i);  /* just i index for now */
00123     fprintf(pFile,fmt,xworld);
00124     fprintf(pFile,fmt,data[i]);
00125     fprintf(pFile,"\n");
00126   }
00127   
00128 /* Compute and store global min/max, for output at end of run */
00129   pOut->gmin = MIN(dmin,pOut->gmin);
00130   pOut->gmax = MAX(dmax,pOut->gmax);
00131 
00132   fclose(pFile);
00133   free_1d_array(data); /* Free the memory we malloc'd */
00134 }
00135 
00136 /*----------------------------------------------------------------------------*/
00137 /*! \fn void output_tab_2d(MeshS *pM, OutputS *pOut, int nl, int nd)
00138  *  \brief Writes 2D data.  Note x/y-coordinate is just i/j-index.  */
00139 
00140 void output_tab_2d(MeshS *pM, OutputS *pOut, int nl, int nd)
00141 {
00142   GridS *pGrid=pM->Domain[nl][nd].Grid;
00143   int i,j,nx1,nx2;
00144   FILE *pFile;
00145   char fmt[80],*fname,*plev=NULL,*pdom=NULL;
00146   char levstr[8],domstr[8];
00147   Real **data=NULL;
00148   Real dmin, dmax, xworld, yworld;
00149 
00150 /* Add a white space to the format, setup format for integer zone columns */
00151   if(pOut->dat_fmt == NULL){
00152      sprintf(fmt," %%12.8e"); /* Use a default format */
00153   }
00154   else{
00155     sprintf(fmt," %s",pOut->dat_fmt);
00156   }
00157 
00158 /* compute 2D array of data */
00159   data = OutData2(pGrid,pOut,&nx1,&nx2);
00160   if (data == NULL) return;  /* slice not in range of Grid */
00161 
00162   minmax2(data,nx2,nx1,&dmin,&dmax);
00163 
00164 /* construct output filename */
00165   if (nl>0) {
00166     plev = &levstr[0];
00167     sprintf(plev,"lev%d",nl);
00168   }
00169   if (nd>0) {
00170     pdom = &domstr[0];
00171     sprintf(pdom,"dom%d",nd);
00172   }
00173 
00174   if((fname = ath_fname(plev,pM->outfilename,plev,pdom,num_digit,pOut->num,
00175       pOut->id,"tab")) == NULL){
00176     ath_error("[output_tab]: Error constructing filename\n");
00177   }
00178 
00179 /* open filename */
00180   pFile = fopen(fname,"w");
00181   if (pFile == NULL) {
00182     ath_error("[output_tab]: Unable to open tab file %s\n",fname);
00183   }
00184 
00185 /* write data */
00186   for (j=0; j<nx2; j++) {
00187     for (i=0; i<nx1; i++) {
00188       xworld = (float)(i); /* just i index for now */
00189       yworld = (float)(j); /* just j index for now */
00190       fprintf(pFile,fmt,xworld);
00191       fprintf(pFile,fmt,yworld);
00192       fprintf(pFile,fmt,data[j][i]);
00193       fprintf(pFile,"\n");
00194     }
00195   }
00196   
00197 /* Compute and store global min/max, for output at end of run */
00198   if (pOut->num == 0) {
00199     pOut->gmin = dmin;
00200     pOut->gmax = dmax;
00201   } else {
00202     pOut->gmin = MIN(dmin,pOut->gmin);
00203     pOut->gmax = MAX(dmax,pOut->gmax);
00204   }
00205 
00206   fclose(pFile);
00207   free_2d_array(data); /* Free the memory we malloc'd */
00208 }
00209 
00210 /*----------------------------------------------------------------------------*/
00211 /*! \fn void output_tab_3d(MeshS *pM, OutputS *pOut, int nl, int nd)
00212  *  \brief Writes 3D data.  Note x/y/z-coordinate is just i/j/k-index  */
00213 
00214 void output_tab_3d(MeshS *pM, OutputS *pOut, int nl, int nd)
00215 {
00216   GridS *pGrid=pM->Domain[nl][nd].Grid;
00217   int i,j,k,nx1,nx2,nx3;
00218   FILE *pFile;
00219   char fmt[80],*fname,*plev=NULL,*pdom=NULL;
00220   char levstr[8],domstr[8];
00221   Real ***data, dmin, dmax, xworld, yworld, zworld;
00222 
00223 /* Add a white space to the format, setup format for integer zone columns */
00224   if(pOut->dat_fmt == NULL){
00225      sprintf(fmt," %%12.8e"); /* Use a default format */
00226   }
00227   else{
00228     sprintf(fmt," %s",pOut->dat_fmt);
00229   }
00230 
00231 /* compute 3D array of data */
00232   data = OutData3(pGrid,pOut,&nx1,&nx2,&nx3);
00233   minmax3(data,nx3,nx2,nx1,&dmin,&dmax);
00234 
00235 /* construct output filename */
00236   if (nl>0) {
00237     plev = &levstr[0];
00238     sprintf(plev,"lev%d",nl);
00239   }
00240   if (nd>0) {
00241     pdom = &domstr[0];
00242     sprintf(pdom,"dom%d",nd);
00243   }
00244 
00245   if((fname = ath_fname(plev,pM->outfilename,plev,pdom,num_digit,pOut->num,
00246       pOut->id,"tab")) == NULL){
00247     ath_error("[output_tab]: Error constructing filename\n");
00248   }
00249 
00250 /* open filename */
00251   pFile = fopen(fname,"w");
00252   if (pFile == NULL) {
00253     ath_error("[output_tab]: Unable to open tab file %s\n",fname);
00254   }
00255 
00256 /* write data */
00257   for (k=0; k<nx3; k++) {
00258     for (j=0; j<nx2; j++) {
00259       for (i=0; i<nx1; i++) {
00260         xworld = (float)(i);  /* just i-index for now */
00261         yworld = (float)(j);  /* just j-index for now */
00262         zworld = (float)(k);  /* just k-index for now */
00263         fprintf(pFile,fmt,xworld);
00264         fprintf(pFile,fmt,yworld);
00265         fprintf(pFile,fmt,zworld);
00266         fprintf(pFile,fmt,data[k][j][i]);
00267         fprintf(pFile,"\n");
00268       }
00269     }
00270   }
00271   
00272 /* Compute and store global min/max, for output at end of run */
00273   if (pOut->num == 0) {
00274     pOut->gmin = dmin;
00275     pOut->gmax = dmax;
00276   } else {
00277     pOut->gmin = MIN(dmin,pOut->gmin);
00278     pOut->gmax = MAX(dmax,pOut->gmax);
00279   }
00280 
00281   fclose(pFile);
00282   free_3d_array(data); /* Free the memory we malloc'd */
00283 }

Generated on Mon Sep 27 2010 23:03:07 for Athena by  doxygen 1.7.1