Salome HOME
Small modif
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingFieldDiscretization.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCouplingFieldDiscretization.hxx"
22 #include "MEDCouplingCMesh.hxx"
23 #include "MEDCouplingUMesh.hxx"
24 #include "MEDCouplingFieldDouble.hxx"
25 #include "MCAuto.hxx"
26
27 #include "CellModel.hxx"
28 #include "InterpolationUtils.hxx"
29 #include "InterpKernelAutoPtr.hxx"
30 #include "InterpKernelGaussCoords.hxx"
31 #include "InterpKernelMatrixTools.hxx"
32
33 #include <set>
34 #include <list>
35 #include <limits>
36 #include <sstream>
37 #include <numeric>
38 #include <algorithm>
39 #include <functional>
40
41 using namespace MEDCoupling;
42
43 const double MEDCouplingFieldDiscretization::DFLT_PRECISION=1.e-12;
44
45 const char MEDCouplingFieldDiscretizationP0::REPR[]="P0";
46
47 const TypeOfField MEDCouplingFieldDiscretizationP0::TYPE=ON_CELLS;
48
49 const char MEDCouplingFieldDiscretizationP1::REPR[]="P1";
50
51 const TypeOfField MEDCouplingFieldDiscretizationP1::TYPE=ON_NODES;
52
53 const int MEDCouplingFieldDiscretizationPerCell::DFT_INVALID_LOCID_VALUE=-1;
54
55 const char MEDCouplingFieldDiscretizationGauss::REPR[]="GAUSS";
56
57 const TypeOfField MEDCouplingFieldDiscretizationGauss::TYPE=ON_GAUSS_PT;
58
59 const char MEDCouplingFieldDiscretizationGaussNE::REPR[]="GSSNE";
60
61 const TypeOfField MEDCouplingFieldDiscretizationGaussNE::TYPE=ON_GAUSS_NE;
62
63 const char MEDCouplingFieldDiscretizationKriging::REPR[]="KRIGING";
64
65 const TypeOfField MEDCouplingFieldDiscretizationKriging::TYPE=ON_NODES_KR;
66
67 // doc is here http://www.code-aster.org/V2/doc/default/fr/man_r/r3/r3.01.01.pdf
68 const double MEDCouplingFieldDiscretizationGaussNE::FGP_POINT1[1]={0.};
69 const double MEDCouplingFieldDiscretizationGaussNE::FGP_SEG2[2]={1.,1.};
70 const double MEDCouplingFieldDiscretizationGaussNE::FGP_SEG3[3]={0.5555555555555556,0.8888888888888888,0.5555555555555556};
71 const double MEDCouplingFieldDiscretizationGaussNE::FGP_SEG4[4]={0.347854845137454,0.347854845137454,0.652145154862546,0.652145154862546};
72 const double MEDCouplingFieldDiscretizationGaussNE::FGP_TRI3[3]={0.16666666666666666,0.16666666666666666,0.16666666666666666};
73 const double MEDCouplingFieldDiscretizationGaussNE::FGP_TRI6[6]={0.0549758718227661,0.0549758718227661,0.0549758718227661,0.11169079483905,0.11169079483905,0.11169079483905};
74 const double MEDCouplingFieldDiscretizationGaussNE::FGP_TRI7[7]={0.062969590272413,0.062969590272413,0.062969590272413,0.066197076394253,0.066197076394253,0.066197076394253,0.1125};
75 const double MEDCouplingFieldDiscretizationGaussNE::FGP_QUAD4[4]={1.,1.,1.,1.};
76 const double MEDCouplingFieldDiscretizationGaussNE::FGP_QUAD8[8]={1.,1.,1.,1.,1.,1.,1.,1.};
77 const double MEDCouplingFieldDiscretizationGaussNE::FGP_QUAD9[9]={0.30864197530864196,0.30864197530864196,0.30864197530864196,0.30864197530864196,0.49382716049382713,0.49382716049382713,0.49382716049382713,0.49382716049382713,0.7901234567901234};
78 const double MEDCouplingFieldDiscretizationGaussNE::FGP_TETRA4[4]={0.041666666666666664,0.041666666666666664,0.041666666666666664,0.041666666666666664};
79 const double MEDCouplingFieldDiscretizationGaussNE::FGP_TETRA10[10]={1.,1.,1.,1.,1.,1.,1.,1.,1.,1.};//to check
80 const double MEDCouplingFieldDiscretizationGaussNE::FGP_PENTA6[6]={0.16666666666666666,0.16666666666666666,0.16666666666666666,0.16666666666666666,0.16666666666666666,0.16666666666666666};
81 const double MEDCouplingFieldDiscretizationGaussNE::FGP_PENTA15[15]={1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.};//to check
82 const double MEDCouplingFieldDiscretizationGaussNE::FGP_HEXA8[8]={1.,1.,1.,1.,1.,1.,1.,1.};
83 const double MEDCouplingFieldDiscretizationGaussNE::FGP_HEXA20[20]={1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.};
84 const double MEDCouplingFieldDiscretizationGaussNE::FGP_HEXA27[27]={1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.};
85 const double MEDCouplingFieldDiscretizationGaussNE::FGP_PYRA5[5]={0.13333333333333333,0.13333333333333333,0.13333333333333333,0.13333333333333333,0.13333333333333333};
86 const double MEDCouplingFieldDiscretizationGaussNE::FGP_PYRA13[13]={1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.};//to check
87 const double MEDCouplingFieldDiscretizationGaussNE::REF_SEG2[2]={-1.,1.};
88 const double MEDCouplingFieldDiscretizationGaussNE::REF_SEG3[3]={-1.,1.,0.};
89 const double MEDCouplingFieldDiscretizationGaussNE::REF_SEG4[4]={-1.,1.,-0.3333333333333333,0.3333333333333333};
90 const double MEDCouplingFieldDiscretizationGaussNE::REF_TRI3[6]={0.,0.,1.,0.,0.,1.};
91 const double MEDCouplingFieldDiscretizationGaussNE::REF_TRI6[12]={0.,0.,1.,0.,0.,1.,0.5,0.,0.5,0.5,0.,0.5};
92 const double MEDCouplingFieldDiscretizationGaussNE::REF_TRI7[14]={0.,0.,1.,0.,0.,1.,0.5,0.,0.5,0.5,0.,0.5,0.3333333333333333,0.3333333333333333};
93 const double MEDCouplingFieldDiscretizationGaussNE::REF_QUAD4[8]={-1.,-1.,1.,-1.,1.,1.,-1.,1.};
94 const double MEDCouplingFieldDiscretizationGaussNE::REF_QUAD8[16]={-1.,-1.,1.,-1.,1.,1.,-1.,1.,0.,-1.,1.,0.,0.,1.,-1.,0.};
95 const double MEDCouplingFieldDiscretizationGaussNE::REF_QUAD9[18]={-1.,-1.,1.,-1.,1.,1.,-1.,1.,0.,-1.,1.,0.,0.,1.,-1.,0.,0.,0.};
96 const double MEDCouplingFieldDiscretizationGaussNE::REF_TETRA4[12]={0.,1.,0.,0.,0.,1.,0.,0.,0.,1.,0.,0.};
97 const double MEDCouplingFieldDiscretizationGaussNE::REF_TETRA10[30]={0.,1.,0.,0.,0.,1.,0.,0.,0.,1.,0.,0.,0.,0.5,0.5,0.,0.,0.5,0.,0.5,0.,0.5,0.5,0.,0.5,0.,0.5,0.5,0.,0.};
98 const double MEDCouplingFieldDiscretizationGaussNE::REF_PENTA6[18]={-1.,1.,0.,-1.,0.,1.,-1.,0.,0.,1.,1.,0.,1.,0.,1.,1.,0.,0.};
99 const double MEDCouplingFieldDiscretizationGaussNE::REF_PENTA15[45]={-1.,1.,0.,-1.,0.,1.,-1.,0.,0.,1.,1.,0.,1.,0.,1.,1.,0.,0.,-1.,0.5,0.5,-1.,0.,0.5,-1.,0.5,0.,0.,1.,0.,0.,0.,1.,0.,0.,0.,1.,0.5,0.5,1.,0.,0.5,1.,0.5,0.};
100 const double MEDCouplingFieldDiscretizationGaussNE::REF_HEXA8[24]={-1.,-1.,-1.,1.,-1.,-1.,1.,1.,-1.,-1.,1.,-1.,-1.,-1.,1.,1.,-1.,1.,1.,1.,1.,-1.,1.,1.};
101 const double MEDCouplingFieldDiscretizationGaussNE::REF_HEXA20[60]={-1.,-1.,-1.,1.,-1.,-1.,1.,1.,-1.,-1.,1.,-1.,-1.,-1.,1.,1.,-1.,1.,1.,1.,1.,-1.,1.,1.,0.,-1.,-1.,1.,0.,-1.,0.,1.,-1.,-1.,0.,-1.,-1.,-1.,0.,1.,-1.,0.,1.,1.,0.,-1.,1.,0.,0.,-1.,1.,1.,0.,1.,0.,1.,1.,-1.,0.,1.};
102 const double MEDCouplingFieldDiscretizationGaussNE::REF_HEXA27[81]={-1.,-1.,-1.,-1.,1.,-1.,1.,1.,-1.,1.,-1.,-1.,-1.,-1.,1.,-1.,1.,1.,1.,1.,1.,1.,-1.,1.,-1.,0.,-1.,0.,1.,-1.,1.,0.,-1.,0.,-1.,-1.,-1.,0.,1.,0.,1.,1.,1.,0.,1.,0.,-1.,1.,-1.,-1.,0.,-1.,1.,0.,1.,1.,0.,1.,-1.,0.,0.,0.,-1.,-1.,0.,0.,0.,1.,0.,1.,0.,0.,0.,-1.,0.,0.,0.,1.,0.,0.,0.};
103 const double MEDCouplingFieldDiscretizationGaussNE::REF_PYRA5[15]={1.,0.,0.,0.,1.,0.,-1.,0.,0.,0.,-1.,0.,0.,0.,1.};
104 const double MEDCouplingFieldDiscretizationGaussNE::REF_PYRA13[39]={1.,0.,0.,0.,-1.,0.,-1.,0.,0.,0.,1.,0.,0.,0.,1.,0.5,-0.5,0.,-0.5,-0.5,0.,-0.5,0.5,0.,0.5,0.5,0.,0.5,0.,0.5,0.,-0.5,0.5,-0.5,0.,0.5,0.,0.5,0.5};
105 const double MEDCouplingFieldDiscretizationGaussNE::LOC_SEG2[2]={0.577350269189626,-0.577350269189626};
106 const double MEDCouplingFieldDiscretizationGaussNE::LOC_SEG3[3]={-0.774596669241,0.,0.774596669241};
107 const double MEDCouplingFieldDiscretizationGaussNE::LOC_SEG4[4]={0.339981043584856,-0.339981043584856,0.861136311594053,-0.861136311594053};
108 const double MEDCouplingFieldDiscretizationGaussNE::LOC_TRI3[6]={0.16666666666666667,0.16666666666666667,0.6666666666666667,0.16666666666666667,0.16666666666666667,0.6666666666666667};
109 const double MEDCouplingFieldDiscretizationGaussNE::LOC_TRI6[12]={0.091576213509771,0.091576213509771,0.816847572980458,0.091576213509771,0.091576213509771,0.816847572980458,0.445948490915965,0.10810301816807,0.445948490915965,0.445948490915965,0.10810301816807,0.445948490915965};
110 const double MEDCouplingFieldDiscretizationGaussNE::LOC_TRI7[14]={0.3333333333333333,0.3333333333333333,0.470142064105115,0.470142064105115,0.05971587178977,0.470142064105115,0.470142064105115,0.05971587178977,0.101286507323456,0.101286507323456,0.797426985353088,0.101286507323456,0.101286507323456,0.797426985353088};
111 const double MEDCouplingFieldDiscretizationGaussNE::LOC_QUAD4[8]={-0.774596669241483,-0.774596669241483,0.774596669241483,-0.774596669241483,0.774596669241483,0.774596669241483,-0.774596669241483,0.774596669241483};
112 const double MEDCouplingFieldDiscretizationGaussNE::LOC_QUAD8[16]={-0.774596669241483,-0.774596669241483,0.774596669241483,-0.774596669241483,0.774596669241483,0.774596669241483,-0.774596669241483,0.774596669241483,0.,-0.774596669241483,0.774596669241483,0.,0.,0.774596669241483,-0.774596669241483,0.};
113 const double MEDCouplingFieldDiscretizationGaussNE::LOC_QUAD9[18]={-0.774596669241483,-0.774596669241483,0.774596669241483,-0.774596669241483,0.774596669241483,0.774596669241483,-0.774596669241483,0.774596669241483,0.,-0.774596669241483,0.774596669241483,0.,0.,0.774596669241483,-0.774596669241483,0.,0.,0.};
114 const double MEDCouplingFieldDiscretizationGaussNE::LOC_TETRA4[12]={0.1381966011250105,0.1381966011250105,0.1381966011250105,0.1381966011250105,0.1381966011250105,0.5854101966249685,0.1381966011250105,0.5854101966249685,0.1381966011250105,0.5854101966249685,0.1381966011250105,0.1381966011250105};
115 const double MEDCouplingFieldDiscretizationGaussNE::LOC_TETRA10[30]={0.,1.,0.,0.,0.,1.,0.,0.,0.,1.,0.,0.,0.,0.5,0.5,0.,0.,0.5,0.,0.5,0.,0.5,0.5,0.,0.5,0.,0.5,0.5,0.,0.};//to check
116 const double MEDCouplingFieldDiscretizationGaussNE::LOC_PENTA6[18]={-0.5773502691896258,0.5,0.5,-0.5773502691896258,0.,0.5,-0.5773502691896258,0.5,0.,0.5773502691896258,0.5,0.5,0.5773502691896258,0.,0.5,0.5773502691896258,0.5,0.};
117 const double MEDCouplingFieldDiscretizationGaussNE::LOC_PENTA15[45]={-1.,1.,0.,-1.,0.,1.,-1.,0.,0.,1.,1.,0.,1.,0.,1.,1.,0.,0.,-1.,0.5,0.5,-1.,0.,0.5,-1.,0.5,0.,0.,1.,0.,0.,0.,1.,0.,0.,0.,1.,0.5,0.5,1.,0.,0.5,1.,0.5,0.};//to check
118 const double MEDCouplingFieldDiscretizationGaussNE::LOC_HEXA8[24]={-0.5773502691896258,-0.5773502691896258,-0.5773502691896258,-0.5773502691896258,-0.5773502691896258,0.5773502691896258,-0.5773502691896258,0.5773502691896258,-0.5773502691896258,-0.5773502691896258,0.5773502691896258,0.5773502691896258,0.5773502691896258,-0.5773502691896258,-0.5773502691896258,0.5773502691896258,-0.5773502691896258,0.5773502691896258,0.5773502691896258,0.5773502691896258,-0.5773502691896258,0.5773502691896258,0.5773502691896258,0.5773502691896258};
119 const double MEDCouplingFieldDiscretizationGaussNE::LOC_HEXA20[60]={-1.,-1.,-1.,1.,-1.,-1.,1.,1.,-1.,-1.,1.,-1.,-1.,-1.,1.,1.,-1.,1.,1.,1.,1.,-1.,1.,1.,0.,-1.,-1.,1.,0.,-1.,0.,1.,-1.,-1.,0.,-1.,-1.,-1.,0.,1.,-1.,0.,1.,1.,0.,-1.,1.,0.,0.,-1.,1.,1.,0.,1.,0.,1.,1.,-1.,0.,1.};//to check
120 const double MEDCouplingFieldDiscretizationGaussNE::LOC_HEXA27[81]={-1.,-1.,-1.,-1.,1.,-1.,1.,1.,-1.,1.,-1.,-1.,-1.,-1.,1.,-1.,1.,1.,1.,1.,1.,1.,-1.,1.,-1.,0.,-1.,0.,1.,-1.,1.,0.,-1.,0.,-1.,-1.,-1.,0.,1.,0.,1.,1.,1.,0.,1.,0.,-1.,1.,-1.,-1.,0.,-1.,1.,0.,1.,1.,0.,1.,-1.,0.,0.,0.,-1.,-1.,0.,0.,0.,1.,0.,1.,0.,0.,0.,-1.,0.,0.,0.,1.,0.,0.,0.};
121 const double MEDCouplingFieldDiscretizationGaussNE::LOC_PYRA5[15]={0.5,0.,0.1531754163448146,0.,0.5,0.1531754163448146,-0.5,0.,0.1531754163448146,0.,-0.5,0.1531754163448146,0.,0.,0.6372983346207416};
122 const double MEDCouplingFieldDiscretizationGaussNE::LOC_PYRA13[39]={1.,0.,0.,0.,-1.,0.,-1.,0.,0.,0.,1.,0.,0.,0.,0.999999999999,0.5,-0.5,0.,-0.5,-0.5,0.,-0.5,0.5,0.,0.5,0.5,0.,0.5,0.,0.5,0.,-0.5,0.5,-0.5,0.,0.5,0.,0.5,0.5};//to check 0.99999... to avoid nan ! on node #4 of PYRA13
123
124 MEDCouplingFieldDiscretization::MEDCouplingFieldDiscretization():_precision(DFLT_PRECISION)
125 {
126 }
127
128 MEDCouplingFieldDiscretization *MEDCouplingFieldDiscretization::New(TypeOfField type)
129 {
130   switch(type)
131   {
132     case MEDCouplingFieldDiscretizationP0::TYPE:
133       return new MEDCouplingFieldDiscretizationP0;
134     case MEDCouplingFieldDiscretizationP1::TYPE:
135       return new MEDCouplingFieldDiscretizationP1;
136     case MEDCouplingFieldDiscretizationGauss::TYPE:
137       return new MEDCouplingFieldDiscretizationGauss;
138     case MEDCouplingFieldDiscretizationGaussNE::TYPE:
139       return new MEDCouplingFieldDiscretizationGaussNE;
140     case MEDCouplingFieldDiscretizationKriging::TYPE:
141       return new MEDCouplingFieldDiscretizationKriging;
142     default:
143       throw INTERP_KERNEL::Exception("Choosen discretization is not implemented yet.");
144   }
145 }
146
147 TypeOfField MEDCouplingFieldDiscretization::GetTypeOfFieldFromStringRepr(const std::string& repr)
148 {
149   if(repr==MEDCouplingFieldDiscretizationP0::REPR)
150     return MEDCouplingFieldDiscretizationP0::TYPE;
151   if(repr==MEDCouplingFieldDiscretizationP1::REPR)
152     return MEDCouplingFieldDiscretizationP1::TYPE;
153   if(repr==MEDCouplingFieldDiscretizationGauss::REPR)
154     return MEDCouplingFieldDiscretizationGauss::TYPE;
155   if(repr==MEDCouplingFieldDiscretizationGaussNE::REPR)
156     return MEDCouplingFieldDiscretizationGaussNE::TYPE;
157   if(repr==MEDCouplingFieldDiscretizationKriging::REPR)
158     return MEDCouplingFieldDiscretizationKriging::TYPE;
159   throw INTERP_KERNEL::Exception("Representation does not match with any field discretization !");
160 }
161
162 std::string MEDCouplingFieldDiscretization::GetTypeOfFieldRepr(TypeOfField type)
163 {
164   if(type==MEDCouplingFieldDiscretizationP0::TYPE)
165     return MEDCouplingFieldDiscretizationP0::REPR;
166   if(type==MEDCouplingFieldDiscretizationP1::TYPE)
167     return MEDCouplingFieldDiscretizationP1::REPR;
168   if(type==MEDCouplingFieldDiscretizationGauss::TYPE)
169     return MEDCouplingFieldDiscretizationGauss::REPR;
170   if(type==MEDCouplingFieldDiscretizationGaussNE::TYPE)
171     return MEDCouplingFieldDiscretizationGaussNE::REPR;
172   if(type==MEDCouplingFieldDiscretizationKriging::TYPE)
173     return MEDCouplingFieldDiscretizationKriging::REPR;
174   throw INTERP_KERNEL::Exception("GetTypeOfFieldRepr : Representation does not match with any field discretization !");
175 }
176
177 bool MEDCouplingFieldDiscretization::isEqual(const MEDCouplingFieldDiscretization *other, double eps) const
178 {
179   std::string reason;
180   return isEqualIfNotWhy(other,eps,reason);
181 }
182
183 bool MEDCouplingFieldDiscretization::isEqualWithoutConsideringStr(const MEDCouplingFieldDiscretization *other, double eps) const
184 {
185   return isEqual(other,eps);
186 }
187
188 /*!
189  * This method is an alias of MEDCouplingFieldDiscretization::clone. It is only here for coherency with all the remaining of MEDCoupling.
190  * \sa MEDCouplingFieldDiscretization::clone.
191  */
192 MEDCouplingFieldDiscretization *MEDCouplingFieldDiscretization::deepCopy() const
193 {
194   return clone();
195 }
196
197 /*!
198  * For all field discretization excepted GaussPts the [ \a startCellIds, \a endCellIds ) has no impact on the cloned instance.
199  */
200 MEDCouplingFieldDiscretization *MEDCouplingFieldDiscretization::clonePart(const int *startCellIds, const int *endCellIds) const
201 {
202   return clone();
203 }
204
205 /*!
206  * For all field discretization excepted GaussPts the slice( \a beginCellId, \a endCellIds, \a stepCellId ) has no impact on the cloned instance.
207  */
208 MEDCouplingFieldDiscretization *MEDCouplingFieldDiscretization::clonePartRange(int beginCellIds, int endCellIds, int stepCellIds) const
209 {
210   return clone();
211 }
212
213 /*!
214  * Excepted for MEDCouplingFieldDiscretizationPerCell no underlying TimeLabel object : nothing to do in generally.
215  */
216 void MEDCouplingFieldDiscretization::updateTime() const
217 {
218 }
219
220 std::size_t MEDCouplingFieldDiscretization::getHeapMemorySizeWithoutChildren() const
221 {
222   return 0;
223 }
224
225 std::vector<const BigMemoryObject *> MEDCouplingFieldDiscretization::getDirectChildrenWithNull() const
226 {
227   return std::vector<const BigMemoryObject *>();
228 }
229
230 /*!
231  * Computes normL1 of DataArrayDouble instance arr.
232  * @param res output parameter expected to be of size arr->getNumberOfComponents();
233  * @throw when the field discretization fails on getMeasure fields (gauss points for example)
234  */
235 void MEDCouplingFieldDiscretization::normL1(const MEDCouplingMesh *mesh, const DataArrayDouble *arr, double *res) const
236 {
237   MCAuto<MEDCouplingFieldDouble> vol=getMeasureField(mesh,true);
238   int nbOfCompo=arr->getNumberOfComponents();
239   int nbOfElems=getNumberOfTuples(mesh);
240   std::fill(res,res+nbOfCompo,0.);
241   const double *arrPtr=arr->getConstPointer();
242   const double *volPtr=vol->getArray()->getConstPointer();
243   double deno=0.;
244   for(int i=0;i<nbOfElems;i++)
245     {
246       double v=fabs(volPtr[i]);
247       for(int j=0;j<nbOfCompo;j++)
248         res[j]+=fabs(arrPtr[i*nbOfCompo+j])*v;
249       deno+=v;
250     }
251   std::transform(res,res+nbOfCompo,res,std::bind2nd(std::multiplies<double>(),1./deno));
252 }
253
254 /*!
255  * Computes normL2 of DataArrayDouble instance arr.
256  * @param res output parameter expected to be of size arr->getNumberOfComponents();
257  * @throw when the field discretization fails on getMeasure fields (gauss points for example)
258  */
259 void MEDCouplingFieldDiscretization::normL2(const MEDCouplingMesh *mesh, const DataArrayDouble *arr, double *res) const
260 {
261   MCAuto<MEDCouplingFieldDouble> vol=getMeasureField(mesh,true);
262   int nbOfCompo=arr->getNumberOfComponents();
263   int nbOfElems=getNumberOfTuples(mesh);
264   std::fill(res,res+nbOfCompo,0.);
265   const double *arrPtr=arr->getConstPointer();
266   const double *volPtr=vol->getArray()->getConstPointer();
267   double deno=0.;
268   for(int i=0;i<nbOfElems;i++)
269     {
270       double v=fabs(volPtr[i]);
271       for(int j=0;j<nbOfCompo;j++)
272         res[j]+=arrPtr[i*nbOfCompo+j]*arrPtr[i*nbOfCompo+j]*v;
273       deno+=v;
274     }
275   std::transform(res,res+nbOfCompo,res,std::bind2nd(std::multiplies<double>(),1./deno));
276   std::transform(res,res+nbOfCompo,res,std::ptr_fun<double,double>(std::sqrt));
277 }
278
279 /*!
280  * Computes integral of DataArrayDouble instance arr.
281  * @param res output parameter expected to be of size arr->getNumberOfComponents();
282  * @throw when the field discretization fails on getMeasure fields (gauss points for example)
283  */
284 void MEDCouplingFieldDiscretization::integral(const MEDCouplingMesh *mesh, const DataArrayDouble *arr, bool isWAbs, double *res) const
285 {
286   if(!mesh)
287     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretization::integral : mesh is NULL !");
288   if(!arr)
289     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretization::integral : input array is NULL !");
290   MCAuto<MEDCouplingFieldDouble> vol=getMeasureField(mesh,isWAbs);
291   int nbOfCompo=arr->getNumberOfComponents();
292   int nbOfElems=getNumberOfTuples(mesh);
293   if(nbOfElems!=arr->getNumberOfTuples())
294     {
295       std::ostringstream oss; oss << "MEDCouplingFieldDiscretization::integral : field is not correct ! number of tuples in array is " << arr->getNumberOfTuples();
296       oss << " whereas number of tuples expected is " << nbOfElems << " !";
297       throw INTERP_KERNEL::Exception(oss.str().c_str());
298     }
299   std::fill(res,res+nbOfCompo,0.);
300   const double *arrPtr=arr->getConstPointer();
301   const double *volPtr=vol->getArray()->getConstPointer();
302   INTERP_KERNEL::AutoPtr<double> tmp=new double[nbOfCompo];
303   for (int i=0;i<nbOfElems;i++)
304     {
305       std::transform(arrPtr+i*nbOfCompo,arrPtr+(i+1)*nbOfCompo,(double *)tmp,std::bind2nd(std::multiplies<double>(),volPtr[i]));
306       std::transform((double *)tmp,(double *)tmp+nbOfCompo,res,res,std::plus<double>());
307     }
308 }
309
310 /*!
311  * This method is strictly equivalent to MEDCouplingFieldDiscretization::buildSubMeshData except that it is optimized for input defined as a range of cell ids.
312  * 
313  * \param [out] beginOut Valid only if \a di is NULL
314  * \param [out] endOut Valid only if \a di is NULL
315  * \param [out] stepOut Valid only if \a di is NULL
316  * \param [out] di is an array returned that specifies entity ids (nodes, cells, Gauss points... ) in array if no output range is foundable.
317  *
318  * \sa MEDCouplingFieldDiscretization::buildSubMeshData
319  */
320 MEDCouplingMesh *MEDCouplingFieldDiscretization::buildSubMeshDataRange(const MEDCouplingMesh *mesh, int beginCellIds, int endCellIds, int stepCellIds, int& beginOut, int& endOut, int& stepOut, DataArrayInt *&di) const
321 {
322   MCAuto<DataArrayInt> da=DataArrayInt::Range(beginCellIds,endCellIds,stepCellIds);
323   return buildSubMeshData(mesh,da->begin(),da->end(),di);
324 }
325
326 void MEDCouplingFieldDiscretization::getSerializationIntArray(DataArrayInt *& arr) const
327 {
328   arr=0;
329 }
330
331 /*!
332  * Empty : Not a bug
333  */
334 void MEDCouplingFieldDiscretization::getTinySerializationIntInformation(std::vector<int>& tinyInfo) const
335 {
336 }
337
338 /*!
339  * Empty : Not a bug
340  */
341 void MEDCouplingFieldDiscretization::getTinySerializationDbleInformation(std::vector<double>& tinyInfo) const
342 {
343 }
344
345 void MEDCouplingFieldDiscretization::resizeForUnserialization(const std::vector<int>& tinyInfo, DataArrayInt *& arr)
346 {
347   arr=0;
348 }
349
350 /*!
351  * Empty : Not a bug
352  */
353 void MEDCouplingFieldDiscretization::checkForUnserialization(const std::vector<int>& tinyInfo, const DataArrayInt *arr)
354 {
355 }
356
357 /*!
358  * Empty : Not a bug
359  */
360 void MEDCouplingFieldDiscretization::finishUnserialization(const std::vector<double>& tinyInfo)
361 {
362 }
363
364 /*!
365  * This method is typically the first step of renumbering. The implementation is empty it is not a bug only gauss is impacted
366  * virtualy by this method.
367  */
368 void MEDCouplingFieldDiscretization::renumberCells(const int *old2NewBg, bool check)
369 {
370 }
371
372 double MEDCouplingFieldDiscretization::getIJK(const MEDCouplingMesh *mesh, const DataArrayDouble *da, int cellId, int nodeIdInCell, int compoId) const
373 {
374   throw INTERP_KERNEL::Exception("getIJK Invalid ! only for GaussPoint and GaussNE discretizations !");
375 }
376
377 void MEDCouplingFieldDiscretization::setGaussLocalizationOnType(const MEDCouplingMesh *m, INTERP_KERNEL::NormalizedCellType type, const std::vector<double>& refCoo,
378                                                                 const std::vector<double>& gsCoo, const std::vector<double>& wg)
379 {
380   throw INTERP_KERNEL::Exception("Invalid method for the corresponding field discretization : available only for GaussPoint discretization !");
381 }
382
383 void MEDCouplingFieldDiscretization::setGaussLocalizationOnCells(const MEDCouplingMesh *m, const int *begin, const int *end, const std::vector<double>& refCoo,
384                                                                  const std::vector<double>& gsCoo, const std::vector<double>& wg)
385 {
386   throw INTERP_KERNEL::Exception("Invalid method for the corresponding field discretization : available only for GaussPoint discretization !");
387 }
388
389 void MEDCouplingFieldDiscretization::clearGaussLocalizations()
390 {
391   throw INTERP_KERNEL::Exception("Invalid method for the corresponding field discretization : available only for GaussPoint discretization !");
392 }
393
394 MEDCouplingGaussLocalization& MEDCouplingFieldDiscretization::getGaussLocalization(int locId)
395 {
396   throw INTERP_KERNEL::Exception("Invalid method for the corresponding field discretization : available only for GaussPoint discretization !");
397 }
398
399 const MEDCouplingGaussLocalization& MEDCouplingFieldDiscretization::getGaussLocalization(int locId) const
400 {
401   throw INTERP_KERNEL::Exception("Invalid method for the corresponding field discretization : available only for GaussPoint discretization !");
402 }
403
404 int MEDCouplingFieldDiscretization::getNbOfGaussLocalization() const
405 {
406   throw INTERP_KERNEL::Exception("Invalid method for the corresponding field discretization : available only for GaussPoint discretization !");
407 }
408
409 int MEDCouplingFieldDiscretization::getGaussLocalizationIdOfOneCell(int cellId) const
410 {
411   throw INTERP_KERNEL::Exception("Invalid method for the corresponding field discretization : available only for GaussPoint discretization !");
412 }
413
414 int MEDCouplingFieldDiscretization::getGaussLocalizationIdOfOneType(INTERP_KERNEL::NormalizedCellType type) const
415 {
416   throw INTERP_KERNEL::Exception("Invalid method for the corresponding field discretization : available only for GaussPoint discretization !");
417 }
418
419 std::set<int> MEDCouplingFieldDiscretization::getGaussLocalizationIdsOfOneType(INTERP_KERNEL::NormalizedCellType type) const
420 {
421   throw INTERP_KERNEL::Exception("Invalid method for the corresponding field discretization : available only for GaussPoint discretization !");
422 }
423
424 void MEDCouplingFieldDiscretization::getCellIdsHavingGaussLocalization(int locId, std::vector<int>& cellIds) const
425 {
426   throw INTERP_KERNEL::Exception("Invalid method for the corresponding field discretization : available only for GaussPoint discretization !");
427 }
428
429 void MEDCouplingFieldDiscretization::RenumberEntitiesFromO2NArr(double eps, const int *old2NewPtr, int newNbOfEntity, DataArrayDouble *arr, const std::string& msg)
430 {
431   if(!arr)
432     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretization::RenumberEntitiesFromO2NArr : input array is NULL !");
433   int oldNbOfElems=arr->getNumberOfTuples();
434   int nbOfComp=arr->getNumberOfComponents();
435   int newNbOfTuples=newNbOfEntity;
436   MCAuto<DataArrayDouble> arrCpy=arr->deepCopy();
437   const double *ptSrc=arrCpy->getConstPointer();
438   arr->reAlloc(newNbOfTuples);
439   double *ptToFill=arr->getPointer();
440   std::fill(ptToFill,ptToFill+nbOfComp*newNbOfTuples,std::numeric_limits<double>::max());
441   INTERP_KERNEL::AutoPtr<double> tmp=new double[nbOfComp];
442   for(int i=0;i<oldNbOfElems;i++)
443     {
444       int newNb=old2NewPtr[i];
445       if(newNb>=0)//if newNb<0 the node is considered as out.
446         {
447           if(std::find_if(ptToFill+newNb*nbOfComp,ptToFill+(newNb+1)*nbOfComp,std::bind2nd(std::not_equal_to<double>(),std::numeric_limits<double>::max()))
448           ==ptToFill+(newNb+1)*nbOfComp)
449             std::copy(ptSrc+i*nbOfComp,ptSrc+(i+1)*nbOfComp,ptToFill+newNb*nbOfComp);
450           else
451             {
452               std::transform(ptSrc+i*nbOfComp,ptSrc+(i+1)*nbOfComp,ptToFill+newNb*nbOfComp,(double *)tmp,std::minus<double>());
453               std::transform((double *)tmp,((double *)tmp)+nbOfComp,(double *)tmp,std::ptr_fun<double,double>(fabs));
454               //if(!std::equal(ptSrc+i*nbOfComp,ptSrc+(i+1)*nbOfComp,ptToFill+newNb*nbOfComp))
455               if(*std::max_element((double *)tmp,((double *)tmp)+nbOfComp)>eps)
456                 {
457                   std::ostringstream oss;
458                   oss << msg << " " << i << " and " << std::find(old2NewPtr,old2NewPtr+i,newNb)-old2NewPtr
459                       << " have been merged and " << msg << " field on them are different !";
460                   throw INTERP_KERNEL::Exception(oss.str().c_str());
461                 }
462             }
463         }
464     }
465 }
466
467 void MEDCouplingFieldDiscretization::RenumberEntitiesFromN2OArr(const int *new2OldPtr, int new2OldSz, DataArrayDouble *arr, const std::string& msg)
468 {
469   int nbOfComp=arr->getNumberOfComponents();
470   MCAuto<DataArrayDouble> arrCpy=arr->deepCopy();
471   const double *ptSrc=arrCpy->getConstPointer();
472   arr->reAlloc(new2OldSz);
473   double *ptToFill=arr->getPointer();
474   for(int i=0;i<new2OldSz;i++)
475     {
476       int oldNb=new2OldPtr[i];
477       std::copy(ptSrc+oldNb*nbOfComp,ptSrc+(oldNb+1)*nbOfComp,ptToFill+i*nbOfComp);
478     }
479 }
480
481 MEDCouplingFieldDiscretization::~MEDCouplingFieldDiscretization()
482 {
483 }
484
485 TypeOfField MEDCouplingFieldDiscretizationP0::getEnum() const
486 {
487   return TYPE;
488 }
489
490 /*!
491  * This method is simply called by MEDCouplingFieldDiscretization::deepCopy. It performs the deep copy of \a this.
492  *
493  * \sa MEDCouplingFieldDiscretization::deepCopy.
494  */
495 MEDCouplingFieldDiscretization *MEDCouplingFieldDiscretizationP0::clone() const
496 {
497   return new MEDCouplingFieldDiscretizationP0;
498 }
499
500 std::string MEDCouplingFieldDiscretizationP0::getStringRepr() const
501 {
502   return std::string(REPR);
503 }
504
505 const char *MEDCouplingFieldDiscretizationP0::getRepr() const
506 {
507   return REPR;
508 }
509
510 bool MEDCouplingFieldDiscretizationP0::isEqualIfNotWhy(const MEDCouplingFieldDiscretization *other, double eps, std::string& reason) const
511 {
512   if(!other)
513     {
514       reason="other spatial discretization is NULL, and this spatial discretization (P0) is defined.";
515       return false;
516     }
517   const MEDCouplingFieldDiscretizationP0 *otherC=dynamic_cast<const MEDCouplingFieldDiscretizationP0 *>(other);
518   bool ret=otherC!=0;
519   if(!ret)
520     reason="Spatial discrtization of this is ON_CELLS, which is not the case of other.";
521   return ret;
522 }
523
524 int MEDCouplingFieldDiscretizationP0::getNumberOfTuples(const MEDCouplingMesh *mesh) const
525 {
526   if(!mesh)
527     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::getNumberOfTuples : NULL input mesh !");
528   return mesh->getNumberOfCells();
529 }
530
531 /*!
532  * This method returns the number of tuples regarding exclusively the input code \b without \b using \b a \b mesh \b in \b input.
533  * The input code coherency is also checked regarding spatial discretization of \a this.
534  * If an incoherency is detected, an exception will be thrown. If the input code is coherent, the number of tuples expected is returned.
535  * The number of tuples expected is equal to those to have a valid field lying on \a this and having a mesh fitting perfectly the input code (geometric type distribution).
536  */
537 int MEDCouplingFieldDiscretizationP0::getNumberOfTuplesExpectedRegardingCode(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const
538 {
539   if(code.size()%3!=0)
540     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::getNumberOfTuplesExpectedRegardingCode : invalid input code !");
541   int nbOfSplit=(int)idsPerType.size();
542   int nbOfTypes=(int)code.size()/3;
543   int ret=0;
544   for(int i=0;i<nbOfTypes;i++)
545     {
546       int nbOfEltInChunk=code[3*i+1];
547       if(nbOfEltInChunk<0)
548         throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::getNumberOfTuplesExpectedRegardingCode : invalid input code ! presence of negative value in a type !");
549       int pos=code[3*i+2];
550       if(pos!=-1)
551         {
552           if(pos<0 || pos>=nbOfSplit)
553             {
554               std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationP0::getNumberOfTuplesExpectedRegardingCode : input code points to pos " << pos << " in typeid " << i << " ! Should be in [0," << nbOfSplit << ") !";
555               throw INTERP_KERNEL::Exception(oss.str().c_str());
556             }
557           const DataArrayInt *ids(idsPerType[pos]);
558           if(!ids || !ids->isAllocated() || ids->getNumberOfComponents()!=1 || ids->getNumberOfTuples()!=nbOfEltInChunk || ids->getMinValueInArray()<0)
559             {
560               std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationP0::getNumberOfTuplesExpectedRegardingCode : input pfl chunck at pos " << pos << " should have " << i << " tuples and one component and with ids all >=0 !";
561               throw INTERP_KERNEL::Exception(oss.str().c_str());
562             }
563         }
564       ret+=nbOfEltInChunk;
565     }
566   return ret;
567 }
568
569 int MEDCouplingFieldDiscretizationP0::getNumberOfMeshPlaces(const MEDCouplingMesh *mesh) const
570 {
571   if(!mesh)
572     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::getNumberOfMeshPlaces : NULL input mesh !");
573   return mesh->getNumberOfCells();
574 }
575
576 DataArrayInt *MEDCouplingFieldDiscretizationP0::getOffsetArr(const MEDCouplingMesh *mesh) const
577 {
578   if(!mesh)
579     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::getOffsetArr : NULL input mesh !");
580   int nbOfTuples=mesh->getNumberOfCells();
581   DataArrayInt *ret=DataArrayInt::New();
582   ret->alloc(nbOfTuples+1,1);
583   ret->iota(0);
584   return ret;
585 }
586
587 void MEDCouplingFieldDiscretizationP0::renumberArraysForCell(const MEDCouplingMesh *mesh, const std::vector<DataArray *>& arrays,
588                                                              const int *old2NewBg, bool check)
589 {
590   if(!mesh)
591     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::renumberArraysForCell : NULL input mesh !");
592   const int *array=old2NewBg;
593   if(check)
594     array=DataArrayInt::CheckAndPreparePermutation(old2NewBg,old2NewBg+mesh->getNumberOfCells());
595   for(std::vector<DataArray *>::const_iterator it=arrays.begin();it!=arrays.end();it++)
596     {
597       if(*it)
598         (*it)->renumberInPlace(array);
599     }
600   if(check)
601     free(const_cast<int *>(array));
602 }
603
604 DataArrayDouble *MEDCouplingFieldDiscretizationP0::getLocalizationOfDiscValues(const MEDCouplingMesh *mesh) const
605 {
606   if(!mesh)
607     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::getLocalizationOfDiscValues : NULL input mesh !");
608   return mesh->computeCellCenterOfMass();
609 }
610
611 void MEDCouplingFieldDiscretizationP0::computeMeshRestrictionFromTupleIds(const MEDCouplingMesh *mesh, const int *tupleIdsBg, const int *tupleIdsEnd,
612                                                                           DataArrayInt *&cellRestriction, DataArrayInt *&trueTupleRestriction) const
613 {
614   if(!mesh)
615     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::computeMeshRestrictionFromTupleIds : NULL input mesh !");
616   MCAuto<DataArrayInt> tmp=DataArrayInt::New();
617   tmp->alloc((int)std::distance(tupleIdsBg,tupleIdsEnd),1);
618   std::copy(tupleIdsBg,tupleIdsEnd,tmp->getPointer());
619   MCAuto<DataArrayInt> tmp2(tmp->deepCopy());
620   cellRestriction=tmp.retn();
621   trueTupleRestriction=tmp2.retn();
622 }
623
624 void MEDCouplingFieldDiscretizationP0::reprQuickOverview(std::ostream& stream) const
625 {
626   stream << "P0 spatial discretization.";
627 }
628
629 void MEDCouplingFieldDiscretizationP0::checkCompatibilityWithNature(NatureOfField nat) const
630 {
631 }
632
633 void MEDCouplingFieldDiscretizationP0::checkCoherencyBetween(const MEDCouplingMesh *mesh, const DataArray *da) const
634 {
635   if(!mesh || !da)
636     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::checkCoherencyBetween : NULL input mesh or DataArray !");
637   if(mesh->getNumberOfCells()!=da->getNumberOfTuples())
638     {
639       std::ostringstream message;
640       message << "Field on cells invalid because there are " << mesh->getNumberOfCells();
641       message << " cells in mesh and " << da->getNumberOfTuples() << " tuples in field !";
642       throw INTERP_KERNEL::Exception(message.str().c_str());
643     }
644 }
645
646 MEDCouplingFieldDouble *MEDCouplingFieldDiscretizationP0::getMeasureField(const MEDCouplingMesh *mesh, bool isAbs) const
647 {
648   if(!mesh)
649     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::getMeasureField : mesh instance specified is NULL !");
650   return mesh->getMeasureField(isAbs);
651 }
652
653 void MEDCouplingFieldDiscretizationP0::getValueOn(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, const double *loc, double *res) const
654 {
655   if(!mesh)
656     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::getValueOn : NULL input mesh !");
657   int id=mesh->getCellContainingPoint(loc,_precision);
658   if(id==-1)
659     throw INTERP_KERNEL::Exception("Specified point is detected outside of mesh : unable to apply P0::getValueOn !");
660   arr->getTuple(id,res);
661 }
662
663 void MEDCouplingFieldDiscretizationP0::getValueOnPos(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, int i, int j, int k, double *res) const
664 {
665   const MEDCouplingCMesh *meshC=dynamic_cast<const MEDCouplingCMesh *>(mesh);
666   if(!meshC)
667     throw INTERP_KERNEL::Exception("P0::getValueOnPos is only accessible for structured meshes !");
668   int id=meshC->getCellIdFromPos(i,j,k);
669   arr->getTuple(id,res);
670 }
671
672 DataArrayDouble *MEDCouplingFieldDiscretizationP0::getValueOnMulti(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, const double *loc, int nbOfPoints) const
673 {
674   if(!mesh)
675     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::getValueOnMulti : NULL input mesh !");
676   MCAuto<DataArrayInt> eltsArr,eltsIndexArr;
677   mesh->getCellsContainingPoints(loc,nbOfPoints,_precision,eltsArr,eltsIndexArr);
678   const int *elts(eltsArr->begin()),*eltsIndex(eltsIndexArr->begin());
679   int spaceDim=mesh->getSpaceDimension();
680   int nbOfComponents=arr->getNumberOfComponents();
681   MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
682   ret->alloc(nbOfPoints,nbOfComponents);
683   double *ptToFill=ret->getPointer();
684   for(int i=0;i<nbOfPoints;i++,ptToFill+=nbOfComponents)
685     if(eltsIndex[i+1]-eltsIndex[i]>=1)
686       arr->getTuple(elts[eltsIndex[i]],ptToFill);
687     else
688       {
689         std::ostringstream oss; oss << "Point #" << i << " with coordinates : (";
690         std::copy(loc+i*spaceDim,loc+(i+1)*spaceDim,std::ostream_iterator<double>(oss,", "));
691         oss << ") detected outside mesh : unable to apply P0::getValueOnMulti ! ";
692         throw INTERP_KERNEL::Exception(oss.str().c_str());
693       }
694   return ret.retn();
695 }
696
697 /*!
698  * Nothing to do. It's not a bug.
699  */
700 void MEDCouplingFieldDiscretizationP0::renumberValuesOnNodes(double , const int *, int newNbOfNodes, DataArrayDouble *) const
701 {
702 }
703
704 void MEDCouplingFieldDiscretizationP0::renumberValuesOnCells(double epsOnVals, const MEDCouplingMesh *mesh, const int *old2New, int newSz, DataArrayDouble *arr) const
705 {
706   RenumberEntitiesFromO2NArr(epsOnVals,old2New,newSz,arr,"Cell");
707 }
708
709 void MEDCouplingFieldDiscretizationP0::renumberValuesOnCellsR(const MEDCouplingMesh *mesh, const int *new2old, int newSz, DataArrayDouble *arr) const
710 {
711   RenumberEntitiesFromN2OArr(new2old,newSz,arr,"Cell");
712 }
713
714 /*!
715  * This method returns a tuple ids selection from cell ids selection [start;end).
716  * This method is called by MEDCouplingFieldDiscretizationP0::buildSubMeshData to return parameter \b di.
717  * Here for P0 it's very simple !
718  *
719  * \return a newly allocated array containing ids to select into the DataArrayDouble of the field.
720  * 
721  */
722 DataArrayInt *MEDCouplingFieldDiscretizationP0::computeTupleIdsToSelectFromCellIds(const MEDCouplingMesh *mesh, const int *startCellIds, const int *endCellIds) const
723 {
724   MCAuto<DataArrayInt> ret=DataArrayInt::New();
725   ret->alloc((int)std::distance(startCellIds,endCellIds),1);
726   std::copy(startCellIds,endCellIds,ret->getPointer());
727   return ret.retn();
728 }
729
730 /*!
731  * This method returns a submesh of 'mesh' instance constituting cell ids contained in array defined as an interval [start;end).
732  * @param di is an array returned that specifies entity ids (here cells ids) in mesh 'mesh' of entity in returned submesh.
733  * Example : The first cell id of returned mesh has the (*di)[0] id in 'mesh'
734  *
735  * \sa MEDCouplingFieldDiscretizationP0::buildSubMeshDataRange
736  */
737 MEDCouplingMesh *MEDCouplingFieldDiscretizationP0::buildSubMeshData(const MEDCouplingMesh *mesh, const int *start, const int *end, DataArrayInt *&di) const
738 {
739   if(!mesh)
740     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::buildSubMeshData : NULL input mesh !");
741   MCAuto<MEDCouplingMesh> ret=mesh->buildPart(start,end);
742   MCAuto<DataArrayInt> diSafe=DataArrayInt::New();
743   diSafe->alloc((int)std::distance(start,end),1);
744   std::copy(start,end,diSafe->getPointer());
745   di=diSafe.retn();
746   return ret.retn();
747 }
748
749 /*!
750  * This method is strictly equivalent to MEDCouplingFieldDiscretizationP0::buildSubMeshData except that it is optimized for input defined as a range of cell ids.
751  * 
752  * \param [out] beginOut Valid only if \a di is NULL
753  * \param [out] endOut Valid only if \a di is NULL
754  * \param [out] stepOut Valid only if \a di is NULL
755  * \param [out] di is an array returned that specifies entity ids (nodes, cells, Gauss points... ) in array if no output range is foundable.
756  *
757  * \sa MEDCouplingFieldDiscretizationP0::buildSubMeshData
758  */
759 MEDCouplingMesh *MEDCouplingFieldDiscretizationP0::buildSubMeshDataRange(const MEDCouplingMesh *mesh, int beginCellIds, int endCellIds, int stepCellIds, int& beginOut, int& endOut, int& stepOut, DataArrayInt *&di) const
760 {
761   if(!mesh)
762     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP0::buildSubMeshDataRange : NULL input mesh !");
763   MCAuto<MEDCouplingMesh> ret=mesh->buildPartRange(beginCellIds,endCellIds,stepCellIds);
764   di=0; beginOut=beginCellIds; endOut=endCellIds; stepOut=stepCellIds;
765   return ret.retn();
766 }
767
768 int MEDCouplingFieldDiscretizationOnNodes::getNumberOfTuples(const MEDCouplingMesh *mesh) const
769 {
770   if(!mesh)
771     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationNodes::getNumberOfTuples : NULL input mesh !");
772   return mesh->getNumberOfNodes();
773 }
774
775 /*!
776  * This method returns the number of tuples regarding exclusively the input code \b without \b using \b a \b mesh \b in \b input.
777  * The input code coherency is also checked regarding spatial discretization of \a this.
778  * If an incoherency is detected, an exception will be thrown. If the input code is coherent, the number of tuples expected is returned.
779  * The number of tuples expected is equal to those to have a valid field lying on \a this and having a mesh fitting perfectly the input code (geometric type distribution).
780  */
781 int MEDCouplingFieldDiscretizationOnNodes::getNumberOfTuplesExpectedRegardingCode(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const
782 {
783   if(code.size()%3!=0)
784     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationOnNodes::getNumberOfTuplesExpectedRegardingCode : invalid input code !");
785   int nbOfSplit=(int)idsPerType.size();
786   int nbOfTypes=(int)code.size()/3;
787   int ret=0;
788   for(int i=0;i<nbOfTypes;i++)
789     {
790       int nbOfEltInChunk=code[3*i+1];
791       if(nbOfEltInChunk<0)
792         throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationOnNodes::getNumberOfTuplesExpectedRegardingCode : invalid input code ! presence of negative value in a type !");
793       int pos=code[3*i+2];
794       if(pos!=-1)
795         {
796           if(pos<0 || pos>=nbOfSplit)
797             {
798               std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationOnNodes::getNumberOfTuplesExpectedRegardingCode : input code points to pos " << pos << " in typeid " << i << " ! Should be in [0," << nbOfSplit << ") !";
799               throw INTERP_KERNEL::Exception(oss.str().c_str());
800             }
801           const DataArrayInt *ids(idsPerType[pos]);
802           if(!ids || !ids->isAllocated() || ids->getNumberOfComponents()!=1 || ids->getNumberOfTuples()!=nbOfEltInChunk || ids->getMinValueInArray()<0)
803             {
804               std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationOnNodes::getNumberOfTuplesExpectedRegardingCode : input pfl chunck at pos " << pos << " should have " << i << " tuples and one component and with ids all >=0 !";
805               throw INTERP_KERNEL::Exception(oss.str().c_str());
806             }
807         }
808       ret+=nbOfEltInChunk;
809     }
810   return ret;
811 }
812
813 int MEDCouplingFieldDiscretizationOnNodes::getNumberOfMeshPlaces(const MEDCouplingMesh *mesh) const
814 {
815   if(!mesh)
816     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationNodes::getNumberOfMeshPlaces : NULL input mesh !");
817   return mesh->getNumberOfNodes();
818 }
819
820 /*!
821  * Nothing to do here.
822  */
823 void MEDCouplingFieldDiscretizationOnNodes::renumberArraysForCell(const MEDCouplingMesh *, const std::vector<DataArray *>& arrays,
824                                                                   const int *old2NewBg, bool check)
825 {
826 }
827
828 DataArrayInt *MEDCouplingFieldDiscretizationOnNodes::getOffsetArr(const MEDCouplingMesh *mesh) const
829 {
830   if(!mesh)
831     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationNodes::getOffsetArr : NULL input mesh !");
832   int nbOfTuples=mesh->getNumberOfNodes();
833   DataArrayInt *ret=DataArrayInt::New();
834   ret->alloc(nbOfTuples+1,1);
835   ret->iota(0);
836   return ret;
837 }
838
839 DataArrayDouble *MEDCouplingFieldDiscretizationOnNodes::getLocalizationOfDiscValues(const MEDCouplingMesh *mesh) const
840 {
841   if(!mesh)
842     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationNodes::getLocalizationOfDiscValues : NULL input mesh !");
843   return mesh->getCoordinatesAndOwner();
844 }
845
846 void MEDCouplingFieldDiscretizationOnNodes::computeMeshRestrictionFromTupleIds(const MEDCouplingMesh *mesh, const int *tupleIdsBg, const int *tupleIdsEnd,
847                                                                                DataArrayInt *&cellRestriction, DataArrayInt *&trueTupleRestriction) const
848 {
849   if(!mesh)
850     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationOnNodes::computeMeshRestrictionFromTupleIds : NULL input mesh !");
851   MCAuto<DataArrayInt> ret1=mesh->getCellIdsFullyIncludedInNodeIds(tupleIdsBg,tupleIdsEnd);
852   const MEDCouplingUMesh *meshc=dynamic_cast<const MEDCouplingUMesh *>(mesh);
853   if(!meshc)
854     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationOnNodes::computeMeshRestrictionFromTupleIds : trying to subpart field on nodes by node ids ! Your mesh has to be unstructured !");
855   MCAuto<MEDCouplingUMesh> meshPart=static_cast<MEDCouplingUMesh *>(meshc->buildPartOfMySelf(ret1->begin(),ret1->end(),true));
856   MCAuto<DataArrayInt> ret2=meshPart->computeFetchedNodeIds();
857   cellRestriction=ret1.retn();
858   trueTupleRestriction=ret2.retn();
859 }
860
861 void MEDCouplingFieldDiscretizationOnNodes::checkCoherencyBetween(const MEDCouplingMesh *mesh, const DataArray *da) const
862 {
863   if(!mesh || !da)
864     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationNodes::checkCoherencyBetween : NULL input mesh or DataArray !");
865   if(mesh->getNumberOfNodes()!=da->getNumberOfTuples())
866     {
867       std::ostringstream message;
868       message << "Field on nodes invalid because there are " << mesh->getNumberOfNodes();
869       message << " nodes in mesh and " << da->getNumberOfTuples() << " tuples in field !";
870       throw INTERP_KERNEL::Exception(message.str().c_str());
871     }
872 }
873
874 /*!
875  * This method returns a submesh of 'mesh' instance constituting cell ids contained in array defined as an interval [start;end).
876  * @param di is an array returned that specifies entity ids (here nodes ids) in mesh 'mesh' of entity in returned submesh.
877  * Example : The first node id of returned mesh has the (*di)[0] id in 'mesh'
878  */
879 MEDCouplingMesh *MEDCouplingFieldDiscretizationOnNodes::buildSubMeshData(const MEDCouplingMesh *mesh, const int *start, const int *end, DataArrayInt *&di) const
880 {
881   if(!mesh)
882     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationNodes::buildSubMeshData : NULL input mesh !");
883   DataArrayInt *diTmp=0;
884   MCAuto<MEDCouplingMesh> ret=mesh->buildPartAndReduceNodes(start,end,diTmp);
885   MCAuto<DataArrayInt> diTmpSafe(diTmp);
886   MCAuto<DataArrayInt> di2=diTmpSafe->invertArrayO2N2N2O(ret->getNumberOfNodes());
887   di=di2.retn();
888   return ret.retn();
889 }
890
891 /*!
892  * This method is strictly equivalent to MEDCouplingFieldDiscretizationNodes::buildSubMeshData except that it is optimized for input defined as a range of cell ids.
893  * 
894  * \param [out] beginOut Valid only if \a di is NULL
895  * \param [out] endOut Valid only if \a di is NULL
896  * \param [out] stepOut Valid only if \a di is NULL
897  * \param [out] di is an array returned that specifies entity ids (nodes, cells, Gauss points... ) in array if no output range is foundable.
898  *
899  * \sa MEDCouplingFieldDiscretizationNodes::buildSubMeshData
900  */
901 MEDCouplingMesh *MEDCouplingFieldDiscretizationOnNodes::buildSubMeshDataRange(const MEDCouplingMesh *mesh, int beginCellIds, int endCellIds, int stepCellIds, int& beginOut, int& endOut, int& stepOut, DataArrayInt *&di) const
902 {
903   if(!mesh)
904     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationOnNodes::buildSubMeshDataRange : NULL input mesh !");
905   DataArrayInt *diTmp=0;
906   MCAuto<MEDCouplingMesh> ret=mesh->buildPartRangeAndReduceNodes(beginCellIds,endCellIds,stepCellIds,beginOut,endOut,stepOut,diTmp);
907   if(diTmp)
908     {
909       MCAuto<DataArrayInt> diTmpSafe(diTmp);
910       MCAuto<DataArrayInt> di2=diTmpSafe->invertArrayO2N2N2O(ret->getNumberOfNodes());
911       di=di2.retn();
912     }
913   return ret.retn();
914 }
915
916 /*!
917  * This method returns a tuple ids selection from cell ids selection [start;end).
918  * This method is called by MEDCouplingFieldDiscretizationOnNodes::buildSubMeshData to return parameter \b di.
919  * Here for P1 only nodes fetched by submesh of mesh[startCellIds:endCellIds) is returned !
920  *
921  * \return a newly allocated array containing ids to select into the DataArrayDouble of the field.
922  * 
923  */
924 DataArrayInt *MEDCouplingFieldDiscretizationOnNodes::computeTupleIdsToSelectFromCellIds(const MEDCouplingMesh *mesh, const int *startCellIds, const int *endCellIds) const
925 {
926   if(!mesh)
927     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP1::computeTupleIdsToSelectFromCellIds : NULL input mesh !");
928   const MCAuto<MEDCouplingUMesh> umesh=mesh->buildUnstructured();
929   MCAuto<MEDCouplingUMesh> umesh2=static_cast<MEDCouplingUMesh *>(umesh->buildPartOfMySelf(startCellIds,endCellIds,true));
930   return umesh2->computeFetchedNodeIds();
931 }
932
933 void MEDCouplingFieldDiscretizationOnNodes::renumberValuesOnNodes(double epsOnVals, const int *old2NewPtr, int newNbOfNodes, DataArrayDouble *arr) const
934 {
935   RenumberEntitiesFromO2NArr(epsOnVals,old2NewPtr,newNbOfNodes,arr,"Node");
936 }
937
938 /*!
939  * Nothing to do it's not a bug.
940  */
941 void MEDCouplingFieldDiscretizationOnNodes::renumberValuesOnCells(double epsOnVals, const MEDCouplingMesh *mesh, const int *old2New, int newSz, DataArrayDouble *arr) const
942 {
943 }
944
945 /*!
946  * Nothing to do it's not a bug.
947  */
948 void MEDCouplingFieldDiscretizationOnNodes::renumberValuesOnCellsR(const MEDCouplingMesh *mesh, const int *new2old, int newSz, DataArrayDouble *arr) const
949 {
950 }
951
952 void MEDCouplingFieldDiscretizationOnNodes::getValueOnPos(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, int i, int j, int k, double *res) const
953 {
954   const MEDCouplingCMesh *meshC=dynamic_cast<const MEDCouplingCMesh *>(mesh);
955   if(!meshC)
956     throw INTERP_KERNEL::Exception("OnNodes::getValueOnPos(i,j,k) is only accessible for structured meshes !");
957   int id=meshC->getNodeIdFromPos(i,j,k);
958   arr->getTuple(id,res);
959 }
960
961 TypeOfField MEDCouplingFieldDiscretizationP1::getEnum() const
962 {
963   return TYPE;
964 }
965
966 /*!
967  * This method is simply called by MEDCouplingFieldDiscretization::deepCopy. It performs the deep copy of \a this.
968  *
969  * \sa MEDCouplingFieldDiscretization::deepCopy.
970  */
971 MEDCouplingFieldDiscretization *MEDCouplingFieldDiscretizationP1::clone() const
972 {
973   return new MEDCouplingFieldDiscretizationP1;
974 }
975
976 std::string MEDCouplingFieldDiscretizationP1::getStringRepr() const
977 {
978   return std::string(REPR);
979 }
980
981 const char *MEDCouplingFieldDiscretizationP1::getRepr() const
982 {
983   return REPR;
984 }
985
986 bool MEDCouplingFieldDiscretizationP1::isEqualIfNotWhy(const MEDCouplingFieldDiscretization *other, double eps, std::string& reason) const
987 {
988   if(!other)
989     {
990       reason="other spatial discretization is NULL, and this spatial discretization (P1) is defined.";
991       return false;
992     }
993   const MEDCouplingFieldDiscretizationP1 *otherC=dynamic_cast<const MEDCouplingFieldDiscretizationP1 *>(other);
994   bool ret=otherC!=0;
995   if(!ret)
996     reason="Spatial discrtization of this is ON_NODES, which is not the case of other.";
997   return ret;
998 }
999
1000 void MEDCouplingFieldDiscretizationP1::checkCompatibilityWithNature(NatureOfField nat) const
1001 {
1002   if(nat!=IntensiveMaximum)
1003     throw INTERP_KERNEL::Exception("Invalid nature for P1 field  : expected IntensiveMaximum !");
1004 }
1005
1006 MEDCouplingFieldDouble *MEDCouplingFieldDiscretizationP1::getMeasureField(const MEDCouplingMesh *mesh, bool isAbs) const
1007 {
1008   if(!mesh)
1009     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP1::getMeasureField : mesh instance specified is NULL !");
1010   return mesh->getMeasureFieldOnNode(isAbs);
1011 }
1012
1013 void MEDCouplingFieldDiscretizationP1::getValueOn(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, const double *loc, double *res) const
1014 {
1015   if(!mesh)
1016     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP1::getValueOn : NULL input mesh !");
1017   int id=mesh->getCellContainingPoint(loc,_precision);
1018   if(id==-1)
1019     throw INTERP_KERNEL::Exception("Specified point is detected outside of mesh : unable to apply P1::getValueOn !");
1020   INTERP_KERNEL::NormalizedCellType type=mesh->getTypeOfCell(id);
1021   if(type!=INTERP_KERNEL::NORM_SEG2 && type!=INTERP_KERNEL::NORM_TRI3 && type!=INTERP_KERNEL::NORM_TETRA4)
1022     throw INTERP_KERNEL::Exception("P1 getValueOn is not specified for not simplex cells !");
1023   getValueInCell(mesh,id,arr,loc,res);
1024 }
1025
1026 /*!
1027  * This method localizes a point defined by 'loc' in a cell with id 'cellId' into mesh 'mesh'.
1028  * The result is put into res expected to be of size at least arr->getNumberOfComponents()
1029  */
1030 void MEDCouplingFieldDiscretizationP1::getValueInCell(const MEDCouplingMesh *mesh, int cellId, const DataArrayDouble *arr, const double *loc, double *res) const
1031 {
1032   if(!mesh)
1033     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP1::getValueInCell : NULL input mesh !");
1034   std::vector<int> conn;
1035   std::vector<double> coo;
1036   mesh->getNodeIdsOfCell(cellId,conn);
1037   for(std::vector<int>::const_iterator iter=conn.begin();iter!=conn.end();iter++)
1038     mesh->getCoordinatesOfNode(*iter,coo);
1039   int spaceDim=mesh->getSpaceDimension();
1040   std::size_t nbOfNodes=conn.size();
1041   std::vector<const double *> vec(nbOfNodes);
1042   for(std::size_t i=0;i<nbOfNodes;i++)
1043     vec[i]=&coo[i*spaceDim];
1044   INTERP_KERNEL::AutoPtr<double> tmp=new double[nbOfNodes];
1045   INTERP_KERNEL::barycentric_coords(vec,loc,tmp);
1046   int sz=arr->getNumberOfComponents();
1047   INTERP_KERNEL::AutoPtr<double> tmp2=new double[sz];
1048   std::fill(res,res+sz,0.);
1049   for(std::size_t i=0;i<nbOfNodes;i++)
1050     {
1051       arr->getTuple(conn[i],(double *)tmp2);
1052       std::transform((double *)tmp2,((double *)tmp2)+sz,(double *)tmp2,std::bind2nd(std::multiplies<double>(),tmp[i]));
1053       std::transform(res,res+sz,(double *)tmp2,res,std::plus<double>());
1054     }
1055 }
1056
1057 DataArrayDouble *MEDCouplingFieldDiscretizationP1::getValueOnMulti(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, const double *loc, int nbOfPoints) const
1058 {
1059   if(!mesh)
1060     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationP1::getValueOnMulti : NULL input mesh !");
1061   MCAuto<DataArrayInt> eltsArr,eltsIndexArr;
1062   mesh->getCellsContainingPoints(loc,nbOfPoints,_precision,eltsArr,eltsIndexArr);
1063   const int *elts(eltsArr->begin()),*eltsIndex(eltsIndexArr->begin());
1064   int spaceDim=mesh->getSpaceDimension();
1065   int nbOfComponents=arr->getNumberOfComponents();
1066   MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
1067   ret->alloc(nbOfPoints,nbOfComponents);
1068   double *ptToFill=ret->getPointer();
1069   for(int i=0;i<nbOfPoints;i++)
1070     if(eltsIndex[i+1]-eltsIndex[i]>=1)
1071       getValueInCell(mesh,elts[eltsIndex[i]],arr,loc+i*spaceDim,ptToFill+i*nbOfComponents);
1072     else
1073       {
1074         std::ostringstream oss; oss << "Point #" << i << " with coordinates : (";
1075         std::copy(loc+i*spaceDim,loc+(i+1)*spaceDim,std::ostream_iterator<double>(oss,", "));
1076         oss << ") detected outside mesh : unable to apply P1::getValueOnMulti ! ";
1077         throw INTERP_KERNEL::Exception(oss.str().c_str());
1078       }
1079   return ret.retn();
1080 }
1081
1082 void MEDCouplingFieldDiscretizationP1::reprQuickOverview(std::ostream& stream) const
1083 {
1084   stream << "P1 spatial discretization.";
1085 }
1086
1087 MEDCouplingFieldDiscretizationPerCell::MEDCouplingFieldDiscretizationPerCell():_discr_per_cell(0)
1088 {
1089 }
1090
1091 MEDCouplingFieldDiscretizationPerCell::~MEDCouplingFieldDiscretizationPerCell()
1092 {
1093   if(_discr_per_cell)
1094     _discr_per_cell->decrRef();
1095 }
1096
1097 /*!
1098  * This constructor deep copies MEDCoupling::DataArrayInt instance from other (if any).
1099  */
1100 MEDCouplingFieldDiscretizationPerCell::MEDCouplingFieldDiscretizationPerCell(const MEDCouplingFieldDiscretizationPerCell& other, const int *startCellIds, const int *endCellIds):_discr_per_cell(0)
1101 {
1102   DataArrayInt *arr=other._discr_per_cell;
1103   if(arr)
1104     {
1105       if(startCellIds==0 && endCellIds==0)
1106         _discr_per_cell=arr->deepCopy();
1107       else
1108         _discr_per_cell=arr->selectByTupleIdSafe(startCellIds,endCellIds);
1109     }
1110 }
1111
1112 MEDCouplingFieldDiscretizationPerCell::MEDCouplingFieldDiscretizationPerCell(const MEDCouplingFieldDiscretizationPerCell& other, int beginCellIds, int endCellIds, int stepCellIds):_discr_per_cell(0)
1113 {
1114   DataArrayInt *arr=other._discr_per_cell;
1115   if(arr)
1116     {
1117       _discr_per_cell=arr->selectByTupleIdSafeSlice(beginCellIds,endCellIds,stepCellIds);
1118     }
1119 }
1120
1121 void MEDCouplingFieldDiscretizationPerCell::updateTime() const
1122 {
1123   if(_discr_per_cell)
1124     updateTimeWith(*_discr_per_cell);
1125 }
1126
1127 std::size_t MEDCouplingFieldDiscretizationPerCell::getHeapMemorySizeWithoutChildren() const
1128 {
1129   std::size_t ret(MEDCouplingFieldDiscretization::getHeapMemorySizeWithoutChildren());
1130   return ret;
1131 }
1132
1133 std::vector<const BigMemoryObject *> MEDCouplingFieldDiscretizationPerCell::getDirectChildrenWithNull() const
1134 {
1135   std::vector<const BigMemoryObject *> ret(MEDCouplingFieldDiscretization::getDirectChildrenWithNull());
1136   ret.push_back(_discr_per_cell);
1137   return ret;
1138 }
1139
1140 void MEDCouplingFieldDiscretizationPerCell::checkCoherencyBetween(const MEDCouplingMesh *mesh, const DataArray *da) const
1141 {
1142   if(!_discr_per_cell)
1143     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationPerCell has no discretization per cell !");
1144   if(!mesh)
1145     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationPerCell::checkCoherencyBetween : NULL input mesh or DataArray !");
1146   int nbOfTuples=_discr_per_cell->getNumberOfTuples();
1147   if(nbOfTuples!=mesh->getNumberOfCells())
1148     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationPerCell has a discretization per cell but it's not matching the underlying mesh !");
1149 }
1150
1151 bool MEDCouplingFieldDiscretizationPerCell::isEqualIfNotWhy(const MEDCouplingFieldDiscretization *other, double eps, std::string& reason) const
1152 {
1153   if(!other)
1154     {
1155       reason="other spatial discretization is NULL, and this spatial discretization (PerCell) is defined.";
1156       return false;
1157     }
1158   const MEDCouplingFieldDiscretizationPerCell *otherC=dynamic_cast<const MEDCouplingFieldDiscretizationPerCell *>(other);
1159   if(!otherC)
1160     {
1161       reason="Spatial discretization of this is ON_GAUSS, which is not the case of other.";
1162       return false;
1163     }
1164   if(_discr_per_cell==0)
1165     return otherC->_discr_per_cell==0;
1166   if(otherC->_discr_per_cell==0)
1167     return false;
1168   bool ret=_discr_per_cell->isEqualIfNotWhy(*otherC->_discr_per_cell,reason);
1169   if(!ret)
1170     reason.insert(0,"Field discretization per cell DataArrayInt given the discid per cell :");
1171   return ret;
1172 }
1173
1174 bool MEDCouplingFieldDiscretizationPerCell::isEqualWithoutConsideringStr(const MEDCouplingFieldDiscretization *other, double eps) const
1175 {
1176   const MEDCouplingFieldDiscretizationPerCell *otherC=dynamic_cast<const MEDCouplingFieldDiscretizationPerCell *>(other);
1177   if(!otherC)
1178     return false;
1179   if(_discr_per_cell==0)
1180     return otherC->_discr_per_cell==0;
1181   if(otherC->_discr_per_cell==0)
1182     return false;
1183   return _discr_per_cell->isEqualWithoutConsideringStr(*otherC->_discr_per_cell);
1184 }
1185
1186 /*!
1187  * This method is typically the first step of renumbering. The impact on _discr_per_cell is necessary here.
1188  * virtualy by this method.
1189  */
1190 void MEDCouplingFieldDiscretizationPerCell::renumberCells(const int *old2NewBg, bool check)
1191 {
1192   int nbCells=_discr_per_cell->getNumberOfTuples();
1193   const int *array=old2NewBg;
1194   if(check)
1195     array=DataArrayInt::CheckAndPreparePermutation(old2NewBg,old2NewBg+nbCells);
1196   //
1197   DataArrayInt *dpc=_discr_per_cell->renumber(array);
1198   _discr_per_cell->decrRef();
1199   _discr_per_cell=dpc;
1200   //
1201   if(check)
1202     free(const_cast<int *>(array));
1203 }
1204
1205 void MEDCouplingFieldDiscretizationPerCell::buildDiscrPerCellIfNecessary(const MEDCouplingMesh *mesh)
1206 {
1207   if(!mesh)
1208     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationPerCell::buildDiscrPerCellIfNecessary : NULL input mesh !");
1209   if(!_discr_per_cell)
1210     {
1211       _discr_per_cell=DataArrayInt::New();
1212       int nbTuples=mesh->getNumberOfCells();
1213       _discr_per_cell->alloc(nbTuples,1);
1214       int *ptr=_discr_per_cell->getPointer();
1215       std::fill(ptr,ptr+nbTuples,DFT_INVALID_LOCID_VALUE);
1216     }
1217 }
1218
1219 void MEDCouplingFieldDiscretizationPerCell::checkNoOrphanCells() const
1220 {
1221   if(!_discr_per_cell)
1222     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationPerCell::checkNoOrphanCells : no discretization defined !");
1223   MCAuto<DataArrayInt> test=_discr_per_cell->findIdsEqual(DFT_INVALID_LOCID_VALUE);
1224   if(test->getNumberOfTuples()!=0)
1225     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationPerCell::checkNoOrphanCells : presence of orphan cells !");
1226 }
1227
1228 /*!
1229  * This method is useful when 'this' describes a field discretization with several gauss discretization on a \b same cell type.
1230  * For example same NORM_TRI3 cells having 6 gauss points and others with 12 gauss points.
1231  * This method returns 2 arrays with same size : the return value and 'locIds' output parameter.
1232  * For a given i into [0,locIds.size) ret[i] represents the set of cell ids of i_th set an locIds[i] represents the set of discretisation of the set.
1233  * The return vector contains a set of newly created instance to deal with.
1234  * The returned vector represents a \b partition of cells ids with a gauss discretization set.
1235  * 
1236  * If no descretization is set in 'this' and exception will be thrown.
1237  */
1238 std::vector<DataArrayInt *> MEDCouplingFieldDiscretizationPerCell::splitIntoSingleGaussDicrPerCellType(std::vector<int>& locIds) const
1239 {
1240   if(!_discr_per_cell)
1241     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationPerCell::splitIntoSingleGaussDicrPerCellType : no descretization set !");
1242   return _discr_per_cell->partitionByDifferentValues(locIds);
1243 }
1244
1245 const DataArrayInt *MEDCouplingFieldDiscretizationPerCell::getArrayOfDiscIds() const
1246 {
1247   return _discr_per_cell;
1248 }
1249
1250 void MEDCouplingFieldDiscretizationPerCell::setArrayOfDiscIds(const DataArrayInt *adids)
1251 {
1252   if(adids!=_discr_per_cell)
1253     {
1254       if(_discr_per_cell)
1255         _discr_per_cell->decrRef();
1256       _discr_per_cell=const_cast<DataArrayInt *>(adids);
1257       if(_discr_per_cell)
1258         _discr_per_cell->incrRef();
1259       declareAsNew();
1260     }
1261 }
1262
1263 MEDCouplingFieldDiscretizationGauss::MEDCouplingFieldDiscretizationGauss()
1264 {
1265 }
1266
1267 MEDCouplingFieldDiscretizationGauss::MEDCouplingFieldDiscretizationGauss(const MEDCouplingFieldDiscretizationGauss& other, const int *startCellIds, const int *endCellIds):MEDCouplingFieldDiscretizationPerCell(other,startCellIds,endCellIds),_loc(other._loc)
1268 {
1269 }
1270
1271 MEDCouplingFieldDiscretizationGauss::MEDCouplingFieldDiscretizationGauss(const MEDCouplingFieldDiscretizationGauss& other, int beginCellIds, int endCellIds, int stepCellIds):MEDCouplingFieldDiscretizationPerCell(other,beginCellIds,endCellIds,stepCellIds),_loc(other._loc)
1272 {
1273 }
1274
1275 TypeOfField MEDCouplingFieldDiscretizationGauss::getEnum() const
1276 {
1277   return TYPE;
1278 }
1279
1280 bool MEDCouplingFieldDiscretizationGauss::isEqualIfNotWhy(const MEDCouplingFieldDiscretization *other, double eps, std::string& reason) const
1281 {
1282   if(!other)
1283     {
1284       reason="other spatial discretization is NULL, and this spatial discretization (Gauss) is defined.";
1285       return false;
1286     }
1287   const MEDCouplingFieldDiscretizationGauss *otherC=dynamic_cast<const MEDCouplingFieldDiscretizationGauss *>(other);
1288   if(!otherC)
1289     {
1290       reason="Spatial discrtization of this is ON_GAUSS, which is not the case of other.";
1291       return false;
1292     }
1293   if(!MEDCouplingFieldDiscretizationPerCell::isEqualIfNotWhy(other,eps,reason))
1294     return false;
1295   if(_loc.size()!=otherC->_loc.size())
1296     {
1297       reason="Gauss spatial discretization : localization sizes differ";
1298       return false;
1299     }
1300   std::size_t sz=_loc.size();
1301   for(std::size_t i=0;i<sz;i++)
1302     if(!_loc[i].isEqual(otherC->_loc[i],eps))
1303       {
1304         std::ostringstream oss; oss << "Gauss spatial discretization : Localization #" << i << " differ from this to other.";
1305         reason=oss.str();
1306         return false;
1307       }
1308   return true;
1309 }
1310
1311 bool MEDCouplingFieldDiscretizationGauss::isEqualWithoutConsideringStr(const MEDCouplingFieldDiscretization *other, double eps) const
1312 {
1313   const MEDCouplingFieldDiscretizationGauss *otherC=dynamic_cast<const MEDCouplingFieldDiscretizationGauss *>(other);
1314   if(!otherC)
1315     return false;
1316   if(!MEDCouplingFieldDiscretizationPerCell::isEqualWithoutConsideringStr(other,eps))
1317     return false;
1318   if(_loc.size()!=otherC->_loc.size())
1319     return false;
1320   std::size_t sz=_loc.size();
1321   for(std::size_t i=0;i<sz;i++)
1322     if(!_loc[i].isEqual(otherC->_loc[i],eps))
1323       return false;
1324   return true;
1325 }
1326
1327 /*!
1328  * This method is simply called by MEDCouplingFieldDiscretization::deepCopy. It performs the deep copy of \a this.
1329  *
1330  * \sa MEDCouplingFieldDiscretization::deepCopy.
1331  */
1332 MEDCouplingFieldDiscretization *MEDCouplingFieldDiscretizationGauss::clone() const
1333 {
1334   return new MEDCouplingFieldDiscretizationGauss(*this);
1335 }
1336
1337 MEDCouplingFieldDiscretization *MEDCouplingFieldDiscretizationGauss::clonePart(const int *startCellIds, const int *endCellIds) const
1338 {
1339   return new MEDCouplingFieldDiscretizationGauss(*this,startCellIds,endCellIds);
1340 }
1341
1342 MEDCouplingFieldDiscretization *MEDCouplingFieldDiscretizationGauss::clonePartRange(int beginCellIds, int endCellIds, int stepCellIds) const
1343 {
1344   return new MEDCouplingFieldDiscretizationGauss(*this,beginCellIds,endCellIds,stepCellIds);
1345 }
1346
1347 std::string MEDCouplingFieldDiscretizationGauss::getStringRepr() const
1348 {
1349   std::ostringstream oss; oss << REPR << "." << std::endl;
1350   if(_discr_per_cell)
1351     {
1352       if(_discr_per_cell->isAllocated())
1353         {
1354           oss << "Discretization per cell : ";
1355           std::copy(_discr_per_cell->begin(),_discr_per_cell->end(),std::ostream_iterator<int>(oss,", "));
1356           oss << std::endl;
1357         }
1358     }
1359   oss << "Presence of " << _loc.size() << " localizations." << std::endl;
1360   int i=0;
1361   for(std::vector<MEDCouplingGaussLocalization>::const_iterator it=_loc.begin();it!=_loc.end();it++,i++)
1362     {
1363       oss << "+++++ Localization #" << i << " +++++" << std::endl;
1364       oss << (*it).getStringRepr();
1365       oss << "++++++++++" << std::endl;
1366     }
1367   return oss.str();
1368 }
1369
1370 std::size_t MEDCouplingFieldDiscretizationGauss::getHeapMemorySizeWithoutChildren() const
1371 {
1372   std::size_t ret(MEDCouplingFieldDiscretizationPerCell::getHeapMemorySizeWithoutChildren());
1373   ret+=_loc.capacity()*sizeof(MEDCouplingGaussLocalization);
1374   for(std::vector<MEDCouplingGaussLocalization>::const_iterator it=_loc.begin();it!=_loc.end();it++)
1375     ret+=(*it).getMemorySize();
1376   return ret;
1377 }
1378
1379 const char *MEDCouplingFieldDiscretizationGauss::getRepr() const
1380 {
1381   return REPR;
1382 }
1383
1384 /*!
1385  * This method returns the number of tuples regarding exclusively the input code \b without \b using \b a \b mesh \b in \b input.
1386  * The input code coherency is also checked regarding spatial discretization of \a this.
1387  * If an incoherency is detected, an exception will be thrown. If the input code is coherent, the number of tuples expected is returned.
1388  * The number of tuples expected is equal to those to have a valid field lying on \a this and having a mesh fitting perfectly the input code (geometric type distribution).
1389  */
1390 int MEDCouplingFieldDiscretizationGauss::getNumberOfTuplesExpectedRegardingCode(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const
1391 {
1392   if(!_discr_per_cell || !_discr_per_cell->isAllocated() || _discr_per_cell->getNumberOfComponents()!=1)
1393     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::getNumberOfTuplesExpectedRegardingCode");
1394   if(code.size()%3!=0)
1395     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::getNumberOfTuplesExpectedRegardingCode : invalid input code !");
1396   int nbOfSplit=(int)idsPerType.size();
1397   int nbOfTypes=(int)code.size()/3;
1398   int ret=0;
1399   for(int i=0;i<nbOfTypes;i++)
1400     {
1401       int nbOfEltInChunk=code[3*i+1];
1402       if(nbOfEltInChunk<0)
1403         throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::getNumberOfTuplesExpectedRegardingCode : invalid input code ! presence of negative value in a type !");
1404       int pos=code[3*i+2];
1405       if(pos!=-1)
1406         {
1407           if(pos<0 || pos>=nbOfSplit)
1408             {
1409               std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationGauss::getNumberOfTuplesExpectedRegardingCode : input code points to pos " << pos << " in typeid " << i << " ! Should be in [0," << nbOfSplit << ") !";
1410               throw INTERP_KERNEL::Exception(oss.str().c_str());
1411             }
1412           const DataArrayInt *ids(idsPerType[pos]);
1413           if(!ids || !ids->isAllocated() || ids->getNumberOfComponents()!=1 || ids->getNumberOfTuples()!=nbOfEltInChunk || ids->getMinValueInArray()<0)
1414             {
1415               std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationGauss::getNumberOfTuplesExpectedRegardingCode : input pfl chunck at pos " << pos << " should have " << i << " tuples and one component and with ids all >=0 !";
1416               throw INTERP_KERNEL::Exception(oss.str().c_str());
1417             }
1418         }
1419       ret+=nbOfEltInChunk;
1420     }
1421   if(ret!=_discr_per_cell->getNumberOfTuples())
1422     {
1423       std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationGauss::getNumberOfTuplesExpectedRegardingCode : input code points to " << ret << " cells whereas discretization percell array lgth is " <<  _discr_per_cell->getNumberOfTuples() << " !";
1424       throw INTERP_KERNEL::Exception(oss.str().c_str());
1425     }
1426   return getNumberOfTuples(0);//0 is not an error ! It is to be sure that input mesh is not used
1427 }
1428
1429 int MEDCouplingFieldDiscretizationGauss::getNumberOfTuples(const MEDCouplingMesh *) const
1430 {
1431   int ret=0;
1432   if (_discr_per_cell == 0)
1433     throw INTERP_KERNEL::Exception("Discretization is not initialized!");
1434   const int *dcPtr=_discr_per_cell->getConstPointer();
1435   int nbOfTuples=_discr_per_cell->getNumberOfTuples();
1436   int maxSz=(int)_loc.size();
1437   for(const int *w=dcPtr;w!=dcPtr+nbOfTuples;w++)
1438     {
1439       if(*w>=0 && *w<maxSz)
1440         ret+=_loc[*w].getNumberOfGaussPt();
1441       else
1442         {
1443           std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationGauss::getNumberOfTuples : At cell #" << std::distance(dcPtr,w) << " localization id is " << *w << " should be in [0," << maxSz << ") !";
1444           throw INTERP_KERNEL::Exception(oss.str().c_str());
1445         }
1446     }
1447   return ret;
1448 }
1449
1450 int MEDCouplingFieldDiscretizationGauss::getNumberOfMeshPlaces(const MEDCouplingMesh *mesh) const
1451 {
1452   if(!mesh)
1453     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::getNumberOfMeshPlaces : NULL input mesh !");
1454   return mesh->getNumberOfCells();
1455 }
1456
1457 /*!
1458  * This method is redevelopped for performance reasons, but it is equivalent to a call to MEDCouplingFieldDiscretizationGauss::buildNbOfGaussPointPerCellField
1459  * and a call to DataArrayDouble::computeOffsetsFull on the returned array.
1460  */
1461 DataArrayInt *MEDCouplingFieldDiscretizationGauss::getOffsetArr(const MEDCouplingMesh *mesh) const
1462 {
1463   if(!mesh)
1464     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::getOffsetArr : NULL input mesh !");
1465   int nbOfTuples=mesh->getNumberOfCells();
1466   MCAuto<DataArrayInt> ret=DataArrayInt::New();
1467   ret->alloc(nbOfTuples+1,1);
1468   int *retPtr=ret->getPointer();
1469   const int *start=_discr_per_cell->getConstPointer();
1470   if(_discr_per_cell->getNumberOfTuples()!=nbOfTuples)
1471     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::getOffsetArr : mismatch between the mesh and the discretization ids array length !");
1472   int maxPossible=(int)_loc.size();
1473   retPtr[0]=0;
1474   for(int i=0;i<nbOfTuples;i++,start++)
1475     {
1476       if(*start>=0 && *start<maxPossible)
1477         retPtr[i+1]=retPtr[i]+_loc[*start].getNumberOfGaussPt();
1478       else
1479         {
1480           std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationGauss::getOffsetArr : At position #" << i << " the locid = " << *start << " whereas it should be in [0," << maxPossible << ") !";
1481           throw INTERP_KERNEL::Exception(oss.str().c_str());
1482         }
1483     }
1484   return ret.retn();
1485 }
1486
1487 void MEDCouplingFieldDiscretizationGauss::renumberArraysForCell(const MEDCouplingMesh *mesh, const std::vector<DataArray *>& arrays,
1488                                                                 const int *old2NewBg, bool check)
1489 {
1490   if(!mesh)
1491     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::renumberArraysForCell : NULL input mesh !");
1492   const int *array=old2NewBg;
1493   if(check)
1494     array=DataArrayInt::CheckAndPreparePermutation(old2NewBg,old2NewBg+mesh->getNumberOfCells());
1495   int nbOfCells=_discr_per_cell->getNumberOfTuples();
1496   int nbOfTuples=getNumberOfTuples(0);
1497   const int *dcPtr=_discr_per_cell->getConstPointer();
1498   int *array2=new int[nbOfTuples];//stores the final conversion array old2New to give to arrays in renumberInPlace.
1499   int *array3=new int[nbOfCells];//store for each cell in present dcp array (already renumbered) the offset needed by each cell in new numbering.
1500   array3[0]=0;
1501   for(int i=1;i<nbOfCells;i++)
1502     array3[i]=array3[i-1]+_loc[dcPtr[i-1]].getNumberOfGaussPt();
1503   int j=0;
1504   for(int i=0;i<nbOfCells;i++)
1505     {
1506       int nbOfGaussPt=_loc[dcPtr[array[i]]].getNumberOfGaussPt();
1507       for(int k=0;k<nbOfGaussPt;k++,j++)
1508         array2[j]=array3[array[i]]+k;
1509     }
1510   delete [] array3;
1511   for(std::vector<DataArray *>::const_iterator it=arrays.begin();it!=arrays.end();it++)
1512     if(*it)
1513       (*it)->renumberInPlace(array2);
1514   delete [] array2;
1515   if(check)
1516     free(const_cast<int*>(array));
1517 }
1518
1519 DataArrayDouble *MEDCouplingFieldDiscretizationGauss::getLocalizationOfDiscValues(const MEDCouplingMesh *mesh) const
1520 {
1521   if(!mesh)
1522     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::getLocalizationOfDiscValues : NULL input mesh !");
1523   checkNoOrphanCells();
1524   MCAuto<MEDCouplingUMesh> umesh=mesh->buildUnstructured();//in general do nothing
1525   int nbOfTuples=getNumberOfTuples(mesh);
1526   MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
1527   int spaceDim=mesh->getSpaceDimension();
1528   ret->alloc(nbOfTuples,spaceDim);
1529   std::vector< int > locIds;
1530   std::vector<DataArrayInt *> parts=splitIntoSingleGaussDicrPerCellType(locIds);
1531   std::vector< MCAuto<DataArrayInt> > parts2(parts.size());
1532   std::copy(parts.begin(),parts.end(),parts2.begin());
1533   MCAuto<DataArrayInt> offsets=buildNbOfGaussPointPerCellField();
1534   offsets->computeOffsets();
1535   const int *ptrOffsets=offsets->getConstPointer();
1536   const double *coords=umesh->getCoords()->getConstPointer();
1537   const int *connI=umesh->getNodalConnectivityIndex()->getConstPointer();
1538   const int *conn=umesh->getNodalConnectivity()->getConstPointer();
1539   double *valsToFill=ret->getPointer();
1540   for(std::size_t i=0;i<parts2.size();i++)
1541     {
1542       INTERP_KERNEL::GaussCoords calculator;
1543       //
1544       const MEDCouplingGaussLocalization& cli(_loc[locIds[i]]);//curLocInfo
1545       INTERP_KERNEL::NormalizedCellType typ(cli.getType());
1546       const std::vector<double>& wg(cli.getWeights());
1547       calculator.addGaussInfo(typ,INTERP_KERNEL::CellModel::GetCellModel(typ).getDimension(),
1548           &cli.getGaussCoords()[0],(int)wg.size(),&cli.getRefCoords()[0],
1549           INTERP_KERNEL::CellModel::GetCellModel(typ).getNumberOfNodes());
1550       //
1551       for(const int *w=parts2[i]->begin();w!=parts2[i]->end();w++)
1552         calculator.calculateCoords(cli.getType(),coords,spaceDim,conn+connI[*w]+1,valsToFill+spaceDim*(ptrOffsets[*w]));
1553     }
1554   ret->copyStringInfoFrom(*umesh->getCoords());
1555   return ret.retn();
1556 }
1557
1558 void MEDCouplingFieldDiscretizationGauss::computeMeshRestrictionFromTupleIds(const MEDCouplingMesh *mesh, const int *tupleIdsBg, const int *tupleIdsEnd,
1559                                                                              DataArrayInt *&cellRestriction, DataArrayInt *&trueTupleRestriction) const
1560 {
1561   if(!mesh)
1562     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::computeMeshRestrictionFromTupleIds : NULL input mesh !");
1563   MCAuto<DataArrayInt> tmp=DataArrayInt::New(); tmp->alloc((int)std::distance(tupleIdsBg,tupleIdsEnd),1);
1564   std::copy(tupleIdsBg,tupleIdsEnd,tmp->getPointer());
1565   tmp->sort(true);
1566   tmp=tmp->buildUnique();
1567   MCAuto<DataArrayInt> nbOfNodesPerCell=buildNbOfGaussPointPerCellField();
1568   nbOfNodesPerCell->computeOffsetsFull();
1569   nbOfNodesPerCell->findIdsRangesInListOfIds(tmp,cellRestriction,trueTupleRestriction);
1570 }
1571
1572 /*!
1573  * Empty : not a bug
1574  */
1575 void MEDCouplingFieldDiscretizationGauss::checkCompatibilityWithNature(NatureOfField nat) const
1576 {
1577 }
1578
1579 void MEDCouplingFieldDiscretizationGauss::getTinySerializationIntInformation(std::vector<int>& tinyInfo) const
1580 {
1581   int val=-1;
1582   if(_discr_per_cell)
1583     val=_discr_per_cell->getNumberOfTuples();
1584   tinyInfo.push_back(val);
1585   tinyInfo.push_back((int)_loc.size());
1586   if(_loc.empty())
1587     tinyInfo.push_back(-1);
1588   else
1589     tinyInfo.push_back(_loc[0].getDimension());
1590   for(std::vector<MEDCouplingGaussLocalization>::const_iterator iter=_loc.begin();iter!=_loc.end();iter++)
1591     (*iter).pushTinySerializationIntInfo(tinyInfo);
1592 }
1593
1594 void MEDCouplingFieldDiscretizationGauss::getTinySerializationDbleInformation(std::vector<double>& tinyInfo) const
1595 {
1596   for(std::vector<MEDCouplingGaussLocalization>::const_iterator iter=_loc.begin();iter!=_loc.end();iter++)
1597     (*iter).pushTinySerializationDblInfo(tinyInfo);
1598 }
1599
1600 void MEDCouplingFieldDiscretizationGauss::getSerializationIntArray(DataArrayInt *& arr) const
1601 {
1602   arr=0;
1603   if(_discr_per_cell)
1604     arr=_discr_per_cell;
1605 }
1606
1607 void MEDCouplingFieldDiscretizationGauss::resizeForUnserialization(const std::vector<int>& tinyInfo, DataArrayInt *& arr)
1608 {
1609   int val=tinyInfo[0];
1610   if(val>=0)
1611     {
1612       _discr_per_cell=DataArrayInt::New();
1613       _discr_per_cell->alloc(val,1);
1614     }
1615   else
1616     _discr_per_cell=0;
1617   arr=_discr_per_cell;
1618   commonUnserialization(tinyInfo);
1619 }
1620
1621 void MEDCouplingFieldDiscretizationGauss::checkForUnserialization(const std::vector<int>& tinyInfo, const DataArrayInt *arr)
1622 {
1623   static const char MSG[]="MEDCouplingFieldDiscretizationGauss::checkForUnserialization : expect to have one not null DataArrayInt !";
1624   int val=tinyInfo[0];
1625   if(val>=0)
1626     {
1627       if(!arr)
1628         throw INTERP_KERNEL::Exception(MSG);
1629       arr->checkNbOfTuplesAndComp(val,1,MSG);
1630       _discr_per_cell=const_cast<DataArrayInt *>(arr);
1631       _discr_per_cell->incrRef();
1632     }
1633   else
1634     _discr_per_cell=0;
1635   commonUnserialization(tinyInfo);
1636 }
1637
1638 void MEDCouplingFieldDiscretizationGauss::finishUnserialization(const std::vector<double>& tinyInfo)
1639 {
1640   double *tmp=new double[tinyInfo.size()];
1641   std::copy(tinyInfo.begin(),tinyInfo.end(),tmp);
1642   const double *work=tmp;
1643   for(std::vector<MEDCouplingGaussLocalization>::iterator iter=_loc.begin();iter!=_loc.end();iter++)
1644     work=(*iter).fillWithValues(work);
1645   delete [] tmp;
1646 }
1647
1648 double MEDCouplingFieldDiscretizationGauss::getIJK(const MEDCouplingMesh *mesh, const DataArrayDouble *da, int cellId, int nodeIdInCell, int compoId) const
1649 {
1650   int offset=getOffsetOfCell(cellId);
1651   return da->getIJ(offset+nodeIdInCell,compoId);
1652 }
1653
1654 void MEDCouplingFieldDiscretizationGauss::checkCoherencyBetween(const MEDCouplingMesh *mesh, const DataArray *da) const
1655 {
1656   if(!mesh || !da)
1657     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::checkCoherencyBetween : NULL input mesh or DataArray !");
1658   MEDCouplingFieldDiscretizationPerCell::checkCoherencyBetween(mesh,da);
1659   for(std::vector<MEDCouplingGaussLocalization>::const_iterator iter=_loc.begin();iter!=_loc.end();iter++)
1660     (*iter).checkConsistencyLight();
1661   int nbOfDesc=(int)_loc.size();
1662   int nbOfCells=mesh->getNumberOfCells();
1663   const int *dc=_discr_per_cell->getConstPointer();
1664   for(int i=0;i<nbOfCells;i++)
1665     {
1666       if(dc[i]>=nbOfDesc)
1667         {
1668           std::ostringstream oss; oss << "Cell # " << i << " of mesh \"" << mesh->getName() << "\" has an undefined gauss location ! Should never happend !";
1669           throw INTERP_KERNEL::Exception(oss.str().c_str());
1670         }
1671       if(dc[i]<0)
1672         {
1673           std::ostringstream oss; oss << "Cell # " << i << " of mesh \"" << mesh->getName() << "\" has no gauss location !";
1674           throw INTERP_KERNEL::Exception(oss.str().c_str());
1675         }
1676       if(mesh->getTypeOfCell(i)!=_loc[dc[i]].getType())
1677         {
1678           std::ostringstream oss; oss << "Types of mesh and gauss location mismatch for cell # " << i;
1679           throw INTERP_KERNEL::Exception(oss.str().c_str());
1680         }
1681     }
1682   int nbOfTuples(getNumberOfTuples(mesh));
1683   if(nbOfTuples!=da->getNumberOfTuples())
1684     {
1685       std::ostringstream oss; oss << "Invalid number of tuples in the array : expecting " << nbOfTuples << " having " << da->getNumberOfTuples() << " !";
1686       throw INTERP_KERNEL::Exception(oss.str().c_str());
1687     }
1688 }
1689
1690 MEDCouplingFieldDouble *MEDCouplingFieldDiscretizationGauss::getMeasureField(const MEDCouplingMesh *mesh, bool isAbs) const
1691 {
1692   if(!mesh)
1693     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::getMeasureField : mesh instance specified is NULL !");
1694   MCAuto<MEDCouplingFieldDouble> vol=mesh->getMeasureField(isAbs);
1695   const double *volPtr=vol->getArray()->begin();
1696   MCAuto<MEDCouplingFieldDouble> ret=MEDCouplingFieldDouble::New(ON_GAUSS_PT);
1697   ret->setMesh(mesh);
1698   ret->setDiscretization(const_cast<MEDCouplingFieldDiscretizationGauss *>(this));
1699   if(!_discr_per_cell)
1700     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::getMeasureField : no discr per cell array not defined ! spatial localization is incorrect !");
1701   _discr_per_cell->checkAllocated();
1702   if(_discr_per_cell->getNumberOfComponents()!=1)
1703     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::getMeasureField : no discr per cell array defined but with nb of components different from 1 !");
1704   if(_discr_per_cell->getNumberOfTuples()!=vol->getNumberOfTuples())
1705     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::getMeasureField : no discr per cell array defined but mismatch between nb of cells of mesh and size of spatial disr array !");
1706   MCAuto<DataArrayInt> offset=getOffsetArr(mesh);
1707   MCAuto<DataArrayDouble> arr=DataArrayDouble::New(); arr->alloc(getNumberOfTuples(mesh),1);
1708   ret->setArray(arr);
1709   double *arrPtr=arr->getPointer();
1710   const int *offsetPtr=offset->getConstPointer();
1711   int maxGaussLoc=(int)_loc.size();
1712   std::vector<int> locIds;
1713   std::vector<DataArrayInt *> ids=splitIntoSingleGaussDicrPerCellType(locIds);
1714   std::vector< MCAuto<DataArrayInt> > ids2(ids.size()); std::copy(ids.begin(),ids.end(),ids2.begin());
1715   for(std::size_t i=0;i<locIds.size();i++)
1716     {
1717       const DataArrayInt *curIds=ids[i];
1718       int locId=locIds[i];
1719       if(locId>=0 && locId<maxGaussLoc)
1720         {
1721           const MEDCouplingGaussLocalization& loc=_loc[locId];
1722           int nbOfGaussPt=loc.getNumberOfGaussPt();
1723           INTERP_KERNEL::AutoPtr<double> weights=new double[nbOfGaussPt];
1724           double sum=std::accumulate(loc.getWeights().begin(),loc.getWeights().end(),0.);
1725           std::transform(loc.getWeights().begin(),loc.getWeights().end(),(double *)weights,std::bind2nd(std::multiplies<double>(),1./sum));
1726           for(const int *cellId=curIds->begin();cellId!=curIds->end();cellId++)
1727             for(int j=0;j<nbOfGaussPt;j++)
1728               arrPtr[offsetPtr[*cellId]+j]=weights[j]*volPtr[*cellId];
1729         }
1730       else
1731         {
1732           std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationGauss::getMeasureField : Presence of localization id " << locId << " in cell #" << curIds->getIJ(0,0) << " ! Must be in [0," << maxGaussLoc << ") !";
1733           throw INTERP_KERNEL::Exception(oss.str().c_str());
1734         }
1735     }
1736   ret->synchronizeTimeWithSupport();
1737   return ret.retn();
1738 }
1739
1740 void MEDCouplingFieldDiscretizationGauss::getValueOn(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, const double *loc, double *res) const
1741 {
1742   throw INTERP_KERNEL::Exception("Not implemented yet !");
1743 }
1744
1745 void MEDCouplingFieldDiscretizationGauss::getValueOnPos(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, int i, int j, int k, double *res) const
1746 {
1747   throw INTERP_KERNEL::Exception("getValueOnPos(i,j,k) : Not applyable for Gauss points !");
1748 }
1749
1750 DataArrayDouble *MEDCouplingFieldDiscretizationGauss::getValueOnMulti(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, const double *loc, int nbOfPoints) const
1751 {
1752   throw INTERP_KERNEL::Exception("getValueOnMulti : Not implemented yet for gauss points !");
1753 }
1754
1755 MEDCouplingMesh *MEDCouplingFieldDiscretizationGauss::buildSubMeshData(const MEDCouplingMesh *mesh, const int *start, const int *end, DataArrayInt *&di) const
1756 {
1757   if(!mesh)
1758     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::buildSubMeshData : NULL input mesh !");
1759   MCAuto<DataArrayInt> diSafe=computeTupleIdsToSelectFromCellIds(mesh,start,end);
1760   MCAuto<MEDCouplingMesh> ret=mesh->buildPart(start,end);
1761   di=diSafe.retn();
1762   return ret.retn();
1763 }
1764
1765 /*!
1766  * This method is strictly equivalent to MEDCouplingFieldDiscretizationGauss::buildSubMeshData except that it is optimized for input defined as a range of cell ids.
1767  * 
1768  * \param [out] beginOut Valid only if \a di is NULL
1769  * \param [out] endOut Valid only if \a di is NULL
1770  * \param [out] stepOut Valid only if \a di is NULL
1771  * \param [out] di is an array returned that specifies entity ids (nodes, cells, Gauss points... ) in array if no output range is foundable.
1772  *
1773  * \sa MEDCouplingFieldDiscretizationGauss::buildSubMeshData
1774  */
1775 MEDCouplingMesh *MEDCouplingFieldDiscretizationGauss::buildSubMeshDataRange(const MEDCouplingMesh *mesh, int beginCellIds, int endCellIds, int stepCellIds, int& beginOut, int& endOut, int& stepOut, DataArrayInt *&di) const
1776 {
1777   if(stepCellIds!=1)//even for stepCellIds==-1 the output will not be a range
1778     return MEDCouplingFieldDiscretization::buildSubMeshDataRange(mesh,beginCellIds,endCellIds,stepCellIds,beginOut,endOut,stepOut,di);
1779   if(!mesh)
1780     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::buildSubMeshDataRange : NULL input mesh !");
1781   if(!_discr_per_cell)
1782     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::buildSubMeshDataRange : no discretization array set !");
1783   di=0; beginOut=0; endOut=0; stepOut=stepCellIds;
1784   const char msg[]="MEDCouplingFieldDiscretizationGauss::buildSubMeshDataRange : cell #";
1785   int nbOfTuples=_discr_per_cell->getNumberOfTuples();
1786   const int *w=_discr_per_cell->begin();
1787   int nbMaxOfLocId=(int)_loc.size();
1788   for(int i=0;i<nbOfTuples;i++,w++)
1789     {
1790       if(*w!=DFT_INVALID_LOCID_VALUE)
1791         {
1792           if(*w>=0 && *w<nbMaxOfLocId)
1793             {
1794               int delta=_loc[*w].getNumberOfGaussPt();
1795               if(i<beginCellIds)
1796                 beginOut+=delta;
1797               endOut+=delta;
1798               if(i>=endCellIds)
1799                 break;
1800             }
1801           else
1802             { std::ostringstream oss; oss << msg << i << " has invalid id (" << *w << ") ! Should be in [0," << nbMaxOfLocId << ") !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); }
1803         }
1804       else
1805         { std::ostringstream oss; oss << msg << i << " is detected as orphan !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); }
1806     }
1807   MCAuto<MEDCouplingMesh> ret=mesh->buildPartRange(beginCellIds,endCellIds,stepCellIds);
1808   return ret.retn();
1809 }
1810
1811 /*!
1812  * This method returns a tuple ids selection from cell ids selection [start;end).
1813  * This method is called by MEDCouplingFieldDiscretizationGauss::buildSubMeshData to return parameter \b di.
1814  *
1815  * \return a newly allocated array containing ids to select into the DataArrayDouble of the field.
1816  * 
1817  */
1818 DataArrayInt *MEDCouplingFieldDiscretizationGauss::computeTupleIdsToSelectFromCellIds(const MEDCouplingMesh *mesh, const int *startCellIds, const int *endCellIds) const
1819 {
1820   if(!mesh)
1821     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::computeTupleIdsToSelectFromCellIds : null mesh !");
1822   MCAuto<DataArrayInt> nbOfNodesPerCell=buildNbOfGaussPointPerCellField();//check of _discr_per_cell not NULL pointer
1823   int nbOfCells=mesh->getNumberOfCells();
1824   if(_discr_per_cell->getNumberOfTuples()!=nbOfCells)
1825     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::computeTupleIdsToSelectFromCellIds : mismatch of nb of tuples of cell ids array and number of cells !");
1826   nbOfNodesPerCell->computeOffsetsFull();
1827   MCAuto<DataArrayInt> sel=DataArrayInt::New(); sel->useArray(startCellIds,false,CPP_DEALLOC,(int)std::distance(startCellIds,endCellIds),1);
1828   return sel->buildExplicitArrByRanges(nbOfNodesPerCell);
1829 }
1830
1831 /*!
1832  * No implementation needed !
1833  */
1834 void MEDCouplingFieldDiscretizationGauss::renumberValuesOnNodes(double , const int *, int newNbOfNodes, DataArrayDouble *) const
1835 {
1836 }
1837
1838 void MEDCouplingFieldDiscretizationGauss::renumberValuesOnCells(double epsOnVals, const MEDCouplingMesh *mesh, const int *old2New, int newSz, DataArrayDouble *arr) const
1839 {
1840   throw INTERP_KERNEL::Exception("Not implemented yet !");
1841 }
1842
1843 void MEDCouplingFieldDiscretizationGauss::renumberValuesOnCellsR(const MEDCouplingMesh *mesh, const int *new2old, int newSz, DataArrayDouble *arr) const
1844 {
1845   throw INTERP_KERNEL::Exception("Number of cells has changed and becomes higher with some cells that have been split ! Unable to conserve the Gauss field !");
1846 }
1847
1848 void MEDCouplingFieldDiscretizationGauss::setGaussLocalizationOnType(const MEDCouplingMesh *mesh, INTERP_KERNEL::NormalizedCellType type, const std::vector<double>& refCoo,
1849                                                                      const std::vector<double>& gsCoo, const std::vector<double>& wg)
1850 {
1851   if(!mesh)
1852     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::setGaussLocalizationOnType : NULL input mesh !");
1853   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(type);
1854   if((int)cm.getDimension()!=mesh->getMeshDimension())
1855     {
1856       std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationGauss::setGaussLocalizationOnType : mismatch of dimensions ! MeshDim==" << mesh->getMeshDimension();
1857       oss << " whereas Type '" << cm.getRepr() << "' has dimension " << cm.getDimension() << " !";
1858       throw INTERP_KERNEL::Exception(oss.str().c_str());
1859     }
1860   buildDiscrPerCellIfNecessary(mesh);
1861   int id=(int)_loc.size();
1862   MEDCouplingGaussLocalization elt(type,refCoo,gsCoo,wg);
1863   _loc.push_back(elt);
1864   int *ptr=_discr_per_cell->getPointer();
1865   int nbCells=mesh->getNumberOfCells();
1866   for(int i=0;i<nbCells;i++)
1867     if(mesh->getTypeOfCell(i)==type)
1868       ptr[i]=id;
1869   zipGaussLocalizations();
1870 }
1871
1872 void MEDCouplingFieldDiscretizationGauss::setGaussLocalizationOnCells(const MEDCouplingMesh *mesh, const int *begin, const int *end, const std::vector<double>& refCoo,
1873                                                                       const std::vector<double>& gsCoo, const std::vector<double>& wg)
1874 {
1875   if(!mesh)
1876     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::setGaussLocalizationOnCells : NULL input mesh !");
1877   buildDiscrPerCellIfNecessary(mesh);
1878   if(std::distance(begin,end)<1)
1879     throw INTERP_KERNEL::Exception("Size of [begin,end) must be equal or greater than 1 !");
1880   INTERP_KERNEL::NormalizedCellType type=mesh->getTypeOfCell(*begin);
1881   MEDCouplingGaussLocalization elt(type,refCoo,gsCoo,wg);
1882   int id=(int)_loc.size();
1883   int *ptr=_discr_per_cell->getPointer();
1884   for(const int *w=begin+1;w!=end;w++)
1885     {
1886       if(mesh->getTypeOfCell(*w)!=type)
1887         {
1888           std::ostringstream oss; oss << "The cell with id " << *w << " has been detected to be incompatible in the [begin,end) array specified !";
1889           throw INTERP_KERNEL::Exception(oss.str().c_str());
1890         }
1891     }
1892   //
1893   for(const int *w2=begin;w2!=end;w2++)
1894     ptr[*w2]=id;
1895   //
1896   _loc.push_back(elt);
1897   zipGaussLocalizations();
1898 }
1899
1900 void MEDCouplingFieldDiscretizationGauss::clearGaussLocalizations()
1901 {
1902   if(_discr_per_cell)
1903     {
1904       _discr_per_cell->decrRef();
1905       _discr_per_cell=0;
1906     }
1907   _loc.clear();
1908 }
1909
1910 void MEDCouplingFieldDiscretizationGauss::setGaussLocalization(int locId, const MEDCouplingGaussLocalization& loc)
1911 {
1912   if(locId<0)
1913     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::setGaussLocalization : localization id has to be >=0 !");
1914   int sz=(int)_loc.size();
1915   MEDCouplingGaussLocalization gLoc(INTERP_KERNEL::NORM_ERROR);
1916   if(locId>=sz)
1917     _loc.resize(locId+1,gLoc);
1918   _loc[locId]=loc;
1919 }
1920
1921 void MEDCouplingFieldDiscretizationGauss::resizeLocalizationVector(int newSz)
1922 {
1923   if(newSz<0)
1924     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::resizeLocalizationVector : new size has to be >=0 !");
1925   MEDCouplingGaussLocalization gLoc(INTERP_KERNEL::NORM_ERROR);
1926   _loc.resize(newSz,gLoc);
1927 }
1928
1929 MEDCouplingGaussLocalization& MEDCouplingFieldDiscretizationGauss::getGaussLocalization(int locId)
1930 {
1931   checkLocalizationId(locId);
1932   return _loc[locId];
1933 }
1934
1935 int MEDCouplingFieldDiscretizationGauss::getNbOfGaussLocalization() const
1936 {
1937   return (int)_loc.size();
1938 }
1939
1940 int MEDCouplingFieldDiscretizationGauss::getGaussLocalizationIdOfOneCell(int cellId) const
1941 {
1942   if(!_discr_per_cell)
1943     throw INTERP_KERNEL::Exception("No Gauss localization still set !");
1944   int locId=_discr_per_cell->begin()[cellId];
1945   if(locId<0)
1946     throw INTERP_KERNEL::Exception("No Gauss localization set for the specified cell !");
1947   return locId;
1948 }
1949
1950 int MEDCouplingFieldDiscretizationGauss::getGaussLocalizationIdOfOneType(INTERP_KERNEL::NormalizedCellType type) const
1951 {
1952   std::set<int> ret=getGaussLocalizationIdsOfOneType(type);
1953   if(ret.empty())
1954     throw INTERP_KERNEL::Exception("No gauss discretization found for the specified type !");
1955   if(ret.size()>1)
1956     throw INTERP_KERNEL::Exception("Several gauss discretizations have been found for the specified type !");
1957   return *ret.begin();
1958 }
1959
1960 std::set<int> MEDCouplingFieldDiscretizationGauss::getGaussLocalizationIdsOfOneType(INTERP_KERNEL::NormalizedCellType type) const
1961 {
1962   if(!_discr_per_cell)
1963     throw INTERP_KERNEL::Exception("No Gauss localization still set !");
1964   std::set<int> ret;
1965   int id=0;
1966   for(std::vector<MEDCouplingGaussLocalization>::const_iterator iter=_loc.begin();iter!=_loc.end();iter++,id++)
1967     if((*iter).getType()==type)
1968       ret.insert(id);
1969   return ret;
1970 }
1971
1972 void MEDCouplingFieldDiscretizationGauss::getCellIdsHavingGaussLocalization(int locId, std::vector<int>& cellIds) const
1973 {
1974   if(locId<0 || locId>=(int)_loc.size())
1975     throw INTERP_KERNEL::Exception("Invalid locId given : must be in range [0:getNbOfGaussLocalization()) !");
1976   int nbOfTuples=_discr_per_cell->getNumberOfTuples();
1977   const int *ptr=_discr_per_cell->getConstPointer();
1978   for(int i=0;i<nbOfTuples;i++)
1979     if(ptr[i]==locId)
1980       cellIds.push_back(i);
1981 }
1982
1983 const MEDCouplingGaussLocalization& MEDCouplingFieldDiscretizationGauss::getGaussLocalization(int locId) const
1984 {
1985   checkLocalizationId(locId);
1986   return _loc[locId];
1987 }
1988
1989 void MEDCouplingFieldDiscretizationGauss::checkLocalizationId(int locId) const
1990 {
1991   if(locId<0 || locId>=(int)_loc.size())
1992     throw INTERP_KERNEL::Exception("Invalid locId given : must be in range [0:getNbOfGaussLocalization()) !");
1993 }
1994
1995 int MEDCouplingFieldDiscretizationGauss::getOffsetOfCell(int cellId) const
1996 {
1997   int ret=0;
1998   const int *start=_discr_per_cell->getConstPointer();
1999   for(const int *w=start;w!=start+cellId;w++)
2000     ret+=_loc[*w].getNumberOfGaussPt();
2001   return ret;
2002 }
2003
2004 /*!
2005  * This method do the assumption that there is no orphan cell. If there is an exception is thrown.
2006  * This method makes the assumption too that '_discr_per_cell' is defined. If not an exception is thrown.
2007  * This method returns a newly created array with number of tuples equals to '_discr_per_cell->getNumberOfTuples' and number of components equal to 1.
2008  * The i_th tuple in returned array is the number of gauss point if the corresponding cell.
2009  */
2010 DataArrayInt *MEDCouplingFieldDiscretizationGauss::buildNbOfGaussPointPerCellField() const
2011 {
2012   if(!_discr_per_cell)
2013     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGauss::buildNbOfGaussPointPerCellField : no discretization array set !");
2014   int nbOfTuples=_discr_per_cell->getNumberOfTuples();
2015   MCAuto<DataArrayInt> ret=DataArrayInt::New();
2016   const int *w=_discr_per_cell->begin();
2017   ret->alloc(nbOfTuples,1);
2018   int *valsToFill=ret->getPointer();
2019   int nbMaxOfLocId=(int)_loc.size();
2020   for(int i=0;i<nbOfTuples;i++,w++)
2021     if(*w!=DFT_INVALID_LOCID_VALUE)
2022       {
2023         if(*w>=0 && *w<nbMaxOfLocId)
2024           valsToFill[i]=_loc[*w].getNumberOfGaussPt();
2025         else
2026           {
2027             std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationGauss::buildNbOfGaussPointPerCellField : cell #" << i << " has invalid id (" << *w << ") ! Should be in [0," << nbMaxOfLocId << ") !";
2028             throw INTERP_KERNEL::Exception(oss.str().c_str());
2029           }
2030       }
2031     else
2032       {
2033         std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationGauss::buildNbOfGaussPointPerCellField : cell #" << i << " is detected as orphan !";
2034         throw INTERP_KERNEL::Exception(oss.str().c_str());
2035       }
2036   return ret.retn();
2037 }
2038
2039 void MEDCouplingFieldDiscretizationGauss::reprQuickOverview(std::ostream& stream) const
2040 {
2041   stream << "Gauss points spatial discretization.";
2042 }
2043
2044 /*!
2045  * This method makes the assumption that _discr_per_cell is set.
2046  * This method reduces as much as possible number size of _loc.
2047  * This method is useful when several set on same cells has been done and that some Gauss Localization are no more used.
2048  */
2049 void MEDCouplingFieldDiscretizationGauss::zipGaussLocalizations()
2050 {
2051   const int *start=_discr_per_cell->begin();
2052   int nbOfTuples=_discr_per_cell->getNumberOfTuples();
2053   INTERP_KERNEL::AutoPtr<int> tmp=new int[_loc.size()];
2054   std::fill((int *)tmp,(int *)tmp+_loc.size(),-2);
2055   for(const int *w=start;w!=start+nbOfTuples;w++)
2056     if(*w>=0)
2057       tmp[*w]=1;
2058   int fid=0;
2059   for(int i=0;i<(int)_loc.size();i++)
2060     if(tmp[i]!=-2)
2061       tmp[i]=fid++;
2062   if(fid==(int)_loc.size())
2063     return;
2064   // zip needed
2065   int *start2=_discr_per_cell->getPointer();
2066   for(int *w2=start2;w2!=start2+nbOfTuples;w2++)
2067     if(*w2>=0)
2068       *w2=tmp[*w2];
2069   std::vector<MEDCouplingGaussLocalization> tmpLoc;
2070   for(int i=0;i<(int)_loc.size();i++)
2071     if(tmp[i]!=-2)
2072       tmpLoc.push_back(_loc[i]);
2073   _loc=tmpLoc;
2074 }
2075
2076 void MEDCouplingFieldDiscretizationGauss::commonUnserialization(const std::vector<int>& tinyInfo)
2077 {
2078   int nbOfLoc=tinyInfo[1];
2079   _loc.clear();
2080   int dim=tinyInfo[2];
2081   int delta=-1;
2082   if(nbOfLoc>0)
2083     delta=((int)tinyInfo.size()-3)/nbOfLoc;
2084   for(int i=0;i<nbOfLoc;i++)
2085     {
2086       std::vector<int> tmp(tinyInfo.begin()+3+i*delta,tinyInfo.begin()+3+(i+1)*delta);
2087       MEDCouplingGaussLocalization elt=MEDCouplingGaussLocalization::BuildNewInstanceFromTinyInfo(dim,tmp);
2088       _loc.push_back(elt);
2089     }
2090 }
2091
2092 MEDCouplingFieldDiscretizationGaussNE::MEDCouplingFieldDiscretizationGaussNE()
2093 {
2094 }
2095
2096 TypeOfField MEDCouplingFieldDiscretizationGaussNE::getEnum() const
2097 {
2098   return TYPE;
2099 }
2100
2101 /*!
2102  * This method is simply called by MEDCouplingFieldDiscretization::deepCopy. It performs the deep copy of \a this.
2103  *
2104  * \sa MEDCouplingFieldDiscretization::deepCopy.
2105  */
2106 MEDCouplingFieldDiscretization *MEDCouplingFieldDiscretizationGaussNE::clone() const
2107 {
2108   return new MEDCouplingFieldDiscretizationGaussNE(*this);
2109 }
2110
2111 std::string MEDCouplingFieldDiscretizationGaussNE::getStringRepr() const
2112 {
2113   return std::string(REPR);
2114 }
2115
2116 const char *MEDCouplingFieldDiscretizationGaussNE::getRepr() const
2117 {
2118   return REPR;
2119 }
2120
2121 bool MEDCouplingFieldDiscretizationGaussNE::isEqualIfNotWhy(const MEDCouplingFieldDiscretization *other, double eps, std::string& reason) const
2122 {
2123   if(!other)
2124     {
2125       reason="other spatial discretization is NULL, and this spatial discretization (GaussNE) is defined.";
2126       return false;
2127     }
2128   const MEDCouplingFieldDiscretizationGaussNE *otherC=dynamic_cast<const MEDCouplingFieldDiscretizationGaussNE *>(other);
2129   bool ret=otherC!=0;
2130   if(!ret)
2131     reason="Spatial discrtization of this is ON_GAUSS_NE, which is not the case of other.";
2132   return ret;
2133 }
2134
2135 /*!
2136  * This method returns the number of tuples regarding exclusively the input code \b without \b using \b a \b mesh \b in \b input.
2137  * The input code coherency is also checked regarding spatial discretization of \a this.
2138  * If an incoherency is detected, an exception will be thrown. If the input code is coherent, the number of tuples expected is returned.
2139  * The number of tuples expected is equal to those to have a valid field lying on \a this and having a mesh fitting perfectly the input code (geometric type distribution).
2140  */
2141 int MEDCouplingFieldDiscretizationGaussNE::getNumberOfTuplesExpectedRegardingCode(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const
2142 {
2143   if(code.size()%3!=0)
2144     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::getNumberOfTuplesExpectedRegardingCode : invalid input code !");
2145   int nbOfSplit=(int)idsPerType.size();
2146   int nbOfTypes=(int)code.size()/3;
2147   int ret(0);
2148   for(int i=0;i<nbOfTypes;i++)
2149     {
2150       const INTERP_KERNEL::CellModel& cm(INTERP_KERNEL::CellModel::GetCellModel((INTERP_KERNEL::NormalizedCellType)code[3*i]));
2151       if(cm.isDynamic())
2152         {
2153           std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationGaussNE::getNumberOfTuplesExpectedRegardingCode : At pos #" << i << " the geometric type " << cm.getRepr() << " is dynamic ! There are not managed by GAUSS_NE field discretization !";
2154           throw INTERP_KERNEL::Exception(oss.str().c_str());
2155         }
2156       int nbOfEltInChunk=code[3*i+1];
2157       if(nbOfEltInChunk<0)
2158         throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::getNumberOfTuplesExpectedRegardingCode : invalid input code ! presence of negative value in a type !");
2159       int pos=code[3*i+2];
2160       if(pos!=-1)
2161         {
2162           if(pos<0 || pos>=nbOfSplit)
2163             {
2164               std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationGaussNE::getNumberOfTuplesExpectedRegardingCode : input code points to pos " << pos << " in typeid " << i << " ! Should be in [0," << nbOfSplit << ") !";
2165               throw INTERP_KERNEL::Exception(oss.str().c_str());
2166             }
2167           const DataArrayInt *ids(idsPerType[pos]);
2168           if(!ids || !ids->isAllocated() || ids->getNumberOfComponents()!=1 || ids->getNumberOfTuples()!=nbOfEltInChunk || ids->getMinValueInArray()<0)
2169             {
2170               std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationGaussNE::getNumberOfTuplesExpectedRegardingCode : input pfl chunck at pos " << pos << " should have " << i << " tuples and one component and with ids all >=0 !";
2171               throw INTERP_KERNEL::Exception(oss.str().c_str());
2172             }
2173         }
2174       ret+=nbOfEltInChunk*(int)cm.getNumberOfNodes();
2175     }
2176   return ret;
2177 }
2178
2179 int MEDCouplingFieldDiscretizationGaussNE::getNumberOfTuples(const MEDCouplingMesh *mesh) const
2180 {
2181   if(!mesh)
2182     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::getNumberOfTuples : NULL input mesh !");
2183   int ret=0;
2184   int nbOfCells=mesh->getNumberOfCells();
2185   for(int i=0;i<nbOfCells;i++)
2186     {
2187       INTERP_KERNEL::NormalizedCellType type=mesh->getTypeOfCell(i);
2188       const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(type);
2189       if(cm.isDynamic())
2190         throw INTERP_KERNEL::Exception("Not implemented yet Gauss node on elements for polygons and polyedrons !");
2191       ret+=cm.getNumberOfNodes();
2192     }
2193   return ret;
2194 }
2195
2196 int MEDCouplingFieldDiscretizationGaussNE::getNumberOfMeshPlaces(const MEDCouplingMesh *mesh) const
2197 {
2198   if(!mesh)
2199     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::getNumberOfMeshPlaces : NULL input mesh !");
2200   return mesh->getNumberOfCells();
2201 }
2202
2203 DataArrayInt *MEDCouplingFieldDiscretizationGaussNE::getOffsetArr(const MEDCouplingMesh *mesh) const
2204 {
2205   if(!mesh)
2206     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::getOffsetArr : NULL input mesh !");
2207   int nbOfTuples=mesh->getNumberOfCells();
2208   DataArrayInt *ret=DataArrayInt::New();
2209   ret->alloc(nbOfTuples+1,1);
2210   int *retPtr=ret->getPointer();
2211   retPtr[0]=0;
2212   for(int i=0;i<nbOfTuples;i++)
2213     {
2214       INTERP_KERNEL::NormalizedCellType type=mesh->getTypeOfCell(i);
2215       const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(type);
2216       if(cm.isDynamic())
2217         throw INTERP_KERNEL::Exception("Not implemented yet Gauss node on elements for polygons and polyedrons !");
2218       retPtr[i+1]=retPtr[i]+cm.getNumberOfNodes();
2219     }
2220   return ret;
2221 }
2222
2223 void MEDCouplingFieldDiscretizationGaussNE::renumberArraysForCell(const MEDCouplingMesh *mesh, const std::vector<DataArray *>& arrays,
2224                                                                   const int *old2NewBg, bool check)
2225 {
2226   if(!mesh)
2227     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::renumberArraysForCell : NULL input mesh !");
2228   const int *array=old2NewBg;
2229   if(check)
2230     array=DataArrayInt::CheckAndPreparePermutation(old2NewBg,old2NewBg+mesh->getNumberOfCells());
2231   int nbOfCells=mesh->getNumberOfCells();
2232   int nbOfTuples=getNumberOfTuples(mesh);
2233   int *array2=new int[nbOfTuples];//stores the final conversion array old2New to give to arrays in renumberInPlace.
2234   int *array3=new int[nbOfCells];//store for each cell in after renumbering the offset needed by each cell in new numbering.
2235   array3[0]=0;
2236   for(int i=1;i<nbOfCells;i++)
2237     {
2238       INTERP_KERNEL::NormalizedCellType type=mesh->getTypeOfCell((int)std::distance(array,std::find(array,array+nbOfCells,i-1)));
2239       const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(type);
2240       array3[i]=array3[i-1]+cm.getNumberOfNodes();
2241     }
2242   int j=0;
2243   for(int i=0;i<nbOfCells;i++)
2244     {
2245       INTERP_KERNEL::NormalizedCellType type=mesh->getTypeOfCell(i);
2246       const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(type);
2247       for(int k=0;k<(int)cm.getNumberOfNodes();k++,j++)
2248         array2[j]=array3[array[i]]+k;
2249     }
2250   delete [] array3;
2251   for(std::vector<DataArray *>::const_iterator it=arrays.begin();it!=arrays.end();it++)
2252     if(*it)
2253       (*it)->renumberInPlace(array2);
2254   delete [] array2;
2255   if(check)
2256     free(const_cast<int *>(array));
2257 }
2258
2259 DataArrayDouble *MEDCouplingFieldDiscretizationGaussNE::getLocalizationOfDiscValues(const MEDCouplingMesh *mesh) const
2260 {
2261   if(!mesh)
2262     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::getLocalizationOfDiscValues : NULL input mesh !");
2263   MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
2264   MCAuto<MEDCouplingUMesh> umesh=mesh->buildUnstructured();//in general do nothing
2265   int nbOfTuples=getNumberOfTuples(umesh);
2266   int spaceDim=mesh->getSpaceDimension();
2267   ret->alloc(nbOfTuples,spaceDim);
2268   const double *coords=umesh->getCoords()->begin();
2269   const int *connI=umesh->getNodalConnectivityIndex()->getConstPointer();
2270   const int *conn=umesh->getNodalConnectivity()->getConstPointer();
2271   int nbCells=umesh->getNumberOfCells();
2272   double *retPtr=ret->getPointer();
2273   for(int i=0;i<nbCells;i++,connI++)
2274     for(const int *w=conn+connI[0]+1;w!=conn+connI[1];w++)
2275       if(*w>=0)
2276         retPtr=std::copy(coords+(*w)*spaceDim,coords+((*w)+1)*spaceDim,retPtr);
2277   return ret.retn();
2278 }
2279
2280 /*!
2281  * Reimplemented from MEDCouplingFieldDiscretization::integral for performance reason. The default implementation is valid too for GAUSS_NE spatial discretization.
2282  */
2283 void MEDCouplingFieldDiscretizationGaussNE::integral(const MEDCouplingMesh *mesh, const DataArrayDouble *arr, bool isWAbs, double *res) const
2284 {
2285   if(!mesh || !arr)
2286     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::integral : input mesh or array is null !");
2287   int nbOfCompo=arr->getNumberOfComponents();
2288   std::fill(res,res+nbOfCompo,0.);
2289   //
2290   MCAuto<MEDCouplingFieldDouble> vol=mesh->getMeasureField(isWAbs);
2291   std::set<INTERP_KERNEL::NormalizedCellType> types=mesh->getAllGeoTypes();
2292   MCAuto<DataArrayInt> nbOfNodesPerCell=mesh->computeNbOfNodesPerCell();
2293   nbOfNodesPerCell->computeOffsetsFull();
2294   const double *arrPtr=arr->begin(),*volPtr=vol->getArray()->begin();
2295   for(std::set<INTERP_KERNEL::NormalizedCellType>::const_iterator it=types.begin();it!=types.end();it++)
2296     {
2297       std::size_t wArrSz=-1;
2298       const double *wArr=GetWeightArrayFromGeometricType(*it,wArrSz);
2299       INTERP_KERNEL::AutoPtr<double> wArr2=new double[wArrSz];
2300       double sum=std::accumulate(wArr,wArr+wArrSz,0.);
2301       std::transform(wArr,wArr+wArrSz,(double *)wArr2,std::bind2nd(std::multiplies<double>(),1./sum));      
2302       MCAuto<DataArrayInt> ids=mesh->giveCellsWithType(*it);
2303       MCAuto<DataArrayInt> ids2=ids->buildExplicitArrByRanges(nbOfNodesPerCell);
2304       const int *ptIds2=ids2->begin(),*ptIds=ids->begin();
2305       int nbOfCellsWithCurGeoType=ids->getNumberOfTuples();
2306       for(int i=0;i<nbOfCellsWithCurGeoType;i++,ptIds++,ptIds2+=wArrSz)
2307         {
2308           for(int k=0;k<nbOfCompo;k++)
2309             {
2310               double tmp=0.;
2311               for(std::size_t j=0;j<wArrSz;j++)
2312                 tmp+=arrPtr[nbOfCompo*ptIds2[j]+k]*wArr2[j];
2313               res[k]+=tmp*volPtr[*ptIds];
2314             }
2315         }
2316     }
2317 }
2318
2319 const double *MEDCouplingFieldDiscretizationGaussNE::GetWeightArrayFromGeometricType(INTERP_KERNEL::NormalizedCellType geoType, std::size_t& lgth)
2320 {
2321   switch(geoType)
2322   {
2323     case INTERP_KERNEL::NORM_POINT1:
2324       lgth=(int)sizeof(FGP_POINT1)/sizeof(double);
2325       return FGP_POINT1;
2326     case INTERP_KERNEL::NORM_SEG2:
2327       lgth=(int)sizeof(FGP_SEG2)/sizeof(double);
2328       return FGP_SEG2;
2329     case INTERP_KERNEL::NORM_SEG3:
2330       lgth=(int)sizeof(FGP_SEG3)/sizeof(double);
2331       return FGP_SEG3;
2332     case INTERP_KERNEL::NORM_SEG4:
2333       lgth=(int)sizeof(FGP_SEG4)/sizeof(double);
2334       return FGP_SEG4;
2335     case INTERP_KERNEL::NORM_TRI3:
2336       lgth=(int)sizeof(FGP_TRI3)/sizeof(double);
2337       return FGP_TRI3;
2338     case INTERP_KERNEL::NORM_TRI6:
2339       lgth=(int)sizeof(FGP_TRI6)/sizeof(double);
2340       return FGP_TRI6;
2341     case INTERP_KERNEL::NORM_TRI7:
2342       lgth=(int)sizeof(FGP_TRI7)/sizeof(double);
2343       return FGP_TRI7;
2344     case INTERP_KERNEL::NORM_QUAD4:
2345       lgth=(int)sizeof(FGP_QUAD4)/sizeof(double);
2346       return FGP_QUAD4;
2347     case INTERP_KERNEL::NORM_QUAD8:
2348       lgth=(int)sizeof(FGP_QUAD8)/sizeof(double);
2349       return FGP_QUAD8;
2350     case INTERP_KERNEL::NORM_QUAD9:
2351       lgth=(int)sizeof(FGP_QUAD9)/sizeof(double);
2352       return FGP_QUAD9;
2353     case INTERP_KERNEL::NORM_TETRA4:
2354       lgth=(int)sizeof(FGP_TETRA4)/sizeof(double);
2355       return FGP_TETRA4;
2356     case INTERP_KERNEL::NORM_TETRA10:
2357       lgth=(int)sizeof(FGP_TETRA10)/sizeof(double);
2358       return FGP_TETRA10;
2359     case INTERP_KERNEL::NORM_PENTA6:
2360       lgth=(int)sizeof(FGP_PENTA6)/sizeof(double);
2361       return FGP_PENTA6;
2362     case INTERP_KERNEL::NORM_PENTA15:
2363       lgth=(int)sizeof(FGP_PENTA15)/sizeof(double);
2364       return FGP_PENTA15;
2365     case INTERP_KERNEL::NORM_HEXA8:
2366       lgth=(int)sizeof(FGP_HEXA8)/sizeof(double);
2367       return FGP_HEXA8;
2368     case INTERP_KERNEL::NORM_HEXA20:
2369       lgth=(int)sizeof(FGP_HEXA20)/sizeof(double);
2370       return FGP_HEXA20;
2371     case INTERP_KERNEL::NORM_HEXA27:
2372       lgth=(int)sizeof(FGP_HEXA27)/sizeof(double);
2373       return FGP_HEXA27;
2374     case INTERP_KERNEL::NORM_PYRA5:
2375       lgth=(int)sizeof(FGP_PYRA5)/sizeof(double);
2376       return FGP_PYRA5;
2377     case INTERP_KERNEL::NORM_PYRA13:
2378       lgth=(int)sizeof(FGP_PYRA13)/sizeof(double);
2379       return FGP_PYRA13;
2380     default:
2381       throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::GetWeightArrayFromGeometricType : only SEG[2,3,4], TRI[3,6,7], QUAD[4,9], TETRA[4,10], PENTA[6,15], HEXA[8,20,27], PYRA[5,13] supported !");
2382   }
2383 }
2384
2385 const double *MEDCouplingFieldDiscretizationGaussNE::GetRefCoordsFromGeometricType(INTERP_KERNEL::NormalizedCellType geoType, std::size_t& lgth)
2386 {
2387   switch(geoType)
2388   {
2389     case INTERP_KERNEL::NORM_POINT1:
2390       lgth=0;
2391       return 0;
2392     case INTERP_KERNEL::NORM_SEG2:
2393       lgth=(int)sizeof(REF_SEG2)/sizeof(double);
2394       return REF_SEG2;
2395     case INTERP_KERNEL::NORM_SEG3:
2396       lgth=(int)sizeof(REF_SEG3)/sizeof(double);
2397       return REF_SEG3;
2398     case INTERP_KERNEL::NORM_SEG4:
2399       lgth=(int)sizeof(REF_SEG4)/sizeof(double);
2400       return REF_SEG4;
2401     case INTERP_KERNEL::NORM_TRI3:
2402       lgth=(int)sizeof(REF_TRI3)/sizeof(double);
2403       return REF_TRI3;
2404     case INTERP_KERNEL::NORM_TRI6:
2405       lgth=(int)sizeof(REF_TRI6)/sizeof(double);
2406       return REF_TRI6;
2407     case INTERP_KERNEL::NORM_TRI7:
2408       lgth=(int)sizeof(REF_TRI7)/sizeof(double);
2409       return REF_TRI7;
2410     case INTERP_KERNEL::NORM_QUAD4:
2411       lgth=(int)sizeof(REF_QUAD4)/sizeof(double);
2412       return REF_QUAD4;
2413     case INTERP_KERNEL::NORM_QUAD8:
2414       lgth=(int)sizeof(REF_QUAD8)/sizeof(double);
2415       return REF_QUAD8;
2416     case INTERP_KERNEL::NORM_QUAD9:
2417       lgth=(int)sizeof(REF_QUAD9)/sizeof(double);
2418       return REF_QUAD9;
2419     case INTERP_KERNEL::NORM_TETRA4:
2420       lgth=(int)sizeof(REF_TETRA4)/sizeof(double);
2421       return REF_TETRA4;
2422     case INTERP_KERNEL::NORM_TETRA10:
2423       lgth=(int)sizeof(REF_TETRA10)/sizeof(double);
2424       return REF_TETRA10;
2425     case INTERP_KERNEL::NORM_PENTA6:
2426       lgth=(int)sizeof(REF_PENTA6)/sizeof(double);
2427       return REF_PENTA6;
2428     case INTERP_KERNEL::NORM_PENTA15:
2429       lgth=(int)sizeof(REF_PENTA15)/sizeof(double);
2430       return REF_PENTA15;
2431     case INTERP_KERNEL::NORM_HEXA8:
2432       lgth=(int)sizeof(REF_HEXA8)/sizeof(double);
2433       return REF_HEXA8;
2434     case INTERP_KERNEL::NORM_HEXA20:
2435       lgth=(int)sizeof(REF_HEXA20)/sizeof(double);
2436       return REF_HEXA20;
2437     case INTERP_KERNEL::NORM_HEXA27:
2438       lgth=(int)sizeof(REF_HEXA27)/sizeof(double);
2439       return REF_HEXA27;
2440     case INTERP_KERNEL::NORM_PYRA5:
2441       lgth=(int)sizeof(REF_PYRA5)/sizeof(double);
2442       return REF_PYRA5;
2443     case INTERP_KERNEL::NORM_PYRA13:
2444       lgth=(int)sizeof(REF_PYRA13)/sizeof(double);
2445       return REF_PYRA13;
2446     default:
2447       throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::GetRefCoordsFromGeometricType : only SEG[2,3,4], TRI[3,6,7], QUAD[4,8,9], TETRA[4,10], PENTA[6,15], HEXA[8,20,27], PYRA[5,13] supported !");
2448   }
2449 }
2450
2451 const double *MEDCouplingFieldDiscretizationGaussNE::GetLocsFromGeometricType(INTERP_KERNEL::NormalizedCellType geoType, std::size_t& lgth)
2452 {
2453   switch(geoType)
2454   {
2455     case INTERP_KERNEL::NORM_POINT1:
2456       {
2457         lgth=0;
2458         return 0;
2459       }
2460     case INTERP_KERNEL::NORM_SEG2:
2461       {
2462         lgth=(int)sizeof(LOC_SEG2)/sizeof(double);
2463         return LOC_SEG2;
2464       }
2465     case INTERP_KERNEL::NORM_SEG3:
2466       {
2467         lgth=(int)sizeof(LOC_SEG3)/sizeof(double);
2468         return LOC_SEG3;
2469       }
2470     case INTERP_KERNEL::NORM_SEG4:
2471       {
2472         lgth=(int)sizeof(LOC_SEG4)/sizeof(double);
2473         return LOC_SEG4;
2474       }
2475     case INTERP_KERNEL::NORM_TRI3:
2476       {
2477         lgth=(int)sizeof(LOC_TRI3)/sizeof(double);
2478         return LOC_TRI3;
2479       }
2480     case INTERP_KERNEL::NORM_TRI6:
2481       {
2482         lgth=(int)sizeof(LOC_TRI6)/sizeof(double);
2483         return LOC_TRI6;
2484       }
2485     case INTERP_KERNEL::NORM_TRI7:
2486       {
2487         lgth=(int)sizeof(LOC_TRI7)/sizeof(double);
2488         return LOC_TRI7;
2489       }
2490     case INTERP_KERNEL::NORM_QUAD4:
2491       {
2492         lgth=(int)sizeof(LOC_QUAD4)/sizeof(double);
2493         return LOC_QUAD4;
2494       }
2495     case INTERP_KERNEL::NORM_QUAD8:
2496       {
2497         lgth=(int)sizeof(LOC_QUAD8)/sizeof(double);
2498         return LOC_QUAD8;
2499       }
2500     case INTERP_KERNEL::NORM_QUAD9:
2501       {
2502         lgth=(int)sizeof(LOC_QUAD9)/sizeof(double);
2503         return LOC_QUAD9;
2504       }
2505     case INTERP_KERNEL::NORM_TETRA4:
2506       {
2507         lgth=(int)sizeof(LOC_TETRA4)/sizeof(double);
2508         return LOC_TETRA4;
2509       }
2510     case INTERP_KERNEL::NORM_TETRA10:
2511       {
2512         lgth=(int)sizeof(LOC_TETRA10)/sizeof(double);
2513         return LOC_TETRA10;
2514       }
2515     case INTERP_KERNEL::NORM_PENTA6:
2516       {
2517         lgth=(int)sizeof(LOC_PENTA6)/sizeof(double);
2518         return LOC_PENTA6;
2519       }
2520     case INTERP_KERNEL::NORM_PENTA15:
2521       {
2522         lgth=(int)sizeof(LOC_PENTA15)/sizeof(double);
2523         return LOC_PENTA15;
2524       }
2525     case INTERP_KERNEL::NORM_HEXA8:
2526       {
2527         lgth=(int)sizeof(LOC_HEXA8)/sizeof(double);
2528         return LOC_HEXA8;
2529       }
2530     case INTERP_KERNEL::NORM_HEXA20:
2531       {
2532         lgth=(int)sizeof(LOC_HEXA20)/sizeof(double);
2533         return LOC_HEXA20;
2534       }
2535     case INTERP_KERNEL::NORM_HEXA27:
2536       {
2537         lgth=(int)sizeof(LOC_HEXA27)/sizeof(double);
2538         return LOC_HEXA27;
2539       }
2540     case INTERP_KERNEL::NORM_PYRA5:
2541       {
2542         lgth=(int)sizeof(LOC_PYRA5)/sizeof(double);
2543         return LOC_PYRA5;
2544       }
2545     case INTERP_KERNEL::NORM_PYRA13:
2546       {
2547         lgth=(int)sizeof(LOC_PYRA13)/sizeof(double);
2548         return LOC_PYRA13;
2549       }
2550     default:
2551       throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::GetLocsFromGeometricType : only SEG[2,3,4], TRI[3,6,7], QUAD[4,8,9], TETRA[4,10], PENTA[6,15], HEXA[8,20,27], PYRA[5,13] supported !");
2552   }
2553 }
2554
2555 void MEDCouplingFieldDiscretizationGaussNE::computeMeshRestrictionFromTupleIds(const MEDCouplingMesh *mesh, const int *tupleIdsBg, const int *tupleIdsEnd,
2556                                                                                DataArrayInt *&cellRestriction, DataArrayInt *&trueTupleRestriction) const
2557 {
2558   if(!mesh)
2559     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::computeMeshRestrictionFromTupleIds : NULL input mesh !");
2560   MCAuto<DataArrayInt> tmp=DataArrayInt::New(); tmp->alloc((int)std::distance(tupleIdsBg,tupleIdsEnd),1);
2561   std::copy(tupleIdsBg,tupleIdsEnd,tmp->getPointer());
2562   tmp->sort(true);
2563   tmp=tmp->buildUnique();
2564   MCAuto<DataArrayInt> nbOfNodesPerCell=mesh->computeNbOfNodesPerCell();
2565   nbOfNodesPerCell->computeOffsetsFull();
2566   nbOfNodesPerCell->findIdsRangesInListOfIds(tmp,cellRestriction,trueTupleRestriction);
2567 }
2568
2569 void MEDCouplingFieldDiscretizationGaussNE::checkCompatibilityWithNature(NatureOfField nat) const
2570 {
2571 }
2572
2573 double MEDCouplingFieldDiscretizationGaussNE::getIJK(const MEDCouplingMesh *mesh, const DataArrayDouble *da, int cellId, int nodeIdInCell, int compoId) const
2574 {
2575   if(!mesh)
2576     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::getIJK : NULL input mesh !");
2577   int offset=0;
2578   for(int i=0;i<cellId;i++)
2579     {
2580       INTERP_KERNEL::NormalizedCellType type=mesh->getTypeOfCell(i);
2581       const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(type);
2582       offset+=cm.getNumberOfNodes();
2583     }
2584   return da->getIJ(offset+nodeIdInCell,compoId);
2585 }
2586
2587 void MEDCouplingFieldDiscretizationGaussNE::checkCoherencyBetween(const MEDCouplingMesh *mesh, const DataArray *da) const
2588 {
2589   int nbOfTuples=getNumberOfTuples(mesh);
2590   if(nbOfTuples!=da->getNumberOfTuples())
2591     {
2592       std::ostringstream oss; oss << "Invalid number of tuples in the array : expecting " << nbOfTuples << " !";
2593       throw INTERP_KERNEL::Exception(oss.str().c_str());
2594     }
2595 }
2596
2597 MEDCouplingFieldDouble *MEDCouplingFieldDiscretizationGaussNE::getMeasureField(const MEDCouplingMesh *mesh, bool isAbs) const
2598 {
2599   if(!mesh)
2600     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::getMeasureField : mesh instance specified is NULL !");
2601   MCAuto<MEDCouplingFieldDouble> vol=mesh->getMeasureField(isAbs);
2602   const double *volPtr=vol->getArray()->begin();
2603   MCAuto<MEDCouplingFieldDouble> ret=MEDCouplingFieldDouble::New(ON_GAUSS_NE);
2604   ret->setMesh(mesh);
2605   //
2606   std::set<INTERP_KERNEL::NormalizedCellType> types=mesh->getAllGeoTypes();
2607   MCAuto<DataArrayInt> nbOfNodesPerCell=mesh->computeNbOfNodesPerCell();
2608   int nbTuples=nbOfNodesPerCell->accumulate(0);
2609   nbOfNodesPerCell->computeOffsetsFull();
2610   MCAuto<DataArrayDouble> arr=DataArrayDouble::New(); arr->alloc(nbTuples,1);
2611   ret->setArray(arr);
2612   double *arrPtr=arr->getPointer();
2613   for(std::set<INTERP_KERNEL::NormalizedCellType>::const_iterator it=types.begin();it!=types.end();it++)
2614     {
2615       std::size_t wArrSz=-1;
2616       const double *wArr=GetWeightArrayFromGeometricType(*it,wArrSz);
2617       INTERP_KERNEL::AutoPtr<double> wArr2=new double[wArrSz];
2618       double sum=std::accumulate(wArr,wArr+wArrSz,0.);
2619       std::transform(wArr,wArr+wArrSz,(double *)wArr2,std::bind2nd(std::multiplies<double>(),1./sum));      
2620       MCAuto<DataArrayInt> ids=mesh->giveCellsWithType(*it);
2621       MCAuto<DataArrayInt> ids2=ids->buildExplicitArrByRanges(nbOfNodesPerCell);
2622       const int *ptIds2=ids2->begin(),*ptIds=ids->begin();
2623       int nbOfCellsWithCurGeoType=ids->getNumberOfTuples();
2624       for(int i=0;i<nbOfCellsWithCurGeoType;i++,ptIds++)
2625         for(std::size_t j=0;j<wArrSz;j++,ptIds2++)
2626           arrPtr[*ptIds2]=wArr2[j]*volPtr[*ptIds];
2627     }
2628   ret->synchronizeTimeWithSupport();
2629   return ret.retn();
2630 }
2631
2632 void MEDCouplingFieldDiscretizationGaussNE::getValueOn(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, const double *loc, double *res) const
2633 {
2634   throw INTERP_KERNEL::Exception("Not implemented yet !");
2635 }
2636
2637 void MEDCouplingFieldDiscretizationGaussNE::getValueOnPos(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, int i, int j, int k, double *res) const
2638 {
2639   throw INTERP_KERNEL::Exception("getValueOnPos(i,j,k) : Not applyable for Gauss points !");
2640 }
2641
2642 DataArrayDouble *MEDCouplingFieldDiscretizationGaussNE::getValueOnMulti(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, const double *loc, int nbOfPoints) const
2643 {
2644   throw INTERP_KERNEL::Exception("getValueOnMulti : Not implemented for Gauss NE !");
2645 }
2646
2647 MEDCouplingMesh *MEDCouplingFieldDiscretizationGaussNE::buildSubMeshData(const MEDCouplingMesh *mesh, const int *start, const int *end, DataArrayInt *&di) const
2648 {
2649   if(!mesh)
2650     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::buildSubMeshData : NULL input mesh !");
2651   MCAuto<DataArrayInt> diSafe=computeTupleIdsToSelectFromCellIds(mesh,start,end);
2652   MCAuto<MEDCouplingMesh> ret=mesh->buildPart(start,end);
2653   di=diSafe.retn();
2654   return ret.retn();
2655 }
2656
2657 /*!
2658  * This method is strictly equivalent to MEDCouplingFieldDiscretizationGauss::buildSubMeshData except that it is optimized for input defined as a range of cell ids.
2659  * 
2660  * \param [out] beginOut Valid only if \a di is NULL
2661  * \param [out] endOut Valid only if \a di is NULL
2662  * \param [out] stepOut Valid only if \a di is NULL
2663  * \param [out] di is an array returned that specifies entity ids (nodes, cells, Gauss points... ) in array if no output range is foundable.
2664  *
2665  * \sa MEDCouplingFieldDiscretizationGauss::buildSubMeshData
2666  */
2667 MEDCouplingMesh *MEDCouplingFieldDiscretizationGaussNE::buildSubMeshDataRange(const MEDCouplingMesh *mesh, int beginCellIds, int endCellIds, int stepCellIds, int& beginOut, int& endOut, int& stepOut, DataArrayInt *&di) const
2668 {
2669   if(stepCellIds!=1)//even for stepCellIds==-1 the output will not be a range
2670     return MEDCouplingFieldDiscretization::buildSubMeshDataRange(mesh,beginCellIds,endCellIds,stepCellIds,beginOut,endOut,stepOut,di);
2671   if(!mesh)
2672     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::buildSubMeshDataRange : NULL input mesh !");
2673   int nbOfCells=mesh->getNumberOfCells();
2674   di=0; beginOut=0; endOut=0; stepOut=stepCellIds;
2675   const char msg[]="MEDCouplingFieldDiscretizationGaussNE::buildSubMeshDataRange : cell #";
2676   for(int i=0;i<nbOfCells;i++)
2677     {
2678       INTERP_KERNEL::NormalizedCellType type=mesh->getTypeOfCell(i);
2679       const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(type);
2680       if(cm.isDynamic())
2681         { std::ostringstream oss; oss << msg << i << " presence of dynamic cell (polygons and polyedrons) ! Not implemented !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); }
2682       int delta=cm.getNumberOfNodes();
2683       if(i<beginCellIds)
2684         beginOut+=delta;
2685       endOut+=delta;
2686       if(i>=endCellIds)
2687         break;
2688     }
2689   MCAuto<MEDCouplingMesh> ret=mesh->buildPartRange(beginCellIds,endCellIds,stepCellIds);
2690   return ret.retn();
2691 }
2692
2693
2694 /*!
2695  * This method returns a tuple ids selection from cell ids selection [start;end).
2696  * This method is called by MEDCouplingFieldDiscretizationGaussNE::buildSubMeshData to return parameter \b di.
2697  *
2698  * \return a newly allocated array containing ids to select into the DataArrayDouble of the field.
2699  * 
2700  */
2701 DataArrayInt *MEDCouplingFieldDiscretizationGaussNE::computeTupleIdsToSelectFromCellIds(const MEDCouplingMesh *mesh, const int *startCellIds, const int *endCellIds) const
2702 {
2703   if(!mesh)
2704     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationGaussNE::computeTupleIdsToSelectFromCellIds : null mesh !");
2705   MCAuto<DataArrayInt> nbOfNodesPerCell=mesh->computeNbOfNodesPerCell();
2706   nbOfNodesPerCell->computeOffsetsFull();
2707   MCAuto<DataArrayInt> sel=DataArrayInt::New(); sel->useArray(startCellIds,false,CPP_DEALLOC,(int)std::distance(startCellIds,endCellIds),1);
2708   return sel->buildExplicitArrByRanges(nbOfNodesPerCell);
2709 }
2710
2711 /*!
2712  * No implementation needed !
2713  */
2714 void MEDCouplingFieldDiscretizationGaussNE::renumberValuesOnNodes(double , const int *, int newNbOfNodes, DataArrayDouble *) const
2715 {
2716 }
2717
2718 void MEDCouplingFieldDiscretizationGaussNE::renumberValuesOnCells(double epsOnVals, const MEDCouplingMesh *mesh, const int *old2New, int newSz, DataArrayDouble *arr) const
2719 {
2720   throw INTERP_KERNEL::Exception("Not implemented yet !");
2721 }
2722
2723 void MEDCouplingFieldDiscretizationGaussNE::renumberValuesOnCellsR(const MEDCouplingMesh *mesh, const int *new2old, int newSz, DataArrayDouble *arr) const
2724 {
2725   throw INTERP_KERNEL::Exception("Not implemented yet !");
2726 }
2727
2728 void MEDCouplingFieldDiscretizationGaussNE::reprQuickOverview(std::ostream& stream) const
2729 {
2730   stream << "Gauss points on nodes per element spatial discretization.";
2731 }
2732
2733 MEDCouplingFieldDiscretizationGaussNE::MEDCouplingFieldDiscretizationGaussNE(const MEDCouplingFieldDiscretizationGaussNE& other):MEDCouplingFieldDiscretization(other)
2734 {
2735 }
2736
2737 TypeOfField MEDCouplingFieldDiscretizationKriging::getEnum() const
2738 {
2739   return TYPE;
2740 }
2741
2742 const char *MEDCouplingFieldDiscretizationKriging::getRepr() const
2743 {
2744   return REPR;
2745 }
2746
2747 /*!
2748  * This method is simply called by MEDCouplingFieldDiscretization::deepCopy. It performs the deep copy of \a this.
2749  *
2750  * \sa MEDCouplingFieldDiscretization::deepCopy.
2751  */
2752 MEDCouplingFieldDiscretization *MEDCouplingFieldDiscretizationKriging::clone() const
2753 {
2754   return new MEDCouplingFieldDiscretizationKriging;
2755 }
2756
2757 std::string MEDCouplingFieldDiscretizationKriging::getStringRepr() const
2758 {
2759   return std::string(REPR);
2760 }
2761
2762 void MEDCouplingFieldDiscretizationKriging::checkCompatibilityWithNature(NatureOfField nat) const
2763 {
2764   if(nat!=IntensiveMaximum)
2765     throw INTERP_KERNEL::Exception("Invalid nature for Kriging field : expected IntensiveMaximum !");
2766 }
2767
2768 bool MEDCouplingFieldDiscretizationKriging::isEqualIfNotWhy(const MEDCouplingFieldDiscretization *other, double eps, std::string& reason) const
2769 {
2770   if(!other)
2771     {
2772       reason="other spatial discretization is NULL, and this spatial discretization (Kriginig) is defined.";
2773       return false;
2774     }
2775   const MEDCouplingFieldDiscretizationKriging *otherC=dynamic_cast<const MEDCouplingFieldDiscretizationKriging *>(other);
2776   bool ret=otherC!=0;
2777   if(!ret)
2778     reason="Spatial discrtization of this is ON_NODES_KR, which is not the case of other.";
2779   return ret;
2780 }
2781
2782 MEDCouplingFieldDouble *MEDCouplingFieldDiscretizationKriging::getMeasureField(const MEDCouplingMesh *mesh, bool isAbs) const
2783 {
2784   if(!mesh)
2785     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationKriging::getMeasureField : mesh instance specified is NULL !");
2786   throw INTERP_KERNEL::Exception("getMeasureField on FieldDiscretizationKriging : not implemented yet !");
2787 }
2788
2789 void MEDCouplingFieldDiscretizationKriging::getValueOn(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, const double *loc, double *res) const
2790 {
2791   MCAuto<DataArrayDouble> res2=MEDCouplingFieldDiscretizationKriging::getValueOnMulti(arr,mesh,loc,1);
2792   std::copy(res2->begin(),res2->end(),res);
2793 }
2794
2795 DataArrayDouble *MEDCouplingFieldDiscretizationKriging::getValueOnMulti(const DataArrayDouble *arr, const MEDCouplingMesh *mesh, const double *loc, int nbOfTargetPoints) const
2796 {
2797   if(!arr || !arr->isAllocated())
2798     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationKriging::getValueOnMulti : input array is null or not allocated !");
2799   int nbOfRows(getNumberOfMeshPlaces(mesh));
2800   if(arr->getNumberOfTuples()!=nbOfRows)
2801     {
2802       std::ostringstream oss; oss << "MEDCouplingFieldDiscretizationKriging::getValueOnMulti : input array does not have correct number of tuples ! Excepted " << nbOfRows << " having " << arr->getNumberOfTuples() << " !";
2803       throw INTERP_KERNEL::Exception(oss.str().c_str());
2804     }
2805   int nbCols(-1),nbCompo(arr->getNumberOfComponents());
2806   MCAuto<DataArrayDouble> m(computeEvaluationMatrixOnGivenPts(mesh,loc,nbOfTargetPoints,nbCols));
2807   MCAuto<DataArrayDouble> ret(DataArrayDouble::New());
2808   ret->alloc(nbOfTargetPoints,nbCompo);
2809   INTERP_KERNEL::matrixProduct(m->begin(),nbOfTargetPoints,nbCols,arr->begin(),nbOfRows,nbCompo,ret->getPointer());
2810   return ret.retn();
2811 }
2812
2813 void MEDCouplingFieldDiscretizationKriging::reprQuickOverview(std::ostream& stream) const
2814 {
2815   stream << "Kriging spatial discretization.";
2816 }
2817
2818 /*!
2819  * Returns the matrix of size nbRows = \a nbOfTargetPoints and \a nbCols = \a nbCols. This matrix is useful if 
2820  * 
2821  * \return the new result matrix to be deallocated by the caller.
2822  */
2823 DataArrayDouble *MEDCouplingFieldDiscretizationKriging::computeEvaluationMatrixOnGivenPts(const MEDCouplingMesh *mesh, const double *loc, int nbOfTargetPoints, int& nbCols) const
2824 {
2825   int isDrift(-1),nbRows(-1);
2826   MCAuto<DataArrayDouble> matrixInv(computeInverseMatrix(mesh,isDrift,nbRows));
2827   //
2828   MCAuto<DataArrayDouble> coords=getLocalizationOfDiscValues(mesh);
2829   int nbOfPts(coords->getNumberOfTuples()),dimension(coords->getNumberOfComponents());
2830   MCAuto<DataArrayDouble> locArr=DataArrayDouble::New();
2831   locArr->useArray(loc,false,CPP_DEALLOC,nbOfTargetPoints,dimension);
2832   nbCols=nbOfPts;
2833   //
2834   MCAuto<DataArrayDouble> matrix2=coords->buildEuclidianDistanceDenseMatrixWith(locArr);
2835   operateOnDenseMatrix(mesh->getSpaceDimension(),nbOfTargetPoints*nbOfPts,matrix2->getPointer());
2836   //
2837   MCAuto<DataArrayDouble> matrix3=DataArrayDouble::New();
2838   matrix3->alloc(nbOfTargetPoints*nbRows,1);
2839   double *work=matrix3->getPointer();
2840   const double *workCst(matrix2->begin()),*workCst2(loc);
2841   for(int i=0;i<nbOfTargetPoints;i++,workCst+=nbOfPts,workCst2+=isDrift-1)
2842     {
2843       for(int j=0;j<nbOfPts;j++)
2844         work[i*nbRows+j]=workCst[j];
2845       work[i*nbRows+nbOfPts]=1.0;
2846       for(int j=0;j<isDrift-1;j++)
2847         work[i*nbRows+(nbOfPts+1+j)]=workCst2[j];
2848     }
2849   MCAuto<DataArrayDouble> ret(DataArrayDouble::New());
2850   ret->alloc(nbOfTargetPoints,nbRows);
2851   INTERP_KERNEL::matrixProduct(matrix3->begin(),nbOfTargetPoints,nbRows,matrixInv->begin(),nbRows,nbRows,ret->getPointer());
2852   MCAuto<DataArrayDouble> ret2(DataArrayDouble::New());
2853   ret2->alloc(nbOfTargetPoints*nbOfPts,1);
2854   workCst=ret->begin(); work=ret2->getPointer();
2855   for(int i=0;i<nbOfTargetPoints;i++,workCst+=nbRows)
2856     work=std::copy(workCst,workCst+nbOfPts,work);
2857   return ret2.retn();
2858 }
2859
2860 /*!
2861  * This method returns the square matrix of size \a matSz that is the inverse of the kriging matrix. The returned matrix can returned all the coeffs of kriging
2862  * when multiplied by the vector of values attached to each point.
2863  * 
2864  * \param [out] isDrift return if drift coefficients are present in the returned vector of coefficients. If different from 0 there is presence of drift coefficients.
2865  * \param [out] matSz the size of returned square matrix
2866  * \return the new result matrix to be deallocated by the caller.
2867  * \sa computeMatrix
2868  */
2869 DataArrayDouble *MEDCouplingFieldDiscretizationKriging::computeInverseMatrix(const MEDCouplingMesh *mesh, int& isDrift, int& matSz) const
2870 {
2871   MCAuto<DataArrayDouble> matrixWithDrift(computeMatrix(mesh,isDrift,matSz));
2872   MCAuto<DataArrayDouble> matrixInv(DataArrayDouble::New());
2873   matrixInv->alloc(matSz*matSz,1);
2874   INTERP_KERNEL::inverseMatrix(matrixWithDrift->getConstPointer(),matSz,matrixInv->getPointer());
2875   return matrixInv.retn();
2876 }
2877
2878 /*!
2879  * This method computes the kriging matrix.
2880  * \return the new result matrix to be deallocated by the caller.
2881  * \sa computeInverseMatrix
2882  */
2883 DataArrayDouble *MEDCouplingFieldDiscretizationKriging::computeMatrix(const MEDCouplingMesh *mesh, int& isDrift, int& matSz) const
2884 {
2885   if(!mesh)
2886       throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationKriging::computeMatrix : NULL input mesh !");
2887     MCAuto<DataArrayDouble> coords(getLocalizationOfDiscValues(mesh));
2888     int nbOfPts(coords->getNumberOfTuples());
2889     MCAuto<DataArrayDouble> matrix(coords->buildEuclidianDistanceDenseMatrix());
2890     operateOnDenseMatrix(mesh->getSpaceDimension(),nbOfPts*nbOfPts,matrix->getPointer());
2891     // Drift
2892     MCAuto<DataArrayDouble> matrixWithDrift(performDrift(matrix,coords,isDrift));
2893     matSz=nbOfPts+isDrift;
2894     return matrixWithDrift.retn();
2895 }
2896
2897 /*!
2898  * This method computes coefficients to apply to each representing points of \a mesh, that is to say the nodes of \a mesh given a field array \a arr whose
2899  * number of tuples should be equal to the number of representing points in \a mesh.
2900  * 
2901  * \param [in] mesh is the sources of nodes on which kriging will be done regarding the parameters and the value of \c this->getSpaceDimension()
2902  * \param [in] arr input field DataArrayDouble whose number of tuples must be equal to the number of nodes in \a mesh
2903  * \param [out] isDrift return if drift coefficients are present in the returned vector of coefficients. If different from 0 there is presence of drift coefficients.
2904  *              Whatever the value of \a isDrift the number of tuples of returned DataArrayDouble  will be equal to \c arr->getNumberOfTuples() + \a isDrift.
2905  * \return a newly allocated array containing coefficients including or not drift coefficient at the end depending the value of \a isDrift parameter.
2906  */
2907 DataArrayDouble *MEDCouplingFieldDiscretizationKriging::computeVectorOfCoefficients(const MEDCouplingMesh *mesh, const DataArrayDouble *arr, int& isDrift) const
2908 {
2909   int nbRows(-1);
2910   MCAuto<DataArrayDouble> matrixInv(computeInverseMatrix(mesh,isDrift,nbRows));
2911   MCAuto<DataArrayDouble> KnewiK(DataArrayDouble::New());
2912   KnewiK->alloc(nbRows*1,1);
2913   MCAuto<DataArrayDouble> arr2(PerformDriftOfVec(arr,isDrift));
2914   INTERP_KERNEL::matrixProduct(matrixInv->getConstPointer(),nbRows,nbRows,arr2->getConstPointer(),arr2->getNumberOfTuples(),1,KnewiK->getPointer());
2915   return KnewiK.retn();
2916 }
2917
2918 /*!
2919  * Apply \f f(x) on each element x in \a matrixPtr. \a matrixPtr is expected to be a dense matrix represented by a chunck of memory of size at least equal to \a nbOfElems.
2920  *
2921  * \param [in] spaceDimension space dimension of the input mesh on which the Kriging has to be performed
2922  * \param [in] nbOfElems is the result of the product of nb of rows and the nb of columns of matrix \a matrixPtr
2923  * \param [in,out] matrixPtr is the dense matrix whose on each values the operation will be applied
2924  */
2925 void MEDCouplingFieldDiscretizationKriging::operateOnDenseMatrix(int spaceDimension, int nbOfElems, double *matrixPtr) const
2926 {
2927   switch(spaceDimension)
2928   {
2929     case 1:
2930       {
2931         OperateOnDenseMatrixH3(nbOfElems,matrixPtr);
2932         break;
2933       }
2934     case 2:
2935       {
2936         OperateOnDenseMatrixH2Ln(nbOfElems,matrixPtr);
2937         break;
2938       }
2939     case 3:
2940       {
2941         //nothing here : it is not a bug g(h)=h with spaceDim 3.
2942         break;
2943       }
2944     default:
2945       throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationKriging::operateOnDenseMatrix : only dimension 1, 2 and 3 implemented !");
2946   }
2947 }
2948
2949 void MEDCouplingFieldDiscretizationKriging::OperateOnDenseMatrixH3(int nbOfElems, double *matrixPtr)
2950 {
2951   for(int i=0;i<nbOfElems;i++)
2952     {
2953       double val=matrixPtr[i];
2954       matrixPtr[i]=val*val*val;
2955     }
2956 }
2957
2958 void MEDCouplingFieldDiscretizationKriging::OperateOnDenseMatrixH2Ln(int nbOfElems, double *matrixPtr)
2959 {
2960   for(int i=0;i<nbOfElems;i++)
2961     {
2962       double val=matrixPtr[i];
2963       if(val!=0.)
2964         matrixPtr[i]=val*val*log(val);
2965     }
2966 }
2967
2968 /*!
2969  * Performs a drift to the rectangular input matrix \a matr.
2970  * This method generate a dense matrix starting from an input dense matrix \a matr and input array \a arr.
2971  * \param [in] matr The rectangular dense matrix (with only one component). The number of rows of \a matr must be equal to the number of tuples of \a arr
2972  * \param [in] arr The array of coords to be appended in the input dense matrix \a matr. Typically arr is an array of coordinates.
2973  * \param [out] delta the delta of number of columns between returned dense matrix and input dense matrix \a matr. \a delta is equal to number of components of \a arr + 1.
2974  * \sa performDrift
2975  */
2976 DataArrayDouble *MEDCouplingFieldDiscretizationKriging::PerformDriftRect(const DataArrayDouble *matr, const DataArrayDouble *arr, int& delta)
2977 {
2978   if(!matr || !matr->isAllocated() || matr->getNumberOfComponents()!=1)
2979     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationKriging::PerformDriftRect : invalid input dense matrix ! Must be allocated not NULL and with exactly one component !");
2980   if(!arr || !arr->isAllocated())
2981     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationKriging::PerformDriftRect : invalid input array of coordiantes ! Must be allocated and not NULL !");
2982   int spaceDimension(arr->getNumberOfComponents()),nbOfPts(arr->getNumberOfTuples()),nbOfEltInMatrx(matr->getNumberOfTuples());
2983   delta=spaceDimension+1;
2984   int nbOfCols(nbOfEltInMatrx/nbOfPts);
2985   if(nbOfEltInMatrx%nbOfPts!=0)
2986     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationKriging::PerformDriftRect : size of input dense matrix and input arrays mismatch ! NbOfElems in matrix % nb of tuples in array must be equal to 0 !");
2987   MCAuto<DataArrayDouble> ret(DataArrayDouble::New()); ret->alloc(nbOfPts*(nbOfCols+delta));
2988   double *retPtr(ret->getPointer());
2989   const double *mPtr(matr->begin()),*aPtr(arr->begin());
2990   for(int i=0;i<nbOfPts;i++,aPtr+=spaceDimension,mPtr+=nbOfCols)
2991     {
2992       retPtr=std::copy(mPtr,mPtr+nbOfCols,retPtr);
2993       *retPtr++=1.;
2994       retPtr=std::copy(aPtr,aPtr+spaceDimension,retPtr);
2995     }
2996   return ret.retn();
2997 }
2998
2999 /*!
3000  * \return a newly allocated array having \a isDrift more tuples than \a arr.
3001  * \sa computeVectorOfCoefficients
3002  */
3003 DataArrayDouble *MEDCouplingFieldDiscretizationKriging::PerformDriftOfVec(const DataArrayDouble *arr, int isDrift)
3004 {
3005   if(!arr || !arr->isAllocated() || arr->getNumberOfComponents()!=1)
3006     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationKriging::PerformDriftOfVec : input array must be not NULL allocated and with one component !");
3007   if(isDrift<0)
3008     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationKriging::PerformDriftOfVec : isDrift parameter must be >=0 !");
3009   MCAuto<DataArrayDouble> arr2(DataArrayDouble::New());
3010   arr2->alloc((arr->getNumberOfTuples()+isDrift)*1,1);
3011   double *work(std::copy(arr->begin(),arr->end(),arr2->getPointer()));
3012   std::fill(work,work+isDrift,0.);
3013   return arr2.retn();
3014 }
3015
3016 /*!
3017  * Starting from a square matrix \a matr, this method returns a newly allocated dense square matrix whose \a matr is included in returned matrix
3018  * in the top left corner, and in the remaining returned matrix the parameters to take into account about the kriging drift.
3019  * For the moment only linear srift is implemented.
3020  *
3021  * \param [in] arr the position of points were input mesh geometry is considered for Kriging
3022  * \param [in] matr input matrix whose drift part will be added
3023  * \param [out] delta the difference between the size of the output matrix and the input matrix \a matr.
3024  * \return a newly allocated matrix bigger than input matrix \a matr.
3025  * \sa MEDCouplingFieldDiscretizationKriging::PerformDriftRect
3026  */
3027 DataArrayDouble *MEDCouplingFieldDiscretizationKriging::performDrift(const DataArrayDouble *matr, const DataArrayDouble *arr, int& delta) const
3028 {
3029   int spaceDimension=arr->getNumberOfComponents();
3030   delta=spaceDimension+1;
3031   int szOfMatrix=arr->getNumberOfTuples();
3032   if(szOfMatrix*szOfMatrix!=matr->getNumberOfTuples())
3033     throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationKriging::performDrift : invalid size");
3034   MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
3035   ret->alloc((szOfMatrix+delta)*(szOfMatrix+delta),1);
3036   const double *srcWork=matr->getConstPointer();
3037   const double *srcWork2=arr->getConstPointer();
3038   double *destWork=ret->getPointer();
3039   for(int i=0;i<szOfMatrix;i++)
3040     {
3041       destWork=std::copy(srcWork,srcWork+szOfMatrix,destWork);
3042       srcWork+=szOfMatrix;
3043       *destWork++=1.;
3044       destWork=std::copy(srcWork2,srcWork2+spaceDimension,destWork);
3045       srcWork2+=spaceDimension;
3046     }
3047   std::fill(destWork,destWork+szOfMatrix,1.); destWork+=szOfMatrix;
3048   std::fill(destWork,destWork+spaceDimension+1,0.); destWork+=spaceDimension+1;
3049   MCAuto<DataArrayDouble> arrNoI=arr->toNoInterlace();
3050   srcWork2=arrNoI->getConstPointer();
3051   for(int i=0;i<spaceDimension;i++)
3052     {
3053       destWork=std::copy(srcWork2,srcWork2+szOfMatrix,destWork);
3054       srcWork2+=szOfMatrix;
3055       std::fill(destWork,destWork+spaceDimension+1,0.);
3056       destWork+=spaceDimension+1;
3057     }
3058   //
3059   return ret.retn();
3060 }