"""Roof model + module layout for an INVENTED sample house in El Paso, TX (no real address). Local grid in metres: x = east, y = north, origin = west end of the house's south eave line. Two-storey cross-hip roof with a south gable wing, one-storey rear roof and garage roof. Pitch and heights are ASSUMED. Street to the east. """ import math, json, csv from shapely.geometry import Polygon, LineString, box, Point from shapely import affinity PITCH = 22.6 # deg, 5:12 ASSUMED TANP = math.tan(math.radians(PITCH)); COSP = math.cos(math.radians(PITCH)) UPPER_EAVE_H = 5.8 # m ASSUMED (2-storey plate + overhang) LOWER_EAVE_H = 2.9 # m ASSUMED (1-storey) MOD_L, MOD_W, MOD_P = 1.722, 1.134, 430 # ASSUMED residential all-black module GAP = 0.05 CLEAR = {"eave": 0.25, "rake": 0.25, "wall": 0.25, "hip": 0.46, "valley": 0.46, "ridge": 0.46} VENT_CLEAR = 0.35; VENT_R = 0.05 # ---- roof faces (plan polygons, CCW not required). each edge typed. # az = downhill azimuth (0 = north, 90 = east, 180 = south, 270 = west) FACES = { # upper roof: E-W block x 3.2..17.2, y 4.3..11.5, ridge y 7.9; south wing x 5.8..13.0 ridge x 9.4 gable at y 0 "U-S1": dict(desc="Upper roof, south slope east of wing", az=180, eave_h=UPPER_EAVE_H, pts=[(13.0,4.3),(17.2,4.3),(13.6,7.9),(9.4,7.9)], edges=["eave","hip","ridge","valley"]), "U-S2": dict(desc="Upper roof, south slope west of wing", az=180, eave_h=UPPER_EAVE_H, pts=[(3.2,4.3),(5.8,4.3),(9.4,7.9),(6.8,7.9)], edges=["eave","valley","ridge","hip"]), "U-W": dict(desc="Upper wing, west slope (faces the street)", az=270, eave_h=UPPER_EAVE_H, pts=[(5.8,0.0),(5.8,4.3),(9.4,7.9),(9.4,0.0)], edges=["eave","valley","ridge","rake"]), "U-E": dict(desc="Upper wing, east slope (rear)", az=90, eave_h=UPPER_EAVE_H, pts=[(13.0,0.0),(13.0,4.3),(9.4,7.9),(9.4,0.0)], edges=["eave","valley","ridge","rake"]), "U-EH": dict(desc="Upper roof, east hip end (rear)", az=90, eave_h=UPPER_EAVE_H, pts=[(17.2,4.3),(17.2,11.5),(13.6,7.9)], edges=["eave","hip","hip"]), "U-WH": dict(desc="Upper roof, west hip end (street side) - kept free as street-side pathway", az=270, eave_h=UPPER_EAVE_H, pts=[(3.2,4.3),(3.2,11.5),(6.8,7.9)], edges=["eave","hip","hip"], keep_free=True), "U-N": dict(desc="Upper roof, north slope - kept free (pathway, low yield)", az=0, eave_h=UPPER_EAVE_H, pts=[(3.2,11.5),(17.2,11.5),(13.6,7.9),(6.8,7.9)], edges=["eave","hip","ridge","hip"], keep_free=True), "L-SE": dict(desc="Lower rear roof (1-storey), slopes south from upper wall", az=180, eave_h=LOWER_EAVE_H, pts=[(13.0,0.0),(17.2,0.0),(17.2,4.3),(13.0,4.3)], edges=["eave","rake","wall","wall"]), # lower front roof over garage: hip, ridge E-W at y 3.5, abuts the upper storey at x 5.4 "G-S": dict(desc="Garage roof, south slope (1-storey)", az=180, eave_h=LOWER_EAVE_H, pts=[(0.0,0.1),(5.4,0.1),(5.4,3.5),(3.4,3.5)], edges=["eave","wall","ridge","hip"]), "G-W": dict(desc="Garage roof, west hip end (faces the street)", az=270, eave_h=LOWER_EAVE_H, pts=[(0.0,0.1),(3.4,3.5),(0.0,6.9)], edges=["hip","hip","eave"]), "G-N": dict(desc="Garage roof, north slope (1-storey)", az=0, eave_h=LOWER_EAVE_H, pts=[(0.0,6.9),(3.4,3.5),(5.4,3.5),(5.4,6.9)], edges=["hip","ridge","wall","eave"]), } # lower front roof (garage / entry) - outline only, not used FRONT_ROOF = [(0.3,6.9),(2.9,6.9),(2.9,11.0),(0.3,11.0)] # porch roof, outline only CLEAR_TIGHT = {"eave": 0.15, "rake": 0.15, "wall": 0.15, "hip": 0.15, "valley": 0.15, "ridge": 0.46} PATIO = [(17.2,5.2),(19.9,5.2),(19.9,9.6),(17.2,9.6)] # roof vents on the sample house (plan, m), before the transform below VENTS = [(8.9,5.3),(15.7,5.2),(11.8,3.2),(12.2,2.6),(12.5,2.0),(10.9,0.7),(11.1,9.5),(11.9,8.5),(9.2,10.9)] # ---- the sample house is invented: the base outline is lengthened 1.2 m east of the wing and mirrored # east-west, so the street is to the EAST. Every outline, vent and keep-out goes through T(). STRETCH, XMAX = 1.2, 18.4 def T(p): x, y = p if x > 13.05: x += STRETCH return (round(XMAX - x, 3), y) FLIP = {90: 270, 270: 90, 0: 0, 180: 180} for _f in FACES.values(): _f["pts"] = [T(p) for p in _f["pts"]]; _f["az"] = FLIP[_f["az"]] _f["desc"] = _f["desc"].replace("west", "WEST").replace("east", "west").replace("WEST", "east").replace("West", "East") REN = {"U-W": "U-E", "U-E": "U-W", "U-EH": "U-WH", "U-WH": "U-EH", "G-W": "G-E"} FACES = {REN.get(k, k): v for k, v in FACES.items()} FRONT_ROOF = [T(p) for p in FRONT_ROOF]; PATIO = [T(p) for p in PATIO]; VENTS = [T(p) for p in VENTS] def face_frame(f): """unit vectors: d = downhill (plan), e = along eave (right-hand when looking uphill)""" a = math.radians(f["az"]) d = (math.sin(a), math.cos(a)) # downhill direction in plan (x east, y north) e = (-d[1], d[0]) # rotate +90 return d, e def to_face(f, p, origin): d, e = face_frame(f) dx, dy = p[0]-origin[0], p[1]-origin[1] u = dx*e[0] + dy*e[1] w = -(dx*d[0] + dy*d[1]) # uphill plan distance return (u, w / COSP) def from_face(f, uv, origin): d, e = face_frame(f) u, v = uv; w = v*COSP x = origin[0] + u*e[0] - w*d[0] y = origin[1] + u*e[1] - w*d[1] return (x, y, f["eave_h"] + w*TANP) def origin_of(f): # lowest point = any eave vertex; use first vertex of the edge typed "eave" i = f["edges"].index("eave"); return f["pts"][i] def usable(f, clear=None, keepouts=(), vents=True): clear = clear or CLEAR o = origin_of(f) fp = [to_face(f, p, o) for p in f["pts"]] poly = Polygon(fp) n = len(fp) for i in range(n): a, b = fp[i], fp[(i+1) % n] c = clear[f["edges"][i]] # extend the edge line far in both directions, cut a flat band dx, dy = b[0]-a[0], b[1]-a[1]; L = math.hypot(dx, dy) ux, uy = dx/L, dy/L line = LineString([(a[0]-ux*50, a[1]-uy*50), (b[0]+ux*50, b[1]+uy*50)]) poly = poly.difference(line.buffer(c, cap_style=2)) for v in (VENTS if vents else []): if Polygon(f["pts"]).contains(Point(v)): vu = to_face(f, v, o) poly = poly.difference(Point(vu).buffer(VENT_CLEAR + VENT_R, 32)) for k in keepouts: kp = Polygon(k).intersection(Polygon(f["pts"])) if not kp.is_empty and kp.area > 1e-6: poly = poly.difference(Polygon([to_face(f, p, o) for p in kp.exterior.coords])) return poly, Polygon(fp), o def fill(poly, W, H): g = fill_grid(poly, W, H); r = fill_greedy(poly, W, H) return g if len(g) >= len(r) else r def fill_grid(poly, W, H): """aligned columns: one u-phase for all rows (tidier arrays)""" best = [] if poly.is_empty: return best minx, miny, maxx, maxy = poly.bounds for vphase in [i*0.05 for i in range(0, int(H/0.05)+1)]: for uphase in [i*0.025 for i in range(0, int((W+GAP)/0.025)+1)]: placed = []; v = miny + vphase while v + H <= maxy + 1e-9: u = minx + uphase - (W+GAP) while u + W <= maxx + 1e-9: if poly.contains(box(u, v, u+W, v+H)): placed.append((u, v)) u += W + GAP v += H + GAP if len(placed) > len(best): best = placed return [(u, v, W, H) for (u, v) in best] def fill_greedy(poly, W, H): """rows parallel to the eave, greedy left->right; try several row/column phase offsets""" best = [] if poly.is_empty: return best minx, miny, maxx, maxy = poly.bounds for vphase in [i*0.05 for i in range(0, int(H/0.05)+1)]: for uphase in [i*0.05 for i in range(0, int(W/0.05)+1)]: placed = [] v = miny + vphase while v + H <= maxy + 1e-9: u = minx + uphase - W row = [] while u + W <= maxx + 1e-9: r = box(u, v, u+W, v+H) if poly.contains(r): row.append((u, v)); u += W + GAP else: u += 0.05 placed += row v += H + GAP if len(placed) > len(best): best = placed return [(u, v, W, H) for (u, v) in best] def layout(face_ids, clear=None, keepouts=(), mixed=False, vents=True): mods = [] for fid in face_ids: f = FACES[fid] poly, _, o = usable(f, clear, keepouts, vents) land = fill(poly, MOD_L, MOD_W); port = fill(poly, MOD_W, MOD_L) pick = land if len(land) >= len(port) else port if mixed: # second pass: fill what is left with the other orientation best = list(pick) for first, other in ((land, (MOD_W, MOD_L)), (port, (MOD_L, MOD_W))): rest = poly for (u, v, W, H) in first: rest = rest.difference(box(u-GAP, v-GAP, u+W+GAP, v+H+GAP)) extra = fill(rest, *other) if not rest.is_empty else [] if len(first) + len(extra) > len(best): best = list(first) + extra pick = best for k, (u, v, W, H) in enumerate(pick): orient = "landscape" if abs(W - MOD_L) < 1e-9 else "portrait" cx, cy, cz = from_face(f, (u+W/2, v+H/2), o) corners = [from_face(f, c, o) for c in [(u,v),(u+W,v),(u+W,v+H),(u,v+H)]] long_bearing = (f["az"] + 90) % 180 if orient == "landscape" else f["az"] % 180 mods.append(dict(face=fid, orientation=orient, long_bearing=long_bearing, cx=cx, cy=cy, cz=cz, u=u, v=v, W=W, H=H, corners=corners)) for i, m in enumerate(mods, 1): m["id"] = f"M{i:02d}" return mods STREET_AZ = 90 # PVWatts v8, El Paso TMY, 22.6 deg tilt, 14% losses: kWh per kWdc per year by downhill azimuth YIELD = {180: 1899.5, 90: 1626.4, 270: 1589.4, 0: 1236.9} # sample household use, Dec..Nov (invented, shaped like a summer-peaking El Paso home) USE = [650, 650, 550, 650, 900, 950, 1050, 1700, 2050, 1800, 1200, 900] SIZE_TO = 0.88 def street_class(az): diff = abs((az - STREET_AZ + 180) % 360 - 180) return "faces street" if diff <= 45 else ("seen at an angle" if diff <= 100 else "not visible from street") if __name__ == "__main__": STRIPS = [[T(p) for p in k] for k in [[(5.8,0.0),(9.4,0.0),(9.4,0.92),(5.8,0.92)], [(9.4,0.0),(13.0,0.0),(13.0,0.92),(9.4,0.92)]]] designs = { "A": (["U-S1","U-S2","U-E","U-W","U-WH","L-SE"], None, (), False, True), "B": (["U-S1","U-S2","U-W","U-WH","L-SE"], None, (), False, True), "C": (["U-S1","U-S2","U-E","U-W","U-WH","L-SE","G-S","G-E"], CLEAR_TIGHT, (), True, True), "D": (["U-S1","U-S2","U-E","U-W","U-WH","L-SE","G-S","G-E","G-N","U-N","U-EH"], CLEAR_TIGHT, STRIPS, True, False), "E": (["U-S1","U-S2","U-E","U-W","U-WH","L-SE"], CLEAR_TIGHT, (), True, True), # C without the garage roof: sized to the bill } out = {} import os cache = json.load(open("shade_cache.json")) if os.path.exists("shade_cache.json") else {} import os cache = json.load(open("shade_cache.json")) if os.path.exists("shade_cache.json") else {} from shapely.ops import unary_union roof_plan = unary_union([Polygon(f["pts"]) for f in FACES.values()] + [Polygon(FRONT_ROOF)]).area meta = {} def kept(mods): return [m for m in mods if (cache.get(f"{m['face']}|{m['cx']:.2f}|{m['cy']:.2f}") or 0) <= 20] for name, (faces, clear, keep, mixed, vents) in designs.items(): clear = dict(clear or CLEAR) mods = layout(faces, clear, keep, mixed, vents) pct = 100*len(kept(mods))*MOD_L*MOD_W*COSP/roof_plan if pct > 33: # IRC R324.6.2: over 33% of plan roof area -> 36 in ridge setback clear["ridge"] = 0.914; mods = layout(faces, clear, keep, mixed, vents) meta[name] = dict(faces=faces, clear=clear, keepouts=[list(map(list, k)) for k in keep], mixed=mixed, vents=vents) for m in mods: m["shade_pct"] = cache.get(f"{m['face']}|{m['cx']:.2f}|{m['cy']:.2f}") dropped = [m for m in mods if (m["shade_pct"] or 0) > 20] mods = [m for m in mods if (m["shade_pct"] or 0) <= 20] if name == "E": # sized to use: keep the best-yielding modules until a year's output reaches SIZE_TO of the sample use yld = lambda m: YIELD[FACES[m["face"]]["az"]]*(1-0.85*(m["shade_pct"] or 0)/100) keep, tot = [], 0.0 for m in sorted(mods, key=lambda m: -yld(m)): e = MOD_P/1000*yld(m) if tot + e > SIZE_TO*sum(USE): continue keep.append(m); tot += e mods = [m for m in mods if m in keep] for i, m in enumerate(mods, 1): m["id"] = f"M{i:02d}" print(name, "dropped for shading >20%:", [(m["face"], m["shade_pct"]) for m in dropped]) out[name] = mods per = {} for m in mods: per[m["face"]] = per.get(m["face"], 0) + 1 print(name, len(mods), round(len(mods)*MOD_P/1000, 2), "kW", per) json.dump({"faces": FACES, "vents": VENTS, "front": FRONT_ROOF, "patio": PATIO, "designs": out, "meta": meta, "module": [MOD_L, MOD_W, MOD_P], "pitch": PITCH}, open("model.json", "w"), indent=1) for name, mods in out.items(): with open(f"modules_design_{name}.csv", "w", newline="") as fh: w = csv.writer(fh) w.writerow(["id","face","orientation","long_side_bearing_deg","centre_east_m","centre_north_m","centre_height_m", "pitch_deg","azimuth_deg","pitch_status","height_status","geometry_status","size_mm","watts","street_view","beam_shading_pct_indicative"]) for m in mods: f = FACES[m["face"]] w.writerow([m["id"], m["face"], m["orientation"], m["long_bearing"], f"{m['cx']:.3f}", f"{m['cy']:.3f}", f"{m['cz']:.3f}", PITCH, f["az"], "ASSUMED", "ASSUMED", "INVENTED", f"{int(MOD_L*1000)} x {int(MOD_W*1000)}", MOD_P, street_class(f["az"]), m["shade_pct"]])