def season(lats, days=[80, 52, 21, 355]): " trade seasonal availibility with weight " V = Aircraft(sp=False) data = {} for l in lats: failed = False mtows = [] for d in days: if failed: mtows = mtows + [np.nan] * (4 - len(mtows)) break M = Mission(V, latitude=range(1, l + 1, 1), day=d) M.cost = M[M.aircraft.Wtotal] try: sol = M.solve() mtow = sol(M.aircraft.Wtotal).magnitude mtows.append(mtow) except RuntimeWarning: mtows.append(np.nan) failed = True mtows = np.hstack(mtows) data[l] = mtows df = pd.DataFrame(data) return df
def test(): " test for integrated testing " v = Aircraft(Npod=0, sp=True) model = Mission(v, latitude=[20]) model.cost = model[model.aircraft.Wtotal] result = model.localsolve() _ = get_highestsens(model, result) vn = { model.aircraft.Wpay: "$W_{\\mathrm{pay}}$", model.aircraft.battery.etacharge: "$\\eta_{\\mathrm{charge}}$" } _ = get_highestsens(model, result, vn)
def build_solar(lat=15, day=140, sp=True): " try different battery specific energies " M = Mission(latitude=lat, day=day, sp=sp) M.cost = M["W_{total}"] M.substitutions.update({"W_{pay}": 0}) for vk in M.varkeys["P_{avn}"]: M.substitutions.update({vk: 50}) for h in [150, 200, 260]: M.substitutions.update({"h_{batt}": h}) try: sol = M.localsolve("mosek", verbosity=2) if sp else M.solve("mosek") print sol.table(["W_{total}", "b_Mission/Aircraft/Wing", "S", "W_Mission/Aircraft/Wing", "W_Mission/Aircraft/Battery", "P_{oper}"]) except RuntimeWarning: print "Not feasible" return M
def pods(N=[1, 3, 5, 7, 9, 0], Nplot=5): "trade number of pods" SP = True data = {} for i in N: print("\nN=%i" % i) from solar import Mission, Aircraft Vehicle = Aircraft(Npod=i, sp=SP) M = Mission(Vehicle, latitude=[20]) M.cost = M[M.aircraft.Wtotal] try: sol = M.localsolve() data[i] = sol("Wtotal").magnitude except RuntimeWarning as e: print("rw: %s" % e) try: V2 = Aircraft(Npod=i, sp=SP) M2 = Mission(V2, latitude=[20]) M2.cost = M2[M2.aircraft.Wtotal] feas = relaxed_constants(M2) sol2 = feas.localsolve() vks = post_process(sol2) data[i] = np.NaN if vks else sol2("Wtotal").magnitude M, sol = M2, sol2 except Exception as e: print("e: %s" % e) data[i] = np.NaN except CalledProcessError as e: print("cpe: %s" % e) data[i] = np.NaN # mosek_cli can't process certain Ns if Nplot == i: plot_shear(M, sol) df = pd.DataFrame(data, index=[0]) return df
ax.bar(ind - 0.25, pss, 0.5, color="#4D606E") ax.bar(ind - 0.25, ngs, 0.5, color="#3FBAC2") ax.set_xlim([0.0, ind[-1]+0.5]) ax.set_xticks(ind) ax.set_xticklabels(labels, rotation=-45, ha="left") ax.legend(["Positive", "Negative"]) ax.set_ylabel("sensitivities") return fig, ax if __name__ == "__main__": sols = [] Ms = [] for l in [25, 29]: for p in [85, 90]: M = Mission(latitude=l) M.cost = M["W_{total}"] Ms.append(M) for vk in M.varkeys["p_{wind}"]: M.substitutions.update({vk: p/100.0}) sol = M.solve("mosek") sols.append(sol) varns = ["p_{wind}", "\\eta_Mission/Aircraft/SolarCells", "\\eta_{charge}", "\\eta_{discharge}", "\\rho_{solar}", "t_{night}", "(E/S)_{irr}", "h_{batt}", "W_{pay}", "\\eta_{prop}"] latns = ["$p_{\\mathrm{wind}}$", "$\\eta_{\\mathrm{solar}}$", "$\\eta_{\\mathrm{charge}}$", "$\\eta_{\\mathrm{discharge}}$", "$\\rho_{\\mathrm{solar}}$", "$t_{\\mathrm{night}}$", "$(E/S)_{\\mathrm{irr}}$",
from solar import Aircraft, Mission from gpkitmodels.GP.aircraft.fuselage.elliptical_fuselage import Fuselage Aircraft.fuseModel = Fuselage m = Mission(latitude=[20]) sol = m.solve()
def plot_contours(path=None): N = 100 stime = 0.0 numsolves = 0 plt.rcParams.update({'font.size': 19}) hmin = 250.0 hmax = 400.0 for av in [80, 85, 90]: for lat in [25, 30, 35]: zo = 10 if lat == 25: bs = range(30, 80, 5) elif lat == 30: bs = range(35, 80, 5) if av == 90: del bs[0] else: bs = range(45, 80, 5) if av == 90: del bs[0] fig, ax = plt.subplots() M = Mission(latitude=lat) M.substitutions.update({"W_{pay}": 10}) for vk in M.varkeys["\\eta_{prop}"]: M.substitutions.update({vk: 0.75}) for vk in M.varkeys["p_{wind}"]: M.substitutions.update({vk: av / 100.0}) del M.substitutions["\\eta_Mission/Aircraft/SolarCells"] del M.substitutions["h_{batt}"] M.cost = M["h_{batt}"] lines = [] midx = [] for b in bs: etamax = 0.4 etamin = 0.15 M.substitutions.update({"b_Mission/Aircraft/Wing": b}) M.substitutions.update( {"\\eta_Mission/Aircraft/SolarCells": etamax}) sol = M.solve("mosek") stime += sol["soltime"] numsolves += 1 del M.substitutions["\\eta_Mission/Aircraft/SolarCells"] if sol("h_{batt}").magnitude < hmin: M.substitutions.update({"h_{batt}": hmin}) M.cost = M["\\eta_Mission/Aircraft/SolarCells"] sol = M.solve("mosek") stime += sol["soltime"] numsolves += sol["soltime"] etamax = sol("\\eta_Mission/Aircraft/SolarCells") M.substitutions.update({"h_{batt}": hmax}) M.cost = M["\\eta_Mission/Aircraft/SolarCells"] sol = M.solve("mosek") stime += sol["soltime"] numsolves += 1 etamin = sol("\\eta_Mission/Aircraft/SolarCells") if etamin > etamax: continue del M.substitutions["h_{batt}"] M.cost = M["h_{batt}"] xmin_ = np.linspace(etamin, etamax, 100) tol = 0.01 bst = autosweep_1d(M, tol, M["\\eta_Mission/Aircraft/SolarCells"], [etamin, etamax], solver="mosek") bsts = find_sols([bst]) sols = np.hstack([bs.sols for bs in bsts]) stime += sum(np.unique([s["soltime"] for s in sols])) numsolves += bst.nsols if b % 10 == 0: l = ax.plot(xmin_, bst.sample_at(xmin_)["cost"], "k", label="%d [ft]" % b, zorder=zo) zo += 2 lines.append(l[0]) midx.append(np.median(xmin_)) else: ax.plot(xmin_, bst.sample_at(xmin_)["cost"], "--", c="0.5", zorder=100) # parato fontier del M.substitutions["b_Mission/Aircraft/Wing"] M.substitutions.update( {"\\eta_Mission/Aircraft/SolarCells": etamax}) M.cost = M["h_{batt}"] sol = M.solve("mosek") etamax = 0.4 stime += sol["soltime"] numsolves += 1 del M.substitutions["\\eta_Mission/Aircraft/SolarCells"] if sol("h_{batt}").magnitude < hmin: M.substitutions.update({"h_{batt}": hmin}) M.cost = M["\\eta_Mission/Aircraft/SolarCells"] sol = M.solve("mosek") stime += sol["soltime"] numsolves += sol["soltime"] etamax = sol("\\eta_Mission/Aircraft/SolarCells") M.substitutions.update({"h_{batt}": hmax}) M.cost = M["\\eta_Mission/Aircraft/SolarCells"] sol = M.solve("mosek") stime += sol["soltime"] numsolves += 1 etamin = sol("\\eta_Mission/Aircraft/SolarCells") if etamin > etamax: break del M.substitutions["h_{batt}"] M.cost = M["h_{batt}"] xmin_ = np.linspace(etamin, etamax, 100) tol = 0.01 bst = autosweep_1d(M, tol, M["\\eta_Mission/Aircraft/SolarCells"], [etamin, etamax], solver="mosek") bsts = find_sols([bst]) sols = np.hstack([b.sols for b in bsts]) stime += sum(np.unique([s["soltime"] for s in sols])) numsolves += bst.nsols ax.set_xlabel("Solar Cell Efficiency") ax.set_ylabel("Battery Specific Energy [Whr/kg]") labelLines(lines[:-1], align=True, xvals=midx, zorder=[11, 13, 15, 17]) ax.fill_between(np.hstack([0.15, xmin_]), 0, np.hstack([hmax, bst.sample_at(xmin_)["cost"]]), edgecolor="r", lw=2, hatch="/", facecolor="None", zorder=100) ax.text(0.17, 260, "Infeasible", fontsize=19) ax.set_xlim([0.15, 0.4]) ax.set_ylim([250, 400]) if path: fig.savefig(path + "bcontourl%da%d.pdf" % (lat, av), bbox_inches="tight") else: fig.savefig("bcontourl%da%d.pdf" % (lat, av)) print "%d solves in %.4f seconds" % (numsolves, stime)
" contour plots " import matplotlib.pyplot as plt import numpy as np from solar import Mission from plotting import windalt_plot, labelLines from gpkit.tools.autosweep import autosweep_1d N = 100 plt.rcParams.update({'font.size':19}) fig, ax = plt.subplots() bmax = [] lines = [] for lat in range(20, 31, 2): hmax = 500 M = Mission(latitude=lat) M.substitutions.update({"W_{pay}": 10}) for vk in M.varkeys["\\eta_{prop}"]: M.substitutions.update({vk: 0.75}) for vk in M.varkeys["p_{wind}"]: M.substitutions.update({vk: 90/100.0}) del M.substitutions["h_{batt}"] M.cost = M["h_{batt}"] sol = M.solve("mosek") hmin = sol["cost"].magnitude + 1e-3 M.cost = M["b_Mission/Aircraft/Wing"] xmin_ = np.linspace(hmin, hmax, 100) tol = 0.01 bst = autosweep_1d(M, tol, M["h_{batt}"], [hmin, hmax], solver="mosek") l = ax.plot(xmin_, bst.sample_at(xmin_)["cost"], "k", label="%d" % lat) lines.append(l[0])
def plot_battsolarcon(): fig, ax = plt.subplots() time = 0 numsolves = 0 etamax = [] lines = [] midx = [] passing = True for lat in range(20, 44, 1): if passing: hmax = 500 M = Mission(latitude=lat) M.substitutions.update({"W_{pay}": 10}) for vk in M.varkeys["\\eta_{prop}"]: M.substitutions.update({vk: 0.75}) for vk in M.varkeys["p_{wind}"]: M.substitutions.update({vk: 90 / 100.0}) del M.substitutions["h_{batt}"] M.substitutions.update({"\\eta_Mission/Aircraft/SolarCells": 0.4}) M.cost = M["h_{batt}"] sol = M.solve("mosek") time += sol["soltime"] numsolves += 1 hmin = sol["cost"].magnitude + 1e-3 if hmin >= hmax: break del M.substitutions["\\eta_Mission/Aircraft/SolarCells"] M.cost = M["\\eta_Mission/Aircraft/SolarCells"] xmin_ = np.linspace(hmin, hmax, 100) tol = 0.01 notpassing = True try: bst = autosweep_1d(M, tol, M["h_{batt}"], [hmin, hmax], solver="mosek") bsts = find_sols([bst]) sols = np.hstack([b.sols for b in bsts]) time += sum(np.unique([s["soltime"] for s in sols])) numsolves += bst.nsols if lat % 4 == 0: l = ax.plot(xmin_, bst.sample_at(xmin_)["cost"], "k", label="%d$^{\\circ}$ Lat" % lat) lines.append(l[0]) etamax.append(max(bst.sample_at(xmin_)["cost"].magnitude)) midx.append(np.median(xmin_)) elif lat % 2 == 0: l = ax.plot(xmin_, bst.sample_at(xmin_)["cost"], "--", c="0.5", label="%d$^{\\circ}$ Lat" % lat) except RuntimeWarning: passing = False # ax.fill_between(xmin_, bst.sample_at(xmin_)["cost"], max(etamax), # edgecolor="r", lw=2, hatch="/", facecolor="None", zorder=100) print "%d solves in %.4f seconds" % (numsolves, time) labelLines(lines, align=False, xvals=midx, zorder=[10] * len(lines)) # ax.text(425, 0.36, "Infeasible") ax.set_ylabel("Solar Cell Efficiency") ax.set_xlabel("Battery Specific Energy [Whr/kg]") ax.set_xlim([250, 500]) ax.set_ylim([0.1, max(etamax)]) ax.grid() return fig, ax