modelica / ModelicaSpecification

Specification of the Modelica Language
https://specification.modelica.org
Creative Commons Attribution Share Alike 4.0 International
101 stars 41 forks source link

Specify a variable initial guess to an iteration variable #1799

Closed modelica-trac-importer closed 6 years ago

modelica-trac-importer commented 6 years ago

Reported by MForey on 9 Oct 2015 15:22 UTC The Modelica standard provides a way to give a fixed value (parameter) as the initial guess of an iteration variable, via the keyword "start". However, there are a number of cases where this fixed initial guess is not valid anymore as the simulation advances in time, and where the previous computed point is not close enough either. In these cases, the user might want to have a variable initial guess, for instance depending on some inputs of the model. An example is given below:

model MyModel
  input Real x;
  output Real y(min = 0, start = 0);
equation 
  (y-x).^2 .* sin(y-x) = 0;
end MyModel;

The user might know that x is a good initial guess for y (actually, in that case, that would even be the exact solution); however, he has no way of telling the solver so (something like: Real y(start = x)). In this particular example, problems might arise when x changes abruptly, for example from 0 to 100.


Migrated-From: https://trac.modelica.org/Modelica/ticket/1799

modelica-trac-importer commented 6 years ago

Comment by stefanv on 9 Oct 2015 15:40 UTC I'm not really sure I understand why you consider it to be a problem that "this fixed initial guess is not valid anymore as the simulation advances in time".

The initial guess is only used during initialization. Once the simulation starts, the initial guess is never used again, so its validity at a later time is irrelevant.

modelica-trac-importer commented 6 years ago

Comment by msasena on 9 Oct 2015 15:53 UTC I believe the confusion is in the difference between providing a guess value to a variable for the initialization problem vs. providing an initial guess value to a variable that is part of nonlinear system that must be solved. The problem is that (as far as I'm aware) there is no way to specify which variables in the model should become the iteration variables in a nonlinear system. Depending on how the compiler does all the symbolic manipulation / variable reduction along the way, there's no guarantee which variables will be left within the nonlinear system and even less certainty about which are selected as the iteration variable during nonlinear system solving. So not only is there no good way of telling the solver how to make an initial guess for the iteration variable value, there is no good way to specify which variable should actually be the iteration variable...

modelica-trac-importer commented 6 years ago

Comment by fcasella on 9 Oct 2015 15:55 UTC Replying to [comment:1 stefanv]:

I'm not really sure I understand why you consider it to be a problem that "this fixed initial guess is not valid anymore as the simulation advances in time".

The initial guess is only used during initialization. Once the simulation starts, the initial guess is never used again, so its validity at a later time is irrelevant.

That's precisely the point. Some variables in the models can be discontinuous at events, and in order to process the event, one needs to find a new consistent solution before restarting integration.

If the discontinuity is large, then it might be possible that the solver cannot converge, if using the previously computed value (or some extrapolation thereof) as an initial guess. In this case, the start attribute, which is valid at initialization, is in general useless.

The problem is, there is currently no way to specify a different one at this point.

modelica-trac-importer commented 6 years ago

Comment by stefanv on 9 Oct 2015 15:58 UTC And to an engineer that just wants her model to run, there's really no good way to know which variable should be an iteration variable. Such decisions are probably best left up to the compiler.

Francesco, thanks, that clarifies what the original post meant. Perhaps we need a restart attribute, whose value can be a variable? But I'm getting out of my depth here; I just do flattening and leave the integration to others.

modelica-trac-importer commented 6 years ago

Comment by hansolsson on 12 Oct 2015 08:04 UTC To me the problem with (x known, y unknown).

(y-x).^2 .* sin(y-x) = 0;

is that it has an infinite number of solutions y=x+pi*n (and additionally y=x is a triple zero, which is sometimes problematic).

The equations are intended to describe the model, but here there is some missing additional knowledge describing which of the solutions is the valid one (or ones). Specifying the tearing variables doesn't help for a scalar equation in one unknown. Specifying the start-value doesn't solve the problem, since numerical solvers will not always find the closest solution - sometimes the convergence regions are fractal. (Start-values are good when we want to find any solution, and may help if there are multiple solutions.)

Or in summary: we want to find the right solution (not just any solution), but we haven't described what we mean with 'right'. How to do that may be non-trivial, and a better description of the underlying problem might help.

modelica-trac-importer commented 6 years ago

Comment by mforey on 12 Oct 2015 14:35 UTC Sorry if my explanations were not clear or detailed enough, but it seems you got what I meant there.

As you rightly understood, what I'm trying to do here is not to say which variables should be iteration variables (which would go against the whole idea of symbolic manipulation allowed by Modelica really), but if they are to give them a good initial guess to "help" the solver get to the solution we're looking for.

I agree with you, hansolsson, we can't be sure that, for all models, the solver will give a solution in the neighborhood of the initial guess. However providing the possibility for the user to set the initial guess at each step will certainly help to solve some practical cases. In all applications I've seen so far (mainly engineering), the model developer has a vague idea of what the solution will be, and the functions are generally quite "regular" near that point (with the solution expected in that zone).

modelica-trac-importer commented 6 years ago

Comment by hansolsson on 7 Sep 2016 11:46 UTC Duplicate of #1787 - both tickets contain important discussion.

modelica-trac-importer commented 6 years ago

Comment by hansolsson on 7 Sep 2016 15:28 UTC Cleanup.