Skip to content

Commit

Permalink
Pass global IDs to micro simulations to make them unique (#66)
Browse files Browse the repository at this point in the history
* Pass ID to micro simulations

* Pass ID to micro simulation class constructors in dummies

* Formatting

* Pass the global ID in the state of the C++ micro simulation in the dummy

* Fix C++ solver dummy

* Remove function set_global_id from Simulation class, as we should never have to change the global ID of a micro simulation

* Change documentation to add passing global id to simulation during defining
  • Loading branch information
IshaanDesai authored Feb 2, 2024
1 parent ea791d7 commit 13fe880
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 13 deletions.
7 changes: 6 additions & 1 deletion docs/micro-simulation-convert-to-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions examples/cpp-dummy/micro_cpp_dummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -64,7 +64,7 @@ PYBIND11_MODULE(micro_dummy, m) {
m.doc() = "pybind11 micro dummy plugin";

py::class_<MicroSimulation>(m, "MicroSimulation")
.def(py::init())
.def(py::init<int>())
.def("solve", &MicroSimulation::solve)
.def("get_state", &MicroSimulation::get_state)
.def("set_state", &MicroSimulation::set_state)
Expand All @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion examples/cpp-dummy/micro_cpp_dummy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ 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);

void set_state(py::list state);
py::list get_state() const;

private:
int _sim_id;
double _micro_scalar_data;
std::vector<double> _micro_vector_data;
double _state;
Expand Down
3 changes: 2 additions & 1 deletion examples/python-dummy/micro_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions micro_manager/micro_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion tests/integration/test_unit_cube/micro_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class MicroSimulation:
def __init__(self):
def __init__(self, sim_id):
self.very_important_value = 0

def initialize(self):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 13fe880

Please sign in to comment.