Skip to content

[newchem-cpp] Consistency tweaks: GrainSpeciesCollection and cool_multi_time_g#332

Merged
brittonsmith merged 11 commits into
grackle-project:newchem-cppfrom
mabruzzo:gen2024transcribe/consistency-tweaks
Aug 13, 2025
Merged

[newchem-cpp] Consistency tweaks: GrainSpeciesCollection and cool_multi_time_g#332
brittonsmith merged 11 commits into
grackle-project:newchem-cppfrom
mabruzzo:gen2024transcribe/consistency-tweaks

Conversation

@mabruzzo

@mabruzzo mabruzzo commented May 18, 2025

Copy link
Copy Markdown
Collaborator

This PR was originally proposed as brittonsmith#25


To be reviewed after #331 is merged (i.e. the PR where solve_rate_cool_g was transcribed)


This PR essentially goes back and makes some tweaks to the internal GrainSpeciesCollection helper-type and the C++ version of the cool_multi_time_g function, both introduced in PR #275, to achieve consistency with some of choices made in #3311

This is actually a fairly small PR. The changes include:

  1. Switching the GrainSpeciesCollection to be more consistent with the similar, but more generic SpeciesCollection type. For concreteness:
    • when I created the SpeciesCollection type, I designed it to use enumerators defined within the SpLUT pseudo-namespace as a compile-time lookup table (mapping species names to an index)
    • this PR's 1st commit refactors GrainSpeciesCollection so that it uses the the enumerators within the newly-defined OnlyGrainSpLUT pseudo-namespace as a lookup-table.2
  2. Various superficial tweaks to the cool_multi_time_g function:
    • it now uses the InternalGrUnits type instead of manually tracking the utem, urho & uxyz floating point values and an instance of the code_units struct (this also removes some boiler-plate from local_calculate_temperature)
    • modified the j,k loop to use grackle_index_helper and IndexRange (the latter helps removes some ambiguities about 0-based and 1-based indexing when calling routines that are not yet transcribed)
    • thanks to the previous two changes in the last 2 bullets, I was able to replace all FORTRAN_NAME({subroutine}) occurrences with the nicer, C++ wrapper functions. Recall that these wrappers take shorter argument lists, mostly composed of structs, and internally forward the struct-members on to the underlying routine. This is important for the future process of transcribing a routine like cool1d_multi_g, which is called from multiple other transcribed routines.3
    • I also made some superficial tweaks to the 2 for-loops within cool_multi_time_g. The variables holding variable values now only hold the 0-based index values (rather than 1-based index values that we adjust every time that they are used). This was not strictly essential to this PR
    • we now pass 2 arguments by value instead of by pointer (for consistency with solve_rate_cool_g). This was not strictly essential to this PR

Footnotes

  1. To put it another way, PR [newchem-cpp] initial transcription of cool_multi_time_g #275 was my first attempt at transcribing a top-level function. PR [newchem-cpp] transcribe: solve_rate_cool_g to C++ #331 was my second attempt at doing so. I learned a lot while working on [newchem-cpp] transcribe: solve_rate_cool_g to C++ #331 and established some conventions that improved quality and made transcription of certain aspects of the code a lot easier. Now I'm going back and applying this experience to improve the quality of the code written as part of my first transcription attempt.

  2. In the future, this change is going to significantly simplify the C++ versions of calc_all_tdust_gasgr_1d_g and calc_grain_size_increment_1d. Currently these routines have multiple instances where the same exact logic is repeated 12 times (once per dust grain species). This change should help us replace instances of this code-duplication with for-loops.

  3. Essentially, we want there to be just 1 location in the entire codebase where we directly reference any given Fortran routine before we transcribe that routine. (there are a couple of reasons for this that I could go into if you want)

This was done to be more consistent with the general-purpose
`SpeciesCollection` type. For concreteness:
- `SpeciesCollection` has always used the enumerators defined within
  `SpLUT` as a compile-time lookup table indices
- this commit refactors `GrainSpeciesCollection` so that it uses the
  the enumerators within the newly-defined `OnlyGrainSpLUT` entity
  as a compile-time lookup table

In the future, this change is going to help simplify the C++ versions
of `calc_all_tdust_gasgr_1d_g` and `calc_grain_size_increment_1d`.
Currently these routines have multiple instances where the same exact
logic is repeated 12 times (once per dust grain species). This change
should help us remove this code-duplication and replace it with a
for-loop.
Comment on lines +90 to +92
for (int i = idx_range.i_start; i < idx_range.i_stop; i++) {
itmask[i] = MASK_TRUE;
}

@mabruzzo mabruzzo Jun 12, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Here is an example of "cleaned up indexing" (for the discussion in #275)

Comment on lines +110 to +114
for (int i = idx_range.i_start; i < idx_range.i_stop; i++) {
double energy = std::fmax(p2d[i]/(my_chemistry->Gamma-1.),
tiny_fortran_val);
cooltime(i,j,k) = (gr_float)(energy/edot[i]);
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Here is another example of "cleaned up indexing" (for the discussion in #275)

@brittonsmith brittonsmith merged commit 5c74d1f into grackle-project:newchem-cpp Aug 13, 2025
5 checks passed
@github-project-automation github-project-automation Bot moved this from Awaiting Review to Done in New Chemistry and C++ Transcription Aug 13, 2025
@mabruzzo mabruzzo deleted the gen2024transcribe/consistency-tweaks branch August 13, 2025 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor internal reorganization or code simplification with no behavior changes

Projects

Development

Successfully merging this pull request may close these issues.

2 participants