PISM, A Parallel Ice Sheet Model stable 0.4.1779

src/base/stressbalance/SNESProblem.hh

Go to the documentation of this file.
00001 // Copyright (C) 2011 David Maxwell
00002 //
00003 // This file is part of PISM.
00004 //
00005 // PISM is free software; you can redistribute it and/or modify it under the
00006 // terms of the GNU General Public License as published by the Free Software
00007 // Foundation; either version 2 of the License, or (at your option) any later
00008 // version.
00009 //
00010 // PISM is distributed in the hope that it will be useful, but WITHOUT ANY
00011 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013 // details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with PISM; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 
00020 #ifndef _SNESPROBLEM_H_
00021 #define _SNESPROBLEM_H_
00022 
00023 #include "grid.hh"
00024 #include "iceModelVec.hh"
00025 
00026 template<int DOF, class U> class SNESProblem{
00027 public:
00028   SNESProblem(IceGrid &g);
00029   
00030   virtual ~SNESProblem();
00031 
00032   virtual PetscErrorCode solve();
00033   
00034   virtual const char *name();
00035 
00036   virtual Vec solution()
00037   {
00038     return m_X;
00039   }
00040 
00041 protected:
00042 
00043   virtual PetscErrorCode initialize();
00044 
00045   virtual PetscErrorCode setFromOptions();
00046 
00047   virtual PetscErrorCode finalize();
00048   
00049   virtual PetscErrorCode compute_local_function(DALocalInfo *info, const U **xg, U **yg) = 0;  
00050 
00051   virtual PetscErrorCode compute_local_jacobian(DALocalInfo *info, const U **x,  Mat B) = 0;
00052 
00053   IceGrid     &m_grid;
00054 
00055   Mat          m_J;
00056   Vec          m_X;
00057   Vec          m_F;
00058   SNES         m_snes;
00059   DA           m_DA;
00060 
00061 private:
00062 
00063   struct SNESProblemCallbackData {
00064     DA           da;
00065     SNESProblem<DOF,U> *solver;
00066   };
00067 
00068   SNESProblemCallbackData m_callbackData;
00069 
00070   static PetscErrorCode LocalFunction(DALocalInfo *info, const U **x, U **f, SNESProblemCallbackData *);
00071   static PetscErrorCode LocalJacobian(DALocalInfo *info, const U **x, Mat B, SNESProblemCallbackData *);
00072 
00073 };
00074 
00075 typedef SNESProblem<1,PetscScalar> SNESScalarProblem;
00076 typedef SNESProblem<2,PISMVector2> SNESVectorProblem;
00077 
00078 
00079 
00080 template<int DOF, class U>
00081 PetscErrorCode SNESProblem<DOF,U>::LocalFunction(DALocalInfo *info,
00082                              const U **x, U **f,
00083                              SNESProblem<DOF,U>::SNESProblemCallbackData *cb)
00084 {
00085   return cb->solver->compute_local_function(info,x,f);
00086 }
00087 
00088 template<int DOF, class U>
00089 PetscErrorCode SNESProblem<DOF,U>::LocalJacobian(DALocalInfo *info,
00090                              const U **x, Mat J,
00091                              SNESProblem<DOF,U>::SNESProblemCallbackData *cb)
00092 {
00093   return cb->solver->compute_local_jacobian(info,x,J);
00094 }
00095 
00096 
00097 template<int DOF, class U>
00098 SNESProblem<DOF,U>::SNESProblem(IceGrid &g) :
00099 m_grid(g)
00100 {
00101   PetscErrorCode ierr;
00102   ierr = setFromOptions(); CHKERRABORT(m_grid.com,ierr);
00103   ierr = initialize();     CHKERRABORT(m_grid.com,ierr);
00104 }
00105 template<int DOF, class U>
00106 SNESProblem<DOF,U>::~SNESProblem()
00107 {
00108   PetscErrorCode ierr = finalize();
00109   CHKERRABORT(m_grid.com,ierr);  
00110 }
00111 
00112 template<int DOF, class U>
00113 PetscErrorCode SNESProblem<DOF,U>::setFromOptions()
00114 {
00115   return 0;
00116 }
00117 
00118 template<int DOF, class U>
00119 PetscErrorCode SNESProblem<DOF,U>::initialize()
00120 {
00121   PetscErrorCode ierr;
00122 
00123   // mimic IceGrid::createDA() with TRANSPOSE :
00124   PetscInt stencil_width=1;
00125   ierr = DACreate2d(m_grid.com, DA_XYPERIODIC, DA_STENCIL_BOX,
00126                     m_grid.My, m_grid.Mx,
00127                     m_grid.Ny, m_grid.Nx,
00128                     DOF, stencil_width,
00129                     m_grid.procs_y.data(), m_grid.procs_x.data(),
00130                     &m_DA); CHKERRQ(ierr);
00131 
00132   ierr = DACreateGlobalVector(m_DA, &m_X); CHKERRQ(ierr);
00133   ierr = DACreateGlobalVector(m_DA, &m_F); CHKERRQ(ierr);
00134   ierr = DAGetMatrix(m_DA, "baij",  &m_J); CHKERRQ(ierr);
00135 
00136   ierr = SNESCreate(m_grid.com, &m_snes);CHKERRQ(ierr);
00137 
00138   // Set the SNES callbacks to call into our compute_local_function and compute_local_jacobian
00139   // methods via SSAFEFunction and SSAFEJ
00140   ierr = DASetLocalFunction(m_DA,(DALocalFunction1)SNESProblem<DOF,U>::LocalFunction);CHKERRQ(ierr);
00141   ierr = DASetLocalJacobian(m_DA,(DALocalFunction1)SNESProblem<DOF,U>::LocalJacobian);CHKERRQ(ierr);
00142   m_callbackData.da = m_DA;  m_callbackData.solver = this;
00143   ierr = SNESSetFunction(m_snes, m_F, SNESDAFormFunction, &m_callbackData);CHKERRQ(ierr);
00144   ierr = SNESSetJacobian(m_snes, m_J, m_J, SNESDAComputeJacobian, &m_callbackData);CHKERRQ(ierr);  
00145 
00146   ierr = SNESSetFromOptions(m_snes);CHKERRQ(ierr);
00147 
00148   return 0;
00149 }
00150 
00152 template<int DOF, class U>
00153 PetscErrorCode SNESProblem<DOF,U>::finalize() {
00154   PetscErrorCode ierr;
00155 
00156   ierr = SNESDestroy(m_snes);CHKERRQ(ierr);
00157   ierr = VecDestroy(m_X); CHKERRQ(ierr);
00158   ierr = VecDestroy(m_F); CHKERRQ(ierr);
00159   ierr = MatDestroy(m_J); CHKERRQ(ierr);
00160 
00161   return 0;
00162 }
00163 
00164 template<int DOF, class U>
00165 const char *SNESProblem<DOF,U>::name()
00166 {
00167   return "UnnamedProblem";
00168 }
00169 
00170 template<int DOF, class U>
00171 PetscErrorCode SNESProblem<DOF,U>::solve()
00172 {
00173   PetscErrorCode ierr;
00174 
00175   // Solve:
00176   ierr = SNESSolve(m_snes,NULL,m_X);CHKERRQ(ierr);
00177   
00178   // See if it worked.
00179   SNESConvergedReason reason;
00180   ierr = SNESGetConvergedReason( m_snes, &reason); CHKERRQ(ierr);
00181   if(reason < 0)
00182   {
00183     SETERRQ2(1, 
00184       "SNESProblem %s solve failed to converge (SNES reason %s)\n\n", name(), SNESConvergedReasons[reason]);
00185   }
00186 
00187   verbPrintf(1,m_grid.com,"SNESProblem %s converged (SNES reason %s)\n", name(), SNESConvergedReasons[reason]);
00188   return 0;
00189 }
00190 
00191 
00192 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines