1 // Copyright (C) 2004-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //=============================================================================
20 // File : GHS3DPlugin_Hypothesis.cxx
21 // Created : Wed Apr 2 12:36:29 2008
22 // Author : Edward AGAPOV (eap)
23 //=============================================================================
25 #include "GHS3DPlugin_Hypothesis.hxx"
27 #include <TCollection_AsciiString.hxx>
31 #define getpid _getpid
34 //=======================================================================
35 //function : GHS3DPlugin_Hypothesis
36 //=======================================================================
38 GHS3DPlugin_Hypothesis::GHS3DPlugin_Hypothesis(int hypId, int studyId, SMESH_Gen * gen)
39 : SMESH_Hypothesis(hypId, studyId, gen),
40 myToMeshHoles(DefaultMeshHoles()),
43 myOptimizationLevel(DefaultOptimizationLevel()),
44 myWorkingDirectory(DefaultWorkingDirectory()),
45 myKeepFiles(DefaultKeepFiles()),
46 myVerboseLevel(DefaultVerboseLevel()),
47 myToCreateNewNodes(DefaultToCreateNewNodes()),
48 myToUseBoundaryRecoveryVersion(DefaultToUseBoundaryRecoveryVersion()),
49 myToUseFemCorrection(DefaultToUseFEMCorrection()),
50 myToRemoveCentralPoint(DefaultToRemoveCentralPoint()),
51 myEnforcedVertices(DefaultEnforcedVertices())
53 _name = "GHS3D_Parameters";
57 //=======================================================================
58 //function : SetToMeshHoles
59 //=======================================================================
61 void GHS3DPlugin_Hypothesis::SetToMeshHoles(bool toMesh)
63 if ( myToMeshHoles != toMesh ) {
64 myToMeshHoles = toMesh;
65 NotifySubMeshesHypothesisModification();
69 //=======================================================================
70 //function : GetToMeshHoles
71 //=======================================================================
73 bool GHS3DPlugin_Hypothesis::GetToMeshHoles(bool checkFreeOption) const
75 if (checkFreeOption && !myTextOption.empty()) {
76 if ( myTextOption.find("-c 0"))
78 if ( myTextOption.find("-c 1"))
84 //=======================================================================
85 //function : SetMaximumMemory
86 //=======================================================================
88 void GHS3DPlugin_Hypothesis::SetMaximumMemory(short MB)
90 if ( myMaximumMemory != MB ) {
92 NotifySubMeshesHypothesisModification();
96 //=======================================================================
97 //function : GetMaximumMemory
98 // * automatic memory adjustment mode. Default is zero
99 //=======================================================================
101 short GHS3DPlugin_Hypothesis::GetMaximumMemory() const
103 return myMaximumMemory;
106 //=======================================================================
107 //function : SetInitialMemory
108 //=======================================================================
110 void GHS3DPlugin_Hypothesis::SetInitialMemory(short MB)
112 if ( myInitialMemory != MB ) {
113 myInitialMemory = MB;
114 NotifySubMeshesHypothesisModification();
118 //=======================================================================
119 //function : GetInitialMemory
120 //=======================================================================
122 short GHS3DPlugin_Hypothesis::GetInitialMemory() const
124 return myInitialMemory;
127 //=======================================================================
128 //function : SetOptimizationLevel
129 //=======================================================================
131 void GHS3DPlugin_Hypothesis::SetOptimizationLevel(OptimizationLevel level)
133 if ( myOptimizationLevel != level ) {
134 myOptimizationLevel = level;
135 NotifySubMeshesHypothesisModification();
139 //=======================================================================
140 //function : GetOptimizationLevel
141 //=======================================================================
143 GHS3DPlugin_Hypothesis::OptimizationLevel GHS3DPlugin_Hypothesis::GetOptimizationLevel() const
145 return (OptimizationLevel) myOptimizationLevel;
148 //=======================================================================
149 //function : SetWorkingDirectory
150 //=======================================================================
152 void GHS3DPlugin_Hypothesis::SetWorkingDirectory(const std::string& path)
154 if ( myWorkingDirectory != path ) {
155 myWorkingDirectory = path;
156 NotifySubMeshesHypothesisModification();
160 //=======================================================================
161 //function : GetWorkingDirectory
162 //=======================================================================
164 std::string GHS3DPlugin_Hypothesis::GetWorkingDirectory() const
166 return myWorkingDirectory;
169 //=======================================================================
170 //function : SetKeepFiles
171 //=======================================================================
173 void GHS3DPlugin_Hypothesis::SetKeepFiles(bool toKeep)
175 if ( myKeepFiles != toKeep ) {
176 myKeepFiles = toKeep;
177 NotifySubMeshesHypothesisModification();
181 //=======================================================================
182 //function : GetKeepFiles
183 //=======================================================================
185 bool GHS3DPlugin_Hypothesis::GetKeepFiles() const
190 //=======================================================================
191 //function : SetVerboseLevel
192 //=======================================================================
194 void GHS3DPlugin_Hypothesis::SetVerboseLevel(short level)
196 if ( myVerboseLevel != level ) {
197 myVerboseLevel = level;
198 NotifySubMeshesHypothesisModification();
202 //=======================================================================
203 //function : GetVerboseLevel
204 //=======================================================================
206 short GHS3DPlugin_Hypothesis::GetVerboseLevel() const
208 return myVerboseLevel;
211 //=======================================================================
212 //function : SetToCreateNewNodes
213 //=======================================================================
215 void GHS3DPlugin_Hypothesis::SetToCreateNewNodes(bool toCreate)
217 if ( myToCreateNewNodes != toCreate ) {
218 myToCreateNewNodes = toCreate;
219 NotifySubMeshesHypothesisModification();
223 //=======================================================================
224 //function : GetToCreateNewNodes
225 //=======================================================================
227 bool GHS3DPlugin_Hypothesis::GetToCreateNewNodes() const
229 return myToCreateNewNodes;
232 //=======================================================================
233 //function : SetToUseBoundaryRecoveryVersion
234 //=======================================================================
236 void GHS3DPlugin_Hypothesis::SetToUseBoundaryRecoveryVersion(bool toUse)
238 if ( myToUseBoundaryRecoveryVersion != toUse ) {
239 myToUseBoundaryRecoveryVersion = toUse;
240 NotifySubMeshesHypothesisModification();
244 //=======================================================================
245 //function : GetToUseBoundaryRecoveryVersion
246 //=======================================================================
248 bool GHS3DPlugin_Hypothesis::GetToUseBoundaryRecoveryVersion() const
250 return myToUseBoundaryRecoveryVersion;
253 //=======================================================================
254 //function : SetFEMCorrection
255 //=======================================================================
257 void GHS3DPlugin_Hypothesis::SetFEMCorrection(bool toUseFem)
259 if ( myToUseFemCorrection != toUseFem ) {
260 myToUseFemCorrection = toUseFem;
261 NotifySubMeshesHypothesisModification();
265 //=======================================================================
266 //function : GetFEMCorrection
267 //=======================================================================
269 bool GHS3DPlugin_Hypothesis::GetFEMCorrection() const
271 return myToUseFemCorrection;
274 //=======================================================================
275 //function : SetToRemoveCentralPoint
276 //=======================================================================
278 void GHS3DPlugin_Hypothesis::SetToRemoveCentralPoint(bool toRemove)
280 if ( myToRemoveCentralPoint != toRemove ) {
281 myToRemoveCentralPoint = toRemove;
282 NotifySubMeshesHypothesisModification();
286 //=======================================================================
287 //function : GetToRemoveCentralPoint
288 //=======================================================================
290 bool GHS3DPlugin_Hypothesis::GetToRemoveCentralPoint() const
292 return myToRemoveCentralPoint;
295 //=======================================================================
296 //function : SetTextOption
297 //=======================================================================
299 void GHS3DPlugin_Hypothesis::SetTextOption(const std::string& option)
301 if ( myTextOption != option ) {
302 myTextOption = option;
303 NotifySubMeshesHypothesisModification();
307 //=======================================================================
308 //function : GetTextOption
309 //=======================================================================
311 std::string GHS3DPlugin_Hypothesis::GetTextOption() const
316 //=======================================================================
317 //function : SetEnforcedVertex
318 //=======================================================================
320 void GHS3DPlugin_Hypothesis::SetEnforcedVertex(double x, double y, double z, double size)
322 std::vector<double> coord(3);
326 myEnforcedVertices[coord] = size;
327 NotifySubMeshesHypothesisModification();
330 //=======================================================================
331 //function : GetEnforcedVertex
332 //=======================================================================
334 double GHS3DPlugin_Hypothesis::GetEnforcedVertex(double x, double y, double z)
335 throw (std::invalid_argument)
337 std::vector<double> coord(3);
341 if (myEnforcedVertices.count(coord)>0)
342 return myEnforcedVertices[coord];
343 std::ostringstream msg ;
344 msg << "No enforced vertex at " << x << ", " << y << ", " << z;
345 throw std::invalid_argument(msg.str());
348 //=======================================================================
349 //function : RemoveEnforcedVertex
350 //=======================================================================
352 void GHS3DPlugin_Hypothesis::RemoveEnforcedVertex(double x, double y, double z)
353 throw (std::invalid_argument)
355 std::vector<double> coord(3);
359 TEnforcedVertexValues::iterator it = myEnforcedVertices.find(coord);
360 if (it != myEnforcedVertices.end()) {
361 myEnforcedVertices.erase(it);
362 NotifySubMeshesHypothesisModification();
365 std::ostringstream msg ;
366 msg << "No enforced vertex at " << x << ", " << y << ", " << z;
367 throw std::invalid_argument(msg.str());
370 //=======================================================================
371 //function : ClearEnforcedVertices
372 //=======================================================================
373 void GHS3DPlugin_Hypothesis::ClearEnforcedVertices()
375 myEnforcedVertices.clear();
376 NotifySubMeshesHypothesisModification();
379 //=======================================================================
380 //function : DefaultMeshHoles
381 //=======================================================================
383 bool GHS3DPlugin_Hypothesis::DefaultMeshHoles()
385 return false; // PAL19680
388 //=======================================================================
389 //function : DefaultMaximumMemory
390 //=======================================================================
393 #include <sys/sysinfo.h>
398 short GHS3DPlugin_Hypothesis::DefaultMaximumMemory()
402 int err = sysinfo( &si );
404 int ramMB = si.totalram * si.mem_unit / 1024 / 1024;
405 return (int) ( 0.7 * ramMB );
408 // See http://msdn.microsoft.com/en-us/library/aa366589.aspx
409 MEMORYSTATUSEX statex;
410 statex.dwLength = sizeof (statex);
411 int err = GlobalMemoryStatusEx (&statex);
414 statex.ullTotalPhys / 1024 / 1024 +
415 statex.ullTotalPageFile / 1024 / 1024 +
416 statex.ullTotalVirtual / 1024 / 1024;
417 return (int) ( 0.7 * totMB );
422 //=======================================================================
423 //function : DefaultInitialMemory
424 //=======================================================================
426 short GHS3DPlugin_Hypothesis::DefaultInitialMemory()
428 return DefaultMaximumMemory();
431 //=======================================================================
432 //function : DefaultOptimizationLevel
433 //=======================================================================
435 short GHS3DPlugin_Hypothesis::DefaultOptimizationLevel()
440 //=======================================================================
441 //function : DefaultWorkingDirectory
442 //=======================================================================
444 std::string GHS3DPlugin_Hypothesis::DefaultWorkingDirectory()
446 TCollection_AsciiString aTmpDir;
448 char *Tmp_dir = getenv("SALOME_TMP_DIR");
449 if(Tmp_dir != NULL) {
454 aTmpDir = TCollection_AsciiString("C:\\");
456 aTmpDir = TCollection_AsciiString("/tmp/");
459 return aTmpDir.ToCString();
462 //=======================================================================
463 //function : DefaultKeepFiles
464 //=======================================================================
466 bool GHS3DPlugin_Hypothesis::DefaultKeepFiles()
471 //=======================================================================
472 //function : DefaultVerboseLevel
473 //=======================================================================
475 short GHS3DPlugin_Hypothesis::DefaultVerboseLevel()
480 //=======================================================================
481 //function : DefaultToCreateNewNodes
482 //=======================================================================
484 bool GHS3DPlugin_Hypothesis::DefaultToCreateNewNodes()
489 //=======================================================================
490 //function : DefaultToUseBoundaryRecoveryVersion
491 //=======================================================================
493 bool GHS3DPlugin_Hypothesis::DefaultToUseBoundaryRecoveryVersion()
498 //=======================================================================
499 //function : DefaultToUseFEMCorrection
500 //=======================================================================
502 bool GHS3DPlugin_Hypothesis::DefaultToUseFEMCorrection()
507 //=======================================================================
508 //function : DefaultToRemoveCentralPoint
509 //=======================================================================
511 bool GHS3DPlugin_Hypothesis::DefaultToRemoveCentralPoint()
516 //=======================================================================
517 //function : DefaultEnforcedVertices
518 //=======================================================================
520 GHS3DPlugin_Hypothesis::TEnforcedVertexValues GHS3DPlugin_Hypothesis::DefaultEnforcedVertices()
522 return GHS3DPlugin_Hypothesis::TEnforcedVertexValues();
526 //=======================================================================
528 //=======================================================================
530 std::ostream & GHS3DPlugin_Hypothesis::SaveTo(std::ostream & save)
532 save << (int) myToMeshHoles << " ";
533 save << myMaximumMemory << " ";
534 save << myInitialMemory << " ";
535 save << myOptimizationLevel << " ";
536 save << myWorkingDirectory << " ";
537 save << (int)myKeepFiles << " ";
538 save << myVerboseLevel << " ";
539 save << (int)myToCreateNewNodes << " ";
540 save << (int)myToUseBoundaryRecoveryVersion << " ";
541 save << (int)myToUseFemCorrection << " ";
542 save << (int)myToRemoveCentralPoint << " ";
543 save << "__OPTIONS_BEGIN__ ";
544 save << myTextOption << " ";
545 save << " __OPTIONS_END__ ";
548 TEnforcedVertexValues::iterator it = myEnforcedVertices.begin();
549 if (it != myEnforcedVertices.end()) {
550 save << "__ENFORCED_VERTICES_BEGIN__ ";
551 for ( ; it != myEnforcedVertices.end(); ++it ) {
552 save << it->first[0] << " "
553 << it->first[1] << " "
554 << it->first[2] << " "
555 << it->second << " ";
557 save << "__ENFORCED_VERTICES_END__ ";
563 //=======================================================================
564 //function : LoadFrom
565 //=======================================================================
567 std::istream & GHS3DPlugin_Hypothesis::LoadFrom(std::istream & load)
576 load.clear(ios::badbit | load.rdstate());
582 load.clear(ios::badbit | load.rdstate());
588 load.clear(ios::badbit | load.rdstate());
592 myOptimizationLevel = i;
594 load.clear(ios::badbit | load.rdstate());
596 isOK = (load >> myWorkingDirectory);
598 if ( myWorkingDirectory == "0") { // myWorkingDirectory was empty
600 myWorkingDirectory.clear();
602 else if ( myWorkingDirectory == "1" ) {
604 myWorkingDirectory.clear();
608 load.clear(ios::badbit | load.rdstate());
610 if ( !myWorkingDirectory.empty() ) {
615 load.clear(ios::badbit | load.rdstate());
620 myVerboseLevel = (short) i;
622 load.clear(ios::badbit | load.rdstate());
626 myToCreateNewNodes = (bool) i;
628 load.clear(ios::badbit | load.rdstate());
632 myToUseBoundaryRecoveryVersion = (bool) i;
634 load.clear(ios::badbit | load.rdstate());
638 myToUseFemCorrection = (bool) i;
640 load.clear(ios::badbit | load.rdstate());
644 myToRemoveCentralPoint = (bool) i;
646 load.clear(ios::badbit | load.rdstate());
648 // isOK = (load >> myTextOption);
651 // if (load >> txt) {
652 // myTextOption += " ";
653 // myTextOption += txt;
659 // // load.clear(ios::badbit | load.rdstate());
662 std::string separator;
663 bool hasOptions = false;
664 bool hasEnforcedVertices = false;
665 isOK = (load >> separator);
668 if (separator == "__OPTIONS_BEGIN__")
670 else if (separator == "__ENFORCED_VERTICES_BEGIN__")
671 hasEnforcedVertices = true;
675 isOK = (load >> myTextOption);
677 if (myTextOption == "__OPTIONS_END__") {
682 if (txt == "__OPTIONS_END__") {
695 isOK = (load >> separator);
697 if (separator == "__ENFORCED_VERTICES_BEGIN__")
698 hasEnforcedVertices = true;
701 if (hasEnforcedVertices) {
705 isOK = (load >> txt);
707 if (txt == "__ENFORCED_VERTICES_END__") {
711 x = atof(txt.c_str());
712 isOK = (load >> y >> z >> size);
715 std::vector<double> coord;
719 myEnforcedVertices[ coord ] = size;
727 //=======================================================================
728 //function : SetParametersByMesh
729 //=======================================================================
731 bool GHS3DPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh* ,const TopoDS_Shape&)
737 //================================================================================
739 * \brief Return false
741 //================================================================================
743 bool GHS3DPlugin_Hypothesis::SetParametersByDefaults(const TDefaults& /*dflts*/,
744 const SMESH_Mesh* /*theMesh*/)
749 //================================================================================
751 * \brief Return command to run ghs3d mesher excluding file prefix (-f)
753 //================================================================================
755 std::string GHS3DPlugin_Hypothesis::CommandToRun(const GHS3DPlugin_Hypothesis* hyp,
756 const bool hasShapeToMesh)
758 TCollection_AsciiString cmd( "ghs3d" );
759 // check if any option is overridden by hyp->myTextOption
760 bool m = hyp ? ( hyp->myTextOption.find("-m") == std::string::npos ) : true;
761 bool M = hyp ? ( hyp->myTextOption.find("-M") == std::string::npos ) : true;
762 bool c = hyp ? ( hyp->myTextOption.find("-c") == std::string::npos ) : true;
763 bool o = hyp ? ( hyp->myTextOption.find("-o") == std::string::npos ) : true;
764 bool p0 = hyp ? ( hyp->myTextOption.find("-p0") == std::string::npos ) : true;
765 bool C = hyp ? ( hyp->myTextOption.find("-C") == std::string::npos ) : true;
766 bool v = hyp ? ( hyp->myTextOption.find("-v") == std::string::npos ) : true;
767 bool fem = hyp ? ( hyp->myTextOption.find("-FEM")== std::string::npos ) : true;
768 bool rem = hyp ? ( hyp->myTextOption.find("-no_initial_central_point")== std::string::npos ) : true;
770 // if use boundary recovery version, few options are allowed
771 bool useBndRecovery = !C;
772 if ( !useBndRecovery && hyp )
773 useBndRecovery = hyp->myToUseBoundaryRecoveryVersion;
775 // ghs3d needs to know amount of memory it may use (MB).
776 // Default memory is defined at ghs3d installation but it may be not enough,
777 // so allow to use about all available memory
779 short aMaximumMemory = hyp ? hyp->myMaximumMemory : -1;
781 if ( aMaximumMemory < 0 )
782 cmd += DefaultMaximumMemory();
784 cmd += aMaximumMemory;
786 if ( M && !useBndRecovery ) {
787 short aInitialMemory = hyp ? hyp->myInitialMemory : -1;
789 if ( aInitialMemory > 0 )
790 cmd += aInitialMemory;
795 // 0 , all components to be meshed
796 // 1 , only the main ( outermost ) component to be meshed
797 if ( c && !useBndRecovery ) {
798 // We always run GHS3D with "to mesh holes'==TRUE (see PAL19680)
799 if ( hasShapeToMesh )
802 bool aToMeshHoles = hyp ? hyp->myToMeshHoles : DefaultMeshHoles();
810 // optimization level
811 if ( o && hyp && !useBndRecovery ) {
812 if ( hyp->myOptimizationLevel >= 0 && hyp->myOptimizationLevel < 5 ) {
813 char* level[] = { "none" , "light" , "standard" , "standard+" , "strong" };
815 cmd += level[ hyp->myOptimizationLevel ];
819 // to create internal nodes
820 if ( p0 && hyp && !hyp->myToCreateNewNodes ) {
827 cmd += hyp->myVerboseLevel;
830 // boundary recovery version
831 if ( useBndRecovery ) {
835 // to use FEM correction
836 if ( fem && hyp && hyp->myToUseFemCorrection) {
840 // to remove initial central point.
841 if ( rem && hyp && hyp->myToRemoveCentralPoint) {
842 cmd += " -no_initial_central_point";
846 if ( hyp && !hyp->myTextOption.empty() ) {
848 cmd += (char*) hyp->myTextOption.c_str();
855 return cmd.ToCString();
858 //================================================================================
860 * \brief Return a unique file name
862 //================================================================================
864 std::string GHS3DPlugin_Hypothesis::GetFileName(const GHS3DPlugin_Hypothesis* hyp)
866 std::string aTmpDir = hyp ? hyp->GetWorkingDirectory() : DefaultWorkingDirectory();
867 const char lastChar = *aTmpDir.rbegin();
869 if(lastChar != '\\') aTmpDir+='\\';
871 if(lastChar != '/') aTmpDir+='/';
874 TCollection_AsciiString aGenericName = (char*)aTmpDir.c_str();
875 aGenericName += "GHS3D_";
876 aGenericName += getpid();
878 aGenericName += Abs((Standard_Integer)(long) aGenericName.ToCString());
880 return aGenericName.ToCString();
884 //================================================================================
886 * \brief Return the enforced vertices
888 //================================================================================
890 GHS3DPlugin_Hypothesis::TEnforcedVertexValues GHS3DPlugin_Hypothesis::GetEnforcedVertices(const GHS3DPlugin_Hypothesis* hyp)
892 return hyp ? hyp->_GetEnforcedVertices():DefaultEnforcedVertices();