def __init__(self, chaosPOD, dim, defectSizes, detection, simulationSize): super(PODaggrChaos, self).__init__(dim, defectSizes.shape[0]) self.chaosPOD = chaosPOD self.dim = dim self.defectSizes = defectSizes self.defectNumber = len(defectSizes) self.simulationSize = simulationSize self.detection = detection # get the sample of coefficient using the coef distribution # used to compute the POD for a given point sampleCoefs = chaosPOD.getCoefficientDistribution().getSample( simulationSize) # get some result from the polynomial chaos to build a vectoriel # chaos function that return the signal values for all chaos with # different coefficients for one specific point chaosResult = chaosPOD.getPolynomialChaosResult() reducedBasis = chaosResult.getReducedBasis() transformation = chaosResult.getTransformation() chaosFunctionCol = [] for i, coefs in enumerate(sampleCoefs): standardChaosFunction = ot.LinearCombinationFunction( reducedBasis, coefs) chaosFunctionCol.append( ot.ComposedFunction(standardChaosFunction, transformation)) self.chaosFunction = ot.AggregatedFunction(chaosFunctionCol)
# # .. math:: # f = (f_1, \dots, f_n) # # %% from __future__ import print_function import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt import math as m ot.Log.Show(ot.Log.NONE) # %% # assume a list of functions to aggregate functions = list() functions.append(ot.SymbolicFunction(['x1', 'x2', 'x3'], ['x1^2 + x2', 'x1 + x2 + x3'])) functions.append(ot.SymbolicFunction(['x1', 'x2', 'x3'], ['x1 + 2 * x2 + x3', 'x1 + x2 - x3'])) # %% # create the aggregated function function = ot.AggregatedFunction(functions) # %% # evaluate the function x = [1.0, 2.0, 3.0] y = function(x) print('x=', x, 'y=', y)
lin31 = func3.isLinearlyDependent(1) assert (not lin3) assert (not lin30) assert (lin31) # 4. 2D non linear function func4 = ot.SymbolicFunction(['x1', 'x2'], ['3*x1*x2']) lin4 = func4.isLinear() lin40 = func4.isLinearlyDependent(0) lin41 = func4.isLinearlyDependent(1) assert (not lin4) assert (lin40) assert (lin41) # 5. Aggregated function func5 = ot.AggregatedFunction([func2, func4]) lin5 = func5.isLinear() lin50 = func5.isLinearlyDependent(0) lin51 = func5.isLinearlyDependent(1) assert (not lin5) assert (lin50) assert (lin51) # 6. Python function def pyFunc6(x): x1, x2 = x result = [3 * x1 - 2 * x2 + 5, -x1 + x2, 3 * x1 * x2] return result
import openturns as ot ot.TESTPREAMBLE() dim = 2 x = [2.0 + i for i in range(dim)] print("x=", x) factory = ot.ConstantBasisFactory(dim) print("factory=", factory) basis = factory.build() print("basis=", basis) f = ot.AggregatedFunction(basis) y = f(x) print("y=", y) factory = ot.LinearBasisFactory(dim) print("factory=", factory) basis = factory.build() print("basis=", basis) f = ot.AggregatedFunction(basis) y = f(x) print("y=", y) factory = ot.QuadraticBasisFactory(dim) print("factory=", factory) basis = factory.build()
def run(self): """ Build the POD models. Notes ----- This method build the polynomial chaos model. First the censored data are filtered if needed. The Box Cox transformation is performed if it is enabled. Then it builds the POD models, the Monte Carlo simulation is performed for each given defect sizes. The confidence interval is computed by simulating new coefficients of the polynomial chaos, then Monte Carlo simulations are performed. """ # run the chaos algorithm and get result if not given if not self._userChaos: if self._verbose: print('Start build polynomial chaos model...') self._algoChaos = self._buildChaosAlgo(self._input, self._signals) self._algoChaos.run() if self._verbose: print('Polynomial chaos model completed') self._chaosResult = self._algoChaos.getResult() # get the metamodel self._chaosPred = self._chaosResult.getMetaModel() # get the basis, coef and transformation, needed for the confidence interval self._chaosCoefs = self._chaosResult.getCoefficients() self._reducedBasis = self._chaosResult.getReducedBasis() self._transformation = self._chaosResult.getTransformation() self._basisFunction = ot.ComposedFunction(ot.AggregatedFunction( self._reducedBasis), self._transformation) # compute the residuals and stderr inputSize = self._input.getSize() basisSize = self._reducedBasis.getSize() self._residuals = self._signals - self._chaosPred(self._input) # residuals self._stderr = np.sqrt(np.sum(np.array(self._residuals)**2) / (inputSize - basisSize - 1)) # Check the quality of the chaos model R2 = self.getR2() Q2 = self.getQ2() if self._verbose: print('Polynomial chaos validation R2 (>0.8) : {:0.4f}'.format(R2)) print('Polynomial chaos validation Q2 (>0.8) : {:0.4f}'.format(Q2)) # Compute the POD values for each defect sizes self.POD = self._computePOD(self._defectSizes, self._chaosCoefs) # create the interpolate function interpModel = interp1d(self._defectSizes, self.POD, kind='linear') self._PODmodel = ot.PythonFunction(1, 1, interpModel) ####################### confidence interval ############################ dof = inputSize - basisSize - 1 varEpsilon = (ot.ChiSquare(dof).inverse() * dof * self._stderr**2).getRealization()[0] gramBasis = ot.Matrix(self._basisFunction(self._input)).computeGram() covMatrix = gramBasis.solveLinearSystem(ot.IdentityMatrix(basisSize)) * varEpsilon self._coefsDist = ot.Normal(np.hstack(self._chaosCoefs), ot.CovarianceMatrix(covMatrix.getImplementation())) coefsRandom = self._coefsDist.getSample(self._simulationSize) self._PODPerDefect = ot.Sample(self._simulationSize, self._defectNumber) for i, coefs in enumerate(coefsRandom): self._PODPerDefect[i, :] = self._computePOD(self._defectSizes, coefs) if self._verbose: updateProgress(i, self._simulationSize, 'Computing POD per defect')