Closed cashTangoTangoCash closed 6 years ago
I need to write the mesh to a .msh file because plotMesh is not working (code appears to be absent in fipy/fipy/viewers/viewer.py develop branch 30594513891f217c62cf03736e1b099dc3e23f7b):
def plotMesh(self, filename=None):
"""
Display a representation of the mesh
:Parameters:
filename
If not `None`, the name of a file to save the image into.
"""
pass
plotMesh is not working in the two fipy diffusion examples where it appears (circle and
Also I cannot find a (non-broken) way to export a fipy mesh to a .msh file via fipy commands. So, to see my mesh and make sure it looks good, I need to write to a .msh file via meshio and then use gmsh gui. The mesh is nonuniform and experimental and I have to see it.
This is what pygmsh and meshio look like:
import pygmsh
points, cells, point_data, cell_data, field_data = pygmsh.generate_mesh(geom)
import meshio
mesh=meshio.Mesh(points=points,cells=cells,point_data=point_data,cell_data=cell_data,field_data=field_data)
meshio.write(filename,mesh)
Thanks.
examples.diffusion.anisotropy is working.
anisotropy.msh might be an older format .msh file?
github will not allow me to attach the offending .msh file.
Sorry, I should have done this right away. Here's code that produces the bug. The mesh in this case is a super simple 2D rectangle with uniform cell size. I am afraid github is goofing up the indentation in what I pasted.
from fipy import (
Variable,
CellVariable,
Grid2D,
TransientTerm,
DiffusionTerm,
ImplicitSourceTerm,
Viewer,
Matplotlib2DGridViewer,
Matplotlib2DViewer,
)
from fipy.tools import numerix
from fipy import Gmsh2D
import pygmsh
import meshio
import numpy as np
def make_mesh(sideLength, cellSizeCoarse, writeMeshToFile, filename):
geom = pygmsh.built_in.Geometry()
# draw an outer box and assign coarse mesh size
poly = geom.add_polygon(
[
[0., 0., 0.],
[sideLength, 0., 0.],
[sideLength, sideLength, 0.],
[0., sideLength, 0.],
],
lcar=cellSizeCoarse,
)
if writeMeshToFile:
points, cells, point_data, cell_data, field_data = pygmsh.generate_mesh(geom)
mesh = meshio.Mesh(
points=points,
cells=cells,
point_data=point_data,
cell_data=cell_data,
field_data=field_data,
)
meshio.write(filename, mesh)
mesh = Gmsh2D(arg=filename) # the fipy mesh
else:
mesh = Gmsh2D("\n".join(geom._GMSH_CODE)) # the fipy mesh
return geom, mesh
def main():
L = 500 * .025 # side length of square domain from examples.phase.anisotropy
cellSizeCoarse = L / 20. # SETTING
geom, mesh = make_mesh(
sideLength=L,
cellSizeCoarse=cellSizeCoarse,
writeMeshToFile=True,
filename="anisotropy_adaptive_initial_mesh.msh",
)
if __name__ == "__main__":
main()
reformatted example using black and wrapped in triple-backticks to render the code block properly
Sorry, the title of this issue should read:
made mesh with pygmsh; wrote mesh to .msh with meshio; Gmsh2D cannot parse .msh file
I realize Grid2D is not going to read in a .msh file that is an unstructured mesh of triangular cells.
meshio.write(filename, mesh)
outputs a MSH binary file, which FiPy doesn't know how to read. Changing this to meshio.write(filename, mesh, file_format="gmsh-ascii")
allows your script to run successfully.
Also I cannot find a (non-broken) way to export a fipy mesh to a .msh file via fipy commands
Try:
from fipy.meshes.gmshMesh import openMSHFile
f = openMSHFile(name="path/to/my.msh", mode='w')
f.write(mesh)
f.close()
pygmsh seems like an essential tool to make more complicated meshes for fipy.
if I make a mesh with pygmsh and write that mesh to .msh file via meshio, fipy Gmsh2D is failing to parse the resulting .msh file (2D problem). The .msh file is readable by the gmsh gui and the mesh looks completely correct. I tried to edit the following terminal output a little bit for clarity:
(FIPY2018071101) username@username-desktop:~/Documents$ python anisotropy_adaptive20180804.py --inline
first pygmsh is making the mesh
Info : Running 'gmsh -3 /tmp/tmp8qE0FG.geo -format msh -bin -o /tmp/tmpAhUlnA.msh' [Gmsh 3.0.6, 1 node, max. 1 thread] Info : Started on Sat Aug 4 17:03:51 2018 Info : Reading '/tmp/tmp8qE0FG.geo'... Info : Done reading '/tmp/tmp8qE0FG.geo' Info : Finalized high order topology of periodic connections Info : Meshing 1D... Info : Meshing curve 1 (Line) Info : Meshing curve 2 (Line) Info : Meshing curve 3 (Line) Info : Meshing curve 4 (Line) Info : Done meshing 1D (0.000565 s) Info : Meshing 2D... Info : Meshing surface 6 (Plane, Delaunay) Info : Done meshing 2D (0.119492 s) Info : Meshing 3D... Info : Done meshing 3D (4e-06 s) Info : 1891 vertices 3783 elements Info : Writing '/tmp/tmpAhUlnA.msh'... Info : Done writing '/tmp/tmpAhUlnA.msh' Info : Stopped on Sat Aug 4 17:03:51 2018 WARNING:root:Binary Gmsh needs 32-bit integers (got int64). Converting. WARNING:root:Binary Gmsh needs 32-bit integers (got int64). Converting. WARNING:root:Binary Gmsh needs 32-bit integers (got int64). Converting.
next meshio writes that mesh to a .msh file (still no Traceback)
Then a Traceback when Gmsh2D tries to read that .msh file and can't parse something:
Traceback (most recent call last): File "anisotropy_adaptive20180804.py", line 70, in
main()
File "anisotropy_adaptive20180804.py", line 66, in main
geom,mesh=make_mesh(sideLength=L,cellSizeFine=cellSizeFine,cellSizeCoarse=cellSizeCoarse,attractorDistMin=attractorDistMin,attractorDistMax=attractorDistMax,writeMeshToFile=True,filename='anisotropy_adaptive_initial_mesh.msh')
File "anisotropy_adaptive20180804.py", line 41, in make_mesh
mesh=Gmsh2D(arg=filename) #the fipy mesh
File "/home/dad84/miniconda3/envs/FIPY2018071101/lib/python2.7/site-packages/FiPy-unknown-py2.7.egg/fipy/meshes/gmshMesh.py", line 1589, in init
self._orderedCellVertexIDs_data) = self.mshFile.read()
File "/home/dad84/miniconda3/envs/FIPY2018071101/lib/python2.7/site-packages/FiPy-unknown-py2.7.egg/fipy/meshes/gmshMesh.py", line 798, in read
facesData) = self._parseElementFile()
File "/home/dad84/miniconda3/envs/FIPY2018071101/lib/python2.7/site-packages/FiPy-unknown-py2.7.egg/fipy/meshes/gmshMesh.py", line 1084, in _parseElementFile
currLineInts = [int(x) for x in el.split()]
ValueError: invalid literal for int() with base 10: '\x0f'
However I can open that .msh file in gmsh gui and it looks perfect: $gmsh filename.msh
Thanks