示例#1
0
"Demonstrates manual and auto sweeping and plotting"
import matplotlib as mpl
mpl.use('Agg')
# comment out the lines above to show figures in a window
import numpy as np
from gpkit import Model, Variable, units
from gpkit.constraints.tight import Tight

x = Variable("x", "m", "Swept Variable")
y = Variable("y", "m^2", "Cost")
m = Model(y, [
    y >= (x / 2)**-0.5 * units.m**2.5 + 1 * units.m**2,
    Tight([y >= (x / 2)**2])
])

# arguments are: model, swept: values, posnomial for y-axis
sol = m.sweep({x: np.linspace(1, 3, 20)}, verbosity=0)
f, ax = sol.plot(y)
ax.set_title("Manually swept (20 points)")
f.show()
f.savefig("plot_sweep1d.png")
sol.save()

# arguments are: model, swept: (min, max, optional logtol), posnomial for y-axis
sol = m.autosweep({x: (1, 3)}, tol=0.001, verbosity=0)
f, ax = sol.plot(y)
ax.set_title("Autoswept (7 points)\nGuaranteed to be in blue region")
f.show()
f.savefig("plot_autosweep1d.png")
# Optimization variables
sigma1a = Variable("sigma1a")
sigma1b = Variable("sigma1b")
sigma2a = Variable("sigma2a")
sigma2b = Variable("sigma2b")
sigma3a = Variable("sigma3a")
sigma3b = Variable("sigma3b")
p1 = Variable("p1")
p2 = Variable("p2")
p3 = Variable("p3")

# Constraints
constraints = [
    p1 <= 0.429 * delta + 0.25 * (1 - delta),
    Tight([
        sigma1a + sigma1b <= 1, sigma2a + sigma2b <= 1, sigma3a + sigma3b <= 1
    ]), p3 == 1,
    (sigma1a * 0.3 + sigma1b * 0.2 +
     (sigma1a * 0.3 + sigma1b * 0.2) * p1) / p1 <= 1
]

# Objective function
objective = 1 / sigma1a + 1 / sigma1b + 1 / sigma2a + 1 / sigma2b + 1 / sigma3a + 1 / sigma3b

# Formulate the Model
m = Model(objective, constraints)

# Solve the Model and print the results table
print(m.solve(verbosity=0).table())
示例#3
0
def SimPleAC():
    "Creates SimpleAC model"
    # Env. constants
    g = Variable("g", 9.81, "m/s^2", "gravitational acceleration")
    mu = Variable("\\mu", 1.775e-5, "kg/m/s", "viscosity of air")
    rho = Variable("\\rho", 1.23, "kg/m^3", "density of air")
    rho_f = Variable("\\rho_f", 817, "kg/m^3", "density of fuel")

    # Non-dimensional constants
    C_Lmax = Variable("C_{L,max}", 1.6, "-", "max CL with flaps down")
    e = Variable("e", 0.92, "-", "Oswald efficiency factor")
    k = Variable("k", 1.17, "-", "form factor")
    N_ult = Variable("N_{ult}", 3.3, "-", "ultimate load factor")
    S_wetratio = Variable("(\\frac{S}{S_{wet}})", 2.075, "-",
                          "wetted area ratio")
    tau = Variable("\\tau", 0.12, "-", "airfoil thickness to chord ratio")
    W_W_coeff1 = Variable("W_{W_{coeff1}}", 2e-5, "1/m",
                          "wing weight coefficent 1")  # 12e-5 originally
    W_W_coeff2 = Variable("W_{W_{coeff2}}", 60, "Pa",
                          "wing weight coefficent 2")

    # Dimensional constants
    Range = Variable("Range", 3000, "km", "aircraft range")
    TSFC = Variable("TSFC", 0.6, "1/hr", "thrust specific fuel consumption")
    V_min = Variable("V_{min}", 25, "m/s", "takeoff speed")
    W_0 = Variable("W_0", 6250, "N", "aircraft weight excluding wing")

    # Free Variables
    LoD = Variable("L/D", "-", "lift-to-drag ratio")
    D = Variable("D", "N", "total drag force")
    V = Variable("V", "m/s", "cruising speed")
    W = Variable("W", "N", "total aircraft weight")
    Re = Variable("Re", "-", "Reynold's number")
    CDA0 = Variable("(CDA0)", "m^2", "fuselage drag area")  # 0.035 originally
    C_D = Variable("C_D", "-", "drag coefficient")
    C_L = Variable("C_L", "-", "lift coefficient of wing")
    C_f = Variable("C_f", "-", "skin friction coefficient")
    W_f = Variable("W_f", "N", "fuel weight")
    V_f = Variable("V_f", "m^3", "fuel volume")
    V_f_avail = Variable("V_{f_{avail}}", "m^3", "fuel volume available")
    T_flight = Variable("T_{flight}", "hr", "flight time")

    # Free variables (fixed for performance eval.)
    A = Variable("A", "-", "aspect ratio")
    S = Variable("S", "m^2", "total wing area")
    W_w = Variable("W_w", "N", "wing weight")
    W_w_strc = Variable("W_w_strc", "N", "wing structural weight")
    W_w_surf = Variable("W_w_surf", "N", "wing skin weight")
    V_f_wing = Variable("V_f_wing", "m^3", "fuel volume in the wing")
    V_f_fuse = Variable("V_f_fuse", "m^3", "fuel volume in the fuselage")

    objective = W_f

    constraints = []

    # Weight and lift model
    constraints += [
        W >= W_0 + W_w + W_f,
        W_0 + W_w + 0.5 * W_f <= 0.5 * rho * S * C_L * V**2,
        W <= 0.5 * rho * S * C_Lmax * V_min**2, T_flight >= Range / V,
        LoD == C_L / C_D
    ]

    # Thrust and drag model
    C_D_fuse = CDA0 / S
    C_D_wpar = k * C_f * S_wetratio
    C_D_ind = C_L**2 / (np.pi * A * e)
    constraints += [
        W_f >= TSFC * T_flight * D, D >= 0.5 * rho * S * C_D * V**2,
        C_D >= C_D_fuse + C_D_wpar + C_D_ind,
        V_f_fuse <= 10 * units("m") * CDA0, Re <=
        (rho / mu) * V * (S / A)**0.5, C_f >= 0.074 / Re**0.2
    ]

    # Fuel volume model
    with SignomialsEnabled():
        constraints += [
            V_f == W_f / g / rho_f,
            # linear with b and tau, quadratic with chord
            V_f_wing**2 <= 0.0009 * S**3 / A * tau**2,
            V_f_avail <= V_f_wing + V_f_fuse,  # [SP]
            Tight([V_f_avail >= V_f])
        ]

    # Wing weight model
    constraints += [
        W_w_surf >= W_W_coeff2 * S,
        W_w_strc**2 >= W_W_coeff1**2 / tau**2 * N_ult**2 * A**3 *
        (V_f_fuse * g * rho_f + W_0) * W * S, W_w >= W_w_surf + W_w_strc
    ]

    m = Model(objective, constraints)

    return m
    def setup(self, N, topology_dict, friction='DW', penalty=10.):
        n_p = len(topology_dict)  # number of pipes
        H = VectorVariable(N, "H", "m", "Head")
        H_min = VectorVariable(N, "H_{min}", "m", "Minimal Head Required")
        source = VectorVariable(N, "\dot{V}_+", "m^3/s", "Source", pr=20)
        sink = VectorVariable(N, "\dot{V}_-", "m^3/s", "Sink", pr=20)
        rough = VectorVariable(n_p, "\\epsilon", "m", "Pipe Roughness")
        pipeCost = VectorVariable(n_p, "c_p", "-", "Pipe Cost")
        L = VectorVariable(n_p, "L", "m", "Pipe Length")
        D = VectorVariable(n_p, "D", "m", "Pipe Diameter")
        flow = VectorVariable(n_p, "q", "m^3/s", "Flow Rate")
        V = VectorVariable(n_p, "v_f", "m/s", "Flow Velocity")
        maxV = VectorVariable(n_p, "v_{max}", 1e20 * np.ones(n_p), "m/s",
                              'Maximum Flow Velocity')
        H_loss = VectorVariable(n_p, "H_L", "m", "Head Loss")
        slack_out = VectorVariable(N, "S_{out}", "-", "Outflow Slack")
        slack_in = VectorVariable(N, "S_{in}", "-", "Inflow Slack")
        slack_h = VectorVariable(n_p, "S_h", "-", "Head Slack")
        totalCost = Variable("C", "-", "Total Cost")
        D_max = Variable("D_{max}", "m", "Maximum Diameter")
        D_min = Variable("D_{min}", "m", "Minimum Diameter")
        rho = Variable("\\rho", 1000, "kg/m^3", "Density")
        mu = Variable("\\mu", 8.9e-4, "kg/m/s", "Viscosity")
        g = Variable("g", 9.81, "m/s^2", "Gravity")

        if friction == 'DW':
            relRough = VectorVariable(n_p, "\\bar{\\epsilon}", "-",
                                      "Relative Pipe Roughness")
            Re = VectorVariable(n_p, "Re", "-", "Reynold's Number")
            f = VectorVariable(n_p, "f", "-", "Friction Factor")

        constraints = []

        with SignomialsEnabled():

            for i in range(0, N):
                flow_in = sink[i]
                flow_out = source[i]
                for pipe_index, node in list(topology_dict.items()):
                    if node[0] == i:
                        flow_in += flow[pipe_index]
                    if node[1] == i:
                        flow_out += flow[pipe_index]

                constraints.extend([
                    Tight([flow_in <= slack_out[i] * flow_out]),
                    Tight([flow_out <= slack_in[i] * flow_in]),
                    Tight([slack_in[i] >= 1]),
                    Tight([slack_out[i] >= 1]), H[i] >= H_min[i]
                ])
                # Head loss constraints
                for pipe_index, node in list(topology_dict.items()):
                    if node[0] == i:
                        constraints.extend([
                            Tight([
                                H[node[0]] >= H_loss[pipe_index] + H[node[1]]
                            ]),
                            Tight([
                                H[node[0]] <= slack_h[pipe_index] *
                                (H_loss[pipe_index] + H[node[1]])
                            ]),
                            Tight([slack_h[pipe_index] >= 1]),
                        ])

            for pipe_index in range(n_p):
                constraints += [
                    V[pipe_index] <= maxV, pipeCost[pipe_index] == 1.1 *
                    D[pipe_index]**1.5 * L[pipe_index] / units.m**2.5,
                    V[pipe_index] == 4 * flow[pipe_index] /
                    (np.pi * D[pipe_index]**2), D[pipe_index] <= D_max,
                    D[pipe_index] >= D_min
                ]

                if friction == "HW":
                    constraints += [
                        H_loss[pipe_index] == 10.67 * L[pipe_index] *
                        (flow[pipe_index] / units('m^3/s'))**1.852 /
                        ((rough[pipe_index] / units('m'))**1.852 *
                         (D[pipe_index] / units('m'))**4.8704)
                    ]
                    # S (hydraulic slope H/L) = 10.67*Q^1.852 (volumetric flow rate) /
                    # C^1.852 (pipe roughness) /d^4.8704 (pipe diameter)

                if friction == 'DW':
                    constraints += [
                        H_loss[pipe_index] == f[pipe_index] * L[pipe_index] *
                        V[pipe_index]**2 / (2 * D[pipe_index] * g),
                        relRough[pipe_index] == rough[pipe_index] /
                        D[pipe_index],
                        Re[pipe_index] == rho * V[pipe_index] * D[pipe_index] /
                        mu,
                        # From frictionFactorFitting.py
                        f[pipe_index]**2.39794 >=
                        3.26853e-06 * Re[pipe_index]**0.0574443 *
                        relRough[pipe_index]**0.364794 +
                        0.0001773 * Re[pipe_index]**-0.529499 *
                        relRough[pipe_index]**-0.0810121 +
                        0.00301918 * Re[pipe_index]**-0.0220498 *
                        relRough[pipe_index]**1.73526 +
                        0.0734922 * Re[pipe_index]**-1.13629 *
                        relRough[pipe_index]**0.0574655 +
                        0.000214297 * Re[pipe_index]**0.00035242 *
                        relRough[pipe_index]**0.823896,
                        f[pipe_index] <= 1
                    ]

            constraints += [
                totalCost >= np.sum(pipeCost) *
                (np.prod(slack_out) * np.prod(slack_in) *
                 np.prod(slack_h)**penalty)
            ]
        return constraints