## Modelica_LinearSystems2.WorkInProgress.Tests.Internal

### Package Content

Name | Description
--- | ---
DesignData | Contains the system matrix A, the input matrix B, the assigned Poles and optionally the ideal feedback matrix
schur | Pole assigment design algorithm for multi input systems
clock |
clock2 |
wrapper_modifyX | Contains a C sub routine of robust pole assignment to modify the eigenvector matrix X according to Kautsky algorithm
modifyX | Contains a C sub routine of robust pole assignment to modify the eigenvector matrix X according to Kautsky algorithm
wrapper_xBase | Compute the eigenvector bases according to Kautsky algorithm
xBase | Compute the eigenvector bases according to Kautsky algorithm
getMatFiles | Returns a vector of files ending with .mat
getMoFiles | Returns a vector of files with *.mo
complexMatrices | Some case studies to complex matrices
getCareMoFiles | Returns a vector of files with care*.mo
multiplyComplexMatrices | Some case studies to complex matrices
multiplyRealMatrices | Some case studies to complex matrices
multiplyRealMatrices2 | Some case studies to complex matrices
multiplyRealMatrices3 | Some case studies to complex matrices
multiplyRealMatrices3cCall | Some case studies to complex matrices
realMatrices | Some case studies to real matrices

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.DesignData

Contains the system matrix A, the input matrix B, the assigned Poles and optionally the ideal feedback matrix

### Information

Extends from Modelica.Icons.Record (Icon for a record).

### Modelica definition

    record DesignData
      "Contains the system matrix A, the input matrix B, the assigned Poles and optionally the ideal feedback matrix"
      import Modelica_LinearSystems2.Math.Complex;
      extends Modelica.Icons.Record;
      Real A[:,size(A,1)] "System matrix";
      Real B[size(A,1),:] "Input matrix";
      Complex assignedPoles[:] "Assigned poles";
      Real K[:,:] "Feedback gain matrix";

    end DesignData;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.schur

Pole assigment design algorithm for multi input systems

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | A[:, :] | [1, 2, 3, 4; 0, 2, 5, 6; 0, ... |
Real | alpha | 0 |

### Outputs

Type | Name | Description
--- | --- | ---
Real | T[:, :] |
Real | Z[:, :] |
Real | alphaReal[size(A, 1)] | Real part of eigenvalue=alphaReal+i*alphaImag
Real | alphaImag[size(A, 1)] | Imaginary part of eigenvalue=(alphaReal+i*alphaImag

### Modelica definition

    encapsulated function schur
      "Pole assigment design algorithm for multi input systems"

      import Modelica_LinearSystems2;
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica;
      import Modelica.Utilities.Streams.print;
      import Modelica_LinearSystems2.Math.Matrices;

      input Real A[:,:]=[1,2,3,4;0,2,5,6;0,0,-2,-1;0,0,0,-3];
      input Real alpha=0;
      output Real T[:,:];
      output Real Z[:,:];
      output Real alphaReal[size(A, 1)]
        "Real part of eigenvalue=alphaReal+i*alphaImag";
      output Real alphaImag[size(A, 1)]
        "Imaginary part of eigenvalue=(alphaReal+i*alphaImag";

    algorithm
      (T,Z,alphaReal,alphaImag) := Matrices.rsf2(A);
      Modelica_LinearSystems2.Math.Matrices.printMatrix(T,6,"T1");
      Modelica_LinearSystems2.Math.Vectors.printVector(alphaReal,6,"ar1");

    // reorder real Schur form according to alpha
       (T,Z,alphaReal,alphaImag) := Matrices.Internal.reorderRSF2(
           T,
           identity(size(A, 1)),
           alphaReal,
           alphaImag,
           alpha);
      Modelica_LinearSystems2.Math.Matrices.printMatrix(T,6,"T2");
      Modelica_LinearSystems2.Math.Vectors.printVector(alphaReal,6,"ar1");
    end schur;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.clock

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | dummy | 0 |

### Outputs

Type | Name | Description
--- | --- | ---
Real | runtime_ms |

### Modelica definition

    function clock

    input Real dummy=0;
    output Real runtime_ms;

    external "C" runtime_ms = clock_ms(dummy);

    end clock;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.clock2

### Outputs

Type | Name | Description
--- | --- | ---
Real | runtime_ms |

### Modelica definition

    function clock2

    output Real runtime_ms;

    external "C" clock_ms(runtime_ms);

    end clock2;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.wrapper_modifyX

Contains a C sub routine of robust pole assignment to modify the eigenvector matrix X according to Kautsky algorithm

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | X_real[:, size(X_real, 1)] |  | Eigenvector matrix, real part
Real | X_imag[size(X_real, 1), size(X_real, 2)] |  | Eigenvector matrix, imaginary part
Integer | n |  | Order of X
Real | S_real[size(X_real, 1), :] |  | Eigenvector bases, real part
Real | S_imag[size(S_real, 1), size(S_real, 2)] |  | Eigenvector bases, imaginary part
Integer | m |  | Rank of the system input matrix B; S_real and S_imag must have n*m columns
Integer | ncp |  | number of complex pairs
Integer | steps |  | Number of iterations

### Outputs

Type | Name | Description
--- | --- | ---
Real | Xm_real[size(X_real, 1), size(X_real, 2)] |
Real | Xm_imag[size(X_imag, 1), size(X_imag, 2)] |

### Modelica definition

    function wrapper_modifyX
      "Contains a C sub routine of robust pole assignment to modify the eigenvector matrix X according to Kautsky algorithm"
      input Real X_real[:,size(X_real, 1)] "Eigenvector matrix, real part";
      input Real X_imag[size(X_real, 1),size(X_real, 2)]
        "Eigenvector matrix, imaginary part";
      input Integer n "Order of X";
      input Real S_real[size(X_real, 1),:] "Eigenvector bases, real part";
      input Real S_imag[size(S_real, 1),size(S_real, 2)]
        "Eigenvector bases, imaginary part";
      input Integer m
        "Rank of the system input matrix B; S_real and S_imag must have n*m columns";
      input Integer ncp "number of complex pairs";
      input Integer steps "Number of iterations";

      output Real Xm_real[size(X_real, 1),size(X_real, 2)]=X_real;
      output Real Xm_imag[size(X_imag, 1),size(X_imag, 2)]=X_imag;

    external"FORTRAN 77" c_inter_modifyX(
        Xm_real,
        Xm_imag,
        n,
        S_real,
        S_imag,
        m,
        ncp,
        steps);

    end wrapper_modifyX;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.modifyX

Contains a C sub routine of robust pole assignment to modify the eigenvector matrix X according to Kautsky algorithm

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Complex | X[:, size(X, 1)] |  | Complex eigenvector matrix
Complex | S[size(X, 1), :] |  | Complex eigenvector matrix
Integer | m |  | Rank of the system input matrix B; S_real and S_imag must have n*m columns
Integer | ncp |  | number of complex pairs
Integer | steps |  | Number of iterations

### Outputs

Type | Name | Description
--- | --- | ---
Complex | Xm[size(X, 1), size(X, 2)] |

### Modelica definition

    function modifyX
      "Contains a C sub routine of robust pole assignment to modify the eigenvector matrix X according to Kautsky algorithm"

      import Modelica_LinearSystems2;
      import Modelica_LinearSystems2.Math.Complex;
      import Re = Modelica_LinearSystems2.Math.Complex.real;
      import Im = Modelica_LinearSystems2.Math.Complex.imag;

      input Complex X[:,size(X,1)] "Complex eigenvector matrix";
      input Complex S[size(X,1),:] "Complex eigenvector matrix";
      input Integer m
        "Rank of the system input matrix B; S_real and S_imag must have n*m columns";
      input Integer ncp "number of complex pairs";
      input Integer steps "Number of iterations";

      output Complex Xm[size(X, 1),size(X, 2)];

    protected
       Complex j=Modelica_LinearSystems2.Math.Complex.j();
       Integer n=size(X,1);
       Real X_real[n,n]=Re(X) "Eigenvector matrix, real part";
       Real X_imag[n,n]=Im(X) "Eigenvector matrix, imaginary part";
       Real S_real[n,m*n]=Re(S) "Eigenvector bases, real part";
       Real S_imag[n,m*n]=Im(S) "Eigenvector bases, imaginary part";

      Real Xm_real[n,n];
      Real Xm_imag[n,n];

      Integer i;
      Integer ii;

    algorithm
      (Xm_real, Xm_imag) :=Modelica_LinearSystems2.WorkInProgress.StateSpace.Internal.wrapper_modifyX_old(
                                                                                       X_real, X_imag, n, S_real, S_imag, m, ncp, steps);
      for i in 1:n loop
        for ii in 1:n loop
          Xm[i,ii] := Complex(Xm_real[i,ii],Xm_imag[i,ii]);
        end for;
      end for;
    end modifyX;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.wrapper_xBase

Compute the eigenvector bases according to Kautsky algorithm

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | A[:, size(A, 1)] |  | Real square system matrix
Real | B[size(A, 1), :] |  | Real input matrix
Real | gamma_real[size(A, 1)] |  | Eigenvalue vector, real part
Real | gamma_imag[size(A, 1)] |  | Eigenvalue vector, imaginary part
Integer | ncp |  | Number of complex pairs of eigenvalues

### Outputs

Type | Name | Description
--- | --- | ---
Real | U0[size(A, 1), size(B, 2)] | U0
Real | Z[size(B, 2), size(B, 2)] | Z
Real | S_real[size(A, 1), size(B, 2)*(size(A, 1) - ncp)] | Eigenvector bases, real part
Real | S_imag[size(A, 1), size(B, 2)*(size(A, 1) - ncp)] | Eigenvector bases, imaginary part
Integer | rankB | Rank of matrix B

### Modelica definition

    function wrapper_xBase
      "Compute the eigenvector bases according to Kautsky algorithm"

      input Real A[:,size(A, 1)] "Real square system matrix";
      input Real B[size(A,1),:] "Real input matrix";
      input Real gamma_real[size(A,1)] "Eigenvalue vector, real part";
      input Real gamma_imag[size(A,1)] "Eigenvalue vector, imaginary part";
      input Integer ncp "Number of complex pairs of eigenvalues";

      output Real U0[size(A,1),size(B,2)] "U0";
      output Real Z[size(B,2),size(B,2)] "Z";
      output Real S_real[size(A,1),size(B,2)*(size(A,1)-ncp)]
        "Eigenvector bases, real part";
      output Real S_imag[size(A,1),size(B,2)*(size(A,1)-ncp)]
        "Eigenvector bases, imaginary part";
      output Integer rankB "Rank of matrix B";

    protected
      Integer n=size(A,1);
      Integer m=size(B,2);

    external"FORTRAN 77" c_inter_xBase(A, B, n, m, gamma_real, gamma_imag, ncp, U0, Z, S_real, S_imag, rankB);

    end wrapper_xBase;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.xBase

Compute the eigenvector bases according to Kautsky algorithm

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | A[:, size(A, 1)] |  | Real square system matrix
Real | B[size(A, 1), :] |  | Real input matrix
Complex | gamma[size(A, 1)] |  | Assigned complex eigenvalues
Integer | ncp |  | Number of complex pairs of eigenvalues

### Outputs

Type | Name | Description
--- | --- | ---
Real | U0[size(A, 1), size(B, 2)] | U0 and Z are the decompositions of B
Real | Z[size(B, 2), size(B, 2)] | Z and U0 are the decompositions of B
Complex | S[size(A, 1), (size(A, 1) - ncp)*size(B, 2)] | Eigenvector bases
Integer | rankB |

### Modelica definition

    function xBase
      "Compute the eigenvector bases according to Kautsky algorithm"
      import Modelica_LinearSystems2;
      import Modelica_LinearSystems2.Math.Complex;
      import Re = Modelica_LinearSystems2.Math.Complex.real;
      import Im = Modelica_LinearSystems2.Math.Complex.imag;

      input Real A[:,size(A,1)] "Real square system matrix";
      input Real B[size(A,1),:] "Real input matrix";
      input Complex gamma[size(A,1)] "Assigned complex eigenvalues";
      input Integer ncp "Number of complex pairs of eigenvalues";

      output Real U0[size(A, 1),size(B, 2)] "U0 and Z are the decompositions of B";
      output Real Z[size(B,2),size(B,2)] "Z and U0 are the decompositions of B";
      output Complex S[size(A,1),(size(A,1)-ncp)*size(B,2)] "Eigenvector bases";
      output Integer rankB;

    protected
       Complex j=Modelica_LinearSystems2.Math.Complex.j();
       Integer n=size(A,1);
       Integer m=size(B,2);

       Real gamma_real[n]=Re(gamma) "Eigenvalue vector, real part";
       Real gamma_imag[n]=Im(gamma) "Eigenvalue vector, imaginary part";
       Real S_real[n,m*(n-ncp)] "Eigenvector bases, real part";
       Real S_imag[n,m*(n-ncp)] "Eigenvector bases, imaginary part";
      Integer i;
      Integer ii;

    algorithm
      (U0, Z, S_real, S_imag, rankB) :=Modelica_LinearSystems2.WorkInProgress.StateSpace.Internal.wrapper_xBase(
                                                                                                 A, B, gamma_real, gamma_imag, ncp);
      assert(m==rankB,"Input matrix B must have full column rank");
      for i in 1:n loop
        for ii in 1:(n-ncp)*m loop
          S[i,ii] := Complex(S_real[i,ii],S_imag[i,ii]);
        end for;
      end for;
    end xBase;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.getMatFiles

Returns a vector of files ending with .mat

### Information

Extends from Modelica.Icons.Function (Icon for a function).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
String | specifier | "data" |
String | directoryName | classDirectory() + "../" + s... |

### Outputs

Type | Name | Description
--- | --- | ---
String | matFiles[:] |
Integer | nrMat |

### Modelica definition

    function getMatFiles "Returns a vector of files ending with .mat"
      extends Modelica.Icons.Function;
      input String specifier="data";
      input String directoryName = classDirectory()+ "../" + specifier;
      output String matFiles[:];
      output Integer nrMat;
    protected
      Integer nEntries=Modelica.Utilities.Internal.FileSystem.getNumberOfFiles(directoryName);
      String files[nEntries];
      String files2[nEntries];
      Integer nrDir=nEntries;
      Integer i;
    algorithm
      files := Modelica.Utilities.Internal.FileSystem.readDirectory(directoryName, nEntries);
      // count mat-files
      nrMat := 0;
      for i in 1:nrDir loop
        if Modelica.Utilities.Strings.find(files[i], ".mat", 1, false)<>0 then
          nrMat := nrMat + 1;
          files2[nrMat] := files[i];
        end if;
      end for;
      matFiles := files2[1:nrMat];

    end getMatFiles;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.getMoFiles

Returns a vector of files with *.mo

### Information

Extends from Modelica.Icons.Function (Icon for a function).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
String | specifier | "data" |
String | directoryName | classDirectory() + "../" + s... |

### Outputs

Type | Name | Description
--- | --- | ---
String | moFiles[:] |
Integer | nrMat |

### Modelica definition

    function getMoFiles "Returns a vector of files with *.mo"
      extends Modelica.Icons.Function;
      input String specifier="data";
      input String directoryName = classDirectory()+ "../" + specifier;
      output String moFiles[:];
      output Integer nrMat;
    protected
      Integer nEntries=Modelica.Utilities.Internal.FileSystem.getNumberOfFiles(directoryName);
      String files[nEntries];
      String files2[nEntries];
      Integer nrDir=nEntries;
      Integer i;
    algorithm
      files := Modelica.Utilities.Internal.FileSystem.readDirectory(directoryName, nEntries);
      // count mo-files
      nrMat := 0;
      for i in 1:nrDir loop
        if Modelica.Utilities.Strings.find(files[i], specifier, 1, false)<>0 and Modelica.Utilities.Strings.find(files[i], ".mo", 1, false)<>0 then
          nrMat := nrMat + 1;
          files2[nrMat] := files[i];
        end if;
      end for;
      moFiles := files2[1:nrMat];

    end getMoFiles;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.complexMatrices

Some case studies to complex matrices

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Integer | n | 200 |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function complexMatrices "Some case studies to complex matrices"
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2;

      input Integer n=200;
      output Boolean ok;
    protected
      Complex C1[n,n]=fill(Complex(1,1),n,n);
      Complex C2[n,n];

    algorithm
      C2 := C1;
      ok := true;
    end complexMatrices;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.getCareMoFiles

Returns a vector of files with care*.mo

### Information

Extends from Modelica.Icons.Function (Icon for a function).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
String | directoryName | classDirectory() + "../care" |

### Outputs

Type | Name | Description
--- | --- | ---
String | matFiles[:] |
Integer | nrMat |

### Modelica definition

    function getCareMoFiles "Returns a vector of files with care*.mo"
      extends Modelica.Icons.Function;
      input String directoryName = classDirectory()+ "../care";
      output String matFiles[:];
      output Integer nrMat;
    protected
      Integer nEntries=Modelica.Utilities.Internal.FileSystem.getNumberOfFiles(directoryName);
      String files[nEntries];
      String files2[nEntries];
      Integer nrDir=nEntries;
      Integer i;
    algorithm
      files := Modelica.Utilities.Internal.FileSystem.readDirectory(directoryName, nEntries);
      // count mat-files
      nrMat := 0;
      for i in 1:nrDir loop
        if Modelica.Utilities.Strings.find(files[i], "care", 1, false)<>0 and Modelica.Utilities.Strings.find(files[i], ".mo", 1, false)<>0 then
          nrMat := nrMat + 1;
          files2[nrMat] := files[i];
        end if;
      end for;
      matFiles := files2[1:nrMat];

    end getCareMoFiles;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.multiplyComplexMatrices

Some case studies to complex matrices

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Integer | n | 30 |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function multiplyComplexMatrices
      "Some case studies to complex matrices"
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2;

      input Integer n=30;
      output Boolean ok;
    protected
      Complex C1[n,n]=fill(Complex(1,1),n,n);
      Complex C2[n,n];
      Real t1;
      Real t2;

    algorithm
    //  C2 := C1*C1;
      C2 := Complex.Matrices.matMatMul(C1,C1);
      ok := true;
    end multiplyComplexMatrices;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.multiplyRealMatrices

Some case studies to complex matrices

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Integer | n | 30 |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function multiplyRealMatrices "Some case studies to complex matrices"
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2;

      input Integer n=30;
      output Boolean ok;
    protected
      Real C1[n,n]=fill(1,n,n);
      Real C2[n,n];
      Real t1;
      Real t2;

    algorithm
      C2 := C1*C1;
    //  Modelica_LinearSystems2.Math.Matrices.printMatrix(C2,6,"C2");
      ok := true;
    end multiplyRealMatrices;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.multiplyRealMatrices2

Some case studies to complex matrices

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Integer | n | 50 |
Integer | nr | 50 |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function multiplyRealMatrices2
      "Some case studies to complex matrices"
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2.Math.Matrices;
      import Modelica_LinearSystems2;

      input Integer n=50;
      input Integer nr=50;
      output Boolean ok;
    protected
      Real C1[n,n]=fill(1,n,n);
      Real C2[n,n]=C1;

    algorithm
      for i in 1:nr loop
       C2 := Modelica_LinearSystems2.Math.Matrices.LAPACK.dgemm(C1,C2,C1);
      end for;

    //  Modelica_LinearSystems2.Math.Matrices.printMatrix(C2,6,"C2");
      ok := true;
    end multiplyRealMatrices2;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.multiplyRealMatrices3

Some case studies to complex matrices

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Integer | n | 50 |
Integer | nr | 50 |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function multiplyRealMatrices3
      "Some case studies to complex matrices"
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2.Math.Matrices;
      import Modelica_LinearSystems2;

      input Integer n=50;
      input Integer nr=50;
      output Boolean ok;

    protected
      Real C1[n,n]=fill(1,n,n);
      Real C2[n,n];

    algorithm
      C2 := Modelica_LinearSystems2.WorkInProgress.Tests.Internal.multiplyRealMatrices3cCall(
                                    n,nr,C1);
    //  Modelica_LinearSystems2.Math.Matrices.printMatrix(C2,6,"C2");
      ok := true;

    end multiplyRealMatrices3;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.multiplyRealMatrices3cCall

Some case studies to complex matrices

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Integer | n | 50 |
Integer | nr | 20 |
Real | C1[n, n] | fill(1, n, n) |

### Outputs

Type | Name | Description
--- | --- | ---
Real | C2[n, n] |

### Modelica definition

    function multiplyRealMatrices3cCall
      "Some case studies to complex matrices"
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2.Math.Matrices;
      import Modelica_LinearSystems2;

      input Integer n=50;
      input Integer nr=20;
      input Real C1[n,n]=fill(1,n,n);
      output Real C2[n,n]=C1;

    external"FORTRAN 77" c_dgemm(C1, C2, n, nr);

    end multiplyRealMatrices3cCall;

## Modelica_LinearSystems2.WorkInProgress.Tests.Internal.realMatrices

Some case studies to real matrices

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Integer | n | 200 |

### Outputs

Type | Name | Description
--- | --- | ---
Boolean | ok |

### Modelica definition

    function realMatrices "Some case studies to real matrices"
      import Modelica_LinearSystems2;

      input Integer n=200;
      output Boolean ok;
    protected
      Real C1[n,n]=fill(1,n,n);
      Real C2[n,n];

    algorithm
      C2 := C1;
      ok := true;
    end realMatrices;