Salome HOME
Merge branch 'gni/adaptation' of https://codev-tuleap.cea.fr/plugins/git/salome/smesh...
[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
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         
153     void setLogFile(std::string);
154     std::string getLogFile();
155
156     void setVerbosityLevel(int verbosity);
157     int getVerbosityLevel();
158
159     void setRemoveOnSuccess(bool mybool);
160     bool getRemoveOnSuccess();
161
162     MgAdaptHypothesisData* getData() const;
163
164     void setUseLocalMap(bool mybool);
165     bool getUseLocalMap();
166
167     void setUseBackgroundMap(bool mybool);
168     bool getUseBackgroundMap();
169
170     void setUseConstantValue(bool mybool);
171     bool getUseConstantValue();
172
173     void setConstantValue(double cnst);
174     double getConstantValue() const;
175
176     void setSizeMapFile(std::string mapFile);
177     std::string getSizeMapFile();
178
179     void setFromMedFile(bool mybool);
180     bool isFromMedFile();
181
182     void setKeepWorkingFiles(bool mybool);
183     bool getKeepWorkingFiles();
184
185     void setPrintLogInFile(bool mybool);
186     bool getPrintLogInFile();
187
188     void setWorkingDir(std::string dir);
189     std::string getWorkingDir() const;
190
191
192     bool setAll();
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) ;
199
200     void checkDirPath(std::string& dirPath);
201
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;
209
210
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);
217
218
219     /*  default values */
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
237
238
239
240     enum Status {
241         DRS_OK,
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.)
249     };
250
251     struct group {
252
253         std::string _name;
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)
257         {
258             std::vector<MEDCoupling::mcIdType>::iterator it = famListId.begin();
259             for (; it!=famListId.end(); ++it)
260                 _famListId.push_back(*it);
261
262             std::vector<std::string>::iterator itt = famNames.begin();
263             for (; itt!=famNames.end(); ++itt)
264                 _famNames.push_back(*itt);
265         }
266     };
267
268     struct family {
269         std::string _famName;
270         mcIdType _famId;
271         family(std::string famName, MEDCoupling::mcIdType famId):_famName(famName), _famId(famId) {}
272     };
273
274
275 private :
276     bool fromMedFile;
277
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;
287     int rank,  timeStep;
288
289     /* advanced options */
290
291
292     std::string logFile;
293     std::string workingDir;
294     int verbosityLevel;
295     bool removeOnSuccess;
296     bool toKeepWorkingFiles;
297     bool printLogInFile;
298
299     /* Model DATA */
300     MgAdaptHypothesisData* data;
301
302     /*            */
303
304     TOptionValues _option2value, _customOption2value;         // user defined values
305     TOptionValues _defaultOptionValues;                       // default values
306     TOptionNames  _doubleOptions, _charOptions, _boolOptions; // to find a type of option
307
308     std::vector <std::string> _myErrorMessages;
309     Status _myStatus;
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;
315
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);
332     void cleanUp();
333     void appendMsgToLogFile(std::string& msg);
334 };
335
336 }
337
338 #endif // MG_ADAPT_HXX