1 //                                               -*- C++ -*-
2 /**
3  *  @brief The test file of class UserDefinedSpectralModel
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/OT.hxx"
22 #include "openturns/OTtestcode.hxx"
23 
24 using namespace OT;
25 using namespace OT::Test;
26 
clean(Scalar in)27 Scalar clean(Scalar in)
28 {
29   // For -0.0 trouble
30   if (std::abs(in) < 1.e-6) return 0.0;
31   return in;
32   //return 1.e-6*round(1.e6*in);
33 }
34 
clean(HermitianMatrix in)35 HermitianMatrix clean(HermitianMatrix in)
36 {
37   UnsignedInteger dim = in.getDimension();
38   for(UnsignedInteger i = 0; i < dim; i++)
39     for(UnsignedInteger j = 0; j <= i; j++)
40     {
41       Scalar realIJ = clean(real(in(i, j)));
42       Scalar imagIJ = clean(imag(in(i, j)));
43       in(i, j) = Complex(realIJ, imagIJ);
44     }
45   return in;
46 }
47 
main(int,char * [])48 int main(int, char *[])
49 {
50   TESTPREAMBLE;
51   OStream fullprint(std::cout);
52 
53   try
54   {
55 
56     /* Default constructor */
57     UserDefinedSpectralModel myDefautModel;
58     fullprint << "myDefautModel = " << myDefautModel << std::endl;
59 
60     /* Default dimension parameter to evaluate the model */
61     const UnsignedInteger dimension = 2;
62 
63     /* Amplitude values */
64     Point amplitude(dimension);
65     /* Scale values */
66     Point scale(dimension);
67     /* Spatial correclation */
68     CorrelationMatrix spatialCorrelation(dimension);
69     for (UnsignedInteger index = 0 ; index < dimension; ++index)
70     {
71       // constant amplitude
72       amplitude[index] = 1.0 ;
73       scale[index] = (index + 1.0) / dimension ;
74       if (index > 0) spatialCorrelation(index, index - 1) = 1.0 / index;
75     }
76 
77     /* Sample a CauchyModel */
78     CauchyModel referenceModel(scale, amplitude, spatialCorrelation);
79 
80     UnsignedInteger size = 5;
81     UserDefinedSpectralModel::HermitianMatrixCollection dspCollection(size);
82     RegularGrid frequencyGrid(0.0, 2.0 / size, size);
83     for (UnsignedInteger i = 0; i < size; ++i)
84       dspCollection[i] = referenceModel(frequencyGrid.getValue(i));
85 
86     /* Create a UserDefinedSpectralModel */
87     UserDefinedSpectralModel myModel(frequencyGrid, dspCollection);
88     fullprint << "myModel=" << myModel << std::endl;
89 
90     /* Sample the UserDefinedSpectralModel */
91     RegularGrid samplingGrid(-0.4, 1.0 / 16, 5 * size);
92     for (UnsignedInteger i = 0; i < samplingGrid.getN(); ++i)
93     {
94       Scalar frequency = samplingGrid.getValue(i);
95       fullprint << "frequency=" << clean(frequency) << ", myModel=\n" << clean(myModel(frequency)) << ", referenceModel=\n" << clean(referenceModel(frequency)) << std::endl;
96     }
97 
98   }
99   catch (TestFailed & ex)
100   {
101     std::cerr << ex << std::endl;
102     return ExitCode::Error;
103   }
104 
105 
106   return ExitCode::Success;
107 }
108