-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModesGen.py
273 lines (188 loc) · 8.08 KB
/
ModesGen.py
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
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 11 13:31:47 2020
Mode generation class
@author: Marcos
"""
import sys
import pathlib
p = pathlib.Path(__file__).parent
path_to_module = p
sys.path.append(str(path_to_module))
import mode_generation_core_library as mgcl
from numpy import *
try:
import cupy as cp
except ModuleNotFoundError:
print("Cupy module couldn't be imported")
class LGmodes():
"""
Creates an object that can contain and create LGmodes:
Input parameters:
- mfd = mode field diameter of the funtamental mode
- group = number of groups
- N = number of samples in of the axis Ex: 1080 --> 1080x1080 modes
- px_size = step size
- generateModes:{True or False} -- if you want to generate the modes when the object is created
- wholeSet = {True or False} -- if you want to generate all mode pyramid or half (no complex conj)
- targetCore = {'GPU' or 'CPU'}
- multicore = {true or false} -- CPU computations done in parallel by numba and Ipyrallel if coreTarget is CPU
GPU:True uses cupy and numba
CPU:True uses Ipyparallel and numba
Output parameters:
- index = modes coefs
- farfieldGouyPhase = Gouy phase at the farfield
- LGmodesArray = Array with the modes
- LGmodesArray__ = Array with the whole set of modes
- LGmodesArrayFarField = Array with the modes at the farfield ( Including Gouy phase )
- LGmodesArrayFarField__ = Array with the whole set of modes at the farfield ( Including Gouy phase )
"""
def __init__(self,mfd,group,N,px_size, generateModes = True, wholeSet = False, farfield= False , engine = 'GPU', multicore = True):
#Input atributes
self.w0 = mfd/2
self.group = group
self.N = N
self.px_size = px_size
#AutoGenerated atributes and constructor
xx = mgcl.np.arange(-N//2,N//2,1) * px_size
self.X,self.Y = mgcl.np.meshgrid(xx,xx)
self.index = mgcl.np.array([0])
self.LGmodesArray = 'None'
self.LGmodesArrayFarField = 'None'
self.LGmodesArray__ = 'None'
self.LGmodesArrayFarField__ = 'None'
self.numModes = 'None'
self.numModesAll = 'None'
self.parameters = 0
#Modes atributes
self.farfieldGouyPhase = 'None'
if generateModes == True:
self.index = self.computeCoefs()
self.LGmodesArray = self.computeLGmodes(engine, multicore)
self.numModes = self.LGmodesArray.shape[0]
if wholeSet == True:
self.LGmodesArray__ = self.computeAllmodes(multicore)
self.numModesAll = self.LGmodesArray__.shape[0]
if farfield == True:
self.LGmodesArrayFarField = self.computeFarFieldLGmodes()
if wholeSet == True:
self.LGmodesArrayFarField__ = self.computeAllmodesFarField(multicore)
self.parameters = self.getSpecs()
def __repr__(self):
return( 'LG_object_mfd:%d_group:%i' %(self.parameters['mfd'],self.parameters['mode_group']) )
#Methods:
def computeCoefs(self):
xx = self.group
print('Generating modes coeficients...')
return( mgcl.graded_index_fiber_coefs(xx))
def updateCoefs(self):
"""
Update or compute coefs in case you changed object atributes
"""
self.index = mgcl.graded_index_fiber_coefs(self.group)
def computeFarFieldGouyPhase(self):
xx = self.index
self.farfieldGouyPhase = mgcl.LGFarFieldGouyPhase(xx)
def computeFarFieldLGmodes(self):
print('Generating modes at the far field...')
self.farfieldGouyPhase = mgcl.LGFarFieldGouyPhase(self.index)
return( mgcl.applyphase(self.LGmodesArray, self.farfieldGouyPhase) )
def computeLGmodes(self, targetEngine, multi):
print('Generating modes...')
return( mgcl.LGmodes(self.w0,self.X,self.Y,self.index, engine = targetEngine, multicore = multi) )
def updateLGmodes(self,targetEngine, multi):
"""
Update or compute LGmodes in case you changed object atributes
"""
self.LGmodesArray = mgcl.LGmodes(self.w0,self.X,self.Y,self.index, engine = targetEngine, multicore = multi)
self.numModes = self.LGmodesArray.shape[0]
def computeAllmodes(self, multi):
print('Generating rest of the modes...')
return( mgcl.computeWholeSetofModes( self.LGmodesArray, self.index,multicore = multi ) )
def computeAllmodesFarField(self, multi):
print('Generating rest of the modes at the far field...')
return( mgcl.computeWholeSetofModesFarField( self.LGmodesArray, self.index, self.farfieldGouyPhase, multicore = multi ) )
def updateLGmodesAll(self,multi):
"""
Update or compute LGmodes in case you changed object atributes
"""
self.LGmodesArray__ = mgcl.computeWholeSetofModes(self.LGmodesArray, self.index,multicore = multi)
self.numModesAll = self.LGmodesArray__.shape[0]
def getSpecs(self):
p = {
'mfd': (2*self.w0),
'mode_group': self.group,
'num_samples': self.N,
'px_size' : self.px_size,
'num_modes' : self.numModes,
'num_modes_all' : self.numModesAll,
'FarFieldGouy' : self.farfieldGouyPhase,
}
self.parameters = p
return(p)
# print('LG modes specs:')
# for key,value in parameters.items():
# print(key, value)
class overlaps():
def __init__(self):
pass
@classmethod
def Modal_decomposition(cls,Modes2D,frames):
N_ele = len(frames.shape)
N_frames = 0
if N_ele == 2:
N_px = frames.shape[0] * frames.shape[1]
N_frames = 1
else:
N_px = frames.shape[1] * frames.shape[2]
N_frames = frames.shape[0]
frames = reshape(frames,(N_frames,N_px))
coef = matmul(frames,Modes2D)
return(coef)
@classmethod
def Modal_reconstruction(cls,Modes2D,coefs):
N_ele = len(coefs.shape)
N_frames = 0
N_px = int(sqrt(Modes2D.shape[0]))
if N_ele == 1:
N_frames = 1
else:
N_frames = coefs.shape[0]
rec = matmul(coefs,transpose(Modes2D))
rec = reshape(rec, (N_frames,N_px,N_px) )
return(rec)
@classmethod
def Modal_decomposition_gpu(cls,Modes2D,frames):
N_ele = len(frames.shape)
N_frames = 0
if N_ele == 2:
N_px = frames.shape[0] * frames.shape[1]
N_frames = 1
else:
N_px = frames.shape[1] * frames.shape[2]
N_frames = frames.shape[0]
frames = cp.reshape(frames,(N_frames,N_px))
coef = cp.matmul(frames,Modes2D)
return(coef)
@classmethod
def Modal_reconstruction_gpu(cls,Modes2D,coefs):
N_ele = len(coefs.shape)
N_frames = 0
N_px = int(cp.sqrt(Modes2D.shape[0]))
if N_ele == 1:
N_frames = 1
else:
N_frames = coefs.shape[0]
rec = cp.matmul(coefs,cp.transpose(Modes2D))
rec = cp.reshape(rec, (N_frames,N_px,N_px) )
return(rec)
if __name__ == '__main__':
t = mgcl.times
import cupy as cp
print(cp.cuda.Device(0).mem_info)
t.tic()
LGgpu = LGmodes(48, 22 , 256, 1 , generateModes = True, wholeSet = True, farfield = False, engine = 'GPU', multicore = True)
t.toc()
#t.tic()
#LGold = LGmodes(15.3,11,1024,1.661217730978261,generateModes = True, wholeSet = False, engine = 'CPU', multicore = False)
#t.toc()