This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3D-material-conductivity-model.c
278 lines (227 loc) · 6.93 KB
/
3D-material-conductivity-model.c
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
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "stdbool.h"
typedef struct particle
{
int type;
bool cluster;
} particle;
typedef struct coordinates
{
int x;
int y;
int z;
} coordinate;
void InitializeArray(particle*** arr_particle, int lx, int ly, int lz)
{
for (int x = 0; x < lx; x++)
{
for (int y = 0; y < ly; y++)
{
for (int z = 0; z < lz; z++)
{
arr_particle[x][y][z].cluster = false;
arr_particle[x][y][z].type = 0;
}
}
}
}
void InputRandomCoordinates(particle*** arr_particle, int lx, int ly, int lz, int N, int fraction)
{
int numb_part = 0;
while (numb_part < N)
{
int xp = (rand() % lx);
int yp = (rand() % ly);
int zp = (rand() % lz);
if (arr_particle[xp][yp][zp].type == 0)
{
if ((double)numb_part < (double)N * (double)fraction / 100)
{
arr_particle[xp][yp][zp].type = -1;
}
else
{
arr_particle[xp][yp][zp].type = 1;
}
numb_part++;
}
}
}
coordinate GetStart(particle*** arr_particles, int lx, int ly, int lz)
{
while (true)
{
int rand_x = rand() % lx;
int rand_y = rand() % ly;
int rand_z = rand() % lz;
if (arr_particles[rand_x][rand_y][rand_z].type != 0)
{
coordinate result = { rand_x , rand_y, rand_z };
return result;
}
}
}
void InitializeRoster(coordinate* roster, int N)
{
for (int index = 0; index < N; index++)
{
roster[index].x = -1;
roster[index].y = -1;
roster[index].z = -1;
}
}
void PrintRoster(coordinate* roster, int N)
{
for (int index = 0; index < N; index++)
{
printf("X = %d Y = %d Z = %d \n", roster[index].x, roster[index].y, roster[index].z);
}
}
bool should_they_connect(particle*** arr_particle, coordinate* roster, int seed, int d, int j, int k, int lx, int ly, int lz)
{
int x = roster[seed].x;
int y = roster[seed].y;
int z = roster[seed].z;
if (x + d < 0 || x + d >= lx || y + j < 0 || y + j >= ly || z + k < 0 || z + k >= lz)
{
return false;
}
//we now know the grid space exists, so we can ask questions about the particle
if (arr_particle[x + d][y + j][z + k].type == 0 || arr_particle[x + d][y + j][z + k].cluster == true)
{
return false;
}
if (arr_particle[x][y][z].type == 1 || arr_particle[x + d][y + j][z + k].type == 1)
{
if ((d != 0 && j != 0)||(k!=0 && j!=0)||(k!=0 && d!=0))
{
return false;
}
}
//if it does not "pass" any of the fail conditions, the particle is indeed in the cluster and as such the function returns true
return true;
}
void AnalyzeRoster(particle*** arr_particle, coordinate* roster, int index, int seed, int lx, int ly, int lz)
{
while (index != seed)
{
for (int d = -1; d <= 1; d++)
{
for (int j = -1; j <= 1; j++)
{
for (int k = -1; k <= 1; k++)
{
if (j != 0 || k != 0 || d != 0)
{ //by this point every neighbour (3d) is being looked at
if (should_they_connect(arr_particle, roster, seed, d, j, k, lx, ly, lz))
{
arr_particle[roster[seed].x + d][roster[seed].y + j][roster[seed].z + k].cluster = true;
roster[index].x = roster[seed].x + d;
roster[index].y = roster[seed].y + j;
roster[index].z = roster[seed].z + k;
index++;
}
}
}
}
}
seed++;
}
}
bool can_current_flow(particle*** arr_particle, int lx, int ly, int lz)
{
for (int y = 0; y < ly; y++)
{
for (int x = 0; x < lx; x++)
{
//checking top line
if (arr_particle[x][y][0].cluster)
{
for (int y1 = 0; y1 < ly; y1++)
{
for (int x1 = 0; x1 < ly; x1++)
{
//checking bottom line
if (arr_particle[x1][y1][lz - 1].cluster)
{
return true;
}
}
}
//so it doesn't iterate through the bottom plane if top line doesn't have anything
return false;
}
}
}
return false;
}
int main()
{
int lx, ly, lz, N, n_grids, fraction;
printf("Enter Ly: ");
scanf("%d", &ly);
printf("Enter Lx: ");
scanf("%d", &lx);
printf("Enter Lz: ");
scanf("%d", &lz);
printf("Enter N: ");
scanf("%d", &N);
printf("Enter how many grids you would like to print: ");
scanf("%d", &n_grids);
printf("Enter the percentage of particles you would like to be superconductors: ");
scanf("%d", &fraction);
srand(time(NULL) + lx);
//printf("\n");
int fraction_grids_with_connecting_path = 0;
for (int i = 0; i < n_grids; i++)
{
if (ly * lx * lz <= N)
{
printf("Too many particles for this grid size.");
exit(-1);
}
particle*** arr_particle;
arr_particle = malloc(lx * sizeof(**arr_particle));
for (int iter_x = 0; iter_x < lx; iter_x++)
{
arr_particle[iter_x] = malloc(ly * sizeof(*arr_particle));
for (int iter_y = 0; iter_y < ly; iter_y++)
{
arr_particle[iter_x][iter_y] = malloc(lz * sizeof(particle));
}
}
InitializeArray(arr_particle, lx, ly, lz);
InputRandomCoordinates(arr_particle, lx, ly, lz, N, fraction);
coordinate first;
first = GetStart(arr_particle, lx, ly, lz);
coordinate* roster;
roster = malloc(N * sizeof(coordinate));
InitializeRoster(roster, N);
int index = 0;
int seed = 0;
//setting the initial zero point to valid
roster[index] = first;
arr_particle[roster[index].x][roster[index].y][roster[index].z].cluster = true;
index++;
AnalyzeRoster(arr_particle, roster, index, seed, lx, ly, lz);
if (can_current_flow(arr_particle, lx, ly, lz))
{
fraction_grids_with_connecting_path++;
}
//PrintRoster(roster, N);
for (int x = 0; x < lx; x++)
{
for (int y = 0; y < ly; y++)
{
free(arr_particle[x][y]);
}
free(arr_particle[x]);
}
free(arr_particle);
free(roster);
}
printf("Fraction of grids for which you there is a connecting path: %d / %d", fraction_grids_with_connecting_path, n_grids);
return 0;
}