Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#2982 closed defect (fixed)

sorting of protected variable in a function

Reported by: Willi Braun Owned by: Martin Sjölund
Priority: high Milestone: 1.9.2
Component: Frontend Version: trunk
Keywords: Cc: Martin Sjölund

Description (last modified by Willi Braun)

Following model fail due to using not initialized memory:

ModelicaTest.Fluid.Dissipation.Verifications.HeatTransfer.StraightPipe.kc_overall_KC

It seems that we sort the protected variables in a function wrong or actually we don't sort them at all, if there is an if-expression involved.

loadString("
function f
  input Real x;
  output Real y;
protected
  Real a = sin(x);
  Real b = if  x > 0 then cos(c) else sin(c);
  Real c = cos(a);
algorithm
  y := c;
end f;

model A
 Real x(start=1);
equation
  der(x) = f(x);
end A;
"); getErrorString();
simulate(A); getErrorString();

Where is this sorting performed?

Change History (9)

comment:1 by Martin Sjölund, 10 years ago

I assume this part in InstUtil.mo:

    // If a binding contains an if-equation we don't really have any idea which
    // branch will be used, which causes some problems with Fluid. So we just
    // reset everything up to this point and pray that we didn't miss anything
    // important.
    case (exp as Absyn.IFEXP(ifExp = ifExp), (all_el, stack_el::rest_stack, _))
      equation
        (_, (_, _, deps)) = Absyn.traverseExpBidir(ifExp, getElementDependenciesTraverserEnter, getElementDependenciesTraverserExit, (all_el, {}, {}));
        deps = listAppend(deps, stack_el);
      then
        (exp, (all_el, rest_stack, deps));

I just searched for calls to Graph.topologicalSort. Maybe we either remove that case, or we ignore that case for function scope.

comment:2 by Willi Braun, 10 years ago

Since praying seems not to help. Why can't we just consider all branches as we do it for the ordinary matching?

comment:3 by Martin Sjölund, 10 years ago

Because... HeatingSystem.mo Error: Variable tank: Cyclically dependent constants or parameters found in scope Modelica.Fluid.Vessels.OpenTank$tank:

  • parameter Medium.SpecificEnthalpy h_start = if use_T_start then Medium.specificEnthalpy_pTX(p_start, T_start, X_start) else Medium.h_default
  • parameter Medium.Temperature T_start = if use_T_start then system.T_start else Medium.temperature_phX(p_start, h_start, X_start)

comment:4 by Willi Braun, 10 years ago

Okay, but this is cycle in non-input bindings and this is illegal, as far as I understand the Modelica Spec.

Modelica Spec:
12.4.4: Initialization and Declaration Assignments of Components in Functions
[...]These bindings must be executed in an order where a variable is not used before its declaration assignment has been executed; it is an error if no such order exists (i.e. the binding must be acyclic). [...]

comment:5 by Willi Braun, 10 years ago

Description: modified (diff)

comment:6 by Martin Sjölund, 10 years ago

We use the same sorting for non-functions is the problem here (to decide the order in which the components are ordered).

comment:7 by Martin Sjölund, 10 years ago

Owner: changed from somebody to Martin Sjölund
Status: newaccepted

comment:8 by Martin Sjölund, 10 years ago

Component: Run-timeFrontend
Milestone: Future1.9.2
Resolution: fixed
Status: acceptedclosed

Fixed in r23351 (treating function context differently).

comment:9 by Willi Braun, 10 years ago

I'm really curious about the positive jump in the coverage tests, since I've just run 4 random ModelicaTest.Fluid examples and they all started to work.

Note: See TracTickets for help on using tickets.