1 //                                               -*- C++ -*-
2 /**
3  *  @brief An interface for all implementation class of process
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/PersistentObjectFactory.hxx"
22 #include "openturns/ProcessImplementation.hxx"
23 #include "openturns/Exception.hxx"
24 #include "openturns/Function.hxx"
25 #include "openturns/PiecewiseLinearEvaluation.hxx"
26 #include "openturns/P1LagrangeEvaluation.hxx"
27 #include "openturns/RegularGrid.hxx"
28 #include "openturns/Process.hxx"
29 
30 BEGIN_NAMESPACE_OPENTURNS
31 
32 CLASSNAMEINIT(ProcessImplementation)
33 
34 static const Factory<ProcessImplementation> Factory_ProcessImplementation;
35 
36 /* Default constructor */
ProcessImplementation()37 ProcessImplementation::ProcessImplementation()
38   : PersistentObject()
39   , description_()
40   , ouputDimension_(1)
41   , mesh_(RegularGrid(0.0, 1.0, 1))
42 {
43   // Nothing to do
44 }
45 
46 /* Virtual constructor */
clone() const47 ProcessImplementation * ProcessImplementation::clone() const
48 {
49   return new ProcessImplementation(*this);
50 }
51 
52 /* String converter */
__repr__() const53 String ProcessImplementation::__repr__() const
54 {
55   OSS oss(true);
56   oss << "class=" << ProcessImplementation::GetClassName()
57       << " ouputDimension=" << ouputDimension_
58       << " description=" << description_
59       << " mesh=" << mesh_;
60   return oss;
61 }
62 
63 /* String converter */
__str__(const String & offset) const64 String ProcessImplementation::__str__(const String & offset) const
65 {
66   OSS oss(false);
67   oss << "class=" << ProcessImplementation::GetClassName()
68       << " ouputDimension=" << ouputDimension_
69       << " description=" << description_.__str__(offset)
70       << " mesh=" << mesh_.__str__(offset);
71   return oss;
72 }
73 
74 /* Dimension accessor */
getInputDimension() const75 UnsignedInteger ProcessImplementation::getInputDimension() const
76 {
77   return mesh_.getDimension();
78 }
79 
getOutputDimension() const80 UnsignedInteger ProcessImplementation::getOutputDimension() const
81 {
82   return ouputDimension_;
83 }
84 
setOutputDimension(const UnsignedInteger ouputDimension)85 void ProcessImplementation::setOutputDimension(const UnsignedInteger ouputDimension)
86 {
87   ouputDimension_ = ouputDimension;
88 }
89 
90 /* Description accessor */
setDescription(const Description & description)91 void ProcessImplementation::setDescription(const Description & description)
92 {
93   description_ = description;
94 }
95 
getDescription() const96 Description ProcessImplementation::getDescription() const
97 {
98   return description_;
99 }
100 
101 /* TimeGrid accessor */
getTimeGrid() const102 RegularGrid ProcessImplementation::getTimeGrid() const
103 {
104   return getMesh();
105 }
106 
setTimeGrid(const RegularGrid & timeGrid)107 void ProcessImplementation::setTimeGrid(const RegularGrid & timeGrid)
108 {
109   setMesh(timeGrid);
110 }
111 
112 /* Mesh accessor */
getMesh() const113 Mesh ProcessImplementation::getMesh() const
114 {
115   return mesh_;
116 }
117 
setMesh(const Mesh & mesh)118 void ProcessImplementation::setMesh(const Mesh & mesh)
119 {
120   mesh_ = mesh;
121 }
122 
123 
124 /* Here is the interface that all derived class must implement */
125 
126 
127 /* Is the underlying a gaussian process ? */
isNormal() const128 Bool ProcessImplementation::isNormal() const
129 {
130   return false;
131 }
132 
133 /* Is the underlying a stationary process ? */
isStationary() const134 Bool ProcessImplementation::isStationary() const
135 {
136   return false;
137 }
138 
139 /* Is the underlying a composite process ? */
isComposite() const140 Bool ProcessImplementation::isComposite() const
141 {
142   return false;
143 }
144 
145 /* Covariance model accessor */
getCovarianceModel() const146 CovarianceModel ProcessImplementation::getCovarianceModel() const
147 {
148   throw NotYetImplementedException(HERE) << "In ProcessImplementation::getCovarianceModel()";
149 }
150 
151 /* Trend accessor */
getTrend() const152 TrendTransform ProcessImplementation::getTrend() const
153 {
154   throw NotYetImplementedException(HERE) << "In ProcessImplementation::getTrend()";
155 }
156 
157 /* Discrete realization accessor */
getRealization() const158 Field ProcessImplementation::getRealization() const
159 {
160   throw NotYetImplementedException(HERE) << "In ProcessImplementation::getRealization() const";
161 }
162 
163 /* Continuous realization accessor */
getContinuousRealization() const164 Function ProcessImplementation::getContinuousRealization() const
165 {
166   // The continuous realization is obtained by a piecewise linear interpolation
167   const Field field(getRealization());
168   const Sample values(field.getValues());
169   if (getInputDimension() == 1)
170   {
171     const Point locations(mesh_.getVertices().getImplementation()->getData());
172     return PiecewiseLinearEvaluation(locations, values);
173   }
174   return P1LagrangeEvaluation(field);
175 }
176 
getSample(const UnsignedInteger size) const177 ProcessSample ProcessImplementation::getSample(const UnsignedInteger size) const
178 {
179   ProcessSample result(size, getRealization());
180   for (UnsignedInteger i = 1; i < size; ++i) result[i] = getRealization().getValues();
181   return result;
182 }
183 
184 /* Future accessor */
getFuture(const UnsignedInteger) const185 TimeSeries ProcessImplementation::getFuture(const UnsignedInteger ) const
186 {
187   throw NotYetImplementedException(HERE) << "In ProcessImplementation::getFuture(const UnsignedInteger stepNumber) const";
188 }
189 
getFuture(const UnsignedInteger stepNumber,const UnsignedInteger size) const190 ProcessSample ProcessImplementation::getFuture(const UnsignedInteger stepNumber,
191     const UnsignedInteger size) const
192 {
193   if (getInputDimension() != 1) throw NotDefinedException(HERE) << "Error: can extend the realization of a process only if defined on a 1D mesh.";
194   if (size == 0) return ProcessSample(mesh_, 0, getOutputDimension());
195   ProcessSample result(size, getFuture(stepNumber));
196   for (UnsignedInteger i = 1; i < size; ++i) result[i] = getFuture(stepNumber).getValues();
197   return result;
198 }
199 
200 /* Get the random vector corresponding to the i-th marginal component */
getMarginal(const UnsignedInteger i) const201 Process ProcessImplementation::getMarginal(const UnsignedInteger i) const
202 {
203   return getMarginal(Indices(1, i));
204 }
205 
206 /* Get the marginal random vector corresponding to indices components */
getMarginal(const Indices & indices) const207 Process ProcessImplementation::getMarginal(const Indices & indices) const
208 {
209   const UnsignedInteger outputDimension = getOutputDimension();
210   if (!indices.check(outputDimension)) throw InvalidArgumentException(HERE) << "Error: the indices of a marginal process must be in the range [0, dim-1] and must be different";
211   if (outputDimension == 1) return clone();
212   throw NotYetImplementedException(HERE) << "In ProcessImplementation::getMarginal(const Indices & indices) const";
213 }
214 
215 
216 /* Method save() stores the object through the StorageManager */
save(Advocate & adv) const217 void ProcessImplementation::save(Advocate & adv) const
218 {
219   PersistentObject::save(adv);
220   adv.saveAttribute( "ouputDimension_", ouputDimension_ );
221   adv.saveAttribute( "description_", description_ );
222   adv.saveAttribute( "mesh_", mesh_ );
223 }
224 
225 /* Method load() reloads the object from the StorageManager */
load(Advocate & adv)226 void ProcessImplementation::load(Advocate & adv)
227 {
228   PersistentObject::load(adv);
229   adv.loadAttribute( "ouputDimension_", ouputDimension_ );
230   adv.loadAttribute( "description_", description_ );
231   adv.loadAttribute( "mesh_", mesh_ );
232 }
233 
234 END_NAMESPACE_OPENTURNS
235