Back to index

salome-med  6.5.0
MEDPARTITIONER_Utils.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "MEDPARTITIONER_Utils.hxx"
00021 
00022 #include "MEDLoader.hxx"
00023 #include "MEDLoaderBase.hxx"
00024 #include "MEDFileUtilities.hxx"
00025 #include "CellModel.hxx"
00026 #include "MEDCouplingUMesh.hxx"
00027 #include "MEDCouplingFieldDouble.hxx"
00028 #include "InterpKernelException.hxx"
00029 #include "MEDCouplingAutoRefCountObjectPtr.hxx"
00030 #include "InterpKernelAutoPtr.hxx"
00031 
00032 #include <fstream>
00033 #include <iostream>
00034 #include <iomanip>
00035 #include <sstream>
00036 #include <string>
00037 #include <cstring>
00038 
00039 using namespace MEDPARTITIONER;
00040 
00041 int MEDPARTITIONER::MyGlobals::_Verbose=0;
00042 int MEDPARTITIONER::MyGlobals::_Is0verbose=0;
00043 int MEDPARTITIONER::MyGlobals::_Rank=-1;
00044 int MEDPARTITIONER::MyGlobals::_World_Size=-1;
00045 int MEDPARTITIONER::MyGlobals::_Randomize=0;
00046 int MEDPARTITIONER::MyGlobals::_Atomize=0;
00047 int MEDPARTITIONER::MyGlobals::_Creates_Boundary_Faces=0;
00048 std::vector<std::string> MEDPARTITIONER::MyGlobals::_File_Names;
00049 std::vector<std::string> MEDPARTITIONER::MyGlobals::_Mesh_Names;
00050 std::vector<std::string> MEDPARTITIONER::MyGlobals::_Field_Descriptions;
00051 std::vector<std::string> MEDPARTITIONER::MyGlobals::_General_Informations;
00052 
00053 std::string MEDPARTITIONER::Trim(const std::string& s,const std::string& drop)
00054 {
00055   std::string r(s);
00056   r.erase(r.find_last_not_of(drop)+1);
00057   return r.erase(0,r.find_first_not_of(drop));
00058 }
00059 
00060 std::string MEDPARTITIONER::IntToStr(const int i)
00061 {
00062   std::ostringstream oss;
00063   oss << i;
00064   return oss.str();
00065 }
00066 
00067 std::string MEDPARTITIONER::DoubleToStr(const double i)
00068 {
00069   std::ostringstream oss;
00070   oss << i;
00071   return oss.str();
00072 }
00073 
00074 int MEDPARTITIONER::StrToInt(const std::string& s)
00075 {
00076   int res;
00077   std::istringstream iss(s);
00078   iss >> res;
00079   return res;
00080 }
00081 
00082 double MEDPARTITIONER::StrToDouble(const std::string& s)
00083 {
00084   double res;
00085   std::istringstream iss(s);
00086   iss >> res;
00087   return res;
00088 }
00089 
00090 bool MEDPARTITIONER::TestArg(const char *arg, const char *argExpected, std::string& argValue)
00091 {
00092   argValue="";
00093   std::size_t i;
00094   for (i=0; i<strlen(arg); i++)
00095     {
00096       if (arg[i]=='=')
00097         break;
00098       if (arg[i]!=argExpected[i])
00099         return false;
00100     }
00101   for (std::size_t j=i+1; j<strlen(arg); j++)
00102     argValue+=arg[j];
00103   return true;
00104 }
00105 
00106 std::vector<int> MEDPARTITIONER::CreateRandomSize(const int size)
00107 {
00108   std::vector<int> res(size);
00109   for (int i=0; i<size; i++)
00110     res[i]=i;
00111   //cvw TODO or not? srand( (unsigned)time( NULL ) );
00112   srand( MyGlobals::_Randomize );
00113   for (int i=0; i<size; i++)
00114     {
00115       int ii=rand()%size;
00116       int tmp=res[ii];
00117       res[ii]=res[i];
00118       res[i]=tmp;
00119     }
00120   return res;
00121 }
00122 
00126 void MEDPARTITIONER::RandomizeAdj(int* xadj, int* adjncy, std::vector<int>& ran, std::vector<int>& vx, std::vector<int>& va)
00127 {
00128   if (MyGlobals::_World_Size>1)
00129     {
00130       std::cerr << "MEDPARTITIONER::RandomizeAdj only works on one proc!" << std::endl;
00131       return;
00132     }
00133   int size=ran.size();
00134   std::vector<int> invran(size);
00135   for (int i=0; i<size; i++)
00136     invran[ran[i]]=i;
00137   vx.resize(size+1);
00138   int lga=xadj[size];
00139   va.resize(lga);
00140   int jj=0;
00141   vx[0]=0;
00142   for (int i=0; i<size; i++)
00143     {
00144       int ir=ran[i];
00145       int ii=xadj[ir];
00146       int lgj=xadj[ir+1]-ii;
00147       for (int j=0; j<lgj; j++)
00148         {
00149           va[jj]=invran[adjncy[ii]];
00150           jj=jj+1;
00151           ii=ii+1;
00152         }
00153       vx[i+1]=jj;
00154     }
00155 }
00156 
00157 void MEDPARTITIONER::TestRandomize()
00158 {
00159   //int xadj[6]={0,2,5,9,12,13}; //for first debug only
00160   //int adjncy[13]={1,4,0,2,4,1,3,4,2,4,4,3,4};
00161   int xadj[6]={0,2,5,9,12,13};
00162   int adjncy[13]={0,0,1,1,1,2,2,2,2,3,3,3,4};
00163   int size=5;
00164   std::vector<int> r=CreateRandomSize(size);
00165   std::vector<int> vx,va;
00166   RandomizeAdj(&xadj[0],&adjncy[0],r,vx,va);
00167 }
00168 
00169 std::string MEDPARTITIONER::ReprVectorOfString(const std::vector<std::string>& vec)
00170 {
00171   if (vec.size()==0)
00172     return std::string(" NONE\n");
00173   std::ostringstream oss;
00174   for (std::vector<std::string>::const_iterator i=vec.begin(); i!=vec.end(); ++i) 
00175     oss << " -> '" << *i << "'" << std::endl;
00176   return oss.str();
00177 }
00178 
00179 std::string MEDPARTITIONER::ReprVectorOfString(const std::vector<std::string>& vec, const std::string separator)
00180 {
00181   if (vec.size()==0)
00182     return std::string(" NONE\n");
00183   std::ostringstream oss;
00184   for (std::vector<std::string>::const_iterator i=vec.begin(); i!=vec.end(); ++i) 
00185     oss << separator << *i;
00186   return oss.str();
00187 }
00188 
00189 std::string MEDPARTITIONER::ReprMapOfStringInt(const std::map<std::string,int>& mymap)
00190 {
00191   if (mymap.size()==0)
00192     return std::string(" NONE\n");
00193   std::ostringstream oss;
00194   for (std::map<std::string,int>::const_iterator i=mymap.begin(); i!=mymap.end(); ++i) 
00195     oss << " -> [" << (*i).first << "]=" << (*i).second << std::endl;
00196   return oss.str();
00197 }
00198 
00199 std::string MEDPARTITIONER::ReprMapOfStringVectorOfString(const std::map< std::string,std::vector<std::string> >& mymap)
00200 {
00201   if (mymap.size()==0)
00202     return std::string(" NONE\n");
00203   std::ostringstream oss;
00204   for (std::map< std::string,std::vector<std::string> >::const_iterator i=mymap.begin(); i!=mymap.end(); ++i) 
00205     oss << " -> [" << (*i).first << "]=" << std::endl << ReprVectorOfString((*i).second) << std::endl;
00206   return oss.str();
00207 }
00208 
00209 std::string MEDPARTITIONER::ReprFieldDescriptions(const std::vector<std::string>& vec, const std::string separator)
00210 {
00211   if (vec.size()==0)
00212     return std::string(" NONE\n");
00213   std::ostringstream oss;
00214   for (std::vector<std::string>::const_iterator i=vec.begin(); i!=vec.end(); ++i)
00215     {
00216       oss << " ->"; 
00217       oss << ReprVectorOfString(DeserializeToVectorOfString(*i), separator) << std::endl;
00218     }
00219   return oss.str();
00220 }
00221 
00227 std::string MEDPARTITIONER::SerializeFromString(const std::string& s)
00228 {
00229   std::ostringstream oss;
00230   oss << std::setw(5) << s.size() << "/" << s << "/";
00231   return oss.str();
00232 }
00233 
00237 std::string MEDPARTITIONER::SerializeFromVectorOfString(const std::vector<std::string>& vec)
00238 {
00239   std::ostringstream oss;
00240   for (std::vector<std::string>::const_iterator i=vec.begin(); i!=vec.end(); ++i)
00241     oss<< std::setw(5) << (*i).size() << "/" << *i << "/";
00242   return oss.str();
00243 }
00244 
00248 std::vector<std::string> MEDPARTITIONER::DeserializeToVectorOfString(const std::string& str)
00249 {
00250   std::vector<std::string> res;
00251   std::size_t pos=0;
00252   std::size_t posmax=str.size();
00253   if (posmax==0)
00254     return res;  //empty vector
00255   std::size_t length;
00256   while (pos < posmax-6)  //setw(5)+" "
00257     {
00258       std::istringstream iss(str.substr(pos,5));
00259       iss >> length;
00260       if ((str[pos+5]!='/') || (str[pos+6+length]!='/'))
00261         {
00262           std::cerr << "Error on string '" << str << "'" << std::endl;;
00263           throw INTERP_KERNEL::Exception("Error on string");
00264         }
00265       res.push_back(str.substr(pos+6,length));
00266       pos=pos+6+length+1;
00267     }
00268   return res;
00269 }
00270 
00271 std::string MEDPARTITIONER::EraseTagSerialized(const std::string& fromStr, const std::string& tag)
00272 {
00273   std::vector<std::string> vec=DeserializeToVectorOfString(fromStr);
00274   std::vector<std::string> res;
00275   for (std::size_t i=0; i<vec.size(); i++)
00276     {
00277       if (vec[i].find(tag)==std::string::npos)
00278         res.push_back(vec[i]);
00279     }
00280   return MEDPARTITIONER::SerializeFromVectorOfString(res);
00281 }
00282 
00287 std::vector<std::string> MEDPARTITIONER::VectorizeFromMapOfStringInt(const std::map<std::string,int>& mymap)
00288 {
00289   std::vector<std::string> res;
00290   for (std::map<std::string,int>::const_iterator i=mymap.begin(); i!=mymap.end(); ++i)
00291     {
00292       std::ostringstream oss;
00293       oss << (*i).second << "/" << (*i).first;
00294       res.push_back(oss.str());
00295     }
00296   return res;
00297 }
00298 
00299 /*
00300  * if existing identicals (first,second) in vector no problem, else Exception
00301  */
00302 std::map<std::string,int> MEDPARTITIONER::DevectorizeToMapOfStringInt(const std::vector<std::string>& vec)
00303 {
00304   std::map<std::string,int> res;
00305   for (std::vector<std::string>::const_iterator i=vec.begin(); i!=vec.end(); ++i)
00306     {
00307       std::size_t pos=0;
00308       std::size_t posmax=(*i).size();
00309       std::size_t found=(*i).find('/'); //first slash
00310       if ((found==std::string::npos) || (found<1))
00311         throw INTERP_KERNEL::Exception("Error aIntNumber/anyString is expected");
00312       int second;
00313       std::istringstream iss((*i).substr(pos,found));
00314       iss >> second;
00315       std::string first=(*i).substr(pos+found+1,posmax-found);
00316       std::map<std::string,int>::iterator it=res.find(first);
00317       if (it!=res.end())
00318         if ((*it).second!=second)
00319           throw INTERP_KERNEL::Exception("Error not the same map value");
00320       res[first]=second;
00321     }
00322   return res;
00323 }
00324 
00330 std::vector<std::string> MEDPARTITIONER::VectorizeFromMapOfStringVectorOfString(const std::map< std::string,std::vector<std::string> >& mymap)
00331 {
00332   std::vector<std::string> res;
00333   for (std::map< std::string,std::vector<std::string> >::const_iterator i=mymap.begin(); i!=mymap.end(); ++i)
00334     {
00335       std::vector<std::string> vs=(*i).second;  //a vector of string;
00336       std::ostringstream oss;
00337       oss << "Keymap/" << (*i).first << "/" << (*i).second.size();
00338       vs.insert(vs.begin(), oss.str());
00339       res.push_back(SerializeFromVectorOfString(vs));
00340     }
00341   return res;
00342 }
00343 
00348 std::map< std::string,std::vector<std::string> > MEDPARTITIONER::DevectorizeToMapOfStringVectorOfString(const std::vector<std::string>& vec)
00349 {
00350   std::map< std::string,std::vector<std::string> > res;
00351   for (std::vector<std::string>::const_iterator i=vec.begin(); i!=vec.end(); ++i)
00352     {
00353       std::vector<std::string> vs=DeserializeToVectorOfString(*i);
00354     
00355       std::string enTete=vs[0];
00356       std::size_t posmax=enTete.size();
00357       std::size_t foundKey=enTete.find("Keymap/");
00358       std::size_t foundSizeVector=enTete.find_last_of('/');
00359       if ((foundKey==std::string::npos) || (foundKey!=0) || ((foundKey+7)>=foundSizeVector))
00360         throw INTERP_KERNEL::Exception("Error Keymap/anyString/aIntNumber is expected");
00361       int sizeVector;
00362       std::istringstream iss(enTete.substr(foundSizeVector+1,posmax-foundSizeVector));
00363       iss >> sizeVector;
00364       std::string keymap=enTete.substr(foundKey+7,foundSizeVector-foundKey-7);
00365       for (int ii=1; ii<=sizeVector; ii++)
00366         res[keymap].push_back(vs[ii]); //add unconditionnaly,so merge duplicates in second vector
00367     }
00368   return res;
00369 }
00370 
00375 std::vector<std::string> MEDPARTITIONER::SelectTagsInVectorOfString(const std::vector<std::string>& vec, const std::string tag)
00376 {
00377   std::vector<std::string> res;
00378   if (vec.size()==0)
00379     return res;
00380   for (std::vector<std::string>::const_iterator i=vec.begin(); i!=vec.end(); ++i)
00381     {
00382       if ((*i).find(tag)!=std::string::npos) res.push_back(*i);
00383     }
00384   return res;
00385 }
00386 
00390 std::vector<std::string> MEDPARTITIONER::DeleteDuplicatesInVectorOfString(const std::vector<std::string>& vec)
00391 {
00392   std::vector<std::string> res;
00393   if (vec.size()==0) return res;
00394   //shit for unique and unique_copy for the duplicate CONSECUTIVE elements
00395   //I do not want to sort
00396   for (std::vector<std::string>::const_iterator i=vec.begin(); i!=vec.end(); ++i)
00397     {
00398       bool found=false;
00399       for (std::vector<std::string>::const_iterator j=res.begin(); j!=res.end(); ++j)
00400         {
00401           if ((*i).compare(*j)==0)
00402             {
00403               found=true;
00404               break;
00405             }
00406         }
00407       if (!found) res.push_back(*i);
00408     }
00409   return res;
00410 }
00411 
00412 std::map< std::string,std::vector<std::string> > MEDPARTITIONER::DeleteDuplicatesInMapOfStringVectorOfString(const std::map< std::string,std::vector<std::string> >& mymap)
00413 {
00414   std::map< std::string,std::vector<std::string> > res;
00415   for (std::map< std::string,std::vector<std::string> >::const_iterator i=mymap.begin(); i!=mymap.end(); ++i)
00416     res[(*i).first]=DeleteDuplicatesInVectorOfString((*i).second);
00417   return res;
00418 }
00419 
00420 //void MEDPARTITIONER::sendRecvVectorOfString(const std::vector<string>& vec, const int source, const int target)
00421 //TODO
00422 std::string MEDPARTITIONER::Cle1ToStr(const std::string& s, const int inew)
00423 {
00424   std::ostringstream oss;
00425   oss << s << " " << inew;
00426   return oss.str();
00427 }
00428 
00429 void MEDPARTITIONER::Cle1ToData(const std::string& key, std::string& s, int& inew)
00430 {
00431   std::size_t posmax=key.size();
00432   std::size_t found=key.find(' ');
00433   if ((found==std::string::npos) || (found<1))
00434     throw INTERP_KERNEL::Exception("Error 'aStringWithoutWhitespace aInt' is expected");
00435   s=key.substr(0,found);
00436   std::istringstream iss(key.substr(found,posmax-found));
00437   iss >> inew;
00438 }
00439 
00440 std::string MEDPARTITIONER::Cle2ToStr(const std::string& s, const int inew, const int iold)
00441 {
00442   std::ostringstream oss;
00443   oss << s << " " << inew << " " << iold;
00444   return oss.str();
00445 }
00446 
00447 void MEDPARTITIONER::Cle2ToData(const std::string& key, std::string& s, int& inew, int& iold)
00448 {
00449   std::size_t posmax=key.size();
00450   std::size_t found=key.find(' ');
00451   if ((found==std::string::npos) || (found<1))
00452     throw INTERP_KERNEL::Exception("Error 'aStringWithoutWhitespace aInt aInt' is expected");
00453   s=key.substr(0,found);
00454   std::istringstream iss(key.substr(found,posmax-found));
00455   iss >> inew >> iold;
00456 }
00457 
00458 std::string MEDPARTITIONER::ExtractFromDescription(const std::string& description,const std::string& tag)
00459 {
00460   std::size_t found=description.find(tag);
00461   if ((found==std::string::npos) || (found<1))
00462     {
00463       std::cerr << "ERROR : not found '" << tag << "' in '"<< description << "'\n";
00464       throw INTERP_KERNEL::Exception("Error ExtractFromDescription");
00465     }
00466   std::size_t beg=found;
00467   std::size_t end=beg;
00468   if (description[found-1]!='/')
00469     {
00470       //find without '/'... and pray looking for first whitespace
00471       //something like 'idomain=0 fileName=tmp.med meshName=...'
00472       end=description.size();
00473       beg+=tag.length();
00474       std::string res=description.substr(beg,end-beg);
00475       found=res.find(' ');
00476       if (found==std::string::npos)
00477         found=res.length();
00478       res=res.substr(0,found);
00479       return res;
00480     }
00481   std::size_t lg=StrToInt(description.substr(found-6,found));
00482   beg+=tag.length();
00483   return description.substr(beg,lg-tag.length());
00484 }
00485 
00486 void MEDPARTITIONER::FieldDescriptionToData(const std::string& description, 
00487                                             int& idomain, std::string& fileName, std::string& meshName, std::string& fieldName, int& typeField, int& DT, int& IT)
00488 {
00489   idomain=StrToInt(ExtractFromDescription(description,"idomain="));
00490   fileName=ExtractFromDescription(description,"fileName=");
00491   meshName=ExtractFromDescription(description,"meshName=");
00492   fieldName=ExtractFromDescription(description,"fieldName=");
00493   typeField=StrToInt(ExtractFromDescription(description,"typeField="));
00494   DT=StrToInt(ExtractFromDescription(description,"DT="));
00495   IT=StrToInt(ExtractFromDescription(description,"IT="));
00496 }
00497 
00498 void MEDPARTITIONER::FieldShortDescriptionToData(const std::string& description, 
00499                                                  std::string& fieldName, int& typeField, int& entity, int& DT, int& IT)
00500 {
00501   fieldName=ExtractFromDescription(description,"fieldName=");
00502   typeField=StrToInt(ExtractFromDescription(description,"typeField="));
00503   entity=StrToInt(ExtractFromDescription(description,"entity="));
00504   DT=StrToInt(ExtractFromDescription(description,"DT="));
00505   IT=StrToInt(ExtractFromDescription(description,"IT="));
00506 }
00507 
00508 ParaMEDMEM::DataArrayInt *MEDPARTITIONER::CreateDataArrayIntFromVector(const std::vector<int>& v)
00509 {
00510   ParaMEDMEM::DataArrayInt* p=ParaMEDMEM::DataArrayInt::New();
00511   p->alloc(v.size(),1);
00512   std::copy(v.begin(),v.end(),p->getPointer());
00513   return p;
00514 }
00515 
00516 ParaMEDMEM::DataArrayInt *MEDPARTITIONER::CreateDataArrayIntFromVector(const std::vector<int>& v,const int nbComponents)
00517 {
00518   ParaMEDMEM::DataArrayInt* p=ParaMEDMEM::DataArrayInt::New();
00519   if (v.size()%nbComponents!=0)
00520     throw INTERP_KERNEL::Exception("Problem size modulo nbComponents != 0");
00521   p->alloc(v.size()/nbComponents,nbComponents);
00522   std::copy(v.begin(),v.end(),p->getPointer());
00523   return p;
00524 }
00525 
00526 ParaMEDMEM::DataArrayDouble* MEDPARTITIONER::CreateDataArrayDoubleFromVector(const std::vector<double>& v)
00527 {
00528   ParaMEDMEM::DataArrayDouble* p=ParaMEDMEM::DataArrayDouble::New();
00529   p->alloc(v.size(),1);
00530   std::copy(v.begin(),v.end(),p->getPointer());
00531   return p;
00532 }
00533 
00536 std::vector<std::string> MEDPARTITIONER::BrowseFieldDouble(const ParaMEDMEM::MEDCouplingFieldDouble* fd)
00537 {
00538   std::vector<std::string> res;
00539   if (fd->getArray())
00540     {
00541       int nb=fd->getArray()->getNumberOfComponents();
00542       res.push_back("nbComponents="); res.back()+=IntToStr(nb);
00543       for (int i=0; i<nb; i++)
00544         {
00545           res.push_back("componentInfo");
00546           res.back()+=IntToStr(i)+"="+fd->getArray()->getInfoOnComponent(i);
00547         }
00548     }
00549   else
00550     {
00551       res.push_back("nbComponents=0");  //unknown
00552     }
00553   return res;
00554 }
00555 
00559 std::vector<std::string> MEDPARTITIONER::BrowseAllFields(const std::string& myfile)
00560 {
00561   std::vector<std::string> res;
00562   std::vector<std::string> meshNames=MEDLoader::GetMeshNames(myfile.c_str());
00563   
00564   for (std::size_t i=0; i<meshNames.size(); i++)
00565     {
00566       std::vector<std::string> fieldNames=
00567         MEDLoader::GetAllFieldNamesOnMesh(myfile.c_str(),meshNames[i].c_str());
00568       for (std::size_t j = 0; j < fieldNames.size(); j++)
00569         {
00570           std::vector< ParaMEDMEM::TypeOfField > typeFields=
00571             MEDLoader::GetTypesOfField(myfile.c_str(), meshNames[i].c_str(), fieldNames[j].c_str());
00572           for (std::size_t k = 0; k < typeFields.size(); k++)
00573             {
00574               std::vector< std::pair< int, int > > its=
00575                 MEDLoader::GetFieldIterations(typeFields[k], myfile.c_str(), meshNames[i].c_str(), fieldNames[j].c_str());
00576               if (MyGlobals::_Is0verbose>100)
00577                 std::cout<< "fieldName " << fieldNames[j] << " typeField " << typeFields[k] << " its.size() " << its.size() << std::endl;
00578               for (std::size_t m = 0; m < its.size(); m++)
00579                 {
00580                   std::vector<std::string> resi;
00581                   resi.push_back("fileName="); resi.back()+=myfile;
00582                   resi.push_back("meshName="); resi.back()+=meshNames[i];
00583                   resi.push_back("fieldName="); resi.back()+=fieldNames[j];
00584                   resi.push_back("typeField="); resi.back()+=IntToStr((int)typeFields[k]);
00585                   resi.push_back("DT="); resi.back()+=IntToStr((int)its[m].first);
00586                   resi.push_back("IT="); resi.back()+=IntToStr((int)its[m].second);
00587                   res.push_back(SerializeFromVectorOfString(resi));
00588                 }
00589             }
00590         }
00591     }
00592   return res;
00593 }
00594 
00595 std::vector<std::string> MEDPARTITIONER::GetInfosOfField(const char *fileName, const char *meshName, const int idomain)
00596 {
00597   const int lggeom=10;
00598   const med_geometry_type GEOMTYPE[lggeom]={ //MED_N_CELL_FIXED_GEO] = { 
00599     //MED_POINT1,
00600     //MED_SEG2,
00601     //MED_SEG3,
00602     //MED_SEG4,
00603     //MED_TRIA3,
00604     //MED_QUAD4,
00605     //MED_TRIA6,
00606     //MED_TRIA7,
00607     //MED_QUAD8,
00608     //MED_QUAD9,
00609     MED_TETRA4,
00610     MED_PYRA5,
00611     MED_PENTA6,
00612     MED_HEXA8,
00613     MED_OCTA12,
00614     MED_TETRA10,
00615     MED_PYRA13,
00616     MED_PENTA15,
00617     MED_HEXA20,
00618     MED_HEXA27,
00619     //MED_POLYGON,
00620     //MED_POLYHEDRON 
00621   };
00622 
00623   const char * const GEOMTYPENAME[lggeom]={
00624     //"MED_POINT1",
00625     //"MED_SEG2",
00626     //"MED_SEG3",
00627     //"MED_SEG4",
00628     //"MED_TRIA3",
00629     //"MED_QUAD4",
00630     //"MED_TRIA6",
00631     //"MED_TRIA7",
00632     //"MED_QUAD8",
00633     //"MED_QUAD9",
00634     "MED_TETRA4",
00635     "MED_PYRA5",
00636     "MED_PENTA6",
00637     "MED_HEXA8",
00638     "MED_OCTA12",
00639     "MED_TETRA10",
00640     "MED_PYRA13",
00641     "MED_PENTA15",
00642     "MED_HEXA20",
00643     "MED_HEXA27",
00644     //"MED_POLYGONE",
00645     //"MED_POLYEDRE",
00646   };
00647 
00648 
00649   const int lgentity=3;
00650   const med_entity_type ENTITYTYPE[lgentity]={ //MED_N_ENTITY_TYPES+2]={
00651     //MED_UNDEF_ENTITY_TYPE,
00652     MED_CELL,
00653     //MED_DESCENDING_FACE,
00654     //MED_DESCENDING_EDGE,
00655     MED_NODE,
00656     MED_NODE_ELEMENT,
00657     //MED_STRUCT_ELEMENT,
00658     //MED_UNDEF_ENTITY_TYPE
00659   };
00660 
00661   const char * const ENTITYTYPENAME[lgentity]={ //MED_N_ENTITY_TYPES+2]={
00662     //"MED_UNDEF_ENTITY_TYPE",
00663     "MED_CELL",
00664     //"MED_FACE",
00665     //"MED_ARETE",
00666     "MED_NODE",
00667     "MED_NODE_ELEMENT",
00668     //"MED_STRUCT_ELEMENT",
00669     //"MED_UNDEF_ENTITY_TYPE"
00670   };
00671   
00672   std::vector<std::string> res;
00673   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
00674   med_int nbFields=MEDnField(fid);
00675   if (MyGlobals::_Verbose>20)
00676     std::cout << "on filename " << fileName << " nbOfField " << nbFields << std::endl;
00677   //
00678   med_field_type typcha;
00679   med_int numdt=0,numo=0;
00680   med_float dt=0.0;
00681   char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
00682   char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
00683   med_bool localmesh;
00684   //
00685   for(int i=1; i<=nbFields; i++)
00686     {
00687       med_int ncomp=MEDfieldnComponent(fid,i);
00688       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
00689       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
00690       INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
00691       med_int nbPdt;
00692       MEDfieldInfo(fid,i,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
00693       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
00694       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
00695       for (int k=1; k<=nbPdt; k++)
00696         {
00697           MEDfieldComputingStepInfo(fid,nomcha,k,&numdt,&numo,&dt);
00698           if (MyGlobals::_Verbose>20) 
00699             std::cout<< "on filename " << fileName << " field " << i << " fieldName " << curFieldName << " meshName " << curMeshName <<
00700               " typ " << typcha << " nbComponent " << ncomp << " nbPdt " << nbPdt << " noPdt " << k <<
00701               " ndt " << numdt << " nor " << numo << " dt " << dt << std::endl;
00702           for (int ie=0; ie<lgentity; ie++)
00703             {
00704               for (int j=0; j<lggeom; j++)
00705                 {
00706                   int profilesize=0,nbi=0;
00707                   med_entity_type enttype=ENTITYTYPE[ie];
00708                   //enttype=MED_NODE;enttype=MED_CELL;enttype=MED_NODE_ELEMENT;
00709                   char pflname[MED_NAME_SIZE+1]="";
00710                   char locname[MED_NAME_SIZE+1]="";
00711                   med_int nbofprofile=MEDfieldnProfile(fid,nomcha,numdt,numo,enttype,GEOMTYPE[j],pflname,locname);
00712                   int profileit=1;
00713                   if (enttype==MED_NODE)
00714                     {
00715                       med_geometry_type mygeomtype=MED_UNDEF_ENTITY_TYPE;
00716                       med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,enttype,mygeomtype,profileit,
00717                                                                 MED_COMPACT_PFLMODE,pflname,&profilesize,locname,&nbi);
00718                       if (nbOfVal>0)
00719                         {
00720                           if (MyGlobals::_Verbose>20)
00721                             std::cout << "on filename " << fileName << " entity " << enttype << " nbOfVal with " <<
00722                               nbofprofile << " profile(s) for geomType (AUCUN) nbOfVal " <<
00723                               nbOfVal << " profilName '" << pflname << "' profileSize " << profilesize << " nbPtGauss " << nbi << std::endl;
00724                           std::vector<std::string> resi;
00725                           resi.push_back("idomain="); resi.back()+=IntToStr(idomain);
00726                           resi.push_back("fileName="); resi.back()+=fileName;
00727                           resi.push_back("meshName="); resi.back()+=curMeshName;
00728                           resi.push_back("fieldName="); resi.back()+=curFieldName;
00729                           resi.push_back("typeField="); resi.back()+=IntToStr((int)ParaMEDMEM::ON_NODES);
00730                           resi.push_back("typeData="); resi.back()+=IntToStr((int)typcha);  //6 for double?
00731                           resi.push_back("nbComponent="); resi.back()+=IntToStr((int)ncomp);
00732                           resi.push_back("DT="); resi.back()+=IntToStr((int)numdt);
00733                           resi.push_back("IT="); resi.back()+=IntToStr((int)numo);
00734                           resi.push_back("time="); resi.back()+=DoubleToStr(dt);
00735                           resi.push_back("entity="); resi.back()+=IntToStr((int)enttype);
00736                           resi.push_back("entityName="); resi.back()+=ENTITYTYPENAME[ie];
00737                           resi.push_back("nbOfVal="); resi.back()+=IntToStr((int)nbOfVal);
00738                           resi.push_back("profilName="); resi.back()+=pflname;
00739                           resi.push_back("profileSize="); resi.back()+=IntToStr((int)profilesize);
00740                           resi.push_back("nbPtGauss="); resi.back()+=IntToStr((int)nbi);
00741                           res.push_back(SerializeFromVectorOfString(resi));
00742                         }
00743                       break; //on nodes no need to scute all geomtype
00744                     }
00745                   else
00746                     {
00747                       med_geometry_type mygeomtype=GEOMTYPE[j];
00748                       med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,enttype,mygeomtype,profileit,
00749                                                                 MED_COMPACT_PFLMODE,pflname,&profilesize,locname,&nbi);
00750                       if (nbOfVal>0)
00751                         {
00752                           if (MyGlobals::_Verbose>20)
00753                             std::cout << "on filename " << fileName << " entity " << enttype << " nbOfVal with " <<
00754                               nbofprofile << " profile(s) for geomType " <<
00755                               GEOMTYPE[j] << " " << GEOMTYPENAME[j] << " nbOfVal " <<
00756                               nbOfVal << " profilName '" << pflname << "' profileSize " << profilesize << " nbPtGauss " << nbi << std::endl;
00757                           int typeField=-1; //unknown
00758                           if (enttype==MED_CELL)
00759                             typeField=ParaMEDMEM::ON_CELLS;
00760                           if (enttype==MED_NODE_ELEMENT)
00761                             typeField=ParaMEDMEM::ON_GAUSS_NE;
00762                           //if (enttype==??) typeField=ON_GAUSS_PT;
00763                           std::vector<std::string> resi;
00764                           resi.push_back("idomain="); resi.back()+=IntToStr(idomain);
00765                           resi.push_back("fileName="); resi.back()+=fileName;
00766                           resi.push_back("meshName="); resi.back()+=curMeshName;
00767                           resi.push_back("fieldName="); resi.back()+=curFieldName;
00768                           resi.push_back("typeField="); resi.back()+=IntToStr((int)typeField);
00769                           resi.push_back("typeData="); resi.back()+=IntToStr((int)typcha);  //6 for double?
00770                           resi.push_back("nbComponent="); resi.back()+=IntToStr((int)ncomp);
00771                           resi.push_back("DT="); resi.back()+=IntToStr((int)numdt);
00772                           resi.push_back("IT="); resi.back()+=IntToStr((int)numo);
00773                           resi.push_back("time="); resi.back()+=DoubleToStr(dt);
00774                           resi.push_back("entity="); resi.back()+=IntToStr((int)enttype);
00775                           resi.push_back("entityName="); resi.back()+=ENTITYTYPENAME[ie];
00776                           resi.push_back("geomType="); resi.back()+=IntToStr((int)GEOMTYPE[j]);
00777                           resi.push_back("geomTypeName="); resi.back()+=GEOMTYPENAME[j];
00778                           resi.push_back("nbOfVal="); resi.back()+=IntToStr((int)nbOfVal);
00779                           resi.push_back("profilName="); resi.back()+=pflname;
00780                           resi.push_back("profileSize="); resi.back()+=IntToStr((int)profilesize);
00781                           resi.push_back("nbPtGauss="); resi.back()+=IntToStr((int)nbi);
00782                           if (typeField==(-1))
00783                             {
00784                               std::cout << "WARNING : unknown typeField for entity type " << enttype << std::endl <<
00785                                 SerializeFromVectorOfString(resi) << std::endl;
00786                               continue;  //do not register push_back
00787                             }
00788                           res.push_back(SerializeFromVectorOfString(resi));
00789                         }
00790                     }
00791                 }
00792             }
00793         }
00794     }
00795   delete [] maa_ass;
00796   delete [] nomcha;
00797   MEDfileClose(fid);
00798   if (MyGlobals::_Verbose>10)
00799     std::cout << "detected fields:\n" << ReprVectorOfString(res) << std::endl;
00800   return res;
00801 }
00802 
00806 std::vector<std::string> MEDPARTITIONER::BrowseAllFieldsOnMesh(const std::string& myfile, const std::string& mymesh, const int idomain)
00807 {
00808   std::vector<std::string> res=GetInfosOfField(myfile.c_str(),mymesh.c_str(),idomain);
00809   return res;
00810 }
00811 
00815 ParaMEDMEM::MEDCouplingUMesh* MEDPARTITIONER::CreateEmptyMEDCouplingUMesh()
00816 {
00817   ParaMEDMEM::MEDCouplingUMesh* umesh=ParaMEDMEM::MEDCouplingUMesh::New();
00818   umesh->setMeshDimension(3);
00819   umesh->allocateCells(0);
00820   umesh->finishInsertingCells();
00821   ParaMEDMEM::DataArrayDouble *myCoords=ParaMEDMEM::DataArrayDouble::New();
00822   myCoords->alloc(0,3);
00823   umesh->setCoords(myCoords);
00824   umesh->setName("EMPTY");
00825   myCoords->decrRef();
00826   umesh->checkCoherency();
00827   return umesh;
00828 }