Opened 7 years ago

Last modified 7 years ago

#4871 new enhancement

Avoid using DIVISION macros in the dynamic equations if the denominator is a parameter-dependent expression

Reported by: Francesco Casella Owned by: Lennart Ochel
Priority: high Milestone: 2.0.0
Component: Code Generation Version:
Keywords: Cc:

Description (last modified by Francesco Casella)

Please consider the attached test case, a simple RC circuit fed by a sinusoidal voltage source.

The Test.c file contains the following function

/*
 equation index: 14
 type: SIMPLE_ASSIGN
 der(C._v) = DIVISION(-source.i, C.C)
 */
void Test_eqFunction_14(DATA *data, threadData_t *threadData)
{
  TRACE_PUSH
  const int equationIndexes[2] = {1,14};
  data->localData[0]->realVars[1] /* der(C.v) STATE_DER */ = DIVISION_SIM((-data->localData[0]->realVars[7] /* source.i variable */),data->simulationInfo->realParameter[0],"C.C",equationIndexes);
  TRACE_POP
}

This function needs to compute der(C._v) = -source.i/C.C. The denominator of this expression is a parameter-dependent expression, so it should be checked to be non-zero only once during initialization, and then computed with a plain division during simulation. With the current implementation, the check that it is non-zero is re-done times and again at each f(x,t) evaluation.

The overhead of this macro in models that have lots of divisions can be substantial: a comparison with the mu-Modelica tool on a simple ODE model, using the same DASSL solver, showed that the slow-down factor can be as high as 4X.

Attachments (1)

Test.mo (1.3 KB ) - added by Francesco Casella 7 years ago.

Download all attachments as: .zip

Change History (3)

by Francesco Casella, 7 years ago

Attachment: Test.mo added

comment:1 by Willi Braun, 7 years ago

Yeah, good point. We should remove this macros at all. And do the related checks with the code generation.

comment:2 by Francesco Casella, 7 years ago

Description: modified (diff)

See also #3568. In fact, almost a duplicate ticket, except that I added the explicit reference to the generated code in this one.

Note: See TracTickets for help on using tickets.