Skip to content

Refactor Read_Grid_HDF5 to make use of FieldInfo#468

Open
mabruzzo wants to merge 13 commits intocholla-hydro:devfrom
mabruzzo:refactor-Read_Grid_HDF5
Open

Refactor Read_Grid_HDF5 to make use of FieldInfo#468
mabruzzo wants to merge 13 commits intocholla-hydro:devfrom
mabruzzo:refactor-Read_Grid_HDF5

Conversation

@mabruzzo
Copy link
Collaborator

This PR is a followup to #467

Quite simply, this PR refactors Read_Grid_HDF5 to make use of the newly introduced FieldInfo type.

Comment on lines +2633 to +2663
// this should work whether Cholla is configured with COOLING_GRACKLE or CHEMISTRY_GPU
// - earlier versions of this logic wouldn't work with Grackle
static void cosmo_init_chemical_species_(const Header &H, const FieldInfo &field_info, Real *host_field_ptr)
{
if (!field_info.field_id("HI_density").has_value()) {
CHOLLA_ERROR("This function has been erroneously executed. There are no chemical species");
}

Real *density = &host_field_ptr[H.n_cells * field_info.field_id("density").value()];
Real *HI_density = &host_field_ptr[H.n_cells * field_info.field_id("HI_density").value()];
Real *HII_density = &host_field_ptr[H.n_cells * field_info.field_id("HII_density").value()];
Real *HeI_density = &host_field_ptr[H.n_cells * field_info.field_id("HeI_density").value()];
Real *HeII_density = &host_field_ptr[H.n_cells * field_info.field_id("HeII_density").value()];
Real *HeIII_density = &host_field_ptr[H.n_cells * field_info.field_id("HeIII_density").value()];
Real *e_density = &host_field_ptr[H.n_cells * field_info.field_id("e_density").value()];

for (int k = 0; k < H.nz_real; k++) {
for (int j = 0; j < H.ny_real; j++) {
for (int i = 0; i < H.nx_real; i++) {
int id = (i + H.n_ghost) + (j + H.n_ghost) * H.nx + (k + H.n_ghost) * H.nx * H.ny;
int buf_id = k + j * (H.nz_real) + i * (H.nz_real) * (H.ny_real);
HI_density[id] = INITIAL_FRACTION_HI * density[id];
HII_density[id] = INITIAL_FRACTION_HII * density[id];
HeI_density[id] = INITIAL_FRACTION_HEI * density[id];
HeII_density[id] = INITIAL_FRACTION_HEII * density[id];
HeIII_density[id] = INITIAL_FRACTION_HEIII * density[id];
e_density[id] = INITIAL_FRACTION_ELECTRON * density[id];
}
}
}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following snippet is more consistent with the original style of the code that we replaced. I'm happy to revert it if that's your preference. But, be aware, I'm 99% that the old version is incompatible with Grackle.

For context, the defined(COOLING_GRACKLE) condition has been inserted for historical consistency, but I'm pretty sure the code won't compile -- Grid3D::Conserved doesn't have data members named HI_density, HII_density, ... in that mode

// this should work whether Cholla is configured with COOLING_GRACKLE or CHEMISTRY_GPU
// - earlier versions of this logic wouldn't work with Grackle
static void cosmo_init_chemical_species_(const Header &H, Grid3D::Conserved& C)
{
  #if defined(COOLING_GRACKLE) || defined(CHEMISTRY_GPU)
  for (int k = 0; k < H.nz_real; k++) {
    for (int j = 0; j < H.ny_real; j++) {
      for (int i = 0; i < H.nx_real; i++) {
        int id            = (i + H.n_ghost) + (j + H.n_ghost) * H.nx + (k + H.n_ghost) * H.nx * H.ny;
        int buf_id        = k + j * (H.nz_real) + i * (H.nz_real) * (H.ny_real);
        C.HI_density[id]    = INITIAL_FRACTION_HI * C.density[id];
        C.HII_density[id]   = INITIAL_FRACTION_HII * C.density[id];
        C.HeI_density[id]   = INITIAL_FRACTION_HEI * C.density[id];
        C.HeII_density[id]  = INITIAL_FRACTION_HEII * C.density[id];
        C.HeIII_density[id] = INITIAL_FRACTION_HEIII * C.density[id];
        C.e_density[id]     = INITIAL_FRACTION_ELECTRON * C.density[id];
      }
    }
  }
  #endif
}

@mabruzzo mabruzzo force-pushed the refactor-Read_Grid_HDF5 branch from 4606463 to 0cb5308 Compare February 4, 2026 18:30
@mabruzzo mabruzzo mentioned this pull request Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant