Skip to content

Commit 7d68e2b

Browse files
committed
update
1 parent 19977e9 commit 7d68e2b

4 files changed

Lines changed: 140 additions & 63 deletions

File tree

SuPyMode/Solver.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,23 @@ def GetSuperSet(self,
6363
):
6464

6565
CppSolver = self.InitBinding(Symmetries, Wavelength, nMode, sMode)
66-
#CppSolver1 = self.InitBinding({'Right': +1, 'Left': 0, 'Top': -1, 'Bottom': 0}, Wavelength, nMode, sMode)
66+
CppSolver1 = self.InitBinding({'Right': -1, 'Left': 0, 'Top': 0, 'Bottom': 0}, Wavelength, nMode, sMode)
6767

6868
self.ITRList = np.linspace(ITRi, ITRf, Nstep)
6969

7070

7171

7272
CppSolver.LoopOverITR(ITR=self.ITRList, ExtrapolationOrder=3)
73-
#CppSolver1.LoopOverITR(ITR=self.ITRList, ExtrapolationOrder=3)
73+
CppSolver1.LoopOverITR(ITR=self.ITRList, ExtrapolationOrder=3)
7474

7575

7676

7777
CppSolver.SortModes(Sorting=Sorting)
78-
#CppSolver1.SortModes(Sorting=Sorting)
78+
CppSolver1.SortModes(Sorting=Sorting)
7979

8080
Set = SuperSet(ParentSolver=self)
8181

82-
self.PopulateSuperSet(Set, [CppSolver])
82+
self.PopulateSuperSet(Set, [CppSolver, CppSolver1])
8383

8484
return Set
8585

SuPyMode/SuperMode.py

Lines changed: 113 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,108 @@ def __repr__(self):
3333
return "".join(String)
3434

3535

36+
37+
class SuperPosition(ReprBase):
38+
Description = 'Mode superposition class'
39+
ReprVar = ["SuperSet", "Amplitudes"]
40+
41+
def __init__(self, SuperSet, InitialAmplitudes: list):
42+
self.SuperSet = SuperSet
43+
self.InitialAmplitudes = np.asarray(InitialAmplitudes).astype(complex)
44+
self._CouplerLength = None
45+
self._Amplitudes = None
46+
47+
48+
def ComputeAmpltiudes(self):
49+
self.MatrixInterp = interp1d(self.Distance, self.SuperSet.Matrix, axis=-1)
50+
51+
def foo(t, y):
52+
return 1j * self.MatrixInterp(t).dot(y)
53+
54+
sol = solve_ivp(foo,
55+
y0 = self.InitialAmplitudes,
56+
t_span = [0, self._CouplerLength],
57+
method = 'RK45')
58+
59+
self.Amplitudes = sol.y
60+
self.Distances = sol.t
61+
self.AmplitudeInterpolation = interp1d(self.Distances, self.Amplitudes, axis=-1)
62+
63+
@property
64+
def ITRList(self):
65+
return self.SuperSet.ITRList
66+
67+
@property
68+
def CouplerLength(self):
69+
assert self._CouplerLength is not None, "CouplerLength attribute has to be defined before computing propagation."
70+
return self._CouplerLength
71+
72+
@CouplerLength.setter
73+
def CouplerLength(self, Value: float):
74+
self._CouplerLength = Value
75+
self.Distance = np.linspace(0, self._CouplerLength, self.ITRList.size)
76+
77+
78+
@property
79+
def Amplitudes(self):
80+
if self._Amplitudes is None:
81+
self.ComputeAmpltiudes()
82+
83+
return self._Amplitudes
84+
85+
86+
@Amplitudes.setter
87+
def Amplitudes(self, Value):
88+
self._Amplitudes = Value
89+
90+
91+
def PlotAmplitudes(self):
92+
Scene = Scene2D(nCols=1, nRows=1, ColorBar=False)
93+
A = self.InitialAmplitudes.dot(self.Amplitudes)
94+
z = self.Distances
95+
Scene.AddLine(Row = 0,
96+
Col = 0,
97+
x = z,
98+
y = A,
99+
Fill = False,
100+
Legend = None,
101+
xLabel = r'Z-Distance [$\mu m$]',
102+
yLabel = r'Mode complex ampltiude')
103+
104+
Scene.SetAxes(0, 0, Equal=False)
105+
Scene.Show()
106+
107+
108+
def PlotFields(self):
109+
if self._Amplitudes is None: self.ComputeAmpltiudes()
110+
111+
y = self.AmplitudeInterpolation(self.Distance)
112+
113+
z = self.Distance
114+
115+
Field = self.SuperSet[0].FullFields.astype(complex)*0.
116+
117+
for mode, _ in enumerate(self.InitialAmplitudes):
118+
a = y[0].astype(complex)
119+
field = self.SuperSet[0].FullFields.astype(complex)
120+
Field += np.einsum('i, ijk->ijk', a, field)
121+
122+
surface = mlab.surf( np.abs( Field[0] ) , warp_scale="auto" )
123+
124+
@mlab.animate(delay=100)
125+
def anim_loc():
126+
for n, _ in enumerate(self.Distance):
127+
surface.mlab_source.scalars = np.abs( Field[n] )
128+
129+
yield
130+
131+
anim_loc()
132+
mlab.show()
133+
134+
135+
136+
137+
36138
class SuperSet(SetProperties, SetPlottings, ReprBase):
37139

38140
Description = 'SuperSet class'
@@ -88,9 +190,11 @@ def ComputeCouplingFactor(self, Length):
88190

89191
dITR = np.gradient(np.log(self.Geometry.ITRList), 1)
90192

91-
factor = dITR/dx
193+
return dITR/dx
92194

93-
return factor/10
195+
196+
def GetSuperposition(self, Amplitudes):
197+
return SuperPosition(SuperSet=self, InitialAmplitudes=Amplitudes)
94198

95199

96200
def Propagate(self, Amplitude=[1,1, 0, 0, 0], Length=1000):
@@ -140,28 +244,6 @@ def foo(t, y):
140244

141245
return sol
142246

143-
144-
def PlotPropagation(self, Modes):
145-
Mode0 = self[0]
146-
Mode1 = self[1]
147-
148-
Field = Mode0[0].GetFullField(Axes=False) + Mode1[0].GetFullField(Axes=False)
149-
150-
surface = mlab.surf( np.abs( Field ) , warp_scale="auto" )
151-
152-
@mlab.animate(delay=100)
153-
def anim_loc():
154-
for n, _ in Mode0.IterateSlice():
155-
Field = Mode0[n].GetFullField(Axes=False) + Mode1[n].GetFullField(Axes=False)
156-
157-
surface.mlab_source.scalars = np.abs( Field )
158-
159-
yield
160-
161-
anim_loc()
162-
mlab.show
163-
164-
165247
@property
166248
def Size(self):
167249
return len(self.SuperModes)
@@ -221,7 +303,7 @@ class SuperMode(ReprBase):
221303
"PlotPropagation"]
222304

223305
def __init__(self, ParentSet, ModeNumber, CppSolver, BindingNumber):
224-
self.Binded = CppSolver.GetMode(ModeNumber)
306+
self.Binded = CppSolver.GetMode(BindingNumber)
225307
self.ModeNumber = ModeNumber
226308

227309
self.CppSolver = CppSolver
@@ -431,6 +513,12 @@ def ComputeFullFields(self):
431513
self._FullxAxis = self.ExtendAxis(Axis=self._FullxAxis, sign="Minus")
432514

433515

516+
def GetSlice(self, Slice: int, Full: bool=True):
517+
if Full:
518+
return self.Betas[Slice], self.FullFields[Slice], self._FullxAxis, self._FullyAxis
519+
else:
520+
return self.Betas[Slice], self.Fields[Slice], self.Axis.X, self.Axis.Y
521+
434522

435523
def CompareSymmetries(self, Other):
436524
assert isinstance(Other, SuperMode), "Can only compare SuperMode instance with another"
@@ -440,9 +528,6 @@ def CompareSymmetries(self, Other):
440528
def AppendSlice(self, SliceNumber):
441529
self.Slice.append( SetSlice(ParentMode=self, SliceNumber=SliceNumber) )
442530

443-
def GetSlices(self, SlicesNumber: list):
444-
return [self[slice] for slice in SlicesNumber]
445-
446531

447532
def __getitem__(self, N):
448533
return self.Slice[N]

SuPyMode/Tools/BaseClass.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,23 @@ def PlotAdiabatic(self, Scene, Col, Row, Combination):
132132
Scene.SetAxes(Col, Row, Equal=False, Legend=True, yScale='log', yLimits=[1e-8, 1e-1], LegendTitle='Mode')
133133

134134

135-
def _PlotFields(self, iter=0):
136-
iter = ToList(iter)
137-
Scene = Scene2D(nCols=self.Size, nRows=len(iter), ColorBar=True, UnitSize=[5,5])
135+
def _PlotFields(self, Slices=0):
136+
Slices = ToList(Slices)
137+
Scene = Scene2D(nCols=self.Size, nRows=len(Slices), ColorBar=True, UnitSize=[5,5])
138138

139-
for supermode in self.IterateSuperMode():
140-
for s, slice in enumerate(supermode.GetSlices(iter)):
141-
Field, x, y = slice.GetFullField()
139+
for supermode in self.SuperModes:
140+
for s, slice in enumerate(Slices):
141+
Beta, Field, x, y = supermode.GetSlice(Slice=slice, Full=True)
142142

143143
Scene.AddMesh(Row = s,
144144
Col = supermode.ModeNumber,
145-
x = x,
146-
y = y,
147-
Scalar = Field,
145+
x = np.arange(x.size),
146+
y = np.arange(y.size),
147+
Scalar = Field.T,
148148
ColorMap = FieldMap,
149149
xLabel = r'X-distance [$\mu$m]',
150150
yLabel = r'Y-distance [$\mu$m]' if supermode.ModeNumber==0 else "",
151-
Title = f'{supermode.Name} [ITR: {slice.ITR:.2f}]'
151+
Title = f'Mode: {supermode.ModeNumber} [ITR: {self.ITRList[slice]:.2f}]'
152152
)
153153

154154
Scene.SetAxes(supermode.ModeNumber, s, Equal=True)
@@ -158,8 +158,8 @@ def _PlotFields(self, iter=0):
158158

159159

160160

161-
def PlotFields(self, iter=0):
162-
Scene = self._PlotFields(iter=iter)
161+
def PlotFields(self, Slices=0):
162+
Scene = self._PlotFields(Slices=Slices)
163163

164164
Scene.Show()
165165

examples/4x4Coupler.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@
1313

1414
nMode = 6
1515

16-
N = Nx = Ny = 30
16+
N = Nx = Ny = 60
1717

1818

1919
Index = ExpData('FusedSilica').GetRI(1.55e-6)
2020

2121
Clad = Fused2(Radius = 62.5, Fusion = 0.95, Index = Index)
2222

23-
Core0 = Circle( Position=Clad.C[0], Radius = 4.2, Index = Index+1.005 )
24-
Core1 = Circle( Position=Clad.C[1], Radius = 4.2, Index = Index+1.005 )
23+
Core0 = Circle( Position=Clad.C[0], Radius = 4.2, Index = Index+0.005 )
24+
Core1 = Circle( Position=Clad.C[1], Radius = 4.2, Index = Index+0.005 )
2525

2626

2727
Geo = Geometry(Clad = Clad,
2828
Objects = [Core0, Core1],
2929
Xbound = [-150, 0],
3030
Ybound = [-150, 150],
31-
Nx = Nx,
31+
Nx = Nx//2,
3232
Ny = Ny)
3333

3434
Geo.Rotate(90)
@@ -43,26 +43,18 @@
4343
ITRi = 1,
4444
ITRf = 0.05,
4545
Sorting = 'Index',
46-
Symmetries = {'Right': 1,
47-
'Left': 0,
48-
'Top': 0,
49-
'Bottom': 0},
50-
nMode=7,
51-
sMode=5
46+
Symmetries = {'Right': 1, 'Left': 0, 'Top': 0, 'Bottom': 0},
47+
nMode=5,
48+
sMode=3
5249
)
5350

54-
SuperSet[0].PlotFields([0])
51+
SuperSet.PlotFields([-1])
5552

56-
Amplitudes = SuperSet.Propagate()
53+
#Mode = SuperSet.GetSuperposition(Amplitudes=[1,1,0,0,0])
5754

55+
#Mode.CouplerLength = 2000
5856

59-
60-
61-
import matplotlib.pyplot as plt
62-
plt.figure()
63-
plt.plot(np.real(Amplitudes[0]) + np.real(Amplitudes[1]))
64-
65-
plt.show()
57+
#Mode.PlotFields()
6658

6759

6860
"""

0 commit comments

Comments
 (0)