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* data);
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 setTimeStepRankLast();
151 void setNoTimeStep();
153 void setLogFile(std::string);
154 std::string getLogFile();
156 void setVerbosityLevel(int verbosity);
157 int getVerbosityLevel();
159 void setRemoveOnSuccess(bool mybool);
160 bool getRemoveOnSuccess();
162 MgAdaptHypothesisData* getData() const;
164 void setUseLocalMap(bool mybool);
165 bool getUseLocalMap();
167 void setUseBackgroundMap(bool mybool);
168 bool getUseBackgroundMap();
170 void setUseConstantValue(bool mybool);
171 bool getUseConstantValue();
173 void setConstantValue(double cnst);
174 double getConstantValue() const;
176 void setSizeMapFile(std::string mapFile);
177 std::string getSizeMapFile();
179 void setFromMedFile(bool mybool);
180 bool isFromMedFile();
182 void setKeepWorkingFiles(bool mybool);
183 bool getKeepWorkingFiles();
185 void setPrintLogInFile(bool mybool);
186 bool getPrintLogInFile();
188 void setWorkingDir(std::string dir);
189 std::string getWorkingDir() const;
193 static std::string getCommandToRun(MgAdapt* );
194 std::string getCommandToRun() ;
195 int compute(std::string& errStr);
196 std::string getFileName() const;
197 static std::string getExeName();
198 void copyMgAdaptHypothesisData( const MgAdaptHypothesisData* from) ;
200 void checkDirPath(std::string& dirPath);
202 bool hasOptionDefined( const std::string& optionName ) const;
203 void setOptionValue(const std::string& optionName,
204 const std::string& optionValue) throw (std::invalid_argument);
205 std::string getOptionValue(const std::string& optionName,
206 bool* isDefault=0) const throw (std::invalid_argument);
207 std::vector <std::string> getCustomOptionValuesStrVec() const;
208 std::vector <std::string> getOptionValuesStrVec() const;
211 TOptionValues getOptionValues() const;
212 const TOptionValues& getCustomOptionValues() const ;
213 static double toDbl(const std::string&, bool* isOk = 0) throw (std::invalid_argument);
214 static bool toBool(const std::string&, bool* isOk = 0) throw (std::invalid_argument);
215 static int toInt(const std::string&, bool* isOk = 0 ) throw (std::invalid_argument);
216 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();
242 DRS_EMPTY, // a file contains no mesh with the given name
243 DRS_WARN_RENUMBER, // a file has overlapped ranges of element numbers,
244 // so the numbers from the file are ignored
245 DRS_WARN_SKIP_ELEM, // some elements were skipped due to incorrect file data
246 DRS_WARN_DESCENDING, // some elements were skipped due to descending connectivity
247 DRS_FAIL, // general failure (exception etc.)
248 DRS_NO_TIME_STEP // general failure (exception etc.)
254 std::vector<MEDCoupling::mcIdType> _famListId;
255 std::vector<std::string> _famNames;
256 group(std::string name, std::vector<MEDCoupling::mcIdType> famListId, std::vector<std::string> famNames):_name(name)
258 std::vector<MEDCoupling::mcIdType>::iterator it = famListId.begin();
259 for (; it!=famListId.end(); ++it)
260 _famListId.push_back(*it);
262 std::vector<std::string>::iterator itt = famNames.begin();
263 for (; itt!=famNames.end(); ++itt)
264 _famNames.push_back(*itt);
269 std::string _famName;
271 family(std::string famName, MEDCoupling::mcIdType famId):_famName(famName), _famId(famId) {}
278 std::string medFileIn;
279 std::string medFileOut;
280 std::string meshName;
281 std::string meshNameOut;
282 bool publish, meshOutMed;
283 bool useLocalMap, useBackgroundMap, useConstantValue;
284 std::string sizeMapFile;
285 std::string fieldName;
286 double constantValue;
289 /* advanced options */
293 std::string workingDir;
295 bool removeOnSuccess;
296 bool toKeepWorkingFiles;
300 MgAdaptHypothesisData* data;
304 TOptionValues _option2value, _customOption2value; // user defined values
305 TOptionValues _defaultOptionValues; // default values
306 TOptionNames _doubleOptions, _charOptions, _boolOptions; // to find a type of option
308 std::vector <std::string> _myErrorMessages;
310 std::string meshFormatOutputMesh;
311 std::vector< std::string> solFormatOutput;
312 std::vector <group> groupVec;
313 std::vector <family> famVec;
314 std::vector< std::string> tmpFilesToBeDeleted;
316 /* convert MED-->.mesh format */
317 void convertMedFile(std::string& meshIn,std::string& solFileIn, std::string& sizeMapIn) ;
318 void storeGroups(MEDCoupling::MEDFileMesh* fileMesh);
319 void restoreGroups(MEDCoupling::MEDFileMesh* fileMesh) const;
320 void storefams(MEDCoupling::MEDFileMesh* fileMesh);
321 void restorefams(MEDCoupling::MEDFileMesh* fileMesh) const;
322 void storeGroupsAndFams(MEDCoupling::MEDFileMesh* fileMesh);
323 void restoreGroupsAndFams(MEDCoupling::MEDFileMesh* fileMesh) const;
324 void convertMeshFile(std::string& meshFormatIn, std::vector< std::string>& solFieldFileNames) const ;
325 void buildConstantSizeMapSolFile(const std::string& solFormatFieldFileName, const int dim, const int version, const mcIdType nbNodes) const;
326 void buildBackGroundMeshAndSolFiles(const std::vector<std::string>& fieldFileNames, const std::string& meshFormatsizeMapFile) const;
327 void getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit);
328 Status addMessage(const std::string& msg, const bool isFatal = false);
329 med_idt openMedFile(const std::string aFile) ;
330 bool isFileExist(std::string& fName) const;
331 void execCmd( const char* cmd, int& err);
333 void appendMsgToLogFile(std::string& msg);
338 #endif // MG_ADAPT_HXX