X-Git-Url: http://git.salome-platform.org/gitweb/?p=modules%2Fsmesh.git;a=blobdiff_plain;f=src%2FSMESH%2FMG_ADAPT.cxx;h=27a57c894cc24edb5544fa21a0d1b1b1807641dc;hp=849a3f9660c0af007eb92f3b9b26adbe735efe65;hb=HEAD;hpb=33b6f8700337fa00f3e48076a6f6d8df87d8e57f diff --git a/src/SMESH/MG_ADAPT.cxx b/src/SMESH/MG_ADAPT.cxx index 849a3f966..b7af53ab0 100644 --- a/src/SMESH/MG_ADAPT.cxx +++ b/src/SMESH/MG_ADAPT.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2021 CEA/DEN, EDF R&D +// Copyright (C) 2020-2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -19,8 +19,11 @@ #include "MG_ADAPT.hxx" -#include "SMESH_File.hxx" -#include "SMESH_Comment.hxx" +#include +#include +#include +#include +#include #include #include @@ -30,6 +33,7 @@ #include #include +#include "SMESH_TypeDefs.hxx" #ifndef WIN32 #include // getpid() @@ -39,6 +43,7 @@ #include #include #include // unique_ptr +#include typedef SMESH_Comment ToComment; @@ -48,11 +53,12 @@ static std::string removeFile(std::string fileName, int& notOk) std::string errStr; notOk = std::remove(fileName.c_str()); if (notOk) errStr = ToComment("\n error while removing file : ") << fileName; - else errStr = ToComment("\n file : ") << fileName << " succesfully deleted! \n "; + else errStr = ToComment("\n file : ") << fileName << " succesfully deleted! \n "; return errStr; } -std::string MG_ADAPT::remove_extension(const std::string& filename) { +std::string MG_ADAPT::remove_extension(const std::string& filename) +{ size_t lastdot = filename.find_last_of("."); if (lastdot == std::string::npos) return filename; return filename.substr(0, lastdot); @@ -65,104 +71,103 @@ namespace return SMESH_File( fName ).exists(); } -// ======================================================================= -med_idt openMedFile(const std::string aFile) -// ======================================================================= -// renvoie le medId associe au fichier Med apres ouverture -{ - med_idt medIdt = MEDfileOpen(aFile.c_str(),MED_ACC_RDONLY); - if (medIdt <0) + // ======================================================================= + med_idt openMedFile(const std::string aFile) + // ======================================================================= + // renvoie le medId associe au fichier Med apres ouverture { - THROW_SALOME_EXCEPTION("\nThe med file " << aFile << " cannot be opened.\n"); + med_idt medIdt = MEDfileOpen(aFile.c_str(),MED_ACC_RDONLY); + if (medIdt <0) + { + THROW_SALOME_EXCEPTION("\nThe med file " << aFile << " cannot be opened.\n"); + } + return medIdt; } - return medIdt; -} - - -// ======================================================================= -void getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit, std::string fieldName) -// ======================================================================= -{ -// Il faut voir si plusieurs maillages - - herr_t erreur = 0 ; - med_idt medIdt ; - - // Ouverture du fichier - //~SCRUTE(aFile.toStdString()); - medIdt = openMedFile(aFile); - if ( medIdt < 0 ) return ; - // Lecture du nombre de champs - med_int ncha = MEDnField(medIdt) ; - if (ncha < 1 ) - { - //~addMessage( ToComment(" error: there is no field in ") << aFile, /*fatal=*/true ); - return; - } - // Lecture des caracteristiques du champs - - // Lecture du type du champ, des noms des composantes et du nom de l'unite - char nomcha [MED_NAME_SIZE+1]; - strcpy(nomcha, fieldName.c_str()); -// Lecture du nombre de composantes - med_int ncomp = MEDfieldnComponentByName(medIdt, nomcha); - char meshname[MED_NAME_SIZE+1]; - char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1); - char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1); - char dtunit[MED_SNAME_SIZE+1]; - med_bool local; - med_field_type typcha; - med_int nbofcstp; - erreur = MEDfieldInfoByName (medIdt, nomcha, meshname,&local,&typcha,comp,unit,dtunit, &nbofcstp); - free(comp); - free(unit); - if ( erreur < 0 ) + // ======================================================================= + void getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit, std::string fieldName) + // ======================================================================= { + // Il faut voir si plusieurs maillages + + herr_t erreur = 0 ; + med_idt medIdt ; + + // Ouverture du fichier + //~SCRUTE(aFile.toStdString()); + medIdt = openMedFile(aFile); + if ( medIdt < 0 ) return ; + // Lecture du nombre de champs + med_int ncha = MEDnField(medIdt) ; + if (ncha < 1 ) + { + //~addMessage( ToComment(" error: there is no field in ") << aFile, /*fatal=*/true ); + return; + } + // Lecture des caracteristiques du champs + + // Lecture du type du champ, des noms des composantes et du nom de l'unite + char nomcha [MED_NAME_SIZE+1]; + strcpy(nomcha, fieldName.c_str()); + // Lecture du nombre de composantes + med_int ncomp = MEDfieldnComponentByName(medIdt, nomcha); + char meshname[MED_NAME_SIZE+1]; + char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1); + char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1); + char dtunit[MED_SNAME_SIZE+1]; + med_bool local; + med_field_type typcha; + med_int nbofcstp; + erreur = MEDfieldInfoByName (medIdt, nomcha, meshname,&local,&typcha,comp,unit,dtunit, &nbofcstp); + free(comp); + free(unit); + if ( erreur < 0 ) + { //~addMessage( ToComment(" error: error while reading field ") << nomcha << " in file " << aFile , /*fatal=*/true ); - return; - } + return; + } - med_float dt; - med_int tmp_numdt, tmp_numit; + med_float dt; + med_int tmp_numdt, tmp_numit; - //~med_int step = data->myUseLastTimeStep ? nbofcstp : data->myTimeStep+1; - //~myPrint("step ", step); - erreur = MEDfieldComputingStepInfo ( medIdt, nomcha, 1, &numdt, &numit, &dt ); - for( int step = 1; step <= nbofcstp; step++ ) - { - erreur = MEDfieldComputingStepInfo ( medIdt, nomcha, step, &tmp_numdt, &tmp_numit, &dt ); - if(tmp_numdt > numdt) + //~med_int step = data->myUseLastTimeStep ? nbofcstp : data->myTimeStep+1; + //~myPrint("step ", step); + erreur = MEDfieldComputingStepInfo ( medIdt, nomcha, 1, &numdt, &numit, &dt ); + for( int step = 1; step <= nbofcstp; step++ ) + { + erreur = MEDfieldComputingStepInfo ( medIdt, nomcha, step, &tmp_numdt, &tmp_numit, &dt ); + if(tmp_numdt > numdt) + { + numdt = tmp_numdt; + numit = tmp_numit; + } + } + if ( erreur < 0 ) { - numdt = tmp_numdt; - numit = tmp_numit; + //~addMessage( ToComment(" error: error while reading field ") << nomcha << "step (numdt, numit) = " <<"("<< numdt<< ", " + //numit<< ")" <<" in file " << aFile , /*fatal=*/true ); + return; } - } - if ( erreur < 0 ) - { - //~addMessage( ToComment(" error: error while reading field ") << nomcha << "step (numdt, numit) = " <<"("<< numdt<< ", " - //numit<< ")" <<" in file " << aFile , /*fatal=*/true ); - return; - } - // Fermeture du fichier - if ( medIdt > 0 ) MEDfileClose(medIdt); + // Fermeture du fichier + if ( medIdt > 0 ) MEDfileClose(medIdt); -} - -struct GET_DEFAULT // struct used to get default value from GetOptionValue() -{ - bool isDefault; - operator bool* () { - return &isDefault; } -}; -class outFileStream : public std::ofstream{ -public: + struct GET_DEFAULT // struct used to get default value from GetOptionValue() + { + bool isDefault; + operator bool* () { + return &isDefault; + } + }; + + class outFileStream : public std::ofstream{ + public: ~outFileStream(){close();} //to close file at dtor -}; -} + }; + +} // anonymous namespace //---------------------------------------------------------------------------------------- MgAdapt::MgAdapt() @@ -319,7 +324,7 @@ MgAdaptHypothesisData* MgAdapt::getData() const } void MgAdapt::setMedFileIn(std::string fileName) { - if ( isFileExist(fileName) ) + if ( isFileExist( fileName )) { medFileIn = fileName; @@ -863,14 +868,15 @@ void MgAdapt::execCmd( const char* cmd, int& err) } std::ostream logStream(buf); - + #if defined(WIN32) -#if defined(UNICODE) +# if defined(UNICODE) const wchar_t * aCmd = Kernel_Utils::utf8_decode(cmd); + SMESHUtils::ArrayDeleter deleter( aCmd ); std::unique_ptr pipe(_wpopen(aCmd, O_RDONLY), _pclose ); -#else +# else std::unique_ptr pipe(_popen(cmd, "r"), _pclose ); -#endif +# endif #else std::unique_ptr pipe(popen(cmd, "r"), pclose ); #endif @@ -951,6 +957,7 @@ std::string MgAdapt::getCommandToRun() cmd+= " --in "+ meshIn; meshFormatOutputMesh = getFileName()+".mesh"; tmpFilesToBeDeleted.push_back(meshFormatOutputMesh); + cmd+= " --out "+ meshFormatOutputMesh; if (useLocalMap || useConstantValue) cmd+= " --sizemap "+ solFileIn; else // (useBackgroundMap) @@ -961,12 +968,12 @@ std::string MgAdapt::getCommandToRun() } //~else //~{ - //~// constant value TODO + //~// constant value TODO //~} // Check coherence between mesh dimension and option fo adaptation checkDimensionOptionAdaptation(); -// sizemap file is written only if level is higher than 3 + // sizemap file is written only if level is higher than 3 if ( verbosityLevel > 3) { std::string solFileOut = getFileName()+".sol"; @@ -1004,7 +1011,7 @@ std::string MgAdapt::getCommandToRun() cmd += " --"; else cmd += " "; -// std::cout << "--- option: '" << option << ", value: '" << value <<"'"<< std::endl; + // std::cout << "--- option: '" << option << ", value: '" << value <<"'"<< std::endl; cmd += option + " " + value; } } @@ -1014,13 +1021,36 @@ std::string MgAdapt::getCommandToRun() { cmd+= " --verbose "+ ToComment(verbosityLevel); } - //~} -//~cmd+= " >" + // get license key + { + smIdType nbVertex, nbEdge, nbFace, nbVol; + DriverGMF_Read gmfReader; + gmfReader.SetFile( meshIn ); + gmfReader.GetMeshInfo( nbVertex, nbEdge, nbFace, nbVol ); + + std::string errorTxt; + std::string key = SMESHUtils_MGLicenseKeyGen::GetKey( meshIn, + FromSmIdType( nbVertex ), + FromSmIdType( nbEdge ), + FromSmIdType( nbFace ), + FromSmIdType( nbVol ), + errorTxt ); + if ( key.empty() ) + return ToComment( "Problem with library SalomeMeshGemsKeyGenerator: " + errorTxt ); + + if ( key!="0" ) + cmd += " --key " + key; + } + #ifdef WIN32 - cmd += " < NUL"; + cmd += " < NUL"; #endif -// std::cout << "--- cmd :"<< std::endl; -// std::cout << cmd << std::endl; + + if (SALOME::VerbosityActivated()) + { + std::cout << "--- cmd :"<< std::endl; + std::cout << cmd << std::endl; + } return cmd; } @@ -1072,7 +1102,9 @@ std::string MgAdapt::defaultWorkingDirectory() { aTmpDir = Tmp_dir; } - else { + + if ( ! isFileExist( aTmpDir )) + { #ifdef WIN32 aTmpDir = "C:\\"; #else @@ -1105,7 +1137,7 @@ std::string MgAdapt::getFileName() const aGenericName << _getpid(); #endif aGenericName << "_"; - aGenericName << std::abs((int)(long) aGenericName.data()); + aGenericName << std::chrono::system_clock::now().time_since_epoch().count(); return aGenericName; } @@ -1357,7 +1389,7 @@ void MgAdapt::convertMedFile(std::string& meshFormatMeshFileName, std::string& s MEDCoupling::MEDFileMeshes* meshes = mfd->getMeshes(); MEDCoupling::MEDFileMesh* fileMesh = meshes->getMeshAtPos(0); // ok only one mesh in file! if (meshNameOut =="") - meshNameOut = fileMesh->getName(); + meshNameOut = fileMesh->getName(); storeGroupsAndFams(fileMesh); MEDCoupling::MCAuto fields = MEDCoupling::MEDFileFields::New(); @@ -1379,8 +1411,17 @@ void MgAdapt::convertMedFile(std::string& meshFormatMeshFileName, std::string& s checkTimeStepRank(medFileIn) ; MEDCoupling::MCAuto fts( mfd->getFields()->getFieldWithName(fieldName) ); MEDCoupling::MCAuto f = fts->getTimeStep(timeStep, rank); - MEDCoupling::MCAuto tmFts = MEDCoupling::MEDFileFieldMultiTS::New(); - tmFts->pushBackTimeStep(f); + MEDCoupling::MCAuto tmFts = MEDCoupling::DynamicCast(fts); + + // if not able to cast to double field, try float field + if (!tmFts) + { + MEDCoupling::MCAuto tmFtsFloat = MEDCoupling::DynamicCast(fts); + if (!tmFtsFloat) + THROW_SALOME_EXCEPTION("\nUnexpected field type.\n"); + // convert float field to double + tmFts = tmFtsFloat->convertToDouble(); + } fields->pushField(tmFts); @@ -1533,18 +1574,18 @@ void MgAdapt::buildBackGroundMeshAndSolFiles(const std::vector& fie } MgAdapt::Status MgAdapt::addMessage(const std::string& msg, - const bool isFatal/*=false*/) + const bool isFatal/*=false*/) { if ( isFatal ) - _myErrorMessages.clear(); // warnings are useless if a fatal error encounters + _errorMessages.clear(); // warnings are useless if a fatal error encounters - _myErrorMessages.push_back( msg ); + _errorMessages.push_back( msg ); -//~MESSAGE(msg); -#ifdef _DEBUG_ - std::cout << msg << std::endl; -#endif - return ( _myStatus = isFatal ? MgAdapt::DRS_FAIL : MgAdapt::DRS_WARN_SKIP_ELEM ); + //~MESSAGE(msg); + if (SALOME::VerbosityActivated()) + std::cout << msg << std::endl; + + return ( _status = isFatal ? MgAdapt::DRS_FAIL : MgAdapt::DRS_WARN_SKIP_ELEM ); } void MgAdapt::updateTimeStepRank()