Binary tree class used for specifying a cell partition of arbitrary sized chunks for use as the `chunk_layout` parameter of the `Simulation` class object.
| 6352 | |
| 6353 | |
| 6354 | class BinaryPartition: |
| 6355 | """ |
| 6356 | Binary tree class used for specifying a cell partition of arbitrary sized chunks for use as the |
| 6357 | `chunk_layout` parameter of the `Simulation` class object. |
| 6358 | """ |
| 6359 | |
| 6360 | def __init__( |
| 6361 | self, |
| 6362 | data=None, |
| 6363 | split_dir=None, |
| 6364 | split_pos=None, |
| 6365 | left=None, |
| 6366 | right=None, |
| 6367 | proc_id=None, |
| 6368 | ): |
| 6369 | """ |
| 6370 | The constructor accepts three separate groups of arguments: (1) `data`: a list of lists where each |
| 6371 | list entry is either (a) a node defined as `[ (split_dir,split_pos), left, right ]` for which `split_dir` |
| 6372 | and `split_pos` define the splitting direction (i.e., `mp.X`, `mp.Y`, `mp.Z`) and position (e.g., `3.5`, |
| 6373 | `-4.2`, etc.) and `left` and `right` are the two branches (themselves `BinaryPartition` objects) |
| 6374 | or (b) a leaf with integer value for the process ID `proc_id` in the range between 0 and number of processes |
| 6375 | - 1 (inclusive), (2) a node defined using `split_dir`, `split_pos`, `left`, and `right`, or (3) a leaf with |
| 6376 | `proc_id`. Note that the same process ID can be assigned to as many chunks as you want, which means that one |
| 6377 | process timesteps multiple chunks. If you use fewer MPI processes, then the process ID is taken modulo the number |
| 6378 | of processes. |
| 6379 | """ |
| 6380 | self.split_dir = None |
| 6381 | self.split_pos = None |
| 6382 | self.proc_id = None |
| 6383 | self.left = None |
| 6384 | self.right = None |
| 6385 | if data is not None: |
| 6386 | if isinstance(data, list) and len(data) == 3: |
| 6387 | if isinstance(data[0], tuple) and len(data[0]) == 2: |
| 6388 | self.split_dir = data[0][0] |
| 6389 | self.split_pos = data[0][1] |
| 6390 | else: |
| 6391 | raise ValueError( |
| 6392 | "expecting 2-tuple (split_dir,split_pos) but got {}".format( |
| 6393 | data[0] |
| 6394 | ) |
| 6395 | ) |
| 6396 | self.left = BinaryPartition(data=data[1]) |
| 6397 | self.right = BinaryPartition(data=data[2]) |
| 6398 | elif isinstance(data, int): |
| 6399 | self.proc_id = data |
| 6400 | else: |
| 6401 | raise ValueError( |
| 6402 | "expecting list [(split_dir,split_pos), left, right] or int (proc_id) but got {}".format( |
| 6403 | data |
| 6404 | ) |
| 6405 | ) |
| 6406 | elif split_dir is not None: |
| 6407 | self.split_dir = split_dir |
| 6408 | self.split_pos = split_pos |
| 6409 | self.left = left |
| 6410 | self.right = right |
| 6411 | else: |