Using pyfmi with multiprocessing to simulate FMU Modelica

I am trying to simulate several FM Modelica modules in parallel using python / pyfmi and multiprocessing. However, I cannot return any pygmi FMI objects from subprocesses after FMU initialization. It seems that FMI pyfmi objects (e.g. pyfmi.fmi.FMUModelCS2 or pyfmi.fmi.FMUState2) are not matched. I also tried to tame the pickle, which does not work for me. With dill, objects can be torn, but this means there is no error, but it is somehow corrupted if I try to reboot them later. Does anyone have an idea on how to solve this problem? Thank!

+4
source share
3 answers

I had a similar problem when I created EstimationPy. I ended up creating a wrapper for parallel modeling of the same FMU using several processes.

I suggest you look at the implementation here https://github.com/lbl-srg/EstimationPy/blob/master/estimationpy/fmu_utils/fmu_pool.py

And for example http://lbl-srg.imtqy.com/EstimationPy/modules/examples/first_order.html#run-multiple-simulations

+3
source

The problem is that pyfmi.fmiFMUModelCS2 is a Cython class that depends on external libraries, which makes it inaccessible. Therefore, unfortunately, this is not possible.

, , , , , FMU . , .

+2

pathos , multiprocessing, dill pickle . Pool model.simulate , :

n_core = 2
n_simulation = 10
# ====

import pyfmi
model = pyfmi.load_fmu(path_fmu)

def worker(*args):
    model.reset()
    print "================> %d" % args[0]
    return model.simulate(options=dict(result_handling="memory"))["y"]


from pathos.multiprocessing import Pool
pool = Pool(n_core)
out = pool.map(worker, range(n_simulation))
pool.close()
pool.join()

, : options=dict(result_handling="memory"). , , . , , -

Exception in thread Thread-27:
Traceback (most recent call last):
  File "/home/USER/anaconda2/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/home/USER/anaconda2/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/USER/anaconda2/lib/python2.7/site-packages/multiprocess/pool.py", line 389, in _handle_results
    task = get()
  File "/home/USER/anaconda2/lib/python2.7/site-packages/dill/dill.py", line 260, in loads
    return load(file)
  File "/home/USER/anaconda2/lib/python2.7/site-packages/dill/dill.py", line 250, in load
    obj = pik.load()
  File "/home/USER/anaconda2/lib/python2.7/pickle.py", line 864, in load
    dispatch[key](self)
  File "/home/USER/anaconda2/lib/python2.7/pickle.py", line 1139, in load_reduce
    value = func(*args)
TypeError: __init__() takes exactly 2 arguments (1 given)

.

0
source

Source: https://habr.com/ru/post/1660319/


All Articles