C Block

Greg Mowry
Greg Mowry Altair Community Member
edited June 2023 in Community Q&A

Simplified C-Block Questions from the help description:

(1)  "formulas are evaluated before simulation starts" & "... interpret and execute the C code at runtime" along with "If no ZOH ... C block is called at every small time step." appear to be conflicting. 

(a) How are formulas evaluated before a simulation starts if C code is executed at each time step?

(b) is the entire block of C code run at each time step? e.g. if variables are defined and library links set up, are these redone at each time step? Additionally & for example, if a variable is set to zero in the C code definition, is this variable set to zero at each time step?

 

(2)  When a ZOH is used at a C block input, is the C code only executed at ZOH steps while the code runs at t and delt steps? Or does the simulation operate only at the ZOH steps?

 

(3)  Can the output of a C block be tied directly to the input of that same C block?

 

Thank you

Professor Greg Mowry

gsmowry@stthomas.edu

Tagged:

Answers

  • Jack_2nd
    Jack_2nd
    Altair Employee
    edited June 2023

    (1) simplified c block may have some variables (or parameters), these variables (usually constants) are evaluated before simulation. the formula is calculated every sampling period. Put a ZOH before a simplified c block is a safe way to specify this c block's sampling frequency.

    (a) If a c block doesn't have a sampling rate, PSIM will calculate it at each time step, put a ZOH before the c block to specify its sampling frequency.

    (b) Again, a c block is calculated at each sampling period, if no any sampling rate specified, it will be calculated every time step. when PSIM calculate the formula, it applies all input values to the formula, and put output values to the output signals. If you define a variable in the c block and want to keep the previous value, set it as static, so it will keep the value at the previous step. for example:

        {    static float cnt = 0.0;

             cnt += x1;

             y1 = cnt;

        }

    (2) When inserting a ZOH before a c block, the c block is calculated at ZOH sampling frequency.

    (3) No, PSIM will report an algebraic loop error. You need to put a unit delay in between the output port and the input port of the same c block. This rule applies to any blocks even the loop includes multiple blocks.

     

  • Albert_Dunford
    Albert_Dunford
    Altair Employee
    edited June 2023

    Hi Greg!

    To add to Jiakai's comments here are some pictures and a little demo circuit. Notice all scenarios are covered and we have multiple c blocks executing at different rates in the same simulation controlled by timestep or ZOH.

    image