File: t_KarhunenLoeveP1Algorithm_std.py

package info (click to toggle)
openturns 1.24-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,204 kB
  • sloc: cpp: 256,662; python: 63,381; ansic: 4,414; javascript: 406; sh: 180; xml: 164; yacc: 123; makefile: 98; lex: 55
file content (72 lines) | stat: -rwxr-xr-x 2,741 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/env python

import openturns as ot
import openturns.testing as ott

mesh = ot.IntervalMesher([9]).build(ot.Interval(-1.0, 1.0))
cov1D = ot.AbsoluteExponential([1.0])
algo = ot.KarhunenLoeveP1Algorithm(mesh, cov1D, 0.0)
algo.setNbModes(8)
algo.run()
result = algo.getResult()
lambd = result.getEigenvalues()
KLModes = result.getModesAsProcessSample()
print("KL modes=", KLModes)
print("KL eigenvalues=", lambd)
process = ot.GaussianProcess(cov1D, KLModes.getMesh())
coefficients = result.project(process.getSample(10))
print("KL coefficients=", coefficients)
KLFunctions = result.getModes()
print("KL functions=", KLFunctions)
print("KL lift=", result.lift(coefficients[0]))
print("KL lift as field=", result.liftAsField(coefficients[0]))

# check spectra variant
if ot.PlatformInfo.HasFeature("spectra"):
    ot.ResourceMap.SetAsString("KarhunenLoeveP1Algorithm-EigenvaluesSolver", "SPECTRA")
    algo.run()
    result2 = algo.getResult()
    lambd2 = result2.getEigenvalues()
    ott.assert_almost_equal(lambd2, lambd)
    ot.ResourceMap.SetAsString("KarhunenLoeveP1Algorithm-EigenvaluesSolver", "LAPACK")

R = ot.CorrelationMatrix(2)
R[0, 1] = 0.5
scale = [1.0]
amplitude = [1.0, 2.0]
cov2D = ot.ExponentialModel(scale, amplitude, R)
algo = ot.KarhunenLoeveP1Algorithm(mesh, cov2D, 0.0)
algo.setNbModes(18)
algo.run()
result = algo.getResult()
lambd = result.getEigenvalues()
KLModes = result.getModesAsProcessSample()
print("KL modes=", KLModes)
print("KL eigenvalues=", lambd)
process = ot.GaussianProcess(cov2D, KLModes.getMesh())
coefficients = result.project(process.getSample(10))
print("KL coefficients=", coefficients)
KLFunctions = result.getModes()
print("KL functions=", KLFunctions)
print("KL lift=", result.lift(coefficients[0]))
print("KL lift as field=", result.liftAsField(coefficients[0]))

# 2d input, to check that issue #1660 is solved
mesh = ot.IntervalMesher([4] * 2).build(ot.Interval([-1.2] * 2, [1.0] * 2))
cov2D = ot.AbsoluteExponential([1.0] * 2)
algo = ot.KarhunenLoeveP1Algorithm(mesh, cov2D, 1e-3)
algo.run()
result = algo.getResult()
lambd = result.getEigenvalues()
KLModesPS = result.getModesAsProcessSample()
# The output is hidden due to near-zero nonreproducible values
# print("KL modes (process sample)=", KLModesPS)
KLScaledModesPS = result.getScaledModesAsProcessSample()
# The output is hidden due to near-zero nonreproducible values
# print("KL scaled modes (process sample)=", KLScaledModesPS)
KLModes = result.getModes()
# The output is hidden due to near-zero nonreproducible values
# print("KL modes (functions)=", KLModes)
KLScaledModes = result.getScaledModes()
# The output is hidden due to near-zero nonreproducible values
# print("KL scaled modes (functions)=", KLScaledModes)