File: t_GaussianLinearCalibration_chaboche.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 (195 lines) | stat: -rwxr-xr-x 6,208 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#! /usr/bin/env python

import openturns as ot
from openturns.testing import assert_almost_equal

ot.TESTPREAMBLE()
ot.PlatformInfo.SetNumericalPrecision(5)


def matrixToSample(matrix):
    """Converts a matrix into a Sample, so that we can
    assert_almost_equal a Matrix"""
    size = matrix.getNbRows()
    dimension = matrix.getNbColumns()
    sample = ot.Sample(size, dimension)
    for i in range(size):
        for j in range(dimension):
            sample[i, j] = matrix[i, j]
    return sample


def local2globalCovariance(localErrorCovariance, size, outputDimension):
    """
    Converts a local covariance into a global covariance.
    size = inputObservations.getSize()
    outputDimension = modelObservations.getDimension()
    """
    covarianceDimension = size * outputDimension
    observationDimension = localErrorCovariance.getDimension()
    globalErrorCovariance = ot.CovarianceMatrix(covarianceDimension)
    for i in range(size):
        for j in range(observationDimension):
            for k in range(observationDimension):
                globalErrorCovariance[
                    i * observationDimension + j, i * observationDimension + k
                ] = localErrorCovariance[j, k]
    return globalErrorCovariance


"""
Thanks to - Antoine Dumas, Phimeca
References
- J. Lemaitre and J. L. Chaboche (2002) "Mechanics of solid materials" Cambridge University Press.
"""
ot.RandomGenerator.SetSeed(0)

# Create the function.
g = ot.SymbolicFunction(["strain", "R", "C", "gam"], ["R + C*(1-exp(-gam*strain))"])

# Define the random vector.
Strain = ot.Uniform(0, 0.07)
unknownR = 750
unknownC = 2750
unknownGamma = 10
R = ot.Dirac(unknownR)
C = ot.Dirac(unknownC)
Gamma = ot.Dirac(unknownGamma)

Strain.setDescription(["Strain"])
R.setDescription(["R"])
C.setDescription(["C"])
Gamma.setDescription(["Gamma"])

# Create the joint input distribution function.
inputRandomVector = ot.JointDistribution([Strain, R, C, Gamma])

# Create the Monte-Carlo sample.
sampleSize = 10
inputSample = inputRandomVector.getSample(sampleSize)
outputStress = g(inputSample)

# Generate observation noise.
stressObservationNoiseSigma = 40.0  # (Pa)
noiseSigma = ot.Normal(0.0, stressObservationNoiseSigma)
sampleNoiseH = noiseSigma.getSample(sampleSize)
observedStress = outputStress + sampleNoiseH
observedStrain = inputSample[:, 0]

# Set the calibration parameters
# Define the value of the reference values of the $\theta$ parameter.
# In the bayesian framework, this is called the mean of the *prior* gaussian distribution.
# In the data assimilation framework, this is called the *background*.
R = 700  # Exact : 750
C = 2500  # Exact : 2750
Gamma = 8.0  # Exact : 10
thetaPrior = ot.Point([R, C, Gamma])

# The following statement create the calibrated function from the model.
# The calibrated parameters Ks, Zv, Zm are at indices 1, 2, 3 in the inputs arguments of the model.
calibratedIndices = [1, 2, 3]
mycf = ot.ParametricFunction(g, calibratedIndices, thetaPrior)

# Gaussian linear calibration
# The standard deviation of the observations.
sigmaStress = 10.0  # (Pa)

# Define the covariance matrix of the output Y of the model.
localErrorCovariance = ot.CovarianceMatrix(1)
localErrorCovariance[0, 0] = sigmaStress**2

# Define the covariance matrix of the parameters $\theta$ to calibrate.
sigmaR = 0.1 * R
sigmaC = 0.1 * C
sigmaGamma = 0.1 * Gamma

priorCovariance = ot.CovarianceMatrix(3)
priorCovariance[0, 0] = sigmaR**2
priorCovariance[1, 1] = sigmaC**2
priorCovariance[2, 2] = sigmaGamma**2

methods = ["SVD", "QR", "Cholesky"]
for method in methods:
    print("method=", method)
    # 1. Local calibration
    # The `GaussianLinearCalibration` class performs the gaussian linear
    # calibration by linearizing the model in the neighbourhood of the prior.
    algo = ot.GaussianLinearCalibration(
        mycf,
        observedStrain,
        observedStress,
        thetaPrior,
        priorCovariance,
        localErrorCovariance,
        method,
    )

    # The `run` method computes the solution of the problem.
    algo.run()

    calibrationResult = algo.getResult()

    # Analysis of the results
    # Maximum A Posteriori estimator
    thetaMAP = calibrationResult.getParameterMAP()
    exactTheta = ot.Point([762.661, 3056.59, 8.52781])
    assert_almost_equal(thetaMAP, exactTheta)

    # Covariance matrix of theta
    thetaPosterior = calibrationResult.getParameterPosterior()
    covarianceThetaStar = matrixToSample(thetaPosterior.getCovariance())
    exactCovarianceTheta = ot.Sample(
        [
            [42.4899, 288.43, -1.70502],
            [288.43, 15977.1, -67.6046],
            [-1.70502, -67.6046, 0.294659],
        ]
    )
    assert_almost_equal(covarianceThetaStar, exactCovarianceTheta)

    print("result=", calibrationResult)

    # 2. Global covariance
    outputDimension = observedStress.getDimension()
    globalErrorCovariance = local2globalCovariance(
        localErrorCovariance, sampleSize, outputDimension
    )

    # The `GaussianLinearCalibration` class performs the gaussian linear
    # calibration by linearizing the model in the neighbourhood of the prior.
    algo = ot.GaussianLinearCalibration(
        mycf,
        observedStrain,
        observedStress,
        thetaPrior,
        priorCovariance,
        globalErrorCovariance,
        method,
    )

    # The `run` method computes the solution of the problem.
    algo.run()

    # Check other fields
    calibrationResult = algo.getResult()

    # Analysis of the results
    # Maximum A Posteriori estimator
    thetaMAP = calibrationResult.getParameterMAP()
    exactTheta = ot.Point([762.661, 3056.59, 8.52781])
    assert_almost_equal(thetaMAP, exactTheta)

    # Covariance matrix of theta
    thetaPosterior = calibrationResult.getParameterPosterior()
    covarianceThetaStar = matrixToSample(thetaPosterior.getCovariance())
    exactCovarianceTheta = ot.Sample(
        [
            [42.4899, 288.43, -1.70502],
            [288.43, 15977.1, -67.6046],
            [-1.70502, -67.6046, 0.294659],
        ]
    )
    assert_almost_equal(covarianceThetaStar, exactCovarianceTheta)

    # Check other fields
    print("result=", calibrationResult)