[SCM] Gerris Flow Solver branch, upstream, updated. b3aa46814a06c9cb2912790b23916ffb44f1f203

Stephane Popinet popinet at users.sf.net
Fri May 15 02:54:33 UTC 2009


The following commit has been merged in the upstream branch:
commit ffb5f8a88a73ccb3dbf0eda18a94f13856d61bcc
Author: Stephane Popinet <popinet at users.sf.net>
Date:   Fri Aug 31 12:42:54 2007 +1000

    Constants can be expressions (not for all objects yet)
    
    darcs-hash:20070831024254-d4795-f86ef30de5a8f1c53cfefd070f34bcf0d4d3f58d.gz

diff --git a/src/adaptive.c b/src/adaptive.c
index a17b7b7..643a0ff 100644
--- a/src/adaptive.c
+++ b/src/adaptive.c
@@ -179,12 +179,9 @@ static void gfs_adapt_read (GtsObject ** o, GtsFile * fp)
 	return;
       }
       gts_file_next_token (fp);
-      if (fp->type != GTS_INT && fp->type != GTS_FLOAT) {
-	gts_file_error (fp, "expecting a number (cmax)");
+      a->cmax = gfs_read_constant (fp, gfs_object_simulation (*o));
+      if (fp->type == GTS_ERROR)
 	return;
-      }
-      a->cmax = atof (fp->token->str);
-      gts_file_next_token (fp);
     }
     else if (!strcmp (fp->token->str, "weight")) {
       gts_file_next_token (fp);
@@ -193,12 +190,9 @@ static void gfs_adapt_read (GtsObject ** o, GtsFile * fp)
 	return;
       }
       gts_file_next_token (fp);
-      if (fp->type != GTS_INT && fp->type != GTS_FLOAT) {
-	gts_file_error (fp, "expecting a number (weight)");
+      a->weight = gfs_read_constant (fp, gfs_object_simulation (*o));
+      if (fp->type == GTS_ERROR)      
 	return;
-      }
-      a->weight = atof (fp->token->str);
-      gts_file_next_token (fp);
     }
     else if (!strcmp (fp->token->str, "c")) {
       GfsDomain * domain;
diff --git a/src/boundary.c b/src/boundary.c
index 633899b..f6c1bf4 100644
--- a/src/boundary.c
+++ b/src/boundary.c
@@ -358,12 +358,7 @@ static void bc_navier_read (GtsObject ** o, GtsFile * fp)
   if (fp->type == GTS_ERROR)
     return;
   
-  if (fp->type != GTS_INT && fp->type != GTS_FLOAT) {
-    gts_file_error (fp, "expecting a number (slip length)");
-    return;
-  }
-  GFS_BC_NAVIER (*o)->lambda = atof (fp->token->str);
-  gts_file_next_token (fp);
+  GFS_BC_NAVIER (*o)->lambda = gfs_read_constant (fp, gfs_object_simulation (*o));
 }
 
 static void bc_navier_write (GtsObject * o, FILE * fp)
diff --git a/src/event.c b/src/event.c
index 817d288..ff967fb 100644
--- a/src/event.c
+++ b/src/event.c
@@ -1103,15 +1103,10 @@ static void gfs_event_harmonic_read (GtsObject ** o, GtsFile * fp)
   }
 
   do {
-    gdouble omega;
-
-    if (fp->type != GTS_INT && fp->type != GTS_FLOAT) {
-      gts_file_error (fp, "expecting a number (omega[%d])", s->omega->len);
+    gdouble omega = gfs_read_constant (fp, domain);
+    if (fp->type == GTS_ERROR)      
       return;
-    }
-    omega = atof (fp->token->str);
     g_array_append_val (s->omega, omega);
-    gts_file_next_token (fp);
   } while (fp->type != '\n' && fp->type != '{');
 
   s->Mn = gfs_matrix_new (2*s->omega->len + 1, sizeof (gdouble));
@@ -1389,11 +1384,9 @@ static void gfs_event_stop_read (GtsObject ** o, GtsFile * fp)
   }
   gts_file_next_token (fp);
 
-  if (fp->type != GTS_INT && fp->type != GTS_FLOAT) {
-    gts_file_error (fp, "expecting a number (max)");
+  s->max = gfs_read_constant (fp, domain);
+  if (fp->type == GTS_ERROR)      
     return;
-  }
-  s->max = atof (fp->token->str);
   s->oldv = gfs_domain_add_variable (domain, NULL, NULL);
   /* fixme: the lines below are necessary in the general case (e.g. when dealing with a VOF tracer)
    * but will crash if s->oldv is not of the same class as s->v.
@@ -1401,20 +1394,15 @@ static void gfs_event_stop_read (GtsObject ** o, GtsFile * fp)
    * s->oldv->coarse_fine = s->v->coarse_fine;
    */
 
-  if (fp->next_token != '\n') {
-    gts_file_next_token (fp);
-    if (fp->type != GTS_STRING) {
-      gts_file_error (fp, "expecting a string (diff)");
-      return;
-    }
+  if (fp->type == GTS_STRING) {
     if (!(s->diff = gfs_variable_from_name (domain->variables, fp->token->str)) &&
 	!(s->diff = gfs_domain_add_variable (domain, fp->token->str, 
 					     "Stopping field difference"))) {
       gts_file_error (fp, "`%s' is a reserved keyword", fp->token->str);
       return;
     }
+    gts_file_next_token (fp);
   }
-  gts_file_next_token (fp);
 }
 
 static void gfs_event_stop_destroy (GtsObject * o)
@@ -1917,16 +1905,14 @@ static void gfs_event_filter_read (GtsObject ** o, GtsFile * fp)
     return;
   }
   gts_file_next_token (fp);
-  
-  if (fp->type != GTS_INT && fp->type != GTS_FLOAT) {
-    gts_file_error (fp, "expecting a number (time scale)");
+
+  GFS_EVENT_FILTER (*o)->scale = gfs_read_constant (fp, domain);
+  if (fp->type == GTS_ERROR)
     return;
-  }
-  if ((GFS_EVENT_FILTER (*o)->scale = atof (fp->token->str)) <= 0.) {
+  if (GFS_EVENT_FILTER (*o)->scale <= 0.) {
     gts_file_error (fp, "time scale must be strictly positive");
     return;
-  }  
-  gts_file_next_token (fp);
+  }
 }
 
 static void gfs_event_filter_write (GtsObject * o, FILE * fp)
diff --git a/src/ocean.c b/src/ocean.c
index 56c466d..d04e4cd 100644
--- a/src/ocean.c
+++ b/src/ocean.c
@@ -1004,12 +1004,9 @@ static void gfs_source_friction_read (GtsObject ** o, GtsFile * fp)
   }
   gts_file_next_token (fp);
 
-  if (fp->type != GTS_INT && fp->type != GTS_FLOAT) {
-    gts_file_error (fp, "expecting a number (f)");
+  GFS_SOURCE_FRICTION (*o)->f = gfs_read_constant (fp, domain);
+  if (fp->type == GTS_ERROR)
     return;
-  }
-  GFS_SOURCE_FRICTION (*o)->f = atof (fp->token->str);
-  gts_file_next_token (fp);
 
   for (c = 0; c <  FTT_DIMENSION; c++)
     GFS_SOURCE_FRICTION (*o)->u[c] = gfs_temporary_variable (domain);
diff --git a/src/output.c b/src/output.c
index 884945b..a09653d 100644
--- a/src/output.c
+++ b/src/output.c
@@ -1484,18 +1484,15 @@ static void gfs_output_scalar_read (GtsObject ** o, GtsFile * fp)
 	return;
       }
       gts_file_next_token (fp);
-      if (fp->type != GTS_INT && fp->type != GTS_FLOAT) {
-	gts_file_error (fp, "expecting a number (min)");
+      output->min = gfs_read_constant (fp, gfs_object_simulation (*o));
+      if (fp->type == GTS_ERROR)
 	return;
-      }
-      output->min = atof (fp->token->str);
       if (output->min > output->max) {
 	gts_file_error (fp, "min `%g' must be smaller than or equal to max `%g'", 
 			output->min, output->max);
 	return;
       }
       output->autoscale = FALSE;
-      gts_file_next_token (fp);
     }
     else if (!strcmp (fp->token->str, "max")) {
       gts_file_next_token (fp);
@@ -1504,18 +1501,15 @@ static void gfs_output_scalar_read (GtsObject ** o, GtsFile * fp)
 	return;
       }
       gts_file_next_token (fp);
-      if (fp->type != GTS_INT && fp->type != GTS_FLOAT) {
-	gts_file_error (fp, "expecting a number (max)");
+      output->max = gfs_read_constant (fp, gfs_object_simulation (*o));
+      if (fp->type == GTS_ERROR)
 	return;
-      }
-      output->max = atof (fp->token->str);
       if (output->max < output->min) {
 	gts_file_error (fp, "max `%g' must be larger than or equal to min `%g'", 
 			output->max, output->min);
 	return;
       }
       output->autoscale = FALSE;
-      gts_file_next_token (fp);
     }
     else if (!strcmp (fp->token->str, "maxlevel")) {
       gts_file_next_token (fp);
diff --git a/src/simulation.c b/src/simulation.c
index df717eb..1abf1c1 100644
--- a/src/simulation.c
+++ b/src/simulation.c
@@ -1197,13 +1197,11 @@ void gfs_physical_params_read (GfsPhysicalParams * p, GfsDomain * domain, GtsFil
       gts_file_next_token (fp);
 
       if (!strcmp (id, "g")) {
-	if (fp->type != GTS_INT && fp->type != GTS_FLOAT) {
+	p->g = gfs_read_constant (fp, domain);
+	if (fp->type == GTS_ERROR) {
 	  g_free (id);
-	  gts_file_error (fp, "expecting a number");
 	  return;
 	}
-	p->g = atof (fp->token->str);
-	gts_file_next_token (fp);
       }
       else if (!strcmp (id, "alpha")) {
 	p->alpha = gfs_function_new (gfs_function_class (), 0.);
diff --git a/src/tension.c b/src/tension.c
index af2312b..c50a749 100644
--- a/src/tension.c
+++ b/src/tension.c
@@ -45,13 +45,7 @@ static void gfs_source_tension_generic_read (GtsObject ** o, GtsFile * fp)
   }
   gts_file_next_token (fp);
 
-  if (fp->type != GTS_INT && fp->type != GTS_FLOAT) {
-    gts_file_error (fp, "expecting a number (sigma)");
-    return;
-  }
-  s->sigma = atof (fp->token->str);
-
-  gts_file_next_token (fp);
+  s->sigma = gfs_read_constant (fp, domain);
 }
 
 static void gfs_source_tension_generic_write (GtsObject * o, FILE * fp)
@@ -745,10 +739,8 @@ static void variable_position_read (GtsObject ** o, GtsFile * fp)
 					      "coordinate of the interface defined by tracer",
 					      GFS_VARIABLE_CURVATURE (v)->f->name, NULL);
   gts_file_next_token (fp);
-  if (fp->type == GTS_INT || fp->type == GTS_FLOAT) {
-    v->ref = atof (fp->token->str);
-    gts_file_next_token (fp);
-  }
+  if (fp->type == GTS_INT || fp->type == GTS_FLOAT || fp->type == GTS_STRING)
+    v->ref = gfs_read_constant (fp, gfs_object_simulation (*o));
 }
 
 static void variable_position_write (GtsObject * o, FILE * fp)
diff --git a/src/utils.c b/src/utils.c
index bbd092b..0f44a68 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -195,7 +195,7 @@ struct _GfsFunction {
   GfsVariable * v;
   GfsDerivedVariable * dv;
   gdouble val;
-  gboolean spatial;
+  gboolean spatial, constant;
 };
 
 static GtsSurface * read_surface (gchar * name, GtsFile * fp)
@@ -554,7 +554,7 @@ static void function_read (GtsObject ** o, GtsFile * fp)
 	return;
       }
     }
-    else if (fp->type == GTS_STRING && !f->spatial) {
+    else if (fp->type == GTS_STRING && !f->spatial && !f->constant) {
       if (strlen (f->expr->str) > 3 &&
 	  !strcmp (&(f->expr->str[strlen (f->expr->str) - 4]), ".gts")) {
 	if ((f->s = read_surface (f->expr->str, fp)) == NULL)
@@ -608,7 +608,7 @@ static void function_read (GtsObject ** o, GtsFile * fp)
 	   fin);
     if (f->spatial)
       fputs ("#include <gerris/spatial.h>\n", fin);
-    else
+    else if (!f->constant)
       fputs ("#include <gerris/function.h>\n", fin);
     i = sim->globals;
     while (i) {
@@ -621,6 +621,8 @@ static void function_read (GtsObject ** o, GtsFile * fp)
       fputs ("double f (double x, double y, double z) {\n"
 	     "  _x = x; _y = y; _z = z;\n", 
 	     fin);
+    else if (f->constant)
+      fputs ("double f (void) {\n", fin);
     else {
       fputs ("typedef double (* Func) (const FttCell * cell,\n"
 	     "                         const FttCellFace * face,\n"
@@ -711,6 +713,16 @@ static void function_read (GtsObject ** o, GtsFile * fp)
     case SIGABRT: return;
     }
   }
+  if (fp->type == GTS_ERROR)
+    return;
+  if (f->constant && f->f) {
+    f->val = (* f->f) (NULL, NULL, NULL);
+    f->f = NULL;
+    if (f->module) g_module_close (f->module);
+    f->module = NULL;
+    if (f->expr) g_string_free (f->expr, TRUE);
+    f->expr = NULL;
+  }    
   gts_file_next_token (fp);
 }
 
@@ -1055,6 +1067,56 @@ gdouble gfs_function_spatial_value (GfsFunction * f, FttVector * p)
     return f->val;
 }
 
+/* GfsFunctionConstant: object */
+
+static void gfs_function_constant_init (GfsFunction * f)
+{
+  f->constant = TRUE;
+}
+
+GfsFunctionClass * gfs_function_constant_class (void)
+{
+  static GfsFunctionClass * klass = NULL;
+
+  if (klass == NULL) {
+    GtsObjectClassInfo gfs_function_info = {
+      "GfsFunctionConstant",
+      sizeof (GfsFunction),
+      sizeof (GfsFunctionClass),
+      (GtsObjectClassInitFunc) NULL,
+      (GtsObjectInitFunc) gfs_function_constant_init,
+      (GtsArgSetFunc) NULL,
+      (GtsArgGetFunc) NULL
+    };
+    klass = gts_object_class_new (GTS_OBJECT_CLASS (gfs_function_class ()),
+				  &gfs_function_info);
+  }
+
+  return klass;
+}
+
+/**
+ * gfs_read_constant:
+ * @fp: a #GtsFile.
+ * @domain: a #GfsDomain.
+ *
+ * Reads a constant value from @fp.
+ *
+ * Returns: the value of the constant or G_MAXDOUBLE if an error
+ * occured.
+ */
+gdouble gfs_read_constant (GtsFile * fp, gpointer domain)
+{
+  g_return_val_if_fail (fp != NULL, G_MAXDOUBLE);
+  g_return_val_if_fail (domain != NULL, G_MAXDOUBLE);
+
+  GfsFunction * f = gfs_function_new (gfs_function_constant_class (), 0.);
+  gfs_function_read (f, domain, fp);
+  gdouble val = fp->type == GTS_ERROR ? G_MAXDOUBLE : gfs_function_get_constant_value (f);
+  gts_object_destroy (GTS_OBJECT (f));
+  return val;
+}
+
 /**
  * gfs_object_class_from_name:
  * @name: the name of the class.
diff --git a/src/utils.h b/src/utils.h
index a19c5f7..eb5b4a3 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -94,6 +94,15 @@ GString *          gfs_function_expression  (GtsFile * fp,
 GfsFunctionClass * gfs_function_spatial_class (void);
 gdouble            gfs_function_spatial_value (GfsFunction * f, FttVector * p);
 
+/* GfsFunctionConstant: Header */
+
+#define GFS_IS_FUNCTION_CONSTANT(obj)         (gts_object_is_from_class (obj,\
+					       gfs_function_constant_class ()))
+
+GfsFunctionClass * gfs_function_constant_class (void);
+gdouble            gfs_read_constant           (GtsFile * fp,
+						gpointer domain);
+
 GtsObjectClass *   gfs_object_class_from_name (const gchar * name);
 
 void               gfs_eigenvalues          (gdouble a[FTT_DIMENSION][FTT_DIMENSION],

-- 
Gerris Flow Solver



More information about the debian-science-commits mailing list