|
| 1 | +// See LICENSE_CELLO file for license and copyright information |
| 2 | + |
| 3 | +/// @file enzo_EnzoInitialZeldovichPancake.cpp |
| 4 | +/// @author Matthew Abruzzo ([email protected]) |
| 5 | +/// @date Sun May 22 2022 |
| 6 | +/// @brief Implementation of Zeldovich Pancake initializer |
| 7 | + |
| 8 | +#include "cello.hpp" |
| 9 | +#include "enzo.hpp" |
| 10 | + |
| 11 | +//---------------------------------------------------------------------- |
| 12 | + |
| 13 | +namespace{ |
| 14 | +struct coordinate_pair{ double x_eulerian; double x_lagrangian; }; |
| 15 | +} |
| 16 | + |
| 17 | +//---------------------------------------------------------------------- |
| 18 | + |
| 19 | +void EnzoInitialZeldovichPancake::enforce_block |
| 20 | +( Block * block, const Hierarchy * hierarchy ) throw() |
| 21 | +{ |
| 22 | + // this function tries to port the initialization routine from |
| 23 | + // Grid_ZeldovichPancakeInitializeGrid.C |
| 24 | + // The initial conditions seem to come from Ryu+ (1993) - they are further |
| 25 | + // discussed in Collins+ (2010) and Li+ (2008). Unfortunately, it's not |
| 26 | + // obvious to me what the analytic solution for linear collapse is |
| 27 | + const double _TOL = 1e-6; |
| 28 | + |
| 29 | + EnzoPhysicsCosmology * cosmology = enzo::cosmology(); |
| 30 | + |
| 31 | + ASSERT("EnzoInitialZeldovichPancake::enforce_block", |
| 32 | + "Cosmology must be in use", enzo::cosmology() != nullptr); |
| 33 | + // todo: add more flexibility or at least be more explicit about the problem |
| 34 | + ASSERT("EnzoInitialZeldovichPancake::enforce_block", |
| 35 | + "invalid cosmological parameters", |
| 36 | + (cosmology->omega_matter_now() == 1.0) & |
| 37 | + (cosmology->omega_baryon_now() == 1.0) & |
| 38 | + (cosmology->omega_cdm_now() == 0.0) & |
| 39 | + (cosmology->omega_lambda_now() == 0.0) ); |
| 40 | + |
| 41 | + const double init_z = cosmology->initial_redshift(); |
| 42 | + const double omega_baryon_now = cosmology->omega_baryon_now(); |
| 43 | + |
| 44 | + // todo: make some/most of the following configurable in the future |
| 45 | + const double init_temperature_K = 100.0; // in units of kelvin |
| 46 | + const double pancake_central_offset = 0.0; |
| 47 | + const double collapse_z = 1.0; |
| 48 | + |
| 49 | + double x_domain_min, x_domain_max; |
| 50 | + cello::hierarchy()->lower(&x_domain_min, nullptr, nullptr); |
| 51 | + cello::hierarchy()->upper(&x_domain_max, nullptr, nullptr); |
| 52 | + const double lambda = x_domain_max - x_domain_min; |
| 53 | + |
| 54 | + // 1. precompute some relevant quantities: |
| 55 | + const double dbl_pi = 2.0 * cello::pi; |
| 56 | + const double kx = dbl_pi / lambda; |
| 57 | + const double amplitude = (1.0 + collapse_z) / (1.0 + init_z); |
| 58 | + const double amplitude_vel = |
| 59 | + -std::sqrt(2.0/3.0) * (1.0 + collapse_z) / ((1.0 + init_z) * dbl_pi); |
| 60 | + |
| 61 | + // 2. Construct a function that returns lagrangian and eularian positions |
| 62 | + |
| 63 | + Data* data = block->data(); |
| 64 | + Field field = data->field(); |
| 65 | + int mx,my,mz; |
| 66 | + int gx,gy,gz; |
| 67 | + field.dimensions (field.field_id("density"),&mx,&my,&mz); |
| 68 | + field.ghost_depth(field.field_id("density"),&gx,&gy,&gz); |
| 69 | + |
| 70 | + std::vector<double> xc(mx); |
| 71 | + std::vector<double> dummy(std::max(my, mz)); |
| 72 | + data->field_cells (xc.data(), dummy.data(), dummy.data(), gx, gy, gz); |
| 73 | + const double pancake_center = (0.5*(x_domain_min + x_domain_max) + |
| 74 | + pancake_central_offset); |
| 75 | + |
| 76 | + auto get_pos_pair = [&](int ix) |
| 77 | + { |
| 78 | + // I don't know why we aren't starting with eulerian coordinates and then |
| 79 | + // computing lagrangian coordinates... (we're following what Enzo does) |
| 80 | + double x_lagrange = xc[ix] - pancake_center; |
| 81 | + |
| 82 | + double x_euler = x_lagrange; |
| 83 | + double x_euler_old = std::numeric_limits<double>::max(); |
| 84 | + while (fabs((x_euler-x_euler_old)/x_euler) > _TOL) { |
| 85 | + x_euler_old = x_euler; |
| 86 | + x_euler += (x_lagrange - x_euler + amplitude*std::sin(kx*x_euler)/kx) |
| 87 | + /(1 - amplitude*std::cos(kx*x_euler) ); |
| 88 | + } |
| 89 | + coordinate_pair out = {x_euler, x_lagrange}; |
| 90 | + return out; |
| 91 | + }; |
| 92 | + |
| 93 | + // todo: use CelloArrays (we aren't using them now to avoid breaking stuff |
| 94 | + // when we merge in another PR) |
| 95 | + enzo_float * density = (enzo_float *) field.values("density"); |
| 96 | + enzo_float * e_int = (enzo_float *) field.values("internal_energy"); |
| 97 | + const bool idual = (e_int != nullptr); |
| 98 | + enzo_float * e_tot = (enzo_float *) field.values("total_energy"); |
| 99 | + enzo_float * vx = (enzo_float *) field.values("velocity_x"); |
| 100 | + enzo_float * vy = (enzo_float *) field.values("velocity_y"); |
| 101 | + enzo_float * vz = (enzo_float *) field.values("velocity_z"); |
| 102 | + |
| 103 | + EnzoUnits * units = enzo::units(); |
| 104 | + |
| 105 | + const double gm1 = gamma_ - 1.0; |
| 106 | + // convert to problem units |
| 107 | + const double bulkv = 0.0; |
| 108 | + const double temperature_units = units->temperature(); |
| 109 | + |
| 110 | + for (int iz=gz; iz<mz-gz; iz++) { |
| 111 | + for (int iy=gy; iy<my-gy; iy++) { |
| 112 | + for (int ix=gx; ix<mx-gx; ix++) { |
| 113 | + coordinate_pair coord_pair = get_pos_pair(ix); |
| 114 | + double x_euler = coord_pair.x_eulerian; |
| 115 | + |
| 116 | + int i = ix + mx*(iy + my*iz); |
| 117 | + |
| 118 | + double cur_density = |
| 119 | + omega_baryon_now / (1 - amplitude*std::cos(kx*x_euler)); |
| 120 | + double cur_vx = amplitude_vel * sin(kx*x_euler) + bulkv; |
| 121 | + double cur_eint = |
| 122 | + ((init_temperature_K / temperature_units) * |
| 123 | + std::pow(cur_density / omega_baryon_now, gm1) / gm1); |
| 124 | + double cur_etot = cur_eint + 0.5 * cur_vx * cur_vx; |
| 125 | + |
| 126 | + density[i] = (enzo_float)cur_density; |
| 127 | + vx[i] = cur_vx; |
| 128 | + vy[i] = 0.0; |
| 129 | + vz[i] = 0.0; |
| 130 | + e_tot[i] = cur_etot; |
| 131 | + if (idual) { e_int[i] = cur_eint; } |
| 132 | + |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // set initial time! |
| 138 | + EnzoInitialCosmology::init_cosmology(block, -1, -1); |
| 139 | + |
| 140 | + block->initial_done(); |
| 141 | +} |
| 142 | + |
0 commit comments