-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDlaTools.java
More file actions
362 lines (306 loc) · 9.19 KB
/
DlaTools.java
File metadata and controls
362 lines (306 loc) · 9.19 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* @author Kenneth McGuinness
* Creates a Diffusion Limited Aggregation Environment
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.regex.Pattern;
public class DlaTools {
//Move Diffusing Object
public static Collagen_Monomer Move(Collagen_Monomer moved)
{
Driver.simStepsBound++;
Driver.stepsOverall++;
Random r = new Random();
int num = r.nextInt(1);
switch(num)
{
case 0: moved = DlaTools.moveMN(moved);break;
case 1: moved = DlaTools.moveZ(moved);break;
}
return moved;
}
//Move Diffusing Object on a Hexagonal Lattice
public static Collagen_Monomer moveMN(Collagen_Monomer moved)
{
Random r = new Random();
int[] anglesx = {1,0,-1,-1,0,1};
int[] anglesy = {0,1,1,0,-1,-1};
int num = r.nextInt(anglesx.length);
moved.m = moved.m + anglesx[num];
moved.n = moved.n + anglesy[num];
//Check Boundary Conditions
if(DlaTools.getDistance(moved,Driver.e.origin) > Driver.e.krad*Driver.e.krad)
{
moved = DlaTools.generateMNZch();
Driver.kills++;
return moved;
}
return moved;
}
//Move Diffusing Object in the Z-Axis
public static Collagen_Monomer moveZ(Collagen_Monomer moved)
{
Random r = new Random();
int[] anglesz = {1,-1};
int num = r.nextInt(anglesz.length);
moved.z = moved.z + anglesz[num];
//Check Boundary Conditions
if(moved.z > Driver.e.kmaxz || moved.z < Driver.e.kminz)
{
moved = DlaTools.generateMNZch();
Driver.kills++;
return moved;
}
return moved;
}
//Determine Action Limited Step- Checks interactions
public static String checkInteraction(File file, Collagen_Monomer movedMonomer) throws Exception
{
int aggsize = Driver.dla.aggregate.size();
int overlap = 0;
Collagen_Chain movedChain = new Collagen_Chain(Collagen_Chain.createChainZ(movedMonomer));
//Search Dla.Aggregate for potential interactions
//Overlap: Minimum of two 1's interacting to stick
for(int j = aggsize-1; j>=0;j--)
{
if(DlaTools.getDistance(Driver.dla.aggregate.get(j).chain.get(0), movedMonomer) == 0
&& Math.abs(Driver.dla.aggregate.get(j).chain.get(0).z - movedMonomer.z) < Driver.dla.aggregate.get(j).chain.size())
return "Collision";
if(DlaTools.getDistance(Driver.dla.aggregate.get(j).chain.get(0), movedMonomer) == 1
&& Math.abs(Driver.dla.aggregate.get(j).chain.get(0).z - movedMonomer.z)
< Driver.dla.aggregate.get(j).chain.size())
{
overlap = overlap + DlaTools.checkOverlap(movedChain,Driver.dla.aggregate.get(j));
}
}
if(overlap >= 2)
{
Driver.dla.aggregate.add(movedChain);
DlaTools.appendCollagen_Chain(file, movedChain);
Driver.e.UpdateEnv(movedChain);
return "Add";
}
return "Move";
}
//Called in DlaTools.checkInteraction
public static int overlapRegistry(Collagen_Chain free, Collagen_Chain fixed)
{
return free.chain.get(0).z - fixed.chain.get(0).z;
}
//Called in DlaTools.checkInteraction
public static double probabilityOfOverlap(int overlapRegistry)
{
//According to Probability of Attachments
double alpha = 0.63;
double beta = 0.03;
double registryStateEnergy = DlaTools.overlapRegistryToEnergy(overlapRegistry);
double nonNormProbability = alpha * Math.exp(-registryStateEnergy*beta);
return nonNormProbability;
}
//Called in DlaTools.probabilityOfOverlap
public static int overlapRegistryToEnergy(int overlapRegistry)
{
switch(overlapRegistry)
{
case -9: return 85;
case -8: return 70;
case -7: return 55;
case -6: return 40;
case -5: return 25;
case -4: return 10;
case -3: return 5;
case -2: return 0;
case -1: return -5;
case 0: return -10;
case 1: return -8;
case 2: return -6;
case 3: return -4;
case 4: return -2;
case 5: return 0;
case 6: return 5;
case 7: return 20;
case 8: return 45;
case 9: return 70;
}
return 0;
}
//Called in DlaTools.checkInteraction
public static int checkOverlap(Collagen_Chain one, Collagen_Chain two)
{
int overlap = 0;
int j = 0;
int k = 0;
while(j < one.chain.size() && k < two.chain.size())
{
if(one.chain.get(j).z > two.chain.get(k).z)
k++;
else if(one.chain.get(j).z < two.chain.get(k).z)
j++;
else if(one.chain.get(j).z == two.chain.get(k).z)
{
if(one.chain.get(j).charge == 1 && two.chain.get(k).charge == 1)
overlap++;
j++;
k++;
}
}
return overlap;
}
public static double getDistance(Collagen_Monomer one, Collagen_Monomer two)
{
return (two.m-one.m)*(two.m-one.m) + (two.n-one.n)*(two.n-one.n) + (two.m-one.m)*(two.n-one.n);
}
//generates mn- hexagonal coord. and z integer coordinates w/ charge of 0
public static Collagen_Monomer generateMNZch()
{
Driver.numMonomers++;
double theta = Math.random() * 6.28318531;
double x = Driver.e.brad*Math.cos(theta);
double y = Driver.e.brad*Math.sin(theta);
int m = (int)Math.rint((x-(y/1.73205081)));
int n = (int)Math.rint(2*y/1.73205081);
Random rand = new Random();
int r = rand.nextInt(4);
int z = 0;
switch(r)
{
case 0: z = Driver.e.bmnz;break;
case 1: z = Driver.e.bmxz;break;
case 2:
case 3: z = (int)(Driver.e.bmnz + Math.random()*(Driver.e.bmxz - Driver.e.bmnz + 1));break;//Along Length of Hexagonal Cylinder
}
Collagen_Monomer mnz = new Collagen_Monomer(m,n,z,0);
return mnz;
}
public static void appendCollagen_Chain(File file, Collagen_Chain one) throws Exception
{
for(int j = 0; j < one.chain.size(); j++)
{
appendCollagen_Monomer(file,one.chain.get(j));
}
}
public static void appendCollagen_Monomer(File fileName, Collagen_Monomer text) throws Exception
{
long fileLength = fileName.length();
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
raf.seek(fileLength);
double x = text.m + (text.n*.5);
double y = .5*text.n*1.73205081;
Driver.dla.cmx += x;
Driver.dla.cmy += y;
Driver.dla.cmz += text.z;
Driver.dla.mass ++;
String Atom = "";
switch(text.charge)
{
case 1: Atom = "Cp"; break;
case 0: Atom = "Co"; break;
case -1: Atom = "Cn"; break;
}
raf.writeBytes(Atom + " " + x + " " + y + " " + text.z + "\n");
raf.close();
}
public static double[][] readXYZdoubleArray(File xyzfile) throws IOException
{
String x, y, z;
double dx, dy, dz;
BufferedReader br =
new BufferedReader(
new InputStreamReader(
new FileInputStream(xyzfile)));
int filelength = (int)xyzfile.length();
double[][] agg = new double[filelength][3];
String line = br.readLine();
for(int j = 0; line !=null ; j++)
{
String xyz = line.substring(2,line.length());
x = xyz.substring(0,xyz.indexOf(" "));
String yz = xyz.substring(xyz.indexOf(" ")+1, xyz.length());
yz.trim();
y = yz.substring(0,yz.indexOf(" "));
z = yz.substring(yz.indexOf(" ") + 1,yz.length());
dx = Double.parseDouble(x);
dy = Double.parseDouble(y);
dz = Double.parseDouble(z);
agg[j][0] = dx;
agg[j][1] = dy;
agg[j][2] = dz;
line = br.readLine();
}
return agg;
}
public static void stringTochargeSequence(String Sequence)
{
if(Sequence.length() == 0)
{
throw new NoSuchElementException();
}
for(int k =0; k < Sequence.length(); k++)
{
if(Sequence.charAt(k) == '0')
{
Driver.charge.add(0);
}
if(Sequence.charAt(k) == '1')
{
Driver.charge.add(1);
}
if(Sequence.charAt(k) == '-')
{
Driver.charge.add(-1);
k++;
}
}
return;
}
//Given an xyxFile, gets the xyz Coords only
public static void xyzCoords(File xyzFile, File coordLocation,String coordFileName) throws Exception
{
File xyzCoords = new File(coordFileName);
FileReader fr = null;
try {
fr = new FileReader(xyzFile);
} catch (FileNotFoundException e) {
System.out.println("xyzFile not found");
}
LineNumberReader lr = new LineNumberReader(fr);
int linenumber = 0;
while (lr.readLine() != null){
linenumber++;
}
linenumber--;//Due to the xyzFile protocal
fr.close();
lr.close();
fr = new FileReader(xyzFile);
lr = new LineNumberReader(fr);
String regex = " ";
Pattern p = Pattern.compile(regex);
String line = lr.readLine();
String[] splitLine = p.split(line);
if(splitLine.length == 1)
line = lr.readLine(); //Skip Line Due to xyzFile monomer count
FileWriter outFile = new FileWriter(xyzCoords);
PrintWriter out = new PrintWriter(outFile);
out.println("3");
out.println(Integer.toString(linenumber));
do
{
splitLine = p.split(line);
out.println((double)Float.parseFloat(splitLine[1])+ " " + (double)Float.parseFloat(splitLine[2]) + " " + (double)Float.parseFloat(splitLine[3]));
}while((line = lr.readLine())!= null);
out.close();
//return xyzCoords;
}
}