Skip to content

Commit 2e0a6d0

Browse files
committed
updated progress on numba and CA files. Still missing full analysis progress and testing report
1 parent 3ff87ef commit 2e0a6d0

1 file changed

Lines changed: 141 additions & 3 deletions

File tree

docs/kimon_updates.md

Lines changed: 141 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,150 @@
11
## Refactoring and Optimization Update
22

3-
### CA Class
3+
---
44

5+
### CA Class (Simulation Engine)
56

6-
### Numba Optimization
7+
Method: ```update_async``` method
78

9+
This method is the primary interface for the async update mode in the PP model. Instead of iterating through the grid in Python, we prepare the data and leaves the heavy computations for the Numba kernel
810

9-
### HPC Script Functionality
11+
Kernel execution: ```self._kernel.update(..)``` uses pre-allocated ```PPKernel``` to modify the gruid and parameter arrays on place.
1012

1113

14+
We mutate the ```grid``` and the ```p_death_arr`` at the same time.
15+
16+
---
17+
18+
### Numba Optimization (```numba_optimization.py```)
19+
20+
Utility: ```set_numba_seed```
21+
22+
Synchronize the RNG used by Numba's JIT compiled functions with a specific seed. Becuase Numba compiles Python code into machine code, it maintains its own internal state for ```numpy.random```. We have global seeding this way under the ```@njit``` decorator
23+
24+
---
25+
26+
Core Logic: ```_pp_async_kernel_random```
27+
28+
This is the engine room of the sim. We do the stochastic async update of the entire grid in a single pass:
29+
30+
1. Update Order with Fisher-Yates shuffle
31+
2. Cellular Interactions
32+
3. Evolutionary Inheritance
33+
34+
This function is called internally in the ```PPKernel.update``` method and is not intended to be called directly by the user.
35+
36+
37+
We use the ```occupied_buffer``` which is a pre-allocated array. We recyle memory instead of creating a new list of active cells at each interation.
38+
39+
Core Logic: ```_pp_async_kernel_directed```
40+
41+
Similar to the previous function but also implements hunting logic. This kernel is activated by passing ```directed_hunting = True``` during the initialization of the ```PP``` model or the ```PPKernel``` wrapper
42+
43+
---
44+
45+
Class: ```PPKernel```
46+
47+
Handles the logistical overhead of the simulation.
48+
49+
- Memory pre-allocation: Initialize an ```occupied_buffer``` during setup. Used in each time step to store the coords of active cells. We don't need to constantly allocate and deallocate memory this way.
50+
51+
- Kernel dispatching: Directs simualtion data to to the appropriate specialized numba function (random or hunting)
52+
53+
---
54+
55+
Spatial Optimization ```_build_cell_list```
56+
57+
Oraganizes particles into a list of cells. We only compare particles to those in the same or adjacent cells.
58+
59+
---
60+
61+
Spatial Stats: ```_pcf_cell_list```
62+
63+
This kernel computes a histogram of distances between two sets of particles. It determines if species are clustered, segregated or randomly distributed.
64+
65+
If ```self_corrrelation = True``` it optimizes the process by only finding the upper triangle of the interaction matrix and doubling the result. We use ```prange``` to distribute the calculation across all available CPU cores.
66+
67+
68+
---
69+
70+
Spatial Interpretation: ```compute_pcf_periodic_fast```
71+
72+
Wrapper that transforms the distance counts into a normalized PCF function. THis value represents the density of particles at a distance r relative to a completely random distrubution.
73+
74+
For the hydra effect, we look at the specific signature on the PCF.
75+
76+
77+
---
78+
79+
Algorithm: ```_flood_fill```
80+
81+
Stack-based implementation that explores all neighborhoods of a starting cell. It marks cells as visited to avoid double counting and returns the total count of cells in a specific clsuter.
82+
83+
---
84+
85+
Kernel: ```_measure_clusters```
86+
87+
Iterate through the grid. When we find a cell of the target species that has not been visited yet, trigger a new flood fill to map out that entire cluster.
88+
89+
---
90+
91+
Wrapper: ```measure_cluster_sizes_fast```
92+
93+
Identify and measure every discrete cluster for a specific species on the grid. We ue a high speed flood fill to paint and count contiguous cells.
94+
95+
96+
The hypothesis for the prey hydra effect is that increased mortality forces prey into tight more resilent clusters. We measure changes inm the average and max cluster size across a parameter sweep to test the spatial hypothesis.
97+
98+
---
99+
100+
### HPC Script Functionality (```pp_analysis.py```)
101+
102+
Class: ```Config``
103+
104+
Central configuration for analysis. Set grid dimensions, intial densities, resolution, death rate range, replications, warmup period, measurement window, stationarity, PCF sampling rates, mutation rate, fine size scaling grid sizes, and equilibrium scaling
105+
106+
Utility:```estimate_runtime```
107+
108+
HPC resource management tool. Estimates total simualtion runtime based on selected configurtion.
109+
110+
---
111+
112+
Data Packing Utility: ```save_sweep_binary```
113+
114+
This function transforms the in-memory Python objects into binary archive. We use array casting ```np.array(val)``` to store NumPy objects.
115+
116+
Structural Reconstruction: ```load_sweep_binary```
117+
118+
Performs the inverse operatiion taking flat binary files and rebuilding the Python list of dictionaries required for plotting.
119+
120+
We use ```np.savez_compressed``` to reduce disk footprint for large arrays such as PCF results.
121+
---
122+
123+
124+
Core Execution Unut: ```run_single_simulation```
125+
126+
Before updating the cell list, we set the seed and environment setup with RNG Synchronization, Model Instantiation and Evolution Activation.
127+
128+
129+
We divide simulation into two stages to examine long-term behavior and avoid initialization bias. The model runs for ```cfg.warmup_steps``` and an additional ```cfg.measurement_steps``` to record data.
130+
131+
The script also measures cluster sizes using ```measure_cluster_sizes_fast``` and uses a ```pcf_sample_rate``` to claculate correlation functions on selected runs.
132+
133+
After each iteration, we flag survivals, evolved statistics, and clustering indices.
134+
135+
---
136+
137+
Finite size scaling Utility: ```run_fass```
138+
139+
Before running the scaling sweep, we perform a sanity check to make sure the system is in an intresting regime (meaning near a critical point).
140+
141+
We check if $\tau$ is near the critical value (2.05 approx.) after anchoring the simulation at a specific point in the parameter space.
142+
143+
Since larger systems usually take longer to each SS, we scale the warmup and measurement steps linearly. For each grid size, we record the mean population, the powetr law exponent and its SDE.
144+
145+
146+
---
147+
148+
Phase Space Utility: ```run_2d_sweep```
149+
12150
### Tests

0 commit comments

Comments
 (0)