Using RAII to remove file.close

This commit is contained in:
Yoann Audouin 2022-09-13 14:33:19 +02:00
parent a71af09b16
commit 2e0b7c8b58
2 changed files with 92 additions and 94 deletions

View File

@ -279,6 +279,7 @@ void NETGENPlugin_NETGEN_3D::exportElementOrientation(SMESH_Mesh& aMesh,
} // loop on elements on a face } // loop on elements on a face
} // loop on faces of a SOLID or SHELL } // loop on faces of a SOLID or SHELL
{
std::ofstream df(output_file, ios::out|ios::binary); std::ofstream df(output_file, ios::out|ios::binary);
int size=elemOrientation.size(); int size=elemOrientation.size();
@ -287,7 +288,7 @@ void NETGENPlugin_NETGEN_3D::exportElementOrientation(SMESH_Mesh& aMesh,
df.write((char*)&id, sizeof(vtkIdType)); df.write((char*)&id, sizeof(vtkIdType));
df.write((char*)&orient, sizeof(bool)); df.write((char*)&orient, sizeof(bool));
} }
df.close(); }
} }
int NETGENPlugin_NETGEN_3D::RemoteCompute(SMESH_Mesh& aMesh, 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; //std::cout << cmd << std::endl;
// Writing command in log // Writing command in log
{
std::ofstream flog(log_file.string()); std::ofstream flog(log_file.string());
flog << cmd << endl; flog << cmd << endl;
flog.close(); }
// TODO: Replace system by something else to handle redirection for windows // TODO: Replace system by something else to handle redirection for windows
int ret = system(cmd.c_str()); int ret = system(cmd.c_str());
auto time5 = std::chrono::high_resolution_clock::now(); auto time5 = std::chrono::high_resolution_clock::now();
@ -382,6 +383,7 @@ int NETGENPlugin_NETGEN_3D::RemoteCompute(SMESH_Mesh& aMesh,
} }
aMesh.Lock(); aMesh.Lock();
{
std::ifstream df(new_element_file.string(), ios::binary); std::ifstream df(new_element_file.string(), ios::binary);
int Netgen_NbOfNodes; int Netgen_NbOfNodes;
@ -412,11 +414,6 @@ int NETGENPlugin_NETGEN_3D::RemoteCompute(SMESH_Mesh& aMesh,
nodeVec.at(nodeIndex) = meshDS->FindNode(nodeID); nodeVec.at(nodeIndex) = meshDS->FindNode(nodeID);
} }
auto time6 = std::chrono::high_resolution_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(time6-time5);
std::cout << "Time for exec of nodeVec: " << elapsed.count() * 1e-9 << std::endl;
// Add new points and update nodeVec // Add new points and update nodeVec
for (int nodeIndex = Netgen_NbOfNodes +1 ; nodeIndex <= Netgen_NbOfNodesNew; ++nodeIndex ) for (int nodeIndex = Netgen_NbOfNodes +1 ; nodeIndex <= Netgen_NbOfNodesNew; ++nodeIndex )
{ {
@ -439,9 +436,9 @@ int NETGENPlugin_NETGEN_3D::RemoteCompute(SMESH_Mesh& aMesh,
nodeVec.at( Netgen_tetrahedron[2] ), nodeVec.at( Netgen_tetrahedron[2] ),
nodeVec.at( Netgen_tetrahedron[3] )); nodeVec.at( Netgen_tetrahedron[3] ));
} }
df.close(); }
auto time7 = std::chrono::high_resolution_clock::now(); auto time7 = std::chrono::high_resolution_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(time7-time6); elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(time7-time5);
std::cout << "Time for exec of add_in_mesh: " << elapsed.count() * 1e-9 << std::endl; std::cout << "Time for exec of add_in_mesh: " << elapsed.count() * 1e-9 << std::endl;
fs::remove_all(tmp_folder); fs::remove_all(tmp_folder);

View File

@ -315,6 +315,8 @@ int netgen3d_internal(TopoDS_Shape &aShape, SMESH_Mesh& aMesh, netgen_params& aP
} }
// Get list of elements + their orientation from element_orientation file // Get list of elements + their orientation from element_orientation file
std::map<vtkIdType, bool> elemOrientation;
{
std::ifstream df(element_orientation_file, ios::binary|ios::in); std::ifstream df(element_orientation_file, ios::binary|ios::in);
int nbElement; int nbElement;
bool orient; bool orient;
@ -323,7 +325,6 @@ int netgen3d_internal(TopoDS_Shape &aShape, SMESH_Mesh& aMesh, netgen_params& aP
// Sizeof was the same but how he othered the type was different // Sizeof was the same but how he othered the type was different
// Maybe using another type (uint64_t) instead would be better // Maybe using another type (uint64_t) instead would be better
vtkIdType id; vtkIdType id;
std::map<vtkIdType, bool> elemOrientation;
df.read((char*)&nbElement, sizeof(int)); df.read((char*)&nbElement, sizeof(int));
for(int ielem=0;ielem<nbElement;++ielem){ for(int ielem=0;ielem<nbElement;++ielem){
@ -331,7 +332,7 @@ int netgen3d_internal(TopoDS_Shape &aShape, SMESH_Mesh& aMesh, netgen_params& aP
df.read((char*) &orient, sizeof(bool)); df.read((char*) &orient, sizeof(bool));
elemOrientation[id] = orient; elemOrientation[id] = orient;
} }
df.close(); }
// Adding elements from Mesh // Adding elements from Mesh
SMDS_ElemIteratorPtr iteratorElem = meshDS->elementsIterator(SMDSAbs_Face); SMDS_ElemIteratorPtr iteratorElem = meshDS->elementsIterator(SMDSAbs_Face);
@ -548,7 +549,7 @@ int netgen3d_internal(TopoDS_Shape &aShape, SMESH_Mesh& aMesh, netgen_params& aP
double Netgen_point[3]; double Netgen_point[3];
int Netgen_tetrahedron[4]; int Netgen_tetrahedron[4];
// Writing nodevec (correspondance netgen numbering mesh numbering) // Writing nodevec (correspondence netgen numbering mesh numbering)
// Number of nodes // Number of nodes
df.write((char*) &Netgen_NbOfNodes, sizeof(int)); df.write((char*) &Netgen_NbOfNodes, sizeof(int));
df.write((char*) &Netgen_NbOfNodesNew, 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); Ng_GetVolumeElement(Netgen_mesh, elemIndex, Netgen_tetrahedron);
df.write((char*) &Netgen_tetrahedron, sizeof(int)*4); df.write((char*) &Netgen_tetrahedron, sizeof(int)*4);
} }
df.close();
} }
auto time3 = std::chrono::high_resolution_clock::now(); auto time3 = std::chrono::high_resolution_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(time3-time2); elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(time3-time2);
@ -775,6 +775,7 @@ int netgen2d_internal(TopoDS_Shape &aShape, SMESH_Mesh& aMesh, netgen_params& aP
occgeoComm.vmap.Clear(); occgeoComm.vmap.Clear();
// Reading list of element to integrate into netgen mesh // Reading list of element to integrate into netgen mesh
{
std::ifstream df(element_orientation_file, ios::in|ios::binary); std::ifstream df(element_orientation_file, ios::in|ios::binary);
int nbElement; int nbElement;
vtkIdType id; vtkIdType id;
@ -786,7 +787,7 @@ int netgen2d_internal(TopoDS_Shape &aShape, SMESH_Mesh& aMesh, netgen_params& aP
df.read((char*) &orient, sizeof(bool)); df.read((char*) &orient, sizeof(bool));
elemOrientation[id] = orient; elemOrientation[id] = orient;
} }
df.close(); }
bool isIn; bool isIn;
// set local size according to size of existing segments // set local size according to size of existing segments