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