def __cost_end_func(self,
                        no_alt_range_int,
                        spike,
                        CEA,
                        downstream_factor=1.2,
                        chr_mesh_n=120,
                        no_core=1):
        alt_range = np.linspace(0, 9144, no_alt_range_int)

        # shuffle arrays so each core computes similar complexity on average
        np.random.shuffle(alt_range)
        (p_atm_r, T_atm_r, rho_atm_r) = gd.standard_atmosphere(alt_range)

        thrust_range = self.__multicore_thrust_compute(spike,
                                                       alt_range,
                                                       CEA.gamma,
                                                       downstream_factor=1.2,
                                                       chr_mesh_n=chr_mesh_n,
                                                       no_core=no_core)

        # unshuffle arrays
        ordered_idx = np.argsort(alt_range)
        alt_range = alt_range[ordered_idx]
        thrust_range = thrust_range[ordered_idx]

        work = np.trapz(thrust_range, alt_range)
        # plt.plot(alt_range,thrust_range,'o')
        # plt.show()
        print('work = ' + str(work))

        ## heat transfer required
        total_heat_flux = heat_flux(CEA.Pr, CEA.cp, CEA.gamma, CEA.c, CEA.w,
                                    CEA.T_c, T_w, spike)

        return (work, total_heat_flux)
def cost_func_contour_params(params,
                             spike,
                             T_w,
                             CEA,
                             alpha,
                             beta,
                             chr_mesh_n=145,
                             no_core=4):
    params = np.asarray(params)
    if len(params.shape) > 1:
        raise ValueError('Input params not correct shape. Should be 1D array')
    x_vals, y_vals = np.split(params, 2)

    spike.x = x_vals
    spike.y = y_vals

    ##	thurst estimation over altitude
    alt_range = np.linspace(0, 9144, 3 * no_core)

    # shuffle arrays so each core computes similar complexity on average
    np.random.shuffle(alt_range)
    (p_atm_r, T_atm_r, rho_atm_r) = gd.standard_atmosphere(alt_range)

    thrust_range = multicore_thrust_compute(spike,
                                            alt_range,
                                            CEA.gamma,
                                            downstream_factor=1.2,
                                            chr_mesh_n=chr_mesh_n,
                                            no_core=no_core)

    # unshuffle arrays
    ordered_idx = np.argsort(alt_range)
    alt_range = alt_range[ordered_idx]
    thrust_range = thrust_range[ordered_idx]

    work = np.trapz(alt_range, thrust_range)
    # plt.plot(alt_range,thrust_range,'o')
    # plt.show()

    ## heat transfer required
    total_heat_flux = heat_flux(CEA.Pr, CEA.cp, CEA.gamma, CEA.c, CEA.w,
                                CEA.T_c, T_w, spike)

    return -alpha * work + beta * total_heat_flux
def COST_FNC(design_alt, truncate_ratio, T_w, CEA, r_e, alpha, beta, n):

    ### DESIGNING NOZZLE
    (p_atm, T_atm, rho_atm) = gd.standard_atmosphere([design_alt])

    PR = CEA.p_c / p_atm

    M_e = gd.PR_expansion_mach(PR, CEA.gamma)

    expansion_ratio = gd.expansion_ratio(1, M_e, CEA.gamma)  #6.64 #8.1273
    # print('Exp. ratio: ' + str(expansion_ratio))
    # print('PR: ' + str(PR))

    A_t = r_e**2 * np.pi / expansion_ratio  # max expansion (r_b = 0, r_e**2 >= A_t*expansion_ratio/np.pi)

    spike = plug_nozzle(expansion_ratio,
                        A_t,
                        r_e,
                        CEA.gamma,
                        CEA.T_c,
                        CEA.p_c,
                        CEA.a_c,
                        CEA.rho_c,
                        n,
                        truncate_ratio=truncate_ratio)

    ### CALCULATING COST
    ##	thurst estimation over altitude
    alt_range = np.linspace(0, 12000, 30)
    (p_atm_r, T_atm_r, rho_atm_r) = gd.standard_atmosphere(alt_range)
    #print(CEA.p_c/p_atm_r)
    thrust_range = np.zeros(alt_range.shape)
    for i in range(alt_range.shape[0]):
        if i == 10:
            MOC_mesh = MOC.chr_mesh(spike,
                                    gamma,
                                    alt_range[i],
                                    50,
                                    downstream_factor=1.2,
                                    plot_chr=0)
        else:
            MOC_mesh = MOC.chr_mesh(spike,
                                    gamma,
                                    alt_range[i],
                                    50,
                                    downstream_factor=1.2,
                                    plot_chr=0)

        thrust_range[i] = MOC_mesh.compute_thrust('nearest', 10)

    work = np.trapz(thrust_range, alt_range)
    plt.plot(alt_range, thrust_range, 'o')
    plt.show()
    ## heat transfer required

    total_heat_flux = heat_flux(CEA.Pr, CEA.cp, CEA.gamma, CEA.c, CEA.w,
                                CEA.T_c, T_w, spike)

    # print('Work*alpha: ' + str(work*alpha))
    # print('Heat flux*beta: ' + str(total_heat_flux*beta))
    return -alpha * work + total_heat_flux * beta