Salome HOME
One more FED24
[tools/medcoupling.git] / src / MEDLoader / MEDFileMeshLL.hxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #ifndef __MEDFILEMESHLL_HXX__
22 #define __MEDFILEMESHLL_HXX__
23
24 #include "MEDFileBasis.hxx"
25 #include "MEDFileMeshElt.hxx"
26
27 #include "MEDCouplingUMesh.hxx"
28 #include "MEDCouplingCMesh.hxx"
29 #include "MEDCoupling1GTUMesh.hxx"
30 #include "MEDCouplingPartDefinition.hxx"
31 #include "MEDCouplingCurveLinearMesh.hxx"
32 #include "MCAuto.hxx"
33
34 #include "InterpKernelAutoPtr.hxx"
35
36 #include "med.h"
37
38 #include <map>
39
40 namespace MEDCoupling
41 {
42   class MEDFileMeshReadSelector;
43
44   class MeshOrStructMeshCls
45   {
46   protected:
47     MeshOrStructMeshCls(int mid):_mid(mid) { }
48   public:
49     virtual ~MeshOrStructMeshCls() {}
50     int getID() const { return _mid; }
51     virtual std::vector<std::string> getAxisInfoOnMesh(med_idt fid, const std::string& mName, MEDCoupling::MEDCouplingMeshType& meshType, MEDCoupling::MEDCouplingAxisType& axType, int& nstep, int& Mdim, MEDFileString& description, MEDFileString& dtunit, MEDFileString& univName) const = 0;
52     virtual double checkMeshTimeStep(med_idt fid, const std::string& mName, int nstep, int dt, int it) const = 0;
53   private:
54     int _mid;
55   };
56
57   class MeshCls : public MeshOrStructMeshCls
58   {
59   public:
60     MeshCls(int mid):MeshOrStructMeshCls(mid) { }
61     std::vector<std::string> getAxisInfoOnMesh(med_idt fid, const std::string& mName, MEDCoupling::MEDCouplingMeshType& meshType, MEDCoupling::MEDCouplingAxisType& axType, int& nstep, int& Mdim, MEDFileString& description, MEDFileString& dtunit, MEDFileString& univName) const;
62     double checkMeshTimeStep(med_idt fid, const std::string& mName, int nstep, int dt, int it) const;
63   };
64
65   class StructMeshCls : public MeshOrStructMeshCls
66   {
67   public:
68     StructMeshCls(int mid):MeshOrStructMeshCls(mid) { }
69     std::vector<std::string> getAxisInfoOnMesh(med_idt fid, const std::string& mName, MEDCoupling::MEDCouplingMeshType& meshType, MEDCoupling::MEDCouplingAxisType& axType, int& nstep, int& Mdim, MEDFileString& description, MEDFileString& dtunit, MEDFileString& univName) const;
70     double checkMeshTimeStep(med_idt fid, const std::string& mName, int nstep, int dt, int it) const;
71   };
72   
73   class MEDFileMeshL2 : public RefCountObject
74   {
75   public:
76     MEDFileMeshL2();
77     std::size_t getHeapMemorySizeWithoutChildren() const;
78     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
79     const char *getName() const { return _name.getReprForWrite(); }
80     const char *getDescription() const { return _description.getReprForWrite(); }
81     const char *getUnivName() const { return _univ_name.getReprForWrite(); }
82     const char *getTimeUnit() const { return _dt_unit.getReprForWrite(); }
83     int getIteration() const { return _iteration; }
84     int getOrder() const { return _order; }
85     double getTime() const { return _time; }
86     MCAuto<PartDefinition> getPartDefOfCoo() const { return _part_coords; }
87     std::vector<std::string> getAxisInfoOnMesh(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, MEDCoupling::MEDCouplingMeshType& meshType, MEDCoupling::MEDCouplingAxisType& axType, int& nstep, int& Mdim);
88     static INTERP_KERNEL::AutoCppPtr<MeshOrStructMeshCls> GetMeshIdFromName(med_idt fid, const std::string& mName, MEDCoupling::MEDCouplingMeshType& meshType, MEDCoupling::MEDCouplingAxisType& axType, int& dt, int& it, std::string& dtunit1);
89     static void ReadFamiliesAndGrps(med_idt fid, const std::string& mname, std::map<std::string,int>& fams, std::map<std::string, std::vector<std::string> >& grps, MEDFileMeshReadSelector *mrs);
90     static void WriteFamiliesAndGrps(med_idt fid, const std::string& mname, const std::map<std::string,int>& fams, const std::map<std::string, std::vector<std::string> >& grps, int tooLongStrPol);
91     static bool RenameFamiliesFromFileToMem(std::vector< std::string >& famNames);
92     static bool RenameFamiliesFromMemToFile(std::vector< std::string >& famNames);
93     static MEDCoupling::MEDCouplingAxisType TraduceAxisType(med_axis_type at);
94     static MEDCoupling::MEDCouplingAxisType TraduceAxisTypeStruct(med_grid_type gt);
95     static med_axis_type TraduceAxisTypeRev(MEDCoupling::MEDCouplingAxisType at);
96     static med_grid_type TraduceAxisTypeRevStruct(MEDCoupling::MEDCouplingAxisType at);
97   private:
98     typedef bool (*RenameFamiliesPatternFunc)(std::vector< std::string >&);
99     static void RenameFamiliesPatternInternal(std::vector< std::pair<std::string,std::pair<int,std::vector<std::string> > > >& crudeFams, RenameFamiliesPatternFunc func);
100     static void RenameFamiliesFromFileToMemInternal(std::vector< std::pair<std::string,std::pair<int,std::vector<std::string> > > >& crudeFams);
101     static void RenameFamiliesFromMemToFileInternal(std::vector< std::pair<std::string,std::pair<int,std::vector<std::string> > > >& crudeFams);
102   public:
103     static const char ZE_SEP_FOR_FAMILY_KILLERS[];
104     static int ZE_SEP2_FOR_FAMILY_KILLERS;
105   protected:
106     MEDFileString _name;
107     MEDFileString _description;
108     MEDFileString _univ_name;
109     MEDFileString _dt_unit;
110     int _iteration;
111     int _order;
112     double _time;
113     MCAuto<PartDefinition> _part_coords;
114   };
115
116   class MEDFileUMeshL2 : public MEDFileMeshL2
117   {
118   public:
119     MEDFileUMeshL2();
120     std::vector<std::string> loadCommonPart(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it, int& Mdim);
121     void loadAll(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs);
122     void loadPart(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, const std::vector<INTERP_KERNEL::NormalizedCellType>& types, const std::vector<int>& slicPerTyp, int dt, int it, MEDFileMeshReadSelector *mrs);
123     void loadConnectivity(med_idt fid, int mdim, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs);
124     void loadPartOfConnectivity(med_idt fid, int mdim, const std::string& mName, const std::vector<INTERP_KERNEL::NormalizedCellType>& types, const std::vector<int>& slicPerTyp, int dt, int it, MEDFileMeshReadSelector *mrs);
125     void loadCoords(med_idt fid, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it);
126     void loadPartCoords(med_idt fid, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it, int nMin, int nMax);
127     int getNumberOfLevels() const { return _per_type_mesh.size(); }
128     bool emptyLev(int levId) const { return _per_type_mesh[levId].empty(); }
129     const std::vector< MCAuto<MEDFileUMeshPerType> >& getLev(int levId) const { return _per_type_mesh[levId]; }
130     bool isFamDefinedOnLev(int levId) const;
131     bool isNumDefinedOnLev(int levId) const;
132     bool isNamesDefinedOnLev(int levId) const;
133     MCAuto<DataArrayDouble> getCoords() const { return _coords; }
134     MCAuto<DataArrayInt> getCoordsFamily() const { return _fam_coords; }
135     MCAuto<DataArrayInt> getCoordsNum() const { return _num_coords; }
136     MCAuto<DataArrayAsciiChar> getCoordsName() const { return _name_coords; }
137     static void WriteCoords(med_idt fid, const std::string& mname, int dt, int it, double time, const DataArrayDouble *coords, const DataArrayInt *famCoords, const DataArrayInt *numCoords, const DataArrayAsciiChar *nameCoords);
138   private:
139     void sortTypes();
140   private:
141     std::vector< std::vector< MCAuto<MEDFileUMeshPerType> > > _per_type_mesh;
142     MCAuto<DataArrayDouble> _coords;
143     MCAuto<DataArrayInt> _fam_coords;
144     MCAuto<DataArrayInt> _num_coords;
145     MCAuto<DataArrayAsciiChar> _name_coords;
146   };
147
148   class MEDFileStrMeshL2 : public MEDFileMeshL2
149   {
150   };
151
152   class MEDFileCMeshL2 : public MEDFileStrMeshL2
153   {
154   public:
155     MEDFileCMeshL2();
156     void loadAll(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it);
157     MEDCouplingCMesh *getMesh() { return _cmesh; }
158     MEDCoupling::MEDCouplingAxisType getAxisType() const { return _ax_type; }
159   private:
160     static med_data_type GetDataTypeCorrespondingToSpaceId(int id);
161   private:
162     MCAuto<MEDCouplingCMesh> _cmesh;
163     MEDCoupling::MEDCouplingAxisType _ax_type;
164   };
165
166   class MEDFileCLMeshL2 : public MEDFileStrMeshL2
167   {
168   public:
169     MEDFileCLMeshL2();
170     void loadAll(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it);
171     MEDCouplingCurveLinearMesh *getMesh() { return _clmesh; }
172   private:
173     MCAuto<MEDCouplingCurveLinearMesh> _clmesh;
174   };
175
176   class MEDFileMesh;
177   class MEDFileUMeshSplitL1;
178
179   class MEDFileUMeshPermCompute : public BigMemoryObject
180   {
181   public:
182     MEDFileUMeshPermCompute(const MEDFileUMeshSplitL1* st);
183     operator MEDCouplingUMesh *() const;
184     void operator=(MEDCouplingUMesh *m);
185     void updateTime() const;
186     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
187     std::size_t getHeapMemorySizeWithoutChildren() const;
188   private:
189     const MEDFileUMeshSplitL1 *_st;
190     mutable std::size_t _mpt_time;
191     mutable std::size_t _num_time;
192     mutable MCAuto<MEDCouplingUMesh> _m;
193   };
194
195   class MEDFileUMeshAggregateCompute : public BigMemoryObject
196   {
197   public:
198     MEDFileUMeshAggregateCompute();
199     void setName(const std::string& name);
200     void assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts);
201     void assignDefParts(const std::vector<const PartDefinition *>& partDefs);
202     void assignUMesh(MEDCouplingUMesh *m);
203     MEDCouplingUMesh *getUmesh() const;
204     int getNumberOfCells() const;
205     std::vector<MEDCoupling1GTUMesh *> getParts() const;
206     std::vector<INTERP_KERNEL::NormalizedCellType> getGeoTypes() const;
207     int getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType ct) const;
208     std::vector<MEDCoupling1GTUMesh *> retrievePartsWithoutComputation() const;
209     MEDCoupling1GTUMesh *retrievePartWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const;
210     void getStartStopOfGeoTypeWithoutComputation(INTERP_KERNEL::NormalizedCellType gt, int& start, int& stop) const;
211     void renumberNodesInConnWithoutComputation(const int *newNodeNumbersO2N);
212     bool isStoredSplitByType() const;
213     std::size_t getTimeOfThis() const;
214     std::size_t getHeapMemorySizeWithoutChildren() const;
215     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
216     MEDFileUMeshAggregateCompute deepCopy(DataArrayDouble *coords) const;
217     void shallowCpyMeshes();
218     bool isEqual(const MEDFileUMeshAggregateCompute& other, double eps, std::string& what) const;
219     void checkConsistency() const;
220     void clearNonDiscrAttributes() const;
221     void synchronizeTinyInfo(const MEDFileMesh& master) const;
222     bool empty() const;
223     int getMeshDimension() const;
224     std::vector<int> getDistributionOfTypes() const;
225     int getSize() const;
226     void setCoords(DataArrayDouble *coords);
227     void forceComputationOfPartsFromUMesh() const;
228     const PartDefinition *getPartDefOfWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const;
229     void serialize(std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI) const;
230     void unserialize(const std::string& name, DataArrayDouble *coo, std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI);
231   private:
232     std::size_t getTimeOfParts() const;
233     std::size_t getTimeOfUMesh() const;
234   private:
235     mutable std::vector< MCAuto<MEDCoupling1GTUMesh> > _m_parts;
236     mutable std::size_t _mp_time;
237     mutable std::size_t _m_time;
238     mutable MCAuto<MEDCouplingUMesh> _m;
239     mutable std::vector< MCAuto<PartDefinition> > _part_def;
240   };
241
242   class MEDFileUMeshSplitL1 : public RefCountObject
243   {
244     friend class MEDFileUMeshPermCompute;
245     friend class MEDFileUMesh;
246   public:
247     MEDFileUMeshSplitL1(const MEDFileUMeshSplitL1& other);
248     MEDFileUMeshSplitL1(const MEDFileUMeshL2& l2, const std::string& mName, int id);
249     MEDFileUMeshSplitL1(MEDCoupling1GTUMesh *m);
250     MEDFileUMeshSplitL1(MEDCouplingUMesh *m);
251     MEDFileUMeshSplitL1(MEDCouplingUMesh *m, bool newOrOld);
252     void setName(const std::string& name);
253     std::size_t getHeapMemorySizeWithoutChildren() const;
254     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
255     MEDFileUMeshSplitL1 *shallowCpyUsingCoords(DataArrayDouble *coords) const;
256     MEDFileUMeshSplitL1 *deepCopy(DataArrayDouble *coords) const;
257     void checkConsistency() const;
258     void setCoords(DataArrayDouble *coords);
259     bool isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const;
260     void clearNonDiscrAttributes() const;
261     void synchronizeTinyInfo(const MEDFileMesh& master) const;
262     void assignMesh(MEDCouplingUMesh *m, bool newOrOld);
263     void assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts);
264     void forceComputationOfParts() const;
265     bool empty() const;
266     bool presenceOfOneFams(const std::vector<int>& ids) const;
267     int getMeshDimension() const;
268     void simpleRepr(std::ostream& oss) const;
269     int getSize() const;
270     MEDCouplingUMesh *getFamilyPart(const int *idsBg, const int *idsEnd, bool renum) const;
271     DataArrayInt *getFamilyPartArr(const int *idsBg, const int *idsEnd, bool renum) const;
272     MEDCouplingUMesh *getWholeMesh(bool renum) const;
273     int getNumberOfCells() const;
274     bool isMeshStoredSplitByType() const { return _m_by_types.isStoredSplitByType(); }
275     std::vector<INTERP_KERNEL::NormalizedCellType> getGeoTypes() const;
276     int getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType ct) const;
277     std::vector<MEDCoupling1GTUMesh *> getDirectUndergroundSingleGeoTypeMeshes() const { return _m_by_types.retrievePartsWithoutComputation(); }
278     MEDCoupling1GTUMesh *getDirectUndergroundSingleGeoTypeMesh(INTERP_KERNEL::NormalizedCellType gt) const { return _m_by_types.retrievePartWithoutComputation(gt); }
279     DataArrayInt *extractFamilyFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const;
280     DataArrayInt *extractNumberFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const;
281     std::vector<int> getDistributionOfTypes() const { return _m_by_types.getDistributionOfTypes(); }
282     DataArrayInt *getOrCreateAndGetFamilyField();
283     const DataArrayInt *getFamilyField() const;
284     const DataArrayInt *getNumberField() const;
285     const DataArrayAsciiChar *getNameField() const;
286     const DataArrayInt *getRevNumberField() const;
287     const PartDefinition *getPartDef(INTERP_KERNEL::NormalizedCellType gt) const;
288     void eraseFamilyField();
289     void setGroupsFromScratch(const std::vector<const MEDCouplingUMesh *>& ms, std::map<std::string,int>& familyIds,
290                               std::map<std::string, std::vector<std::string> >& groups);
291     void write(med_idt fid, const std::string& mName, int mdim) const;
292     //
293     void setFamilyArr(DataArrayInt *famArr);
294     DataArrayInt *getFamilyField();
295     void setRenumArr(DataArrayInt *renumArr);
296     void setNameArr(DataArrayAsciiChar *nameArr);
297     void changeFamilyIdArr(int oldId, int newId);
298     //
299     void renumberNodesInConn(const int *newNodeNumbersO2N);
300     //
301     void serialize(std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI) const;
302     void unserialize(const std::string& name, DataArrayDouble *coo, std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI);
303     //
304     static void ClearNonDiscrAttributes(const MEDCouplingMesh *tmp);
305     static std::vector<int> GetNewFamiliesNumber(int nb, const std::map<std::string,int>& families);
306     static void TraduceFamilyNumber(const std::vector< std::vector<int> >& fidsGrps, std::map<std::string,int>& familyIds,
307                                     std::map<int,int>& famIdTrad, std::map<int,std::string>& newfams);
308     static DataArrayInt *Renumber(const DataArrayInt *renum, const DataArrayInt *da);
309     static MEDCouplingUMesh *Renumber2(const DataArrayInt *renum, MEDCouplingUMesh *m, const int *cellIds);
310     static MEDFileUMeshSplitL1 *Unserialize(const std::string& name, DataArrayDouble *coo, std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI);
311   private:
312     MEDFileUMeshSplitL1();
313     void assignCommonPart();
314     MEDCouplingUMesh *renumIfNeeded(MEDCouplingUMesh *m, const int *cellIds) const;
315     DataArrayInt *renumIfNeededArr(const DataArrayInt *da) const;
316     void computeRevNum() const;
317   private:
318     MEDFileUMeshAggregateCompute _m_by_types;
319     MCAuto<DataArrayInt> _fam;
320     MCAuto<DataArrayInt> _num;
321     MCAuto<DataArrayAsciiChar> _names;
322     mutable MCAuto<DataArrayInt> _rev_num;
323     MEDFileUMeshPermCompute _m;
324   };
325
326   class MEDFileEltStruct4Mesh : public RefCountObject
327   {
328   public:
329     static MEDFileEltStruct4Mesh *New(med_idt fid, const std::string& mName, int dt, int it, int iterOnStEltOfMesh, MEDFileMeshReadSelector *mrs);
330     std::string getGeoTypeName() const { return _geo_type_name; }
331     MCAuto<DataArrayInt> getConn() const { return _conn; }
332     MCAuto<MEDFileUMeshPerTypeCommon> getMeshDef() const { return _common; }
333     const std::vector< MCAuto<DataArray> >& getVars() const { return _vars; }
334   private:
335     std::size_t getHeapMemorySizeWithoutChildren() const;
336     std::vector<const MEDCoupling::BigMemoryObject*> getDirectChildrenWithNull() const;
337   private:
338     ~MEDFileEltStruct4Mesh() { }
339   private:
340     MEDFileEltStruct4Mesh(med_idt fid, const std::string& mName, int dt, int it, int iterOnStEltOfMesh, MEDFileMeshReadSelector *mrs);
341   private:
342     std::string _geo_type_name;
343     int _geo_type;
344     MCAuto<DataArrayInt> _conn;
345     MCAuto<MEDFileUMeshPerTypeCommon> _common;
346     std::vector< MCAuto<DataArray> > _vars;
347   };
348 }
349
350 #endif