<commit_before>#include "domain.h"

// Domain initialization
void domain_time_grid_init( Domain *dom, Config *conf );
void domain_spatial_mesh_init( Domain *dom, Config *conf );
void domain_particles_init( Domain *dom, Config *conf );
// Pic algorithm
void eval_charge_density( Domain *dom );
void eval_potential_and_fields( Domain *dom );
void push_particles( Domain *dom );
void apply_domain_constrains( Domain *dom );
void update_time_grid( Domain *dom );
// Eval charge density on grid
void weight_particles_charge_to_mesh( Domain *dom );
void next_node_num_and_weight( const double x, const double grid_step, int *next_node, double *weight );
// Eval fields from charges
void solve_poisson_eqn( Domain *dom );
extern void hwscrt_( double *, double *, int *, int *, double *, double *,
		    double *, double *, int *, int *, double *, double *,
		    double *, double *, int *, double *, int *, double * );
void rowmajor_to_colmajor( double **c, double *fortran, int dim1, int dim2 );
void colmajor_to_rowmajor( double *fortran, double **c, int dim1, int dim2 );		  
void hwscrt_init_f( double left, double top, 
		    double right, double bottom, 
		    double **charge_density, double **hwscrt_f);
double **poisson_init_rhs( Spatial_mesh *spm );
void poisson_free_rhs( double **rhs, int nrow, int ncol );
void eval_fields_from_potential( Domain *dom );
double boundary_difference( double phi1, double phi2, double dx );
double central_difference( double phi1, double phi2, double dx );
// Push particles
void leap_frog( Domain *dom );
void update_momentum( Domain *dom );
void update_position( Domain *dom );
// Boundaries and generation
void apply_domain_boundary_conditions( Domain *dom );
bool out_of_bound( Domain *dom, Vec2d r );
void remove_particle( int *i, Domain *dom );
void proceed_to_next_particle( int *i, Domain *dom );
// Domain print
char *construct_output_filename( const char *output_filename_prefix, 
				 const int current_time_step,
				 const char *output_filename_suffix );
// Various functions
void domain_print_particles( Domain *dom );





//
// Domain initialization
//

void domain_prepare( Domain *dom, Config *conf )
{
    config_check_correctness( conf );
    domain_time_grid_init( dom, conf );    
    domain_spatial_mesh_init( dom, conf );
    domain_particles_init( dom, conf );
    return;
}

void domain_time_grid_init( Domain *dom, Config *conf )
{
    double total_time = conf->total_time;
    double step_size = conf->time_step_size;
    dom->time_grid = time_grid_init( total_time, step_size );
    return;
}

void domain_spatial_mesh_init( Domain *dom, Config *conf )
{
    double x_size = conf->grid_x_size;
    double x_step = conf->grid_x_step;
    double y_size = conf->grid_y_size;
    double y_step = conf->grid_y_step;
    double phi_left = conf->boundary_phi_left;
    double phi_right = conf->boundary_phi_right;
    double phi_top = conf->boundary_phi_top;
    double phi_bottom = conf->boundary_phi_bottom;
    dom->spat_mesh = spatial_mesh_init( x_size, x_step, y_size, y_step );
    spatial_mesh_set_boundary_conditions( &(dom->spat_mesh), phi_left, phi_right, phi_top, phi_bottom );
    return;
}

void domain_particles_init( Domain *dom, Config *conf )
{
    particles_test_init( &(dom->particles), &(dom->num_of_particles), conf );
    return;
}

//
// Pic algorithm
//

void domain_run_pic( Domain *dom )
{
    eval_charge_density( dom );
    eval_potential_and_fields( dom );
    push_particles( dom );
    apply_domain_constrains( dom );
    update_time_grid( dom );
    return;
}

void eval_charge_density( Domain *dom )
{
    weight_particles_charge_to_mesh( dom );
    return;
}

void eval_potential_and_fields( Domain *dom )
{
    solve_poisson_eqn( dom );
    eval_fields_from_potential( dom );
    return;
}

void push_particles( Domain *dom )
{
    leap_frog( dom );
    return;
}

void apply_domain_constrains( Domain *dom )
{
    apply_domain_boundary_conditions( dom );
    //generate_new_particles();
    return;
}

//
// Eval charge density
//

void weight_particles_charge_to_mesh( Domain *dom )
{
    // Rewrite:
    // forall particles {
    //   find nonzero weights and corresponding nodes
    //   charge[node] = weight(particle, node) * particle.charge
    // }
    double dx = dom->spat_mesh.x_cell_size;
    double dy = dom->spat_mesh.y_cell_size;<commit_msg>Clear old values of charge density<commit_after>#include "domain.h"

// Domain initialization
void domain_time_grid_init( Domain *dom, Config *conf );
void domain_spatial_mesh_init( Domain *dom, Config *conf );
void domain_particles_init( Domain *dom, Config *conf );
// Pic algorithm
void eval_charge_density( Domain *dom );
void eval_potential_and_fields( Domain *dom );
void push_particles( Domain *dom );
void apply_domain_constrains( Domain *dom );
void update_time_grid( Domain *dom );
// Eval charge density on grid
void clear_old_density_values( Domain *dom );
void weight_particles_charge_to_mesh( Domain *dom );
void next_node_num_and_weight( const double x, const double grid_step, int *next_node, double *weight );
// Eval fields from charges
void solve_poisson_eqn( Domain *dom );
extern void hwscrt_( double *, double *, int *, int *, double *, double *,
		    double *, double *, int *, int *, double *, double *,
		    double *, double *, int *, double *, int *, double * );
void rowmajor_to_colmajor( double **c, double *fortran, int dim1, int dim2 );
void colmajor_to_rowmajor( double *fortran, double **c, int dim1, int dim2 );		  
void hwscrt_init_f( double left, double top, 
		    double right, double bottom, 
		    double **charge_density, double **hwscrt_f);
double **poisson_init_rhs( Spatial_mesh *spm );
void poisson_free_rhs( double **rhs, int nrow, int ncol );
void eval_fields_from_potential( Domain *dom );
double boundary_difference( double phi1, double phi2, double dx );
double central_difference( double phi1, double phi2, double dx );
// Push particles
void leap_frog( Domain *dom );
void update_momentum( Domain *dom );
void update_position( Domain *dom );
// Boundaries and generation
void apply_domain_boundary_conditions( Domain *dom );
bool out_of_bound( Domain *dom, Vec2d r );
void remove_particle( int *i, Domain *dom );
void proceed_to_next_particle( int *i, Domain *dom );
// Domain print
char *construct_output_filename( const char *output_filename_prefix, 
				 const int current_time_step,
				 const char *output_filename_suffix );
// Various functions
void domain_print_particles( Domain *dom );





//
// Domain initialization
//

void domain_prepare( Domain *dom, Config *conf )
{
    config_check_correctness( conf );
    domain_time_grid_init( dom, conf );    
    domain_spatial_mesh_init( dom, conf );
    domain_particles_init( dom, conf );
    return;
}

void domain_time_grid_init( Domain *dom, Config *conf )
{
    double total_time = conf->total_time;
    double step_size = conf->time_step_size;
    dom->time_grid = time_grid_init( total_time, step_size );
    return;
}

void domain_spatial_mesh_init( Domain *dom, Config *conf )
{
    double x_size = conf->grid_x_size;
    double x_step = conf->grid_x_step;
    double y_size = conf->grid_y_size;
    double y_step = conf->grid_y_step;
    double phi_left = conf->boundary_phi_left;
    double phi_right = conf->boundary_phi_right;
    double phi_top = conf->boundary_phi_top;
    double phi_bottom = conf->boundary_phi_bottom;
    dom->spat_mesh = spatial_mesh_init( x_size, x_step, y_size, y_step );
    spatial_mesh_set_boundary_conditions( &(dom->spat_mesh), phi_left, phi_right, phi_top, phi_bottom );
    return;
}

void domain_particles_init( Domain *dom, Config *conf )
{
    particles_test_init( &(dom->particles), &(dom->num_of_particles), conf );
    return;
}

//
// Pic algorithm
//

void domain_run_pic( Domain *dom )
{
    eval_charge_density( dom );
    eval_potential_and_fields( dom );
    push_particles( dom );
    apply_domain_constrains( dom );
    update_time_grid( dom );
    return;
}

void eval_charge_density( Domain *dom )
{
    clear_old_density_values( dom );
    weight_particles_charge_to_mesh( dom );
    return;
}

void eval_potential_and_fields( Domain *dom )
{
    solve_poisson_eqn( dom );
    eval_fields_from_potential( dom );
    return;
}

void push_particles( Domain *dom )
{
    leap_frog( dom );
    return;
}

void apply_domain_constrains( Domain *dom )
{
    apply_domain_boundary_conditions( dom );
    //generate_new_particles();
    return;
}

//
// Eval charge density
//

void clear_old_density_values( Domain *dom )
{
    Spatial_mesh *spm = &( dom->spat_mesh );
    int nx = spm->x_n_nodes;
    int ny = spm->y_n_nodes;    
    
    for ( int i = 0; i < nx; i++ ) {
	for ( int j = 0; j < ny; j++ ) {
	    spm->charge_density[i][j] = 0;
	}
    }
}


void weight_particles_charge_to_mesh( Domain *dom )
{
    // Rewrite:
    // forall particles {
    //   find nonzero weights and corresponding nodes
    //   charge[node] = weight(particle, node) * particle.charge
    // }
    double dx = dom->spat_mesh.x_cell_size;
    double dy = dom->spat_mesh.y_cell_size;
{'fraction_non_alphanumeric': 0.10857835040193811, 'fraction_numerical': 0.0023125206475057814, 'mean_word_length': 4.136877828054299, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, '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': '27993', 'n_tokens_mistral': 3140, 'n_tokens_neox': 2982, 'n_words': 805}