Save/Load simulation state

Discussion in 'Modeling' started by Rusen, Aug 16, 2017.

  1. Hello!

    I'm trying to save a simulation step-by-step to be able to view it in interactive mode afterwards (as if simulation is running normally, except the controls and movements will come from saved files). Is this possible?

    I understand that mjModel is not supposed to change during the simulation except for rare cases (maybe more clarification on when it'd likely change would be useful for me as well). Assuming it won't change, I am planning to use mj_saveModel and mj_LoadModel to save and load mjModel.

    Can I save and load mjData in a similar fashion? I know Mujoco's simulation runs in continuous time, would this be a problem? I was hoping I could essentially replace mj_step with my own function that re-creates every step of the simulation from saved data.

    Thanks!

    Rusen
     
  2. Emo Todorov

    Emo Todorov Administrator Staff Member

    This already exists as part of the HAPTIX project. The player is stand-alone and is called playlog.exe, while the writer is embedded in mjhaptix.exe. The code for the player is attached - feel free to use it as a starting point. The code for the writer is entangled with the rest of the HAPTIX code, but it should be obvious how to write it. The custom file format is described here:

    http://www.mujoco.org/book/haptix.html#uiRecord

    The idea is to store the minimal amount of information needed to recompute everything during playback. You don't want to store the entire mjData at each frame because it is too big. In the attached code, the function setFrame sets some fields of mjData from a memory buffer loaded from a file, and then mj_forward is called to compute everything else.
     

    Attached Files:

  3. Thank you! I'll take it from here. Can I assume mjModel will not change during the simulation (to only save it once)?

    Rusen
     
  4. Emo Todorov

    Emo Todorov Administrator Staff Member

    Indeed, mjModel is not changed by the simulation functions. So you should only load it once. As for where you load it from, my approach is to leave it as a separate model file instead of dumping it into the log file... but you can do it either way.
     
  5. In case someone else needs to do the same thing: I've attached the functions I used for writing into a binary file. The file pointer needs to be opened in "wb" mode (fopen(<fileName>, "wb")) . writeHeader needs to be done once after you create the mjModel. You can run savelog either at every step of simulation, or skip the steps you don't need.

    I can visualize the simulation using playlog application (use: ./playlog <modelFile> <logFile>).
     

    Attached Files: