diff --git a/libsrc/interface/nginterface.cpp b/libsrc/interface/nginterface.cpp index 2a340281..58d957f0 100644 --- a/libsrc/interface/nginterface.cpp +++ b/libsrc/interface/nginterface.cpp @@ -2,7 +2,7 @@ #include #include - +#include #ifdef SOCKETS #include "../sockets/sockets.hpp" @@ -17,6 +17,9 @@ namespace netgen { DLL_HEADER MeshingParameters mparam; + + /** Force linking of geom2d library (for SplineGeometryRegister)**/ + SplineGeometry2d dummy_2dgeom; } static std::thread meshingthread; @@ -118,116 +121,154 @@ void Ng_LoadMeshFromStream ( istream & input ) void Ng_LoadMesh (const char * filename) { - { - ifstream infile(filename); - if(!infile.good()) - throw NgException(string("Error opening file ") + filename); - } #ifdef PARALLEL MPI_Comm_size(MPI_COMM_WORLD, &ntasks); MPI_Comm_rank(MPI_COMM_WORLD, &id); - - if (id == 0) - { #endif - if ( string(filename).find(".vol") == string::npos ) - /* - if ( (strlen (filename) > 4) && - strcmp (filename + (strlen (filename)-4), ".vol") != 0 ) - */ - { - mesh.reset (new Mesh()); - ReadFile(*mesh,filename); - - //mesh->SetGlobalH (mparam.maxh); - //mesh->CalcLocalH(); - return; - } - string fn(filename); + { + ifstream infile(filename); + if(!infile.good()) + throw NgException(string("Error opening file ") + filename); + } - istream * infile; - if (fn.substr (fn.length()-3, 3) == ".gz") - infile = new igzstream (filename); - else - infile = new ifstream (filename); - - Ng_LoadMeshFromStream(*infile); - delete infile; - + if ( string(filename).find(".vol") == string::npos ) + { #ifdef PARALLEL - if (ntasks > 1) - { - - char * weightsfilename = new char [strlen(filename)+1]; - strcpy (weightsfilename, filename); - weightsfilename[strlen (weightsfilename)-3] = 'w'; - weightsfilename[strlen (weightsfilename)-2] = 'e'; - weightsfilename[strlen (weightsfilename)-1] = 'i'; - - ifstream weightsfile(weightsfilename); - delete [] weightsfilename; - - if (!(weightsfile.good())) - { - // cout << "regular distribute" << endl; - mesh -> Distribute(); - } - else - { - char str[20]; - bool endfile = false; - int n, dummy; - - Array segment_weights; - Array surface_weights; - Array volume_weights; - - while (weightsfile.good() && !endfile) - { - weightsfile >> str; - - if (strcmp (str, "edgeweights") == 0) - { - weightsfile >> n; - segment_weights.SetSize(n); - for (int i = 0; i < n; i++) - weightsfile >> dummy >> segment_weights[i]; - } - - if (strcmp (str, "surfaceweights") == 0) - { - weightsfile >> n; - surface_weights.SetSize(n); - for (int i=0; i> dummy >> surface_weights[i]; - } - - if (strcmp (str, "volumeweights") == 0) - { - weightsfile >> n; - volume_weights.SetSize(n); - for (int i=0; i> dummy >> volume_weights[i]; - } - - if (strcmp (str, "endfile") == 0) - endfile = true; - } - - mesh -> Distribute(volume_weights, surface_weights, segment_weights); - } - } - } - else - { - mesh.reset (new Mesh()); -// vssolution.SetMesh(mesh); -// vsmesh.SetMesh(mesh); - SetGlobalMesh (mesh); - mesh->SendRecvMesh(); - } + if(ntasks>1) + throw NgException("Not sure what to do with this?? Does this work with MPI??"); #endif + mesh.reset (new Mesh()); + ReadFile(*mesh,filename); + //mesh->SetGlobalH (mparam.maxh); + //mesh->CalcLocalH(); + return; + } + + istream * infile; + char* buf; // for distributing geometry! + int strs; + + #ifdef PARALLEL + if( id == 0) { + #endif + + string fn(filename); + if (fn.substr (fn.length()-3, 3) == ".gz") + infile = new igzstream (filename); + else + infile = new ifstream (filename); + mesh.reset (new Mesh()); + mesh -> Load(*infile); + + // make string from rest of file (for geometry info!) + if(!ng_geometry) { + stringstream geom_part; + geom_part << infile->rdbuf(); + string geom_part_string = geom_part.str(); + strs = geom_part_string.size(); + buf = new char[strs]; + memcpy(buf, geom_part_string.c_str(), strs*sizeof(char)); + } + delete infile; + +#ifdef PARALLEL + if (ntasks > 1) + { + + char * weightsfilename = new char [strlen(filename)+1]; + strcpy (weightsfilename, filename); + weightsfilename[strlen (weightsfilename)-3] = 'w'; + weightsfilename[strlen (weightsfilename)-2] = 'e'; + weightsfilename[strlen (weightsfilename)-1] = 'i'; + + ifstream weightsfile(weightsfilename); + delete [] weightsfilename; + + if (!(weightsfile.good())) + { + // cout << "regular distribute" << endl; + mesh -> Distribute(); + } + else + { + char str[20]; + bool endfile = false; + int n, dummy; + + Array segment_weights; + Array surface_weights; + Array volume_weights; + + while (weightsfile.good() && !endfile) + { + weightsfile >> str; + + if (strcmp (str, "edgeweights") == 0) + { + weightsfile >> n; + segment_weights.SetSize(n); + for (int i = 0; i < n; i++) + weightsfile >> dummy >> segment_weights[i]; + } + + if (strcmp (str, "surfaceweights") == 0) + { + weightsfile >> n; + surface_weights.SetSize(n); + for (int i=0; i> dummy >> surface_weights[i]; + } + + if (strcmp (str, "volumeweights") == 0) + { + weightsfile >> n; + volume_weights.SetSize(n); + for (int i=0; i> dummy >> volume_weights[i]; + } + + if (strcmp (str, "endfile") == 0) + endfile = true; + } + + mesh -> Distribute(volume_weights, surface_weights, segment_weights); + } + } // ntasks>1 end + } // id==0 end + else { + mesh.reset (new Mesh()); + SetGlobalMesh (mesh); + mesh->SendRecvMesh(); + } + + if(!ng_geometry && ntasks>1) { + /** Scatter the geometry-string **/ + MPI_Bcast(&strs, 1, MPI_INT, 0, MPI_COMM_WORLD); + if(id!=0) buf = new char[strs]; + MPI_Bcast(buf, strs, MPI_CHAR, 0, MPI_COMM_WORLD); + } +#endif + + if(!ng_geometry) { + infile = new istringstream(string((const char*)buf, (size_t)strs)); + delete[] buf; + for (int i = 0; i < geometryregister.Size(); i++) + { + NetgenGeometry * hgeom = geometryregister[i]->LoadFromMeshFile (*infile); + if (hgeom) + { + ng_geometry.reset (hgeom); + mesh->SetGeometry(ng_geometry); + break; + } + } + } + /** Dummy Geometry if we still could not find any geometry info! **/ + // if (!ng_geometry) + // ng_geometry = make_shared(); + if(ng_geometry) + mesh->SetGeometry(ng_geometry); } void Ng_LoadMeshFromString (const char * mesh_as_string)