1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 void
 UpdateDatabase(
    DbInfo_t *dbInfo,            /* [IN/OUT] database info object (currentFile member can be modified) */
    int stepNum,                 /* [IN] the current step number in the sequence starting from zero */
    int numSteps,                /* [IN] total number of steps to be output (>=1) */
    hbool_t do_multifile,        /* [IN] boolean to indicate if each step will go to a different supplemental file */
    int numFields,               /* [IN] the number of fields on the mesh */
    SAF_Field *theFields         /* [IN] array of length numFields of the field handles */
 )
 {
   char tmpName[32];
   static int stepZero = 0;
   int i;
   float stepVal;
   SAF_FieldTmpl *fieldTmpls;
   SAF_StateGrp currentStateGrp;
   SAF_StateTmpl currentStateTmpl;
   SAF_Suite currentSuite;
   SAF_Rel rel;
   SAF_Unit usec;

   /* local vars obtained from dbinfo object */
   SAF_Db *db = dbInfo->db;
   SAF_Set aggregateMesh = dbInfo->mainMesh;
   SAF_Set currentMesh = dbInfo->currentMesh;
   SAF_Db *stepFile = dbInfo->currentFile;

   /* link currentMesh into aggregate at the current position */
   saf_declare_subset_relation(SAF_ALL, db, &aggregateMesh, &currentMesh, SAF_COMMON(SAF_SELF(db)), SAF_TUPLES,
                               H5I_INVALID_HID, NULL, H5I_INVALID_HID, NULL, &rel);
   saf_write_subset_relation(SAF_ALL, &rel, SAF_INT, &stepNum, H5I_INVALID_HID, NULL, stepFile);

   /* get the handle for the seconds unit */
   saf_find_one_unit(db, "second", &usec);

   /* obtain all the field templates for all the fields */
   fieldTmpls = calloc((size_t)numFields, sizeof *fieldTmpls);
   for (i = 0; i < numFields; i++)
     saf_describe_field(SAF_ALL, theFields+i, fieldTmpls+i, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                        NULL, NULL, NULL, NULL, NULL, NULL);

   /* get the handle for the seconds unit */
   saf_find_one_unit(db, "second", &usec);

   /* create the suite */
   sprintf(tmpName, "stateSuite_%03d", stepNum);
   saf_declare_suite(SAF_ALL, db, tmpName, &currentMesh, NULL, &currentSuite);

   /* create a new state template */
   sprintf(tmpName, "stateTmpl_%03d", stepNum);
   saf_declare_state_tmpl(SAF_ALL, db, tmpName, numFields, fieldTmpls, &currentStateTmpl);
   free(fieldTmpls);

   /* create a new state group */
   sprintf(tmpName, "stateGrp_%03d", stepNum);
   saf_declare_state_group(SAF_ALL, db, tmpName, &currentSuite, &currentMesh, &currentStateTmpl, SAF_QTIME,
                           &usec, SAF_FLOAT, &currentStateGrp);

   /* Issue: we always write to the 0'th index of this new suite */
   stepVal = (float) stepNum;
   saf_write_state(SAF_ALL, &currentStateGrp, stepZero, &currentMesh, SAF_FLOAT, (void*) &stepVal, theFields);


   /* work to do for the next step if there is a next step */
   if (stepNum + 1 < numSteps)
     {
         dbInfo->lastMesh = dbInfo->currentMesh;

       /* create a new supplemental file */
       if (do_multifile) {
           SAF_DbProps *dbprops = saf_createProps_database();
           saf_setProps_Clobber(dbprops);
           sprintf(tmpName,"step_%03d", stepNum+1);
           stepFile = saf_open_database(tmpName, dbprops);
           saf_freeProps_database(dbprops);

           /* update the currentFile handle in the dbinfo object */
           dbInfo->currentFile = stepFile;
         }
     }

   /* flush the database */
   saf_update_database(db);

   /* keep track of the last mesh */
   dbInfo->lastMesh = dbInfo->currentMesh;
 }