[cig-commits] r6956 - in short/3D/PyLith/trunk: libsrc/topology
modulesrc/topology pylith/topology unittests/libtests/topology
brad at geodynamics.org
brad at geodynamics.org
Thu May 24 15:58:33 PDT 2007
Author: brad
Date: 2007-05-24 15:58:33 -0700 (Thu, 24 May 2007)
New Revision: 6956
Modified:
short/3D/PyLith/trunk/libsrc/topology/FieldsManager.cc
short/3D/PyLith/trunk/libsrc/topology/FieldsManager.hh
short/3D/PyLith/trunk/modulesrc/topology/topology.pyxe.src
short/3D/PyLith/trunk/pylith/topology/FieldsManager.py
short/3D/PyLith/trunk/unittests/libtests/topology/TestFieldsManager.cc
short/3D/PyLith/trunk/unittests/libtests/topology/TestFieldsManager.hh
Log:
Added history manager for subset of fields to FieldsManager.
Modified: short/3D/PyLith/trunk/libsrc/topology/FieldsManager.cc
===================================================================
--- short/3D/PyLith/trunk/libsrc/topology/FieldsManager.cc 2007-05-24 21:02:04 UTC (rev 6955)
+++ short/3D/PyLith/trunk/libsrc/topology/FieldsManager.cc 2007-05-24 22:58:33 UTC (rev 6956)
@@ -208,5 +208,52 @@
} // for
} // copyLayout
+// ----------------------------------------------------------------------
+// Create history manager for a subset of the managed fields.
+void
+pylith::topology::FieldsManager::createHistory(const char** fields,
+ const int size)
+{ // createHistory
+ if (size > 0 && 0 != fields) {
+ _history.resize(size);
+ for (int i=0; i < size; ++i) {
+ map_real_type::const_iterator iter = _real.find(fields[i]);
+ if (iter == _real.end()) {
+ std::ostringstream msg;
+ msg << "Cannot use unknown field '" << fields[i]
+ << "' when creating history.";
+ throw std::runtime_error(msg.str());
+ } // if
+ _history[i] = fields[i];
+ } // for
+ } // if
+} // createHistory
+// ----------------------------------------------------------------------
+// Shift fields in history. Handles to fields are shifted so that the
+// most recent values become associated with the second most recent
+// item in the history, etc.
+void
+pylith::topology::FieldsManager::shiftHistory(void)
+{ // shiftHistory
+
+ assert(_history.size() > 0);
+ const int size = _history.size();
+ ALE::Obj<real_section_type> tmp = _real[_history[size-1]];
+ tmp.addRef();
+ for (int i=size-1; i > 0; --i)
+ _real[_history[i]] = _real[_history[i-1]];
+ _real[_history[0]] = tmp;
+} // shiftHistory
+
+// ----------------------------------------------------------------------
+// Get field in history by position.
+const ALE::Obj<pylith::real_section_type>&
+pylith::topology::FieldsManager::getHistoryItem(const int index)
+{ // getHistoryItem
+ assert(index < _history.size());
+ return getReal(_history[index].c_str());
+} // getHistoryItem
+
+
// End of file
Modified: short/3D/PyLith/trunk/libsrc/topology/FieldsManager.hh
===================================================================
--- short/3D/PyLith/trunk/libsrc/topology/FieldsManager.hh 2007-05-24 21:02:04 UTC (rev 6955)
+++ short/3D/PyLith/trunk/libsrc/topology/FieldsManager.hh 2007-05-24 22:58:33 UTC (rev 6956)
@@ -22,6 +22,7 @@
#include <map> // HASA std::map
#include <string> // USES std::string
+#include <vector> // USES std::vector
#include "pylith/utils/sievetypes.hh" // USES PETSc Mesh
@@ -92,6 +93,26 @@
*/
void copyLayout(const ALE::Obj<real_section_type>& field);
+ /** Create history manager for a subset of the managed fields.
+ *
+ * @param fields Fields in history (first is most recent).
+ * @param size Number of fields in history.
+ */
+ void createHistory(const char** fields,
+ const int size);
+
+ /** Shift fields in history. Handles to fields are shifted so that
+ * the most recent values become associated with the second most
+ * recent item in the history, etc.
+ */
+ void shiftHistory(void);
+
+ /** Get field in history by position.
+ *
+ * @param index Index in history [0=most recent, 1=previous, etc]
+ */
+ const ALE::Obj<real_section_type>& getHistoryItem(const int index);
+
// NOT IMPLEMENTED //////////////////////////////////////////////////////
private :
@@ -115,6 +136,9 @@
/// Map for fieldss stored as real fields
map_real_type _real;
+ /// History manager for a subset of the fields
+ std::vector<std::string> _history;
+
}; // FieldsManager
#endif // pylith_topology_fieldsmanager_hh
Modified: short/3D/PyLith/trunk/modulesrc/topology/topology.pyxe.src
===================================================================
--- short/3D/PyLith/trunk/modulesrc/topology/topology.pyxe.src 2007-05-24 21:02:04 UTC (rev 6955)
+++ short/3D/PyLith/trunk/modulesrc/topology/topology.pyxe.src 2007-05-24 22:58:33 UTC (rev 6956)
@@ -700,6 +700,89 @@
return
+ def createHistory(self, labels):
+ """
+ Create history manager for a subset of the managed fields.
+ """
+ # create shim for createHistory
+ #embed{ void FieldsManager_createHistory(void* objVptr, char** labels, int size)
+ try {
+ assert(0 != objVptr);
+ ((pylith::topology::FieldsManager*) objVptr)->createHistory((const char**) labels, size);
+ } catch (const std::exception& err) {
+ PyErr_SetString(PyExc_RuntimeError,
+ const_cast<char*>(err.what()));
+ } catch (const ALE::Exception& err) {
+ PyErr_SetString(PyExc_RuntimeError,
+ const_cast<char*>(err.msg().c_str()));
+ } catch (...) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "Caught unknown C++ exception.");
+ } // try/catch
+ #}embed
+ cdef char** labelsPtr
+ size = len(labels)
+ labelsPtr = <char**> malloc(size*sizeof(char*))
+ for i from 0 <= i < size:
+ labelsPtr[i] = labels[i]
+ FieldsManager_createHistory(self.thisptr, labelsPtr, size)
+ free(labelsPtr)
+ return
+
+
+ def shiftHistory(self):
+ """
+ Shift fields in history.
+ """
+ # create shim for shiftHistory
+ #embed{ void FieldsManager_shiftHistory(void* objVptr)
+ try {
+ assert(0 != objVptr);
+ ((pylith::topology::FieldsManager*) objVptr)->shiftHistory();
+ } catch (const std::exception& err) {
+ PyErr_SetString(PyExc_RuntimeError,
+ const_cast<char*>(err.what()));
+ } catch (const ALE::Exception& err) {
+ PyErr_SetString(PyExc_RuntimeError,
+ const_cast<char*>(err.msg().c_str()));
+ } catch (...) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "Caught unknown C++ exception.");
+ } // try/catch
+ #}embed
+ FieldsManager_shiftHistory(self.thisptr)
+ return
+
+
+ def getHistoryItem(self, index):
+ """
+ Get field in history by position.
+ """
+ # create shim for getHistoryItem
+ #embed{ void* FieldsManager_getHistoryItem(void* objVptr, int index)
+ void* result = 0;
+ try {
+ assert(0 != objVptr);
+ const ALE::Obj<pylith::real_section_type>& field =
+ ((pylith::topology::FieldsManager*) objVptr)->getHistoryItem(index);
+ result = (void*) &field;
+ } catch (const std::exception& err) {
+ PyErr_SetString(PyExc_RuntimeError,
+ const_cast<char*>(err.what()));
+ } catch (const ALE::Exception& err) {
+ PyErr_SetString(PyExc_RuntimeError,
+ const_cast<char*>(err.msg().c_str()));
+ } catch (...) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "Caught unknown C++ exception.");
+ } // try/catch
+ return result;
+ #}embed
+ cdef void* ptr
+ ptr = FieldsManager_getHistoryItem(self.thisptr, index)
+ return PyCObject_FromVoidPtr(ptr, NULL)
+
+
def _createHandle(self):
"""
Wrap pointer to C++ object in PyCObject.
Modified: short/3D/PyLith/trunk/pylith/topology/FieldsManager.py
===================================================================
--- short/3D/PyLith/trunk/pylith/topology/FieldsManager.py 2007-05-24 21:02:04 UTC (rev 6955)
+++ short/3D/PyLith/trunk/pylith/topology/FieldsManager.py 2007-05-24 22:58:33 UTC (rev 6956)
@@ -89,4 +89,28 @@
return self.cppHandle.copyLayoutFromSrc(field)
+ def createHistory(self, labels):
+ """
+ Create history manager for a subset of the managed fields.
+ """
+ assert(None != self.cppHandle)
+ self.cppHandle.createHistory(labels)
+
+
+ def shiftHistory(self):
+ """
+ Shift fields in history.
+ """
+ assert(None != self.cppHandle)
+ self.cppHandle.shiftHistory()
+
+
+ def getHistoryItem(self, index):
+ """
+ Get field in history by position.
+ """
+ assert(None != self.cppHandle)
+ return self.cppHandle.getHistoryItem(index)
+
+
# End of file
Modified: short/3D/PyLith/trunk/unittests/libtests/topology/TestFieldsManager.cc
===================================================================
--- short/3D/PyLith/trunk/unittests/libtests/topology/TestFieldsManager.cc 2007-05-24 21:02:04 UTC (rev 6955)
+++ short/3D/PyLith/trunk/unittests/libtests/topology/TestFieldsManager.cc 2007-05-24 22:58:33 UTC (rev 6956)
@@ -253,7 +253,95 @@
} // testCopyLayoutFromField
// ----------------------------------------------------------------------
+// Test createHistory().
void
+pylith::topology::TestFieldsManager::testCreateHistory(void)
+{ // testCreateHistory
+ ALE::Obj<Mesh> mesh;
+ _initialize(&mesh);
+ FieldsManager manager(mesh);
+
+ const char* labels[] = { "field A", "field B", "field C" };
+ const int totalSize = 3;
+ const int historySize = 2;
+
+ // Add fields
+ for (int i=0; i < totalSize; ++i)
+ manager.addReal(labels[i]);
+
+ manager.createHistory(labels, historySize);
+ for (int i=0; i < historySize; ++i)
+ CPPUNIT_ASSERT_EQUAL(std::string(labels[i]), manager._history[i]);
+} // testCreateHistory
+
+// ----------------------------------------------------------------------
+// Test shiftHistory().
+void
+pylith::topology::TestFieldsManager::testShiftHistory(void)
+{ // testShiftHistory
+ ALE::Obj<Mesh> mesh;
+ _initialize(&mesh);
+ FieldsManager manager(mesh);
+
+ const char* labels[] = { "field A", "field B" };
+ const int size = 2;
+ const int fiberDimA = 2;
+ const int fiberDimB = 3;
+
+ for (int i=0; i < size; ++i)
+ manager.addReal(labels[i]);
+ manager.createHistory(labels, size);
+
+ const ALE::Obj<Mesh::label_sequence>& vertices = mesh->depthStratum(0);
+ const ALE::Obj<real_section_type>& fieldA = manager.getReal(labels[0]);
+ const ALE::Obj<real_section_type>& fieldB = manager.getReal(labels[1]);
+ fieldA->setFiberDimension(vertices, fiberDimA);
+ fieldB->setFiberDimension(vertices, fiberDimB);
+
+ manager.shiftHistory();
+ const ALE::Obj<real_section_type>& testA = manager.getReal(labels[0]);
+ const ALE::Obj<real_section_type>& testB = manager.getReal(labels[1]);
+ CPPUNIT_ASSERT_EQUAL(fiberDimB,
+ testA->getFiberDimension(*(vertices->begin())));
+ CPPUNIT_ASSERT_EQUAL(fiberDimA,
+ testB->getFiberDimension(*(vertices->begin())));
+} // testShiftHistory
+
+// ----------------------------------------------------------------------
+// Test getHistoryItem().
+void
+pylith::topology::TestFieldsManager::testGetHistoryItem(void)
+{ // testGetHistoryItem
+ ALE::Obj<Mesh> mesh;
+ _initialize(&mesh);
+ FieldsManager manager(mesh);
+
+ const char* labels[] = { "field A", "field B" };
+ const int size = 2;
+ const int fiberDimA = 2;
+ const int fiberDimB = 3;
+
+ for (int i=0; i < size; ++i)
+ manager.addReal(labels[i]);
+ manager.createHistory(labels, size);
+
+ const ALE::Obj<Mesh::label_sequence>& vertices = mesh->depthStratum(0);
+ const ALE::Obj<real_section_type>& fieldA = manager.getReal(labels[0]);
+ const ALE::Obj<real_section_type>& fieldB = manager.getReal(labels[1]);
+ fieldA->setFiberDimension(vertices, fiberDimA);
+ fieldB->setFiberDimension(vertices, fiberDimB);
+
+ const ALE::Obj<real_section_type>& testA = manager.getHistoryItem(0);
+ CPPUNIT_ASSERT_EQUAL(fiberDimA,
+ testA->getFiberDimension(*(vertices->begin())));
+
+ const ALE::Obj<real_section_type>& testB = manager.getHistoryItem(1);
+ CPPUNIT_ASSERT_EQUAL(fiberDimB,
+ testB->getFiberDimension(*(vertices->begin())));
+} // testGetHistoryItem
+
+// ----------------------------------------------------------------------
+void
pylith::topology::TestFieldsManager::_initialize(ALE::Obj<Mesh>* mesh) const
{ // _initialize
CPPUNIT_ASSERT(0 != mesh);
Modified: short/3D/PyLith/trunk/unittests/libtests/topology/TestFieldsManager.hh
===================================================================
--- short/3D/PyLith/trunk/unittests/libtests/topology/TestFieldsManager.hh 2007-05-24 21:02:04 UTC (rev 6955)
+++ short/3D/PyLith/trunk/unittests/libtests/topology/TestFieldsManager.hh 2007-05-24 22:58:33 UTC (rev 6956)
@@ -48,6 +48,9 @@
CPPUNIT_TEST( testAllocate );
CPPUNIT_TEST( testCopyLayout );
CPPUNIT_TEST( testCopyLayoutFromField );
+ CPPUNIT_TEST( testCreateHistory );
+ CPPUNIT_TEST( testShiftHistory );
+ CPPUNIT_TEST( testGetHistoryItem );
CPPUNIT_TEST_SUITE_END();
// PUBLIC METHODS /////////////////////////////////////////////////////
@@ -77,6 +80,15 @@
/// Test copyLayoutFromField().
void testCopyLayoutFromField(void);
+ /// Test createHistory().
+ void testCreateHistory(void);
+
+ /// Test shiftHistory().
+ void testShiftHistory(void);
+
+ /// Test getHistoryItem().
+ void testGetHistoryItem(void);
+
// PRIVATE METHODS ////////////////////////////////////////////////////
private :
More information about the cig-commits
mailing list