Skip to content

Commit

Permalink
A bit more sparse maze
Browse files Browse the repository at this point in the history
  • Loading branch information
Dilawar Singh committed Nov 15, 2021
1 parent 488481b commit f0d54c8
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 79 deletions.
21 changes: 12 additions & 9 deletions examples/S15_python/change_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
r1_ = None
bouton_ = None


def generate_spike(t, args):
x = random.random()
if x < 0.1:
Expand All @@ -19,7 +20,7 @@ def update_kf(val):
global r1_, bouton_
r1_.kf = 1000 ** val
c = min(1.0, val)
bouton_.setStyle('both', color=[c, c, c])
bouton_.setStyle("both", color=[c, c, c])


def build_model_smoldyn():
Expand All @@ -31,7 +32,9 @@ def build_model_smoldyn():
"""
global r1_, bouton_
s = smoldyn.Simulation(low=[-500, -500], high=[1500, 1500])
sv = s.addSpecies("SV", difc=dict(all=2400, front=10), color="blue", display_size=10)
sv = s.addSpecies(
"SV", difc=dict(all=2400, front=10), color="blue", display_size=10
)
sv.addToSolution(100, lowpos=(0, 0), highpos=(1000, 1000))

# add a reaction with generates the sv at a fixed rate. Its better to
Expand All @@ -47,17 +50,17 @@ def build_model_smoldyn():
decay = s.addReaction("decay", subs=[trans], prds=[], rate=math.log(2) / 20e-3)

# BOUTON
path = s.addPath2D((1000, 0), (1000, 1000), (0, 1000), (0, 0))
path = s.addPath2D([(1000, 0), (1000, 1000), (0, 1000), (0, 0)])
bouton_ = s.addSurface("bouton", panels=path.panels)
bouton_.setStyle('both', color="blue")
bouton_.setAction('both', [sv], "reflect")
bouton_.setStyle("both", color="blue")
bouton_.setAction("both", [sv], "reflect")

# this is the bottom surface of bouton. This is sticky for synaptic
# vesciles
rect1 = smoldyn.Rectangle(corner=(0, 0), dimensions=[1000], axis="+y")
bottom = s.addSurface("boutonBottom", panels=[rect1])
bottom.setStyle('both', color="red")
bottom.setAction('back', trans, "reflect") # but it reflect neurotranmitter
bottom.setStyle("both", color="red")
bottom.setAction("back", trans, "reflect") # but it reflect neurotranmitter

# SV stick to bottom of the bouton and also detach back with a smaller
# rate.
Expand All @@ -72,14 +75,14 @@ def build_model_smoldyn():
s.connect(generate_spike, update_kf, step=20)

s.addGraphics("opengl", iter=10, text_display="time")
print('[INFO] Starting simulation ...')
print("[INFO] Starting simulation ...")
s.run(stop=20, dt=0.001)
print("Done")


def main():
build_model_smoldyn()


if __name__ == "__main__":
main()

2 changes: 1 addition & 1 deletion examples/S15_python/integrate_with_moose.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def update_kf(val):
s.addReaction("decay", subs=[trans], prds=[], rate=math.log(2)/20e-3)

# BOUTON
path = s.addPath2D((1000, 0), (550, 1000), (450, 1000), (0, 0))
path = s.addPath2D([(1000, 0), (550, 1000), (450, 1000), (0, 0)])
bouton_ = s.addSurface("bouton", panels=path.panels)
bouton_.setStyle('both', color="blue")
bouton_.setAction('both', [sv], "reflect")
Expand Down
27 changes: 16 additions & 11 deletions examples/S15_python/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
__author__ = "Dilawar Singh"
__email__ = "[email protected]"

import smoldyn
import smoldyn

# Model parameters
K_FWD = 0.001 # substrate-enzyme association reaction rate
K_BACK = 1 # complex dissociation reaction rate
K_PROD = 1 # complex reaction rate to product
K_FWD = 0.001 # substrate-enzyme association reaction rate
K_BACK = 1 # complex dissociation reaction rate
K_PROD = 1 # complex reaction rate to product

# Simulation starts with declaring a Simulation object with the system boundaries.
s = smoldyn.Simulation(low=[-1, -1], high=[1, 1])
Expand All @@ -36,30 +36,35 @@
# as well. Here, `all` molecules reflect at both surface faces.
sph1 = smoldyn.Sphere(center=(0, 0), radius=1, slices=50)
membrane = s.addSurface("membrane", panels=[sph1])
membrane.setAction('both', [S, E, P, ES], "reflect")
membrane.setStyle('both', color="black", thickness=1)
membrane.setAction("both", [S, E, P, ES], "reflect")
membrane.setStyle("both", color="black", thickness=1)

# Define a compartment, which is region inside the 'membrane' surface.
inside = s.addCompartment(name="inside", surface=membrane, point=[0, 0])

# Chemical reactions. Here, E + S <-> ES -> P
r1 = s.addBidirectionalReaction(
"r1", subs=[(E,"front"), (S,"bsoln")], prds=[(ES,"front")], kf=K_FWD, kb=K_BACK)
"r1", subs=[(E, "front"), (S, "bsoln")], prds=[(ES, "front")], kf=K_FWD, kb=K_BACK
)
r1.reverse.productPlacement("pgemmax", 0.2)

r2 = s.addReaction(
"r2", subs=[(ES, "front")], prds=[(E, "front"), (P, "bsoln")], rate=K_PROD)
"r2", subs=[(ES, "front")], prds=[(E, "front"), (P, "bsoln")], rate=K_PROD
)

# Place molecules for initial condition
inside.addMolecules(S, 500)
membrane.addMolecules((E, "front"), 100)

# Output and other run-time commands
s.setOutputFile('templateout.txt', True)
s.setOutputFile("templateout.txt", True)
s.addCommand("molcountheader templateout.txt", "B")
s.addCommand("molcount templateout.txt", "N", step=10)

s.setGraphics(
"opengl_good", bg_color="white", frame_thickness=1,
text_display=["time", S, (E, "front"), (ES, "front"), P] )
"opengl_good",
bg_color="white",
frame_thickness=1,
text_display=["time", S, (E, "front"), (ES, "front"), P],
)
s = s.run(stop=10, dt=0.01)
14 changes: 8 additions & 6 deletions examples/S7_surfaces/stick2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,28 @@

s = smoldyn.Simulation(low=[0, 0], high=[100, 100])
red = s.addSpecies("red", color="red", difc=dict(all=3, front=0), display_size=5)
yellow = s.addSpecies("yellow", color="black", difc=dict(soln=3, back=1), display_size=5)
yellow = s.addSpecies(
"yellow", color="black", difc=dict(soln=3, back=1), display_size=5
)
blue = s.addSpecies("blue", color="blue", difc=3, display_size=5)

red.addToSolution(100)
yellow.addToSolution(50, pos=(50, 50))
blue.addToSolution(50, pos=(20, 20))

# Construct a closed path in 2D.
p = s.addPath2D((0, 0), (100, 0), (100, 100), (0, 100), closed=True)
p = s.addPath2D([(0, 0), (100, 0), (100, 100), (0, 100)], closed=True)
walls = s.addSurface("walls", panels=p.panels)
walls.setAction('both', [red, yellow, blue], "reflect")
walls.setStyle('both', color="black")
walls.setAction("both", [red, yellow, blue], "reflect")
walls.setStyle("both", color="black")

sph = smoldyn.Sphere(center=(50, 50), radius=20, slices=20)
surf = s.addSurface("stick", panels=[sph])
surf.setRate(red, "fsoln", "front", rate=1, revrate=0.1)
surf.setRate(yellow, "bsoln", "back", rate=1, revrate=0.1)
surf.setRate(blue, "fsoln", "bsoln", rate=1, revrate=0.1)
surf.setStyle('front', color=(1, 0.7, 0))
surf.setStyle('back', color=(0.6, 0, 0.6))
surf.setStyle("front", color=(1, 0.7, 0))
surf.setStyle("back", color=(0.6, 0, 0.6))
surf.addMolecules((red, "front"), 100)

s.setGraphics("opengl")
Expand Down
22 changes: 9 additions & 13 deletions examples/S99_more/maze/maze.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@
-0.5 9.5 9 9.5
9.5 -0.5 9.5 9.5
0 -0.5 9.5 -0.5
0.49999999999999994 -0.5 0.5 0.5
0.5 1.5 -0.5 1.5
-0.5 1.5 0.5 1.5
0.5 -0.5 0.5 0.5
0.5 2.5 -0.5 2.5
-0.5 2.5 0.5 2.5
0.5 4.5 -0.5 4.5
-0.5 4.5 0.5 4.5
0.49999999999999994 5.5 0.5 6.5
0.5 5.5 0.5 6.5
0.5 7.5 -0.5 7.5
-0.5 7.5 0.5 7.5
0.49999999999999994 7.5 0.5 8.5
0.5 7.5 0.5 8.5
1.5 -0.5 1.5 0.5
0.5000000000000001 0.5 0.4999999999999999 -0.5
0.5 0.5 0.5 -0.5
1.5 1.5 0.5 1.5
0.5 1.5 1.5 1.5
1.5 1.5 1.5 2.5
1.5 5.5 0.5 5.5
1.5 6.5 0.5 6.5
1.5 5.5 1.5 6.5
0.5 5.5 1.5 5.5
0.5000000000000001 6.5 0.4999999999999999 5.5
0.5 6.5 0.5 5.5
0.5 6.5 1.5 6.5
0.5000000000000001 8.5 0.4999999999999999 7.5
0.5 8.5 0.5 7.5
1.5 7.5 1.5 8.5
1.5 0.5 1.5 -0.5
2.5 2.5 1.5 2.5
Expand All @@ -36,7 +33,7 @@
2.5 3.5 2.5 4.5
1.5 6.5 1.5 5.5
1.5 8.5 1.5 7.5
3.5 0.49999999999999994 2.5 0.5000000000000001
3.5 0.5 2.5 0.5
3.5 1.5 2.5 1.5
3.5 0.5 3.5 1.5
2.5 0.5 3.5 0.5
Expand Down Expand Up @@ -68,7 +65,6 @@
4.5 7.5 3.5 7.5
3.5 7.5 4.5 7.5
3.5 9.5 3.5 8.5
5.5 -0.5 5.5 0.5
5.5 0.5 5.5 1.5
5.5 1.5 4.5 1.5
4.5 1.5 5.5 1.5
Expand All @@ -83,7 +79,7 @@
5.5 6.5 5.5 7.5
5.5 8.5 4.5 8.5
4.5 8.5 5.5 8.5
6.5 0.49999999999999994 5.5 0.5000000000000001
6.5 0.5 5.5 0.5
6.5 -0.5 6.5 0.5
5.5 0.5 5.5 -0.5
6.5 1.5 5.5 1.5
Expand Down Expand Up @@ -114,7 +110,7 @@
7.5 7.5 7.5 8.5
8.5 -0.5 8.5 0.5
7.5 0.5 7.5 -0.5
8.5 0.49999999999999994 7.5 0.5000000000000001
8.5 0.5 7.5 0.5
7.5 1.5 7.5 0.5
7.5 0.5 8.5 0.5
8.5 2.5 8.5 3.5
Expand Down
55 changes: 25 additions & 30 deletions examples/S99_more/maze/maze_solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,47 @@

import math
import smoldyn
from pathlib import Path


def get_axis(line):
x0, y0, x1, y1 = line
theta = math.atan2(y1 - y0, x1 - x0)
if math.isclose(theta, 0.0) or math.isclose(theta, math.pi):
return "+y"
return "+x"

from pathlib import Path
import typing as T

def add_maze(s, mazefile):
def add_maze(s, mazefile) -> T.List[smoldyn.Panel]:
"""Add maze"""
panels = []
offset = 1.5
scale = 10
offset, scale = 1.5, 10
with open(mazefile, "r") as f:
for i, l in enumerate(f.read().strip().split("\n")):
line = [scale * (offset + float(x)) for x in l.split()]
axis = get_axis(line)
x0, y0, x1, y1 = line
r = s.addRectangle(
corner=[x0, y0],
dimensions=[max((x1 - x0), (y1 - y0))],
axis=axis,
name=f"r{i}",
)
panels.append(r)
for i, ll in enumerate(f.read().strip().split("\n")):
l = [scale * (offset + float(x)) for x in ll.split()]
panels += s.addPath2D([(l[0], l[1]), (l[2], l[3])]).panels

# close the entry notch
r = s.addRectangle(
corner=[10, 10],
dimensions=[30],
axis="+y",
name=f"rrr",
)
r = s.addRectangle(corner=[10, 10], dimensions=[30], axis="+y", name="r")
panels.append(r)
return panels


s = smoldyn.Simulation(low=[0, 0], high=[120, 120])

A = s.addSpecies("A", difc=2, color="blue")
A.addToSolution(1000, pos=[15, 15])

B = s.addSpecies("B", difc=0, color='black', display_size=3)
B.addToSolution(10, lowpos=[105,112], highpos=[110,112])

panels = add_maze(s, Path(__file__).parent / "maze.txt")
maze = s.addSurface("maze", panels=panels)
maze.setAction("front", [A], "reflect")
maze.setAction("back", [A], "reflect")
maze.setStyle("both", thickness=1, color='red')
maze.setStyle("both", thickness=1, color="red")

# When any A meets B, maze is solved. So we set a reaction between A and B
s.addReaction("done", [A, B], [], rate=10)
s.addCommand("ifless B 10 text ", "E")

# Stop when A + B -> ∅ happens i.e. maze is solved.
s.addCommand("ifless B 10 pause", "E")

# TODO: Show the solution.

s.addGraphics("opengl", iter=50)
s.run(3000, dt=0.01)
33 changes: 28 additions & 5 deletions source/python/smoldyn/smoldyn.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@

from typing import Union, Tuple, List, Dict, Optional, Sequence


from smoldyn.types import Color, BoundType, ColorType, DiffConst
from smoldyn.types import Color, BoundType, ColorType, DiffConst, PointType
from smoldyn import _smoldyn

# Path of model file.
Expand Down Expand Up @@ -845,8 +844,32 @@ def setAction(
assert k == _smoldyn.ErrorCode.ok


def _axis(p1: PointType, p2: PointType) -> str:
(x1, y1), (x2, y2) = p1, p2
theta = math.atan2(y2 - y1, x2 - x1)
__logger__.debug(f"theta={theta} {p1} and {p2}")
if theta in [0.0, math.pi, math.pi / 2.0, -math.pi / 2.0]:
if math.isclose(theta, 0.0):
axis = "+y"
elif math.isclose(theta, math.pi):
axis = "-y"
elif math.isclose(theta, math.pi / 2.0):
axis = "-x"
elif math.isclose(theta, -math.pi / 2.0):
axis = "+x"
else:
raise RuntimeError(
"Should not be here Python3 numerical computation is broken!"
)


class Path2D(object):
def __init__(self, *points, simulation: _smoldyn.Simulation, closed: bool = False):
def __init__(
self,
points: List[PointType],
simulation: _smoldyn.Simulation,
closed: bool = False,
):
"""Construct a 2D path from given points.
A Path2D consists of `Rectangle` and `Triangle`.
Expand Down Expand Up @@ -2268,8 +2291,8 @@ def connect(self, func, target, step: int, args: List[float] = []):
"""
return super().connect(func, target, step, args)

def addPath2D(self, *points, closed: bool = False):
return Path2D(*points, simulation=super(), closed=closed)
def addPath2D(self, points: List[PointType], closed: bool = False):
return Path2D(points, simulation=super(), closed=closed)

def addPort(
self,
Expand Down
Loading

0 comments on commit f0d54c8

Please sign in to comment.