inVars[i] = "x" + str(i) model = ot.SymbolicFunction(inVars, inVars) # The output vector Y = ot.CompositeRandomVector(model, X) # The domain: [0, 1]^dim domain = ot.Interval(dim) # The event event = ot.DomainEvent(Y, domain) print("sample=", event.getSample(10)) # # Case 2: process based event # # The input process X = ot.WhiteNoise(distribution) # The domain: [0, 1]^dim domain = ot.Interval(dim) # The event event = ot.ProcessEvent(X, domain) print("sample=", event.getSample(10)) # 3. from distribution antecedent = ot.RandomVector(ot.Normal(2)) domain = ot.LevelSet(ot.SymbolicFunction(['x', 'y'], ['x^2+y^2']), ot.Less(), 1.0) event = ot.DomainEvent(antecedent, domain) print('sample=', event.getSample(10))
#! /usr/bin/env python import openturns as ot ot.PlatformInfo.SetNumericalPrecision(6) dim = 2 # First domain: [0,2]x[0,2] cube = ot.Interval([0.0] * dim, [2.0] * dim) # Second domain: sphere center=(0,0) r=1 function = ot.SymbolicFunction(["x", "y"], ["x^2 + y^2"]) sphere = ot.LevelSet(function, ot.LessOrEqual(), 1.0) # Inside sphere but not cube p0 = [-0.25, 0.25] # Inside cube and sphere p1 = [0.25, 0.25] # Inside cube but not sphere p2 = [1.8, 1.8] # Outside p3 = [4.0, 4.0] domain = ot.DomainDisjunctiveUnion(cube, sphere) print("cube=", cube) print("sphere=", sphere) print("disjunctive union=", domain) # Accessors print("Dimension=", domain.getDimension()) # Contains
"OptimizationAlgorithm-DefaultMaximumAbsoluteError", 1.0e-7) ot.ResourceMap.SetAsScalar( "OptimizationAlgorithm-DefaultMaximumRelativeError", 1.0e-7) ot.ResourceMap.SetAsScalar( "OptimizationAlgorithm-DefaultMaximumResidualError", 1.0e-7) ot.ResourceMap.SetAsScalar( "OptimizationAlgorithm-DefaultMaximumConstraintError", 1.0e-7) ot.PlatformInfo.SetNumericalPrecision(2) # The 1D mesher mesher1D = ot.LevelSetMesher([7]) print("mesher1D=", mesher1D) level = 0.5 function1D = ot.SymbolicFunction("x", "cos(x)/(1+0.1*x^2)") levelSet1D = ot.LevelSet(function1D, ot.LessOrEqual(), level) # Manual bounding box mesh1D = mesher1D.build(levelSet1D, ot.Interval(-10.0, 10.0)) print("mesh1D=", mesh1D) # The 2D mesher mesher2D = ot.LevelSetMesher([5] * 2) print("mesher2D=", mesher2D) function2D = ot.SymbolicFunction( ["x0", "x1"], ["cos(x0 * x1)/(1 + 0.1 * (x0^2 + x1^2))"]) levelSet2D = ot.LevelSet(function2D, ot.LessOrEqual(), level) # Manual bounding box mesh2D = mesher2D.build(levelSet2D, ot.Interval([-10.0] * 2, [10.0] * 2))
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View # Create the mesher mesher = ot.LevelSetMesher([50] * 2) # Create a level set function = ot.SymbolicFunction(['x0', 'x1'], ['10*(x0^3+x1)^2+x0^2']) level = 0.5 set = ot.LevelSet(function, level) # Mesh the level set mesh = mesher.build(set, ot.Interval([-1.0] * 2, [1.0] * 2)) # Draw the first mesh graph = mesh.draw() graph.setXTitle('$x_0$') graph.setYTitle('$x_1$') fig = plt.figure(figsize=(10, 4)) plt.suptitle('Mesh of a level set') graph_axis = fig.add_subplot(111) graph_axis.set_xlim(auto=True) View(graph, figure=fig, axes=[graph_axis], add_legend=True)
import openturns as ot from openturns.viewer import View analytical = ot.SymbolicFunction(['x'], ['2*x-8']) levelset = ot.LevelSet(analytical, ot.Less(), 0.0) f = ot.IndicatorFunction(levelset) graph = f.draw(0.0, 10.0) graph.setTitle('$y=\mathbb{1}_{2x-8<0}$') View(graph, figure_kw={'figsize': (8, 4)}, add_legend=True)
from __future__ import print_function import openturns as ot from openturns.viewer import View from time import time A = 4.0 N = 100 threshold = 1.0e-3 # Create a domain f = ot.SymbolicFunction( ["x0", "x1"], ["1.0 - (1.2 + cos(_pi * x0))^2 - (1.2 + cos(_pi * x1))^2"]) level = 0.0 domain = ot.LevelSet(f, level) interval = ot.Interval([-A] * 2, [A] * 2) domain.setLowerBound(interval.getLowerBound()) domain.setUpperBound(interval.getUpperBound()) # Create the mesh # To have a more symmetric mesh ot.ResourceMap.SetAsBool("IntervalMesher-UseDiamond", True) discretization = [N] * 2 mesh = ot.LevelSetMesher(discretization).build(domain, interval) print("vertices=", mesh.getVerticesNumber()) # Second, a model with functional output class FUNC(ot.OpenTURNSPythonFunction): def __init__(self, mesh, KLResult): super(FUNC, self).__init__(KLResult.getEigenValues().getSize(), mesh.getVerticesNumber()) self.mesh_ = mesh
#! /usr/bin/env python from __future__ import print_function import openturns as ot ot.PlatformInfo.SetNumericalPrecision(6) dim = 2 # First domain: [0,2]x[0,2] cube = ot.Interval([0.0] * dim, [2.0] * dim) # Second domain: sphere center=(0,0) r=1 function = ot.SymbolicFunction(["x", "y"], ["x^2 + y^2"]) sphere = ot.LevelSet(function, 1.0) # Inside sphere but not cube p0 = [-0.25, 0.25] # Inside cube and sphere p1 = [0.25, 0.25] # Inside cube but not sphere p2 = [1.8, 1.8] # Outside p3 = [4.0, 4.0] domain = ot.DomainIntersection(cube, sphere) print("cube=", cube) print("sphere=", sphere) print("intersection=", domain) # Accessors print("Dimension=", domain.getDimension())
print('probability distribution=', myAlgo.getResult().getProbabilityDistribution()) print('-' * 32) ot.RandomGenerator.SetSeed(0) description = ot.Description() description.add('composite vector/comparison event') dim = 2 distribution = ot.Normal(dim) Xvector = ot.RandomVector(distribution) f = ot.SymbolicFunction(['x0', 'x1'], ['x0+x1']) Yvector = ot.CompositeRandomVector(f, Xvector) s = 1.0 event1 = ot.Event(Yvector, ot.Greater(), s) description.add('composite vector/domain event') domain1D = ot.LevelSet(ot.SymbolicFunction(['x0'], ['sin(x0)']), ot.LessOrEqual(), -0.5) event2 = ot.Event(Yvector, domain1D) description.add('composite vector/interval event') interval = ot.Interval(0.5, 1.5) event3 = ot.Event(Yvector, interval) description.add('process/domain event') Xprocess = ot.WhiteNoise(distribution, ot.RegularGrid(0.0, 0.1, 10)) domain2D = ot.LevelSet(ot.SymbolicFunction(['x0', 'x1'], ['(x0-1)^2+x1^2']), ot.LessOrEqual(), 1.0) event4 = ot.Event(Xprocess, domain2D) all_events = [event1, event2, event3, event4] for i, event in enumerate(all_events): print(description[i]) if event.isComposite(): experiment = ot.MonteCarloExperiment() myAlgo = ot.ProbabilitySimulationAlgorithm(event, experiment)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View # Create the mesher mesher = ot.LevelSetMesher([50] * 2) # Create a level set function = ot.SymbolicFunction(['x0', 'x1'], ['10*(x0^3+x1)^2+x0^2']) level = 0.5 set = ot.LevelSet(function, ot.LessOrEqual(), level) # Mesh the level set mesh = mesher.build(set, ot.Interval([-1.0] * 2, [1.0] * 2)) # Draw the first mesh graph = mesh.draw() graph.setXTitle('$x_0$') graph.setYTitle('$x_1$') graph.setTitle('Mesh of a level set') fig = plt.figure(figsize=(10, 4)) graph_axis = fig.add_subplot(111) graph_axis.set_xlim(auto=True) View(graph, figure=fig, axes=[graph_axis], add_legend=True)
formula += "(x" + str(i) + "-(" + str(c[i]) + "))^" + str(2 * (p + j)) return formula points = [[1.0, 0.0, -1.0 / sqrt(2.0)], [-1.0, 0.0, -1.0 / sqrt(2.0)], [0.0, 1.0, 1.0 / sqrt(2.0)], [0.0, -1.0, 1.0 / sqrt(2.0)]] formula = "" for i in range(len(points)): if (i > 0): formula += " + " formula += "1.0/(" + buildFormula(points[i], i) + ")" print(formula) f = ot.SymbolicFunction(["x0", "x1", "x2"], ["-(" + formula + ")"]) ls = ot.LevelSet(f, level) print("build mesh") t0 = time() mesh1 = ot.LevelSetMesher([N] * 3).build(ls, ot.Interval([-2.5] * 3, [2.5] * 3), False, True) print("t (mesh1)=", time() - t0, "s") mesh2 = ot.LevelSetMesher([3 * N] * 3).build( ls, ot.Interval([-2.5] * 3, [2.5] * 3), True, True) print("t (mesh1+mesh2)=", time() - t0, "s") print("mesh1=", mesh1.getVerticesNumber(), "vertices and", mesh1.getSimplicesNumber(), "simplices") print("mesh2=", mesh2.getVerticesNumber(), "vertices and", mesh2.getSimplicesNumber(), "simplices") print("draw mesh") t0 = time()
"OptimizationAlgorithm-DefaultMaximumAbsoluteError", 1.0e-7) ot.ResourceMap.SetAsScalar( "OptimizationAlgorithm-DefaultMaximumRelativeError", 1.0e-7) ot.ResourceMap.SetAsScalar( "OptimizationAlgorithm-DefaultMaximumResidualError", 1.0e-7) ot.ResourceMap.SetAsScalar( "OptimizationAlgorithm-DefaultMaximumConstraintError", 1.0e-7) ot.PlatformInfo.SetNumericalPrecision(2) # The 1D mesher mesher1D = ot.LevelSetMesher([7]) print("mesher1D=", mesher1D) level = 0.5 function1D = ot.SymbolicFunction("x", "cos(x)/(1+0.1*x^2)") levelSet1D = ot.LevelSet(function1D, level) # Automatic bounding box mesh1D = mesher1D.build(levelSet1D) print("mesh1D=", mesh1D) # Manual bounding box mesh1D = mesher1D.build(levelSet1D, ot.Interval(-10.0, 10.0)) print("mesh1D=", mesh1D) # The 2D mesher mesher2D = ot.LevelSetMesher([5] * 2) print("mesher2D=", mesher2D) function2D = ot.SymbolicFunction( ["x0", "x1"], ["cos(x0 * x1)/(1 + 0.1 * (x0^2 + x1^2))"])
"OptimizationAlgorithm-DefaultMaximumAbsoluteError", 1.0e-7) ot.ResourceMap.SetAsScalar( "OptimizationAlgorithm-DefaultMaximumRelativeError", 1.0e-7) ot.ResourceMap.SetAsScalar( "OptimizationAlgorithm-DefaultMaximumResidualError", 1.0e-7) ot.ResourceMap.SetAsScalar( "OptimizationAlgorithm-DefaultMaximumConstraintError", 1.0e-7) ot.PlatformInfo.SetNumericalPrecision(2) # The 1D mesher mesher1D = ot.LevelSetMesher([7]) print("mesher1D=", mesher1D) level = 0.5 function1D = ot.SymbolicFunction("x", "cos(x)/(1+0.1*x^2)") levelSet1D = ot.LevelSet(function1D, ot.LessOrEqual(), level) # Manual bounding box mesh1D = mesher1D.build(levelSet1D, ot.Interval(-10.0, 10.0)) print("mesh1D=", mesh1D) # The 2D mesher mesher2D = ot.LevelSetMesher([5] * 2) print("mesher2D=", mesher2D) function2D = ot.SymbolicFunction( ["x0", "x1"], ["cos(x0 * x1)/(1 + 0.1 * (x0^2 + x1^2))"]) levelSet2D = ot.LevelSet(function2D, ot.LessOrEqual(), level) # Manual bounding box mesh2D = mesher2D.build(levelSet2D, ot.Interval([-10.0] * 2, [10.0] * 2))
def __init__(self, inputDOE, outputDOE, physicalModel=None, nMorePoints=0, detection=None, noiseThres=None, saturationThres=None): # initialize the POD class boxCox = False super(AdaptiveHitMissPOD, self).__init__(inputDOE, outputDOE, detection, noiseThres, saturationThres, boxCox) # inherited attributes # self._simulationSize # self._detection # self._inputSample # self._outputSample # self._noiseThres # self._saturationThres # self._lambdaBoxCox # self._boxCox # self._size # self._dim # self._censored self._distribution = None self._classifierType = 'rf' # random forest classifier or svc self._ClassifierParameters = [[100], [None], [2], [0]] self._classifierModel = None self._confMat = None self._pmax = 0.52 self._pmin = 0.45 self._initialStartSize = 1000 self._samplingSize = 10000 # Number of MC simulations to compute POD self._candidateSize = 5000 self._nMorePoints = nMorePoints self._verbose = True self._graph = False # flag to print or not the POD curves at each iteration self._probabilityLevel = None # default graph option self._confidenceLevel = None # default graph option self._graphDirectory = None # graph directory for saving self._normalDist = ot.Normal() if self._censored: logging.info('Censored data are not taken into account : the ' + \ 'kriging model is only built on filtered data.') # Run the preliminary run of the POD class result = self._run(self._inputSample, self._outputSample, self._detection, self._noiseThres, self._saturationThres, self._boxCox, self._censored) # get some results self._input = result['inputSample'] self._signals = result['signals'] self._detectionBoxCox = result['detectionBoxCox'] self._boxCoxTransform = result['boxCoxTransform'] self._shift = result['shift'] # define the defect sizes for the interpolation function if not defined self._defectNumber = 20 self._defectSizes = np.linspace(self._input[:, 0].getMin()[0], self._input[:, 0].getMax()[0], self._defectNumber) if detection is None: # case where the physical model already returns 0 or 1 self._physicalModel = physicalModel else: # case where the physical model returns a true signal value # the physical model is turned into a binary model with respect # to the detection value. if parse_version(ot.__version__) < parse_version("1.18"): self._physicalModel = ot.IndicatorFunction( physicalModel, ot.Greater(), self._detection) else: self._physicalModel = ot.IndicatorFunction( ot.LevelSet(physicalModel, ot.Greater(), self._detection)) self._signals = np.array(np.array(self._signals) > self._detection, dtype='int')