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