function [Er,Ar,Br,Cr] = IRKA_rail(k,r,istest)
% Computes a loclly H2-optimal reduced order model of order r for
% the selective cooling of Steel profiles application described in
% [1,2,3] via the tangential IRKA method.
%
% Usage: IRKA_rail(k,r,istest)
%
% Inputs:
%
% k           refinement level of the model to use
%             (1-4, i.e. 1357-79841Dofs)
%             (optinal, defaults to 1)
%
% r           desired dimension of the reduced order model
%             (optional, defaults to 10)
%
% istest      flag to determine whether this demo runs as a CI test or
%             interactive demo
%             (optional, defaults to 0, i.e. interactive demo)
%
% References:
% [1] J. Saak, Effiziente numerische Lösung eines
%     Optimalsteuerungsproblems für die Abkühlung von Stahlprofilen,
%     Diplomarbeit, Fachbereich 3/Mathematik und Informatik, Universität
%     Bremen, D-28334 Bremen (Sep. 2003).
%
% [2] P. Benner, J. Saak, A semi-discretized heat transfer model for
%     optimal cooling of steel profiles, in: P. Benner, V. Mehrmann, D.
%     Sorensen (Eds.), Dimension Reduction of Large-Scale Systems, Vol. 45
%     of Lect. Notes Comput. Sci. Eng., Springer-Verlag, Berlin/Heidelberg,
%     Germany, 2005, pp. 353–356. https://doi.org/10.1007/3-540-27909-1_19.
%
% [3] J. Saak, Efficient numerical solution of large scale algebraic matrix
%     equations in PDE control and model order reduction, Dissertation,
%     Technische Universität Chemnitz, Chemnitz, Germany (Jul. 2009).
%     URL http://nbn-resolving.de/urn:nbn:de:bsz:ch1-200901642
%
% [4] S. Gugercin, A. C. Antoulas, C. Beattie, H2 model reduction
%     for large-scale linear dynamical systems, SIAM J. Matrix
%     Anal. Appl. 30 (2) (2008) pp. 609–638.
%     https://doi.org/10.1137/060666123.

%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, see <http://www.gnu.org/licenses/>.
%
% Copyright (C) Jens Saak, Martin Koehler, Peter Benner and others
%               2009-2020
if nargin<1
    k=1;
end
eqn = getrail(k);
if nargin<2
    opts.irka.r = 20;
else
    opts.irka.r = r;
end

if nargin < 3
    istest = 0;
end
opts.irka.maxiter =20;
opts.irka.shift_tol = 1e-3;
opts.irka.h2_tol = 1e-6;
if istest
    opts.irka.info = 1;
else
    opts.irka.info = 2;
end
opts.irka.init = 'logspace';

[Er,Ar,Br,Cr] = mess_tangential_irka(eqn.E_,eqn.A_,eqn.B,eqn.C,opts);

if istest
    [~, ID] = lastwarn;
    lastwarn('');
    if strcmp(ID,'MESS:IRKA:convergence')
        error('IRKA converged unexpectedly slow');
    end
end
IRKA step   1, rel. chg. shifts = 1.010139e+01 , rel. H2-norm chg. ROM = 1.000000e+00
Warning: IRKA step 2 : 1 non-stable reduced eigenvalues have been flipped.
 
IRKA step   2, rel. chg. shifts = 8.189732e+00 , rel. H2-norm chg. ROM = Inf
Warning: IRKA step 3 : 2 non-stable reduced eigenvalues have been flipped.
 
IRKA step   3, rel. chg. shifts = 4.215028e+00 , rel. H2-norm chg. ROM = Inf
Warning: IRKA step 4 : 3 non-stable reduced eigenvalues have been flipped.
 
IRKA step   4, rel. chg. shifts = 1.137401e+00 , rel. H2-norm chg. ROM = Inf
Warning: IRKA step 5 : 1 non-stable reduced eigenvalues have been flipped.
 
IRKA step   5, rel. chg. shifts = 1.011476e+00 , rel. H2-norm chg. ROM = Inf
Warning: IRKA step 6 : 1 non-stable reduced eigenvalues have been flipped.
 
IRKA step   6, rel. chg. shifts = 1.292713e+00 , rel. H2-norm chg. ROM = Inf
IRKA step   7, rel. chg. shifts = 2.267604e+00 , rel. H2-norm chg. ROM = 9.033771e-01
Warning: IRKA step 8 : 1 non-stable reduced eigenvalues have been flipped.
 
IRKA step   8, rel. chg. shifts = 1.531156e+00 , rel. H2-norm chg. ROM = Inf
Warning: IRKA step 9 : 1 non-stable reduced eigenvalues have been flipped.
 
IRKA step   9, rel. chg. shifts = 2.489504e-01 , rel. H2-norm chg. ROM = Inf
Warning: IRKA step 10 : 1 non-stable reduced eigenvalues have been flipped.
 
IRKA step  10, rel. chg. shifts = 2.082839e-01 , rel. H2-norm chg. ROM = Inf
IRKA step  11, rel. chg. shifts = 1.116899e-01 , rel. H2-norm chg. ROM = 3.767657e-01
IRKA step  12, rel. chg. shifts = 7.394137e-02 , rel. H2-norm chg. ROM = 3.710620e-02
IRKA step  13, rel. chg. shifts = 1.454364e-02 , rel. H2-norm chg. ROM = 1.654871e-02
IRKA step  14, rel. chg. shifts = 1.680084e+00 , rel. H2-norm chg. ROM = 9.279420e-03
IRKA step  15, rel. chg. shifts = 1.412799e-02 , rel. H2-norm chg. ROM = 6.742917e-03
IRKA step  16, rel. chg. shifts = 1.631372e+00 , rel. H2-norm chg. ROM = 5.549769e-03
IRKA step  17, rel. chg. shifts = 1.601840e+00 , rel. H2-norm chg. ROM = 5.858310e-03
IRKA step  18, rel. chg. shifts = 1.770883e-02 , rel. H2-norm chg. ROM = 6.032019e-03
IRKA step  19, rel. chg. shifts = 5.441607e-03 , rel. H2-norm chg. ROM = 6.539502e-03
IRKA step  20, rel. chg. shifts = 8.651545e-03 , rel. H2-norm chg. ROM = 7.087094e-03
Warning: IRKA: No convergence in 20 iterations.
 
Computing TFMs of original and reduced order systems and MOR errors
 Step  10 / 100 Step  20 / 100 Step  30 / 100 Step  40 / 100 Step  50 / 100 Step  60 / 100 Step  70 / 100 Step  80 / 100 Step  90 / 100 Step 100 / 100


ans =

  Columns 1 through 13

     1     0     0     0     0     0     0     0     0     0     0     0     0
     0     1     0     0     0     0     0     0     0     0     0     0     0
     0     0     1     0     0     0     0     0     0     0     0     0     0
     0     0     0     1     0     0     0     0     0     0     0     0     0
     0     0     0     0     1     0     0     0     0     0     0     0     0
     0     0     0     0     0     1     0     0     0     0     0     0     0
     0     0     0     0     0     0     1     0     0     0     0     0     0
     0     0     0     0     0     0     0     1     0     0     0     0     0
     0     0     0     0     0     0     0     0     1     0     0     0     0
     0     0     0     0     0     0     0     0     0     1     0     0     0
     0     0     0     0     0     0     0     0     0     0     1     0     0
     0     0     0     0     0     0     0     0     0     0     0     1     0
     0     0     0     0     0     0     0     0     0     0     0     0     1
     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0

  Columns 14 through 20

     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     1     0     0     0     0     0     0
     0     1     0     0     0     0     0
     0     0     1     0     0     0     0
     0     0     0     1     0     0     0
     0     0     0     0     1     0     0
     0     0     0     0     0     1     0
     0     0     0     0     0     0     1