[cig-commits] r13466 - in short/3D/PyLith/trunk: libsrc/topology unittests/libtests/topology

brad at geodynamics.org brad at geodynamics.org
Fri Dec 5 11:22:26 PST 2008


Author: brad
Date: 2008-12-05 11:22:26 -0800 (Fri, 05 Dec 2008)
New Revision: 13466

Modified:
   short/3D/PyLith/trunk/libsrc/topology/Field.cc
   short/3D/PyLith/trunk/libsrc/topology/Field.hh
   short/3D/PyLith/trunk/libsrc/topology/Field.icc
   short/3D/PyLith/trunk/libsrc/topology/FieldUniform.cc
   short/3D/PyLith/trunk/unittests/libtests/topology/TestField.cc
   short/3D/PyLith/trunk/unittests/libtests/topology/TestField.hh
   short/3D/PyLith/trunk/unittests/libtests/topology/TestFieldUniform.cc
Log:
Added unit tests for operations with Field. Adjusted Field implementation to allow null section.

Modified: short/3D/PyLith/trunk/libsrc/topology/Field.cc
===================================================================
--- short/3D/PyLith/trunk/libsrc/topology/Field.cc	2008-12-05 15:33:09 UTC (rev 13465)
+++ short/3D/PyLith/trunk/libsrc/topology/Field.cc	2008-12-05 19:22:26 UTC (rev 13466)
@@ -29,13 +29,10 @@
   _mesh(mesh),
   _scale(1.0),
   _name("unknown"),
-  _spaceDim(0),
   _vecFieldType(OTHER),
   _dimensionsOkay(false)
 { // constructor
   assert(!mesh.isNull());
-
-  _section = new SieveRealSection(mesh->comm(), mesh->debug());
 } // constructor
 
 // ----------------------------------------------------------------------
@@ -45,19 +42,39 @@
 } // destructor
 
 // ----------------------------------------------------------------------
+// Get spatial dimension of domain.
+int
+pylith::topology::Field::spaceDim(void) const
+{ // spaceDim
+  assert(!_mesh.isNull());
+  return _mesh->getDimension();
+} // spaceDim
+
+// ----------------------------------------------------------------------
+// Create seive section.
+void
+pylith::topology::Field::newSection(void)
+{ // newSection
+  assert(!_mesh.isNull());
+  _section = new SieveRealSection(_mesh->comm(), _mesh->debug());  
+} // newSection
+
+// ----------------------------------------------------------------------
 // Create section given atlas.
 void
 pylith::topology::Field::copyLayout(const Field& src)
 { // createSection
-  _spaceDim = src._spaceDim;
   _vecFieldType = src._vecFieldType;
 
   const ALE::Obj<SieveRealSection>& srcSection = src.section();
-  assert(!_section.isNull());
+  if (!srcSection.isNull() && _section.isNull())
+    newSection();
 
-  _section->setAtlas(srcSection->getAtlas());
-  _section->allocateStorage();
-  _section->setBC(srcSection->getBC());
+  if (!_section.isNull()) {
+    _section->setAtlas(srcSection->getAtlas());
+    _section->allocateStorage();
+    _section->setBC(srcSection->getBC());
+  } // if
 } // createSection
 
 // ----------------------------------------------------------------------
@@ -65,8 +82,8 @@
 void
 pylith::topology::Field::clear(void)
 { // clear
-  assert(!_section.isNull());
-  _section->clear();
+  if (!_section.isNull())
+    _section->clear();
 
   _scale = 1.0;
   _vecFieldType = OTHER;
@@ -78,8 +95,8 @@
 void
 pylith::topology::Field::zero(void)
 { // zero
-  assert(!_section.isNull());
-  _section->zero();
+  if (!_section.isNull())
+    _section->zero();
 } // zero
 
 // ----------------------------------------------------------------------
@@ -87,10 +104,10 @@
 void
 pylith::topology::Field::complete(void)
 { // complete
-  assert(!_section.isNull());
-  ALE::Completion::completeSectionAdd(_mesh->getSendOverlap(),
-				      _mesh->getRecvOverlap(), 
-				      _section, _section);
+  if (!_section.isNull())
+    ALE::Completion::completeSectionAdd(_mesh->getSendOverlap(),
+					_mesh->getRecvOverlap(), 
+					_section, _section);
 } // complete
 
 // ----------------------------------------------------------------------
@@ -99,11 +116,9 @@
 pylith::topology::Field::copy(const Field& field)
 { // copy
   // Check compatibility of sections
-  assert(!_section.isNull());
-  assert(!field._section.isNull());
-  const int srcSize = field._section->size();
-  const int dstSize = _section->size();
-  if (field._spaceDim != _spaceDim ||
+  const int srcSize = (!field._section.isNull()) ? field._section->size() : 0;
+  const int dstSize = (!_section.isNull()) ? _section->size() : 0;
+  if (field.spaceDim() != spaceDim() ||
       field._vecFieldType != _vecFieldType ||
       field._scale != _scale ||
       srcSize != dstSize) {
@@ -112,29 +127,33 @@
     msg << "Cannot copy values from section '" << field._name 
 	<< "' to section '" << _name << "'. Sections are incompatible.\n"
 	<< "  Source section:\n"
-	<< "    space dim: " << field._spaceDim << "\n"
+	<< "    space dim: " << field.spaceDim() << "\n"
 	<< "    vector field type: " << field._vecFieldType << "\n"
 	<< "    scale: " << field._scale << "\n"
 	<< "    size: " << srcSize
 	<< "  Destination section:\n"
-	<< "    space dim: " << _spaceDim << "\n"
+	<< "    space dim: " << spaceDim() << "\n"
 	<< "    vector field type: " << _vecFieldType << "\n"
 	<< "    scale: " << _scale << "\n"
 	<< "    size: " << dstSize;
     throw std::runtime_error(msg.str());
   } // if
+  assert( (_section.isNull() && field._section.isNull()) ||
+	  (!_section.isNull() && !field._section.isNull()) );
 
-  // Copy values from field
-  const SieveRealSection::chart_type& chart = _section->getChart();
-  const SieveRealSection::chart_type::const_iterator chartEnd = chart.end();
+  if (!_section.isNull()) {
+    // Copy values from field
+    const SieveRealSection::chart_type& chart = _section->getChart();
+    const SieveRealSection::chart_type::const_iterator chartEnd = chart.end();
 
-  for (SieveRealSection::chart_type::const_iterator c_iter = chart.begin();
-       c_iter != chartEnd;
-       ++c_iter) {
-    assert(field._section->getFiberDimension(*c_iter) ==
-	   _section->getFiberDimension(*c_iter));
-    _section->updatePoint(*c_iter, field._section->restrictPoint(*c_iter));
-  } // for
+    for (SieveRealSection::chart_type::const_iterator c_iter = chart.begin();
+	 c_iter != chartEnd;
+	 ++c_iter) {
+      assert(field._section->getFiberDimension(*c_iter) ==
+	     _section->getFiberDimension(*c_iter));
+      _section->updatePoint(*c_iter, field._section->restrictPoint(*c_iter));
+    } // for
+  } // if
 } // copy
 
 // ----------------------------------------------------------------------
@@ -143,11 +162,9 @@
 pylith::topology::Field::operator+=(const Field& field)
 { // operator+=
   // Check compatibility of sections
-  assert(!_section.isNull());
-  assert(!field._section.isNull());
-  const int srcSize = field._section->size();
-  const int dstSize = _section->size();
-  if (field._spaceDim != _spaceDim ||
+  const int srcSize = (!field._section.isNull()) ? field._section->size() : 0;
+  const int dstSize = (!_section.isNull()) ? _section->size() : 0;
+  if (field.spaceDim() != spaceDim() ||
       field._vecFieldType != _vecFieldType ||
       field._scale != _scale ||
       srcSize != dstSize) {
@@ -156,34 +173,38 @@
     msg << "Cannot add values from section '" << field._name 
 	<< "' to section '" << _name << "'. Sections are incompatible.\n"
 	<< "  Source section:\n"
-	<< "    space dim: " << field._spaceDim << "\n"
+	<< "    space dim: " << field.spaceDim() << "\n"
 	<< "    vector field type: " << field._vecFieldType << "\n"
 	<< "    scale: " << field._scale << "\n"
 	<< "    size: " << srcSize
 	<< "  Destination section:\n"
-	<< "    space dim: " << _spaceDim << "\n"
+	<< "    space dim: " << spaceDim() << "\n"
 	<< "    vector field type: " << _vecFieldType << "\n"
 	<< "    scale: " << _scale << "\n"
 	<< "    size: " << dstSize;
     throw std::runtime_error(msg.str());
   } // if
+  assert( (_section.isNull() && field._section.isNull()) ||
+	  (!_section.isNull() && !field._section.isNull()) );
 
-  // Add values from field
-  const SieveRealSection::chart_type& chart = _section->getChart();
-  const SieveRealSection::chart_type::const_iterator chartEnd = chart.end();
+  if (!_section.isNull()) {
+    // Add values from field
+    const SieveRealSection::chart_type& chart = _section->getChart();
+    const SieveRealSection::chart_type::const_iterator chartEnd = chart.end();
 
-  // Assume fiber dimension is uniform
-  const int fiberDim = _section->getFiberDimension(*chart.begin());
-  double_array values(fiberDim);
+    // Assume fiber dimension is uniform
+    const int fiberDim = _section->getFiberDimension(*chart.begin());
+    double_array values(fiberDim);
 
-  for (SieveRealSection::chart_type::const_iterator c_iter = chart.begin();
-       c_iter != chartEnd;
-       ++c_iter) {
-    assert(fiberDim == field._section->getFiberDimension(*c_iter));
-    assert(fiberDim == _section->getFiberDimension(*c_iter));
-    field._section->restrictPoint(*c_iter, &values[0], values.size());
-    _section->updateAddPoint(*c_iter, &values[0]);
-  } // for
+    for (SieveRealSection::chart_type::const_iterator c_iter = chart.begin();
+	 c_iter != chartEnd;
+	 ++c_iter) {
+      assert(fiberDim == field._section->getFiberDimension(*c_iter));
+      assert(fiberDim == _section->getFiberDimension(*c_iter));
+      field._section->restrictPoint(*c_iter, &values[0], values.size());
+      _section->updateAddPoint(*c_iter, &values[0]);
+    } // for
+  } // if
 } // operator+=
 
 // ----------------------------------------------------------------------
@@ -198,27 +219,73 @@
     throw std::runtime_error(msg.str());
   } // if
 
-  assert(!_section.isNull());
-  const SieveRealSection::chart_type& chart = _section->getChart();
-  const SieveRealSection::chart_type::const_iterator chartEnd = chart.end();
+  if (!_section.isNull()) {
+    const SieveRealSection::chart_type& chart = _section->getChart();
+    const SieveRealSection::chart_type::const_iterator chartEnd = chart.end();
 
-  // Assume fiber dimension is uniform
-  const int fiberDim = _section->getFiberDimension(*chart.begin());
-  double_array values(fiberDim);
+    // Assume fiber dimension is uniform
+    const int fiberDim = _section->getFiberDimension(*chart.begin());
+    double_array values(fiberDim);
 
-  spatialdata::units::Nondimensional normalizer;
+    spatialdata::units::Nondimensional normalizer;
 
-  for (SieveRealSection::chart_type::const_iterator c_iter = chart.begin();
-       c_iter != chartEnd;
-       ++c_iter) {
-    assert(fiberDim == _section->getFiberDimension(*c_iter));
-
-    _section->restrictPoint(*c_iter, &values[0], values.size());
-    normalizer.dimensionalize(&values[0], values.size(), _scale);
-    _section->updatePoint(*c_iter, &values[0]);
-  } // for
-  
+    for (SieveRealSection::chart_type::const_iterator c_iter = chart.begin();
+	 c_iter != chartEnd;
+	 ++c_iter) {
+      assert(fiberDim == _section->getFiberDimension(*c_iter));
+      
+      _section->restrictPoint(*c_iter, &values[0], values.size());
+      normalizer.dimensionalize(&values[0], values.size(), _scale);
+      _section->updatePoint(*c_iter, &values[0]);
+    } // for
+  } // if
 } // dimensionalize
 
+// ----------------------------------------------------------------------
+// Print field to standard out.
+void
+pylith::topology::Field::view(const char* label)
+{ // view
+  std::string vecFieldString;
+  switch(_vecFieldType)
+    { // switch
+    case SCALAR:
+      vecFieldString = "scalar";
+      break;
+    case VECTOR:
+      vecFieldString = "vector";
+      break;
+    case TENSOR:
+      vecFieldString = "tensor";
+      break;
+    case OTHER:
+      vecFieldString = "other";
+      break;
+    case MULTI_SCALAR:
+      vecFieldString = "multiple scalars";
+      break;
+    case MULTI_VECTOR:
+      vecFieldString = "multiple vectors";
+      break;
+    case MULTI_TENSOR:
+      vecFieldString = "multiple tensors";
+      break;
+    case MULTI_OTHER:
+      vecFieldString = "multiple other values";
+      break;
+    default :
+      std::cerr << "Unknown vector field value '" << _vecFieldType
+		<< "'." << std::endl;
+      assert(0);
+    } // switch
 
+  std::cout << "Viewing field '" << _name << "' "<< label << ".\n"
+	    << "  vector field type: " << vecFieldString << "\n"
+	    << "  scale: " << _scale << "\n"
+	    << "  dimensionalize flag: " << _dimensionsOkay << std::endl;
+  if (!_section.isNull())
+    _section->view(label);
+} // view
+
+
 // End of file 

Modified: short/3D/PyLith/trunk/libsrc/topology/Field.hh
===================================================================
--- short/3D/PyLith/trunk/libsrc/topology/Field.hh	2008-12-05 15:33:09 UTC (rev 13465)
+++ short/3D/PyLith/trunk/libsrc/topology/Field.hh	2008-12-05 19:22:26 UTC (rev 13466)
@@ -96,12 +96,6 @@
    */
   VectorFieldEnum vectorFieldType(void) const;
 
-  /** Set spatial dimension of domain.
-   *
-   * @param value Spatial dimension of domain.
-   */
-  void spaceDim(const int value);
-
   /** Get spatial dimension of domain.
    *
    * @returns Spatial dimension of domain.
@@ -132,6 +126,9 @@
    */
   bool addDimensionOkay(void) const;
 
+  /// Create sieve section.
+  void newSection(void);
+
   /** Create section with same layout (fiber dimension and
    * constraints) as another section. This allows the layout data
    * structures to be reused across multiple fields, reducing memory
@@ -167,7 +164,7 @@
    */
   void dimensionalize(void);
 
-  /** Print section to standard out.
+  /** Print field to standard out.
    *
    * @param label Label for output.
    */
@@ -184,7 +181,6 @@
 
   double _scale; ///< Dimensional scale associated with field
   std::string _name; ///< Name of field
-  int _spaceDim; ///< Spatial dimension of domain
   VectorFieldEnum _vecFieldType; ///< Type of vector field
   bool _dimensionsOkay; ///< Flag indicating it is okay to dimensionalize
 

Modified: short/3D/PyLith/trunk/libsrc/topology/Field.icc
===================================================================
--- short/3D/PyLith/trunk/libsrc/topology/Field.icc	2008-12-05 15:33:09 UTC (rev 13465)
+++ short/3D/PyLith/trunk/libsrc/topology/Field.icc	2008-12-05 19:22:26 UTC (rev 13466)
@@ -49,20 +49,6 @@
   return _vecFieldType;
 }
 
-// Set spatial dimension of domain.
-inline
-void
-pylith::topology::Field::spaceDim(const int value) {
-  _spaceDim = value;
-}
-
-// Get spatial dimension of domain.
-inline
-int
-pylith::topology::Field::spaceDim(void) const {
-  return _spaceDim;
-}
-
 // Set scale for dimensionalizing field.
 inline
 void

Modified: short/3D/PyLith/trunk/libsrc/topology/FieldUniform.cc
===================================================================
--- short/3D/PyLith/trunk/libsrc/topology/FieldUniform.cc	2008-12-05 15:33:09 UTC (rev 13465)
+++ short/3D/PyLith/trunk/libsrc/topology/FieldUniform.cc	2008-12-05 19:22:26 UTC (rev 13466)
@@ -39,7 +39,8 @@
 pylith::topology::FieldUniform::createSection(
 			const ALE::Obj<SieveMesh::label_sequence>& points)
 { // createSection
-  assert(!_section.isNull());
+  if (_section.isNull())
+    newSection();
 
   const SieveMesh::point_type pointMin = 
     *std::min_element(points->begin(), points->end());
@@ -56,7 +57,8 @@
 pylith::topology::FieldUniform::createSection(
 			const SieveRealSection::chart_type& chart)
 { // createSection
-  assert(!_section.isNull());
+  if (_section.isNull())
+    newSection();
 
   _section->setChart(chart);
 

Modified: short/3D/PyLith/trunk/unittests/libtests/topology/TestField.cc
===================================================================
--- short/3D/PyLith/trunk/unittests/libtests/topology/TestField.cc	2008-12-05 15:33:09 UTC (rev 13465)
+++ short/3D/PyLith/trunk/unittests/libtests/topology/TestField.cc	2008-12-05 19:22:26 UTC (rev 13466)
@@ -27,7 +27,6 @@
 namespace pylith {
   namespace topology {
     namespace _TestField {
-      const int spaceDim = 2;
       const int cellDim = 2;
       const int nvertices = 4;
       const int ncells = 1;
@@ -63,6 +62,9 @@
   Field field(mesh.sieveMesh());
 
   const ALE::Obj<SieveMesh::real_section_type>& section = field.section();
+  CPPUNIT_ASSERT(section.isNull());
+
+  field.newSection();
   CPPUNIT_ASSERT(!section.isNull());
 } // testSection
 
@@ -101,14 +103,13 @@
 void
 pylith::topology::TestField::testSpaceDim(void)
 { // testSpaceDim
-  Mesh mesh;
+  Mesh mesh(PETSC_COMM_WORLD, _TestField::cellDim);
+  _buildMesh(&mesh);
   Field field(mesh.sieveMesh());
 
-  CPPUNIT_ASSERT_EQUAL(0, field.spaceDim());
+  std::cout << "MESH DIM: " << mesh.sieveMesh()->getDimension() << std::endl;
 
-  const int spaceDim = 2;
-  field.spaceDim(spaceDim);
-  CPPUNIT_ASSERT_EQUAL(spaceDim, field.spaceDim());
+  CPPUNIT_ASSERT_EQUAL(_TestField::cellDim, field.spaceDim());
 } // testSpaceDim
 
 // ----------------------------------------------------------------------
@@ -154,7 +155,7 @@
     0, 1, 2,  // 3
   };
     
-  Mesh mesh;
+  Mesh mesh(PETSC_COMM_WORLD, _TestField::cellDim);
   _buildMesh(&mesh);
   const ALE::Obj<SieveMesh>& sieveMesh = mesh.sieveMesh();
   const ALE::Obj<SieveMesh::label_sequence>& vertices = 
@@ -163,8 +164,9 @@
   // Create field with atlas to use to create new field
   Field fieldSrc(sieveMesh);
   { // Setup source field
+    fieldSrc.newSection();
     const ALE::Obj<SieveRealSection>& section = fieldSrc.section();
-    const int spaceDim = _TestField::spaceDim;
+    const int spaceDim = _TestField::cellDim;
     section->setChart(SieveMesh::real_section_type::chart_type(
 		  *std::min_element(vertices->begin(), vertices->end()),
 		  *std::max_element(vertices->begin(), vertices->end())+1));
@@ -195,24 +197,292 @@
 // Test clear().
 void
 pylith::topology::TestField::testClear(void)
-{ // testAddDimensionOkay
-  Mesh mesh;
+{ // testClear
+  Mesh mesh(PETSC_COMM_WORLD, _TestField::cellDim);
   Field field(mesh.sieveMesh());
 
-  field.spaceDim(3);
   field.scale(2.0);
   field.vectorFieldType(Field::TENSOR);
   field.addDimensionOkay(true);
   
   field.clear();
 
-  CPPUNIT_ASSERT_EQUAL(3, field._spaceDim); // no change
   CPPUNIT_ASSERT_EQUAL(1.0, field._scale);
   CPPUNIT_ASSERT_EQUAL(Field::OTHER, field._vecFieldType);
   CPPUNIT_ASSERT_EQUAL(false, field._dimensionsOkay);
 } // testClear
 
 // ----------------------------------------------------------------------
+// Test zero().
+void
+pylith::topology::TestField::testZero(void)
+{ // testZero
+  const int fiberDim = 3;
+  const double scale = 2.0;
+  const double valuesNondim[] = {
+    1.1, 2.2, 3.3,
+    1.2, 2.3, 3.4,
+    1.3, 2.4, 3.5,
+    1.4, 2.5, 3.6,
+  };
+
+  Mesh mesh(PETSC_COMM_WORLD, _TestField::cellDim);
+  _buildMesh(&mesh);
+  const ALE::Obj<SieveMesh>& sieveMesh = mesh.sieveMesh();
+  const ALE::Obj<SieveMesh::label_sequence>& vertices = 
+    sieveMesh->depthStratum(0);
+
+  Field field(sieveMesh);
+  field.newSection();
+  const ALE::Obj<SieveRealSection>& section = field.section();
+  const int spaceDim = _TestField::cellDim;
+    
+  section->setChart(SieveMesh::real_section_type::chart_type(
+		  *std::min_element(vertices->begin(), vertices->end()),
+		  *std::max_element(vertices->begin(), vertices->end())+1));
+  section->setFiberDimension(vertices, fiberDim);
+  sieveMesh->allocate(section);
+
+  double_array values(fiberDim);
+  int i = 0;
+  for (SieveMesh::label_sequence::iterator v_iter=vertices->begin();
+       v_iter != vertices->end();
+       ++v_iter) {
+    for (int iDim=0; iDim < fiberDim; ++iDim)
+      values[iDim] = valuesNondim[i++];
+    section->updatePoint(*v_iter, &values[0]);
+  } // for
+
+  field.zero();
+
+  const double tolerance = 1.0e-6;
+  for (SieveMesh::label_sequence::iterator v_iter=vertices->begin();
+       v_iter != vertices->end();
+       ++v_iter) {
+    section->restrictPoint(*v_iter, &values[0], values.size());
+    for (int iDim=0; iDim < fiberDim; ++iDim) {
+      CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, values[iDim], tolerance);
+    } // for
+  } // for
+} // testZero
+
+// ----------------------------------------------------------------------
+// Test complete().
+void
+pylith::topology::TestField::testComplete(void)
+{ // testComplete
+  const int fiberDim = 3;
+  const double scale = 2.0;
+  const double valuesNondim[] = {
+    1.1, 2.2, 3.3,
+    1.2, 2.3, 3.4,
+    1.3, 2.4, 3.5,
+    1.4, 2.5, 3.6,
+  };
+
+  Mesh mesh(PETSC_COMM_WORLD, _TestField::cellDim);
+  _buildMesh(&mesh);
+  const ALE::Obj<SieveMesh>& sieveMesh = mesh.sieveMesh();
+  const ALE::Obj<SieveMesh::label_sequence>& vertices = 
+    sieveMesh->depthStratum(0);
+
+  Field field(sieveMesh);
+  field.newSection();
+  const ALE::Obj<SieveRealSection>& section = field.section();
+  const int spaceDim = _TestField::cellDim;
+    
+  section->setChart(SieveMesh::real_section_type::chart_type(
+		  *std::min_element(vertices->begin(), vertices->end()),
+		  *std::max_element(vertices->begin(), vertices->end())+1));
+  section->setFiberDimension(vertices, fiberDim);
+  sieveMesh->allocate(section);
+
+  double_array values(fiberDim);
+  int i = 0;
+  for (SieveMesh::label_sequence::iterator v_iter=vertices->begin();
+       v_iter != vertices->end();
+       ++v_iter) {
+    for (int iDim=0; iDim < fiberDim; ++iDim)
+      values[iDim] = valuesNondim[i++];
+    section->updatePoint(*v_iter, &values[0]);
+  } // for
+
+  field.complete();
+
+  // Expect no change for this serial test
+  i = 0;
+  const double tolerance = 1.0e-6;
+  for (SieveMesh::label_sequence::iterator v_iter=vertices->begin();
+       v_iter != vertices->end();
+       ++v_iter) {
+    section->restrictPoint(*v_iter, &values[0], values.size());
+    for (int iDim=0; iDim < fiberDim; ++iDim) {
+      CPPUNIT_ASSERT_DOUBLES_EQUAL(valuesNondim[i++], values[iDim], tolerance);
+    } // for
+  } // for
+} // testComplete
+
+// ----------------------------------------------------------------------
+// Test copy().
+void
+pylith::topology::TestField::testCopy(void)
+{ // testCopy
+  const int fiberDim = 3;
+  const double scale = 2.0;
+  const double valuesNondim[] = {
+    1.1, 2.2, 3.3,
+    1.2, 2.3, 3.4,
+    1.3, 2.4, 3.5,
+    1.4, 2.5, 3.6,
+  };
+
+  Mesh mesh(PETSC_COMM_WORLD, _TestField::cellDim);
+  _buildMesh(&mesh);
+  const ALE::Obj<SieveMesh>& sieveMesh = mesh.sieveMesh();
+  const ALE::Obj<SieveMesh::label_sequence>& vertices = 
+    sieveMesh->depthStratum(0);
+
+  Field fieldSrc(sieveMesh);
+  { // Setup source field
+    fieldSrc.newSection();
+    const ALE::Obj<SieveRealSection>& section = fieldSrc.section();
+    const int spaceDim = _TestField::cellDim;
+    
+    section->setChart(SieveMesh::real_section_type::chart_type(
+		  *std::min_element(vertices->begin(), vertices->end()),
+		  *std::max_element(vertices->begin(), vertices->end())+1));
+    section->setFiberDimension(vertices, fiberDim);
+    sieveMesh->allocate(section);
+
+    double_array values(fiberDim);
+    int i = 0;
+    for (SieveMesh::label_sequence::iterator v_iter=vertices->begin();
+	 v_iter != vertices->end();
+	 ++v_iter) {
+      for (int iDim=0; iDim < fiberDim; ++iDim)
+	values[iDim] = valuesNondim[i++];
+      section->updatePoint(*v_iter, &values[0]);
+    } // for
+  } // Setup source field
+
+  Field field(sieveMesh);
+  field.newSection();
+  const ALE::Obj<SieveRealSection>& section = field.section();
+  { // Setup destination field
+    const int spaceDim = _TestField::cellDim;
+    
+    section->setChart(SieveMesh::real_section_type::chart_type(
+		  *std::min_element(vertices->begin(), vertices->end()),
+		  *std::max_element(vertices->begin(), vertices->end())+1));
+    section->setFiberDimension(vertices, fiberDim);
+    sieveMesh->allocate(section);
+  } // Setup destination field
+
+  field.copy(fieldSrc);
+
+  int i = 0;
+  double_array values(fiberDim);
+  const double tolerance = 1.0e-6;
+  for (SieveMesh::label_sequence::iterator v_iter=vertices->begin();
+       v_iter != vertices->end();
+       ++v_iter) {
+    section->restrictPoint(*v_iter, &values[0], values.size());
+    for (int iDim=0; iDim < fiberDim; ++iDim) {
+      CPPUNIT_ASSERT_DOUBLES_EQUAL(valuesNondim[i++], values[iDim], tolerance);
+    } // for
+  } // for
+} // testCopy
+
+// ----------------------------------------------------------------------
+// Test operator+=().
+void
+pylith::topology::TestField::testOperatorAdd(void)
+{ // testOperateAdd
+  const int fiberDim = 3;
+  const double scale = 2.0;
+  const double valuesA[] = {
+    1.1, 2.2, 3.3,
+    1.2, 2.3, 3.4,
+    1.3, 2.4, 3.5,
+    1.4, 2.5, 3.6,
+  };
+  const double valuesB[] = {
+    10.1, 20.2, 30.3,
+    10.2, 20.3, 30.4,
+    10.3, 20.4, 30.5,
+    10.4, 20.5, 30.6,
+  };
+
+  Mesh mesh(PETSC_COMM_WORLD, _TestField::cellDim);
+  _buildMesh(&mesh);
+  const ALE::Obj<SieveMesh>& sieveMesh = mesh.sieveMesh();
+  const ALE::Obj<SieveMesh::label_sequence>& vertices = 
+    sieveMesh->depthStratum(0);
+
+  Field fieldSrc(sieveMesh);
+  { // Setup source field
+    fieldSrc.newSection();
+    const ALE::Obj<SieveRealSection>& section = fieldSrc.section();
+    const int spaceDim = _TestField::cellDim;
+    
+    section->setChart(SieveMesh::real_section_type::chart_type(
+		  *std::min_element(vertices->begin(), vertices->end()),
+		  *std::max_element(vertices->begin(), vertices->end())+1));
+    section->setFiberDimension(vertices, fiberDim);
+    sieveMesh->allocate(section);
+
+    double_array values(fiberDim);
+    int i = 0;
+    for (SieveMesh::label_sequence::iterator v_iter=vertices->begin();
+	 v_iter != vertices->end();
+	 ++v_iter) {
+      for (int iDim=0; iDim < fiberDim; ++iDim)
+	values[iDim] = valuesA[i++];
+      section->updatePoint(*v_iter, &values[0]);
+    } // for
+  } // Setup source field
+
+  Field field(sieveMesh);
+  field.newSection();
+  const ALE::Obj<SieveRealSection>& section = field.section();
+  { // Setup destination field
+    const int spaceDim = _TestField::cellDim;
+    
+    section->setChart(SieveMesh::real_section_type::chart_type(
+		  *std::min_element(vertices->begin(), vertices->end()),
+		  *std::max_element(vertices->begin(), vertices->end())+1));
+    section->setFiberDimension(vertices, fiberDim);
+    sieveMesh->allocate(section);
+
+    double_array values(fiberDim);
+    int i = 0;
+    for (SieveMesh::label_sequence::iterator v_iter=vertices->begin();
+	 v_iter != vertices->end();
+	 ++v_iter) {
+      for (int iDim=0; iDim < fiberDim; ++iDim)
+	values[iDim] = valuesB[i++];
+      section->updatePoint(*v_iter, &values[0]);
+    } // for
+  } // Setup destination field
+
+  field += fieldSrc;
+
+  int i = 0;
+  double_array values(fiberDim);
+  const double tolerance = 1.0e-6;
+  for (SieveMesh::label_sequence::iterator v_iter=vertices->begin();
+       v_iter != vertices->end();
+       ++v_iter) {
+    section->restrictPoint(*v_iter, &values[0], values.size());
+    for (int iDim=0; iDim < fiberDim; ++iDim) {
+      const double valueE = valuesA[i] + valuesB[i];
+      ++i;
+      CPPUNIT_ASSERT_DOUBLES_EQUAL(valueE, values[iDim], tolerance);
+    } // for
+  } // for
+} // testOperateAdd
+
+// ----------------------------------------------------------------------
 // Test dimensionalize().
 void
 pylith::topology::TestField::testDimensionalize(void)
@@ -226,12 +496,13 @@
     1.4, 2.5, 3.6,
   };
 
-  Mesh mesh;
+  Mesh mesh(PETSC_COMM_WORLD, _TestField::cellDim);
   _buildMesh(&mesh);
   const ALE::Obj<SieveMesh>& sieveMesh = mesh.sieveMesh();
   Field field(sieveMesh);
+  field.newSection();
   const ALE::Obj<SieveRealSection>& section = field.section();
-  const int spaceDim = _TestField::spaceDim;
+  const int spaceDim = _TestField::cellDim;
 
   const ALE::Obj<SieveMesh::label_sequence>& vertices = 
     sieveMesh->depthStratum(0);
@@ -246,14 +517,12 @@
   for (SieveMesh::label_sequence::iterator v_iter=vertices->begin();
        v_iter != vertices->end();
        ++v_iter) {
-    section->restrictPoint(*v_iter, &values[0], values.size());
     for (int iDim=0; iDim < fiberDim; ++iDim)
       values[iDim] = valuesNondim[i++];
     section->updatePoint(*v_iter, &values[0]);
   } // for
 
   field.scale(scale);
-  field.spaceDim(spaceDim);
   field.addDimensionOkay(true);
   field.dimensionalize();
 
@@ -272,7 +541,51 @@
 } // testDimensionalize
 
 // ----------------------------------------------------------------------
+// Test view().
 void
+pylith::topology::TestField::testView(void)
+{ // testView
+  const int fiberDim = 3;
+  const double scale = 2.0;
+  const double valuesNondim[] = {
+    1.1, 2.2, 3.3,
+    1.2, 2.3, 3.4,
+    1.3, 2.4, 3.5,
+    1.4, 2.5, 3.6,
+  };
+
+  Mesh mesh(PETSC_COMM_WORLD, _TestField::cellDim);
+  _buildMesh(&mesh);
+  const ALE::Obj<SieveMesh>& sieveMesh = mesh.sieveMesh();
+  const ALE::Obj<SieveMesh::label_sequence>& vertices = 
+    sieveMesh->depthStratum(0);
+
+  Field field(sieveMesh);
+  field.newSection();
+  const ALE::Obj<SieveRealSection>& section = field.section();
+  const int spaceDim = _TestField::cellDim;
+    
+  section->setChart(SieveMesh::real_section_type::chart_type(
+		  *std::min_element(vertices->begin(), vertices->end()),
+		  *std::max_element(vertices->begin(), vertices->end())+1));
+  section->setFiberDimension(vertices, fiberDim);
+  sieveMesh->allocate(section);
+
+  double_array values(fiberDim);
+  int i = 0;
+  for (SieveMesh::label_sequence::iterator v_iter=vertices->begin();
+       v_iter != vertices->end();
+       ++v_iter) {
+    for (int iDim=0; iDim < fiberDim; ++iDim)
+      values[iDim] = valuesNondim[i++];
+    section->updatePoint(*v_iter, &values[0]);
+  } // for
+
+  field.view("Testing view");
+} // testView
+
+// ----------------------------------------------------------------------
+void
 pylith::topology::TestField::_buildMesh(Mesh* mesh)
 { // _buildMesh
   assert(0 != mesh);
@@ -291,7 +604,7 @@
   const int* cells = _TestField::cells;
   const int nvertices = _TestField::nvertices;
   const int ncorners = _TestField::ncorners;
-  const int spaceDim = _TestField::spaceDim;
+  const int spaceDim = _TestField::cellDim;
   const double* coordinates = _TestField::coordinates;
   const bool interpolate = false;
   ALE::SieveBuilder<ALE::Mesh>::buildTopology(s, cellDim, ncells, (int*) cells,

Modified: short/3D/PyLith/trunk/unittests/libtests/topology/TestField.hh
===================================================================
--- short/3D/PyLith/trunk/unittests/libtests/topology/TestField.hh	2008-12-05 15:33:09 UTC (rev 13465)
+++ short/3D/PyLith/trunk/unittests/libtests/topology/TestField.hh	2008-12-05 19:22:26 UTC (rev 13466)
@@ -50,7 +50,12 @@
   CPPUNIT_TEST( testAddDimensionOkay );
   CPPUNIT_TEST( testCopyLayout );
   CPPUNIT_TEST( testClear );
+  CPPUNIT_TEST( testZero );
+  CPPUNIT_TEST( testComplete );
+  CPPUNIT_TEST( testCopy );
+  CPPUNIT_TEST( testOperatorAdd );
   CPPUNIT_TEST( testDimensionalize );
+  CPPUNIT_TEST( testView );
 
   CPPUNIT_TEST_SUITE_END();
 
@@ -60,7 +65,7 @@
   /// Test constructor.
   void testConstructor(void);
 
-  /// Test section().
+  /// Test newSection() and section().
   void testSection(void);
 
   /// Test name().
@@ -84,9 +89,24 @@
   /// Test clear().
   void testClear(void);
 
+  /// Test zero().
+  void testZero(void);
+
+  /// Test complete().
+  void testComplete(void);
+
+  /// Test copy().
+  void testCopy(void);
+
+  /// Test operator+=().
+  void testOperatorAdd(void);
+
   /// Test dimensionalize().
   void testDimensionalize(void);
 
+  /// Test view().
+  void testView(void);
+
 // PRIVATE METHODS /////////////////////////////////////////////////////
 private :
 

Modified: short/3D/PyLith/trunk/unittests/libtests/topology/TestFieldUniform.cc
===================================================================
--- short/3D/PyLith/trunk/unittests/libtests/topology/TestFieldUniform.cc	2008-12-05 15:33:09 UTC (rev 13465)
+++ short/3D/PyLith/trunk/unittests/libtests/topology/TestFieldUniform.cc	2008-12-05 19:22:26 UTC (rev 13466)
@@ -63,7 +63,7 @@
 { // testCreateSectionPoints
   const int fiberDim = 3;
     
-  Mesh mesh;
+  Mesh mesh(PETSC_COMM_WORLD, _TestFieldUniform::cellDim);
   _buildMesh(&mesh);
   const ALE::Obj<SieveMesh>& sieveMesh = mesh.sieveMesh();
   const ALE::Obj<SieveMesh::label_sequence>& vertices = 
@@ -86,7 +86,7 @@
 { // testCreateSectionChart
   const int fiberDim = 2;
     
-  Mesh mesh;
+  Mesh mesh(PETSC_COMM_WORLD, _TestFieldUniform::cellDim);
   _buildMesh(&mesh);
   const ALE::Obj<SieveMesh>& sieveMesh = mesh.sieveMesh();
   const ALE::Obj<SieveMesh::label_sequence>& vertices = 
@@ -95,6 +95,7 @@
   // Create field with to use to create new field
   FieldUniform fieldSrc(sieveMesh, fiberDim);
   { // Setup source field
+    fieldSrc.newSection();
     const ALE::Obj<SieveRealSection>& section = fieldSrc.section();
     section->setChart(SieveMesh::real_section_type::chart_type(
 		  *std::min_element(vertices->begin(), vertices->end()),



More information about the CIG-COMMITS mailing list