Source

active-particles / src / enzo / WriteAllData.C

  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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
/***********************************************************************
/
/  WRITE OUT ALL THE DATA (DATA & RESTART DUMP)
/
/  written by: Greg Bryan
/  date:       November, 1994
/  modified1:  Robert Harkness
/  date:       February, 2004
/              October, 2004
/              Direct or indirect SRB driver
/              Local or Global file system, or cwd
/              July , 2006
/              Assemble group file in-core
/  modified2:  Robert Harkness
/  date:       April 2008
/  modified3:  Michael Kuhlen, October 2010, HDF5 hierarchy
/
/  PURPOSE:
/
************************************************************************/
#define SYSCALL
 
// This function writes out the data hierarchy (TopGrid), the External
//   Boundary (Exterior), the TopGridData, and the global_data.
#include "preincludes.h"
 
#ifdef USE_MPI
#include "mpi.h"
#endif /* USE_MPI */
 
#include <hdf5.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.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 "Hierarchy.h"
#include "TopGridData.h"
#include "CosmologyParameters.h"
#include "CommunicationUtilities.h"
#ifdef TRANSFER
#include "ImplicitProblemABC.h"
#endif

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

int GenerateGridArray(LevelHierarchyEntry *LevelArray[], int level,
		      HierarchyEntry **Grids[]);

int SysMkdir(char *startdir, char *directory);
 
void AddLevel(LevelHierarchyEntry *Array[], HierarchyEntry *Grid, int level);
int WriteDataCubes(HierarchyEntry *TopGrid, int TDdims[], char *gridbasename, int &GridID, FLOAT WriteTime);
int Group_WriteDataHierarchy(FILE *fptr, TopGridData &MetaData, HierarchyEntry *TopGrid,
		       char *gridbasename, int &GridID, FLOAT WriteTime, hid_t file_id,
               int CheckpointDump = FALSE);
int WriteHDF5HierarchyFile(char *base_name, HierarchyEntry *TopGrid, TopGridData MetaData, LevelHierarchyEntry *LevelArray[]);
int WriteMemoryMap(FILE *fptr, HierarchyEntry *TopGrid,
		   char *gridbasename, int &GridID, FLOAT WriteTime);
int WriteConfigure(FILE *optr);
int WriteTaskMap(FILE *fptr, HierarchyEntry *TopGrid,
		 char *gridbasename, int &GridID, FLOAT WriteTime);
int WriteParameterFile(FILE *fptr, TopGridData &MetaData, char *Filename);
int WriteStarParticleData(FILE *fptr, TopGridData &MetaData);
int WriteRadiationData(FILE *fptr);
 
int CosmologyComputeExpansionFactor(FLOAT time, FLOAT *a, FLOAT *dadt);
int CommunicationCombineGrids(HierarchyEntry *OldHierarchy,
			      HierarchyEntry **NewHierarchyPointer,
			      FLOAT WriteTime, int CheckpointDump);
void DeleteGridHierarchy(HierarchyEntry *GridEntry);
void ContinueExecution(void);
int CreateSmoothedDarkMatterFields(TopGridData &MetaData, HierarchyEntry *TopGrid);
 
int CreateGriddedStarParticleFields(TopGridData &MetaData, HierarchyEntry *TopGrid); 

#ifndef FAST_SIB
int SetBoundaryConditions(HierarchyEntry *Grids[], int NumberOfGrids,
                          int level, TopGridData *MetaData,
                          ExternalBoundary *Exterior, LevelHierarchyEntry * Level);
#endif 


#ifdef TRANSFER
extern char RTSuffix[];
#endif
extern char BCSuffix[];
extern char GridSuffix[];
extern char HierarchySuffix[];
extern char hdfsuffix[];
extern char RadiationSuffix[];
extern char TaskMapSuffix[];
extern char MemoryMapSuffix[];
extern char ConfigureSuffix[];

char CPUSuffix[]       = ".cpu";
char BHierarchySuffix[] = ".harrays";
 
extern char LastFileNameWritten[MAX_LINE_LENGTH];
 
 
 
 
int Group_WriteAllData(char *basename, int filenumber,
		 HierarchyEntry *TopGrid, TopGridData &MetaData,
		 ExternalBoundary *Exterior, 
#ifdef TRANSFER
		 ImplicitProblemABC *ImplicitSolver,
#endif
		 FLOAT WriteTime = -1, 
		 int CheckpointDump = FALSE)
{
 
  char id[MAX_CYCLE_TAG_SIZE], *cptr, name[MAX_LINE_LENGTH];
  char dumpdirname[MAX_LINE_LENGTH];
  char dumpdirroot[MAX_LINE_LENGTH];
  char unixcommand[MAX_LINE_LENGTH];
  char gridbasename[MAX_LINE_LENGTH];
  char hierarchyname[MAX_LINE_LENGTH];
  char bhierarchyname[MAX_LINE_LENGTH];
  char radiationname[MAX_LINE_LENGTH];
  char taskmapname[MAX_LINE_LENGTH];
  char memorymapname[MAX_LINE_LENGTH];
  char configurename[MAX_LINE_LENGTH];
  char groupfilename[MAX_LINE_LENGTH];
 
  int unixresult;
  int status;
  int local, global;
  int file_status;
  int ii, pe, nn;
  double twrite0, twrite1;

  char pid[MAX_TASK_TAG_SIZE];
 
  FILE *fptr;
  FILE *sptr;
  FILE *gptr;
  FILE *tptr;
  FILE *mptr;
  FILE *optr;
 
  hid_t       file_id;
  hid_t       file_acc_template;
  size_t      memory_increment; // in bytes
  hbool_t     dump_flag;
 
  herr_t      h5_status;
  herr_t      h5_error = -1;
 
  int GridID = 1;
  int GridJD = 1;
  int GridKD = 1;
  int GridLD = 1;
 
  /* If this is an interpolated time step, then temporary replace  the time
     in MetaData.  Note:  Modified 6 Feb 2006 to fix interpolated  data outputs. */

  FLOAT SavedTime = MetaData.Time;
  MetaData.Time = ((WriteTime < 0) || (CheckpointDump == TRUE)) ? MetaData.Time : WriteTime;

  // Global or local filesystem?
 
  local = 0;
  global = 0;
 
  if (MetaData.LocalDir != NULL)
  {
     local = 1;
     strcpy(dumpdirroot, MetaData.LocalDir);
     // fprintf(stdout, "XXXX local dir: %s\n", MetaData.LocalDir);
     // Create on node - locking?
  }
 
  if (MetaData.GlobalDir != NULL)
  {
     global = 1;
     strcpy(dumpdirroot, MetaData.GlobalDir);
     // fprintf(stdout, "XXXX global dir: %s\n", MetaData.GlobalDir);
     // Create on task 0 only
  }
  
  if (( local == 1) && (global == 1))
    fprintf(stdout, "Local AND Global !!\n");
 
  // Create main name
 
  if (ComovingCoordinates && (cptr = strstr(name, "RRRR"))) {
    FLOAT a, dadt;
    CosmologyComputeExpansionFactor(MetaData.Time, &a, &dadt);
    sprintf(cptr, "%"CYCLE_TAG_FORMAT""ISYM, nint(100*((1 + InitialRedshift)/a - 1)));
  } else {
 
    sprintf(id, "%"CYCLE_TAG_FORMAT""ISYM, filenumber);
    sprintf(pid, "%"TASK_TAG_FORMAT""ISYM, MyProcessorNumber);

    /******************** CYCLE / DT BASED OUTPUTS ********************/
 
    if ( (cptr = strstr(basename, MetaData.DataDumpName)) ) {
 
      if (MetaData.DataDumpDir != NULL)
      {
        if (MetaData.LocalDir != NULL) {
          // Local fs
          strcpy(dumpdirname, MetaData.LocalDir);
          strcat(dumpdirname, "/");
          strcat(dumpdirname, MetaData.DataDumpDir);
          strcat(dumpdirname, id);
  
          // Create once per node...
#ifdef USE_NODE_LOCAL
            strcat(dumpdirname, "/mpi");
            strcat(dumpdirname, pid);
#endif /* USE_NODE_LOCAL */
 
          strcpy(name, dumpdirname);
          strcat(name, "/");
          strcat(name, basename);
        } // if LocalDir
 
        else
 
        {
          if (MetaData.GlobalDir != NULL) {
            // Global fs
            strcpy(dumpdirname, MetaData.GlobalDir);
            strcat(dumpdirname, "/");
            strcat(dumpdirname, MetaData.DataDumpDir);
            strcat(dumpdirname, id);
            // Do mkdir on cpu 0 only
            strcpy(name, dumpdirname);
            strcat(name, "/");
            strcat(name, basename);
          } // if GlobalDir
 
          else
 
          {
            // No local or global specified
            strcpy(name, basename);
          } // else GlobalDir
 
        } // else LocalDir
 
      } // if DataDumpDir
 
      else
 
      {
        strcpy(name, basename);
      } // else DataDumpDir
 
      if (debug) fprintf(stdout, "DATA dump: %s\n", name);
 
    } // if DataDumpName

    /******************** REDSHIFT BASED OUTPUTS ********************/
 
    if ( (cptr = strstr(basename, MetaData.RedshiftDumpName)) ) {
 
      if (MetaData.RedshiftDumpDir != NULL)
      {
        if (MetaData.LocalDir != NULL) {
          // Local fs
          strcpy(dumpdirname, MetaData.LocalDir);
          strcat(dumpdirname, "/");
          strcat(dumpdirname, MetaData.RedshiftDumpDir);
          strcat(dumpdirname, id);
  
          // Create once per node...
#ifdef USE_NODE_LOCAL
          strcat(dumpdirname, "/mpi");
          strcat(dumpdirname, pid);
#endif /* USE_NODE_LOCAL */
 
          strcpy(name, dumpdirname);
          strcat(name, "/");
          strcat(name, basename);
        } // if LocalDir
 
        else
 
        {
          if (MetaData.GlobalDir != NULL) {
            // Global fs
            strcpy(dumpdirname, MetaData.GlobalDir);
            strcat(dumpdirname, "/");
            strcat(dumpdirname, MetaData.RedshiftDumpDir);
            strcat(dumpdirname, id);
            // Do mkdir on cpu 0 only
            strcpy(name, dumpdirname);
            strcat(name, "/");
            strcat(name, basename);
          } // if GlobalDir
 
          else
 
          {
            // No local or global specified
            strcpy(name, basename);
          } // else GlobalDir
 
        } // else LocalDir
 
      } // if RedshiftDumpDir
 
      else
 
      {
        strcpy(name, basename);
      } // else RedshiftDumpDir

      if (debug)
	fprintf(stdout, "REDSHIFT dump: %s\n", name);
 
    } // if RedshiftDumpName

    /******************** RESTART BASED OUTPUTS ********************/

    if ( (cptr = strstr(basename, MetaData.RestartDumpName)) ) {
 
      if (MetaData.RestartDumpDir != NULL)
      {
        if (MetaData.LocalDir != NULL) {
          // Local fs
          strcpy(dumpdirname, MetaData.LocalDir);
          strcat(dumpdirname, "/");
          strcat(dumpdirname, MetaData.RestartDumpDir);
          strcat(dumpdirname, id);
  
          // Create once per node...
#ifdef USE_NODE_LOCAL
          strcat(dumpdirname, "/mpi");
          strcat(dumpdirname, pid);
#endif /* USE_NODE_LOCAL */
 
          strcpy(name, dumpdirname);
          strcat(name, "/");
          strcat(name, basename);
        } // if LocalDir
 
        else
 
        {
          if (MetaData.GlobalDir != NULL) {
            // Global fs
            strcpy(dumpdirname, MetaData.GlobalDir);
            strcat(dumpdirname, "/");
            strcat(dumpdirname, MetaData.RestartDumpDir);
            strcat(dumpdirname, id);
            // Do mkdir on cpu 0 only
            strcpy(name, dumpdirname);
            strcat(name, "/");
            strcat(name, basename);
          } // if GlobalDir
 
          else
 
          {
            // No local or global specified
            strcpy(name, basename);
          } // else GlobalDir
 
        } // else LocalDir
 
      } // if RedshiftDumpDir
 
      else
 
      {
        strcpy(name, basename);
      } // else RedshiftDumpDir

      if (debug)
	fprintf(stdout, "RESTART dump: %s\n", name);
 
    } // if RestartDumpName
 
    if (filenumber >= 0)
      strcat(name, id);
  }
 
  strcpy(LastFileNameWritten, name);
 
  strcpy(groupfilename, name);
  strcat(groupfilename, CPUSuffix);
  strcat(groupfilename, pid);
 
  if (debug)
    fprintf(stdout, "WriteAllData: writing group file %s\n", groupfilename);
 
//  Synchronization point for directory creation
 
  CommunicationBarrier();
#ifdef USE_MPI
  twrite0 = MPI_Wtime();
#endif
 
//  Get cwd
//  Generate command
//  Execute system call
 
    if ( local )
    {

      MPI_Arg mpi_size;
      MPI_Arg mpi_rank;

#ifdef USE_MPI
      MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
      MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
#else
      mpi_rank = 0;
      mpi_size = 1;
#endif

      pe = mpi_rank;
      nn = mpi_size;

 
      for ( ii = 0; ii < nn; ii++ )
      {

        CommunicationBarrier();
        if( pe == ii )
        {
 
          if ( (cptr = strstr(basename, MetaData.DataDumpName)) ) {
            if (MetaData.DataDumpDir != NULL) {
#ifdef SYSCALL
              unixresult = SysMkdir("", dumpdirname);
              if (debug) fprintf(stdout, "DATA dump: dumpdirname=(%s) == unixresult=%"ISYM"\n", dumpdirname, unixresult);
#else
              strcat(strcpy(unixcommand, "mkdir -p "), dumpdirname);
              unixresult = system(unixcommand);
              if (debug) fprintf(stdout, "DATA dump: %s == %"ISYM"\n", unixcommand, unixresult);
#endif
            }
          } // ENDIF datadump
 
          if ( (cptr = strstr(basename, MetaData.RedshiftDumpName)) ) {
            if (MetaData.RedshiftDumpDir != NULL) {
#ifdef SYSCALL
              unixresult = SysMkdir("", dumpdirname);
              fprintf(stdout, "REDSHIFT dump: dumpdirname=(%s) == unixresult=%"ISYM"\n", dumpdirname, unixresult);
#else
              strcat(strcpy(unixcommand, "mkdir -p "), dumpdirname);
              unixresult = system(unixcommand);
              fprintf(stdout, "REDSHIFT dump: %s == %"ISYM"\n", unixcommand, unixresult);
#endif
            }
          } // ENDIF redshift

          if ( (cptr = strstr(basename, MetaData.RestartDumpName)) ) {
            if (MetaData.RestartDumpDir != NULL) {
#ifdef SYSCALL
              unixresult = SysMkdir("", dumpdirname);
              fprintf(stdout, "RESTART dump: dumpdirname=(%s) == unixresult=%"ISYM"\n", dumpdirname, unixresult);
#else
              strcat(strcpy(unixcommand, "mkdir -p "), dumpdirname);
              unixresult = system(unixcommand);
              fprintf(stdout, "RESTART dump: %s == %"ISYM"\n", unixcommand, unixresult);
#endif
            }
          } // ENDIF restart

        } // ENDIF pe == ii
      } // ENDFOR ii
    } // ENDIF local
 
    if ( global )
    {
      if ( MyProcessorNumber == ROOT_PROCESSOR )
      {
 
        if ( (cptr = strstr(basename, MetaData.DataDumpName)) ) {
          if (MetaData.DataDumpDir != NULL) {
#ifdef SYSCALL
            unixresult = SysMkdir("", dumpdirname);
            if (debug) fprintf(stdout, "DATA dump: dumpdirname=(%s) == unixresult=%"ISYM"\n", dumpdirname, unixresult);
#else
            strcat(strcpy(unixcommand, "mkdir -p "), dumpdirname);
            unixresult = system(unixcommand);
            if (debug) fprintf(stdout, "DATA dump: %s == %"ISYM"\n", unixcommand, unixresult);
#endif
          }
        } // ENDIF datadump
 
        if ( (cptr = strstr(basename, MetaData.RedshiftDumpName)) ) {
          if (MetaData.RedshiftDumpDir != NULL) {
#ifdef SYSCALL
            unixresult = SysMkdir("", dumpdirname);
            fprintf(stdout, "REDSHIFT dump: dumpdirname=(%s) == unixresult=%"ISYM"\n", dumpdirname, unixresult);
#else
            strcat(strcpy(unixcommand, "mkdir -p "), dumpdirname);
            unixresult = system(unixcommand);
            fprintf(stdout, "REDSHIFT dump: %s == %"ISYM"\n", unixcommand, unixresult);
#endif
          }
        } // ENDIF redshift

        if ( (cptr = strstr(basename, MetaData.RestartDumpName)) ) {
          if (MetaData.RestartDumpDir != NULL) {
#ifdef SYSCALL
            unixresult = SysMkdir("", dumpdirname);
            fprintf(stdout, "RESTART dump: dumpdirname=(%s) == unixresult=%"ISYM"\n", dumpdirname, unixresult);
#else
            strcat(strcpy(unixcommand, "mkdir -p "), dumpdirname);
            unixresult = system(unixcommand);
            fprintf(stdout, "RESTART dump: %s == %"ISYM"\n", unixcommand, unixresult);
#endif
          }
        } // ENDIF restart
 
      }
    }


  
//  fprintf(stdout, "Sync point ok\n");
 
  CommunicationBarrier();
 
//  Start I/O timing
 
#ifdef USE_HDF5_OUTPUT_BUFFERING

  memory_increment = 1024*1024;
  dump_flag = 1;

  file_acc_template = H5Pcreate (H5P_FILE_ACCESS);
    if( file_acc_template == h5_error ){my_exit(EXIT_FAILURE);}

  h5_status = H5Pset_fapl_core(file_acc_template, memory_increment, dump_flag);
    if( h5_status == h5_error ){my_exit(EXIT_FAILURE);}

  file_id = H5Fcreate(groupfilename, H5F_ACC_TRUNC, H5P_DEFAULT, file_acc_template);
    if( file_id == h5_error ){my_exit(EXIT_FAILURE);}

#else

  file_id = H5Fcreate(groupfilename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
  //  h5_status = H5Fclose(file_id);
  //    if( h5_status == h5_error ){my_exit(EXIT_FAILURE);}

#endif
 
  // Set MetaData.BoundaryConditionName
 
  if (MetaData.BoundaryConditionName != NULL)
    delete [] MetaData.BoundaryConditionName;
  MetaData.BoundaryConditionName = new char[MAX_LINE_LENGTH];
  strcpy(MetaData.BoundaryConditionName, name);
  strcat(MetaData.BoundaryConditionName, BCSuffix);

  /* We set our global variable CheckpointRestart to TRUE here, so that it gets
     output in the parameter file. */

  CheckpointRestart = CheckpointDump;
 
#ifdef TRANSFER
  if (ImplicitProblem) {
    // Output ImplicitSolver module parameter file

    //    Reset MetaData.RadHydroParameterFname
    if (MetaData.RadHydroParameterFname != NULL)
      delete MetaData.RadHydroParameterFname;
    MetaData.RadHydroParameterFname = new char[MAX_LINE_LENGTH];
    strcpy(MetaData.RadHydroParameterFname, name);
    strcat(MetaData.RadHydroParameterFname, RTSuffix);
    
    // Open RT module parameter file
    if ((fptr = fopen(MetaData.RadHydroParameterFname, "w")) == NULL) {
      fprintf(stderr, "Error opening RT module parameter file: %s\n",
	      MetaData.RadHydroParameterFname);
      return FAIL;
    }
    
    // Write RT module parameters to file
    if (ImplicitSolver->WriteParameters(fptr) == FAIL) {
      fprintf(stderr, "Error in ImplicitSolver::WriteParameters\n");
      return FAIL;
    }
    fclose(fptr);
  }
#endif		 

  // Output TopGrid data
 
  if (MyProcessorNumber == ROOT_PROCESSOR) {
    if ((fptr = fopen(name, "w")) == NULL) 
      ENZO_VFAIL("Error opening output file %s\n", name)
    if (CheckpointDump == TRUE) {
      fprintf(fptr, "# WARNING! This is a checkpoint dump! Lots of data!\n");
    }
    else if (WriteTime >= 0) {
      fprintf(fptr, "# WARNING! Interpolated output: level = %"ISYM"\n",
	      MetaData.OutputFirstTimeAtLevel-1);
    }
    if (WriteParameterFile(fptr, MetaData, name) == FAIL)
      ENZO_FAIL("Error in WriteParameterFile");
    fclose(fptr);
  
  }

 
  // Output Boundary condition info
 
  if (MyProcessorNumber == ROOT_PROCESSOR) {
    if ((fptr = fopen(MetaData.BoundaryConditionName, "w")) == NULL) 
      ENZO_VFAIL("Error opening boundary condition file: %s\n",
	      MetaData.BoundaryConditionName)
    strcat(MetaData.BoundaryConditionName, hdfsuffix);
    if (Exterior->WriteExternalBoundary(fptr, MetaData.BoundaryConditionName)
	== FAIL)
      ENZO_FAIL("Error in WriteExternalBoundary");
    fclose(fptr);
 
  }
 
  // Create hierarchy name and grid base name
 
  strcpy(hierarchyname, name);
  strcat(hierarchyname, HierarchySuffix);
 
  strcpy(gridbasename, name);
//  strcat(gridbasename, GridSuffix);

  strcpy(taskmapname, name);
  strcat(taskmapname, TaskMapSuffix);
  strcat(taskmapname, pid);

  strcpy(memorymapname, name);
  strcat(memorymapname, MemoryMapSuffix);

  strcpy(configurename, name);
  strcat(configurename, ConfigureSuffix);

 
  /* If we're writing interpolated dark matter fields, create them now. */

  CreateSmoothedDarkMatterFields(MetaData, TopGrid);
 
  /* Combine the top level grids into a single grid for output
     (TempTopGrid is the top of an entirely new hierarchy). */
 
  HierarchyEntry *TempTopGrid;
  CommunicationCombineGrids(TopGrid, &TempTopGrid, WriteTime, CheckpointDump);
 
  LevelHierarchyEntry *LevelArray[MAX_DEPTH_OF_HIERARCHY];
  if ((HierarchyFileOutputFormat % 2) == 0  || VelAnyl==1 || BAnyl==1) {
    /* Create LevelArray */
    for (int level = 0; level < MAX_DEPTH_OF_HIERARCHY; level++)
      LevelArray[level] = NULL;
    AddLevel(LevelArray, TempTopGrid, 0);

#ifndef FAST_SIB
    if(VelAnyl==1||BAnyl==1){
      for (int level = 0; level < MAX_DEPTH_OF_HIERARCHY; level++) {
	HierarchyEntry **Grids;
	int NumberOfGrids = GenerateGridArray(LevelArray, level, &Grids);
	if (LevelArray[level] != NULL) {
	  
	  if (SetBoundaryConditions(Grids, NumberOfGrids, level, &MetaData, 
				    Exterior, LevelArray[level]) == FAIL) {
	    printf("error setboundary");
	  }}}}
#endif
  }
  
  // Output Data Hierarchy

  if (MyProcessorNumber == ROOT_PROCESSOR) {

    if( HierarchyFileOutputFormat % 2 == 0 )
      WriteHDF5HierarchyFile(name, TempTopGrid, MetaData, LevelArray);      
    
    if (HierarchyFileOutputFormat > 0)
      if ((fptr = fopen(hierarchyname, "w")) == NULL) 
	ENZO_VFAIL("Error opening hierarchy file %s\n", hierarchyname);
  }

  if (Group_WriteDataHierarchy(fptr, MetaData, TempTopGrid,
            gridbasename, GridID, WriteTime, file_id, CheckpointDump) == FAIL)
    ENZO_FAIL("Error in Group_WriteDataHierarchy");

    hid_t metadata_group = H5Gcreate(file_id, "Metadata", 0);
    if(metadata_group == h5_error)ENZO_FAIL("Error writing metadata!");
    writeArrayAttribute(metadata_group, HDF5_INT, MAX_DEPTH_OF_HIERARCHY,
                        "LevelCycleCount", LevelCycleCount);
  if(CheckpointDump == TRUE){
    // Write our supplemental (global) data
    FLOAT dtThisLevelCopy[MAX_DEPTH_OF_HIERARCHY];
    FLOAT dtThisLevelSoFarCopy[MAX_DEPTH_OF_HIERARCHY];
    for (int level = 0; level < MAX_DEPTH_OF_HIERARCHY; level++) {
        dtThisLevelCopy[level] = dtThisLevel[level];
        dtThisLevelSoFarCopy[level] = dtThisLevelSoFar[level];
    }
    writeArrayAttribute(metadata_group, HDF5_PREC, MAX_DEPTH_OF_HIERARCHY,
                        "dtThisLevel", dtThisLevelCopy);
    writeArrayAttribute(metadata_group, HDF5_PREC, MAX_DEPTH_OF_HIERARCHY,
                        "dtThisLevelSoFar", dtThisLevelSoFarCopy);
    writeScalarAttribute(metadata_group, HDF5_PREC, "Time", &MetaData.Time);
  }
    H5Gclose(metadata_group);

  // At this point all the grid data has been written

  h5_status = H5Fclose(file_id);
    if( h5_status == h5_error ){my_exit(EXIT_FAILURE);}

#ifdef USE_HDF5_OUTPUT_BUFFERING

  h5_status = H5Pclose(file_acc_template);
    if( h5_status == h5_error ){my_exit(EXIT_FAILURE);}

#endif


  if (MyProcessorNumber == ROOT_PROCESSOR)
    if ((mptr = fopen(memorymapname, "w")) == NULL) 
      ENZO_VFAIL("Error opening memory map file %s\n", memorymapname)

  if (WriteMemoryMap(mptr, TempTopGrid, gridbasename, GridKD, WriteTime) == FAIL)
    ENZO_FAIL("Error in WriteMemoryMap");

  // Output configure

  if (MyProcessorNumber == ROOT_PROCESSOR) {
    if ((optr = fopen(configurename, "w")) == NULL) {
      fprintf(stdout, "Error opening configure file %s\n", configurename);
      fprintf(stdout, "Not crucial but worrysome. Will continue.\n" );
      //      ENZO_VFAIL("Error opening configure file %s\n", configurename)
    }

    WriteConfigure(optr);

    fclose(optr);
  }

  // Output task map

#ifdef TASKMAP
  if ((tptr = fopen(taskmapname, "w")) == NULL)
    ENZO_VFAIL("Error opening task map file %s\n", taskmapname)

  if (WriteTaskMap(tptr, TempTopGrid, gridbasename, GridLD, WriteTime) == FAIL)
    ENZO_FAIL("Error in WriteTaskMap");
#endif
 
  int TGdims[3];
 
  TGdims[0] = MetaData.TopGridDims[0];
  TGdims[1] = MetaData.TopGridDims[1];
  TGdims[2] = MetaData.TopGridDims[2];
 
  //  fprintf(stdout, "TGdims  %"ISYM"  %"ISYM"  %"ISYM"\n", TGdims[0], TGdims[1], TGdims[2]);
 
  if (CubeDumpEnabled == 1)
    if (WriteDataCubes(TempTopGrid, TGdims, name, GridJD, WriteTime) == FAIL)
      ENZO_FAIL("Error in WriteDataCubes");

 
  // Clean up combined top level grid, and first two levels of hierarchy
 
  if (TempTopGrid != TopGrid) {
    if (TempTopGrid->NextGridNextLevel != NULL)
      DeleteGridHierarchy(TempTopGrid->NextGridNextLevel);
    delete TempTopGrid->GridData;
    delete TempTopGrid;
  }
 
  // Output star particle data 
 
  if (WriteStarParticleData(fptr, MetaData) == FAIL)
    ENZO_FAIL("Error in WriteStarParticleData");
 
  // Output MBH particle data
  
  if (MBHParticleIO == TRUE && MyProcessorNumber == ROOT_PROCESSOR) {
    FILE *MBHfptr;

    if ((MBHfptr = fopen(MBHParticleIOFilename, "a")) == NULL) {
      ENZO_VFAIL("Error opening file %s\n", MBHParticleIOFilename)
    }
    
    // printing order: time, regular star count, MBH id, MBH mass, MBH angular momentum
    for (int i = 0; i < G_TotalNumberOfStars; i++) { 
      fprintf(MBHfptr, " %"FSYM"  %"ISYM"  %"ISYM"  %lf  %"FSYM"  %"FSYM"  %"FSYM"  %lf\n", 
	      MetaData.Time, NumberOfStarParticles, (int)(MBHParticleIOTemp[i][0]), 
	      MBHParticleIOTemp[i][1], (float)(MBHParticleIOTemp[i][2]), 
	      (float)(MBHParticleIOTemp[i][3]), (float)(MBHParticleIOTemp[i][4]),
	      MBHParticleIOTemp[i][5]);
    }
    
    fclose(MBHfptr);
    
  }

  // Create radiation name and write radiation data
 
  if (((RadiationFieldType >= 10 && RadiationFieldType <= 11) ||
       RadiationData.RadiationShield == TRUE) &&
      MyProcessorNumber == ROOT_PROCESSOR) {
 
    FILE *Radfptr;
 
    strcpy(radiationname, name);
    strcat(radiationname, RadiationSuffix);
 
    if ((Radfptr = fopen(radiationname, "w")) == NULL) {
      ENZO_VFAIL("Error opening radiation file %s\n", radiationname)
    }
    if (WriteRadiationData(Radfptr) == FAIL)
      ENZO_FAIL("Error in WriteRadiationData");
 
    fclose(Radfptr);
 
  }
 
  if (MyProcessorNumber == ROOT_PROCESSOR) {
    if (HierarchyFileOutputFormat  > 0)
      fclose(fptr);
    fclose(mptr);
  }

#ifdef TASKMAP
  fclose(tptr);
#endif

  // Replace the time in metadata with the saved value (above)
 
  MetaData.Time = SavedTime;

  /* This should always be false outside of writing and the restart process */
    
  CheckpointRestart = FALSE;

  CommunicationBarrier();
// if (debug)
    //  fprintf(stdout, "WriteAllData: finished writing data\n");
 
//  Stop I/O timing
 
//  Synchronization point for SRB
 
  CommunicationBarrier();
 
  ContinueExecution();
 
  CommunicationBarrier();
#ifdef USE_MPI
  twrite1 = MPI_Wtime();
#endif

  if ( MyProcessorNumber == ROOT_PROCESSOR ){
    sptr = fopen("OutputLog", "a");
    fprintf(sptr, "DATASET WRITTEN %s %8"ISYM" %18.16"GSYM" %18.8"FSYM" %18.8"FSYM"\n", 
	    name, MetaData.CycleNumber, MetaData.Time, twrite0, (twrite1-twrite0));
    fclose(sptr);
  }
 
  return SUCCESS;
}

/* 
void DeleteGridHierarchy(HierarchyEntry *GridEntry)
{
  if (GridEntry->NextGridThisLevel != NULL)

     DeleteGridHierarchy(GridEntry->NextGridThisLevel);
 
  delete GridEntry;
 
  return;
}
*/