<commit_before>#ifdef GRAVITY

#include"../grid3D.h"
#include"../global.h"
#include "../io.h"
#include "../error_handling.h"
#include <cstring>

#ifdef CUDA
#include "../cuda_mpi_routines.h"
#endif

#ifdef PARALLEL_OMP
#include"../parallel_omp.h"
#endif

#ifdef PARIS
#include <vector>
#endif

#ifdef PARTICLES
#include "../model/disk_galaxy.h"
#endif

//Set delta_t when using gravity
void Grid3D::set_dt_Gravity(){
  
  //Delta_t for the hydro
  Real dt_hydro = H.dt;
  
  #ifdef AVERAGE_SLOW_CELLS
  Real min_dt_slow;
  #endif
  
  #ifdef PARTICLES
  //Compute delta_t for particles and choose min(dt_particles, dt_hydro)
  Real dt_particles, dt_min;
  
  #ifdef COSMOLOGY
  chprintf( "Current_z: %f \n", Cosmo.current_z );
  Real da_particles, da_min, dt_physical;
  
  //Compute the particles delta_t
  Particles.dt = Calc_Particles_dt_Cosmo();
  dt_particles = Particles.dt;
  //Convert delta_t to delta_a ( a = scale factor )
  da_particles = Cosmo.Get_da_from_dt( dt_particles );
  da_particles = fmin( da_particles, 1.0 ); //Limit delta_a
  
  #ifdef ONLY_PARTICLES
  //If only particles da_min is only da_particles
  da_min = da_particles;
  chprintf( " Delta_a_particles: %f \n", da_particles );

  #else //NOT ONLY_PARTICLES
  //Here da_min is the minumum between da_particles and da_hydro
  Real da_hydro;
  da_hydro = Cosmo.Get_da_from_dt( dt_hydro ) * Cosmo.current_a * Cosmo.current_a / Cosmo.H0; //Convet delta_t to delta_a
  da_min = fmin( da_hydro, da_particles ); //Find the minumum delta_a
  chprintf( " Delta_a_particles: %f      Delta_a_gas: %f   \n", da_particles, da_hydro );
  
  #endif//ONLY_PARTICLES
  
  //Limit delta_a by the expansion rate
  Cosmo.max_delta_a = fmin( MAX_EXPANSION_RATE * Cosmo.current_a, MAX_DELTA_A );
  if( da_min > Cosmo.max_delta_a){
    da_min = Cosmo.max_delta_a;
    chprintf( " Seting max delta_a: %f\n", da_min );
  }
  
  //Small delta_a when reionization starts
  #ifdef COOLING_GRACKLE
  if ( fabs(Cosmo.current_a + da_min - Cool.scale_factor_UVB_on) < 0.005 ){
    da_min /= 2;
    chprintf( " Starting UVB. Limiting delta_a:  %f \n", da_min);
  }
  #endif
  
  //Limit delta_a if it's time to output
  if ( (Cosmo.current_a + da_min) >  Cosmo.next_output ){
    da_min = Cosmo.next_output - Cosmo.current_a;
    H.Output_Now = true;
  }
  
  #ifdef ANALYSIS
  //Limit delta_a if it's time to run analisys
  if( Analysis.next_output_indx < Analysis.n_outputs ){
    if ( (Cosmo.current_a + da_min) >=  Analysis.next_output ){
      if ( !H.Output_Now ) da_min = Analysis.next_output - Cosmo.current_a;
      Analysis.Output_Now = true;
    }
  }
  #endif
  
  
  //Set delta_a after it has been computed
  Cosmo.delta_a = da_min;
  //Convert delta_a back to delta_t
  dt_min = Cosmo.Get_dt_from_da( Cosmo.delta_a ) * Cosmo.H0 / ( Cosmo.current_a * Cosmo.current_a );
  //Set the new delta_t for the hydro step
  H.dt = dt_min; 
  chprintf( " Current_a: %f    delta_a: %f     dt:  %f\n", Cosmo.current_a, Cosmo.delta_a, H.dt  );
  
  #ifdef AVERAGE_SLOW_CELLS
  //Set the min_delta_t for averaging a slow cell
  min_dt_slow = Particles.dt / Particles.C_cfl * Cosmo.H0 / ( Cosmo.current_a * Cosmo.current_a ) / SLOW_FACTOR;
  H.min_dt_slow = min_dt_slow;
  #endif 
  
  //Compute the physical time
  dt_physical = Cosmo.Get_dt_from_da( Cosmo.delta_a );
  Cosmo.dt_secs = dt_physical * Cosmo.time_conversion;
  Cosmo.t_secs += Cosmo.dt_secs;
  chprintf( " t_physical: %f Myr   dt_physical: %f Myr\n", Cosmo.t_secs/MYR, Cosmo.dt_secs/MYR );  
  Particles.dt = dt_physical; 
  
  #else // Not Cosmology
  //If NOT using COSMOLOGY
  
  //Compute the particles delta_t
  dt_particles = Calc_Particles_dt();
  dt_particles = fmin( dt_particles, Particles.max_dt);<commit_msg>print error and exit if da_min < 0<commit_after>#ifdef GRAVITY

#include"../grid3D.h"
#include"../global.h"
#include "../io.h"
#include "../error_handling.h"
#include <cstring>

#ifdef CUDA
#include "../cuda_mpi_routines.h"
#endif

#ifdef PARALLEL_OMP
#include"../parallel_omp.h"
#endif

#ifdef PARIS
#include <vector>
#endif

#ifdef PARTICLES
#include "../model/disk_galaxy.h"
#endif

//Set delta_t when using gravity
void Grid3D::set_dt_Gravity(){
  
  //Delta_t for the hydro
  Real dt_hydro = H.dt;
  
  #ifdef AVERAGE_SLOW_CELLS
  Real min_dt_slow;
  #endif
  
  #ifdef PARTICLES
  //Compute delta_t for particles and choose min(dt_particles, dt_hydro)
  Real dt_particles, dt_min;
  
  #ifdef COSMOLOGY
  chprintf( "Current_z: %f \n", Cosmo.current_z );
  Real da_particles, da_min, dt_physical;
  
  //Compute the particles delta_t
  Particles.dt = Calc_Particles_dt_Cosmo();
  dt_particles = Particles.dt;
  //Convert delta_t to delta_a ( a = scale factor )
  da_particles = Cosmo.Get_da_from_dt( dt_particles );
  da_particles = fmin( da_particles, 1.0 ); //Limit delta_a
  
  #ifdef ONLY_PARTICLES
  //If only particles da_min is only da_particles
  da_min = da_particles;
  chprintf( " Delta_a_particles: %f \n", da_particles );

  #else //NOT ONLY_PARTICLES
  //Here da_min is the minumum between da_particles and da_hydro
  Real da_hydro;
  da_hydro = Cosmo.Get_da_from_dt( dt_hydro ) * Cosmo.current_a * Cosmo.current_a / Cosmo.H0; //Convet delta_t to delta_a
  da_min = fmin( da_hydro, da_particles ); //Find the minumum delta_a
  chprintf( " Delta_a_particles: %f      Delta_a_gas: %f   \n", da_particles, da_hydro );
  
  #endif//ONLY_PARTICLES
  
  //Limit delta_a by the expansion rate
  Cosmo.max_delta_a = fmin( MAX_EXPANSION_RATE * Cosmo.current_a, MAX_DELTA_A );
  if( da_min > Cosmo.max_delta_a){
    da_min = Cosmo.max_delta_a;
    chprintf( " Seting max delta_a: %f\n", da_min );
  }
  
  //Small delta_a when reionization starts
  #ifdef COOLING_GRACKLE
  if ( fabs(Cosmo.current_a + da_min - Cool.scale_factor_UVB_on) < 0.005 ){
    da_min /= 2;
    chprintf( " Starting UVB. Limiting delta_a:  %f \n", da_min);
  }
  #endif
  
  //Limit delta_a if it's time to output
  if ( (Cosmo.current_a + da_min) >  Cosmo.next_output ){
    da_min = Cosmo.next_output - Cosmo.current_a;
    H.Output_Now = true;
  }
  
  #ifdef ANALYSIS
  //Limit delta_a if it's time to run analisys
  if( Analysis.next_output_indx < Analysis.n_outputs ){
    if ( (Cosmo.current_a + da_min) >=  Analysis.next_output ){
      if ( !H.Output_Now ) da_min = Analysis.next_output - Cosmo.current_a;
      Analysis.Output_Now = true;
    }
  }
  #endif
  
  if ( da_min < 0 ){
    chprintf( 'ERROR: Negative delta_a');
    exit(-1);
  } 
  
  
  //Set delta_a after it has been computed
  Cosmo.delta_a = da_min;
  //Convert delta_a back to delta_t
  dt_min = Cosmo.Get_dt_from_da( Cosmo.delta_a ) * Cosmo.H0 / ( Cosmo.current_a * Cosmo.current_a );
  //Set the new delta_t for the hydro step
  H.dt = dt_min; 
  chprintf( " Current_a: %f    delta_a: %f     dt:  %f\n", Cosmo.current_a, Cosmo.delta_a, H.dt  );
  
  #ifdef AVERAGE_SLOW_CELLS
  //Set the min_delta_t for averaging a slow cell
  min_dt_slow = Particles.dt / Particles.C_cfl * Cosmo.H0 / ( Cosmo.current_a * Cosmo.current_a ) / SLOW_FACTOR;
  H.min_dt_slow = min_dt_slow;
  #endif 
  
  //Compute the physical time
  dt_physical = Cosmo.Get_dt_from_da( Cosmo.delta_a );
  Cosmo.dt_secs = dt_physical * Cosmo.time_conversion;
  Cosmo.t_secs += Cosmo.dt_secs;
  chprintf( " t_physical: %f Myr   dt_physical: %f Myr\n", Cosmo.t_secs/MYR, Cosmo.dt_secs/MYR );  
  Particles.dt = dt_physical; 
  
  #else // Not Cosmology
  //If NOT using COSMOLOGY
  
  //Compute the particles delta_t
  dt_particles = Calc_Particles_dt();
  dt_particles = fmin( dt_particles, Particles.max_dt);
{'fraction_non_alphanumeric': 0.11313772138514407, 'fraction_numerical': 0.0035685963521015066, 'mean_word_length': 3.381586566299942, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7568662', 'n_tokens_mistral': 3130, 'n_tokens_neox': 3039, 'n_words': 691}