[cig-commits] r14415 - short/3D/PyLith/branches/pylith-swig/libsrc/problems

brad at geodynamics.org brad at geodynamics.org
Sun Mar 22 15:49:25 PDT 2009


Author: brad
Date: 2009-03-22 15:49:24 -0700 (Sun, 22 Mar 2009)
New Revision: 14415

Added:
   short/3D/PyLith/branches/pylith-swig/libsrc/problems/SolverNonlinear.cc
Modified:
   short/3D/PyLith/branches/pylith-swig/libsrc/problems/SolverNonlinear.hh
Log:
Started implementing wrapper for SNES.

Added: short/3D/PyLith/branches/pylith-swig/libsrc/problems/SolverNonlinear.cc
===================================================================
--- short/3D/PyLith/branches/pylith-swig/libsrc/problems/SolverNonlinear.cc	                        (rev 0)
+++ short/3D/PyLith/branches/pylith-swig/libsrc/problems/SolverNonlinear.cc	2009-03-22 22:49:24 UTC (rev 14415)
@@ -0,0 +1,98 @@
+// -*- C++ -*-
+//
+// ======================================================================
+//
+//                           Brad T. Aagaard
+//                        U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ======================================================================
+//
+
+#include <portinfo>
+
+#include "SolverNonlinear.hh" // implementation of class methods
+
+#include "pylith/topology/SolutionFields.hh" // USES SolutionFields
+#include "pylith/topology/Jacobian.hh" // USES Jacobian
+
+#include <petscsnes.h> // USES PetscSNES
+
+#include "pylith/utils/petscerror.h" // USES CHECK_PETSC_ERROR
+
+// ----------------------------------------------------------------------
+// Constructor
+pylith::problems::SolverNonlinear::SolverNonlinear(void) :
+  _snes(0)
+{ // constructor
+} // constructor
+
+// ----------------------------------------------------------------------
+// Destructor
+pylith::problems::SolverNonlinear::~SolverNonlinear(void)
+{ // destructor
+  if (0 != _snes) {
+    PetscErrorCode err = SNESDestroy(_snes); _snes = 0;
+    CHECK_PETSC_ERROR(err);
+  } // if
+} // destructor
+
+// ----------------------------------------------------------------------
+// Initialize solver.
+void
+pylith::problems::SolverNonlinear::initialize(
+			             topology::SolutionFields* fields,
+				     const topology::Jacobian& jacobian,
+				     Formulation::ResidualFn* residualFn,
+				     Formulation::ArgsResidual* argsResidual,
+				     Formulation::JacobianFn* jacobianFn,
+				     Formulation::ArgsJacobian* argsJacobian)
+{ // initialize
+  assert(0 != fields);
+
+  Solver::initialize(fields);
+
+  PetscErrorCode err = 0;
+  if (0 != _snes) {
+    err = SNESDestroy(_snes); _snes = 0;
+    CHECK_PETSC_ERROR(err);
+  } // if    
+  err = SNESCreate(fields->mesh().comm(), &_snes); CHECK_PETSC_ERROR(err);
+  err = SNESSetFromOptions(_snes); CHECK_PETSC_ERROR(err);
+
+  const topology::Field<topology::Mesh>& residual = fields->residual();
+  const PetscVec residualVec = residual.vector();
+  err = SNESSetFunction(_snes, residualVec, residualFn, argsResidual);
+  CHECK_PETSC_ERROR(err);
+
+  const PetscMat jacobianMat = jacobian.matrix();
+  err = SNESSetJacobian(_snes, jacobianMat, jacobianMat,
+			jacobianFn, argsJacobian);
+  CHECK_PETSC_ERROR(err);
+} // initialize
+
+// ----------------------------------------------------------------------
+// Solve the system.
+void
+pylith::problems::SolverNonlinear::solve(
+			      topology::Field<topology::Mesh>* solution,
+			      const topology::Jacobian& jacobian,
+			      const topology::Field<topology::Mesh>& residual)
+{ // solve
+  assert(0 != solution);
+
+  PetscErrorCode err = 0;
+
+  // Update PetscVector view of field.
+  residual.scatterSectionToVector();
+
+  const PetscVec solutionVec = solution->vector();
+  err = SNESSolve(_ksp, residualVec, solutionVec); CHECK_PETSC_ERROR(err);
+
+  // Update section view of field.
+  solution->scatterVectorToSection();
+} // solve
+
+
+// End of file

Modified: short/3D/PyLith/branches/pylith-swig/libsrc/problems/SolverNonlinear.hh
===================================================================
--- short/3D/PyLith/branches/pylith-swig/libsrc/problems/SolverNonlinear.hh	2009-03-22 15:36:18 UTC (rev 14414)
+++ short/3D/PyLith/branches/pylith-swig/libsrc/problems/SolverNonlinear.hh	2009-03-22 22:49:24 UTC (rev 14415)
@@ -13,7 +13,8 @@
 /**
  * @file pylith/problems/SolverNonlinear.hh
  *
- * @brief Object for using PETSc scalable nonlinear equation solvers (SNES).
+ * @brief Object for using PETSc scalable nonlinear equation solvers
+ * (SNES).
  *
  * The PETSc nonlinear solvers provide an interface to Newton-based
  * methods for solving nonlinear equations.
@@ -23,10 +24,9 @@
 #define pylith_problems_solvernonlinear_hh
 
 // Include directives ---------------------------------------------------
-#include "problemsfwd.hh" // forward declarations
+#include "Solver.hh" // ISA Solver
 
-#include "pylith/feassemble/feassemblefwd.hh" // USES Integrator
-#include "pylith/topology/topologyfwd.hh" // USES Mesh, Field, SolutionFields
+#include "pylith/utils/petscfwd.h" // HASA PetscSNES
 
 // SolverNonlinear ---------------------------------------------------------
 class pylith::problems::SolverNonlinear
@@ -42,10 +42,37 @@
   /// Destructor
   ~SolverNonlinear(void);
 
+  /** Initialize solver.
+   *
+   * @param fields Solution fields.
+   * @param jacobian Jacobian of the system.
+   * @param residualFn Function for reforming residual.
+   * @param argsResidual Structure holding args for reforming residual.
+   * @param jacobianFn Function for reforming residual.
+   * @param argsJacobian Structure holding args for reforming residual.
+   */
+  void
+  initialize(topology::SolutionFields* fields,
+	     const topology::Jacobian& jacobian,
+	     Formulation::ResidualFn* residualFn,
+	     Formulation::ArgsResidual* argsResidual,
+	     Formulation::JacobianFn* jacobianFn,
+	     Formulation::ArgsJacobian* argsJacobian);
 
+  /** Solve the system.
+   *
+   * @param solution Solution field.
+   * @param jacobian Jacobian of the system.
+   * @param residual Residual field.
+   */
+  void solve(topology::Field<topology::Mesh>* solution,
+	     const topology::Jacobian& jacobian,
+	     const topology::Field<topology::Mesh>& residual);
+
 // PRIVATE MEMBERS //////////////////////////////////////////////////////
 private :
 
+  PetscSNES _snes; ///< PETSc SNES nonlinear solver.
 
 // NOT IMPLEMENTED //////////////////////////////////////////////////////
 private :



More information about the CIG-COMMITS mailing list