Passing Simulation time to a C++ block

Is there a way to pass or variable available to a C++ block to pass the actual simulation time? By that I mean the time within the simulation not how long it has taken to execute. I have used a voltage source as a clock, but for simulating integration, filtering etc. within a C++ block time is a necessary variable I am sure you already have a solution that I am not aware of. Thank you for your help.

Normally, implementation of integral or filter in digital controller does not require passing simulation time as clock signal can tell the duration between each sample, which is the key in digital control.

But I think you can pass Time to C++ block, the concept is that you can use a B-source with V=Time in .tran to convert Time into a voltage, and you can use this voltage to feed into C++ block.

here is an example to convert Time into voltage

Thank you for your response. With a clock input there can be some simulation jitter, as the simulation time does not necessarily perfectly align with the simulation time. Your solution will work well so thank you again.

1 Like

Hello,

Some addition to the topic. As far as I know the parameter t in code block is the simulation time, so not necessary to add it from outside.

There is need for fixed time step from some users to simulate discrete control loops or MCU based PWM etc. in code blocks but it is not easy to achieve because the variable time step is (if I understand is correctly) fundamental is spice’s numerical algorithms.

C++ block question

See this topic above, my and ImNotCreative’s comments. I tried to show the time difference between function block callings (I assume this is the jitter what you mentioned), maybe you can use it some way. If you search the forum for code block and fixed time step (if you need that) you can find other and better solutions.

1 Like

Thanks for your response, I was hopeful that time was already a variable that was accessible. Fortunately I am not looking for constant time steps, just an accurate time measure. :grinning: