Salome HOME
Before refactoring for convergence between meshes and structureMeshes
[tools/medcoupling.git] / src / MEDLoader / MEDFileMeshSupport.cxx
1 // Copyright (C) 2007-2017  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 "MEDFileMeshSupport.hxx"
22
23 #include "MEDLoaderBase.hxx"
24 #include "MEDFileMeshLL.hxx"
25 #include "MEDFileSafeCaller.txx"
26
27 #include "InterpKernelAutoPtr.hxx"
28
29 using namespace MEDCoupling;
30
31 MEDFileMeshSupport *MEDFileMeshSupport::New()
32 {
33   return new MEDFileMeshSupport;
34 }
35
36 MEDFileMeshSupport *MEDFileMeshSupport::New(const std::string& fileName, int smid)
37 {
38   MEDFileUtilities::AutoFid fid(OpenMEDFileForRead(fileName));
39   return New(fid,smid);
40 }
41
42 MEDFileMeshSupport *MEDFileMeshSupport::New(med_idt fid, int smid)
43 {
44   return new MEDFileMeshSupport(fid,smid);
45 }
46
47 std::vector<const BigMemoryObject *> MEDFileMeshSupport::getDirectChildrenWithNull() const
48 {
49   return std::vector<const BigMemoryObject *>();
50 }
51
52 std::size_t MEDFileMeshSupport::getHeapMemorySizeWithoutChildren() const
53 {
54   return 0;
55 }
56
57 int MEDFileMeshSupport::getSpaceDim() const
58 {
59   return _space_dim;
60 }
61
62 void MEDFileMeshSupport::setSpaceDim(int dim)
63 {
64   _space_dim=dim;
65 }
66
67 int MEDFileMeshSupport::getMeshDim() const
68 {
69   return _mesh_dim;
70 }
71
72 void MEDFileMeshSupport::setMeshDim(int dim)
73 {
74   _mesh_dim=dim;
75 }
76
77 void MEDFileMeshSupport::writeLL(med_idt fid) const
78 {
79 }
80
81 MEDFileMeshSupport::MEDFileMeshSupport(med_idt fid, int smid)
82 {
83   INTERP_KERNEL::AutoPtr<char> msn(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
84   INTERP_KERNEL::AutoPtr<char> description(MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE));
85   med_axis_type axType;
86   int nAxis(MEDsupportMeshnAxis(fid,smid+1));
87   INTERP_KERNEL::AutoPtr<char> axisName(new char[MED_SNAME_SIZE*nAxis+1]),axisUnit(new char[MED_SNAME_SIZE*nAxis+1]);
88   MEDFILESAFECALLERRD0(MEDsupportMeshInfo,(fid,smid+1,msn,&_space_dim,&_mesh_dim,description,&axType,axisName,axisUnit));
89   _name=MEDLoaderBase::buildStringFromFortran(msn,MED_NAME_SIZE);
90   _description=MEDLoaderBase::buildStringFromFortran(description,MED_COMMENT_SIZE);
91   _axis_type=MEDFileMeshL2::TraduceAxisType(axType);
92   int nCoords(0),nmodels(0);
93   {
94     med_bool chgt=MED_FALSE,trsf=MED_FALSE;
95     nCoords=MEDmeshnEntity(fid,_name.c_str(),MED_NO_DT,MED_NO_IT,MED_NODE,MED_NO_GEOTYPE,MED_COORDINATE,MED_NODAL,&chgt,&trsf);
96     nmodels=MEDmeshnEntity(fid,_name.c_str(),MED_NO_DT,MED_NO_IT,MED_STRUCT_ELEMENT,MED_GEO_ALL,MED_CONNECTIVITY,MED_NODAL,&chgt,&trsf);
97   }
98   _coords=DataArrayDouble::New(); _coords->alloc(nCoords,nAxis);
99   for(int i=0;i<nAxis;i++)
100     {
101       std::string info(DataArray::BuildInfoFromVarAndUnit(MEDLoaderBase::buildStringFromFortran(axisName+i*MED_SNAME_SIZE,MED_SNAME_SIZE),
102                                                           MEDLoaderBase::buildStringFromFortran(axisUnit+i*MED_SNAME_SIZE,MED_SNAME_SIZE)));
103       _coords->setInfoOnComponent(i,info);
104     }
105   MEDFILESAFECALLERRD0(MEDmeshNodeCoordinateRd,(fid,_name.c_str(),MED_NO_DT,MED_NO_IT,MED_FULL_INTERLACE,_coords->getPointer()));
106   {
107     med_bool chgt=MED_FALSE,trsf=MED_FALSE;
108     if(MEDmeshnEntity(fid,_name.c_str(),MED_NO_DT,MED_NO_IT,MED_NODE,MED_NO_GEOTYPE,MED_FAMILY_NUMBER,MED_NODAL,&chgt,&trsf)>0)
109       {
110         _fam_coords=DataArrayInt::New();
111         _fam_coords->alloc(nCoords,1);
112         MEDFILESAFECALLERRD0(MEDmeshEntityFamilyNumberRd,(fid,_name.c_str(),MED_NO_DT,MED_NO_IT,MED_NODE,MED_NO_GEOTYPE,_fam_coords->getPointer()));
113       }
114     else
115       _fam_coords=0;
116     if(MEDmeshnEntity(fid,_name.c_str(),MED_NO_DT,MED_NO_IT,MED_NODE,MED_NO_GEOTYPE,MED_NUMBER,MED_NODAL,&chgt,&trsf)>0)
117       {
118         _num_coords=DataArrayInt::New();
119         _num_coords->alloc(nCoords,1);
120         MEDFILESAFECALLERRD0(MEDmeshEntityNumberRd,(fid,_name.c_str(),MED_NO_DT,MED_NO_IT,MED_NODE,MED_NO_GEOTYPE,_num_coords->getPointer()));
121       }
122     else
123     _num_coords=0;
124     if(MEDmeshnEntity(fid,_name.c_str(),MED_NO_DT,MED_NO_IT,MED_NODE,MED_NO_GEOTYPE,MED_NAME,MED_NODAL,&chgt,&trsf)>0)
125       {
126         _name_coords=DataArrayAsciiChar::New();
127         _name_coords->alloc(nCoords+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
128         MEDFILESAFECALLERRD0(MEDmeshEntityNameRd,(fid,_name.c_str(),MED_NO_DT,MED_NO_IT,MED_NODE,MED_NO_GEOTYPE,_name_coords->getPointer()));
129         _name_coords->reAlloc(nCoords);//not a bug to avoid the memory corruption due to last \0 at the end
130       }
131     else
132       _name_coords=0;
133   }
134     //med_bool withnodename;
135   //MEDmeshNodeRd(fid,_name.c_str(),MED_NO_DT,MED_NO_IT,MED_FULL_INTERLACE,_coords->getPointer(),);
136   std::cerr << nCoords << " ** " << nmodels << std::endl;
137 }
138
139 MEDFileMeshSupport::~MEDFileMeshSupport()
140 {
141 }
142
143 MEDFileMeshSupport::MEDFileMeshSupport():_space_dim(-1),_mesh_dim(-1),_axis_type(AX_CART)
144 {
145 }
146
147 MEDFileMeshSupports *MEDFileMeshSupports::New(med_idt fid)
148 {
149   return new MEDFileMeshSupports(fid);
150 }
151
152 MEDFileMeshSupports *MEDFileMeshSupports::New()
153 {
154   return new MEDFileMeshSupports;
155 }
156
157 MEDFileMeshSupports::MEDFileMeshSupports(med_idt fid)
158 {
159   int nbSM(MEDnSupportMesh(fid));
160   _supports.resize(nbSM);
161   for(int i=0;i<nbSM;i++)
162     _supports[i]=MEDFileMeshSupport::New(fid,i);
163 }
164
165 MEDFileMeshSupports::MEDFileMeshSupports()
166 {
167 }
168
169 MEDFileMeshSupports::~MEDFileMeshSupports()
170 {
171 }
172
173 std::vector<const BigMemoryObject *> MEDFileMeshSupports::getDirectChildrenWithNull() const
174 {
175   std::size_t sz(_supports.size());
176   std::vector<const BigMemoryObject *> ret(sz);
177   for(std::size_t i=0;i<sz;i++)
178     ret[i]=_supports[i];
179   return ret;
180 }
181
182 std::size_t MEDFileMeshSupports::getHeapMemorySizeWithoutChildren() const
183 {
184   return _supports.capacity()*sizeof(MCAuto<MEDFileMeshSupport>);
185 }
186
187 void MEDFileMeshSupports::writeLL(med_idt fid) const
188 {
189   for(std::vector< MCAuto<MEDFileMeshSupport> >::const_iterator it=_supports.begin();it!=_supports.end();it++)
190     if((*it).isNotNull())
191       (*it)->writeLL(fid);
192 }