Salome HOME
Copyright update 2022
[tools/medcoupling.git] / src / MEDLoader / MEDFileBlowStrEltUp.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (EDF R&D)
20
21 #include "MEDFileBlowStrEltUp.hxx"
22 #include "MEDCouplingFieldDouble.hxx"
23 #include "MEDFileFieldVisitor.hxx"
24 #include "MEDCouplingPartDefinition.hxx"
25 #include "MCAuto.txx"
26 #include <numeric>
27
28 using namespace MEDCoupling;
29
30 const char MEDFileBlowStrEltUp::MED_BALL_STR[]="MED_BALL";
31
32 MEDFileBlowStrEltUp::MEDFileBlowStrEltUp(const MEDFileFields *fsOnlyOnSE, const MEDFileMeshes *ms, const MEDFileStructureElements *ses)
33 {
34   if(!fsOnlyOnSE || !ms || !ses)
35     throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp constructor : NULL input pointer !");
36   _ms.takeRef(ms); _ses.takeRef(ses);
37   std::vector< std::pair<std::string,std::string> > ps;
38   fsOnlyOnSE->getMeshSENames(ps);
39   std::size_t sz(ps.size());
40   _elts.resize(sz);
41   for(std::size_t i=0;i<sz;i++)
42     {
43       const std::pair<std::string,std::string>& p(ps[i]);
44       MCAuto<MEDFileFields> f(fsOnlyOnSE->partOfThisLyingOnSpecifiedMeshSEName(p.first,p.second));
45       _elts[i]=f;
46     }
47   for(std::size_t i=0;i<sz;i++)
48     {
49       const std::pair<std::string,std::string>& p(ps[i]);
50       MEDFileMesh *mesh(_ms->getMeshWithName(p.first));
51       if(!mesh)
52         throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp : NULL mesh !");
53       MEDFileUMesh *umesh(dynamic_cast<MEDFileUMesh *>(mesh));
54       if(!umesh)
55         throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp : Blow up of Stru Elt not managed yet for unstructured meshes !");
56     }
57 }
58
59 /*!
60  * \param [in] mesh - The mesh containing structure element called \a seName. After the call of this method the Structure elements parts will be removed.
61  * \param [out] mOut - the physical mesh of the structure element \a seName in mesh \a mesh
62  * \param [out] fsOut - the list of var attribute of structure element \a seName - \b WARNING no time steps here
63  */
64 MCAuto<MEDFileEltStruct4Mesh> MEDFileBlowStrEltUp::dealWithSEInMesh(const std::string& seName, MEDFileUMesh *mesh, MCAuto<MEDFileUMesh>& mOut, MCAuto<MEDFileFields>& fsOut) const
65 {
66   if(!mesh)
67     throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithSEInMesh : null pointer !");
68   if(seName==MED_BALL_STR)
69     {
70       MCAuto<MEDFileEltStruct4Mesh> ret(dealWithMEDBALLInMesh(mesh,mOut,fsOut));
71       mesh->killStructureElements();
72       return ret;
73     }
74   throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithSEInMesh : only MED_BALL is managed for the moment, but if you are interested please send spec to anthony.geay@edf.fr !");
75 }
76
77 MCAuto<MEDFileEltStruct4Mesh> MEDFileBlowStrEltUp::dealWithMEDBALLInMesh(const MEDFileUMesh *mesh, MCAuto<MEDFileUMesh>& mOut, MCAuto<MEDFileFields>& fsOut) const
78 {
79   mOut=MEDFileUMesh::New(); fsOut=MEDFileFields::New();
80   const std::vector< MCAuto<MEDFileEltStruct4Mesh> >& strs(mesh->getAccessOfUndergroundEltStrs());
81   MCAuto<MEDFileEltStruct4Mesh> zeStr;
82   for(std::vector< MCAuto<MEDFileEltStruct4Mesh> >::const_iterator it=strs.begin();it!=strs.end();it++)
83     {
84       if((*it)->getGeoTypeName()==MED_BALL_STR)
85         {
86           zeStr=*it;
87           break;
88         }
89     }
90   if(zeStr.isNull())
91     {
92       std::ostringstream oss; oss << "MEDFileBlowStrEltUp::dealWithMEDBALLInMesh : no geo type with name " <<  MED_BALL_STR << " in " << mesh->getName() << " !";
93       throw INTERP_KERNEL::Exception(oss.str());
94     }
95   const DataArrayDouble *coo(mesh->getCoords());
96   if(!coo)
97     throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithMEDBALLInMesh : null coords !");
98   MCAuto<DataArrayIdType> conn(zeStr->getConn());
99   if(conn.isNull())
100     throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithMEDBALLInMesh : null connectivity !");
101   conn->checkAllocated();
102   if(conn->getNumberOfComponents()!=1)
103     throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithMEDBALLInMesh : excepted to be single compo !");
104   MCAuto<DataArrayDouble> connOut(coo->selectByTupleIdSafe(conn->begin(),conn->end()));
105   MCAuto<MEDCouplingUMesh> mcOut(MEDCouplingUMesh::Build0DMeshFromCoords(connOut));
106   mcOut->setName(BuildNewMeshName(mesh->getName(),MED_BALL_STR));
107   mOut->setMeshAtLevel(0,mcOut);
108   const DataArrayIdType *ff1(mesh->getFamilyFieldAtLevel(1));
109   if(ff1)
110     {
111       MCAuto<DataArrayIdType> ff1o(ff1->selectByTupleIdSafe(conn->begin(),conn->end()));
112       mOut->setFamilyFieldArr(1,ff1o);
113     }
114   const DataArrayIdType *nf1(mesh->getNumberFieldAtLevel(1));
115   if(nf1)
116     {
117       MCAuto<DataArrayIdType> nf1o(nf1->selectByTupleIdSafe(conn->begin(),conn->end()));
118       mOut->setRenumFieldArr(1,nf1o);
119     }
120   MCAuto<MEDFileUMeshPerTypeCommon> md(zeStr->getMeshDef());
121   const DataArrayIdType *ff0(md->getFam());
122   if(ff0)
123     mOut->setFamilyFieldArr(0,const_cast<DataArrayIdType *>(ff0));
124   const DataArrayIdType *nf0(md->getNum());
125   if(nf0)
126     mOut->setRenumFieldArr(0,const_cast<DataArrayIdType *>(nf0));
127   mOut->copyFamGrpMapsFrom(*mesh);
128   const std::vector< MCAuto<DataArray> >& vars(zeStr->getVars());
129   for(std::vector< MCAuto<DataArray> >::const_iterator it=vars.begin();it!=vars.end();it++)
130     {
131       const DataArray *elt(*it);
132       if(!elt)
133         continue;
134       {
135         const DataArrayDouble *eltC(dynamic_cast<const DataArrayDouble *>(elt));
136         if(eltC)
137           {
138             MCAuto<MEDFileFieldMultiTS> fmts(MEDFileFieldMultiTS::New());
139             MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
140             MCAuto<MEDCouplingFieldDouble> f(MEDCouplingFieldDouble::New(ON_NODES));
141             f->setMesh(mcOut);
142             f->setArray(const_cast<DataArrayDouble *>(eltC));
143             f->setName(eltC->getName());
144             f1ts->setFieldNoProfileSBT(f);
145             fmts->pushBackTimeStep(f1ts);
146             fsOut->pushField(fmts);
147           }
148       }
149     }
150   return zeStr;
151 }
152
153 /*!
154  * \param [in] fs - fields lying all on same mesh and on same structure element
155  * \param [in] zeStr - ze structure of current structure element
156  * \param [in] varAtt - fields containing var att of current structure element. WARNING at this stage the number of iteration are equal to one for each field in \a varAtt
157  * \param [out] zeOutputs - ze fields that are the concatenation of fields in \a fs transformed and those in \a varAtt normalized in time space
158  */
159 void MEDFileBlowStrEltUp::dealWithSEInFields(const std::string& seName, const MEDFileFields *fs, const MEDFileEltStruct4Mesh *zeStr, const MEDFileFields *varAtt, MEDFileFields *zeOutputs) const
160 {
161   if(!fs)
162     throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithSEInFields : null pointer !");
163   if(seName==MED_BALL_STR)
164     {
165       dealWithMEDBALLSInFields(fs,zeStr,varAtt,zeOutputs);
166       return ;
167     }
168   throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithSEInFields : only MED_BALL is managed for the moment, but if you are interested please send spec to anthony.geay@edf.fr !");
169 }
170
171 void MEDFileBlowStrEltUp::dealWithMEDBALLSInFields(const MEDFileFields *fs, const MEDFileEltStruct4Mesh *zeStr, const MEDFileFields *varAtt, MEDFileFields *zeOutputs) const
172 {
173   int nbf(fs->getNumberOfFields());
174   std::vector< MCAuto<MEDFileAnyTypeFieldMultiTS> > elts0;
175   std::vector< MEDFileAnyTypeFieldMultiTS * > elts1;
176   std::string zeMeshName;
177   for(int i=0;i<nbf;i++)
178     {
179       MCAuto<MEDFileAnyTypeFieldMultiTS> elt(fs->getFieldAtPos(i));
180       MCAuto<MEDFileAnyTypeFieldMultiTS> eltOut(elt->buildNewEmpty());
181       int nbTS(elt->getNumberOfTS());
182       for(int j=0;j<nbTS;j++)
183         {
184           MCAuto<MEDFileAnyTypeField1TS> eltt(elt->getTimeStepAtPos(j));
185           MCAuto<MEDFileAnyTypeField1TS> elttOut(eltt->deepCopy());
186           std::string meshName(eltt->getMeshName());
187           zeMeshName=BuildNewMeshName(meshName,MED_BALL_STR);
188           elttOut->setMeshName(zeMeshName);
189           elttOut->convertMedBallIntoClassic();
190           eltOut->pushBackTimeStep(elttOut);
191         }
192       elts0.push_back(eltOut); elts1.push_back(eltOut);
193     }
194   //
195   const MEDFileMesh *zeCurrentMesh(_ms->getMeshWithName(zeMeshName));
196   //
197   std::size_t ii(0);
198   std::vector< std::vector<MEDFileAnyTypeFieldMultiTS *> > sp(MEDFileAnyTypeFieldMultiTS::SplitIntoCommonTimeSeries(elts1));
199   for(std::vector< std::vector<MEDFileAnyTypeFieldMultiTS *> >::const_iterator it0=sp.begin();it0!=sp.end();it0++,ii++)
200     {
201       std::vector< MCAuto<MEDFileFastCellSupportComparator> > fsc;
202       std::vector< std::vector<MEDFileAnyTypeFieldMultiTS *> > sp2(MEDFileAnyTypeFieldMultiTS::SplitPerCommonSupport(*it0,zeCurrentMesh,fsc));
203       std::size_t jj(0);
204       for(std::vector< std::vector<MEDFileAnyTypeFieldMultiTS *> >::const_iterator it1=sp2.begin();it1!=sp2.end();it1++,jj++)
205         {
206           for(std::vector<MEDFileAnyTypeFieldMultiTS *>::const_iterator it2=(*it1).begin();it2!=(*it1).end();it2++)
207             zeOutputs->pushField(*it2);
208           // The most exciting part. Users that put profiles on struct elements part of fields. Reduce var att.
209           if((*it1).size()<1)
210             throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithMEDBALLSInFields : take a deep breath !");
211           MCAuto<MEDFileAnyTypeField1TS> zeGuideForPfl;// This var is the reference for pfl management.
212           {
213             if(!(*it1)[0])
214               throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithMEDBALLSInFields : take a deep breath 2 !");
215             int pdm((*it1)[0]->getNumberOfTS());
216             if(pdm<1)
217               throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithMEDBALLSInFields : take a deep breath 3 !");
218             zeGuideForPfl=(*it1)[0]->getTimeStepAtPos(0);
219           }
220           if(zeGuideForPfl.isNull())
221             throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithMEDBALLSInFields : take a deep breath 4 !");
222           std::vector<std::string> pfls(zeGuideForPfl->getPflsReallyUsed());
223           if(pfls.size()>=2)
224             throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithMEDBALLSInFields : drink less coffee");
225           MCAuto<DataArrayIdType> pflMyLove;
226           if(pfls.size()==1)
227             pflMyLove.takeRef(zeGuideForPfl->getProfile(pfls[0]));
228           // Yeah we have pfls
229           std::vector<double> t2s;
230           std::vector< std::pair<int,int> > t1s((*it1)[0]->getTimeSteps(t2s));
231           std::size_t nbTS3(t2s.size());
232           int nbf2(varAtt->getNumberOfFields());
233           for(int i=0;i<nbf2;i++)
234             {
235               MCAuto<MEDFileAnyTypeFieldMultiTS> elt(varAtt->getFieldAtPos(i));
236               int nbTS2(elt->getNumberOfTS());
237               if(nbTS2!=1)
238                 throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::dealWithMEDBALLSInFields : internal error ! The dealWithMEDBALLInMesh is expected to return a single TS !");
239               MCAuto<MEDFileAnyTypeField1TS> elt2(elt->getTimeStepAtPos(0));
240               MCAuto<MEDFileAnyTypeFieldMultiTS> elt4(elt->buildNewEmpty());
241               for(std::size_t j=0;j<nbTS3;j++)
242                 {
243                   MCAuto<MEDFileAnyTypeField1TS> elt3(elt2->deepCopy());
244                   elt3->setTime(t1s[j].first,t1s[j].second,t2s[j]);
245                   elt3->setName(BuildVarAttName(ii,sp.size(),jj,sp2.size(),elt3->getName()));
246                   if(pflMyLove.isNotNull())
247                     elt3->makeReduction(INTERP_KERNEL::NORM_ERROR,ON_NODES,pflMyLove);
248                   elt4->pushBackTimeStep(elt3);
249                 }
250               zeOutputs->pushField(elt4);
251             }
252         }
253     }
254 }
255
256 void MEDFileBlowStrEltUp::generate(MEDFileMeshes *msOut, MEDFileFields *allZeOutFields)
257 {
258   for(std::vector< MCAuto<MEDFileFields> >::iterator elt=_elts.begin();elt!=_elts.end();elt++)
259     {
260       std::vector< std::pair<std::string,std::string> > ps;
261       (*elt)->getMeshSENames(ps);
262       if(ps.size()!=1)
263         throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::generateMeshes : internal error !");
264       MEDFileMesh *mesh(_ms->getMeshWithName(ps[0].first));
265       if(!mesh)
266         throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::generateMeshes : NULL mesh !");
267       MEDFileUMesh *umesh(dynamic_cast<MEDFileUMesh *>(mesh));
268       if(!umesh)
269         throw INTERP_KERNEL::Exception("MEDFileBlowStrEltUp::generateMeshes : Blow up of Stru Elt not managed yet for unstructured meshes !");
270       //
271       MCAuto<MEDFileFields> classicalSEFields(splitFieldsPerLoc(*elt,umesh,msOut,allZeOutFields));
272       if(classicalSEFields.isNotNull())
273         {
274           MCAuto<MEDFileUMesh> mOut;
275           MCAuto<MEDFileFields> fsOut1;
276           MCAuto<MEDFileEltStruct4Mesh> zeStr(dealWithSEInMesh(ps[0].second,umesh,mOut,fsOut1));
277           msOut->pushMesh(mOut);
278           dealWithSEInFields(ps[0].second,classicalSEFields,zeStr,fsOut1,allZeOutFields);
279         }
280     }
281 }
282
283 std::string MEDFileBlowStrEltUp::BuildNewMeshName(const std::string& meshName, const std::string& seName)
284 {
285   std::ostringstream mNameOut;
286   mNameOut << meshName << "_" << seName;
287   return mNameOut.str();
288 }
289
290 std::string MEDFileBlowStrEltUp::BuildVarAttName(std::size_t iPart, std::size_t totINbParts, std::size_t jPart, std::size_t totJNbParts, const std::string& name)
291 {
292   if(totINbParts==1 && totJNbParts==1)
293     return name;
294   std::ostringstream oss;
295   oss << name << "@" << iPart << "@" << jPart;
296   return oss.str();
297 }
298
299 void MEDFileBlowStrEltUp::DealWithSE(MEDFileFields *fs, MEDFileMeshes *ms, const MEDFileStructureElements *ses)
300 {
301   MCAuto<MEDFileFields> fsSEOnly(fs->partOfThisOnStructureElements());
302   fs->killStructureElements();
303   MEDFileBlowStrEltUp bu(fsSEOnly,ms,ses);
304   bu.generate(ms,fs);
305   fs->killStructureElementsInGlobs();
306 }
307
308 //
309
310 class FieldWalker2
311 {
312 public:
313   FieldWalker2(const MEDFileFieldPerMeshPerTypePerDisc *pmptpd);
314   std::string getLoc() const { return _loc; }
315   std::string getPfl() const { return _pfl; }
316   INTERP_KERNEL::NormalizedCellType getGeoType() const { return _ct; }
317   bool isClassic() const { return _is_classic; }
318   bool operator!=(const FieldWalker2& other) const;
319   bool operator==(const FieldWalker2& other) const;
320   const SlicePartDefinition *getPartDef() const { return _pd; }
321 private:
322   std::string _loc;
323   std::string _pfl;
324   INTERP_KERNEL::NormalizedCellType _ct;
325   bool _is_classic;
326   MCAuto<SlicePartDefinition> _pd;
327 };
328
329 class LocInfo
330 {
331 public:
332   LocInfo() { }
333   LocInfo(const std::vector<FieldWalker2>& fw);
334   bool operator==(const LocInfo& other) const { return _locs==other._locs && _pfl==other._pfl; }
335   void push(const std::string& loc, const std::string& pfl) { checkUniqueLoc(loc); _locs.push_back(loc); _pfl.push_back(pfl); }
336   MCAuto<MEDFileUMesh> generateNonClassicalData(int zePos, const MEDFileUMesh *mesh, const MEDFileFieldGlobsReal *globs) const;
337   const PartDefinition *getPartDef() const { return _pd; }
338 private:
339   void checkUniqueLoc(const std::string& loc) const;
340   static MCAuto<DataArrayDouble> BuildMeshFromAngleVrille(INTERP_KERNEL::NormalizedCellType gt, const DataArrayDouble *angleDeVrille, const std::string& pfl, const MEDFileFieldLoc& loc, const MEDFileEltStruct4Mesh *zeStr, const MEDFileUMesh *mesh, const MEDFileUMesh *section, const MEDFileFieldGlobsReal *globs);
341   static MCAuto<DataArrayDouble> BuildMeshFromEpaisseur(INTERP_KERNEL::NormalizedCellType gt, const DataArrayDouble *thickness, const std::string& pfl, const MEDFileFieldLoc& loc, const MEDFileEltStruct4Mesh *zeStr, const MEDFileUMesh *mesh, const MEDFileUMesh *section, const MEDFileFieldGlobsReal *globs);
342   static MCAuto<DataArrayDouble> BuildMeshPipeSEG3(const DataArrayDouble *angle, const DataArrayDouble *scale, const std::string& pfl, const MEDFileFieldLoc& loc, const MEDFileEltStruct4Mesh *zeStr, const MEDFileUMesh *mesh, const MEDFileUMesh *section, const MEDFileFieldGlobsReal *globs);
343   static MCAuto<MEDCouplingUMesh> BuildMeshCommon(INTERP_KERNEL::NormalizedCellType gt, const std::string& pfl, const MEDFileFieldLoc& loc, const MEDFileEltStruct4Mesh *zeStr, const MEDFileUMesh *mesh, const MEDFileUMesh *section, const MEDFileFieldGlobsReal *globs, MCAuto<DataArrayDouble>& ptsForLoc);
344   static MCAuto<DataArrayDouble> BuildMeshFromStructure(INTERP_KERNEL::NormalizedCellType gt, const std::string& pfl, const MEDFileFieldLoc& loc, const MEDFileEltStruct4Mesh *zeStr, const MEDFileUMesh *mesh, const MEDFileUMesh *section, const MEDFileFieldGlobsReal *globs);
345 public:
346   static const char ANGLE_DE_VRILLE[];
347   static const char ANGLE[];
348   static const char SCALE[];
349   static const char EPAISSEUR[];
350 private:
351   std::vector<std::string> _locs;
352   std::vector<std::string> _pfl;
353   std::vector<INTERP_KERNEL::NormalizedCellType> _cts;
354   MCAuto<PartDefinition> _pd;
355 };
356
357 const char LocInfo::ANGLE_DE_VRILLE[]="ANGLE DE VRILLE";
358
359 const char LocInfo::ANGLE[]="ANGLE";
360
361 const char LocInfo::SCALE[]="SCALE";
362
363 const char LocInfo::EPAISSEUR[]="EPAISSEUR";
364
365 LocInfo::LocInfo(const std::vector<FieldWalker2>& fw)
366 {
367   std::size_t sz(fw.size());
368   _locs.resize(sz); _pfl.resize(sz); _cts.resize(sz);
369   if(sz>0)
370     _pd=fw[0].getPartDef()->deepCopy();
371   for(std::size_t i=0;i<sz;i++)
372     {
373       _locs[i]=fw[i].getLoc();
374       _pfl[i]=fw[i].getPfl();
375       _cts[i]=fw[i].getGeoType();
376       if(i>0)
377         _pd=(*_pd)+(*(fw[i].getPartDef()));
378     }
379 }
380
381 void LocInfo::checkUniqueLoc(const std::string& loc) const
382 {
383   if(std::find(_locs.begin(),_locs.end(),loc)!=_locs.end())
384     {
385       std::ostringstream oss; oss << "LocInfo::checkUniqueLoc : loc \"" << loc << "\" already exists !";
386       throw INTERP_KERNEL::Exception(oss.str());
387     }
388 }
389
390 MCAuto<MEDCouplingUMesh> LocInfo::BuildMeshCommon(INTERP_KERNEL::NormalizedCellType gt, const std::string& pfl, const MEDFileFieldLoc& loc, const MEDFileEltStruct4Mesh *zeStr, const MEDFileUMesh *mesh, const MEDFileUMesh *section, const MEDFileFieldGlobsReal *globs, MCAuto<DataArrayDouble>& ptsForLoc)
391 {
392   MCAuto<DataArrayIdType> conn(zeStr->getConn());
393   conn=conn->deepCopy(); conn->rearrange(1);
394   MCAuto<MEDCouplingUMesh> geoMesh;
395   {
396     MCAuto<MEDCoupling1SGTUMesh> umesh(MEDCoupling1SGTUMesh::New("",gt));
397     umesh->setCoords(mesh->getCoords());
398     umesh->setNodalConnectivity(conn);
399     geoMesh=umesh->buildUnstructured();
400   }
401   //
402   if(!pfl.empty())
403     {
404       const DataArrayIdType *pflArr(globs->getProfile(pfl));
405       geoMesh=geoMesh->buildPartOfMySelf(pflArr->begin(),pflArr->end(),true);
406     }
407   //
408   MCAuto<MEDCouplingFieldDouble> fakeF(MEDCouplingFieldDouble::New(ON_GAUSS_PT));
409   fakeF->setMesh(geoMesh);
410   fakeF->setGaussLocalizationOnType(gt,loc.getRefCoords(),loc.getGaussCoords(),loc.getGaussWeights());
411   ptsForLoc=fakeF->getLocalizationOfDiscr();
412   //
413   return geoMesh;
414 }
415
416 MCAuto<DataArrayDouble> LocInfo::BuildMeshFromAngleVrille(INTERP_KERNEL::NormalizedCellType gt, const DataArrayDouble *angleDeVrille, const std::string& pfl, const MEDFileFieldLoc& loc, const MEDFileEltStruct4Mesh *zeStr, const MEDFileUMesh *mesh, const MEDFileUMesh *section, const MEDFileFieldGlobsReal *globs)
417 {
418   MCAuto<DataArrayDouble> ptsForLoc;
419   MCAuto<MEDCouplingUMesh> geoMesh(BuildMeshCommon(gt,pfl,loc,zeStr,mesh,section,globs,ptsForLoc));
420   //
421   MCConstAuto<DataArrayDouble> angleVrille;
422   if(!pfl.empty())
423     {
424       const DataArrayIdType *pflArr(globs->getProfile(pfl));
425       angleVrille=angleDeVrille->selectByTupleIdSafe(pflArr->begin(),pflArr->end());
426     }
427   else
428     angleVrille.takeRef(angleDeVrille);
429   //
430   MCAuto<MEDCouplingFieldDouble> dir(geoMesh->buildDirectionVectorField());
431   MCAuto<DataArrayDouble> rot(dir->getArray()->fromCartToSpher());
432   std::size_t nbCompo(ptsForLoc->getNumberOfComponents());
433   MCAuto<DataArrayDouble> secPts(section->getCoords()->changeNbOfComponents(nbCompo,0.));
434   mcIdType nbSecPts(secPts->getNumberOfTuples()),nbCells(geoMesh->getNumberOfCells()),nbg(ToIdType(loc.getGaussWeights().size()));
435   {
436     const int TAB[3]={2,0,1};
437     std::vector<std::size_t> v(TAB,TAB+3);
438     secPts=secPts->keepSelectedComponents(v);
439   }
440   const double CENTER[3]={0.,0.,0.},AX0[3]={0.,0.,1.};
441   double AX1[3]; AX1[2]=0.;
442   std::vector< MCAuto<DataArrayDouble> > arrs(nbCells*nbg);
443   for(int j=0;j<nbCells;j++)
444     {
445       MCAuto<DataArrayDouble> p(secPts->deepCopy());
446       double ang0(rot->getIJ(j,2));
447       DataArrayDouble::Rotate3DAlg(CENTER,AX0,ang0,nbSecPts,p->begin(),p->getPointer());
448       AX1[0]=-sin(ang0); AX1[1]=cos(ang0);// rot Oy around OZ
449       double ang1(M_PI/2.-rot->getIJ(j,1));
450       DataArrayDouble::Rotate3DAlg(CENTER,AX1,-ang1,nbSecPts,p->begin(),p->getPointer());
451       DataArrayDouble::Rotate3DAlg(CENTER,dir->getArray()->begin()+j*3,angleVrille->getIJ(j,0),nbSecPts,p->begin(),p->getPointer());
452       for(int l=0;l<nbg;l++)
453         {
454           MCAuto<DataArrayDouble> p2(p->deepCopy());
455           for(std::size_t k=0;k<nbCompo;k++)
456             p2->applyLin(1.,ptsForLoc->getIJ(j*nbg+l,k),k);
457           arrs[j*nbg+l]=p2;
458         }
459     }
460   std::vector<const DataArrayDouble *> arrs2(VecAutoToVecOfCstPt(arrs));
461   MCAuto<DataArrayDouble> resu(DataArrayDouble::Aggregate(arrs2));
462   return resu;
463 }
464
465 MCAuto<DataArrayDouble> LocInfo::BuildMeshFromEpaisseur(INTERP_KERNEL::NormalizedCellType gt, const DataArrayDouble *thickness, const std::string& pfl, const MEDFileFieldLoc& loc, const MEDFileEltStruct4Mesh *zeStr, const MEDFileUMesh *mesh, const MEDFileUMesh *section, const MEDFileFieldGlobsReal *globs)
466 {
467   MCAuto<DataArrayDouble> ptsForLoc;
468   MCAuto<MEDCouplingUMesh> geoMesh(BuildMeshCommon(gt,pfl,loc,zeStr,mesh,section,globs,ptsForLoc));
469   mcIdType nbCells(geoMesh->getNumberOfCells()),nbg(ToIdType(loc.getGaussWeights().size()));
470   MCConstAuto<DataArrayDouble> zeThickness;
471   if(!pfl.empty())
472     {
473       const DataArrayIdType *pflArr(globs->getProfile(pfl));
474       zeThickness=thickness->selectByTupleIdSafe(pflArr->begin(),pflArr->end());
475     }
476   else
477     zeThickness.takeRef(thickness);
478   MCAuto<DataArrayDouble> orthoArr;
479   {
480     MCAuto<MEDCouplingFieldDouble> ortho(geoMesh->buildOrthogonalField());
481     orthoArr.takeRef(ortho->getArray());
482   }
483   mcIdType nbCompo(ToIdType(orthoArr->getNumberOfComponents()));
484   MCAuto<DataArrayDouble> secPts(section->getCoords()->duplicateEachTupleNTimes(nbCompo));
485   secPts->rearrange(nbCompo);
486   std::vector< MCAuto<DataArrayDouble> > arrs(nbCells*nbg);
487   for(mcIdType j=0;j<nbCells;j++)
488     {
489       double thck(zeThickness->getIJ(j,0)),eccentricity(zeThickness->getIJ(j,1));
490       MCAuto<DataArrayDouble> fact(DataArrayDouble::New()),fact2(DataArrayDouble::New()); fact->alloc(1,nbCompo); fact2->alloc(1,nbCompo);
491       std::copy(orthoArr->begin()+j*nbCompo,orthoArr->begin()+(j+1)*nbCompo,fact->getPointer());
492       std::copy(orthoArr->begin()+j*nbCompo,orthoArr->begin()+(j+1)*nbCompo,fact2->getPointer());
493       std::for_each(fact2->rwBegin(),fact2->rwEnd(),[eccentricity](double& val){ val*=eccentricity; });
494       std::transform(fact->begin(),fact->end(),fact->getPointer(),[thck](const double& val){ return val*thck/2.; });
495       MCAuto<DataArrayDouble> p(DataArrayDouble::Multiply(secPts,fact));
496       p=DataArrayDouble::Add(p,fact2);
497       for(int l=0;l<nbg;l++)
498         {
499           MCAuto<DataArrayDouble> p2(p->deepCopy());
500           for(int k=0;k<nbCompo;k++)
501             p2->applyLin(1.,ptsForLoc->getIJ(j*nbg+l,k),k);
502           arrs[j*nbg+l]=p2;
503         }
504     }
505   std::vector<const DataArrayDouble *> arrs2(VecAutoToVecOfCstPt(arrs));
506   MCAuto<DataArrayDouble> resu(DataArrayDouble::Aggregate(arrs2));
507   return resu;
508 }
509
510 MCAuto<DataArrayDouble> LocInfo::BuildMeshPipeSEG3(const DataArrayDouble *angle, const DataArrayDouble *scale, const std::string& pfl, const MEDFileFieldLoc& loc, const MEDFileEltStruct4Mesh *zeStr, const MEDFileUMesh *mesh, const MEDFileUMesh *section, const MEDFileFieldGlobsReal *globs)
511 {
512   static const char MSG1[]="BuildMeshPipeSEG3 : not recognized pattern ! Send mail to anthony.geay@edf.fr with corresponding MED file !";
513   MCAuto<DataArrayDouble> ptsForLoc;
514   MCAuto<MEDCouplingUMesh> geoMesh(BuildMeshCommon(INTERP_KERNEL::NORM_SEG3,pfl,loc,zeStr,mesh,section,globs,ptsForLoc));
515   mcIdType nbSecPts(section->getNumberOfNodes()),nbCells(geoMesh->getNumberOfCells()),nbg(ToIdType(loc.getGaussWeights().size()));
516   MCConstAuto<DataArrayDouble> zeAngle,zeScale;
517   if(!pfl.empty())
518     {
519       const DataArrayIdType *pflArr(globs->getProfile(pfl));
520       zeAngle=angle->selectByTupleIdSafe(pflArr->begin(),pflArr->end());
521       zeScale=scale->selectByTupleIdSafe(pflArr->begin(),pflArr->end());
522     }
523   else
524     {
525       zeAngle.takeRef(angle);
526       zeScale.takeRef(scale);
527     }
528   if(zeAngle->getNumberOfComponents()!=3 || zeScale->getNumberOfComponents()!=2 || nbg!=3)
529     throw INTERP_KERNEL::Exception(MSG1);
530   MCAuto<MEDCouplingFieldDouble> dir;
531   {
532     MCAuto<MEDCouplingUMesh> geoMesh2(geoMesh->deepCopy());
533     geoMesh2->convertQuadraticCellsToLinear();
534     dir=geoMesh2->buildDirectionVectorField();
535   }
536   MCAuto<DataArrayDouble> rot(dir->getArray()->fromCartToSpher());
537   std::size_t nbCompo(ptsForLoc->getNumberOfComponents());
538   MCAuto<DataArrayDouble> secPts(section->getCoords()->changeNbOfComponents(nbCompo,0.));
539   {
540     const int TAB[3]={2,0,1};
541     std::vector<std::size_t> v(TAB,TAB+3);
542     secPts=secPts->keepSelectedComponents(v);
543   }
544   const double CENTER[3]={0.,0.,0.},AX0[3]={0.,0.,1.};
545   double AX1[3]; AX1[2]=0.;
546   std::vector< MCAuto<DataArrayDouble> > arrs(nbCells*nbg);
547   for(mcIdType j=0;j<nbCells;j++)
548     {
549       constexpr int DIM=3;
550       MCAuto<DataArrayDouble> p(secPts->deepCopy());
551       double ang0(rot->getIJ(j,2));
552       double rmin(zeScale->getIJ(j,0)),rmax(zeScale->getIJ(j,1));
553       {
554         auto pt(p->rwBegin());
555         for(int i=0;i<nbSecPts;i++)
556           {
557             auto nrm(sqrt(std::accumulate(pt,pt+DIM,0.,[](double sum, double v) { return sum+v*v; } )));
558             auto sca((rmin+2.*(nrm-0.5)*(rmax-rmin))/nrm);
559             std::for_each(pt,pt+3,[sca](double& val) { val*=sca; } );
560             std::advance(pt,DIM);
561           }
562       }
563       DataArrayDouble::Rotate3DAlg(CENTER,AX0,ang0,nbSecPts,p->begin(),p->getPointer());
564       AX1[0]=-sin(ang0); AX1[1]=cos(ang0);// rot Oy around OZ
565       double ang1(M_PI/2.-rot->getIJ(j,1));
566       DataArrayDouble::Rotate3DAlg(CENTER,AX1,-ang1,nbSecPts,p->begin(),p->getPointer());
567       for(int l=0;l<3;l++)
568         {
569           MCAuto<DataArrayDouble> p3(p->deepCopy());
570           DataArrayDouble::Rotate3DAlg(CENTER,dir->getArray()->begin()+j*3,zeAngle->getIJ(j,l),nbSecPts,p3->begin(),p3->getPointer());
571           MCAuto<DataArrayDouble> p2(p3->deepCopy());
572           for(std::size_t k=0;k<nbCompo;k++)
573             p2->applyLin(1.,ptsForLoc->getIJ(j*nbg+l,k),k);
574           arrs[j*nbg+l]=p2;
575         }
576     }
577   std::vector<const DataArrayDouble *> arrs2(VecAutoToVecOfCstPt(arrs));
578   MCAuto<DataArrayDouble> resu(DataArrayDouble::Aggregate(arrs2));
579   return resu;
580 }
581
582 MCAuto<DataArrayDouble> LocInfo::BuildMeshFromStructure(INTERP_KERNEL::NormalizedCellType gt, const std::string& pfl, const MEDFileFieldLoc& loc, const MEDFileEltStruct4Mesh *zeStr, const MEDFileUMesh *mesh, const MEDFileUMesh *section, const MEDFileFieldGlobsReal *globs)
583 {
584   static const char MSG1[]="BuildMeshFromStructure : not recognized pattern ! Send mail to anthony.geay@edf.fr with corresponding MED file !";
585   const std::vector< MCAuto<DataArray> >& vars(zeStr->getVars());
586   if(vars.size()==1)
587     {
588       MCAuto<DataArray> zeArr(vars[0]);
589       if(zeArr.isNull())
590         throw INTERP_KERNEL::Exception(MSG1);
591       MCAuto<DataArrayDouble> zeArr2(DynamicCast<DataArray,DataArrayDouble>(zeArr));
592       if(zeArr2.isNull())
593         throw INTERP_KERNEL::Exception(MSG1);
594       if(zeArr2->getName()==ANGLE_DE_VRILLE || zeArr2->getName()==ANGLE)
595         return BuildMeshFromAngleVrille(gt,zeArr2,pfl,loc,zeStr,mesh,section,globs);
596       if(zeArr2->getName()==EPAISSEUR || zeArr2->getName()==SCALE)
597         return BuildMeshFromEpaisseur(gt,zeArr2,pfl,loc,zeStr,mesh,section,globs);
598     }
599   if(vars.size()==2)
600     {
601       MCAuto<DataArray> zeArr0(vars[0]),zeArr1(vars[1]);
602       if(zeArr0.isNull() || zeArr1.isNull())
603         throw INTERP_KERNEL::Exception(MSG1);
604       MCAuto<DataArrayDouble> zeArr00(DynamicCastSafe<DataArray,DataArrayDouble>(zeArr0)),zeArr11(DynamicCastSafe<DataArray,DataArrayDouble>(zeArr1));
605       switch(gt)
606       {
607         case INTERP_KERNEL::NORM_SEG3:
608           {
609             MCAuto<DataArrayDouble> angle,scale;
610             if(zeArr00->getName()==ANGLE)
611               angle=zeArr00;
612             if(zeArr00->getName()==SCALE)
613               scale=zeArr00;
614             if(zeArr11->getName()==ANGLE)
615               angle=zeArr11;
616             if(zeArr11->getName()==SCALE)
617               scale=zeArr11;
618             if(angle.isNull() || scale.isNull())
619               throw INTERP_KERNEL::Exception(MSG1);
620             return BuildMeshPipeSEG3(angle,scale,pfl,loc,zeStr,mesh,section,globs);
621           }
622         default:
623           throw INTERP_KERNEL::Exception(MSG1);
624       }
625     }
626   throw INTERP_KERNEL::Exception(MSG1);
627 }
628
629 MCAuto<MEDFileUMesh> LocInfo::generateNonClassicalData(int zePos, const MEDFileUMesh *mesh, const MEDFileFieldGlobsReal *globs) const
630 {
631   static const char MSG1[]="LocInfo::generateNonClassicalData : no spec for GAUSS on StructureElement with more than one cell !";
632   std::size_t sz(_locs.size());
633   std::vector< MCAuto<DataArrayDouble> > arrs(sz);
634   for(std::size_t i=0;i<sz;i++)
635     {
636       const MEDFileFieldLoc& loc(globs->getLocalization(_locs[i]));
637       const MEDFileGTKeeper *gtk(loc.getUndergroundGTKeeper());
638       const MEDFileGTKeeperDyn *gtk2(dynamic_cast<const MEDFileGTKeeperDyn *>(gtk));
639       if(!gtk2)
640         throw INTERP_KERNEL::Exception("LocInfo::generateNonClassicalData : internal error !");
641       const MEDFileUMesh *meshLoc(gtk2->getMesh()),*section(gtk2->getSection());
642       const MEDFileStructureElement *se(gtk2->getSE());
643       MCAuto<MEDCouplingUMesh> um(meshLoc->getMeshAtLevel(0));
644       INTERP_KERNEL::NormalizedCellType gt(_cts[i]);
645       {
646         std::vector<int> nel(meshLoc->getNonEmptyLevels());
647         if(nel.size()!=1)
648           throw INTERP_KERNEL::Exception(MSG1);
649         if(nel[0]!=0)
650           throw INTERP_KERNEL::Exception(MSG1);
651         mcIdType zePos(-1);
652         for(mcIdType icell = 0 ; icell < um->getNumberOfCells() ; ++icell)
653           if( gt == um->getTypeOfCell(icell) )
654             zePos = icell;
655         if(zePos == -1)
656           throw INTERP_KERNEL::Exception(MSG1);
657         std::vector<mcIdType> v;
658         um->getNodeIdsOfCell(zePos,v);
659         std::size_t sz2(v.size());
660         for(std::size_t j=0;j<sz2;j++)
661           if(v[j]!=ToIdType(j))
662             throw INTERP_KERNEL::Exception(MSG1);
663       }
664       const std::vector< MCAuto<MEDFileEltStruct4Mesh> >& strs(mesh->getAccessOfUndergroundEltStrs());
665       MCAuto<MEDFileEltStruct4Mesh> zeStr;
666       for(std::vector< MCAuto<MEDFileEltStruct4Mesh> >::const_iterator it=strs.begin();it!=strs.end();it++)
667         {
668           if((*it)->getGeoTypeName()==se->getName())
669             {
670               zeStr=*it;
671               break;
672             }
673         }
674       if(zeStr.isNull())
675         {
676           std::ostringstream oss; oss << "LocInfo::generateNonClassicalData :  : no geo type with name " <<  se->getName() << " in " << mesh->getName() << " !";
677           throw INTERP_KERNEL::Exception(oss.str());
678         }
679       arrs[i]=BuildMeshFromStructure(gt,_pfl[i],loc,zeStr,mesh,section,globs);
680     }
681   std::vector<const DataArrayDouble *> arrs2(VecAutoToVecOfCstPt(arrs));
682   MCAuto<DataArrayDouble> resu(DataArrayDouble::Aggregate(arrs2));
683   MCAuto<MEDFileUMesh> ret(MEDFileUMesh::New());
684   ret->setCoords(resu);
685   std::ostringstream meshName; meshName << mesh->getName() << "_on_" << sz << "_sections" << "_" << zePos;
686   ret->setName(meshName.str());
687   return ret;
688 }
689
690 FieldWalker2::FieldWalker2(const MEDFileFieldPerMeshPerTypePerDisc *pmptpd)
691 {
692   _loc=pmptpd->getLocalization();
693   _pfl=pmptpd->getProfile();
694   _ct=pmptpd->getGeoTypeStatic();
695   _is_classic=pmptpd->getType()!=ON_GAUSS_PT;
696   _pd=SlicePartDefinition::New(pmptpd->getStart(),pmptpd->getEnd(),1);
697 }
698
699 bool FieldWalker2::operator!=(const FieldWalker2& other) const
700 {
701   return !((*this)==other);
702 }
703
704 bool FieldWalker2::operator==(const FieldWalker2& other) const
705 {
706   bool ret2(false);
707   {
708     std::string tmp;
709     ret2=_pd->isEqual(other._pd,tmp);
710   }
711   bool ret(_loc==other._loc && _pfl==other._pfl && _is_classic==other._is_classic && ret2);
712   return ret;
713 }
714
715 class FieldWalker1
716 {
717 public:
718   FieldWalker1(const MEDFileAnyTypeField1TSWithoutSDA *ts):_ts(ts),_pm_pt(0),_nb_mesh(0) { }
719   void newMeshEntry(const MEDFileFieldPerMesh *fpm);
720   void endMeshEntry(const MEDFileFieldPerMesh *fpm) { }
721   void newPerMeshPerTypeEntry(const MEDFileFieldPerMeshPerTypeCommon *pmpt);
722   void endPerMeshPerTypeEntry(const MEDFileFieldPerMeshPerTypeCommon *pmpt);
723   void newPerMeshPerTypePerDisc(const MEDFileFieldPerMeshPerTypePerDisc *pmptpd);
724   void checkOK(const FieldWalker1& other) const;
725   bool isClassical() const;
726   std::vector<FieldWalker2> getNonClassicalData() const { return _fw; }
727 private:
728   const MEDFileAnyTypeField1TSWithoutSDA *_ts;
729   const MEDFileFieldPerMeshPerTypeDyn *_pm_pt;
730   std::vector<FieldWalker2> _fw;
731   int _nb_mesh;
732 };
733
734 void FieldWalker1::newMeshEntry(const MEDFileFieldPerMesh *fpm)
735 {
736   if(_nb_mesh++==1)
737     throw INTERP_KERNEL::Exception("FieldWalker1::newMeshEntry : multi mesh not supported !");
738 }
739
740 void FieldWalker1::newPerMeshPerTypeEntry(const MEDFileFieldPerMeshPerTypeCommon *pmpt)
741 {
742   if(_pm_pt)
743     throw INTERP_KERNEL::Exception("FieldWalker1::newPerMeshPerTypeEntry : multi SE loc not managed yet !");
744   const MEDFileFieldPerMeshPerTypeDyn *pmpt2(dynamic_cast<const MEDFileFieldPerMeshPerTypeDyn *>(pmpt));
745   if(!pmpt2)
746     throw INTERP_KERNEL::Exception("newPerMeshPerTypeEntry : internal error !");
747   _pm_pt=pmpt2;
748 }
749
750 void FieldWalker1::endPerMeshPerTypeEntry(const MEDFileFieldPerMeshPerTypeCommon *)
751 {
752   isClassical();
753 }
754
755 void FieldWalker1::newPerMeshPerTypePerDisc(const MEDFileFieldPerMeshPerTypePerDisc *pmptpd)
756 {
757   _fw.push_back(FieldWalker2(pmptpd));
758 }
759
760 void FieldWalker1::checkOK(const FieldWalker1& other) const
761 {
762   std::size_t sz(_fw.size());
763   if(other._fw.size()!=sz)
764     throw INTERP_KERNEL::Exception("checkOK : not OK because size are not the same !");
765   for(std::size_t i=0;i<sz;i++)
766     if(_fw[i]!=other._fw[i])
767       throw INTERP_KERNEL::Exception("checkOK : not OK because an element mismatches !");
768 }
769
770 bool FieldWalker1::isClassical() const
771 {
772   if(_fw.empty())
773     throw INTERP_KERNEL::Exception("FieldWalker1::endPerMeshPerTypeEntry : internal error !");
774   std::size_t ic(0),inc(0);
775   for(std::vector<FieldWalker2>::const_iterator it=_fw.begin();it!=_fw.end();it++)
776     {
777       if((*it).isClassic())
778         ic++;
779       else
780         inc++;
781     }
782   if(ic!=0 && inc!=0)
783     throw INTERP_KERNEL::Exception("FieldWalker1::endPerMeshPerTypeEntry : mix is not allowed yet !");
784   return inc==0;
785 }
786
787 class FieldWalker
788 {
789 public:
790   FieldWalker(const MEDFileAnyTypeFieldMultiTSWithoutSDA *f):_f(f) { }
791   void newTimeStepEntry(const MEDFileAnyTypeField1TSWithoutSDA *ts);
792   void endTimeStepEntry(const MEDFileAnyTypeField1TSWithoutSDA *ts);
793   void newMeshEntry(const MEDFileFieldPerMesh *fpm);
794   void endMeshEntry(const MEDFileFieldPerMesh *fpm);
795   void newPerMeshPerTypeEntry(const MEDFileFieldPerMeshPerTypeCommon *pmpt);
796   void endPerMeshPerTypeEntry(const MEDFileFieldPerMeshPerTypeCommon *pmpt);
797   void newPerMeshPerTypePerDisc(const MEDFileFieldPerMeshPerTypePerDisc *pmptpd);
798 public:
799   bool isEmpty() const;
800   bool isClassical() const;
801   const MEDFileAnyTypeFieldMultiTSWithoutSDA *field() const { return _f; }
802   std::vector<FieldWalker2> getNonClassicalData() const { return _fw_prev->getNonClassicalData(); }
803 private:
804   const MEDFileAnyTypeFieldMultiTSWithoutSDA *_f;
805   mutable INTERP_KERNEL::AutoCppPtr<FieldWalker1> _fw;
806   mutable INTERP_KERNEL::AutoCppPtr<FieldWalker1> _fw_prev;
807 };
808
809 bool FieldWalker::isEmpty() const
810 {
811   return _fw_prev.isNull();
812 }
813
814 bool FieldWalker::isClassical() const
815 {
816   return _fw_prev->isClassical();
817 }
818
819 void FieldWalker::newTimeStepEntry(const MEDFileAnyTypeField1TSWithoutSDA *ts)
820 {
821   _fw=new FieldWalker1(ts);
822 }
823
824 void FieldWalker::endTimeStepEntry(const MEDFileAnyTypeField1TSWithoutSDA *ts)
825 {
826   if(_fw_prev.isNull())
827     _fw_prev=new FieldWalker1(*_fw);
828   else
829     _fw_prev->checkOK(*_fw);
830   _fw=0;
831 }
832
833 void FieldWalker::newMeshEntry(const MEDFileFieldPerMesh *fpm)
834 {
835   _fw->newMeshEntry(fpm);
836 }
837
838 void FieldWalker::endMeshEntry(const MEDFileFieldPerMesh *fpm)
839 {
840   _fw->endMeshEntry(fpm);
841 }
842
843 void FieldWalker::newPerMeshPerTypeEntry(const MEDFileFieldPerMeshPerTypeCommon *pmpt)
844 {
845   _fw->newPerMeshPerTypeEntry(pmpt);
846 }
847
848 void FieldWalker::endPerMeshPerTypeEntry(const MEDFileFieldPerMeshPerTypeCommon *pmpt)
849 {
850   _fw->endPerMeshPerTypeEntry(pmpt);
851 }
852
853 void FieldWalker::newPerMeshPerTypePerDisc(const MEDFileFieldPerMeshPerTypePerDisc *pmptpd)
854 {
855   _fw->newPerMeshPerTypePerDisc(pmptpd);
856 }
857
858 // this class splits fields into same
859 class LocSpliter : public MEDFileFieldVisitor
860 {
861 public:
862   LocSpliter(const MEDFileFieldGlobsReal *globs):_globs(globs),_fw(0) { }
863   MCAuto<MEDFileFields> getClassical() const { return _classical; }
864   void generateNonClassicalData(const MEDFileUMesh *mesh, std::vector< MCAuto<MEDFileFields> >& outFields, std::vector< MCAuto<MEDFileUMesh> >& outMeshes) const;
865 private:
866   void newFieldEntry(const MEDFileAnyTypeFieldMultiTSWithoutSDA *field);
867   void endFieldEntry(const MEDFileAnyTypeFieldMultiTSWithoutSDA *field);
868   //
869   void newTimeStepEntry(const MEDFileAnyTypeField1TSWithoutSDA *ts);
870   void endTimeStepEntry(const MEDFileAnyTypeField1TSWithoutSDA *ts);
871   //
872   void newMeshEntry(const MEDFileFieldPerMesh *fpm);
873   void endMeshEntry(const MEDFileFieldPerMesh *fpm);
874   //
875   void newPerMeshPerTypeEntry(const MEDFileFieldPerMeshPerTypeCommon *pmpt);
876   void endPerMeshPerTypeEntry(const MEDFileFieldPerMeshPerTypeCommon *pmpt);
877   //
878   void newPerMeshPerTypePerDisc(const MEDFileFieldPerMeshPerTypePerDisc *pmptpd);
879 private:
880   const MEDFileFieldGlobsReal *_globs;
881   std::vector< LocInfo > _locs;
882   std::vector< MCAuto<MEDFileFields> > _fields_on_locs;//size of _locs== size of _fields_on_locs
883   MCAuto<MEDFileFields> _classical;
884 private:
885   mutable INTERP_KERNEL::AutoCppPtr<FieldWalker> _fw;
886 };
887
888 void LocSpliter::newFieldEntry(const MEDFileAnyTypeFieldMultiTSWithoutSDA *field)
889 {
890   _fw=new FieldWalker(field);
891 }
892
893 void LocSpliter::endFieldEntry(const MEDFileAnyTypeFieldMultiTSWithoutSDA *field)
894 {
895   if(_fw->isEmpty())
896     return ;
897   MCAuto<MEDFileAnyTypeFieldMultiTS> f(MEDFileAnyTypeFieldMultiTS::BuildNewInstanceFromContent(const_cast<MEDFileAnyTypeFieldMultiTSWithoutSDA *>(field)));
898   if(_fw->isClassical())
899     {
900       if(_classical.isNull())
901         {
902           _classical=MEDFileFields::New();
903           _classical->shallowCpyGlobs(*_globs);
904         }
905       _classical->pushField(f);
906     }
907   else
908     {
909       std::vector<FieldWalker2> fw2(_fw->getNonClassicalData());
910       LocInfo elt(fw2);
911       std::vector< LocInfo >::iterator it(std::find(_locs.begin(),_locs.end(),elt));
912       if(it==_locs.end())
913         {
914           _locs.push_back(elt);
915           MCAuto<MEDFileFields> zeF(MEDFileFields::New());
916           zeF->shallowCpyGlobs(*_globs);
917           zeF->pushField(f);
918           _fields_on_locs.push_back(zeF);
919         }
920       else
921         {
922           MCAuto<MEDFileFields> zeF(_fields_on_locs[std::distance(_locs.begin(),it)]);
923           zeF->pushField(f);
924         }
925     }
926 }
927
928 void LocSpliter::generateNonClassicalData(const MEDFileUMesh *mesh, std::vector< MCAuto<MEDFileFields> >& outFields, std::vector< MCAuto<MEDFileUMesh> >& outMeshes) const
929 {
930   int i(0);
931   for(std::vector<LocInfo>::const_iterator it=_locs.begin();it!=_locs.end();it++,i++)
932     {
933       MCAuto<MEDFileUMesh> m((*it).generateNonClassicalData(i,mesh,_globs));
934       outMeshes.push_back(m);
935       MCAuto<MEDCouplingUMesh> mcm(MEDCouplingUMesh::Build0DMeshFromCoords(m->getCoords()));
936       mcm->setName(m->getName());
937       MCAuto<MEDFileFields> fs(_fields_on_locs[i]);
938       MCAuto<MEDFileFields> outFs(MEDFileFields::New());
939       for(int j=0;j<fs->getNumberOfFields();j++)
940         {
941           MCAuto<MEDFileAnyTypeFieldMultiTS> fmtsNC(fs->getFieldAtPos(j));
942           MCAuto<MEDFileFieldMultiTS> fmts(DynamicCastSafe<MEDFileAnyTypeFieldMultiTS,MEDFileFieldMultiTS>(fmtsNC));
943           MCAuto<MEDFileFieldMultiTS> outFmts(MEDFileFieldMultiTS::New());
944           for(int k=0;k<fmts->getNumberOfTS();k++)
945             {
946               MCAuto<MEDFileField1TS> outF1t(MEDFileField1TS::New());
947               MCAuto<MEDFileField1TS> f1ts(fmts->getTimeStepAtPos(k));
948               int t2,t3;
949               double t1(f1ts->getTime(t2,t3));
950               MCAuto<MEDCouplingFieldDouble> mcf(MEDCouplingFieldDouble::New(ON_NODES));
951               MCAuto<DataArrayDouble> arr,arr2;
952               arr.takeRef(f1ts->getUndergroundDataArray());
953               arr2=arr->selectPartDef((*it).getPartDef());
954               mcf->setArray(arr2);
955               mcf->setTime(t1,t2,t3);
956               mcf->setName(f1ts->getName());
957               mcf->setMesh(mcm);
958               outF1t->setFieldNoProfileSBT(mcf);
959               outFmts->pushBackTimeStep(outF1t);
960             }
961           outFs->pushField(outFmts);
962         }
963       outFields.push_back(outFs);
964     }
965 }
966
967 void LocSpliter::newTimeStepEntry(const MEDFileAnyTypeField1TSWithoutSDA *ts)
968 {
969   _fw->newTimeStepEntry(ts);
970 }
971
972 void LocSpliter::endTimeStepEntry(const MEDFileAnyTypeField1TSWithoutSDA *ts)
973 {
974   _fw->endTimeStepEntry(ts);
975 }
976
977 void LocSpliter::newMeshEntry(const MEDFileFieldPerMesh *fpm)
978 {
979   _fw->newMeshEntry(fpm);
980 }
981
982 void LocSpliter::endMeshEntry(const MEDFileFieldPerMesh *fpm)
983 {
984   _fw->endMeshEntry(fpm);
985 }
986
987 void LocSpliter::newPerMeshPerTypeEntry(const MEDFileFieldPerMeshPerTypeCommon *pmpt)
988 {
989   _fw->newPerMeshPerTypeEntry(pmpt);
990 }
991
992 void LocSpliter::endPerMeshPerTypeEntry(const MEDFileFieldPerMeshPerTypeCommon *pmpt)
993 {
994   _fw->endPerMeshPerTypeEntry(pmpt);
995 }
996
997 void LocSpliter::newPerMeshPerTypePerDisc(const MEDFileFieldPerMeshPerTypePerDisc *pmptpd)
998 {
999   _fw->newPerMeshPerTypePerDisc(pmptpd);
1000 }
1001
1002 void MEDFileBlowStrEltUp::DealWithConflictNames(MEDFileAnyTypeFieldMultiTS *fmtsToAdd, const MEDFileFields *fs)
1003 {
1004   std::vector<std::string> fnames(fs->getFieldsNames());
1005   for(int i=0;i<1000;i++)
1006     {
1007       std::ostringstream oss; oss << fmtsToAdd->getName();
1008       if(i>=1)
1009         oss << "_" << i-1;
1010       if(std::find(fnames.begin(),fnames.end(),oss.str())==fnames.end())
1011         {
1012           fmtsToAdd->setName(oss.str());
1013           return ;
1014         }
1015     }
1016   throw INTERP_KERNEL::Exception("DealWithConflictNames : Eh eh interesting !");
1017 }
1018
1019 MCAuto<MEDFileFields> MEDFileBlowStrEltUp::splitFieldsPerLoc(const MEDFileFields *fields, const MEDFileUMesh *mesh, MEDFileMeshes *msOut, MEDFileFields *allZeOutFields)
1020 {
1021   LocSpliter ls(fields);
1022   fields->accept(ls);
1023   std::vector< MCAuto<MEDFileFields> > outFields;
1024   std::vector< MCAuto<MEDFileUMesh> > outMeshes;
1025   ls.generateNonClassicalData(mesh,outFields,outMeshes);
1026   for(std::vector< MCAuto<MEDFileFields> >::iterator it=outFields.begin();it!=outFields.end();it++)
1027     {
1028       for(int j=0;j<(*it)->getNumberOfFields();j++)
1029         {
1030           MCAuto<MEDFileAnyTypeFieldMultiTS> fmts((*it)->getFieldAtPos(j));
1031           //DealWithConflictNames(fmts,allZeOutFields);// uncomment to have a writable data structure
1032           allZeOutFields->pushField(fmts);
1033         }
1034     }
1035   for(std::vector< MCAuto<MEDFileUMesh> >::iterator it=outMeshes.begin();it!=outMeshes.end();it++)
1036     msOut->pushMesh(*it);
1037   return ls.getClassical();
1038 }