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