Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "Sundance.hpp"
00044
00045
00046
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() << "Fixed-point iteration" << endl << endl;
00064
00065 VectorType<double> vecType = new EpetraVectorType();
00066
00067 MeshType meshType = new BasicSimplicialMeshType();
00068 MeshSource mesher = new PartitionedLineMesher(0.0, 1.0, nx, meshType);
00069 Mesh mesh = mesher.getMesh();
00070
00071 CellFilter interior = new MaximalCellFilter();
00072 CellFilter sides = new DimensionalCellFilter(mesh.spatialDim()-1);
00073 CellFilter left = sides.subset(new CoordinateValueCellPredicate(0, 0.0));
00074 CellFilter right = sides.subset(new CoordinateValueCellPredicate(0, 1.0));
00075
00076 BasisFamily basis = new Lagrange(1);
00077 Expr u = new UnknownFunction(basis, "u");
00078 Expr v = new TestFunction(basis, "v");
00079
00080 Expr grad = gradient(1);
00081
00082 Expr x = new CoordExpr(0);
00083
00084
00085
00086 const double pi = 4.0*atan(1.0);
00087 Expr uExact = sin(pi*x);
00088 Expr R = pi*pi*uExact - lambda*exp(uExact);
00089
00090 QuadratureFamily quad4 = new GaussianQuadrature(4);
00091 QuadratureFamily quad2 = new GaussianQuadrature(2);
00092
00093 DiscreteSpace discSpace(mesh, basis, vecType);
00094 Expr uPrev = new DiscreteFunction(discSpace, 0.5);
00095 Expr uCur = copyDiscreteFunction(uPrev);
00096
00097 Expr eqn
00098 = Integral(interior, (grad*u)*(grad*v) - v*lambda*exp(uPrev) - v*R, quad4);
00099
00100 Expr h = new CellDiameterExpr();
00101 Expr bc = EssentialBC(left+right, v*u/h, quad4);
00102
00103 LinearProblem prob(mesh, eqn, bc, v, u, vecType);
00104
00105 Expr normSqExpr = Integral(interior, pow(u-uPrev, 2.0), quad2);
00106 Functional normSqFunc(mesh, normSqExpr, vecType);
00107 FunctionalEvaluator normSqEval = normSqFunc.evaluator(u, uCur);
00108
00109 LinearSolver<double> linSolver
00110 = LinearSolverBuilder::createSolver("amesos.xml");
00111
00112 Out::root() << "Fixed-point iteration" << endl;
00113 int maxIters = 20;
00114 Expr soln ;
00115 bool converged = false;
00116
00117 for (int i=0; i<maxIters; i++)
00118 {
00119
00120 prob.solve(linSolver, uCur);
00121
00122
00123 double deltaU = sqrt(normSqEval.evaluate());
00124 Out::root() << "Iter=" << setw(3) << i << " ||Delta u||=" << setw(20)
00125 << deltaU << endl;
00126
00127 if (deltaU < convTol)
00128 {
00129 soln = uCur;
00130 converged = true;
00131 break;
00132 }
00133
00134 Vector<double> uVec = getDiscreteFunctionVector(uCur);
00135
00136 setDiscreteFunctionVector(uPrev, uVec);
00137 }
00138 TEUCHOS_TEST_FOR_EXCEPTION(!converged, std::runtime_error,
00139 "Fixed point iteration did not converge after "
00140 << maxIters << " iterations");
00141
00142 FieldWriter writer = new DSVWriter("FixedPointBratu.dat");
00143 writer.addMesh(mesh);
00144 writer.addField("soln", new ExprFieldWrapper(soln[0]));
00145 writer.write();
00146
00147 Out::root() << "Converged!" << endl << endl;
00148
00149 double L2Err = L2Norm(mesh, interior, soln-uExact, quad4);
00150 Out::root() << "L2 Norm of error: " << L2Err << endl;
00151
00152 Sundance::passFailTest(L2Err, 1.5/((double) nx*nx));
00153 }
00154 catch(std::exception& e)
00155 {
00156 Sundance::handleException(e);
00157 }
00158 Sundance::finalize();
00159 }
00160