-
Notifications
You must be signed in to change notification settings - Fork 641
/
Copy pathobjective.py
253 lines (216 loc) · 10.3 KB
/
objective.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""Handling of objective functions and objective quantities."""
from abc import ABC, abstractmethod
import numpy as np
import meep as mp
from .filter_source import FilteredSource
from .optimization_problem import atleast_3d, Grid
class ObjectiveQuantitiy(ABC):
@abstractmethod
def __init__(self):
return
@abstractmethod
def register_monitors(self):
return
@abstractmethod
def place_adjoint_source(self):
return
@abstractmethod
def __call__(self):
return
@abstractmethod
def get_evaluation(self):
return
class EigenmodeCoefficient(ObjectiveQuantitiy):
def __init__(self,sim,volume,mode,forward=True,kpoint_func=None,**kwargs):
'''
'''
self.sim = sim
self.volume=volume
self.mode=mode
self.forward = 0 if forward else 1
self.normal_direction = None
self.kpoint_func = kpoint_func
self.eval = None
self.EigenMode_kwargs = kwargs
return
def register_monitors(self,frequencies):
self.frequencies = np.asarray(frequencies)
self.monitor = self.sim.add_mode_monitor(frequencies,mp.ModeRegion(center=self.volume.center,size=self.volume.size),yee_grid=True)
self.normal_direction = self.monitor.normal_direction
return self.monitor
def place_adjoint_source(self,dJ):
'''Places an equivalent eigenmode monitor facing the opposite direction. Calculates the
correct scaling/time profile.
dJ ........ the user needs to pass the dJ/dMonitor evaluation
'''
dJ = np.atleast_1d(dJ)
dt = self.sim.fields.dt # the timestep size from sim.fields.dt of the forward sim
# determine starting kpoint for reverse mode eigenmode source
direction_scalar = 1 if self.forward else -1
if self.kpoint_func is None:
if self.normal_direction == 0:
k0 = direction_scalar * mp.Vector3(x=1)
elif self.normal_direction == 1:
k0 = direction_scalar * mp.Vector3(y=1)
elif self.normal_direction == 2:
k0 == direction_scalar * mp.Vector3(z=1)
else:
k0 = direction_scalar * self.kpoint_func(self.time_src.frequency,1)
if dJ.ndim == 2:
dJ = np.sum(dJ,axis=1)
da_dE = 0.5 * self.cscale # scalar popping out of derivative
scale = adj_src_scale(self, dt)
if self.frequencies.size == 1:
# Single frequency simulations. We need to drive it with a time profile.
amp = da_dE * dJ * scale # final scale factor
src = self.time_src
else:
# multi frequency simulations
scale = da_dE * dJ * scale
src = FilteredSource(self.time_src.frequency,self.frequencies,scale,dt) # generate source from broadband response
amp = 1
# generate source object
self.source = [mp.EigenModeSource(src,
eig_band=self.mode,
direction=mp.NO_DIRECTION,
eig_kpoint=k0,
amplitude=amp,
eig_match_freq=True,
size=self.volume.size,
center=self.volume.center,
**self.EigenMode_kwargs)]
return self.source
def __call__(self):
# We just need a workable time profile, so just grab the first available time profile and use that.
self.time_src = self.sim.sources[0].src
# Eigenmode data
direction = mp.NO_DIRECTION if self.kpoint_func else mp.AUTOMATIC
ob = self.sim.get_eigenmode_coefficients(self.monitor,[self.mode],direction=direction,kpoint_func=self.kpoint_func,**self.EigenMode_kwargs)
self.eval = np.squeeze(ob.alpha[:,:,self.forward]) # record eigenmode coefficients for scaling
self.cscale = ob.cscale # pull scaling factor
return self.eval
def get_evaluation(self):
'''Returns the requested eigenmode coefficient.
'''
try:
return self.eval
except AttributeError:
raise RuntimeError("You must first run a forward simulation before resquesting an eigenmode coefficient.")
class FourierFields(ObjectiveQuantitiy):
def __init__(self,sim,volume, component):
self.sim = sim
self.volume=volume
self.eval = None
self.component = component
return
def register_monitors(self,frequencies):
self.frequencies = np.asarray(frequencies)
self.num_freq = len(self.frequencies)
self.monitor = self.sim.add_dft_fields([self.component], self.frequencies, where=self.volume, yee_grid=False)
return self.monitor
def place_adjoint_source(self,dJ):
dt = self.sim.fields.dt # the timestep size from sim.fields.dt of the forward sim
self.sources = []
scale = adj_src_scale(self, dt)
if self.num_freq == 1:
amp = -atleast_3d(dJ[0]) * scale
src = self.time_src
if self.component in [mp.Hx, mp.Hy, mp.Hz]:
amp = -amp
for zi in range(len(self.dg.z)):
for yi in range(len(self.dg.y)):
for xi in range(len(self.dg.x)):
if amp[xi, yi, zi] != 0:
self.sources += [mp.Source(src, component=self.component, amplitude=amp[xi, yi, zi],
center=mp.Vector3(self.dg.x[xi], self.dg.y[yi], self.dg.z[zi]))]
else:
dJ_4d = np.array([atleast_3d(dJ[f]) for f in range(self.num_freq)])
if self.component in [mp.Hx, mp.Hy, mp.Hz]:
dJ_4d = -dJ_4d
for zi in range(len(self.dg.z)):
for yi in range(len(self.dg.y)):
for xi in range(len(self.dg.x)):
final_scale = -dJ_4d[:,xi,yi,zi] * scale
src = FilteredSource(self.time_src.frequency,self.frequencies,final_scale,dt)
self.sources += [mp.Source(src, component=self.component, amplitude=1,
center=mp.Vector3(self.dg.x[xi], self.dg.y[yi], self.dg.z[zi]))]
return self.sources
def __call__(self):
self.time_src = self.sim.sources[0].src
self.dg = Grid(*self.sim.get_array_metadata(dft_cell=self.monitor))
self.eval = np.array([self.sim.get_dft_array(self.monitor, self.component, i) for i in range(self.num_freq)]) #Shape = (num_freq, [pts])
return self.eval
def get_evaluation(self):
try:
return self.eval
except AttributeError:
raise RuntimeError("You must first run a forward simulation.")
class Near2FarFields(ObjectiveQuantitiy):
def __init__(self,sim,Near2FarRegions, far_pt):
self.sim = sim
self.Near2FarRegions=Near2FarRegions
self.eval = None
self.far_pt = far_pt
return
def register_monitors(self,frequencies):
self.frequencies = np.asarray(frequencies)
self.num_freq = len(self.frequencies)
self.monitor = self.sim.add_near2far(self.frequencies, *self.Near2FarRegions, yee_grid=True)
return self.monitor
def place_adjoint_source(self,dJ):
dt = self.sim.fields.dt # the timestep size from sim.fields.dt of the forward sim
self.sources = []
dJ = dJ.flatten()
#TODO far_pts in 3d or cylindrical, perhaps py_v3_to_vec from simulation.py
self.all_nearsrcdata = self.monitor.swigobj.near_sourcedata(mp.vec(self.far_pt.x, self.far_pt.y), dJ)
for near_data in self.all_nearsrcdata:
cur_comp = near_data.near_fd_comp
amp_arr = np.array(near_data.amp_arr).reshape(-1, self.num_freq)
scale = amp_arr * adj_src_scale(self, dt, include_resolution=False)
if self.num_freq == 1:
self.sources += [mp.IndexedSource(self.time_src, near_data, scale[:,0])]
else:
src = FilteredSource(self.time_src.frequency,self.frequencies,scale,dt)
(num_basis, num_pts) = src.nodes.shape
for basis_i in range(num_basis):
self.sources += [mp.IndexedSource(src.time_src_bf[basis_i], near_data, src.nodes[basis_i])]
return self.sources
def __call__(self):
self.time_src = self.sim.sources[0].src
self.eval = np.array(self.sim.get_farfield(self.monitor, self.far_pt))
self.eval = self.eval.reshape((self.num_freq, 6))
return self.eval
def get_evaluation(self):
try:
return self.eval
except AttributeError:
raise RuntimeError("You must first run a forward simulation.")
def adj_src_scale(obj_quantity, dt, include_resolution=True):
# -------------------------------------- #
# Get scaling factor
# -------------------------------------- #
# leverage linearity and combine source for multiple frequencies
T = obj_quantity.sim.meep_time()
if not include_resolution:
dV = 1
elif obj_quantity.sim.cell_size.y == 0:
dV = 1/obj_quantity.sim.resolution
elif obj_quantity.sim.cell_size.z == 0:
dV = 1/obj_quantity.sim.resolution * 1/obj_quantity.sim.resolution
else:
dV = 1/obj_quantity.sim.resolution * 1/obj_quantity.sim.resolution * 1/obj_quantity.sim.resolution
iomega = (1.0 - np.exp(-1j * (2 * np.pi * obj_quantity.frequencies) * dt)) * (1.0 / dt) # scaled frequency factor with discrete time derivative fix
src = obj_quantity.time_src
# an ugly way to calcuate the scaled dtft of the forward source
y = np.array([src.swigobj.current(t,dt) for t in np.arange(0,T,dt)]) # time domain signal
fwd_dtft = np.matmul(np.exp(1j*2*np.pi*obj_quantity.frequencies[:,np.newaxis]*np.arange(y.size)*dt), y)*dt/np.sqrt(2*np.pi) # dtft
# we need to compensate for the phase added by the time envelope at our freq of interest
src_center_dtft = np.matmul(np.exp(1j*2*np.pi*np.array([src.frequency])[:,np.newaxis]*np.arange(y.size)*dt), y)*dt/np.sqrt(2*np.pi)
adj_src_phase = np.exp(1j*np.angle(src_center_dtft))
if obj_quantity.frequencies.size == 1:
# Single frequency simulations. We need to drive it with a time profile.
scale = dV * iomega / fwd_dtft / adj_src_phase # final scale factor
else:
# multi frequency simulations
scale = dV * iomega / adj_src_phase
return scale