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