def test_FuzzyAnalysis(self): from scipy.optimize import rosen, rosen_der def SysEq2(p, x): return rosen(p) def SensEq2(p, r, g, x): return rosen_der(p) pFuzz1 = pu.UncertainNumber([1, 2, 3, 4], Form='trapazoid', nalpha=3) pFuzz2 = pu.UncertainNumber([1, 2, 3, 4], Form='trapazoid', nalpha=3) pFuzz = [pFuzz1, pFuzz2] Prob = pu.UncertainAnalysis(SysEq2, pUnc=pFuzz, SensEq=SensEq2) Prob.Alg = 'NLPQLP' Prob.nAlpha = 3 Prob.paraNorm = 0 Prob.epsStop = 1e-6 Prob.para = 1 Prob.calculate() rUncTarget = np.array([ [1.01000000e02, 4.90400000e03], [2.50000000e-01, 1.15625000e04], [0, 2.25090000e04], ]) # self.assertAlmostEqual(Prob.rUnc.Value.tolist(), # rUncTarget.tolist()) self.assertAlmostEqual(Prob.rUnc.Value.tolist()[0], rUncTarget.tolist()[0]) self.assertAlmostEqual(Prob.rUnc.Value.tolist()[1], rUncTarget.tolist()[1]) self.assertAlmostEqual(Prob.rUnc.Value.tolist()[2][0], rUncTarget.tolist()[2][0]) self.assertAlmostEqual(Prob.rUnc.Value.tolist()[2][1], rUncTarget.tolist()[2][1])
def test_FuzzyNumber(self): nAlpha = 3 pFuzz = pu.UncertainNumber([1, 2, 3, 4], Form='trapazoid', nalpha=nAlpha) pFuzzTarget = np.array([[2.0, 3.0], [1.5, 3.5], [1.0, 4.0]]) self.assertAlmostEqual(pFuzz.Value.tolist(), pFuzzTarget.tolist())
def test_IntervalAnalysis(self): pInt = pu.UncertainNumber([1, 5]) def SysEq1(p, x): return p - p UncertainProblem = pu.UncertainAnalysis() UncertainProblem.pUnc = pInt UncertainProblem.SysEq = SysEq1 UncertainProblem.calculate() rUncTarget = np.array([[0.0, 0.0]]) self.assertAlmostEqual(UncertainProblem.rUnc.Value.tolist(), rUncTarget.tolist())
x2 = 1.7639 ux1 = 0.001 ux2 = 0.001 # Function of estimated damping ratio based on two measures def DampEst(p, x): x1 = p[0] x2 = p[1] Lambda = np.log(x1 / x2) zeta = np.sqrt(Lambda**2 / (4 * np.pi**2 + Lambda**2)) return zeta # Interval uncertainty problem: Worst-case interval defined as +/-6sigma x1Unc = pu.UncertainNumber([x1 - 6 * ux1, x1 + 6 * ux1]) x2Unc = pu.UncertainNumber([x2 - 6 * ux2, x2 + 6 * ux2]) Prob = pu.UncertainAnalysis(DampEst, pUnc=[x1Unc, x2Unc]) Prob.nr = 1 Prob.deltax = 1e-6 Prob.epsStop = 1e-6 Prob.Alg = 'NLPQLP' # Calculate uncertain response Prob.calculate() # Calculation with standard uncertainty using first-order Taylor series zeta = DampEst([x1, x2], []) dzetadx1 = 1 / (2 * np.pi * x1) dzetadx2 = 1 / (2 * np.pi * x2) uzeta = (np.sqrt(ux1**2 * dzetadx1**2 + ux2**2 * dzetadx2**2) / 2 / np.pi)
def test_Robustness(self): UncertainProblem = pu.UncertainAnalysis() UncertainProblem.pUnc = pu.UncertainNumber([1, 2]) UncertainProblem.rUnc = pu.UncertainNumber([3, 6]) UncertainProblem.calcRobustness() self.assertAlmostEqual(UncertainProblem.SystemRobustness, 1 / 3)
import uncertainties PyUncertainties = True except: PyUncertainties = False print("Package 'uncertainties' not installed!") def SysEq(p, x): return p - p nAlpha = 1 pL = 1.0 pU = 5.0 pInt = pu.UncertainNumber([1, 5]) Prob = pu.UncertainAnalysis() Prob.SysEq = SysEq Prob.pUnc = pInt Prob.calculate() if IntPy: pInt1 = intpy.IReal(pL, pU) rInt1 = pInt1 - pInt1 if PyInterval: pInt2 = interval.interval(pL, pU) rInt2 = pInt2 - pInt2 if PyUncertainties: pMean = (pU - pL) / 2.0 pDelta = pMean - pL pUnc = uncertainties.ufloat(pMean, pDelta)
# -*- coding: utf-8 -*- import pyUngewiss as pu import numpy as np def Eigenfrequency1DoF(p, x): m = p[0] k = p[1] omega0 = np.sqrt(k / m) f0 = omega0 / 2 / np.pi return f0 m = pu.UncertainNumber([2.0, 2.5]) k = pu.UncertainNumber([40000, 60000]) pUnc = [m, k] Prob = pu.UncertainAnalysis(Eigenfrequency1DoF, pUnc) Prob.deltax = 1e-3 Prob.epsStop = 1e-3 Prob.calculate() m.printValue() k.printValue() plt, _ = pu.plotIntervals([m.Value, k.Value], labels=['mass $m$ [kg]', 'stiffness $k$ [N/mm]']) plt.show() f0Unc = Prob.rUnc f0Unc.printValue()
epsilon = np.linspace(0, epsilonMax, nS) rFnUnc = [[]] * nS nEvaluation = 0 for i, val in enumerate(epsilon): Prob.para = val Prob.calculate() rFnUnc[i] = Prob.rUnc nEvaluation += Prob.nEval Prob.rFnUnc = rFnUnc Prob.nEval = nEvaluation Prob.epsilon = epsilon return Prob # Interval parameters sigmaYInt = pu.UncertainNumber([240, 260]) sigmaPInt = pu.UncertainNumber([40, 60]) cHSInt = pu.UncertainNumber([8, 12]) nHSInt = pu.UncertainNumber([0.7, 0.8]) pInt = [sigmaYInt, sigmaPInt, cHSInt, nHSInt] # Fuzzy parameters nAlpha = 6 sigmaYFuzz = pu.UncertainNumber( [230, 240, 260, 270], Form='trapazoid', nalpha=nAlpha ) sigmaPFuzz = pu.UncertainNumber( [35, 40, 60, 65], Form='trapazoid', nalpha=nAlpha ) cHSFuzz = pu.UncertainNumber([7, 8, 12, 13], Form='trapazoid', nalpha=nAlpha) nHSFuzz = pu.UncertainNumber(
import numpy as np import pyUngewiss as pu def HockettSherby(p, x): sigma = p[0] + p[1] - p[1] * np.exp(-p[2] * x**p[3]) return [sigma] sigmaY = pu.UncertainNumber([240, 260]) sigmaP = pu.UncertainNumber([40, 60]) cHS = pu.UncertainNumber([8, 12]) nHS = pu.UncertainNumber([0.7, 0.8]) pUnc = [sigmaY, sigmaP, cHS, nHS] Prob = pu.UncertainAnalysis(HockettSherby, pUnc) Prob.deltax = 1e-3 Prob.epsStop = 1e-3 nS = 100 epsilonMax = 0.5 epsilon = np.linspace(0, epsilonMax, nS) rFnInt = [[]] * nS nEvaluation = 0 for i, val in enumerate(epsilon): Prob.para = val Prob.calculate() rFnInt[i] = Prob.rUnc nEvaluation += Prob.nEval pu.plotUncertainFn( rFnInt,
import pyUngewiss as pu import numpy as np def SysEq(p, x): return (x + p)**2 def SensEq(p, r, g, x): return 2 * x + 2 * p nAlpha = 11 pFuzz = pu.UncertainNumber([8, 9, 11, 12], Form='trapazoid', nalpha=nAlpha) x = 1.0 ProbFD = pu.UncertainAnalysis(SysEq, pUnc=pFuzz) ProbFD.Alg = 'NLPQLP' ProbFD.nAlpha = nAlpha ProbFD.deltax = (1e-6, ) ProbFD.paraNorm = 0 ProbFD.SBFA = False ProbFD.Surr = (False, ) ProbFD.epsStop = 1e-6 ProbFD.para = x ProbFD.SensCalc = 'FD' ProbFD.calculate() ProbAS = pu.UncertainAnalysis(SysEq, pUnc=pFuzz, SensEq=SensEq) ProbAS.Alg = 'NLPQLP' ProbAS.nAlpha = nAlpha ProbAS.paraNorm = 0 ProbAS.SBFA = False
[350, 850], [400, 800], [450, 750], [500, 700], [550, 650], [599, 601], [300, 900], [350, 850], [400, 800], [450, 750], [500, 700], [550, 650], ]) AIntArray = [[]] * len(data) for i, val in enumerate(data): AIntArray[i] = pu.UncertainNumber((val)) labels = [] for ii in range(len(data)): labels.append('Stiffness ' + str(ii + 1)) ax = pu.plotIntervals(AIntArray, labels=labels, Units='N/mm') data = np.array([ [300, 900], [400, 800], [450, 750], [500, 700], [550, 650], [599, 601], [300, 900], ]) labels = [] for ii in range(len(data)):
import numpy as np import pyUngewiss as pu def SysEq(p, x): y = x**2 + p return y pInt = pu.UncertainNumber([-10, 10]) nS = 200 x = np.linspace(-10, 10, nS) rFnInt = [[]] * nS Prob = pu.UncertainAnalysis(SysEq, pInt) Prob.deltax = 1e-6 Prob.epsStop = 1e-6 for ii in range(len(x)): Prob.para = x[ii] Prob.calculate() rFnInt[ii] = Prob.rUnc plt, _ = pu.plotUncertainFn(rFnInt, x) plt.show()