question

Stephane R avatar image
0 Likes"
Stephane R asked Mischa Spelt edited

How to synchronize FlexSim simulation clock with an external service?

I'm looking for pointers, or a previous body of work.

I'm evaluating whether or not I can use FlexSim for parts of a large simulation, while other parts are controlled by a different/legacy simulator. Both simulators are Discrete Event based.

I envision using network calls, to transfer information between the two, but I need some way to synchronize the clocks, or at least limit the amount of drift between the 2.

I don't want to solely rely on speed factor, as I can not guarantee that either simulation can consistently process event at any given rate.

What would be the best way to periodically communicate to an outside process the current FlexSim simulation time; and block/delay FlexSim own event processing until the outside process has confirmed it's own time ?

FlexSim 19.0.2
custom codedllsimulation time
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

David Seo avatar image David Seo commented ·

@Stephane R

I am not sure what is your meaning, but would you use the connectivity feature of flexsim?

The connectivity module is located in the ToolBox>Connectivty option menu. It has a Database connector, PLC connector,...etc.

You can make FlexSim connected to PLC Emulator in your PC.

0 Likes 0 ·
Mischa Spelt avatar image
2 Likes"
Mischa Spelt answered Mischa Spelt edited

Hi Stephane,

Mid 2017 we created a multi-threaded Networking module in C++ that is able to handle TCP/IP communication asynchronously - i.e. even when the model is stopped, incoming messages will trigger a response in FlexSim such as a user command getting executed.

We haven't updated the code in a while and FlexSim can only act as a server, not as a client. So getting it up and running would probably require a little bit of effort from our side. If you'd like to discuss the options feel free to contact me through the email address in my profile.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jordan Johnson avatar image
2 Likes"
Jordan Johnson answered Jordan Johnson commented

I don't know of any previous work, but maybe the following would be helpful:

  • You can use commands in FlexSim to communicate over a network:
    • clientconnect(), clientsend(), clientreceive()
  • You can also control the model run using commands:
    • runspeed(), stop(), go()
  • You can a token with executesubflow()

So using these tools, you could probably come up with some way for FlexSim to be driven by another DES package. The way I see it, the other package would be the driving process. When you run that model, you would have it listen for a network connection, and wait till it gets one. You could then run the FlexSim model, which would connect to the other server as its first event.

Once they are connected, you could have the other DES package send a "tick" message with the current simulation time. You could configure the FlexSim model to check for that tick message at the expected time. For example, you could send a tick every 5 model time units from the one package, and have FlexSim try to get that tick every 5 model time units. If FlexSim is ahead, you could set the run speed to very, very slow (making sure to schedule an extra tick check for when to go back to normal). If FlexSim is behind, you could set the run speed higher.

As for transferring other data, FlexSim could check for that during the tick messages, or at other times. It would be completely up to you.

For sending data back, you would probably need to block the other DES package until FlexSim sends the message you are looking for.

If you use the Module SDK, you could write a C++ dll that communicates via websocket, which might simplify communication.

Those are my ideas. I've never done it, and it seems difficult, but possible. As far as I know, you are in new territory. If you come up with a solution, feel free to share it. Good luck!

· 3
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Stephane R avatar image Stephane R commented ·

Thanks Jordan, for your thoughtful answers.

I looked at clientreceive, and noticed the 'noblock = 0' parameters. Could that be the simplest way to synchronize?
If I create a FlexSim task to run every 1s (sim time), to contact my external service, and pass in the current sim time. Then my external service can check it's own clock. If it has reached the same sim time, it can respond immediately. If not, it can delay the response, until it's own sim clock has reached it, therefore freezing FlexSim (This is the part I need confirmation about). Once the external sim has reached the time, it can respond back on the channel, unlocking FlexSim for one more second.

With this simple lock step execution, I would not have to fuss with FlexSim runSpeed, whichever sim is the slowest would just slow down the other one.

0 Likes 0 ·
Ralf Gruber avatar image Ralf Gruber ♦ Stephane R commented ·
@Stephane R

not that I can contribute anything constructive to your question, but forcing the communication every simulation second will likely slow down the simulation speed to "useless"? I am excited to hear, if you get it to work and let us know about the performance.

Thanks
Ralf

0 Likes 0 ·
Jordan Johnson avatar image Jordan Johnson ♦♦ Stephane R commented ·

Hi Stephane, it is true that passing a 1 parameter will essentially "freeze" flexsim in place. Off the top of my head, I can't see a problem with it. If the other package can do as you say, and take a request, and maybe execute some events before responding, then it sounds like it could work. I hope it does!

0 Likes 0 ·