mpacts.io. save_load

Functionality for saving and loading of complete Mpacts simulations.

This module provides two main functions: mpacts.io.save_load.save and mpacts.io.save_load.load for saving and loading a full mpacts simulation tree. Doing so will normally ensure that the complete state of the simulation will be serialized.

Example usage:

import mpacts.io.save_load as sl
import mpacts.core.simulation as sim

name = 'example_simulation'
oldsim = sim.Simulation(name, timestep=1.0)

sl.save( oldsim, name+".mpacts" )

newsim = sl.load(name+".mpacts")

print newsim.name()

Note

Although similar in behavior, the functionality of save_load is completely different than the functionality of mpacts.io.datasave. The latter provides an efficient and convenient format to save the simulated data (arrays, properties, ...) during and after a simulation, for post-processing purposes.

save_load is a much more elaborate (and also computationally more costly) tool that reconstructs a full dependency tree of the simulation, and stores everything that can be stored including arrays, properties and contact states.

Attention

save() only stores whatever is in the simulation tree. Of course it cannot know everything that is defined in your local Python script. Make sure that parameters or important data are saved for example as mpacts.core.valueproperties.Variable to ensure their persistence when re-loading.

One particular exception which is NOT saved but still is in the simulation tree is a local PythonCommand, i.e. a PythonCommand that is not defined somewhere in the Mpacts API, but in the local script for running the simulation itself. If you want to re-introduce such a command when reloading, it is important that the resuming script redefines this command.

In order to be able to use this module import it like this:

import mpacts.io.save_load
#or assign it to a shorter name
import mpacts.io.save_load as sav

SaveCommand

class mpacts.io.save_load.SaveCommand(sim, gate, filename, allowZip64=False)

Bases: mpacts.core.command.PythonCommand

SaveCommand(name, parent, **kwargs)
execute()
mpacts.io.save_load.ast_unserialise_data(string)
mpacts.io.save_load.construct(state, dicts, zf)
mpacts.io.save_load.construct_PythonCommand(state, dicts, zf)
mpacts.io.save_load.convert_id_to_baseobject_if_necessary(t, dicts)
mpacts.io.save_load.filter_baseobjects_and_set_state(obj, state, adict)
mpacts.io.save_load.get_constructing_function(mod, constr_def)
mpacts.io.save_load.json_serialise_data(data)
mpacts.io.save_load.json_unserialise_data(string)
mpacts.io.save_load.load(filename, verbose=True, allowZip64=False, parent=None)

Function to read in a given file and from it reconstruct (load) an Mpacts simulation tree

Parameters:
  • filename (str) – Name of the file that is to be loaded
  • verbose (bool) – If True, the loading will report its progress
  • parent (obj) – If given, the loaded object will be made as a child of the given parent
Returns:

An Mpacts tree of whatever was saved (for example an mpacts.core.simulation.Simulation)

mpacts.io.save_load.reconstruct_kwargs(kwargs, adict)
mpacts.io.save_load.repr_serialise_data(data)
mpacts.io.save_load.save(rootnode, filename, allowZip64=False)

Function to save a given simulation tree as a file with a given name.

Parameters:
  • rootnode (instance of mpacts.core.baseobject.BaseObject) – Root node of an Mpacts tree below (and including) which everything will be saved
  • filename (str) – Full name of the save file.
mpacts.io.save_load.serialise_data(data)
mpacts.io.save_load.set_existing_child_in_dict(parent, name, oldid, dicts)
mpacts.io.save_load.unserialise_data(string)