4. Scaling

Many physical models contain variables with values that differ several orders of magnitude. A typical example is thermodynamic models containing pressures, temperatures and mass flows. Such large differences in values may have a severe deteriorating effect on the performance of numerical algorithms, and may in some cases even lead to the algorithm failing. In order to relieve the user from the burden of manually scaling variables, Modelica offers the nominal attribute, which can be used to automatically scale a model. Consider the Modelica variable declaration:

Real pressure(start=101.3e3, nominal=1e5);

Here, the nominal attribute is used to specify that the variable pressure takes on values which are about 1e5. In order to use nominal attributes for scaling with CasADi-based algorithms, scaling is enabled by setting the algorithm option variable_scaling to True, and is enabled by default . In case of enabling scaling, all variables with a set nominal attribute are then scaled by dividing the variable value with its nominal value, i.e., from an algorithm point of view, all variables will take on values close to one. Notice that variables typically vary during a simulation or optimization and that it is therefore not possible to obtain perfect scaling. In order to ensure that model equations are fulfilled, each occurrence of a variable is multiplied with its nominal value in equations. For example, the equation:

T = f(p)

is replaced by the equation

T_scaled*T_nom = f(p_scaled*p_nom)

when variable scaling is enabled.

The algorithm in Section 5, “Dynamic optimization of DAEs using direct collocation with CasADi” also has support for providing trajectories (obtained by for example simulation) that are used for scaling. This means that it usually is not necessary to provide nominal values for all variables, and that it is possible to use time-varying scaling factors.

For debugging purposes, is is sometimes useful to write a simulation/optimization/initialization result to file in scaled format, in order to detect if there are some variables which require additional scaling. The option write_scaled_result has been introduced as an option to the initialize, simulate and optimize methods for this purpose.