from Simulation import Simulation

def build_trace_with_split(sim: 'Simulation', 
                           N=20, 
                           Rseg=0.05, 
                           Lseg=8e-9, 
                           Csh0=1e-12, 
                           Cshi=1e-14,
                           Rsrc=10.0, 
                           Rs_gnd=120.0, 
                           Rload=1e5,
                           Rsplit=0.3, 
                           Lsplit=50e-9, 
                           Cgap=100e-12,
                           Rsplit_parallel=0.1):
    
    # Nodes. Vs, V0..V{N}, GNDL, GNDR
    sim.add_node("Vs")
    for k in range(N+1):
        sim.add_node(f"V{k}")
    sim.add_node("GNDL"); sim.add_node("GNDR")

    # Source
    sim.add_branch("Vs","V0", R=Rsrc)
    sim.add_branch("Vs","GNDL", R=Rs_gnd)

    # series ladder
    for k in range(N):
        Lk = Lseg if (k==0 or k==N-1) else (Lseg*(N/(N-1)))
        sim.add_branch(f"V{k}", f"V{k+1}", R=Rseg, L=Lk)

    # Shunt caps to local grounds
    sim.add_shunt_C("V0","GNDL", Csh0)
    for k in range(1, N):
        g = "GNDL" if k <= N//2 else "GNDR"
        sim.add_shunt_C(f"V{k}", g, Cshi)
    sim.add_shunt_C(f"V{N}", "GNDR", Csh0)

    # Load
    sim.add_branch(f"V{N}", "GNDR", R=Rload)

    sim.add_branch("GNDL","GNDR", R=Rsplit, L=Lsplit)
    sim.add_shunt_C("GNDL","GNDR", Cgap)
    
    sim.add_branch("GNDL","GNDR", R=Rsplit_parallel)