Source

enzo-dev-problem-types / src / enzo / ProblemType_RotatingCylinder.C

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
/***********************************************************************
/
/  ROTATING CYLINDER PROBLEM TYPE
/
/  written by: Matthew Turk, Brian O'Shea
/  date:       July, 2010
/
/  PURPOSE:
/
************************************************************************/

#ifdef NEW_PROBLEM_TYPES
#include <string.h>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <iostream>
#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 "ProblemType.h"
#include "EventHooks.h"

class ProblemType_RotatingCylinder;

class RotatingCylinderGrid : private grid {
    friend class ProblemType_RotatingCylinder;
};

int FindField(int field, int farray[], int numfields);

void JustPrintSomething(HierarchyEntry *Grids[], TopGridData &MetaData)
{
    std::cout << "I am being called!" << std::endl;
}

class ProblemType_RotatingCylinder : public EnzoProblemType
{
    private:
        FLOAT RotatingCylinderSubgridLeft, RotatingCylinderSubgridRight;
        FLOAT LeftEdge[MAX_DIMENSION], RightEdge[MAX_DIMENSION];
        FLOAT RotatingCylinderCenterPosition[MAX_DIMENSION];
        float RotatingCylinderVelocity[3];   // gas initally at rest
        float RotatingCylinderBField[3];   // gas initally at rest
        FLOAT RotatingCylinderRadius;
        float RotatingCylinderLambda;
        float RotatingCylinderOverdensity;
        float RotatingCylinderDensity;
        float RotatingCylinderTotalEnergy;

    public:
    ProblemType_RotatingCylinder() : EnzoProblemType()
    { 
        std::cout << "Creating problem type Rotating Cylinder" << std::endl;
        RegisterEventPlugin("Printing", &JustPrintSomething);
        RegisterEventHook("EvolveLevelTop", "Printing");
    }

    ~ProblemType_RotatingCylinder()
    {
    }

    virtual int InitializeFromRestart(
            HierarchyEntry &TopGrid, TopGridData &MetaData)
    {
       return SUCCESS;
    }

    virtual int InitializeSimulation(FILE *fptr, FILE *Outfptr,
            HierarchyEntry &TopGrid, TopGridData &MetaData)
    {
      if(debug){
        printf("Entering RotatingCylinderInitialize\n");
        fflush(stdout);
      }

      char *DensName = "Density";
      char *TEName   = "TotalEnergy";
      char *GEName   = "GasEnergy";
      char *Vel1Name = "x-velocity";
      char *Vel2Name = "y-velocity";
      char *Vel3Name = "z-velocity";
      char *MetalName = "Metal_Density";

      /* local declarations */

      int  i, j, dim, NumberOfSubgridZones[MAX_DIMENSION],
           SubgridDims[MAX_DIMENSION];

      /* make sure it is 3D */

      if (MetaData.TopGridRank != 3) {
        printf("Cannot do RotatingCylinder in %"ISYM" dimension(s)\n", MetaData.TopGridRank);
        ENZO_FAIL("");
      }

      for(i=0; i<MAX_DIMENSION; i++)
        RotatingCylinderCenterPosition[i] = 0.5;  // right in the middle of the box

      this->RotatingCylinderVelocity[0] = 
        this->RotatingCylinderVelocity[1] = 
        this->RotatingCylinderVelocity[2] = 0.0; // gas initally at rest
      this->RotatingCylinderBField[0] =
        this->RotatingCylinderBField[1] =
        this->RotatingCylinderBField[2] = 0.0; // gas initally at rest
      this->RotatingCylinderRadius = 0.3;
      this->RotatingCylinderLambda = 0.05;
      this->RotatingCylinderOverdensity = 20.0;
      this->RotatingCylinderDensity = 1.0;
      this->RotatingCylinderTotalEnergy = 1.0;
      float Pi                      = 3.14159;

      /* set no subgrids by default. */

      RotatingCylinderSubgridLeft         = 0.0;    // start of subgrid(s)
      RotatingCylinderSubgridRight        = 0.0;    // end of subgrid(s)

      /* read input from file */

      this->ReadConfig(fptr);

      FieldContainer background;
      this->SetupFields(background);

      this->InitializeUniformGrid(TopGrid.GridData,
            RotatingCylinderDensity,
            RotatingCylinderTotalEnergy,
            RotatingCylinderTotalEnergy,
            RotatingCylinderVelocity,
            RotatingCylinderBField);

      /* Create as many subgrids as there are refinement levels
         needed to resolve the initial explosion region upon the start-up. */

      HierarchyEntry ** Subgrid;
      if (MaximumRefinementLevel > 0)
        Subgrid   = new HierarchyEntry*[MaximumRefinementLevel];

      /* Create new HierarchyEntries. */

      int lev;
      for (lev = 0; lev < MaximumRefinementLevel; lev++)
        Subgrid[lev] = new HierarchyEntry;

      for (lev = 0; lev < MaximumRefinementLevel; lev++) {

        for (dim = 0; dim < MetaData.TopGridRank; dim++)
          NumberOfSubgridZones[dim] =
            nint((RotatingCylinderSubgridRight - RotatingCylinderSubgridLeft)/
                ((DomainRightEdge[dim] - DomainLeftEdge[dim] )/
                 float(MetaData.TopGridDims[dim])))
            *int(POW(RefineBy, lev + 1));

        if (debug)
          printf("RotatingCylinder:: Level[%"ISYM"]: NumberOfSubgridZones[0] = %"ISYM"\n", lev+1,
              NumberOfSubgridZones[0]);

        if (NumberOfSubgridZones[0] > 0) {

          /* fill them out */

          if (lev == 0)
            TopGrid.NextGridNextLevel  = Subgrid[0];
          Subgrid[lev]->NextGridThisLevel = NULL;
          if (lev == MaximumRefinementLevel-1)
            Subgrid[lev]->NextGridNextLevel = NULL;
          else
            Subgrid[lev]->NextGridNextLevel = Subgrid[lev+1];
          if (lev == 0)
            Subgrid[lev]->ParentGrid        = &TopGrid;
          else
            Subgrid[lev]->ParentGrid        = Subgrid[lev-1];

          /* compute the dimensions and left/right edges for the subgrid */

          for (dim = 0; dim < MetaData.TopGridRank; dim++) {
            SubgridDims[dim] = NumberOfSubgridZones[dim] + 2*DEFAULT_GHOST_ZONES;
            LeftEdge[dim]    = RotatingCylinderSubgridLeft;
            RightEdge[dim]   = RotatingCylinderSubgridRight;
          }

          /* create a new subgrid and initialize it */

          Subgrid[lev]->GridData = this->CreateNewUniformGrid(
                                        TopGrid.GridData,
                                        MetaData.TopGridRank, SubgridDims,
                                        LeftEdge, RightEdge, 0,
                                        RotatingCylinderDensity,
                                        RotatingCylinderTotalEnergy,
                                        RotatingCylinderTotalEnergy,
                                        RotatingCylinderVelocity,
                                        RotatingCylinderBField);

          /* set up the initial explosion area on the finest resolution subgrid */

          if (lev == MaximumRefinementLevel - 1)
            if (this->InitializeGrid(Subgrid[lev]->GridData, TopGrid, MetaData)
                == FAIL) {
              ENZO_FAIL("Error in RotatingCylinderInitialize[Sub]Grid.");
            }

        }
        else{
          printf("RotatingCylinder: single grid start-up.\n");
        }
      }

      this->FinalizeGrids(Subgrid, TopGrid, MetaData);

      /* set up field names and units -- NOTE: these absolutely MUST be in 
         the same order that they are in Grid_InitializeUniformGrids.C, or 
         else you'll find out that data gets written into incorrectly-named
         fields.  Just FYI. */

      i = 0;
      DataLabel[i++] = DensName;
      DataLabel[i++] = TEName;
      if(DualEnergyFormalism)
        DataLabel[i++] = GEName;
      DataLabel[i++] = Vel1Name;

      if(MetaData.TopGridRank > 1)
        DataLabel[i++] = Vel2Name;

      if(MetaData.TopGridRank > 2)
        DataLabel[i++] = Vel3Name;

      if (TestProblemData.UseMetallicityField)
        DataLabel[i++] = MetalName;

      for(j=0; j < i; j++)
        DataUnits[j] = NULL;

      /* Write parameters to parameter output file */

      if (MyProcessorNumber == ROOT_PROCESSOR) {
        fprintf(Outfptr, "RotatingCylinderOverdensity         = %"FSYM"\n"  , RotatingCylinderOverdensity);
        fprintf(Outfptr, "RotatingCylinderLambda         = %"FSYM"\n"  , RotatingCylinderLambda);
        fprintf(Outfptr, "RotatingCylinderTotalEnergy         = %"FSYM"\n"  , RotatingCylinderTotalEnergy);
        fprintf(Outfptr, "RotatingCylinderRadius         = %"PSYM"\n"  , RotatingCylinderRadius);
        fprintf(Outfptr, "RotatingCylinderCenterPosition = %"PSYM" %"PSYM" %"PSYM"\n",
            RotatingCylinderCenterPosition, RotatingCylinderCenterPosition+1,
            RotatingCylinderCenterPosition+2);
        fprintf(Outfptr, "TestProblemUseMetallicityField  = %"ISYM"\n", TestProblemData.UseMetallicityField);
        fprintf(Outfptr, "TestProblemInitialMetallicityFraction  = %"FSYM"\n", TestProblemData.MetallicityField_Fraction);

      } //   if (MyProcessorNumber == ROOT_PROCESSOR) 


      if(debug){
        printf("Exiting RotatingCylinderInitialize\n");
        fflush(stdout);
      }

      return SUCCESS;

    }

/*

This is the grid-by-grid initializer.

*/
    int InitializeGrid(grid *thisgrid_orig,
            HierarchyEntry &TopGrid, TopGridData &MetaData)
    {

      RotatingCylinderGrid *thisgrid =
        static_cast<RotatingCylinderGrid *>(thisgrid_orig);

      if (thisgrid->ProcessorNumber != MyProcessorNumber)
        return SUCCESS;

      if(debug){
        printf("Entering RotatingCylinderInitializeGrid\n");
        fflush(stdout);
      }

      printf("RotatingCylinderRadius = %e\n", this->RotatingCylinderRadius);
      printf("RotatingCylinderCenterPosition = %e %e %e\n", 
          this->RotatingCylinderCenterPosition[0],
          this->RotatingCylinderCenterPosition[1],
          this->RotatingCylinderCenterPosition[2]);
      printf("RotatingCylinderLambda = %e\n",this->RotatingCylinderLambda);
      printf("RotatingCylinderOverdensity = %e\n",this->RotatingCylinderOverdensity);


      /* declarations */

      int size = 1, dim, cellindex;

      for (dim = 0; dim < thisgrid->GridRank; dim++)
        size *= thisgrid->GridDimension[dim];

      FLOAT r,x,y,z, radius, zdist;

      float sintheta, costheta, omega;

      int DensNum, GENum, TENum, Vel1Num, Vel2Num, Vel3Num, MetalNum;

      if (thisgrid->IdentifyPhysicalQuantities(DensNum, GENum, Vel1Num, Vel2Num,
            Vel3Num, TENum) == FAIL) {
        fprintf(stderr, "Error in IdentifyPhysicalQuantities.\n");
        ENZO_FAIL("");
      }

      int MetallicityField = FALSE;
      if ((MetalNum = FindField(Metallicity, thisgrid->FieldType, thisgrid->NumberOfBaryonFields))
          != -1)
        MetallicityField = TRUE;
      else
        MetalNum = 0;

      /* set fields in the cylinder region */

      int index, jndex, i, j, k;
      float outside_rho, outside_TE, outside_GE;

      outside_rho =  thisgrid->BaryonField[DensNum][0];

      omega = RotatingCylinderLambda * sqrt(GravitationalConstant * RotatingCylinderOverdensity * outside_rho) / 0.117;

      if(HydroMethod==2){  // ZEUS

        outside_TE = thisgrid->BaryonField[TENum][0];

      } else { // PPM

        outside_TE = thisgrid->BaryonField[TENum][0];

        if(DualEnergyFormalism){
          outside_GE = thisgrid->BaryonField[GENum][0];
        }

      }  // if(HydroMethod==2)

      for (k = 0; k < thisgrid->GridDimension[2]; k++)
        for (j = 0; j < thisgrid->GridDimension[1]; j++)
          for (i = 0; i < thisgrid->GridDimension[0]; i++){

            /* Compute position */
            x=y=z=0.0;

            cellindex = i + j*thisgrid->GridDimension[0]
                          + k*thisgrid->GridDimension[0]
                             *thisgrid->GridDimension[1];

            x = thisgrid->CellLeftEdge[0][i] + 0.5*thisgrid->CellWidth[0][i];
            y = thisgrid->CellLeftEdge[1][j] + 0.5*thisgrid->CellWidth[1][j];
            z = thisgrid->CellLeftEdge[2][k] + 0.5*thisgrid->CellWidth[2][k];

            /* Find distance from center. */

            // it's REALLY r^2 right now
            radius = POW(x-RotatingCylinderCenterPosition[0], 2.0) +
              POW(y-RotatingCylinderCenterPosition[1], 2.0);

            radius = sqrt(radius);  // ok, now it's just radius

            zdist = fabs(z-RotatingCylinderCenterPosition[2]);

            if ( (radius <= RotatingCylinderRadius) && (zdist <= RotatingCylinderRadius) ){

              thisgrid->BaryonField[DensNum][cellindex] = outside_rho * RotatingCylinderOverdensity;

              if(TestProblemData.UseMetallicityField>0 && MetalNum != FALSE)
                thisgrid->BaryonField[MetalNum][cellindex] = thisgrid->BaryonField[DensNum][cellindex]*TestProblemData.MetallicityField_Fraction;

              sintheta = (y-RotatingCylinderCenterPosition[1])/radius;
              costheta = (x-RotatingCylinderCenterPosition[0])/radius;


              // x,y, and maybe z velocity.  
              thisgrid->BaryonField[Vel1Num][cellindex] = -1.0*sintheta*omega*radius;

              thisgrid->BaryonField[Vel2Num][cellindex] = costheta*omega*radius;

              thisgrid->BaryonField[Vel3Num][cellindex] = 0.0;

              if(HydroMethod == 2){

                // ZEUS
                thisgrid->BaryonField[TENum][cellindex] = outside_TE / RotatingCylinderOverdensity;

              } else {

                // PPM
                thisgrid->BaryonField[TENum][cellindex] = outside_TE / RotatingCylinderOverdensity
                  + 0.5 * thisgrid->BaryonField[Vel1Num][cellindex] *
                  thisgrid->BaryonField[Vel1Num][cellindex]
                  + 0.5 * thisgrid->BaryonField[Vel2Num][cellindex] *
                  thisgrid->BaryonField[Vel2Num][cellindex]
                  + 0.5 * thisgrid->BaryonField[Vel3Num][cellindex] *
                  thisgrid->BaryonField[Vel3Num][cellindex];

                // gas energy (PPM dual energy formalims)
                if(DualEnergyFormalism)
                  thisgrid->BaryonField[GENum][cellindex] = outside_GE / RotatingCylinderOverdensity;

              } // if(HydroMethod == 2)

            } // if (r <= RotatingCylinderRadius)

          } // for (i = 0; i < GridDimension[0]; i++)

      if(debug){
        printf("Exiting RotatingCylinderInitialize\n");
        fflush(stdout);
      }

      return SUCCESS;

    } 


    void SetupFields(FieldContainer &Fields)
    {
        Fields.add_field("Density", Density, RotatingCylinderDensity);
        Fields.add_field("TotalEnergy", TotalEnergy, RotatingCylinderTotalEnergy);
        Fields.add_field("GasEnergy", InternalEnergy,
                          RotatingCylinderTotalEnergy);
        Fields.add_field("x-velocity", Velocity1, 0.0);
        Fields.add_field("y-velocity", Velocity2, 0.0);
        Fields.add_field("z-velocity", Velocity3, 0.0);
        Fields.add_field("Metal_Density", Metallicity, 0.0);
    }

    void ReadConfig(FILE *fptr)
    {
        /* read input from file */
      char line[MAX_LINE_LENGTH];
      int ret;
      while (fgets(line, MAX_LINE_LENGTH, fptr) != NULL) {
          ret = 0;

          /* read parameters specifically for RotatingCylinder problem*/
          ret += sscanf(line, "RotatingCylinderOverdensity  = %"FSYM, this->RotatingCylinderOverdensity);
          ret += sscanf(line, "RotatingCylinderSubgridLeft = %"PSYM,
              this->RotatingCylinderSubgridLeft);
          ret += sscanf(line, "RotatingCylinderSubgridRight = %"PSYM,
              this->RotatingCylinderSubgridRight);
          ret += sscanf(line, "RotatingCylinderLambda = %"FSYM,
              this->RotatingCylinderLambda);
          ret += sscanf(line, "RotatingCylinderTotalEnergy = %"FSYM,
              this->RotatingCylinderTotalEnergy);
          ret += sscanf(line, "RotatingCylinderRadius = %"PSYM,
              this->RotatingCylinderRadius);
          ret += sscanf(line, "RotatingCylinderCenterPosition = %"PSYM" %"PSYM" %"PSYM,
            this->RotatingCylinderCenterPosition, (this->RotatingCylinderCenterPosition)+1,
            (this->RotatingCylinderCenterPosition)+2);

          ret += sscanf(line, "TestProblemUseMetallicityField  = %"ISYM, TestProblemData.UseMetallicityField);
          ret += sscanf(line, "TestProblemInitialMetallicityFraction  = %"FSYM, TestProblemData.MetallicityField_Fraction);

          /* if the line is suspicious, issue a warning */

          if (ret == 0 && strstr(line, "=") && (strstr(line, "RotatingCylinder") || strstr(line, "TestProblem")) &&
              line[0] != '#' && MyProcessorNumber == ROOT_PROCESSOR)
            fprintf(stderr,
                "*** warning: the following parameter line was not interpreted:\n%s\n", line);

    } // end input from parameter file
  }  

};


//.. register:
namespace{
    EnzoProblemType_creator_concrete<ProblemType_RotatingCylinder>
        rotating_cylinder("RotatingCylinder");
}

#endif