TransientHeat2D.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 int main(int argc, char** argv)
00047 {
00048   try
00049   {
00050     const double pi = 4.0*atan(1.0);
00051     double lambda = 1.25*pi*pi;
00052 
00053     int nx = 32;
00054     int nt = 10;
00055     double tFinal = 1.0/lambda;
00056 
00057     Sundance::setOption("nx", nx, "Number of elements");
00058     Sundance::setOption("nt", nt, "Number of timesteps");
00059     Sundance::setOption("tFinal", tFinal, "Final time");
00060     
00061     Sundance::init(&argc, &argv);
00062 
00063     /* Creation of vector type */
00064     VectorType<double> vecType = new EpetraVectorType();
00065 
00066     /* Set up mesh */
00067     MeshType meshType = new BasicSimplicialMeshType();
00068       
00069     MeshSource meshSrc = new PartitionedRectangleMesher(
00070       0.0, 1.0, nx,
00071       0.0, 1.0, nx,
00072       meshType);
00073     Mesh mesh = meshSrc.getMesh();
00074 
00075     /* 
00076      * Specification of cell filters
00077      */
00078     CellFilter interior = new MaximalCellFilter();
00079     CellFilter edges = new DimensionalCellFilter(1);
00080     CellFilter west = edges.coordSubset(0, 0.0);
00081     CellFilter east = edges.coordSubset(0, 1.0);
00082     CellFilter south = edges.coordSubset(1, 0.0);
00083     CellFilter north = edges.coordSubset(1, 1.0);
00084 
00085     /* set up test and unknown functions */
00086     BasisFamily basis = new Lagrange(1);
00087     Expr u = new UnknownFunction(basis, "u");
00088     Expr v = new TestFunction(basis, "v");
00089 
00090     /* set up differential operators */
00091     Expr grad = gradient(2);
00092 
00093     Expr x = new CoordExpr(0);
00094     Expr y = new CoordExpr(1);
00095 
00096     Expr t = new Sundance::Parameter(0.0);
00097     Expr tPrev = new Sundance::Parameter(0.0);
00098 
00099 
00100     DiscreteSpace discSpace(mesh, basis, vecType);
00101     Expr uExact = cos(0.5*pi*y)*sin(pi*x)*exp(-lambda*t);
00102     L2Projector proj(discSpace, uExact);
00103     Expr uPrev = proj.project();
00104 
00105 
00106     /* 
00107      * We need a quadrature rule for doing the integrations 
00108      */
00109     QuadratureFamily quad = new GaussianQuadrature(2);
00110 
00111     double deltaT = tFinal/nt;
00112 
00113     Expr gWest = -pi*exp(-lambda*t)*cos(0.5*pi*y);
00114     Expr gWestPrev = -pi*exp(-lambda*tPrev)*cos(0.5*pi*y);
00115     
00116     /* Create the weak form */
00117     Expr eqn = Integral(interior, v*(u-uPrev)/deltaT
00118       + 0.5*(grad*v)*(grad*u + grad*uPrev), quad)
00119       + Integral(west, -0.5*v*(gWest+gWestPrev), quad);
00120 
00121     Expr bc = EssentialBC(east + north, v*u, quad);
00122 
00123     
00124     LinearProblem prob(mesh, eqn, bc, v, u, vecType);
00125 
00126     
00127     LinearSolver<double> solver 
00128       = LinearSolverBuilder::createSolver("amesos.xml");
00129 
00130     FieldWriter w0 = new VTKWriter("TransientHeat2D-0");
00131     w0.addMesh(mesh);
00132     w0.addField("T", new ExprFieldWrapper(uPrev[0]));
00133     w0.write();
00134 
00135     for (int i=0; i<nt; i++)
00136     {
00137       t.setParameterValue((i+1)*deltaT);
00138       tPrev.setParameterValue(i*deltaT);
00139       Out::root() << "t=" << (i+1)*deltaT << endl;
00140       Expr uNext = prob.solve(solver);
00141       
00142       std::ostringstream oss;
00143       oss << "TransientHeat2D-" << i+1;
00144       FieldWriter w = new VTKWriter(oss.str());
00145       w.addMesh(mesh);
00146       w.addField("T", new ExprFieldWrapper(uNext[0]));
00147       w.write();
00148 
00149       updateDiscreteFunction(uNext, uPrev);
00150     }
00151 
00152 
00153     
00154     double err = L2Norm(mesh, interior, uExact-uPrev, quad);
00155     Out::root() << "error norm=" << err << endl;
00156 
00157     double h = 1.0/(nx-1.0);
00158     double tol = 0.1*(pow(h,2.0) + pow(lambda*deltaT, 2.0));
00159     Out::root() << "tol=" << tol << endl;
00160     
00161     
00162     Sundance::passFailTest(err, tol);
00163   }
00164   catch(std::exception& e) 
00165   {
00166     Sundance::handleException(e);
00167   }
00168   Sundance::finalize(); 
00169   return Sundance::testStatus();
00170 }
00171 

Site Contact