[cig-commits] r5347 - in mc/3D/CitcomS/trunk: CitcomS/Components CitcomS/Solver lib module

tan2 at geodynamics.org tan2 at geodynamics.org
Mon Nov 27 12:05:11 PST 2006


Author: tan2
Date: 2006-11-27 12:05:11 -0800 (Mon, 27 Nov 2006)
New Revision: 5347

Modified:
   mc/3D/CitcomS/trunk/CitcomS/Components/Output.py
   mc/3D/CitcomS/trunk/CitcomS/Solver/Solver.py
   mc/3D/CitcomS/trunk/lib/Instructions.c
   mc/3D/CitcomS/trunk/lib/Output.c
   mc/3D/CitcomS/trunk/lib/global_defs.h
   mc/3D/CitcomS/trunk/module/setProperties.c
Log:
Changes how to specify output directory

* "output_format" must be either "ascii" (the default) or "hdf5", the
  "ascii-local" option is gone
* "datafile" becomes the prefix of the filename and cannot contain "/"
* "datadir" becomes mandatory input parameters
* If "datadir" is "%DATADIR", the output directory is determined by an
  external program "citcoms_datadir"
* Special substrings "%RANK", "%HOSTNAME" in datadir will be substituted
  by mpi rank, machine's hostname, $DATADIR environment variable,
  respectively. 
* These special strings start with "%", rather than "$",  becuase it's very
  difficult to input "$" from the command line.
        


Modified: mc/3D/CitcomS/trunk/CitcomS/Components/Output.py
===================================================================
--- mc/3D/CitcomS/trunk/CitcomS/Components/Output.py	2006-11-22 21:26:09 UTC (rev 5346)
+++ mc/3D/CitcomS/trunk/CitcomS/Components/Output.py	2006-11-27 20:05:11 UTC (rev 5347)
@@ -48,9 +48,8 @@
 
         import pyre.inventory as inv
 
-        output_format = inv.str("output_format", default="ascii-local",
-                                validator=inv.choice(["ascii-local",
-                                                      "ascii",
+        output_format = inv.str("output_format", default="ascii",
+                                validator=inv.choice(["ascii",
                                                       "hdf5"]))
         output_optional = inv.str("output_optional", default="surf,botm")
 

Modified: mc/3D/CitcomS/trunk/CitcomS/Solver/Solver.py
===================================================================
--- mc/3D/CitcomS/trunk/CitcomS/Solver/Solver.py	2006-11-22 21:26:09 UTC (rev 5346)
+++ mc/3D/CitcomS/trunk/CitcomS/Solver/Solver.py	2006-11-27 20:05:11 UTC (rev 5347)
@@ -275,8 +275,8 @@
         tracer = inv.facility("tracer", factory=Tracer)
         visc = inv.facility("visc", factory=Visc)
 
-        datadir = inv.str("datadir", default=".")
-        datadir_old = inv.str("datadir_old", default=".")
+        datadir = inv.str("datadir", default="")
+        datadir_old = inv.str("datadir_old", default="")
 
         rayleigh = inv.float("rayleigh", default=1e+05)
         Q0 = inv.float("Q0", default=0.0)

Modified: mc/3D/CitcomS/trunk/lib/Instructions.c
===================================================================
--- mc/3D/CitcomS/trunk/lib/Instructions.c	2006-11-22 21:26:09 UTC (rev 5346)
+++ mc/3D/CitcomS/trunk/lib/Instructions.c	2006-11-27 20:05:11 UTC (rev 5347)
@@ -31,8 +31,10 @@
 
 #include <math.h>
 #include <string.h>
+#include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/errno.h>
+#include <unistd.h>
 #include <ctype.h>
 #include "element_definitions.h"
 #include "global_defs.h"
@@ -47,6 +49,9 @@
 #include "phase_change.h"
 #include "interuption.h"
 
+extern void parallel_process_termination();
+
+
 void read_instructions(struct All_variables *E, char *filename)
 {
     void allocate_common_vars();
@@ -267,10 +272,10 @@
      Default is no information recorded (apart from special things for given applications.
   */
 
-  input_string("datadir",E->control.data_dir,".",m);
-  input_string("datafile",E->control.data_file,"initialize",m);
-  input_string("datadir_old",E->control.data_dir_old,"initialize",m);
-  input_string("datafile_old",E->control.old_P_file,"initialize",m);
+  input_string("datadir",E->control.data_dir,"",m);
+  input_string("datafile",E->control.data_prefix,"initialize",m);
+  input_string("datadir_old",E->control.data_dir_old,"",m);
+  input_string("datafile_old",E->control.data_prefix_old,"initialize",m);
 
   input_int("mgunitx",&(E->mesh.mgunitx),"1",m);
   input_int("mgunitz",&(E->mesh.mgunitz),"1",m);
@@ -762,7 +767,7 @@
 
   /* THIRD: you forgot and then went home, let's see if we can help out */
 
-    sprintf(E->control.data_file,"citcom.tmp.%d",getpid());
+    sprintf(E->control.data_prefix,"citcom.tmp.%d",getpid());
 
     E->control.NASSEMBLE = 0;
 
@@ -926,7 +931,7 @@
 
 
 
-void open_log(struct All_variables *E)
+static void open_log(struct All_variables *E)
 {
   char logfile[255];
 
@@ -938,7 +943,7 @@
 }
 
 
-void open_time(struct All_variables *E)
+static void open_time(struct All_variables *E)
 {
   char timeoutput[255];
 
@@ -952,7 +957,7 @@
 }
 
 
-void open_info(struct All_variables *E)
+static void open_info(struct All_variables *E)
 {
   char output_file[255];
 
@@ -966,7 +971,7 @@
 }
 
 
-void output_parse_optional(struct  All_variables *E)
+static void output_parse_optional(struct  All_variables *E)
 {
     char* strip(char*);
 
@@ -1030,92 +1035,140 @@
     return;
 }
 
-/* check whether E->control.data_file contains a path */
-void chkdatafile(struct  All_variables *E)
+/* check whether E->control.data_file contains a path seperator */
+static void chk_prefix(struct  All_variables *E)
 {
-  void parallel_process_termination();
-
   char *found;
-  char newdatafile[100];
 
-  found = strchr(E->control.data_file, '/');
+  found = strchr(E->control.data_prefix, '/');
   if (found) {
       fprintf(stderr, "error in input parameter: datafile='%s' contains '/'\n", E->control.data_file);
       parallel_process_termination();
   }
 
-  /* prepend the path to data_file */
-  sprintf(newdatafile, "%s/%d/%s", E->control.data_dir, E->parallel.me,
-           E->control.data_file);
-  strcpy(E->control.data_file, newdatafile);
-
   if (E->control.restart) {
-      found = strchr(E->control.old_P_file, '/');
+      found = strchr(E->control.data_prefix_old, '/');
       if (found) {
 	  fprintf(stderr, "error in input parameter: datafile_old='%s' contains '/'\n", E->control.data_file);
 	  parallel_process_termination();
       }
-
-      /* prepend the path to old_P_file */
-      sprintf(newdatafile, "%s/%d/%s", E->control.data_dir_old, E->parallel.me,
-	      E->control.old_P_file);
-      strcpy(E->control.old_P_file, newdatafile);
   }
 }
 
 
-void mkdatadir(struct  All_variables *E)
+/* search src and substitue the 1st occurance of target by value */
+static void expand_str(char *src, size_t max_size,
+		       const char *target, const char *value)
 {
-  void parallel_process_termination();
+    char *pos, *end, *tmp;
 
+    /* is target a substring of src? */
+    pos = strstr(src, target);
+    if (pos != NULL) {
+	/* the end char of target */
+	end = pos + strlen(target);
+
+	/* make a copy of the 2nd part of the original string */
+	tmp = strndup(end, max_size);
+
+	/* terminate src at pos */
+	*pos = '\0';
+
+	/* src + value + end */
+	strncat(src, value, max_size);
+	strncat(src, tmp, max_size);
+
+	free(tmp);
+    }
+}
+
+static void expand_datadir(struct All_variables *E, char *datadir)
+{
+    char *found, *err;
+    char tmp[150];
+    int diff;
+    FILE *pipe;
+    const char str1[] = "%HOSTNAME";
+    const char str2[] = "%RANK";
+    const char str3[] = "%DATADIR";
+    const char str3_prog[] = "citcoms_datadir";
+
+    /* expand str1 by machine's hostname */
+    found = strstr(datadir, str1);
+    if (found) {
+	gethostname(tmp, 100);
+	expand_str(datadir, 150, str1, tmp);
+    }
+
+    /* expand str2 by MPI rank */
+    found = strstr(datadir, str2);
+    if (found) {
+	sprintf(tmp, "%d", E->parallel.me);
+	expand_str(datadir, 150, str2, tmp);
+    }
+
+    /* expand str3 by the result of the external program */
+    diff = strcmp(datadir, str3);
+    if (!diff) {
+	pipe = popen(str3_prog, "r");
+	err = fgets(tmp, 150, pipe);
+	pclose(stdout);
+	if (err != NULL)
+	    sscanf(tmp, " %s", datadir);
+	else {
+	    fprintf(stderr, "Cannot get datadir from command '%s'\n", str3_prog);
+	    parallel_process_termination();
+	}
+    }
+}
+
+
+void mkdatadir(const char *dir)
+{
   int err;
-  char newdir[110];
 
-  err = mkdir(E->control.data_dir, 0755);
+  err = mkdir(dir, 0755);
   if (err && errno != EEXIST) {
       /* if error occured and the directory is not exisitng */
-      fprintf(stderr, "Cannot make new directory '%s'\n", E->control.data_dir);
+      fprintf(stderr, "Cannot make new directory '%s'\n", dir);
       parallel_process_termination();
   }
-  sprintf(newdir, "%s/%d", E->control.data_dir, E->parallel.me);
-  err = mkdir(newdir, 0755);
-  if (err && errno != EEXIST) {
-      /* if error occured and the directory is not exisitng */
-      fprintf(stderr, "Cannot make new directory '%s'\n", newdir);
-      parallel_process_termination();
-  }
 }
 
 
 void output_init(struct  All_variables *E)
 {
-    /*DEBUG
-    //strcpy(E->output.format, "hdf5");
-    //fprintf(stderr, "output format is %s\n", E->output.format);*/
-    if (strcmp(E->output.format, "ascii-local") == 0)
+    chk_prefix(E);
+    expand_datadir(E, E->control.data_dir);
+    mkdatadir(E->control.data_dir);
+    snprintf(E->control.data_file, 200, "%s/%s", E->control.data_dir,
+	     E->control.data_prefix);
+
+    if (E->control.restart) {
+	expand_datadir(E, E->control.data_dir_old);
+	snprintf(E->control.old_P_file, 200, "%s/%s", E->control.data_dir_old,
+		 E->control.data_prefix_old);
+    }
+
+    open_log(E);
+    open_time(E);
+    open_info(E);
+
+    if (strcmp(E->output.format, "ascii") == 0) {
         E->problem_output = output;
-    else if (strcmp(E->output.format, "ascii") == 0) {
-        chkdatafile(E);
-        mkdatadir(E);
-        E->problem_output = output;
     }
     else if (strcmp(E->output.format, "hdf5") == 0)
         E->problem_output = h5output;
     else {
         /* indicate error here */
         if (E->parallel.me == 0) {
-            fprintf(stderr, "wrong output_format, must be either 'ascii-local', 'ascii' or 'hdf5'\n");
-            fprintf(E->fp, "wrong output_format, must be either 'ascii-local', 'ascii' or 'hdf5'\n");
+            fprintf(stderr, "wrong output_format, must be either 'ascii' or 'hdf5'\n");
+            fprintf(E->fp, "wrong output_format, must be either 'ascii' or 'hdf5'\n");
         }
         parallel_process_termination(E);
     }
 
-    open_log(E);
-    open_time(E);
-    open_info(E);
-
     output_parse_optional(E);
-
 }
 
 

Modified: mc/3D/CitcomS/trunk/lib/Output.c
===================================================================
--- mc/3D/CitcomS/trunk/lib/Output.c	2006-11-22 21:26:09 UTC (rev 5346)
+++ mc/3D/CitcomS/trunk/lib/Output.c	2006-11-27 20:05:11 UTC (rev 5347)
@@ -59,7 +59,7 @@
 {
     int m = E->parallel.me;
 
-    input_string("output_format", E->output.format, "ascii-local",m);
+    input_string("output_format", E->output.format, "ascii",m);
     input_string("output_optional", E->output.optional, "surf,botm",m);
 
 }

Modified: mc/3D/CitcomS/trunk/lib/global_defs.h
===================================================================
--- mc/3D/CitcomS/trunk/lib/global_defs.h	2006-11-22 21:26:09 UTC (rev 5346)
+++ mc/3D/CitcomS/trunk/lib/global_defs.h	2006-11-27 20:05:11 UTC (rev 5347)
@@ -443,10 +443,15 @@
     char B_is_good[MAX_LEVELS];  /* general information controlling program flow */
     char Ahat_is_good[MAX_LEVELS];  /* general information controlling program flow */
 
-    char data_dir[100];
-    char data_file[100];
-    char data_dir_old[100];
-    char old_P_file[100];
+    char data_prefix[50];
+    char data_prefix_old[50];
+
+    char data_dir[150];
+    char data_dir_old[150];
+
+    char data_file[200];
+    char old_P_file[200];
+
     char post_topo_file[100];
     char slabgeoid_file[100];
 
@@ -621,7 +626,7 @@
 };
 
 struct Output {
-    char format[20];  /* ascii-local, ascii or hdf5 */
+    char format[20];  /* ascii or hdf5 */
     char optional[1000]; /* comma-delimited list of objects to output */
 
     int llmax;  /* max degree of spherical harmonics output */

Modified: mc/3D/CitcomS/trunk/module/setProperties.c
===================================================================
--- mc/3D/CitcomS/trunk/module/setProperties.c	2006-11-22 21:26:09 UTC (rev 5346)
+++ mc/3D/CitcomS/trunk/module/setProperties.c	2006-11-27 20:05:11 UTC (rev 5347)
@@ -445,9 +445,9 @@
     PUTS(("[CitcomS.solver]\n"));
 
     getStringProperty(properties, "datadir", E->control.data_dir, fp);
-    getStringProperty(properties, "datafile", E->control.data_file, fp);
+    getStringProperty(properties, "datafile", E->control.data_prefix, fp);
     getStringProperty(properties, "datadir_old", E->control.data_dir_old, fp);
-    getStringProperty(properties, "datafile_old", E->control.old_P_file, fp);
+    getStringProperty(properties, "datafile_old", E->control.data_prefix_old, fp);
 
     getFloatProperty(properties, "rayleigh", E->control.Atemp, fp);
     getFloatProperty(properties, "Q0", E->control.Q0, fp);



More information about the cig-commits mailing list