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 string multipr::removeExtension(const char* pFilename, const char* pExtension)
47 string strPrefix(pFilename);
48 strPrefix.erase(strPrefix.rfind(pExtension), strlen(pExtension));
54 void multipr::printArray2D(
55 const med_float* pData,
60 for (int itElt = 0 ; itElt < pNumElt ; itElt++)
62 cout << pPrefix << " " << (itElt + 1) << ": ";
63 for (int itDim = 0 ; itDim < pDimElt ; itDim++)
65 cout << pData[itElt * pDimElt + itDim] << " ";
72 string multipr::realToString(med_float mV)
75 sprintf(str, "%lf", mV);
77 int len = strlen(str);
78 if (str[len - 1] == '.')
87 vector<string> multipr::getListMeshes(const char* pMEDfilename)
89 if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
94 //---------------------------------------------------------------------
95 // Open MED file (READ_ONLY)
96 //---------------------------------------------------------------------
97 med_idt file = MEDouvrir(const_cast<char*>(pMEDfilename), MED_LECTURE); // open MED file for reading
98 if (file <= 0) throw multipr::IOException("MED file not found or not a sequential MED file", __FILE__, __LINE__);
100 //---------------------------------------------------------------------
101 // Read name of meshes
102 //---------------------------------------------------------------------
103 med_int nbMeshes = MEDnMaa(file);
104 if (nbMeshes <= 0) throw multipr::IOException("i/o error while reading number of meshes in MED file", __FILE__, __LINE__);
106 // for each mesh in the file (warning: first mesh is number 1)
107 for (int itMesh = 1 ; itMesh <= nbMeshes ; itMesh++)
109 char meshName[MED_TAILLE_NOM + 1];
112 med_maillage meshType;
113 char meshDesc[MED_TAILLE_DESC + 1];
123 if (ret != 0) throw multipr::IOException("i/o error while reading mesh information in MED file", __FILE__, __LINE__);
125 res.push_back(meshName);
128 //---------------------------------------------------------------------
129 // Close the MED file
130 //---------------------------------------------------------------------
131 ret = MEDfermer(file);
132 if (ret != 0) throw multipr::IOException("i/o error while closing MED file", __FILE__, __LINE__);
138 vector<pair<string,int> > multipr::getListFields(const char* pMEDfilename)
140 if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
142 vector<pair<string, int> > res;
145 //---------------------------------------------------------------------
146 // Open MED file (READ_ONLY)
147 //---------------------------------------------------------------------
148 med_idt file = MEDouvrir(const_cast<char*>(pMEDfilename), MED_LECTURE); // open MED file for reading
149 if (file <= 0) throw multipr::IOException("MED file not found or not a sequential MED file", __FILE__, __LINE__);
151 //---------------------------------------------------------------------
152 // Read number of fields
153 //---------------------------------------------------------------------
154 med_int numFields = MEDnChamp(file, 0);
155 if (numFields <= 0) throw IOException("", __FILE__, __LINE__);
157 //---------------------------------------------------------------------
158 // For each field, read its name
159 //---------------------------------------------------------------------
160 for (int itField = 1 ; itField <= numFields ; itField++)
162 char name[MED_TAILLE_NOM + 1];
165 med_int numComponents = MEDnChamp(file, itField);
167 if (numComponents < 0) throw IOException("", __FILE__, __LINE__);
169 // collect scalar field only (not vectorial fields)
170 if (numComponents != 1)
173 char* strComponent = new char[numComponents * MED_TAILLE_PNOM + 1];
174 char* strUnit = new char[numComponents * MED_TAILLE_PNOM + 1];
176 strComponent[0] = '\0';
179 med_err ret = MEDchampInfo(
188 if (ret != 0) throw IOException("", __FILE__, __LINE__);
191 delete[] strComponent;
193 med_int numTimeStep = MEDnPasdetemps(
197 (med_geometrie_element) 0);
199 if (numTimeStep < 0) throw IOException("", __FILE__, __LINE__);
201 if (numTimeStep == 0)
203 numTimeStep = MEDnPasdetemps(
209 if (numTimeStep < 0) throw IOException("", __FILE__, __LINE__);
213 res.push_back(make_pair(name, numTimeStep));
216 //---------------------------------------------------------------------
217 // Close the MED file
218 //---------------------------------------------------------------------
219 ret = MEDfermer(file);
220 if (ret != 0) throw multipr::IOException("i/o error while closing MED file", __FILE__, __LINE__);