FmuPool

@author: Marco Bonvini

class estimationpy.fmu_utils.fmu_pool.FmuPool(model, processes=3)

This class manages a pool of processes that execute parallel simulation of an FMU model.

NOTE:

The processes running the simulations, executed in parallel if multiple processors are available, produce results that are stored in a queue. If the queue reaches its limit the execution will be blocked until the resources are freed.
__init__(model, processes=3)

Constructor that initializes the pool of processes that runs the simulations.

Parameters:
NOTE
If the parameter processes is less or equal to 1, by default the number of processes allocated is equal to one. Also, the implementation avoid to spawn a new process in case processes == 1. This improves the performances and solves an issue when the method run() is called from a Celery task. The problem is caused by the inability of a Celery task to create a new process.
run(values, start=None, stop=None)

This method performs the simulation of the model with multiple initial states or parameters using multiple processes in parallel. The parameter values is a list that contains the values that the states and parameters will assume in the multiple simulations. For example:

pars = [{"state":[1,1,1], "parameters": [0,1,2]},
        {"state":[1,1,10], "parameters": [0,2,2]},
        {"state":[1,1,100], "parameters": [0,3,2]},
        {"state":[1,1,1000], "parameters": [0,4,2]}]

indicates to run four simulations in parallel, and for each simulation the values of the initial states and parameters are the ones indicated by the dictionary.

Parameters:
  • values (list) – a list of dictionaries that contains the values of the initial states and parameters used in each of the simulations.
  • start (datetime.datetime) – the initial time for the simulation, if not specified the initial time of the data series associated to the inputs of the models is used
  • stop (datetime.datetime) – the final time for the simulation, if not specified the final time of the data series associated to the inputs of the models is used
Returns:

a dictionary that contains the results of each simulation. The results are indexed with integers that correspond to the positions of the elements in pars. For example results[0] contains the results of the simulation run with state and parameters specified by pars[0]["state"] and pars[0]["parameters"]. If a problem occurs when running the simulations, an empty dictionary is returned.

Return type:

dict

class estimationpy.fmu_utils.fmu_pool.P(model, x0, pars, startTime, stopTime, results_queue, index)

This class represents a process running a single simulation of an FMU model. Multiple instances of this class can be executed simultaneously to run simulations in parallel on multiple processors.

The class has the following attributes:

  • model, that is an instance of the class estimationpy.fmu_utils.model.Model,
  • x0, the initial state of the model,
  • pars, the parameters to be modified before simulating the model,
  • startTime, the initial time of the simulation period,
  • stopTime, the end time of the simulation period,
  • result_queue, a queue of type multiprocesing.Queue where all the results of the simulations are stored and can be retrieved after the simulations are terminated,
  • index, an integer that is used to sort the results data by the class that manages a pool of processes,
__init__(model, x0, pars, startTime, stopTime, results_queue, index)

Constructor of the class initialing the process that runs the simulation.

Parameters:
  • model (estimationpy.fmu_utils.model.Model) – The model to simulate
  • numpy.array – the vector containing the initial state of the model
  • pars (np.array) – a list or an ordered iterable objects containing the values of the parameters that have to be estimated and are defined in the model.
  • startTime (datetime.datetime) – the initial time of the simulation period
  • stopTime (datetime.datetime) – the end time of the simulation period
  • result_queue (multiprocesing.Queue) – the queue that stores the results of the simulation,
  • index (int) – the index used to save data in the queue, this is used to identify who generated the results during the post processing phase.
run()

Method that is called when the start() method of this class is invoked. The method executes the following steps:

  1. Sets the values of the selected states,
  2. Sets the values of the parameters selected,
  3. Creates a folder that will contain the results of the simulation in case PyFMI is configured to write to the file system,
  4. Run the simulation by calling the method estimationpy.fmu_utils.model.Model.simulate()
  5. Saves the results in the queue using the specified index,
  6. Removes the folder containing the result data if they were written.
Returns:False, is there are problem during the simulation, None otherwise.
estimationpy.fmu_utils.fmu_pool.threaded_function(queue, results, N_RESULTS)

This is a function executed in the main thread that reads the values in the queue, and moves them to a dictionary. The function, and thus the thread, terminates when all the expected results have been read. The number of expected results is specified by the parameter N_RESULTS.

Parameters:
  • queue (multiprocesing.Queue) – the queue containing the results generated by the processes.
  • results (dict) – reference to a dictionary where the results enqueued are moved.
  • N_RESULTS (int) – the number of results to dequeue and move to the dictionary.