def __init__(self): self.dim = 4 # number of inputs self.outputDimension = 1 # dimension of the output self.tmin = 0.0 # Minimum time self.tmax = 12.0 # Maximum time self.gridsize = 100 # Number of time steps self.mesh = ot.IntervalMesher([self.gridsize - 1]).build( ot.Interval(self.tmin, self.tmax)) self.vertices = self.mesh.getVertices() # Marginals self.distZ0 = ot.Uniform(100.0, 150.0) self.distV0 = ot.Normal(55.0, 10.0) self.distM = ot.Normal(80.0, 8.0) self.distC = ot.Uniform(0.0, 30.0) # Joint distribution self.distribution = ot.ComposedDistribution( [self.distZ0, self.distV0, self.distM, self.distC]) # Exact solution self.alti = ot.PythonPointToFieldFunction(self.dim, self.mesh, self.outputDimension, AltiFunc)
def test_export_fmu(self): # export fmu f = ot.SymbolicFunction(['E', 'F', 'L', 'I'], ['(F*L^3)/(3.0*E*I)']) start = [3e7, 3e4, 250.0, 400.0] if sys.platform.startswith('win'): return temp_path = tempfile.mkdtemp() path_fmu = os.path.join(temp_path, 'Deviation.fmu') fe = otfmi.FunctionExporter(f, start) fe.export_fmu(path_fmu, fmuType='cs', verbose=False) assert os.path.isfile(path_fmu), "fmu not created" # reimport fmu model_fmu = otfmi.FMUFunction(path_fmu, inputs_fmu=["E", "F", "L", "I"], outputs_fmu=["y0"]) print(model_fmu) # call x = [3.1e7, 3.1e4, 255.0, 420.0] y = model_fmu(x) print(y) assert abs(y[0] - 13.1598) < 1e-4, "wrong value" if 0: # field function mesh = ot.RegularGrid(0, 1, 100) #g = ot.SymbolicFunction(['t', 'a', 'b'], ['a*sin(t)+b']) #f = ot.VertexValuePointToFieldFunction(g, mesh) def g(X): a, b = X Y = [[a*m.sin(t)+b] for t in range(100)] return Y f = ot.PythonPointToFieldFunction(2, mesh, 1, g) start = [4.0, 5.0] # export fe = otfmi.FunctionExporter(f, start) fe.export_fmu(path_fmu, fmuType='cs', verbose=True) assert os.path.isfile(path_fmu), "fmu not created" # import import pyfmi model = pyfmi.load_fmu(path_fmu) model.initialize() res = model.simulate(options={'silent_mode': True}) print(model.get_model_variables().keys()) print(res['y0']) shutil.rmtree(temp_path)
import openturns as ot ot.TESTPREAMBLE() mesh = ot.RegularGrid(0.0, 0.1, 11) def f(X): size = 11 Y = [ot.Point(X)*i for i in range(size)] return Y inputDim = 2 outputDim = 2 function = ot.PythonPointToFieldFunction(inputDim, mesh, outputDim, f) function.setInputDescription(['u1', 'u2']) function.setOutputDescription(['v1', 'v2']) # freeze u1=5.0 parametric = ot.ParametricPointToFieldFunction(function, [0], [5.0]) # properties print('dim=', parametric.getInputDimension(), parametric.getOutputDimension()) print('description=', parametric.getInputDescription(), parametric.getOutputDescription()) print('mesh=', parametric.getOutputMesh()) # op(Point) x = [1.0] y = parametric(x)
#! /usr/bin/env python import openturns as ot import math as m import dill # ensures python code is included dill.settings['recurse'] = True ot.TESTPREAMBLE() mesh = ot.RegularGrid(0, 1, 100) def g(X): a, b = X Y = [[a * m.sin(t) + b] for t in range(100)] return Y f = ot.PythonPointToFieldFunction(2, mesh, 1, g) x = [4, 5] print(f(x)) # save study = ot.Study() study.setStorageManager(ot.XMLStorageManager('pyp2ff.xml')) study.add('f', f) study.save()
z0 = X[0] v0 = X[1] m = X[2] c = X[3] zmin = X[4] tau = m / c vinf = - m * g / c t = np.array(vertices) z = z0 + vinf * t + tau * (v0 - vinf) * (1 - np.exp(- t / tau)) z = np.maximum(z, zmin) return [[zeta[0]] for zeta in z] # %% outputDimension = 1 altitudeWithFiveInputs = ot.PythonPointToFieldFunction( 5, mesh, outputDimension, AltiFunc) # %% # Restrict the number of inputs # ----------------------------- # %% # We define a function which has 4 inputs and 5 outputs: the 5th ouput `zmin` is set to zero. # %% projectionFunction = ot.SymbolicFunction( ["z0", "v0", "m", "c"], ["z0", "v0", "m", "c", "0.0"]) # %% # Then we use the `PointToFieldConnection` to create a function which has 4 inputs and returns the output field.
v0 = X[1] m = X[2] c = X[3] zmin = X[4] tau = m / c vinf = -m * g / c t = np.linspace(tmin, tmax, gridsize) z = z0 + vinf * t + tau * (v0 - vinf) * (1 - np.exp(-t / tau)) z = np.maximum(z, zmin) altitude = [[zeta] for zeta in z] return altitude inputDim = 5 outputDim = 1 alti = ot.PythonPointToFieldFunction(inputDim, mesh, outputDim, AltiFunc) # Creation of the input distribution distZ0 = ot.Uniform(100.0, 150.0) distV0 = ot.Normal(55.0, 10.0) distM = ot.Normal(80.0, 8.0) distC = ot.Uniform(0.0, 30.0) distZmin = ot.Dirac([0.0]) distX = ot.ComposedDistribution([distZ0, distV0, distM, distC, distZmin]) # Sample the model samplesize = 1000 inputSample = distX.getSample(samplesize) outputSample = alti(inputSample) # Draw some curves
# Convertit le tableau 2D en tableau 1D t = t.flatten() # Récupère la date initiale t0 = t[0] # Calcule la trajectoire y0, a, b = X y = a * y0 / (b * y0 + (a - b * y0) * exp(-a * (t - t0))) y = y / 1.0e6 # Créée la liste des altitudes à partir du array numpy matrajectoire = [[zeta] for zeta in y] return matrajectoire inputDim = 3 # Nombre de variables en entrée (i.e. taille du vecteur aléatoire) outputDim = 1 # Nombre de champs en sortie (ici, une seule trajectoire) maFonctionChamp = ot.PythonPointToFieldFunction(inputDim, mesh, outputDim, logisticSolution) # Teste une évaluation y0 = 3.9e6 # Population initiale a = 0.03134 b = 1.5887e-10 X = [y0, a, b] unetrajectoire = maFonctionChamp(X) unetrajectoire.setDescription([fieldName]) # print("Une trajectoire:") # print(unetrajectoire) # Creation of the input distribution distY0 = ot.Normal(y0, 0.1 * y0) distA = ot.Normal(a, 0.3 * a) distB = ot.Normal(b, 0.3 * b)
m = X[2] c = X[3] tau = m / c vinf = -m * g / c t = np.array(vertices) z = z0 + vinf * t + tau * (v0 - vinf) * (1 - np.exp(-t / tau)) z = np.maximum(z, 0.) return [[zeta[0]] for zeta in z] # %% # In order to create a `Function` from this Python function, we use the `PythonPointToFieldFunction` class. Since the altitude is the only output field, the third argument `outputDimension` is equal to `1`. If we had computed the speed as an extra output field, we would have set `2` instead. # %% outputDimension = 1 alti = ot.PythonPointToFieldFunction(dimension, mesh, outputDimension, AltiFunc) # %% # Sample trajectories # ------------------- # %% # In order to sample trajectories, we use the `getSample` method of the input distribution and apply the field function. # %% size = 10 inputSample = distribution.getSample(size) outputSample = alti(inputSample) # %% ot.ResourceMap.SetAsUnsignedInteger('Drawable-DefaultPalettePhase', size)