% Section 5.1, L. Vandenberghe, S. Boyd, and A. El Gamal
% "Optimizing dominant time constant in RC circuits"
% Original by Lieven Vandenberghe
% Adapted for CVX by Joelle Skaf - 11/25/05
% Modified by Michael Grant - 3/8/06
%
% we consider the problem of sizing an interconnect wire that connects
% a voltage source and conductance G to a capacitive load C. We divide the
% wire into n segments of length li, and width xi, i = 1,...,n, which is
% constrained as 0 <= xi <= Wmax. The total area of the interconnect wire
% is therefore sum(li*xi). We use a pi-model of each wire segment, with
% capacitors beta_i*xi and conductance alpha_i*xi.
% To minimize the total area subject to the width bound and a bound Tmax on
% dominant time constant, we solve the SDP
%               minimize        sum_{i=1}^20 xi*li
%                   s.t.        Tmax*G(x) - C(x) >= 0
%                               0 <= xi <= Wmax

%
% Circuit parameters
%

n=21;          % number of nodes; n-1 is number of segments in the wire
m=n-1;         % number of segments
beta = 0.5;    % segment has two capacitances beta*xi
alpha = 1;     % conductance is alpha*xi per segment
Go = 1;        % driver conductance
Co = 10;       % load capacitance
wmax = 1.0;    % upper bound on x

%
% Construct the capacitance and conductance matrices
%   C(x) = C0 + x1 * C1 + x2 * C2 + ... + xn * Cn
%   G(x) = G0 + x1 * G1 + x2 * G2 + ... + xn * Gn
% We assemble the coefficient matrices together as follows:
%   CC = [ C0(:) C1(:) C2(:) ... Cn(:) ]
%   GG = [ G0(:) G1(:) G2(:) ... Gn(:) ]
%

CC = zeros(n,n,m+1);
GG = zeros(n,n,m+1);
% constant terms
CC(n,n,1) = Co;
GG(1,1,1) = Go;
% segment values
for i = 1 : n - 1,
    CC(i,  i,  i+1) = beta;
    CC(i+1,i+1,i+1) = beta;
    GG(i,  i,  i+1) = +alpha;
    GG(i+1,i,  i+1) = -alpha;
    GG(i,  i+1,i+1) = -alpha;
    GG(i+1,i+1,i+1) = +alpha;
end
% Reshape for easy Matlab use
CC = reshape(CC,n*n,m+1);
GG = reshape(GG,n*n,m+1);

%
% Compute points the tradeoff curve, and the four sample points
%

npts    = 50;
delays  = linspace(400,2000,npts);
xdelays = [ 370, 400, 600, 1800 ];
xnpts   = length(xdelays);
areas   = zeros(1,npts);
xareas  = zeros(1,xnpts);
sizes   = zeros(m,xnpts);
for i = 1 : npts + xnpts,

    if i > npts,
        xi = i - npts;
        delay = xdelays(xi);
        disp( sprintf( 'Particular solution %d of %d (Tmax = %g)', xi, xnpts, delay ) );
    else,
        delay = delays(i);
        disp( sprintf( 'Point %d of %d on the tradeoff curve (Tmax = %g)', i, npts, delay ) );
    end

    %
    % Construct and solve the convex model
    %

    cvx_begin sdp quiet
        variable x(m)
        variable G(n,n) symmetric
        variable C(n,n) symmetric
        minimize( sum(x) )
        G == reshape( GG * [ 1 ; x ], n, n );
        C == reshape( CC * [ 1 ; x ], n, n );
        delay * G - C >= 0;
        0 <= x <= wmax;
    cvx_end

    if i <= npts,
        areas(i) = cvx_optval;
    else,
        xareas(xi) = cvx_optval;
        sizes(:,xi) = x;

        %
        % Plot the step response
        %

        figure(xi+2);
        A = -inv(C)*G;
        B = -A*ones(n,1);
        T = linspace(0,2000,1000);
        Y = simple_step(A,B,T(2),length(T));
        hold off; plot(T,Y,'-');  hold on;
        xlabel('time');
        ylabel('v');

        % compute threshold delay, elmore delay, dominant time constant
        tthres=T(min(find(Y(n,:)>0.5)));
        GinvC=full(G\C);
        tdom=max(eig(GinvC));
        telm=max(sum(GinvC'));
        plot(tdom*[1;1], [0;1], '--', telm*[1;1], [0;1],'--', ...
             tthres*[1;1], [0;1], '--');
        text(tdom,0.01,'d');
        text(telm,0.01,'e');
        text(tthres,0.01,'t');
        title(sprintf('Step responses at the 21 nodes for solution (%d), Tmax=%g', xi, delay ));
        axis([0,2000,-0.01,1]);

    end

end

%
% Plot the tradeoff curve
%

figure(1)
ind = isfinite(areas);
plot(areas(ind), delays(ind));
xlabel('Area');
ylabel('Tdom');
title('Area-delay tradeoff curve');
hold on
for k = 1 : xnpts,
    text( xareas(k), xdelays(k), sprintf( '(%d)', k ) );
end


%
% Draw wires for the four solutions
%

figure(2)
m2 = 2 * m;
x1 = reshape( [ 1 : m ; 1 : m ], 1, m2 );
x2 = x1( 1, end : -1 : 1 );
y  = [ - 0.5 * sizes(x1,:) ; + 0.5 * sizes(x2,:) ; - 0.5 * sizes(1,:)  ];
x1 = reshape( [ 0 : m - 1 ; 1 : m ], m2, 1 );
x2 = x1( end : -1 : 1, 1 );
x  = [ x1 ; x2 ; 0 ];
h = fill( x, y, ones(4*m+1,1)*[0.9,0.8,0.7,0.6] );
hold on
h2 = plot( x, y, '-' );
axis([ -0.1, m + 0.1, min(y(:))-0.1, max(y(:))+0.1 ]);
colormap(gray);
caxis([-1,1]);
title('Solutions at points on the tradeoff curve');
legends = {};
for k = 1 : xnpts,
    set( h(k), 'EdgeColor', get( h2(k), 'Color' ) );
    legends{k} = sprintf( 'Tmax=%g', xdelays(k) );
end
legend(legends{:},'Location','southeast');
Point 1 of 50 on the tradeoff curve (Tmax = 400)
Point 2 of 50 on the tradeoff curve (Tmax = 432.653)
Point 3 of 50 on the tradeoff curve (Tmax = 465.306)
Point 4 of 50 on the tradeoff curve (Tmax = 497.959)
Point 5 of 50 on the tradeoff curve (Tmax = 530.612)
Point 6 of 50 on the tradeoff curve (Tmax = 563.265)
Point 7 of 50 on the tradeoff curve (Tmax = 595.918)
Point 8 of 50 on the tradeoff curve (Tmax = 628.571)
Point 9 of 50 on the tradeoff curve (Tmax = 661.224)
Point 10 of 50 on the tradeoff curve (Tmax = 693.878)
Point 11 of 50 on the tradeoff curve (Tmax = 726.531)
Point 12 of 50 on the tradeoff curve (Tmax = 759.184)
Point 13 of 50 on the tradeoff curve (Tmax = 791.837)
Point 14 of 50 on the tradeoff curve (Tmax = 824.49)
Point 15 of 50 on the tradeoff curve (Tmax = 857.143)
Point 16 of 50 on the tradeoff curve (Tmax = 889.796)
Point 17 of 50 on the tradeoff curve (Tmax = 922.449)
Point 18 of 50 on the tradeoff curve (Tmax = 955.102)
Point 19 of 50 on the tradeoff curve (Tmax = 987.755)
Point 20 of 50 on the tradeoff curve (Tmax = 1020.41)
Point 21 of 50 on the tradeoff curve (Tmax = 1053.06)
Point 22 of 50 on the tradeoff curve (Tmax = 1085.71)
Point 23 of 50 on the tradeoff curve (Tmax = 1118.37)
Point 24 of 50 on the tradeoff curve (Tmax = 1151.02)
Point 25 of 50 on the tradeoff curve (Tmax = 1183.67)
Point 26 of 50 on the tradeoff curve (Tmax = 1216.33)
Point 27 of 50 on the tradeoff curve (Tmax = 1248.98)
Point 28 of 50 on the tradeoff curve (Tmax = 1281.63)
Point 29 of 50 on the tradeoff curve (Tmax = 1314.29)
Point 30 of 50 on the tradeoff curve (Tmax = 1346.94)
Point 31 of 50 on the tradeoff curve (Tmax = 1379.59)
Point 32 of 50 on the tradeoff curve (Tmax = 1412.24)
Point 33 of 50 on the tradeoff curve (Tmax = 1444.9)
Point 34 of 50 on the tradeoff curve (Tmax = 1477.55)
Point 35 of 50 on the tradeoff curve (Tmax = 1510.2)
Point 36 of 50 on the tradeoff curve (Tmax = 1542.86)
Point 37 of 50 on the tradeoff curve (Tmax = 1575.51)
Point 38 of 50 on the tradeoff curve (Tmax = 1608.16)
Point 39 of 50 on the tradeoff curve (Tmax = 1640.82)
Point 40 of 50 on the tradeoff curve (Tmax = 1673.47)
Point 41 of 50 on the tradeoff curve (Tmax = 1706.12)
Point 42 of 50 on the tradeoff curve (Tmax = 1738.78)
Point 43 of 50 on the tradeoff curve (Tmax = 1771.43)
Point 44 of 50 on the tradeoff curve (Tmax = 1804.08)
Point 45 of 50 on the tradeoff curve (Tmax = 1836.73)
Point 46 of 50 on the tradeoff curve (Tmax = 1869.39)
Point 47 of 50 on the tradeoff curve (Tmax = 1902.04)
Point 48 of 50 on the tradeoff curve (Tmax = 1934.69)
Point 49 of 50 on the tradeoff curve (Tmax = 1967.35)
Point 50 of 50 on the tradeoff curve (Tmax = 2000)
Particular solution 1 of 4 (Tmax = 370)
Particular solution 2 of 4 (Tmax = 400)
Particular solution 3 of 4 (Tmax = 600)
Particular solution 4 of 4 (Tmax = 1800)