10
$\begingroup$

Say I have generated a mesh using ToElementMesh, following one of the examples here: http://reference.wolfram.com/language/FEMDocumentation/ref/ToElementMesh.html

How do I export it to a text file? Say I get my mesh using

<< NDSolve`FEM`
ToElementMesh[Disk[]]
$\endgroup$
3
  • $\begingroup$ What is your precise goal with exporting it? What format do you want to export it to? $\endgroup$
    – Szabolcs
    Feb 4, 2015 at 14:07
  • $\begingroup$ @Szabolcs I would like just to have a datafile with the mesh. The format I would like are the coordinates of the nodes on each row of the data file, $x_i, y_i, z_i$ $\endgroup$
    – BillyJean
    Feb 4, 2015 at 14:14
  • 1
    $\begingroup$ Let me be more explicit: what do you want to do with that file? Store it and later re-import it into Mathematica? Or use with other software? Just exporting the coordinates is easy: extract them (em["Coordinates"]), then use Export with the "Table" format. But the coordinates don't contain full information about the mesh and in the original version of the question you asked about exporting the "mesh", not just the "coordinates". $\endgroup$
    – Szabolcs
    Feb 4, 2015 at 15:15

2 Answers 2

5
$\begingroup$

Based on the comment, is this what you are looking for?

<< NDSolve`FEM`
em = ToElementMesh[Disk[]]
Export["mesh.txt", em["Coordinates"], "Table"]

em["Coordinates"] extracts the coordinates and the Table export format is just a tab-separated plaint text table.

This method will not preserve the precise cells, only the vertex coordinates.

$\endgroup$
1
$\begingroup$

To Export the full information contained in ToElementMesh you have to convert it into a list, then transform all the possible exponentials to plain decimals so that you can export the whole list as a string without notation issues.

mesh = ToElementMesh[Disk[]]
Export["diskmesh.txt",NumberForm[#, ExponentFunction -> (Null &)] & /@ List @@ mesh, "String"]

Now, note that the mesh you created has four elements

In[175]:= Length@mesh
Out[175]= 4

These are "Coordinates", "MeshElements", "BoundaryElements", "PointElements" Therefore, when you recreate ToElementMesh from the imported .txt file you gotta specify what is what, like this

In[177]:= #1->#2 & ~MapThread~{{"Coordinates", "MeshElements","BoundaryElements","PointElements"},ToExpression@Import["diskmesh.txt"]}//ToElementMesh@@#&

Out[177]= ElementMesh[{{-1., 1.}, {-1., 1.}}, {TriangleElement[<504>]}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.