TSFPoissonBoltzmannOp.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 /* ***********************************************************************
00003 // 
00004 //           TSFExtended: Trilinos Solver Framework Extended
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // **********************************************************************/
00027  /* @HEADER@ */
00028 
00029 #include "TSFPoissonBoltzmannOp.hpp"
00030 
00031 
00032 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00033 #include "TSFVectorImpl.hpp"
00034 #include "TSFLinearOperatorImpl.hpp"
00035 #endif
00036 
00037 using namespace TSFExtended;
00038 using namespace Teuchos;
00039 
00040 
00041 PoissonBoltzmannOp::PoissonBoltzmannOp(int nLocal, const VectorType<double>& vecType)
00042   : NonlinearOperatorBase<double>(), J_(nLocal, vecType), importer_(),
00043     uLeftBC_(0.0), uRightBC_(2.0*log(cosh(1.0/sqrt(2.0))))
00044 {
00045   setDomainAndRange(J_.domain(), J_.range());
00046 
00047   int rank = MPIComm::world().getRank();
00048   int nProc = MPIComm::world().getNProc();
00049   if (nProc > 1)
00050     {
00051       Array<int> ghosts;
00052       int low = J_.domain().lowestLocallyOwnedIndex();
00053       int high = low + J_.domain().numLocalElements();
00054       if (rank != nProc - 1)
00055         {
00056           ghosts.append(high);
00057         }
00058       if (rank != 0) 
00059         {
00060           ghosts.append(low-1);
00061         }
00062 
00063       importer_ = vecType.createGhostImporter(J_.domain(), ghosts.size(), &(ghosts[0]));
00064     }
00065   else
00066     {
00067       importer_ = vecType.createGhostImporter(J_.domain(), 0, 0);
00068     }
00069 }
00070 
00071 Vector<double> PoissonBoltzmannOp::getInitialGuess() const
00072 {
00073   Vector<double> rtn = J_.domain().createMember();
00074 
00075   rtn.setToConstant(0.5);
00076 
00077   return rtn;
00078 }
00079 
00080 
00081 LinearOperator<double> 
00082 PoissonBoltzmannOp::computeJacobianAndFunction(Vector<double>& functionValue) const 
00083 {
00084   J_.setEvalPoint(currentEvalPt());
00085 
00086   RCP<GhostView<double> > u;
00087   importer_->importView(currentEvalPt(), u);
00088 
00089   int low = J_.domain().lowestLocallyOwnedIndex();
00090   int high = low + J_.domain().numLocalElements();
00091 
00092   functionValue = J_.range().createMember();
00093   double h= J_.h();
00094 
00095   for (int r=low; r<high; r++)
00096     {
00097       double u_i = u->getElement(r);
00098       double f = 0.0;
00099       if (r==0) 
00100         {
00101           f = u_i - uLeftBC_;
00102         }
00103       else if (r==J_.domain().dim()-1)
00104         {
00105           f = u_i - uRightBC_;
00106         }
00107       else
00108         {
00109           double u_plus = u->getElement(r+1);
00110           double u_minus = u->getElement(r-1);
00111           f = (u_plus + u_minus - 2.0*u_i)/h/h - exp(-u_i);
00112         }
00113       functionValue.setElement(r, f);
00114     }
00115 
00116   return J_.getOp();
00117 }
00118 
00119 Vector<double> PoissonBoltzmannOp::exactSoln() const
00120 {
00121   Vector<double> rtn = J_.domain().createMember();
00122 
00123   int low = J_.domain().lowestLocallyOwnedIndex();
00124   int high = low + J_.domain().numLocalElements();
00125   double h= J_.h();
00126   
00127   for (int r=low; r<high; r++)
00128     {
00129       double x = r*h;
00130       double u = 2.0*log(cosh(x/sqrt(2.0)));
00131       std::cerr << x << " " << u << std::endl;
00132       rtn.setElement(r, u);
00133     }
00134 
00135   return rtn;
00136 }
00137 

Site Contact