1 // Copyright (C) 2004-2010 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
20 //=============================================================================
21 // File : GHS3DPlugin_Hypothesis.cxx
22 // Created : Wed Apr 2 12:36:29 2008
23 // Author : Edward AGAPOV (eap)
24 //=============================================================================
26 #include "GHS3DPlugin_Hypothesis.hxx"
28 #include <TCollection_AsciiString.hxx>
32 #define getpid _getpid
35 //=======================================================================
36 //function : GHS3DPlugin_Hypothesis
37 //=======================================================================
39 GHS3DPlugin_Hypothesis::GHS3DPlugin_Hypothesis(int hypId, int studyId, SMESH_Gen * gen)
40 : SMESH_Hypothesis(hypId, studyId, gen),
41 myToMeshHoles(DefaultMeshHoles()),
44 myOptimizationLevel(DefaultOptimizationLevel()),
45 myWorkingDirectory(DefaultWorkingDirectory()),
46 myKeepFiles(DefaultKeepFiles()),
47 myVerboseLevel(DefaultVerboseLevel()),
48 myToCreateNewNodes(DefaultToCreateNewNodes()),
49 myToUseBoundaryRecoveryVersion(DefaultToUseBoundaryRecoveryVersion()),
50 myToUseFemCorrection(DefaultToUseFEMCorrection()),
51 myToRemoveCentralPoint(DefaultToRemoveCentralPoint()),
52 myEnforcedVertices(DefaultEnforcedVertices())
54 _name = "GHS3D_Parameters";
58 //=======================================================================
59 //function : SetToMeshHoles
60 //=======================================================================
62 void GHS3DPlugin_Hypothesis::SetToMeshHoles(bool toMesh)
64 if ( myToMeshHoles != toMesh ) {
65 myToMeshHoles = toMesh;
66 NotifySubMeshesHypothesisModification();
70 //=======================================================================
71 //function : GetToMeshHoles
72 //=======================================================================
74 bool GHS3DPlugin_Hypothesis::GetToMeshHoles(bool checkFreeOption) const
76 if (checkFreeOption && !myTextOption.empty()) {
77 if ( myTextOption.find("-c 0"))
79 if ( myTextOption.find("-c 1"))
85 //=======================================================================
86 //function : SetMaximumMemory
87 //=======================================================================
89 void GHS3DPlugin_Hypothesis::SetMaximumMemory(short MB)
91 if ( myMaximumMemory != MB ) {
93 NotifySubMeshesHypothesisModification();
97 //=======================================================================
98 //function : GetMaximumMemory
99 // * automatic memory adjustment mode. Default is zero
100 //=======================================================================
102 short GHS3DPlugin_Hypothesis::GetMaximumMemory() const
104 return myMaximumMemory;
107 //=======================================================================
108 //function : SetInitialMemory
109 //=======================================================================
111 void GHS3DPlugin_Hypothesis::SetInitialMemory(short MB)
113 if ( myInitialMemory != MB ) {
114 myInitialMemory = MB;
115 NotifySubMeshesHypothesisModification();
119 //=======================================================================
120 //function : GetInitialMemory
121 //=======================================================================
123 short GHS3DPlugin_Hypothesis::GetInitialMemory() const
125 return myInitialMemory;
128 //=======================================================================
129 //function : SetOptimizationLevel
130 //=======================================================================
132 void GHS3DPlugin_Hypothesis::SetOptimizationLevel(OptimizationLevel level)
134 if ( myOptimizationLevel != level ) {
135 myOptimizationLevel = level;
136 NotifySubMeshesHypothesisModification();
140 //=======================================================================
141 //function : GetOptimizationLevel
142 //=======================================================================
144 GHS3DPlugin_Hypothesis::OptimizationLevel GHS3DPlugin_Hypothesis::GetOptimizationLevel() const
146 return (OptimizationLevel) myOptimizationLevel;
149 //=======================================================================
150 //function : SetWorkingDirectory
151 //=======================================================================
153 void GHS3DPlugin_Hypothesis::SetWorkingDirectory(const std::string& path)
155 if ( myWorkingDirectory != path ) {
156 myWorkingDirectory = path;
157 NotifySubMeshesHypothesisModification();
161 //=======================================================================
162 //function : GetWorkingDirectory
163 //=======================================================================
165 std::string GHS3DPlugin_Hypothesis::GetWorkingDirectory() const
167 return myWorkingDirectory;
170 //=======================================================================
171 //function : SetKeepFiles
172 //=======================================================================
174 void GHS3DPlugin_Hypothesis::SetKeepFiles(bool toKeep)
176 if ( myKeepFiles != toKeep ) {
177 myKeepFiles = toKeep;
178 NotifySubMeshesHypothesisModification();
182 //=======================================================================
183 //function : GetKeepFiles
184 //=======================================================================
186 bool GHS3DPlugin_Hypothesis::GetKeepFiles() const
191 //=======================================================================
192 //function : SetVerboseLevel
193 //=======================================================================
195 void GHS3DPlugin_Hypothesis::SetVerboseLevel(short level)
197 if ( myVerboseLevel != level ) {
198 myVerboseLevel = level;
199 NotifySubMeshesHypothesisModification();
203 //=======================================================================
204 //function : GetVerboseLevel
205 //=======================================================================
207 short GHS3DPlugin_Hypothesis::GetVerboseLevel() const
209 return myVerboseLevel;
212 //=======================================================================
213 //function : SetToCreateNewNodes
214 //=======================================================================
216 void GHS3DPlugin_Hypothesis::SetToCreateNewNodes(bool toCreate)
218 if ( myToCreateNewNodes != toCreate ) {
219 myToCreateNewNodes = toCreate;
220 NotifySubMeshesHypothesisModification();
224 //=======================================================================
225 //function : GetToCreateNewNodes
226 //=======================================================================
228 bool GHS3DPlugin_Hypothesis::GetToCreateNewNodes() const
230 return myToCreateNewNodes;
233 //=======================================================================
234 //function : SetToUseBoundaryRecoveryVersion
235 //=======================================================================
237 void GHS3DPlugin_Hypothesis::SetToUseBoundaryRecoveryVersion(bool toUse)
239 if ( myToUseBoundaryRecoveryVersion != toUse ) {
240 myToUseBoundaryRecoveryVersion = toUse;
241 NotifySubMeshesHypothesisModification();
245 //=======================================================================
246 //function : GetToUseBoundaryRecoveryVersion
247 //=======================================================================
249 bool GHS3DPlugin_Hypothesis::GetToUseBoundaryRecoveryVersion() const
251 return myToUseBoundaryRecoveryVersion;
254 //=======================================================================
255 //function : SetFEMCorrection
256 //=======================================================================
258 void GHS3DPlugin_Hypothesis::SetFEMCorrection(bool toUseFem)
260 if ( myToUseFemCorrection != toUseFem ) {
261 myToUseFemCorrection = toUseFem;
262 NotifySubMeshesHypothesisModification();
266 //=======================================================================
267 //function : GetFEMCorrection
268 //=======================================================================
270 bool GHS3DPlugin_Hypothesis::GetFEMCorrection() const
272 return myToUseFemCorrection;
275 //=======================================================================
276 //function : SetToRemoveCentralPoint
277 //=======================================================================
279 void GHS3DPlugin_Hypothesis::SetToRemoveCentralPoint(bool toRemove)
281 if ( myToRemoveCentralPoint != toRemove ) {
282 myToRemoveCentralPoint = toRemove;
283 NotifySubMeshesHypothesisModification();
287 //=======================================================================
288 //function : GetToRemoveCentralPoint
289 //=======================================================================
291 bool GHS3DPlugin_Hypothesis::GetToRemoveCentralPoint() const
293 return myToRemoveCentralPoint;
296 //=======================================================================
297 //function : SetTextOption
298 //=======================================================================
300 void GHS3DPlugin_Hypothesis::SetTextOption(const std::string& option)
302 if ( myTextOption != option ) {
303 myTextOption = option;
304 NotifySubMeshesHypothesisModification();
308 //=======================================================================
309 //function : GetTextOption
310 //=======================================================================
312 std::string GHS3DPlugin_Hypothesis::GetTextOption() const
317 //=======================================================================
318 //function : SetEnforcedVertex
319 //=======================================================================
321 void GHS3DPlugin_Hypothesis::SetEnforcedVertex(double x, double y, double z, double size)
323 std::vector<double> coord(3);
327 myEnforcedVertices[coord] = size;
328 NotifySubMeshesHypothesisModification();
331 //=======================================================================
332 //function : GetEnforcedVertex
333 //=======================================================================
335 double GHS3DPlugin_Hypothesis::GetEnforcedVertex(double x, double y, double z)
336 throw (std::invalid_argument)
338 std::vector<double> coord(3);
342 if (myEnforcedVertices.count(coord)>0)
343 return myEnforcedVertices[coord];
344 std::ostringstream msg ;
345 msg << "No enforced vertex at " << x << ", " << y << ", " << z;
346 throw std::invalid_argument(msg.str());
349 //=======================================================================
350 //function : RemoveEnforcedVertex
351 //=======================================================================
353 void GHS3DPlugin_Hypothesis::RemoveEnforcedVertex(double x, double y, double z)
354 throw (std::invalid_argument)
356 std::vector<double> coord(3);
360 TEnforcedVertexValues::iterator it = myEnforcedVertices.find(coord);
361 if (it != myEnforcedVertices.end()) {
362 myEnforcedVertices.erase(it);
363 NotifySubMeshesHypothesisModification();
366 std::ostringstream msg ;
367 msg << "No enforced vertex at " << x << ", " << y << ", " << z;
368 throw std::invalid_argument(msg.str());
371 //=======================================================================
372 //function : ClearEnforcedVertices
373 //=======================================================================
374 void GHS3DPlugin_Hypothesis::ClearEnforcedVertices()
376 myEnforcedVertices.clear();
377 NotifySubMeshesHypothesisModification();
380 //=======================================================================
381 //function : DefaultMeshHoles
382 //=======================================================================
384 bool GHS3DPlugin_Hypothesis::DefaultMeshHoles()
386 return false; // PAL19680
389 //=======================================================================
390 //function : DefaultMaximumMemory
391 //=======================================================================
394 #include <sys/sysinfo.h>
399 short GHS3DPlugin_Hypothesis::DefaultMaximumMemory()
403 int err = sysinfo( &si );
405 int ramMB = si.totalram * si.mem_unit / 1024 / 1024;
406 return (int) ( 0.7 * ramMB );
409 // See http://msdn.microsoft.com/en-us/library/aa366589.aspx
410 MEMORYSTATUSEX statex;
411 statex.dwLength = sizeof (statex);
412 int err = GlobalMemoryStatusEx (&statex);
415 statex.ullTotalPhys / 1024 / 1024 +
416 statex.ullTotalPageFile / 1024 / 1024 +
417 statex.ullTotalVirtual / 1024 / 1024;
418 return (int) ( 0.7 * totMB );
424 //=======================================================================
425 //function : DefaultInitialMemory
426 //=======================================================================
428 short GHS3DPlugin_Hypothesis::DefaultInitialMemory()
430 return DefaultMaximumMemory();
433 //=======================================================================
434 //function : DefaultOptimizationLevel
435 //=======================================================================
437 short GHS3DPlugin_Hypothesis::DefaultOptimizationLevel()
442 //=======================================================================
443 //function : DefaultWorkingDirectory
444 //=======================================================================
446 std::string GHS3DPlugin_Hypothesis::DefaultWorkingDirectory()
448 TCollection_AsciiString aTmpDir;
450 char *Tmp_dir = getenv("SALOME_TMP_DIR");
451 if(Tmp_dir != NULL) {
456 aTmpDir = TCollection_AsciiString("C:\\");
458 aTmpDir = TCollection_AsciiString("/tmp/");
461 return aTmpDir.ToCString();
464 //=======================================================================
465 //function : DefaultKeepFiles
466 //=======================================================================
468 bool GHS3DPlugin_Hypothesis::DefaultKeepFiles()
473 //=======================================================================
474 //function : DefaultVerboseLevel
475 //=======================================================================
477 short GHS3DPlugin_Hypothesis::DefaultVerboseLevel()
482 //=======================================================================
483 //function : DefaultToCreateNewNodes
484 //=======================================================================
486 bool GHS3DPlugin_Hypothesis::DefaultToCreateNewNodes()
491 //=======================================================================
492 //function : DefaultToUseBoundaryRecoveryVersion
493 //=======================================================================
495 bool GHS3DPlugin_Hypothesis::DefaultToUseBoundaryRecoveryVersion()
500 //=======================================================================
501 //function : DefaultToUseFEMCorrection
502 //=======================================================================
504 bool GHS3DPlugin_Hypothesis::DefaultToUseFEMCorrection()
509 //=======================================================================
510 //function : DefaultToRemoveCentralPoint
511 //=======================================================================
513 bool GHS3DPlugin_Hypothesis::DefaultToRemoveCentralPoint()
518 //=======================================================================
519 //function : DefaultEnforcedVertices
520 //=======================================================================
522 GHS3DPlugin_Hypothesis::TEnforcedVertexValues GHS3DPlugin_Hypothesis::DefaultEnforcedVertices()
524 return GHS3DPlugin_Hypothesis::TEnforcedVertexValues();
528 //=======================================================================
530 //=======================================================================
532 std::ostream & GHS3DPlugin_Hypothesis::SaveTo(std::ostream & save)
534 save << (int) myToMeshHoles << " ";
535 save << myMaximumMemory << " ";
536 save << myInitialMemory << " ";
537 save << myOptimizationLevel << " ";
538 save << myWorkingDirectory << " ";
539 save << (int)myKeepFiles << " ";
540 save << myVerboseLevel << " ";
541 save << (int)myToCreateNewNodes << " ";
542 save << (int)myToUseBoundaryRecoveryVersion << " ";
543 save << (int)myToUseFemCorrection << " ";
544 save << (int)myToRemoveCentralPoint << " ";
545 if (!myTextOption.empty()) {
546 save << "__OPTIONS_BEGIN__ ";
547 save << myTextOption << " ";
548 save << "__OPTIONS_END__ ";
552 TEnforcedVertexValues::iterator it = myEnforcedVertices.begin();
553 if (it != myEnforcedVertices.end()) {
554 save << "__ENFORCED_VERTICES_BEGIN__ ";
555 for ( ; it != myEnforcedVertices.end(); ++it ) {
556 save << it->first[0] << " "
557 << it->first[1] << " "
558 << it->first[2] << " "
559 << it->second << " ";
561 save << "__ENFORCED_VERTICES_END__ ";
567 //=======================================================================
568 //function : LoadFrom
569 //=======================================================================
571 std::istream & GHS3DPlugin_Hypothesis::LoadFrom(std::istream & load)
580 load.clear(ios::badbit | load.rdstate());
586 load.clear(ios::badbit | load.rdstate());
592 load.clear(ios::badbit | load.rdstate());
596 myOptimizationLevel = i;
598 load.clear(ios::badbit | load.rdstate());
600 isOK = (load >> myWorkingDirectory);
602 if ( myWorkingDirectory == "0") { // myWorkingDirectory was empty
604 myWorkingDirectory.clear();
606 else if ( myWorkingDirectory == "1" ) {
608 myWorkingDirectory.clear();
612 load.clear(ios::badbit | load.rdstate());
614 if ( !myWorkingDirectory.empty() ) {
619 load.clear(ios::badbit | load.rdstate());
624 myVerboseLevel = (short) i;
626 load.clear(ios::badbit | load.rdstate());
630 myToCreateNewNodes = (bool) i;
632 load.clear(ios::badbit | load.rdstate());
636 myToUseBoundaryRecoveryVersion = (bool) i;
638 load.clear(ios::badbit | load.rdstate());
642 myToUseFemCorrection = (bool) i;
644 load.clear(ios::badbit | load.rdstate());
648 myToRemoveCentralPoint = (bool) i;
650 load.clear(ios::badbit | load.rdstate());
652 std::string separator;
653 bool hasOptions = false;
654 bool hasEnforcedVertices = false;
655 isOK = (load >> separator);
658 if (separator == "__OPTIONS_BEGIN__")
660 else if (separator == "__ENFORCED_VERTICES_BEGIN__")
661 hasEnforcedVertices = true;
666 isOK = (load >> txt);
668 if (txt == "__OPTIONS_END__") {
669 if (!myTextOption.empty()) {
671 myTextOption.erase(myTextOption.end()-1);
683 isOK = (load >> separator);
685 if (separator == "__ENFORCED_VERTICES_BEGIN__")
686 hasEnforcedVertices = true;
689 if (hasEnforcedVertices) {
693 isOK = (load >> txt);
695 if (txt == "__ENFORCED_VERTICES_END__") {
699 x = atof(txt.c_str());
700 isOK = (load >> y >> z >> size);
703 std::vector<double> coord;
707 myEnforcedVertices[ coord ] = size;
715 //=======================================================================
716 //function : SetParametersByMesh
717 //=======================================================================
719 bool GHS3DPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh* ,const TopoDS_Shape&)
725 //================================================================================
727 * \brief Return false
729 //================================================================================
731 bool GHS3DPlugin_Hypothesis::SetParametersByDefaults(const TDefaults& /*dflts*/,
732 const SMESH_Mesh* /*theMesh*/)
737 //================================================================================
739 * \brief Return command to run ghs3d mesher excluding file prefix (-f)
741 //================================================================================
743 std::string GHS3DPlugin_Hypothesis::CommandToRun(const GHS3DPlugin_Hypothesis* hyp,
744 const bool hasShapeToMesh)
746 TCollection_AsciiString cmd( "ghs3d" );
747 // check if any option is overridden by hyp->myTextOption
748 bool m = hyp ? ( hyp->myTextOption.find("-m") == std::string::npos ) : true;
749 bool M = hyp ? ( hyp->myTextOption.find("-M") == std::string::npos ) : true;
750 bool c = hyp ? ( hyp->myTextOption.find("-c") == std::string::npos ) : true;
751 bool o = hyp ? ( hyp->myTextOption.find("-o") == std::string::npos ) : true;
752 bool p0 = hyp ? ( hyp->myTextOption.find("-p0") == std::string::npos ) : true;
753 bool C = hyp ? ( hyp->myTextOption.find("-C") == std::string::npos ) : true;
754 bool v = hyp ? ( hyp->myTextOption.find("-v") == std::string::npos ) : true;
755 bool fem = hyp ? ( hyp->myTextOption.find("-FEM")== std::string::npos ) : true;
756 bool rem = hyp ? ( hyp->myTextOption.find("-no_initial_central_point")== std::string::npos ) : true;
758 // if use boundary recovery version, few options are allowed
759 bool useBndRecovery = !C;
760 if ( !useBndRecovery && hyp )
761 useBndRecovery = hyp->myToUseBoundaryRecoveryVersion;
763 // ghs3d needs to know amount of memory it may use (MB).
764 // Default memory is defined at ghs3d installation but it may be not enough,
765 // so allow to use about all available memory
767 short aMaximumMemory = hyp ? hyp->myMaximumMemory : -1;
769 if ( aMaximumMemory < 0 )
770 cmd += DefaultMaximumMemory();
772 cmd += aMaximumMemory;
774 if ( M && !useBndRecovery ) {
775 short aInitialMemory = hyp ? hyp->myInitialMemory : -1;
777 if ( aInitialMemory > 0 )
778 cmd += aInitialMemory;
783 // 0 , all components to be meshed
784 // 1 , only the main ( outermost ) component to be meshed
785 if ( c && !useBndRecovery ) {
786 // We always run GHS3D with "to mesh holes'==TRUE (see PAL19680)
787 if ( hasShapeToMesh )
790 bool aToMeshHoles = hyp ? hyp->myToMeshHoles : DefaultMeshHoles();
798 // optimization level
799 if ( o && hyp && !useBndRecovery ) {
800 if ( hyp->myOptimizationLevel >= 0 && hyp->myOptimizationLevel < 5 ) {
801 char* level[] = { "none" , "light" , "standard" , "standard+" , "strong" };
803 cmd += level[ hyp->myOptimizationLevel ];
807 // to create internal nodes
808 if ( p0 && hyp && !hyp->myToCreateNewNodes ) {
815 cmd += hyp->myVerboseLevel;
818 // boundary recovery version
819 if ( useBndRecovery ) {
823 // to use FEM correction
824 if ( fem && hyp && hyp->myToUseFemCorrection) {
828 // to remove initial central point.
829 if ( rem && hyp && hyp->myToRemoveCentralPoint) {
830 cmd += " -no_initial_central_point";
834 if ( hyp && !hyp->myTextOption.empty() ) {
836 cmd += (char*) hyp->myTextOption.c_str();
843 return cmd.ToCString();
846 //================================================================================
848 * \brief Return a unique file name
850 //================================================================================
852 std::string GHS3DPlugin_Hypothesis::GetFileName(const GHS3DPlugin_Hypothesis* hyp)
854 std::string aTmpDir = hyp ? hyp->GetWorkingDirectory() : DefaultWorkingDirectory();
855 const char lastChar = *aTmpDir.rbegin();
857 if(lastChar != '\\') aTmpDir+='\\';
859 if(lastChar != '/') aTmpDir+='/';
862 TCollection_AsciiString aGenericName = (char*)aTmpDir.c_str();
863 aGenericName += "GHS3D_";
864 aGenericName += getpid();
866 aGenericName += Abs((Standard_Integer)(long) aGenericName.ToCString());
868 return aGenericName.ToCString();
872 //================================================================================
874 * \brief Return the enforced vertices
876 //================================================================================
878 GHS3DPlugin_Hypothesis::TEnforcedVertexValues GHS3DPlugin_Hypothesis::GetEnforcedVertices(const GHS3DPlugin_Hypothesis* hyp)
880 return hyp ? hyp->_GetEnforcedVertices():DefaultEnforcedVertices();