Advertisement
rdiazfuentes

Untitled

Mar 23rd, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.02 KB | None | 0 0
  1. '''
  2. Script to test the TE mode converter from the fundamental mode to the second-order mode
  3. '''
  4.  
  5. # Import necessary python libraries and SPINS-B libraries.
  6. import os
  7. import sys
  8.  
  9. import datetime
  10. import logging
  11. import os
  12. import pickle
  13.  
  14. import matplotlib.pyplot as plt
  15. import numpy as np
  16.  
  17. from spins import goos
  18. from spins.goos import compat
  19. from spins.goos_sim import maxwell
  20. from spins.invdes.problem_graph import optplan, log_tools
  21.  
  22. '''
  23. Set up Optimization Plan object.
  24. '''
  25. ## Create folder where all spins-b results will be saved. ##
  26.  
  27. # Currently, the folder will be named "TEmc_{current_timestamp}" and will
  28. # be located in the folder containing spins-b. To change this, change
  29. # the following line and set `out_folder_name` somewhere else.
  30. out_folder_name = "TEmc_" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
  31. out_folder = os.path.join(os.getcwd(),out_folder_name)
  32. if (not os.path.exists(out_folder)):
  33. os.makedirs(out_folder)
  34.  
  35. ## Setup logging and Optimization Plan. ##
  36. plan = goos.OptimizationPlan(save_path = out_folder)
  37.  
  38. '''
  39. Define the constant background structures that will not be changed
  40. during the design. In this case, these are the input and output waveguides.
  41. '''
  42. with plan:
  43. # Define input waveguide.
  44. wg_in = goos.Cuboid(pos=goos.Constant([0, -2000, 0]),
  45. extents=goos.Constant([400, 3000, 220]),
  46. material=goos.material.Material(index=3.45))
  47. # Define output waveguide.
  48. wg_out = goos.Cuboid(pos=goos.Constant([0, 2000, 0]),
  49. extents=goos.Constant([400, 3000, 220]),
  50. material=goos.material.Material(index=3.45))
  51.  
  52. # Group these background structures together.
  53. eps_background_structures = goos.GroupShape([wg_in, wg_out])
  54.  
  55. '''
  56. Visualize the constant background structures we just defined.
  57. '''
  58. with plan:
  59. eps_rendered = maxwell.RenderShape(
  60. eps_background_structures,
  61. region=goos.Box3d(center=[0, 0, 0], extents=[3000, 3000, 0]),
  62. mesh=maxwell.UniformMesh(dx=40),
  63. wavelength=1550,
  64. )
  65.  
  66. goos.util.visualize_eps(eps_rendered.get().array[2])
  67.  
  68. '''
  69. Define and initialize the design region.
  70. '''
  71. with plan:
  72. # Use random initialization, where each pixel is randomly assigned
  73. # a value in the range [0.3,0.7].
  74. def initializer(size):
  75. # Set the seed immediately before calling `random` to ensure
  76. # reproducibility.
  77. np.random.seed(247)
  78. return np.random.random(size) * 0.2 + 0.5
  79.  
  80. # Define the design region as a pixelated continuous shape, which is composed
  81. # of voxels whose permittivities can take on any value between `material` and
  82. # `material2`.
  83. var, design = goos.pixelated_cont_shape(
  84. initializer=initializer,
  85. pos=goos.Constant([0, 0, 0]),
  86. extents=[2000, 2000, 220],
  87. material=goos.material.Material(index=1),
  88. material2=goos.material.Material(index=3.45),
  89. pixel_size=[40, 40, 220],
  90. var_name="var_cont")
  91.  
  92. eps = goos.GroupShape([eps_background_structures, design])
  93.  
  94. '''
  95. Visualize the design region as a sanity check.
  96. '''
  97. with plan:
  98. eps_rendered = maxwell.RenderShape(
  99. eps,
  100. region=goos.Box3d(center=[0, 0, 0], extents=[3000, 3000, 0]),
  101. mesh=maxwell.UniformMesh(dx=40),
  102. wavelength=1550,
  103. )
  104. goos.util.visualize_eps(eps_rendered.get().array[2])
  105.  
  106. '''
  107. Setup EM solver - in this case, we use the FDFD solver that comes with
  108. spins-b, maxwell.
  109. '''
  110. with plan:
  111. # Define wavelength and solver.
  112. my_wavelength = 1550
  113.  
  114. sim_3d = True
  115.  
  116. if sim_3d:
  117. sim_z_extent = 2500
  118. solver_info = maxwell.MaxwellSolver(solver="maxwell_cg",
  119. err_thresh=1e-2)
  120. pml_thickness = [400] * 6
  121. else:
  122. sim_z_extent = 40
  123. solver_info = maxwell.DirectSolver()
  124. pml_thickness = [400, 400, 400, 400, 0, 0]
  125.  
  126. # Define simulation space.
  127. my_simulation_space = maxwell.SimulationSpace(
  128. mesh=maxwell.UniformMesh(dx=40),
  129. sim_region=goos.Box3d(
  130. center=[0, 0, 0],
  131. extents=[4000, 4000, sim_z_extent],
  132. ),
  133. pml_thickness=pml_thickness)
  134.  
  135. # Define a waveguide mode source.
  136. my_sources = [maxwell.WaveguideModeSource(center=[0, -1400, 0],
  137. extents=[2500, 0, 1000],
  138. normal=[0, 1, 0],
  139. mode_num=0,
  140. power=1)]
  141.  
  142.  
  143. # Define simulation outputs.
  144. my_outputs=[ maxwell.Epsilon(name="eps"),
  145. maxwell.ElectricField(name="field"),
  146. maxwell.WaveguideModeOverlap(name="overlap",
  147. center=[0, 1400, 0],
  148. extents=[2500, 0, 1000],
  149. normal=[0, 1, 0],
  150. mode_num=2,
  151. power=1)]
  152.  
  153. # Setup the simulation object.
  154. sim = maxwell.fdfd_simulation(
  155. name="sim_cont",
  156. wavelength= my_wavelength,
  157. background=goos.material.Material(index=1.0),
  158. eps=eps,
  159. simulation_space = my_simulation_space,
  160. solver_info = solver_info,
  161. sources = my_sources,
  162. outputs= my_outputs
  163. )
  164.  
  165. '''
  166. Visualize simulation of initial structure, as a sanity check.
  167. '''
  168. with plan:
  169. initial_structure = np.squeeze(sim['eps'].get().array)
  170. initial_field = np.squeeze(sim['field'].get().array)
  171. initial_overlap = np.squeeze(sim['overlap'].get().array)
  172.  
  173. plt.figure()
  174. plt.subplot(1,2,1)
  175. plt.imshow(
  176. np.abs(
  177. initial_structure[2]))
  178. plt.colorbar()
  179.  
  180. plt.subplot(1,2,2)
  181. plt.imshow(
  182. np.abs(
  183. initial_field[2]))
  184. plt.colorbar()
  185. plt.show()
  186.  
  187. print("Initial overlap is {}.".format(np.abs(initial_overlap)))
  188.  
  189. '''
  190. Define objective function to maximize the transmission of the fundamental
  191. waveguide mode.
  192. '''
  193. obj = goos.rename(goos.abs(sim["overlap"]), name="obj_cont")
  194.  
  195. '''
  196. Setup the optimization strategy.
  197. '''
  198. with plan:
  199. goos.opt.scipy_maximize(
  200. obj,
  201. "L-BFGS-B",
  202. monitor_list=[sim["eps"], sim["field"], sim["overlap"], obj],
  203. max_iters=20,
  204. name="opt_cont")
  205.  
  206. '''
  207. Save the plan.
  208. '''
  209. with plan:
  210. plan.save()
  211.  
  212. '''
  213. Run the plan.
  214. '''
  215. # Setup logging.
  216. LOG_FORMAT = "[%(asctime)-15s][%(levelname)s][%(module)s][%(funcName)s] %(message)s"
  217. logging.basicConfig(stream=sys.stdout,format=LOG_FORMAT)
  218. logging.getLogger("").setLevel(logging.INFO)
  219. logger = logging.getLogger("")
  220.  
  221. # Run the plan.
  222. with plan:
  223. plan.run()
  224.  
  225. '''
  226. Set the folder for visualizing optimization results.
  227. '''
  228. # Currently, this will visualize results in the output folder defined
  229. # at the beginning of this notebook in the cell where we define the
  230. # OptimizationPlan object - but, you can change this variable
  231. # to visualize results from other folders in your google drive.
  232. folder_plt = out_folder
  233.  
  234. '''
  235. Open a pkl file and look at what's inside, as a sanity check.
  236. '''
  237. with open(os.path.join(folder_plt, "step1.pkl"), "rb") as fp:
  238. data = pickle.load(fp)
  239.  
  240. print("Pkl dictionary: ", data.keys())
  241. print("'monitor_data' dictionary: ", data['monitor_data'].keys())
  242. print("'objective value': ", data['monitor_data']['obj_cont'])
  243.  
  244. '''
  245. Visualize the objective function and overlap monitors as a function of iteration.
  246. '''
  247.  
  248. # Load the logged monitor data.
  249. df = log_tools.create_log_data_frame(log_tools.load_all_logs(folder_plt))
  250. monitor_obj = log_tools.get_joined_scalar_monitors(df,"obj_cont", event_name = "optimizing", scalar_operation = None)
  251. monitor_overlap = log_tools.get_joined_scalar_monitors(df,"sim_cont.overlap", event_name = "optimizing",scalar_operation = "magnitude")
  252.  
  253. # Plot the logged monitor data.
  254. plt.figure()
  255.  
  256. plt.subplot(2, 1, 1)
  257. plt.plot(monitor_obj.iterations,monitor_obj.data,'k-')
  258. plt.xlabel("Iteration")
  259. plt.ylabel("Objective")
  260. plt.tight_layout()
  261.  
  262. plt.subplot(2, 1, 2)
  263. plt.plot(monitor_overlap.iterations,monitor_overlap.data,'k-')
  264. plt.xlabel("Iteration")
  265. plt.ylabel("Overlap")
  266. plt.tight_layout()
  267.  
  268.  
  269. '''
  270. Visualize the final structure and fields.
  271. '''
  272. step = goos.util.get_latest_log_step(folder_plt)
  273.  
  274. with open(os.path.join(folder_plt, "step{}.pkl".format(step)), "rb") as fp:
  275. data = pickle.load(fp)
  276.  
  277. eps = np.linalg.norm(data["monitor_data"]["sim_cont.eps"], axis=0)
  278. field_norm = np.linalg.norm(data["monitor_data"]["sim_cont.field"], axis=0)
  279.  
  280. plt.figure()
  281.  
  282. plt.subplot(1, 2, 1)
  283. plt.imshow(
  284. eps[:, :, eps.shape[2] // 2].squeeze())
  285. plt.colorbar()
  286. plt.title("|Eps_z|")
  287. plt.tight_layout()
  288.  
  289. plt.subplot(1, 2, 2)
  290. plt.imshow(
  291. field_norm[:, :, field_norm.shape[2] // 2].squeeze())
  292. plt.colorbar()
  293. plt.title("|E_z|")
  294. plt.tight_layout()
  295.  
  296. plt.savefig(os.path.join(folder_plt,"fields_step"+str(step)+".png"))
  297. plt.show()
  298.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement