aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortradke <tradke@b589c3ab-70e8-4b4d-a09f-cba2dd200880>2000-01-17 14:48:06 +0000
committertradke <tradke@b589c3ab-70e8-4b4d-a09f-cba2dd200880>2000-01-17 14:48:06 +0000
commit3332cf64441f7ad9530939a71921328470cdd02d (patch)
tree04bbccdc28825bf3102e9b9cb9c893e8c1be698e
parent80789c18045b6fed18a031a2307d871ed332eb4e (diff)
Don't write to files that could not be opened
(eg. because of invalid filenames). git-svn-id: http://svn.cactuscode.org/arrangements/CactusBase/IOBasic/trunk@23 b589c3ab-70e8-4b4d-a09f-cba2dd200880
-rw-r--r--src/WriteGF.c148
1 files changed, 78 insertions, 70 deletions
diff --git a/src/WriteGF.c b/src/WriteGF.c
index 1681cd5..ed54c9d 100644
--- a/src/WriteGF.c
+++ b/src/WriteGF.c
@@ -23,11 +23,29 @@
void IOBasic_WriteGF (cGH *GH, int index, const char *alias)
{
DECLARE_CCTK_PARAMETERS
- CCTK_REAL tt[1];
int i;
- int reduce_handle;
+ iobasicGH *myGH;
+ FILE *file;
char *openmode;
- FILE *file[4];
+ char *filename;
+ char title_start_char;
+ int reduction_handle;
+ CCTK_REAL reduction_value;
+ /* add more reductions to this table if you want, telling it
+ - the reduction operator
+ - the label for the file header
+ - the filename extension (suffixed by ".tl" for trace line) */
+ const struct {
+ char *operator;
+ char *label;
+ char *extension;
+ } reductions [] = {
+ {"minimum", "min", "min"},
+ {"maximum", "max", "max"},
+ {"norm1", "norm1", "nm1"},
+ {"norm2", "norm2", "nm2"},
+ };
+#define NUM_REDUCTIONS (sizeof (reductions) / sizeof (reductions [0]))
/* first, check if variable has storage assigned */
@@ -43,88 +61,78 @@ void IOBasic_WriteGF (cGH *GH, int index, const char *alias)
return;
}
- /* Open the file (we write only on proc0) */
- if (CCTK_MyProc (GH) == 0) {
+ if (CCTK_Equals (outScalar_style, "gnuplot"))
+ title_start_char = '#';
+ else {
+ if (! CCTK_Equals (outScalar_style,"xgraph"))
+ CCTK_WARN (3, "Don't understand outScalar_style ... using xgraph");
+ title_start_char = 34;
+ }
- char fname[256];
- iobasicGH *myGH;
+ myGH = (iobasicGH *) GH->extensions [CCTK_GHExtensionHandle ("IOBasic")];
- myGH = (iobasicGH *) GH->extensions [CCTK_GHExtensionHandle ("IOBasic")];
+ /* the extra characters should be sufficient
+ for the longest filename extension */
+ filename = (char *)
+ malloc (strlen (myGH->outdirScalar) + strlen (alias) + 20);
- /* see if output files for this alias name were already created */
- if (GetNamedData (myGH->filenameListScalar, alias) == NULL) {
- /* just store a non-NULL pointer in database */
- StoreNamedData (&myGH->filenameListScalar, alias, (void *) 1);
- openmode = "w";
- } else
- openmode = "a";
+ /* now loop over all IOBasic reduction operations */
+ for (i = 0; i < NUM_REDUCTIONS; i++) {
- sprintf (fname, "%s/%s_max.tl", myGH->outdirScalar, alias);
- file[0] = fopen(fname,openmode);
+ /* get the reduction handle from the reduction operator */
+ reduction_handle = CCTK_ReductionHandle (reductions [i].operator);
+ if (reduction_handle < 0) {
+ char *msg = (char *) malloc (100 + strlen (reductions [i].operator));
- sprintf(fname,"%s/%s_min.tl", myGH->outdirScalar, alias);
- file[1] = fopen(fname,openmode);
+ sprintf (msg, "Invalid reduction operator '%s'", reductions [i].operator);
+ CCTK_WARN (3, msg);
+ free (msg);
+ continue;
+ }
- sprintf(fname,"%s/%s_nm1.tl", myGH->outdirScalar, alias);
- file[2] = fopen(fname,openmode);
+ /* do the reduction (all processors) */
+ CCTK_Reduce (GH, 0, reduction_handle, 1, CCTK_VARIABLE_REAL,
+ &reduction_value, 1, index);
- sprintf(fname,"%s/%s_nm2.tl", myGH->outdirScalar, alias);
- file[3] = fopen(fname,openmode);
+ /* dump the reduction value to file by processor 0 */
+ if (CCTK_MyProc (GH) == 0) {
- if (*openmode == 'w') {
- char title_start_char;
+ /* build the filename */
+ sprintf (filename, "%s/%s_%s.tl", myGH->outdirScalar, alias,
+ reductions [i].extension);
- if (CCTK_Equals(outScalar_style,"gnuplot"))
- title_start_char = '#';
- else {
- if (! CCTK_Equals(outScalar_style,"xgraph"))
- CCTK_WARN(3,"Don't understand outScalar_style ... using xgraph");
- title_start_char = 34;
- }
+ /* see if output files for this alias name were already created */
+ if (GetNamedData (myGH->filenameListScalar, filename) == NULL)
+ openmode = "w";
+ else
+ openmode = "a";
- fprintf (file[0],"%c%s max v time\n",title_start_char,alias);
- fprintf (file[1],"%c%s min v time\n",title_start_char,alias);
- fprintf (file[2],"%c%s norm1 v time\n",title_start_char,alias);
- fprintf (file[3],"%c%s norm2 v time\n",title_start_char,alias);
- }
+ file = fopen (filename, openmode);
+ if (file == NULL) {
+ char *msg = (char *) malloc (100 + strlen (filename));
- }
-
- reduce_handle = CCTK_ReductionHandle("maximum");
- if (reduce_handle > -1)
- {
- CCTK_Reduce(GH,-1,reduce_handle,1,CCTK_VARIABLE_REAL,tt,1,index);
- if (CCTK_MyProc(GH) == 0)
- fprintf(file[0],"%f %25.13f\n",GH->cctk_time,tt[0]);
- }
-
- reduce_handle = CCTK_ReductionHandle("minimum");
- if (reduce_handle > -1)
- {
- CCTK_Reduce(GH,-1,reduce_handle,1,CCTK_VARIABLE_REAL,tt,1,index);
- if (CCTK_MyProc(GH) == 0)
- fprintf(file[1],"%f %25.13f\n",GH->cctk_time,tt[0]);
- }
+ sprintf (msg, "Could not open output file '%s'", filename);
+ CCTK_WARN (3, msg);
+ free (msg);
+ continue;
+ }
- reduce_handle = CCTK_ReductionHandle("norm1");
- if (reduce_handle > -1)
- {
- CCTK_Reduce(GH,-1,reduce_handle,1,CCTK_VARIABLE_REAL,tt,1,index);
- if (CCTK_MyProc(GH) == 0)
- fprintf(file[2],"%f %25.13f\n",GH->cctk_time,tt[0]);
- }
+ /* when creating the file, write the header
+ and also save the filename in the database */
+ if (*openmode == 'w') {
+ fprintf (file, "%c%s %s v time\n", title_start_char, alias,
+ reductions [i].label);
+ /* just store a non-NULL pointer in database */
+ StoreNamedData (&myGH->filenameListScalar, filename, (void *) 1);
+ }
- reduce_handle = CCTK_ReductionHandle("norm2");
- if (reduce_handle > -1)
- {
- CCTK_Reduce(GH,-1,reduce_handle,1,CCTK_VARIABLE_REAL,tt,1,index);
- if (CCTK_MyProc(GH) == 0)
- fprintf(file[3],"%f %25.13f\n",GH->cctk_time,tt[0]);
+ /* write the data and close the file */
+ fprintf (file, "%f %25.13f\n", GH->cctk_time, reduction_value);
+ fclose (file);
+ }
}
- if (CCTK_MyProc (GH) == 0)
- for (i=0;i<4;i++)
- fclose(file[i]);
+ free (filename);
USE_CCTK_PARAMETERS