From: Yoann Audouin Date: Tue, 13 Sep 2022 12:33:19 +0000 (+0200) Subject: Using RAII to remove file.close X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=80e4ccd74d3198385e5efd2769726eba3d4b6d2e;p=plugins%2Fnetgenplugin.git Using RAII to remove file.close --- diff --git a/src/NETGENPlugin/NETGENPlugin_NETGEN_3D.cxx b/src/NETGENPlugin/NETGENPlugin_NETGEN_3D.cxx index a69bc69..b3d624f 100644 --- a/src/NETGENPlugin/NETGENPlugin_NETGEN_3D.cxx +++ b/src/NETGENPlugin/NETGENPlugin_NETGEN_3D.cxx @@ -279,15 +279,16 @@ void NETGENPlugin_NETGEN_3D::exportElementOrientation(SMESH_Mesh& aMesh, } // loop on elements on a face } // loop on faces of a SOLID or SHELL - std::ofstream df(output_file, ios::out|ios::binary); - int size=elemOrientation.size(); + { + std::ofstream df(output_file, ios::out|ios::binary); + int size=elemOrientation.size(); - df.write((char*)&size, sizeof(int)); - for(auto const& [id, orient]:elemOrientation){ - df.write((char*)&id, sizeof(vtkIdType)); - df.write((char*)&orient, sizeof(bool)); + df.write((char*)&size, sizeof(int)); + for(auto const& [id, orient]:elemOrientation){ + df.write((char*)&id, sizeof(vtkIdType)); + df.write((char*)&orient, sizeof(bool)); + } } - df.close(); } int NETGENPlugin_NETGEN_3D::RemoteCompute(SMESH_Mesh& aMesh, @@ -363,10 +364,10 @@ int NETGENPlugin_NETGEN_3D::RemoteCompute(SMESH_Mesh& aMesh, //std::cout << cmd << std::endl; // Writing command in log - std::ofstream flog(log_file.string()); - flog << cmd << endl; - flog.close(); - + { + std::ofstream flog(log_file.string()); + flog << cmd << endl; + } // TODO: Replace system by something else to handle redirection for windows int ret = system(cmd.c_str()); auto time5 = std::chrono::high_resolution_clock::now(); @@ -382,66 +383,62 @@ int NETGENPlugin_NETGEN_3D::RemoteCompute(SMESH_Mesh& aMesh, } aMesh.Lock(); - std::ifstream df(new_element_file.string(), ios::binary); - - int Netgen_NbOfNodes; - int Netgen_NbOfNodesNew; - int Netgen_NbOfTetra; - double Netgen_point[3]; - int Netgen_tetrahedron[4]; - int nodeID; - - SMESH_MesherHelper helper(aMesh); - // This function - int _quadraticMesh = helper.IsQuadraticSubMesh(aShape); - helper.SetElementsOnShape( true ); - - // Number of nodes in intial mesh - df.read((char*) &Netgen_NbOfNodes, sizeof(int)); - // Number of nodes added by netgen - df.read((char*) &Netgen_NbOfNodesNew, sizeof(int)); - - // Filling nodevec (correspondence netgen numbering mesh numbering) - vector< const SMDS_MeshNode* > nodeVec ( Netgen_NbOfNodesNew + 1 ); - //vector nodeTmpVec ( Netgen_NbOfNodesNew + 1 ); - SMESHDS_Mesh * meshDS = helper.GetMeshDS(); - for (int nodeIndex = 1 ; nodeIndex <= Netgen_NbOfNodes; ++nodeIndex ) { - //Id of the point - df.read((char*) &nodeID, sizeof(int)); - nodeVec.at(nodeIndex) = meshDS->FindNode(nodeID); - } - - auto time6 = std::chrono::high_resolution_clock::now(); - elapsed = std::chrono::duration_cast(time6-time5); - std::cout << "Time for exec of nodeVec: " << elapsed.count() * 1e-9 << std::endl; + std::ifstream df(new_element_file.string(), ios::binary); + int Netgen_NbOfNodes; + int Netgen_NbOfNodesNew; + int Netgen_NbOfTetra; + double Netgen_point[3]; + int Netgen_tetrahedron[4]; + int nodeID; + + SMESH_MesherHelper helper(aMesh); + // This function + int _quadraticMesh = helper.IsQuadraticSubMesh(aShape); + helper.SetElementsOnShape( true ); + + // Number of nodes in intial mesh + df.read((char*) &Netgen_NbOfNodes, sizeof(int)); + // Number of nodes added by netgen + df.read((char*) &Netgen_NbOfNodesNew, sizeof(int)); + + // Filling nodevec (correspondence netgen numbering mesh numbering) + vector< const SMDS_MeshNode* > nodeVec ( Netgen_NbOfNodesNew + 1 ); + //vector nodeTmpVec ( Netgen_NbOfNodesNew + 1 ); + SMESHDS_Mesh * meshDS = helper.GetMeshDS(); + for (int nodeIndex = 1 ; nodeIndex <= Netgen_NbOfNodes; ++nodeIndex ) + { + //Id of the point + df.read((char*) &nodeID, sizeof(int)); + nodeVec.at(nodeIndex) = meshDS->FindNode(nodeID); + } - // Add new points and update nodeVec - for (int nodeIndex = Netgen_NbOfNodes +1 ; nodeIndex <= Netgen_NbOfNodesNew; ++nodeIndex ) - { - df.read((char *) &Netgen_point, sizeof(double)*3); + // Add new points and update nodeVec + for (int nodeIndex = Netgen_NbOfNodes +1 ; nodeIndex <= Netgen_NbOfNodesNew; ++nodeIndex ) + { + df.read((char *) &Netgen_point, sizeof(double)*3); - nodeVec.at(nodeIndex) = helper.AddNode(Netgen_point[0], - Netgen_point[1], - Netgen_point[2]); - } + nodeVec.at(nodeIndex) = helper.AddNode(Netgen_point[0], + Netgen_point[1], + Netgen_point[2]); + } - // Add tetrahedrons - df.read((char*) &Netgen_NbOfTetra, sizeof(int)); + // Add tetrahedrons + df.read((char*) &Netgen_NbOfTetra, sizeof(int)); - for ( int elemIndex = 1; elemIndex <= Netgen_NbOfTetra; ++elemIndex ) - { - df.read((char*) &Netgen_tetrahedron, sizeof(int)*4); - helper.AddVolume( - nodeVec.at( Netgen_tetrahedron[0] ), - nodeVec.at( Netgen_tetrahedron[1] ), - nodeVec.at( Netgen_tetrahedron[2] ), - nodeVec.at( Netgen_tetrahedron[3] )); + for ( int elemIndex = 1; elemIndex <= Netgen_NbOfTetra; ++elemIndex ) + { + df.read((char*) &Netgen_tetrahedron, sizeof(int)*4); + helper.AddVolume( + nodeVec.at( Netgen_tetrahedron[0] ), + nodeVec.at( Netgen_tetrahedron[1] ), + nodeVec.at( Netgen_tetrahedron[2] ), + nodeVec.at( Netgen_tetrahedron[3] )); + } } - df.close(); auto time7 = std::chrono::high_resolution_clock::now(); - elapsed = std::chrono::duration_cast(time7-time6); + elapsed = std::chrono::duration_cast(time7-time5); std::cout << "Time for exec of add_in_mesh: " << elapsed.count() * 1e-9 << std::endl; fs::remove_all(tmp_folder); diff --git a/src/NETGENPlugin/netgen_mesher.cxx b/src/NETGENPlugin/netgen_mesher.cxx index d5d1cf1..d867f3b 100644 --- a/src/NETGENPlugin/netgen_mesher.cxx +++ b/src/NETGENPlugin/netgen_mesher.cxx @@ -315,23 +315,24 @@ int netgen3d_internal(TopoDS_Shape &aShape, SMESH_Mesh& aMesh, netgen_params& aP } // Get list of elements + their orientation from element_orientation file - std::ifstream df(element_orientation_file, ios::binary|ios::in); - int nbElement; - bool orient; - - // Warning of the use of vtkIdType (I had issue when run_mesher was compiled with internal vtk) and salome not - // Sizeof was the same but how he othered the type was different - // Maybe using another type (uint64_t) instead would be better - vtkIdType id; std::map elemOrientation; - df.read((char*)&nbElement, sizeof(int)); - - for(int ielem=0;ielemelementsIterator(SMDSAbs_Face); @@ -548,7 +549,7 @@ int netgen3d_internal(TopoDS_Shape &aShape, SMESH_Mesh& aMesh, netgen_params& aP double Netgen_point[3]; int Netgen_tetrahedron[4]; - // Writing nodevec (correspondance netgen numbering mesh numbering) + // Writing nodevec (correspondence netgen numbering mesh numbering) // Number of nodes df.write((char*) &Netgen_NbOfNodes, sizeof(int)); df.write((char*) &Netgen_NbOfNodesNew, sizeof(int)); @@ -574,7 +575,6 @@ int netgen3d_internal(TopoDS_Shape &aShape, SMESH_Mesh& aMesh, netgen_params& aP Ng_GetVolumeElement(Netgen_mesh, elemIndex, Netgen_tetrahedron); df.write((char*) &Netgen_tetrahedron, sizeof(int)*4); } - df.close(); } auto time3 = std::chrono::high_resolution_clock::now(); elapsed = std::chrono::duration_cast(time3-time2); @@ -775,18 +775,19 @@ int netgen2d_internal(TopoDS_Shape &aShape, SMESH_Mesh& aMesh, netgen_params& aP occgeoComm.vmap.Clear(); // Reading list of element to integrate into netgen mesh - std::ifstream df(element_orientation_file, ios::in|ios::binary); - int nbElement; - vtkIdType id; - bool orient; - df.read((char*)&nbElement, sizeof(int)); - - for(int ielem=0;ielem