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