3. Simulator

Introduction || 1. Getting started || 2. Generator || 3. Simulator || 4. Script || 5. Next steps

Next, we’ll write our simulator function or sim_f. Simulator functions perform calculations based on values from the generator. sim_specs is a dictionary containing user-defined fields and parameters.

Create a new Python file named sine_sim.py. Write the following:

examples/tutorials/simple_sine/sine_sim.py
 1import numpy as np
 2
 3
 4def sim_find_sine(InputArray, _, sim_specs):
 5    # Create an output array of a single zero
 6    OutputArray = np.zeros(1, dtype=sim_specs["out"])
 7
 8    # Set the zero to the sine of the InputArray value
 9    OutputArray["y"] = np.sin(InputArray["x"])
10
11    # Send back our output
12    return OutputArray

Our simulator function is called by a worker for every work item produced by the generator. This function calculates the sine of the passed value, and then returns it so the worker can store the result.