Suggestion: more efficient Jacobian computation for algebraic loops

2 posts / 0 new
Last post
Joined: 2014-01-03
Suggestion: more efficient Jacobian computation for algebraic loops

I set up a simple model in JModelica which produces the following C code:

else if (evaluation_mode == JMI_BLOCK_INITIALIZE) {
        x[0] = _thermalConductor1_dT_45;
        x[1] = _prescribedHeatFlow1_port_T_5;
        init_with_lbound(x[1], 0.0, "Resetting initial value for variable prescribedHeatFlow1.port.T");
        x[2] = _thermalConductor2_dT_48;
        x[3] = _thermalConductor2_Q_flow_47;
        x[4] = _thermalConductor1_Q_flow_44;
    } else if (evaluation_mode == JMI_BLOCK_EVALUATE_JACOBIAN) {
        memset(residual, 0, 25 * sizeof(jmi_real_t));
        residual[0] = (- _thermalConductor1_G_43);
        residual[1] = 1.0;
        residual[6] = 1.0;
        residual[7] = -1.0;
        residual[12] = 1.0;
        residual[13] = (- _thermalConductor2_G_46);
        residual[18] = 1.0;
        residual[19] = 1.0;
        residual[20] = 1.0;
        residual[24] = -1.0;

Whenever the Jacobian is evaluated, the respective partial derivatives are assigned to the variable 'residual', even though these derivatives are constant and they will never change. Even assigning variables incurs an overhead, which can be avoided in this case. Wouldn't it therefore make sense to assign some memory for Jacobian coefficients in the fmi_t struct and only write/compute the values of those coefficients that do change? They could then simply be set in the branch corresponding to JMI_BLOCK_INITIALIZE.

Edit: I think you'd also avoid overhead for allocating memory to residual, which can't hurt either!

Joined: 2009-07-29
Hi,   Thanks for the



Thanks for the feedback! Regarding your example if the variable *_G_43 is a parameter or constant we will only evaluate the Jacobian once. If they are continuous however, we have to evaluate it repeatedly. An additional improvement in the continuous case is as you say only to update the coefficents that change. So far though this has not though caused a significant overhead but we will consider updating the behavior, thanks!




Login or register to post comments