Salome HOME
Copyright update 2021
[tools/medcoupling.git] / src / MEDLoader / SauvWriter.hxx
1 // Copyright (C) 2007-2021  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 // File      : SauvWriter.hxx
20 // Created   : Wed Aug 24 11:18:49 2011
21 // Author    : Edward AGAPOV (eap)
22
23 #ifndef __SAUVWRITER_HXX__
24 #define __SAUVWRITER_HXX__
25
26 #include "MEDLoaderDefines.hxx"
27 #include "MEDCouplingRefCountObject.hxx"
28 #include "NormalizedUnstructuredMesh.hxx"
29 #include "SauvUtilities.hxx"
30 #include "MCAuto.hxx"
31 #include "MCType.hxx"
32
33 #include <vector>
34 #include <string>
35 #include <map>
36
37 namespace MEDCoupling
38 {
39   class MEDFileData;
40   class MEDFileMesh;
41   class MEDFileFieldMultiTS;
42   class DataArrayIdType;
43
44   /*!
45    * \brief Class to write a MEDFileData into a SAUVE format file
46    */
47   class SauvWriter : public MEDCoupling::RefCountObject
48   {
49   public:
50     MEDLOADER_EXPORT static SauvWriter *New();
51     MEDLOADER_EXPORT void setMEDFileDS(const MEDFileData* medData, unsigned meshIndex = 0);
52     MEDLOADER_EXPORT void write(const std::string& fileName);
53     MEDLOADER_EXPORT void setCpyGrpIfOnASingleFamilyStatus(bool status);
54     MEDLOADER_EXPORT bool getCpyGrpIfOnASingleFamilyStatus() const;
55     MEDLOADER_EXPORT std::string getClassName() const override { return std::string("SauvWriter"); }
56   private:
57     SauvWriter();
58     std::size_t getHeapMemorySizeWithoutChildren() const;
59     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
60     /*!
61      * \brief Class representing a GIBI sub-mesh (described in the pile 1 of the SAUVE file).
62      * It stands for a named med sub-mesh (family, etc) and contains either cell IDs or other sub-meshes. 
63      */
64     struct SubMesh
65     {
66       std::vector<mcIdType> _cellIDsByType[ INTERP_KERNEL::NORM_MAXTYPE+1 ];
67       std::vector<SubMesh*> _subs;
68       std::string           _name;
69       int                   _id;
70       int                   _nbSauvObjects;
71       int                   _dimRelExt;
72       int nbTypes() const;
73       static int cellIDsByTypeSize() { return INTERP_KERNEL::NORM_MAXTYPE+1; }
74     };
75     SubMesh* addSubMesh(const std::string& name, int dimRelExt);
76
77     void fillSubMeshes(int& nbSauvObjects, std::map<std::string,int>& nameNbMap);
78     void fillFamilySubMeshes();
79     void fillGroupSubMeshes();
80     void fillProfileSubMeshes();
81     int evaluateNbProfileSubMeshes() const;
82     void makeCompNames(const std::string&                  fieldName,
83                        const std::vector<std::string>&     compInfo,
84                        std::map<std::string, std::string>& compMedToGibi );
85     void makeProfileIDs( SubMesh*                          sm,
86                          INTERP_KERNEL::NormalizedCellType type,
87                          const DataArrayIdType*            profile );
88     void writeFileHead();
89     void writeSubMeshes();
90     void writeCompoundSubMesh(int iSub);
91     void writeNodes();
92     void writeLongNames();
93     void writeNodalFields(std::map<std::string,int>& fldNamePrefixMap);
94     void writeElemFields(std::map<std::string,int>& fldNamePrefixMap);
95     void writeFieldNames(const bool isNodal, std::map<std::string,int>& fldNamePrefixMap);
96     void writeElemTimeStamp(int iF, int iter, int order);
97     void writeLastRecord();
98     void writeNames( const std::map<std::string,int>& nameNbMap );
99
100   private:
101
102     MCAuto< MEDFileMesh >                        _fileMesh;
103     std::vector< MCAuto< MEDFileFieldMultiTS > > _nodeFields;
104     std::vector< MCAuto< MEDFileFieldMultiTS > > _cellFields;
105
106     std::vector<SubMesh>                      _subs;
107     std::map< mcIdType, SubMesh* >            _famIDs2Sub;
108     std::map< std::string, SubMesh* >         _profile2Sub;
109     enum
110       {
111         LN_MAIL=0, LN_CHAM, LN_COMP, LN_NB
112       };
113     std::vector<SauvUtilities::nameGIBItoMED> _longNames[ LN_NB ];
114
115     std::fstream*                             _sauvFile;
116     bool _cpy_grp_if_on_single_family;
117   };
118 }
119
120 #endif