1 // Copyright (C) 2011-2020 CEA/DEN, EDF R&D
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/
19 // File : MG_ADAPT.hxx
32 #include "MEDFileMesh.hxx"
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;
61 class outFileStream : public std::ofstream{
63 ~outFileStream(){close();} //to close file at dtor
67 * \brief Class to generate string from any type
69 class ToComment : public std::string
71 std::ostringstream _s ;
75 ToComment():std::string("") {}
77 ToComment(const ToComment& c):std::string() {
79 this->std::string::operator=( _s.str() );
82 ToComment & operator=(const ToComment& c) {
84 this->std::string::operator=( _s.str() );
89 ToComment( const T &anything ) {
91 this->std::string::operator=( _s.str() );
95 ToComment & operator<<( const T &anything ) {
97 this->std::string::operator=( _s.str() );
101 operator char*() const {
102 return (char*)c_str();
105 std::ostream& Stream() {
117 MgAdapt(MgAdaptHypothesisData*);
118 MgAdapt(const MgAdapt&);
121 void setData( MgAdaptHypothesisData*);
123 void setMedFileIn(std::string fileName);
124 std::string getMedFileIn();
126 void setMedFileOut(std::string fileOut);
127 std::string getMedFileOut();
129 void setMeshName(std::string name);
130 std::string getMeshName();
132 void setMeshNameOut(std::string name);
133 std::string getMeshNameOut();
135 void setMeshOutMed(bool mybool);
136 bool getMeshOutMed();
138 void setPublish(bool mybool);
141 void setFieldName(std::string myFieldName);
142 std::string getFieldName();
144 void setTimeStep(int time);
145 int getTimeStep() const;
147 void setRankTimeStep(int time, int myRank);
150 void setLogFile(std::string);
151 std::string getLogFile();
153 void setVerbosityLevel(int verbosity);
154 int getVerbosityLevel();
156 void setRemoveOnSuccess(bool mybool);
157 bool getRemoveOnSuccess();
159 MgAdaptHypothesisData* getData() const;
161 void setUseLocalMap(bool mybool);
162 bool getUseLocalMap();
164 void setUseBackgroundMap(bool);
165 bool getUseBackgroundMap();
167 void setUseConstantValue(bool);
168 bool getUseConstantValue();
170 void setConstantValue(double);
171 double getConstantValue() const;
173 void setSizeMapFile(std::string);
174 std::string getSizeMapFile();
176 void setFromMedFile(bool);
177 bool isFromMedFile();
179 void setKeepWorkingFiles(bool);
180 bool getKeepWorkingFiles();
182 void setPrintLogInFile(bool);
183 bool getPrintLogInFile();
185 void setWorkingDir(std::string);
186 std::string getWorkingDir() const;
190 static std::string getCommandToRun(MgAdapt* );
191 std::string getCommandToRun() ;
192 int compute(std::string& errStr);
193 std::string getFileName() const;
194 static std::string getExeName();
195 void copyMgAdaptHypothesisData( MgAdaptHypothesisData* ) ;
197 void checkDirPath(std::string& );
201 bool hasOptionDefined( const std::string& optionName ) const;
202 void setOptionValue(const std::string& optionName,
203 const std::string& optionValue) throw (std::invalid_argument);
204 std::string getOptionValue(const std::string& optionName,
205 bool* isDefault=0) const throw (std::invalid_argument);
206 std::vector <std::string> getCustomOptionValuesStrVec() const;
207 std::vector <std::string> getOptionValuesStrVec() const;
210 TOptionValues getOptionValues() const;
211 const TOptionValues& getCustomOptionValues() const ;
212 static double toDbl(const std::string&, bool* isOk = 0) throw (std::invalid_argument);
213 static bool toBool(const std::string&, bool* isOk = 0) throw (std::invalid_argument);
214 static int toInt(const std::string&, bool* isOk = 0 ) throw (std::invalid_argument);
215 static std::string toLowerStr(const std::string& str);
219 static std::string defaultWorkingDirectory();
220 static std::string defaultLogFile();
221 static bool defaultKeepFiles();
222 static bool defaultRemoveLogOnSuccess();
223 static int defaultVerboseLevel();
224 static bool defaultPrintLogInFile();
225 static bool defaultFromMedFile();
226 static bool defaultMeshOutMed();
227 static bool defaultPublish();
228 static bool defaultUseLocalMap();
229 static bool defaultUseBackgroundMap();
230 static bool defaultUseConstantValue();
231 static bool defaultUseNoTimeStep();
232 static bool defaultUseLastTimeStep();
233 static bool defaultUseChosenTimeStep();
234 static double defaultMaximumMemory();
241 DRS_EMPTY, // a file contains no mesh with the given name
242 DRS_WARN_RENUMBER, // a file has overlapped ranges of element numbers,
243 // so the numbers from the file are ignored
244 DRS_WARN_SKIP_ELEM, // some elements were skipped due to incorrect file data
245 DRS_WARN_DESCENDING, // some elements were skipped due to descending connectivity
246 DRS_FAIL, // general failure (exception etc.)
247 DRS_NO_TIME_STEP // general failure (exception etc.)
253 std::vector<MEDCoupling::mcIdType> _famListId;
254 std::vector<std::string> _famNames;
255 group(std::string name, std::vector<MEDCoupling::mcIdType> famListId, std::vector<std::string> famNames):_name(name)
257 std::vector<MEDCoupling::mcIdType>::iterator it = famListId.begin();
258 for (; it!=famListId.end(); ++it)
259 _famListId.push_back(*it);
261 std::vector<std::string>::iterator itt = famNames.begin();
262 for (; itt!=famNames.end(); ++itt)
263 _famNames.push_back(*itt);
268 std::string _famName;
270 family(std::string famName, MEDCoupling::mcIdType famId):_famName(famName), _famId(famId) {}
277 std::string medFileIn;
278 std::string medFileOut;
279 std::string meshName;
280 std::string meshNameOut;
281 bool publish, meshOutMed;
282 bool useLocalMap, useBackgroundMap, useConstantValue;
283 std::string sizeMapFile;
284 std::string fieldName;
285 double constantValue;
288 /* advanced options */
292 std::string workingDir;
294 bool removeOnSuccess;
295 bool toKeepWorkingFiles;
299 MgAdaptHypothesisData* data;
303 TOptionValues _option2value, _customOption2value; // user defined values
304 TOptionValues _defaultOptionValues; // default values
305 TOptionNames _doubleOptions, _charOptions, _boolOptions; // to find a type of option
307 std::vector <std::string> _myErrorMessages;
309 std::string meshFormatOutputMesh;
310 std::vector< std::string> solFormatOutput;
311 std::vector <group> groupVec;
312 std::vector <family> famVec;
313 std::vector< std::string> tmpFilesToBeDeleted;
315 /* convert MED-->.mesh format */
316 void convertMedFile(std::string& meshIn,std::string& solFileIn, std::string& sizeMapIn) ;
317 void storeGroups(MEDCoupling::MEDFileMesh* fileMesh);
318 void restoreGroups(MEDCoupling::MEDFileMesh* fileMesh) const;
319 void storefams(MEDCoupling::MEDFileMesh* fileMesh);
320 void restorefams(MEDCoupling::MEDFileMesh* fileMesh) const;
321 void storeGroupsAndFams(MEDCoupling::MEDFileMesh* fileMesh);
322 void restoreGroupsAndFams(MEDCoupling::MEDFileMesh* fileMesh) const;
323 void convertMeshFile(std::string& meshFormatIn, std::vector< std::string>& solFieldFileNames) const ;
324 void buildConstantSizeMapSolFile(const std::string& solFormatFieldFileName, const int dim, const int version, const mcIdType nbNodes) const;
325 void buildBackGroundMeshAndSolFiles(const std::vector<std::string>& fieldFileNames, const std::string& meshFormatsizeMapFile) const;
326 void getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit);
327 Status addMessage(const std::string& msg, const bool isFatal = false);
328 med_idt openMedFile(const std::string aFile) ;
329 bool isFileExist(std::string& fName) const;
330 void execCmd( const char* cmd, int& err);
332 void appendMsgToLogFile(std::string& msg);
337 #endif // MG_ADAPT_HXX