[cig-commits] r11217 - mc/3D/CitcomS/trunk/lib

tan2 at geodynamics.org tan2 at geodynamics.org
Wed Feb 20 16:56:29 PST 2008


Author: tan2
Date: 2008-02-20 16:56:29 -0800 (Wed, 20 Feb 2008)
New Revision: 11217

Modified:
   mc/3D/CitcomS/trunk/lib/Pan_problem_misc_functions.c
   mc/3D/CitcomS/trunk/lib/Tracer_setup.c
Log:
Read multiple extraq

Modified: mc/3D/CitcomS/trunk/lib/Pan_problem_misc_functions.c
===================================================================
--- mc/3D/CitcomS/trunk/lib/Pan_problem_misc_functions.c	2008-02-21 00:55:41 UTC (rev 11216)
+++ mc/3D/CitcomS/trunk/lib/Pan_problem_misc_functions.c	2008-02-21 00:56:29 UTC (rev 11217)
@@ -189,6 +189,54 @@
 }
 
 
+/*
+ * Scan input str to get a double vector *values. The vector length is from
+ * input len. The input str contains white-space seperated numbers. Return
+ * the number of columns read (can be less than len).
+ */
+static int scan_double_vector(const char *str, int len, double *values)
+{
+    char *nptr, *endptr;
+    int i;
+
+    /* cast to avoid compiler warning */
+    nptr = endptr = (char *) str;
+
+    for (i = 0; i < len; ++i) {
+        values[i] = strtod(nptr, &endptr);
+        if (nptr == endptr) {
+            /* error: no conversion is performed */
+            return i;
+        }
+        nptr = endptr;
+    }
+
+    /** debug **
+    for (i = 0; i < len; ++i) fprintf(stderr, "%e, ", values[i]);
+    fprintf(stderr, "\n");
+    /**/
+    return len;
+}
+
+
+/*
+ * From input file, read a line, which contains white-space seperated numbers
+ * of lenght num_columns, store the numbers in a double array, return the
+ * number of columns read (can be less than num_columns).
+ */
+int read_double_vector(FILE *in, int num_columns, double *fields)
+{
+    char buffer[256], *p;
+
+    p = fgets(buffer, 255, in);
+    if (!p) {
+        return 0;
+    }
+
+    return scan_double_vector(buffer, num_columns, fields);
+}
+
+
 /* Read in a file containing previous values of a field. The input in the parameter
    file for this should look like: `previous_name_file=string' and `previous_name_column=int'
    where `name' is substituted by the argument of the function.

Modified: mc/3D/CitcomS/trunk/lib/Tracer_setup.c
===================================================================
--- mc/3D/CitcomS/trunk/lib/Tracer_setup.c	2008-02-21 00:55:41 UTC (rev 11216)
+++ mc/3D/CitcomS/trunk/lib/Tracer_setup.c	2008-02-21 00:56:29 UTC (rev 11217)
@@ -861,6 +861,7 @@
     int icushion;
     int i, j;
 
+    int read_double_vector(FILE *in, int num_columns, double *fields);
     int icheck_processor_shell();
     int isum_tracers();
     void sphere_to_cart();
@@ -869,7 +870,7 @@
 
     double x,y,z;
     double theta,phi,rad;
-    double extra[100];
+    double buffer[100];
 
     FILE *fptracer;
 
@@ -900,21 +901,20 @@
         allocate_tracer_arrays(E,j,iestimate);
 
         for (kk=1;kk<=number_of_tracers;kk++) {
-            fgets(input_s,200,fptracer);
-            if (E->trace.number_of_extra_quantities==0) {
-                sscanf(input_s,"%lf %lf %lf\n",&theta,&phi,&rad);
-            }
-            else if (E->trace.number_of_extra_quantities==1) {
-                sscanf(input_s,"%lf %lf %lf %lf\n",&theta,&phi,&rad,&extra[0]);
-            }
-            /* XXX: if E->trace.number_of_extra_quantities is greater than 1 */
-            /* this part has to be changed... */
-            else {
-                fprintf(E->trace.fpt,"ERROR(read tracer file)-huh?\n");
+            int len, ncol;
+            ncol = 3 + E->trace.number_of_extra_quantities;
+
+            len = read_double_vector(fptracer, ncol, buffer);
+            if (len != ncol) {
+                fprintf(E->trace.fpt,"ERROR(read tracer file) - wrong input file format: %s\n", E->trace.tracer_file);
                 fflush(E->trace.fpt);
                 exit(10);
             }
 
+            theta = buffer[0];
+            phi = buffer[1];
+            rad = buffer[2];
+
             sphere_to_cart(E,theta,phi,rad,&x,&y,&z);
 
 
@@ -950,7 +950,7 @@
             E->trace.basicq[j][5][E->trace.ntracers[j]]=z;
 
             for (i=0; i<E->trace.number_of_extra_quantities; i++)
-                E->trace.extraq[j][i][E->trace.ntracers[j]]=extra[i];
+                E->trace.extraq[j][i][E->trace.ntracers[j]]=buffer[i+3];
 
         } /* end kk, number of tracers */
 
@@ -992,8 +992,8 @@
 
     double rdum1;
     double theta,phi,rad;
-    double extra[100];
     double x,y,z;
+    double buffer[100];
 
     void sphere_to_cart();
 
@@ -1051,21 +1051,20 @@
         E->trace.ntracers[j]=numtracers;
 
         for (kk=1;kk<=numtracers;kk++) {
-            fgets(input_s,200,fp1);
-            if (E->trace.number_of_extra_quantities==0) {
-                sscanf(input_s,"%lf %lf %lf\n",&theta,&phi,&rad);
-            }
-            else if (E->trace.number_of_extra_quantities==1) {
-                sscanf(input_s,"%lf %lf %lf %lf\n",&theta,&phi,&rad,&extra[0]);
-            }
-            /* XXX: if E->trace.number_of_extra_quantities is greater than 1 */
-            /* this part has to be changed... */
-            else {
-                fprintf(E->trace.fpt,"ERROR(read_old_tracer_file)-huh?\n");
+            int len, ncol;
+            ncol = 3 + E->trace.number_of_extra_quantities;
+
+            len = read_double_vector(fp1, ncol, buffer);
+            if (len != ncol) {
+                fprintf(E->trace.fpt,"ERROR(read_old_tracer_file) - wrong input file format: %s\n", output_file);
                 fflush(E->trace.fpt);
                 exit(10);
             }
 
+            theta = buffer[0];
+            phi = buffer[1];
+            rad = buffer[2];
+
             sphere_to_cart(E,theta,phi,rad,&x,&y,&z);
 
             /* it is possible that if on phi=0 boundary, significant digits can push phi over 2pi */
@@ -1080,7 +1079,7 @@
             E->trace.basicq[j][5][kk]=z;
 
             for (i=0; i<E->trace.number_of_extra_quantities; i++)
-                E->trace.extraq[j][i][kk]=extra[i];
+                E->trace.extraq[j][i][kk]=buffer[i+3];
 
         }
 



More information about the cig-commits mailing list