nschloe / meshio

:spider_web: input/output for many mesh formats
MIT License
1.96k stars 402 forks source link

[BUG] tetgen .ele attributes are REAL #1318

Open tricaric opened 2 years ago

tricaric commented 2 years ago

When converting a tetgen mesh to another format (vtk in my case), meshio reads the .ele file setting dtype=int, but the attribute columns are of type REAL as defined in tetgen.h code of the tetgen library.

To Reproduce For example, with mesh.node as:

26  3  0  0
   0    0  -0.52573111211900003  -0.85065080835200002
   1    0  0.52573111211900003  -0.85065080835200002
   2    0  -0.52573111211900003  0.85065080835200002
   3    0  0.52573111211900003  0.85065080835200002
   4    -0.52573111211900003  -0.85065080835200002  0
   5    0.52573111211900003  -0.85065080835200002  0
   6    -0.52573111211900003  0.85065080835200002  0
   7    0.52573111211900003  0.85065080835200002  0
   8    -0.85065080835200002  0  -0.52573111211900003
   9    -0.85065080835200002  0  0.52573111211900003
  10    0.85065080835200002  0  -0.52573111211900003
  11    0.85065080835200002  0  0.52573111211900003
  12    -0.097092233456921501  -0.047082660031440328  0.0047358453536701373
  13    0.68819096023550008  0.42532540417600001  0.26286555605950002
  14    -0.68819096023550008  -0.42532540417600001  0.26286555605950002
  15    0  0  -0.85065080835200002
  16    -0.68819096023550008  0.42532540417600001  -0.26286555605950002
  17    -0.68819096023550008  0.42532540417600001  0.26286555605950002
  18    -0.26286555605950002  -0.68819096023550008  0.42532540417600001
  19    0.42532540417600001  -0.26286555605950002  0.68819096023550008
  20    0  0.85065080835200002  0
  21    -0.42532540417600001  -0.26286555605950002  0.68819096023550008
  22    -0.68819096023550008  -0.42532540417600001  -0.26286555605950002
  23    0.26286555605950002  -0.68819096023550008  -0.42532540417600001
  24    0.26286555605950002  0.68819096023550008  -0.42532540417600001
  25    0.68819096023550008  -0.42532540417600001  0.26286555605950002

and mesh.ele as:

64  4  1
    0       6    24    16     1    1.1
    1      10    12    15    23    1.2
    2       3    12    19    21    1.3
    3      16    20    17    12    1.4
    4       9    12    17    21    1.5
    5      20    13    24     7    1
    6      12     2    19    21    1
    7      12    14    18    21    1
    8      12    16    20    24    1
    9      19    13    25    12    1
   10       3    12    17    20    1
   11      12     9    14    21    1
   12       5    18    19    25    1
   13      12     4    14    22    1
   14      20    16    17     6    1
   15      12     0    15    23    1
   16      12     5    23    25    1
   17      12    10    15    24    1
   18       0    12    15    22    1
   19      12     8    15    22    1
   20      14     8    16    22    1
   21       4    12    14    18    1
   22       4    12    18    23    1
   23      12     1    16    24    1
   24      12     5    18    23    1
   25      12     3    17    21    1
   26       5    12    18    25    1
   27      10    12    13    24    1
   28       8    17    14    16    1
   29      12     1    15    16    1
   30       8    12    15    16    1
   31       8    17     9    14    1
   32       1     8    15    16    1
   33       9    12    14    17    1
   34      14    12    16    17    1
   35      18    12    19    25    1
   36      12     2    18    19    1
   37      13    19    25    11    1
   38       3    12    13    19    1
   39       2     5    18    19    1
   40      11     3    13    19    1
   41      12     3    13    20    1
   42      13    20    24    12    1
   43       6    24    20    16    1
   44       6     3    17    20    1
   45       3     7    13    20    1
   46       2    12    18    21    1
   47       3     9    17    21    1
   48       2     3    19    21    1
   49      12    14    16    22    1
   50       8    12    16    22    1
   51       8     0    15    22    1
   52      12     4    22    23    1
   53       0    12    22    23    1
   54       0    10    15    23    1
   55       5     4    18    23    1
   56       4     0    22    23    1
   57       1    12    15    24    1
   58       7    10    13    24    1
   59      10     1    15    24    1
   60      10    12    23    25    1
   61      12    10    13    25    1
   62       5    10    23    25    1
   63      10    11    13    25    1

when you run: meshio convert mesh.node mesh.vtk the output is:

  File "/usr/local/bin/meshio", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/site-packages/meshio/_cli/_main.py", line 52, in main
    return args.func(args)
  File "/usr/local/lib/python3.9/site-packages/meshio/_cli/_convert.py", line 53, in convert
    mesh = read(args.infile, file_format=args.input_format)
  File "/usr/local/lib/python3.9/site-packages/meshio/_helpers.py", line 71, in read
    return _read_file(Path(filename), file_format)
  File "/usr/local/lib/python3.9/site-packages/meshio/_helpers.py", line 103, in _read_file
    return reader_map[file_format](str(path))
  File "/usr/local/lib/python3.9/site-packages/meshio/tetgen/_tetgen.py", line 74, in read
    cells = np.fromfile(
ValueError: cannot reshape array of size 6 into shape (64,6)

Diagnose The relevant meshio code seems to be at line 75 of meshio/tetgen/_tetgen.py. A quick fix seems to be to remove "dtype=int" from that line, but perhaps that is not entirely correct and a better fix is needed.

Thanks!

nschloe commented 2 years ago

The format specification says that the data is int:

If the region attribute in the first line is '1', each tetrahedra has an additional region attribute (an integer) in the last column.

tricaric commented 2 years ago

In tetgen.h (v1.6.0) we have (line 210 and following):

// 'tetrahedronattributelist': An array of tetrahedron attributes. Each // tetrahedron's attributes occupy 'numberoftetrahedronattributes' REALs.

and then:

REAL *tetrahedronattributelist; int numberoftetrahedronattributes;

The tetrahedron attributes can be used to define regions, in which case it can take integer values, but in general, and for any positive number of attributes, the attributes can take REAL values, as appropriate for general physical quantities. Importing the converted vtk mesh example into ParaView (after removing the "dtype=int" as described above) confirms that the tetrahedron attribute is a double with values in [1, 1.5].