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