Note
Go to the end to download the full example code.
FMUPointToFieldFunction basics
otfmi.FMUPointToFieldFunction
wraps the FMU into an
openturns.PointToFieldFunction
object.
This kind of function accepts openturns.Point
or
openturns.Sample
as inputs, and outputs a
openturns.Sample
or a set of openturns.Field
.
First, retrieve the path to epid.fmu. Recall the deviation model is dynamic, i.e. its output evolves over time.
import openturns as ot
import otfmi.example.utility
import matplotlib.pyplot as plt
import openturns.viewer as viewer
path_fmu = otfmi.example.utility.get_path_fmu("epid")
Define the time grid for the FMU’s output. The last value of the time grid, here 10., will define the FMU stop time for simulation.
mesh = ot.RegularGrid(0.0, 0.1, 100)
meshSample = mesh.getVertices()
print(meshSample)
[ t ]
0 : [ 0 ]
1 : [ 0.1 ]
2 : [ 0.2 ]
3 : [ 0.3 ]
4 : [ 0.4 ]
5 : [ 0.5 ]
6 : [ 0.6 ]
7 : [ 0.7 ]
8 : [ 0.8 ]
9 : [ 0.9 ]
10 : [ 1 ]
11 : [ 1.1 ]
12 : [ 1.2 ]
13 : [ 1.3 ]
14 : [ 1.4 ]
15 : [ 1.5 ]
16 : [ 1.6 ]
17 : [ 1.7 ]
18 : [ 1.8 ]
19 : [ 1.9 ]
20 : [ 2 ]
21 : [ 2.1 ]
22 : [ 2.2 ]
23 : [ 2.3 ]
24 : [ 2.4 ]
25 : [ 2.5 ]
26 : [ 2.6 ]
27 : [ 2.7 ]
28 : [ 2.8 ]
29 : [ 2.9 ]
30 : [ 3 ]
31 : [ 3.1 ]
32 : [ 3.2 ]
33 : [ 3.3 ]
34 : [ 3.4 ]
35 : [ 3.5 ]
36 : [ 3.6 ]
37 : [ 3.7 ]
38 : [ 3.8 ]
39 : [ 3.9 ]
40 : [ 4 ]
41 : [ 4.1 ]
42 : [ 4.2 ]
43 : [ 4.3 ]
44 : [ 4.4 ]
45 : [ 4.5 ]
46 : [ 4.6 ]
47 : [ 4.7 ]
48 : [ 4.8 ]
49 : [ 4.9 ]
50 : [ 5 ]
51 : [ 5.1 ]
52 : [ 5.2 ]
53 : [ 5.3 ]
54 : [ 5.4 ]
55 : [ 5.5 ]
56 : [ 5.6 ]
57 : [ 5.7 ]
58 : [ 5.8 ]
59 : [ 5.9 ]
60 : [ 6 ]
61 : [ 6.1 ]
62 : [ 6.2 ]
63 : [ 6.3 ]
64 : [ 6.4 ]
65 : [ 6.5 ]
66 : [ 6.6 ]
67 : [ 6.7 ]
68 : [ 6.8 ]
69 : [ 6.9 ]
70 : [ 7 ]
71 : [ 7.1 ]
72 : [ 7.2 ]
73 : [ 7.3 ]
74 : [ 7.4 ]
75 : [ 7.5 ]
76 : [ 7.6 ]
77 : [ 7.7 ]
78 : [ 7.8 ]
79 : [ 7.9 ]
80 : [ 8 ]
81 : [ 8.1 ]
82 : [ 8.2 ]
83 : [ 8.3 ]
84 : [ 8.4 ]
85 : [ 8.5 ]
86 : [ 8.6 ]
87 : [ 8.7 ]
88 : [ 8.8 ]
89 : [ 8.9 ]
90 : [ 9 ]
91 : [ 9.1 ]
92 : [ 9.2 ]
93 : [ 9.3 ]
94 : [ 9.4 ]
95 : [ 9.5 ]
96 : [ 9.6 ]
97 : [ 9.7 ]
98 : [ 9.8 ]
99 : [ 9.9 ]
Note
The FMU solver uses its own time grid for simulation. The FMU output is then interpolated on the user-provided time grid.
Wrap the FMU in an openturns.PointToFieldFunction
object:
function = otfmi.FMUPointToFieldFunction(
mesh,
path_fmu,
inputs_fmu=["infection_rate"],
outputs_fmu=["infected"],
start_time=0.0,
final_time=10.0,
)
print(type(function))
<class 'openturns.func.PointToFieldFunction'>
Note
The start and final times must define an interval comprising the mesh. Setting manually the start and final times is recommended to avoid uncontrolled simulation duration.
Simulate the function on an input openturns.Point
yields an output
openturns.Sample
, corresponding to the output evolution over time:
inputPoint = ot.Point([0.007])
outputSample = function(inputPoint)
plt.xlabel("FMU simulation time (s)")
plt.ylabel("Number of Infected")
plt.plot(meshSample, outputSample)
plt.show()
Simulate the function on a input openturns.Sample
yields a set of
fields called openturns.ProcessSample
:
inputSample = ot.Sample([[0.007], [0.005], [0.003]])
outputProcessSample = function(inputSample)
print(outputProcessSample)
[field 0:
[ t infected ]
0 : [ 0 1 ]
1 : [ 0.1 1.59178 ]
2 : [ 0.2 2.53261 ]
3 : [ 0.3 4.02656 ]
4 : [ 0.4 6.39434 ]
5 : [ 0.5 10.1358 ]
6 : [ 0.6 16.02 ]
7 : [ 0.7 25.2052 ]
8 : [ 0.8 39.377 ]
9 : [ 0.9 60.852 ]
10 : [ 1 92.5148 ]
11 : [ 1.1 137.344 ]
12 : [ 1.2 197.251 ]
13 : [ 1.3 271.29 ]
14 : [ 1.4 354.226 ]
15 : [ 1.5 437.182 ]
16 : [ 1.6 510.947 ]
17 : [ 1.7 569.713 ]
18 : [ 1.8 612.363 ]
19 : [ 1.9 641.111 ]
20 : [ 2 659.398 ]
21 : [ 2.1 670.465 ]
22 : [ 2.2 676.81 ]
23 : [ 2.3 680.17 ]
24 : [ 2.4 681.681 ]
25 : [ 2.5 682.06 ]
26 : [ 2.6 681.752 ]
27 : [ 2.7 681.028 ]
28 : [ 2.8 680.053 ]
29 : [ 2.9 678.928 ]
30 : [ 3 677.712 ]
31 : [ 3.1 676.444 ]
32 : [ 3.2 675.144 ]
33 : [ 3.3 673.827 ]
34 : [ 3.4 672.499 ]
35 : [ 3.5 671.167 ]
36 : [ 3.6 669.833 ]
37 : [ 3.7 668.499 ]
38 : [ 3.8 667.165 ]
39 : [ 3.9 665.834 ]
40 : [ 4 664.504 ]
41 : [ 4.1 663.177 ]
42 : [ 4.2 661.852 ]
43 : [ 4.3 660.53 ]
44 : [ 4.4 659.21 ]
45 : [ 4.5 657.892 ]
46 : [ 4.6 656.578 ]
47 : [ 4.7 655.266 ]
48 : [ 4.8 653.956 ]
49 : [ 4.9 652.649 ]
50 : [ 5 651.345 ]
51 : [ 5.1 650.043 ]
52 : [ 5.2 648.744 ]
53 : [ 5.3 647.448 ]
54 : [ 5.4 646.154 ]
55 : [ 5.5 644.863 ]
56 : [ 5.6 643.574 ]
57 : [ 5.7 642.288 ]
58 : [ 5.8 641.004 ]
59 : [ 5.9 639.723 ]
60 : [ 6 638.445 ]
61 : [ 6.1 637.169 ]
62 : [ 6.2 635.896 ]
63 : [ 6.3 634.625 ]
64 : [ 6.4 633.357 ]
65 : [ 6.5 632.091 ]
66 : [ 6.6 630.828 ]
67 : [ 6.7 629.567 ]
68 : [ 6.8 628.309 ]
69 : [ 6.9 627.054 ]
70 : [ 7 625.801 ]
71 : [ 7.1 624.55 ]
72 : [ 7.2 623.302 ]
73 : [ 7.3 622.056 ]
74 : [ 7.4 620.813 ]
75 : [ 7.5 619.572 ]
76 : [ 7.6 618.334 ]
77 : [ 7.7 617.099 ]
78 : [ 7.8 615.865 ]
79 : [ 7.9 614.635 ]
80 : [ 8 613.406 ]
81 : [ 8.1 612.181 ]
82 : [ 8.2 610.957 ]
83 : [ 8.3 609.736 ]
84 : [ 8.4 608.518 ]
85 : [ 8.5 607.302 ]
86 : [ 8.6 606.088 ]
87 : [ 8.7 604.877 ]
88 : [ 8.8 603.668 ]
89 : [ 8.9 602.462 ]
90 : [ 9 601.258 ]
91 : [ 9.1 600.056 ]
92 : [ 9.2 598.857 ]
93 : [ 9.3 597.66 ]
94 : [ 9.4 596.466 ]
95 : [ 9.5 595.274 ]
96 : [ 9.6 594.084 ]
97 : [ 9.7 592.897 ]
98 : [ 9.8 591.712 ]
99 : [ 9.9 590.53 ]
field 1:
[ t infected ]
0 : [ 0 1 ]
1 : [ 0.1 1.39918 ]
2 : [ 0.2 1.95728 ]
3 : [ 0.3 2.73717 ]
4 : [ 0.4 3.82621 ]
5 : [ 0.5 5.34539 ]
6 : [ 0.6 7.46164 ]
7 : [ 0.7 10.4038 ]
8 : [ 0.8 14.4832 ]
9 : [ 0.9 20.1179 ]
10 : [ 1 27.8602 ]
11 : [ 1.1 38.4222 ]
12 : [ 1.2 52.6901 ]
13 : [ 1.3 71.709 ]
14 : [ 1.4 96.6143 ]
15 : [ 1.5 128.474 ]
16 : [ 1.6 168.023 ]
17 : [ 1.7 215.305 ]
18 : [ 1.8 269.325 ]
19 : [ 1.9 327.883 ]
20 : [ 2 387.789 ]
21 : [ 2.1 445.478 ]
22 : [ 2.2 497.811 ]
23 : [ 2.3 542.704 ]
24 : [ 2.4 579.341 ]
25 : [ 2.5 607.985 ]
26 : [ 2.6 629.58 ]
27 : [ 2.7 645.36 ]
28 : [ 2.8 656.568 ]
29 : [ 2.9 664.301 ]
30 : [ 3 669.46 ]
31 : [ 3.1 672.742 ]
32 : [ 3.2 674.674 ]
33 : [ 3.3 675.642 ]
34 : [ 3.4 675.926 ]
35 : [ 3.5 675.727 ]
36 : [ 3.6 675.188 ]
37 : [ 3.7 674.41 ]
38 : [ 3.8 673.465 ]
39 : [ 3.9 672.403 ]
40 : [ 4 671.259 ]
41 : [ 4.1 670.059 ]
42 : [ 4.2 668.819 ]
43 : [ 4.3 667.553 ]
44 : [ 4.4 666.269 ]
45 : [ 4.5 664.972 ]
46 : [ 4.6 663.668 ]
47 : [ 4.7 662.359 ]
48 : [ 4.8 661.048 ]
49 : [ 4.9 659.736 ]
50 : [ 5 658.424 ]
51 : [ 5.1 657.112 ]
52 : [ 5.2 655.802 ]
53 : [ 5.3 654.494 ]
54 : [ 5.4 653.188 ]
55 : [ 5.5 651.883 ]
56 : [ 5.6 650.582 ]
57 : [ 5.7 649.282 ]
58 : [ 5.8 647.985 ]
59 : [ 5.9 646.69 ]
60 : [ 6 645.398 ]
61 : [ 6.1 644.109 ]
62 : [ 6.2 642.821 ]
63 : [ 6.3 641.537 ]
64 : [ 6.4 640.255 ]
65 : [ 6.5 638.975 ]
66 : [ 6.6 637.699 ]
67 : [ 6.7 636.424 ]
68 : [ 6.8 635.152 ]
69 : [ 6.9 633.883 ]
70 : [ 7 632.616 ]
71 : [ 7.1 631.352 ]
72 : [ 7.2 630.09 ]
73 : [ 7.3 628.831 ]
74 : [ 7.4 627.575 ]
75 : [ 7.5 626.32 ]
76 : [ 7.6 625.069 ]
77 : [ 7.7 623.82 ]
78 : [ 7.8 622.573 ]
79 : [ 7.9 621.329 ]
80 : [ 8 620.087 ]
81 : [ 8.1 618.848 ]
82 : [ 8.2 617.611 ]
83 : [ 8.3 616.377 ]
84 : [ 8.4 615.145 ]
85 : [ 8.5 613.916 ]
86 : [ 8.6 612.689 ]
87 : [ 8.7 611.465 ]
88 : [ 8.8 610.243 ]
89 : [ 8.9 609.023 ]
90 : [ 9 607.806 ]
91 : [ 9.1 606.592 ]
92 : [ 9.2 605.379 ]
93 : [ 9.3 604.17 ]
94 : [ 9.4 602.962 ]
95 : [ 9.5 601.757 ]
96 : [ 9.6 600.555 ]
97 : [ 9.7 599.355 ]
98 : [ 9.8 598.157 ]
99 : [ 9.9 596.961 ]
field 2:
[ t infected ]
0 : [ 0 1 ]
1 : [ 0.1 1.22566 ]
2 : [ 0.2 1.50213 ]
3 : [ 0.3 1.8408 ]
4 : [ 0.4 2.25559 ]
5 : [ 0.5 2.76348 ]
6 : [ 0.6 3.38519 ]
7 : [ 0.7 4.14596 ]
8 : [ 0.8 5.07648 ]
9 : [ 0.9 6.21401 ]
10 : [ 1 7.60371 ]
11 : [ 1.1 9.30013 ]
12 : [ 1.2 11.3689 ]
13 : [ 1.3 13.8889 ]
14 : [ 1.4 16.9539 ]
15 : [ 1.5 20.6753 ]
16 : [ 1.6 25.1841 ]
17 : [ 1.7 30.6325 ]
18 : [ 1.8 37.196 ]
19 : [ 1.9 45.0727 ]
20 : [ 2 54.4827 ]
21 : [ 2.1 65.6632 ]
22 : [ 2.2 78.862 ]
23 : [ 2.3 94.3244 ]
24 : [ 2.4 112.277 ]
25 : [ 2.5 132.903 ]
26 : [ 2.6 156.317 ]
27 : [ 2.7 182.534 ]
28 : [ 2.8 211.439 ]
29 : [ 2.9 242.765 ]
30 : [ 3 276.088 ]
31 : [ 3.1 310.833 ]
32 : [ 3.2 346.304 ]
33 : [ 3.3 381.741 ]
34 : [ 3.4 416.375 ]
35 : [ 3.5 449.497 ]
36 : [ 3.6 480.512 ]
37 : [ 3.7 508.975 ]
38 : [ 3.8 534.606 ]
39 : [ 3.9 557.284 ]
40 : [ 4 577.029 ]
41 : [ 4.1 593.965 ]
42 : [ 4.2 608.294 ]
43 : [ 4.3 620.263 ]
44 : [ 4.4 630.138 ]
45 : [ 4.5 638.185 ]
46 : [ 4.6 644.659 ]
47 : [ 4.7 649.795 ]
48 : [ 4.8 653.803 ]
49 : [ 4.9 656.865 ]
50 : [ 5 659.142 ]
51 : [ 5.1 660.767 ]
52 : [ 5.2 661.856 ]
53 : [ 5.3 662.503 ]
54 : [ 5.4 662.789 ]
55 : [ 5.5 662.78 ]
56 : [ 5.6 662.528 ]
57 : [ 5.7 662.08 ]
58 : [ 5.8 661.471 ]
59 : [ 5.9 660.732 ]
60 : [ 6 659.887 ]
61 : [ 6.1 658.956 ]
62 : [ 6.2 657.956 ]
63 : [ 6.3 656.9 ]
64 : [ 6.4 655.798 ]
65 : [ 6.5 654.66 ]
66 : [ 6.6 653.492 ]
67 : [ 6.7 652.301 ]
68 : [ 6.8 651.092 ]
69 : [ 6.9 649.868 ]
70 : [ 7 648.632 ]
71 : [ 7.1 647.387 ]
72 : [ 7.2 646.136 ]
73 : [ 7.3 644.879 ]
74 : [ 7.4 643.618 ]
75 : [ 7.5 642.355 ]
76 : [ 7.6 641.091 ]
77 : [ 7.7 639.825 ]
78 : [ 7.8 638.559 ]
79 : [ 7.9 637.294 ]
80 : [ 8 636.029 ]
81 : [ 8.1 634.765 ]
82 : [ 8.2 633.502 ]
83 : [ 8.3 632.241 ]
84 : [ 8.4 630.981 ]
85 : [ 8.5 629.724 ]
86 : [ 8.6 628.468 ]
87 : [ 8.7 627.214 ]
88 : [ 8.8 625.963 ]
89 : [ 8.9 624.713 ]
90 : [ 9 623.466 ]
91 : [ 9.1 622.221 ]
92 : [ 9.2 620.978 ]
93 : [ 9.3 619.738 ]
94 : [ 9.4 618.5 ]
95 : [ 9.5 617.265 ]
96 : [ 9.6 616.032 ]
97 : [ 9.7 614.801 ]
98 : [ 9.8 613.572 ]
99 : [ 9.9 612.346 ]]
Visualize the time evolution of the infected
over time, depending on the
ìnfection_rate` value:
gridLayout = outputProcessSample.draw()
graph = gridLayout.getGraph(0, 0)
graph.setTitle("")
graph.setXTitle("FMU simulation time (s)")
graph.setYTitle("Number of infected")
graph.setLegends([str(line[0]) for line in inputSample])
view = viewer.View(graph, legend_kw={"title": "infection rate"})
view.ShowAll()
Total running time of the script: (0 minutes 0.177 seconds)