-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaster_controller.py
More file actions
454 lines (368 loc) · 16.7 KB
/
Master_controller.py
File metadata and controls
454 lines (368 loc) · 16.7 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<<<<<<< HEAD
import subprocess, os, shutil, numpy as np, csv, json
from Stats import *
from Functions import *
from rstools import RSoftUserFunction, RSoftCircuit
from HexProperties import generate_hex_grid, number_rows
'''
Now used inside the jupyter notebook file!
'''
# # name the output file
# name = 'MCF_Test'
# # Collection of symbols to use wihtin the RSoft CAD
# sym = {}
#############################################################################################################################################################################################
# '''
# Define the fibre and RSoft properties
# '''
# # properties of the structure (units of um)
# with open("fibre_prop.json", "r") as f:
# params = json.load(f)
# for key in params:
# sym[key] = params[key]
# # creating the design file, load the seetings and add symbols
# c = RSoftCircuit()
# for key in sym:
# c.set_symbol(key,sym[key])
#############################################################################################################################################################################################
# '''
# Generating the positional coordinates for each fibre
# '''
# # this must be an odd number!!
# core_num = [sym['core_num']]
# for i in range(len(core_num)):
# if core_num[i] % 2 == 0:
# raise ValueError(f"The number of cores must be odd to perfectly fit inside the hex grid. Received:{core_num}")
# row_numbers = [number_rows(n) for n in core_num]
# for idx, row_num in enumerate(row_numbers):
# hcoord, vcoord = generate_hex_grid(row_num, sym["Core_sep"])
#############################################################################################################################################################################################
# '''
# Generating the segments and assigning the positional coordinates found in
# the previous sections
# '''
# core_name = [f"core_{n+1:02}" for n in range(7)]
# cladding = c.add_segment(
# position=(0,0,0), offset=(0,0,'Length'),
# dimensions = (sym['Claddiam']/sym['taper_ratio'],sym['Claddiam']/sym['taper_ratio']), # /sym['taper_ratio']
# dimensions_end = (('Claddiam', 'Claddiam'))
# )
# cladding.set_name("MMF Cladding")
# for j, (x, y) in enumerate(zip(hcoord, vcoord)):
# core = c.add_segment(position=(x/sym['taper_ratio'],y/sym['taper_ratio'],0), offset=(x,y,'Length'),
# dimensions = (sym['Corediam']/sym['taper_ratio'], sym['Corediam']/sym['taper_ratio']),
# dimensions_end = (('Corediam', 'Corediam')))
# core.set_name(core_name[j])
# c.write('%s.ind'%name)
#############################################################################################################################################################################################
# '''
# Controller
# '''
# # # This runs the original script with rspython and generates
# # subprocess.run(["rspython", "MCF_Test.py"], check=True)
# uf = RSoftUserFunction()
# # extract the number of cores from the template file
# core_num = int(Extract_params("core_num "))
# Corediam = Extract_params("Corediam ")
# fibre_length = Extract_params("Length")
# Core_delta = Extract_params("Core_delta")
# with open("prior.json", "r") as f:
# priors = json.load(f)
# core_diam = priors["core_diam"]
###############################################################################################################################################
# '''
# This is the hacking part which adds all of the pathways, then the monitors and then the
# launch fields. They are done individually to account for formatting issues that may exist
# within RSoft's .ind files.
# '''
# block_text = {
# "pathway":'''
# pathway {n}
# {n}
# end pathway
# ''',
# "monitor": '''
# monitor {n}
# pathway = {n}
# monitor_type = MONITOR_WGMODE_POWER
# monitor_tilt = 1
# monitor_component = COMPONENT_BOTH
# monitor_width = {Corediam}
# monitor_height = {Corediam}
# end monitor
# ''',
# "launch_field":'''
# launch_field {n}
# launch_pathway = {n}
# launch_type = LAUNCH_WGMODE
# launch_tilt = 1
# launch_align_file = 1
# end launch_field
# '''
# }
# # appends the above text to the .ind file
# with open('%s.ind'%name, "a") as f:
# for block_type in ["pathway","monitor","launch_field"]:
# for i in range(1,int(core_num) + 1):
# if block_type == "launch_field" and i != 1:
# continue
# text = block_text[block_type].format(n=i,Corediam=Corediam)
# f.write(text)
# # Add separate effective index for the cores.
# insert_param_into_file('%s.ind'%name,Core_delta)
###############################################################################################################################################
'''
FAIL SAFE: writes only the symbols listed in the template folder
'''
# # Append extracted block to pre-generated .ind file
# with open("MCF_Test.ind", "a") as f:
# f.writelines((pathway_block,monitor_block,launchfield_block))
###############################################################################################################################################
# '''
# Manual setup to loop through a list of values
# Runs the terminal line that will initiate RSoft.
# All output files will appear in a subfolder on the Desktop (windows)
# '''
# # initialise results dictionary
# results = {}
# l = 0
# # open and read the generated .ind file
# with open('%s.ind'%name, "r") as r:
# lines = r.readlines()
# # loop through list of parameters
# '''
# To add: make this universal so any list of parameters can be looped through, not just core diameter
# '''
# # for core in core_diam:
# modified_line = []
# for line in lines:
# if line.strip().startswith("Corediam ="):
# rep_line = f"Corediam = {core_diam}\n"
# modified_line.append(rep_line)
# else:
# modified_line.append(line)
# # generate separate .ind file based on parameters
# with open(f"MCF_{core_diam}.ind", "w") as out:
# out.writelines(modified_line)
# # This runs the beamprop simulation and also allocates the results in their own folder on the desktop
# filename = f"MCF_{core_diam}.ind"
# prefix = f"prefix=Corediam_{core_diam}"
# Folder_name = f"CoreSim_{core_diam}"
# desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
# results_folder_root = os.path.join(desktop_path,"Results")
# os.makedirs(results_folder_root, exist_ok=True)
# results_folder = os.path.join(results_folder_root,Folder_name)
# os.makedirs(results_folder, exist_ok = True)
# subprocess.run(["bsimw32", filename, prefix, "wait=0"], check=True)
# '''
# To do: make this test metric a choice. Incorporate other metric and make it an option to choose between metrics to test.
# '''
# # read pathway monitor file
# uf.read(f'Corediam_{core_diam}.mon')
# (x,y) = uf.get_arrays()
# # x is the fibre length
# # y is the pathway monitor power
# average_throughput = (sum(y))/len(y)
# results[l] = y
# l += 1
# for file in os.listdir():
# if file.startswith(f"Corediam_{core_diam}"):
# shutil.move(file, os.path.join(results_folder, file))
# if file.startswith(filename):
# shutil.move(file, os.path.join(results_folder, file))
# # using throughput as the test metric, saves pathway monitor results to a separate excel file
# source_path = os.path.join("C:\\Users\\justinvella\\OneDrive - The University of Sydney (Students)", "Apps", "New Caprica Stuff", "Python", "MCF", "Throughput_results.csv")
# destination_path = os.path.join("C:\\Users\\justinvella\\OneDrive - The University of Sydney (Students)", "Apps","VSCode", "Throughput", "Files", "Throughput_results.csv")
# with open("Throughput_results.csv", mode = "w", newline="") as file:
# writer = csv.writer(file)
# header = ["x"] + [f"{core_diam:.2f}"]
# writer.writerow(header)
# for i in range(len(x)):
# row = [x[i]] + [results[j][i] for j in range(l)]
# writer.writerow(row)
# shutil.copy(source_path,destination_path)
=======
import subprocess, os, shutil, numpy as np, csv, json
from Stats import *
from Functions import *
from rstools import RSoftUserFunction, RSoftCircuit
from HexProperties import generate_hex_grid, number_rows
'''
Now used inside the jupyter notebook file!
'''
# # name the output file
# name = 'MCF_Test'
# # Collection of symbols to use wihtin the RSoft CAD
# sym = {}
#############################################################################################################################################################################################
# '''
# Define the fibre and RSoft properties
# '''
# # properties of the structure (units of um)
# with open("fibre_prop.json", "r") as f:
# params = json.load(f)
# for key in params:
# sym[key] = params[key]
# # creating the design file, load the seetings and add symbols
# c = RSoftCircuit()
# for key in sym:
# c.set_symbol(key,sym[key])
#############################################################################################################################################################################################
# '''
# Generating the positional coordinates for each fibre
# '''
# # this must be an odd number!!
# core_num = [sym['core_num']]
# for i in range(len(core_num)):
# if core_num[i] % 2 == 0:
# raise ValueError(f"The number of cores must be odd to perfectly fit inside the hex grid. Received:{core_num}")
# row_numbers = [number_rows(n) for n in core_num]
# for idx, row_num in enumerate(row_numbers):
# hcoord, vcoord = generate_hex_grid(row_num, sym["Core_sep"])
#############################################################################################################################################################################################
# '''
# Generating the segments and assigning the positional coordinates found in
# the previous sections
# '''
# core_name = [f"core_{n+1:02}" for n in range(7)]
# cladding = c.add_segment(
# position=(0,0,0), offset=(0,0,'Length'),
# dimensions = (sym['Claddiam']/sym['taper_ratio'],sym['Claddiam']/sym['taper_ratio']), # /sym['taper_ratio']
# dimensions_end = (('Claddiam', 'Claddiam'))
# )
# cladding.set_name("MMF Cladding")
# for j, (x, y) in enumerate(zip(hcoord, vcoord)):
# core = c.add_segment(position=(x/sym['taper_ratio'],y/sym['taper_ratio'],0), offset=(x,y,'Length'),
# dimensions = (sym['Corediam']/sym['taper_ratio'], sym['Corediam']/sym['taper_ratio']),
# dimensions_end = (('Corediam', 'Corediam')))
# core.set_name(core_name[j])
# c.write('%s.ind'%name)
#############################################################################################################################################################################################
# '''
# Controller
# '''
# # # This runs the original script with rspython and generates
# # subprocess.run(["rspython", "MCF_Test.py"], check=True)
# uf = RSoftUserFunction()
# # extract the number of cores from the template file
# core_num = int(Extract_params("core_num "))
# Corediam = Extract_params("Corediam ")
# fibre_length = Extract_params("Length")
# Core_delta = Extract_params("Core_delta")
# with open("prior.json", "r") as f:
# priors = json.load(f)
# core_diam = priors["core_diam"]
###############################################################################################################################################
# '''
# This is the hacking part which adds all of the pathways, then the monitors and then the
# launch fields. They are done individually to account for formatting issues that may exist
# within RSoft's .ind files.
# '''
# block_text = {
# "pathway":'''
# pathway {n}
# {n}
# end pathway
# ''',
# "monitor": '''
# monitor {n}
# pathway = {n}
# monitor_type = MONITOR_WGMODE_POWER
# monitor_tilt = 1
# monitor_component = COMPONENT_BOTH
# monitor_width = {Corediam}
# monitor_height = {Corediam}
# end monitor
# ''',
# "launch_field":'''
# launch_field {n}
# launch_pathway = {n}
# launch_type = LAUNCH_WGMODE
# launch_tilt = 1
# launch_align_file = 1
# end launch_field
# '''
# }
# # appends the above text to the .ind file
# with open('%s.ind'%name, "a") as f:
# for block_type in ["pathway","monitor","launch_field"]:
# for i in range(1,int(core_num) + 1):
# if block_type == "launch_field" and i != 1:
# continue
# text = block_text[block_type].format(n=i,Corediam=Corediam)
# f.write(text)
# # Add separate effective index for the cores.
# insert_param_into_file('%s.ind'%name,Core_delta)
###############################################################################################################################################
'''
FAIL SAFE: writes only the symbols listed in the template folder
'''
# # Append extracted block to pre-generated .ind file
# with open("MCF_Test.ind", "a") as f:
# f.writelines((pathway_block,monitor_block,launchfield_block))
###############################################################################################################################################
# '''
# Manual setup to loop through a list of values
# Runs the terminal line that will initiate RSoft.
# All output files will appear in a subfolder on the Desktop (windows)
# '''
# # initialise results dictionary
# results = {}
# l = 0
# # open and read the generated .ind file
# with open('%s.ind'%name, "r") as r:
# lines = r.readlines()
# # loop through list of parameters
# '''
# To add: make this universal so any list of parameters can be looped through, not just core diameter
# '''
# # for core in core_diam:
# modified_line = []
# for line in lines:
# if line.strip().startswith("Corediam ="):
# rep_line = f"Corediam = {core_diam}\n"
# modified_line.append(rep_line)
# else:
# modified_line.append(line)
# # generate separate .ind file based on parameters
# with open(f"MCF_{core_diam}.ind", "w") as out:
# out.writelines(modified_line)
# # This runs the beamprop simulation and also allocates the results in their own folder on the desktop
# filename = f"MCF_{core_diam}.ind"
# prefix = f"prefix=Corediam_{core_diam}"
# Folder_name = f"CoreSim_{core_diam}"
# desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
# results_folder_root = os.path.join(desktop_path,"Results")
# os.makedirs(results_folder_root, exist_ok=True)
# results_folder = os.path.join(results_folder_root,Folder_name)
# os.makedirs(results_folder, exist_ok = True)
# subprocess.run(["bsimw32", filename, prefix, "wait=0"], check=True)
# '''
# To do: make this test metric a choice. Incorporate other metric and make it an option to choose between metrics to test.
# '''
# # read pathway monitor file
# uf.read(f'Corediam_{core_diam}.mon')
# (x,y) = uf.get_arrays()
# # x is the fibre length
# # y is the pathway monitor power
# average_throughput = (sum(y))/len(y)
# results[l] = y
# l += 1
# for file in os.listdir():
# if file.startswith(f"Corediam_{core_diam}"):
# shutil.move(file, os.path.join(results_folder, file))
# if file.startswith(filename):
# shutil.move(file, os.path.join(results_folder, file))
# # using throughput as the test metric, saves pathway monitor results to a separate excel file
# source_path = os.path.join("C:\\Users\\justinvella\\OneDrive - The University of Sydney (Students)", "Apps", "New Caprica Stuff", "Python", "MCF", "Throughput_results.csv")
# destination_path = os.path.join("C:\\Users\\justinvella\\OneDrive - The University of Sydney (Students)", "Apps","VSCode", "Throughput", "Files", "Throughput_results.csv")
# with open("Throughput_results.csv", mode = "w", newline="") as file:
# writer = csv.writer(file)
# header = ["x"] + [f"{core_diam:.2f}"]
# writer.writerow(header)
# for i in range(len(x)):
# row = [x[i]] + [results[j][i] for j in range(l)]
# writer.writerow(row)
# shutil.copy(source_path,destination_path)
>>>>>>> 6fd1dd9af4d81c8c2f1b6c87ad535bb284f0c98c
###############################################################################################################################################