File: plot_export_metamodel.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (48 lines) | stat: -rw-r--r-- 1,246 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
"""
Export a metamodel
------------------
"""

# %%
# In this example we will see how to export a metamodel from the context it is created
# to another context where it will actually be used with the help of the pickle module.

# %%
import openturns as ot
from openturns.usecases import cantilever_beam
import pickle

# %%
# Load the cantilever beam use-case.
# We want to use the independent distribution to get meaningful Sobol' indices.
beam = cantilever_beam.CantileverBeam()
g = beam.model
distribution = beam.independentDistribution

# %%
# Generate a learning sample with Monte-Carlo simulation (or retrieve the design from experimental data).
N = 30  # size of the experimental design
X = distribution.getSample(N)
Y = g(X)

# %%
# Build a chaos metamodel
algo = ot.FunctionalChaosAlgorithm(X, Y, distribution)
algo.run()
metamodel = algo.getResult().getMetaModel()

# %%
# Save the metamodel into a `.pkl` file for later use
with open("metamodel.pkl", "wb") as f:
    pickle.dump(metamodel, f)

# %%
# Reload the metamodel in another context from the same `.pkl`
with open("metamodel.pkl", "rb") as f:
    metamodel = pickle.load(f)

# %%
# Reuse the loaded metamodel
x = [6.70455e10, 300.0, 2.55, 1.45385e-07]
y = metamodel(x)
print(y)