"""Indicative beam-shading check: own house (roof + walls) and the 2-storey neighbour to the south (modelled as a copy of this sample house one lot south, 13.8 m). Clear-sky beam weighting, hourly, 1 year.""" import json, math import numpy as np import model as M LAT, LON = 31.76, -106.44 # El Paso, city level D = json.load(open("model.json")) def sun_positions(): out = [] for doy in range(1, 366, 2): g = 2*math.pi/365*(doy-1) decl = 0.006918-0.399912*math.cos(g)+0.070257*math.sin(g)-0.006758*math.cos(2*g)+0.000907*math.sin(2*g)-0.002697*math.cos(3*g)+0.00148*math.sin(3*g) for hh in np.arange(5, 20, 0.5): # apparent solar time ha = math.radians(15*(hh-12)) la = math.radians(LAT) cz = math.sin(la)*math.sin(decl)+math.cos(la)*math.cos(decl)*math.cos(ha) alt = math.asin(max(-1, min(1, cz))) if alt < math.radians(3): continue az = math.atan2(math.sin(ha), math.cos(ha)*math.sin(la)-math.tan(decl)*math.cos(la)) + math.pi # 0 = north am = 1/max(cz, 0.05) dni = 1353*0.7**(am**0.678) s = np.array([math.sin(az)*math.cos(alt), math.cos(az)*math.cos(alt), math.sin(alt)]) out.append((s, dni)) return out def tris(): T = [] for dy in (0.0, -13.8): for fid, f in M.FACES.items(): org = M.origin_of(f) P = [np.array(M.from_face(f, M.to_face(f, p, org), org)) + [0, dy, 0] for p in f["pts"]] for i in range(1, len(P)-1): T.append((P[0], P[i], P[i+1])) # walls of the upper storey (eave line down to ground) for the wing and block walls = [tuple(M.T(p) for p in w) for w in [((5.8,0),(13.0,0)), ((13.0,0),(13.0,4.3)), ((13.0,4.3),(17.2,4.3)), ((17.2,4.3),(17.2,11.5)), ((5.8,0),(5.8,4.3)), ((3.2,4.3),(5.8,4.3))]] for (a, b) in walls: p1 = np.array([a[0], a[1]+dy, 0]); p2 = np.array([b[0], b[1]+dy, 0]) p3 = np.array([b[0], b[1]+dy, M.UPPER_EAVE_H]); p4 = np.array([a[0], a[1]+dy, M.UPPER_EAVE_H]) T += [(p1, p2, p3), (p1, p3, p4)] if dy != 0: # neighbour's own lower rear roof too f = M.FACES["L-SE"]; org = M.origin_of(f) P = [np.array(M.from_face(f, M.to_face(f, p, org), org)) + [0, dy, 0] for p in f["pts"]] T += [(P[0], P[1], P[2]), (P[0], P[2], P[3])] return T def prep(T): A = np.array([t[0] for t in T]); B = np.array([t[1] for t in T]); C = np.array([t[2] for t in T]) return A, B-A, C-A def hitv(o, d, P): A, E1, E2 = P p = np.cross(d, E2); det = np.einsum('ij,ij->i', E1, p) ok = np.abs(det) > 1e-9; inv = np.where(ok, 1/np.where(ok, det, 1), 0) t = o - A; u = np.einsum('ij,ij->i', t, p)*inv q = np.cross(t, E1); v = (q @ d)*inv; dist = np.einsum('ij,ij->i', E2, q)*inv return bool(np.any(ok & (u >= 0) & (u <= 1) & (v >= 0) & (u+v <= 1) & (dist > 0.05))) def hit(o, d, T): eps = 1e-7 for a, b, c in T: e1, e2 = b-a, c-a; p = np.cross(d, e2); det = e1 @ p if abs(det) < eps: continue inv = 1/det; t = o-a; u = (t @ p)*inv if u < 0 or u > 1: continue q = np.cross(t, e1); v = (d @ q)*inv if v < 0 or u+v > 1: continue if (e2 @ q)*inv > 0.05: return True return False def run_mods(mods): S = sun_positions(); P = prep(tris()); res = {} for m in mods: f = M.FACES[m["face"]] a = math.radians(f["az"]); p = math.radians(M.PITCH) n = np.array([math.sin(a)*math.sin(p), math.cos(a)*math.sin(p), math.cos(p)]) C = np.array(m["corners"]); cen = C.mean(axis=0) pts = [cen] + [cen + 0.8*(c-cen) for c in C] pts = [q + n*0.08 for q in pts] tot = sh = 0.0 for s, dni in S: ci = n @ s if ci <= 0: continue w = dni*ci; tot += w frac = sum(hitv(q, s, P) for q in pts)/len(pts) sh += w*frac res[f"{m['face']}|{m['cx']:.2f}|{m['cy']:.2f}"] = round(100*sh/tot, 1) if tot else 0 return res if __name__ == "__main__": import sys # model.json holds the kept modules; recompute the full candidate layouts so dropped ones get values too mods = {} for name, meta in D["meta"].items(): clear = meta["clear"]; keep = [tuple(map(tuple, k)) for k in meta["keepouts"]] for m in M.layout(meta["faces"], clear, keep, meta.get("mixed", False), meta.get("vents", True)): mods[f"{m['face']}|{m['cx']:.2f}|{m['cy']:.2f}"] = m old = json.load(open("shade_cache.json")) todo = [m for k, m in mods.items() if k not in old] res = dict(old); res.update(run_mods(todo)) json.dump(res, open("shade_cache.json", "w"), indent=1); print(len(res), res)