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,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<int> 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);
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<int> 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);
nodeVec.at(nodeIndex) = helper.AddNode(Netgen_point[0],
Netgen_point[1],
Netgen_point[2]);
}
// 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] ));
}
}
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
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]);
}
// 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] ));
}
df.close();
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;
fs::remove_all(tmp_folder);

View File

@ -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<vtkIdType, bool> elemOrientation;
df.read((char*)&nbElement, sizeof(int));
{
std::ifstream df(element_orientation_file, ios::binary|ios::in);
int nbElement;
bool orient;
for(int ielem=0;ielem<nbElement;++ielem){
df.read((char*) &id, sizeof(vtkIdType));
df.read((char*) &orient, sizeof(bool));
elemOrientation[id] = 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;
df.read((char*)&nbElement, sizeof(int));
for(int ielem=0;ielem<nbElement;++ielem){
df.read((char*) &id, sizeof(vtkIdType));
df.read((char*) &orient, sizeof(bool));
elemOrientation[id] = orient;
}
}
df.close();
// Adding elements from Mesh
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];
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<std::chrono::nanoseconds>(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));
{
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<nbElement;++ielem){
df.read((char*) &id, sizeof(vtkIdType));
df.read((char*) &orient, sizeof(bool));
elemOrientation[id] = orient;
for(int ielem=0;ielem<nbElement;++ielem){
df.read((char*) &id, sizeof(vtkIdType));
df.read((char*) &orient, sizeof(bool));
elemOrientation[id] = orient;
}
}
df.close();
bool isIn;
// set local size according to size of existing segments