Salome HOME
to remove tmp file
[modules/smesh.git] / src / SMESHGUI / MG_ADAPT.hxx
1 // Copyright (C) 2011-2020  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //
21 // File   : MG_ADAPT.hxx
22 //
23 #ifndef MG_ADAPT_HXX
24 #define MG_ADAPT_HXX
25 #include <string>
26 # include <sstream>
27 #include <map>
28 #include <vector>
29 #include <set>
30 #include <fstream>
31
32 #include "MCAuto.hxx"
33 #include "MCType.hxx"
34 #include "MEDFileMesh.hxx"
35
36 #include <med.h>
37 // SMESH includes
38
39 //~#include <med.h>
40
41
42 class MgAdapt;
43
44 typedef std::map< std::string, std::string > TOptionValues;
45 typedef std::set< std::string >              TOptionNames;
46
47 struct MgAdaptHypothesisData
48 {
49     std::string myFileInDir, myMeshFileIn, myInMeshName, myMeshFileBackground, myOutMeshName,
50         myMeshFileOut,  myFileOutDir, myFileSizeMapDir, myFieldName;
51     bool    fromMedFile;
52     bool    myPublish, myMeshOutMed;
53     bool    myUseLocalMap, myUseBackgroundMap, myUseConstantValue;
54     double  myConstantValue;
55     int     myRank, myTimeStep;
56     bool    myUseNoTimeStep, myUseLastTimeStep, myUseChosenTimeStep;
57     std::string myWorkingDir, myLogFile;
58     bool    myPrintLogInFile, myKeepFiles, myRemoveLogOnSuccess;
59     int   myVerboseLevel;
60
61 };
62
63 class outFileStream : public std::ofstream{
64 public:
65     ~outFileStream(){close();} //to close file at dtor
66 };
67
68 /*!
69  * \brief Class to generate string from any type
70  */
71 class ToComment : public std::string
72 {
73     std::ostringstream _s ;
74
75 public :
76
77     ToComment():std::string("") {}
78
79     ToComment(const ToComment& c):std::string() {
80         _s << c.c_str() ;
81         this->std::string::operator=( _s.str() );
82     }
83
84     ToComment & operator=(const ToComment& c) {
85         _s << c.c_str() ;
86         this->std::string::operator=( _s.str() );
87         return *this;
88     }
89
90     template <class T>
91     ToComment( const T &anything ) {
92         _s << anything ;
93         this->std::string::operator=( _s.str() );
94     }
95
96     template <class T>
97     ToComment & operator<<( const T &anything ) {
98         _s << anything ;
99         this->std::string::operator=( _s.str() );
100         return *this ;
101     }
102
103     operator char*() const {
104         return (char*)c_str();
105     }
106
107     std::ostream& Stream() {
108         return _s;
109     }
110 };
111
112
113 class MgAdapt
114 {
115     enum YesNo {YES, NO};
116 public:
117
118     MgAdapt();
119     MgAdapt(MgAdaptHypothesisData*);
120     MgAdapt(const MgAdapt&);
121     ~MgAdapt();
122     void buildModel();
123     void setData( MgAdaptHypothesisData*);
124
125     void setMedFileIn(std::string);
126     std::string getMedFileIn();
127
128     void setMedFileOut(std::string);
129     std::string getMedFileOut();
130
131     void setMeshName(std::string);
132     std::string getMeshName();
133
134     void setMeshNameOut(std::string);
135     std::string getMeshNameOut();
136
137     void setMeshOutMed(bool);
138     bool getMeshOutMed();
139
140     void setPublish(bool);
141     bool getPublish();
142
143     void setFieldName(std::string);
144     std::string getFieldName();
145
146     void setTimeStep(int);
147     int getTimeStep() const;
148
149     void setRankTimeStep(int, int );
150     int getRank();
151
152     void setLogFile(std::string);
153     std::string getLogFile();
154
155     void setVerbosityLevel(int);
156     int getVerbosityLevel();
157
158     void setRemoveOnSuccess(bool);
159     bool getRemoveOnSuccess();
160
161     MgAdaptHypothesisData* getData() const;
162
163     void setUseLocalMap(bool);
164     bool getUseLocalMap();
165
166     void setUseBackgroundMap(bool);
167     bool getUseBackgroundMap();
168
169     void setUseConstantValue(bool);
170     bool getUseConstantValue();
171
172     void setConstantValue(double);
173     bool getConstantValue();
174
175     void setSizeMapFile(std::string);
176     std::string getSizeMapFile();
177
178     void setFromMedFile(bool);
179     bool isFromMedFile();
180
181     void setKeepWorkingFiles(bool);
182     bool getKeepWorkingFiles();
183
184     void setPrintLogInFile(bool);
185     bool getPrintLogInFile();
186
187     void setWorkingDir(std::string);
188     std::string getWorkingDir() const;
189
190
191     bool setAll();
192     static std::string getCommandToRun(MgAdapt* );
193     std::string getCommandToRun() ;
194     int compute(std::string& errStr);
195     std::string getFileName() const;
196     static std::string getExeName();
197     void copyMgAdaptHypothesisData( MgAdaptHypothesisData* ) ;
198
199     void checkDirPath(std::string& );
200
201
202
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;
210
211
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);
218
219
220     /*  default values */
221     static std::string defaultWorkingDirectory();
222     static std::string defaultLogFile();
223     static bool   defaultKeepFiles();
224     static bool   defaultRemoveLogOnSuccess();
225     static int  defaultVerboseLevel();
226     static bool  defaultPrintLogInFile();
227     static bool  defaultFromMedFile();
228     static bool  defaultMeshOutMed();
229     static bool  defaultPublish();
230     static bool  defaultUseLocalMap();
231     static bool  defaultUseBackgroundMap();
232     static bool  defaultUseConstantValue();
233     static bool  defaultUseNoTimeStep();
234     static bool  defaultUseLastTimeStep();
235     static bool  defaultUseChosenTimeStep();
236     static double  defaultMaximumMemory();
237
238
239
240
241     enum Status {
242         DRS_OK,
243         DRS_EMPTY,          // a file contains no mesh with the given name
244         DRS_WARN_RENUMBER,  // a file has overlapped ranges of element numbers,
245         // so the numbers from the file are ignored
246         DRS_WARN_SKIP_ELEM, // some elements were skipped due to incorrect file data
247         DRS_WARN_DESCENDING, // some elements were skipped due to descending connectivity
248         DRS_FAIL,            // general failure (exception etc.)
249         DRS_NO_TIME_STEP            // general failure (exception etc.)
250     };
251
252     struct group {
253
254         std::string _name;
255         std::vector<MEDCoupling::mcIdType> _famListId;
256         std::vector<std::string> _famNames;
257         group(std::string name, std::vector<MEDCoupling::mcIdType> famListId, std::vector<std::string> famNames):_name(name)
258         {
259             std::vector<MEDCoupling::mcIdType>::iterator it = famListId.begin();
260             for (; it!=famListId.end(); ++it)
261                 _famListId.push_back(*it);
262
263             std::vector<std::string>::iterator itt = famNames.begin();
264             for (; itt!=famNames.end(); ++itt)
265                 _famNames.push_back(*itt);
266         }
267     };
268
269     struct family {
270         std::string _famName;
271         mcIdType _famId;
272         family(std::string famName, MEDCoupling::mcIdType famId):_famName(famName), _famId(famId) {}
273     };
274
275
276 private :
277     bool fromMedFile;
278
279     std::string medFileIn;
280     std::string medFileOut;
281     std::string meshName;
282     std::string meshNameOut;
283     bool publish, meshOutMed;
284     bool useLocalMap, useBackgroundMap, useConstantValue;
285     std::string sizeMapFile;
286     std::string fieldName;
287     double constantValue;
288     int rank,  timeStep;
289
290     /* advanced options */
291
292
293     std::string logFile;
294     std::string workingDir;
295     int verbosityLevel;
296     bool removeOnSuccess;
297     bool toKeepWorkingFiles;
298     bool printLogInFile;
299
300     /* Model DATA */
301     MgAdaptHypothesisData* data;
302
303     /*            */
304
305     TOptionValues _option2value, _customOption2value;         // user defined values
306     TOptionValues _defaultOptionValues;                       // default values
307     TOptionNames  _doubleOptions, _charOptions, _boolOptions; // to find a type of option
308
309     std::vector <std::string> _myErrorMessages;
310     Status _myStatus;
311     std::string meshFormatOutputMesh;
312     std::vector< std::string> solFormatOutput;
313     std::vector <group> groupVec;
314     std::vector <family> famVec;
315     std::vector< std::string> tmpFilesToBeDeleted;
316
317     /* convert MED-->.mesh format */
318     void convertMedFile(std::string& meshIn,std::string& solFileIn,  std::string& sizeMapIn)  ;
319     void storeGroups(MEDCoupling::MEDFileMesh* fileMesh);
320     void restoreGroups(MEDCoupling::MEDFileMesh* fileMesh) const;
321     void storefams(MEDCoupling::MEDFileMesh* fileMesh);
322     void restorefams(MEDCoupling::MEDFileMesh* fileMesh) const;
323     void storeGroupsAndFams(MEDCoupling::MEDFileMesh* fileMesh);
324     void restoreGroupsAndFams(MEDCoupling::MEDFileMesh* fileMesh) const;
325     void convertMeshFile(std::string& meshFormatIn, std::vector< std::string>& solFieldFileNames) const ;
326     void buildConstantSizeMapSolFile(const std::string& solFormatFieldFileName, const int dim, const int version, const mcIdType nbNodes) const;
327     void buildBackGroundMeshAndSolFiles(const std::vector<std::string>& fieldFileNames, const std::string& meshFormatsizeMapFile) const;
328     void getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit);
329     Status addMessage(const std::string& msg, const bool isFatal = false);
330     med_idt openMedFile(const std::string aFile) ;
331     bool isFileExist(std::string& fName) const;
332     void execCmd( const char* cmd, int& err);
333     void cleanUp();
334     void appendMsgToLogFile(std::string& msg);
335 };
336
337
338
339 #endif // MG_ADAPT_HXX