1 // -*- C++ -*-
2 /**
3 * @brief First order polynomial response surface by Taylor expansion
4 *
5 * Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6 *
7 * This library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21 #include "openturns/LinearTaylor.hxx"
22 #include "openturns/LinearEvaluation.hxx"
23 #include "openturns/ConstantGradient.hxx"
24 #include "openturns/ConstantHessian.hxx"
25 #include "openturns/PersistentObjectFactory.hxx"
26
27 BEGIN_NAMESPACE_OPENTURNS
28
29
30
31
32 CLASSNAMEINIT(LinearTaylor)
33
34 static const Factory<LinearTaylor> Factory_LinearTaylor;
35
LinearTaylor()36 LinearTaylor::LinearTaylor()
37 : PersistentObject()
38 {
39 // Nothing to do
40 }
41
42 /* Constructor with parameters */
LinearTaylor(const Point & center,const Function & inputFunction)43 LinearTaylor::LinearTaylor(const Point & center,
44 const Function & inputFunction)
45 : PersistentObject(),
46 center_(center),
47 inputFunction_(inputFunction)
48 {
49 // Nothing to do
50 }
51
52 /* Virtual constructor */
clone() const53 LinearTaylor * LinearTaylor::clone() const
54 {
55 return new LinearTaylor(*this);
56 }
57
58 /* String converter */
__repr__() const59 String LinearTaylor::__repr__() const
60 {
61 OSS oss;
62 oss << "class=" << GetClassName()
63 << " name=" << getName()
64 << " center=" << center_
65 << " function=" << inputFunction_
66 << " responseSurface=" << responseSurface_
67 << " constant=" << constant_
68 << " linear=" << linear_;
69 return oss;
70 }
71
72 /* Response surface computation */
run()73 void LinearTaylor::run()
74 {
75 /* Compute the three first terms of the Taylor expansion */
76 constant_ = inputFunction_(center_);
77 linear_ = inputFunction_.gradient(center_);
78 /* Build the several implementations and set it into the response surface */
79 responseSurface_.setEvaluation(new LinearEvaluation(center_, constant_, linear_));
80 responseSurface_.setGradient(new ConstantGradient(linear_));
81 responseSurface_.setHessian(new ConstantHessian(SymmetricTensor(center_.getDimension(), constant_.getDimension())));
82 responseSurface_.setDescription(inputFunction_.getDescription());
83 }
84
85 /* Center accessor */
getCenter() const86 Point LinearTaylor::getCenter() const
87 {
88 return center_;
89 }
90
91 /* Constant accessor */
getConstant() const92 Point LinearTaylor::getConstant() const
93 {
94 return constant_;
95 }
96
97 /* Linear accessor */
getLinear() const98 Matrix LinearTaylor::getLinear() const
99 {
100 return linear_;
101 }
102
103 /* Function accessor */
getInputFunction() const104 Function LinearTaylor::getInputFunction() const
105 {
106 return inputFunction_;
107 }
108
109 /* Metamodel accessor */
getMetaModel() const110 Function LinearTaylor::getMetaModel() const
111 {
112 return responseSurface_;
113 }
114
save(Advocate & adv) const115 void LinearTaylor::save(Advocate & adv) const
116 {
117 PersistentObject::save(adv);
118 adv.saveAttribute("center_", center_);
119 adv.saveAttribute("inputFunction_", inputFunction_);
120 adv.saveAttribute("responseSurface_", responseSurface_);
121 adv.saveAttribute("constant_", constant_);
122 adv.saveAttribute("linear_", linear_);
123 }
124
125 /* Method load() reloads the object from the StorageManager */
load(Advocate & adv)126 void LinearTaylor::load(Advocate & adv)
127 {
128 PersistentObject::load(adv);
129 adv.loadAttribute("center_", center_);
130 adv.loadAttribute("inputFunction_", inputFunction_);
131 adv.loadAttribute("responseSurface_", responseSurface_);
132 adv.loadAttribute("constant_", constant_);
133 adv.loadAttribute("linear_", linear_);
134 }
135
136 END_NAMESPACE_OPENTURNS
137