| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Module for formatting DOLFIN XML files.""" |
| 3 |
|
| 4 |
# Copyright (C) 2012 Garth N. Wells |
| 5 |
# |
| 6 |
# This file is part of DOLFIN. |
| 7 |
# |
| 8 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU Lesser General Public License as published by |
| 10 |
# the Free Software Foundation, either version 3 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# DOLFIN is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU Lesser General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU Lesser General Public License |
| 19 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
# |
| 21 |
# Modified by Jan Blechta, 2012. |
| 22 |
# |
| 23 |
# Last changed: 2012-11-22 |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
# Write mesh header |
| 28 |
def write_header_mesh(ofile, cell_type, dim): |
| 29 |
ofile.write("""\ |
| 30 |
<?xml version=\"1.0\" encoding=\"UTF-8\"?> |
| 31 |
|
| 32 |
<dolfin xmlns:dolfin=\"http://www.fenicsproject.org\"> |
| 33 |
<mesh celltype="%s" dim="%d"> |
| 34 |
""" % (cell_type, dim)) |
| 35 |
|
| 36 |
# Write graph header |
| 37 |
def write_header_graph(ofile, graph_type): |
| 38 |
ofile.write("""\ |
| 39 |
<?xml version=\"1.0\" encoding=\"UTF-8\"?> |
| 40 |
|
| 41 |
<dolfin xmlns:dolfin=\"http://www.fenicsproject.org/\"> |
| 42 |
<graph type="%s"> |
| 43 |
""" % (graph_type)) |
| 44 |
|
| 45 |
# Write mesh footer |
| 46 |
def write_footer_mesh(ofile): |
| 47 |
ofile.write("""\ |
| 48 |
</mesh> |
| 49 |
</dolfin> |
| 50 |
""") |
| 51 |
|
| 52 |
# Write graph footer |
| 53 |
def write_footer_graph(ofile): |
| 54 |
ofile.write("""\ |
| 55 |
</graph> |
| 56 |
</dolfin> |
| 57 |
""") |
| 58 |
|
| 59 |
def write_header_vertices(ofile, num_vertices): |
| 60 |
"Write vertices header" |
| 61 |
print("Expecting %d vertices" % num_vertices) |
| 62 |
ofile.write(" <vertices size=\"%d\">\n" % num_vertices) |
| 63 |
|
| 64 |
def write_footer_vertices(ofile): |
| 65 |
"Write vertices footer" |
| 66 |
ofile.write(" </vertices>\n") |
| 67 |
print("Found all vertices") |
| 68 |
|
| 69 |
def write_header_edges(ofile, num_edges): |
| 70 |
"Write edges header" |
| 71 |
print("Expecting %d edges" % num_edges) |
| 72 |
ofile.write(" <edges size=\"%d\">\n" % num_edges) |
| 73 |
|
| 74 |
def write_footer_edges(ofile): |
| 75 |
"Write edges footer" |
| 76 |
ofile.write(" </edges>\n") |
| 77 |
print("Found all edges") |
| 78 |
|
| 79 |
def write_vertex(ofile, vertex, *args): |
| 80 |
"Write vertex" |
| 81 |
coords = " ".join(['%s="%.16e"' % (comp, num) for (comp, num) in zip(["x","y","z"], args)]) |
| 82 |
ofile.write(' <vertex index="%d" %s/>\n' % \ |
| 83 |
(vertex, coords)) |
| 84 |
|
| 85 |
def write_graph_vertex(ofile, vertex, num_edges, weight = 1): |
| 86 |
"Write graph vertex" |
| 87 |
ofile.write(" <vertex index=\"%d\" num_edges=\"%d\" weight=\"%d\"/>\n" % \ |
| 88 |
(vertex, num_edges, weight)) |
| 89 |
|
| 90 |
def write_graph_edge(ofile, v1, v2, weight = 1): |
| 91 |
"Write graph edge" |
| 92 |
ofile.write(" <edge v1=\"%d\" v2=\"%d\" weight=\"%d\"/>\n" % \ |
| 93 |
(v1, v2, weight)) |
| 94 |
|
| 95 |
def write_header_cells(ofile, num_cells): |
| 96 |
"Write cells header" |
| 97 |
ofile.write(" <cells size=\"%d\">\n" % num_cells) |
| 98 |
print("Expecting %d cells" % num_cells) |
| 99 |
|
| 100 |
def write_footer_cells(ofile): |
| 101 |
"Write cells footer" |
| 102 |
ofile.write(" </cells>\n") |
| 103 |
print("Found all cells") |
| 104 |
|
| 105 |
def write_cell_interval(ofile, cell, n0, n1): |
| 106 |
"Write cell (interval)" |
| 107 |
ofile.write(" <interval index=\"%d\" v0=\"%d\" v1=\"%d\"/>\n" % \ |
| 108 |
(cell, n0, n1)) |
| 109 |
|
| 110 |
def write_cell_triangle(ofile, cell, n0, n1, n2): |
| 111 |
"Write cell (triangle)" |
| 112 |
ofile.write(" <triangle index=\"%d\" v0=\"%d\" v1=\"%d\" v2=\"%d\"/>\n" % \ |
| 113 |
(cell, n0, n1, n2)) |
| 114 |
|
| 115 |
def write_cell_tetrahedron(ofile, cell, n0, n1, n2, n3): |
| 116 |
"Write cell (tetrahedron)" |
| 117 |
ofile.write(" <tetrahedron index=\"%d\" v0=\"%d\" v1=\"%d\" v2=\"%d\" v3=\"%d\"/>\n" % \ |
| 118 |
(cell, n0, n1, n2, n3)) |
| 119 |
|
| 120 |
def write_header_domains(ofile): |
| 121 |
ofile.write(" <domains>\n") |
| 122 |
|
| 123 |
def write_footer_domains(ofile): |
| 124 |
ofile.write(" </domains>\n") |
| 125 |
|
| 126 |
def write_header_meshvaluecollection(ofile, name, dim, size, etype): |
| 127 |
ofile.write(" <mesh_value_collection name=\"%s\" type=\"%s\" dim=\"%d\" size=\"%d\">\n" % (name, etype, dim, size)) |
| 128 |
|
| 129 |
def write_entity_meshvaluecollection(ofile, dim, index, value, local_entity=0): |
| 130 |
ofile.write(" <value cell_index=\"%d\" local_entity=\"%d\" value=\"%d\"/>\n" % (index, local_entity, value)) |
| 131 |
|
| 132 |
def write_footer_meshvaluecollection(ofile): |
| 133 |
ofile.write(" </mesh_value_collection>\n") |
| 134 |
|
| 135 |
def write_header_meshfunction(ofile, dimensions, size): |
| 136 |
header = """<?xml version="1.0" encoding="UTF-8"?> |
| 137 |
<dolfin xmlns:dolfin="http://fenicsproject.org"> |
| 138 |
<mesh_function type="uint" dim="%d" size="%d"> |
| 139 |
""" % (dimensions, size) |
| 140 |
ofile.write(header) |
| 141 |
|
| 142 |
def write_header_meshfunction2(ofile): |
| 143 |
header = """<?xml version="1.0" encoding="UTF-8"?> |
| 144 |
<dolfin xmlns:dolfin="http://fenicsproject.org"> |
| 145 |
<mesh_function> |
| 146 |
""" |
| 147 |
ofile.write(header) |
| 148 |
|
| 149 |
def write_entity_meshfunction(ofile, index, value): |
| 150 |
ofile.write(""" <entity index=\"%d\" value=\"%d\"/> |
| 151 |
""" % (index, value)) |
| 152 |
|
| 153 |
def write_footer_meshfunction(ofile): |
| 154 |
ofile.write(""" </mesh_function> |
| 155 |
</dolfin>""") |