/test/unit/io/python/XDMF.py

https://bitbucket.org/pefarrell/dolfin
Python | 239 lines | 175 code | 40 blank | 24 comment | 12 complexity | e7ed7aa0f2d319b808263cae1ff37aa4 MD5 | raw file
  1. """Unit tests for the XDMF io library"""
  2. # Copyright (C) 2012 Garth N. Wells
  3. #
  4. # This file is part of DOLFIN.
  5. #
  6. # DOLFIN is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Lesser General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # DOLFIN is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License
  17. # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. # First added: 2012-09-14
  20. # Last changed: 2013-03-05
  21. import unittest
  22. from dolfin import *
  23. if has_hdf5():
  24. class XDMF_Mesh_Output_and_Input(unittest.TestCase):
  25. """Test output and input of Meshes to/from XDMF files"""
  26. def test_save_and_load_1d_mesh(self):
  27. mesh = UnitIntervalMesh(32)
  28. File("output/mesh.xdmf") << mesh
  29. XDMFFile(mesh.mpi_comm(), "output/mesh.xdmf") << mesh
  30. mesh2 = Mesh("output/mesh.xdmf")
  31. self.assertEqual(mesh.size_global(0), mesh2.size_global(0))
  32. dim = mesh.topology().dim()
  33. self.assertEqual(mesh.size_global(dim), mesh2.size_global(dim))
  34. def test_save_and_load_2d_mesh(self):
  35. mesh = UnitSquareMesh(32, 32)
  36. File("output/mesh_2D.xdmf") << mesh
  37. XDMFFile(mesh.mpi_comm(), "output/mesh_2D.xdmf") << mesh
  38. mesh2 = Mesh("output/mesh_2D.xdmf")
  39. self.assertEqual(mesh.size_global(0), mesh2.size_global(0))
  40. dim = mesh.topology().dim()
  41. self.assertEqual(mesh.size_global(dim), mesh2.size_global(dim))
  42. def test_save_and_load_3d_mesh(self):
  43. mesh = UnitCubeMesh(8, 8, 8)
  44. File("output/mesh_3D.xdmf") << mesh
  45. XDMFFile(mesh.mpi_comm(), "output/mesh_3D.xdmf") << mesh
  46. mesh2 = Mesh("output/mesh_3D.xdmf")
  47. self.assertEqual(mesh.size_global(0), mesh2.size_global(0))
  48. dim = mesh.topology().dim()
  49. self.assertEqual(mesh.size_global(dim), mesh2.size_global(dim))
  50. class XDMF_Vertex_Function_Output(unittest.TestCase):
  51. """Test output of vertex-based Functions to XDMF files"""
  52. def test_save_1d_scalar(self):
  53. mesh = UnitIntervalMesh(32)
  54. u = Function(FunctionSpace(mesh, "Lagrange", 2))
  55. u.vector()[:] = 1.0
  56. File("output/u.xdmf") << u
  57. XDMFFile(mesh.mpi_comm(), "output/u.xdmf") << u
  58. def test_save_2d_scalar(self):
  59. mesh = UnitSquareMesh(16, 16)
  60. u = Function(FunctionSpace(mesh, "Lagrange", 2))
  61. u.vector()[:] = 1.0
  62. File(mesh.mpi_comm(), "output/u.xdmf") << u
  63. XDMFFile(mesh.mpi_comm(), "output/u.xdmf") << u
  64. def test_save_3d_scalar(self):
  65. mesh = UnitCubeMesh(8, 8, 8)
  66. u = Function(FunctionSpace(mesh, "Lagrange", 2))
  67. u.vector()[:] = 1.0
  68. File(mesh.mpi_comm(), "output/u.xdmf") << u
  69. XDMFFile(mesh.mpi_comm(), "output/u.xdmf") << u
  70. def test_save_2d_vector(self):
  71. mesh = UnitSquareMesh(16, 16)
  72. u = Function(VectorFunctionSpace(mesh, "Lagrange", 2))
  73. c = Constant((1.0, 2.0))
  74. u.interpolate(c)
  75. File(mesh.mpi_comm(), "output/u_2dv.xdmf") << u
  76. XDMFFile(mesh.mpi_comm(), "output/u.xdmf") << u
  77. def test_save_3d_vector(self):
  78. mesh = UnitCubeMesh(1, 1, 1)
  79. u = Function(VectorFunctionSpace(mesh, "Lagrange", 1))
  80. c = Constant((1.0, 2.0, 3.0))
  81. u.interpolate(c)
  82. File(mesh.mpi_comm(), "output/u_3Dv.xdmf") << u
  83. XDMFFile(mesh.mpi_comm(), "output/u.xdmf") << u
  84. def test_save_3d_vector_series(self):
  85. mesh = UnitCubeMesh(8, 8, 8)
  86. u = Function(VectorFunctionSpace(mesh, "Lagrange", 2))
  87. file = File(mesh.mpi_comm(), "output/u_3D.xdmf")
  88. u.vector()[:] = 1.0
  89. file << (u, 0.1)
  90. u.vector()[:] = 2.0
  91. file << (u, 0.2)
  92. u.vector()[:] = 3.0
  93. file << (u, 0.3)
  94. del file
  95. file = XDMFFile(mesh.mpi_comm(), "output/u_3D.xdmf")
  96. u.vector()[:] = 1.0
  97. file << (u, 0.1)
  98. u.vector()[:] = 2.0
  99. file << (u, 0.2)
  100. u.vector()[:] = 3.0
  101. file << (u, 0.3)
  102. def test_save_2d_tensor(self):
  103. mesh = UnitSquareMesh(16, 16)
  104. u = Function(TensorFunctionSpace(mesh, "Lagrange", 2))
  105. u.vector()[:] = 1.0
  106. File(mesh.mpi_comm(), "output/tensor.xdmf") << u
  107. XDMFFile(mesh.mpi_comm(), "output/tensor.xdmf") << u
  108. def test_save_3d_tensor(self):
  109. mesh = UnitCubeMesh(8, 8, 8)
  110. u = Function(TensorFunctionSpace(mesh, "Lagrange", 2))
  111. u.vector()[:] = 1.0
  112. File(mesh.mpi_comm(), "output/u.xdmf") << u
  113. XDMFFile(mesh.mpi_comm(), "output/u.xdmf") << u
  114. class XDMF_MeshFunction_Output(unittest.TestCase):
  115. """Test output of Meshes to XDMF files"""
  116. def test_save_1d_mesh(self):
  117. mesh = UnitIntervalMesh(32)
  118. mf = CellFunction("size_t", mesh)
  119. for cell in cells(mesh):
  120. mf[cell] = cell.index()
  121. File(mesh.mpi_comm(), "output/mf_1D.xdmf") << mf
  122. XDMFFile(mesh.mpi_comm(), "output/mf_1D.xdmf") << mf
  123. def test_save_2D_cell_function(self):
  124. mesh = UnitSquareMesh(32, 32)
  125. mf = CellFunction("size_t", mesh)
  126. for cell in cells(mesh):
  127. mf[cell] = cell.index()
  128. File(mesh.mpi_comm(), "output/mf_2D.xdmf") << mf
  129. XDMFFile(mesh.mpi_comm(), "output/mf_2D.xdmf") << mf
  130. def test_save_3D_cell_function(self):
  131. mesh = UnitCubeMesh(8, 8, 8)
  132. mf = CellFunction("size_t", mesh)
  133. for cell in cells(mesh):
  134. mf[cell] = cell.index()
  135. File(mesh.mpi_comm(), "output/mf_3D.xdmf") << mf
  136. XDMFFile(mesh.mpi_comm(), "output/mf_3D.xdmf") << mf
  137. def test_save_2D_facet_function(self):
  138. mesh = UnitSquareMesh(32, 32)
  139. mf = FacetFunction("size_t", mesh)
  140. for facet in facets(mesh):
  141. mf[facet] = facet.index()
  142. File(mesh.mpi_comm(), "output/mf_facet_2D.xdmf") << mf
  143. XDMFFile(mesh.mpi_comm(), "output/mf_facet_2D.xdmf") << mf
  144. def test_save_3D_facet_function(self):
  145. mesh = UnitCubeMesh(8, 8, 8)
  146. mf = FacetFunction("size_t", mesh)
  147. for facet in facets(mesh):
  148. mf[facet] = facet.index()
  149. File(mesh.mpi_comm(), "output/mf_facet_3D.xdmf") << mf
  150. XDMFFile(mesh.mpi_comm(), "output/mf_facet_3D.xdmf") << mf
  151. def test_save_3D_edge_function(self):
  152. mesh = UnitCubeMesh(8, 8, 8)
  153. mf = EdgeFunction("size_t", mesh)
  154. for edge in edges(mesh):
  155. mf[edge] = edge.index()
  156. File(mesh.mpi_comm(), "output/mf_edge_3D.xdmf") << mf
  157. XDMFFile(mesh.mpi_comm(), "output/mf_edge_3D.xdmf") << mf
  158. def test_save_2D_vertex_function(self):
  159. mesh = UnitSquareMesh(32, 32)
  160. mf = VertexFunction("size_t", mesh)
  161. for vertex in vertices(mesh):
  162. mf[vertex] = vertex.index()
  163. File(mesh.mpi_comm(), "output/mf_vertex_2D.xdmf") << mf
  164. XDMFFile(mesh.mpi_comm(), "output/mf_vertex_2D.xdmf") << mf
  165. def test_save_3D_vertex_function(self):
  166. mesh = UnitCubeMesh(8, 8, 8)
  167. mf = VertexFunction("size_t", mesh)
  168. for vertex in vertices(mesh):
  169. mf[vertex] = vertex.index()
  170. File(mesh.mpi_comm(), "output/mf_vertex_3D.xdmf") << mf
  171. XDMFFile(mesh.mpi_comm(), "output/mf_vertex_3D.xdmf") << mf
  172. class XDMF_Point_Output(unittest.TestCase):
  173. """Test output of points to XDMF files"""
  174. def test_save_points_2D(self):
  175. import numpy
  176. mesh = UnitSquareMesh(16, 16)
  177. points, values = [], []
  178. for v in vertices(mesh):
  179. points.append(v.point())
  180. values.append(v.point().norm())
  181. vals = numpy.array(values)
  182. file = XDMFFile(mesh.mpi_comm(), "output/points_2D.xdmf")
  183. file.write(points)
  184. file = XDMFFile(mesh.mpi_comm(), "output/point_values_2D.xdmf")
  185. file.write(points, vals)
  186. def test_save_points_3D(self):
  187. import numpy
  188. mesh = UnitCubeMesh(4, 4, 4)
  189. points, values = [], []
  190. for v in vertices(mesh):
  191. points.append(v.point())
  192. values.append(v.point().norm())
  193. vals = numpy.array(values)
  194. file = XDMFFile(mesh.mpi_comm(), "output/points_3D.xdmf")
  195. file.write(points)
  196. file = XDMFFile(mesh.mpi_comm(), "output/point_values_3D.xdmf")
  197. file.write(points, vals)
  198. if __name__ == "__main__":
  199. unittest.main()