Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maze solver #115

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
134 changes: 134 additions & 0 deletions examples/S99_more/maze/maze.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
-0.5 -0.5 -0.5 9.5
-0.5 9.5 9 9.5
9.5 -0.5 9.5 9.5
0 -0.5 9.5 -0.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.5 5.5 0.5 6.5
0.5 7.5 -0.5 7.5
-0.5 7.5 0.5 7.5
0.5 7.5 0.5 8.5
1.5 -0.5 1.5 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 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.5 6.5 0.5 5.5
0.5 6.5 1.5 6.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
1.5 2.5 1.5 1.5
2.5 3.5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.5 5.5 3.5
5.5 4.5 5.5 3.5
5.5 3.5 6.5 3.5
6.5 5.5 5.5 5.5
5.5 5.5 6.5 5.5
6.5 7.5 5.5 7.5
5.5 7.5 5.5 6.5
5.5 7.5 6.5 7.5
7.5 -0.5 7.5 0.5
6.5 0.5 6.5 -0.5
6.5 1.5 6.5 0.5
7.5 0.5 7.5 1.5
7.5 2.5 6.5 2.5
7.5 3.5 6.5 3.5
6.5 2.5 7.5 2.5
6.5 3.5 7.5 3.5
7.5 6.5 7.5 7.5
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.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
8.5 4.5 7.5 4.5
8.5 3.5 8.5 4.5
7.5 4.5 8.5 4.5
7.5 7.5 7.5 6.5
8.5 8.5 7.5 8.5
7.5 8.5 7.5 7.5
7.5 8.5 8.5 8.5
8.5 8.5 8.5 9.5
8.5 0.5 8.5 -0.5
9.5 1.5 8.5 1.5
8.5 1.5 9.5 1.5
9.5 2.5 8.5 2.5
8.5 3.5 8.5 2.5
8.5 2.5 9.5 2.5
8.5 4.5 8.5 3.5
9.5 5.5 8.5 5.5
8.5 5.5 9.5 5.5
8.5 9.5 8.5 8.5
56 changes: 56 additions & 0 deletions examples/S99_more/maze/maze_solve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#
# Solve a maze using brute force
#

__author__ = "Dilawar Singh"
__email__ = "[email protected]"

import math
import smoldyn

from pathlib import Path
import typing as T


def add_maze(s, mazefile) -> T.List[smoldyn.Panel]:
"""Add maze"""
panels = []
offset, scale = 1.5, 10
with open(mazefile, "r") as f:
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="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")

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

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

s.addGraphics("opengl", iter=50)
s.run(3000, dt=0.01, quit_at_end=True)

print(s.getOutputData("posdata"))
print('all done')
10 changes: 9 additions & 1 deletion source/Smoldyn/smolsim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2533,8 +2533,16 @@ int simulatetimestep(simptr sim) {
sim->time+=sim->dt; // --- end of time step ---
simsetvariable(sim,"time",sim->time);
er=simdocommands(sim);
if(er) return er;
#ifdef COMPILE_AS_PY_MODULE
if(er == 7) {
fprintf(stdout, "command stop");
sim->tmax = sim->time;
sim->tbreak = sim->time;
er = 0;
}
#endif

if(er) return er;

if(sim->time>=sim->tmax) return 1;
if(sim->time>=sim->tbreak) return 10;
Expand Down
1 change: 0 additions & 1 deletion source/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ target_compile_options(_pysmoldyn PRIVATE -DENABLE_PYTHON_CALLBACK)
# especially where scanf is used for user input.
target_compile_options(_pysmoldyn PRIVATE -DCOMPILE_AS_PY_MODULE)


target_include_directories(_pysmoldyn PRIVATE ${Python3_INCLUDE_DIRS} ${PYBIND11_SOURCE_DIR}/include)

# This is suggested by pybind11: reduces binary size and disables some warnings.
Expand Down
Loading