Opened 5 years ago

Closed 2 years ago

#4111 closed defect (fixed)

Munkres score for high order derivatives

Reported by: jsten Owned by: jsten
Priority: major Milestone: Pending
Component: Modelica/MiddleEnd Version: trunk
Keywords: Cc:

Description

The Munkres score for high order derivatives is currently calculated incorrectly. Currently it only looks for state select in the directly integrated variable. This seems to be wrong, however there are models that crash when correctly implemented.
Changes:

#!
### Eclipse Workspace Patch 1.0
#P JModelica
Index: Compiler/ModelicaMiddleEnd/src/jastadd/structural/IndexReduction.jrag
===================================================================
--- Compiler/ModelicaMiddleEnd/src/jastadd/structural/IndexReduction.jrag	(revision 7095)
+++ Compiler/ModelicaMiddleEnd/src/jastadd/structural/IndexReduction.jrag	(working copy)
@@ -984,8 +984,9 @@
                 int linearity = calculateLinearityWeight(fv);
                 if (fv.getMeIntegrated() != null)
                     linearity += calculateLinearityWeight(fv.getMeIntegrated());
-                boolean fixed = fv.getTopIntegrated().fixedAttribute();
-                IndexReductionCost weight = new IndexReductionCost(fv.stateSelection(), fixed, fv.order(), linearity);
+                FVariable topVar = fv.getTopIntegrated();
+                boolean fixed = topVar.fixedAttribute();
+                IndexReductionCost weight = new IndexReductionCost(topVar.stateSelectAttribute(), fixed, fv.order(), linearity);
                 ASTNode.log.info("Weight: %s %s", var, weight);
                 weights.put(var, weight);
             }
@@ -1112,13 +1113,13 @@
         }
     }
     
-    syn StateSelect FAbstractVariable.stateSelection() {
-        throw new UnsupportedOperationException("Unable to get state select from variable type " + getClass().getSimpleName());
-    }
-    eq FVariable.stateSelection() {
-        FRealVariable frv = (FRealVariable) getMeIntegrated();
-        return (frv == null) ? StateSelect.DEFAULT : frv.stateSelectAttribute();
-    }
+//    syn StateSelect FAbstractVariable.stateSelection() {
+//        throw new UnsupportedOperationException("Unable to get state select from variable type " + getClass().getSimpleName());
+//    }
+//    eq FVariable.stateSelection() {
+//        FRealVariable frv = (FRealVariable) getMeIntegrated();
+//        return (frv == null) ? StateSelect.DEFAULT : frv.stateSelectAttribute();
+//    }
     
     public abstract class AbstractBiPGraph{
        

It might also be an idea to reconsider the score for high order derivatives where state select for the base variable is always or prefer, then we must make sure that the high order variables are selected as dummy derivatives so that the base variable gets exposed to Munkres.

Change History (10)

comment:1 Changed 5 years ago by jsten

The bug was fixed by r7203 and #3993.
However I still wonder whether the state select should be inherited if the value is prefer or always, since that means that the base variable (or first order) variable won't be exposed.
One idea is to set avoid or default for second order derivatives and higher if the set ss is prefer or avoid?

comment:2 Changed 4 years ago by jsten

Still need to think about this one.

comment:3 Changed 4 years ago by jsten

Milestone: 1.16.x1.17.x

comment:4 Changed 4 years ago by Tove Bergdahl

Milestone: 1.17Pending

comment:5 Changed 4 years ago by jsten

Regarding state select for higher order derivatives:

However I still wonder whether the state select should be inherited if the value is prefer or always, since that means that the base variable (or first order) variable won't be exposed.

Not true, if der(x,2) is selected as state then der(x) is automatically selected as state as well:

model A
    parameter Real L = 1 "Pendulum length";
    parameter Real g =9.81 "Acceleration due to gravity";
    Real x(stateSelect=StateSelect.always) "Cartesian x coordinate";
    Real y "Cartesian x coordinate";
    Real vx "Velocity in x coordinate";
    Real vy "Velocity in y coordinate";
    Real lambda "Lagrange multiplier";
  equation
    der(x) = vx;
    der(y) = vy;
    der(vx) = lambda*x;
    der(vy) = lambda*y - g;
    x^2 + y^2 = L;
end A;

States:

States:
  Real x(stateSelect = StateSelect.always) "Cartesian x coordinate"
  Real _der_x

Munkres log:

BiPGraph (3 equations, 4 variables)
Variables: {der(vx) der(vy) der(x,2) der(y,2) }
eq_1_d : der(x,2)@ der(vx)@ // der(x,2) = der(vx)
eq_2_d : der(y,2)@ der(vy)@ // der(y,2) = der(vy)
eq_5_d_d : der(x,2)@ der(y,2)@ // 2 * x * der(x,2) + 2 * der(x) * der(x) + (2 * y * der(y,2) + 2 * der(y) * der(y)) = 0.0

Weight: der(vx) (0, 0, 0, 1, 0, 0, 0, -1, 0)
Weight: der(vy) (0, 0, 0, 1, 0, 0, 0, -1, 0)
Weight: der(x,2) (0, 1, 0, 0, 0, 0, 0, -2, 4)
Weight: der(y,2) (0, 0, 0, 1, 0, 0, 0, -2, 4)
Munkres result:
eq_1_d: der(vx) (0, 0, 0, 1, 0, 0, 0, -1, 0)
eq_2_d: der(vy) (0, 0, 0, 1, 0, 0, 0, -1, 0)
eq_5_d_d: der(y,2) (0, 0, 0, 1, 0, 0, 0, -2, 4)
Optimal cost: (0, 0, 0, 3, 0, 0, 0, -4, 4)
----------------------------------------
BiPGraph matching:
eq_1_d : der(vx)
eq_2_d : der(vy)
eq_5_d_d : der(y,2)
Unmatched equations: {}
Unmatched variables: {der(x,2) }
----------------------------------------

 Dummy derivatives selected in iteration: 
   der(vx)
   der(vy)
   der(y,2)

----------------
BiPGraph (1 equations, 3 variables)
Variables: {vx vy der(y) }
eq_5_d : der(x)@ der(y)@ // 2 * x * der(x) + 2 * y * der(y) = 0.0

Weight: vx (0, 0, 0, 1, 0, 0, 0, 0, 0)
Weight: vy (0, 0, 0, 1, 0, 0, 0, 0, 0)
Weight: der(y) (0, 0, 0, 1, 0, 0, 0, -1, 2)
Munkres result:
eq_5_d: der(y) (0, 0, 0, 1, 0, 0, 0, -1, 2)
Optimal cost: (0, 0, 0, 1, 0, 0, 0, -1, 2)
----------------------------------------
BiPGraph matching:
eq_5_d : der(y)
Unmatched equations: {}
Unmatched variables: {vx vy }
----------------------------------------

 Dummy derivatives selected in iteration: 
   der(y)

 Dummy derivatives: 
   Real der(vx)
   Real der(vy)
   Real der(y,2)
   Real der(y)

However the state select for higher order derivatives should probably not inherit the state select for lower order variables if they have always or prefer. The reason for this is that the modeller probably don't want der(_der_x) in their models and would probably rather see another variable there. I.e. for multibody models there probably is a der(v) which is more suitable. So generally higher order derivatives should have a lower munkres score (more likely to be matched and thus a dummy derivative).

comment:6 Changed 3 years ago by jsten

changeset:8987
Changed state select for second and higher order derivative to state select avoid. This will make us more prone to not selecting derivatives as states. Still need to add a testcase.

comment:7 Changed 3 years ago by jsten

changeset:9000
Tweaked heuristics for higherorder derivatives. They are now not given any state select value at all during static state selection. This will make them even more prone to selection.

comment:8 Changed 2 years ago by Jesper Mattsson

Is this done?

comment:9 Changed 2 years ago by jsten

Let's close this for now, open a new ticket if we get problems in the future.

comment:10 Changed 2 years ago by jsten

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.