function [ As,Bs,Cs,Ks,nSystems,switchingConditions ] = ExampleTwoRooms( dimFOM, yThreshold)
% ExampleTwoRooms generates a switched system for the two room example of
% [Schulze, Unger 2018], where in the first subsystem the door is closed
% and in the second subsystem the door is open.
%
% Reference:
%  P. Schulze, B. Unger: Model reduction for linear systems with low-rank
%  switching (2018)
%
% Input arguments:
%   dimFOM:     dimension of the subsystems
%   yThreshold: vector with two components for the output dependent
%               switching signal (this parameter is optional)
%
% Output arguments:
%   As:                 cell array of length 2 containing the A matrices
%   Bs:                 cell array of length 2 containing the B matrices
%   Cs:                 cell array of length 2 containing the C matrices
%   Ks:                 2 x 2 cell containing transition matrices between 
%                       the subsystems (all identity matrices)
%   nSystems:           number of subsystems = 2
%   switchingCondition: function for output-dependent switching 
%           switchingCondition(y,mode) = i 
%       with y current output, mode = current mode, and i the new mode
%
% Authors:
%   Philipp Schulze, pschulze@math.tu-berlin.de
%   Benjamin Unger, unger@math.tu-berlin.de
%
% Version:
%   1.0 (January 06, 2018)
% 

    % Assign parameter values
    rho     = [2000 1] ;
    cv      = [1000 700] ;
    k  = [0.015 3] ;
    h   = 100 ;
    L       = [5 0.3 5] ;
    zeta = rho.*cv ;

    % Discretization with finite volumes    
    Ltot    = sum(L) ;
    Ncells  = round(dimFOM*(L/Ltot)) ;
    Ntot    = sum(Ncells) ;
    dxi     = L./Ncells ;
    Es      = cell(2,1) ;
    As      = cell(2,1) ;
    
    % door is closed
    tmpE        = [zeta(2)*dxi(1)*ones(Ncells(1),1); zeta(1)*dxi(2)*ones(Ncells(2),1); zeta(2)*dxi(3)*ones(Ncells(3),1)] ;
    Es{1}       = spdiags(tmpE, 0, Ntot, Ntot) ;
    
    lambdaEff1  = [repmat(k(2)/(2*dxi(1)),Ncells(1)-1,1); 1/(dxi(1)/k(2)+dxi(2)/k(1)); repmat(k(1)/(2*dxi(2)),Ncells(2)-1,1); 1/(dxi(3)/k(2)+dxi(2)/k(1)); repmat(k(2)/(2*dxi(3)),Ncells(3)-1,1)] ;
    tmpA        = 2*[[lambdaEff1;0],[-lambdaEff1(1);-lambdaEff1(2:end)-lambdaEff1(1:end-1);-lambdaEff1(end)-0.25*h],[0;lambdaEff1]] ;
    As{1}       = Es{1}\spdiags(tmpA, [-1,0,1], Ntot, Ntot) ;
    
    % door is open
    tmpE        = [zeta(2)*dxi(1)*ones(Ncells(1),1); zeta(2)*dxi(2)*ones(Ncells(2),1); zeta(2)*dxi(3)*ones(Ncells(3),1)] ;
    Es{2}       = spdiags(tmpE, 0, Ntot, Ntot) ;
    
    lambdaEff2  = [repmat(k(2)/(2*dxi(1)),Ncells(1)-1,1); k(2)/(dxi(1)+dxi(2)); repmat(k(2)/(2*dxi(2)),Ncells(2)-1,1); k(2)/(dxi(2)+dxi(3)); repmat(k(2)/(2*dxi(3)),Ncells(3)-1,1)] ;
    tmpA        = 2*[[lambdaEff2;0],[-lambdaEff2(1);-lambdaEff2(2:end)-lambdaEff2(1:end-1);-lambdaEff2(end)-0.25*h],[0;lambdaEff2]] ;
    As{2}       = Es{2}\spdiags(tmpA, [-1,0,1], Ntot, Ntot) ;
    
    % input and output matrices
    B           = zeros(Ntot, 1) ;
    B(1)        = 1 ;
    C           = 1/(Ncells(3))*[zeros(1,sum(Ncells(1:2))),ones(1,Ncells(3))] ;
    
    B           = Es{1}\B ;
    Bs          = {B, B} ;
    Cs          = {C, C} ;
    
    % assign remaining variables
    Ks          = {{speye(Ntot) speye(Ntot)},{speye(Ntot) speye(Ntot)}}; 
    nSystems    = 2 ;
    
    switchingConditions = @(y,mode) switchingConditionsTwoRooms(y, mode, yThreshold) ;
end

function [bool, system] = switchingConditionsTwoRooms(y, mode, yThreshold)
% SWITCHINGCONDITIONSTOWROOMS the output dependent switching condition for
% the two-room example
%
% Input arguments:
%   y:          current output of the switched system
%   mode:       current active mode
%   yThreshold: the threshold for the switching condition
%
% Output arguments:
%   bool:       boolean variable that is true if a different mode becomes
%               active
%   system:     the new active mode
%   
    bool = false ;
    system = mode ;

    switch mode
        case 1
            if(y<yThreshold(1))
                bool = true ;
                system = 2 ;
            end
        case 2
            if(y>yThreshold(2))
                bool = true ;
                system = 1 ;
            end
        otherwise
            error('Only two modes!')
    end
end

