-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMass_Diffusion4.go
324 lines (241 loc) · 9.34 KB
/
Mass_Diffusion4.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
///////////////////////////////////////////////////////////////
//
// Can we maintain small particle coherence with a centre of mass
// guided by a psi wave?
//
// This version, with two slits for psi and * delayed start * ...
///////////////////////////////////////////////////////////////
package main
import (
// "fmt"
C "Cellibrium"
)
// **********************************************************
const DoF = 10000
const wrange = 10000
const PERIOD = C.WAVELENGTH * wrange
// ****************************************************************
func main () {
C.MODEL_NAME = "MassDiffusion4"
var st [C.Ylim]string
st[0] = ".........*..........................."
st[1] = ".........*..........................|"
st[2] = ".........*..........................|"
st[3] = ".........*..........................|"
st[4] = ".........*..........................|"
st[5] = ".........*..........................|"
st[6] = ".........*..........................|"
st[7] = ".........*..........................|"
st[8] = ".........*..........................|"
st[9] = ".........*..........................|"
st[10] = ".........*..........................|"
st[11] = ".........*..........................|"
st[12] = ".........*..........................|"
st[13] = ".........*..........................|"
st[14] = ".........*..........................|"
st[15] = ".........*..........................|"
st[16] = ".........*..........................|"
st[17] = ".........*..........................|"
st[18] = ".........*..........................|"
st[19] = ".........*..........................|"
st[20] = ".........*..........................|"
st[21] = ".........*..........................|"
st[22] = ".........*..........................|"
st[23] = ".........*..........................|"
st[24] = ".........*..........................|"
st[25] = ".........*..........................|"
st[26] = ">...................................|"
st[27] = ">..mmm..............................|" // X
st[28] = ">..mmm..............................|" // X
st[29] = ">..mmm..............................|" // X
st[30] = ">...................................|"
st[31] = ">...................................|"
st[32] = ".........*..........................|"
st[33] = ".........*..........................|"
st[34] = ".........*..........................|"
st[35] = ".........*..........................|"
st[36] = ".........*..........................|"
st[37] = ".........*..........................|"
st[38] = ".........*..........................|"
st[39] = ".........*..........................|"
st[40] = ".........*..........................|"
st[41] = ".........*..........................|"
st[42] = ".........*..........................|"
st[43] = ".........*..........................|"
st[44] = ".........*..........................|"
st[45] = ">...................................|"
st[46] = ">..mmm..............................|"
st[47] = ">..mmm..............................|"
st[48] = ">..mmm..............................|"
st[49] = ">...................................|"
st[50] = ">...................................|"
st[51] = ".........*..........................|"
st[52] = ".........*..........................|"
st[53] = ".........*..........................|"
st[54] = ".........*..........................|"
st[55] = ".........*..........................|"
st[56] = ".........*..........................|"
st[57] = ".........*..........................|"
st[58] = ".........*..........................|"
st[59] = ".........*..........................|"
st[60] = ".........*..........................|"
st[61] = ".........*..........................|"
st[62] = ".........*..........................|"
st[63] = ".........*..........................|"
st[64] = ".........*..........................|"
st[65] = ".........*..........................|"
st[66] = ".........*..........................|"
st[67] = ".........*..........................|"
st[68] = ".........*..........................|"
st[69] = ".........*..........................|"
st[70] = ".........*..........................|"
st[71] = ".........*..........................|"
st[72] = ".........*..........................|"
st[73] = ".........*..........................|"
st[74] = ".........*..........................|"
st[75] = ".........*..........................."
// Keep the data structures for agents global too for convenience
C.Initialize(st,DoF)
C.ShowState(st,1,37,76,"+")
EquilGuideRail()
C.ShowState(st,C.MAXTIME,37,76,"+")
C.ShowAffinity(st,C.MAXTIME,37,76)
//go C.MovingPromise()
//C.ShowPhase(st,C.MAXTIME,37,76)
}
// ****************************************************************
func EquilGuideRail() {
for i := 1; i < C.Adim; i++ {
go UpdateAgent_Flow(i)
}
}
// ****************************************************************
func UpdateAgent_Flow(agent int) {
// Start with an unconditional promise to break the deadlock symmetry
for direction := 0; direction < C.N; direction++ {
neighbour := C.AGENT[agent].Neigh[direction]
C.AGENT[agent].P[direction] = -1
if neighbour != 0 {
var breaker C.Message
breaker.Value = C.AGENT[agent].Psi
breaker.Phase = C.TICK
C.CHANNEL[agent][neighbour] = breaker
}
}
// Set the boundary condition
const PsiQuant = 1
C.AGENT[agent].Moment = C.EAST
C.CausalIndependence(true)
for t := 0; t < C.MAXTIME; t++ {
// Every pair of agents has a private directional channel that's not overwritten by anyone else
// Messages persist until they are read and cannot unseen
for direction := C.AGENT[agent].Moment; direction < (C.AGENT[agent].Moment + C.N); direction++ {
var send,recv C.Message
d := direction % C.N
dbar := (direction + C.N/2) % C.N
neighbour := C.AGENT[agent].Neigh[d]
if neighbour == 0 {
continue // wall signal
}
// We need to wait for a positive signal indicating a new transfer to avoid double/empty reading
recv = C.AcceptFromChannel(neighbour,agent)
// ****************** PROCESS *********************
switch recv.Phase {
case C.TICK: // me
C.AGENT[agent].V[d] = recv.Value // the value here is psi
C.AGENT[agent].M[d] = recv.MassID
C.AGENT[agent].P[d] = recv.Moment
C.AGENT[agent] = EvolveAgents(C.AGENT[agent],d)
// In this phase we can choose to make an offer to accept
// a neighbouring massID, we have to have received an update first
const starting_time = 800
if t > starting_time { // wait for the wave to equilibrate somewhat
if AcceptingMass(C.AGENT[agent],d,dbar) > 0 {
send.Phase = C.TAKE
send.Value = 1
C.AGENT[agent].Offer[d] = send.Value
C.AGENT[agent].Moment = dbar
C.ConditionalChannelOffer(agent,neighbour,send)
continue
}
}
send.MassID = C.AGENT[agent].MassID
send.Moment = C.AGENT[agent].Moment
send.Value = C.AGENT[agent].Psi
send.Angle = C.AGENT[agent].Theta
send.Phase = C.TICK
C.ConditionalChannelOffer(agent,neighbour,send)
continue
case C.TAKE: // YOU
transfer_offer := recv.Value // This is massID now
send.Value = transfer_offer
send.Phase = C.TACK
C.AGENT[agent].MassID = 0 // assume you'll take it
C.ConditionalChannelOffer(agent,neighbour,send)
continue
case C.TACK: // ME
transfer_offer := recv.Value
C.AGENT[agent].MassID = transfer_offer
send.Value = transfer_offer
send.Phase = C.TOCK
C.ConditionalChannelOffer(agent,neighbour,send)
continue
case C.TOCK: // YOU - initiate a change / Xfer
C.AGENT[agent].MassID = 0
send.MassID = C.AGENT[agent].MassID
send.Value = C.AGENT[agent].Psi
send.Moment = C.AGENT[agent].Moment
send.Angle = C.AGENT[agent].Theta
send.Phase = C.TICK
C.ConditionalChannelOffer(agent,neighbour,send)
continue
}
}
}
}
// ****************************************************************
func AcceptingMass(agent C.STAgent,d,dbar int) int {
// We look to accept some mass from d if dbar looks empty
if agent.Psi * agent.Psi < 0.1 {
return 0
}
affinity := agent.V[d] * agent.V[d] - agent.Psi * agent.Psi
const threshold = 20.0 // should really express in dimensionless vars..?
if affinity > threshold {
if (agent.M[d] > 0) && (agent.MassID == 0) && (dbar == agent.Moment) {
return 1
}
}
return 0
}
// ****************************************************************
func WillingToPassToRecipient(agent,d int) bool {
dbar := (d + C.N/2) % C.N
if C.AGENT[agent].MassID > 0 && C.AGENT[agent].Moment == dbar {
return true
}
return false
}
// ****************************************************************
func EvolveAgents(agent C.STAgent,direction int) C.STAgent { // Laplacian
agent.Theta += dTheta(agent,direction) // float64(int(dTheta(agent)+0.5) % PERIOD)
agent.Psi += dPsi(agent)
return agent
}
// ******************************************************************
func dTheta(agent C.STAgent,di int) float64 { // Laplacian
var d2 float64 = 0
const dt = 0.01
const mass = 2.0
// Velocity = laplaciant gradient
d2 += agent.V[di] - agent.Psi
// This is negative when Psi is higher than neighbours
dtheta := dt * d2 / float64(C.N * mass)
return dtheta
}
// ******************************************************************
func dPsi(agent C.STAgent) float64 { // Laplacian
const dt = 0.01
deltaPsi := agent.Theta * dt
return deltaPsi
}