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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#!/usr/bin/env python
import openturns as ot
import persalys
Study_0 = persalys.Study("Study_0")
persalys.Study.Add(Study_0)
# variables
z0 = persalys.Input("z0", 100, "")
v0 = persalys.Input("v0", 55, "")
m = persalys.Input("m", 80, "")
c = persalys.Input("c", 15, "")
z = persalys.Output("z", "")
inputs = [z0, v0, m, c]
outputs = [z]
# mesh model
meshModel = persalys.GridMeshModel(ot.Interval(0.0, 12.0), [20])
# Python model
code = "from math import exp\n\n"
code += "def _exec(z0,v0,m,c):\n g = 9.81\n zmin = 0.\n tau = m / c\n vinf = -m * g / c\n\n"
code += " # mesh nodes\n t = getMesh().getVertices()\n\n"
code += " z = [max(z0 + vinf * t_i[0] + tau * (v0 - vinf) * (1 - exp(-t_i[0] / tau)), zmin) for t_i in t]\n"
code += " return z"
model = persalys.PythonFieldModel("model", meshModel, inputs, outputs, code)
Study_0.add(model)
f = model.getPointToFieldFunction()
print(f([100, 55, 81, 17]))
print(f([[100, 55, 81, 17], [100, 55, 81, 18]]))
# model with an error : ZeroDivisionError
code = "from math import exp\n\n"
code += "def _exec(z0,v0,m,c):\n g = 9.81 / 0.\n zmin = 0.\n tau = m / c\n vinf = -m * g / c\n\n"
code += " # mesh nodes\n t = mesh.getVertices()\n\n"
code += " z = [max(z0 + vinf * t_i[0] + tau * (v0 - vinf) * (1 - exp(-t_i[0] / tau)), zmin) for t_i in t]\n"
code += " return z"
model.setCode(code)
model.setParallel(True)
f = model.getPointToFieldFunction()
try:
print(f([[100, 55, 81, 17]]))
except Exception as e:
ok = "ZeroDivisionError" in str(e)
print(f"ZeroDivisionError occurred: {ok}")
print("Error on the line 4 : %s" % ("line 4" in str(e)))
try:
print(f([100, 55, 81, 17]))
except Exception as e:
ok = "ZeroDivisionError" in str(e)
print(f"ZeroDivisionError occurred: {ok}")
print("Error on the line 4 : %s" % ("line 4" in str(e)))
# model with an error : wrong output type
model.setCode("def _exec(z0,v0,m,c):\n z = z0\n return z")
f = model.getPointToFieldFunction()
try:
print(f([100, 55, 81, 17]))
except Exception as e:
print(
"InvalidArgumentException occurred: %s" % ("InvalidArgumentException" in str(e))
)
try:
print(f([[100, 55, 81, 17]]))
except Exception as e:
print(
"InvalidArgumentException occurred: %s" % ("InvalidArgumentException" in str(e))
)
# model with an error : wrong output sequence size
model.setCode("def _exec(z0,v0,m,c):\n z = [z0]*2\n return z")
f = model.getPointToFieldFunction()
try:
print(f([100, 55, 81, 17]))
except Exception as e:
print(
"InvalidArgumentException occurred: %s" % ("InvalidArgumentException" in str(e))
)
try:
print(f([[100, 55, 81, 17]]))
except Exception as e:
print(
"InvalidArgumentException occurred: %s" % ("InvalidArgumentException" in str(e))
)
# Python model with two outputs
z1 = persalys.Output("z1", "")
outputs = [z, z1]
code = "from math import exp\n\ndef _exec(z0,v0,m,c):\n g = 9.81\n zmin = 0.\n tau = m / c\n vinf = -m * g / c\n\n"
code += " # mesh nodes\n t = getMesh().getVertices()\n\n"
code += " z = [max(z0 + vinf * t_i[0] + tau * (v0 - vinf) * (1 - exp(-t_i[0] / tau)), zmin) for t_i in t]\n z1 = z[:]\n"
code += " return z, z1"
model2 = persalys.PythonFieldModel("model2", meshModel, inputs, outputs, code)
model2.setParallel(True)
Study_0.add(model2)
f = model2.getPointToFieldFunction()
print(f([100, 55, 81, 17]))
print(f([[100, 55, 81, 17], [100, 55, 81, 18]]))
# model2 with an error : wrong output sequence size
code = "from math import exp\n\n"
code += "def _exec(z0,v0,m,c):\n g = 9.81\n zmin = 0.\n tau = m / c\n vinf = -m * g / c\n\n"
code += " # mesh nodes\n t = getMesh().getVertices()\n\n"
code += " z = [max(z0 + vinf * t_i[0] + tau * (v0 - vinf) * (1 - exp(-t_i[0] / tau)), zmin) for t_i in t]\n z1 = [2.]\n"
code += " return z, z1"
model.setCode(code)
f = model2.getPointToFieldFunction()
try:
print(f([100, 55, 81, 17]))
except Exception as e:
print(
"InvalidArgumentException occurred: %s" % ("InvalidArgumentException" in str(e))
)
try:
print(f([[100, 55, 81, 17]]))
except Exception as e:
print(
"InvalidArgumentException occurred: %s" % ("InvalidArgumentException" in str(e))
)
# script
script = Study_0.getPythonScript()
print(script)
exec(script)
|