Salome HOME
Structure element inside Gauss Points management
[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     int getID() const { return _mid; }
50     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;
51     virtual double checkMeshTimeStep(med_idt fid, const std::string& mName, int nstep, int dt, int it) const = 0;
52   private:
53     int _mid;
54   };
55
56   class MeshCls : public MeshOrStructMeshCls
57   {
58   public:
59     MeshCls(int mid):MeshOrStructMeshCls(mid) { }
60     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;
61     double checkMeshTimeStep(med_idt fid, const std::string& mName, int nstep, int dt, int it) const;
62   };
63
64   class StructMeshCls : public MeshOrStructMeshCls
65   {
66   public:
67     StructMeshCls(int mid):MeshOrStructMeshCls(mid) { }
68     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;
69     double checkMeshTimeStep(med_idt fid, const std::string& mName, int nstep, int dt, int it) const;
70   };
71   
72   class MEDFileMeshL2 : public RefCountObject
73   {
74   public:
75     MEDFileMeshL2();
76     std::size_t getHeapMemorySizeWithoutChildren() const;
77     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
78     const char *getName() const { return _name.getReprForWrite(); }
79     const char *getDescription() const { return _description.getReprForWrite(); }
80     const char *getUnivName() const { return _univ_name.getReprForWrite(); }
81     const char *getTimeUnit() const { return _dt_unit.getReprForWrite(); }
82     int getIteration() const { return _iteration; }
83     int getOrder() const { return _order; }
84     double getTime() const { return _time; }
85     MCAuto<PartDefinition> getPartDefOfCoo() const { return _part_coords; }
86     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);
87     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);
88     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);
89     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);
90     static bool RenameFamiliesFromFileToMem(std::vector< std::string >& famNames);
91     static bool RenameFamiliesFromMemToFile(std::vector< std::string >& famNames);
92     static MEDCoupling::MEDCouplingAxisType TraduceAxisType(med_axis_type at);
93     static MEDCoupling::MEDCouplingAxisType TraduceAxisTypeStruct(med_grid_type gt);
94     static med_axis_type TraduceAxisTypeRev(MEDCoupling::MEDCouplingAxisType at);
95     static med_grid_type TraduceAxisTypeRevStruct(MEDCoupling::MEDCouplingAxisType at);
96   private:
97     typedef bool (*RenameFamiliesPatternFunc)(std::vector< std::string >&);
98     static void RenameFamiliesPatternInternal(std::vector< std::pair<std::string,std::pair<int,std::vector<std::string> > > >& crudeFams, RenameFamiliesPatternFunc func);
99     static void RenameFamiliesFromFileToMemInternal(std::vector< std::pair<std::string,std::pair<int,std::vector<std::string> > > >& crudeFams);
100     static void RenameFamiliesFromMemToFileInternal(std::vector< std::pair<std::string,std::pair<int,std::vector<std::string> > > >& crudeFams);
101   public:
102     static const char ZE_SEP_FOR_FAMILY_KILLERS[];
103     static int ZE_SEP2_FOR_FAMILY_KILLERS;
104   protected:
105     MEDFileString _name;
106     MEDFileString _description;
107     MEDFileString _univ_name;
108     MEDFileString _dt_unit;
109     int _iteration;
110     int _order;
111     double _time;
112     MCAuto<PartDefinition> _part_coords;
113   };
114
115   class MEDFileUMeshL2 : public MEDFileMeshL2
116   {
117   public:
118     MEDFileUMeshL2();
119     std::vector<std::string> loadCommonPart(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it, int& Mdim);
120     void loadAll(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs);
121     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);
122     void loadConnectivity(med_idt fid, int mdim, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs);
123     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);
124     void loadCoords(med_idt fid, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it);
125     void loadPartCoords(med_idt fid, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it, int nMin, int nMax);
126     int getNumberOfLevels() const { return _per_type_mesh.size(); }
127     bool emptyLev(int levId) const { return _per_type_mesh[levId].empty(); }
128     const std::vector< MCAuto<MEDFileUMeshPerType> >& getLev(int levId) const { return _per_type_mesh[levId]; }
129     bool isFamDefinedOnLev(int levId) const;
130     bool isNumDefinedOnLev(int levId) const;
131     bool isNamesDefinedOnLev(int levId) const;
132     MCAuto<DataArrayDouble> getCoords() const { return _coords; }
133     MCAuto<DataArrayInt> getCoordsFamily() const { return _fam_coords; }
134     MCAuto<DataArrayInt> getCoordsNum() const { return _num_coords; }
135     MCAuto<DataArrayAsciiChar> getCoordsName() const { return _name_coords; }
136     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);
137   private:
138     void sortTypes();
139   private:
140     std::vector< std::vector< MCAuto<MEDFileUMeshPerType> > > _per_type_mesh;
141     MCAuto<DataArrayDouble> _coords;
142     MCAuto<DataArrayInt> _fam_coords;
143     MCAuto<DataArrayInt> _num_coords;
144     MCAuto<DataArrayAsciiChar> _name_coords;
145   };
146
147   class MEDFileStrMeshL2 : public MEDFileMeshL2
148   {
149   };
150
151   class MEDFileCMeshL2 : public MEDFileStrMeshL2
152   {
153   public:
154     MEDFileCMeshL2();
155     void loadAll(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it);
156     MEDCouplingCMesh *getMesh() { return _cmesh; }
157     MEDCoupling::MEDCouplingAxisType getAxisType() const { return _ax_type; }
158   private:
159     static med_data_type GetDataTypeCorrespondingToSpaceId(int id);
160   private:
161     MCAuto<MEDCouplingCMesh> _cmesh;
162     MEDCoupling::MEDCouplingAxisType _ax_type;
163   };
164
165   class MEDFileCLMeshL2 : public MEDFileStrMeshL2
166   {
167   public:
168     MEDFileCLMeshL2();
169     void loadAll(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it);
170     MEDCouplingCurveLinearMesh *getMesh() { return _clmesh; }
171   private:
172     MCAuto<MEDCouplingCurveLinearMesh> _clmesh;
173   };
174
175   class MEDFileMesh;
176   class MEDFileUMeshSplitL1;
177
178   class MEDFileUMeshPermCompute : public BigMemoryObject
179   {
180   public:
181     MEDFileUMeshPermCompute(const MEDFileUMeshSplitL1* st);
182     operator MEDCouplingUMesh *() const;
183     void operator=(MEDCouplingUMesh *m);
184     void updateTime() const;
185     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
186     std::size_t getHeapMemorySizeWithoutChildren() const;
187   private:
188     const MEDFileUMeshSplitL1 *_st;
189     mutable std::size_t _mpt_time;
190     mutable std::size_t _num_time;
191     mutable MCAuto<MEDCouplingUMesh> _m;
192   };
193
194   class MEDFileUMeshAggregateCompute : public BigMemoryObject
195   {
196   public:
197     MEDFileUMeshAggregateCompute();
198     void setName(const std::string& name);
199     void assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts);
200     void assignDefParts(const std::vector<const PartDefinition *>& partDefs);
201     void assignUMesh(MEDCouplingUMesh *m);
202     MEDCouplingUMesh *getUmesh() const;
203     int getNumberOfCells() const;
204     std::vector<MEDCoupling1GTUMesh *> getParts() const;
205     std::vector<INTERP_KERNEL::NormalizedCellType> getGeoTypes() const;
206     int getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType ct) const;
207     std::vector<MEDCoupling1GTUMesh *> retrievePartsWithoutComputation() const;
208     MEDCoupling1GTUMesh *retrievePartWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const;
209     void getStartStopOfGeoTypeWithoutComputation(INTERP_KERNEL::NormalizedCellType gt, int& start, int& stop) const;
210     void renumberNodesInConnWithoutComputation(const int *newNodeNumbersO2N);
211     bool isStoredSplitByType() const;
212     std::size_t getTimeOfThis() const;
213     std::size_t getHeapMemorySizeWithoutChildren() const;
214     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
215     MEDFileUMeshAggregateCompute deepCopy(DataArrayDouble *coords) const;
216     void shallowCpyMeshes();
217     bool isEqual(const MEDFileUMeshAggregateCompute& other, double eps, std::string& what) const;
218     void checkConsistency() const;
219     void clearNonDiscrAttributes() const;
220     void synchronizeTinyInfo(const MEDFileMesh& master) const;
221     bool empty() const;
222     int getMeshDimension() const;
223     std::vector<int> getDistributionOfTypes() const;
224     int getSize() const;
225     void setCoords(DataArrayDouble *coords);
226     void forceComputationOfPartsFromUMesh() const;
227     const PartDefinition *getPartDefOfWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const;
228     void serialize(std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI) const;
229     void unserialize(const std::string& name, DataArrayDouble *coo, std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI);
230   private:
231     std::size_t getTimeOfParts() const;
232     std::size_t getTimeOfUMesh() const;
233   private:
234     mutable std::vector< MCAuto<MEDCoupling1GTUMesh> > _m_parts;
235     mutable std::size_t _mp_time;
236     mutable std::size_t _m_time;
237     mutable MCAuto<MEDCouplingUMesh> _m;
238     mutable std::vector< MCAuto<PartDefinition> > _part_def;
239   };
240
241   class MEDFileUMeshSplitL1 : public RefCountObject
242   {
243     friend class MEDFileUMeshPermCompute;
244     friend class MEDFileUMesh;
245   public:
246     MEDFileUMeshSplitL1(const MEDFileUMeshSplitL1& other);
247     MEDFileUMeshSplitL1(const MEDFileUMeshL2& l2, const std::string& mName, int id);
248     MEDFileUMeshSplitL1(MEDCoupling1GTUMesh *m);
249     MEDFileUMeshSplitL1(MEDCouplingUMesh *m);
250     MEDFileUMeshSplitL1(MEDCouplingUMesh *m, bool newOrOld);
251     void setName(const std::string& name);
252     std::size_t getHeapMemorySizeWithoutChildren() const;
253     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
254     MEDFileUMeshSplitL1 *shallowCpyUsingCoords(DataArrayDouble *coords) const;
255     MEDFileUMeshSplitL1 *deepCopy(DataArrayDouble *coords) const;
256     void checkConsistency() const;
257     void setCoords(DataArrayDouble *coords);
258     bool isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const;
259     void clearNonDiscrAttributes() const;
260     void synchronizeTinyInfo(const MEDFileMesh& master) const;
261     void assignMesh(MEDCouplingUMesh *m, bool newOrOld);
262     void assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts);
263     void forceComputationOfParts() const;
264     bool empty() const;
265     bool presenceOfOneFams(const std::vector<int>& ids) const;
266     int getMeshDimension() const;
267     void simpleRepr(std::ostream& oss) const;
268     int getSize() const;
269     MEDCouplingUMesh *getFamilyPart(const int *idsBg, const int *idsEnd, bool renum) const;
270     DataArrayInt *getFamilyPartArr(const int *idsBg, const int *idsEnd, bool renum) const;
271     MEDCouplingUMesh *getWholeMesh(bool renum) const;
272     int getNumberOfCells() const;
273     bool isMeshStoredSplitByType() const { return _m_by_types.isStoredSplitByType(); }
274     std::vector<INTERP_KERNEL::NormalizedCellType> getGeoTypes() const;
275     int getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType ct) const;
276     std::vector<MEDCoupling1GTUMesh *> getDirectUndergroundSingleGeoTypeMeshes() const { return _m_by_types.retrievePartsWithoutComputation(); }
277     MEDCoupling1GTUMesh *getDirectUndergroundSingleGeoTypeMesh(INTERP_KERNEL::NormalizedCellType gt) const { return _m_by_types.retrievePartWithoutComputation(gt); }
278     DataArrayInt *extractFamilyFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const;
279     DataArrayInt *extractNumberFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const;
280     std::vector<int> getDistributionOfTypes() const { return _m_by_types.getDistributionOfTypes(); }
281     DataArrayInt *getOrCreateAndGetFamilyField();
282     const DataArrayInt *getFamilyField() const;
283     const DataArrayInt *getNumberField() const;
284     const DataArrayAsciiChar *getNameField() const;
285     const DataArrayInt *getRevNumberField() const;
286     const PartDefinition *getPartDef(INTERP_KERNEL::NormalizedCellType gt) const;
287     void eraseFamilyField();
288     void setGroupsFromScratch(const std::vector<const MEDCouplingUMesh *>& ms, std::map<std::string,int>& familyIds,
289                               std::map<std::string, std::vector<std::string> >& groups);
290     void write(med_idt fid, const std::string& mName, int mdim) const;
291     //
292     void setFamilyArr(DataArrayInt *famArr);
293     DataArrayInt *getFamilyField();
294     void setRenumArr(DataArrayInt *renumArr);
295     void setNameArr(DataArrayAsciiChar *nameArr);
296     void changeFamilyIdArr(int oldId, int newId);
297     //
298     void renumberNodesInConn(const int *newNodeNumbersO2N);
299     //
300     void serialize(std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI) const;
301     void unserialize(const std::string& name, DataArrayDouble *coo, std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI);
302     //
303     static void ClearNonDiscrAttributes(const MEDCouplingMesh *tmp);
304     static std::vector<int> GetNewFamiliesNumber(int nb, const std::map<std::string,int>& families);
305     static void TraduceFamilyNumber(const std::vector< std::vector<int> >& fidsGrps, std::map<std::string,int>& familyIds,
306                                     std::map<int,int>& famIdTrad, std::map<int,std::string>& newfams);
307     static DataArrayInt *Renumber(const DataArrayInt *renum, const DataArrayInt *da);
308     static MEDCouplingUMesh *Renumber2(const DataArrayInt *renum, MEDCouplingUMesh *m, const int *cellIds);
309     static MEDFileUMeshSplitL1 *Unserialize(const std::string& name, DataArrayDouble *coo, std::vector<int>& tinyInt, std::vector< MCAuto<DataArrayInt> >& bigArraysI);
310   private:
311     MEDFileUMeshSplitL1();
312     void assignCommonPart();
313     MEDCouplingUMesh *renumIfNeeded(MEDCouplingUMesh *m, const int *cellIds) const;
314     DataArrayInt *renumIfNeededArr(const DataArrayInt *da) const;
315     void computeRevNum() const;
316   private:
317     MEDFileUMeshAggregateCompute _m_by_types;
318     MCAuto<DataArrayInt> _fam;
319     MCAuto<DataArrayInt> _num;
320     MCAuto<DataArrayAsciiChar> _names;
321     mutable MCAuto<DataArrayInt> _rev_num;
322     MEDFileUMeshPermCompute _m;
323   };
324
325   class MEDFileEltStruct4Mesh : public RefCountObject
326   {
327   public:
328     static MEDFileEltStruct4Mesh *New(med_idt fid, const std::string& mName, int dt, int it, int iterOnStEltOfMesh, MEDFileMeshReadSelector *mrs);
329     std::string getGeoTypeName() const { return _geo_type_name; }
330     MCAuto<DataArrayInt> getConn() const { return _conn; }
331     MCAuto<MEDFileUMeshPerTypeCommon> getMeshDef() const { return _common; }
332     const std::vector< MCAuto<DataArray> >& getVars() const { return _vars; }
333   private:
334     std::size_t getHeapMemorySizeWithoutChildren() const;
335     std::vector<const MEDCoupling::BigMemoryObject*> getDirectChildrenWithNull() const;
336   private:
337     ~MEDFileEltStruct4Mesh() { }
338   private:
339     MEDFileEltStruct4Mesh(med_idt fid, const std::string& mName, int dt, int it, int iterOnStEltOfMesh, MEDFileMeshReadSelector *mrs);
340   private:
341     std::string _geo_type_name;
342     int _geo_type;
343     MCAuto<DataArrayInt> _conn;
344     MCAuto<MEDFileUMeshPerTypeCommon> _common;
345     std::vector< MCAuto<DataArray> > _vars;
346   };
347 }
348
349 #endif