def test(): Study_0 = persalys.Study('Study_0') persalys.Study.Add(Study_0) X0 = persalys.Input('X0', 0, '') Y0 = persalys.Output('Y0', '') Y0.setValue(0) inputCollection = [] inputCollection.append(X0) outputCollection = [] outputCollection.append(Y0) code = 'def _exec(X0):\n Y0 = X0\n return Y0' PhysicalModel_0 = persalys.PythonPhysicalModel('PhysicalModel_0', inputCollection, outputCollection, code) Study_0.add(PhysicalModel_0) print(Study_0.getPythonScript())
#!/usr/bin/env python import openturns as ot import openturns.testing import persalys myStudy = persalys.Study('myStudy') # Model R = persalys.Input('R', 0., ot.LogNormalMuSigma( 300., 30.).getDistribution(), 'Yield strength') F = persalys.Input('F', 0., ot.Normal(75000., 5000.), 'Traction load') G = persalys.Output('G', 'deviation') code = 'from math import pi\n\ndef _exec(R, F):\n G = R-F/(pi*100.0)\n return G\n' model = persalys.PythonPhysicalModel('myPhysicalModel', [R, F], [G], code) myStudy.add(model) f = model.getFunction() print(f([300., 75000.])) # several outputs model.setCode( 'from math import pi\n\ndef _exec(R, F):\n G = 6*F\n H = 42.0\n return G, H\n') f = model.getFunction() print(f([300., 75000.])) # test operator() (sample) model.setCode( 'from math import pi\n\ndef _exec(R, F):\n G = 2*R-F/(pi*100.0)\n return G\n') f = model.getFunction()
model.setFormula('Y1', 'sin(X0) + 8*X1 + X2') analysis2 = persalys.TaylorExpansionMomentsAnalysis('aTaylor2', model) analysis2.setInterestVariables(['Y1']) myStudy.add(analysis2) print(analysis2) analysis2.run() print("result=", analysis2.getResult()) print("\n") # Taylor Expansion ## # test definition gradient with PythonPhysicalModel code = 'from math import sin\n\ndef _exec(X0, X1):\n Y0 = sin(X0) + 8*X1\n fake_Y0 = X0\n return Y0, fake_Y0\n' model2 = persalys.PythonPhysicalModel('myPhysicalModel', [X0, X1], [Y0], code) myStudy.add(model2) # default finite difference steps analysis3 = persalys.TaylorExpansionMomentsAnalysis('aTaylor3', model2) analysis3.setInterestVariables(['Y0']) myStudy.add(analysis3) print(analysis3) analysis3.run() print("result=", analysis3.getResult()) print("\n") # greater finite difference steps model2.setFiniteDifferenceStep('X0', 0.005)
y0 = persalys.Output('y0') fake_y0 = persalys.Output('fake_y0') y1 = persalys.Output('y1') # symboilc model ## formula_fake_var = 'x1' formula_y0 = 'cos(0.5*x1) + sin(x2)' formula_y1 = 'cos(0.5*x1) + sin(x2) + x3' symbolicModel = persalys.SymbolicPhysicalModel('symbolicModel', [x1, x2, x3], [fake_var, y0, fake_y0, y1], [ formula_fake_var, formula_y0, formula_y0, formula_y1]) myStudy.add(symbolicModel) # python model ## code = 'from math import cos, sin, sqrt\n\ndef _exec(x1, x2, x3):\n y0 = cos(0.5*x1) + sin(x2) + sqrt(x3)\n return y0\n' pythonModel = persalys.PythonPhysicalModel('pythonModel', [x1, x2, x3], [y0], code) myStudy.add(pythonModel) filename = 'data.csv' cDist = ot.ComposedDistribution([ot.Normal(), ot.Gumbel(), ot.Normal(), ot.Uniform()], ot.ComposedCopula([ot.IndependentCopula(2), ot.GumbelCopula()])) sample = cDist.getSample(200) sample.exportToCSVFile(filename, ' ') # Designs of Experiment ## # fixed design ## ot.RandomGenerator.SetSeed(0) fixedDesign = persalys.FixedDesignOfExperiment('fixedDesign', symbolicModel) inputSample = ot.LHSExperiment(ot.ComposedDistribution([ot.Uniform(0., 10.), ot.Uniform(0., 10.)]), 10).generate() inputSample.stack(ot.Sample(10, [0.5]))
#! /usr/bin/env python import openturns as ot import persalys import os # Model with error X0 = persalys.Input('X0', 1, ot.Normal()) X1 = persalys.Input('X1', 2) Y0 = persalys.Output('Y0') Y1 = persalys.Output('Y1') model2 = persalys.PythonPhysicalModel('model2', [X0, X1], [Y0], 'from math import pi\n\ndef _exec(X0, X1):\n Y0 = 2*X1 - 1/X0\n return Y0\n') model2.setParallel(True) design5 = persalys.FixedDesignOfExperiment('design5', model2) design5.setOriginalInputSample([[0.9, 1.1], [1.8, 2.2], [0, 2.2]]) design5.setBlockSize(3) design5.run() assert design5.getResult().getDesignOfExperiment().getOutputSample().getSize() == 2, "whole block must no fail" model3 = persalys.PythonPhysicalModel('model3', [X0, X1], [Y0, Y1], 'from math import pi\n\ndef _exec(X0, X1):\n Y0 = 2*X1 - 1/X0\n Y1=5\n return Y0, Y1\n') model3.setParallel(True) # same with multiple outputs design6 = persalys.FixedDesignOfExperiment('design6', model3) design6.setInterestVariables(['Y0']) design6.setOriginalInputSample([[0.9, 1.1], [1.8, 2.2], [0, 2.2]]) design6.setBlockSize(3) design6.run() assert design6.getResult().getDesignOfExperiment().getOutputSample().getSize() == 2, "whole block must no fail"
y_fake = 0. return y """ myStudy = persalys.Study('myStudy') inputs = [] for i in range(20): inputs.append(persalys.Input('x' + str(i + 1), ot.Uniform(0, 1))) y = persalys.Output('y') y_fake = persalys.Output('y_fake') print(inputs) model = persalys.PythonPhysicalModel('MorrisModel', inputs, [y, y_fake], code) myStudy.add(model) # Morris ## analysis = persalys.MorrisAnalysis('aMorris', model) analysis.setInterestVariables(['y']) analysis.setLevel(4) analysis.setTrajectoriesNumber(4) analysis.setSeed(2) myStudy.add(analysis) print(analysis) analysis.run() print("error message=", analysis.getErrorMessage())