File: t_PythonPhysicalModel_mp.py

package info (click to toggle)
persalys 13.1.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 71,916 kB
  • sloc: xml: 496,859; cpp: 53,848; python: 3,435; sh: 332; makefile: 131; ansic: 14
file content (69 lines) | stat: -rwxr-xr-x 1,961 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python

import openturns as ot
import openturns.testing
import persalys

myStudy = persalys.Study("myStudy")

# Model
R = persalys.Input(
    "R", 0.0, ot.LogNormalMuSigma(300.0, 30.0).getDistribution(), "Yield strength"
)
F = persalys.Input("F", 0.0, ot.Normal(75000.0, 5000.0), "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.0, 75000.0]))

# 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.0, 75000.0]))

# 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"
)
model.setParallel(True)
f = model.getFunction()
print(f([[300.0, 75000.0]]))
print(f([[300.0, 75000.0], [400.0, 74000.0]]))

# test operator() (sample)
code = "from math import pi\nimport time\ndef _exec(R, F):\n"
code += "    if R == 300.:\n      time.sleep(0.5)\n    G = 2*R-F/(pi*100.0)\n    return G\n"
model.setCode(code)
plan_0 = persalys.FixedDesignOfExperiment(
    "plan_0", model, [[300.0, 75000.0], [400.0, 74000.0]]
)
plan_0.setBlockSize(2)
myStudy.add(plan_0)
plan_0.run()
print("Sample :\n %s" % plan_0.getResult().getDesignOfExperiment().getSample())

# model with an error
model.setCode(
    "from math import pi\n\ndef _exec(R, F):\n    G = 2*R-F/(pi*100.0)/0.\n    return G\n"
)
model.setParallel(True)
f = model.getFunction()
try:
    print(f([[300.0, 75000.0]]))
except Exception as e:
    print(
        "ZeroDivisionError occured: %s"
        % ("ZeroDivisionError: float division by zero" in str(e))
    )
    print("Error on the line 4 : %s" % ("line 4" in str(e)))

# script
script = myStudy.getPythonScript()
print(script)
exec(script)