Skip to content

Commit 170a3f6

Browse files
committed
update
1 parent e6b5e5b commit 170a3f6

5 files changed

Lines changed: 429 additions & 202 deletions

File tree

SuPyMode/Geometry.py

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,19 @@ class Geometry(object):
9090
Number of point for Y dimensions discretization.
9191
"""
9292

93-
def __init__(self, Clad, Objects, Xbound, Ybound, Nx, Ny, Length=None, GConv=0, debug='INFO'):
93+
def __init__(self, Clad, Objects, Xbound, Ybound, Nx, Ny, GConv=0, BackGroundIndex=1.):
94+
self.Clad = Clad
9495

95-
Mlogger.setLevel(getattr(logging, debug))
96-
97-
self.Clad = Clad
98-
99-
self.Objects = ToList(Objects)
96+
self.Objects = ToList(Objects)
10097

10198
self.Boundaries = [Xbound, Ybound]
10299

103100
self.Shape = [Nx, Ny]
104101

105-
self.Length = Length
106-
107102
self.GConv = GConv
103+
self._Mesh = None
104+
105+
self.BackGroundIndex = BackGroundIndex
108106

109107
self.Axes = Axes( {'wavelength': 1.0,
110108
'Xbound' : Xbound,
@@ -113,8 +111,14 @@ def __init__(self, Clad, Objects, Xbound, Ybound, Nx, Ny, Length=None, GConv=0,
113111
'Ny' : Ny } )
114112

115113
self.CreateBackGround()
114+
self.GetAllIndex()
116115

117116

117+
@property
118+
def Mesh(self):
119+
if self._Mesh is None:
120+
self.CreateMesh()
121+
return self._Mesh
118122

119123
@property
120124
def AllObjects(self):
@@ -156,11 +160,19 @@ def CreateBackGround(self):
156160
miny=-yBound,
157161
maxx=+xBound,
158162
maxy=+yBound)
159-
self.BackGround.Index = 1
163+
164+
self.BackGround.Index = self.BackGroundIndex
160165
self.BackGround.hole = None
161166
self.BackGround.Gradient = None
162167

163168

169+
def GetAllIndex(self,):
170+
self.AllIndex = []
171+
for obj in self.AllObjects:
172+
self.AllIndex.append(obj.Index)
173+
174+
175+
164176
def Rotate(self, Angle):
165177
for object in self.AllObjects:
166178
object.Object = affinity.rotate(object.Object, Angle, (0,0))
@@ -211,22 +223,22 @@ def add_object_to_mesh(self, polygone):
211223
212224
"""
213225

214-
self.mesh[np.where(polygone.raster > 0)] = 0
226+
self._Mesh[np.where(polygone.raster > 0)] = 0
215227

216228
if polygone.Gradient:
217229
Grad = polygone.Gradient.Evaluate( self.X, self.Y )
218-
self.mesh += polygone.raster * Grad
230+
self._Mesh += polygone.raster * Grad
219231

220232
else:
221-
self.mesh += polygone.raster * polygone.Index
233+
self._Mesh += polygone.raster * polygone.Index
222234

223235

224236
def CreateMesh(self):
225237
""" The method create the RI profile mesh according to the user input.
226238
227239
"""
228240

229-
self.mesh = np.zeros(self.Shape)
241+
self._Mesh = np.zeros(self.Shape)
230242

231243
self.X, self.Y = np.mgrid[self.xMin: self.xMax: complex(self.Shape[0]),
232244
self.yMin: self.yMax: complex(self.Shape[1]) ]
@@ -237,7 +249,7 @@ def CreateMesh(self):
237249
self.rasterize_polygone(object)
238250
self.add_object_to_mesh(object)
239251

240-
self.mesh = gaussian_filter(self.mesh, sigma=self.GConv)
252+
self._Mesh = gaussian_filter(self._Mesh, sigma=self.GConv)
241253

242254

243255
def Plot(self):
@@ -247,26 +259,32 @@ def Plot(self):
247259

248260
self.CreateMesh()
249261

250-
Scene = Scene2D(nCols=1, nRows=1, UnitSize=(4, 4))
251-
252-
Scene.AddMesh(Row = 0,
253-
Col = 0,
254-
x = self.X,
255-
y = self.Y,
256-
Scalar = self.mesh,
257-
ColorMap = 'coolwarm',
258-
xLabel = r'X-distance [$\mu$m]',
259-
yLabel = r'Y-distance [$\mu$m]',
262+
Scene = Scene2D(nCols=1, nRows=1, UnitSize=(6, 6))
263+
264+
Scene.AddContour(Row = 0,
265+
Col = 0,
266+
x = self.X,
267+
y = self.Y,
268+
Scalar = self._Mesh,
269+
ColorMap = 'coolwarm',
270+
xLabel = r'X-distance [$\mu$m]',
271+
yLabel = r'Y-distance [$\mu$m]',
272+
IsoLines = np.sort( [0.99] + list(set(self._Mesh.flatten())) + [1.6] )
273+
)
274+
275+
Scene.SetAxes(Col=0,
276+
Row=0,
277+
xLimits=[self.xMin, self.xMax],
278+
yLimits=[self.yMin, self.yMax],
279+
Equal=True,
260280
)
261281

262-
Scene.SetAxes(0, 0, xLimits=[self.xMin, self.xMax], yLimits=[self.yMin, self.yMax], Equal=True)
263-
264282
Scene.Show()
265283

266284

267285
def _Gradient(self):
268286

269-
Ygrad, Xgrad = gradientO4( self.mesh.T**2, self.Axes.dx, self.Axes.dy )
287+
Ygrad, Xgrad = gradientO4( self._Mesh.T**2, self.Axes.dx, self.Axes.dy )
270288

271289
return Ygrad, Xgrad
272290

@@ -275,7 +293,7 @@ def Gradient(self, Plot=False):
275293

276294
#blurred = gaussian_filter(self.mesh, sigma=0)
277295

278-
Ygrad, Xgrad = gradientO4( self.mesh.T**2, self.Axes.dx, self.Axes.dy )
296+
Ygrad, Xgrad = gradientO4( self._Mesh.T**2, self.Axes.dx, self.Axes.dy )
279297

280298
gradient = (Xgrad * self.Axes.XX + Ygrad * self.Axes.YY)
281299

SuPyMode/Plotting/Plots.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def InitScene(self):
5252
else:
5353
self.Axes = np.reshape(np.asarray( [self.Axes] ), (self.nRows, self.nCols))
5454

55+
5556
def AddLine(self, x, y, Col, Row, Title=None, Fill=True, Color=None, xLabel=None, yLabel=None, Legend=None):
5657
ax = self.Axes[Row, Col]
5758

@@ -122,9 +123,11 @@ def SetAxes(self, Col, Row, Equal=None, Legend=None, xLimits=None, yLimits=None,
122123

123124

124125

125-
def AddMesh(self, Col, Row, x, y, Scalar, ColorMap='viridis', Title=None, xLabel=None, yLabel=None):
126+
def AddMesh(self, Col, Row, x, y, Scalar, ColorMap='viridis', Title=None, xLabel=None, yLabel=None, DiscretNorm=None):
126127
ax = self.Axes[Row, Col]
127128

129+
norm = colors.BoundaryNorm(DiscretNorm, ColorMap) if DiscretNorm is not None else None
130+
128131
Image = ax.pcolormesh(x, y, Scalar, cmap=ColorMap, shading='auto', vmin=-np.max(np.abs(Scalar)), vmax=+np.max(np.abs(Scalar)) )
129132

130133
if Title is not None:
@@ -143,6 +146,28 @@ def AddMesh(self, Col, Row, x, y, Scalar, ColorMap='viridis', Title=None, xLabel
143146
plt.tight_layout(pad=3)
144147

145148

149+
def AddContour(self, Col, Row, x, y, Scalar, ColorMap='viridis', Title=None, xLabel=None, yLabel=None, IsoLines=None):
150+
ax = self.Axes[Row, Col]
151+
152+
ax.contour(x, y, Scalar, levels=IsoLines, colors='black', linewidth=.5)
153+
Image = ax.contourf(x, y, Scalar, levels=IsoLines, cmap='jet', norm=colors.LogNorm() )
154+
155+
if Title is not None:
156+
ax.set_title(Title)
157+
158+
if xLabel is not None:
159+
ax.set_xlabel(xLabel)
160+
161+
if yLabel is not None:
162+
ax.set_ylabel(yLabel)
163+
164+
if self.ColorBar:
165+
plt.colorbar(Image, ax=ax, location='bottom', format="%.3f")
166+
167+
self.UpdateBoundary(x=x, y=y)
168+
plt.tight_layout(pad=3)
169+
170+
146171
def Show(self):
147172
#plt.tight_layout(pad=3)
148173
plt.show()

SuPyMode/Solver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self, Coupler, Tolerance, MaxIter, Error=2, debug='INFO'):
2626

2727
def InitBinding(self, Symmetries: dict, Wavelength: float, nMode: int, sMode: int):
2828

29-
CppSolver = EigenSolving(Mesh = self.Geometry.mesh,
29+
CppSolver = EigenSolving(Mesh = self.Geometry._Mesh,
3030
Gradient = self.Geometry.Gradient().ravel(),
3131
nMode = nMode,
3232
sMode = sMode,

Untitled.ipynb

Lines changed: 354 additions & 171 deletions
Large diffs are not rendered by default.

examples/4x4Coupler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
Xbound = [-150, 0],
3030
Ybound = [-150, 150],
3131
Nx = Nx//2,
32-
Ny = Ny)
32+
Ny = Ny,
33+
BackGroundIndex = 1.)
3334

3435
Geo.Rotate(90)
3536

0 commit comments

Comments
 (0)