1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
#! /usr/bin/env python
import openturns as ot
import math as m
ot.TESTPREAMBLE()
def sobol(indices, a):
value = 1.0
for i in range(indices.getSize()):
value *= 1.0 / (3.0 * (1.0 + a[indices[i]]) ** 2)
return value
# Problem parameters
inputDimension = 3
outputDimension = 2
# Reference analytical values
meanTh_Sobol = 1.0
covTh_Sobol = 1.0
kappa = ot.Point(inputDimension)
a = 7.0
b = 0.1
# Create the gSobol function
inputVariables = ot.Description(inputDimension)
outputVariables = ot.Description(outputDimension)
formula = ot.Description(outputDimension)
formula[0] = "1.0"
for i in range(inputDimension):
kappa[i] = 0.5 * i
covTh_Sobol *= 1.0 + 1.0 / (3.0 * (1.0 + kappa[i]) ** 2)
inputVariables[i] = "xi" + str(i)
formula[0] = (
formula[0]
+ " * ((abs(4.0 * xi"
+ str(i)
+ " - 2.0) + "
+ str(kappa[i])
+ ") / (1.0 + "
+ str(kappa[i])
+ "))"
)
formula[1] = (
"sin("
+ str(-m.pi)
+ " + 2 * "
+ str(m.pi)
+ " * xi0) + ("
+ str(a)
+ ") * (sin("
+ str(-m.pi)
+ " + 2 * "
+ str(m.pi)
+ " * xi1)) ^ 2 + ("
+ str(b)
+ ") * ("
+ str(-m.pi)
+ " + 2 * "
+ str(m.pi)
+ " * xi2)^4 * sin("
+ str(-m.pi)
+ " + 2 * "
+ str(m.pi)
+ " * xi0)"
)
covTh_Sobol -= 1
# Reference analytical values
meanTh_Ishigami = a / 2.0
covTh_Ishigami = b**2 * m.pi**8 / 18.0 + (b * m.pi**4) / 5.0 + a**2 / 8.0 + 1.0 / 2.0
sob_1_Ishigami = ot.Point(3)
sob_1_Ishigami[0] = (
b * m.pi**4 / 5.0 + b**2 * m.pi**8 / 50.0 + 1.0 / 2.0
) / covTh_Ishigami
sob_1_Ishigami[1] = (a**2 / 8.0) / covTh_Ishigami
sob_1_Ishigami[2] = 0.0
sob_2_Ishigami = ot.Point(3)
sob_2_Ishigami[0] = 0.0
sob_2_Ishigami[1] = (b**2 * m.pi**8 / 18.0 - b**2 * m.pi**8 / 50.0) / covTh_Ishigami
sob_2_Ishigami[2] = 0.0
sob_3_Ishigami = ot.Point(1, 0.0)
# Multidimensional reference values
# Mean
meanTh = ot.Point(outputDimension)
meanTh[0] = meanTh_Sobol
meanTh[1] = meanTh_Ishigami
# Covariance
covTh = ot.CovarianceMatrix(outputDimension)
covTh[0, 0] = covTh_Sobol
covTh[1, 1] = covTh_Ishigami
# 1rst order Sobol
sob_1 = ot.Point(inputDimension * outputDimension)
indices = ot.Indices(1)
indices[0] = 0
sob_1[0] = sobol(indices, kappa) / covTh_Sobol
indices[0] = 1
sob_1[1] = sobol(indices, kappa) / covTh_Sobol
indices[0] = 2
sob_1[2] = sobol(indices, kappa) / covTh_Sobol
sob_1[3] = sob_1_Ishigami[0]
sob_1[4] = sob_1_Ishigami[1]
sob_1[5] = sob_1_Ishigami[2]
# 2nd order Sobol
sob_2 = ot.Point(inputDimension * outputDimension)
indices = ot.Indices(2)
indices[0] = 0
indices[1] = 1
sob_2[0] = sobol(indices, kappa) / covTh_Sobol
indices[1] = 2
sob_2[1] = sobol(indices, kappa) / covTh_Sobol
indices[0] = 1
indices[1] = 2
sob_2[2] = sobol(indices, kappa) / covTh_Sobol
sob_2[3] = sob_2_Ishigami[0]
sob_2[4] = sob_2_Ishigami[1]
sob_2[5] = sob_2_Ishigami[2]
# 3rd order Sobol
sob_3 = ot.Point(outputDimension)
indices = ot.Indices(3)
indices[0] = 0
indices[1] = 1
indices[2] = 2
sob_3[0] = sobol(indices, kappa) / covTh_Sobol
sob_3[1] = sob_3_Ishigami[0]
# 1rst order Total Sobol
sob_T1 = ot.Point(inputDimension * outputDimension)
sob_T1[0] = sob_1[0] + sob_2[0] + sob_2[1] + sob_3[0]
sob_T1[1] = sob_1[1] + sob_2[0] + sob_2[2] + sob_3[0]
sob_T1[2] = sob_1[2] + sob_2[1] + sob_2[2] + sob_3[0]
sob_T1[3] = sob_1[3] + sob_2[3] + sob_2[4] + sob_3[1]
sob_T1[4] = sob_1[4] + sob_2[3] + sob_2[5] + sob_3[1]
sob_T1[5] = sob_1[5] + sob_2[4] + sob_2[5] + sob_3[1]
sob_T2 = ot.Point(inputDimension * outputDimension)
sob_T2[0] = sob_2[0] + sob_3[0]
sob_T2[1] = sob_2[1] + sob_3[0]
sob_T2[2] = sob_2[2] + sob_3[0]
sob_T2[3] = sob_2[3] + sob_3[1]
sob_T2[4] = sob_2[4] + sob_3[1]
sob_T2[5] = sob_2[5] + sob_3[1]
# 3rd order Total Sobol
sob_T3 = ot.Point(sob_3)
model = ot.SymbolicFunction(inputVariables, formula)
# Create the input distribution
distribution = ot.JointDistribution([ot.Uniform(0.0, 1.0)] * inputDimension)
# Create the orthogonal basis
enumerateFunction = ot.LinearEnumerateFunction(inputDimension)
productBasis = ot.OrthogonalProductPolynomialFactory(
[ot.LegendreFactory()] * inputDimension, enumerateFunction
)
# Create the adaptive strategy
# We can choose amongst several strategies
# First, the most efficient (but more complex!) strategy
listAdaptiveStrategy = list()
degree = 6
indexMax = enumerateFunction.getStrataCumulatedCardinal(degree)
basisDimension = enumerateFunction.getStrataCumulatedCardinal(degree // 2)
threshold = 1.0e-6
listAdaptiveStrategy.append(
ot.CleaningStrategy(productBasis, indexMax, basisDimension, threshold)
)
# Second, the most used (and most basic!) strategy
listAdaptiveStrategy.append(
ot.FixedStrategy(productBasis, enumerateFunction.getStrataCumulatedCardinal(degree))
)
for adaptiveStrategyIndex in range(len(listAdaptiveStrategy)):
adaptiveStrategy = listAdaptiveStrategy[adaptiveStrategyIndex]
# Create the projection strategy
samplingSize = 250
listExperiment = list()
# LHS experiment
listExperiment.append(ot.LHSExperiment(distribution, samplingSize))
for experiment in listExperiment:
ot.RandomGenerator.SetSeed(0)
X = experiment.generate()
Y = model(X)
# Create the polynomial chaos algorithm
maximumResidual = 1.0e-10
algo = ot.FunctionalChaosAlgorithm(X, Y, distribution, adaptiveStrategy)
algo.setMaximumResidual(maximumResidual)
algo.run()
# Examine the results
result = algo.getResult()
print("###################################")
print(algo.getAdaptiveStrategy())
print(algo.getProjectionStrategy())
residuals = result.getResiduals()
print("residuals=", residuals)
relativeErrors = result.getRelativeErrors()
print("relative errors=", relativeErrors)
# Post-process the results
vector = ot.FunctionalChaosRandomVector(result)
sensitivity = ot.FunctionalChaosSobolIndices(result)
for outputIndex in range(outputDimension):
print("output=", outputIndex)
mean = vector.getMean()[outputIndex]
print(
"mean= %.5f" % mean,
"absolute error=%.5e" % abs(mean - meanTh[outputIndex]),
)
variance = vector.getCovariance()[outputIndex, outputIndex]
print(
"variance=%.5f" % variance,
"absolute error=%.5e" % abs(variance - covTh[outputIndex, outputIndex]),
)
indices = ot.Indices(1)
for i in range(inputDimension):
indices[0] = i
value = sensitivity.getSobolIndex(i, outputIndex)
print("value= %.5g" % value)
print(
"Sobol index ",
i,
" =%.5f" % value,
"absolute error=%.5e"
% abs(value - sob_1[i + inputDimension * outputIndex]),
)
indices = ot.Indices(2)
k = 0
for i in range(inputDimension):
indices[0] = i
for j in range(i + 1, inputDimension):
indices[1] = j
value = sensitivity.getSobolIndex(indices, outputIndex)
print(
"Sobol index ",
indices,
" =%.5f" % value,
"absolute error=%.5e"
% abs(value - sob_2[k + inputDimension * outputIndex]),
)
k += 1
indices = ot.Indices([0, 1, 2])
value = sensitivity.getSobolIndex(indices, outputIndex)
print(
"Sobol index ",
indices,
" =%.5f" % value,
"absolute error=%.5e" % abs(value - sob_3[outputIndex]),
)
for i in range(inputDimension):
value = sensitivity.getSobolTotalIndex(i, outputIndex)
print(
"Sobol total index ",
i,
" =%.5f" % value,
"absolute error=%.5e"
% abs(value - sob_T1[i + inputDimension * outputIndex]),
)
indices = ot.Indices(2)
k = 0
for i in range(inputDimension):
indices[0] = i
for j in range(i + 1, inputDimension):
indices[1] = j
value = sensitivity.getSobolTotalIndex(indices, outputIndex)
print(
"Sobol total index ",
indices,
" =%.5f" % value,
"absolute error=%.5e"
% abs(value - sob_T2[k + inputDimension * outputIndex]),
)
k += 1
indices = ot.Indices([0, 1, 2])
value = sensitivity.getSobolTotalIndex(indices, outputIndex)
print(
"Sobol total index ",
indices,
" =%.5f" % value,
"absolute error=%.5e" % abs(value - sob_T3[1]),
)
|