Salome HOME
bos #24009 Merge gni/adaptation branch (MG-Adapt)
[modules/smesh.git] / src / SMESH / MG_ADAPT.hxx
1 // Copyright (C) 2020-2021  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 #ifndef MG_ADAPT_HXX
21 #define MG_ADAPT_HXX
22
23 #include <string>
24 #include <map>
25 #include <vector>
26 #include <set>
27
28 #include "SMESH_SMESH.hxx"
29
30 namespace MEDCoupling
31 {
32   class MEDFileMesh;
33 }
34
35 namespace MG_ADAPT
36 {
37 typedef std::map< std::string, std::string > TOptionValues;
38 typedef std::set< std::string > TOptionNames;
39
40 std::string remove_extension(const std::string& filename);
41
42 struct MgAdaptHypothesisData
43 {
44   std::string myFileInDir, myMeshFileIn, myInMeshName, myMeshFileBackground, myOutMeshName,
45               myMeshFileOut, myFileOutDir, myFileSizeMapDir, myFieldName;
46   bool fromMedFile;
47   bool myPublish, myMeshOutMed;
48   bool myUseLocalMap, myUseBackgroundMap, myUseConstantValue;
49   double myConstantValue;
50   int  myRank, myTimeStep;
51   bool myUseNoTimeStep, myUseLastTimeStep, myUseChosenTimeStep;
52   std::string myWorkingDir, myLogFile;
53   bool myPrintLogInFile, myKeepFiles, myRemoveLogOnSuccess;
54   int myVerboseLevel;
55 };
56
57
58 class SMESH_EXPORT MgAdapt
59 {
60
61 public:
62
63   MgAdapt();
64   MgAdapt(MgAdaptHypothesisData*);
65   MgAdapt(const MgAdapt&);
66   ~MgAdapt();
67   void buildModel();
68   void setData( MgAdaptHypothesisData* data);
69
70   void setMedFileIn(std::string fileName);
71   std::string getMedFileIn();
72
73   void setMedFileOut(std::string fileOut);
74   std::string getMedFileOut();
75
76   void setMeshName(std::string name);
77   std::string getMeshName();
78
79   void setMeshNameOut(std::string  name);
80   std::string getMeshNameOut();
81
82   void setMeshOutMed(bool mybool);
83   bool getMeshOutMed();
84
85   void setPublish(bool mybool);
86   bool getPublish();
87
88   void setFieldName(std::string myFieldName);
89   std::string getFieldName();
90
91   void setTimeStep(int time);
92   int getTimeStep() const;
93
94   void setRankTimeStep(int time, int myRank);
95   int getRank();
96
97   void setTimeStepRankLast();
98   void setNoTimeStep();
99   void setChosenTimeStepRank();
100   void updateTimeStepRank();
101
102   void setLogFile(std::string);
103   std::string getLogFile();
104
105   void setVerbosityLevel(int verbosity);
106   int getVerbosityLevel();
107
108   void setRemoveOnSuccess(bool mybool);
109   bool getRemoveOnSuccess();
110
111   MgAdaptHypothesisData* getData() const;
112
113   void setUseLocalMap(bool mybool);
114   bool getUseLocalMap();
115
116   void setUseBackgroundMap(bool mybool);
117   bool getUseBackgroundMap();
118
119   void setUseConstantValue(bool mybool);
120   bool getUseConstantValue();
121
122   void setConstantValue(double cnst);
123   double getConstantValue() const;
124
125   void setSizeMapFile(std::string mapFile);
126   std::string getSizeMapFile();
127
128   void setFromMedFile(bool mybool);
129   bool isFromMedFile();
130
131   void setKeepWorkingFiles(bool mybool);
132   bool getKeepWorkingFiles();
133
134   void setPrintLogInFile(bool mybool);
135   bool getPrintLogInFile();
136
137   void setWorkingDir(std::string dir);
138   std::string getWorkingDir() const;
139
140
141   bool setAll();
142   static std::string getCommandToRun(MgAdapt* );
143   std::string getCommandToRun() ;
144   int compute(std::string& errStr);
145   std::string getFileName() const;
146   static std::string getExeName();
147   void copyMgAdaptHypothesisData( const MgAdaptHypothesisData* from) ;
148
149   void checkDirPath(std::string& dirPath);
150
151   bool hasOptionDefined( const std::string& optionName ) const;
152   void setOptionValue(const std::string& optionName,
153                       const std::string& optionValue);
154   std::string getOptionValue(const std::string& optionName,
155                               bool*              isDefault=0) const;
156   std::vector <std::string> getCustomOptionValuesStrVec() const;
157   std::vector <std::string> getOptionValuesStrVec() const;
158
159
160   TOptionValues        getOptionValues()       const;
161   const TOptionValues& getCustomOptionValues() const ;
162   static double toDbl(const std::string&, bool* isOk = 0);
163   static bool toBool(const std::string&, bool* isOk = 0);
164   static int toInt(const std::string&, bool* isOk = 0 );
165   static std::string toLowerStr(const std::string& str);
166
167   /*  default values */
168   static std::string defaultWorkingDirectory();
169   static std::string defaultLogFile();
170   static bool  defaultKeepFiles();
171   static bool  defaultRemoveLogOnSuccess();
172   static int   defaultVerboseLevel();
173   static bool  defaultPrintLogInFile();
174   static bool  defaultFromMedFile();
175   static bool  defaultMeshOutMed();
176   static bool  defaultPublish();
177   static bool  defaultUseLocalMap();
178   static bool  defaultUseBackgroundMap();
179   static bool  defaultUseConstantValue();
180   static bool  defaultUseNoTimeStep();
181   static bool  defaultUseLastTimeStep();
182   static bool  defaultUseChosenTimeStep();
183   static double  defaultMaximumMemory();
184
185   enum Status {
186     DRS_OK,
187     DRS_EMPTY,          // a file contains no mesh with the given name
188     DRS_WARN_RENUMBER,  // a file has overlapped ranges of element numbers,
189     // so the numbers from the file are ignored
190     DRS_WARN_SKIP_ELEM, // some elements were skipped due to incorrect file data
191     DRS_WARN_DESCENDING, // some elements were skipped due to descending connectivity
192     DRS_FAIL,            // general failure (exception etc.)
193     DRS_NO_TIME_STEP            // general failure (exception etc.)
194   };
195
196   struct group
197   {
198     std::string              _name;
199     std::vector<int>         _famListId;
200     std::vector<std::string> _famNames;
201     group(std::string name, std::vector<int> famListId, std::vector<std::string> famNames)
202       :_name(name), _famListId( famListId ), _famNames( famNames ) {}
203   };
204
205   struct family
206   {
207     std::string _famName;
208     int         _famId;
209     family(std::string famName, int famId):_famName(famName), _famId(famId) {}
210   };
211
212
213 private :
214   bool fromMedFile;
215   std::string medFileIn;
216   std::string medFileOut;
217   std::string meshName;
218   std::string meshNameOut;
219   bool publish, meshOutMed;
220   bool useLocalMap, useBackgroundMap, useConstantValue;
221   bool myUseLastTimeStep, myUseNoTimeStep, myUseChosenTimeStep;
222   std::string sizeMapFile;
223   std::string fieldName;
224   double constantValue;
225   int rank,  timeStep;
226
227   /* advanced options */
228
229
230   std::string logFile;
231   std::string workingDir;
232   int verbosityLevel;
233   bool removeOnSuccess;
234   bool toKeepWorkingFiles;
235   bool printLogInFile;
236
237   /* Model DATA */
238   MgAdaptHypothesisData* data;
239
240   /*            */
241
242   TOptionValues _option2value, _customOption2value;         // user defined values
243   TOptionValues _defaultOptionValues;                       // default values
244   TOptionNames  _doubleOptions, _charOptions, _boolOptions; // to find a type of option
245
246   std::vector <std::string> _myErrorMessages;
247   Status _myStatus;
248   std::string meshFormatOutputMesh;
249   std::vector< std::string> solFormatOutput;
250   std::vector <group> groupVec;
251   std::vector <family> famVec;
252   std::vector< std::string> tmpFilesToBeDeleted;
253
254   /* convert MED-->.mesh format */
255   void convertMedFile(std::string& meshIn,std::string& solFileIn,  std::string& sizeMapIn)  ;
256   void storeGroups(MEDCoupling::MEDFileMesh* fileMesh);
257   void restoreGroups(MEDCoupling::MEDFileMesh* fileMesh) const;
258   void storefams(MEDCoupling::MEDFileMesh* fileMesh);
259   void restorefams(MEDCoupling::MEDFileMesh* fileMesh) const;
260   void storeGroupsAndFams(MEDCoupling::MEDFileMesh* fileMesh);
261   void restoreGroupsAndFams(MEDCoupling::MEDFileMesh* fileMesh) const;
262   void convertMeshFile(std::string& meshFormatIn, std::vector< std::string>& solFieldFileNames) const ;
263   void buildConstantSizeMapSolFile(const std::string& solFormatFieldFileName, const int dim, const int version, const size_t nbNodes) const;
264   void buildBackGroundMeshAndSolFiles(const std::vector<std::string>& fieldFileNames, const std::string& meshFormatsizeMapFile) const;
265   Status addMessage(const std::string& msg, const bool isFatal = false);
266   void execCmd( const char* cmd, int& err);
267   void cleanUp();
268   void appendMsgToLogFile(std::string& msg);
269   std::vector<std::string> getListFieldsNames(std::string fileIn) ;
270   void checkDimensionOptionAdaptation() ;
271   void checkFieldName(std::string fileIn) ;
272   void checkTimeStepRank(std::string fileIn) ;
273
274 };
275
276 } // namespace MG_ADAPT
277
278 #endif // MG_ADAPT_HXX