Salome HOME
First step of ParaMEDMEM -> MEDCoupling
[tools/medcoupling.git] / src / MEDLoader / MEDFileMeshLL.hxx
1 // Copyright (C) 2007-2015  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 "MEDCouplingAutoRefCountObjectPtr.hxx"
33
34 #include "med.h"
35
36 #include <map>
37
38 namespace MEDCoupling
39 {
40   class MEDFileMeshReadSelector;
41
42   class MEDFileMeshL2 : public RefCountObject
43   {
44   public:
45     MEDFileMeshL2();
46     std::size_t getHeapMemorySizeWithoutChildren() const;
47     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
48     const char *getName() const { return _name.getReprForWrite(); }
49     const char *getDescription() const { return _description.getReprForWrite(); }
50     const char *getUnivName() const { return _univ_name.getReprForWrite(); }
51     const char *getTimeUnit() const { return _dt_unit.getReprForWrite(); }
52     int getIteration() const { return _iteration; }
53     int getOrder() const { return _order; }
54     double getTime() const { return _time; }
55     MEDCouplingAutoRefCountObjectPtr<PartDefinition> getPartDefOfCoo() const { return _part_coords; }
56     std::vector<std::string> getAxisInfoOnMesh(med_idt fid, int mId, const std::string& mName, MEDCoupling::MEDCouplingMeshType& meshType, MEDCoupling::MEDCouplingAxisType& axType, int& nstep, int& Mdim);
57     static int GetMeshIdFromName(med_idt fid, const std::string& mName, MEDCoupling::MEDCouplingMeshType& meshType, MEDCoupling::MEDCouplingAxisType& axType, int& dt, int& it, std::string& dtunit1);
58     static double CheckMeshTimeStep(med_idt fid, const std::string& mname, int nstep, int dt, int it);
59     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);
60     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);
61     static bool RenameFamiliesFromFileToMem(std::vector< std::string >& famNames);
62     static bool RenameFamiliesFromMemToFile(std::vector< std::string >& famNames);
63     static MEDCoupling::MEDCouplingAxisType TraduceAxisType(med_axis_type at);
64     static MEDCoupling::MEDCouplingAxisType TraduceAxisTypeStruct(med_grid_type gt);
65     static med_axis_type TraduceAxisTypeRev(MEDCoupling::MEDCouplingAxisType at);
66     static med_grid_type TraduceAxisTypeRevStruct(MEDCoupling::MEDCouplingAxisType at);
67   private:
68     typedef bool (*RenameFamiliesPatternFunc)(std::vector< std::string >&);
69     static void RenameFamiliesPatternInternal(std::vector< std::pair<std::string,std::pair<int,std::vector<std::string> > > >& crudeFams, RenameFamiliesPatternFunc func);
70     static void RenameFamiliesFromFileToMemInternal(std::vector< std::pair<std::string,std::pair<int,std::vector<std::string> > > >& crudeFams);
71     static void RenameFamiliesFromMemToFileInternal(std::vector< std::pair<std::string,std::pair<int,std::vector<std::string> > > >& crudeFams);
72   public:
73     static const char ZE_SEP_FOR_FAMILY_KILLERS[];
74     static int ZE_SEP2_FOR_FAMILY_KILLERS;
75   protected:
76     MEDFileString _name;
77     MEDFileString _description;
78     MEDFileString _univ_name;
79     MEDFileString _dt_unit;
80     int _iteration;
81     int _order;
82     double _time;
83     MEDCouplingAutoRefCountObjectPtr<PartDefinition> _part_coords;
84   };
85
86   class MEDFileUMeshL2 : public MEDFileMeshL2
87   {
88   public:
89     MEDFileUMeshL2();
90     std::vector<std::string> loadCommonPart(med_idt fid, int mId, const std::string& mName, int dt, int it, int& Mdim);
91     void loadAll(med_idt fid, int mId, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs);
92     void loadPart(med_idt fid, int mId, const std::string& mName, const std::vector<INTERP_KERNEL::NormalizedCellType>& types, const std::vector<int>& slicPerTyp, int dt, int it, MEDFileMeshReadSelector *mrs);
93     void loadConnectivity(med_idt fid, int mdim, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs);
94     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);
95     void loadCoords(med_idt fid, int mId, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it);
96     void loadPartCoords(med_idt fid, int mId, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it, int nMin, int nMax);
97     int getNumberOfLevels() const { return _per_type_mesh.size(); }
98     bool emptyLev(int levId) const { return _per_type_mesh[levId].empty(); }
99     const std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >& getLev(int levId) const { return _per_type_mesh[levId]; }
100     bool isFamDefinedOnLev(int levId) const;
101     bool isNumDefinedOnLev(int levId) const;
102     bool isNamesDefinedOnLev(int levId) const;
103     MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> getCoords() const { return _coords; }
104     MEDCouplingAutoRefCountObjectPtr<DataArrayInt> getCoordsFamily() const { return _fam_coords; }
105     MEDCouplingAutoRefCountObjectPtr<DataArrayInt> getCoordsNum() const { return _num_coords; }
106     MEDCouplingAutoRefCountObjectPtr<DataArrayAsciiChar> getCoordsName() const { return _name_coords; }
107     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);
108   private:
109     void sortTypes();
110   private:
111     std::vector< std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > > _per_type_mesh;
112     MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> _coords;
113     MEDCouplingAutoRefCountObjectPtr<DataArrayInt> _fam_coords;
114     MEDCouplingAutoRefCountObjectPtr<DataArrayInt> _num_coords;
115     MEDCouplingAutoRefCountObjectPtr<DataArrayAsciiChar> _name_coords;
116   };
117
118   class MEDFileStrMeshL2 : public MEDFileMeshL2
119   {
120   };
121
122   class MEDFileCMeshL2 : public MEDFileStrMeshL2
123   {
124   public:
125     MEDFileCMeshL2();
126     void loadAll(med_idt fid, int mId, const std::string& mName, int dt, int it);
127     MEDCouplingCMesh *getMesh() { return _cmesh; }
128     MEDCoupling::MEDCouplingAxisType getAxType() const { return _ax_type; }
129   private:
130     static med_data_type GetDataTypeCorrespondingToSpaceId(int id);
131   private:
132     MEDCouplingAutoRefCountObjectPtr<MEDCouplingCMesh> _cmesh;
133     MEDCoupling::MEDCouplingAxisType _ax_type;
134   };
135
136   class MEDFileCLMeshL2 : public MEDFileStrMeshL2
137   {
138   public:
139     MEDFileCLMeshL2();
140     void loadAll(med_idt fid, int mId, const std::string& mName, int dt, int it);
141     MEDCouplingCurveLinearMesh *getMesh() { return _clmesh; }
142   private:
143     MEDCouplingAutoRefCountObjectPtr<MEDCouplingCurveLinearMesh> _clmesh;
144   };
145
146   class MEDFileMesh;
147   class MEDFileUMeshSplitL1;
148
149   class MEDFileUMeshPermCompute : public BigMemoryObject
150   {
151   public:
152     MEDFileUMeshPermCompute(const MEDFileUMeshSplitL1* st);
153     operator MEDCouplingUMesh *() const;
154     void operator=(MEDCouplingUMesh *m);
155     void updateTime() const;
156     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
157     std::size_t getHeapMemorySizeWithoutChildren() const;
158   private:
159     const MEDFileUMeshSplitL1 *_st;
160     mutable std::size_t _mpt_time;
161     mutable std::size_t _num_time;
162     mutable MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> _m;
163   };
164
165   class MEDFileUMeshAggregateCompute : public BigMemoryObject
166   {
167   public:
168     MEDFileUMeshAggregateCompute();
169     void setName(const std::string& name);
170     void assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts);
171     void assignDefParts(const std::vector<const PartDefinition *>& partDefs);
172     void assignUMesh(MEDCouplingUMesh *m);
173     MEDCouplingUMesh *getUmesh() const;
174     int getNumberOfCells() const;
175     std::vector<MEDCoupling1GTUMesh *> getParts() const;
176     std::vector<INTERP_KERNEL::NormalizedCellType> getGeoTypes() const;
177     int getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType ct) const;
178     std::vector<MEDCoupling1GTUMesh *> retrievePartsWithoutComputation() const;
179     MEDCoupling1GTUMesh *retrievePartWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const;
180     void getStartStopOfGeoTypeWithoutComputation(INTERP_KERNEL::NormalizedCellType gt, int& start, int& stop) const;
181     void renumberNodesInConnWithoutComputation(const int *newNodeNumbersO2N);
182     bool isStoredSplitByType() const;
183     std::size_t getTimeOfThis() const;
184     std::size_t getHeapMemorySizeWithoutChildren() const;
185     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
186     MEDFileUMeshAggregateCompute deepCpy(DataArrayDouble *coords) const;
187     void shallowCpyMeshes();
188     bool isEqual(const MEDFileUMeshAggregateCompute& other, double eps, std::string& what) const;
189     void clearNonDiscrAttributes() const;
190     void synchronizeTinyInfo(const MEDFileMesh& master) const;
191     bool empty() const;
192     int getMeshDimension() const;
193     std::vector<int> getDistributionOfTypes() const;
194     int getSize() const;
195     void setCoords(DataArrayDouble *coords);
196     void forceComputationOfPartsFromUMesh() const;
197     const PartDefinition *getPartDefOfWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const;
198     void serialize(std::vector<int>& tinyInt, std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> >& bigArraysI) const;
199     void unserialize(const std::string& name, DataArrayDouble *coo, std::vector<int>& tinyInt, std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> >& bigArraysI);
200   private:
201     std::size_t getTimeOfParts() const;
202     std::size_t getTimeOfUMesh() const;
203   private:
204     mutable std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> > _m_parts;
205     mutable std::size_t _mp_time;
206     mutable std::size_t _m_time;
207     mutable MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> _m;
208     mutable std::vector< MEDCouplingAutoRefCountObjectPtr<PartDefinition> > _part_def;
209   };
210
211   class MEDFileUMeshSplitL1 : public RefCountObject
212   {
213     friend class MEDFileUMeshPermCompute;
214   public:
215     MEDFileUMeshSplitL1(const MEDFileUMeshSplitL1& other);
216     MEDFileUMeshSplitL1(const MEDFileUMeshL2& l2, const std::string& mName, int id);
217     MEDFileUMeshSplitL1(MEDCoupling1GTUMesh *m);
218     MEDFileUMeshSplitL1(MEDCouplingUMesh *m);
219     MEDFileUMeshSplitL1(MEDCouplingUMesh *m, bool newOrOld);
220     void setName(const std::string& name);
221     std::size_t getHeapMemorySizeWithoutChildren() const;
222     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
223     MEDFileUMeshSplitL1 *shallowCpyUsingCoords(DataArrayDouble *coords) const;
224     MEDFileUMeshSplitL1 *deepCpy(DataArrayDouble *coords) const;
225     void setCoords(DataArrayDouble *coords);
226     bool isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const;
227     void clearNonDiscrAttributes() const;
228     void synchronizeTinyInfo(const MEDFileMesh& master) const;
229     void assignMesh(MEDCouplingUMesh *m, bool newOrOld);
230     void assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts);
231     void forceComputationOfParts() const;
232     bool empty() const;
233     bool presenceOfOneFams(const std::vector<int>& ids) const;
234     int getMeshDimension() const;
235     void simpleRepr(std::ostream& oss) const;
236     int getSize() const;
237     MEDCouplingUMesh *getFamilyPart(const int *idsBg, const int *idsEnd, bool renum) const;
238     DataArrayInt *getFamilyPartArr(const int *idsBg, const int *idsEnd, bool renum) const;
239     MEDCouplingUMesh *getWholeMesh(bool renum) const;
240     int getNumberOfCells() const;
241     bool isMeshStoredSplitByType() const { return _m_by_types.isStoredSplitByType(); }
242     std::vector<INTERP_KERNEL::NormalizedCellType> getGeoTypes() const;
243     int getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType ct) const;
244     std::vector<MEDCoupling1GTUMesh *> getDirectUndergroundSingleGeoTypeMeshes() const { return _m_by_types.retrievePartsWithoutComputation(); }
245     MEDCoupling1GTUMesh *getDirectUndergroundSingleGeoTypeMesh(INTERP_KERNEL::NormalizedCellType gt) const { return _m_by_types.retrievePartWithoutComputation(gt); }
246     DataArrayInt *extractFamilyFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const;
247     DataArrayInt *extractNumberFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const;
248     std::vector<int> getDistributionOfTypes() const { return _m_by_types.getDistributionOfTypes(); }
249     DataArrayInt *getOrCreateAndGetFamilyField();
250     const DataArrayInt *getFamilyField() const;
251     const DataArrayInt *getNumberField() const;
252     const DataArrayAsciiChar *getNameField() const;
253     const DataArrayInt *getRevNumberField() const;
254     const PartDefinition *getPartDef(INTERP_KERNEL::NormalizedCellType gt) const;
255     void eraseFamilyField();
256     void setGroupsFromScratch(const std::vector<const MEDCouplingUMesh *>& ms, std::map<std::string,int>& familyIds,
257                               std::map<std::string, std::vector<std::string> >& groups);
258     void write(med_idt fid, const std::string& mName, int mdim) const;
259     //
260     void setFamilyArr(DataArrayInt *famArr);
261     DataArrayInt *getFamilyField();
262     void setRenumArr(DataArrayInt *renumArr);
263     void setNameArr(DataArrayAsciiChar *nameArr);
264     void changeFamilyIdArr(int oldId, int newId);
265     //
266     void renumberNodesInConn(const int *newNodeNumbersO2N);
267     //
268     void serialize(std::vector<int>& tinyInt, std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> >& bigArraysI) const;
269     void unserialize(const std::string& name, DataArrayDouble *coo, std::vector<int>& tinyInt, std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> >& bigArraysI);
270     //
271     static void ClearNonDiscrAttributes(const MEDCouplingMesh *tmp);
272     static std::vector<int> GetNewFamiliesNumber(int nb, const std::map<std::string,int>& families);
273     static void TraduceFamilyNumber(const std::vector< std::vector<int> >& fidsGrps, std::map<std::string,int>& familyIds,
274                                     std::map<int,int>& famIdTrad, std::map<int,std::string>& newfams);
275     static DataArrayInt *Renumber(const DataArrayInt *renum, const DataArrayInt *da);
276     static MEDCouplingUMesh *Renumber2(const DataArrayInt *renum, MEDCouplingUMesh *m, const int *cellIds);
277     static MEDFileUMeshSplitL1 *Unserialize(const std::string& name, DataArrayDouble *coo, std::vector<int>& tinyInt, std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> >& bigArraysI);
278   private:
279     MEDFileUMeshSplitL1();
280     void assignCommonPart();
281     MEDCouplingUMesh *renumIfNeeded(MEDCouplingUMesh *m, const int *cellIds) const;
282     DataArrayInt *renumIfNeededArr(const DataArrayInt *da) const;
283     void computeRevNum() const;
284   private:
285     MEDFileUMeshAggregateCompute _m_by_types;
286     MEDCouplingAutoRefCountObjectPtr<DataArrayInt> _fam;
287     MEDCouplingAutoRefCountObjectPtr<DataArrayInt> _num;
288     MEDCouplingAutoRefCountObjectPtr<DataArrayAsciiChar> _names;
289     mutable MEDCouplingAutoRefCountObjectPtr<DataArrayInt> _rev_num;
290     MEDFileUMeshPermCompute _m;
291   };
292 }
293
294 #endif