|
| 1 | +"""OpenMDAO component for computing Sobol sensitivity indices.""" |
| 2 | +import numpy as np |
| 3 | +import openmdao.api as om |
| 4 | +from uqpce.pce._helpers import calc_sobols, create_total_sobols |
| 5 | + |
| 6 | + |
| 7 | +class SobolComp(om.ExplicitComponent): |
| 8 | + """ |
| 9 | + Computes Sobol sensitivity indices using existing UQPCE functions. |
| 10 | +
|
| 11 | + This component wraps uqpce.pce._helpers.calc_sobols() and |
| 12 | + create_total_sobols() to expose Sobol indices as OpenMDAO outputs. |
| 13 | +
|
| 14 | + Individual Sobol indices show the variance contribution of each PCE term. |
| 15 | + Total Sobol indices show the variance contribution of each uncertain variable |
| 16 | + (including interaction effects). |
| 17 | +
|
| 18 | + Mathematical formulation: |
| 19 | + variance = Σ(coeff[i]² × norm_sq[i]) for i > 0 |
| 20 | + sobol[i-1] = (coeff[i]² × norm_sq[i]) / variance |
| 21 | + total_sobol[j] = Σ sobol[i-1] where variable j appears in term i |
| 22 | + """ |
| 23 | + |
| 24 | + def initialize(self): |
| 25 | + """Declare options for the component.""" |
| 26 | + self.options.declare( |
| 27 | + 'norm_sq', types=np.ndarray, allow_none=False, |
| 28 | + desc='Norm squared for PCE terms' |
| 29 | + ) |
| 30 | + self.options.declare( |
| 31 | + 'model_matrix', types=np.ndarray, allow_none=False, |
| 32 | + desc='Interaction matrix for computing total Sobols. ' |
| 33 | + 'Shape: (n_terms, n_vars). Entry [i,j] indicates if variable j ' |
| 34 | + 'appears in PCE term i.' |
| 35 | + ) |
| 36 | + |
| 37 | + def setup(self): |
| 38 | + """Set up inputs and outputs for the component.""" |
| 39 | + norm_sq = self.options['norm_sq'] |
| 40 | + model_matrix = self.options['model_matrix'] |
| 41 | + |
| 42 | + n_terms = len(norm_sq) |
| 43 | + n_vars = model_matrix.shape[1] |
| 44 | + n_sobols = n_terms - 1 # Exclude intercept |
| 45 | + |
| 46 | + # Input: PCE coefficients from CoefficientsComp |
| 47 | + self.add_input( |
| 48 | + 'matrix_coeffs', shape=(n_terms,), |
| 49 | + desc='PCE coefficients from CoefficientsComp' |
| 50 | + ) |
| 51 | + |
| 52 | + # Output: Individual Sobol indices (one per PCE term, excluding intercept) |
| 53 | + self.add_output( |
| 54 | + 'sobols', shape=(n_sobols,), |
| 55 | + desc='Individual Sobol indices (variance contribution per PCE term)' |
| 56 | + ) |
| 57 | + |
| 58 | + # Output: Total Sobol indices (one per uncertain variable) |
| 59 | + self.add_output( |
| 60 | + 'total_sobols', shape=(n_vars,), |
| 61 | + desc='Total Sobol indices (variance contribution per variable, including interactions)' |
| 62 | + ) |
| 63 | + |
| 64 | + # Declare partials with analytic derivatives |
| 65 | + # Individual Sobols depend on all coefficients (through variance in denominator) |
| 66 | + self.declare_partials('sobols', 'matrix_coeffs', method='exact') |
| 67 | + |
| 68 | + # Total Sobols are linear combinations of individual Sobols |
| 69 | + # The Jacobian is sparse based on model_matrix structure |
| 70 | + # For simplicity, declare as dense (could optimize to sparse later) |
| 71 | + self.declare_partials('total_sobols', 'matrix_coeffs', method='exact') |
| 72 | + |
| 73 | + def compute(self, inputs, outputs): |
| 74 | + """ |
| 75 | + Compute Sobols using existing UQPCE functions. |
| 76 | +
|
| 77 | + Uses: |
| 78 | + - uqpce.pce._helpers.calc_sobols() for individual Sobols |
| 79 | + - uqpce.pce._helpers.create_total_sobols() for total Sobols |
| 80 | +
|
| 81 | + Supports complex step by taking real part of complex inputs. |
| 82 | + """ |
| 83 | + matrix_coeffs = inputs['matrix_coeffs'] |
| 84 | + norm_sq = self.options['norm_sq'] |
| 85 | + model_matrix = self.options['model_matrix'] |
| 86 | + |
| 87 | + # Handle complex step: UQPCE functions don't support complex, |
| 88 | + # but Sobols are real-valued functions of real coefficients |
| 89 | + if np.iscomplexobj(matrix_coeffs): |
| 90 | + matrix_coeffs = matrix_coeffs.real |
| 91 | + |
| 92 | + # Compute individual Sobols using existing tested UQPCE function |
| 93 | + sobols = calc_sobols(matrix_coeffs, norm_sq) |
| 94 | + outputs['sobols'] = sobols |
| 95 | + |
| 96 | + # Compute total Sobols using existing tested UQPCE function |
| 97 | + var_count = model_matrix.shape[1] |
| 98 | + |
| 99 | + # create_total_sobols expects sobols to be 2D: (n_terms, n_responses) |
| 100 | + # Reshape 1D sobols to 2D (n_terms, 1) for single response |
| 101 | + sobols_2d = sobols.reshape(-1, 1) |
| 102 | + total_sobols = create_total_sobols(var_count, model_matrix, sobols_2d) |
| 103 | + |
| 104 | + # Flatten to 1D array for output |
| 105 | + outputs['total_sobols'] = total_sobols.flatten() |
| 106 | + |
| 107 | + def compute_partials(self, inputs, partials): |
| 108 | + """ |
| 109 | + Compute analytic derivatives of Sobols with respect to coefficients. |
| 110 | +
|
| 111 | + For sobol[i-1] = (coeff[i]² × norm_sq[i]) / variance: |
| 112 | +
|
| 113 | + ∂sobol[i-1]/∂coeff[i] = 2×coeff[i]×norm_sq[i]/variance |
| 114 | + - sobol[i-1]×2×coeff[i]×norm_sq[i]/variance |
| 115 | + = 2×coeff[i]×norm_sq[i]/variance × (1 - sobol[i-1]) |
| 116 | +
|
| 117 | + ∂sobol[i-1]/∂coeff[j] (j≠i) = -sobol[i-1]×2×coeff[j]×norm_sq[j]/variance |
| 118 | + = -2×coeff[j]×norm_sq[j]×sobol[i-1]/variance |
| 119 | +
|
| 120 | + For total_sobol[var] = Σ sobol[i] where model_matrix[i, var] != 0: |
| 121 | +
|
| 122 | + ∂total_sobol[var]/∂coeff[j] = Σ ∂sobol[i]/∂coeff[j] for relevant i |
| 123 | + """ |
| 124 | + matrix_coeffs = inputs['matrix_coeffs'] |
| 125 | + norm_sq = self.options['norm_sq'] |
| 126 | + model_matrix = self.options['model_matrix'] |
| 127 | + |
| 128 | + n_terms = len(matrix_coeffs) |
| 129 | + n_sobols = n_terms - 1 |
| 130 | + |
| 131 | + # Handle complex step: take real part |
| 132 | + if np.iscomplexobj(matrix_coeffs): |
| 133 | + matrix_coeffs = matrix_coeffs.real |
| 134 | + |
| 135 | + # Compute variance (denominator of Sobol formula) |
| 136 | + # variance = Σ(coeff[i]² × norm_sq[i]) for i > 0 |
| 137 | + # norm_sq is 2D (n_terms, 1), extract scalar values |
| 138 | + variance = 0.0 |
| 139 | + for i in range(1, n_terms): |
| 140 | + variance += matrix_coeffs[i]**2 * float(norm_sq[i]) |
| 141 | + |
| 142 | + # Compute individual Sobols for derivative calculation |
| 143 | + sobols = np.zeros(n_sobols) |
| 144 | + for i in range(1, n_terms): |
| 145 | + sobols[i-1] = (matrix_coeffs[i]**2 * float(norm_sq[i])) / variance |
| 146 | + |
| 147 | + # Initialize Jacobian matrix for d(sobols)/d(matrix_coeffs) |
| 148 | + jac_sobols = np.zeros((n_sobols, n_terms)) |
| 149 | + |
| 150 | + # Derivatives of individual Sobols |
| 151 | + for i in range(1, n_terms): # For each Sobol index |
| 152 | + sobol_idx = i - 1 |
| 153 | + |
| 154 | + for j in range(n_terms): # For each coefficient |
| 155 | + if j == 0: |
| 156 | + # Intercept doesn't affect Sobols |
| 157 | + jac_sobols[sobol_idx, j] = 0.0 |
| 158 | + elif j == i: |
| 159 | + # Derivative with respect to own coefficient |
| 160 | + # ∂sobol[i]/∂coeff[i] = 2×coeff[i]×norm_sq[i]/variance × (1 - sobol[i]) |
| 161 | + jac_sobols[sobol_idx, j] = ( |
| 162 | + 2.0 * matrix_coeffs[i] * float(norm_sq[i]) / variance * (1.0 - sobols[sobol_idx]) |
| 163 | + ) |
| 164 | + else: |
| 165 | + # Derivative with respect to other coefficients (through variance) |
| 166 | + # ∂sobol[i]/∂coeff[j] = -2×coeff[j]×norm_sq[j]×sobol[i]/variance |
| 167 | + jac_sobols[sobol_idx, j] = ( |
| 168 | + -2.0 * matrix_coeffs[j] * float(norm_sq[j]) * sobols[sobol_idx] / variance |
| 169 | + ) |
| 170 | + |
| 171 | + partials['sobols', 'matrix_coeffs'] = jac_sobols |
| 172 | + |
| 173 | + # Derivatives of total Sobols |
| 174 | + # total_sobol[var] = Σ sobol[i-1] where model_matrix[i, var] != 0 |
| 175 | + # ∂total_sobol[var]/∂coeff[j] = Σ ∂sobol[i-1]/∂coeff[j] for relevant i |
| 176 | + |
| 177 | + n_vars = model_matrix.shape[1] |
| 178 | + jac_total = np.zeros((n_vars, n_terms)) |
| 179 | + |
| 180 | + for var_idx in range(n_vars): # For each variable |
| 181 | + for term_idx in range(1, n_terms): # For each PCE term (excluding intercept) |
| 182 | + if model_matrix[term_idx, var_idx] != 0: |
| 183 | + # This term contributes to this variable's total Sobol |
| 184 | + # Add the derivatives from individual Sobol |
| 185 | + jac_total[var_idx, :] += jac_sobols[term_idx-1, :] |
| 186 | + |
| 187 | + partials['total_sobols', 'matrix_coeffs'] = jac_total |
0 commit comments