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 );
423 //=======================================================================
424 //function : DefaultInitialMemory
425 //=======================================================================
427 short GHS3DPlugin_Hypothesis::DefaultInitialMemory()
429 return DefaultMaximumMemory();
432 //=======================================================================
433 //function : DefaultOptimizationLevel
434 //=======================================================================
436 short GHS3DPlugin_Hypothesis::DefaultOptimizationLevel()
441 //=======================================================================
442 //function : DefaultWorkingDirectory
443 //=======================================================================
445 std::string GHS3DPlugin_Hypothesis::DefaultWorkingDirectory()
447 TCollection_AsciiString aTmpDir;
449 char *Tmp_dir = getenv("SALOME_TMP_DIR");
450 if(Tmp_dir != NULL) {
455 aTmpDir = TCollection_AsciiString("C:\\");
457 aTmpDir = TCollection_AsciiString("/tmp/");
460 return aTmpDir.ToCString();
463 //=======================================================================
464 //function : DefaultKeepFiles
465 //=======================================================================
467 bool GHS3DPlugin_Hypothesis::DefaultKeepFiles()
472 //=======================================================================
473 //function : DefaultVerboseLevel
474 //=======================================================================
476 short GHS3DPlugin_Hypothesis::DefaultVerboseLevel()
481 //=======================================================================
482 //function : DefaultToCreateNewNodes
483 //=======================================================================
485 bool GHS3DPlugin_Hypothesis::DefaultToCreateNewNodes()
490 //=======================================================================
491 //function : DefaultToUseBoundaryRecoveryVersion
492 //=======================================================================
494 bool GHS3DPlugin_Hypothesis::DefaultToUseBoundaryRecoveryVersion()
499 //=======================================================================
500 //function : DefaultToUseFEMCorrection
501 //=======================================================================
503 bool GHS3DPlugin_Hypothesis::DefaultToUseFEMCorrection()
508 //=======================================================================
509 //function : DefaultToRemoveCentralPoint
510 //=======================================================================
512 bool GHS3DPlugin_Hypothesis::DefaultToRemoveCentralPoint()
517 //=======================================================================
518 //function : DefaultEnforcedVertices
519 //=======================================================================
521 GHS3DPlugin_Hypothesis::TEnforcedVertexValues GHS3DPlugin_Hypothesis::DefaultEnforcedVertices()
523 return GHS3DPlugin_Hypothesis::TEnforcedVertexValues();
527 //=======================================================================
529 //=======================================================================
531 std::ostream & GHS3DPlugin_Hypothesis::SaveTo(std::ostream & save)
533 save << (int) myToMeshHoles << " ";
534 save << myMaximumMemory << " ";
535 save << myInitialMemory << " ";
536 save << myOptimizationLevel << " ";
537 save << myWorkingDirectory << " ";
538 save << (int)myKeepFiles << " ";
539 save << myVerboseLevel << " ";
540 save << (int)myToCreateNewNodes << " ";
541 save << (int)myToUseBoundaryRecoveryVersion << " ";
542 save << (int)myToUseFemCorrection << " ";
543 save << (int)myToRemoveCentralPoint << " ";
544 if (!myTextOption.empty()) {
545 save << "__OPTIONS_BEGIN__ ";
546 save << myTextOption << " ";
547 save << "__OPTIONS_END__ ";
551 TEnforcedVertexValues::iterator it = myEnforcedVertices.begin();
552 if (it != myEnforcedVertices.end()) {
553 save << "__ENFORCED_VERTICES_BEGIN__ ";
554 for ( ; it != myEnforcedVertices.end(); ++it ) {
555 save << it->first[0] << " "
556 << it->first[1] << " "
557 << it->first[2] << " "
558 << it->second << " ";
560 save << "__ENFORCED_VERTICES_END__ ";
566 //=======================================================================
567 //function : LoadFrom
568 //=======================================================================
570 std::istream & GHS3DPlugin_Hypothesis::LoadFrom(std::istream & load)
579 load.clear(ios::badbit | load.rdstate());
585 load.clear(ios::badbit | load.rdstate());
591 load.clear(ios::badbit | load.rdstate());
595 myOptimizationLevel = i;
597 load.clear(ios::badbit | load.rdstate());
599 isOK = (load >> myWorkingDirectory);
601 if ( myWorkingDirectory == "0") { // myWorkingDirectory was empty
603 myWorkingDirectory.clear();
605 else if ( myWorkingDirectory == "1" ) {
607 myWorkingDirectory.clear();
611 load.clear(ios::badbit | load.rdstate());
613 if ( !myWorkingDirectory.empty() ) {
618 load.clear(ios::badbit | load.rdstate());
623 myVerboseLevel = (short) i;
625 load.clear(ios::badbit | load.rdstate());
629 myToCreateNewNodes = (bool) i;
631 load.clear(ios::badbit | load.rdstate());
635 myToUseBoundaryRecoveryVersion = (bool) i;
637 load.clear(ios::badbit | load.rdstate());
641 myToUseFemCorrection = (bool) i;
643 load.clear(ios::badbit | load.rdstate());
647 myToRemoveCentralPoint = (bool) i;
649 load.clear(ios::badbit | load.rdstate());
651 std::string separator;
652 bool hasOptions = false;
653 bool hasEnforcedVertices = false;
654 isOK = (load >> separator);
657 if (separator == "__OPTIONS_BEGIN__")
659 else if (separator == "__ENFORCED_VERTICES_BEGIN__")
660 hasEnforcedVertices = true;
665 isOK = (load >> txt);
667 if (txt == "__OPTIONS_END__") {
668 if (!myTextOption.empty()) {
670 myTextOption.erase(myTextOption.end()-1);
682 isOK = (load >> separator);
684 if (separator == "__ENFORCED_VERTICES_BEGIN__")
685 hasEnforcedVertices = true;
688 if (hasEnforcedVertices) {
692 isOK = (load >> txt);
694 if (txt == "__ENFORCED_VERTICES_END__") {
698 x = atof(txt.c_str());
699 isOK = (load >> y >> z >> size);
702 std::vector<double> coord;
706 myEnforcedVertices[ coord ] = size;
714 //=======================================================================
715 //function : SetParametersByMesh
716 //=======================================================================
718 bool GHS3DPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh* ,const TopoDS_Shape&)
724 //================================================================================
726 * \brief Return false
728 //================================================================================
730 bool GHS3DPlugin_Hypothesis::SetParametersByDefaults(const TDefaults& /*dflts*/,
731 const SMESH_Mesh* /*theMesh*/)
736 //================================================================================
738 * \brief Return command to run ghs3d mesher excluding file prefix (-f)
740 //================================================================================
742 std::string GHS3DPlugin_Hypothesis::CommandToRun(const GHS3DPlugin_Hypothesis* hyp,
743 const bool hasShapeToMesh)
745 TCollection_AsciiString cmd( "ghs3d" );
746 // check if any option is overridden by hyp->myTextOption
747 bool m = hyp ? ( hyp->myTextOption.find("-m") == std::string::npos ) : true;
748 bool M = hyp ? ( hyp->myTextOption.find("-M") == std::string::npos ) : true;
749 bool c = hyp ? ( hyp->myTextOption.find("-c") == std::string::npos ) : true;
750 bool o = hyp ? ( hyp->myTextOption.find("-o") == std::string::npos ) : true;
751 bool p0 = hyp ? ( hyp->myTextOption.find("-p0") == std::string::npos ) : true;
752 bool C = hyp ? ( hyp->myTextOption.find("-C") == std::string::npos ) : true;
753 bool v = hyp ? ( hyp->myTextOption.find("-v") == std::string::npos ) : true;
754 bool fem = hyp ? ( hyp->myTextOption.find("-FEM")== std::string::npos ) : true;
755 bool rem = hyp ? ( hyp->myTextOption.find("-no_initial_central_point")== std::string::npos ) : true;
757 // if use boundary recovery version, few options are allowed
758 bool useBndRecovery = !C;
759 if ( !useBndRecovery && hyp )
760 useBndRecovery = hyp->myToUseBoundaryRecoveryVersion;
762 // ghs3d needs to know amount of memory it may use (MB).
763 // Default memory is defined at ghs3d installation but it may be not enough,
764 // so allow to use about all available memory
766 short aMaximumMemory = hyp ? hyp->myMaximumMemory : -1;
768 if ( aMaximumMemory < 0 )
769 cmd += DefaultMaximumMemory();
771 cmd += aMaximumMemory;
773 if ( M && !useBndRecovery ) {
774 short aInitialMemory = hyp ? hyp->myInitialMemory : -1;
776 if ( aInitialMemory > 0 )
777 cmd += aInitialMemory;
782 // 0 , all components to be meshed
783 // 1 , only the main ( outermost ) component to be meshed
784 if ( c && !useBndRecovery ) {
785 // We always run GHS3D with "to mesh holes'==TRUE (see PAL19680)
786 if ( hasShapeToMesh )
789 bool aToMeshHoles = hyp ? hyp->myToMeshHoles : DefaultMeshHoles();
797 // optimization level
798 if ( o && hyp && !useBndRecovery ) {
799 if ( hyp->myOptimizationLevel >= 0 && hyp->myOptimizationLevel < 5 ) {
800 char* level[] = { "none" , "light" , "standard" , "standard+" , "strong" };
802 cmd += level[ hyp->myOptimizationLevel ];
806 // to create internal nodes
807 if ( p0 && hyp && !hyp->myToCreateNewNodes ) {
814 cmd += hyp->myVerboseLevel;
817 // boundary recovery version
818 if ( useBndRecovery ) {
822 // to use FEM correction
823 if ( fem && hyp && hyp->myToUseFemCorrection) {
827 // to remove initial central point.
828 if ( rem && hyp && hyp->myToRemoveCentralPoint) {
829 cmd += " -no_initial_central_point";
833 if ( hyp && !hyp->myTextOption.empty() ) {
835 cmd += (char*) hyp->myTextOption.c_str();
842 return cmd.ToCString();
845 //================================================================================
847 * \brief Return a unique file name
849 //================================================================================
851 std::string GHS3DPlugin_Hypothesis::GetFileName(const GHS3DPlugin_Hypothesis* hyp)
853 std::string aTmpDir = hyp ? hyp->GetWorkingDirectory() : DefaultWorkingDirectory();
854 const char lastChar = *aTmpDir.rbegin();
856 if(lastChar != '\\') aTmpDir+='\\';
858 if(lastChar != '/') aTmpDir+='/';
861 TCollection_AsciiString aGenericName = (char*)aTmpDir.c_str();
862 aGenericName += "GHS3D_";
863 aGenericName += getpid();
865 aGenericName += Abs((Standard_Integer)(long) aGenericName.ToCString());
867 return aGenericName.ToCString();
871 //================================================================================
873 * \brief Return the enforced vertices
875 //================================================================================
877 GHS3DPlugin_Hypothesis::TEnforcedVertexValues GHS3DPlugin_Hypothesis::GetEnforcedVertices(const GHS3DPlugin_Hypothesis* hyp)
879 return hyp ? hyp->_GetEnforcedVertices():DefaultEnforcedVertices();