def find_rate_equations_steady_state(settings):
    """ find the rate equations steady state using a relaxation algorithm """
    start_time = time.time()
    initialize_logging(settings)

    # initialize densities
    settings, state = initialize_densities(settings)

    # initialize temperatures
    state['Ti'] = get_isentrope_temperature(state['n'],
                                            settings,
                                            species='ions')
    state['Te'] = get_isentrope_temperature(state['n'],
                                            settings,
                                            species='electrons')

    # relaxation algorithm initialization
    t_curr = 0
    num_time_steps = 0
    status_counter = 0
    state['termination_criterion_reached'] = False
    state['successful_termination'] = False

    while t_curr < settings['t_stop']:

        state['v_th'] = get_thermal_velocity(state['Ti'],
                                             settings,
                                             species='ions')
        state['coulomb_scattering_rate'] = get_coulomb_scattering_rate(
            state['n'], state['Ti'], state['Te'], settings, species='ions')
        state['mean_free_path'] = calculate_mean_free_path(state['n'],
                                                           state['Ti'],
                                                           state['Te'],
                                                           settings,
                                                           state=state,
                                                           species='ions')
        state['mirror_cell_sizes'] = get_mirror_cell_sizes(state['n'],
                                                           state['Ti'],
                                                           state['Te'],
                                                           settings,
                                                           state=state)
        state['v_col'], state['flux_E'] = get_collective_velocity(
            state, settings)
        state['f_above'], state['f_below'] = get_transition_filters(
            state['n'], settings)  # transition filters
        state['v_R'], state['v_L'] = get_transmission_velocities(
            state, settings)
        state['U'] = get_mmm_velocity(state, settings)
        state['alpha_tR'], state['alpha_tL'], state[
            'alpha_c'] = define_loss_cone_fractions(state, settings)
        state['dn_c_dt'], state['dn_tL_dt'], state[
            'dn_tR_dt'] = get_density_time_derivatives(state, settings)

        # variables for plots
        state[
            'transmission_rate_R'] = state['v_R'] / state['mirror_cell_sizes']
        state[
            'transmission_rate_L'] = state['v_L'] / state['mirror_cell_sizes']
        state['mmm_drag_rate'] = state['U'] / state['mirror_cell_sizes']

        # print basic run info
        if num_time_steps == 0:
            print_basic_run_info(state, settings)
            logging.info('***  Begin relaxation iterations  ***')

        # advance step
        dt, state = define_time_step(state, settings)
        t_curr += dt
        num_time_steps += 1
        state = advance_densities_time_step(state, dt)

        # check if densities are too low and deal with them
        state = check_minimal_density(state, settings, dt, t_curr,
                                      num_time_steps)

        # boundary conditions
        state = enforce_boundary_conditions(state, settings)

        # update temperatures
        state['Ti'] = get_isentrope_temperature(state['n'],
                                                settings,
                                                species='ions')
        state['Te'] = get_isentrope_temperature(state['n'],
                                                settings,
                                                species='electrons')

        if check_status_threshold_passed(settings, t_curr, num_time_steps, status_counter) \
                or state['termination_criterion_reached']:
            # print basic information
            if settings['print_time_step_info'] is True:
                print_time_step_info(dt, t_curr, num_time_steps)

            # print minimal density values for debugging
            print_minimal_densities(state)

            # define fluxes and check if termination criterion is reached
            state = get_fluxes(state, settings)
            state = save_fluxes_evolution(state, t_curr)
            state = check_termination_criterion_reached(
                state, settings, t_curr, num_time_steps, status_counter)

            # plot status
            if settings['draw_plots'] is True:
                plot_relaxation_status(state, settings)
                if settings['save_plots_scheme'] == 'status_plots':
                    save_plots(settings)

            status_counter += 1

        if state['termination_criterion_reached'] is True:
            break

    logging.info('*************************************')
    logging.info('*** Finished relaxation iterations ***')
    if state['successful_termination'] == False:
        # print in red color
        logging.info('\x1b[5;30;41m' + 'Termination unsuccessful.' + '\x1b[0m')
    else:
        # print in green color
        logging.info('\x1b[5;30;42m' + 'Termination successful.' + '\x1b[0m')

    # save the plots
    if settings['draw_plots'] is True:
        if settings['save_plots_scheme'] == 'status_plots':
            save_plots(settings)
        elif settings['save_plots_scheme'] == 'only_at_calculation_end':
            plot_relaxation_status(state, settings)
            save_plots(settings)
        else:
            raise ValueError(
                'draw_plots is True but save_plots_scheme is invalid. '
                'save_plots_scheme = ' + str(settings['save_plots_scheme']))

    # run time
    state['run_time'] = get_simulation_time(start_time)
    state['t_end'] = t_curr
    state['num_time_steps'] = num_time_steps

    # save results
    if settings['save_state'] is True:
        save_simulation(state, settings)

    return state
Exemplo n.º 2
0
### Plot fusion and radiation loss parameters

settings = define_default_settings()

# Z_ion = settings['Z_ion']
Z_ion = 1
# B = 7 # [Tesla]
B = 15  # [Tesla]
# n0 = settings['n0']
n0 = 2e22  # = ni = ne
n_tot = 2 * n0
Ti_0 = settings['Ti_0']
Te_0 = settings['Te_0']

v_th = get_thermal_velocity(Ti_0, settings, species='ions')

T_keV_array = np.linspace(0.2, 200, 1000)

reactions = []
reactions += ['D_T_to_n_alpha']
reactions += ['D_D_to_p_T_n_He3']
reactions += ['D_He3_to_p_alpha']
# reactions += ['T_T_to_alpha_2n']
reactions += ['p_B_to_3alpha']
# reactions += ['p_D_to_He3_gamma']
# reactions += ['He3_He3_to_alpha_2p']
# reactions += ['p_p_to_D_e_nu']

# colors = ['b', 'g', 'r', 'k', 'm', 'y', 'c', 'b']
colors = cm.rainbow(np.linspace(0, 1, len(reactions)))
Exemplo n.º 3
0
                                               species='ions')
Te_isentrope_array = get_isentrope_temperature(n_array,
                                               settings,
                                               species='electrons')
plt.figure(100)
plt.plot(n_array, Ti_isentrope_array / settings['keV'], label='i', color='r')
plt.plot(n_array, Te_isentrope_array / settings['keV'], label='e', color='b')
plt.legend()
plt.xlabel('n [g/cc]')
plt.ylabel('T [keV]')
plt.title('Isentropes')
plt.tight_layout()
plt.grid()

# plot rates
v_th_i = get_thermal_velocity(Ti_isentrope_array, settings, species='ions')
v_th_e = get_thermal_velocity(Te_isentrope_array,
                              settings,
                              species='electrons')
mirror_cell_sizes = get_mirror_cell_sizes(n_array, Ti_isentrope_array,
                                          Te_isentrope_array, settings)
# nu_i_trans = get_transmission_rate(v_th_i, mirror_cell_sizes)
# nu_e_trans = get_transmission_rate(v_th_e, mirror_cell_sizes)
nu_i_scat = get_coulomb_scattering_rate(n_array,
                                        Ti_isentrope_array,
                                        Te_isentrope_array,
                                        settings,
                                        species='ions')
nu_e_scat = get_coulomb_scattering_rate(n_array,
                                        Ti_isentrope_array,
                                        Te_isentrope_array,