# Initialization failures🔗

### Background🔗

A model needs to be initialized before it is simulated, this means the solver needs to find consistent start values for all the states of a model at time `t=0`. If the solver is unable to do so, an initialization failure occurs. This can happen due to several different reasons. This article explains the most common ones with simple examples.

### Examples🔗

Consider the non-linear model `A`:

``````model A "Model that fails during initialization."
Real x;
Real y;
equation
x*x*y = -1;
y*y*x = 1;
end A;
``````
Model A fails during initialization. This is because the solver implicitly assumes that both `x` and `y` start at `0` as no start value has been set. If iteration variables (`x` and `y` in model `A`) are missing start values, they are displayed in the simulation log in the form of warnings. A start variable can be set using a modifier, that is `Real x(start = 1)` explicitly defines `x(0) = 1`.

For model `A` it is not a good start value for neither `x` nor `y`. Technically this makes it very difficult (or impossible) for the solver to find a solution. It can also be noted from the two equations in model `A` that the equations are true if and only if `x = 1` and `y = -1` implying this is the only real solution.

As a test, add the actual (and only solution in this case) as start values to see if it resolves the initialization issue.

``````model A_Fixed "Same as model A but with start values."
Real x(start = 1);
Real y(start = -1);
equation
x*x*y = -1;
y*y*x = 1;
end A_Fixed;
``````

Note

A missing start value does not necessarily give an initialization error.

The example model `B` below is almost identical to model `A` but the first equation is equal to `1` instead of `-1`. This simulates without any initialization errors even though the start values are not set.

``````model B "Model without start values and no initialization failure."
Real x;
Real y;
equation
x*x*y = 1;
y*y*x = 1;
end B;
``````

## How to investigate the issue🔗

When an initialization failure occurs, it is displayed as a simulation failure. The information displayed in the simulation log varies slightly based on the options chosen and also if the model is linear or not. For example, consider model `A` in Modelon Impact. Attempting to simulate model `A` gives a simulation error. In the simulation log, information about initialization errors can be seen. The iteration variables `x` and `y` are mentioned and both have values `0` (since no start value is set), and the residuals are equal to `-1` & `1` respectively. The information that the error occurred at time `t=0` confirms that this is indeed an initialization failure. If the time (`t`) is such that `t > 0` then this is not an initialization failure.

However in the simulation log of model A (a subset of the output looks as follows), it can be seen that the iteration variables are missing start values,

``````[Nonlinear Block Convergence Error]:
Failed to find a solution for block = "(init)1" at time = 0.
Could not converge even after re-scaling the residual equations.
[Initialization Failure Info]: Missing start values in block = "(init)1",
this is a possible reason for the initialization failure.
This error says that the solver was unable to solve the nonlinear block (a subset of equations where all the equations have to be solved together) at time `t=0`. It is likely due to a bad choice of start values. In particular, the error hints that both `x` and `y` are missing start values.
If appropriate start values are set but initialization failures still occur, then the recommendation is to investigate if the system is ill-conditioned or if the solver tolerance is not appropriate. The solver tolerance is set via simulation options (the cogwheel in the upper right in the UI). The model can also be investigated via HTML diagnostics. This provides a detailed overview of the system. All variables, such as iteration variables, states, constants and parameters are displayed with all of their attributes, such as start, min, max and nominal. Parameters can also be browsed easily via the tab `Properties` displayed in the right-most column.