1 // Project MULTIPR, IOLS WP1.2.1 - EDF/CS
2 // Partitioning/decimation module for the SALOME v3.2 platform
5 * \file MULTIPR_Utils.cxx
7 * \brief see MULTIPR_Utils.hxx
9 * \author Olivier LE ROUX - CS, Virtual Reality Dpt
14 //*****************************************************************************
16 //*****************************************************************************
18 #include "MULTIPR_Utils.hxx"
19 #include "MULTIPR_Exceptions.hxx"
26 //*****************************************************************************
28 //*****************************************************************************
30 void multipr::trim(char* pStr, char pChar)
32 int len = strlen(pStr) - 1;
34 while (pStr[p] == pChar)
45 bool multipr::startWith(const char* pStr, const char* pStrPrefix)
47 int lenStr = strlen(pStr);
50 while ((pStr[i] != '\0') && (pStrPrefix[i] != '\0') && (pStr[i] == pStrPrefix[i]))
54 return (pStrPrefix[i] == '\0');
58 string multipr::removeExtension(const char* pFilename, const char* pExtension)
60 string strPrefix(pFilename);
61 strPrefix.erase(strPrefix.rfind(pExtension), strlen(pExtension));
67 void multipr::printArray2D(
68 const med_float* pData,
73 for (int itElt = 0 ; itElt < pNumElt ; itElt++)
75 cout << pPrefix << " " << (itElt + 1) << ": ";
76 for (int itDim = 0 ; itDim < pDimElt ; itDim++)
78 cout << pData[itElt * pDimElt + itDim] << " ";
85 string multipr::realToString(med_float mV)
88 sprintf(str, "%lf", mV);
90 int len = strlen(str);
91 if (str[len - 1] == '.')
100 vector<string> multipr::getListMeshes(const char* pMEDfilename)
102 if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
107 //---------------------------------------------------------------------
108 // Open MED file (READ_ONLY)
109 //---------------------------------------------------------------------
110 med_idt file = MEDouvrir(const_cast<char*>(pMEDfilename), MED_LECTURE); // open MED file for reading
111 if (file <= 0) throw multipr::IOException("MED file not found or not a sequential MED file", __FILE__, __LINE__);
113 //---------------------------------------------------------------------
114 // Read name of meshes
115 //---------------------------------------------------------------------
116 med_int nbMeshes = MEDnMaa(file);
117 if (nbMeshes <= 0) throw multipr::IOException("i/o error while reading number of meshes in MED file", __FILE__, __LINE__);
119 // for each mesh in the file (warning: first mesh is number 1)
120 for (int itMesh = 1 ; itMesh <= nbMeshes ; itMesh++)
122 char meshName[MED_TAILLE_NOM + 1];
125 med_maillage meshType;
126 char meshDesc[MED_TAILLE_DESC + 1];
136 if (ret != 0) throw multipr::IOException("i/o error while reading mesh information in MED file", __FILE__, __LINE__);
138 res.push_back(meshName);
141 //---------------------------------------------------------------------
142 // Close the MED file
143 //---------------------------------------------------------------------
144 ret = MEDfermer(file);
145 if (ret != 0) throw multipr::IOException("i/o error while closing MED file", __FILE__, __LINE__);
151 vector<pair<string,int> > multipr::getListFields(const char* pMEDfilename)
153 if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
155 vector<pair<string, int> > res;
158 //---------------------------------------------------------------------
159 // Open MED file (READ_ONLY)
160 //---------------------------------------------------------------------
161 med_idt file = MEDouvrir(const_cast<char*>(pMEDfilename), MED_LECTURE); // open MED file for reading
162 if (file <= 0) throw multipr::IOException("MED file not found or not a sequential MED file", __FILE__, __LINE__);
164 //---------------------------------------------------------------------
165 // Read number of fields
166 //---------------------------------------------------------------------
167 med_int numFields = MEDnChamp(file, 0);
168 if (numFields <= 0) throw IOException("", __FILE__, __LINE__);
170 //---------------------------------------------------------------------
171 // For each field, read its name
172 //---------------------------------------------------------------------
173 for (int itField = 1 ; itField <= numFields ; itField++)
175 char name[MED_TAILLE_NOM + 1];
178 med_int numComponents = MEDnChamp(file, itField);
180 if (numComponents < 0) throw IOException("", __FILE__, __LINE__);
182 // collect scalar field only (not vectorial fields)
183 if (numComponents != 1)
186 char* strComponent = new char[numComponents * MED_TAILLE_PNOM + 1];
187 char* strUnit = new char[numComponents * MED_TAILLE_PNOM + 1];
189 strComponent[0] = '\0';
192 med_err ret = MEDchampInfo(
201 if (ret != 0) throw IOException("", __FILE__, __LINE__);
204 delete[] strComponent;
206 med_int numTimeStep = MEDnPasdetemps(
210 (med_geometrie_element) 0);
212 if (numTimeStep < 0) throw IOException("", __FILE__, __LINE__);
214 if (numTimeStep == 0)
216 numTimeStep = MEDnPasdetemps(
222 if (numTimeStep < 0) throw IOException("", __FILE__, __LINE__);
226 res.push_back(make_pair(name, numTimeStep));
229 //---------------------------------------------------------------------
230 // Close the MED file
231 //---------------------------------------------------------------------
232 ret = MEDfermer(file);
233 if (ret != 0) throw multipr::IOException("i/o error while closing MED file", __FILE__, __LINE__);