From a262678c1cc26bc24d53b1db2482276b54dc10bb Mon Sep 17 00:00:00 2001 From: Mark Piper Date: Fri, 21 Mar 2025 11:40:19 -0600 Subject: [PATCH 1/7] Add a diffusivity parameter to the config file --- examples/config-big.yaml | 1 + examples/config.yaml | 1 + 2 files changed, 2 insertions(+) diff --git a/examples/config-big.yaml b/examples/config-big.yaml index 4c9c2b3..db6b231 100644 --- a/examples/config-big.yaml +++ b/examples/config-big.yaml @@ -5,4 +5,5 @@ DiffusionModel: n_cols: 100 n_rows: 100 initial_location: [50, 50] + diffusivity: 12 bmi_version: "2.0" diff --git a/examples/config.yaml b/examples/config.yaml index a9e193c..e6cae2e 100644 --- a/examples/config.yaml +++ b/examples/config.yaml @@ -5,4 +5,5 @@ DiffusionModel: n_cols: 10 n_rows: 10 initial_location: [5, 5] + diffusivity: 3 bmi_version: "2.0" From cf8a5535309badffee7e580f1d467e9e474063e9 Mon Sep 17 00:00:00 2001 From: Mark Piper Date: Fri, 21 Mar 2025 11:41:07 -0600 Subject: [PATCH 2/7] Mention the pseudo diffusivity --- examples/explore-model.ipynb | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/explore-model.ipynb b/examples/explore-model.ipynb index 7371096..da9a65a 100644 --- a/examples/explore-model.ipynb +++ b/examples/explore-model.ipynb @@ -61,6 +61,7 @@ "The model uses 100 agents.\n", "Initially, all the agents are placed at position `[5,5]` on a 10 x 10 grid.\n", "By default, the model runs for 20 time steps.\n", + "It has a pseudo diffusivity of 3.\n", "\n", "Initialize the model, then call its `setup` and `update` methods to get a view of the initial conditions." ] From 9066d7bb139c4c23a814f376847be5c491467545 Mon Sep 17 00:00:00 2001 From: Mark Piper Date: Fri, 21 Mar 2025 12:12:36 -0600 Subject: [PATCH 3/7] Add a pseudo diffusivity to the model --- diffusion/diffusion_model.py | 24 +++++++++++++++--------- tests/diffusion_model_test.py | 9 +++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/diffusion/diffusion_model.py b/diffusion/diffusion_model.py index b355d52..1d73944 100644 --- a/diffusion/diffusion_model.py +++ b/diffusion/diffusion_model.py @@ -1,23 +1,25 @@ """Model diffusion through random motion of particles.""" import agentpy as ap +import numpy as np +directions = [ + (0, 0), + (1, 0), + (0, 1), + (-1, 0), + (0, -1), +] +weights = np.ones(len(directions), dtype=int) -class Particle(ap.Agent): - direction = [ - (0, 0), - (1, 0), - (0, 1), - (-1, 0), - (0, -1), - ] +class Particle(ap.Agent): def setup(self): self.displacement = [0, 0] def set_random_displacement(self): - self.displacement = self.model.random.choice(Particle.direction) + self.displacement = self.model.random.choices(directions, weights=weights).pop() class DiffusionModel(ap.Model): @@ -29,8 +31,12 @@ def setup(self): positions = (self.p.initial_location,) * self.p.agents self.grid.add_agents(self.agents, positions=positions) + self.set_diffusivity() self.histogram = None + def set_diffusivity(self): + weights[0] = self.p.diffusivity + def update(self): self.histogram = self.grid.apply(len, field="agents") self.agents.set_random_displacement() diff --git a/tests/diffusion_model_test.py b/tests/diffusion_model_test.py index e70e925..c5a821f 100644 --- a/tests/diffusion_model_test.py +++ b/tests/diffusion_model_test.py @@ -12,6 +12,7 @@ "n_cols": 6, "n_rows": 5, "initial_location": [2, 2], + "diffusivity": 2, "bmi_version": "2.0", } @@ -32,6 +33,7 @@ def test_model_has_parameters(): assert m.p.steps == PARAMETERS["steps"] assert m.p.n_cols == PARAMETERS["n_cols"] assert m.p.n_rows == PARAMETERS["n_rows"] + assert m.p.diffusivity == PARAMETERS["diffusivity"] def test_model_setup(): @@ -53,3 +55,10 @@ def test_particle_displacement(): final_positions = list(m.grid.positions.values()) for i in range(len(m.agents)): assert math.dist(initial_positions[i], final_positions[i]) <= 1.0 + + +def test_zero_diffusivity(): + PARAMETERS["diffusivity"] = 0 + m = DiffusionModel(PARAMETERS) + m.run(steps=1) + assert m.histogram[tuple(PARAMETERS["initial_location"])] == 0 From b5ea6cc547fb1fbfac8702f34cbbc07fe0b493b1 Mon Sep 17 00:00:00 2001 From: Mark Piper Date: Fri, 21 Mar 2025 13:32:31 -0600 Subject: [PATCH 4/7] Fix diffusivity so increasing it increases spread of particles I set a base weight of 10 and applied the diffusivity value to the weights attached to moving the particle from its current position. --- diffusion/diffusion_model.py | 6 ++++-- tests/diffusion_model_test.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/diffusion/diffusion_model.py b/diffusion/diffusion_model.py index 1d73944..8160df3 100644 --- a/diffusion/diffusion_model.py +++ b/diffusion/diffusion_model.py @@ -3,6 +3,8 @@ import agentpy as ap import numpy as np +BASE_WEIGHT = 10 + directions = [ (0, 0), (1, 0), @@ -10,7 +12,7 @@ (-1, 0), (0, -1), ] -weights = np.ones(len(directions), dtype=int) +weights = np.ones(len(directions), dtype=int)*BASE_WEIGHT class Particle(ap.Agent): @@ -35,7 +37,7 @@ def setup(self): self.histogram = None def set_diffusivity(self): - weights[0] = self.p.diffusivity + weights[1:] = self.p.diffusivity def update(self): self.histogram = self.grid.apply(len, field="agents") diff --git a/tests/diffusion_model_test.py b/tests/diffusion_model_test.py index c5a821f..65f1d3e 100644 --- a/tests/diffusion_model_test.py +++ b/tests/diffusion_model_test.py @@ -12,7 +12,7 @@ "n_cols": 6, "n_rows": 5, "initial_location": [2, 2], - "diffusivity": 2, + "diffusivity": 5, "bmi_version": "2.0", } @@ -61,4 +61,4 @@ def test_zero_diffusivity(): PARAMETERS["diffusivity"] = 0 m = DiffusionModel(PARAMETERS) m.run(steps=1) - assert m.histogram[tuple(PARAMETERS["initial_location"])] == 0 + assert m.histogram[tuple(PARAMETERS["initial_location"])] == PARAMETERS["agents"] From 10cb93e089d316a2338b3c4b37664abe004407a8 Mon Sep 17 00:00:00 2001 From: Mark Piper Date: Fri, 21 Mar 2025 13:35:24 -0600 Subject: [PATCH 5/7] Make pretty --- diffusion/diffusion_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diffusion/diffusion_model.py b/diffusion/diffusion_model.py index 8160df3..e24f217 100644 --- a/diffusion/diffusion_model.py +++ b/diffusion/diffusion_model.py @@ -12,7 +12,7 @@ (-1, 0), (0, -1), ] -weights = np.ones(len(directions), dtype=int)*BASE_WEIGHT +weights = np.ones(len(directions), dtype=int) * BASE_WEIGHT class Particle(ap.Agent): From 0b2eb65f10b668694efab07e51b7ef9e5c58b63d Mon Sep 17 00:00:00 2001 From: Mark Piper Date: Fri, 21 Mar 2025 13:36:04 -0600 Subject: [PATCH 6/7] Invert values of diffusivity --- examples/config-big.yaml | 2 +- examples/config.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/config-big.yaml b/examples/config-big.yaml index db6b231..ff32cb1 100644 --- a/examples/config-big.yaml +++ b/examples/config-big.yaml @@ -5,5 +5,5 @@ DiffusionModel: n_cols: 100 n_rows: 100 initial_location: [50, 50] - diffusivity: 12 + diffusivity: 2 bmi_version: "2.0" diff --git a/examples/config.yaml b/examples/config.yaml index e6cae2e..4bd2d61 100644 --- a/examples/config.yaml +++ b/examples/config.yaml @@ -5,5 +5,5 @@ DiffusionModel: n_cols: 10 n_rows: 10 initial_location: [5, 5] - diffusivity: 3 + diffusivity: 7 bmi_version: "2.0" From 202d5dbd0257c58bdcf66a1b9707f65a823458c7 Mon Sep 17 00:00:00 2001 From: Mark Piper Date: Fri, 21 Mar 2025 13:47:39 -0600 Subject: [PATCH 7/7] Match diffusivity value with config file [skip ci] --- examples/explore-model.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/explore-model.ipynb b/examples/explore-model.ipynb index da9a65a..377f48b 100644 --- a/examples/explore-model.ipynb +++ b/examples/explore-model.ipynb @@ -61,7 +61,7 @@ "The model uses 100 agents.\n", "Initially, all the agents are placed at position `[5,5]` on a 10 x 10 grid.\n", "By default, the model runs for 20 time steps.\n", - "It has a pseudo diffusivity of 3.\n", + "It has a pseudo diffusivity of 7.\n", "\n", "Initialize the model, then call its `setup` and `update` methods to get a view of the initial conditions." ]