1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File : MG_ADAPT.hxx
32 #include "MEDFileMesh.hxx"
38 std::string remove_extension(const std::string& filename);
42 typedef std::map< std::string, std::string > TOptionValues;
43 typedef std::set< std::string > TOptionNames;
45 struct MgAdaptHypothesisData
47 std::string myFileInDir, myMeshFileIn, myInMeshName, myMeshFileBackground, myOutMeshName,
48 myMeshFileOut, myFileOutDir, myFileSizeMapDir, myFieldName;
50 bool myPublish, myMeshOutMed;
51 bool myUseLocalMap, myUseBackgroundMap, myUseConstantValue;
52 double myConstantValue;
53 int myRank, myTimeStep;
54 bool myUseNoTimeStep, myUseLastTimeStep, myUseChosenTimeStep;
55 std::string myWorkingDir, myLogFile;
56 bool myPrintLogInFile, myKeepFiles, myRemoveLogOnSuccess;
60 class outFileStream : public std::ofstream{
62 ~outFileStream(){close();} //to close file at dtor
66 * \brief Class to generate string from any type
68 class ToComment : public std::string
70 std::ostringstream _s ;
74 ToComment():std::string("") {}
76 ToComment(const ToComment& c):std::string() {
78 this->std::string::operator=( _s.str() );
81 ToComment & operator=(const ToComment& c) {
83 this->std::string::operator=( _s.str() );
88 ToComment( const T &anything ) {
90 this->std::string::operator=( _s.str() );
94 ToComment & operator<<( const T &anything ) {
96 this->std::string::operator=( _s.str() );
100 operator char*() const {
101 return (char*)c_str();
104 std::ostream& Stream() {
116 MgAdapt(MgAdaptHypothesisData*);
117 MgAdapt(const MgAdapt&);
120 void setData( MgAdaptHypothesisData* data);
122 void setMedFileIn(std::string fileName);
123 std::string getMedFileIn();
125 void setMedFileOut(std::string fileOut);
126 std::string getMedFileOut();
128 void setMeshName(std::string name);
129 std::string getMeshName();
131 void setMeshNameOut(std::string name);
132 std::string getMeshNameOut();
134 void setMeshOutMed(bool mybool);
135 bool getMeshOutMed();
137 void setPublish(bool mybool);
140 void setFieldName(std::string myFieldName);
141 std::string getFieldName();
143 void setTimeStep(int time);
144 int getTimeStep() const;
146 void setRankTimeStep(int time, int myRank);
149 void setTimeStepRankLast();
150 void setNoTimeStep();
151 void setChosenTimeStepRank();
152 void updateTimeStepRank();
154 void setLogFile(std::string);
155 std::string getLogFile();
157 void setVerbosityLevel(int verbosity);
158 int getVerbosityLevel();
160 void setRemoveOnSuccess(bool mybool);
161 bool getRemoveOnSuccess();
163 MgAdaptHypothesisData* getData() const;
165 void setUseLocalMap(bool mybool);
166 bool getUseLocalMap();
168 void setUseBackgroundMap(bool mybool);
169 bool getUseBackgroundMap();
171 void setUseConstantValue(bool mybool);
172 bool getUseConstantValue();
174 void setConstantValue(double cnst);
175 double getConstantValue() const;
177 void setSizeMapFile(std::string mapFile);
178 std::string getSizeMapFile();
180 void setFromMedFile(bool mybool);
181 bool isFromMedFile();
183 void setKeepWorkingFiles(bool mybool);
184 bool getKeepWorkingFiles();
186 void setPrintLogInFile(bool mybool);
187 bool getPrintLogInFile();
189 void setWorkingDir(std::string dir);
190 std::string getWorkingDir() const;
194 static std::string getCommandToRun(MgAdapt* );
195 std::string getCommandToRun() ;
196 int compute(std::string& errStr);
197 std::string getFileName() const;
198 static std::string getExeName();
199 void copyMgAdaptHypothesisData( const MgAdaptHypothesisData* from) ;
201 void checkDirPath(std::string& dirPath);
203 bool hasOptionDefined( const std::string& optionName ) const;
204 void setOptionValue(const std::string& optionName,
205 const std::string& optionValue) throw (std::invalid_argument);
206 std::string getOptionValue(const std::string& optionName,
207 bool* isDefault=0) const throw (std::invalid_argument);
208 std::vector <std::string> getCustomOptionValuesStrVec() const;
209 std::vector <std::string> getOptionValuesStrVec() const;
212 TOptionValues getOptionValues() const;
213 const TOptionValues& getCustomOptionValues() const ;
214 static double toDbl(const std::string&, bool* isOk = 0) throw (std::invalid_argument);
215 static bool toBool(const std::string&, bool* isOk = 0) throw (std::invalid_argument);
216 static int toInt(const std::string&, bool* isOk = 0 ) throw (std::invalid_argument);
217 static std::string toLowerStr(const std::string& str);
220 static std::string defaultWorkingDirectory();
221 static std::string defaultLogFile();
222 static bool defaultKeepFiles();
223 static bool defaultRemoveLogOnSuccess();
224 static int defaultVerboseLevel();
225 static bool defaultPrintLogInFile();
226 static bool defaultFromMedFile();
227 static bool defaultMeshOutMed();
228 static bool defaultPublish();
229 static bool defaultUseLocalMap();
230 static bool defaultUseBackgroundMap();
231 static bool defaultUseConstantValue();
232 static bool defaultUseNoTimeStep();
233 static bool defaultUseLastTimeStep();
234 static bool defaultUseChosenTimeStep();
235 static double defaultMaximumMemory();
236 static bool isFileExist(const std::string& fName);
240 DRS_EMPTY, // a file contains no mesh with the given name
241 DRS_WARN_RENUMBER, // a file has overlapped ranges of element numbers,
242 // so the numbers from the file are ignored
243 DRS_WARN_SKIP_ELEM, // some elements were skipped due to incorrect file data
244 DRS_WARN_DESCENDING, // some elements were skipped due to descending connectivity
245 DRS_FAIL, // general failure (exception etc.)
246 DRS_NO_TIME_STEP // general failure (exception etc.)
252 std::vector<MEDCoupling::mcIdType> _famListId;
253 std::vector<std::string> _famNames;
254 group(std::string name, std::vector<MEDCoupling::mcIdType> famListId, std::vector<std::string> famNames):_name(name)
256 std::vector<MEDCoupling::mcIdType>::iterator it = famListId.begin();
257 for (; it!=famListId.end(); ++it)
258 _famListId.push_back(*it);
260 std::vector<std::string>::iterator itt = famNames.begin();
261 for (; itt!=famNames.end(); ++itt)
262 _famNames.push_back(*itt);
267 std::string _famName;
269 family(std::string famName, MEDCoupling::mcIdType famId):_famName(famName), _famId(famId) {}
275 std::string medFileIn;
276 std::string medFileOut;
277 std::string meshName;
278 std::string meshNameOut;
279 bool publish, meshOutMed;
280 bool useLocalMap, useBackgroundMap, useConstantValue;
281 bool myUseLastTimeStep, myUseNoTimeStep, myUseChosenTimeStep;
282 std::string sizeMapFile;
283 std::string fieldName;
284 double constantValue;
287 /* advanced options */
291 std::string workingDir;
293 bool removeOnSuccess;
294 bool toKeepWorkingFiles;
298 MgAdaptHypothesisData* data;
302 TOptionValues _option2value, _customOption2value; // user defined values
303 TOptionValues _defaultOptionValues; // default values
304 TOptionNames _doubleOptions, _charOptions, _boolOptions; // to find a type of option
306 std::vector <std::string> _myErrorMessages;
308 std::string meshFormatOutputMesh;
309 std::vector< std::string> solFormatOutput;
310 std::vector <group> groupVec;
311 std::vector <family> famVec;
312 std::vector< std::string> tmpFilesToBeDeleted;
314 /* convert MED-->.mesh format */
315 void convertMedFile(std::string& meshIn,std::string& solFileIn, std::string& sizeMapIn) ;
316 void storeGroups(MEDCoupling::MEDFileMesh* fileMesh);
317 void restoreGroups(MEDCoupling::MEDFileMesh* fileMesh) const;
318 void storefams(MEDCoupling::MEDFileMesh* fileMesh);
319 void restorefams(MEDCoupling::MEDFileMesh* fileMesh) const;
320 void storeGroupsAndFams(MEDCoupling::MEDFileMesh* fileMesh);
321 void restoreGroupsAndFams(MEDCoupling::MEDFileMesh* fileMesh) const;
322 void convertMeshFile(std::string& meshFormatIn, std::vector< std::string>& solFieldFileNames) const ;
323 void buildConstantSizeMapSolFile(const std::string& solFormatFieldFileName, const int dim, const int version, const mcIdType nbNodes) const;
324 void buildBackGroundMeshAndSolFiles(const std::vector<std::string>& fieldFileNames, const std::string& meshFormatsizeMapFile) const;
325 void getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit);
326 Status addMessage(const std::string& msg, const bool isFatal = false);
327 med_idt openMedFile(const std::string aFile) ;
328 void execCmd( const char* cmd, int& err);
330 void appendMsgToLogFile(std::string& msg);
331 std::vector<std::string> getListFieldsNames(std::string fileIn) ;
332 void checkDimensionOptionAdaptation() ;
333 void checkFieldName(std::string fileIn) ;
334 void checkTimeStepRank(std::string fileIn) ;
340 #endif // MG_ADAPT_HXX