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 #include "PlayaNonlinearSolverBuilder.hpp"
00045
00046
00047
00048
00049
00050 int main(int argc, char** argv)
00051 {
00052 try
00053 {
00054 int nx = 32;
00055 double convTol = 1.0e-8;
00056 int nSteps = 5;
00057 double lambdaMax = 0.5;
00058 Sundance::setOption("nx", nx, "Number of elements");
00059 Sundance::setOption("tol", convTol, "Convergence tolerance");
00060 Sundance::setOption("lambda-max", lambdaMax,
00061 "final lambda (parameter in Bratu's equation)");
00062 Sundance::setOption("nSteps", nSteps,
00063 "number of steps in lambda (continuation from lambda=0 to lambda=lambdaMax)");
00064
00065 Sundance::init(&argc, &argv);
00066
00067 Out::root() << "Bratu problem with continuation (lambda=[0, " << lambdaMax << "] in "
00068 << nSteps << " steps)" << endl;
00069 Out::root() << "Newton's method with automated linearization"
00070 << endl << endl;
00071
00072 VectorType<double> vecType = new EpetraVectorType();
00073
00074 MeshType meshType = new BasicSimplicialMeshType();
00075 MeshSource mesher = new PartitionedLineMesher(0.0, 1.0, nx, meshType);
00076 Mesh mesh = mesher.getMesh();
00077
00078 CellFilter interior = new MaximalCellFilter();
00079 CellFilter sides = new DimensionalCellFilter(mesh.spatialDim()-1);
00080 CellFilter left = sides.subset(new CoordinateValueCellPredicate(0, 0.0));
00081 CellFilter right = sides.subset(new CoordinateValueCellPredicate(0, 1.0));
00082
00083 BasisFamily basis = new Lagrange(1);
00084 Expr u = new UnknownFunction(basis, "w");
00085 Expr v = new TestFunction(basis, "v");
00086
00087 Expr grad = gradient(1);
00088
00089 Expr x = new CoordExpr(0);
00090
00091 Expr lambda = new Sundance::Parameter(0.0);
00092 const double pi = 4.0*atan(1.0);
00093 Expr uExact = sin(pi*x);
00094 Expr R = pi*pi*uExact - lambda*exp(uExact);
00095
00096 QuadratureFamily quad4 = new GaussianQuadrature(4);
00097 QuadratureFamily quad2 = new GaussianQuadrature(2);
00098
00099 DiscreteSpace discSpace(mesh, basis, vecType);
00100 Expr uPrev = new DiscreteFunction(discSpace, 0.5);
00101
00102 Expr eqn
00103 = Integral(interior, (grad*v)*(grad*u) - v*lambda*exp(u) - v*R, quad4);
00104
00105 Expr h = new CellDiameterExpr();
00106 Expr bc = EssentialBC(left+right, v*u/h, quad2);
00107
00108 NonlinearProblem prob(mesh, eqn, bc, v, u, uPrev, vecType);
00109
00110 NonlinearSolver<double> solver
00111 = NonlinearSolverBuilder::createSolver("playa-newton-amesos.xml");
00112
00113 Expr soln = uPrev;
00114 for (int n=0; n<nSteps; n++)
00115 {
00116 double lambdaVal = n*lambdaMax/(nSteps-1.0);
00117
00118 lambda.setParameterValue(lambdaVal);
00119 Out::root() << "continuation step n=" << n
00120 << " of " << nSteps << ", lambda="
00121 << lambdaVal << endl;
00122
00123 SolverState<double> state = prob.solve(solver);
00124
00125 TEUCHOS_TEST_FOR_EXCEPTION(state.finalState() != SolveConverged,
00126 std::runtime_error,
00127 "Nonlinear solve failed to converge: message=" << state.finalMsg());
00128
00129 Expr soln = uPrev;
00130 FieldWriter writer = new DSVWriter("ContinuationBratu-"
00131 + Teuchos::toString(n) + ".dat");
00132 writer.addMesh(mesh);
00133 writer.addField("soln", new ExprFieldWrapper(soln[0]));
00134 writer.write();
00135 }
00136
00137
00138 double L2Err = L2Norm(mesh, interior, soln-uExact, quad4);
00139 Out::root() << "L2 Norm of error: " << L2Err << endl;
00140
00141 Sundance::passFailTest(L2Err, 1.5/((double) nx*nx));
00142 }
00143 catch(std::exception& e)
00144 {
00145 Sundance::handleException(e);
00146 }
00147 Sundance::finalize();
00148 }
00149