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