20    : d_filename(filename){};
 
 
   23                                     std::vector<util::Point> *nodes,
 
   24                                     size_t &element_type, 
size_t &num_elems,
 
   25                                     std::vector<size_t> *enc,
 
   26                                     std::vector<std::vector<size_t>> *nec,
 
   27                                     std::vector<double> *volumes, 
bool is_fd) {
 
   30    std::cerr << 
"Error: Filename = " << d_filename <<
 
   31                 " in MshReader is either nonexistent or empty.\n";
 
   36  if (d_file) d_file.close();
 
   38  d_file.open(d_filename);
 
   41    std::cerr << 
"Error: Can not open file = " << d_filename + 
".msh" 
   58  if (dim != 2 and dim != 3) {
 
   59    std::cerr << 
"Error: MshReader currently only supports reading of " 
   60                 "triangle/quadrangle elements in dimension 2 and tetragonal " 
   65  unsigned int num_nodes_con = 0;
 
   66  bool read_nodes = 
false;
 
   67  bool read_elements = 
false;
 
   70    std::getline(d_file, line);
 
   73      if (line.find(
"$MeshFormat") == 
static_cast<std::string::size_type
>(0)) {
 
   74        d_file >> version >> format >> size;
 
   75        if ((version != 2.0) && (version != 2.1) && (version != 2.2)) {
 
   76          std::cerr << 
"Error: Unknown .msh file version " << version << 
"\n";
 
   83          std::cerr << 
"Error: Format of .msh is possibly binary which is not" 
   84                       " supported currently.\n ";
 
   89      if (line.find(
"$NOD") == 
static_cast<std::string::size_type
>(0) ||
 
   90          line.find(
"$NOE") == 
static_cast<std::string::size_type
>(0) ||
 
   91          line.find(
"$Nodes") == 
static_cast<std::string::size_type
>(0)) {
 
   93        unsigned int num_nodes = 0;
 
   97        nodes->resize(num_nodes);
 
   98        nec->resize(num_nodes);
 
  105        for (
unsigned int i = 0; i < num_nodes; ++i) {
 
  106          d_file >> 
id >> x >> y >> z;
 
  110        std::getline(d_file, line);
 
  113      else if (line.find(
"$ELM") == 
static_cast<std::string::size_type
>(0) ||
 
  114               line.find(
"$Elements") ==
 
  115               static_cast<std::string::size_type
>(0)) {
 
  116        read_elements = 
true;
 
  119        unsigned int num_elem = 0;
 
  120        unsigned int node_id = 0;
 
  130        size_t elem_counter = 0;
 
  131        bool found_tri = 
false;
 
  132        bool found_quad = 
false;
 
  133        bool found_tet = 
false;
 
  134        for (
unsigned int iel = 0; iel < num_elem; ++iel) {
 
  139          d_file >> 
id >> type >> ntags;
 
  140          for (
unsigned int j = 0; j < ntags; j++) d_file >> tag;
 
  143          bool read_this_element = 
false;
 
  148            read_this_element = 
true;
 
  154            read_this_element = 
true;
 
  160            read_this_element = 
true;
 
  168          if (read_this_element) {
 
  170            for (
unsigned int i = 0; i < num_nodes_con; i++) {
 
  174              enc->push_back(node_id - 1);
 
  179              (*nec)[node_id - 1].push_back(elem_counter);
 
  195            for (
unsigned int i = 0; i < n; i++) d_file >> node_id;
 
  200        if (found_quad and found_tri) {
 
  201          std::cerr << 
"Error: Check mesh file. It appears to have both " 
  202                       "quadrangle elements and triangle elements. " 
  203                       "Currently we only support one kind of elements.\n";
 
  208        num_elems = elem_counter;
 
  211        std::getline(d_file, line);
 
  217    if (d_file.eof()) 
break;
 
  219    if (read_nodes and read_elements) 
break;
 
 
  233    std::cerr << 
"Error: Filename = " << d_filename <<
 
  234              " in MshReader is either nonexistent or empty.\n";
 
  239  if (d_file) d_file.close();
 
  241  d_file.open(d_filename);
 
  244    std::cerr << 
"Error: Can not open file = " << d_filename + 
".msh.\n";
 
  251  double version = 1.0;
 
  255  bool read_nodes = 
false;
 
  258    std::getline(d_file, line);
 
  261      if (line.find(
"$MeshFormat") == 
static_cast<std::string::size_type
>(0)) {
 
  262        d_file >> version >> format >> size;
 
  263        if ((version != 2.0) && (version != 2.1) && (version != 2.2)) {
 
  264          std::cerr << 
"Error: Unknown .msh file version " << version << 
"\n";
 
  271          std::cerr << 
"Error: Format of .msh is possibly binary which is not" 
  272                       " supported currently.\n ";
 
  277      if (line.find(
"$NOD") == 
static_cast<std::string::size_type
>(0) ||
 
  278          line.find(
"$NOE") == 
static_cast<std::string::size_type
>(0) ||
 
  279          line.find(
"$Nodes") == 
static_cast<std::string::size_type
>(0)) {
 
  281        unsigned int num_nodes = 0;
 
  285        nodes->resize(num_nodes);
 
  292        for (
unsigned int i = 0; i < num_nodes; ++i) {
 
  293          d_file >> 
id >> x >> y >> z;
 
  297        std::getline(d_file, line);
 
  303    if (d_file.eof()) 
break;
 
  305    if (read_nodes) 
break;
 
 
  317        size_t &num_elems, std::vector<size_t> *enc,
 
  318        std::vector<std::vector<size_t>> *nec) {
 
  321    std::cerr << 
"Error: Filename = " << d_filename <<
 
  322              " in MshReader is either nonexistent or empty.\n";
 
  327  if (d_file) d_file.close();
 
  329  d_file.open(d_filename);
 
  332    std::cerr << 
"Error: Can not open file = " << d_filename + 
".msh" 
  340  double version = 1.0;
 
  347  if (dim != 2 and dim != 3) {
 
  348    std::cerr << 
"Error: MshReader currently only supports reading of " 
  349                 "triangle/quadrangle elements in dimension 2 and tetragonal " 
  354  unsigned int num_nodes_con = 0;
 
  355  bool read_elements = 
false;
 
  358    std::getline(d_file, line);
 
  361      if (line.find(
"$MeshFormat") == 
static_cast<std::string::size_type
>(0)) {
 
  362        d_file >> version >> format >> size;
 
  363        if ((version != 2.0) && (version != 2.1) && (version != 2.2)) {
 
  364          std::cerr << 
"Error: Unknown .msh file version " << version << 
"\n";
 
  371          std::cerr << 
"Error: Format of .msh is possibly binary which is not" 
  372                       " supported currently.\n ";
 
  377      if (line.find(
"$ELM") == 
static_cast<std::string::size_type
>(0) ||
 
  378               line.find(
"$Elements") ==
 
  379               static_cast<std::string::size_type
>(0)) {
 
  380        read_elements = 
true;
 
  383        unsigned int num_elem = 0;
 
  384        unsigned int node_id = 0;
 
  394        size_t elem_counter = 0;
 
  395        bool found_tri = 
false;
 
  396        bool found_quad = 
false;
 
  397        bool found_tet = 
false;
 
  398        for (
unsigned int iel = 0; iel < num_elem; ++iel) {
 
  403          d_file >> 
id >> type >> ntags;
 
  404          for (
unsigned int j = 0; j < ntags; j++) d_file >> tag;
 
  407          bool read_this_element = 
false;
 
  412            read_this_element = 
true;
 
  418            read_this_element = 
true;
 
  424            read_this_element = 
true;
 
  432          if (read_this_element) {
 
  434            for (
unsigned int i = 0; i < num_nodes_con; i++) {
 
  438              enc->push_back(node_id - 1);
 
  441              (*nec)[node_id - 1].push_back(elem_counter);
 
  450            for (
unsigned int i = 0; i < n; i++) d_file >> node_id;
 
  455        if (found_quad and found_tri) {
 
  456          std::cerr << 
"Error: Check mesh file. It appears to have both " 
  457                       "quadrangle elements and triangle elements. " 
  458                       "Currently we only support one kind of elements.\n";
 
  463        num_elems = elem_counter;
 
  466        std::getline(d_file, line);
 
  472    if (d_file.eof()) 
break;
 
  474    if (read_elements) 
break;
 
 
  482                                          std::vector<util::Point> *data) {
 
  484  if (!d_file) d_file = std::ifstream(d_filename);
 
  488      std::cerr << 
"Error: Can not open file = " << d_filename + 
".msh" 
  493  bool found_data = 
false;
 
  496    std::getline(d_file, line);
 
  499      if (line.find(
"$NodeData") == 
static_cast<std::string::size_type
>(0)) {
 
  503        std::string tag[num_tags];
 
  504        for (
size_t i = 0; i < num_tags; i++) d_file >> tag[i];
 
  508        double real_tag = 0.;
 
  514        d_file >> tag_number >> field_type >> num_data;
 
  517        if (tag[0] == name) {
 
  519          if (field_type != 3) {
 
  520            std::cerr << 
"Error: Data " << tag[0] << 
" is of type " 
  521                      << field_type << 
" but we expect it to be of type " << 3
 
  527          data->resize(num_data);
 
  531        for (
size_t i = 0; i < num_data; i++) {
 
  532          double d[field_type];
 
  533          for (
size_t j = 0; j < field_type; j++) d_file >> d[j];
 
  535          if (found_data) (*data)[i] = 
util::Point(d[0], d[1], d[2]);
 
  538        std::getline(d_file, line);
 
  542    if (found_data) 
break;
 
  546    if (d_file.eof()) 
break;
 
 
  554                                          std::vector<double> *data) {
 
  556  if (!d_file) d_file = std::ifstream(d_filename);
 
  560      std::cerr << 
"Error: Can not open file = " << d_filename + 
".msh" 
  565  bool found_data = 
false;
 
  568    std::getline(d_file, line);
 
  571      if (line.find(
"$NodeData") == 
static_cast<std::string::size_type
>(0)) {
 
  575        std::string tag[num_tags];
 
  576        for (
size_t i = 0; i < num_tags; i++) d_file >> tag[i];
 
  580        double real_tag = 0.;
 
  586        d_file >> tag_number >> field_type >> num_data;
 
  589        if (tag[0] == name) {
 
  591          if (field_type != 1) {
 
  592            std::cerr << 
"Error: Data " << tag[0] << 
" is of type " 
  593                      << field_type << 
" but we expect it to be of type " << 1
 
  599          data->resize(num_data);
 
  603        for (
size_t i = 0; i < num_data; i++) {
 
  604          double d[field_type];
 
  605          for (
size_t j = 0; j < field_type; j++) d_file >> d[j];
 
  607          if (found_data) (*data)[i] = d[0];
 
  610        std::getline(d_file, line);
 
  614    if (found_data) 
break;
 
  618    if (d_file.eof()) 
break;
 
 
void readMesh(size_t dim, std::vector< util::Point > *nodes, size_t &element_type, size_t &num_elems, std::vector< size_t > *enc, std::vector< std::vector< size_t > > *nec, std::vector< double > *volumes, bool is_fd=false)
Reads mesh data into node file and element file.
 
void readCells(size_t dim, size_t &element_type, size_t &num_elems, std::vector< size_t > *enc, std::vector< std::vector< size_t > > *nec)
Reads cell data, i.e. element-node connectivity and node-element connectivity.
 
bool readPointData(const std::string &name, std::vector< util::Point > *data)
reads point data from .vtu file
 
void close()
Close the file.
 
MshReader(const std::string &filename)
Constructor.
 
void readNodes(std::vector< util::Point > *nodes)
Reads nodal position.
 
static const int msh_type_quadrangle
Integer flag for quadrangle element.
 
static const int vtk_type_triangle
Integer flag for triangle element.
 
static const int vtk_type_quad
Integer flag for quad element.
 
static const int vtk_type_tetra
Integer flag for tetrahedron element.
 
static int msh_map_element_to_num_nodes[16]
Map from element type to number of nodes (for msh)
 
static const int msh_type_triangle
Integer flag for triangle element.
 
static const int msh_type_tetrahedron
Integer flag for tetrahedron element.
 
bool isFileEmpty(std::string filename)
Check if file is empty or null.
 
A structure to represent 3d vectors.