<commit_before>#ifdef GRAVITY

#include"../grid3D.h"
#include"../global.h"
#include "../io.h"
#include "../error_handling.h"

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

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


void Grid3D::set_dt_Gravity(){
  
  Real dt_hydro = H.dt;
  
  #ifdef PARTICLES
  Real dt_particles, dt_min;
  dt_particles = Calc_Particles_dt();
  dt_particles = fmin( dt_particles, Particles.max_dt);
  
  #ifdef ONLY_PARTICLES
  chprintf( " dt_particles: %f \n", dt_particles );
  dt_min = dt_particles;
  #else
  chprintf( " dt_hydro: %f   dt_particles: %f \n", dt_hydro, dt_particles );
  dt_min = fmin( dt_hydro, dt_particles );
  #endif//ONLY_PARTICLES
  
  H.dt = dt_min;
  Particles.dt = H.dt;
  #endif//PARTICLES
  
  // Set times for potential extrapolation
  if ( Grav.INITIAL ){
    Grav.dt_prev = H.dt;
    Grav.dt_now = H.dt;
  }else{
    Grav.dt_prev = Grav.dt_now;
    Grav.dt_now = H.dt;
  }
}


Real Grav3D::Get_Average_Density(){
  
  Real dens_sum, dens_mean;

  #ifndef PARALLEL_OMP
  dens_sum = Get_Average_Density_function( 0, nz_local );
  #else
  dens_sum = 0;
  Real dens_sum_all[N_OMP_THREADS];
  #pragma omp parallel num_threads( N_OMP_THREADS )
  {
    int omp_id, n_omp_procs;
    int g_start, g_end;

    omp_id = omp_get_thread_num();
    n_omp_procs = omp_get_num_threads();
    Get_OMP_Grid_Indxs( nz_local, n_omp_procs, omp_id, &g_start, &g_end  );
    dens_sum_all[omp_id] = Get_Average_Density_function(  g_start, g_end );

  }
  for ( int i=0; i<N_OMP_THREADS; i++ ){
    dens_sum += dens_sum_all[i];
  }
  #endif

  dens_mean = dens_sum /  ( nx_local * ny_local * nz_local);

  Real dens_avrg_all;
  #ifdef MPI_CHOLLA
  dens_avrg_all = ReduceRealAvg( dens_mean );
  #else
  dens_avrg_all = dens_mean
  #endif

  dens_avrg = dens_avrg_all;
  
  return dens_avrg_all;

}

Real Grav3D::Get_Average_Density_function( int g_start, int g_end){
  
  int nx = nx_local;
  int ny = ny_local;
  int nz = nz_local;
  int k, j, i, id;
  Real dens_sum=0;
  for( k=g_start; k<g_end; k++){
    for( j=0; j<ny; j++){
      for( i=0; i<nx; i++){
        id = (i) + (j)*nx + (k)*nx*ny;
        dens_sum += F.density_h[id];
      }
    }
  }
  return dens_sum;
}
  




void Grid3D::Initialize_Gravity( struct parameters *P ){
  chprintf( "\nInitializing Gravity... \n");
  Grav.Initialize( H.xblocal, H.yblocal, H.zblocal, H.xdglobal, H.ydglobal, H.zdglobal, P->nx, P->ny, P->nz, H.nx_real, H.ny_real, H.nz_real, H.dx, H.dy, H.dz, H.n_ghost_potential_offset  );
  chprintf( "Gravity Successfully Initialized. \n\n");
  
  //initialize cuda for use with mpi
  // Needed to initialize cuda after gravity in order to work on Summit
  #ifdef CUDA
  chprintf( "Initializing CUDA...\n");
  if(initialize_cuda_mpi(procID_node,nproc_node))
  {
    chprintf("Error initializing cuda with mpi.\n");
    chexit(-10);
  }
  #endif /*CUDA*/
  
  #ifdef ONLY_PARTICLES
  chprintf("ONLY_PARTICLES: Initializing without CUDA support.\n");
  #else
  chprintf( "CUDA Successfully Initialized.\n");
  #endif
}

void Grid3D::Compute_Gravitational_Potential( struct parameters *P ){
  
  Real Grav_Constant = 1;
  
  Real dens_avrg, current_a;
  
  dens_avrg = 0;
  current_a = 1;
  
  #ifdef COSMOLOGY
  Grav_Constant = Cosmo.cosmo_G;
  current_a = Cosmo.current_a;
  dens_avrg = Grav.Get_Average_Density();
  chprintf( " Density Average:  %f\n", dens_avrg);
  
  #endif
  
  
  #ifdef PARTICLES
  Copy_Particles_Density_to_Gravity( *P);
  #endif
  
  
  #ifdef CPU_TIME
  Timer.Start_Timer();
  #endif
  
  #ifndef ONLY_PARTICLES
  Copy_Hydro_Density_to_Gravity();
  #endif
  <commit_msg>get density average after copying particles and hydro density to gravity density array<commit_after>#ifdef GRAVITY

#include"../grid3D.h"
#include"../global.h"
#include "../io.h"
#include "../error_handling.h"

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

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


void Grid3D::set_dt_Gravity(){
  
  Real dt_hydro = H.dt;
  
  #ifdef PARTICLES
  Real dt_particles, dt_min;
  dt_particles = Calc_Particles_dt();
  dt_particles = fmin( dt_particles, Particles.max_dt);
  
  #ifdef ONLY_PARTICLES
  chprintf( " dt_particles: %f \n", dt_particles );
  dt_min = dt_particles;
  #else
  chprintf( " dt_hydro: %f   dt_particles: %f \n", dt_hydro, dt_particles );
  dt_min = fmin( dt_hydro, dt_particles );
  #endif//ONLY_PARTICLES
  
  H.dt = dt_min;
  Particles.dt = H.dt;
  #endif//PARTICLES
  
  // Set times for potential extrapolation
  if ( Grav.INITIAL ){
    Grav.dt_prev = H.dt;
    Grav.dt_now = H.dt;
  }else{
    Grav.dt_prev = Grav.dt_now;
    Grav.dt_now = H.dt;
  }
}


Real Grav3D::Get_Average_Density(){
  
  Real dens_sum, dens_mean;

  #ifndef PARALLEL_OMP
  dens_sum = Get_Average_Density_function( 0, nz_local );
  #else
  dens_sum = 0;
  Real dens_sum_all[N_OMP_THREADS];
  #pragma omp parallel num_threads( N_OMP_THREADS )
  {
    int omp_id, n_omp_procs;
    int g_start, g_end;

    omp_id = omp_get_thread_num();
    n_omp_procs = omp_get_num_threads();
    Get_OMP_Grid_Indxs( nz_local, n_omp_procs, omp_id, &g_start, &g_end  );
    dens_sum_all[omp_id] = Get_Average_Density_function(  g_start, g_end );

  }
  for ( int i=0; i<N_OMP_THREADS; i++ ){
    dens_sum += dens_sum_all[i];
  }
  #endif

  dens_mean = dens_sum /  ( nx_local * ny_local * nz_local);

  Real dens_avrg_all;
  #ifdef MPI_CHOLLA
  dens_avrg_all = ReduceRealAvg( dens_mean );
  #else
  dens_avrg_all = dens_mean
  #endif

  dens_avrg = dens_avrg_all;
  
  return dens_avrg_all;

}

Real Grav3D::Get_Average_Density_function( int g_start, int g_end){
  
  int nx = nx_local;
  int ny = ny_local;
  int nz = nz_local;
  int k, j, i, id;
  Real dens_sum=0;
  for( k=g_start; k<g_end; k++){
    for( j=0; j<ny; j++){
      for( i=0; i<nx; i++){
        id = (i) + (j)*nx + (k)*nx*ny;
        dens_sum += F.density_h[id];
      }
    }
  }
  return dens_sum;
}
  




void Grid3D::Initialize_Gravity( struct parameters *P ){
  chprintf( "\nInitializing Gravity... \n");
  Grav.Initialize( H.xblocal, H.yblocal, H.zblocal, H.xdglobal, H.ydglobal, H.zdglobal, P->nx, P->ny, P->nz, H.nx_real, H.ny_real, H.nz_real, H.dx, H.dy, H.dz, H.n_ghost_potential_offset  );
  chprintf( "Gravity Successfully Initialized. \n\n");
  
  //initialize cuda for use with mpi
  // Needed to initialize cuda after gravity in order to work on Summit
  #ifdef CUDA
  chprintf( "Initializing CUDA...\n");
  if(initialize_cuda_mpi(procID_node,nproc_node))
  {
    chprintf("Error initializing cuda with mpi.\n");
    chexit(-10);
  }
  #endif /*CUDA*/
  
  #ifdef ONLY_PARTICLES
  chprintf("ONLY_PARTICLES: Initializing without CUDA support.\n");
  #else
  chprintf( "CUDA Successfully Initialized.\n");
  #endif
}

void Grid3D::Compute_Gravitational_Potential( struct parameters *P ){
  
  Real Grav_Constant = 1;
  
  Real dens_avrg, current_a;
  
  dens_avrg = 0;
  current_a = 1;
  
  
  #ifdef PARTICLES
  Copy_Particles_Density_to_Gravity( *P);
  #endif
  
  
  #ifdef CPU_TIME
  Timer.Start_Timer();
  #endif
  
  #ifndef ONLY_PARTICLES
  Copy_Hydro_Density_to_Gravity();
  #endif

  #ifdef COSMOLOGY
  Grav_Constant = Cosmo.cosmo_G;
  current_a = Cosmo.current_a;
  dens_avrg = Grav.Get_Average_Density();
  chprintf( " Density Average:  %f\n", dens_avrg);
  #endif
  
{'fraction_non_alphanumeric': 0.127799016930639, 'fraction_numerical': 0.004642271982523211, 'mean_word_length': 3.455596107055961, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '4312379', 'n_tokens_mistral': 3167, 'n_tokens_neox': 3052, 'n_words': 624}