From 0c3b83364878044a2aa2563a91b6022012ec0fb2 Mon Sep 17 00:00:00 2001
From: Thorsten Hater <24411438+thorstenhater@users.noreply.github.com>
Date: Thu, 23 Jun 2022 14:42:56 +0200
Subject: [PATCH] Diffusion Example Improvements (and a bit of clean-up)
 (#1914)

Post-Merge clean-up for the diffusion PR
- remove redundant `WRITE`s in mechanisms
- diffusion.py is now closer in functionality to diffusion.cpp
- diffusion.py now prints a table of results
---
 mechanisms/default/decay.mod   |  7 +++++--
 mechanisms/default/inject.mod  |  2 +-
 python/example/diffusion.py    | 28 +++++++++++++++++++---------
 scripts/run_python_examples.sh |  1 +
 4 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/mechanisms/default/decay.mod b/mechanisms/default/decay.mod
index a380d5fd..3dadbb83 100644
--- a/mechanisms/default/decay.mod
+++ b/mechanisms/default/decay.mod
@@ -1,8 +1,11 @@
 NEURON {
     SUFFIX decay
-    USEION x WRITE xd, ix
+    USEION x WRITE xd
+    RANGE F, tau
 }
 
+PARAMETER { tau = 5 }
+
 INITIAL { F = xd }
 
 STATE { F }
@@ -14,5 +17,5 @@ BREAKPOINT {
 
 DERIVATIVE dF {
    F = xd
-   F' = -5*F
+   F' = -tau*F
 }
diff --git a/mechanisms/default/inject.mod b/mechanisms/default/inject.mod
index 39289576..5eb469cc 100644
--- a/mechanisms/default/inject.mod
+++ b/mechanisms/default/inject.mod
@@ -1,6 +1,6 @@
 NEURON {
   POINT_PROCESS inject
-  USEION x WRITE xd, ix
+  USEION x WRITE xd
   RANGE alpha, beta
 }
 
diff --git a/python/example/diffusion.py b/python/example/diffusion.py
index 5198b4cb..438ae0d5 100644
--- a/python/example/diffusion.py
+++ b/python/example/diffusion.py
@@ -28,14 +28,18 @@ class recipe(A.recipe):
     def global_properties(self, kind):
         return self.the_props
 
+    def event_generators(self, gid):
+        return [A.event_generator("Zap", 0.005, A.explicit_schedule([0.0]))]
+
 
 tree = A.segment_tree()
 s = tree.append(A.mnpos, A.mpoint(-3, 0, 0, 3), A.mpoint(3, 0, 0, 3), tag=1)
 _ = tree.append(s, A.mpoint(3, 0, 0, 1), A.mpoint(33, 0, 0, 1), tag=3)
 
 dec = A.decor()
-dec.set_property(Vm=-40)
-# dec.paint('(tag 1)', A.density('hh'))
+dec.set_ion("na", int_con=0.0, diff=0.005)
+dec.place("(location 0 0.5)", A.synapse("inject/x=na", {"alpha": 200.0}), "Zap")
+dec.paint("(all)", A.density("decay/x=na"))
 dec.discretization(A.cv_policy("(max-extent 5)"))
 
 # Set up ion diffusion
@@ -47,9 +51,7 @@ prb = [
 ]
 cel = A.cable_cell(tree, A.label_dict(), dec)
 rec = recipe(cel, prb)
-ctx = A.context()
-dom = A.partition_load_balance(rec, ctx)
-sim = A.simulation(rec, dom, ctx)
+sim = A.simulation(rec)
 hdl = (sim.sample((0, 0), A.regular_schedule(0.1)),)
 
 sim.run(tfinal=0.5)
@@ -58,10 +60,18 @@ sns.set_theme()
 fg, ax = plt.subplots()
 for h in hdl:
     for d, m in sim.samples(h):
-        xs = d[:, 0]
+        # Plot
         for lbl, ix in zip(m, range(1, d.shape[1])):
-            ys = d[:, ix]
-            print(lbl, ys.min(), ys.max())
-            ax.plot(xs, ys, label=lbl)
+            ax.plot(d[:, 0], d[:, ix], label=lbl)
+        # Table
+        print("Sodium concentration (NaD/mM)")
+        print("|-" + "-+-".join("-" * 20 for _ in range(d.shape[1])) + "-|")
+        print(
+            "| Time (ms)            | " + " | ".join(f"{str(l):<20}" for l in m) + " |"
+        )
+        print("|-" + "-+-".join("-" * 20 for _ in range(d.shape[1])) + "-|")
+        for ix in range(d.shape[0]):
+            print("| " + " | ".join(f"{v:>20.3f}" for v in d[ix, :]) + " |")
+        print("|-" + "-+-".join("-" * 20 for _ in range(d.shape[1])) + "-|")
 ax.legend()
 fg.savefig("results.pdf")
diff --git a/scripts/run_python_examples.sh b/scripts/run_python_examples.sh
index 23344437..557bd5e3 100755
--- a/scripts/run_python_examples.sh
+++ b/scripts/run_python_examples.sh
@@ -28,3 +28,4 @@ $PREFIX python python/example/single_cell_recipe.py
 $PREFIX python python/example/single_cell_stdp.py
 $PREFIX python python/example/single_cell_swc.py python/example/single_cell_detailed.swc
 $PREFIX python python/example/two_cell_gap_junctions.py
+$PREFIX python python/example/diffusion.py
-- 
GitLab