# Derivatives (Sensitivities)ΒΆ

Derivative information of the goal functional with respect to input parameters are useful for studying the sensitivity of the output quantity under small perturbations of the input parameters. Furthermore, the most performance optimization and higher order interpolation require the computation of functional derivatives.

To support this, `JCMsuite`

allows for the automatic computation of the derivatives. Furthermore, by using derivative information parameter scans can be accelerated by an higher order interpolation method.

This example demonstrates the computation of derivatives of the transmitted Fourier coefficients with respect to the plane wave illumination parameters and the direction angles , , as well as with respect to the geometrical parameters (sidewall angles of the line structure).

To define the illumination derivatives, remind that the `sources.jcm`

input file has the data-tree like form

```
SourceBag {
Source {
ElectricFieldStrength {
PlaneWave {
Lambda0 = ...
ThetaPhi = [...]
}
}
}
```

You can identify each input value by its tree path. For example the parameter `Lambda0`

within the `PlaneWave`

section has the path `SourceBag/Source/PlaneWave/Lambda0`

.

To activate automatic differentiation we declare derivative parameters at the beginning of the `sources.jcm`

input file:

```
DerivativeParameter {
Name = "Lambda0"
TreeEntry = SourceBag/Source/ElectricFieldStrength/PlaneWave/Lambda0
Scaling = 1e-9 # scale wavelength parameter in nanometer to have similar
# scaling of all derivative parameters
}
DerivativeParameter {
Name = "Theta"
TreeEntry = SourceBag/Source/ElectricFieldStrength/PlaneWave/ThetaPhi(1)
}
DerivativeParameter {
Name = "Phi"
TreeEntry = SourceBag/Source/ElectricFieldStrength/PlaneWave/ThetaPhi(2)
}
```

With a `TreeEntry`

declaration we select a scalar input value for differentiation its tree path. The `Name`

of the derivative parameters is used in the output files to refer to the specific derivative (see below for an usage within Matlab). A `Scaling`

can be applied to account for a non-unitary influence on the input parameter.

Similarly, the `layout.jcm`

input file has the data-tree like form

```
Layout2D {
Objects {
...
Trapezoid {
Name = "Line"
InnerAngles = [86 86]
...
}
...
}
}
```

The first sidewall angle of the line grating structure has the tree path `Layout2D/Objects/Line/InnerAngles(1)`

(Here we used `Line`

instead of the section tag `Trapezoid`

. This is allowed for named section with a `Name`

attribute).

To activate automatic differentiation we declare derivative parameters at the beginning of the `layout.jcm`

input file:

```
DerivativeParameter {
Name = "Angle1"
TreeEntry = Layout2D/Objects/Line/InnerAngles(1)
}
DerivativeParameter {
Name = "Angle2"
TreeEntry = Layout2D/Objects/Line/InnerAngles(2)
}
```

Within Matlab, the table of the transmitted Fourier coefficients now comes with additional fields, named after the parameter`s name, containing the derivatives with respect to the three parameters:

```
%% primary Fourier coefficients of the two fields
ft_s = fourierTransform_transmitted.ElectricFieldStrength{1};
ft_p = fourierTransform_transmitted.ElectricFieldStrength{2};
%% lambda0-derivatives of the Fourier coefficients of the two fields
dLambda0_ft_s = fourierTransform_transmitted.d_Lambda0.ElectricFieldStrength{1};
dLambda0_ft_p = fourierTransform_transmitted.d_Lambda0.ElectricFieldStrength{2};
%% theta-derivatives of the Fourier coefficients of the two fields
dTheta_ft_s = fourierTransform_transmitted.d_Theta.ElectricFieldStrength{1};
dTheta_ft_p = fourierTransform_transmitted.d_Theta.ElectricFieldStrength{2};
%% phi-derivatives of the Fourier coefficients of the two fields
dPhi_ft_s = fourierTransform_transmitted.d_Phi.ElectricFieldStrength{1};
dPhi_ft_p = fourierTransform_transmitted.d_Phi.ElectricFieldStrength{2};
%% Angle1-derivatives of the Fourier coefficients of the two fields
dAngle1_ft_s = fourierTransform_transmitted.d_Angle1.ElectricFieldStrength{1};
dAngle1_ft_p = fourierTransform_transmitted.d_Angle1.ElectricFieldStrength{2};
%% Angle2-derivatives of the Fourier coefficients of the two fields
dAngle2_ft_s = fourierTransform_transmitted.d_Angle2.ElectricFieldStrength{1};
dAngle2_ft_p = fourierTransform_transmitted.d_Angle2.ElectricFieldStrength{2};
```

For demonstration purposes the script `data_analysis/compare_diff_quotient.m`

compares the automatically computed derivatives with a numerical differentiation based on a difference quotient.

Note

The built-in automatic differentiation is faster and more accurate than the numerical differentiation based on difference quotients.