Salome HOME
les champs par medcoupling
[modules/smesh.git] / src / SMESH / MG_ADAPT.hxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
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 // 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 class outFileStream : public std::ofstream{
61 public:
62     ~outFileStream(){close();} //to close file at dtor
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
113 public:
114
115   MgAdapt();
116   MgAdapt(MgAdaptHypothesisData*);
117   MgAdapt(const MgAdapt&);
118   ~MgAdapt();
119   void buildModel();
120   void setData( MgAdaptHypothesisData* data);
121
122   void setMedFileIn(std::string fileName);
123   std::string getMedFileIn();
124
125   void setMedFileOut(std::string fileOut);
126   std::string getMedFileOut();
127
128   void setMeshName(std::string name);
129   std::string getMeshName();
130
131   void setMeshNameOut(std::string  name);
132   std::string getMeshNameOut();
133
134   void setMeshOutMed(bool mybool);
135   bool getMeshOutMed();
136
137   void setPublish(bool mybool);
138   bool getPublish();
139
140   void setFieldName(std::string myFieldName);
141   std::string getFieldName();
142
143   void setTimeStep(int time);
144   int getTimeStep() const;
145
146   void setRankTimeStep(int time, int myRank);
147   int getRank();
148
149   void setTimeStepRankLast();
150   void setNoTimeStep();
151   void setChosenTimeStepRank();
152   void updateTimeStepRank();
153
154   void setLogFile(std::string);
155   std::string getLogFile();
156
157   void setVerbosityLevel(int verbosity);
158   int getVerbosityLevel();
159
160   void setRemoveOnSuccess(bool mybool);
161   bool getRemoveOnSuccess();
162
163   MgAdaptHypothesisData* getData() const;
164
165   void setUseLocalMap(bool mybool);
166   bool getUseLocalMap();
167
168   void setUseBackgroundMap(bool mybool);
169   bool getUseBackgroundMap();
170
171   void setUseConstantValue(bool mybool);
172   bool getUseConstantValue();
173
174   void setConstantValue(double cnst);
175   double getConstantValue() const;
176
177   void setSizeMapFile(std::string mapFile);
178   std::string getSizeMapFile();
179
180   void setFromMedFile(bool mybool);
181   bool isFromMedFile();
182
183   void setKeepWorkingFiles(bool mybool);
184   bool getKeepWorkingFiles();
185
186   void setPrintLogInFile(bool mybool);
187   bool getPrintLogInFile();
188
189   void setWorkingDir(std::string dir);
190   std::string getWorkingDir() const;
191
192
193   bool setAll();
194   static std::string getCommandToRun(MgAdapt* );
195   std::string getCommandToRun() ;
196   int compute(std::string& errStr);
197   std::string getFileName() const;
198   static std::string getExeName();
199   void copyMgAdaptHypothesisData( const MgAdaptHypothesisData* from) ;
200
201   void checkDirPath(std::string& dirPath);
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   /*  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   static bool isFileExist(const std::string& fName);
237
238   enum Status {
239     DRS_OK,
240     DRS_EMPTY,          // a file contains no mesh with the given name
241     DRS_WARN_RENUMBER,  // a file has overlapped ranges of element numbers,
242     // so the numbers from the file are ignored
243     DRS_WARN_SKIP_ELEM, // some elements were skipped due to incorrect file data
244     DRS_WARN_DESCENDING, // some elements were skipped due to descending connectivity
245     DRS_FAIL,            // general failure (exception etc.)
246     DRS_NO_TIME_STEP            // general failure (exception etc.)
247   };
248
249   struct group {
250
251     std::string _name;
252     std::vector<MEDCoupling::mcIdType> _famListId;
253     std::vector<std::string> _famNames;
254     group(std::string name, std::vector<MEDCoupling::mcIdType> famListId, std::vector<std::string> famNames):_name(name)
255     {
256       std::vector<MEDCoupling::mcIdType>::iterator it = famListId.begin();
257       for (; it!=famListId.end(); ++it)
258           _famListId.push_back(*it);
259
260       std::vector<std::string>::iterator itt = famNames.begin();
261       for (; itt!=famNames.end(); ++itt)
262           _famNames.push_back(*itt);
263     }
264   };
265
266   struct family {
267     std::string _famName;
268     mcIdType _famId;
269     family(std::string famName, MEDCoupling::mcIdType famId):_famName(famName), _famId(famId) {}
270   };
271
272
273 private :
274   bool fromMedFile;
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   bool myUseLastTimeStep, myUseNoTimeStep, myUseChosenTimeStep;
282   std::string sizeMapFile;
283   std::string fieldName;
284   double constantValue;
285   int rank,  timeStep;
286
287   /* advanced options */
288
289
290   std::string logFile;
291   std::string workingDir;
292   int verbosityLevel;
293   bool removeOnSuccess;
294   bool toKeepWorkingFiles;
295   bool printLogInFile;
296
297   /* Model DATA */
298   MgAdaptHypothesisData* data;
299
300   /*            */
301
302   TOptionValues _option2value, _customOption2value;         // user defined values
303   TOptionValues _defaultOptionValues;                       // default values
304   TOptionNames  _doubleOptions, _charOptions, _boolOptions; // to find a type of option
305
306   std::vector <std::string> _myErrorMessages;
307   Status _myStatus;
308   std::string meshFormatOutputMesh;
309   std::vector< std::string> solFormatOutput;
310   std::vector <group> groupVec;
311   std::vector <family> famVec;
312   std::vector< std::string> tmpFilesToBeDeleted;
313
314   /* convert MED-->.mesh format */
315   void convertMedFile(std::string& meshIn,std::string& solFileIn,  std::string& sizeMapIn)  ;
316   void storeGroups(MEDCoupling::MEDFileMesh* fileMesh);
317   void restoreGroups(MEDCoupling::MEDFileMesh* fileMesh) const;
318   void storefams(MEDCoupling::MEDFileMesh* fileMesh);
319   void restorefams(MEDCoupling::MEDFileMesh* fileMesh) const;
320   void storeGroupsAndFams(MEDCoupling::MEDFileMesh* fileMesh);
321   void restoreGroupsAndFams(MEDCoupling::MEDFileMesh* fileMesh) const;
322   void convertMeshFile(std::string& meshFormatIn, std::vector< std::string>& solFieldFileNames) const ;
323   void buildConstantSizeMapSolFile(const std::string& solFormatFieldFileName, const int dim, const int version, const mcIdType nbNodes) const;
324   void buildBackGroundMeshAndSolFiles(const std::vector<std::string>& fieldFileNames, const std::string& meshFormatsizeMapFile) const;
325   void getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit);
326   Status addMessage(const std::string& msg, const bool isFatal = false);
327   med_idt openMedFile(const std::string aFile) ;
328   void execCmd( const char* cmd, int& err);
329   void cleanUp();
330   void appendMsgToLogFile(std::string& msg);
331   std::vector<std::string> getListFieldsNames(std::string fileIn) ;
332   bool checkFieldName(std::string fileIn) ;
333   bool checkTimeStepRank(std::string fileIn) ;
334
335 };
336
337 }
338
339 #endif // MG_ADAPT_HXX