Skip to content

Cosmo ics - #522

Merged
evaneschneider merged 441 commits into
devfrom
cosmo-ics
Aug 19, 2026
Merged

Cosmo ics#522
evaneschneider merged 441 commits into
devfrom
cosmo-ics

Conversation

@brantr

@brantr brantr commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

This update provides several additions to the cholla code base:

  • Cosmological initial conditions generation. Given an input power spectrum, the initial dark matter particle positions and velocities and gas cell densities and velocities are generated. Currently uses the Zel'dovich approximation but can be extended to higher order perturbation theory.
  • Adds infrastructure for creating parallel random numbers (used in the cosmo ICs)
  • Adds infrastructure for using Henry as an FFT platform separate from Paris
  • Adds infrastructure for ghost cell communications for individual fields separate from the bulk Cholla communication routines.
  • Adds differential equation solver for computing the cosmological growth function (including for w0-wa cosmologies)
  • Ports in the OTVET radiative transfer scheme from the RT branch and has a developmental version of M1 RT.
  • Addresses bugs in the MPI_GPU communication for particles when communication buffers need to be resized.

brantr and others added 30 commits May 27, 2026 16:23
The power spectra provided are now going to be the total matter power spectrum at the redshift of interest and the power spectrum of the baryon-cdm overdensity (redshift-independent in linear theory). This approach is adopted from Hahn et al., MNRAS 503, 426–445 (2021).
Note that when generating the cosmological ICs we need the cosmological parameters before the particles or gravity have been initialized.  We may need to split the cosmological initialization from the gravity and particle initialization.

In the routine to write the growth function, the growth function file header may need to set the cosmological parameters explicitly from the parameter class rather than inherit from Cosmology(), since they may not be set at that stage.
At this point, all the basic calculations required for the cosmological ICs have been computed.

Given an input power spectrum from CAMB, we generate the total matter density fluctuations and the fluctuations in the baryon vs. CDM densities. We compute the initial gravitational potential from the matter overdensity field. We use the gradient of that potential times the time derivative of the growth factor to compute the gas velocities on the grid, and use the matter and baryon v. cdm overdensities to set the gas density on the grid. We then compute the potential of the baryon v. cdm overdensities. The particles are then initialized at the Lagrangian positions at the grid centers. The particle velocities are set by the time derivative of the growth factor times the gradient of the initial matter potential, just like the gas. The particle positions are perturbed by the growth factor times the gradient of the matter potential, and then further by (just) the gradient of the baryon v. cdm  potential.

Things that need to be checked:

1) units, especially on the time derivative of the growth factor. compare displacements from grid centers and velocities to a comparable set of ICs from another generator.
2) the initial hydrogen and helium ionization states needs to be inherited from CAMB correctly, not set via hard-coding as is now done for some reason
3) the matter power spectrum and the baryon v. cdm power spectrum should probably be checked.
4) test simulations need to be run to z=0
5) scaling tests to check the ICs generator performance with scale.
The fact that the local density field is only real cells vs. the potential being real + ghost in paris complicates using simple copy and paste.  The indexing of just about everything needs to be adjusted. Likely the easiest route is to keep all the FFT routines connected to only real cells, copy in the potential solved from the density field via FFT into another array that included ghost cells, perform the required communication between ghost cells, and then proceed with the potential gradient calculation.  This will require communication of the boundary values for the potentials corresponding to delta_m and delta_bc.

How do we deal with populating phi when it's a different size than delta?  We can perform the FFT treating them as the same size (ignoring the extra extent of phi).  We can then copy from phi back to delta m.  Then reindex phi, reading from delta m.  We can then copy the boundary values after phi is remapped.

This is a bit exhausting.  It could all require revision.
We now ensure that all FFT fields are local in size, including the delta_m and delta_bc device and host fields. The potential fields require gradients and are now the full grid size. We avoid buffer transfers between the FFT-computed potential and the full grid potential by re-using the delta fields.  The particle initialization now uses the gradients of the cosmological potential fields.  We still need to address the transfer of the ghost cells for the cosmological potential, but the code should execute (incorrectly) through the particle initialization. We can debug memory issues, and then turn to the ghost cell communications.
Unless I invoke P.nx/ny/nz in main(), then the gravitational calculation initialization fails because the number of grid cells propagated to Henry is garbage.  Maybe P is changed somewhere haphazardly?

This is not my problem for now, focus on communication of boundary cells from the potential.
Note these are written for CPU only, but allow for a field to be provided as input and can be re-used with other fields beyond the cosmo potential.

Re-writing for GPU+MPI should be straightforward, but will wait.
brantr and others added 11 commits July 29, 2026 16:29
Co-authored-by: Matthew Abruzzo <matthewabruzzo@gmail.com>
Co-authored-by: Matthew Abruzzo <matthewabruzzo@gmail.com>
Co-authored-by: Matthew Abruzzo <matthewabruzzo@gmail.com>
Co-authored-by: Matthew Abruzzo <matthewabruzzo@gmail.com>
Co-authored-by: Matthew Abruzzo <matthewabruzzo@gmail.com>
Co-authored-by: Matthew Abruzzo <matthewabruzzo@gmail.com>
Co-authored-by: Matthew Abruzzo <matthewabruzzo@gmail.com>
Co-authored-by: Matthew Abruzzo <matthewabruzzo@gmail.com>
Co-authored-by: Matthew Abruzzo <matthewabruzzo@gmail.com>
Co-authored-by: Matthew Abruzzo <matthewabruzzo@gmail.com>
@brantr

brantr commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator Author

pre-commit.ci autofix

@brantr

brantr commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator Author

So, I think I finished skimming through most everything (I haven't carefully traced out all of the detailed logic -- but nothing egregious jumps out to me). In all of my new comments/suggested changes, I tried to emphasize what I thought was particularly important. As per usual, feel free to push back on comments you disagree with.

I haven't looked at the Altair stuff or the RT example plotting scripts, but I'm not really planning to look at that.

Thank you for your efforts.

I'm not thrilled about all of the stuff in the examples/3D/illev directory. I am most irked by the images that are saved there, and we should probably consider a better place for storing the plotting scripts. But, given the experimental nature of RT (and the fact that quite a bit of work is probably required to rearrange the files while ensuring that they continue to work), I'm will to let this go.

We can move these items, but they are extremely helpful for diagnosing performance. I would recommend pushing this issue to the M1 merge.

I would appreciate it if you would at least consider a suggestion for changing the architecture of the code that sets the Cosmo ICs.

I think this is a design consideration that would require some effort. I will not have time for this until at least September.

To be clear, I think I'll be ready to approve the PR once you address the new comments (regardless of whether or not you actually adopt my suggested alternative Cosmo IC Architecture).

Thank you for your efforts during review and your support, it's been extremely helpful.

Alternative Cosmo IC Architecture

At a high-level, the suggestion is quite simple: what if we created a new class that was responsible for all of the cosmology initialization logic?

For the sake of this discussion, let's call this class CosmoICMaker. In more detail, we would move all of the Cosmo IC methods (other than Grid3D::Cosmological_ICs) and all of the associated data members (CP, rng_states, fft) into the new CosmoICMaker class.

I would disagree that rng_states and fft should be part of this isolated structure. These could be generally useful.

There's obviously already a lot of precedence for doing something like this in the codebase. For example, Particles3D is responsible for managing the deposited density field, Grav3D is responsible for managing the potential field, and Rad3D is responsible for managing all the RT fields. I'm frankly not a fan of this kind of distributed ownership of fields.1 However, I think the ephemeral nature of the Cosmo IC fields (i.e. we explicitly allocate and deallocate them before running the entire simulation) makes a very compelling case for treating them distinctly from all other fields in the codebase.

I agree that the ephemeral nature of the fields make them amenable to special treatment.

I think this change is a good idea for 2 related (and potentially obvious) reasons:

  1. The Grid3D object is unfortunately a "god object," which is universally recognized as an anti-pattern.2 As we add more and more functions and data members, we start to introduce lots of the issues associated with using global variables. In particular, it becomes harder and harder to reason about any one small section of the codebase in isolation. Just the sheer number of methods is problematic.3 Thus, we should be trying to delegate as much logic as possible to helper classes to both (i) make it easier to reason about that new logic and (ii) avoid making the problem even worse. If we don't factor out this logic, we would be adding ~20 new methods.

I agree that we could work on this in the future.

  1. Less importantly, I'm not a fan of a class having lots of initialization methods. It essentially means that class has more and more valid partially initialized states that we need to reason about. If we create a CosmoICMaker class, then from the perspective of the Grid3D class, I think we only need to worry about "is the CosmoICMaker instance valid or not?" The precise details of what it means to be in a valid state would then be handled by the CosmoICMaker class (largely in isolation of the rest of Grid3D's state).

I agree.

Beyond the up front inconvenience, I don't actually see a compelling reason not move this logic into a new CosmoICMaker class. Since you have already written a bunch of new boundary update logic, I don't think this would even be that much of the inconvenience.4 While I can think of some potential performance benefits to the current design, they all seem pretty negligible relative to the cost of actually driving the Cosmic IC logic. Am I missing something?

No, I don't disagree.

I actually feel VERY strongly about this topic. In fact, if this were a normal PR, I would probably insist that this change (or something like it) be made before the PR could be accepted5 or to be convinced that the current design makes more sense. I honestly wouldn't care that much if this logic weren't in such a central data structure of the codebase.

I think we are just person-hour limited at this time. I'm open to revisiting this in September.

However, the reality of the situation is that we really want to merge in all the other changes in the PR, so I'll let this go if you don't want to do it.

I do want to do it, we just have time constraints over the next four weeks on the cosmo ICs + RT issues that will require concerned development effort on the physics.

I am happy to meet next week to discuss.

Comment thread docs/sphinx/Physics/Cosmology.md Outdated

@evaneschneider evaneschneider left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm happy to merge this in once the new Makefile parameters (the ones added in src/cholla_config.h.in) and input file parameters have been added and defined on the relevant docs pages https://cholla.readthedocs.io/en/latest/MakefileParameters.html and https://cholla.readthedocs.io/en/latest/Reference/Parameters.html. Thank you for the documentation on the ICs generation.

Comment thread src/rk/rk4.cpp Outdated
error.resize(ny);

int i, j;
for (i = 0; i < 7; i++)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is now triggering the clang-tidy error "statement should be inside braces"

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.

Fixed, thank you.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This line (22) is still triggering the clang tidy error. I think you need braces on line 22 (and probably 23 as well).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It looks like maybe there's an accidental duplication here as well?

Comment thread src/rk/rk4.cpp Outdated
brantr and others added 4 commits August 13, 2026 14:13
Typo in cosmology ics documentation

Co-authored-by: Evan Schneider <eschneider@pitt.edu>
move statement inside braces

Co-authored-by: Matthew Abruzzo <matthewabruzzo@gmail.com>
@brantr

brantr commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator Author

@evaneschneider I have added to the MakefileParameters.md and Parameters.md files. I have tried to briefly document all the existing un-documented makefile parameters (including old ones) and added descriptions of all the new parameters for the cosmological simulations.

@evaneschneider

Copy link
Copy Markdown
Collaborator

Looks good, thank you. I'll merge this in once the CI tests finish.

Comment thread src/rk/rk4.cpp Outdated
Co-authored-by: Matthew Abruzzo <matthewabruzzo@gmail.com>
@evaneschneider
evaneschneider merged commit f691f1d into dev Aug 19, 2026
15 of 16 checks passed
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.

3 participants