Source

enzo-3.0 / src / enzo / grid / utilities / Grid_WriteGrid.C

The active_particles branch has multiple heads

  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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/***********************************************************************
/
/  GRID CLASS (WRITE OUT GRID)
/
/  written by: Greg Bryan
/  date:       November, 1994
/  modified1:  Robert Harkness, July 2002
/  modified2:  Robert Harkness, July 2006
/  modified3:  Robert Harkness, April 2008
/  modified4:  Matthew Turk, September 2009
/  modified5:  Michael Kuhlen, October 2010, HDF5 hierarchy
/  modified6:  Nathan Goldbaum, November 2011, Active Particle Support  
/
/  PURPOSE:
/
************************************************************************/
 
//  Write grid to file pointer fptr
//     (we assume that the grid is at an appropriate stopping point,
//      where the Old values aren't required)
 
#include <hdf5.h>
#include "preincludes.h"
#include "h5utilities.h"
 
#include "ErrorExceptions.h"
#include "macros_and_parameters.h"
#include "typedefs.h"
#include "global_data.h"
#include "Fluxes.h"
#include "GridList.h"
#include "ExternalBoundary.h"
#include "Grid.h"
#include "fortran.def"
#include "CosmologyParameters.h"
#include "ActiveParticle.h"

void my_exit(int status);
 
// HDF5 function prototypes
 

 
// function prototypes
 
void WriteListOfFloats(FILE *fptr, int N, FLOAT floats[]);
void WriteListOfInts(FILE *fptr, int N, int nums[]);
int WriteStringAttr(hid_t dset_id, char *Alabel, char *String, FILE *log_fptr);
int FindField(int field, int farray[], int numfields);

int GetUnits(float *DensityUnits, float *LengthUnits,
	     float *TemperatureUnits, float *TimeUnits,
	     float *VelocityUnits, FLOAT Time);

int grid::WriteGrid(FILE *fptr, char *base_name, int grid_id, HDF5_hid_t file_id,
                          int WriteEverything)
{

  int i, j, k, dim, field, size, active_size, ActiveDim[MAX_DIMENSION];
  int file_status;
 
  float *temp, *temp_VelAnyl;
  float *temperature, *dust_temperature,
    *cooling_time;
 
  FILE *log_fptr;
  FILE *procmap_fptr;
 
  hid_t       group_id, dset_id;
  hid_t       float_type_id, FLOAT_type_id;
  hid_t       file_type_id, FILE_type_id;
  hid_t       file_dsp_id;
  hid_t       old_fields, acc_node;
 
  hsize_t     GMFOutDims[MAX_DIMENSION];
  hsize_t     OutDims[MAX_DIMENSION];
  hsize_t     FullOutDims[MAX_DIMENSION];
  hsize_t     TempIntArray[1];
 
  herr_t      h5_status;
  herr_t      h5_error = -1;

  file_type_id = HDF5_REAL;

  char node_name[255];

  int CopyOnlyActive = TRUE;
  //if(WriteEverything==TRUE)CopyOnlyActive = FALSE;
 
  char *ParticlePositionLabel[] =
     {"position_x", "position_y", "position_z"};
  char *ParticleVelocityLabel[] =
     {"velocity_x", "velocity_y", "velocity_z"};
  char *OldParticlePositionLabel[] =
     {"particle_position_x", "particle_position_y", "particle_position_z"};
  char *OldParticleVelocityLabel[] =
     {"particle_velocity_x", "particle_velocity_y", "particle_velocity_z"};
  char *SmoothedDMLabel[] = {"Dark_Matter_Density", "Velocity_Dispersion",
			     "Particle_x-velocity", "Particle_y-velocity",
			     "Particle_z-velocity"};
  /* initialize */
 
  char id[MAX_GROUP_TAG_SIZE];
  sprintf(id, "%"GROUP_TAG_FORMAT""ISYM, grid_id);
 
  /* make sure quantities defined at least for 3d */
 
  for (dim = GridRank; dim < 3; dim++) {
    GridDimension[dim] = 1;
    GridStartIndex[dim] = 0;
    GridEndIndex[dim] = 0;
  }
 
  for (dim = 0; dim < 3; dim++)
    ActiveDim[dim] = GridEndIndex[dim] - GridStartIndex[dim] +1;
 
  /* ------------------------------------------------------------------- */
  /* 1) Save general grid class data */

  char pid[MAX_TASK_TAG_SIZE];
  sprintf(pid, "%"TASK_TAG_FORMAT""ISYM, MyProcessorNumber);

  char gpid[MAX_TASK_TAG_SIZE];
  sprintf(gpid, "%"TASK_TAG_FORMAT""ISYM, ProcessorNumber);

  char *groupfilename = new char[MAX_LINE_LENGTH];
  strcpy(groupfilename, base_name);
  strcat(groupfilename, ".cpu");
  strcat(groupfilename, pid);

  char *procfilename = new char[MAX_LINE_LENGTH];
  strcpy(procfilename, base_name);
  strcat(procfilename, ".cpu");
  strcat(procfilename, gpid);

  char *name = new char[MAX_LINE_LENGTH];
  strcpy(name, "/Grid");
  strcat(name, id);

  if (MyProcessorNumber == ROOT_PROCESSOR && HierarchyFileOutputFormat > 0) {

    fprintf(fptr, "Task              = %"ISYM"\n", ProcessorNumber);
 
    fprintf(fptr, "GridRank          = %"ISYM"\n", GridRank);
 
    fprintf(fptr, "GridDimension     = ");
    WriteListOfInts(fptr, GridRank, GridDimension);
 
    fprintf(fptr, "GridStartIndex    = ");
    WriteListOfInts(fptr, GridRank, GridStartIndex);
 
    fprintf(fptr, "GridEndIndex      = ");
    WriteListOfInts(fptr, GridRank, GridEndIndex);
 
    fprintf(fptr, "GridLeftEdge      = ");
    WriteListOfFloats(fptr, GridRank, GridLeftEdge);
 
    fprintf(fptr, "GridRightEdge     = ");
    WriteListOfFloats(fptr, GridRank, GridRightEdge);
 
    fprintf(fptr, "Time              = %"GOUTSYM"\n", Time);

    if(WriteEverything == TRUE)
    fprintf(fptr, "OldTime           = %"GOUTSYM"\n", OldTime);
 
    fprintf(fptr, "SubgridsAreStatic = %"ISYM"\n", SubgridsAreStatic);
 
    fprintf(fptr, "NumberOfBaryonFields = %"ISYM"\n", NumberOfBaryonFields);
 
    if (NumberOfBaryonFields > 0) {
      fprintf(fptr, "FieldType = ");

      WriteListOfInts(fptr, NumberOfBaryonFields, FieldType);

      fprintf(fptr, "BaryonFileName = %s\n", procfilename);

      fprintf(fptr, "CourantSafetyNumber    = %"FSYM"\n", CourantSafetyNumber);
      fprintf(fptr, "PPMFlatteningParameter = %"ISYM"\n", PPMFlatteningParameter);
      fprintf(fptr, "PPMDiffusionParameter  = %"ISYM"\n", PPMDiffusionParameter);
      fprintf(fptr, "PPMSteepeningParameter = %"ISYM"\n", PPMSteepeningParameter);

    }

    fprintf(fptr, "NumberOfParticles   = %"ISYM"\n", NumberOfParticles);

    fprintf(fptr, "NumberOfActiveParticles = %"ISYM"\n", NumberOfActiveParticles);

    if ((NumberOfParticles > 0) || (NumberOfActiveParticles > 0))
      fprintf(fptr, "ParticleFileName = %s\n", procfilename); // must be same as above
 
    if (SelfGravity)
      fprintf(fptr, "GravityBoundaryType = %"ISYM"\n", GravityBoundaryType);

  }

  /* Return if this does not concern us */
  if (MyProcessorNumber != ProcessorNumber) {
    delete [] name;
    delete [] procfilename;
    delete [] groupfilename;
    return SUCCESS;
  }
 
 
  /* Open HDF file for writing. */

  group_id = H5Gcreate(file_id, name, 0);
  if( group_id == h5_error ){ENZO_FAIL("IO Problem creating Grid Group");}

  if(WriteEverything == TRUE) {
    FLOAT dtFixedCopy = this->dtFixed;
    old_fields = H5Gcreate(group_id, "OldFields", 0);
    writeScalarAttribute(old_fields, HDF5_PREC, "Time", &this->Time);
    writeScalarAttribute(old_fields, HDF5_PREC, "OldTime", &this->OldTime);
    writeScalarAttribute(old_fields, HDF5_PREC, "dtFixed", &dtFixedCopy);
  }
 
  /* ------------------------------------------------------------------- */
  /* 2) save baryon field quantities (including fields). */
 
  if (NumberOfBaryonFields > 0) {
 
    /* 2a) Set HDF file dimensions (use FORTRAN ordering). */
 
    for (dim = 0; dim < GridRank; dim++) {
      OutDims[GridRank-dim-1] = ActiveDim[dim];
      FullOutDims[GridRank-dim-1] = GridDimension[dim];
      GMFOutDims[GridRank-dim-1] = GravitatingMassFieldDimension[dim];
    }
 
    /* 2b) Write out co-ordinate values.  Use the centre of each cell. */
 
    size = 1;
 
    for (dim = 0; dim < GridRank; dim++) size *= GridDimension[dim];
 
    /* create temporary buffer */
 
    temp = new float[size];
 
    /* 2c) Loop over fields, writing each one. */
 
    for (field = 0; field < NumberOfBaryonFields; field++) {

      if(WriteEverything == FALSE) {
      if (debug1)
	fprintf(stdout, "field = %i %s\n", field, DataLabel[field]);

        this->write_dataset(GridRank, OutDims, DataLabel[field],
            group_id, file_type_id, (VOIDP) BaryonField[field],
            CopyOnlyActive, temp);
	//	fprintf(stderr, "%i field\n", field);
      } else {

        this->write_dataset(GridRank, FullOutDims, DataLabel[field],
            group_id, file_type_id, (VOIDP) BaryonField[field],
            FALSE);

        /* In this case, we write the OldBaryonField, too */

        this->write_dataset(GridRank, FullOutDims, DataLabel[field],
            old_fields, file_type_id, (VOIDP) OldBaryonField[field],
            FALSE);

      }
 
    }   // end of loop over fields

    
    if (WriteEverything == TRUE) {
        /* Clean up our reference here */

        H5Gclose(old_fields);

        if(AccelerationField[0] != NULL) {
          acc_node = H5Gcreate(group_id, "Acceleration", 0);
          if(acc_node == h5_error)ENZO_FAIL("Couldn't create Acceleration node!");

          /* If we're to write everything, we must also write 
             the AccelerationField */

          for(dim = 0; dim < GridRank; dim++) {
            snprintf(node_name, 254, "AccelerationField%"ISYM"", dim);
            this->write_dataset(GridRank, FullOutDims, node_name,
                acc_node, file_type_id, (VOIDP) AccelerationField[dim],
                FALSE);
          }

          H5Gclose(acc_node);
        }
        if(GravitatingMassField != NULL) {
          this->write_dataset(GridRank, GMFOutDims, "GravitatingMassField",
              group_id, file_type_id, (VOIDP) GravitatingMassField,
              FALSE);
        }
        if(PotentialField != NULL) {
          this->write_dataset(GridRank, GMFOutDims, "PotentialField",
              group_id, file_type_id, (VOIDP) PotentialField,
              FALSE);
        }
    }

    if (VelAnyl==1){

      float *curl_x, *curl_y, *curl_z, *div;

        this->ComputeVectorAnalysisFields(Velocity1, Velocity2, Velocity3,
            curl_x, curl_y, curl_z, div);

        this->write_dataset(GridRank, OutDims, "Velocity_Div",
            group_id, file_type_id, (VOIDP) div, TRUE, temp);
        this->write_dataset(GridRank, OutDims, "Velocity_Vorticity3",
            group_id, file_type_id, (VOIDP) curl_z, TRUE, temp);

        if (GridRank==3){
          this->write_dataset(GridRank, OutDims, "Velocity_Vorticity1",
              group_id, file_type_id, (VOIDP) curl_x, TRUE, temp);
          this->write_dataset(GridRank, OutDims, "Velocity_Vorticity2",
              group_id, file_type_id, (VOIDP) curl_y, TRUE, temp);
        }

        delete [] curl_z;
        delete [] div;
        if(GridRank==3){
          delete [] curl_x;
          delete [] curl_y;
        }
    }

    if (BAnyl==1){

      float *curl_x, *curl_y, *curl_z, *div;

        this->ComputeVectorAnalysisFields(Bfield1, Bfield2, Bfield3,
            curl_x, curl_y, curl_z, div);

        this->write_dataset(GridRank, OutDims, "B_Div",
            group_id, file_type_id, (VOIDP) div, TRUE, temp);
        this->write_dataset(GridRank, OutDims, "B_Vorticity3",
            group_id, file_type_id, (VOIDP) curl_z, TRUE, temp);

        if (GridRank==3){
          this->write_dataset(GridRank, OutDims, "B_Vorticity1",
              group_id, file_type_id, (VOIDP) curl_x, TRUE, temp);
          this->write_dataset(GridRank, OutDims, "B_Vorticity2",
              group_id, file_type_id, (VOIDP) curl_y, TRUE, temp);
        }

        delete [] curl_z;
        delete [] div;
        if(GridRank==3){
          delete [] curl_x;
          delete [] curl_y;
        }
    }

   

    /* If requested, compute and output the temperature field 
       as well since its such a pain to compute after the fact. */
 
    if (OutputTemperature) {
 
      /* Allocate field and compute temperature. */
 
      temperature = new float[size];
 
      if (this->ComputeTemperatureField(temperature) == FAIL) {
		ENZO_FAIL("Error in grid->ComputeTemperatureField.");
      }
 
      this->write_dataset(GridRank, OutDims, "Temperature",
                    group_id, file_type_id, (VOIDP) temperature, TRUE, temp);

      /* Copy active part of field into grid */
 
      // If outputing dust temperature, keep temperature field for the calculation.
      if (!OutputDustTemperature) {
	delete [] temperature;
      }
 
    } // end: if (OutputTemperature)

    /* If requested, compute and output the dust temperature field 
       as well since its such a pain to compute after the fact. */
 
    if (OutputDustTemperature) {
 
      /* Get temperature field if we do not already have it. */

      if (!OutputTemperature) {
	temperature = new float[size];

	if (this->ComputeTemperatureField(temperature) == FAIL) {
	  ENZO_FAIL("Error in grid->ComputeTemperatureField.\n");
	}
      }

      /* Allocate field and compute temperature. */
 
      dust_temperature = new float[size];
 
      if (this->ComputeDustTemperatureField(temperature,
					    dust_temperature) == FAIL) {
		ENZO_FAIL("Error in grid->ComputeDustTemperatureField.");
      }
 
      this->write_dataset(GridRank, OutDims, "Dust_Temperature",
                    group_id, file_type_id, (VOIDP) dust_temperature, TRUE, temp);

      /* Copy active part of field into grid */
 
      // If outputing dust temperature, keep temperature field for the calculation.
      if (!OutputTemperature) {
	delete [] temperature;
      }
      delete [] dust_temperature;
 
    } // end: if (OutputDustTemperature)

    if (OutputCoolingTime != FALSE) {
 
      /* Allocate field and compute cooling time. */

      cooling_time = new float[size];
 
      float TemperatureUnits = 1, DensityUnits = 1, LengthUnits = 1,
	VelocityUnits = 1, TimeUnits = 1, aUnits = 1;

      GetUnits(&DensityUnits, &LengthUnits, &TemperatureUnits,
	       &TimeUnits, &VelocityUnits, Time);

      if (this->ComputeCoolingTime(cooling_time) == FAIL) {
		ENZO_FAIL("Error in grid->ComputeCoolingTime.");
      }

      // Make all cooling time values positive and convert to seconds.
      for (i = 0;i < size;i++) {
	cooling_time[i] = fabs(cooling_time[i]) * TimeUnits;
      }
 
      this->write_dataset(GridRank, OutDims, "Cooling_Time",
                    group_id, file_type_id, (VOIDP) cooling_time, TRUE, temp);
 
      delete [] cooling_time;
 
    } // if (OutputCoolingTime)

    /* Make sure that there is a copy of dark matter field to save
       (and at the right resolution). */

    if (OutputSmoothedDarkMatter == FALSE) {
    
      if (SelfGravity && NumberOfParticles > 0) {
        if (this->ComputeDarkMatterDensity(temp) == FAIL) {
          ENZO_FAIL("Error in grid->ComputeDarkMatterDensity.\n")
        }
 
    this->write_dataset(GridRank, OutDims, "Dark_Matter_Density",
                  group_id, file_type_id, (VOIDP) temp, FALSE);

      } // end of (if (SelfGravity && NumberOfParticles > 0))

    } // ENDIF !OutputSmoothedDarkMatter

    delete [] temp;
 
    /* Write BoundaryFluxes info (why? it's just recreated when the grid
                                  is read in) */
 
  } // end: if (NumberOfBaryonFields > 0)

  /* ------------------------------------------------------------------- */
  /* 2b) Save particle quantities smoothed to the grid. */
 
  if (OutputSmoothedDarkMatter > 0) {

    size = active_size = 1;
    for (dim = 0; dim < GridRank; dim++) {
      OutDims[GridRank-dim-1] = ActiveDim[dim];
      size *= GridDimension[dim];
      active_size *= ActiveDim[dim];
    }
 
    temp = new float[active_size];

    int NumberOfDMFields;
    switch (OutputSmoothedDarkMatter) {
    case 1: NumberOfDMFields = 1; break;  // density
    case 2: NumberOfDMFields = 5; break;  // + rms velocity + 3-velocity
    } // ENDSWITCH
      
    for (field = 0; field < NumberOfDMFields; field++) {

      // Only the active part was calculated, so no copying in the routine
      if (debug1)
	fprintf(stdout, "DM field = %i\n", field);
      this->write_dataset(GridRank, OutDims, SmoothedDMLabel[field],
                    group_id, file_type_id, (VOIDP) InterpolatedField[field], FALSE);

      delete [] InterpolatedField[field];
      InterpolatedField[field] = NULL;

    } // ENDFOR field

    delete [] temp;
      
  } // ENDIF OutputSmoothedDarkMatter
 
  /* ------------------------------------------------------------------- */
  /* 3) Save particle quantities. */

  hid_t ParticleGroupID = h5_error;
 
  if (NumberOfParticles > 0) {

    if (ParticleGroupID == h5_error) {
        ParticleGroupID = H5Gcreate(group_id, "Particles", 0);
    }

    hid_t dm_group_id = H5Gcreate(ParticleGroupID, "DarkMatter", 0);
    /* Sort particles according to their identifier. */

    this->SortParticlesByNumber();
    this->SortActiveParticlesByNumber();

    /* Create a temporary buffer (64 bit). */

    temp = new float[NumberOfParticles];

    /* "128-bit" particle positions are stored as what HDF5 calls
       'native long double.' */

    TempIntArray[0] = NumberOfParticles;

    for (dim = 0; dim < GridRank; dim++) {
      this->write_dataset(1, TempIntArray, ParticlePositionLabel[dim],
          dm_group_id, HDF5_FILE_PREC, (VOIDP) ParticlePosition[dim], FALSE);
      H5Lcreate_hard(dm_group_id, ParticlePositionLabel[dim], 
                     group_id, OldParticlePositionLabel[dim],
                     H5P_DEFAULT, H5P_DEFAULT);
    }

    /* Copy particle velocities to temp and write them. */

    for (dim = 0; dim < GridRank; dim++) {
      this->write_dataset(1, TempIntArray, ParticleVelocityLabel[dim],
          dm_group_id, HDF5_REAL, (VOIDP) ParticleVelocity[dim], FALSE);
      H5Lcreate_hard(dm_group_id, ParticleVelocityLabel[dim], 
                     group_id, OldParticleVelocityLabel[dim],
                     H5P_DEFAULT, H5P_DEFAULT);
    }

    /* Copy mass to temp and write it. */

    this->write_dataset(1, TempIntArray, "mass",
        dm_group_id, HDF5_REAL, (VOIDP) ParticleMass, FALSE);
    H5Lcreate_hard(dm_group_id, "mass",
                   group_id, "particle_mass",
                   H5P_DEFAULT, H5P_DEFAULT);

    this->write_dataset(1, TempIntArray, "index",
        dm_group_id, HDF5_PINT, (VOIDP) ParticleNumber, FALSE);
    H5Lcreate_hard(dm_group_id, "index",
                   group_id, "particle_index",
                   H5P_DEFAULT, H5P_DEFAULT);

    /* Copy type to temp and write it. */

    if (ParticleTypeInFile == TRUE) {

      /* We leave this here instead of below so that we can output the
         dataspaces.  Ideally this would be handled with a callback function
         passed in.  */

      if( ParticleType == NULL ){ENZO_FAIL("Particle Type is NULL!");}

      file_dsp_id = H5Screate_simple((Eint32) 1, TempIntArray, NULL);
      if( file_dsp_id == h5_error ){ENZO_FAIL("Can't create particle_type dataspace");}

      dset_id =  H5Dcreate(dm_group_id, "particle_type", HDF5_FILE_INT, file_dsp_id, H5P_DEFAULT);
      if( dset_id == h5_error ){ENZO_FAIL("Can't create particle_type dataset");}

      h5_status = H5Dwrite(dset_id, HDF5_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
          (VOIDP) ParticleType);
      if( h5_status == h5_error ){ENZO_FAIL("Can't write particle_type");}

      h5_status = H5Sclose(file_dsp_id);
      if( h5_status == h5_error ){ENZO_FAIL("Problem closing particle_type dataspace");}

      h5_status = H5Dclose(dset_id);
      if( h5_status == h5_error ){ENZO_FAIL("Problem closing particle_type dataset");}

    }


    /* Copy particle attributes to temp and write them. */

    /* clean up */

    H5Gclose(dm_group_id);

    delete [] temp;

  } // end: if (NumberOfParticles > 0)

  /* ------------------------------------------------------------------- */
  /* 4) Save active particle quantities. */

  if (NumberOfActiveParticles > 0) {
    /* Iterate over the enabled active particle types */

    if (ParticleGroupID == h5_error) {
        ParticleGroupID = H5Gcreate(group_id, "Particles", 0);
    }

    for (i = 0; i < EnabledActiveParticlesCount; i++)
      {

        /* Instantitate an active particle helper of this type
           This class contains the function that allows us to write to disk */

        ActiveParticleType_info *ActiveParticleTypeToEvaluate = EnabledActiveParticles[i];

        /* Write them to disk */

        ActiveParticleTypeToEvaluate->WriteParticles(
            this->ActiveParticles, i, NumberOfActiveParticles,
            ActiveParticleTypeToEvaluate->particle_name,
            ParticleGroupID);
						     
      }


  }  // end: if (NumberOfActiveParticles > 0)

  if (ParticleGroupID != h5_error) {
    H5Gclose(ParticleGroupID);
  }
  
  /* Close HDF group and file. */
  
  if (WriteEverything == TRUE) this->WriteAllFluxes(group_id);
  h5_status = H5Gclose(group_id);
  
  /* 4) Save Gravity info. */
  
  /* Clean up. */
  
  delete [] name;
  delete [] procfilename;
  delete [] groupfilename;
  
  return SUCCESS;
  
}

int grid::write_dataset(int ndims, hsize_t *dims, const char *name, hid_t group,
                  hid_t data_type, void *data, int active_only,
                  float *temp)
{
    hid_t file_dsp_id;
    hid_t dset_id;
    hid_t h5_status;
    herr_t      h5_error = -1;
    int i, j, k, dim, ActiveDim[MAX_DIMENSION];

    for (dim = 0; dim < 3; dim++)
      ActiveDim[dim] = GridEndIndex[dim] - GridStartIndex[dim] +1;
 
    if(active_only == TRUE) {
       if (data_type != HDF5_REAL) ENZO_FAIL("Can't cast to float!");
       float *data_float = (float *) data;
      for (k = GridStartIndex[2]; k <= GridEndIndex[2]; k++)
	for (j = GridStartIndex[1]; j <= GridEndIndex[1]; j++)
	  for (i = GridStartIndex[0]; i <= GridEndIndex[0]; i++)
	    temp[(i-GridStartIndex[0])                           +
	         (j-GridStartIndex[1])*ActiveDim[0]              +
	         (k-GridStartIndex[2])*ActiveDim[0]*ActiveDim[1] ] =
	      data_float[i + j*GridDimension[0] +
		                     k*GridDimension[0]*GridDimension[1]];
    } else { 
      temp = (float *) data; /* Should be fine, since we re-cast back to VOID */
    }

  file_dsp_id = H5Screate_simple((Eint32) ndims, dims, NULL);
  if( file_dsp_id == h5_error )
    ENZO_VFAIL("Error creating dataspace for %s", name)
      
  dset_id =  H5Dcreate(group, name, data_type, file_dsp_id, H5P_DEFAULT);
  if( dset_id == h5_error )
    ENZO_VFAIL("Error creating dataset %s", name)
      
  h5_status = H5Dwrite(dset_id, data_type, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                        (VOIDP) temp);
  if( h5_status == h5_error )
     ENZO_VFAIL("Error writing dataset %s", name)

  h5_status = H5Sclose(file_dsp_id);
  if( h5_status == h5_error )
     ENZO_VFAIL("Error closing dataspace %s", name)

  h5_status = H5Dclose(dset_id);
  if( h5_status == h5_error )
     ENZO_VFAIL("Error closing dataset %s", name)

  return SUCCESS;

}

int grid::WriteAllFluxes(hid_t grid_node)
{
  /* We have to set up the group pointer here */

  int i;

  hid_t h5_error = -1;
  hid_t subgrid_group = h5_error;

  hid_t fluxes_node = H5Gcreate(grid_node, "Fluxes", 0);
  if(fluxes_node == h5_error){ENZO_FAIL("Can't create group Fluxes");}

  char name[255];

  writeScalarAttribute(grid_node, HDF5_INT, "NumberOfSubgrids",
            &this->NumberOfSubgrids);

  for (i = 0; i < this->NumberOfSubgrids; i++) {

    /* Make our group here */

    snprintf(name, 254, "Subgrid%08d", i);

    subgrid_group = H5Gcreate(fluxes_node, name, 0);
    if(subgrid_group == h5_error)ENZO_VFAIL("IO Problem creating %s", name)

    this->WriteFluxGroup(subgrid_group, this->SubgridFluxStorage[i]);

    H5Gclose(subgrid_group);
  }

  subgrid_group = H5Gcreate(fluxes_node, "BoundaryFluxes", 0);
  if(subgrid_group == h5_error)ENZO_FAIL("IO Problem creating BoundaryFluxes")
  this->WriteFluxGroup(subgrid_group, this->BoundaryFluxes);

  H5Gclose(subgrid_group);
  H5Gclose(fluxes_node);

}

int grid::WriteFluxGroup(hid_t top_group, fluxes *fluxgroup)
{
  hid_t h5_error = -1;
  hid_t axis_group = h5_error;
  hid_t left_group, right_group;
  int i, j, field, dim;
  hsize_t size;

  char name[255];

  for (dim = 0; dim < GridRank; dim++) {
    /* compute size (in floats) of flux storage */

    snprintf(name, 254, "Axis%"ISYM, dim);
    axis_group = H5Gcreate(top_group, name, 0);
    if(axis_group == h5_error)ENZO_VFAIL("Can't create %s", name)

    size = 1;

    left_group = H5Gcreate(axis_group, "Left", 0);
    if(left_group == h5_error){ENZO_FAIL("IO Problem with Left");}

    right_group = H5Gcreate(axis_group, "Right", 0);
    if(right_group == h5_error){ENZO_FAIL("IO Problem with Right");}

    for (j = 0; j < GridRank; j++)
      size *= fluxgroup->LeftFluxEndGlobalIndex[dim][j] -
        fluxgroup->LeftFluxStartGlobalIndex[dim][j] + 1;

    for (field = 0; field < NumberOfBaryonFields; field++) {
      /* Every single baryon field should exist, if this is called after the
         hydro solver but before deallocation. */
      /* Now we just write it out, and we know the name and all that. */

      this->write_dataset(1, &size, DataLabel[field], left_group,
          HDF5_REAL, (void *) fluxgroup->LeftFluxes[field][dim],
          FALSE);

      this->write_dataset(1, &size, DataLabel[field], right_group,
          HDF5_REAL, (void *) fluxgroup->RightFluxes[field][dim],
          FALSE);

    }

    /* The dims are always three long, even if zeros... */
    hsize_t dims = 3;

    writeArrayAttribute(left_group, HDF5_I8, dims, "StartIndex",
            fluxgroup->LeftFluxStartGlobalIndex[dim]);
    writeArrayAttribute(left_group, HDF5_I8, dims, "EndIndex",
            fluxgroup->LeftFluxEndGlobalIndex[dim]);

    H5Gclose(left_group);

    writeArrayAttribute(right_group, HDF5_I8, dims, "StartIndex",
            fluxgroup->RightFluxStartGlobalIndex[dim]);
    writeArrayAttribute(right_group, HDF5_I8, dims, "EndIndex",
            fluxgroup->RightFluxEndGlobalIndex[dim]);

    H5Gclose(right_group);

    H5Gclose(axis_group);
  }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.