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
|
#! /usr/bin/env python
import openturns as ot
mesh = ot.RegularGrid(0.0, 0.1, 11)
class FUNC(ot.OpenTURNSPythonFieldToPointFunction):
def __init__(self):
# first argument:
super(FUNC, self).__init__(mesh, 2, 2)
self.setInputDescription(["R", "S"])
self.setOutputDescription(["T", "U"])
def _exec(self, X):
Y = ot.Sample(X).computeMean()
return Y
F = FUNC()
print(
"in_dim="
+ str(F.getInputDimension())
+ " out_dim="
+ str(F.getOutputDimension())
+ " spatial_dim="
+ str(F.getInputMesh().getDimension())
)
X = ot.Field(mesh, ot.Normal(2).getSample(11))
print(F(X.getValues()))
Xsample = ot.ProcessSample(5, X)
print(F(Xsample))
# Instance creation
myFunc = ot.FieldToPointFunction(F)
# Copy constructor
newFunc = ot.FieldToPointFunction(myFunc)
print("myFunc input dimension= " + str(myFunc.getInputDimension()))
print("myFunc output dimension= " + str(myFunc.getOutputDimension()))
print(myFunc(X.getValues()))
print(myFunc(Xsample))
vertices = []
vertices.append([0.0, 0.0, 0.0])
vertices.append([0.0, 0.0, 1.0])
vertices.append([0.0, 1.0, 0.0])
vertices.append([0.0, 1.0, 1.0])
vertices.append([1.0, 0.0, 0.0])
vertices.append([1.0, 0.0, 1.0])
vertices.append([1.0, 1.0, 0.0])
vertices.append([1.0, 1.0, 1.0])
simplicies = []
simplicies.append([0, 1, 2, 4])
simplicies.append([3, 5, 6, 7])
simplicies.append([1, 2, 3, 6])
simplicies.append([1, 2, 4, 6])
simplicies.append([1, 3, 5, 6])
simplicies.append([1, 4, 5, 6])
mesh3D = ot.Mesh(vertices, simplicies)
def myPyFunc(X):
Y = ot.Sample(X).computeMean()
return Y
in_dim = 3
out_dim = 3
myFunc = ot.PythonFieldToPointFunction(mesh3D, in_dim, out_dim, myPyFunc)
print("myFunc=", myFunc)
values = ot.Normal(mesh3D.getDimension()).getSample(mesh3D.getVerticesNumber())
# Evaluation over a single field
X = ot.Field(mesh3D, values)
print("X=", X)
Y = myFunc(X.getValues())
print("Y=", Y)
print("myFunc input dimension=", myFunc.getInputDimension())
print("myFunc output dimension=", myFunc.getOutputDimension())
print("myFunc input dimension=", myFunc.getInputMesh().getDimension())
print("called ", myFunc.getCallsNumber(), " times")
# Evaluation over a process sample
X = ot.ProcessSample(5, X)
print("X=", X)
Y = myFunc(X)
print("Y=", Y)
print("myFunc input dimension=", myFunc.getInputDimension())
print("myFunc output dimension=", myFunc.getOutputDimension())
print("myFunc input dimension=", myFunc.getInputMesh().getDimension())
print("called ", myFunc.getCallsNumber(), " times")
|