Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ionbox refactor #427

Merged
merged 36 commits into from
Nov 13, 2024
Merged

Ionbox refactor #427

merged 36 commits into from
Nov 13, 2024

Conversation

daviesje
Copy link
Contributor

This is a coarse modularisation of the ionised box calculation which separates the large function into a number of smaller steps. This should make future development easier. I have tested that we get the same results under a few variations of parameters, and it has not slowed down.

Key differences:

  • constants are calculated at the beginning and held in a struct which is passed into each function.
  • Radii are explicitly calculated at the beginning of the run, instead of on-the-fly in a while loop
  • the RNG for partial ionisations has been explicitly removed (it only affected the !USE_MASS_DEPENDENT_ZETA case)

Further modularisation is possible, but best left to another PR

@daviesje daviesje linked an issue Sep 16, 2024 that may be closed by this pull request
@daviesje daviesje requested a review from qyx268 November 7, 2024 17:49
Copy link

codecov bot commented Nov 11, 2024

Codecov Report

Attention: Patch coverage is 62.06897% with 11 lines in your changes missing coverage. Please review.

Project coverage is 79.51%. Comparing base (4071f4e) to head (246d89c).
Report is 38 commits behind head on v4-prep.

Files with missing lines Patch % Lines
src/py21cmfast/wrapper/structs.py 20.00% 8 Missing ⚠️
src/py21cmfast/wrapper/globals.py 33.33% 1 Missing and 1 partial ⚠️
src/py21cmfast/drivers/single_field.py 80.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           v4-prep     #427      +/-   ##
===========================================
+ Coverage    74.11%   79.51%   +5.40%     
===========================================
  Files           23       23              
  Lines         3747     3764      +17     
  Branches       641      644       +3     
===========================================
+ Hits          2777     2993     +216     
+ Misses         742      549     -193     
+ Partials       228      222       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@steven-murray steven-murray left a comment

Choose a reason for hiding this comment

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

Nice work @daviesje, this is going to make things much easier!

// This should be very rare, and even when it occurs, it will usually use a different initialiser,
// as well as be applied to different cells/halos, so I can't forsee any issues.
//Just in case I'm not using it for the initial conditions, which is okay since the slower version is only used once
void seed_rng_threads_fast(gsl_rng * rng_arr[], unsigned long long int seed){
Copy link
Member

Choose a reason for hiding this comment

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

Let's add a line that just checks if any of the seeds are the same and re-samples if so.

Comment on lines 14 to 15
//TODO: these seeding functions should probably be somewhere else
// Possibly make an rng.c/h
Copy link
Member

Choose a reason for hiding this comment

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

I agree with this

@@ -299,6 +299,46 @@ int stoc_halo_sample(struct HaloSamplingConstants *hs_constants, gsl_rng * rng,
return 0;
}

//same as stoc_halo_sample but with a mass tolerance for comparison with Ivan
int stoc_halo_sample_tol(struct HaloSamplingConstants *hs_constants, gsl_rng * rng, int *n_halo_out, float *M_out){
Copy link
Member

Choose a reason for hiding this comment

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

I think this function needs to be documented a bit to say what it does and why it's here.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

Comment on lines 358 to 375
//Making a dummy previous box which has the required fields for the first snapshot:
void setup_first_z_prevbox(IonizedBox *previous_ionize_box, PerturbedField *previous_perturb, int n_radii){
LOG_DEBUG("first redshift, do some initialization");
int i,j,k;
unsigned long long int ct;

#pragma omp parallel shared(box) private(ct) num_threads(user_params->N_THREADS)
//z_re_box is used in all cases
previous_ionize_box->z_re_box = (float *) calloc(HII_TOT_NUM_PIXELS, sizeof(float));
#pragma omp parallel shared(previous_ionize_box) private(i,j,k) num_threads(user_params_global->N_THREADS)
{
#pragma omp for
for (ct=0; ct<HII_TOT_NUM_PIXELS; ct++) {
box->z_re_box[ct] = -1.0;
#pragma omp for
for (i=0; i<user_params_global->HII_DIM; i++){
for (j=0; j<user_params_global->HII_DIM; j++){
for (k=0; k<HII_D_PARA; k++){
previous_ionize_box->z_re_box[HII_R_INDEX(i, j, k)] = -1.0;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

is this not something that's better done from the python wrapper?

Comment on lines 1174 to 1177
//TODO: moved from inside the R loop, either place verification in python wrapper or write a function to verify backend parameters
if (global_params.FIND_BUBBLE_ALGORITHM != 2 && global_params.FIND_BUBBLE_ALGORITHM != 1){ // center method
LOG_ERROR("Incorrect choice of find bubble algorithm: %i",
global_params.FIND_BUBBLE_ALGORITHM);
Copy link
Member

Choose a reason for hiding this comment

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

This should be done in the GlobalOptions validator. I think we should be writing the C functions essentially without validation, assuming all has been validated externally.

Comment on lines 1198 to 1211
//boxes which aren't guaranteed to have every element assigned to need to be initialised
//TODO: they should be zero'd in the wrapper, right?
if(flag_options->INHOMO_RECO) {
if(INIT_RECOMBINATIONS) {
init_MHR();
INIT_RECOMBINATIONS=0;
}

// remember to add the factor of VOLUME/TOT_NUM_PIXELS when converting from
// real space to k-space
// Note: we will leave off factor of VOLUME, in anticipation of the inverse FFT below
#pragma omp parallel shared(deltax_unfiltered,xe_unfiltered,N_rec_unfiltered,prev_deltax_unfiltered,\
log10_Mturnover_unfiltered,log10_Mturnover_MINI_unfiltered,stars_unfiltered) \
private(ct) num_threads(user_params->N_THREADS)
#pragma omp parallel shared(box) private(ct) num_threads(user_params->N_THREADS)
{
#pragma omp for
for (ct=0; ct<HII_KSPACE_NUM_PIXELS; ct++){
deltax_unfiltered[ct] /= (HII_TOT_NUM_PIXELS+0.0);
if(flag_options->USE_TS_FLUCT) { xe_unfiltered[ct] /= (double)HII_TOT_NUM_PIXELS; }
if (recomb_filter_flag){ N_rec_unfiltered[ct] /= (double)HII_TOT_NUM_PIXELS; }
if(flag_options->USE_HALO_FIELD) {
stars_unfiltered[ct] /= (double)HII_TOT_NUM_PIXELS;
sfr_unfiltered[ct] /= (double)HII_TOT_NUM_PIXELS;
}
if(flag_options->USE_MINI_HALOS){
prev_deltax_unfiltered[ct] /= (HII_TOT_NUM_PIXELS+0.0);
log10_Mturnover_unfiltered[ct] /= (HII_TOT_NUM_PIXELS+0.0);
log10_Mturnover_MINI_unfiltered[ct] /= (HII_TOT_NUM_PIXELS+0.0);
}
#pragma omp for
for (ct=0; ct<HII_TOT_NUM_PIXELS; ct++) {
box->Gamma12_box[ct] = 0.0;
box->MFP_box[ct] = 0.0;
Copy link
Member

Choose a reason for hiding this comment

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

Yes, this is all wrapper stuff.

Comment on lines 1249 to 1253
// perform a very rudimentary check to see if we are underresolved and not using the linear approx
if ((user_params->BOX_LEN > user_params->DIM) && !(global_params.EVOLVE_DENSITY_LINEARLY)){
LOG_WARNING("Resolution is likely too low for accurate evolved density fields\n It Is recommended \
that you either increase the resolution (DIM/BOX_LEN) or set the EVOLVE_DENSITY_LINEARLY flag to 1\n");
}
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this also should be in the wrapper.

@daviesje daviesje marked this pull request as ready for review November 12, 2024 18:02
Copy link
Member

@steven-murray steven-murray left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @daviesje !

@steven-murray
Copy link
Member

@daviesje most recent changes look good -- I didn't understand the switch from true to false but I assume it was for a good reason. The increase in the halo buffer seems perhaps a little arbitrary, though not really a problem.

The two failing tests with nomdz -- these segfault here but pass locally? Can you run with valgrind or something?

@daviesje
Copy link
Contributor Author

daviesje commented Nov 13, 2024

@daviesje most recent changes look good -- I didn't understand the switch from true to false but I assume it was for a good reason.

This was a bug introduced in the refactor where the wrong interpolation tables were being initialised (the previous redshift instead of the current).

The increase in the halo buffer seems perhaps a little arbitrary, though not really a problem.

This is entirely arbitrary, but doesn't effect the test results. I just need to make sure that it can hold all the halos that the tests produce, I thought about 500MB should be enough.

The two failing tests with nomdz -- these segfault here but pass locally? Can you run with valgrind or something?

This has just been fixed, it was an error on my part incorrectly setting interpolation table limits when USE_MASS_DEPENDENT_ZETA=False. For some reason running tests with -v made it not segfault, although the results would surely have been garbage

@daviesje daviesje merged commit d974de9 into v4-prep Nov 13, 2024
6 of 12 checks passed
@daviesje daviesje deleted the ionbox-refactor branch November 13, 2024 17:27
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.

Refactor of ComputeIonizedBox
2 participants