#! /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
from __future__ import print_function
import openturns as ot

ot.TESTPREAMBLE()

# Uncertain parameters
distribution = ot.Normal([1.0] * 3, [2.0] * 3, ot.CorrelationMatrix(3))
distribution.setName("Unnamed")
# Model
f = ot.SymbolicFunction(["x", "y", "z"], ["x-1.5*y+2*z"])
# Sampling
size = 100
inputSample = distribution.getSample(size)
outputSample = f(inputSample)
comparisonOperators = [
    ot.Less(), ot.LessOrEqual(),
    ot.Greater(),
    ot.GreaterOrEqual()
]
threshold = 3.0
ot.ResourceMap.SetAsUnsignedInteger(
    "SimulationSensitivityAnalysis-DefaultSampleMargin", 10)
for i in range(4):
    # Analysis based on an event
    X = ot.RandomVector(distribution)
    Y = ot.CompositeRandomVector(f, X)
    event = ot.ThresholdEvent(Y, comparisonOperators[i], threshold)
    algo = ot.SimulationSensitivityAnalysis(event, inputSample, outputSample)
    print("algo=", algo)
    # Perform the analysis
    print("Mean point in event domain=", algo.computeMeanPointInEventDomain())
# %%
g = ot.MemoizeFunction(g)

# %%
# Create the output random vector :math:`Y = g(X)`:

# %%
Y = ot.CompositeRandomVector(g, X)

# %%
# Create the event :math:`\{ Y = g(X) \leq 0 \}`
# ----------------------------------------------

# %%
myEvent = ot.ThresholdEvent(Y, ot.LessOrEqual(), 0.0)

# %%
# Evaluate the probability with the subset sampling technique
# -----------------------------------------------------------

# %%
algo = ot.SubsetSampling(myEvent)

# %%
# In order to get all the inputs and outputs that realize the event, you have to mention it now:

# %%
algo.setKeepEventSample(True)

# %%
Exemplo n.º 4
0
        "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))
Exemplo n.º 5
0
#! /usr/bin/env python

from __future__ import print_function
import openturns as ot

ot.TESTPREAMBLE()

for op in [
        ot.Less(),
        ot.LessOrEqual(),
        ot.Equal(),
        ot.GreaterOrEqual(),
        ot.Greater()
]:
    print('op=', op, 'op(10,20)=', op(10, 20))
    print('op=', op, 'op(20,20)=', op(20, 20))
    print('op=', op, 'op(30,20)=', op(30, 20))

# getImplementation() crash #1461
print(ot.ComparisonOperator().getImplementation())
Exemplo n.º 6
0
parameters6 = ot.LogNormalMuSigma(40, 8, 0.0)
dist_X6 = ot.ParametrizedDistribution(parameters6)

myDistribution = ot.ComposedDistribution(
    [dist_X1, dist_X2, dist_X3, dist_X4, dist_X5, dist_X6])
myRandomVector = ot.RandomVector(myDistribution)

# Fonction
# ~ myFunction = ot.PythonFunction(2, 1, gfun_22)
myFunction = ot.PythonFunction(6, 1, gfun_8)

myOutputVector = ot.CompositeRandomVector(myFunction, myRandomVector)

# Evènement fiabiliste
event = ot.Event(myOutputVector, ot.LessOrEqual(), 0.0)

# ~ #Run FORM
# ~ FORM_result = run_FORM(event, myRandomVector, verbose=True,
#                          failure_domain=None)

# ~ #Run CMC

CMC_result = run_MonteCarlo(
    event,
    coefVar=0.01,
    outerSampling=3000,
    blockSize=100,
    verbose=True,
    failure_domain=None,
)
Exemplo n.º 7
0
          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)
from __future__ import print_function
import openturns as ot

ot.TESTPREAMBLE()


# Uncertain parameters
distribution = ot.Normal([1.0] * 3, [2.0] * 3, ot.CorrelationMatrix(3))
distribution.setName("Unnamed")
# Model
f = ot.SymbolicFunction(["x", "y", "z"], ["x-1.5*y+2*z"])
# Sampling
size = 100
inputSample = distribution.getSample(size)
outputSample = f(inputSample)
comparisonOperators = [ot.Less(), ot.LessOrEqual(), ot.Greater(), ot.GreaterOrEqual()]
threshold = 3.0
ot.ResourceMap.SetAsUnsignedInteger(
    "SimulationSensitivityAnalysis-DefaultSampleMargin", 10)
for i in range(4):
    # Analysis based on an event
    X = ot.RandomVector(distribution)
    Y = ot.CompositeRandomVector(f, X)
    event = ot.ThresholdEvent(Y, comparisonOperators[i], threshold)
    algo = ot.SimulationSensitivityAnalysis(event, inputSample, outputSample)
    print("algo=", algo)
    # Perform the analysis
    print("Mean point in event domain=",
          algo.computeMeanPointInEventDomain())
    print("Importance factors at threshold ", threshold,
          " =", algo.computeImportanceFactors())
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
          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.ThresholdEvent(Yvector, ot.Greater(), s)
description.add('composite vector/domain event')
domain1D = ot.LevelSet(ot.SymbolicFunction(
    ['x0'], ['sin(x0)']), ot.LessOrEqual(), -0.5)
event2 = ot.DomainEvent(Yvector, domain1D)
description.add('composite vector/interval event')
interval = ot.Interval(0.5, 1.5)
event3 = ot.ThresholdEvent(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.ProcessEvent(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)