Source

enzo-3.0 / src / enzo / headers / ActiveParticle.h

The active_particles branch has multiple heads

Full commit
  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
/*-*-C++-*-*/
/***********************************************************************
/
/  STAR PARTICLE STRUCTURE
/
/  written by: John Wise
/  date:       September, 2005
/  modified1:  John Wise
/  date:       March, 2009 (converted into a class)
/  modified2:  John Wise, Greg Bryan, Britton Smith, Cameron Hummels,
/              Matt Turk
/  date:       May, 2011 (converting from Star to ActiveParticle)
/
/  PURPOSE:
/
************************************************************************/
#ifndef __ACTIVE_PARTICLE_H
#define __ACTIVE_PARTICLE_H

#include <typeinfo>
#ifdef FLUX_FIX
#include "TopGridData.h"
#endif
#include "ParticleAttributeHandler.h"
#include "h5utilities.h"

struct ActiveParticleFormationData;
struct ActiveParticleFormationDataFlags;

class ActiveParticleType
{
public:
  void static ConstructData(grid *_grid,
			    ActiveParticleFormationDataFlags &flags,
			    ActiveParticleFormationData &data);
  void static DestroyData(grid *_grid,
			  ActiveParticleFormationData &data);
  int static WriteDataset(int ndims, hsize_t *dims, const char *name, hid_t group,
			  hid_t data_type, void *data);
  int static ReadDataset(int ndims, hsize_t *dims, const char *name, hid_t group,
			 hid_t data_type, void *read_to);
  void static SetupBaseParticleAttributes(
    std::vector<ParticleAttributeHandler*> &handlers);

  void OutputPositionInformation(void);

  /* Several pure virtual functions */
  
  /* This should return the number of new star particles created, and should
   * create them. */

  ActiveParticleType(void);
  ActiveParticleType(grid *_grid, ActiveParticleFormationData &data);
  ActiveParticleType(grid *_grid, int _id, int _level);
  ActiveParticleType(ActiveParticleType* part);
  ~ActiveParticleType(void);

  void operator=(ActiveParticleType *a);

  template <class active_particle_class> active_particle_class *copy(void);

  PINT   ReturnID(void) { return Identifier; };
  double ReturnMass(void) { return Mass; };
  float ReturnBirthTime(void) { return BirthTime; };
  float ReturnDynamicalTime(void) { return DynamicalTime; };
  float ReturnMetallicity(void) { return Metallicity; };
  int   ReturnType(void) { return type; };
  int   ReturnLevel(void) { return level; };
  int   ReturnDestProcessor(void) { return dest_processor; };
  int   ReturnGridID(void) { return GridID; };
  grid *ReturnCurrentGrid(void) { return CurrentGrid; };

  void  ReduceLevel(void) { level--; };
  void  ReduceLevel(int x) { level -= x; };
  void  IncreaseLevel(void) { level++; };
  void  IncreaseLevel(int x) { level += x; };
  void  SetLevel(int i) { level = i; };
  void  SetGridID(int i) { GridID = i; };
  void  SetDestProcessor(int i) { dest_processor = i; };
  void  AssignCurrentGrid(grid *a) { this->CurrentGrid = a; };
  void  AddMass(double dM) { Mass += dM; };
  void  AdjustMassByFactor(double factor) { Mass *= factor; };
  void  AdjustVelocity(float VelocityIncrement[]);
  void  SetVelocity(float NewVelocity[]);
  void  SetPosition(FLOAT NewPosition[]);
  void  SetPositionPeriod(FLOAT period[]);

  FLOAT *ReturnPosition(void) { return pos; };
  float *ReturnVelocity(void) { return vel; };
  float ReturnMomentum(int dim) { return Mass*vel[dim]; };
  void   ConvertAllMassesToSolar(void);
  void   ConvertMassToSolar(void);
  void   Merge(ActiveParticleType *a);
  float Separation(ActiveParticleType *a);
  float Separation2(ActiveParticleType *a);
  float RelativeVelocity2(ActiveParticleType *a);
  void  UpdatePositionVelocity(void);
  void  MirrorToParticle(void);
  void  CopyFromParticle(grid *_grid, int _id, int _level);
  int   DisableParticle(LevelHierarchyEntry *LevelArray[], int NewProcessorNumber);
  int   SphereContained(LevelHierarchyEntry *LevelArray[], int level, 
			float Radius);
  void  PrintInfo(void);
  //void  ActivateNewStar(FLOAT Time, float Timestep);
  //void  DeleteCopyInGrid(void);
  //int   DeleteCopyInGridGlobal(LevelHierarchyEntry *LevelArray[]);

  /* Virtual and pure virtual functions in this base class */

  virtual bool IsARadiationSource(FLOAT Time) { return false; };
  virtual bool Mergable(ActiveParticleType *a);
  virtual int GetEnabledParticleID(int id = -1) {
    ENZO_FAIL("Not implemented.");
  };

#ifdef TRANSFER
  RadiationSourceEntry* RadiationSourceInitialize(void);
#endif

protected:
  grid *CurrentGrid;
  FLOAT	pos[MAX_DIMENSION];
  float vel[MAX_DIMENSION];
  double Mass;	      
  float BirthTime;
  float DynamicalTime;      
  float Metallicity;
  PINT Identifier;
  int level;
  int GridID;
  int dest_processor;  // used for communication
  int type;

private: /* Cannot be accessed by subclasses! */
  
  friend class grid;
  friend class ActiveParticleType_info;

};

/* Comparer functions for sorting particle buffers with std::sort */

struct cmp_ap_grid {
  bool operator()(ActiveParticleType* const& a, ActiveParticleType* const& b) const {
    if (a->ReturnGridID() < b->ReturnGridID()) return true;
    else return false;
  }
};

struct cmp_ap_proc {
  bool operator()(ActiveParticleType* const& a, ActiveParticleType* const& b) const {
    if (a->ReturnDestProcessor() < b->ReturnDestProcessor()) return true;
    else return false;
  }
};

struct cmp_ap_type {
  bool operator()(ActiveParticleType* const& a, ActiveParticleType* const& b) const {
    if (a->ReturnType() < b->ReturnType()) return true;
    else return false;
  }
};


struct cmp_ap_number {
  bool operator()(ActiveParticleType* const& a, ActiveParticleType* const& b) const {
    if (a->ReturnID() < b->ReturnID()) return true;
    else return false;
  }
};

struct ActiveParticleFormationData {
  int NumberOfNewParticles;
  int MaxNumberOfNewParticles;
  ActiveParticleType **NewParticles;
  /* This is where all the pointers that normally get passed into
   * formation routines gets placed. Things like fractional h2, dark
   * matter density, etc etc. Anything that's derived.  It's okay to
   * add to this.  */
  float *DarkMatterDensity;
  float *H2Fraction;
  float *CoolingTime;
  float *CoolingRate;
  float *Temperature;
  float *TotalMetals;
  float DensityUnits;
  float LengthUnits;
  float TemperatureUnits;
  float TimeUnits;
  float VelocityUnits;
  double MassUnits;
  int DensNum;
  int Vel1Num;
  int Vel2Num;
  int Vel3Num;
  int TENum;
  int GENum;
  int MetalNum;
  int MetalIaNum;
  int ColourNum;
  int level;
  FLOAT CellSize;
  int GridID;
};

const struct ActiveParticleFormationData data_default = {
  0,        // NumberOfNewParticles
  0,        // MaxNumberOfNewParticles
  NULL,     // NewParticles
  NULL,     // DarkMatterDensity
  NULL,     // H2Fraction
  NULL,     // CoolingTime
  NULL,     // CoolingRate
  NULL,     // Temperature
  NULL,     // TotalMetals
  0.0,      // DensityUnits
  0.0,      // LengthUnits
  0.0,      // TemperatureUnits
  0.0,      // TimeUnits
  0.0,      // VelocityUnits
  0.0,      //  MassUnits
  -1,       // DensNum
  -1,       // Vel1Num
  -1,       // Vel2Num
  -1,       // Vel3Num
  -1,       // TENum
  -1,       // GENum
  -1,       // MetalNum
  -1,       // MetalIaNum
  -1,       // ColourNum
  -1,       // level
  -1        // GridID
};


struct ActiveParticleFormationDataFlags {
  /* For every entry in the ActiveParticleFormationData struct, we
   * have a bool here. */
  bool DarkMatterDensity;
  bool H2Fraction;
  bool CoolingTime;
  bool CoolingRate;
  bool Temperature;
  bool UnitConversions;
  bool DataFieldNumbers;
  bool MetalField;
};

const struct ActiveParticleFormationDataFlags flags_default = {
  false,    // DarkMatterDensity
  false,    // H2Fraction
  false,    // CoolingTime
  false,    // CoolingRate
  false,    // Temperature
  false,    // UnitConversions
  false,    // DataFieldNumbers
  false     // MetalField
};

namespace ActiveParticleHelpers {

  template <class APClass> int CalculateElementSize() {
      static int particle_size = 0;
      if (particle_size > 0) return particle_size;
      AttributeVector &handlers = APClass::AttributeHandlers;
      for(AttributeVector::iterator it = handlers.begin();
          it != handlers.end(); ++it) {
          particle_size += (**it).element_size;
      }
      return particle_size;
  }

  template <class APClass> void Allocate(int Count, char **buffer) {
          
      /* This routine is called for each particle type. */
      /* So we need to re-calculate the element and header size for each. */

      int particle_size = CalculateElementSize<APClass>();
      int header_size = sizeof(int);

      *buffer = new char[particle_size * Count + header_size];

  }

  template <class APClass> void PrintActiveParticle(APClass *ap,
      const std::string prefix = "") {

      AttributeVector &handlers = APClass::AttributeHandlers;
      for(AttributeVector::iterator it = handlers.begin();
        it != handlers.end(); ++it) {
          (*it)->PrintAttribute(ap);
          std::cout << " ";
      }

      std::cout << std::endl;

  }

  template <class APClass> void WriteParticles(
          ActiveParticleType **InList, int ParticleTypeID,
          int TotalParticles,
          const std::string name, hid_t grid_node)
  {
      int i, size = 0, Count = 0;
      char *buffer, *_buffer;
      AttributeVector &handlers = APClass::AttributeHandlers;
      const char *_name = name.c_str();
      hid_t node = H5Gcreate(grid_node, _name, 0);

      /* Now we count up our particles */
      for (i = 0; i < TotalParticles; i++) {
        if (InList[i]->GetEnabledParticleID() == ParticleTypeID) Count++;
      }

      writeScalarAttribute(node, HDF5_INT, "Count", &Count);

      if(Count == 0) {
        H5Gclose(node);
        return;
      }

      int ndims = 1;
      hsize_t dims[1] = {Count};

      APClass *In;

      for (AttributeVector::iterator it = handlers.begin();
          it != handlers.end(); ++it) {
          size = Count * (*it)->element_size;
          _buffer = buffer = new char[size];
          for (i = 0; i < TotalParticles; i++) {
            if (InList[i]->GetEnabledParticleID() != ParticleTypeID)
              continue;
            (*it)->GetAttribute(&_buffer, InList[i]);
          }
          /* Now write it to disk */
          _name = (*it)->name.c_str();
          APClass::WriteDataset(ndims, dims, _name, node,
                                (*it)->hdf5type, buffer);
          delete buffer;
      }

      H5Gclose(node);
  }

  template <class APClass> int ReadParticles(
          ActiveParticleType **OutList, int &offset, const std::string name,
          hid_t grid_node)
  {
      int i, size = 0;
      AttributeVector &handlers = APClass::AttributeHandlers;
      const char *_name = name.c_str();
      hid_t node = H5Gopen(grid_node, _name);
      /* We now have a reference to the top level node. */
      int Count;
      readAttribute(node, HDF5_INT, "Count", &Count, false);
      if(Count == 0) {
        H5Gclose(node);
        return offset;
      }

      char *buffer, *_buffer;
      int ndims = 1;
      hsize_t dims[1] = {Count};
      for (i = 0; i < Count; i++) {
          OutList[i+offset] = new APClass();
      }

      for (AttributeVector::iterator it = handlers.begin();
          it != handlers.end(); ++it) {
          size = Count * (*it)->element_size;
          _buffer = buffer = new char[size];
          _name = (*it)->name.c_str();
          APClass::ReadDataset(ndims, dims, _name, node,
                               (*it)->hdf5type, buffer);
          for (i = 0; i < Count; i++) {
              (*it)->SetAttribute(&_buffer, OutList[i+offset]);
          }
          delete buffer;
      }

      H5Gclose(node);
      offset += Count;
      return Count;

  }



  template <class APClass> int FillBuffer(
          ActiveParticleType **InList_, int InCount, char *buffer_) {

      int i;
      int size = 0;

      if (buffer_ == NULL) {
          ENZO_FAIL("Buffer not allocated!");
      }
      /* We increment the pointer as we fill */
      char **buffer = &buffer_;

      AttributeVector &handlers = APClass::AttributeHandlers;

      APClass *In;

      for (i = 0; i < InCount; i++) {
          In = dynamic_cast<APClass*>(InList_[i]);
          /* We'll put debugging output here */
          for(AttributeVector::iterator it = handlers.begin();
              it != handlers.end(); ++it) {
              size += (*it)->GetAttribute(buffer, In);
          }
          /* 
          std::cout << "APF[" << MyProcessorNumber << "] " << i << " " << size << " ";
          PrintActiveParticle<APClass>(In);
           */
      }
      return size;
  }

  template <class APClass> void Unpack(
          char *buffer_, int offset,
          ActiveParticleType** OutList_, int OutCount) {

      APClass **OutList = reinterpret_cast<APClass**>(OutList_);
      AttributeVector &handlers = APClass::AttributeHandlers;
      APClass *Out;
      int i;
      char *buffer = buffer_;

      for (i = 0; i < OutCount; i++) {
          Out = new APClass();
          OutList[i + offset] = Out;
          for(AttributeVector::iterator it = handlers.begin();
              it != handlers.end(); ++it) {
              (*it)->SetAttribute(&buffer, Out);
          }
          /*
          std::cout << "APU[" << MyProcessorNumber << "] " << i << " ";
          PrintActiveParticle<APClass>(Out);
          */
      }

  }

}

//! maps the name of a plug-in to a pointer of the factory pattern
class ActiveParticleType_info;
typedef std::map<std::string, ActiveParticleType_info *> ActiveParticleMap;

ActiveParticleMap &get_active_particle_types();

void EnableActiveParticleType(char *active_particle_type_name);

ActiveParticleType** ActiveParticleFindAll(LevelHierarchyEntry *LevelArray[], int *GlobalNumberOfActiveParticles, 
					   int ActiveParticleIDToFind);

class ActiveParticleType_info
{
public:
       
  /* We will add more functions to this as necessary */
  ActiveParticleType_info
  (std::string this_name,
   /* These functions hang off the ActiveParticle subclass */
   int (*evaluate_formation)(grid *thisgrid_orig, ActiveParticleFormationData &data),
   void (*describe_data)(ActiveParticleFormationDataFlags &flags),
   int (*initialize)(),
   int (*feedback)(grid *thisgrid_orig, ActiveParticleFormationData &data),
   int (*before_evolvelevel)(HierarchyEntry *Grids[], TopGridData *MetaData,
		  int NumberOfGrids, LevelHierarchyEntry *LevelArray[], 
		  int ThisLevel, int TotalStarParticleCountPrevious[],
		  int ActiveParticleID),
   int (*after_evolvelevel)(HierarchyEntry *Grids[], TopGridData *MetaData,
		  int NumberOfGrids, LevelHierarchyEntry *LevelArray[], 
		  int ThisLevel, int TotalStarParticleCountPrevious[],
		  int ActiveParticleID),
   int (*deposit_mass)(HierarchyEntry *Grids[], TopGridData *MetaData,
				int NumberOfGrids, LevelHierarchyEntry *LevelArray[], 
				int ThisLevel, int GalaxyParticleID),
   int (*flagfield)(LevelHierarchyEntry *LevelArray[], int level, int TopGridDims[], int ActiveParticleID),
   void (*allocate_buffer)(int Count, char **buffer),
   int (*fill_buffer)(ActiveParticleType **InList_, int InCount, char *buffer),
   void (*unpack_buffer)(char *buffer, int offset, ActiveParticleType** Outlist,
                       int OutCount),
   int (*element_size)(void),
   void (*write_particles)(ActiveParticleType **particles, 
                         int type_id, int total_particles,
                         const std::string name, hid_t node),
   int (*read_particles)(ActiveParticleType **particles, int &offset, const
                         std::string name, hid_t node),
   ActiveParticleType *particle)
   {

    this->InitializeParticleType = initialize;
    this->EvaluateFormation = evaluate_formation;
    this->EvaluateFeedback = feedback;
    this->BeforeEvolveLevel = before_evolvelevel;
    this->AfterEvolveLevel = after_evolvelevel;
    this->DepositMass = deposit_mass;
    this->SetFlaggingField = flagfield;
    this->DescribeSupplementalData = describe_data;
    this->FillBuffer = fill_buffer;
    this->AllocateBuffer = allocate_buffer;
    this->UnpackBuffer = unpack_buffer;
    this->ReturnElementSize = element_size;
    this->WriteParticles = write_particles;
    this->ReadParticles = read_particles;
    this->particle_instance = particle;
    this->particle_name = this_name;
    get_active_particle_types()[this_name] = this;
  }

  static int count(){return get_active_particle_types().size();}
  int GetEnabledParticleID(){return this->MyEnabledParticleID;}
  std::string GetEnabledParticleName(){return this->particle_name;}

  int Enable(){
    /* 0-indexed */
    this->MyEnabledParticleID = this->TotalEnabledParticleCount++;
    this->particle_instance->GetEnabledParticleID(this->MyEnabledParticleID);
    return this->MyEnabledParticleID;
  }

  int (*InitializeParticleType)(void);
  int (*EvaluateFormation)(grid *thisgrid_orig, ActiveParticleFormationData &data);
  int (*EvaluateFeedback)(grid *thisgrid_orig, ActiveParticleFormationData &data);
  int (*BeforeEvolveLevel)(HierarchyEntry *Grids[], TopGridData *MetaData,
				     int NumberOfGrids, LevelHierarchyEntry *LevelArray[], 
				     int ThisLevel, int TotalStarParticleCountPrevious[],
				     int ActiveParticleID);
  int (*AfterEvolveLevel)(HierarchyEntry *Grids[], TopGridData *MetaData,
				    int NumberOfGrids, LevelHierarchyEntry *LevelArray[], 
				    int ThisLevel, int TotalStarParticleCountPrevious[],
				    int ActiveParticleID);
  int (*DepositMass)(HierarchyEntry *Grids[], TopGridData *MetaData,
                    int NumberOfGrids, LevelHierarchyEntry *LevelArray[],
                    int ThisLevel, int ActiveParticleID);
  int (*SetFlaggingField)(LevelHierarchyEntry *LevelArray[], int level, int TopGridDims[], int ActiveParticleID);
  void (*DescribeSupplementalData)(ActiveParticleFormationDataFlags &flags);
  void (*AllocateBuffer)(int Count, char **buffer);
  void (*UnpackBuffer)(char *buffer, int offset, ActiveParticleType **Outlist,
                       int OutCount);
  int (*FillBuffer)(ActiveParticleType **InList, int InCount, char *buffer);
  int (*ReturnElementSize)(void);
  void (*WriteParticles)(ActiveParticleType **InList, 
                       int ParticleTypeID, int TotalParticles,
                       const std::string name, hid_t node);
  int (*ReadParticles)(ActiveParticleType **OutList, int &offset, const
                       std::string name, hid_t node);
  ActiveParticleType* particle_instance;
  std::string particle_name;

  // At the moment the communication buffers don't contain a header.
  int ReturnHeaderSize(void) { return 0; }

private:
  /* This is distinct from the global as a redundant error-checking
     pattern */
  static int TotalEnabledParticleCount;
  int MyEnabledParticleID; /* Defaults to 0 */
  int *EnabledParticleIDPointer;

};

template <class APClass>
ActiveParticleType_info *register_ptype(std::string name)
{
  APClass *pp = new APClass();
  
  ActiveParticleType_info *pinfo = new ActiveParticleType_info
    (name,
     (&APClass::EvaluateFormation),
     (&APClass::DescribeSupplementalData),
     (&APClass::InitializeParticleType),
     (&APClass::EvaluateFeedback),
     (&APClass::template BeforeEvolveLevel<APClass>),
     (&APClass::template AfterEvolveLevel<APClass>),
     (&APClass::template DepositMass<APClass>),
     (&APClass::SetFlaggingField),
     (&ActiveParticleHelpers::Allocate<APClass>),
     (&ActiveParticleHelpers::FillBuffer<APClass>),
     (&ActiveParticleHelpers::Unpack<APClass>),
     (&ActiveParticleHelpers::CalculateElementSize<APClass>),
     (&ActiveParticleHelpers::WriteParticles<APClass>),
     (&ActiveParticleHelpers::ReadParticles<APClass>),
     pp);
  return pinfo;
}

#define ENABLED_PARTICLE_ID_ACCESSOR					\
  int GetEnabledParticleID(int myid = -1) {				\
    static int ParticleID = -1;						\
    if (myid >= 0) {							\
      if (ParticleID != -1) ENZO_FAIL("Setting Particle ID Twice!");	\
      ParticleID = myid;						\
    }									\
    return ParticleID;							\
  };


#endif