[cig-commits] r11346 - in short/3D/PyLith/trunk: pylith/meshio pylith/topology unittests/pytests/meshio

brad at geodynamics.org brad at geodynamics.org
Thu Mar 6 13:01:39 PST 2008


Author: brad
Date: 2008-03-06 13:01:38 -0800 (Thu, 06 Mar 2008)
New Revision: 11346

Added:
   short/3D/PyLith/trunk/unittests/pytests/meshio/TestOutputManager.py
Modified:
   short/3D/PyLith/trunk/pylith/meshio/OutputManager.py
   short/3D/PyLith/trunk/pylith/topology/Mesh.py
   short/3D/PyLith/trunk/unittests/pytests/meshio/Makefile.am
   short/3D/PyLith/trunk/unittests/pytests/meshio/testdriver.py
Log:
Added Python unit tests for OuputManager. Fixed exposed bug in _checkWrite().

Modified: short/3D/PyLith/trunk/pylith/meshio/OutputManager.py
===================================================================
--- short/3D/PyLith/trunk/pylith/meshio/OutputManager.py	2008-03-06 20:30:42 UTC (rev 11345)
+++ short/3D/PyLith/trunk/pylith/meshio/OutputManager.py	2008-03-06 21:01:38 UTC (rev 11346)
@@ -124,6 +124,13 @@
     if None == self.dataProvider:
       raise ValueError("Need to set 'dataProvider' in OutputManager.")
     self._verifyFields(self.dataProvider.availableFields)
+
+    if not "getDataMesh" in dir(self.dataProvider):
+      raise TypeError("Data provider must have a 'getDataMesh' function.")
+    if not "getVertexField" in dir(self.dataProvider):
+      raise TypeError("Data provider must have a 'getVertexField' function.")
+    if not "getCellField" in dir(self.dataProvider):
+      raise TypeError("Data provider must have a 'getCellField' function.")
     return
 
 
@@ -289,13 +296,28 @@
     Check if we want to write data at time t.
     """
     write = False
-    if self._stepWrite == None or not "value" in dir(self._tWrite):
+    
+    # If first call, then _stepWrite and _tWrite are None
+    if None == self._stepWrite and None == self._tWrite:
       write = True
+      self._stepWrite = self._stepCur
+      self._tWrite = t
+
     elif self.outputFreq == "skip":
       if self._stepCur > self._stepWrite + self.skip:
         write = True
-    elif t >= self._tWrite + self.dt:
-      write = True
+        self._stepWrite = self._stepCur
+
+    elif self.outputFreq == "time_step":
+      if t >= self._tWrite + self.dt:
+       write = True
+       self._tWrite = t
+
+    else:
+      raise ValueError, \
+            "Unknown value '%s' for output frequency." % self.outputFreq
+
+    self._stepCur += 1
     return write
 
 

Modified: short/3D/PyLith/trunk/pylith/topology/Mesh.py
===================================================================
--- short/3D/PyLith/trunk/pylith/topology/Mesh.py	2008-03-06 20:30:42 UTC (rev 11345)
+++ short/3D/PyLith/trunk/pylith/topology/Mesh.py	2008-03-06 21:01:38 UTC (rev 11346)
@@ -107,9 +107,10 @@
     Allocated (and zero) real section.
     """
     assert(None != self.cppHandle)
-    return self.cppHandle.allocateRealSection(field)
+    self.cppHandle.allocateRealSection(field)
+    return
+  
 
-
   def createMatrix(self, field):
     """
     Create sparse matrix compatible with field.

Modified: short/3D/PyLith/trunk/unittests/pytests/meshio/Makefile.am
===================================================================
--- short/3D/PyLith/trunk/unittests/pytests/meshio/Makefile.am	2008-03-06 20:30:42 UTC (rev 11345)
+++ short/3D/PyLith/trunk/unittests/pytests/meshio/Makefile.am	2008-03-06 21:01:38 UTC (rev 11346)
@@ -25,12 +25,18 @@
 	TestMeshIOCubit.py \
 	TestMeshIOLagrit.py \
 	TestVertexFilterVecNorm.py \
-	TestCellFilterAvg.py
+	TestCellFilterAvg.py \
+	TestOutputManager.py
 
 if ENABLE_CUBIT
   TESTS += testdriver_cubit.py
   check_SCRIPTS += testdriver_cubit.py
 endif	
 
+noinst_tmp = \
+	output_info.vtk \
+	output_t20.vtk
 
+CLEANFILES = $(noinst_tmp)
+
 # End of file 

Added: short/3D/PyLith/trunk/unittests/pytests/meshio/TestOutputManager.py
===================================================================
--- short/3D/PyLith/trunk/unittests/pytests/meshio/TestOutputManager.py	                        (rev 0)
+++ short/3D/PyLith/trunk/unittests/pytests/meshio/TestOutputManager.py	2008-03-06 21:01:38 UTC (rev 11346)
@@ -0,0 +1,293 @@
+#!/usr/bin/env python
+#
+# ======================================================================
+#
+#                           Brad T. Aagaard
+#                        U.S. Geological Survey
+#
+# {LicenseText}
+#
+# ======================================================================
+#
+
+## @file unittests/pytests/meshio/TestOutputManager.py
+
+## @brief Unit testing of Python OutputManager object.
+
+import unittest
+
+from pylith.meshio.OutputManager import OutputManager
+
+# ----------------------------------------------------------------------
+class TestProvider(object):
+
+  def __init__(self):
+    """
+    Constructor.
+    """
+    self.availableFields = \
+        {'vertex': \
+           {'info': ["vertex info"],
+            'data': ["vertex data 1",
+                     "vertex data 2"]},
+         'cell': \
+           {'info': ["cell info"],
+            'data': ["cell data"]}}
+
+    from pylith.meshio.MeshIOAscii import MeshIOAscii
+    iohandler = MeshIOAscii()
+    filename = "data/twohex8.txt"
+    
+    from spatialdata.geocoords.CSCart import CSCart
+    iohandler.filename = filename
+    iohandler.coordsys = CSCart()
+    mesh = iohandler.read(debug=False, interpolate=False)
+    self._mesh = mesh
+    return
+
+
+  def getDataMesh(self):
+    """
+    Get mesh.
+    """
+    return (self._mesh, None, None)
+
+
+  def getVertexField(self, name, fields=None):
+    """
+    Get vertex field.
+    """
+    mesh = self._mesh
+    
+    if name == "vertex info":
+      fieldType = 0
+      fiberDim = 1
+    elif name == "vertex data 1":
+      fieldType = 1
+      fiberDim = mesh.dimension()
+    elif name == "vertex data 2":
+      fieldType = 3
+      fiberDim = 5
+    else:
+      raise ValueError("Unknown field '%s'." % name)
+
+    field = mesh.createRealSection(name, fiberDim)
+    mesh.allocateRealSection(field)
+    return (field, fieldType)
+
+
+  def getCellField(self, name, fields=None):
+    """
+    Get cell field.
+    """
+    raise NotImplementedError("Please implement 'getCellField().'")
+    return
+
+
+# ----------------------------------------------------------------------
+class TestOutputManager(unittest.TestCase):
+  """
+  Unit testing of Python OutputManager object.
+  """
+
+  def test_constructor(self):
+    """
+    Test constructor.
+    """
+    output = OutputManager()
+    output._configure()
+    output.writer._configure()
+    return
+
+
+  def test_sync(self):
+    """
+    Test _sync().
+    """
+    output = OutputManager()
+    output._configure()
+    output.writer._configure()
+    output.writer.initialize()
+    output._sync()
+    self.assertNotEqual(None, output.cppHandle)
+    return
+  
+
+  def test_preinitialize(self):
+    """
+    Test preinitialize().
+    """
+    output = OutputManager()
+    dataProvider = TestProvider()
+    output.preinitialize(dataProvider)
+    
+    self.assertEqual(dataProvider, output.dataProvider)
+    return
+
+
+  def test_verifyConfiguration(self):
+    """
+    Test verifyConfiguration().
+    """
+    output = OutputManager()
+    dataProvider = TestProvider()
+    output.preinitialize(dataProvider)
+
+    output.vertexInfoFields = ["vertex info"]
+    output.vertexDataFields = ["vertex data 2"]
+    output.cellInfoFields = []
+    output.cellDataFields = ["cell data"]
+    output.verifyConfiguration()
+    return
+  
+  
+  def test_initialize(self):
+    """
+    Test initialize().
+    """
+    # No quadrature
+    output = OutputManager()
+    output._configure()
+    output.writer._configure()
+    output.writer.filename = "test.vtk"
+    dataProvider = TestProvider()
+    output.preinitialize(dataProvider)
+    output.initialize()
+    self.assertNotEqual(None, output.cppHandle)
+
+    # With quadrature
+    from pylith.feassemble.FIATSimplex import FIATSimplex
+    from pylith.feassemble.quadrature.Quadrature1D import Quadrature1D
+    cell = FIATSimplex()
+    cell.shape = "line"
+    cell.degree = 2
+    cell.order = 2
+
+    quadrature = Quadrature1D()
+    quadrature.cell = cell
+    
+    output = OutputManager()
+    output._configure()
+    output.writer._configure()
+    output.writer.filename = "test.vtk"
+    dataProvider = TestProvider()
+    output.preinitialize(dataProvider)
+    output.initialize(quadrature)
+    self.assertNotEqual(None, output.cppHandle)
+    return
+
+
+  def test_openclose(self):
+    """
+    Test open() and close().
+    """
+    output = OutputManager()
+    output._configure()
+    output.writer._configure()
+    output.writer.filename = "output.vtk"
+    dataProvider = TestProvider()
+    output.preinitialize(dataProvider)
+    output.initialize()
+    from pyre.units.time import s
+    output.open(totalTime=5.0*s, numTimeSteps=2)
+    output.close()
+    return
+
+
+  def test_writeInfo(self):
+    """
+    Test writeInfo().
+    """
+    output = OutputManager()
+    output._configure()
+    output.writer._configure()
+    output.writer.filename = "output.vtk"
+    output.vertexInfoFields = ["vertex info"]
+    
+    dataProvider = TestProvider()
+    output.preinitialize(dataProvider)
+    output.initialize()
+    
+    from pyre.units.time import s
+    output.open(totalTime=5.0*s, numTimeSteps=2)
+    output.writeInfo()
+    output.close()
+    return
+
+
+  def test_writeData(self):
+    """
+    Test writeData().
+    """
+    output = OutputManager()
+    output._configure()
+    output.writer._configure()
+    output.writer.filename = "output.vtk"
+    output.writer.timeFormat = "%3.1f"
+    output.vertexDataFields = ["vertex data 2",
+                               "vertex data 1"]
+    
+    dataProvider = TestProvider()
+    output.preinitialize(dataProvider)
+    output.initialize()
+
+    from pylith.topology.FieldsManager import FieldsManager
+    fields = FieldsManager(dataProvider._mesh)
+    
+    from pyre.units.time import s
+    output.open(totalTime=5.0*s, numTimeSteps=2)
+    output.writeData(2.0*s, fields)
+    output.close()
+    return
+
+
+  def test_checkWrite(self):
+    """
+    Test _checkWrite().
+    """
+    from pyre.units.time import s
+
+    # Default values should be true
+    output = OutputManager()
+    output._configure()
+    self.assertEqual(True, output._checkWrite(0.0*s))
+    self.assertEqual(True, output._checkWrite(3.234e+8*s))
+
+    # Check writing based on time
+    output = OutputManager()
+    output._configure()
+    output.outputFreq = "time_step"
+    t = 0.0*s
+    dt = output.dt
+    self.assertEqual(True, output._checkWrite(t))
+    self.assertEqual(False, output._checkWrite(t))
+    self.assertEqual(False, output._checkWrite(t + 0.8*dt))
+    t += dt
+    self.assertEqual(True, output._checkWrite(t))
+    t = 2*dt
+    self.assertEqual(True, output._checkWrite(t))
+    
+    # Check writing based on number of steps
+    output = OutputManager()
+    output._configure()
+    output.outputFreq = "skip"
+    output.skip = 1
+    t = 0.0*s
+    dt = 1.0*s
+    self.assertEqual(True, output._checkWrite(t))
+    t += dt
+    self.assertEqual(False, output._checkWrite(t))
+    t += dt
+    self.assertEqual(True, output._checkWrite(t))
+    output.skip = 2
+    t += dt
+    self.assertEqual(False, output._checkWrite(t))
+    t += dt
+    self.assertEqual(False, output._checkWrite(t))
+    t += dt
+    self.assertEqual(True, output._checkWrite(t))
+    
+    return
+
+
+# End of file 

Modified: short/3D/PyLith/trunk/unittests/pytests/meshio/testdriver.py
===================================================================
--- short/3D/PyLith/trunk/unittests/pytests/meshio/testdriver.py	2008-03-06 20:30:42 UTC (rev 11345)
+++ short/3D/PyLith/trunk/unittests/pytests/meshio/testdriver.py	2008-03-06 21:01:38 UTC (rev 11346)
@@ -66,6 +66,9 @@
     from TestCellFilterAvg import TestCellFilterAvg
     suite.addTest(unittest.makeSuite(TestCellFilterAvg))
 
+    from TestOutputManager import TestOutputManager
+    suite.addTest(unittest.makeSuite(TestOutputManager))
+
     return suite
 
 



More information about the cig-commits mailing list