AutoLinearizedNewtonBratu1D.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 
00043 #include "Sundance.hpp"
00044 
00045 /* 
00046  * Solve the Bratu problem in 1D using fixed-point iteration 
00047  */
00048 
00049 int main(int argc, char** argv)
00050 {
00051   try
00052   {
00053     int nx = 32;
00054     double convTol = 1.0e-8;
00055     double lambda = 0.5;
00056     Sundance::setOption("nx", nx, "Number of elements");
00057     Sundance::setOption("tol", convTol, "Convergence tolerance");
00058     Sundance::setOption("lambda", lambda, "Lambda (parameter in Bratu's equation)");
00059 
00060     Sundance::init(&argc, &argv);
00061 
00062     Out::root() << "Bratu problem (lambda=" << lambda << ")" << endl;
00063     Out::root() << "Newton's method with automated linearization" 
00064                 << endl << endl;
00065 
00066     VectorType<double> vecType = new EpetraVectorType();
00067 
00068     MeshType meshType = new BasicSimplicialMeshType();
00069     MeshSource mesher = new PartitionedLineMesher(0.0, 1.0, nx, meshType);
00070     Mesh mesh = mesher.getMesh();
00071 
00072     CellFilter interior = new MaximalCellFilter();
00073     CellFilter sides = new DimensionalCellFilter(mesh.spatialDim()-1);
00074     CellFilter left = sides.subset(new CoordinateValueCellPredicate(0, 0.0));
00075     CellFilter right = sides.subset(new CoordinateValueCellPredicate(0, 1.0));
00076     
00077     BasisFamily basis = new Lagrange(1);
00078     Expr u = new UnknownFunction(basis, "w");
00079     Expr v = new TestFunction(basis, "v");
00080 
00081     Expr grad = gradient(1);
00082 
00083     Expr x = new CoordExpr(0);
00084 
00085     const double pi = 4.0*atan(1.0);
00086     Expr uExact = sin(pi*x);
00087     Expr R = pi*pi*uExact - lambda*exp(uExact);
00088 
00089     QuadratureFamily quad4 = new GaussianQuadrature(4);
00090     QuadratureFamily quad2 = new GaussianQuadrature(2);
00091 
00092     DiscreteSpace discSpace(mesh, basis, vecType);
00093     Expr uPrev = new DiscreteFunction(discSpace, 0.5);
00094 
00095     Expr eqn 
00096       = Integral(interior, (grad*v)*(grad*u) - v*lambda*exp(u) - v*R, quad4);
00097 
00098     Expr h = new CellDiameterExpr();
00099     Expr bc = EssentialBC(left+right, v*u/h, quad2); 
00100 
00101     NonlinearProblem prob(mesh, eqn, bc, v, u, uPrev, vecType);
00102 
00103     LinearSolver<double> linSolver 
00104       = LinearSolverBuilder::createSolver("amesos.xml");
00105 
00106     Out::root() << "Newton iteration" << endl;
00107     int maxIters = 20;
00108     Expr soln ;
00109     bool converged = false;
00110 
00111     LinearOperator<double> J = prob.allocateJacobian();
00112     Vector<double> residVec = J.range().createMember();
00113     Vector<double> stepVec;
00114 
00115     for (int i=0; i<maxIters; i++)
00116     {
00117       prob.setInitialGuess(uPrev);
00118       prob.computeJacobianAndFunction(J, residVec);
00119       
00120       linSolver.solve(J, -1.0*residVec, stepVec);
00121       
00122       double deltaU = stepVec.norm2();
00123       Out::root() << "Iter=" << setw(3) << i << " ||Delta u||=" << setw(20)
00124                   << deltaU << endl;
00125       addVecToDiscreteFunction(uPrev, stepVec);
00126       if (deltaU < convTol) 
00127       {
00128         soln = uPrev;
00129         converged = true;
00130         break;
00131       }
00132     } 
00133     TEUCHOS_TEST_FOR_EXCEPTION(!converged, std::runtime_error, 
00134       "Newton iteration did not converge after " 
00135       << maxIters << " iterations");
00136     
00137     FieldWriter writer = new DSVWriter("AutoLinearizedBratu.dat");
00138     writer.addMesh(mesh);
00139     writer.addField("soln", new ExprFieldWrapper(soln[0]));
00140     writer.write();
00141 
00142     Out::root() << "Converged!" << endl << endl;
00143 
00144     double L2Err = L2Norm(mesh, interior, soln-uExact, quad4);
00145     Out::root() << "L2 Norm of error: " << L2Err << endl;
00146     
00147     Sundance::passFailTest(L2Err, 1.5/((double) nx*nx));
00148   }
00149   catch(std::exception& e) 
00150   {
00151     Sundance::handleException(e);
00152   }
00153   Sundance::finalize(); 
00154 }
00155 

Site Contact