-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation_random_points.java
337 lines (239 loc) · 11.7 KB
/
simulation_random_points.java
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package a_lineOfDataGenPoints;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.vecmath.Vector3d;
import processing.core.PGraphics3D;
import bsim.BSim;
import bsim.BSimChemicalField;
import bsim.BSimTicker;
import bsim.BSimUtils;
import bsim.export.*;
import bsim.draw.BSimP3DDrawer;
import bsim.export.BSimLogger;
import bsim.particle.BSimBacterium;
import java.util.Random;
/**
* Tests out the functionality of the {@link BSimChemicalField}.</br>
* A number of {@link BSimBacterium} bacteria are set up to swim around in a chemical field.
* The bacteria deposit chemical randomly in space.
* This acts as an attractor for other bacteria in the vicinity (chemotaxis).
*/
public class lineOfDataRandPoints {
/*********************************************************
* Simulation Definition
*********************************************************/
public static void main(String[] args) {
/*********************************************************
* Create a new simulation object and set up simulation settings
*/
final int bounds = 128;
final int shape_bounds = 64;
BSim sim = new BSim();
sim.setDt(1);//default is 0.01
sim.setTimeFormat("0.00");
sim.setSolid(true,true,true);
sim.setBound(bounds,bounds,bounds);
sim.setSimulationTime(1000); // Total simulation time [sec]
/*********************************************************
* Set up the chemical field
*/
boolean average_over_sphere = false;
final int boxno = bounds;
final int xyboxno = bounds; //changing boxes sizes and nos changes the graph. can be due to accuracy of the results. change is slight
final double c = 5e9; // molecules (controls intensity with no major graph difference in morphology)
final double decayRate = 1e-5; // 4.44e-4 good Can regulate decay rate with pulse to get the top hat shape.
final double diffusivity = 0.1; // (microns)^2/sec
final double threshold = 3e8; //Threshold of FDF chemical 7e8 for cfgl
final double quantityadd = 5e9; //Quanitity of chemical added
final double firstwave = 1400; // Time slot where first wave stops
final double secondwave = 1440; // Time slot for second wave beginning
final BSimChemicalField field = new BSimChemicalField(sim, new int[]
{xyboxno,xyboxno,boxno}, diffusivity, decayRate);
// field.linearZ(0,c);
//Set temporary number and total number of bacteria
int totbac = 9000;
//For even space
double tempnum = 1/(2*totbac);
//For packed
//double tempnum = 1;
/*********************************************************
* Set up the bacteria
*/
final Vector<BSimBacterium> bacteria = new Vector<BSimBacterium>();
while(bacteria.size() < totbac) {
////////////////////////////////////////////////////////////////////////////////
//////////////////// Generate position of bacteria /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
double hole_size = 0;
double u = Math.random();
double theta = 2 * Math.PI * u;
// Full box
double x0 = Math.random();
double y0 = Math.random();
double z0 = Math.random();
while ( 0.5-(width/2) < x0 && x0 < 0.5+(width/2) && 0.5-(width/2) < z0 && z0 < 0.5+(width/2)) {
x0 = Math.random();
y0 = Math.random();
z0 = Math.random();
}
// Scale the shape to fit the shape
double x00 = (x0 * (1) * shape_bounds);
double y00 = y0 * (1) * shape_bounds;
double z00 = z0 * (1) * shape_bounds;
double rad = 0.05;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
BSimBacterium p = new BSimBacterium(sim,
//This uses random bacteria
//Spherical Test
new Vector3d(0.5*bounds+(x00),
1*bounds+y00,
0.5*bounds+z00)) {
// This uses the even distancing
//new Vector3d(/*0*Math.random()*/0.5*sim.getBound().x,
// /*0*Math.random()*/0.5*sim.getBound().y,
// /*0*Math.random()*/0.5*(tempnum)*sim.getBound().z)) {
// Bacteria move etc. and also add chemical to the global field.
public void action() {
super.action();
/*if (Math.random() < sim.getDt())
field.addQuantity(position, 1e9);*/
//Condition of Pulse
//if ((sim.getTime() > 60 && sim.getTime() < 64 && (x00/sim.getBound().x < rad && y00/sim.getBound().y < rad && z00/sim.getBound().z < rad && x00/sim.getBound().x > -rad && y00/sim.getBound().y > -rad && z00/sim.getBound().z > -rad)) || (field.getConc(position) > 1e4 && sim.getTime() < 300 && field.getConc(position)<5e9))
//field.addQuantity(position, 5e9);
//System.out.println("Fire");
if ((sim.getTime()%1440 > 0 &&
sim.getTime()%1440 < 4 &&
sim.getTime() < secondwave))
{
field.addQuantity(bounds/2, bounds-1, bounds/2, quantityadd);
//for(int i = 0; i < 64; i++) {
//field.addQuantity(xyboxno/2, bounds - 1 - i, boxno/2, quantityadd);
//}
}
if ((field.getConc(position) > threshold &&
sim.getTime() < firstwave))
field.addQuantity(position, quantityadd);
//if (field.getConc(position) < threshold/2 && sim.getTime() > firstwave)
//field.addQuantity(position, 0.001*quantityadd);
if (field.getConc(position) > threshold &&
sim.getTime()>secondwave &&
field.getConc(position) < quantityadd)
field.addQuantity(position, quantityadd)
//position is the box position and q is the quantity of chemical released in the box. makes curves more prominent up to a certain point
;
/*if (sim.getTime() > 4800 && (sim.getTime()) % 10 == 0 && sim.getTime() < 8400 && Math.random() > 0.99)
field.addQuantity(position, 5e9)
;*/
}
};
// Chemotaxis according to chemical field strength
p.setGoal(field);
if(!p.intersection(bacteria)) bacteria.add(p);
//Update the temporary number for different position for new bacteria
//Evenly Spread
//tempnum = tempnum + 1/totbac;
//Packed
tempnum = tempnum + 0.015;
}
/*********************************************************
* Set the ticker, define what happens each time step
*/
sim.setTicker(new BSimTicker() {
public void tick() {
for(BSimBacterium b : bacteria) {
b.action();
//b.updatePosition();
}
field.update();
}
});
/*********************************************************
* Set the drawer for the simulation
*/
sim.setDrawer(new BSimP3DDrawer(sim, 600,600) {
@Override
public void scene(PGraphics3D p3d) {
draw(field, Color.CYAN, (float)(0.1*255/threshold));
for(BSimBacterium p : bacteria) draw(p, Color.GREEN); // Colour the bacteria
}
});
String resultsDir = BSimUtils.generateDirectoryPath("./results/Cylinder2/");
BSimLogger logger = new BSimLogger(sim, resultsDir + "CylinderNewRep5.csv") {
@Override
public void before() {
super.before();
write("time,Intensity");
}
public void during() {
// Counter for the number of current collisions
//double Intensity = field.getConc(0, 0, 0);
//List<Double> intensities = new ArrayList<>();
// Loop through the bacteria and count collisions
//for (BSimTutorialBacterium p : bacteria){
//String intensities = "";
String intensities = "";
String totchem = "";
//At infinity the diffusion should be closer to each next distance
if(average_over_sphere){
for(int r = 0; r<boxno/2; r++){
if(r==0){
intensities += "," + (field.getConc(xyboxno/2,xyboxno/2,boxno/2));
}else{
Vector<Double> intensities_on_sphere = new Vector<Double>();
// generate points on a sphere of radius r
final int points_density = 10;
// determine the number of points on the sphere from the density
int points_nb = (int) (points_density* Math.pow(r,2));
// generate the random points
for(int i=0; i<=points_nb;i++){
// x,y,z for points on a sphere
double x = new Random().nextGaussian(0,1);
double y = new Random().nextGaussian(0,1);
double z = new Random().nextGaussian(0,1);
// normalise to a sphere
double normalisation = r/Math.sqrt(x*x + y*y + z*z);
// change units to be box index
normalisation *= (float)(boxno)/(float)(bounds);
// generate the final x, y and z
x *= normalisation;
y *= normalisation;
z *= normalisation;
// for details on these last few steps, refer to https://www.notion.so/Lab-book-01dac4b0eedd4f47b7bee779863b8507#cf3faa9db8fc4f34a6030758018426e8
// round the coordinates up and displaces them to the center of the sim, read the intensity at this point on the sphere and store it
intensities_on_sphere.add(field.getConc((int)(Math.ceil(x) + boxno/2),
(int)(Math.ceil(y) + boxno/2),
(int)(Math.ceil(z) + boxno/2)));
}
// average over all the points in intensities_on_sphere
double sum = 0;
for(Double v: intensities_on_sphere){
sum += v;
}
// write to file the intensity
intensities += "," + sum/ intensities_on_sphere.size();
totchem += "," + field.totalQuantity();
}
}
}else{ // give the intensities along the z-axis
for (int i = 0; i < 64; i++) {
//Intensity = field.getConc(0, 0, i);
final int z_index = boxno/2+i;
totchem += "," + field.totalQuantity();
intensities += "," + (field.getConc(xyboxno/2, bounds - 1 - i, boxno/2)); // these are supposed to be the indexes of the boxes coordinates, ie boxsize dependent
}
}
write(sim.getFormattedTime() + intensities);
//}
// Write the time and number of collisions to file
}
};
sim.addExporter(logger);
// Run the simulation
//sim.export();
sim.preview();
}
}