MCPcopy Create free account
hub / github.com/NanoComp/meep / get_gradient

Method get_gradient

python/adjoint/utils.py:46–77  ·  view source on GitHub ↗
(
        self,
        sim: mp.Simulation,
        fields_a: List[mp.DftFields],
        fields_f: List[mp.DftFields],
        frequencies: List[float],
        finite_difference_step: float,
    )

Source from the content-addressed store, hash-verified

44 self.design_parameters.beta = beta
45
46 def get_gradient(
47 self,
48 sim: mp.Simulation,
49 fields_a: List[mp.DftFields],
50 fields_f: List[mp.DftFields],
51 frequencies: List[float],
52 finite_difference_step: float,
53 ) -> onp.ndarray:
54 num_freqs = onp.array(frequencies).size
55 """We have the option to linearly scale the gradients up front
56 using the scalegrad parameter (leftover from MPB API). Not
57 currently needed for any existing feature (but available for
58 future use)"""
59 scalegrad = 1
60 grad = onp.zeros((num_freqs, self.num_design_params)) # preallocate
61 vol = sim._fit_volume_to_simulation(self.volume)
62 # compute the gradient
63 mp._get_gradient(
64 grad,
65 scalegrad,
66 fields_a[0].swigobj,
67 fields_a[1].swigobj,
68 fields_a[2].swigobj,
69 fields_f[0].swigobj,
70 fields_f[1].swigobj,
71 fields_f[2].swigobj,
72 sim.gv,
73 onp.array(frequencies),
74 sim.geps,
75 finite_difference_step,
76 )
77 return onp.squeeze(grad).T
78
79
80def _check_if_cylindrical(sim: mp.Simulation) -> bool:

Callers 2

calculate_vjpsFunction · 0.80
calculate_gradientMethod · 0.80

Calls 1

Tested by

no test coverage detected