#!/usr/bin/env python3
"""Recalculate the worked numbers in the material data sheet article.

Q1  - microstrip Z0 sensitivity to Dk and pressed height (Hammerstad-Jensen, t=0, no mask)
Q4  - dielectric loss estimate alpha_d = 2.3 * f[GHz] * Df * sqrt(Dk) dB/in
Q5  - glass-weave skew bound: delta_t = (sqrt(Dk1) - sqrt(Dk2)) / c
Q7  - pressed dielectric estimate: h = t_PP - tCu1(1-R1) - tCu2(1-R2)
Q12 - Z-axis growth over reflow range vs. copper barrel
Q18 - MCPCB dielectric thermal resistance R = t / (k * A)

Zero-thickness, uncoated, lossless geometry - screening numbers, not a field solver.
"""
import math

C_MM_PS = 0.299792458  # speed of light in mm/ps


def microstrip_eps_eff(er, w, h):
    return (er + 1) / 2 + (er - 1) / 2 * (1 + 12 * h / w) ** -0.5


def microstrip_z0(er, w, h):
    """Hammerstad-Jensen forward equation, t = 0."""
    ee = microstrip_eps_eff(er, w, h)
    u = w / h
    return 120 * math.pi / (math.sqrt(ee) * (u + 1.393 + 0.667 * math.log(u + 1.444)))


def solve_width(er, h, z_target):
    """Bisection on the forward equation."""
    lo, hi = 0.001 * h, 100 * h
    for _ in range(80):
        mid = (lo + hi) / 2
        if microstrip_z0(er, mid, h) > z_target:
            lo = mid
        else:
            hi = mid
    return (lo + hi) / 2


# --- Q1: 50 ohm microstrip, Dk 4.2, h = 0.20 mm --------------------------------
print("Q1  Dk 4.2, h 0.20 mm -> synthesized W = {:.3f} mm".format(solve_width(4.2, 0.20, 50.0)))
print("    (sensitivity table below evaluates W = 0.400 mm)")
W = 0.400
for label, h, dk in [
    ("design basis", 0.200, 4.2), ("lower Dk only", 0.200, 3.8),
    ("thinner pressed only", 0.175, 4.2), ("both", 0.175, 3.8)]:
    print("    {:<22s} h={:.3f}  Dk={:.1f}  Z0={:5.1f} ohm  eps_eff={:.2f}"
          .format(label, h, dk, microstrip_z0(dk, W, h), microstrip_eps_eff(dk, W, h)))

# --- Q4: dielectric loss at 14 GHz, Dk 3.7 ------------------------------------
print("Q4  alpha_d = 2.3 f Df sqrt(Dk)  @14 GHz, Dk 3.7:")
for df in (0.020, 0.010, 0.004):
    a = 2.3 * 14 * df * math.sqrt(3.7)
    print("    Df={:.3f} -> {:.2f} dB/in, {:.1f} dB over 10 in".format(df, a, 10 * a))

# --- Q5: weave skew bound, local Dk 3.8 vs 3.5 --------------------------------
dt = (math.sqrt(3.8) - math.sqrt(3.5)) / C_MM_PS
print("Q5  delta_t = {:.3f} ps/mm -> {:.1f} ps over 254 mm".format(dt, dt * 254))
for deg in (7, 3):
    print("    weave pitch crossings per pitch at {} deg: {:.1f}p".format(deg, 1 / math.tan(math.radians(deg))))

# --- Q7: pressed dielectric estimate -------------------------------------------
print("Q7  h_pressed = tPP - tCu1(1-R1) - tCu2(1-R2), tPP=100 um:")
for l1, l2 in [((35, 0.8), (35, 0.3)), ((105, 0.3), (35, 0.8))]:
    fill = l1[0] * (1 - l1[1]) + l2[0] * (1 - l2[1])
    print("    tCu {:.0f}(R={}) + {:.0f}(R={}) -> fill {:.1f} um, dielectric {:.1f} um"
          .format(l1[0], l1[1], l2[0], l2[1], fill, 100 - fill))

# --- Q12: Z growth, 2.8% of 1.6 mm vs copper barrel 17 ppm/C over 210 C --------
print("Q12 board Z growth 2.8% x 1600 um = {:.1f} um; Cu barrel 17ppm/C x 210C = {:.1f} um"
      .format(0.028 * 1600, 17e-6 * 210 * 1600))

# --- Q18: MCPCB dielectric R = t/(kA), A = 1 cm2 -------------------------------
for name, t_m, k in [("A", 100e-6, 2.0), ("B", 75e-6, 3.0)]:
    print("Q18 dielectric {}: t={:.0f} um k={:.1f} -> R = {:.2f} K/W per cm2"
          .format(name, t_m * 1e6, k, t_m / (k * 1e-4)))
