diff --git a/docs/micro-simulation-convert-to-library.md b/docs/micro-simulation-convert-to-library.md index 7ed8dd48..a145852f 100644 --- a/docs/micro-simulation-convert-to-library.md +++ b/docs/micro-simulation-convert-to-library.md @@ -14,9 +14,14 @@ Restructure your micro simulation code into a Python class with the structure gi ```python class MicroSimulation: # Name is fixed - def __init__(self): + def __init__(self, sim_id): """ Constructor of class MicroSimulation. + + Parameters + ---------- + sim_id : int + ID of the simulation instance, that the Micro Manager has set for it. """ def solve(self, macro_data: dict, dt: float) -> dict: diff --git a/examples/cpp-dummy/micro_cpp_dummy.cpp b/examples/cpp-dummy/micro_cpp_dummy.cpp index df922470..dd0b6863 100644 --- a/examples/cpp-dummy/micro_cpp_dummy.cpp +++ b/examples/cpp-dummy/micro_cpp_dummy.cpp @@ -13,7 +13,7 @@ #include "micro_cpp_dummy.hpp" // Constructor -MicroSimulation::MicroSimulation() : _micro_scalar_data(0), _state(0) {} +MicroSimulation::MicroSimulation(int sim_id) : _sim_id(sim_id), _micro_scalar_data(0), _state(0) {} // Solve py::dict MicroSimulation::solve(py::dict macro_data, double dt) @@ -64,7 +64,7 @@ PYBIND11_MODULE(micro_dummy, m) { m.doc() = "pybind11 micro dummy plugin"; py::class_(m, "MicroSimulation") - .def(py::init()) + .def(py::init()) .def("solve", &MicroSimulation::solve) .def("get_state", &MicroSimulation::get_state) .def("set_state", &MicroSimulation::set_state) @@ -77,7 +77,7 @@ PYBIND11_MODULE(micro_dummy, m) { throw std::runtime_error("Invalid state!"); /* Create a new C++ instance */ - MicroSimulation ms; + MicroSimulation ms(0); ms.set_state(t); diff --git a/examples/cpp-dummy/micro_cpp_dummy.hpp b/examples/cpp-dummy/micro_cpp_dummy.hpp index d370afc2..fb230ea1 100644 --- a/examples/cpp-dummy/micro_cpp_dummy.hpp +++ b/examples/cpp-dummy/micro_cpp_dummy.hpp @@ -14,7 +14,7 @@ namespace py = pybind11; class MicroSimulation { public: - MicroSimulation(); + MicroSimulation(int sim_id); // solve takes a python dict data, and the timestep dt as inputs, and returns a python dict py::dict solve(py::dict macro_write_data, double dt); @@ -22,6 +22,7 @@ class MicroSimulation py::list get_state() const; private: + int _sim_id; double _micro_scalar_data; std::vector _micro_vector_data; double _state; diff --git a/examples/python-dummy/micro_dummy.py b/examples/python-dummy/micro_dummy.py index f267d3b9..eeed2984 100644 --- a/examples/python-dummy/micro_dummy.py +++ b/examples/python-dummy/micro_dummy.py @@ -6,10 +6,11 @@ class MicroSimulation: - def __init__(self): + def __init__(self, sim_id): """ Constructor of MicroSimulation class. """ + self._sim_id = sim_id self._dims = 3 self._micro_scalar_data = None self._micro_vector_data = None diff --git a/micro_manager/micro_simulation.py b/micro_manager/micro_simulation.py index df505bb3..902eaba1 100644 --- a/micro_manager/micro_simulation.py +++ b/micro_manager/micro_simulation.py @@ -21,13 +21,10 @@ def create_simulation_class(micro_simulation_class): """ class Simulation(micro_simulation_class): def __init__(self, global_id): - micro_simulation_class.__init__(self) + micro_simulation_class.__init__(self, global_id) self._global_id = global_id def get_global_id(self) -> int: return self._global_id - def set_global_id(self, global_id) -> None: - self._global_id = global_id - return Simulation diff --git a/tests/integration/test_unit_cube/micro_dummy.py b/tests/integration/test_unit_cube/micro_dummy.py index 59bef26c..433967ff 100644 --- a/tests/integration/test_unit_cube/micro_dummy.py +++ b/tests/integration/test_unit_cube/micro_dummy.py @@ -6,10 +6,11 @@ class MicroSimulation: - def __init__(self): + def __init__(self, sim_id): """ Constructor of MicroSimulation class. """ + self._sim_id = sim_id self._micro_scalar_data = None self._micro_vector_data = None self._checkpoint = None diff --git a/tests/unit/test_micro_manager.py b/tests/unit/test_micro_manager.py index e0111d4a..ade67b32 100644 --- a/tests/unit/test_micro_manager.py +++ b/tests/unit/test_micro_manager.py @@ -5,7 +5,7 @@ class MicroSimulation: - def __init__(self): + def __init__(self, sim_id): self.very_important_value = 0 def initialize(self): @@ -83,7 +83,7 @@ def test_solve_mico_sims(self): """ manager = micro_manager.MicroManager('micro-manager-config.json') manager._local_number_of_sims = 4 - manager._micro_sims = [MicroSimulation() for _ in range(4)] + manager._micro_sims = [MicroSimulation(i) for i in range(4)] manager._micro_sims_active_steps = np.zeros(4, dtype=np.int32) micro_sims_output = manager._solve_micro_simulations(self.fake_read_data)