[cig-commits] r6928 - in short/3D/PyLith/trunk: . libsrc/topology modulesrc/topology pylith pylith/topology unittests/libtests/bc

brad at geodynamics.org brad at geodynamics.org
Fri May 18 17:33:56 PDT 2007


Author: brad
Date: 2007-05-18 17:33:56 -0700 (Fri, 18 May 2007)
New Revision: 6928

Added:
   short/3D/PyLith/trunk/pylith/topology/FieldsManager.py
Modified:
   short/3D/PyLith/trunk/TODO
   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/Makefile.am
   short/3D/PyLith/trunk/pylith/topology/__init__.py
   short/3D/PyLith/trunk/unittests/libtests/bc/TestDirichlet.cc
Log:
Added Python implementation of PythonManager and bindings. Still need unit testing for C++ and Python.

Modified: short/3D/PyLith/trunk/TODO
===================================================================
--- short/3D/PyLith/trunk/TODO	2007-05-18 22:27:56 UTC (rev 6927)
+++ short/3D/PyLith/trunk/TODO	2007-05-19 00:33:56 UTC (rev 6928)
@@ -13,9 +13,23 @@
   dimensions, we probably want to allow it in MeshIO, just not in the
   simulation where we don't support it.
 
-1. Unit tests for Mesh.py
-  create section, matrix, etc
+0. Create FieldsManager
+  b. C++ unit tests
+  c. Python unit tests
 
+Update TestDirichlet to check constraints.
+  getConstraintDof() IS IMPLEMENTED!
+
+1. Switch to more uniform implementation of integrators.
+
+  Formulation HASA FieldsManager (solnfields)
+
+  Integrators have
+    integrateResidual(residual, solnfields, mesh, timeId)
+    integrateJacobian(jacobian, solnfields, mesh, timeId)
+
+  timeId = {'t+dt', 't', 't-dt'}
+
 2. Finish implementing ExplicitElasticity and Explicit
    a. Replace integrateConstant() with integrateResidual()
 
@@ -26,10 +40,11 @@
 
    c. Add unit test for IntegratorElasticity::calcTotalStrain
 
-2a.
+2a. Cleanup integrators and boundary conditions.
 
-  Add feature to materials and integrators wherein they indicate if
-  the Jacobian needs to be reformed at the current time step.
+   a. Categorize into "integrators" and "constraints"
+   b. Add feature to materials and integrators wherein they indicate
+      if the Jacobian needs to be reformed at the current time step.
 
 3. Add dualBasis to Quadrature.
    a. Python

Modified: short/3D/PyLith/trunk/libsrc/topology/FieldsManager.cc
===================================================================
--- short/3D/PyLith/trunk/libsrc/topology/FieldsManager.cc	2007-05-18 22:27:56 UTC (rev 6927)
+++ short/3D/PyLith/trunk/libsrc/topology/FieldsManager.cc	2007-05-19 00:33:56 UTC (rev 6928)
@@ -65,5 +65,98 @@
   return _real[name];
 } // getReal
 
+// ----------------------------------------------------------------------
+// Remove field.
+void
+pylith::topology::FieldsManager::delReal(const char* name)
+{ // delReal
+  map_real_type::const_iterator iter = _real.find(name);
+  if (iter == _real.end()) {
+    std::ostringstream msg;
+    msg << "Could not find field '" << name << "' to delete.";
+    throw std::runtime_error(msg.str());
+  } // if
+  _real.erase(name);
+} // delReal
 
+// ----------------------------------------------------------------------
+// Copy layout of field to all other fields.
+void
+pylith::topology::FieldsManager::copyLayout(const char* name)
+{ // copyLayout
+  assert(!_mesh.isNull());
+
+  map_real_type::const_iterator src = _real.find(name);
+  if (src == _real.end()) {
+    std::ostringstream msg;
+    msg << "Could not find field '" << name << "'.";
+    throw std::runtime_error(msg.str());
+  } // if
+  
+  assert(!src->second.isNull());
+  const real_section_type::chart_type& srcChart = src->second->getChart();
+  const real_section_type::chart_type::iterator srcBegin = srcChart.begin();
+  const real_section_type::chart_type::iterator srcEnd = srcChart.end();
+  
+  const map_real_type::iterator begin = _real.begin();
+  const map_real_type::iterator end = _real.end();
+  for (map_real_type::iterator iter=begin; iter != end; ++iter)
+    if (iter != src) {
+      // Make sure fields are same size
+      assert(!iter->second.isNull());
+      assert(src->second->sizeWithBC() == iter->second->sizeWithBC());
+      for (real_section_type::chart_type::iterator p_iter=srcBegin;
+	   p_iter != srcEnd;
+	   ++p_iter) {
+	iter->second->setFiberDimension(*p_iter, 
+					src->second->getFiberDimension(*p_iter));
+	iter->second->setConstraintDimension(*p_iter, 
+					     src->second->getConstraintDimension(*p_iter));
+      } // for
+      _mesh->allocate(iter->second);
+      for (real_section_type::chart_type::iterator p_iter=srcBegin;
+	   p_iter != srcEnd;
+	   ++p_iter)
+	iter->second->setConstraintDof(*p_iter, 
+				       src->second->getConstraintDof(*p_iter));
+    } // if
+} // copyLayout
+
+// ----------------------------------------------------------------------
+// Copy layout of field to managed fields.
+void
+pylith::topology::FieldsManager::copyLayout(
+				    const ALE::Obj<real_section_type>& field)
+{ // copyLayout
+  assert(!_mesh.isNull());
+  assert(!field.isNull());
+  
+  const real_section_type::chart_type& srcChart = field->getChart();
+  const real_section_type::chart_type::iterator srcBegin = srcChart.begin();
+  const real_section_type::chart_type::iterator srcEnd = srcChart.end();
+  
+  const map_real_type::iterator begin = _real.begin();
+  const map_real_type::iterator end = _real.end();
+  for (map_real_type::iterator iter=begin; iter != end; ++iter) {
+    // Make sure fields are same size
+    assert(!iter->second.isNull());
+    assert(field->sizeWithBC() == iter->second->sizeWithBC());
+    for (real_section_type::chart_type::iterator p_iter=srcBegin;
+	 p_iter != srcEnd;
+	 ++p_iter) {
+      iter->second->setFiberDimension(*p_iter, 
+				      field->getFiberDimension(*p_iter));
+      iter->second->setConstraintDimension(*p_iter, 
+					   field->getConstraintDimension(*p_iter));
+    } // for
+    _mesh->allocate(iter->second);
+    for (real_section_type::chart_type::iterator p_iter=srcBegin;
+	 p_iter != srcEnd;
+	 ++p_iter)
+      iter->second->setConstraintDof(*p_iter, 
+				     field->getConstraintDof(*p_iter));
+  } // for
+} // copyLayout
+
+
 // End of file 

Modified: short/3D/PyLith/trunk/libsrc/topology/FieldsManager.hh
===================================================================
--- short/3D/PyLith/trunk/libsrc/topology/FieldsManager.hh	2007-05-18 22:27:56 UTC (rev 6927)
+++ short/3D/PyLith/trunk/libsrc/topology/FieldsManager.hh	2007-05-19 00:33:56 UTC (rev 6928)
@@ -45,18 +45,36 @@
   /// Destructor
   ~FieldsManager(void);
 
-  /** Add fields.
+  /** Add field.
    *
-   * @param name Name of fields field
+   * @param name Name of field.
    */
   void addReal(const char* name);
 
-  /** Get fields.
+  /** Get field.
    *
-   * @param name Name of fields field
+   * @param name Name of field.
    */
   const ALE::Obj<real_section_type>& getReal(const char* name);
 
+  /** Remove field.
+   *
+   * @param name Name of field.
+   */
+  void delReal(const char* name);
+
+  /** Copy layout of field to all other fields.
+   *
+   * @param name Name of field.
+   */
+  void copyLayout(const char* name);
+
+  /** Copy layout of field to managed fields.
+   *
+   * @param field Field from which to copy layout.
+   */
+  void copyLayout(const ALE::Obj<real_section_type>& field);
+
 // NOT IMPLEMENTED //////////////////////////////////////////////////////
 private :
 

Modified: short/3D/PyLith/trunk/modulesrc/topology/topology.pyxe.src
===================================================================
--- short/3D/PyLith/trunk/modulesrc/topology/topology.pyxe.src	2007-05-18 22:27:56 UTC (rev 6927)
+++ short/3D/PyLith/trunk/modulesrc/topology/topology.pyxe.src	2007-05-19 00:33:56 UTC (rev 6928)
@@ -11,6 +11,7 @@
 #
 
 #header{
+#include "pylith/topology/FieldsManager.hh"
 #include "pylith/utils/sievetypes.hh"
 #include "pylith/utils/petscfwd.h"
 #include <Generator.hh>
@@ -65,6 +66,17 @@
   PetscMat_destructor_cpp(obj)
   return
 
+cdef void FieldsManager_destructor(void* obj):
+  """
+  Destroy FieldsManager.
+  """
+  #embed{ void FieldsManager_destructor_cpp(void* objVptr)
+  pylith::topology::FieldsManager* manager = (pylith::topology::FieldsManager*) objVptr;
+  delete manager;
+  #}embed
+  FieldsManager_destructor_cpp(obj)
+  return
+
 # ----------------------------------------------------------------------
 
 cimport mpi
@@ -113,15 +125,13 @@
     Get real section from mesh.
     """
     # create shim for getRealSection
-    #embed{ void* Mesh_getRealSection(void* objVptr, char* label)
-    typedef ALE::Mesh::real_section_type real_section_type;
-    
+    #embed{ void* Mesh_getRealSection(void* objVptr, char* label)    
     void* result = 0;
     try {
       ALE::Obj<ALE::Mesh>* mesh = (ALE::Obj<ALE::Mesh>*) objVptr;
       assert(0 != mesh);
       assert(!mesh->isNull());
-      const ALE::Obj<real_section_type>& section =
+      const ALE::Obj<pylith::real_section_type>& section =
         (*mesh)->getRealSection(label);
       assert(!section.isNull());
       result = (void*) &section;
@@ -148,14 +158,12 @@
     """
     # create shim for createRealSection
     #embed{ void* Mesh_createRealSection(void* objVptr, char* label, int fiberDim)
-    typedef ALE::Mesh::real_section_type real_section_type;
-    
     void* result = 0;
     try {
       ALE::Obj<ALE::Mesh>* mesh = (ALE::Obj<ALE::Mesh>*) objVptr;
       assert(0 != mesh);
       assert(!mesh->isNull());
-      const ALE::Obj<real_section_type>& section =
+      const ALE::Obj<pylith::real_section_type>& section =
         (*mesh)->getRealSection(label);
       assert(!section.isNull());
       section->setFiberDimension((*mesh)->depthStratum(0), fiberDim);
@@ -183,8 +191,6 @@
     """
     # create shim for allocate
     #embed{ void* Mesh_allocateRealSection(void* objVptr, void* sectionVptr)
-    typedef ALE::Mesh::real_section_type real_section_type;
-    
     try {
       ALE::Obj<ALE::Mesh>* mesh = (ALE::Obj<ALE::Mesh>*) objVptr;
       ALE::Obj<pylith::real_section_type>* section =
@@ -217,15 +223,13 @@
     """
     # create shim for MeshCreateMatrix
     #embed{ void* Mesh_createMatrix(void* objVptr, void* fieldVptr)
-    typedef ALE::Mesh::real_section_type real_section_type;
-    
     void* result = 0;
     try {
       ALE::Obj<ALE::Mesh>* mesh = (ALE::Obj<ALE::Mesh>*) objVptr;
       assert(0 != mesh);
       assert(!mesh->isNull());
-      const ALE::Obj<real_section_type>* field =
-        (ALE::Obj<real_section_type>*) fieldVptr;
+      const ALE::Obj<pylith::real_section_type>* field =
+        (ALE::Obj<pylith::real_section_type>*) fieldVptr;
       assert(!field->isNull());
 
       PetscMat* mat = new PetscMat;
@@ -478,6 +482,184 @@
 
 
 # ----------------------------------------------------------------------
+cdef class FieldsManager:
+
+  cdef void* thisptr # Pointer to C++ object
+  cdef readonly object handle # PyCObject holding pointer to C++ object
+  cdef readonly object name # Identifier for object base type
+
+  def __init__(self):
+    """
+    Constructor.
+    """
+    # create shim for constructor
+    #embed{ void* FieldsManager_constructor(void* meshVptr)
+    void* result = 0;
+    try {
+      ALE::Obj<ALE::Mesh>* mesh = (ALE::Obj<ALE::Mesh>*) meshVptr;
+      assert(0 != mesh);
+      assert(!mesh->isNull());
+      pylith::topology::FieldsManager* manager =
+        new pylith::topology::FieldsManager(*mesh);
+      result = (void*) manager;
+    } 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
+
+    self.thisptr = FieldsManager_constructor(ptrFromHandle(mesh))
+    self.handle = self._createHandle()
+    self.name = "pylith_topology_FieldsManager"
+    return
+
+
+  def getReal(self, label):
+    """
+    Get real field over mesh.
+    """
+    # create shim for getReal
+    #embed{ void* Mesh_getReal(void* objVptr, char* label)
+    void* result = 0;
+    try {
+      pylith::topology::FieldsManager* manager =
+      (pylith::topology::FieldsManager*) objVptr;
+      assert(0 != manager);
+      const ALE::Obj<pylith::real_section_type>& field =
+        manager->getReal(label);
+      assert(!field.isNull());
+      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 = Mesh_getReal(self.thisptr, label)
+    return PyCObject_FromVoidPtr(ptr, NULL)
+    
+
+  def addReal(self, label):
+    """
+    Create real section over mesh.
+    """
+    # create shim for addReal
+    #embed{ void Mesh_addReal(void* objVptr, char* label)
+    try {
+      assert(0 != objVptr);
+      ((pylith::topology::FieldsManager*) objVptr)->addReal(label);
+    } 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
+    Mesh_addReal(self.thisptr, label)
+    return
+
+
+  def delReal(self, label):
+    """
+    Delete real section over mesh.
+    """
+    # create shim for delReal
+    #embed{ void Mesh_delReal(void* objVptr, char* label)
+    try {
+      assert(0 != objVptr);
+      ((pylith::topology::FieldsManager*) objVptr)->delReal(label);
+    } 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
+    Mesh_delReal(self.thisptr, label)
+    return
+
+
+  def copyLayout(self, label):
+    """
+    Copy layout of given section to all other sections in manager.
+    """
+    # create shim for copyLayout
+    #embed{ void Mesh_copyLayout(void* objVptr, char* label)
+    try {
+      assert(0 != objVptr);
+      ((pylith::topology::FieldsManager*) objVptr)->copyLayout(label);
+    } 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
+    Mesh_copyLayout(self.thisptr, label)
+    return
+
+
+  def copyLayoutFromSrc(self, field):
+    """
+    Copy layout of section to all sections in manager.
+    """
+    # create shim for copyLayout
+    #embed{ void Mesh_copyLayoutFromSrc(void* objVptr, void* fieldVptr)
+    try {
+      assert(0 != objVptr);
+      assert(0 != fieldVptr);
+      const ALE::Obj<pylith::real_section_type>* field =
+        (ALE::Obj<pylith::real_section_type>*) fieldVptr;
+      ((pylith::topology::FieldsManager*) objVptr)->copyLayout(*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
+    #}embed
+    Mesh_copyLayoutFromSrc(self.thisptr, PyCObject_AsVoidPtr(field))
+    return
+
+
+  def _createHandle(self):
+    """
+    Wrap pointer to C++ object in PyCObject.
+    """
+    return PyCObject_FromVoidPtr(self.thisptr, FieldsManager_destructor)
+    
+
+# ----------------------------------------------------------------------
 def zeroRealSection(section):
   """
   Zero real section.

Modified: short/3D/PyLith/trunk/pylith/Makefile.am
===================================================================
--- short/3D/PyLith/trunk/pylith/Makefile.am	2007-05-18 22:27:56 UTC (rev 6927)
+++ short/3D/PyLith/trunk/pylith/Makefile.am	2007-05-19 00:33:56 UTC (rev 6928)
@@ -74,6 +74,7 @@
 	solver/SolverLinear.py \
 	topology/__init__.py \
 	topology/Distributor.py \
+	topology/FieldsManager.py \
 	topology/Mesh.py \
 	topology/MeshGenerator.py \
 	topology/MeshImporter.py \

Added: short/3D/PyLith/trunk/pylith/topology/FieldsManager.py
===================================================================
--- short/3D/PyLith/trunk/pylith/topology/FieldsManager.py	2007-05-18 22:27:56 UTC (rev 6927)
+++ short/3D/PyLith/trunk/pylith/topology/FieldsManager.py	2007-05-19 00:33:56 UTC (rev 6928)
@@ -0,0 +1,74 @@
+#!/usr/bin/env python
+#
+# ----------------------------------------------------------------------
+#
+#                           Brad T. Aagaard
+#                        U.S. Geological Survey
+#
+# <LicenseText>
+#
+# ----------------------------------------------------------------------
+#
+
+## @file pylith/topology/FieldsManager.py
+##
+## @brief Python manager for fields over a mesh.
+
+# FieldsManager class
+class FieldsManager(object):
+  """
+  Python manager for fields over a mesh.
+  """
+
+  # PUBLIC METHODS /////////////////////////////////////////////////////
+
+  def __init__(self, mesh):
+    """
+    Constructor.
+    """
+    import pylith.topology.topology as bindings
+    self.cppHandle = bindings.FieldsManager(mesh.cppHandle)
+    return
+
+
+  def addReal(self, label):
+    """
+    Create real field over mesh.
+    """
+    assert(None != self.cppHandle)
+    return self.cppHandle.addReal(label)
+
+
+  def getReal(self, label):
+    """
+    Get real field over mesh.
+    """
+    assert(None != self.cppHandle)
+    return self.cppHandle.getReal(label)
+
+
+  def delReal(self, label):
+    """
+    Delete real field over mesh.
+    """
+    assert(None != self.cppHandle)
+    return self.cppHandle.delReal(label)
+
+
+  def copyLayout(self, label):
+    """
+    Copy layout of field to all fields in manager.
+    """
+    assert(None != self.cppHandle)
+    return self.cppHandle.copyLayout(label)
+
+
+  def copyLayoutFromSrc(self, field):
+    """
+    Copy layout of field to all fields in manager..
+    """
+    assert(None != self.cppHandle)
+    return self.cppHandle.copyLayoutFromSrc(label)
+
+
+# End of file 

Modified: short/3D/PyLith/trunk/pylith/topology/__init__.py
===================================================================
--- short/3D/PyLith/trunk/pylith/topology/__init__.py	2007-05-18 22:27:56 UTC (rev 6927)
+++ short/3D/PyLith/trunk/pylith/topology/__init__.py	2007-05-19 00:33:56 UTC (rev 6928)
@@ -14,7 +14,9 @@
 
 ## @brief Python PyLith finite-element topology module initialization
 
-__all__ = ['Mesh',
+__all__ = ['Distributor',
+           'FieldsManager',
+           'Mesh',
            'MeshDistributor',
            'MeshGenerator',
            'MeshImporter']

Modified: short/3D/PyLith/trunk/unittests/libtests/bc/TestDirichlet.cc
===================================================================
--- short/3D/PyLith/trunk/unittests/libtests/bc/TestDirichlet.cc	2007-05-18 22:27:56 UTC (rev 6927)
+++ short/3D/PyLith/trunk/unittests/libtests/bc/TestDirichlet.cc	2007-05-19 00:33:56 UTC (rev 6928)
@@ -131,6 +131,9 @@
   mesh->allocate(field);
   bc.setConstraints(field, mesh);
 
+  // ADD STUFF HERE
+  // use getConstraintDof()
+
   // No accessor in real_section_type to verify constraints are set
   // correctly. For now, rely on testSetField test.
 } // testSetConstraints



More information about the cig-commits mailing list