1"""
2This does not work with pytest
3"""
4__author__ = "Dilawar Singh"
5__email__ = "dilawars@ncbs.res.in"
6
7import smoldyn
8import random
9import math
10
11random.seed(0)
12smoldyn.seed = 0
13
14# First argument is always t.
15a, avals = None, []
16
17expected_a = [
18    (0.0, 0.1),
19    (10.0, 1.0),
20    (20.0, 0.4559788891106302),
21    (30.0, 1.9129452507276277),
22    (40.0, 0.011968375907138173),
23    (50.0, 1.7451131604793488),
24    (60.0, 0.7376251462960712),
25    (70.0, 0.6951893788977833),
26    (80.0, 1.773890681557889),
27    (90.0, 0.00611134607662478),
28]
29
30
31def new_dif(t, args):
32    global a, avals
33    x, y = args
34    # note that b.difc is not still updated.
35    avals.append((t, a.difc["soln"]))
36    return x * math.sin(t) + y
37
38
39def update_difc(val):
40    global a
41    a.difc = val
42
43
44def test_connect():
45    global a, avals
46    s = smoldyn.Simulation(low=(0, 0), high=(10, 10))
47    a = s.addSpecies("a", color="black", difc=0.1)
48    s.connect(new_dif, update_difc, step=10, args=[1, 1])
49    s.run(100, dt=1)
50    for a, b in zip(avals[1:], expected_a[1:]):
51        print("test_connect", a, b)
52        assert math.isclose(a[1], b[1], rel_tol=1e-6, abs_tol=1e-6), (a[1], b[1])
53
54
55def main():
56    test_connect()
57
58
59if __name__ == "__main__":
60    main()
61