From 3714256e9f1f253fa41e675283e74e04ac1237f2 Mon Sep 17 00:00:00 2001 From: rhaas Date: Sat, 10 Aug 2013 08:01:28 +0000 Subject: add option "alias" to read datasets into different variables Extents the file readers capabilities to not only take a cctk_iteration string but also a string alias. A value of IO::filereader_vars = "hydrobase::vel[0]{alias=admbase::shiftx}" will read the datasets admbase::shiftx into the variable hydrobase::vel[0]. The idea is to be able to read variables of evolution thorns into postprocessing thorns. git-svn-id: http://svn.cactuscode.org/arrangements/CactusBase/IOUtil/trunk@304 b32723a9-ab3a-4a60-88e2-2e5d99d7c17a --- src/CheckpointRecovery.c | 58 +++++++++++++++++++++++++++++++++++++++++++++--- src/ioGH.h | 6 +++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/CheckpointRecovery.c b/src/CheckpointRecovery.c index 328053e..cac3690 100644 --- a/src/CheckpointRecovery.c +++ b/src/CheckpointRecovery.c @@ -426,8 +426,12 @@ int IOUtil_RecoverVarsFromDatafiles (cGH *GH, if (CCTK_NumVars () > 0) { + void *calldata[2]; myGH->do_inVars = calloc (CCTK_NumVars (), sizeof (CCTK_INT)); - if (CCTK_TraverseString (in_vars, SetInputFlag, myGH->do_inVars, + myGH->alias = calloc (CCTK_NumVars (), sizeof (char *)); + calldata[0] = myGH->do_inVars; + calldata[1] = myGH->alias; + if (CCTK_TraverseString (in_vars, SetInputFlag, calldata, CCTK_GROUP_OR_VAR) < 0) { CCTK_WARN (myGH->stop_on_parse_errors ? 0 : 1, @@ -437,6 +441,7 @@ int IOUtil_RecoverVarsFromDatafiles (cGH *GH, else { myGH->do_inVars = NULL; + myGH->alias = NULL; } num_recovered_vars = 0; @@ -492,6 +497,18 @@ int IOUtil_RecoverVarsFromDatafiles (cGH *GH, myGH->do_inVars = NULL; } + if (myGH->alias) + { + const int numVars = CCTK_NumVars (); + for (int i = 0; i < numVars; i++) + { + if (myGH->alias[i]) + free ((void*)myGH->alias[i]); + } + free (myGH->alias); + myGH->alias = NULL; + } + return (num_recovered_vars); } @@ -1085,17 +1102,22 @@ static char *DecodeString (const char *string) /* callback for CCTK_TraverseString() to set the input flag for the given variable */ -static void SetInputFlag (int vindex, const char *optstring, void *flags) +static void SetInputFlag (int vindex, const char *optstring, void *calldata) { int table, iterator; char key[128]; CCTK_INT type, nelems; - CCTK_INT *do_inVars = (CCTK_INT *) flags; + void **flags = (void**)calldata; + CCTK_INT *do_inVars = (CCTK_INT *) flags[0]; + const char **alias = (const char **) flags[1]; /* default -1 is to read the last iteration number from the file */ do_inVars[vindex] = -1; + /* default is NULL to use CCTK_FullName() */ + alias[vindex] = NULL; + if (optstring) { table = Util_TableCreateFromString (optstring); @@ -1124,6 +1146,36 @@ static void SetInputFlag (int vindex, const char *optstring, void *flags) Util_TableDeleteKey (table, "cctk_iteration"); } + if (Util_TableQueryValueInfo (table, &type, &nelems, "alias") >0) + { + if (type == CCTK_VARIABLE_CHAR) + { + char *buf = malloc(nelems+1); + if (! (buf)) + { + CCTK_VError (__LINE__, __FILE__,"IOUtil", + "Could not allocate %d bytes", nelems+1); + } + int len = Util_TableGetString (table, nelems+1, buf, "alias"); + if (len != nelems) + { + CCTK_VError (__LINE__, __FILE__,"IOUtil", + "Unexpected length %d of 'alias' option value in '%s'. Expected length %d.", + len, optstring, nelems); + } + + alias[vindex] = buf; + } + else + { + CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING, + "Invalid value for option 'alias' in option " + "string '%s' (must be a string)", optstring); + CCTK_WARN (1, "Option will be ignored by file reader routines"); + } + Util_TableDeleteKey (table, "alias"); + } + /* warn about other options */ for (iterator = Util_TableItCreate (table); Util_TableItQueryIsNonNull (iterator) > 0 && diff --git a/src/ioGH.h b/src/ioGH.h index 998e6bf..7b5b16f 100644 --- a/src/ioGH.h +++ b/src/ioGH.h @@ -16,6 +16,10 @@ extern "C" { #endif +/* advertise that this is the new version of this API, which has 'alias' + * members in the grid extension */ +#define IOUTIL_IOGH_HAS_ALIAS 1 + /* IOUtil's GH extension structure */ typedef struct @@ -36,6 +40,8 @@ typedef struct CCTK_INT *do_inVars; /* flags indicating to read in variable i with iteration number do_inVars[i] (or -1 to read the last iteration */ + const char **alias; /* name under which a variable appears in data files. + If NULL, use CCTK_FullName() */ } ioGH; -- cgit v1.2.3