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