Commit dcf121d2 authored by Jonathan Lambrechts's avatar Jonathan Lambrechts Committed by Valentin Vallaeys
Browse files

dgMesh : load msh format 3

parent 4a6899f6
......@@ -357,14 +357,9 @@ static void buildInterfaces(const dgMesh &mesh, LoadMesh &lmesh, std::vector<dgI
#define ferror Msg::Fatal("Invalid file format (%s) %i", fileName, __LINE__);
#include <iostream>
static void loadMSH(const char *fileName, LoadMesh &mesh, bool sequentialLoad)
static void loadMSH2(const char *fileName, std::ifstream &input, LoadMesh &mesh, bool sequentialLoad)
{
mesh.dimension = -1;
std::ifstream input(fileName);
std::string line;
getline (input, line);
getline (input, line);
getline (input, line);
if (!(input>>line)) ferror;
if (line=="$PhysicalNames"){
int nphys;
......@@ -459,6 +454,141 @@ static void loadMSH(const char *fileName, LoadMesh &mesh, bool sequentialLoad)
}
}
static void loadMSH3(const char *fileName, std::ifstream &input, LoadMesh &mesh, bool sequentialLoad)
{
std::string line;
if (!(input>>line)) ferror;
if (line=="$PhysicalNames"){
int nphys;
if (!(input>>nphys)) ferror;
getline(input, line);
for (int i = 0; i < nphys; ++i) {
int tag, dim;
std::string pname;
if (!(input >> dim >> tag)) ferror;
getline(input, line);
auto it1 = line.find('\"'); if (it1 == line.npos) ferror;
auto it2 = line.rfind('\"'); if (it2 == line.npos) ferror;
pname = line.substr(it1+1,it2-it1-1);
mesh.physicalTags[std::make_pair(dim,tag)] = pname;
}
if (!(input>>line)) ferror;
if (line!="$EndPhysicalNames") ferror;
}
if (!(input>>line)) ferror;
if (line!="$Entities") ferror;
int nent[4];
std::map<int, int> ent2phys[4];
if(!(input>>nent[0]>>nent[1]>>nent[2]>>nent[3])) ferror;
for(int dim = 0; dim < 4; ++dim) {
for (int i = 0; i < nent[dim]; ++i) {
int tag, nsub, nphys;
if (!(input >> tag >> nsub)) ferror;
for (int j = 0; j < nsub; ++j){
int buf;
if (!(input >> buf)) ferror;
}
if (!(input >> nphys)) ferror;
if (nphys>=1)
if (!(input >> ent2phys[dim][tag])) ferror;
getline(input, line);
}
}
getline(input, line);
if (line!="$EndEntities") ferror;
getline (input, line);
if (line!="$Nodes") ferror;
int nnodes;
if(!(input>>nnodes)) ferror;
std::map<int, Vertex> vmap;
for (int i = 0; i < nnodes; i++) {
int vid;
Vertex v;
if (!(input >> vid >> v.x >> v.y >> v.z)) ferror;
getline (input, line);
v.id = -1;
vmap[vid] = v;
}
getline (input, line);
if (line!="$EndNodes") ferror;
getline (input, line);
if (line!="$Elements") ferror;
int nelements;
if(!(input >> nelements)) ferror;
getline (input, line);
int cid = 0;
for (int i = 0; i < nelements; i++) {
std::vector<int> evertices;
int elementType, entity, ntags, num;
if (!(input >> num >> elementType >> entity >> ntags)) ferror;
std::vector<int> tags(ntags);
for (int j = 0; j < ntags; ++j)
if (!(input >> tags[j])) ferror;
int part = 0;
auto *fs = BasisFactory::getNodalBasis(elementType);
int nNodes = fs->points.size1();
bool keep = true;
if (ntags - nNodes > 0) {
int npart = tags[nNodes];
if (npart > 0)
keep = sequentialLoad;
for (int ipart = 0; ipart < npart; ++ipart) {
int tmp = tags[nNodes = ipart];
if (tmp > 0 && !sequentialLoad)
part = tmp-1;
if (tmp < 0)
tmp = -tmp;
keep |= (tmp-1 == Msg::GetCommRank());
}
}
if (fs->dimension > mesh.dimension)
mesh.dimension = fs->dimension;
evertices.reserve(nNodes);
for (int j = 0; j < nNodes; ++j) {
Vertex &v = vmap[tags[j]];
if (v.id == -1 && keep)
v.id = cid++;
evertices.push_back(v.id);
}
if (keep) {
ElementKey key = {elementType, ent2phys[fs->dimension][entity], part};
mesh.elements[key].push_back(MElement(evertices, num));
}
}
if (!(input >> line)) ferror;
if (line!="$EndElements") ferror;
mesh.nodes.resize(cid, 3);
for (auto &it : vmap) {
Vertex &v = it.second;
if (v.id != -1) {
mesh.mshToVertexId[it.first] = v.id;
mesh.nodes(v.id, 0) = v.x;
mesh.nodes(v.id, 1) = v.y;
mesh.nodes(v.id, 2) = v.z;
}
}
}
static void loadMSH(const char *fileName, LoadMesh &mesh, bool sequentialLoad)
{
mesh.dimension = -1;
std::ifstream input(fileName);
std::string line;
getline (input, line);
if(line != "$MeshFormat") ferror;
double format[3];
input >> format[0] >> format[1] >> format[2];
getline (input, line);
getline (input, line);
if(line != "$EndMeshFormat") ferror;
if ((int)format[0] == 2)
loadMSH2(fileName, input, mesh, sequentialLoad);
else if ((int)format[0] == 3)
loadMSH3(fileName, input, mesh, sequentialLoad);
else
Msg::Fatal("invalid mesh file format %s.", fileName);
}
static dgFullMatrix<double> copyCoordinates(const std::vector<MElement> &elements, const fullMatrix<double> &vcoord) {
if(elements.empty())
return dgFullMatrix<double>();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment