def numerical_loop(real_time, pos, vel, Ie, W_elec, idx, B, E_int, q_dens, Ji, Ve, Te, DT, max_inc, data_iter, ch_flag): ''' Does the number crunching for a short snippet. Logs number of time variable changes in ch_flag as powers of 2 (-1 = half, 2 = 4 times slower) Array values are mutable: Don't have to be returned. Only integer values ''' qq = 0 while qq < data_iter: # Check timestep used for this iteration vel, qq, DT, max_inc, data_iter, ch_flag = aux.check_timestep( qq, DT, pos, vel, B, E_int, q_dens, Ie, W_elec, max_inc, data_iter, idx) # Add timestep to counter real_time += DT # Main loop pos, vel, Ie, W_elec, q_dens_adv, Ji = particles.advance_particles_and_moments( pos, vel, Ie, W_elec, idx, B, E_int, DT) q_dens = 0.5 * (q_dens + q_dens_adv) B = fields.push_B(B, E_int, DT) E_half, Ve, Te = fields.calculate_E(B, Ji, q_dens) q_dens = q_dens_adv.copy() # Predictor-Corrector: Advance fields to start of next timestep E_int, B = fields.predictor_corrector(B, E_int, E_half, pos, vel, q_dens_adv, Ie, W_elec, idx, DT) # Increment loop variable qq += 1 return DT, ch_flag, max_inc, data_iter, real_time
if ch_flag == 1: print 'Timestep halved. Syncing particle velocity with DT = {}'.format( DT) elif ch_flag == 2: print 'Timestep Doubled. Syncing particle velocity with DT = {}'.format( DT) # Main loop pos, vel, Ie, W_elec, q_dens_adv, Ji = particles.advance_particles_and_moments( pos, vel, Ie, W_elec, idx, B, E_int, DT) q_dens = 0.5 * (q_dens + q_dens_adv) B = fields.push_B(B, E_int, DT) E_half, Ve, Te = fields.calculate_E(B, Ji, q_dens) q_dens = q_dens_adv.copy() # Predictor-Corrector: Advance fields to start of next timestep E_int, B = fields.predictor_corrector(B, E_int, E_half, pos, vel, q_dens_adv, Ie, W_elec, idx, DT) if generate_data == 1: if qq % data_iter == 0: save.save_data(DT, data_iter, qq, pos, vel, Ji, E_int, B, Ve, Te, q_dens) if (qq + 1) % 25 == 0: print 'Timestep {} of {} complete'.format(qq + 1, max_inc) qq += 1 print "Time to execute program: {0:.2f} seconds".format( round(timer() - start_time, 2)) # Time taken to run simulation
# TIMESTEP CHECK if adaptive_timestep == True: qq, DT, max_inc, data_iter, plot_iter \ = aux.check_timestep(qq, DT, pos, vel, B, E_int, q_dens, max_inc, data_iter, plot_iter, idx) # MAIN LOOP q_dens_adv, Ji = particles.advance_particles_and_moments( pos, vel, idx, B, E_int, DT) q_dens = 0.5 * (q_dens + q_dens_adv) B = fields.push_B(B, E_int, DT) E_half, Ve, Te = fields.calculate_E(B, Ji, q_dens) q_dens = q_dens_adv.copy() E_int, B = fields.predictor_corrector(B, E_int, E_half, pos, vel, q_dens, idx, DT) # OUTPUT if qq % data_iter == 0 and generate_data == 1: pas.save_data(DT, data_iter, qq, pos, vel, Ji, E_int, B, Ve, Te, q_dens) if qq % plot_iter == 0 and generate_plots == 1: pas.create_figure_and_save(pos, vel, E_int, B, q_dens, qq, DT, plot_iter) if (qq + 1) % 1 == 0: print 'Timestep {} of {} complete'.format(qq + 1, max_inc) qq += 1