Salome HOME
[EDF23738] : multi geometric type in a single mesh having structure elements on each...
[tools/medcoupling.git] / src / MEDLoader / MEDFileMeshSupport.cxx
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 // Author : Anthony Geay (EDF R&D)
20
21 #include "MEDFileMeshSupport.hxx"
22 #include "MEDLoaderBase.hxx"
23 #include "MEDFileMeshLL.hxx"
24 #include "MEDFileSafeCaller.txx"
25
26 #include "InterpKernelAutoPtr.hxx"
27
28 using namespace MEDCoupling;
29
30 MEDFileMeshSupports *MEDFileMeshSupports::New(const std::string& fileName)
31 {
32   MEDFileUtilities::AutoFid fid(OpenMEDFileForRead(fileName));
33   return New(fid);
34 }
35
36 MEDFileMeshSupports *MEDFileMeshSupports::New(med_idt fid)
37 {
38   return new MEDFileMeshSupports(fid);
39 }
40
41 MEDFileMeshSupports *MEDFileMeshSupports::New()
42 {
43   return new MEDFileMeshSupports;
44 }
45
46 MEDFileMeshSupports::MEDFileMeshSupports(med_idt fid)
47 {
48   med_int nbSM(MEDnSupportMesh(fid));
49   _supports.resize(nbSM);
50   for(int i=0;i<nbSM;i++)
51     {
52       INTERP_KERNEL::AutoPtr<char> msn(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
53       INTERP_KERNEL::AutoPtr<char> description(MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE));
54       med_axis_type axType;
55       med_int nAxis(MEDsupportMeshnAxis(fid,i+1));
56       INTERP_KERNEL::AutoPtr<char> axisName(new char[MED_SNAME_SIZE*nAxis+1]),axisUnit(new char[MED_SNAME_SIZE*nAxis+1]);
57       med_int spaceDim(0),meshDim(0);
58       MEDFILESAFECALLERRD0(MEDsupportMeshInfo,(fid,i+1,msn,&spaceDim,&meshDim,description,&axType,axisName,axisUnit));
59       std::string name(MEDLoaderBase::buildStringFromFortran(msn,MED_NAME_SIZE));
60       _supports[i]=MEDFileUMesh::New(fid,name);
61     }
62 }
63
64 MEDFileMeshSupports::MEDFileMeshSupports()
65 {
66 }
67
68 MEDFileMeshSupports::~MEDFileMeshSupports()
69 {
70 }
71
72 std::vector<const BigMemoryObject *> MEDFileMeshSupports::getDirectChildrenWithNull() const
73 {
74   std::size_t sz(_supports.size());
75   std::vector<const BigMemoryObject *> ret(sz);
76   for(std::size_t i=0;i<sz;i++)
77     ret[i]=_supports[i];
78   return ret;
79 }
80
81 std::size_t MEDFileMeshSupports::getHeapMemorySizeWithoutChildren() const
82 {
83   return _supports.capacity()*sizeof(MCAuto<MEDFileUMesh>);
84 }
85
86 void MEDFileMeshSupports::writeLL(med_idt fid) const
87 {
88   for(std::vector< MCAuto<MEDFileUMesh> >::const_iterator it=_supports.begin();it!=_supports.end();it++)
89     if((*it).isNotNull())
90       (*it)->writeLL(fid);
91 }
92
93 std::vector<std::string> MEDFileMeshSupports::getSupMeshNames() const
94 {
95   std::vector<std::string> ret;
96   for(std::vector< MCAuto<MEDFileUMesh> >::const_iterator it=_supports.begin();it!=_supports.end();it++)
97     if((*it).isNotNull())
98       ret.push_back((*it)->getName());
99   return ret;
100 }
101
102 const MEDFileUMesh *MEDFileMeshSupports::getSupMeshWithName(const std::string& name) const
103 {
104   std::vector<std::string> mns;
105   for(std::vector< MCAuto<MEDFileUMesh> >::const_iterator it=_supports.begin();it!=_supports.end();it++)
106     {
107       if((*it).isNotNull())
108         {
109           std::string na((*it)->getName());
110           if(na==name)
111             return *it;
112           else
113             mns.push_back(na);
114         }
115     }
116   std::ostringstream oss;
117   oss << "MEDFileMeshSupports::getSupMeshWithName : no such name \"" << name << "\". Possibilities are :";
118   std::copy(mns.begin(),mns.end(),std::ostream_iterator<std::string>(oss,","));
119   oss << " !";
120   throw INTERP_KERNEL::Exception(oss.str());
121 }
122
123 mcIdType MEDFileMeshSupports::getNumberOfNodesInConnOf(TypeOfField entity, INTERP_KERNEL::NormalizedCellType gt, const std::string& name) const
124 {
125   const MEDFileUMesh *sup(getSupMeshWithName(name));
126   switch(entity)
127     {
128     case ON_NODES:
129       return sup->getNumberOfNodes();
130     case ON_CELLS:
131       {
132         std::vector<INTERP_KERNEL::NormalizedCellType> gts(sup->getAllGeoTypes());
133         if(std::find(gts.begin(),gts.end(),gt) == gts.end())
134           throw INTERP_KERNEL::Exception("MEDFileMeshSupports::getNumberOfNodesInConnOf : specified geometric type not found !");
135         const INTERP_KERNEL::CellModel& cm(INTERP_KERNEL::CellModel::GetCellModel(gt));
136         return sup->getNumberOfCellsWithType(gt)*cm.getNumberOfNodes();
137       }
138     default:
139       throw INTERP_KERNEL::Exception("MEDFileMeshSupports::getNumberOfNodesInConnOf : not recognized entity type !");
140     }
141 }