[cig-commits] r9143 - cs/avm/trunk

tan2 at geodynamics.org tan2 at geodynamics.org
Fri Jan 25 14:38:36 PST 2008


Author: tan2
Date: 2008-01-25 14:38:36 -0800 (Fri, 25 Jan 2008)
New Revision: 9143

Modified:
   cs/avm/trunk/avm-partition.c
Log:
Reading variable numbers of columns

Modified: cs/avm/trunk/avm-partition.c
===================================================================
--- cs/avm/trunk/avm-partition.c	2008-01-25 17:22:22 UTC (rev 9142)
+++ cs/avm/trunk/avm-partition.c	2008-01-25 22:38:36 UTC (rev 9143)
@@ -30,10 +30,78 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-
 #define IREGION_CRUST_MANTLE 1
+#define MAX_NUM_COMPOSITIONS 10
 
+/*
+ * This function takes the temperatur (in Kelvin), pressure (in Pascal), and
+ * composition (a vector of concentration) at a point, and returns the
+ * density, bulk and shear moduli (in Pascal).
+ */
+void elastic_model(double temperature, double pressure, double rho,
+                   double *compositions, int num_compositions,
+                   double *ks, double *mu)
+{
+    /* XXX: user modification here */
+    *ks = *mu = 1.0;
+    return;
+}
 
+
+/*
+ * Scan input str to get a double vector *values. The vector length is from
+ * input len. The input str contains white-space seperated numbers.
+ */
+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 -1;
+        }
+        nptr = endptr;
+    }
+
+    return len;
+}
+
+
+int read_convection_variables(FILE *in, int num_compositions,
+                              int *rank, int *glob,
+                              double *temperature, double *pressure,
+                              double *rho, double *compositions)
+{
+    char buffer[256], *p;
+    double temp[MAX_NUM_COMPOSITIONS + 5];
+    int c, i, j;
+
+    p = fgets(buffer, 255, in);
+    if (!p) {
+        return 0;
+    }
+
+    c = scan_double_vector(buffer, num_compositions, temp);
+
+    *rank = (int) temp[0];
+    *glob = (int) temp[1];
+    *temperature = temp[2];
+    *pressure = temp[3];
+    *rho = temp[4];
+
+    for (i = 0, j = 5; i < num_compositions; ++i, ++j) {
+        compositions[i] = temp[j];
+    }    
+    return c;
+}
+
+
 int main(int argc, char **argv)
 {
     FILE **out; int nout;
@@ -48,10 +116,16 @@
     out = 0;
     for (i = 1; i < argc; ++i) {
         char *ifilename;
+        char buffer[256], *p;
         FILE *in;
-        int c;
-        double rho, kappa, mu, rank, glob;
-        
+        int c, num_columns;
+        int num_compositions;
+        int rank, glob;
+        double temperature, pressure, rho;
+        double compositions[MAX_NUM_COMPOSITIONS];
+        double ks, mu;
+
+        /* open input file(s) */
         ifilename = argv[i];
         in = fopen(ifilename, "r");
         if (!in) {
@@ -60,21 +134,35 @@
             return 1;
         }
         
-        /* skip header (?) */
-        do {
-            c = fgetc(in);
-            if (c == EOF) {
-                fprintf(stderr, "%s: bad input file: `%s'\n", argv[0], ifilename);
-                return 1;
-            }
-        } while (c != '\n');
-        
-        while (fscanf(in, "%lf %lf %lf %lf %lf\n", &rho, &kappa, &mu, &rank, &glob) == 5) {
-            int irank, nproc;
+        /* read header */
+        p = fgets(buffer, 255, in);
+        if (!p) {
+            fprintf(stderr, "%s: bad input file: `%s'\n", argv[0], ifilename);
+            return 1;
+        }
+
+        c = scanf(buffer, "%* %d", &num_columns);
+        if (c != 2) {
+            fprintf(stderr, "%s: bad input file: `%s'\n", argv[0], ifilename);
+            return 1;
+        }
+
+        /* except the first five columns, the rest columns are compositions */
+        num_compositions = num_columns - 5;
+        if (num_compositions > MAX_NUM_COMPOSITIONS) {
+            fprintf(stderr, "%s: too many compositions in input file: `%s'\n", argv[0], ifilename);
+            return 1;
+        }
+
+        while (read_convection_variables(in, num_compositions,
+                                         &rank, &glob,
+                                         &temperature, &pressure,
+                                         &rho, compositions)
+               >= (5 + num_compositions) ) {
+            int nproc;
             
             /* open more output files as needed */
-            irank = (int)rank;
-            nproc = irank + 1;
+            nproc = rank + 1;
             if (nproc > nout) {
                 out = (FILE **)realloc(out, nproc * sizeof(FILE *));
                 for ( ; nout < nproc; ++nout) {
@@ -83,8 +171,12 @@
                     out[nout] = fopen(ofilename, "w");
                 }
             }
-            
-            fprintf(out[irank], "%d %lf %lf %lf\n", (int)glob, rho, kappa, mu);
+
+            /* convert convection variables to elastic moduli */
+            elastic_model(temperature, pressure, rho,
+                          compositions, num_compositions,
+                          &ks, &mu);
+            fprintf(out[rank], "%d %lf %lf %lf\n", glob, rho, ks, mu);
         }
         
         fclose(in);



More information about the cig-commits mailing list