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