A differentiable LDOS
| 545 | |
| 546 | |
| 547 | class LDOS(ObjectiveQuantity): |
| 548 | """A differentiable LDOS""" |
| 549 | |
| 550 | def __init__(self, sim: mp.Simulation, **kwargs): |
| 551 | """Initialize a differentiable LDOS instance |
| 552 | |
| 553 | Args: |
| 554 | sim: the Meep simulation object of the problem. |
| 555 | """ |
| 556 | super().__init__(sim) |
| 557 | self.srckwarg = kwargs |
| 558 | |
| 559 | def register_monitors(self, frequencies): |
| 560 | self._frequencies = np.asarray(frequencies) |
| 561 | self._forward_src = self.sim.sources |
| 562 | return |
| 563 | |
| 564 | def place_adjoint_source(self, dJ): |
| 565 | time_src = self._create_time_profile() |
| 566 | if dJ.ndim == 2: |
| 567 | dJ = np.sum(dJ, axis=1) |
| 568 | dJ = dJ.flatten() |
| 569 | sources = [] |
| 570 | forward_f_scale = np.array( |
| 571 | [self._ldos_scale / self._ldos_Jdata[k] for k in range(self.num_freq)] |
| 572 | ) |
| 573 | if self._frequencies.size == 1: |
| 574 | amp = (dJ * self._adj_src_scale(False) * forward_f_scale)[0] |
| 575 | src = time_src |
| 576 | else: |
| 577 | scale = dJ * self._adj_src_scale(False) * forward_f_scale |
| 578 | src = FilteredSource( |
| 579 | time_src.frequency, |
| 580 | self._frequencies, |
| 581 | scale, |
| 582 | self.sim.fields.dt, |
| 583 | ) |
| 584 | amp = 1 |
| 585 | for forward_src_i in self._forward_src: |
| 586 | if isinstance(forward_src_i, mp.EigenModeSource): |
| 587 | src_i = mp.EigenModeSource( |
| 588 | src, |
| 589 | component=forward_src_i.component, |
| 590 | eig_kpoint=forward_src_i.eig_kpoint, |
| 591 | amplitude=amp, |
| 592 | eig_band=forward_src_i.eig_band, |
| 593 | size=forward_src_i.size, |
| 594 | center=forward_src_i.center, |
| 595 | **self.srckwarg, |
| 596 | ) |
| 597 | else: |
| 598 | src_i = mp.Source( |
| 599 | src, |
| 600 | component=forward_src_i.component, |
| 601 | amplitude=amp, |
| 602 | size=forward_src_i.size, |
| 603 | center=forward_src_i.center, |
| 604 | **self.srckwarg, |
nothing calls this directly
no outgoing calls
no test coverage detected