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)
ot.ResourceMap.SetAsUnsignedInteger( "OptimizationAlgorithm-DefaultMaximumIterationNumber", 1000) ot.ResourceMap.SetAsUnsignedInteger( "OptimizationAlgorithm-DefaultMaximumEvaluationNumber", 100000) ot.ResourceMap.SetAsScalar( "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(
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() graph = mesh1.draw3D(True, pi / 16, 0, pi / 4, False, 1.0) mesh2.setVertices(mesh2.getVertices() + ot.Point([-4.0, 0.0, 0.0])) ot.ResourceMap.SetAsScalar("Mesh-AmbientFactor", 0.1) ot.ResourceMap.SetAsScalar("Mesh-DiffuseFactor", 0.6)
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 self.KLResult_ = KLResult self.nc_ = 0 def _exec(self, X): self.nc_ += 1 print("nc=", self.nc_)
ot.ResourceMap.SetAsUnsignedInteger( "OptimizationAlgorithm-DefaultMaximumIteration", 1000) ot.ResourceMap.SetAsUnsignedInteger( "OptimizationAlgorithm-DefaultMaximumEvaluationNumber", 100000) ot.ResourceMap.SetAsScalar( "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