.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_reliability/reliability_analysis/plot_four_branch_function.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_reliability_reliability_analysis_plot_four_branch_function.py: Four Branch function visualization and reliability analysis ============================================================ This example demonstrates the Four Branch function (Schueremans & van Gemert, 2005), visualizes its limit state, and performs basic reliability analysis. .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python import openturns as ot import openturns.viewer as otv from openturns.usecases import four_branch_function import math .. GENERATED FROM PYTHON SOURCE LINES 15-16 Load the standard benchmark model (p=6, standard normal inputs) .. GENERATED FROM PYTHON SOURCE LINES 16-22 .. code-block:: Python fb = four_branch_function.FourBranchModel() print("Standard model:") print(f" p = {fb.p}") print(f" Distribution: {fb.distribution}") print(f" Model: {fb.model}") .. rst-class:: sphx-glr-script-out .. code-block:: none Standard model: p = 6.0 Distribution: JointDistribution(Normal(mu = 0, sigma = 1), Normal(mu = 0, sigma = 1), IndependentCopula(dimension = 2)) Model: class=PythonEvaluation name=OpenTURNSPythonFunction .. GENERATED FROM PYTHON SOURCE LINES 23-24 Visualize the function surface .. GENERATED FROM PYTHON SOURCE LINES 24-31 .. code-block:: Python graph = fb.model.draw([-4.0, -4.0], [4.0, 4.0], [80, 80]) graph.setTitle("Four Branch function (Schueremans & van Gemert, 2005)") graph.setXTitle("X1") graph.setYTitle("X2") graph.setLegendPosition("upper right") view = otv.View(graph, figure_kw={"figsize": (8, 6)}) .. image-sg:: /auto_reliability/reliability_analysis/images/sphx_glr_plot_four_branch_function_001.svg :alt: Four Branch function (Schueremans & van Gemert, 2005) :srcset: /auto_reliability/reliability_analysis/images/sphx_glr_plot_four_branch_function_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 32-33 Contour plot with limit state and input PDF .. GENERATED FROM PYTHON SOURCE LINES 33-63 .. code-block:: Python graph = fb.model.draw([-4.0, -4.0], [4.0, 4.0], [128, 128]) dr = graph.getDrawable(0) dr.setLevels([-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0, 3.0]) dr.setLegend("f(x)") dr.setLineWidth(1.5) # Highlight limit state ls_graph = fb.model.draw([-4.0, -4.0], [4.0, 4.0], [200, 200]) ls_dr = ls_graph.getDrawable(0) ls_dr.setLevels([0.0]) ls_dr.setLegend("Limit state f(x) = 0") ls_dr.setLineWidth(3) ls_dr.setColor("red") # Input PDF pdf_graph = fb.distribution.drawPDF([-4.0, -4.0], [4.0, 4.0], [80, 80]) pdf_dr = pdf_graph.getDrawable(0) pdf_dr.setAlpha(0.3) pdf_dr.setLegend("Input PDF (Normal)") pdf_dr.setLegend("Input PDF (Normal)") pdf_dr.setLineStyle("dotted") graph.add(ls_dr) graph.add(pdf_dr) graph.setTitle("Four Branch: contours, limit state (red), input PDF") graph.setXTitle("X1") graph.setYTitle("X2") graph.setLegendPosition("upper right") view = otv.View(graph, figure_kw={"figsize": (8, 6)}) .. image-sg:: /auto_reliability/reliability_analysis/images/sphx_glr_plot_four_branch_function_002.svg :alt: Four Branch: contours, limit state (red), input PDF :srcset: /auto_reliability/reliability_analysis/images/sphx_glr_plot_four_branch_function_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 64-65 Individual branches visualization .. GENERATED FROM PYTHON SOURCE LINES 65-104 .. code-block:: Python p = fb.p sqrt2 = math.sqrt(2.0) g1 = ot.SymbolicFunction(["x1", "x2"], [f"3 + 0.1*(x1 - x2)^2 - (x1 + x2)/{sqrt2:.6f}"]) g2 = ot.SymbolicFunction(["x1", "x2"], [f"3 + 0.1*(x1 - x2)^2 + (x1 + x2)/{sqrt2:.6f}"]) g3 = ot.SymbolicFunction(["x1", "x2"], [f"(x1 - x2) + {p}/{sqrt2:.6f}"]) g4 = ot.SymbolicFunction(["x1", "x2"], [f"(x2 - x1) + {p}/{sqrt2:.6f}"]) graph = ot.Graph("Individual branches (zero contours)", "X1", "X2") graph.setAxes(True) graph.setGrid(True) branches = [ (g1, "g1 (quadratic)", "blue"), (g2, "g2 (quadratic)", "green"), (g3, "g3 (linear)", "orange"), (g4, "g4 (linear)", "purple"), ] for func, name, color in branches: dr = func.draw([-4.0, -4.0], [4.0, 4.0], [100, 100]).getDrawable(0) dr.setLevels([0.0]) dr.setLegend(name) dr.setColor(color) dr.setLineWidth(2) graph.add(dr) # Overall limit state dr_all = fb.model.draw([-4.0, -4.0], [4.0, 4.0], [200, 200]).getDrawable(0) dr_all.setLevels([0.0]) dr_all.setLegend("Overall min(g1,g2,g3,g4)") dr_all.setColor("red") dr_all.setLineWidth(3) dr_all.setLineStyle("dashed") graph.add(dr_all) graph.setLegendPosition("upper right") view = otv.View(graph, figure_kw={"figsize": (8, 6)}) .. image-sg:: /auto_reliability/reliability_analysis/images/sphx_glr_plot_four_branch_function_003.svg :alt: Individual branches (zero contours) :srcset: /auto_reliability/reliability_analysis/images/sphx_glr_plot_four_branch_function_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 105-106 Failure domain visualization via Monte Carlo sampling .. GENERATED FROM PYTHON SOURCE LINES 106-147 .. code-block:: Python sample = fb.distribution.getSample(50000) values = fb.model(sample) failure_indices = [i for i, v in enumerate(values) if v[0] <= 0] safe_indices = [i for i, v in enumerate(values) if v[0] > 0] failure_sample = sample[failure_indices] safe_sample = sample[safe_indices] pf_mc = len(failure_indices) / 50000 print(f"\nMonte Carlo estimate (N=50000): P_f = {pf_mc:.6f}") graph = ot.Graph(f"Failure domain (Monte Carlo, P_f ≈ {pf_mc:.4f})", "X1", "X2") graph.setAxes(True) graph.setGrid(True) if failure_sample.getSize() > 0: cloud_fail = ot.Cloud(failure_sample) cloud_fail.setColor("red") cloud_fail.setPointStyle("bullet") cloud_fail.setLegend(f"Failure ({failure_sample.getSize()} pts)") graph.add(cloud_fail) cloud_safe = ot.Cloud(safe_sample) cloud_safe.setColor("blue") cloud_safe.setPointStyle("dot") cloud_safe.setLegend(f"Safe ({safe_sample.getSize()} pts)") graph.add(cloud_safe) # Limit state dr_all = fb.model.draw([-4.0, -4.0], [4.0, 4.0], [200, 200]).getDrawable(0) dr_all.setLevels([0.0]) dr_all.setLineWidth(2) dr_all.setColor("black") dr_all.setLineStyle("dashed") dr_all.setLegend("Limit state") graph.add(dr_all) graph.setLegendPosition("upper right") view = otv.View(graph, figure_kw={"figsize": (8, 6)}) .. image-sg:: /auto_reliability/reliability_analysis/images/sphx_glr_plot_four_branch_function_004.svg :alt: Failure domain (Monte Carlo, P_f ≈ 0.0045) :srcset: /auto_reliability/reliability_analysis/images/sphx_glr_plot_four_branch_function_004.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Monte Carlo estimate (N=50000): P_f = 0.004460 .. GENERATED FROM PYTHON SOURCE LINES 148-149 Reliability analysis: Monte Carlo with larger sample .. GENERATED FROM PYTHON SOURCE LINES 149-168 .. code-block:: Python print("\n--- Reliability Analysis ---") # Monte Carlo ot.RandomGenerator.SetSeed(0) event = ot.ThresholdEvent( ot.CompositeRandomVector(fb.model, ot.RandomVector(fb.distribution)), ot.Less(), 0.0 ) mc_algo = ot.ProbabilitySimulationAlgorithm(event, ot.MonteCarloExperiment()) mc_algo.setMaximumCoefficientOfVariation(0.05) mc_algo.setMaximumOuterSampling(int(1e6)) mc_algo.run() mc_result = mc_algo.getResult() print(" Monte Carlo (COV=5%):") print(f" P_f = {mc_result.getProbabilityEstimate():.6e}") print(f" COV = {mc_result.getCoefficientOfVariation():.4f}") print(f" N_calls = {mc_result.getOuterSampling()}") otv.View.ShowAll() .. rst-class:: sphx-glr-script-out .. code-block:: none --- Reliability Analysis --- Monte Carlo (COV=5%): P_f = 4.354184e-03 COV = 0.0500 N_calls = 91636 .. _sphx_glr_download_auto_reliability_reliability_analysis_plot_four_branch_function.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_four_branch_function.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_four_branch_function.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_four_branch_function.zip `