Source

enzo-3.0 / src / enzo / particles / active_particles / ActiveParticle.C

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
/***********************************************************************
/
/  PROBLEM TYPE CLASS
/
/  written by: Matthew Turk, Oliver Hahn
/  date:       July, 2010
/
/  PURPOSE:
/
************************************************************************/

#ifdef USE_MPI
#include "mpi.h"
#endif 

#include "preincludes.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 "fortran.def"
#include "CosmologyParameters.h"

#include "ActiveParticle.h"

/* function prototypes */
 
int CosmologyComputeExpansionFactor(FLOAT time, FLOAT *a, FLOAT *dadt);
int GetUnits(float *DensityUnits, float *LengthUnits,
	     float *TemperatureUnits, float *TimeUnits,
	     float *VelocityUnits, FLOAT Time);
int FindField(int field, int farray[], int numfields);

extern "C" void FORTRAN_NAME(copy3d)(float *source, float *dest,
                                   int *sdim1, int *sdim2, int *sdim3,
                                   int *ddim1, int *ddim2, int *ddim3,
                                   int *sstart1, int *sstart2, int *sstart3,
                                   int *dstart1, int *dstart2, int *dststart3);
 
ActiveParticleMap& get_active_particle_types()
{
    static ActiveParticleMap active_particle_type_map;
    return active_particle_type_map;
}

void EnableActiveParticleType(char *active_particle_type_name) {
    std::string dummy = std::string(active_particle_type_name);
    ActiveParticleType_info *my_type =
        get_active_particle_types()[dummy];
    if (my_type == NULL) {
        ENZO_FAIL("Unknown ParticleType");
    }
    
    // retrieves active particle specific parameters
    my_type->InitializeParticleType();

    EnabledActiveParticles[EnabledActiveParticlesCount++] = my_type;
    int this_id = my_type->Enable();
    /* Note that this ID is only unique for each individual instantiation of
       Enzo.  The next time you start it, these numbers might be different.
       This is why they aren't output to a file!
    */
    assert(my_type->GetEnabledParticleID() == EnabledActiveParticlesCount-1);
    return;
}

int ActiveParticleType::ReadDataset(int ndims, hsize_t *dims, const char *name,
                    hid_t group, hid_t data_type, void *read_to)
{
  hid_t file_dsp_id;
  hid_t dset_id;
  hid_t h5_status;
  herr_t      h5_error = -1;
  int i, j, k, dim;
  /* get data into temporary array */

  file_dsp_id = H5Screate_simple((Eint32) ndims, dims, NULL);
  if( file_dsp_id == h5_error ){ENZO_FAIL("Error creating file dataspace");}

  dset_id =  H5Dopen(group, name);
  if( dset_id == h5_error )ENZO_VFAIL("Error opening %s", name)

  h5_status = H5Dread(dset_id, data_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, (VOIDP) read_to);
  if( dset_id == h5_error )ENZO_VFAIL("Error reading %s", name)

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

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

int ActiveParticleType::WriteDataset(int ndims, hsize_t *dims, const char *name, hid_t group,
				     hid_t data_type, void *data)
{

  hid_t file_dsp_id;
  hid_t dset_id;
  hid_t h5_status;
  herr_t h5_error = -1;

  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) data);
  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;
}



void ActiveParticleType::ConstructData(grid *_grid,
            ActiveParticleFormationDataFlags &flags,
            ActiveParticleFormationData &data) {

    /* We have a number of items that can be required; we now attempt to
       generate them.

       This uses code from the old grid::StarParticleHandler routine. */

    
  /* initialize */
 
  int dim, i, j, k, index, size, field, GhostZones = DEFAULT_GHOST_ZONES;
  int DensNum, GENum, TENum, Vel1Num, Vel2Num, Vel3Num, B1Num, B2Num, B3Num,H2INum, H2IINum;
  const double m_h = 1.673e-24;

  /* Compute size (in floats) of the current grid. */
 
  size = 1;
  for (dim = 0; dim < _grid->GridRank; dim++)
    size *= _grid->GridDimension[dim];
 
  data.CellSize = _grid->CellWidth[0][0];

  /* Find fields: density, total energy, velocity1-3. */
 
  _grid->DebugCheck("StarParticleHandler");
  if (_grid->IdentifyPhysicalQuantities(DensNum, GENum, Vel1Num, Vel2Num,
				       Vel3Num, TENum, B1Num, B2Num, B3Num) == FAIL) {
        ENZO_FAIL("Error in IdentifyPhysicalQuantities.");
  }
 
  /* Set the units. */
 
  data.DensityUnits = 1, data.LengthUnits = 1, data.TemperatureUnits = 1,
    data.TimeUnits = 1, data.VelocityUnits = 1;
  if (GetUnits(&data.DensityUnits, &data.LengthUnits, &data.TemperatureUnits,
	       &data.TimeUnits, &data.VelocityUnits, _grid->Time) == FAIL) {
        ENZO_FAIL("Error in GetUnits.");
  }

  data.MassUnits = data.DensityUnits*POW(data.LengthUnits,3);

  /* If using MHD, subtract magnetic energy from total energy because 
     density may be modified in star_maker8. */
  
  float *Bfieldx = NULL, *Bfieldy = NULL, *Bfieldz = NULL;
  if (HydroMethod == MHD_RK) {
    Bfieldx = _grid->BaryonField[B1Num];
    Bfieldy = _grid->BaryonField[B2Num];
    Bfieldz = _grid->BaryonField[B3Num];
    for (int n = 0; n < size; n++) {
      float den = _grid->BaryonField[DensNum][n];
      float Bx  = _grid->BaryonField[B1Num  ][n];
      float By  = _grid->BaryonField[B2Num  ][n];
      float Bz  = _grid->BaryonField[B3Num  ][n];
      float B2 = Bx*Bx + By*By + Bz*Bz;
      _grid->BaryonField[TENum][n] -= 0.5*B2/den;
    }
  }

  if (MultiSpecies > 1) {
    H2INum   = FindField(H2IDensity, _grid->FieldType, _grid->NumberOfBaryonFields);
    H2IINum  = FindField(H2IIDensity, _grid->FieldType, _grid->NumberOfBaryonFields);
  }

  /* Find metallicity field and set flag. */
 
  int SNColourNum, MetalNum, MetalIaNum, MBHColourNum, Galaxy1ColourNum, Galaxy2ColourNum; 

  _grid->IdentifyColourFields(SNColourNum, MetalNum, MetalIaNum, MBHColourNum, 
			      Galaxy1ColourNum, Galaxy2ColourNum);

  /* Now we fill in the *Num attributes of data */
  data.DensNum = DensNum;
  data.Vel1Num = Vel1Num;
  data.Vel2Num = Vel2Num;
  data.Vel3Num = Vel3Num;
  data.MetalNum = MetalNum;
  data.TENum = TENum;
  data.GENum = GENum;
  data.ColourNum = SNColourNum;
  data.MetalNum = MetalNum;
  data.MetalIaNum = MetalIaNum;

  /* Compute the redshift. */
 
  float zred;
  FLOAT a = 1, dadt;
  if (ComovingCoordinates)
    CosmologyComputeExpansionFactor(_grid->Time, &a, &dadt);
  zred = 1.0*(1.0+InitialRedshift)/a - 1.0;
 

  if (flags.Temperature) {
    /* Compute the temperature field. */

    float *temperature = new float[size];
    _grid->ComputeTemperatureField(temperature);
    data.Temperature = temperature;
  }
 
  if (flags.DarkMatterDensity) {
    /* Get the dark matter field in a usable size for star_maker
       (if level > MaximumGravityRefinementLevel then the dark matter
       field is not valid, so just make it zero - by _grid time, the
       evolution will be dominated by baryonic matter anyway). */

    float *dmfield = new float[size];
    int StartIndex[MAX_DIMENSION], Zero[] = {0,0,0};
    if (data.level <= MaximumGravityRefinementLevel &&
        _grid->GravitatingMassFieldParticles != NULL) {
      for (dim = 0; dim < MAX_DIMENSION; dim++)
        StartIndex[dim] =
          nint((_grid->CellLeftEdge[dim][0] - _grid->GravitatingMassFieldParticlesLeftEdge[dim])/
              _grid->GravitatingMassFieldParticlesCellSize);
      FORTRAN_NAME(copy3d)(_grid->GravitatingMassFieldParticles, dmfield,
          _grid->GravitatingMassFieldParticlesDimension,
          _grid->GravitatingMassFieldParticlesDimension+1,
          _grid->GravitatingMassFieldParticlesDimension+2,
          _grid->GridDimension, _grid->GridDimension+1, _grid->GridDimension+2,
          Zero, Zero+1, Zero+2,
          StartIndex, StartIndex+1, StartIndex+2);
    } else {
      for (i = 0; i < size; i++)
        dmfield[i] = 0;
    }
    data.DarkMatterDensity = dmfield;
  }
 
  _grid->ConvertColorFieldsToFractions();

  /* If creating primordial stars, make a total H2 density field */

  float *h2field = NULL;
  if (flags.H2Fraction) {
    h2field = new float[size];
    for (k = _grid->GridStartIndex[2]; k <= _grid->GridEndIndex[2]; k++)
      for (j = _grid->GridStartIndex[1]; j <= _grid->GridEndIndex[1]; j++) {
	index = (k*_grid->GridDimension[1] + j)*_grid->GridDimension[0] + 
	  _grid->GridStartIndex[0];
	for (i = _grid->GridStartIndex[0]; i <= _grid->GridEndIndex[0]; i++, index++) 
	  h2field[index] = _grid->BaryonField[H2INum][index] + _grid->BaryonField[H2IINum][index];
      }
    data.H2Fraction = h2field;
  }

  if (flags.CoolingTime) {
    /* Compute the cooling time. */
 
    data.CoolingTime = new float[size];
    _grid->ComputeCoolingTime(data.CoolingTime);
  }

  if (flags.CoolingRate) {

    /* Compute the cooling rate. */
 
    data.CoolingRate = new float[size];
    float cgsdensity;
    float *electronguessptr;
    float electronguess = 0.01;
    electronguessptr = &electronguess;
    for (i = 0; i < size; i++)
      cgsdensity = data.DensityUnits*_grid->BaryonField[DensNum][i];
      data.CoolingRate[i] = _grid->GadgetCoolingRate
	(log10(data.Temperature[i]), cgsdensity, electronguessptr, zred);

  } // ENDIF CoolingRate
 
  /* If both metal fields exist, make a total metal field */

  if (flags.MetalField) {
    float *MetalPointer = NULL;
    float *TotalMetals = NULL;
    int MetallicityField;

    MetallicityField = (MetalNum != -1 || SNColourNum != -1);

    if (MetalNum != -1 && SNColourNum != -1) {
      TotalMetals = new float[size];
      for (i = 0; i < size; i++)
        TotalMetals[i] = _grid->BaryonField[MetalNum][i]
                       + _grid->BaryonField[SNColourNum][i];
      MetalPointer = TotalMetals;
    } // ENDIF both metal types
    else {
      if (MetalNum != -1)
        MetalPointer = _grid->BaryonField[MetalNum];
      else if (SNColourNum != -1)
        MetalPointer = _grid->BaryonField[SNColourNum];
    } // ENDELSE both metal types
    data.TotalMetals = MetalPointer;
  }

  //printf("Star type \n");

  data.MaxNumberOfNewParticles = size;
  data.NewParticles = new ActiveParticleType*[size];

  return;
}

void ActiveParticleType::DestroyData(grid *_grid,
        ActiveParticleFormationData &data) {

    /* We don't actually need to inspect the flags.
     * We just need to know if the data has been allocated. */

    if (data.DarkMatterDensity != NULL) delete data.DarkMatterDensity;
    if (data.H2Fraction != NULL) delete data.H2Fraction;
    if (data.CoolingTime != NULL) delete data.CoolingTime;
    if (data.CoolingRate != NULL) delete data.CoolingRate;
    if (data.Temperature != NULL) delete data.Temperature;
    if (data.TotalMetals != NULL && data.MetalNum != -1 && data.ColourNum != -1) 
      delete data.TotalMetals;

    /* We convert back from Fractions to Values */
    _grid->ConvertColorFieldsFromFractions();

    delete data.NewParticles;

    /* We don't need to reset anything else. */

}

void ActiveParticleType::SetupBaseParticleAttributes(
    std::vector<ParticleAttributeHandler *> &handlers)
{
    typedef ActiveParticleType ap;

    /* Can be handled manually */
   
    handlers.push_back(new ArrayHandler<ap, FLOAT, 3, &ap::pos>("position_x", 0));
    handlers.push_back(new ArrayHandler<ap, FLOAT, 3, &ap::pos>("position_y", 1));
    handlers.push_back(new ArrayHandler<ap, FLOAT, 3, &ap::pos>("position_z", 2));

    handlers.push_back(new ArrayHandler<ap, float, 3, &ap::vel>("velocity_x", 0));
    handlers.push_back(new ArrayHandler<ap, float, 3, &ap::vel>("velocity_y", 1));
    handlers.push_back(new ArrayHandler<ap, float, 3, &ap::vel>("velocity_z", 2));

    handlers.push_back(new Handler<ap, double, &ap::Mass>("mass"));
    handlers.push_back(new Handler<ap, float, &ap::BirthTime>("creation_time"));
    handlers.push_back(new Handler<ap, float, &ap::DynamicalTime>("dynamical_time"));
    handlers.push_back(new Handler<ap, float, &ap::Metallicity>("metallicity"));
    handlers.push_back(new Handler<ap, PINT, &ap::Identifier>("identifier"));
    handlers.push_back(new Handler<ap, int, &ap::level>("level"));
    handlers.push_back(new Handler<ap, int, &ap::GridID>("GridID"));
}

void ActiveParticleType::OutputPositionInformation()
{
    std::cout << "P: " << MyProcessorNumber << " ";
    std::cout << "API: " << this->ReturnID() << ": ";
    std::cout << this->pos[0] << " ";
    std::cout << this->pos[1] << " ";
    std::cout << this->pos[2] << " ";
    std::cout << "V: ";
    std::cout << this->vel[0] << " ";
    std::cout << this->vel[1] << " ";
    std::cout << this->vel[2] << " ";
    std::cout << "M: ";
    std::cout << this->Mass;
    std::cout << std::endl;
}

int ActiveParticleType_info::TotalEnabledParticleCount = 0;