Skip to content

Commit 242bc39

Browse files
committed
Pushing changes that somehow slipped between the cracks
1 parent 5253b9b commit 242bc39

File tree

3 files changed

+130
-49
lines changed

3 files changed

+130
-49
lines changed

Diff for: emopt/fdtd.py

+10
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ def __init__(self, X, Y, Z, dx, dy, dz, wavelength, rtol=1e-6, nconv=None,
414414
# the fields between processors
415415
self._gc = GhostComm(k0, j0, i0, K, J, I, Nx, Ny, Nz)
416416

417+
# keep track of number of simulations
418+
self.forward_count = 0
419+
self.adjoint_count = 0
420+
417421
@property
418422
def wavelength(self):
419423
self._wavelength = wavelength
@@ -1234,6 +1238,9 @@ def solve_forward(self):
12341238
else:
12351239
self._source_power = MathDummy()
12361240

1241+
# update count (for diagnostics)
1242+
self.forward_count += 1
1243+
12371244
def solve_adjoint(self):
12381245
"""Run an adjoint simulation.
12391246
@@ -1281,6 +1288,9 @@ def solve_adjoint(self):
12811288

12821289
self.__solve()
12831290

1291+
# update count (for diagnostics)
1292+
self.adjoint_count += 1
1293+
12841294
def update_saved_fields(self):
12851295
"""Update the fields contained in the regions specified by
12861296
self.field_domains.

Diff for: emopt/fomutils.py

+119-48
Original file line numberDiff line numberDiff line change
@@ -554,16 +554,24 @@ def ndisty(x, y, y0):
554554

555555
return dists
556556

557-
def d_ndisty_dx1(x, y, y0):
557+
def d_ndisty_dx1(x1, x2, x3, y1, y2, y3, y0):
558558
"""Calculate the derivative of ndisty with respect the the x coordinate of
559559
the 'previous' point.
560560
561561
Parameters
562562
---------
563-
x : list or np.array
564-
X coordinates of polygon
565-
y : list or np.array
566-
Y coordinates of polygon
563+
x1 : float or list or np.array
564+
X coordinate(s) of "previous" point
565+
x2 : float or list or np.array
566+
X coordinate(s) of "current" point
567+
x3 : float or list or np.array
568+
X coordinate(s) of "next" point
569+
y1 : float or list or np.array
570+
Y coordinate(s) of "previous" point
571+
y2 : float or list or np.array
572+
Y coordinate(s) of "current" point
573+
y3 : float or list or np.array
574+
Y coordinate(s) of "next" point
567575
y0 : float
568576
The y position to which the distance is calculated.
569577
@@ -572,19 +580,27 @@ def d_ndisty_dx1(x, y, y0):
572580
np.array
573581
List of derivatives with length N-2 where N=len(x)
574582
"""
575-
return -(y0 - y2)**2*(y1 - y3)**2/(sqrt((y0 - y2)**2*((x1 - x3)**2 + \
583+
return -(y0 - y2)**2*(y1 - y3)**2/(np.sqrt((y0 - y2)**2*((x1 - x3)**2 + \
576584
(y1 - y3)**2)/(x1 - x3)**2)*(x1 - x3)**3)
577585

578-
def d_ndisty_dx2(x, y, y0):
586+
def d_ndisty_dx2(x1, x2, x3, y1, y2, y3, y0):
579587
"""Calculate the derivative of ndisty with respect the the x coordinate of
580588
the 'current' point.
581589
582590
Parameters
583591
---------
584-
x : list or np.array
585-
X coordinates of polygon
586-
y : list or np.array
587-
Y coordinates of polygon
592+
x1 : float or list or np.array
593+
X coordinate(s) of "previous" point
594+
x2 : float or list or np.array
595+
X coordinate(s) of "current" point
596+
x3 : float or list or np.array
597+
X coordinate(s) of "next" point
598+
y1 : float or list or np.array
599+
Y coordinate(s) of "previous" point
600+
y2 : float or list or np.array
601+
Y coordinate(s) of "current" point
602+
y3 : float or list or np.array
603+
Y coordinate(s) of "next" point
588604
y0 : float
589605
The y position to which the distance is calculated.
590606
@@ -595,16 +611,24 @@ def d_ndisty_dx2(x, y, y0):
595611
"""
596612
return 0.0
597613

598-
def d_ndisty_dx3(x, y, y0):
614+
def d_ndisty_dx3(x1, x2, x3, y1, y2, y3, y0):
599615
"""Calculate the derivative of ndisty with respect the the x coordinate of
600616
the 'next' point.
601617
602618
Parameters
603619
---------
604-
x : list or np.array
605-
X coordinates of polygon
606-
y : list or np.array
607-
Y coordinates of polygon
620+
x1 : float or list or np.array
621+
X coordinate(s) of "previous" point
622+
x2 : float or list or np.array
623+
X coordinate(s) of "current" point
624+
x3 : float or list or np.array
625+
X coordinate(s) of "next" point
626+
y1 : float or list or np.array
627+
Y coordinate(s) of "previous" point
628+
y2 : float or list or np.array
629+
Y coordinate(s) of "current" point
630+
y3 : float or list or np.array
631+
Y coordinate(s) of "next" point
608632
y0 : float
609633
The y position to which the distance is calculated.
610634
@@ -613,19 +637,27 @@ def d_ndisty_dx3(x, y, y0):
613637
np.array
614638
List of derivatives with length N-2 where N=len(x)
615639
"""
616-
return (y0 - y2)**2*(y1 - y3)**2/(sqrt((y0 - y2)**2*((x1 - x3)**2 + \
640+
return (y0 - y2)**2*(y1 - y3)**2/(np.sqrt((y0 - y2)**2*((x1 - x3)**2 + \
617641
(y1 - y3)**2)/(x1 - x3)**2)*(x1 - x3)**3)
618642

619-
def d_ndisty_dy1(x, y, y0):
643+
def d_ndisty_dy1(x1, x2, x3, y1, y2, y3, y0):
620644
"""Calculate the derivative of ndisty with respect the the y coordinate of
621645
the 'previous' point.
622646
623647
Parameters
624648
---------
625-
x : list or np.array
626-
X coordinates of polygon
627-
y : list or np.array
628-
Y coordinates of polygon
649+
x1 : float or list or np.array
650+
X coordinate(s) of "previous" point
651+
x2 : float or list or np.array
652+
X coordinate(s) of "current" point
653+
x3 : float or list or np.array
654+
X coordinate(s) of "next" point
655+
y1 : float or list or np.array
656+
Y coordinate(s) of "previous" point
657+
y2 : float or list or np.array
658+
Y coordinate(s) of "current" point
659+
y3 : float or list or np.array
660+
Y coordinate(s) of "next" point
629661
y0 : float
630662
The y position to which the distance is calculated.
631663
@@ -634,19 +666,27 @@ def d_ndisty_dy1(x, y, y0):
634666
np.array
635667
List of derivatives with length N-2 where N=len(x)
636668
"""
637-
return (y0 - y2)**2*(y1 - y3)/(sqrt((y0 - y2)**2*((x1 - x3)**2 + \
669+
return (y0 - y2)**2*(y1 - y3)/(np.sqrt((y0 - y2)**2*((x1 - x3)**2 + \
638670
(y1 - y3)**2)/(x1 - x3)**2)*(x1 - x3)**2)
639671

640-
def d_ndisty_dy2(x, y, y0):
672+
def d_ndisty_dy2(x1, x2, x3, y1, y2, y3, y0):
641673
"""Calculate the derivative of ndisty with respect the the y coordinate of
642674
the 'current' point.
643675
644676
Parameters
645677
---------
646-
x : list or np.array
647-
X coordinates of polygon
648-
y : list or np.array
649-
Y coordinates of polygon
678+
x1 : float or list or np.array
679+
X coordinate(s) of "previous" point
680+
x2 : float or list or np.array
681+
X coordinate(s) of "current" point
682+
x3 : float or list or np.array
683+
X coordinate(s) of "next" point
684+
y1 : float or list or np.array
685+
Y coordinate(s) of "previous" point
686+
y2 : float or list or np.array
687+
Y coordinate(s) of "current" point
688+
y3 : float or list or np.array
689+
Y coordinate(s) of "next" point
650690
y0 : float
651691
The y position to which the distance is calculated.
652692
@@ -655,19 +695,27 @@ def d_ndisty_dy2(x, y, y0):
655695
np.array
656696
List of derivatives with length N-2 where N=len(x)
657697
"""
658-
return (-y0 + y2)*((x1 - x3)**2 + (y1 - y3)**2)/(sqrt((y0 - y2)**2 * \
698+
return (-y0 + y2)*((x1 - x3)**2 + (y1 - y3)**2)/(np.sqrt((y0 - y2)**2 * \
659699
((x1 - x3)**2 + (y1 - y3)**2)/(x1 - x3)**2)*(x1 - x3)**2)
660700

661-
def d_ndisty_dy3(x, y, y0):
701+
def d_ndisty_dy3(x1, x2, x3, y1, y2, y3, y0):
662702
"""Calculate the derivative of ndisty with respect the the y coordinate of
663703
the 'next' point.
664704
665705
Parameters
666706
---------
667-
x : list or np.array
668-
X coordinates of polygon
669-
y : list or np.array
670-
Y coordinates of polygon
707+
x1 : float or list or np.array
708+
X coordinate(s) of "previous" point
709+
x2 : float or list or np.array
710+
X coordinate(s) of "current" point
711+
x3 : float or list or np.array
712+
X coordinate(s) of "next" point
713+
y1 : float or list or np.array
714+
Y coordinate(s) of "previous" point
715+
y2 : float or list or np.array
716+
Y coordinate(s) of "current" point
717+
y3 : float or list or np.array
718+
Y coordinate(s) of "next" point
671719
y0 : float
672720
The y position to which the distance is calculated.
673721
@@ -676,7 +724,7 @@ def d_ndisty_dy3(x, y, y0):
676724
np.array
677725
List of derivatives with length N-2 where N=len(x)
678726
"""
679-
return (y0 - y2)**2*(-y1 + y3)/(sqrt((y0 - y2)**2*((x1 - x3)**2 + \
727+
return (y0 - y2)**2*(-y1 + y3)/(np.sqrt((y0 - y2)**2*((x1 - x3)**2 + \
680728
(y1 - y3)**2)/(x1 - x3)**2)*(x1 - x3)**2)
681729

682730
def ndisty_penalty(x, y, y0, dmin, delta_d, inds=None):
@@ -694,9 +742,8 @@ def ndisty_penalty(x, y, y0, dmin, delta_d, inds=None):
694742
695743
Notes
696744
-----
697-
This function assumes that the supplied indices are contiguous in
698-
increasing order. In other words, indices must have the form
699-
[i, i+1, i+2, i+3, i+4, ...].
745+
The inds parameter may not contain 0 or N-1 where N is the length of x and
746+
y.
700747
701748
Parameters
702749
---------
@@ -720,17 +767,14 @@ def ndisty_penalty(x, y, y0, dmin, delta_d, inds=None):
720767
float
721768
The value of the penalty.
722769
"""
723-
if(inds == None):
770+
if(inds is None):
724771
inds = np.arange(1,len(x)-1)
725772
else:
726773
inds = np.array(inds)
727774

728-
indices = np.concatenate([inds[0:1]-1, inds, inds[-1:]+1])
729-
xs = x[indices]
730-
ys = y[indices]
731-
ndist = ndisty(xs, ys, y0)
775+
ndist = ndisty(x, y, y0)
732776

733-
penalties = rect(ndist, dmin*2, delta_d)
777+
penalties = rect(ndist[inds-1], dmin*2, delta_d)
734778
return np.sum(penalties)
735779

736780
def ndisty_penalty_derivative(x, y, y0, dmin, delta_d, inds=None):
@@ -739,9 +783,8 @@ def ndisty_penalty_derivative(x, y, y0, dmin, delta_d, inds=None):
739783
740784
Notes
741785
-----
742-
This function assumes that the supplied indices are contiguous in
743-
increasing order. In other words, indices must have the form
744-
[i, i+1, i+2, i+3, i+4, ...].
786+
The inds parameter may not contain 0 or N-1 where N is the length of x and
787+
y.
745788
746789
Parameters
747790
---------
@@ -765,11 +808,39 @@ def ndisty_penalty_derivative(x, y, y0, dmin, delta_d, inds=None):
765808
numpy.ndarray, numpy.ndarray
766809
The derivatives with respect to the x and y coordinates, respectively.
767810
"""
768-
if(inds == None):
811+
if(inds is None):
769812
inds = np.arange(1,len(x)-1)
770813
else:
771814
inds = np.array(inds)
772815

816+
Ni = len(inds)
817+
dPdx = np.zeros(Ni)
818+
dPdy = np.zeros(Ni)
819+
820+
# Currently using a for loop in order to allow indices to be supplied in
821+
# any order. If points were supplied in increasing order, the we could
822+
# vectorize it :S
823+
for i in range(Ni):
824+
j = inds[i]
825+
x1 = x[j-1]; x2 = x[j]; x3 = x[j+1]
826+
y1 = y[j-1]; y2 = y[j]; y3 = y[j+1]
827+
828+
ndist = ndisty([x1, x2, x3], [y1, y2, y3], y0)
829+
rect_deriv = rect_derivative(ndist, 2*dmin, delta_d)
830+
831+
dPdx[i] += rect_deriv * d_ndisty_dx2(x1, x2, x3, y1, y2, y3, y0)
832+
dPdy[i] += rect_deriv * d_ndisty_dy2(x1, x2, x3, y1, y2, y3, y0)
833+
834+
if(i > 0 and j-1 == inds[i-1]):
835+
dPdx[i-1] += rect_deriv * d_ndisty_dx1(x1, x2, x3, y1, y2, y3, y0)
836+
dPdy[i-1] += rect_deriv * d_ndisty_dy1(x1, x2, x3, y1, y2, y3, y0)
837+
838+
if(i < Ni-1 and j+1 == inds[i+1]):
839+
dPdx[i+1] += rect_deriv * d_ndisty_dx3(x1, x2, x3, y1, y2, y3, y0)
840+
dPdy[i+1] += rect_deriv * d_ndisty_dy3(x1, x2, x3, y1, y2, y3, y0)
841+
842+
return np.nan_to_num(dPdx), np.nan_to_num(dPdy)
843+
773844
def dist_to_edges(x1, x2, x3, y1, y2, y3, xe, ye):
774845
"""Calculate the signed distance to a set of edges.
775846

Diff for: examples/Silicon_Grating_Coupler_2L/sg2l_opt.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def plot_update(params, fom_list, sim, am):
255255
# define the system parameters
256256
####################################################################################
257257
wavelength = 1.55
258-
W = 25.0
258+
W = 24.0
259259
H = 8.0
260260
dx = 0.03
261261
dy = dx

0 commit comments

Comments
 (0)