[SCM] Gerris Flow Solver branch, upstream, updated. b3aa46814a06c9cb2912790b23916ffb44f1f203
Cedric Penard
cedric.penard at ifremer.fr
Fri May 15 02:54:08 UTC 2009
The following commit has been merged in the upstream branch:
commit 9ea7b8c05d66bfbfda1e5e899c535ee3a5b4f2ee
Author: Cedric Penard <cedric.penard at ifremer.fr>
Date: Mon Mar 19 11:39:13 2007 +1100
Cartesian functions
darcs-hash:20070319003913-a3e3b-29bfb1f7878d8e80445a0d3b20cee5f99a19115c.gz
diff --git a/src/cartesian.c b/src/cartesian.c
index 70ef245..738840d 100644
--- a/src/cartesian.c
+++ b/src/cartesian.c
@@ -35,7 +35,8 @@ static void gfs_cartesian_grid_read (GtsObject ** o, GtsFile * fp)
return;
/* do object-specific read here */
- if (fp->type == '\n')
+
+ while (fp->type == '\n')
gts_file_next_token (fp);
if (fp->type != GTS_INT) {
gts_file_error (fp, "expecting an integer (N)");
@@ -44,10 +45,21 @@ static void gfs_cartesian_grid_read (GtsObject ** o, GtsFile * fp)
cgd->N = atoi (fp->token->str);
gts_file_next_token (fp);
- cgd->n = g_malloc (cgd->N*sizeof (guint));
+ cgd->name = g_malloc0 ((cgd->N + 1)*sizeof(char*));
+
+ for (i = 0; i < cgd->N + 1; i++) {
+ if (fp->type != GTS_STRING) {
+ gts_file_error (fp, "expecting a string (name[%d])", i);
+ return;
+ }
+ cgd->name[i] = g_strdup (fp->token->str);
+ gts_file_next_token (fp);
+ }
+
+ cgd->n = g_malloc(cgd->N*sizeof(guint));
for (i = 0; i < cgd->N; i++) {
- if (fp->type == '\n')
+ while (fp->type == '\n')
gts_file_next_token (fp);
if (fp->type != GTS_INT) {
gts_file_error (fp, "expecting an integer (n[%d])", i);
@@ -59,7 +71,7 @@ static void gfs_cartesian_grid_read (GtsObject ** o, GtsFile * fp)
taille *= cgd->n[i];
}
- cgd->x = g_malloc (cgd->N*sizeof (gint));
+ cgd->x = g_malloc0 (cgd->N*sizeof (gint));
for (i = 0; i < cgd->N; i++) {
cgd->x[i] = g_malloc (cgd->n[i]*sizeof (gdouble));
@@ -103,9 +115,12 @@ static void gfs_cartesian_grid_write (GtsObject * o, FILE * fp)
for (i = 0; i < cgd->N; i++)
taille *= cgd->n[i];
- fprintf (fp, "%d\n", cgd->N);
- for (i = 0; i < cgd->N; i++)
- fprintf (fp, "%d\n", cgd->n[i]);
+ fprintf (fp, "%d ", cgd->N);
+ for (i = 0; i < cgd->N+1; i++)
+ fprintf (fp, "%s ", cgd->name[i]);
+ fputc ('\n', fp);
+ for (i=0;i<cgd->N;i++)
+ fprintf (fp,"%d\n",cgd->n[i]);
for (i = 0; i < cgd->N; i++)
for (j = 0; j < cgd->n[i]; j++)
@@ -119,12 +134,19 @@ static void gfs_cartesian_grid_destroy (GtsObject * object)
{
/* do object-specific cleanup here */
GfsCartesianGrid * cgd = GFS_CARTESIAN_GRID (object);
- guint i;
+ guint i;
+ if (cgd->name) {
+ for (i = 0; i < cgd->N+1; i++)
+ g_free (cgd->name[i]);
+ g_free (cgd->name);
+ }
g_free (cgd->n);
- for (i = 0; i < cgd->N; i++)
- g_free (cgd->x[i]);
- g_free (cgd->x);
+ if (cgd->x) {
+ for (i = 0; i < cgd->N; i++)
+ g_free (cgd->x[i]);
+ g_free (cgd->x);
+ }
g_free (cgd->v);
/* do not forget to call destroy method of the parent */
diff --git a/src/cartesian.h b/src/cartesian.h
index 18e9a39..602db15 100644
--- a/src/cartesian.h
+++ b/src/cartesian.h
@@ -33,10 +33,12 @@ typedef struct _GfsCartesianGrid GfsCartesianGrid;
struct _GfsCartesianGrid {
/*< private >*/
GtsObject parent;
- guint N; /* Number of dimension */
- guint * n; /* Size of each dimension */
- gdouble ** x; /* Position of each point in the grid */
- gdouble * v; /* Data */
+ guint N; /* Number of dimension */
+ guint * n; /* Size of each dimension */
+ gdouble ** x; /* Position of each point in the grid */
+ gdouble * v; /* Data */
+ gchar ** name; /* Name of each dimension */
+
/*< public >*/
/* add extra data here (if public) */
diff --git a/src/utils.c b/src/utils.c
index 051fc82..1eb19e9 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -27,6 +27,7 @@
#include "config.h"
#include "solid.h"
#include "simulation.h"
+#include "cartesian.h"
/**
* @c: a character.
@@ -189,6 +190,8 @@ struct _GfsFunction {
GfsFunctionFunc f;
gchar * sname;
GtsSurface * s;
+ GfsCartesianGrid * g;
+ guint index[4];
GfsVariable * v;
GfsDerivedVariable * dv;
gdouble val;
@@ -217,6 +220,77 @@ static GtsSurface * read_surface (gchar * name, GtsFile * fp)
return s;
}
+static GfsCartesianGrid * read_cartesian_grid (gchar * name, GtsFile * fp)
+{
+ FILE * fptr = fopen (name, "r");
+ GtsFile * fp1;
+ GfsCartesianGrid * grid;
+ GtsObjectClass * klass;
+
+ if (fptr == NULL) {
+ gts_file_error (fp, "cannot open file `%s'", name);
+ return NULL;
+ }
+
+ fp1 = gts_file_new (fptr);
+
+ klass = gfs_cartesian_grid_class ();
+
+ grid = gfs_cartesian_grid_new (klass);
+
+ (* klass->read)((GtsObject **)&grid, fp1);
+
+ if (fp1->type == GTS_ERROR) {
+ gts_file_error (fp, "%s:%d:%d: %s", name, fp1->line, fp1->pos, fp1->error);
+ gts_object_destroy (GTS_OBJECT(grid));
+ grid = NULL;
+ }
+ gts_file_destroy (fp1);
+ fclose (fptr);
+ return grid;
+}
+
+static gboolean fit_index_dimension (GfsCartesianGrid * grid, guint * val, GtsFile * fp)
+{
+ guint i,j;
+ gchar liste[]={'x','y','z','t'};
+
+ if (grid->N > 4)
+ return FALSE;
+
+ for(i = 0; i < grid->N; i++) {
+ for (j = 0; j < 4 && *grid->name[i] != liste[j]; j++);
+ if (j == 4)
+ return FALSE;
+ val[i]=j;
+ }
+ // fprintf(stderr,"%d %d %d %d\n",val[0],val[1],val[2],val[3]);
+ return TRUE;
+}
+
+static gdouble interpolated_cgd (GfsFunction * f, FttVector * p)
+{
+ gdouble vecteur[4];
+ gdouble val;
+ guint i;
+
+ for (i = 0; i < f->g->N; i++)
+ switch (f->index[i]) {
+ case 0: vecteur[i] = p->x; break;
+ case 1: vecteur[i] = p->y; break;
+ case 2: vecteur[i] = p->z; break;
+ case 3: vecteur[i] = gfs_object_simulation (f)->time.t; break;
+ default: g_assert_not_reached ();
+ }
+
+ if(!gfs_cartesian_grid_interpolate (f->g, vecteur, &val))
+ return 0.;
+ return val;
+}
+
+
+
+
static gboolean load_module (GfsFunction * f, GtsFile * fp, gchar * mname)
{
gchar * path;
@@ -475,6 +549,16 @@ static void function_read (GtsObject ** o, GtsFile * fp)
gts_file_next_token (fp);
return;
}
+ else if (strlen (f->expr->str) > 3 &&
+ !strcmp (&(f->expr->str[strlen (f->expr->str) - 4]), ".cgd")) {
+ if ((f->g = read_cartesian_grid (f->expr->str, fp)) == NULL)
+ return;
+ if (!fit_index_dimension (f->g, f->index, fp))
+ return;
+ f->sname = g_strdup (f->expr->str);
+ gts_file_next_token (fp);
+ return;
+ }
else if ((f->v = gfs_variable_from_name (domain->variables, f->expr->str))) {
gts_file_next_token (fp);
return;
@@ -635,6 +719,8 @@ static void function_write (GtsObject * o, FILE * fp)
fprintf (fp, " %s", f->v->name);
else if (f->s)
fprintf (fp, " %s", f->sname);
+ else if (f->g)
+ fprintf (fp, " %s", f->sname);
else
fprintf (fp, " %g", f->val);
}
@@ -649,6 +735,10 @@ static void function_destroy (GtsObject * object)
gts_object_destroy (GTS_OBJECT (f->s));
g_free (f->sname);
}
+ if (f->g) {
+ gts_object_destroy (GTS_OBJECT (f->g));
+ g_free (f->sname);
+ }
(* GTS_OBJECT_CLASS (gfs_function_class ())->parent_class->destroy)
(object);
@@ -770,10 +860,14 @@ gdouble gfs_function_value (GfsFunction * f, FttCell * cell)
if (f->s) {
FttVector p;
-
gfs_cell_cm (cell, &p);
return interpolated_value (f, &p);
}
+ else if (f->g) {
+ FttVector p;
+ gfs_cell_cm (cell, &p);
+ return interpolated_cgd (f, &p);
+ }
else if (f->v)
return GFS_VARIABLE (cell, f->v->i);
else if (f->dv)
--
Gerris Flow Solver
More information about the debian-science-commits
mailing list