Coupled2D.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 #include "Sundance.hpp"
00043 
00044 using Sundance::List;
00045 
00046 
00047 /** 
00048  * Solves the coupled equations
00049  *
00050  * \f[\nabla^2 u_1 = u_2 \f]
00051  * \f[\nabla^2 u_2 = x \f]
00052  * \f[u_1 = u_2 = 0\;\;\;\mathrm{on\; east\; and\; west}\f]
00053  * \f[\frac{\partial u_1}{\partial n} = \frac{\partial u_2}{\partial n} = 0\;\;\;\mathrm{on\; north\; and\; south}\f]
00054  * 
00055  *
00056  * The solution is
00057  * \f[u_1(x,y)=\frac{1}{360}(3 x^5 - 10 x^3 + 7 x)\f]
00058  * \f[u_2(x,y)= \frac{1}{6} x (x^2-1)\f]
00059  *
00060  * 
00061  */
00062 
00063 int main(int argc, char** argv)
00064 {
00065   
00066   try
00067     {
00068       Sundance::init(&argc, &argv);
00069 
00070       /* We will do our linear algebra using Epetra */
00071       VectorType<double> vecType = new EpetraVectorType();
00072 
00073       /* Create a mesh. It will be of type BasisSimplicialMesh, and will
00074        * be built using a PartitionedRectangleMesher. */
00075       MeshType meshType = new BasicSimplicialMeshType();
00076       int nx = 32;
00077 
00078       MeshSource mesher = new PartitionedRectangleMesher(0.0, 1.0, nx,
00079         0.0, 2.0, nx,
00080         meshType);
00081       Mesh mesh = mesher.getMesh();
00082 
00083       /* Create a cell filter that will identify the maximal cells
00084        * in the interior of the domain */
00085       CellFilter interior = new MaximalCellFilter();
00086 
00087       /* Create cell filters for the boundary surfaces */
00088       CellFilter edges = new DimensionalCellFilter(1);
00089 
00090       CellFilter west = edges.coordSubset(0, 0.0);
00091       CellFilter east = edges.coordSubset(0, 1.0);
00092 
00093       
00094       /* Create unknown and test functions, discretized using first-order
00095        * Lagrange interpolants */
00096       Expr u1 = new UnknownFunction(new Lagrange(3), "u");
00097       Expr u2 = new UnknownFunction(new Lagrange(2), "v");
00098       Expr du1 = new TestFunction(new Lagrange(3), "du");
00099       Expr du2 = new TestFunction(new Lagrange(2), "dv");
00100 
00101       /* Create differential operator and coordinate function */
00102       Expr dx = new Derivative(0);
00103       Expr x = new CoordExpr(0);
00104       Expr dy = new Derivative(1);
00105       Expr y = new CoordExpr(1);
00106       Expr grad = List(dx, dy);
00107 
00108       /* We need a quadrature rule for doing the integrations */
00109       QuadratureFamily quad = new GaussianQuadrature(6);
00110 
00111       
00112       /* Define the weak form */
00113       Expr eqn = Integral(interior, 
00114                           (grad*du1)*(grad*u1) + du1*u2 + (grad*du2)*(grad*u2) + x*du2, 
00115                           quad);
00116       /* Define the Dirichlet BC */
00117       Expr bc = EssentialBC(east + west, du1*u1 + du2*u2, quad);
00118 
00119       /* We can now set up the linear problem! The order of the test functions in the
00120       * list determines the order of these variables in the matrix. Liewise for
00121       * the unknown functions. */
00122       LinearProblem prob(mesh, eqn, bc, List(du1,du2), List(u1,u2), vecType);
00123 
00124       
00125 
00126       /* Get a solver */
00127       LinearSolver<double> solver = LinearSolverBuilder::createSolver("aztec-ml.xml");
00128 
00129 
00130       /* Solver */
00131       Expr soln = prob.solve(solver);
00132 
00133       /* Check */
00134       Expr x2 = x*x;
00135       Expr x3 = x*x2;
00136 
00137       Expr u1Exact = (1.0/120.0)*x2*x3 - 1.0/36.0 * x3 + 7.0/360.0 * x;
00138       Expr u2Exact = 1.0/6.0 * x * (x2 - 1.0);
00139 
00140       Expr u1Err = u1Exact - soln[0];
00141       Expr u2Err = u2Exact - soln[1];
00142       
00143       double u1ErrorNorm = L2Norm(mesh, interior, u1Err, quad);
00144       std::cerr << "u1 error norm = " << u1ErrorNorm << std::endl << std::endl;
00145 
00146       double u2ErrorNorm = L2Norm(mesh, interior, u2Err, quad);
00147       std::cerr << "u2 error norm = " << u2ErrorNorm << std::endl << std::endl;
00148 
00149 
00150       
00151       /* Write the field in VTK format */
00152       FieldWriter w = new VTKWriter("Coupled2D");
00153       w.addMesh(mesh);
00154       w.addField("v", new ExprFieldWrapper(soln[0]));
00155       w.addField("u", new ExprFieldWrapper(soln[1]));
00156       w.write();
00157 
00158 
00159 
00160       double tol = 1.0e-5;
00161       Sundance::passFailTest(std::max(u1ErrorNorm, u2ErrorNorm), tol);
00162     }
00163   catch(std::exception& e)
00164     {
00165       Sundance::handleException(e);
00166     }
00167   Sundance::finalize(); 
00168   return Sundance::testStatus(); 
00169 }

Site Contact