## Modelica_LinearSystems2.Utilities.Import

Functions to import data in a Modelica environment

### Package Content

Name | Description
--- | ---
Examples | Demonstrate the usage of the Import functions
linearize | Linearize a model after simulation up to a given time

## Modelica_LinearSystems2.Utilities.Import.linearize

Linearize a model after simulation up to a given time

### Information

This function initializes a Modelica model and then simulates the model with its default experiment options until time instant "t_linearize". If t_linearize=0, no simulation takes place (only initialization). At the simulation stop time, the model is linearized in such a form that

- all top-level signals with prefix "input" are treated as inputs u(t) of the model ,
- all top-level signals with prefix "output" are treated as outputs y(t) of the model,
- all variables that appear differentiated and that are selected as states at this time instant are treated as states x of the model.

Formally, the non-linear hybrid differential-algebraic equation system is therefore treated as the following ordinary equation system at time instant t_linearize:

        der(x) = f(x,u)

             y = g(x,u)

Taylor series expansion (linearization) of this model around the simulation stop time t_linearize:

       u0 = u(t_linearize)

       y0 = y(t_linearize)

       x0 = x(t_linearize)

and neglecting higher order terms results in the following system:

       der(x0+dx) = f(x0,u0) + der(f,x)*dx + der(f,u)*du

          y0 + dy = g(x0,u0) + der(g,x)*dx + der(g,u)*du

where der(f,x) is the partial derivative of f with respect to x, and the partial derivatives are computed at the linearization point t_linearize. Re-ordering of terms gives (note der(x0) = 0):

       der(dx) = der(f,x)*dx + der(f,u)*du + f(x0,u0)

            dy = der(g,x)*dx + der(g,u)*du + (g(x0,u0) - y0)

or

       der(dx) = A*dx + B*du + f0

            dy = C*dx + D*du

This function returns the matrices A, B, C, D and assumes that the linearization point is a steady-state point of the simulation (i.e., f(x0,u0) = 0). Additionally, the full Modelica names of all inputs, outputs and states shall be returned if possible (default is to return empty name strings).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
String | modelName |  | Name of the Modelica model
Time | t_linearize | 0 | Simulate until T_linearize and then linearize [s]

### Outputs

Type | Name | Description
--- | --- | ---
Real | A[nx, nx] | A-matrix
Real | B[nx, nu] | B-matrix
Real | C[ny, nx] | C-matrix
Real | D[ny, nu] | D-matrix
String | inputNames[nu] | Modelica names of inputs
String | outputNames[ny] | Modelica names of outputs
String | stateNames[nx] | Modelica names of states

### Modelica definition

    function linearize
      "Linearize a model after simulation up to a given time"
      input String modelName "Name of the Modelica model";
      input Modelica.SIunits.Time t_linearize= 0
        "Simulate until T_linearize and then linearize";

    protected
      String fileName="dslin";
      String fileName2=fileName+".mat";

      // Simulate until t_linearize and then linearize at this time instant
      Boolean OK1 = simulateModel(problem=modelName, startTime=0, stopTime=t_linearize);
      Boolean OK2 = importInitial("dsfinal.txt");
      Boolean OK3 = linearizeModel(problem=modelName, resultFile=fileName, startTime=t_linearize, stopTime=t_linearize);

      // Read linear system from file
      Real nxMat[1,1]=readMatrix(fileName2, "nx", 1, 1);
      Integer ABCDsizes[2]=readMatrixSize(fileName2, "ABCD");
      Integer nx=integer(nxMat[1, 1]);
      Integer nu=ABCDsizes[2] - nx;
      Integer ny=ABCDsizes[1] - nx;
      Real ABCD[nx + ny,nx + nu]=readMatrix(fileName2, "ABCD", nx + ny, nx + nu);
      String xuyName[nx + nu + ny]=readStringMatrix(fileName2, "xuyName", nx + nu + ny);
    public
      output Real A[nx,nx] =  ABCD[1:nx, 1:nx] "A-matrix";
      output Real B[nx,nu] =  ABCD[1:nx, nx + 1:nx + nu] "B-matrix";
      output Real C[ny,nx] =  ABCD[nx + 1:nx + ny, 1:nx] "C-matrix";
      output Real D[ny,nu] =  ABCD[nx + 1:nx + ny, nx + 1:nx + nu] "D-matrix";
      output String inputNames[nu] =  xuyName[nx + 1:nx + nu]
        "Modelica names of inputs";
      output String outputNames[ny] =  xuyName[nx + nu + 1:nx + nu + ny]
        "Modelica names of outputs";
      output String stateNames[nx] =  xuyName[1:nx] "Modelica names of states";
    algorithm

    end linearize;