// TODO: Merge with ConcolicRuntime::printSolution and put somewhere accessible by both. void ConcolicTargetGenerator::printSolution(const SolutionPtr solution) const { QStringList symbols = solution->symbols(); foreach (QString var, symbols) { Symbolvalue value = solution->findSymbol(var); assert(value.found); // TODO: Only object type should be seen here? switch (value.kind) { case Symbolic::INT: Log::debug(QString(" %1 = %2").arg(var).arg(value.u.integer).toStdString()); break; case Symbolic::BOOL: Log::debug(QString(" %1 = %2").arg(var).arg(value.u.boolean ? "true" : "false").toStdString()); break; case Symbolic::STRING: if (value.string.empty()) { Log::debug(QString(" %1 = \"\"").arg(var).toStdString()); } else { Log::debug(QString(" %1 = \"%2\"").arg(var).arg(value.string.c_str()).toStdString()); } break; case Symbolic::OBJECT: Log::debug(QString(" %1 -> %2").arg(var).arg(value.string.c_str()).toStdString()); break; default: Log::fatal(QString("Unimplemented value type encountered for variable %1 (%2)").arg(var).arg(value.kind).toStdString()); exit(1); } }
/** * Implementation of the Clarke and Wright savings algorithm * This function modifies an existing solution by merging tours from the same depot if this results in a net * cost reduction. The tours are merged in order of net reduction from highest to lowest. This is primarily * used to initiate the solver with a sensible first guess. */ SolutionPtr cw_savings(SolutionPtr prevsol) { set<TourPtr> newsol; for (auto depot : prevsol->get_problem()->get_depots()) { vector<TourPtr> tours_vec = prevsol->tours_from_depot(depot); set<TourPtr> tours(tours_vec.begin(), tours_vec.end()); while (true) { double reduction = -1; TourPtr left, right; for (auto t1 : tours) for (auto t2 : tours) { if (t1 == t2) continue; double test_reduction = triangle(*t1->last(), *depot, *t2->first()); // Only accept this merge if it doesn't break the daily cap condition if ( test_reduction > reduction && t1->duration() + t2->duration() - test_reduction <= prevsol->get_problem()->get_daily_cap()) { reduction = test_reduction; left = t1; right = t2; } } if (reduction >= 0) { tours.erase(left); tours.erase(right); tours.insert(TourPtr(new Tour(*left, *right))); } else break; } newsol.insert(tours.begin(), tours.end()); } return SolutionPtr(new Solution(prevsol->get_problem(), newsol)); }
void writePatchValues(double xMin, double xMax, double yMin, double yMax, SolutionPtr solution, VarPtr u1, string filename, int numPoints=100) { FieldContainer<double> points = pointGrid(xMin,xMax,yMin,yMax,numPoints); FieldContainer<double> values(numPoints*numPoints); solution->solutionValues(values, u1->ID(), points); ofstream fout(filename.c_str()); fout << setprecision(15); fout << "X = zeros(" << numPoints << ",1);\n"; // fout << "Y = zeros(numPoints);\n"; fout << "U = zeros(" << numPoints << "," << numPoints << ");\n"; for (int i=0; i<numPoints; i++) { fout << "X(" << i+1 << ")=" << points(i,0) << ";\n"; } for (int i=0; i<numPoints; i++) { fout << "Y(" << i+1 << ")=" << points(i,1) << ";\n"; } for (int i=0; i<numPoints; i++) { for (int j=0; j<numPoints; j++) { int pointIndex = i*numPoints + j; fout << "U("<<i+1<<","<<j+1<<")=" << values(pointIndex) << ";" << endl; } } fout.close(); }
int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL); // initialize MPI Teuchos::CommandLineProcessor cmdp(false,true); // false: don't throw exceptions; true: do return errors for unrecognized options int numElements = 3; double xLeft = 0.0, xRight = 1.0; int polyOrder = 1, delta_k = 1; cmdp.setOption("numElements", &numElements ); cmdp.setOption("polyOrder", &polyOrder ); cmdp.setOption("delta_k", &delta_k ); cmdp.setOption("xLeft", &xLeft ); cmdp.setOption("xRight", &xRight ); if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } int spaceDim = 1; bool conformingTraces = true; // conformingTraces argument has no effect in 1D PoissonFormulation poissonForm(spaceDim, conformingTraces); MeshPtr mesh = MeshFactory::intervalMesh(poissonForm.bf(), xLeft, xRight, numElements, polyOrder + 1, delta_k); // 1D equispaced RHSPtr rhs = RHS::rhs(); // zero RHS IPPtr ip = poissonForm.bf()->graphNorm(); BCPtr bc = BC::bc(); bc->addDirichlet(poissonForm.phi_hat(), SpatialFilter::allSpace(), Function::zero()); SolutionPtr solution = Solution::solution(poissonForm.bf(), mesh, bc, rhs, ip); solution->solve(); GDAMinimumRule* minRule = dynamic_cast<GDAMinimumRule*>(mesh->globalDofAssignment().get()); // minRule->printGlobalDofInfo(); Teuchos::RCP<Epetra_CrsMatrix> A = solution->getStiffnessMatrix(); EpetraExt::RowMatrixToMatrixMarketFile("A.dat",*A, NULL, NULL, false); HDF5Exporter exporter(mesh); return 0; }
FieldContainer<double> solutionData(FieldContainer<double> &points, SolutionPtr solution, VarPtr u1) { int numPoints = points.dimension(0); FieldContainer<double> values(numPoints); solution->solutionValues(values, u1->ID(), points); FieldContainer<double> xyzData(numPoints, 3); for (int ptIndex=0; ptIndex<numPoints; ptIndex++) { xyzData(ptIndex,0) = points(ptIndex,0); xyzData(ptIndex,1) = points(ptIndex,1); xyzData(ptIndex,2) = values(ptIndex); } return xyzData; }
void writeSol(EnvPtr env, VarVector *orig_v, PresolverPtr pres, SolutionPtr sol, SolveStatus status, MINOTAUR_AMPL::AMPLInterface* iface) { if (sol) { sol = pres->getPostSol(sol); } if (env->getOptions()->findFlag("AMPL")->getValue() || true == env->getOptions()->findBool("write_sol_file")->getValue()) { iface->writeSolution(sol, status); } else if (sol && env->getLogger()->getMaxLevel()>=LogExtraInfo && env->getOptions()->findBool("display_solution")->getValue()) { sol->writePrimal(env->getLogger()->msgStream(LogExtraInfo), orig_v); } }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif int commRank = Teuchos::GlobalMPISession::getRank(); Comm.Barrier(); // set breakpoint here to allow debugger attachment to other MPI processes than the one you automatically attached to. Teuchos::CommandLineProcessor cmdp(false,true); // false: don't throw exceptions; true: do return errors for unrecognized options // problem parameters: double mu = 0.1; double permCoef = 1e4; int numRefs = 0; int k = 2, delta_k = 2; string norm = "Graph"; cmdp.setOption("polyOrder",&k,"polynomial order for field variable u"); cmdp.setOption("delta_k", &delta_k, "test space polynomial order enrichment"); cmdp.setOption("numRefs",&numRefs,"number of refinements"); cmdp.setOption("norm", &norm, "norm"); cmdp.setOption("mu", &mu, "mu"); cmdp.setOption("permCoef", &permCoef, "Permeability coefficient"); if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } FunctionPtr zero = TFunction<double>::zero(); FunctionPtr one = TFunction<double>::constant(1); FunctionPtr sin2pix = Teuchos::rcp( new Sin_ax(2*pi) ); FunctionPtr cos2pix = Teuchos::rcp( new Cos_ax(2*pi) ); FunctionPtr sin2piy = Teuchos::rcp( new Sin_ay(2*pi) ); FunctionPtr cos2piy = Teuchos::rcp( new Cos_ay(2*pi) ); FunctionPtr u1_exact = sin2pix*cos2piy; FunctionPtr u2_exact = -cos2pix*sin2piy; FunctionPtr x2 = TFunction<double>::xn(2); FunctionPtr y2 = TFunction<double>::yn(2); FunctionPtr p_exact = x2*y2 - 1./9; FunctionPtr permInv = permCoef*(sin2pix + 1.1); VarFactoryPtr vf = VarFactory::varFactory(); //fields: VarPtr sigma1 = vf->fieldVar("sigma1", VECTOR_L2); VarPtr sigma2 = vf->fieldVar("sigma2", VECTOR_L2); VarPtr u1 = vf->fieldVar("u1", L2); VarPtr u2 = vf->fieldVar("u2", L2); VarPtr p = vf->fieldVar("p", L2); // traces: VarPtr u1hat = vf->traceVar("u1hat"); VarPtr u2hat = vf->traceVar("u2hat"); VarPtr t1c = vf->fluxVar("t1c"); VarPtr t2c = vf->fluxVar("t2c"); // test: VarPtr v1 = vf->testVar("v1", HGRAD); VarPtr v2 = vf->testVar("v2", HGRAD); VarPtr tau1 = vf->testVar("tau1", HDIV); VarPtr tau2 = vf->testVar("tau2", HDIV); VarPtr q = vf->testVar("q", HGRAD); BFPtr bf = Teuchos::rcp( new BF(vf) ); bf->addTerm(1./mu*sigma1, tau1); bf->addTerm(1./mu*sigma2, tau2); bf->addTerm(u1, tau1->div()); bf->addTerm(u2, tau2->div()); bf->addTerm(-u1hat, tau1->dot_normal()); bf->addTerm(-u2hat, tau2->dot_normal()); bf->addTerm(sigma1, v1->grad()); bf->addTerm(sigma2, v2->grad()); bf->addTerm(-p, v1->dx()); bf->addTerm(-p, v2->dy()); bf->addTerm(t1c, v1); bf->addTerm(t2c, v2); bf->addTerm(mu*permInv*u1, v1); bf->addTerm(mu*permInv*u2, v2); bf->addTerm(-u1, q->dx()); bf->addTerm(-u2, q->dy()); bf->addTerm(u1hat, q->times_normal_x()); bf->addTerm(u2hat, q->times_normal_y()); RHSPtr rhs = RHS::rhs(); BCPtr bc = BC::bc(); SpatialFilterPtr y_equals_one = SpatialFilter::matchingY(1.0); SpatialFilterPtr y_equals_zero = SpatialFilter::matchingY(0); SpatialFilterPtr x_equals_one = SpatialFilter::matchingX(1.0); SpatialFilterPtr x_equals_zero = SpatialFilter::matchingX(0.0); bc->addDirichlet(u1hat, y_equals_zero, u1_exact); bc->addDirichlet(u2hat, y_equals_zero, u2_exact); bc->addDirichlet(u1hat, x_equals_zero, u1_exact); bc->addDirichlet(u2hat, x_equals_zero, u2_exact); bc->addDirichlet(u1hat, y_equals_one, u1_exact); bc->addDirichlet(u2hat, y_equals_one, u2_exact); bc->addDirichlet(u1hat, x_equals_one, u1_exact); bc->addDirichlet(u2hat, x_equals_one, u2_exact); bc->addZeroMeanConstraint(p); MeshPtr mesh = MeshFactory::quadMesh(bf, k+1, delta_k, 1, 1, 4, 4); map<string, IPPtr> brinkmanIPs; brinkmanIPs["Graph"] = bf->graphNorm(); brinkmanIPs["Decoupled"] = Teuchos::rcp(new IP); brinkmanIPs["Decoupled"]->addTerm(tau1); brinkmanIPs["Decoupled"]->addTerm(tau2); brinkmanIPs["Decoupled"]->addTerm(tau1->div()); brinkmanIPs["Decoupled"]->addTerm(tau2->div()); brinkmanIPs["Decoupled"]->addTerm(permInv*v1); brinkmanIPs["Decoupled"]->addTerm(permInv*v2); brinkmanIPs["Decoupled"]->addTerm(v1->grad()); brinkmanIPs["Decoupled"]->addTerm(v2->grad()); brinkmanIPs["Decoupled"]->addTerm(q); brinkmanIPs["Decoupled"]->addTerm(q->grad()); // brinkmanIPs["CoupledRobust"] = Teuchos::rcp(new IP); // brinkmanIPs["CoupledRobust"]->addTerm(tau->div()-beta*v->grad()); // brinkmanIPs["CoupledRobust"]->addTerm(Function<double>::min(one/Function<double>::h(),Function<double>::constant(1./sqrt(epsilon)))*tau); // brinkmanIPs["CoupledRobust"]->addTerm(sqrt(epsilon)*v->grad()); // brinkmanIPs["CoupledRobust"]->addTerm(beta*v->grad()); // brinkmanIPs["CoupledRobust"]->addTerm(Function<double>::min(sqrt(epsilon)*one/Function<double>::h(),one)*v); IPPtr ip = brinkmanIPs[norm]; SolutionPtr soln = TSolution<double>::solution(mesh, bc, rhs, ip); double threshold = 0.20; RefinementStrategy refStrategy(soln, threshold); ostringstream refName; refName << "brinkman"; HDF5Exporter exporter(mesh,refName.str()); for (int refIndex=0; refIndex <= numRefs; refIndex++) { soln->solve(false); double energyError = soln->energyErrorTotal(); if (commRank == 0) { // if (refIndex > 0) // refStrategy.printRefinementStatistics(refIndex-1); cout << "Refinement:\t " << refIndex << " \tElements:\t " << mesh->numActiveElements() << " \tDOFs:\t " << mesh->numGlobalDofs() << " \tEnergy Error:\t " << energyError << endl; } exporter.exportSolution(soln, refIndex); if (refIndex != numRefs) refStrategy.refine(); } return 0; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); choice::MpiArgs args( argc, argv ); #else choice::Args args( argc, argv ); #endif int rank = Teuchos::GlobalMPISession::getRank(); int numProcs = Teuchos::GlobalMPISession::getNProc(); // define mathematical "constants" vector<double> e1(2); // (1,0) e1[0] = 1; vector<double> e2(2); // (0,1) e2[1] = 1; FunctionPtr negOne = Function::constant(-1.0); FunctionPtr one = Function::constant(1.0); // problem params double Re = args.Input<double>("--Re","Reynolds number",1e3); double dt = args.Input<double>("--dt","Timestep",.25); double Ma = args.Input<double>("--Ma","Mach number",3.0); // solver int nCells = args.Input<int>("--nCells", "num cells",2); int polyOrder = args.Input<int>("--p","order of approximation",2); int pToAdd = args.Input<int>("--pToAdd", "test space enrichment",2); double time_tol_orig = args.Input<double>("--timeTol", "time step tolerance",1e-8); bool useLineSearch = args.Input<bool>("--useLineSearch", "flag for line search",true); // default to zero int maxNRIter = args.Input<int>("--maxNRIter","maximum number of NR iterations",2); // default to one per timestep int numTimeSteps = args.Input<int>("--maxTimeSteps","max number of time steps",150); // max time steps double minDt = args.Input<double>("--minDt","min timestep for adaptive timestepping",.01); // max time steps double maxDt = args.Input<double>("--maxDt","max timestep for adaptive timestepping",.1); // max time steps // adaptivity int numUniformRefs = args.Input<int>("--numUniformRefs","num uniform refinements (pre-adaptivity)",0); int numRefs = args.Input<int>("--numRefs","num adaptive refinements",0); double energyThreshold = args.Input<double>("--energyThreshold", "energy thresh for adaptivity",0.25); // for mesh refinements bool useHpStrategy = args.Input<bool>("--useHpStrategy","option to use a 'cheap' hp strategy", false); double anisotropicThresh = args.Input<int>("--anisotropicThresh","anisotropy threshhold",10.0); bool useAnisotropy = args.Input<bool>("--useAnisotropy","anisotropy flag",false); bool usePointViscosity = args.Input<bool>("--usePointViscosity","use extra viscosity at plate point",false); bool useAdaptiveTimestepping = args.Input<bool>("--useAdaptiveTimestepping","use adaptive timestepping a la Ben Kirk", false); // conditioning for DPG int hScaleOption = args.Input<int>("--hScaleOption","option to scale terms to offset conditioning for small h", 0); int hScaleTauOption = args.Input<int>("--hScaleTauOption","option to scale tau terms to offset conditioning for small h", 0); // etc - experimental bool useHigherOrderForU = args.Input<bool>("--useHigherOrderForU","option to increase order for field vars",false); // HGRAD is one higher order bool useConditioningCFL = args.Input<bool>("--useConditioningCFL","option to use a CFL limit for conditioning",false); int numPreRefs = args.Input<int>("--numPreRefs","pre-refinements on singularity",0); bool scalePlate = args.Input<bool>("--scalePlate","flag to weight plate so it matters less",false); double ipSwitch = args.Input<double>("--ipSwitch","smallest elem thresh to switch to graph norm",0.0); // default to not changing // IO stuff string saveFile = args.Input<string>("--meshSaveFile", "file to which to save refinement history", ""); string replayFile = args.Input<string>("--meshLoadFile", "file with refinement history to replay", ""); string solnSaveFile = args.Input<string>("--solnSaveFile", "file to which to save soln", ""); string solnLoadFile = args.Input<string>("--solnLoadFile", "file from which to load soln", ""); string dir = args.Input<string>("--dir", "dir to which we save/from which we load files",""); bool reportTimingResults = args.Input<bool>("--reportTimings", "flag to report timings of solve", false); bool writeTimestepFiles = args.Input<bool>("--writeTimestepFiles","flag to turn on and off time step writing",false); if (rank==0) { cout << "saveFile is " << dir + saveFile << endl; cout << "saveSolnFile is " << dir + solnSaveFile << endl; } if (rank==0) { cout << "loadFile is " << dir + replayFile << endl; cout << "loadSolnFile is " << dir + solnLoadFile << endl; } /////////////////////////////////////////////////////////////////////////////////////////////// // END OF INPUT ARGUMENTS /////////////////////////////////////////////////////////////////////////////////////////////// if (useHigherOrderForU) { polyOrder = 1; } bool useTriangles = false; FieldContainer<double> domainPoints(4,2); domainPoints(0,0) = 0.0; // x1 domainPoints(0,1) = 0.0; // y1 domainPoints(1,0) = X_BOUNDARY; domainPoints(1,1) = 0.0; domainPoints(2,0) = X_BOUNDARY; domainPoints(2,1) = YTOP; domainPoints(3,0) = 0.0; domainPoints(3,1) = YTOP; int H1Order = polyOrder + 1; // int H1Order = 2; // start with linears, and keep singularity linear at singularity int horizontalCells = 2*nCells, verticalCells = nCells; //////////////////////////////////////////////////////////////////// // DEFINE VARIABLES //////////////////////////////////////////////////////////////////// // new-style bilinear form definition // traces VarFactory varFactory; VarPtr u1hat = varFactory.traceVar("\\widehat{u}_1"); VarPtr u2hat = varFactory.traceVar("\\widehat{u}_2"); VarPtr That = varFactory.traceVar("\\widehat{T}"); // fluxes VarPtr F1nhat = varFactory.fluxVar("\\widehat{F}_1n"); VarPtr F2nhat = varFactory.fluxVar("\\widehat{F}_2n"); VarPtr F3nhat = varFactory.fluxVar("\\widehat{F}_3n"); VarPtr F4nhat = varFactory.fluxVar("\\widehat{F}_4n"); // stress fields VarPtr sigma11 = varFactory.fieldVar("\\sigma_{11}"); VarPtr sigma12 = varFactory.fieldVar("\\sigma_{12}"); VarPtr sigma22 = varFactory.fieldVar("\\sigma_{22}"); VarPtr q1 = varFactory.fieldVar("q_1"); VarPtr q2 = varFactory.fieldVar("q_2"); VarPtr omega = varFactory.fieldVar("\\omega"); VarPtr u_1,u_2,u_3,u_4; // H1-ish fields if (useHigherOrderForU) // HGRAD is one higher order { u_1 = varFactory.fieldVar("u_1",HGRAD); u_2 = varFactory.fieldVar("u_2",HGRAD); u_3 = varFactory.fieldVar("u_3",HGRAD); u_4 = varFactory.fieldVar("u_4",HGRAD); } else { u_1 = varFactory.fieldVar("u_1"); u_2 = varFactory.fieldVar("u_2"); u_3 = varFactory.fieldVar("rho"); u_4 = varFactory.fieldVar("T"); } // test fxns VarPtr tau1 = varFactory.testVar("\\tau_1",HDIV); VarPtr tau2 = varFactory.testVar("\\tau_2",HDIV); VarPtr tau3 = varFactory.testVar("\\tau_3",HDIV); VarPtr v1 = varFactory.testVar("v_1",HGRAD); VarPtr v2 = varFactory.testVar("v_2",HGRAD); VarPtr v3 = varFactory.testVar("v_3",HGRAD); VarPtr v4 = varFactory.testVar("v_4",HGRAD); //////////////////////////////////////////////////////////////////// // CREATE BILINEAR FORM PTR AND MESH //////////////////////////////////////////////////////////////////// BFPtr bf = Teuchos::rcp( new BF(varFactory) ); // initialize bilinear form // create a pointer to a new mesh: // Teuchos::RCP<Mesh> mesh = Mesh::buildQuadMesh(domainPoints, horizontalCells, verticalCells, bf, H1Order, H1Order+pToAdd, useTriangles); Teuchos::RCP<Mesh> mesh = MeshUtilities::buildRampMesh(rampHeight,bf, H1Order, H1Order+pToAdd); // mesh->setPartitionPolicy(Teuchos::rcp(new ZoltanMeshPartitionPolicy("HSFC"))); mesh->setPartitionPolicy(Teuchos::rcp(new ZoltanMeshPartitionPolicy("REFTREE"))); MeshInfo meshInfo(mesh); // gets info like cell measure, etc // for writing ref history to file Teuchos::RCP< RefinementHistory > refHistory = Teuchos::rcp( new RefinementHistory ); mesh->registerObserver(refHistory); //////////////////////////////////////////////////////////////////// // CREATE SOLUTION OBJECT //////////////////////////////////////////////////////////////////// BCPtr nullBC = Teuchos::rcp((BC*)NULL); RHSPtr nullRHS = Teuchos::rcp((RHS*)NULL); IPPtr nullIP = Teuchos::rcp((IP*)NULL); SolutionPtr backgroundFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); SolutionPtr prevTimeFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); Teuchos::RCP<Solution> solution = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP)); int enrichDegree = 2; // just for kicks. if (rank==0) cout << "enriching cubature by " << enrichDegree << endl; solution->setCubatureEnrichmentDegree(enrichDegree); // double cubature enrichment if (reportTimingResults) { solution->setReportTimingResults(true); } mesh->registerSolution(solution); mesh->registerSolution(backgroundFlow); // u_t(i) mesh->registerSolution(prevTimeFlow); // u_t(i-1) // for loading refinement history if (replayFile.length() > 0) { RefinementHistory refHistory; replayFile = dir + replayFile; refHistory.loadFromFile(replayFile); refHistory.playback(mesh); int numElems = mesh->numActiveElements(); if (rank==0) { double minSideLength = meshInfo.getMinCellSideLength() ; cout << "after replay, num elems = " << numElems << " and min side length = " << minSideLength << endl; } } if (solnLoadFile.length() > 0) { std::ostringstream ss; // ss << dir << "solution_" << solnLoadFile; // solution->readFromFile(ss.str()); ss.str(""); ss << dir << "backgroundFlow_" << solnLoadFile; backgroundFlow->readFromFile(ss.str()); ss.str(""); ss << dir << "prevTimeFlow_" << solnLoadFile; prevTimeFlow->readFromFile(ss.str()); } //////////////////////////////////////////////////////////////////// // PSEUDO-TIME SOLVE STRATEGY //////////////////////////////////////////////////////////////////// VTKExporter exporter(solution, mesh, varFactory); VTKExporter backgroundFlowExporter(backgroundFlow, mesh, varFactory); if (rank==0) { exporter.exportSolution("dU"); backgroundFlowExporter.exportSolution("U"); } return 0; }
int main(int argc, char *argv[]) { int rank = 0; #ifdef HAVE_MPI // TODO: figure out the right thing to do here... // may want to modify argc and argv before we make the following call: Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); rank=mpiSession.getRank(); #else #endif bool useLineSearch = false; int pToAdd = 2; // for optimal test function approximation int pToAddForStreamFunction = 2; double nonlinearStepSize = 1.0; double dt = 0.5; double nonlinearRelativeEnergyTolerance = 0.015; // used to determine convergence of the nonlinear solution // double nonlinearRelativeEnergyTolerance = 0.15; // used to determine convergence of the nonlinear solution double eps = 1.0/64.0; // width of ramp up to 1.0 for top BC; eps == 0 ==> soln not in H1 // epsilon above is chosen to match our initial 16x16 mesh, to avoid quadrature errors. // double eps = 0.0; // John Evans's problem: not in H^1 bool enforceLocalConservation = false; bool enforceOneIrregularity = true; bool reportPerCellErrors = true; bool useMumps = true; int horizontalCells, verticalCells; int maxIters = 50; // for nonlinear steps vector<double> ReValues; // usage: polyOrder [numRefinements] // parse args: if (argc < 6) { cout << "Usage: NavierStokesCavityFlowContinuationFixedMesh fieldPolyOrder hCells vCells energyErrorGoal Re0 [Re1 ...]\n"; return -1; } int polyOrder = atoi(argv[1]); horizontalCells = atoi(argv[2]); verticalCells = atoi(argv[3]); double energyErrorGoal = atof(argv[4]); for (int i=5; i<argc; i++) { ReValues.push_back(atof(argv[i])); } if (rank == 0) { cout << "L^2 order: " << polyOrder << endl; cout << "initial mesh size: " << horizontalCells << " x " << verticalCells << endl; cout << "energy error goal: " << energyErrorGoal << endl; cout << "Reynolds number values for continuation:\n"; for (int i=0; i<ReValues.size(); i++) { cout << ReValues[i] << ", "; } cout << endl; } FieldContainer<double> quadPoints(4,2); quadPoints(0,0) = 0.0; // x1 quadPoints(0,1) = 0.0; // y1 quadPoints(1,0) = 1.0; quadPoints(1,1) = 0.0; quadPoints(2,0) = 1.0; quadPoints(2,1) = 1.0; quadPoints(3,0) = 0.0; quadPoints(3,1) = 1.0; // define meshes: int H1Order = polyOrder + 1; bool useTriangles = false; bool meshHasTriangles = useTriangles; double minL2Increment = 1e-8; // get variable definitions: VarFactory varFactory = VGPStokesFormulation::vgpVarFactory(); u1 = varFactory.fieldVar(VGP_U1_S); u2 = varFactory.fieldVar(VGP_U2_S); sigma11 = varFactory.fieldVar(VGP_SIGMA11_S); sigma12 = varFactory.fieldVar(VGP_SIGMA12_S); sigma21 = varFactory.fieldVar(VGP_SIGMA21_S); sigma22 = varFactory.fieldVar(VGP_SIGMA22_S); p = varFactory.fieldVar(VGP_P_S); u1hat = varFactory.traceVar(VGP_U1HAT_S); u2hat = varFactory.traceVar(VGP_U2HAT_S); t1n = varFactory.fluxVar(VGP_T1HAT_S); t2n = varFactory.fluxVar(VGP_T2HAT_S); v1 = varFactory.testVar(VGP_V1_S, HGRAD); v2 = varFactory.testVar(VGP_V2_S, HGRAD); tau1 = varFactory.testVar(VGP_TAU1_S, HDIV); tau2 = varFactory.testVar(VGP_TAU2_S, HDIV); q = varFactory.testVar(VGP_Q_S, HGRAD); FunctionPtr u1_0 = Teuchos::rcp( new U1_0(eps) ); FunctionPtr u2_0 = Teuchos::rcp( new U2_0 ); FunctionPtr zero = Function::zero(); ParameterFunctionPtr Re_param = ParameterFunction::parameterFunction(1); VGPNavierStokesProblem problem = VGPNavierStokesProblem(Re_param,quadPoints, horizontalCells,verticalCells, H1Order, pToAdd, u1_0, u2_0, // BC for u zero, zero); // zero forcing function SolutionPtr solution = problem.backgroundFlow(); SolutionPtr solnIncrement = problem.solutionIncrement(); Teuchos::RCP<Mesh> mesh = problem.mesh(); mesh->registerSolution(solution); mesh->registerSolution(solnIncrement); /////////////////////////////////////////////////////////////////////////// // define bilinear form for stream function: VarFactory streamVarFactory; VarPtr phi_hat = streamVarFactory.traceVar("\\widehat{\\phi}"); VarPtr psin_hat = streamVarFactory.fluxVar("\\widehat{\\psi}_n"); VarPtr psi_1 = streamVarFactory.fieldVar("\\psi_1"); VarPtr psi_2 = streamVarFactory.fieldVar("\\psi_2"); VarPtr phi = streamVarFactory.fieldVar("\\phi"); VarPtr q_s = streamVarFactory.testVar("q_s", HGRAD); VarPtr v_s = streamVarFactory.testVar("v_s", HDIV); BFPtr streamBF = Teuchos::rcp( new BF(streamVarFactory) ); streamBF->addTerm(psi_1, q_s->dx()); streamBF->addTerm(psi_2, q_s->dy()); streamBF->addTerm(-psin_hat, q_s); streamBF->addTerm(psi_1, v_s->x()); streamBF->addTerm(psi_2, v_s->y()); streamBF->addTerm(phi, v_s->div()); streamBF->addTerm(-phi_hat, v_s->dot_normal()); Teuchos::RCP<Mesh> streamMesh, overkillMesh; streamMesh = MeshFactory::buildQuadMesh(quadPoints, horizontalCells, verticalCells, streamBF, H1Order+pToAddForStreamFunction, H1Order+pToAdd+pToAddForStreamFunction, useTriangles); mesh->registerObserver(streamMesh); // will refine streamMesh in the same way as mesh. map<int, double> dofsToL2error; // key: numGlobalDofs, value: total L2error compared with overkill vector< VarPtr > fields; fields.push_back(u1); fields.push_back(u2); fields.push_back(sigma11); fields.push_back(sigma12); fields.push_back(sigma21); fields.push_back(sigma22); fields.push_back(p); if (rank == 0) { cout << "Starting mesh has " << horizontalCells << " x " << verticalCells << " elements and "; cout << mesh->numGlobalDofs() << " total dofs.\n"; cout << "polyOrder = " << polyOrder << endl; cout << "pToAdd = " << pToAdd << endl; cout << "eps for top BC = " << eps << endl; if (useTriangles) { cout << "Using triangles.\n"; } if (enforceLocalConservation) { cout << "Enforcing local conservation.\n"; } else { cout << "NOT enforcing local conservation.\n"; } if (enforceOneIrregularity) { cout << "Enforcing 1-irregularity.\n"; } else { cout << "NOT enforcing 1-irregularity.\n"; } } //////////////////// CREATE BCs /////////////////////// SpatialFilterPtr entireBoundary = Teuchos::rcp( new SpatialFilterUnfiltered ); FunctionPtr u1_prev = Function::solution(u1,solution); FunctionPtr u2_prev = Function::solution(u2,solution); FunctionPtr u1hat_prev = Function::solution(u1hat,solution); FunctionPtr u2hat_prev = Function::solution(u2hat,solution); //////////////////// SOLVE & REFINE /////////////////////// FunctionPtr vorticity = Teuchos::rcp( new PreviousSolutionFunction(solution, - u1->dy() + u2->dx() ) ); // FunctionPtr vorticity = Teuchos::rcp( new PreviousSolutionFunction(solution,sigma12 - sigma21) ); RHSPtr streamRHS = RHS::rhs(); streamRHS->addTerm(vorticity * q_s); ((PreviousSolutionFunction*) vorticity.get())->setOverrideMeshCheck(true); ((PreviousSolutionFunction*) u1_prev.get())->setOverrideMeshCheck(true); ((PreviousSolutionFunction*) u2_prev.get())->setOverrideMeshCheck(true); BCPtr streamBC = BC::bc(); // streamBC->addDirichlet(psin_hat, entireBoundary, u0_cross_n); streamBC->addDirichlet(phi_hat, entireBoundary, zero); // streamBC->addZeroMeanConstraint(phi); IPPtr streamIP = Teuchos::rcp( new IP ); streamIP->addTerm(q_s); streamIP->addTerm(q_s->grad()); streamIP->addTerm(v_s); streamIP->addTerm(v_s->div()); SolutionPtr streamSolution = Teuchos::rcp( new Solution( streamMesh, streamBC, streamRHS, streamIP ) ); if (enforceLocalConservation) { FunctionPtr zero = Function::zero(); solution->lagrangeConstraints()->addConstraint(u1hat->times_normal_x() + u2hat->times_normal_y()==zero); solnIncrement->lagrangeConstraints()->addConstraint(u1hat->times_normal_x() + u2hat->times_normal_y()==zero); } if (true) { FunctionPtr u1_incr = Function::solution(u1, solnIncrement); FunctionPtr u2_incr = Function::solution(u2, solnIncrement); FunctionPtr sigma11_incr = Function::solution(sigma11, solnIncrement); FunctionPtr sigma12_incr = Function::solution(sigma12, solnIncrement); FunctionPtr sigma21_incr = Function::solution(sigma21, solnIncrement); FunctionPtr sigma22_incr = Function::solution(sigma22, solnIncrement); FunctionPtr p_incr = Function::solution(p, solnIncrement); FunctionPtr l2_incr = u1_incr * u1_incr + u2_incr * u2_incr + p_incr * p_incr + sigma11_incr * sigma11_incr + sigma12_incr * sigma12_incr + sigma21_incr * sigma21_incr + sigma22_incr * sigma22_incr; double energyThreshold = 0.20; Teuchos::RCP< RefinementStrategy > refinementStrategy = Teuchos::rcp( new RefinementStrategy( solnIncrement, energyThreshold )); for (int i=0; i<ReValues.size(); i++) { double Re = ReValues[i]; Re_param->setValue(Re); if (rank==0) cout << "Solving with Re = " << Re << ":\n"; double energyErrorTotal; do { double incr_norm; do { problem.iterate(useLineSearch); incr_norm = sqrt(l2_incr->integrate(problem.mesh())); if (rank==0) { cout << "\x1B[2K"; // Erase the entire current line. cout << "\x1B[0E"; // Move to the beginning of the current line. cout << "Iteration: " << problem.iterationCount() << "; L^2(incr) = " << incr_norm; flush(cout); } } while ((incr_norm > minL2Increment ) && (problem.iterationCount() < maxIters)); if (rank==0) cout << endl; problem.setIterationCount(1); // 1 means reuse background flow (which we must, given that we want continuation in Re...) energyErrorTotal = solnIncrement->energyErrorTotal(); //solution->energyErrorTotal(); if (energyErrorTotal > energyErrorGoal) { refinementStrategy->refine(false); } if (rank==0) { cout << "Energy error: " << energyErrorTotal << endl; } } while (energyErrorTotal > energyErrorGoal); } } double energyErrorTotal = solution->energyErrorTotal(); double incrementalEnergyErrorTotal = solnIncrement->energyErrorTotal(); if (rank == 0) { cout << "final mesh has " << mesh->numActiveElements() << " elements and " << mesh->numGlobalDofs() << " dofs.\n"; cout << "energy error: " << energyErrorTotal << endl; cout << " (Incremental solution's energy error is " << incrementalEnergyErrorTotal << ".)\n"; } FunctionPtr u1_sq = u1_prev * u1_prev; FunctionPtr u_dot_u = u1_sq + (u2_prev * u2_prev); FunctionPtr u_mag = Teuchos::rcp( new SqrtFunction( u_dot_u ) ); FunctionPtr u_div = Teuchos::rcp( new PreviousSolutionFunction(solution, u1->dx() + u2->dy() ) ); FunctionPtr massFlux = Teuchos::rcp( new PreviousSolutionFunction(solution, u1hat->times_normal_x() + u2hat->times_normal_y()) ); // check that the zero mean pressure is being correctly imposed: FunctionPtr p_prev = Teuchos::rcp( new PreviousSolutionFunction(solution,p) ); double p_avg = p_prev->integrate(mesh); if (rank==0) cout << "Integral of pressure: " << p_avg << endl; // integrate massFlux over each element (a test): // fake a new bilinear form so we can integrate against 1 VarPtr testOne = varFactory.testVar("1",CONSTANT_SCALAR); BFPtr fakeBF = Teuchos::rcp( new BF(varFactory) ); LinearTermPtr massFluxTerm = massFlux * testOne; CellTopoPtrLegacy quadTopoPtr = Teuchos::rcp(new shards::CellTopology(shards::getCellTopologyData<shards::Quadrilateral<4> >() )); DofOrderingFactory dofOrderingFactory(fakeBF); int fakeTestOrder = H1Order; DofOrderingPtr testOrdering = dofOrderingFactory.testOrdering(fakeTestOrder, *quadTopoPtr); int testOneIndex = testOrdering->getDofIndex(testOne->ID(),0); vector< ElementTypePtr > elemTypes = mesh->elementTypes(); // global element types map<int, double> massFluxIntegral; // cellID -> integral double maxMassFluxIntegral = 0.0; double totalMassFlux = 0.0; double totalAbsMassFlux = 0.0; double maxCellMeasure = 0; double minCellMeasure = 1; for (vector< ElementTypePtr >::iterator elemTypeIt = elemTypes.begin(); elemTypeIt != elemTypes.end(); elemTypeIt++) { ElementTypePtr elemType = *elemTypeIt; vector< ElementPtr > elems = mesh->elementsOfTypeGlobal(elemType); vector<GlobalIndexType> cellIDs; for (int i=0; i<elems.size(); i++) { cellIDs.push_back(elems[i]->cellID()); } FieldContainer<double> physicalCellNodes = mesh->physicalCellNodesGlobal(elemType); BasisCachePtr basisCache = Teuchos::rcp( new BasisCache(elemType,mesh,polyOrder) ); // enrich by trial space order basisCache->setPhysicalCellNodes(physicalCellNodes,cellIDs,true); // true: create side caches FieldContainer<double> cellMeasures = basisCache->getCellMeasures(); FieldContainer<double> fakeRHSIntegrals(elems.size(),testOrdering->totalDofs()); massFluxTerm->integrate(fakeRHSIntegrals,testOrdering,basisCache,true); // true: force side evaluation // cout << "fakeRHSIntegrals:\n" << fakeRHSIntegrals; for (int i=0; i<elems.size(); i++) { int cellID = cellIDs[i]; // pick out the ones for testOne: massFluxIntegral[cellID] = fakeRHSIntegrals(i,testOneIndex); } // find the largest: for (int i=0; i<elems.size(); i++) { int cellID = cellIDs[i]; maxMassFluxIntegral = max(abs(massFluxIntegral[cellID]), maxMassFluxIntegral); } for (int i=0; i<elems.size(); i++) { int cellID = cellIDs[i]; maxCellMeasure = max(maxCellMeasure,cellMeasures(i)); minCellMeasure = min(minCellMeasure,cellMeasures(i)); maxMassFluxIntegral = max(abs(massFluxIntegral[cellID]), maxMassFluxIntegral); totalMassFlux += massFluxIntegral[cellID]; totalAbsMassFlux += abs( massFluxIntegral[cellID] ); } } if (rank==0) { cout << "largest mass flux: " << maxMassFluxIntegral << endl; cout << "total mass flux: " << totalMassFlux << endl; cout << "sum of mass flux absolute value: " << totalAbsMassFlux << endl; cout << "largest h: " << sqrt(maxCellMeasure) << endl; cout << "smallest h: " << sqrt(minCellMeasure) << endl; cout << "ratio of largest / smallest h: " << sqrt(maxCellMeasure) / sqrt(minCellMeasure) << endl; } if (rank == 0) { cout << "phi ID: " << phi->ID() << endl; cout << "psi1 ID: " << psi_1->ID() << endl; cout << "psi2 ID: " << psi_2->ID() << endl; cout << "streamMesh has " << streamMesh->numActiveElements() << " elements.\n"; cout << "solving for approximate stream function...\n"; } streamSolution->solve(useMumps); energyErrorTotal = streamSolution->energyErrorTotal(); if (rank == 0) { cout << "...solved.\n"; cout << "Stream mesh has energy error: " << energyErrorTotal << endl; } if (rank==0) { solution->writeToVTK("nsCavitySoln.vtk"); if (! meshHasTriangles ) { massFlux->writeBoundaryValuesToMATLABFile(solution->mesh(), "massFlux.dat"); u_mag->writeValuesToMATLABFile(solution->mesh(), "u_mag.m"); u_div->writeValuesToMATLABFile(solution->mesh(), "u_div.m"); solution->writeFieldsToFile(u1->ID(), "u1.m"); solution->writeFluxesToFile(u1hat->ID(), "u1_hat.dat"); solution->writeFieldsToFile(u2->ID(), "u2.m"); solution->writeFluxesToFile(u2hat->ID(), "u2_hat.dat"); solution->writeFieldsToFile(p->ID(), "p.m"); streamSolution->writeFieldsToFile(phi->ID(), "phi.m"); streamSolution->writeFluxesToFile(phi_hat->ID(), "phi_hat.dat"); streamSolution->writeFieldsToFile(psi_1->ID(), "psi1.m"); streamSolution->writeFieldsToFile(psi_2->ID(), "psi2.m"); vorticity->writeValuesToMATLABFile(streamMesh, "vorticity.m"); FunctionPtr ten = Teuchos::rcp( new ConstantScalarFunction(10) ); ten->writeBoundaryValuesToMATLABFile(solution->mesh(), "skeleton.dat"); cout << "wrote files: u_mag.m, u_div.m, u1.m, u1_hat.dat, u2.m, u2_hat.dat, p.m, phi.m, vorticity.m.\n"; } else { solution->writeToFile(u1->ID(), "u1.dat"); solution->writeToFile(u2->ID(), "u2.dat"); solution->writeToFile(u2->ID(), "p.dat"); cout << "wrote files: u1.dat, u2.dat, p.dat\n"; } FieldContainer<double> points = pointGrid(0, 1, 0, 1, 100); FieldContainer<double> pointData = solutionData(points, streamSolution, phi); GnuPlotUtil::writeXYPoints("phi_patch_navierStokes_cavity.dat", pointData); set<double> patchContourLevels = diagonalContourLevels(pointData,1); vector<string> patchDataPath; patchDataPath.push_back("phi_patch_navierStokes_cavity.dat"); GnuPlotUtil::writeContourPlotScript(patchContourLevels, patchDataPath, "lidCavityNavierStokes.p"); GnuPlotUtil::writeExactMeshSkeleton("lid_navierStokes_continuation_adaptive", mesh, 2); writePatchValues(0, 1, 0, 1, streamSolution, phi, "phi_patch.m"); writePatchValues(0, .1, 0, .1, streamSolution, phi, "phi_patch_detail.m"); writePatchValues(0, .01, 0, .01, streamSolution, phi, "phi_patch_minute_detail.m"); writePatchValues(0, .001, 0, .001, streamSolution, phi, "phi_patch_minute_minute_detail.m"); } return 0; }
int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL); // initialize MPI Teuchos::CommandLineProcessor cmdp(false,true); // false: don't throw exceptions; true: do return errors for unrecognized options int numElements = 3; vector<vector<double>> domainDim(3,vector<double>{0.0,1.0}); // first index: spaceDim; second: 0/1 for x0, x1, etc. int polyOrder = 2, delta_k = 1; int spaceDim = 2; cmdp.setOption("numElements", &numElements ); cmdp.setOption("polyOrder", &polyOrder ); cmdp.setOption("delta_k", &delta_k ); cmdp.setOption("x0", &domainDim[0][0] ); cmdp.setOption("x1", &domainDim[0][1] ); cmdp.setOption("y0", &domainDim[1][0] ); cmdp.setOption("y1", &domainDim[1][1] ); cmdp.setOption("z0", &domainDim[2][0] ); cmdp.setOption("z1", &domainDim[2][1] ); cmdp.setOption("spaceDim", &spaceDim); if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } vector<double> x0(spaceDim); vector<double> domainSize(spaceDim); vector<int> elementCounts(spaceDim); for (int d=0; d<spaceDim; d++) { x0[d] = domainDim[d][0]; domainSize[d] = domainDim[d][1] - x0[d]; elementCounts[d] = numElements; } bool conformingTraces = true; // no difference for primal/continuous formulations PoissonFormulation formCG(spaceDim, conformingTraces, PoissonFormulation::CONTINUOUS_GALERKIN); VarPtr q = formCG.q(); VarPtr phi = formCG.phi(); BFPtr bf = formCG.bf(); MeshPtr bubnovMesh = MeshFactory::rectilinearMesh(bf, domainSize, elementCounts, polyOrder, 0, x0); // Right now, hanging nodes don't work with continuous field variables // there is a GDAMinimumRule test demonstrating the failure, SolvePoisson2DContinuousGalerkinHangingNode. // make a mesh with hanging nodes (when spaceDim > 1) // { // set<GlobalIndexType> cellsToRefine = {0}; // bubnovMesh->hRefine(cellsToRefine); // } RHSPtr rhs = RHS::rhs(); rhs->addTerm(1.0 * q); // unit forcing IPPtr ip = Teuchos::null; // will give Bubnov-Galerkin BCPtr bc = BC::bc(); bc->addDirichlet(phi, SpatialFilter::allSpace(), Function::zero()); SolutionPtr solution = Solution::solution(bf, bubnovMesh, bc, rhs, ip); solution->solve(); HDF5Exporter exporter(bubnovMesh, "PoissonContinuousGalerkin"); exporter.exportSolution(solution); /**** Sort-of-primal experiment ****/ // an experiment: try doing "primal" DPG with IBP to the boundary // ip = IP::ip(); // ip->addTerm(q->grad()); // ip->addTerm(q); // // solution = Solution::solution(bf, bubnovMesh, bc, rhs, ip); // solution->solve(); // // HDF5Exporter primalNoFluxExporter(bubnovMesh, "PoissonPrimalNoFlux"); // primalNoFluxExporter.exportSolution(solution); //*** Primal Formulation ***// PoissonFormulation form(spaceDim, conformingTraces, PoissonFormulation::PRIMAL); q = form.q(); phi = form.phi(); bf = form.bf(); bc = BC::bc(); bc->addDirichlet(phi, SpatialFilter::allSpace(), Function::zero()); rhs = RHS::rhs(); rhs->addTerm(1.0 * q); // unit forcing MeshPtr primalMesh = MeshFactory::rectilinearMesh(bf, domainSize, elementCounts, polyOrder, delta_k, x0); ip = IP::ip(); ip->addTerm(q->grad()); ip->addTerm(q); // Right now, hanging nodes don't work with continuous field variables // there is a GDAMinimumRule test demonstrating the failure, SolvePoisson2DContinuousGalerkinHangingNode. // make a mesh with hanging nodes (when spaceDim > 1) // { // set<GlobalIndexType> cellsToRefine = {0}; // primalMesh->hRefine(cellsToRefine); // } solution = Solution::solution(bf, primalMesh, bc, rhs, ip); solution->solve(); HDF5Exporter primalExporter(primalMesh, "PoissonPrimal"); primalExporter.exportSolution(solution); return 0; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); int rank=mpiSession.getRank(); int numProcs=mpiSession.getNProc(); #else int rank = 0; int numProcs = 1; #endif int polyOrder = 3; int pToAdd = 2; // for tests // define our manufactured solution or problem bilinear form: bool useTriangles = false; FieldContainer<double> meshPoints(4,2); meshPoints(0,0) = 0.0; // x1 meshPoints(0,1) = 0.0; // y1 meshPoints(1,0) = 1.0; meshPoints(1,1) = 0.0; meshPoints(2,0) = 1.0; meshPoints(2,1) = 1.0; meshPoints(3,0) = 0.0; meshPoints(3,1) = 1.0; int H1Order = polyOrder + 1; int horizontalCells = 4, verticalCells = 4; double energyThreshold = 0.2; // for mesh refinements double nonlinearStepSize = 0.5; double nonlinearRelativeEnergyTolerance = 1e-8; // used to determine convergence of the nonlinear solution //////////////////////////////////////////////////////////////////// // DEFINE VARIABLES //////////////////////////////////////////////////////////////////// // new-style bilinear form definition VarFactory varFactory; VarPtr fhat = varFactory.fluxVar("\\widehat{f}"); VarPtr u = varFactory.fieldVar("u"); VarPtr v = varFactory.testVar("v",HGRAD); BFPtr bf = Teuchos::rcp( new BF(varFactory) ); // initialize bilinear form //////////////////////////////////////////////////////////////////// // CREATE MESH //////////////////////////////////////////////////////////////////// // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = Mesh::buildQuadMesh(meshPoints, horizontalCells, verticalCells, bf, H1Order, H1Order+pToAdd, useTriangles); mesh->setPartitionPolicy(Teuchos::rcp(new ZoltanMeshPartitionPolicy("HSFC"))); //////////////////////////////////////////////////////////////////// // INITIALIZE BACKGROUND FLOW FUNCTIONS //////////////////////////////////////////////////////////////////// BCPtr nullBC = Teuchos::rcp((BC*)NULL); RHSPtr nullRHS = Teuchos::rcp((RHS*)NULL); IPPtr nullIP = Teuchos::rcp((IP*)NULL); SolutionPtr backgroundFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); vector<double> e1(2); // (1,0) e1[0] = 1; vector<double> e2(2); // (0,1) e2[1] = 1; FunctionPtr u_prev = Teuchos::rcp( new PreviousSolutionFunction(backgroundFlow, u) ); FunctionPtr beta = e1 * u_prev + Teuchos::rcp( new ConstantVectorFunction( e2 ) ); //////////////////////////////////////////////////////////////////// // DEFINE BILINEAR FORM //////////////////////////////////////////////////////////////////// // v: // (sigma, grad v)_K - (sigma_hat_n, v)_dK - (u, beta dot grad v) + (u_hat * n dot beta, v)_dK bf->addTerm( -u, beta * v->grad()); bf->addTerm( fhat, v); // ==================== SET INITIAL GUESS ========================== mesh->registerSolution(backgroundFlow); FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); FunctionPtr u0 = Teuchos::rcp( new U0 ); map<int, Teuchos::RCP<Function> > functionMap; functionMap[u->ID()] = u0; backgroundFlow->projectOntoMesh(functionMap); // ==================== END SET INITIAL GUESS ========================== //////////////////////////////////////////////////////////////////// // DEFINE INNER PRODUCT //////////////////////////////////////////////////////////////////// IPPtr ip = Teuchos::rcp( new IP ); ip->addTerm( v ); ip->addTerm( beta * v->grad() ); //////////////////////////////////////////////////////////////////// // DEFINE RHS //////////////////////////////////////////////////////////////////// Teuchos::RCP<RHSEasy> rhs = Teuchos::rcp( new RHSEasy ); FunctionPtr u_prev_squared_div2 = 0.5 * u_prev * u_prev; rhs->addTerm( (e1 * u_prev_squared_div2 + e2 * u_prev) * v->grad()); //////////////////////////////////////////////////////////////////// // DEFINE DIRICHLET BC //////////////////////////////////////////////////////////////////// Teuchos::RCP<BCEasy> inflowBC = Teuchos::rcp( new BCEasy ); // Create spatial filters SpatialFilterPtr bottomBoundary = Teuchos::rcp( new BottomBoundary ); SpatialFilterPtr leftBoundary = Teuchos::rcp( new LeftBoundary ); SpatialFilterPtr rightBoundary = Teuchos::rcp( new LeftBoundary ); // Create BCs FunctionPtr n = Teuchos::rcp( new UnitNormalFunction ); FunctionPtr u0_squared_div_2 = 0.5 * u0 * u0; SimpleFunction* u0Ptr = static_cast<SimpleFunction *>(u0.get()); double u0Left = u0Ptr->value(0,0); double u0Right = u0Ptr->value(1.0,0); FunctionPtr leftVal = Teuchos::rcp( new ConstantScalarFunction( -0.5*u0Left*u0Left ) ); FunctionPtr rightVal = Teuchos::rcp( new ConstantScalarFunction( 0.5*u0Right*u0Right ) ); inflowBC->addDirichlet(fhat, bottomBoundary, -u0 ); inflowBC->addDirichlet(fhat, leftBoundary, leftVal ); inflowBC->addDirichlet(fhat, rightBoundary, rightVal ); //////////////////////////////////////////////////////////////////// // CREATE SOLUTION OBJECT //////////////////////////////////////////////////////////////////// Teuchos::RCP<Solution> solution = Teuchos::rcp(new Solution(mesh, inflowBC, rhs, ip)); mesh->registerSolution(solution); if (enforceLocalConservation) { FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); solution->lagrangeConstraints()->addConstraint(fhat == zero); } //////////////////////////////////////////////////////////////////// // DEFINE REFINEMENT STRATEGY //////////////////////////////////////////////////////////////////// Teuchos::RCP<RefinementStrategy> refinementStrategy; refinementStrategy = Teuchos::rcp(new RefinementStrategy(solution,energyThreshold)); //////////////////////////////////////////////////////////////////// // SOLVE //////////////////////////////////////////////////////////////////// for (int refIndex=0; refIndex<=numRefs; refIndex++) { double L2Update = 1e7; int iterCount = 0; while (L2Update > nonlinearRelativeEnergyTolerance && iterCount < maxNewtonIterations) { solution->solve(); L2Update = solution->L2NormOfSolutionGlobal(u->ID()); cout << "L2 Norm of Update = " << L2Update << endl; // backgroundFlow->clear(); backgroundFlow->addSolution(solution, newtonStepSize); iterCount++; } cout << endl; // check conservation VarPtr testOne = varFactory.testVar("1", CONSTANT_SCALAR); // Create a fake bilinear form for the testing BFPtr fakeBF = Teuchos::rcp( new BF(varFactory) ); // Define our mass flux FunctionPtr massFlux = Teuchos::rcp( new PreviousSolutionFunction(solution, fhat) ); LinearTermPtr massFluxTerm = massFlux * testOne; Teuchos::RCP<shards::CellTopology> quadTopoPtr = Teuchos::rcp(new shards::CellTopology(shards::getCellTopologyData<shards::Quadrilateral<4> >() )); DofOrderingFactory dofOrderingFactory(fakeBF); int fakeTestOrder = H1Order; DofOrderingPtr testOrdering = dofOrderingFactory.testOrdering(fakeTestOrder, *quadTopoPtr); int testOneIndex = testOrdering->getDofIndex(testOne->ID(),0); vector< ElementTypePtr > elemTypes = mesh->elementTypes(); // global element types map<int, double> massFluxIntegral; // cellID -> integral double maxMassFluxIntegral = 0.0; double totalMassFlux = 0.0; double totalAbsMassFlux = 0.0; for (vector< ElementTypePtr >::iterator elemTypeIt = elemTypes.begin(); elemTypeIt != elemTypes.end(); elemTypeIt++) { ElementTypePtr elemType = *elemTypeIt; vector< ElementPtr > elems = mesh->elementsOfTypeGlobal(elemType); vector<int> cellIDs; for (int i=0; i<elems.size(); i++) { cellIDs.push_back(elems[i]->cellID()); } FieldContainer<double> physicalCellNodes = mesh->physicalCellNodesGlobal(elemType); BasisCachePtr basisCache = Teuchos::rcp( new BasisCache(elemType,mesh) ); basisCache->setPhysicalCellNodes(physicalCellNodes,cellIDs,true); // true: create side caches FieldContainer<double> cellMeasures = basisCache->getCellMeasures(); FieldContainer<double> fakeRHSIntegrals(elems.size(),testOrdering->totalDofs()); massFluxTerm->integrate(fakeRHSIntegrals,testOrdering,basisCache,true); // true: force side evaluation for (int i=0; i<elems.size(); i++) { int cellID = cellIDs[i]; // pick out the ones for testOne: massFluxIntegral[cellID] = fakeRHSIntegrals(i,testOneIndex); } // find the largest: for (int i=0; i<elems.size(); i++) { int cellID = cellIDs[i]; maxMassFluxIntegral = max(abs(massFluxIntegral[cellID]), maxMassFluxIntegral); } for (int i=0; i<elems.size(); i++) { int cellID = cellIDs[i]; maxMassFluxIntegral = max(abs(massFluxIntegral[cellID]), maxMassFluxIntegral); totalMassFlux += massFluxIntegral[cellID]; totalAbsMassFlux += abs( massFluxIntegral[cellID] ); } } if (rank==0) { cout << endl; cout << "largest mass flux: " << maxMassFluxIntegral << endl; cout << "total mass flux: " << totalMassFlux << endl; cout << "sum of mass flux absolute value: " << totalAbsMassFlux << endl; cout << endl; stringstream outfile; outfile << "burgers_" << refIndex; backgroundFlow->writeToVTK(outfile.str(), 5); } if (refIndex < numRefs) refinementStrategy->refine(rank==0); // print to console on rank 0 } return 0; }
int main(int argc, char *argv[]) { // Process command line arguments #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); int rank=mpiSession.getRank(); int numProcs=mpiSession.getNProc(); #else int rank = 0; int numProcs = 1; #endif //////////////////// DECLARE VARIABLES /////////////////////// // define test variables VarFactory varFactory; VarPtr v = varFactory.testVar("v", HGRAD); // define trial variables VarPtr beta_n_u_hat = varFactory.fluxVar("\\widehat{\\beta \\cdot n }"); VarPtr u = varFactory.fieldVar("u"); FunctionPtr beta = Teuchos::rcp(new Beta()); //////////////////// BUILD MESH /////////////////////// BFPtr confusionBF = Teuchos::rcp( new BF(varFactory) ); // define nodes for mesh FieldContainer<double> meshBoundary(4,2); meshBoundary(0,0) = -1.0; // x1 meshBoundary(0,1) = -1.0; // y1 meshBoundary(1,0) = 1.0; meshBoundary(1,1) = -1.0; meshBoundary(2,0) = 1.0; meshBoundary(2,1) = 1.0; meshBoundary(3,0) = -1.0; meshBoundary(3,1) = 1.0; int horizontalCells = 32, verticalCells = 32; // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = Mesh::buildQuadMesh(meshBoundary, horizontalCells, verticalCells, confusionBF, H1Order, H1Order+pToAdd); //////////////////////////////////////////////////////////////////// // INITIALIZE FLOW FUNCTIONS //////////////////////////////////////////////////////////////////// BCPtr nullBC = Teuchos::rcp((BC*)NULL); RHSPtr nullRHS = Teuchos::rcp((RHS*)NULL); IPPtr nullIP = Teuchos::rcp((IP*)NULL); SolutionPtr prevTimeFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); SolutionPtr flowResidual = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); FunctionPtr u_prev_time = Teuchos::rcp( new PreviousSolutionFunction(prevTimeFlow, u) ); //////////////////// DEFINE BILINEAR FORM /////////////////////// Teuchos::RCP<RHSEasy> rhs = Teuchos::rcp( new RHSEasy ); FunctionPtr invDt = Teuchos::rcp(new ScalarParamFunction(1.0/dt)); // v terms: confusionBF->addTerm( beta * u, - v->grad() ); confusionBF->addTerm( beta_n_u_hat, v); confusionBF->addTerm( u, invDt*v ); rhs->addTerm( u_prev_time * invDt * v ); //////////////////// SPECIFY RHS /////////////////////// FunctionPtr f = Teuchos::rcp( new ConstantScalarFunction(0.0) ); rhs->addTerm( f * v ); // obviously, with f = 0 adding this term is not necessary! //////////////////// DEFINE INNER PRODUCT(S) /////////////////////// // robust test norm IPPtr ip = confusionBF->graphNorm(); // IPPtr ip = Teuchos::rcp(new IP); // ip->addTerm(v); // ip->addTerm(invDt*v - beta*v->grad()); //////////////////// CREATE BCs /////////////////////// Teuchos::RCP<BCEasy> bc = Teuchos::rcp( new BCEasy ); SpatialFilterPtr inflowBoundary = Teuchos::rcp( new InflowSquareBoundary(beta) ); FunctionPtr u0 = Teuchos::rcp( new ConstantScalarFunction(0) ); FunctionPtr n = Teuchos::rcp( new UnitNormalFunction ); bc->addDirichlet(beta_n_u_hat, inflowBoundary, beta*n*u0); Teuchos::RCP<Solution> solution = Teuchos::rcp( new Solution(mesh, bc, rhs, ip) ); // ==================== Register Solutions ========================== mesh->registerSolution(solution); mesh->registerSolution(prevTimeFlow); mesh->registerSolution(flowResidual); // ==================== SET INITIAL GUESS ========================== FunctionPtr u_init = Teuchos::rcp(new InitialCondition()); map<int, Teuchos::RCP<Function> > functionMap; functionMap[u->ID()] = u_init; prevTimeFlow->projectOntoMesh(functionMap); //////////////////// SOLVE & REFINE /////////////////////// // if (enforceLocalConservation) { // // FunctionPtr parity = Teuchos::rcp<Function>( new SideParityFunction ); // // LinearTermPtr conservedQuantity = Teuchos::rcp<LinearTerm>( new LinearTerm(parity, beta_n_u_minus_sigma_n) ); // LinearTermPtr conservedQuantity = Teuchos::rcp<LinearTerm>( new LinearTerm(1.0, beta_n_u_minus_sigma_n) ); // LinearTermPtr sourcePart = Teuchos::rcp<LinearTerm>( new LinearTerm(invDt, u) ); // conservedQuantity->addTerm(sourcePart, true); // solution->lagrangeConstraints()->addConstraint(conservedQuantity == u_prev_time * invDt); // } int timestepCount = 0; double time_tol = 1e-8; double L2_time_residual = 1e9; while((L2_time_residual > time_tol) && (timestepCount < numTimeSteps)) { solution->solve(false); // Subtract solutions to get residual flowResidual->setSolution(solution); flowResidual->addSolution(prevTimeFlow, -1.0); L2_time_residual = flowResidual->L2NormOfSolutionGlobal(u->ID()); if (rank == 0) { cout << endl << "Timestep: " << timestepCount << ", dt = " << dt << ", Time residual = " << L2_time_residual << endl; stringstream outfile; outfile << "rotatingCylinder_" << timestepCount; solution->writeToVTK(outfile.str(), 5); // Check local conservation FunctionPtr flux = Teuchos::rcp( new PreviousSolutionFunction(solution, beta_n_u_hat) ); FunctionPtr source = Teuchos::rcp( new PreviousSolutionFunction(flowResidual, u) ); source = invDt * source; Teuchos::Tuple<double, 3> fluxImbalances = checkConservation(flux, source, varFactory, mesh); cout << "Mass flux: Largest Local = " << fluxImbalances[0] << ", Global = " << fluxImbalances[1] << ", Sum Abs = " << fluxImbalances[2] << endl; } prevTimeFlow->setSolution(solution); // reset previous time solution to current time sol timestepCount++; } return 0; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); choice::MpiArgs args( argc, argv ); #else choice::Args args( argc, argv ); #endif int rank = Teuchos::GlobalMPISession::getRank(); int numProcs = Teuchos::GlobalMPISession::getNProc(); int nCells = args.Input<int>("--nCells", "num cells",2); int numSteps = args.Input<int>("--numSteps", "num NR steps",20); int polyOrder = 0; // define our manufactured solution or problem bilinear form: bool useTriangles = false; int pToAdd = 1; args.Process(); int H1Order = polyOrder + 1; //////////////////////////////////////////////////////////////////// // DEFINE VARIABLES //////////////////////////////////////////////////////////////////// // new-style bilinear form definition VarFactory varFactory; VarPtr fn = varFactory.fluxVar("\\widehat{\\beta_n_u}"); VarPtr u = varFactory.fieldVar("u"); VarPtr v = varFactory.testVar("v",HGRAD); BFPtr bf = Teuchos::rcp( new BF(varFactory) ); // initialize bilinear form //////////////////////////////////////////////////////////////////// // CREATE MESH //////////////////////////////////////////////////////////////////// // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = MeshUtilities::buildUnitQuadMesh(nCells , bf, H1Order, H1Order+pToAdd); //////////////////////////////////////////////////////////////////// // INITIALIZE BACKGROUND FLOW FUNCTIONS //////////////////////////////////////////////////////////////////// BCPtr nullBC = Teuchos::rcp((BC*)NULL); RHSPtr nullRHS = Teuchos::rcp((RHS*)NULL); IPPtr nullIP = Teuchos::rcp((IP*)NULL); SolutionPtr backgroundFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); SolutionPtr solnPerturbation = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); vector<double> e1(2),e2(2); e1[0] = 1; e2[1] = 1; FunctionPtr u_prev = Teuchos::rcp( new PreviousSolutionFunction(backgroundFlow, u) ); FunctionPtr beta = e1 * u_prev + Teuchos::rcp( new ConstantVectorFunction( e2 ) ); //////////////////////////////////////////////////////////////////// // DEFINE BILINEAR FORM //////////////////////////////////////////////////////////////////// // v: bf->addTerm( -u, beta * v->grad()); bf->addTerm( fn, v); //////////////////////////////////////////////////////////////////// // DEFINE RHS //////////////////////////////////////////////////////////////////// Teuchos::RCP<RHSEasy> rhs = Teuchos::rcp( new RHSEasy ); FunctionPtr u_prev_squared_div2 = 0.5 * u_prev * u_prev; rhs->addTerm((e1 * u_prev_squared_div2 + e2 * u_prev) * v->grad()); // ==================== SET INITIAL GUESS ========================== mesh->registerSolution(backgroundFlow); FunctionPtr zero = Function::constant(0.0); FunctionPtr u0 = Teuchos::rcp( new U0 ); FunctionPtr n = Teuchos::rcp( new UnitNormalFunction ); // FunctionPtr parity = Teuchos::rcp(new SideParityFunction); FunctionPtr u0_squared_div_2 = 0.5 * u0 * u0; map<int, Teuchos::RCP<Function> > functionMap; functionMap[u->ID()] = u0; // functionMap[fn->ID()] = -(e1 * u0_squared_div_2 + e2 * u0) * n * parity; backgroundFlow->projectOntoMesh(functionMap); // ==================== END SET INITIAL GUESS ========================== //////////////////////////////////////////////////////////////////// // DEFINE INNER PRODUCT //////////////////////////////////////////////////////////////////// IPPtr ip = Teuchos::rcp( new IP ); ip->addTerm( v ); ip->addTerm(v->grad()); // ip->addTerm( beta * v->grad() ); // omitting term to make IP non-dependent on u //////////////////////////////////////////////////////////////////// // DEFINE DIRICHLET BC //////////////////////////////////////////////////////////////////// SpatialFilterPtr outflowBoundary = Teuchos::rcp( new TopBoundary); SpatialFilterPtr inflowBoundary = Teuchos::rcp( new NegatedSpatialFilter(outflowBoundary) ); Teuchos::RCP<BCEasy> inflowBC = Teuchos::rcp( new BCEasy ); inflowBC->addDirichlet(fn,inflowBoundary, ( e1 * u0_squared_div_2 + e2 * u0) * n ); //////////////////////////////////////////////////////////////////// // CREATE SOLUTION OBJECT //////////////////////////////////////////////////////////////////// Teuchos::RCP<Solution> solution = Teuchos::rcp(new Solution(mesh, inflowBC, rhs, ip)); mesh->registerSolution(solution); solution->setCubatureEnrichmentDegree(10); //////////////////////////////////////////////////////////////////// // HESSIAN BIT + CHECKS ON GRADIENT + HESSIAN //////////////////////////////////////////////////////////////////// VarFactory hessianVars = varFactory.getBubnovFactory(VarFactory::BUBNOV_TRIAL); VarPtr du = hessianVars.test(u->ID()); // BFPtr hessianBF = Teuchos::rcp( new BF(hessianVars) ); // initialize bilinear form FunctionPtr du_current = Teuchos::rcp( new PreviousSolutionFunction(solution, u) ); FunctionPtr fnhat = Teuchos::rcp(new PreviousSolutionFunction(solution,fn)); LinearTermPtr residual = Teuchos::rcp(new LinearTerm);// residual residual->addTerm(fnhat*v,true); residual->addTerm( - (e1 * (u_prev_squared_div2) + e2 * (u_prev)) * v->grad(),true); LinearTermPtr Bdu = Teuchos::rcp(new LinearTerm);// residual Bdu->addTerm( - du_current*(beta*v->grad())); Teuchos::RCP<RieszRep> riesz = Teuchos::rcp(new RieszRep(mesh, ip, residual)); Teuchos::RCP<RieszRep> duRiesz = Teuchos::rcp(new RieszRep(mesh, ip, Bdu)); riesz->computeRieszRep(); FunctionPtr e_v = Teuchos::rcp(new RepFunction(v,riesz)); e_v->writeValuesToMATLABFile(mesh, "e_v.m"); FunctionPtr posErrPart = Teuchos::rcp(new PositivePart(e_v->dx())); // hessianBF->addTerm(e_v->dx()*u,du); // hessianBF->addTerm(posErrPart*u,du); // Teuchos::RCP<NullFilter> nullFilter = Teuchos::rcp(new NullFilter); // Teuchos::RCP<HessianFilter> hessianFilter = Teuchos::rcp(new HessianFilter(hessianBF)); Teuchos::RCP< LineSearchStep > LS_Step = Teuchos::rcp(new LineSearchStep(riesz)); double NL_residual = 9e99; for (int i = 0;i<numSteps;i++){ // write matrix to file and then resollve without hessian /* solution->setFilter(hessianFilter); stringstream oss; oss << "hessianMatrix" << i << ".dat"; solution->setWriteMatrixToFile(true,oss.str()); solution->solve(false); solution->setFilter(nullFilter); oss.str(""); // clear oss << "stiffnessMatrix" << i << ".dat"; solution->setWriteMatrixToFile(false,oss.str()); */ solution->solve(false); // do one solve to initialize things... double stepLength = 1.0; stepLength = LS_Step->stepSize(backgroundFlow,solution, NL_residual); // solution->setWriteMatrixToFile(true,"stiffness.dat"); backgroundFlow->addSolution(solution,stepLength); NL_residual = LS_Step->getNLResidual(); if (rank==0){ cout << "NL residual after adding = " << NL_residual << " with step size " << stepLength << endl; } double fd_gradient; for (int dofIndex = 0;dofIndex<mesh->numGlobalDofs();dofIndex++){ TestingUtilities::initializeSolnCoeffs(solnPerturbation); TestingUtilities::setSolnCoeffForGlobalDofIndex(solnPerturbation,1.0,dofIndex); fd_gradient = FiniteDifferenceUtilities::finiteDifferenceGradient(mesh, riesz, backgroundFlow, dofIndex); // CHECK GRADIENT LinearTermPtr b_u = bf->testFunctional(solnPerturbation); map<int,FunctionPtr> NL_err_rep_map; NL_err_rep_map[v->ID()] = Teuchos::rcp(new RepFunction(v,riesz)); FunctionPtr gradient = b_u->evaluate(NL_err_rep_map, TestingUtilities::isFluxOrTraceDof(mesh,dofIndex)); // use boundary part only if flux or trace double grad; if (TestingUtilities::isFluxOrTraceDof(mesh,dofIndex)){ grad = gradient->integralOfJump(mesh,10); }else{ grad = gradient->integrate(mesh,10); } double fdgrad = fd_gradient; double diff = grad-fdgrad; if (abs(diff)>1e-6 && i>0){ cout << "Found difference of " << diff << ", " << " with fd val = " << fdgrad << " and gradient = " << grad << " in dof " << dofIndex << ", isTraceDof = " << TestingUtilities::isFluxOrTraceDof(mesh,dofIndex) << endl; } } } VTKExporter exporter(solution, mesh, varFactory); if (rank==0){ exporter.exportSolution("qopt"); cout << endl; } return 0; }
int main() { // Generate output. ofstream output; output.open("numknapcov.txt"); // Generate input. ifstream input; input.open("list.txt"); // Check if input is opened succesfully. if (input.is_open() == false) { cerr << "Input file read error." << endl; output << "Input file read error." << endl; exit(0); } /********************************************************************************/ // Headers for output data. output << "Statistics of knapsack cover cuts applied to root relaxation." << endl; output << "problem " << "vars " << "cons " << "lincons " << "knapcons " << "knapcov " << "knaps " << "totalcuts " << "cuts " << "violknapcuts " << "initobj " << "endobj " << "gapclosed " << "timeinit " << "timecut " << "timemod" << endl; /********************************************************************************/ // loop to test all problems in list.txt while (input.good()) { // problem name string pname; getline(input, pname); // At the end of file just exit from loop. if (pname.empty()) { break; } cout << "Problem considered is: " << pname << endl; // Real stuff begins. // Ampl interface, jacobian and hessian. MINOTAUR_AMPL::AMPLInterfacePtr iface = MINOTAUR_AMPL::AMPLInterfacePtr(); JacobianPtr jPtr; //! Jacobian read from AMPL HessianOfLagPtr hPtr; //! Hessian read from AMPL // environment, timers and options: EnvPtr env = (EnvPtr) new Environment(); OptionDBPtr options; // problem to be solved. ProblemPtr minlp; // solver pointers, including status. FilterSQPEngine e(env); EngineStatus status; // Presolver. PresolverPtr pres; // give parameters. UInt argc2 = 2; std::string arg1 = "bnb"; std::string arg2 = pname; char** argv2 = new char* [2]; argv2[0] = &arg1[0]; argv2[1] = &arg2[0]; // Default options env->getOptions()->findBool("presolve")->setValue(false); env->getOptions()->findBool("use_native_cgraph")->setValue(true); env->getOptions()->findBool("nl_presolve")->setValue(false); // parse options env->readOptions(argc2, argv2); options = env->getOptions(); options->findString("interface_type")->setValue("AMPL"); // read minlp from AMPL. iface = (MINOTAUR_AMPL::AMPLInterfacePtr) new MINOTAUR_AMPL::AMPLInterface(env); minlp = iface->readInstance(pname); // Timer is obtained. Timer * timer = env->getNewTimer(); // Nonlinearize objective function. Bool MIPCONSIDERED = false; if (MIPCONSIDERED == true) { ObjectivePtr initobjfun = minlp->getObjective(); if (initobjfun->getObjectiveType() == Maximize) { cerr << "Objective type is Maximize, change it to Minimize." << endl; exit(0); } LinearFunctionPtr lfinitobj = initobjfun->getLinearFunction(); // NonlinearFunctionPtr nlfobj = (NonlinearFunctionPtr) new NonlinearFunction(); CGraphPtr nlfobj = (CGraphPtr) new CGraph(); logobj(lfinitobj, nlfobj); FunctionPtr logobjfun = (FunctionPtr) new Function(nlfobj); ObjectiveType otyp = Minimize; minlp->changeObj(logobjfun, 0); } minlp->calculateSize(); minlp->prepareForSolve(); // Change format of problem to be suitable for Minotaur. HandlerVector handlers; // Use presolver to standardize problem. //pres = (PresolverPtr) new Presolver(minlp, env, handlers); //pres->standardize(); minlp->calculateSize(); minlp->prepareForSolve(); minlp->setJacobian(jPtr); minlp->setHessian(hPtr); minlp->setNativeDer(); minlp->calculateSize(); minlp->prepareForSolve(); minlp->setNativeDer(); //minlp->write(std::cout); /**************************************************************/ // Given problem statistics . // Number of variables. UInt numvars = minlp->getNumVars(); // number of constraints. UInt numcons = minlp->getNumCons(); // linear constraints. UInt numlin = minlp->getNumLinCons(); /*************************************************************/ // set option for engine to resolve to solve NLP repeatedly. // Probbaly does nothing. e.setOptionsForRepeatedSolve(); // load problem. e.load(minlp); // Solve problem. timer->start(); status = e.solve(); /********************************************************************/ // Solution time of relaxation. Double timeinit = timer->query(); timer->stop(); // Solution objective value Double initobj = e.getSolutionValue(); /********************************************************************/ std::cout << "Relaxation objective value = " << initobj << std::endl; // Get solution from engine. ConstSolutionPtr sol = e.getSolution(); // Construct relaxation. RelaxationPtr rel = (RelaxationPtr) new Relaxation(minlp); // Time for cut generation. timer->start(); // Generate kanpsack cover cuts. CoverCutGeneratorPtr knapgen = (CoverCutGeneratorPtr) new CoverCutGenerator(rel, sol, env); /*******************************************************************/ Double timecut = timer->query(); timer->stop(); /*******************************************************************/ // Get statistics of cut generator. ConstCovCutGenStatsPtr knapstats = knapgen->getStats(); /*******************************************************************/ // Knapsack cut generator statistics. // knapsack constraints. UInt numknap = (knapgen->getKnapsackList())->getNumKnaps(); // knapsacks that has cover sets. UInt numknapcov = knapgen->getNumCons(); // knapsack subproblems solved, i.e number of lifting subproblems solved. UInt knaps = knapstats->knaps; // cover cuts including duplicates. UInt totalcuts = knapstats->totalcuts; // cuts without duplicates. UInt numknapcuts = knapstats->cuts; // violated cuts. UInt violknapcuts = knapstats->violated; /*******************************************************************/ std::cout << "Number of knapsack cover cuts to be applied is: " << knapstats->violated << std::endl; // Get the violated cuts from generator. CutVector knapcuts = knapgen->getViolatedCutList(); // Iterators for cuts CutVectorConstIter it; CutVectorConstIter begin = knapcuts.begin(); CutVectorConstIter end = knapcuts.end(); // Apply the cuts to the problem. // Violation list. DoubleVector knapviols = knapgen->getViolList(); UInt curknap = 0; Double maxviol = 0.0; for (it=begin; it!=end; ++it) { std::cout << "Violation obtained from this constraint is: " << knapviols[curknap] << std::endl; ConstraintPtr newcons = rel->newConstraint((*it)->getFunction(), (*it)->getLb(), (*it)->getUb()); if (maxviol < knapviols[curknap]) { maxviol = knapviols[curknap]; } // add constraint to engine does not do anything. // Thus, we add constraint to the relaxation and reload it to engine. // e.addConstraint(newcons); } /*******************************************************************/ // Solution time of knapsack cover cuts added problem. Double timemod = 0.0; // Objective value after adding knapsack cover cuts. Double endobj = 0.0; // Gap closed by using knapsack cover cuts. Double gapknap = 0.0; /*******************************************************************/ if (violknapcuts >= 1) { // Reload problem to engine. // Check if we should reload the modified problem. e.clear(); const Double * xupdated; if (WARMSTART == 1) { // Set initial point as the solution of root solution. xupdated = sol->getPrimal(); rel->setInitialPoint(xupdated); } // Load the modified problem. e.load(rel); // warmstart continues. if (WARMSTART == 1) { // Before presolve, we set initial primal and // dual solutions as the root solution. SolutionPtr solupdated = (SolutionPtr) new Solution(initobj, xupdated, rel); // Create new dual solution. const Double * dualofvars = sol->getDualOfVars(); solupdated->setDualOfVars(dualofvars); const Double * initdualofcons = sol->getDualOfCons(); UInt numconsupdated = rel->getNumCons(); Double * dualofcons = new Double[numconsupdated]; memcpy(dualofcons, initdualofcons, numcons*sizeof(Double)); for (UInt indexx = numcons; indexx < numconsupdated; ++indexx) { dualofcons[indexx] = 0.0; } solupdated->setDualOfCons(dualofcons); FilterWSPtr warmstart = (FilterWSPtr) new FilterSQPWarmStart(); warmstart->setPoint(solupdated); e.loadFromWarmStart(warmstart); delete [] dualofcons; } // Solution time after adding knapsack cover cuts to relaxation. timer->start(); // Resolve the problem. e.solve(); /*******************************************************************/ // Solution time of knapsack cover cuts added problem. timemod = timer->query(); timer->stop(); // Objective value after adding knapsack cover cuts. endobj = e.getSolutionValue(); // Gap closed by using knapsack cover cuts. gapknap = (endobj-initobj)/fabs(initobj) * 100; /*******************************************************************/ } else { /*******************************************************************/ // Solution time of knapsack cover cuts added problem. timemod = timeinit; // Objective value after adding knapsack cover cuts. endobj = initobj; // Gap closed by using knapsack cover cuts. gapknap = 0.0; /*******************************************************************/ } std::cout << "Objective value of relaxation after adding cuts: " << endobj << std::endl; cout << pname << " " << numvars << " " << numcons << " " << numlin << " " << numknap << " " << numknapcov << " " << knaps << " " << totalcuts << " " << numknapcuts << " " << violknapcuts << std::fixed << std::setprecision(2) << " " << initobj << " " << endobj << " " << gapknap << " " << timeinit << " " << timecut << " " << timemod << endl; if (numknap >= 1) { // Save output data. output << pname << " " << numvars << " " << numcons << " " << numlin << " " << numknap << " " << numknapcov << " " << knaps << " " << totalcuts << " " << numknapcuts << " " << violknapcuts << std::fixed << std::setprecision(2) << " " << initobj << " " << endobj << " " << gapknap << " " << timeinit << " " << timecut << " " << timemod << endl; } delete iface; delete [] argv2; } output.close(); input.close(); return 0; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif int commRank = Teuchos::GlobalMPISession::getRank(); Comm.Barrier(); // set breakpoint here to allow debugger attachment to other MPI processes than the one you automatically attached to. Teuchos::CommandLineProcessor cmdp(false,true); // false: don't throw exceptions; true: do return errors for unrecognized options // problem parameters: int spaceDim = 2; double Re = 40; bool steady = false; string problemChoice = "TaylorGreen"; int numRefs = 1; int p = 2, delta_p = 2; int numXElems = 1; int numTElems = 1; int numSlabs = 1; bool useConformingTraces = false; string solverChoice = "KLU"; string multigridStrategyString = "W-cycle"; bool useCondensedSolve = false; bool useConjugateGradient = true; bool logFineOperator = false; // double solverTolerance = 1e-8; double nonlinearTolerance = 1e-5; // int maxLinearIterations = 10000; int maxNonlinearIterations = 20; int cgMaxIterations = 10000; double cgTol = 1e-8; bool computeL2Error = false; bool exportSolution = false; bool saveSolution = false; bool loadSolution = false; int loadRef = 0; int loadDirRef = 0; string norm = "Graph"; string rootDir = "."; string tag=""; cmdp.setOption("spaceDim", &spaceDim, "spatial dimension"); cmdp.setOption("Re", &Re, "Re"); cmdp.setOption("steady", "transient", &steady, "use steady incompressible Navier-Stokes"); cmdp.setOption("problem", &problemChoice, "Kovasznay, TaylorGreen"); cmdp.setOption("polyOrder",&p,"polynomial order for field variable u"); cmdp.setOption("delta_p", &delta_p, "test space polynomial order enrichment"); cmdp.setOption("numRefs",&numRefs,"number of refinements"); cmdp.setOption("numXElems",&numXElems,"number of elements in x direction"); cmdp.setOption("numTElems",&numTElems,"number of elements in t direction"); cmdp.setOption("numSlabs",&numSlabs,"number of time slabs to use"); cmdp.setOption("norm", &norm, "norm"); cmdp.setOption("conformingTraces", "nonconformingTraces", &useConformingTraces, "use conforming traces"); cmdp.setOption("solver", &solverChoice, "KLU, SuperLU, MUMPS, GMG-Direct, GMG-ILU, GMG-IC"); cmdp.setOption("multigridStrategy", &multigridStrategyString, "Multigrid strategy: V-cycle, W-cycle, Full, or Two-level"); cmdp.setOption("useCondensedSolve", "useStandardSolve", &useCondensedSolve); cmdp.setOption("CG", "GMRES", &useConjugateGradient); cmdp.setOption("logFineOperator", "dontLogFineOperator", &logFineOperator); // cmdp.setOption("solverTolerance", &solverTolerance, "iterative solver tolerance"); cmdp.setOption("nonlinearTolerance", &nonlinearTolerance, "nonlinear solver tolerance"); // cmdp.setOption("maxLinearIterations", &maxLinearIterations, "maximum number of iterations for linear solver"); cmdp.setOption("maxNonlinearIterations", &maxNonlinearIterations, "maximum number of iterations for Newton solver"); cmdp.setOption("exportDir", &rootDir, "export directory"); cmdp.setOption("computeL2Error", "skipL2Error", &computeL2Error, "compute L2 error"); cmdp.setOption("exportSolution", "skipExport", &exportSolution, "export solution to HDF5"); cmdp.setOption("saveSolution", "skipSave", &saveSolution, "save mesh and solution to HDF5"); cmdp.setOption("loadSolution", "skipLoad", &loadSolution, "load mesh and solution from HDF5"); cmdp.setOption("loadRef", &loadRef, "load refinement number"); cmdp.setOption("loadDirRef", &loadDirRef, "which refinement directory to load from"); cmdp.setOption("tag", &tag, "output tag"); if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } map<string, Teuchos::RCP<IncompressibleProblem>> problems; problems["ManufacturedSolution"] = Teuchos::rcp(new IncompressibleManufacturedSolution(steady, Re, numXElems)); problems["Kovasznay"] = Teuchos::rcp(new KovasznayProblem(steady, Re)); problems["TaylorGreen"] = Teuchos::rcp(new TaylorGreenProblem(steady, Re, numXElems, numSlabs)); problems["Cylinder"] = Teuchos::rcp(new CylinderProblem(steady, Re, numSlabs)); problems["SquareCylinder"] = Teuchos::rcp(new SquareCylinderProblem(steady, Re, numSlabs)); Teuchos::RCP<IncompressibleProblem> problem = problems.at(problemChoice); // if (commRank == 0) // { // Solver::printAvailableSolversReport(); // cout << endl; // } Teuchos::RCP<Time> totalTimer = Teuchos::TimeMonitor::getNewCounter("Total Time"); totalTimer->start(true); for (; problem->currentStep() < problem->numSlabs(); problem->advanceStep()) { if (problem->numSlabs() > 1 && commRank == 0 && !steady) cout << "Solving time slab [" << problem->currentT0() << ", " << problem->currentT1() << "]" << endl; ostringstream problemName; string isSteady = "Steady"; if (!steady) isSteady = "Transient"; problemName << isSteady << problemChoice << spaceDim << "D_slab" << problem->currentStep() << "_" << norm << "_" << Re << "_p" << p << "_" << solverChoice; if (tag != "") problemName << "_" << tag; ostringstream saveDir; saveDir << problemName.str() << "_ref" << loadRef; int success = mkdir((rootDir+"/"+saveDir.str()).c_str(), S_IRWXU | S_IRWXG); string dataFileLocation = rootDir + "/" + saveDir.str() + "/" + saveDir.str() + ".data"; string exportName = saveDir.str(); ostringstream loadDir; loadDir << problemName.str() << "_ref" << loadDirRef; string loadFilePrefix = ""; if (loadSolution) { loadFilePrefix = rootDir + "/" + loadDir.str() + "/" + saveDir.str(); if (commRank == 0) cout << "Loading previous solution " << loadFilePrefix << endl; } // ostringstream saveDir; // saveDir << problemName.str() << "_ref" << loadRef; string saveFilePrefix = rootDir + "/" + saveDir.str() + "/" + problemName.str(); if (saveSolution && commRank == 0) cout << "Saving to " << saveFilePrefix << endl; Teuchos::ParameterList parameters; parameters.set("spaceDim", spaceDim); parameters.set("steady", steady); parameters.set("mu", 1./Re); parameters.set("useConformingTraces", useConformingTraces); parameters.set("fieldPolyOrder", p); parameters.set("delta_p", delta_p); parameters.set("numTElems", numTElems); parameters.set("norm", norm); parameters.set("savedSolutionAndMeshPrefix", loadFilePrefix); SpaceTimeIncompressibleFormulationPtr form = Teuchos::rcp(new SpaceTimeIncompressibleFormulation(problem, parameters)); MeshPtr mesh = form->solutionUpdate()->mesh(); vector<MeshPtr> meshesCoarseToFine; MeshPtr k0Mesh = Teuchos::rcp( new Mesh (mesh->getTopology()->deepCopy(), form->bf(), 1, delta_p) ); meshesCoarseToFine.push_back(k0Mesh); meshesCoarseToFine.push_back(mesh); // mesh->registerObserver(k0Mesh); // Set up boundary conditions problem->setBCs(form); // Set up solution SolutionPtr solutionUpdate = form->solutionUpdate(); SolutionPtr solutionBackground = form->solutionBackground(); // dynamic_cast<AnalyticalIncompressibleProblem*>(problem.get())->projectExactSolution(solutionBackground); RefinementStrategyPtr refStrategy = form->getRefinementStrategy(); Teuchos::RCP<HDF5Exporter> exporter; if (exportSolution) exporter = Teuchos::rcp(new HDF5Exporter(mesh,exportName, rootDir)); Teuchos::RCP<Time> solverTime = Teuchos::TimeMonitor::getNewCounter("Solve Time"); map<string, SolverPtr> solvers; solvers["KLU"] = Solver::getSolver(Solver::KLU, true); #if defined(HAVE_AMESOS_SUPERLUDIST) || defined(HAVE_AMESOS2_SUPERLUDIST) solvers["SuperLUDist"] = Solver::getSolver(Solver::SuperLUDist, true); #endif #ifdef HAVE_AMESOS_MUMPS solvers["MUMPS"] = Solver::getSolver(Solver::MUMPS, true); #endif bool useStaticCondensation = false; GMGOperator::MultigridStrategy multigridStrategy; if (multigridStrategyString == "Two-level") { multigridStrategy = GMGOperator::TWO_LEVEL; } else if (multigridStrategyString == "W-cycle") { multigridStrategy = GMGOperator::W_CYCLE; } else if (multigridStrategyString == "V-cycle") { multigridStrategy = GMGOperator::V_CYCLE; } else if (multigridStrategyString == "Full-V") { multigridStrategy = GMGOperator::FULL_MULTIGRID_V; } else if (multigridStrategyString == "Full-W") { multigridStrategy = GMGOperator::FULL_MULTIGRID_W; } else { TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, "unrecognized multigrid strategy"); } ofstream dataFile(dataFileLocation); dataFile << "ref\t " << "elements\t " << "dofs\t " << "energy\t " << "l2\t " << "solvetime\t" << "iterations\t " << endl; // { // // ostringstream saveFile; // // saveFile << saveFilePrefix << "_ref" << -1; // // form->save(saveFile.str()); // exporter->exportSolution(solutionBackground, -1); // if (commRank == 0) // cout << "Done exporting" << endl; // } for (int refIndex=loadRef; refIndex <= numRefs; refIndex++) { double l2Update = 1e10; int iterCount = 0; solverTime->start(true); Teuchos::RCP<GMGSolver> gmgSolver; if (solverChoice[0] == 'G') { // gmgSolver = Teuchos::rcp( new GMGSolver(solutionUpdate, k0Mesh, maxLinearIterations, solverTolerance, Solver::getDirectSolver(true), useStaticCondensation)); bool reuseFactorization = true; SolverPtr coarseSolver = Solver::getDirectSolver(reuseFactorization); gmgSolver = Teuchos::rcp(new GMGSolver(solutionUpdate, meshesCoarseToFine, cgMaxIterations, cgTol, multigridStrategy, coarseSolver, useCondensedSolve)); gmgSolver->setUseConjugateGradient(useConjugateGradient); int azOutput = 20; // print residual every 20 CG iterations gmgSolver->setAztecOutput(azOutput); gmgSolver->gmgOperator()->setNarrateOnRankZero(logFineOperator,"finest GMGOperator"); // gmgSolver->setAztecOutput(azOutput); // if (solverChoice == "GMG-Direct") // gmgSolver->gmgOperator()->setSchwarzFactorizationType(GMGOperator::Direct); // if (solverChoice == "GMG-ILU") // gmgSolver->gmgOperator()->setSchwarzFactorizationType(GMGOperator::ILU); // if (solverChoice == "GMG-IC") // gmgSolver->gmgOperator()->setSchwarzFactorizationType(GMGOperator::IC); } while (l2Update > nonlinearTolerance && iterCount < maxNonlinearIterations) { if (solverChoice[0] == 'G') solutionUpdate->solve(gmgSolver); else solutionUpdate->condensedSolve(solvers[solverChoice]); // Compute L2 norm of update double u1L2Update = solutionUpdate->L2NormOfSolutionGlobal(form->u(1)->ID()); double u2L2Update = solutionUpdate->L2NormOfSolutionGlobal(form->u(2)->ID()); l2Update = sqrt(u1L2Update*u1L2Update + u2L2Update*u2L2Update); if (commRank == 0) cout << "Nonlinear Update:\t " << l2Update << endl; form->updateSolution(); iterCount++; } double solveTime = solverTime->stop(); double energyError = solutionUpdate->energyErrorTotal(); double l2Error = 0; if (computeL2Error) { l2Error = problem->computeL2Error(form, solutionBackground); } if (commRank == 0) { cout << "Refinement: " << refIndex << " \tElements: " << mesh->numActiveElements() << " \tDOFs: " << mesh->numGlobalDofs() << " \tEnergy Error: " << energyError << " \tL2 Error: " << l2Error << " \tSolve Time: " << solveTime << " \tTotal Time: " << totalTimer->totalElapsedTime(true) // << " \tIteration Count: " << iterationCount << endl; dataFile << refIndex << " " << mesh->numActiveElements() << " " << mesh->numGlobalDofs() << " " << energyError << " " << l2Error << " " << solveTime << " " << totalTimer->totalElapsedTime(true) // << " " << iterationCount << endl; } if (exportSolution) exporter->exportSolution(solutionBackground, refIndex); if (saveSolution) { ostringstream saveFile; saveFile << saveFilePrefix << "_ref" << refIndex; form->save(saveFile.str()); } if (refIndex != numRefs) { // k0Mesh = Teuchos::rcp( new Mesh (mesh->getTopology()->deepCopy(), form->bf(), 1, delta_p) ); // meshesCoarseToFine.push_back(k0Mesh); refStrategy->refine(); meshesCoarseToFine.push_back(mesh); } } dataFile.close(); } double totalTime = totalTimer->stop(); if (commRank == 0) cout << "Total time = " << totalTime << endl; return 0; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); choice::MpiArgs args( argc, argv ); #else choice::Args args( argc, argv ); #endif int commRank = Teuchos::GlobalMPISession::getRank(); int numProcs = Teuchos::GlobalMPISession::getNProc(); // Required arguments int numRefs = args.Input<int>("--numRefs", "number of refinement steps"); bool enforceLocalConservation = args.Input<bool>("--conserve", "enforce local conservation"); bool steady = args.Input<bool>("--steady", "run steady rather than transient"); // Optional arguments (have defaults) double dt = args.Input("--dt", "time step", 0.25); int numTimeSteps = args.Input("--nt", "number of time steps", 20); halfWidth = args.Input("--halfWidth", "half width of inlet profile", 1.0); args.Process(); //////////////////// DECLARE VARIABLES /////////////////////// // define test variables VarFactory varFactory; VarPtr v = varFactory.testVar("v", HGRAD); // define trial variables VarPtr beta_n_u_hat = varFactory.fluxVar("\\widehat{\\beta \\cdot n }"); VarPtr u = varFactory.fieldVar("u"); vector<double> beta; beta.push_back(1.0); beta.push_back(0.0); //////////////////// BUILD MESH /////////////////////// BFPtr bf = Teuchos::rcp( new BF(varFactory) ); // define nodes for mesh FieldContainer<double> meshBoundary(4,2); meshBoundary(0,0) = 0.0; // x1 meshBoundary(0,1) = -2.0; // y1 meshBoundary(1,0) = 4.0; meshBoundary(1,1) = -2.0; meshBoundary(2,0) = 4.0; meshBoundary(2,1) = 2.0; meshBoundary(3,0) = 0.0; meshBoundary(3,1) = 2.0; int horizontalCells = 8, verticalCells = 8; // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = Mesh::buildQuadMesh(meshBoundary, horizontalCells, verticalCells, bf, H1Order, H1Order+pToAdd); //////////////////////////////////////////////////////////////////// // INITIALIZE FLOW FUNCTIONS //////////////////////////////////////////////////////////////////// BCPtr nullBC = Teuchos::rcp((BC*)NULL); RHSPtr nullRHS = Teuchos::rcp((RHS*)NULL); IPPtr nullIP = Teuchos::rcp((IP*)NULL); SolutionPtr prevTimeFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); SolutionPtr flowResidual = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); FunctionPtr u_prev_time = Teuchos::rcp( new PreviousSolutionFunction(prevTimeFlow, u) ); //////////////////// DEFINE BILINEAR FORM /////////////////////// Teuchos::RCP<RHSEasy> rhs = Teuchos::rcp( new RHSEasy ); FunctionPtr invDt = Teuchos::rcp(new ScalarParamFunction(1.0/dt)); // v terms: bf->addTerm( beta * u, - v->grad() ); bf->addTerm( beta_n_u_hat, v); if (!steady) { bf->addTerm( u, invDt*v ); rhs->addTerm( u_prev_time * invDt * v ); } //////////////////// SPECIFY RHS /////////////////////// FunctionPtr f = Teuchos::rcp( new ConstantScalarFunction(0.0) ); rhs->addTerm( f * v ); // obviously, with f = 0 adding this term is not necessary! //////////////////// DEFINE INNER PRODUCT(S) /////////////////////// IPPtr ip = bf->graphNorm(); // ip->addTerm(v); // ip->addTerm(beta*v->grad()); //////////////////// CREATE BCs /////////////////////// Teuchos::RCP<BCEasy> bc = Teuchos::rcp( new BCEasy ); SpatialFilterPtr lBoundary = Teuchos::rcp( new LeftBoundary ); FunctionPtr u1 = Teuchos::rcp( new InletBC ); bc->addDirichlet(beta_n_u_hat, lBoundary, -u1); Teuchos::RCP<Solution> solution = Teuchos::rcp( new Solution(mesh, bc, rhs, ip) ); // ==================== Register Solutions ========================== mesh->registerSolution(solution); mesh->registerSolution(prevTimeFlow); mesh->registerSolution(flowResidual); // ==================== SET INITIAL GUESS ========================== double u_free = 0.0; map<int, Teuchos::RCP<Function> > functionMap; // functionMap[u->ID()] = Teuchos::rcp( new ConInletBC functionMap[u->ID()] = Teuchos::rcp( new InletBC ); prevTimeFlow->projectOntoMesh(functionMap); //////////////////// SOLVE & REFINE /////////////////////// if (enforceLocalConservation) { if (steady) { FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); solution->lagrangeConstraints()->addConstraint(beta_n_u_hat == zero); } else { // FunctionPtr parity = Teuchos::rcp<Function>( new SideParityFunction ); // LinearTermPtr conservedQuantity = Teuchos::rcp<LinearTerm>( new LinearTerm(parity, beta_n_u_minus_sigma_n) ); LinearTermPtr conservedQuantity = Teuchos::rcp<LinearTerm>( new LinearTerm(1.0, beta_n_u_hat) ); LinearTermPtr sourcePart = Teuchos::rcp<LinearTerm>( new LinearTerm(invDt, u) ); conservedQuantity->addTerm(sourcePart, true); solution->lagrangeConstraints()->addConstraint(conservedQuantity == u_prev_time * invDt); } } double energyThreshold = 0.2; // for mesh refinements RefinementStrategy refinementStrategy( solution, energyThreshold ); VTKExporter exporter(solution, mesh, varFactory); for (int refIndex=0; refIndex<=numRefs; refIndex++) { if (steady) { solution->solve(false); if (commRank == 0) { stringstream outfile; outfile << "Convection_" << refIndex; exporter.exportSolution(outfile.str()); // Check local conservation FunctionPtr flux = Teuchos::rcp( new PreviousSolutionFunction(solution, beta_n_u_hat) ); FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); Teuchos::Tuple<double, 3> fluxImbalances = checkConservation(flux, zero, varFactory, mesh); cout << "Mass flux: Largest Local = " << fluxImbalances[0] << ", Global = " << fluxImbalances[1] << ", Sum Abs = " << fluxImbalances[2] << endl; } } else { int timestepCount = 0; double time_tol = 1e-8; double L2_time_residual = 1e9; // cout << L2_time_residual <<" "<< time_tol << timestepCount << numTimeSteps << endl; while((L2_time_residual > time_tol) && (timestepCount < numTimeSteps)) { solution->solve(false); // Subtract solutions to get residual flowResidual->setSolution(solution); flowResidual->addSolution(prevTimeFlow, -1.0); L2_time_residual = flowResidual->L2NormOfSolutionGlobal(u->ID()); if (commRank == 0) { cout << endl << "Timestep: " << timestepCount << ", dt = " << dt << ", Time residual = " << L2_time_residual << endl; stringstream outfile; outfile << "TransientConvection_" << refIndex << "-" << timestepCount; exporter.exportSolution(outfile.str()); // Check local conservation FunctionPtr flux = Teuchos::rcp( new PreviousSolutionFunction(solution, beta_n_u_hat) ); FunctionPtr source = Teuchos::rcp( new PreviousSolutionFunction(flowResidual, u) ); source = -invDt * source; Teuchos::Tuple<double, 3> fluxImbalances = checkConservation(flux, source, varFactory, mesh); cout << "Mass flux: Largest Local = " << fluxImbalances[0] << ", Global = " << fluxImbalances[1] << ", Sum Abs = " << fluxImbalances[2] << endl; } prevTimeFlow->setSolution(solution); // reset previous time solution to current time sol timestepCount++; } } if (refIndex < numRefs) refinementStrategy.refine(commRank==0); // print to console on commRank 0 } return 0; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); int rank=mpiSession.getRank(); int numProcs=mpiSession.getNProc(); #else int rank = 0; int numProcs = 1; #endif int polyOrder = 2; // define our manufactured solution or problem bilinear form: double epsilon = 1e-3; bool useTriangles = false; int pToAdd = 2; int nCells = 2; if ( argc > 1) { nCells = atoi(argv[1]); if (rank==0) { cout << "numCells = " << nCells << endl; } } int numSteps = 20; if ( argc > 2) { numSteps = atoi(argv[2]); if (rank==0) { cout << "num NR steps = " << numSteps << endl; } } int useHessian = 0; // defaults to "not use" if ( argc > 3) { useHessian = atoi(argv[3]); if (rank==0) { cout << "useHessian = " << useHessian << endl; } } int thresh = numSteps; // threshhold for when to apply linesearch/hessian if ( argc > 4) { thresh = atoi(argv[4]); if (rank==0) { cout << "thresh = " << thresh << endl; } } int H1Order = polyOrder + 1; double energyThreshold = 0.2; // for mesh refinements double nonlinearStepSize = 0.5; double nonlinearRelativeEnergyTolerance = 1e-8; // used to determine convergence of the nonlinear solution //////////////////////////////////////////////////////////////////// // DEFINE VARIABLES //////////////////////////////////////////////////////////////////// // new-style bilinear form definition VarFactory varFactory; VarPtr uhat = varFactory.traceVar("\\widehat{u}"); VarPtr beta_n_u_minus_sigma_hat = varFactory.fluxVar("\\widehat{\\beta_n u - \\sigma_n}"); VarPtr u = varFactory.fieldVar("u"); VarPtr sigma1 = varFactory.fieldVar("\\sigma_1"); VarPtr sigma2 = varFactory.fieldVar("\\sigma_2"); VarPtr tau = varFactory.testVar("\\tau",HDIV); VarPtr v = varFactory.testVar("v",HGRAD); BFPtr bf = Teuchos::rcp( new BF(varFactory) ); // initialize bilinear form //////////////////////////////////////////////////////////////////// // CREATE MESH //////////////////////////////////////////////////////////////////// // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = MeshUtilities::buildUnitQuadMesh(nCells, bf, H1Order, H1Order+pToAdd); mesh->setPartitionPolicy(Teuchos::rcp(new ZoltanMeshPartitionPolicy("HSFC"))); //////////////////////////////////////////////////////////////////// // INITIALIZE BACKGROUND FLOW FUNCTIONS //////////////////////////////////////////////////////////////////// BCPtr nullBC = Teuchos::rcp((BC*)NULL); RHSPtr nullRHS = Teuchos::rcp((RHS*)NULL); IPPtr nullIP = Teuchos::rcp((IP*)NULL); SolutionPtr backgroundFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); vector<double> e1(2); // (1,0) e1[0] = 1; vector<double> e2(2); // (0,1) e2[1] = 1; FunctionPtr u_prev = Teuchos::rcp( new PreviousSolutionFunction(backgroundFlow, u) ); FunctionPtr beta = e1 * u_prev + Teuchos::rcp( new ConstantVectorFunction( e2 ) ); //////////////////////////////////////////////////////////////////// // DEFINE BILINEAR FORM //////////////////////////////////////////////////////////////////// // tau parts: // 1/eps (sigma, tau)_K + (u, div tau)_K - (u_hat, tau_n)_dK bf->addTerm(sigma1 / epsilon, tau->x()); bf->addTerm(sigma2 / epsilon, tau->y()); bf->addTerm(u, tau->div()); bf->addTerm( - uhat, tau->dot_normal() ); // v: // (sigma, grad v)_K - (sigma_hat_n, v)_dK - (u, beta dot grad v) + (u_hat * n dot beta, v)_dK bf->addTerm( sigma1, v->dx() ); bf->addTerm( sigma2, v->dy() ); bf->addTerm( -u, beta * v->grad()); bf->addTerm( beta_n_u_minus_sigma_hat, v); // ==================== SET INITIAL GUESS ========================== mesh->registerSolution(backgroundFlow); FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); FunctionPtr u0 = Teuchos::rcp( new U0 ); map<int, Teuchos::RCP<Function> > functionMap; functionMap[u->ID()] = u0; functionMap[sigma1->ID()] = zero; functionMap[sigma2->ID()] = zero; backgroundFlow->projectOntoMesh(functionMap); // ==================== END SET INITIAL GUESS ========================== //////////////////////////////////////////////////////////////////// // DEFINE INNER PRODUCT //////////////////////////////////////////////////////////////////// // function to scale the squared guy by epsilon/h FunctionPtr epsilonOverHScaling = Teuchos::rcp( new EpsilonScaling(epsilon) ); IPPtr ip = Teuchos::rcp( new IP ); ip->addTerm( epsilonOverHScaling * (1.0/sqrt(epsilon))* tau); ip->addTerm( tau->div()); // ip->addTerm( epsilonOverHScaling * v ); ip->addTerm( v ); ip->addTerm( sqrt(epsilon) * v->grad() ); ip->addTerm(v->grad()); // ip->addTerm( beta * v->grad() ); //////////////////////////////////////////////////////////////////// // DEFINE RHS //////////////////////////////////////////////////////////////////// RHSPtr rhs = RHS::rhs(); FunctionPtr u_prev_squared_div2 = 0.5 * u_prev * u_prev; rhs->addTerm((e1 * u_prev_squared_div2 + e2 * u_prev) * v->grad() - u_prev * tau->div()); //////////////////////////////////////////////////////////////////// // DEFINE DIRICHLET BC //////////////////////////////////////////////////////////////////// FunctionPtr n = Teuchos::rcp( new UnitNormalFunction ); SpatialFilterPtr outflowBoundary = Teuchos::rcp( new TopBoundary); SpatialFilterPtr inflowBoundary = Teuchos::rcp( new NegatedSpatialFilter(outflowBoundary) ); BCPtr inflowBC = BC::bc(); FunctionPtr u0_squared_div_2 = 0.5 * u0 * u0; inflowBC->addDirichlet(beta_n_u_minus_sigma_hat,inflowBoundary, ( e1 * u0_squared_div_2 + e2 * u0) * n ); //////////////////////////////////////////////////////////////////// // CREATE SOLUTION OBJECT //////////////////////////////////////////////////////////////////// Teuchos::RCP<Solution> solution = Teuchos::rcp(new Solution(mesh, inflowBC, rhs, ip)); mesh->registerSolution(solution); //////////////////////////////////////////////////////////////////// // WARNING: UNFINISHED HESSIAN BIT //////////////////////////////////////////////////////////////////// VarFactory hessianVars = varFactory.getBubnovFactory(VarFactory::BUBNOV_TRIAL); VarPtr du = hessianVars.test(u->ID()); BFPtr hessianBF = Teuchos::rcp( new BF(hessianVars) ); // initialize bilinear form // FunctionPtr e_v = Function::constant(1.0); // dummy error rep function for now - should do nothing FunctionPtr u_current = Teuchos::rcp( new PreviousSolutionFunction(solution, u) ); FunctionPtr sig1_prev = Teuchos::rcp( new PreviousSolutionFunction(solution, sigma1) ); FunctionPtr sig2_prev = Teuchos::rcp( new PreviousSolutionFunction(solution, sigma2) ); FunctionPtr sig_prev = (e1*sig1_prev + e2*sig2_prev); FunctionPtr fnhat = Teuchos::rcp(new PreviousSolutionFunction(solution,beta_n_u_minus_sigma_hat)); FunctionPtr uhat_prev = Teuchos::rcp(new PreviousSolutionFunction(solution,uhat)); LinearTermPtr residual = Teuchos::rcp(new LinearTerm);// residual residual->addTerm(fnhat*v - (e1 * (u_prev_squared_div2 - sig1_prev) + e2 * (u_prev - sig2_prev)) * v->grad()); residual->addTerm((1/epsilon)*sig_prev * tau + u_prev * tau->div() - uhat_prev*tau->dot_normal()); LinearTermPtr Bdu = Teuchos::rcp(new LinearTerm);// residual Bdu->addTerm( u_current*tau->div() - u_current*(beta*v->grad())); Teuchos::RCP<RieszRep> riesz = Teuchos::rcp(new RieszRep(mesh, ip, residual)); Teuchos::RCP<RieszRep> duRiesz = Teuchos::rcp(new RieszRep(mesh, ip, Bdu)); riesz->computeRieszRep(); FunctionPtr e_v = Teuchos::rcp(new RepFunction(v,riesz)); e_v->writeValuesToMATLABFile(mesh, "e_v.m"); FunctionPtr posErrPart = Teuchos::rcp(new PositivePart(e_v->dx())); hessianBF->addTerm(e_v->dx()*u,du); // hessianBF->addTerm(posErrPart*u,du); Teuchos::RCP<HessianFilter> hessianFilter = Teuchos::rcp(new HessianFilter(hessianBF)); if (useHessian) { solution->setWriteMatrixToFile(true,"hessianStiffness.dat"); } else { solution->setWriteMatrixToFile(true,"stiffness.dat"); } Teuchos::RCP< LineSearchStep > LS_Step = Teuchos::rcp(new LineSearchStep(riesz)); ofstream out; out.open("Burgers.txt"); double NL_residual = 9e99; for (int i = 0; i<numSteps; i++) { solution->solve(false); // do one solve to initialize things... double stepLength = 1.0; stepLength = LS_Step->stepSize(backgroundFlow,solution, NL_residual); if (useHessian) { solution->setFilter(hessianFilter); } backgroundFlow->addSolution(solution,stepLength); NL_residual = LS_Step->getNLResidual(); if (rank==0) { cout << "NL residual after adding = " << NL_residual << " with step size " << stepLength << endl; out << NL_residual << endl; // saves initial NL error } } out.close(); //////////////////////////////////////////////////////////////////// // DEFINE REFINEMENT STRATEGY //////////////////////////////////////////////////////////////////// Teuchos::RCP<RefinementStrategy> refinementStrategy; refinementStrategy = Teuchos::rcp(new RefinementStrategy(solution,energyThreshold)); int numRefs = 0; Teuchos::RCP<NonlinearStepSize> stepSize = Teuchos::rcp(new NonlinearStepSize(nonlinearStepSize)); Teuchos::RCP<NonlinearSolveStrategy> solveStrategy; solveStrategy = Teuchos::rcp( new NonlinearSolveStrategy(backgroundFlow, solution, stepSize, nonlinearRelativeEnergyTolerance)); //////////////////////////////////////////////////////////////////// // SOLVE //////////////////////////////////////////////////////////////////// for (int refIndex=0; refIndex<numRefs; refIndex++) { solveStrategy->solve(rank==0); // print to console on rank 0 refinementStrategy->refine(rank==0); // print to console on rank 0 } // solveStrategy->solve(rank==0); if (rank==0) { backgroundFlow->writeToVTK("Burgers.vtu",min(H1Order+1,4)); solution->writeFluxesToFile(uhat->ID(), "burgers.dat"); cout << "wrote solution files" << endl; } return 0; }
bool FunctionTests::testBasisSumFunction() { bool success = true; // on a single-element mesh, the BasisSumFunction should be identical to // the Solution with those coefficients // define a new mesh: more interesting if we're not on the ref cell int spaceDim = 2; FieldContainer<double> quadPoints(4,2); quadPoints(0,0) = 0.0; // x1 quadPoints(0,1) = 0.0; // y1 quadPoints(1,0) = 2.0; quadPoints(1,1) = 0.0; quadPoints(2,0) = 1.0; quadPoints(2,1) = 1.0; quadPoints(3,0) = 0.0; quadPoints(3,1) = 1.0; int H1Order = 1, pToAdd = 0; int horizontalCells = 1, verticalCells = 1; // create a pointer to a new mesh: MeshPtr spectralConfusionMesh = MeshFactory::buildQuadMesh(quadPoints, horizontalCells, verticalCells, _confusionBF, H1Order, H1Order+pToAdd); BCPtr bc = BC::bc(); SolutionPtr soln = Teuchos::rcp( new Solution(spectralConfusionMesh, bc) ); soln->initializeLHSVector(); int cellID = 0; double tol = 1e-16; // overly restrictive, just for now. DofOrderingPtr trialSpace = spectralConfusionMesh->getElementType(cellID)->trialOrderPtr; set<int> trialIDs = trialSpace->getVarIDs(); BasisCachePtr volumeCache = BasisCache::basisCacheForCell(spectralConfusionMesh, cellID); for (set<int>::iterator trialIt=trialIDs.begin(); trialIt != trialIDs.end(); trialIt++) { int trialID = *trialIt; const vector<int>* sidesForVar = &trialSpace->getSidesForVarID(trialID); bool boundaryValued = sidesForVar->size() != 1; // note that for volume trialIDs, sideIndex = 0, and numSides = 1… for (vector<int>::const_iterator sideIt = sidesForVar->begin(); sideIt != sidesForVar->end(); sideIt++) { int sideIndex = *sideIt; BasisCachePtr sideCache = volumeCache->getSideBasisCache(sideIndex); BasisPtr basis = trialSpace->getBasis(trialID, sideIndex); int basisCardinality = basis->getCardinality(); for (int basisOrdinal = 0; basisOrdinal<basisCardinality; basisOrdinal++) { FieldContainer<double> basisCoefficients(basisCardinality); basisCoefficients(basisOrdinal) = 1.0; soln->setSolnCoeffsForCellID(basisCoefficients, cellID, trialID, sideIndex); VarPtr v = Var::varForTrialID(trialID, spectralConfusionMesh->bilinearForm()); FunctionPtr solnFxn = Function::solution(v, soln, false); FunctionPtr basisSumFxn = Teuchos::rcp( new BasisSumFunction(basis, basisCoefficients, Teuchos::rcp((BasisCache*)NULL), OP_VALUE, boundaryValued) ); if (!boundaryValued) { double l2diff = (solnFxn - basisSumFxn)->l2norm(spectralConfusionMesh); // cout << "l2diff = " << l2diff << endl; if (l2diff > tol) { success = false; cout << "testBasisSumFunction: l2diff of " << l2diff << " exceeds tol of " << tol << endl; cout << "l2norm of basisSumFxn: " << basisSumFxn->l2norm(spectralConfusionMesh) << endl; cout << "l2norm of solnFxn: " << solnFxn->l2norm(spectralConfusionMesh) << endl; } l2diff = (solnFxn->dx() - basisSumFxn->dx())->l2norm(spectralConfusionMesh); // cout << "l2diff = " << l2diff << endl; if (l2diff > tol) { success = false; cout << "testBasisSumFunction: l2diff of dx() " << l2diff << " exceeds tol of " << tol << endl; cout << "l2norm of basisSumFxn->dx(): " << basisSumFxn->dx()->l2norm(spectralConfusionMesh) << endl; cout << "l2norm of solnFxn->dx(): " << solnFxn->dx()->l2norm(spectralConfusionMesh) << endl; } // test that the restriction to a side works int numSides = volumeCache->cellTopology()->getSideCount(); for (int i=0; i<numSides; i++) { BasisCachePtr mySideCache = volumeCache->getSideBasisCache(i); if (! solnFxn->equals(basisSumFxn, mySideCache, tol)) { success = false; cout << "testBasisSumFunction: on side 0, l2diff of " << l2diff << " exceeds tol of " << tol << endl; reportFunctionValueDifferences(solnFxn, basisSumFxn, mySideCache, tol); } if (! solnFxn->grad(spaceDim)->equals(basisSumFxn->grad(spaceDim), mySideCache, tol)) { success = false; cout << "testBasisSumFunction: on side 0, l2diff of dx() " << l2diff << " exceeds tol of " << tol << endl; reportFunctionValueDifferences(solnFxn->grad(spaceDim), basisSumFxn->grad(spaceDim), mySideCache, tol); } } } else { FieldContainer<double> cellIntegral(1); // compute l2 diff of integral along the one side where we can legitimately assert equality: FunctionPtr diffFxn = solnFxn - basisSumFxn; (diffFxn*diffFxn)->integrate(cellIntegral, sideCache); double l2diff = sqrt(cellIntegral(0)); if (l2diff > tol) { success = false; cout << "testBasisSumFunction: on side " << sideIndex << ", l2diff of " << l2diff << " exceeds tol of " << tol << endl; int numCubPoints = sideCache->getPhysicalCubaturePoints().dimension(1); FieldContainer<double> solnFxnValues(1,numCubPoints); FieldContainer<double> basisFxnValues(1,numCubPoints); solnFxn->values(solnFxnValues, sideCache); basisSumFxn->values(basisFxnValues, sideCache); cout << "solnFxnValues:\n" << solnFxnValues; cout << "basisFxnValues:\n" << basisFxnValues; } else { // cout << "testBasisSumFunction: on side " << sideIndex << ", l2diff of " << l2diff << " is within tol of " << tol << endl; } } } } } return success; }
bool ScratchPadTests::testGalerkinOrthogonality() { double tol = 1e-11; bool success = true; //////////////////// DECLARE VARIABLES /////////////////////// // define test variables VarFactoryPtr varFactory = VarFactory::varFactory(); VarPtr v = varFactory->testVar("v", HGRAD); vector<double> beta; beta.push_back(1.0); beta.push_back(1.0); //////////////////// DEFINE INNER PRODUCT(S) /////////////////////// // robust test norm IPPtr ip = Teuchos::rcp(new IP); ip->addTerm(v); ip->addTerm(beta*v->grad()); // define trial variables VarPtr beta_n_u = varFactory->fluxVar("\\widehat{\\beta \\cdot n }"); VarPtr u = varFactory->fieldVar("u"); //////////////////// BUILD MESH /////////////////////// BFPtr convectionBF = Teuchos::rcp( new BF(varFactory) ); FunctionPtr n = Function::normal(); // v terms: convectionBF->addTerm( -u, beta * v->grad() ); convectionBF->addTerm( beta_n_u, v); // define nodes for mesh int order = 2; int H1Order = order+1; int pToAdd = 1; // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = MeshUtilities::buildUnitQuadMesh(4, convectionBF, H1Order, H1Order+pToAdd); //////////////////// SOLVE /////////////////////// RHSPtr rhs = RHS::rhs(); BCPtr bc = BC::bc(); SpatialFilterPtr inflowBoundary = Teuchos::rcp( new InflowSquareBoundary ); SpatialFilterPtr outflowBoundary = Teuchos::rcp( new NegatedSpatialFilter(inflowBoundary) ); FunctionPtr uIn; uIn = Teuchos::rcp(new Uinflow); // uses a discontinuous piecewise-constant basis function on left and bottom sides of square bc->addDirichlet(beta_n_u, inflowBoundary, beta*n*uIn); Teuchos::RCP<Solution> solution; solution = Teuchos::rcp( new Solution(mesh, bc, rhs, ip) ); solution->solve(false); FunctionPtr uFxn = Function::solution(u, solution); FunctionPtr fnhatFxn = Function::solution(beta_n_u,solution); // make residual for riesz representation function LinearTermPtr residual = Teuchos::rcp(new LinearTerm);// residual FunctionPtr parity = Function::sideParity(); residual->addTerm(-fnhatFxn*v + (beta*uFxn)*v->grad()); Teuchos::RCP<RieszRep> riesz = Teuchos::rcp(new RieszRep(mesh, ip, residual)); riesz->computeRieszRep(); map<int,FunctionPtr> err_rep_map; err_rep_map[v->ID()] = RieszRep::repFunction(v,riesz); //////////////////// GET BOUNDARY CONDITION DATA /////////////////////// FieldContainer<GlobalIndexType> bcGlobalIndices; FieldContainer<double> bcGlobalValues; mesh->boundary().bcsToImpose(bcGlobalIndices,bcGlobalValues,*(solution->bc()), NULL); set<int> bcInds; for (int i=0; i<bcGlobalIndices.dimension(0); i++) { bcInds.insert(bcGlobalIndices(i)); } //////////////////// CHECK GALERKIN ORTHOGONALITY /////////////////////// BCPtr nullBC; RHSPtr nullRHS; IPPtr nullIP; SolutionPtr solnPerturbation = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); map< int, vector<DofInfo> > infoMap = constructGlobalDofToLocalDofInfoMap(mesh); for (map< int, vector<DofInfo> >::iterator mapIt = infoMap.begin(); mapIt != infoMap.end(); mapIt++) { int dofIndex = mapIt->first; vector< DofInfo > dofInfoVector = mapIt->second; // all the local dofs that map to dofIndex // create perturbation in direction du solnPerturbation->clear(); // clear all solns // set each corresponding local dof to 1.0 for (vector< DofInfo >::iterator dofInfoIt = dofInfoVector.begin(); dofInfoIt != dofInfoVector.end(); dofInfoIt++) { DofInfo info = *dofInfoIt; FieldContainer<double> solnCoeffs(info.basisCardinality); solnCoeffs(info.basisOrdinal) = 1.0; solnPerturbation->setSolnCoeffsForCellID(solnCoeffs, info.cellID, info.trialID, info.sideIndex); } // solnPerturbation->setSolnCoeffForGlobalDofIndex(1.0,dofIndex); LinearTermPtr b_du = convectionBF->testFunctional(solnPerturbation); FunctionPtr gradient = b_du->evaluate(err_rep_map, TestingUtilities::isFluxOrTraceDof(mesh,dofIndex)); // use boundary part only if flux double grad = gradient->integrate(mesh,10); if (!TestingUtilities::isFluxOrTraceDof(mesh,dofIndex) && abs(grad)>tol) // if we're not single-precision zero FOR FIELDS { // int cellID = mesh->getGlobalToLocalMap()[dofIndex].first; cout << "Failed testGalerkinOrthogonality() for fields with diff " << abs(grad) << " at dof " << dofIndex << "; info:" << endl; cout << dofInfoString(infoMap[dofIndex]); success = false; } } FieldContainer<double> errorJumps(mesh->numGlobalDofs()); //initialized to zero // just test fluxes ON INTERNAL SKELETON here set<GlobalIndexType> activeCellIDs = mesh->getActiveCellIDsGlobal(); for (GlobalIndexType activeCellID : activeCellIDs) { ElementPtr elem = mesh->getElement(activeCellID); for (int sideIndex = 0; sideIndex < 4; sideIndex++) { ElementTypePtr elemType = elem->elementType(); vector<int> localDofIndices = elemType->trialOrderPtr->getDofIndices(beta_n_u->ID(), sideIndex); for (int i = 0; i<localDofIndices.size(); i++) { int globalDofIndex = mesh->globalDofIndex(elem->cellID(), localDofIndices[i]); vector< DofInfo > dofInfoVector = infoMap[globalDofIndex]; solnPerturbation->clear(); TestingUtilities::setSolnCoeffForGlobalDofIndex(solnPerturbation,1.0,globalDofIndex); // also add in BCs for (int i = 0; i<bcGlobalIndices.dimension(0); i++) { TestingUtilities::setSolnCoeffForGlobalDofIndex(solnPerturbation,bcGlobalValues(i),bcGlobalIndices(i)); } LinearTermPtr b_du = convectionBF->testFunctional(solnPerturbation); FunctionPtr gradient = b_du->evaluate(err_rep_map, TestingUtilities::isFluxOrTraceDof(mesh,globalDofIndex)); // use boundary part only if flux double jump = gradient->integrate(mesh,10); errorJumps(globalDofIndex) += jump; } } } for (int i = 0; i<mesh->numGlobalDofs(); i++) { if (abs(errorJumps(i))>tol) { cout << "Failing Galerkin orthogonality test for fluxes with diff " << errorJumps(i) << " at dof " << i << endl; cout << dofInfoString(infoMap[i]); success = false; } } return success; }
int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); int rank=mpiSession.getRank(); int numProcs=mpiSession.getNProc(); int spaceDim = 2; #ifdef HAVE_MPI choice::MpiArgs args( argc, argv ); #else choice::Args args(argc, argv ); #endif int minPolyOrder = args.Input<int>("--minPolyOrder", "L^2 (field) minimum polynomial order",0); int maxPolyOrder = args.Input<int>("--maxPolyOrder", "L^2 (field) maximum polynomial order",1); int minLogElements = args.Input<int>("--minLogElements", "base 2 log of the minimum number of elements in one mesh direction", 0); int maxLogElements = args.Input<int>("--maxLogElements", "base 2 log of the maximum number of elements in one mesh direction", 4); double Re = args.Input<double>("--Re", "Reynolds number", 40); // bool outputStiffnessMatrix = args.Input<bool>("--writeFinalStiffnessToDisk", "write the final stiffness matrix to disk.", false); bool computeMaxConditionNumber = args.Input<bool>("--computeMaxConditionNumber", "compute the maximum Gram matrix condition number for final mesh.", false); int maxIters = args.Input<int>("--maxIters", "maximum number of Newton-Raphson iterations to take to try to match tolerance", 50); double minL2Increment = args.Input<double>("--NRtol", "Newton-Raphson tolerance, L^2 norm of increment", 1e-12); string normChoice = args.Input<string>("--norm", "norm choice: graph, compliantGraph, stokesGraph, or stokesCompliantGraph", "graph"); bool useCondensedSolve = args.Input<bool>("--useCondensedSolve", "use static condensation", true); double dt = args.Input<double>("--timeStep", "time step (0 for none)", 0); double zmcRho = args.Input<double>("--zmcRho", "zero-mean constraint rho (stabilization parameter)", -1); // string replayFile = args.Input<string>("--replayFile", "file with refinement history to replay", ""); // string saveFile = args.Input<string>("--saveReplay", "file to which to save refinement history", ""); args.Process(); int pToAdd = 2; // for optimal test function approximation bool useLineSearch = false; bool computeRelativeErrors = true; // we'll say false when one of the exact solution components is 0 bool useEnrichedTraces = true; // enriched traces are the right choice, mathematically speaking BasisFactory::basisFactory()->setUseEnrichedTraces(useEnrichedTraces); // parse args: bool useTriangles = false, useGraphNorm = false, useCompliantNorm = false, useStokesCompliantNorm = false, useStokesGraphNorm = false; if (normChoice=="graph") { useGraphNorm = true; } else if (normChoice=="compliantGraph") { useCompliantNorm = true; } else if (normChoice=="stokesGraph") { useStokesGraphNorm = true; } else if (normChoice=="stokesCompliantGraph") { useStokesCompliantNorm = true; } else { if (rank==0) cout << "unknown norm choice. Exiting.\n"; exit(-1); } bool artificialTimeStepping = (dt > 0); if (rank == 0) { cout << "pToAdd = " << pToAdd << endl; cout << "useTriangles = " << (useTriangles ? "true" : "false") << "\n"; cout << "norm = " << normChoice << endl; } // define Kovasznay domain: FieldContainer<double> quadPointsKovasznay(4,2); // domain from Cockburn Kanschat for Stokes: quadPointsKovasznay(0,0) = -0.5; // x1 quadPointsKovasznay(0,1) = 0.0; // y1 quadPointsKovasznay(1,0) = 1.5; quadPointsKovasznay(1,1) = 0.0; quadPointsKovasznay(2,0) = 1.5; quadPointsKovasznay(2,1) = 2.0; quadPointsKovasznay(3,0) = -0.5; quadPointsKovasznay(3,1) = 2.0; // Domain from Evans Hughes for Navier-Stokes: // quadPointsKovasznay(0,0) = 0.0; // x1 // quadPointsKovasznay(0,1) = -0.5; // y1 // quadPointsKovasznay(1,0) = 1.0; // quadPointsKovasznay(1,1) = -0.5; // quadPointsKovasznay(2,0) = 1.0; // quadPointsKovasznay(2,1) = 0.5; // quadPointsKovasznay(3,0) = 0.0; // quadPointsKovasznay(3,1) = 0.5; // double Re = 10.0; // Cockburn Kanschat Stokes // double Re = 40.0; // Evans Hughes Navier-Stokes // double Re = 1000.0; string formulationTypeStr = "vgp"; FunctionPtr u1_exact, u2_exact, p_exact; int numCellsFineMesh = 20; // for computing a zero-mean pressure int H1OrderFineMesh = 5; // VGPNavierStokesProblem(double Re, Intrepid::FieldContainer<double> &quadPoints, int horizontalCells, // int verticalCells, int H1Order, int pToAdd, // TFunctionPtr<double> u1_0, TFunctionPtr<double> u2_0, TFunctionPtr<double> f1, TFunctionPtr<double> f2, // bool enrichVelocity = false, bool enhanceFluxes = false) FunctionPtr zero = Function::zero(); VGPNavierStokesProblem zeroProblem = VGPNavierStokesProblem(Re, quadPointsKovasznay, numCellsFineMesh, numCellsFineMesh, H1OrderFineMesh, pToAdd, zero, zero, zero, useCompliantNorm || useStokesCompliantNorm); VarFactoryPtr varFactory = VGPStokesFormulation::vgpVarFactory(); VarPtr u1_vgp = varFactory->fieldVar(VGP_U1_S); VarPtr u2_vgp = varFactory->fieldVar(VGP_U2_S); VarPtr sigma11_vgp = varFactory->fieldVar(VGP_SIGMA11_S); VarPtr sigma12_vgp = varFactory->fieldVar(VGP_SIGMA12_S); VarPtr sigma21_vgp = varFactory->fieldVar(VGP_SIGMA21_S); VarPtr sigma22_vgp = varFactory->fieldVar(VGP_SIGMA22_S); VarPtr p_vgp = varFactory->fieldVar(VGP_P_S); VarPtr v1_vgp = varFactory->testVar(VGP_V1_S, HGRAD); VarPtr v2_vgp = varFactory->testVar(VGP_V2_S, HGRAD); // if (rank==0) { // cout << "bilinear form with zero background flow:\n"; // zeroProblem.bf()->printTrialTestInteractions(); // } VGPStokesFormulation stokesForm(1/Re); NavierStokesFormulation::setKovasznay(Re, zeroProblem.mesh(), u1_exact, u2_exact, p_exact); // if (rank==0) cout << "Stokes bilinearForm: " << stokesForm.bf()->displayString() << endl; map< string, string > convergenceDataForMATLAB; // key: field file name for (int polyOrder = minPolyOrder; polyOrder <= maxPolyOrder; polyOrder++) { int H1Order = polyOrder + 1; int numCells1D = pow(2.0,minLogElements); if (rank==0) { cout << "L^2 order: " << polyOrder << endl; cout << "Re = " << Re << endl; } int kovasznayCubatureEnrichment = 20; // 20 is better than 10 for accurately measuring error on the coarser meshes. vector< VGPNavierStokesProblem > problems; do { VGPNavierStokesProblem problem = VGPNavierStokesProblem(Re,quadPointsKovasznay, numCells1D,numCells1D, H1Order, pToAdd, u1_exact, u2_exact, p_exact, useCompliantNorm || useStokesCompliantNorm); problem.backgroundFlow()->setCubatureEnrichmentDegree(kovasznayCubatureEnrichment); problem.solutionIncrement()->setCubatureEnrichmentDegree(kovasznayCubatureEnrichment); problem.backgroundFlow()->setZeroMeanConstraintRho(zmcRho); problem.solutionIncrement()->setZeroMeanConstraintRho(zmcRho); FunctionPtr dt_inv; if (artificialTimeStepping) { // // LHS gets u_inc / dt: BFPtr bf = problem.bf(); dt_inv = ParameterFunction::parameterFunction(1.0 / dt); //Teuchos::rcp( new ConstantScalarFunction(1.0 / dt, "\\frac{1}{dt}") ); bf->addTerm(-dt_inv * u1_vgp, v1_vgp); bf->addTerm(-dt_inv * u2_vgp, v2_vgp); problem.setIP( bf->graphNorm() ); // graph norm has changed... } else { dt_inv = Function::zero(); } problems.push_back(problem); if ( useCompliantNorm ) { problem.setIP(problem.vgpNavierStokesFormulation()->scaleCompliantGraphNorm(dt_inv)); } else if (useStokesCompliantNorm) { VGPStokesFormulation stokesForm(1.0); // pretend Re = 1 in the graph norm problem.setIP(stokesForm.scaleCompliantGraphNorm()); } else if (useStokesGraphNorm) { VGPStokesFormulation stokesForm(1.0); // pretend Re = 1 in the graph norm problem.setIP(stokesForm.graphNorm()); } else if (! useGraphNorm ) { // then use the naive: problem.setIP(problem.bf()->naiveNorm(spaceDim)); } if (rank==0) { cout << numCells1D << " x " << numCells1D << ": " << problem.mesh()->numGlobalDofs() << " dofs " << endl; } numCells1D *= 2; } while (pow(2.0,maxLogElements) >= numCells1D); // note that rhs and bilinearForm aren't really going to be right here, since they // involve a background flow which varies over the various problems... HConvergenceStudy study(problems[0].exactSolution(), problems[0].mesh()->bilinearForm(), problems[0].exactSolution()->rhs(), problems[0].backgroundFlow()->bc(), problems[0].bf()->graphNorm(), minLogElements, maxLogElements, H1Order, pToAdd, false, useTriangles, false); study.setReportRelativeErrors(computeRelativeErrors); study.setCubatureDegreeForExact(kovasznayCubatureEnrichment); vector< SolutionPtr > solutions; numCells1D = pow(2.0,minLogElements); for (vector< VGPNavierStokesProblem >::iterator problem = problems.begin(); problem != problems.end(); problem++) { SolutionPtr solnIncrement = problem->solutionIncrement(); FunctionPtr u1_incr = Function::solution(u1_vgp, solnIncrement); FunctionPtr u2_incr = Function::solution(u2_vgp, solnIncrement); FunctionPtr sigma11_incr = Function::solution(sigma11_vgp, solnIncrement); FunctionPtr sigma12_incr = Function::solution(sigma12_vgp, solnIncrement); FunctionPtr sigma21_incr = Function::solution(sigma21_vgp, solnIncrement); FunctionPtr sigma22_incr = Function::solution(sigma22_vgp, solnIncrement); FunctionPtr p_incr = Function::solution(p_vgp, solnIncrement); // LinearTermPtr rhsLT = problem->backgroundFlow()->rhs()->linearTerm(); // if (rank==0) cout << "bilinearForm: " << problems[0].mesh()->bilinearForm()->displayString() << endl; // if (rank==0) cout << "RHS: " << rhsLT->displayString() << endl; FunctionPtr l2_incr = u1_incr * u1_incr + u2_incr * u2_incr + p_incr * p_incr + sigma11_incr * sigma11_incr + sigma12_incr * sigma12_incr + sigma21_incr * sigma21_incr + sigma22_incr * sigma22_incr; double weight = 1.0; do { weight = problem->iterate(useLineSearch, useCondensedSolve); LinearTermPtr rhsLT = problem->backgroundFlow()->rhs()->linearTerm(); RieszRep rieszRep(problem->backgroundFlow()->mesh(), problem->backgroundFlow()->ip(), rhsLT); rieszRep.computeRieszRep(); double costFunction = rieszRep.getNorm(); double incr_norm = sqrt(l2_incr->integrate(problem->mesh())); if (rank==0) { cout << setprecision(6) << scientific; cout << "\x1B[2K"; // Erase the entire current line. cout << "\x1B[0E"; // Move to the beginning of the current line. cout << "Iteration: " << problem->iterationCount() << "; L^2(incr) = " << incr_norm; flush(cout); // cout << setprecision(6) << scientific; // cout << "Took " << weight << "-weighted step for " << numCells1D; // cout << " x " << numCells1D << " mesh: " << problem->iterationCount(); // cout << setprecision(6) << fixed; // cout << " iterations; cost function " << costFunction << endl; } } while ((sqrt(l2_incr->integrate(problem->mesh())) > minL2Increment ) && (problem->iterationCount() < maxIters) && (weight != 0)); if (rank==0) cout << endl; solutions.push_back( problem->backgroundFlow() ); // set the IP to the naive norm for clearer comparison with the best approximation energy error // problem->backgroundFlow()->setIP(problem->bf()->naiveNorm()); // double energyError = problem->backgroundFlow()->energyErrorTotal(); // if (rank==0) { // cout << setprecision(6) << fixed; // cout << numCells1D << " x " << numCells1D << ": " << problem->iterationCount(); // cout << " iterations; actual energy error " << energyError << endl; // } numCells1D *= 2; } study.setSolutions(solutions); for (int i=0; i<=maxLogElements-minLogElements; i++) { SolutionPtr bestApproximation = study.bestApproximations()[i]; VGPNavierStokesFormulation nsFormBest = VGPNavierStokesFormulation(Re, bestApproximation); SpatialFilterPtr entireBoundary = Teuchos::rcp( new SpatialFilterUnfiltered ); // SpatialFilterUnfiltered returns true everywhere Teuchos::RCP<ExactSolution<double>> exact = nsFormBest.exactSolution(u1_exact, u2_exact, p_exact, entireBoundary); // bestApproximation->setIP( nsFormBest.bf()->naiveNorm() ); // bestApproximation->setRHS( exact->rhs() ); // use backgroundFlow's IP so that they're comparable IPPtr ip = problems[i].backgroundFlow()->ip(); LinearTermPtr rhsLT = exact->rhs()->linearTerm(); RieszRep rieszRep(bestApproximation->mesh(), ip, rhsLT); rieszRep.computeRieszRep(); double bestCostFunction = rieszRep.getNorm(); if (rank==0) cout << "best energy error (measured according to the actual solution's test space IP): " << bestCostFunction << endl; } map< int, double > energyNormWeights; if (useCompliantNorm) { energyNormWeights[u1_vgp->ID()] = 1.0; // should be 1/h energyNormWeights[u2_vgp->ID()] = 1.0; // should be 1/h energyNormWeights[sigma11_vgp->ID()] = Re; // 1/mu energyNormWeights[sigma12_vgp->ID()] = Re; // 1/mu energyNormWeights[sigma21_vgp->ID()] = Re; // 1/mu energyNormWeights[sigma22_vgp->ID()] = Re; // 1/mu if (Re < 1) // assuming we're using the experimental small Re thing { energyNormWeights[p_vgp->ID()] = Re; } else { energyNormWeights[p_vgp->ID()] = 1.0; } } else { energyNormWeights[u1_vgp->ID()] = 1.0; energyNormWeights[u2_vgp->ID()] = 1.0; energyNormWeights[sigma11_vgp->ID()] = 1.0; energyNormWeights[sigma12_vgp->ID()] = 1.0; energyNormWeights[sigma21_vgp->ID()] = 1.0; energyNormWeights[sigma22_vgp->ID()] = 1.0; energyNormWeights[p_vgp->ID()] = 1.0; } vector<double> bestEnergy = study.weightedL2Error(energyNormWeights,true); vector<double> solnEnergy = study.weightedL2Error(energyNormWeights,false); map<int, double> velocityWeights; velocityWeights[u1_vgp->ID()] = 1.0; velocityWeights[u2_vgp->ID()] = 1.0; vector<double> bestVelocityError = study.weightedL2Error(velocityWeights,true); vector<double> solnVelocityError = study.weightedL2Error(velocityWeights,false); map<int, double> pressureWeight; pressureWeight[p_vgp->ID()] = 1.0; vector<double> bestPressureError = study.weightedL2Error(pressureWeight,true); vector<double> solnPressureError = study.weightedL2Error(pressureWeight,false); if (rank==0) { cout << setw(25); cout << "Solution Energy Error:" << setw(25) << "Best Energy Error:" << endl; cout << scientific << setprecision(1); for (int i=0; i<bestEnergy.size(); i++) { cout << setw(25) << solnEnergy[i] << setw(25) << bestEnergy[i] << endl; } cout << setw(25); cout << "Solution Velocity Error:" << setw(25) << "Best Velocity Error:" << endl; cout << scientific << setprecision(1); for (int i=0; i<bestEnergy.size(); i++) { cout << setw(25) << solnVelocityError[i] << setw(25) << bestVelocityError[i] << endl; } cout << setw(25); cout << "Solution Pressure Error:" << setw(25) << "Best Pressure Error:" << endl; cout << scientific << setprecision(1); for (int i=0; i<bestEnergy.size(); i++) { cout << setw(25) << solnPressureError[i] << setw(25) << bestPressureError[i] << endl; } vector< string > tableHeaders; vector< vector<double> > dataTable; vector< double > meshWidths; for (int i=minLogElements; i<=maxLogElements; i++) { double width = pow(2.0,i); meshWidths.push_back(width); } tableHeaders.push_back("mesh_width"); dataTable.push_back(meshWidths); tableHeaders.push_back("soln_energy_error"); dataTable.push_back(solnEnergy); tableHeaders.push_back("best_energy_error"); dataTable.push_back(bestEnergy); tableHeaders.push_back("soln_velocity_error"); dataTable.push_back(solnVelocityError); tableHeaders.push_back("best_velocity_error"); dataTable.push_back(bestVelocityError); tableHeaders.push_back("soln_pressure_error"); dataTable.push_back(solnPressureError); tableHeaders.push_back("best_pressure_error"); dataTable.push_back(bestPressureError); ostringstream fileNameStream; fileNameStream << "nsStudy_Re" << Re << "k" << polyOrder << "_results.dat"; DataIO::outputTableToFile(tableHeaders,dataTable,fileNameStream.str()); } if (rank == 0) { cout << study.TeXErrorRateTable(); vector<int> primaryVariables; stokesForm.primaryTrialIDs(primaryVariables); vector<int> fieldIDs,traceIDs; vector<string> fieldFileNames; stokesForm.trialIDs(fieldIDs,traceIDs,fieldFileNames); cout << "******** Best Approximation comparison: ********\n"; cout << study.TeXBestApproximationComparisonTable(primaryVariables); ostringstream filePathPrefix; filePathPrefix << "navierStokes/" << formulationTypeStr << "_p" << polyOrder << "_velpressure"; study.TeXBestApproximationComparisonTable(primaryVariables,filePathPrefix.str()); filePathPrefix.str(""); filePathPrefix << "navierStokes/" << formulationTypeStr << "_p" << polyOrder << "_all"; study.TeXBestApproximationComparisonTable(fieldIDs); for (int i=0; i<fieldIDs.size(); i++) { int fieldID = fieldIDs[i]; int traceID = traceIDs[i]; string fieldName = fieldFileNames[i]; ostringstream filePathPrefix; filePathPrefix << "navierStokes/" << fieldName << "_p" << polyOrder; bool writeMATLABplotData = false; study.writeToFiles(filePathPrefix.str(),fieldID,traceID, writeMATLABplotData); } for (int i=0; i<primaryVariables.size(); i++) { string convData = study.convergenceDataMATLAB(primaryVariables[i], minPolyOrder); cout << convData; convergenceDataForMATLAB[fieldFileNames[i]] += convData; } filePathPrefix.str(""); filePathPrefix << "navierStokes/" << formulationTypeStr << "_p" << polyOrder << "_numDofs"; cout << study.TeXNumGlobalDofsTable(); } if (computeMaxConditionNumber) { for (int i=minLogElements; i<=maxLogElements; i++) { SolutionPtr soln = study.getSolution(i); ostringstream fileNameStream; fileNameStream << "nsStudy_maxConditionIPMatrix_" << i << ".dat"; IPPtr ip = Teuchos::rcp( dynamic_cast< IP* >(soln->ip().get()), false ); bool jacobiScalingTrue = true; double maxConditionNumber = MeshUtilities::computeMaxLocalConditionNumber(ip, soln->mesh(), jacobiScalingTrue, fileNameStream.str()); if (rank==0) { cout << "max Gram matrix condition number estimate for logElements " << i << ": " << maxConditionNumber << endl; cout << "putative worst-conditioned Gram matrix written to: " << fileNameStream.str() << "." << endl; } } } } if (rank==0) { ostringstream filePathPrefix; filePathPrefix << "navierStokes/" << formulationTypeStr << "_"; for (map<string,string>::iterator convIt = convergenceDataForMATLAB.begin(); convIt != convergenceDataForMATLAB.end(); convIt++) { string fileName = convIt->first + ".m"; string data = convIt->second; fileName = filePathPrefix.str() + fileName; ofstream fout(fileName.c_str()); fout << data; fout.close(); } } }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); choice::MpiArgs args( argc, argv ); #else choice::Args args( argc, argv ); #endif int rank = Teuchos::GlobalMPISession::getRank(); int numProcs = Teuchos::GlobalMPISession::getNProc(); int nCells = args.Input<int>("--nCells", "num cells",2); int numRefs = args.Input<int>("--numRefs","num adaptive refinements",0); int numPreRefs = args.Input<int>("--numPreRefs","num preemptive adaptive refinements",0); int order = args.Input<int>("--order","order of approximation",2); double eps = args.Input<double>("--epsilon","diffusion parameter",1e-2); double energyThreshold = args.Input<double>("-energyThreshold","energy thresh for adaptivity", .5); double rampHeight = args.Input<double>("--rampHeight","ramp height at x = 2", 0.0); double ipSwitch = args.Input<double>("--ipSwitch","point at which to switch to graph norm", 0.0); // default to 0 to remain on robust norm bool useAnisotropy = args.Input<bool>("--useAnisotropy","aniso flag ", false); int H1Order = order+1; int pToAdd = args.Input<int>("--pToAdd","test space enrichment", 2); FunctionPtr zero = Function::constant(0.0); FunctionPtr one = Function::constant(1.0); FunctionPtr n = Teuchos::rcp( new UnitNormalFunction ); vector<double> e1,e2; e1.push_back(1.0);e1.push_back(0.0); e2.push_back(0.0);e2.push_back(1.0); //////////////////// DECLARE VARIABLES /////////////////////// // define test variables VarFactory varFactory; VarPtr tau = varFactory.testVar("\\tau", HDIV); VarPtr v = varFactory.testVar("v", HGRAD); // define trial variables VarPtr uhat = varFactory.traceVar("\\widehat{u}"); VarPtr beta_n_u_minus_sigma_n = varFactory.fluxVar("\\widehat{\\beta \\cdot n u - \\sigma_{n}}"); VarPtr u = varFactory.fieldVar("u"); VarPtr sigma1 = varFactory.fieldVar("\\sigma_1"); VarPtr sigma2 = varFactory.fieldVar("\\sigma_2"); vector<double> beta; beta.push_back(1.0); beta.push_back(0.0); //////////////////// DEFINE BILINEAR FORM /////////////////////// BFPtr confusionBF = Teuchos::rcp( new BF(varFactory) ); // tau terms: confusionBF->addTerm(sigma1 / eps, tau->x()); confusionBF->addTerm(sigma2 / eps, tau->y()); confusionBF->addTerm(u, tau->div()); confusionBF->addTerm(uhat, -tau->dot_normal()); // v terms: confusionBF->addTerm( sigma1, v->dx() ); confusionBF->addTerm( sigma2, v->dy() ); confusionBF->addTerm( -u, beta * v->grad() ); confusionBF->addTerm( beta_n_u_minus_sigma_n, v); // first order term with magnitude alpha double alpha = 0.0; // confusionBF->addTerm(alpha * u, v); //////////////////// BUILD MESH /////////////////////// // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = MeshUtilities::buildUnitQuadMesh(nCells,confusionBF, H1Order, H1Order+pToAdd); mesh->setPartitionPolicy(Teuchos::rcp(new ZoltanMeshPartitionPolicy("HSFC"))); MeshInfo meshInfo(mesh); // gets info like cell measure, etc //////////////////// DEFINE INNER PRODUCT(S) /////////////////////// IPPtr ip = Teuchos::rcp(new IP); /* // robust test norm FunctionPtr C_h = Teuchos::rcp( new EpsilonScaling(eps) ); FunctionPtr invH = Teuchos::rcp(new InvHScaling); FunctionPtr invSqrtH = Teuchos::rcp(new InvSqrtHScaling); FunctionPtr sqrtH = Teuchos::rcp(new SqrtHScaling); FunctionPtr hSwitch = Teuchos::rcp(new HSwitch(ipSwitch,mesh)); ip->addTerm(hSwitch*sqrt(eps) * v->grad() ); ip->addTerm(hSwitch*beta * v->grad() ); ip->addTerm(hSwitch*tau->div() ); // graph norm ip->addTerm( (one-hSwitch)*((1.0/eps) * tau + v->grad())); ip->addTerm( (one-hSwitch)*(beta * v->grad() - tau->div())); // regularizing terms ip->addTerm(C_h/sqrt(eps) * tau ); ip->addTerm(invSqrtH*v); */ // robust test norm IPPtr robIP = Teuchos::rcp(new IP); FunctionPtr C_h = Teuchos::rcp( new EpsilonScaling(eps) ); FunctionPtr invH = Teuchos::rcp(new InvHScaling); FunctionPtr invSqrtH = Teuchos::rcp(new InvSqrtHScaling); FunctionPtr sqrtH = Teuchos::rcp(new SqrtHScaling); FunctionPtr hSwitch = Teuchos::rcp(new HSwitch(ipSwitch,mesh)); robIP->addTerm(sqrt(eps) * v->grad() ); robIP->addTerm(beta * v->grad() ); robIP->addTerm(tau->div() ); // regularizing terms robIP->addTerm(C_h/sqrt(eps) * tau ); robIP->addTerm(invSqrtH*v); IPPtr graphIP = confusionBF->graphNorm(); graphIP->addTerm(invSqrtH*v); // graphIP->addTerm(C_h/sqrt(eps) * tau ); IPPtr switchIP = Teuchos::rcp(new IPSwitcher(robIP,graphIP,ipSwitch)); // rob IP for h>ipSwitch mesh size, graph norm o/w ip = switchIP; LinearTermPtr vVecLT = Teuchos::rcp(new LinearTerm); LinearTermPtr tauVecLT = Teuchos::rcp(new LinearTerm); vVecLT->addTerm(sqrt(eps)*v->grad()); tauVecLT->addTerm(C_h/sqrt(eps)*tau); LinearTermPtr restLT = Teuchos::rcp(new LinearTerm); restLT->addTerm(alpha*v); restLT->addTerm(invSqrtH*v); restLT = restLT + beta * v->grad(); restLT = restLT + tau->div(); //////////////////// SPECIFY RHS /////////////////////// Teuchos::RCP<RHSEasy> rhs = Teuchos::rcp( new RHSEasy ); FunctionPtr f = zero; // f = one; rhs->addTerm( f * v ); // obviously, with f = 0 adding this term is not necessary! //////////////////// CREATE BCs /////////////////////// Teuchos::RCP<BCEasy> bc = Teuchos::rcp( new BCEasy ); SpatialFilterPtr Inflow = Teuchos::rcp(new LeftInflow); SpatialFilterPtr wallBoundary = Teuchos::rcp(new WallBoundary);//MeshUtilities::rampBoundary(rampHeight); SpatialFilterPtr freeStream = Teuchos::rcp(new FreeStreamBoundary); bc->addDirichlet(uhat, wallBoundary, one); // bc->addDirichlet(uhat, wallBoundary, Teuchos::rcp(new WallSmoothBC(eps))); bc->addDirichlet(beta_n_u_minus_sigma_n, Inflow, zero); bc->addDirichlet(beta_n_u_minus_sigma_n, freeStream, zero); //////////////////// SOLVE & REFINE /////////////////////// Teuchos::RCP<Solution> solution; solution = Teuchos::rcp( new Solution(mesh, bc, rhs, ip) ); BCPtr nullBC = Teuchos::rcp((BC*)NULL); RHSPtr nullRHS = Teuchos::rcp((RHS*)NULL); IPPtr nullIP = Teuchos::rcp((IP*)NULL); SolutionPtr backgroundFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); mesh->registerSolution(backgroundFlow); // to trigger issue with p-refinements map<int, Teuchos::RCP<Function> > functionMap; functionMap[u->ID()] = Function::constant(3.14); backgroundFlow->projectOntoMesh(functionMap); // lower p to p = 1 at SINGULARITY only vector<int> ids; /* for (int i = 0;i<mesh->numActiveElements();i++){ bool cellIDset = false; int cellID = mesh->activeElements()[i]->cellID(); int elemOrder = mesh->cellPolyOrder(cellID)-1; FieldContainer<double> vv(4,2); mesh->verticesForCell(vv, cellID); bool vertexOnWall = false; bool vertexAtSingularity = false; for (int j = 0;j<4;j++){ if ((abs(vv(j,0)-.5) + abs(vv(j,1)))<1e-10){ vertexAtSingularity = true; cellIDset = true; } } if (!vertexAtSingularity && elemOrder<2 && !cellIDset ){ ids.push_back(cellID); cout << "celliD = " << cellID << endl; } } */ ids.push_back(1); ids.push_back(3); mesh->pRefine(ids); // to put order = 1 return 0; LinearTermPtr residual = rhs->linearTermCopy(); residual->addTerm(-confusionBF->testFunctional(solution)); RieszRepPtr rieszResidual = Teuchos::rcp(new RieszRep(mesh, ip, residual)); rieszResidual->computeRieszRep(); FunctionPtr e_v = Teuchos::rcp(new RepFunction(v,rieszResidual)); FunctionPtr e_tau = Teuchos::rcp(new RepFunction(tau,rieszResidual)); map<int,FunctionPtr> errRepMap; errRepMap[v->ID()] = e_v; errRepMap[tau->ID()] = e_tau; FunctionPtr errTau = tauVecLT->evaluate(errRepMap,false); FunctionPtr errV = vVecLT->evaluate(errRepMap,false); FunctionPtr errRest = restLT->evaluate(errRepMap,false); FunctionPtr xErr = (errTau->x())*(errTau->x()) + (errV->dx())*(errV->dx()); FunctionPtr yErr = (errTau->y())*(errTau->y()) + (errV->dy())*(errV->dy()); FunctionPtr restErr = errRest*errRest; RefinementStrategy refinementStrategy( solution, energyThreshold ); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // PRE REFINEMENTS //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (rank==0){ cout << "Number of pre-refinements = " << numPreRefs << endl; } for (int i =0;i<=numPreRefs;i++){ vector<ElementPtr> elems = mesh->activeElements(); vector<ElementPtr>::iterator elemIt; vector<int> wallCells; for (elemIt=elems.begin();elemIt != elems.end();elemIt++){ int cellID = (*elemIt)->cellID(); int numSides = mesh->getElement(cellID)->numSides(); FieldContainer<double> vertices(numSides,2); //for quads mesh->verticesForCell(vertices, cellID); bool cellIDset = false; for (int j = 0;j<numSides;j++){ if ((abs(vertices(j,0)-.5)<1e-7) && (abs(vertices(j,1))<1e-7) && !cellIDset){ // if at singularity, i.e. if a vertex is (1,0) wallCells.push_back(cellID); cellIDset = true; } } } if (i<numPreRefs){ refinementStrategy.refineCells(wallCells); } } double minSideLength = meshInfo.getMinCellSideLength() ; double minCellMeasure = meshInfo.getMinCellMeasure() ; if (rank==0){ cout << "after prerefs, sqrt min cell measure = " << sqrt(minCellMeasure) << ", min side length = " << minSideLength << endl; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// VTKExporter exporter(solution, mesh, varFactory); for (int refIndex=0;refIndex<numRefs;refIndex++){ if (rank==0){ cout << "on ref index " << refIndex << endl; } rieszResidual->computeRieszRep(); // in preparation to get anisotropy vector<int> cellIDs; refinementStrategy.getCellsAboveErrorThreshhold(cellIDs); map<int,double> energyError = solution->energyError(); map<int,double> xErrMap = xErr->cellIntegrals(cellIDs,mesh,5,true); map<int,double> yErrMap = yErr->cellIntegrals(cellIDs,mesh,5,true); map<int,double> restErrMap = restErr->cellIntegrals(cellIDs,mesh,5,true); for (vector<ElementPtr>::iterator elemIt = mesh->activeElements().begin();elemIt!=mesh->activeElements().end();elemIt++){ int cellID = (*elemIt)->cellID(); double err = xErrMap[cellID]+ yErrMap[cellID] + restErrMap[cellID]; // if (rank==0) // cout << "err thru LT = " << sqrt(err) << ", while energy err = " << energyError[cellID] << endl; } /* map<int,double> ratio,xErr,yErr; vector<ElementPtr> elems = mesh->activeElements(); for (vector<ElementPtr>::iterator elemIt = elems.begin();elemIt!=elems.end();elemIt++){ int cellID = (*elemIt)->cellID(); ratio[cellID] = 0.0; xErr[cellID] = 0.0; yErr[cellID] = 0.0; if (std::find(cellIDs.begin(),cellIDs.end(),cellID)!=cellIDs.end()){ // if this cell is above energy thresh ratio[cellID] = yErrMap[cellID]/xErrMap[cellID]; xErr[cellID] = xErrMap[cellID]; yErr[cellID] = yErrMap[cellID]; } } FunctionPtr ratioFxn = Teuchos::rcp(new EnergyErrorFunction(ratio)); FunctionPtr xErrFxn = Teuchos::rcp(new EnergyErrorFunction(xErr)); FunctionPtr yErrFxn = Teuchos::rcp(new EnergyErrorFunction(yErr)); exporter.exportFunction(ratioFxn, string("ratio")+oss.str()); exporter.exportFunction(xErrFxn, string("xErr")+oss.str()); exporter.exportFunction(yErrFxn, string("yErr")+oss.str()); */ if (useAnisotropy){ refinementStrategy.refine(rank==0,xErrMap,yErrMap); //anisotropic refinements }else{ refinementStrategy.refine(rank==0); // no anisotropy } // lower p to p = 1 at SINGULARITY only vector<int> ids; for (int i = 0;i<mesh->numActiveElements();i++){ int cellID = mesh->activeElements()[i]->cellID(); int elemOrder = mesh->cellPolyOrder(cellID)-1; FieldContainer<double> vv(4,2); mesh->verticesForCell(vv, cellID); bool vertexOnWall = false; bool vertexAtSingularity = false; for (int j = 0;j<4;j++){ if ((abs(vv(j,0)-.5) + abs(vv(j,1)))<1e-10) vertexAtSingularity = true; } if (!vertexAtSingularity && elemOrder<2){ ids.push_back(cellID); } } mesh->pRefine(ids); // to put order = 1 /* if (elemOrder>1){ if (vertexAtSingularity){ vector<int> ids; ids.push_back(cellID); mesh->pRefine(ids,1-(elemOrder-1)); // to put order = 1 // mesh->pRefine(ids); // to put order = 1 if (rank==0) cout << "p unrefining elem with elemOrder = " << elemOrder << endl; } }else{ if (!vertexAtSingularity){ vector<int> ids; ids.push_back(cellID); mesh->pRefine(ids,2-elemOrder); } } */ double minSideLength = meshInfo.getMinCellSideLength() ; if (rank==0) cout << "minSideLength is " << minSideLength << endl; solution->condensedSolve(); std::ostringstream oss; oss << refIndex; } // final solve on final mesh solution->setWriteMatrixToFile(true,"K.mat"); solution->condensedSolve(); //////////////////////////////////////////////////////////////////////////////////////////////////////////// // CHECK CONDITIONING //////////////////////////////////////////////////////////////////////////////////////////////////////////// bool checkConditioning = true; if (checkConditioning){ double minSideLength = meshInfo.getMinCellSideLength() ; StandardAssembler assembler(solution); double maxCond = 0.0; int maxCellID = 0; for (int i = 0;i<mesh->numActiveElements();i++){ int cellID = mesh->getActiveElement(i)->cellID(); FieldContainer<double> ipMat = assembler.getIPMatrix(mesh->getElement(cellID)); double cond = SerialDenseWrapper::getMatrixConditionNumber(ipMat); if (cond>maxCond){ maxCond = cond; maxCellID = cellID; } } if (rank==0){ cout << "cell ID " << maxCellID << " has minCellLength " << minSideLength << " and condition estimate " << maxCond << endl; } string ipMatName = string("ipMat.mat"); ElementPtr maxCondElem = mesh->getElement(maxCellID); FieldContainer<double> ipMat = assembler.getIPMatrix(maxCondElem); SerialDenseWrapper::writeMatrixToMatlabFile(ipMatName,ipMat); } //////////////////// print to file /////////////////////// if (rank==0){ exporter.exportSolution(string("robustIP")); cout << endl; } return 0; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); #endif int commRank = Teuchos::GlobalMPISession::getRank(); int numProcs = Teuchos::GlobalMPISession::getNProc(); // { // // 1D tests // CellTopoPtrLegacy line_2 = Teuchos::rcp( new shards::CellTopology(shards::getCellTopologyData<shards::Line<2> >() ) ); // // let's draw a line // vector<double> v0 = makeVertex(0); // vector<double> v1 = makeVertex(1); // vector<double> v2 = makeVertex(2); // vector< vector<double> > vertices; // vertices.push_back(v0); // vertices.push_back(v1); // vertices.push_back(v2); // vector<unsigned> line1VertexList; // vector<unsigned> line2VertexList; // line1VertexList.push_back(0); // line1VertexList.push_back(1); // line2VertexList.push_back(1); // line2VertexList.push_back(2); // vector< vector<unsigned> > elementVertices; // elementVertices.push_back(line1VertexList); // elementVertices.push_back(line2VertexList); // vector< CellTopoPtrLegacy > cellTopos; // cellTopos.push_back(line_2); // cellTopos.push_back(line_2); // MeshGeometryPtr meshGeometry = Teuchos::rcp( new MeshGeometry(vertices, elementVertices, cellTopos) ); // MeshTopologyPtr meshTopology = Teuchos::rcp( new MeshTopology(meshGeometry) ); // FunctionPtr x = Function::xn(1); // FunctionPtr function = x; // FunctionPtr fbdr = Function::restrictToCellBoundary(function); // vector<FunctionPtr> functions; // functions.push_back(function); // functions.push_back(function); // vector<string> functionNames; // functionNames.push_back("function1"); // functionNames.push_back("function2"); // // { // // HDF5Exporter exporter(mesh, "function1", false); // // exporter.exportFunction(function, "function1"); // // } // // { // // HDF5Exporter exporter(mesh, "boundary1", false); // // exporter.exportFunction(fbdr, "boundary1"); // // } // // { // // HDF5Exporter exporter(mesh, "functions1", false); // // exporter.exportFunction(functions, functionNames); // // } // } { // 2D tests // CellTopoPtrLegacy quad_4 = Teuchos::rcp( new shards::CellTopology(shards::getCellTopologyData<shards::Quadrilateral<4> >() ) ); // CellTopoPtrLegacy tri_3 = Teuchos::rcp( new shards::CellTopology(shards::getCellTopologyData<shards::Triangle<3> >() ) ); CellTopoPtr quad_4 = CellTopology::quad(); CellTopoPtr tri_3 = CellTopology::triangle(); // let's draw a little house vector<double> v0 = makeVertex(-1,0); vector<double> v1 = makeVertex(1,0); vector<double> v2 = makeVertex(1,2); vector<double> v3 = makeVertex(-1,2); vector<double> v4 = makeVertex(0.0,3); vector< vector<double> > vertices; vertices.push_back(v0); vertices.push_back(v1); vertices.push_back(v2); vertices.push_back(v3); vertices.push_back(v4); vector<unsigned> quadVertexList; quadVertexList.push_back(0); quadVertexList.push_back(1); quadVertexList.push_back(2); quadVertexList.push_back(3); vector<unsigned> triVertexList; triVertexList.push_back(3); triVertexList.push_back(2); triVertexList.push_back(4); vector< vector<unsigned> > elementVertices; elementVertices.push_back(quadVertexList); elementVertices.push_back(triVertexList); // vector< CellTopoPtrLegacy > cellTopos; vector< CellTopoPtr> cellTopos; cellTopos.push_back(quad_4); cellTopos.push_back(tri_3); MeshGeometryPtr meshGeometry = Teuchos::rcp( new MeshGeometry(vertices, elementVertices, cellTopos) ); MeshTopologyPtr meshTopology = Teuchos::rcp( new MeshTopology(meshGeometry) ); //////////////////// DECLARE VARIABLES /////////////////////// // define test variables VarFactoryPtr vf = VarFactory::varFactory(); VarPtr tau = vf->testVar("tau", HDIV); VarPtr v = vf->testVar("v", HGRAD); // define trial variables VarPtr uhat = vf->traceVar("uhat"); VarPtr fhat = vf->fluxVar("fhat"); VarPtr u = vf->fieldVar("u"); VarPtr sigma = vf->fieldVar("sigma", VECTOR_L2); //////////////////// DEFINE BILINEAR FORM /////////////////////// BFPtr bf = Teuchos::rcp( new BF(vf) ); // tau terms: bf->addTerm(sigma, tau); bf->addTerm(u, tau->div()); bf->addTerm(-uhat, tau->dot_normal()); // v terms: bf->addTerm( sigma, v->grad() ); bf->addTerm( fhat, v); //////////////////// DEFINE INNER PRODUCT(S) /////////////////////// IPPtr ip = bf->graphNorm(); //////////////////// SPECIFY RHS /////////////////////// RHSPtr rhs = RHS::rhs(); FunctionPtr one = Function::constant(1.0); rhs->addTerm( one * v ); //////////////////// CREATE BCs /////////////////////// BCPtr bc = BC::bc(); FunctionPtr zero = Function::zero(); SpatialFilterPtr entireBoundary = SpatialFilter::allSpace(); bc->addDirichlet(uhat, entireBoundary, zero); //////////////////// SOLVE & REFINE /////////////////////// // Output solution Intrepid::FieldContainer<GlobalIndexType> savedCellPartition; Teuchos::RCP<Epetra_FEVector> savedLHSVector; { //////////////////// BUILD MESH /////////////////////// int H1Order = 4, pToAdd = 2; Teuchos::RCP<Mesh> mesh = Teuchos::rcp( new Mesh (meshTopology, bf, H1Order, pToAdd) ); Teuchos::RCP<Solution> solution = Teuchos::rcp( new Solution(mesh, bc, rhs, ip) ); solution->solve(false); RefinementStrategy refinementStrategy( solution, 0.2); HDF5Exporter exporter(mesh, "Poisson"); // exporter.exportSolution(solution, vf, 0, 2, cellIDToSubdivision(mesh, 4)); exporter.exportSolution(solution, 0, 2); mesh->saveToHDF5("MeshSave.h5"); solution->saveToHDF5("SolnSave.h5"); solution->save("PoissonProblem"); // int numRefs = 1; // for (int ref = 1; ref <= numRefs; ref++) // { // refinementStrategy.refine(commRank==0); // solution->solve(false); // mesh->saveToHDF5("MeshSave.h5"); // solution->saveToHDF5("SolnSave.h5"); // exporter.exportSolution(solution, vf, ref, 2, cellIDToSubdivision(mesh, 4)); // } mesh->globalDofAssignment()->getPartitions(savedCellPartition); savedLHSVector = solution->getLHSVector(); } { SolutionPtr loadedSolution = Solution::load(bf, "PoissonProblem"); HDF5Exporter exporter(loadedSolution->mesh(), "ProblemLoaded"); // exporter.exportSolution(loadedSolution, vf, 0, 2, cellIDToSubdivision(loadedSolution->mesh(), 4)); exporter.exportSolution(loadedSolution, 0, 2); } // { // MeshPtr loadedMesh = MeshFactory::loadFromHDF5(bf, "Test0.h5"); // Teuchos::RCP<Solution> loadedSolution = Teuchos::rcp( new Solution(loadedMesh, bc, rhs, ip) ); // loadedSolution->solve(false); // HDF5Exporter exporter(loadedMesh, "MeshLoaded"); // exporter.exportSolution(loadedSolution, vf, 0, 2, cellIDToSubdivision(loadedMesh, 4)); // } { MeshPtr loadedMesh = MeshFactory::loadFromHDF5(bf, "MeshSave.h5"); Intrepid::FieldContainer<GlobalIndexType> loadedCellPartition; loadedMesh->globalDofAssignment()->getPartitions(loadedCellPartition); if (loadedCellPartition.size() != savedCellPartition.size()) { cout << "Error: the loaded partition has different size/shape than the saved one.\n"; cout << "loaded size: " << loadedCellPartition.size() << "; saved size: " << savedCellPartition.size() << endl; } else { bool partitionsMatch = true; for (int i=0; i<loadedCellPartition.size(); i++) { if (loadedCellPartition[i] != savedCellPartition[i]) { partitionsMatch = false; break; } } if (partitionsMatch) cout << "Saved and loaded cell partitions match!\n"; else { cout << "Saved and loaded cell partitions differ.\n"; cout << "saved:\n" << savedCellPartition; cout << "loaded:\n" << loadedCellPartition; } } Teuchos::RCP<Solution> loadedSolution = Teuchos::rcp( new Solution(loadedMesh, bc, rhs, ip) ); loadedSolution->loadFromHDF5("SolnSave.h5"); Teuchos::RCP<Epetra_FEVector> loadedLHSVector = loadedSolution->getLHSVector(); if (loadedLHSVector->Map().MinLID() != savedLHSVector->Map().MinLID()) { cout << "On rank " << commRank << ", loaded min LID = " << loadedLHSVector->Map().MinLID(); cout << ", but saved min LID = " << savedLHSVector->Map().MinLID() << endl; } else if (loadedLHSVector->Map().MaxLID() != savedLHSVector->Map().MaxLID()) { cout << "On rank " << commRank << ", loaded max LID = " << loadedLHSVector->Map().MaxLID(); cout << ", but saved max LID = " << savedLHSVector->Map().MaxLID() << endl; } else { bool globalIDsMatch = true; for (int lid = loadedLHSVector->Map().MinLID(); lid <= loadedLHSVector->Map().MaxLID(); lid++) { if (loadedLHSVector->Map().GID(lid) != savedLHSVector->Map().GID(lid)) { globalIDsMatch = false; } } if (! globalIDsMatch) { cout << "On rank " << commRank << ", loaded and saved solution vector maps differ in their global IDs.\n"; } else { cout << "On rank " << commRank << ", loaded and saved solution vector maps match in their global IDs.\n"; } bool entriesMatch = true; double tol = 1e-16; if (loadedLHSVector->Map().MinLID() != loadedLHSVector->Map().MaxLID()) { for (int lid = loadedLHSVector->Map().MinLID(); lid <= loadedLHSVector->Map().MaxLID(); lid++) { double loadedValue = (*loadedLHSVector)[0][lid]; double savedValue = (*savedLHSVector)[0][lid]; double diff = abs( loadedValue - savedValue ); if (diff > tol) { entriesMatch = false; cout << "On rank " << commRank << ", loaded and saved solution vectors differ in entry with lid " << lid; cout << "; loaded value = " << loadedValue << "; saved value = " << savedValue << ".\n"; } } if (entriesMatch) { cout << "On rank " << commRank << ", loaded and saved solution vectors match!\n"; } else { cout << "On rank " << commRank << ", loaded and saved solution vectors do not match.\n"; } } } HDF5Exporter exporter(loadedMesh, "SolutionLoaded"); // exporter.exportSolution(loadedSolution, vf, 0, 2, cellIDToSubdivision(loadedMesh, 4)); exporter.exportSolution(loadedSolution, 0, 2); } } // { // // 3D tests // CellTopoPtrLegacy hex = Teuchos::rcp(new shards::CellTopology(shards::getCellTopologyData<shards::Hexahedron<8> >() )); // // let's draw a little box // vector<double> v0 = makeVertex(0,0,0); // vector<double> v1 = makeVertex(1,0,0); // vector<double> v2 = makeVertex(1,1,0); // vector<double> v3 = makeVertex(0,1,0); // vector<double> v4 = makeVertex(0,0,1); // vector<double> v5 = makeVertex(1,0,1); // vector<double> v6 = makeVertex(1,1,1); // vector<double> v7 = makeVertex(0,1,1); // vector< vector<double> > vertices; // vertices.push_back(v0); // vertices.push_back(v1); // vertices.push_back(v2); // vertices.push_back(v3); // vertices.push_back(v4); // vertices.push_back(v5); // vertices.push_back(v6); // vertices.push_back(v7); // vector<unsigned> hexVertexList; // hexVertexList.push_back(0); // hexVertexList.push_back(1); // hexVertexList.push_back(2); // hexVertexList.push_back(3); // hexVertexList.push_back(4); // hexVertexList.push_back(5); // hexVertexList.push_back(6); // hexVertexList.push_back(7); // // vector<unsigned> triVertexList; // // triVertexList.push_back(2); // // triVertexList.push_back(3); // // triVertexList.push_back(4); // vector< vector<unsigned> > elementVertices; // elementVertices.push_back(hexVertexList); // // elementVertices.push_back(triVertexList); // vector< CellTopoPtrLegacy > cellTopos; // cellTopos.push_back(hex); // // cellTopos.push_back(tri_3); // MeshGeometryPtr meshGeometry = Teuchos::rcp( new MeshGeometry(vertices, elementVertices, cellTopos) ); // MeshTopologyPtr meshTopology = Teuchos::rcp( new MeshTopology(meshGeometry) ); // FunctionPtr x = Function::xn(1); // FunctionPtr y = Function::yn(1); // FunctionPtr z = Function::zn(1); // FunctionPtr function = x + y + z; // FunctionPtr fbdr = Function::restrictToCellBoundary(function); // FunctionPtr vect = Function::vectorize(x, y, z); // vector<FunctionPtr> functions; // functions.push_back(function); // functions.push_back(vect); // vector<string> functionNames; // functionNames.push_back("function"); // functionNames.push_back("vect"); // // { // // HDF5Exporter exporter(mesh, "function3", false); // // exporter.exportFunction(function, "function3"); // // } // // { // // HDF5Exporter exporter(mesh, "boundary3", false); // // exporter.exportFunction(fbdr, "boundary3"); // // } // // { // // HDF5Exporter exporter(mesh, "vect3", false); // // exporter.exportFunction(vect, "vect3"); // // } // // { // // HDF5Exporter exporter(mesh, "functions3", false); // // exporter.exportFunction(functions, functionNames); // // } // } }
int main(int argc, char *argv[]) { // TODO: figure out the right thing to do here... // may want to modify argc and argv before we make the following call: Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); int rank=mpiSession.getRank(); int numProcs=mpiSession.getNProc(); #ifdef HAVE_MPI choice::MpiArgs args( argc, argv ); #else choice::Args args(argc, argv ); #endif int polyOrder, pToAdd; try { // read args: polyOrder = args.Input<int>("--polyOrder", "L^2 (field) polynomial order"); pToAdd = args.Input<int>("--delta_p", "delta p for test enrichment", 2); args.Process(); } catch ( choice::ArgException& e ) { exit(0); } int H1Order = polyOrder + 1; bool useCompliantGraphNorm = false; // weights to improve conditioning of the local problems bool useExtendedPrecisionForOptimalTestInversion = false; /////////////////////////// "VGP_CONFORMING" VERSION /////////////////////// // fluxes and traces: VarPtr u1hat, u2hat, t1n, t2n; // fields for SGP: VarPtr phi, p, sigma11, sigma12, sigma21, sigma22; // fields specific to VGP: VarPtr u1, u2; BFPtr stokesBF; IPPtr qoptIP; double mu = 1; FunctionPtr h = Teuchos::rcp( new hFunction() ); VarPtr tau1,tau2,v1,v2,q; VarFactory varFactory; tau1 = varFactory.testVar("\\tau_1", HDIV); tau2 = varFactory.testVar("\\tau_2", HDIV); v1 = varFactory.testVar("v_1", HGRAD); v2 = varFactory.testVar("v_2", HGRAD); q = varFactory.testVar("q", HGRAD); u1hat = varFactory.traceVar("\\widehat{u}_1"); u2hat = varFactory.traceVar("\\widehat{u}_2"); t1n = varFactory.fluxVar("\\widehat{t_{1n}}"); t2n = varFactory.fluxVar("\\widehat{t_{2n}}"); if (!useCompliantGraphNorm) { u1 = varFactory.fieldVar("u_1"); u2 = varFactory.fieldVar("u_2"); } else { u1 = varFactory.fieldVar("u_1", HGRAD); u2 = varFactory.fieldVar("u_2", HGRAD); } sigma11 = varFactory.fieldVar("\\sigma_11"); sigma12 = varFactory.fieldVar("\\sigma_12"); sigma21 = varFactory.fieldVar("\\sigma_21"); sigma22 = varFactory.fieldVar("\\sigma_22"); p = varFactory.fieldVar("p"); stokesBF = Teuchos::rcp( new BF(varFactory) ); // tau1 terms: stokesBF->addTerm(u1,tau1->div()); stokesBF->addTerm(sigma11,tau1->x()); // (sigma1, tau1) stokesBF->addTerm(sigma12,tau1->y()); stokesBF->addTerm(-u1hat, tau1->dot_normal()); // tau2 terms: stokesBF->addTerm(u2, tau2->div()); stokesBF->addTerm(sigma21,tau2->x()); // (sigma2, tau2) stokesBF->addTerm(sigma22,tau2->y()); stokesBF->addTerm(-u2hat, tau2->dot_normal()); // v1: stokesBF->addTerm(mu * sigma11,v1->dx()); // (mu sigma1, grad v1) stokesBF->addTerm(mu * sigma12,v1->dy()); stokesBF->addTerm( - p, v1->dx() ); stokesBF->addTerm( -t1n, v1); // v2: stokesBF->addTerm(mu * sigma21,v2->dx()); // (mu sigma2, grad v2) stokesBF->addTerm(mu * sigma22,v2->dy()); stokesBF->addTerm( -p, v2->dy()); stokesBF->addTerm( -t2n, v2); // q: stokesBF->addTerm(-u1,q->dx()); // (-u, grad q) stokesBF->addTerm(-u2,q->dy()); stokesBF->addTerm(u1hat->times_normal_x() + u2hat->times_normal_y(), q); if (rank==0) stokesBF->printTrialTestInteractions(); stokesBF->setUseExtendedPrecisionSolveForOptimalTestFunctions(useExtendedPrecisionForOptimalTestInversion); mesh = MeshFactory::quadMesh(stokesBF, H1Order, pToAdd); //////////////////// CREATE BCs /////////////////////// BCPtr bc = BC::bc(); //////////////////// CREATE RHS /////////////////////// RHSPtr rhs = RHS::rhs(); // zero for now... IPPtr ip; qoptIP = Teuchos::rcp(new IP()); if (useCompliantGraphNorm) { qoptIP->addTerm( mu * v1->dx() + tau1->x() ); // sigma11 qoptIP->addTerm( mu * v1->dy() + tau1->y() ); // sigma12 qoptIP->addTerm( mu * v2->dx() + tau2->x() ); // sigma21 qoptIP->addTerm( mu * v2->dy() + tau2->y() ); // sigma22 qoptIP->addTerm( mu * v1->dx() + mu * v2->dy() ); // pressure qoptIP->addTerm( h * tau1->div() - h * q->dx() ); // u1 qoptIP->addTerm( h * tau2->div() - h * q->dy()); // u2 qoptIP->addTerm( (mu / h) * v1 ); qoptIP->addTerm( (mu / h) * v2 ); qoptIP->addTerm( q ); qoptIP->addTerm( tau1 ); qoptIP->addTerm( tau2 ); } else { // standard graph norm, then qoptIP = stokesBF->graphNorm(); } ip = qoptIP; if (rank==0) ip->printInteractions(); // aim is just to answer one simple question: // have I figured out a trial-space preimage for optimal test function (q=1, tau=0, v=0)? SolutionPtr soln = Teuchos::rcp(new Solution(mesh)); FunctionPtr x = Function::xn(); FunctionPtr y = Function::yn(); // u1 = u1_hat = x / 2 FunctionPtr u1_exact = x / 2; // u2 = u2_hat = y / 2 FunctionPtr u2_exact = y / 2; // sigma = 0.5 * I FunctionPtr sigma11_exact = Function::constant(0.5); FunctionPtr sigma22_exact = Function::constant(0.5); // tn_hat = 0.5 * n FunctionPtr n = Function::normal(); FunctionPtr t1n_exact = n->x() / 2; FunctionPtr t2n_exact = n->y() / 2; map<int, FunctionPtr > exact_soln; exact_soln[u1->ID()] = u1_exact; exact_soln[u1hat->ID()] = u1_exact; exact_soln[u2->ID()] = u2_exact; exact_soln[u2hat->ID()] = u2_exact; exact_soln[sigma11->ID()] = sigma11_exact; exact_soln[sigma22->ID()] = sigma22_exact; exact_soln[t1n->ID()] = t1n_exact; exact_soln[t2n->ID()] = t2n_exact; exact_soln[p->ID()] = Function::zero(); exact_soln[sigma12->ID()] = Function::zero(); exact_soln[sigma21->ID()] = Function::zero(); soln->projectOntoMesh(exact_soln); LinearTermPtr soln_functional = stokesBF->testFunctional(soln); RieszRepPtr rieszRep = Teuchos::rcp( new RieszRep(mesh, ip, soln_functional) ); rieszRep->computeRieszRep(); // get test functions: FunctionPtr q_fxn = Teuchos::rcp( new RepFunction(q, rieszRep) ); FunctionPtr v1_fxn = Teuchos::rcp( new RepFunction(v1, rieszRep) ); FunctionPtr v2_fxn = Teuchos::rcp( new RepFunction(v2, rieszRep) ); FunctionPtr tau1_fxn = Teuchos::rcp( new RepFunction(tau1, rieszRep) ); FunctionPtr tau2_fxn = Teuchos::rcp( new RepFunction(tau2, rieszRep) ); cout << "L2 norm of (q-1) : " << (q_fxn - 1)->l2norm(mesh) << endl; cout << "L2 norm of (v1) : " << (v1_fxn)->l2norm(mesh) << endl; cout << "L2 norm of (v2) : " << (v2_fxn)->l2norm(mesh) << endl; cout << "L2 norm of (tau1) : " << (tau1_fxn)->l2norm(mesh) << endl; cout << "L2 norm of (tau2) : " << (tau2_fxn)->l2norm(mesh) << endl; VTKExporter exporter(soln, mesh, varFactory); exporter.exportSolution("conservationPreimage", H1Order*2); cout << "Checking that the soln_functional is what I expect:\n"; FunctionPtr xyVector = Function::vectorize(x, y); cout << "With v1 = x, integral: " << integralOverMesh(soln_functional, v1, x) << endl; cout << "With v2 = y, integral: " << integralOverMesh(soln_functional, v2, y) << endl; cout << "With tau1=(x,y), integral: " << integralOverMesh(soln_functional, tau1, xyVector) << endl; cout << "With tau2=(x,y), integral: " << integralOverMesh(soln_functional, tau2, xyVector) << endl; cout << "With q =x, integral: " << integralOverMesh(soln_functional, q, x) << endl; cout << "(Expect 0s all around, except for q, where we expect (1,x) == 0.5.)\n"; return 0; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); choice::MpiArgs args( argc, argv ); #else choice::Args args( argc, argv ); #endif int commRank = Teuchos::GlobalMPISession::getRank(); int numProcs = Teuchos::GlobalMPISession::getNProc(); // Required arguments int numRefs = args.Input<int>("--numRefs", "number of refinement steps"); int norm = args.Input<int>("--norm", "0 = graph\n 1 = robust\n 2 = coupled robust"); // Optional arguments (have defaults) int uniformRefinements = args.Input("--uniformRefinements", "number of uniform refinements", 0); bool enforceLocalConservation = args.Input<bool>("--conserve", "enforce local conservation", false); double radius = args.Input("--r", "cylinder radius", 0.6); int Re = args.Input("--Re", "Reynolds number", 1); int maxNewtonIterations = args.Input("--maxIterations", "maximum number of Newton iterations", 1); int polyOrder = args.Input("--polyOrder", "polynomial order for field variables", 2); int deltaP = args.Input("--deltaP", "how much to enrich test space", 2); // string saveFile = args.Input<string>("--meshSaveFile", "file to which to save refinement history", ""); // string replayFile = args.Input<string>("--meshLoadFile", "file with refinement history to replay", ""); args.Process(); //////////////////// PROBLEM DEFINITIONS /////////////////////// int H1Order = polyOrder+1; //////////////////// DECLARE VARIABLES /////////////////////// // define test variables VarFactory varFactory; VarPtr tau1 = varFactory.testVar("tau1", HDIV); VarPtr tau2 = varFactory.testVar("tau2", HDIV); VarPtr v1 = varFactory.testVar("v1", HGRAD); VarPtr v2 = varFactory.testVar("v2", HGRAD); VarPtr vc = varFactory.testVar("vc", HGRAD); // define trial variables VarPtr u1 = varFactory.fieldVar("u1"); VarPtr u2 = varFactory.fieldVar("u2"); VarPtr p = varFactory.fieldVar("p"); VarPtr u1hat = varFactory.traceVar("u1hat"); VarPtr u2hat = varFactory.traceVar("u2hat"); VarPtr t1hat = varFactory.fluxVar("t1hat"); VarPtr t2hat = varFactory.fluxVar("t2hat"); VarPtr sigma1 = varFactory.fieldVar("sigma1", VECTOR_L2); VarPtr sigma2 = varFactory.fieldVar("sigma2", VECTOR_L2); //////////////////// BUILD MESH /////////////////////// BFPtr bf = Teuchos::rcp( new BF(varFactory) ); // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = MeshFactory::shiftedHemkerMesh(-1, 3, 2, radius, bf, H1Order, deltaP); //////////////////////////////////////////////////////////////////// // INITIALIZE BACKGROUND FLOW FUNCTIONS //////////////////////////////////////////////////////////////////// BCPtr nullBC = Teuchos::rcp((BC*)NULL); RHSPtr nullRHS = Teuchos::rcp((RHS*)NULL); IPPtr nullIP = Teuchos::rcp((IP*)NULL); SolutionPtr backgroundFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); vector<double> e1(2); // (1,0) e1[0] = 1; vector<double> e2(2); // (0,1) e2[1] = 1; FunctionPtr u1_prev = Function::solution(u1, backgroundFlow); FunctionPtr u2_prev = Function::solution(u2, backgroundFlow); FunctionPtr sigma1_prev = Function::solution(sigma1, backgroundFlow); FunctionPtr sigma2_prev = Function::solution(sigma2, backgroundFlow); FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); FunctionPtr one = Teuchos::rcp( new ConstantScalarFunction(1.0) ); FunctionPtr beta = e1 * u1_prev + e2 * u2_prev; // ==================== SET INITIAL GUESS ========================== map<int, Teuchos::RCP<Function> > functionMap; functionMap[u1->ID()] = one; functionMap[u2->ID()] = zero; functionMap[sigma1->ID()] = Function::vectorize(zero,zero); functionMap[sigma2->ID()] = Function::vectorize(zero,zero); functionMap[p->ID()] = zero; backgroundFlow->projectOntoMesh(functionMap); //////////////////// DEFINE BILINEAR FORM /////////////////////// // // stress equation bf->addTerm( sigma1, tau1 ); bf->addTerm( sigma2, tau2 ); bf->addTerm( u1, tau1->div() ); bf->addTerm( u2, tau2->div() ); bf->addTerm( -u1hat, tau1->dot_normal() ); bf->addTerm( -u2hat, tau2->dot_normal() ); // momentum equation // bf->addTerm( Function::xPart(sigma1_prev)*u1, v1 ); // bf->addTerm( Function::yPart(sigma1_prev)*u2, v1 ); // bf->addTerm( Function::xPart(sigma2_prev)*u1, v2 ); // bf->addTerm( Function::yPart(sigma2_prev)*u2, v2 ); // bf->addTerm( beta*sigma1, v1); // bf->addTerm( beta*sigma2, v2); bf->addTerm( 1./Re*sigma1, v1->grad() ); bf->addTerm( 1./Re*sigma2, v2->grad() ); bf->addTerm( t1hat, v1); bf->addTerm( t2hat, v2); bf->addTerm( -p, v1->dx() ); bf->addTerm( -p, v2->dy() ); // continuity equation bf->addTerm( -u1, vc->dx() ); bf->addTerm( -u2, vc->dy() ); bf->addTerm( u1hat, vc->times_normal_x() ); bf->addTerm( u2hat, vc->times_normal_y() ); //////////////////// SPECIFY RHS /////////////////////// Teuchos::RCP<RHSEasy> rhs = Teuchos::rcp( new RHSEasy ); // stress equation rhs->addTerm( -sigma1_prev * tau1 ); rhs->addTerm( -sigma2_prev * tau2 ); rhs->addTerm( -u1_prev * tau1->div() ); rhs->addTerm( -u2_prev * tau2->div() ); // momentum equation // rhs->addTerm( -beta*sigma1_prev * v1 ); // rhs->addTerm( -beta*sigma2_prev * v2 ); rhs->addTerm( -1./Re*sigma1_prev * v1->grad() ); rhs->addTerm( -1./Re*sigma2_prev * v2->grad() ); // continuity equation rhs->addTerm( u1_prev * vc->dx() ); rhs->addTerm( u2_prev * vc->dy() ); //////////////////// DEFINE INNER PRODUCT(S) /////////////////////// IPPtr ip = Teuchos::rcp(new IP); if (norm == 0) { ip = bf->graphNorm(); } else if (norm == 1) { // ip = bf->l2Norm(); } //////////////////// CREATE BCs /////////////////////// Teuchos::RCP<BCEasy> bc = Teuchos::rcp( new BCEasy ); SpatialFilterPtr left = Teuchos::rcp( new ConstantXBoundary(-1) ); SpatialFilterPtr right = Teuchos::rcp( new ConstantXBoundary(3) ); SpatialFilterPtr top = Teuchos::rcp( new ConstantYBoundary(1) ); SpatialFilterPtr bottom = Teuchos::rcp( new ConstantYBoundary(-1) ); SpatialFilterPtr circle = Teuchos::rcp( new CircleBoundary(radius) ); FunctionPtr boundaryU1 = Teuchos::rcp( new BoundaryU1 ); bc->addDirichlet(u1hat, left, boundaryU1); bc->addDirichlet(u2hat, left, zero); bc->addDirichlet(u1hat, right, boundaryU1); bc->addDirichlet(u2hat, right, zero); bc->addDirichlet(u1hat, top, zero); bc->addDirichlet(u2hat, top, zero); bc->addDirichlet(u1hat, bottom, zero); bc->addDirichlet(u2hat, bottom, zero); bc->addDirichlet(u1hat, circle, zero); bc->addDirichlet(u2hat, circle, zero); // zero mean constraint on pressure bc->addZeroMeanConstraint(p); Teuchos::RCP<Solution> solution = Teuchos::rcp( new Solution(mesh, bc, rhs, ip) ); if (enforceLocalConservation) { solution->lagrangeConstraints()->addConstraint(u1hat->times_normal_x() + u2hat->times_normal_y() == zero); } // ==================== Register Solutions ========================== mesh->registerSolution(solution); mesh->registerSolution(backgroundFlow); // Teuchos::RCP< RefinementHistory > refHistory = Teuchos::rcp( new RefinementHistory ); // mesh->registerObserver(refHistory); //////////////////// SOLVE & REFINE /////////////////////// double energyThreshold = 0.2; // for mesh refinements RefinementStrategy refinementStrategy( solution, energyThreshold ); VTKExporter exporter(backgroundFlow, mesh, varFactory); ofstream errOut; ofstream fluxOut; if (commRank == 0) { errOut.open("stokeshemker_err.txt"); fluxOut.open("stokeshemker_flux.txt"); } errOut.precision(15); fluxOut.precision(15); // Cell IDs for flux calculations vector< pair<ElementPtr, int> > cellFace0; vector< pair<ElementPtr, int> > cellFace1; vector< pair<ElementPtr, int> > cellFace2; vector< pair<ElementPtr, int> > cellFace3; vector< pair<ElementPtr, int> > cellFace4; cellFace0.push_back(make_pair(mesh->getElement(12), 3)); cellFace0.push_back(make_pair(mesh->getElement(13), 3)); cellFace0.push_back(make_pair(mesh->getElement(14), 3)); cellFace0.push_back(make_pair(mesh->getElement(15), 3)); cellFace1.push_back(make_pair(mesh->getElement(12), 1)); cellFace1.push_back(make_pair(mesh->getElement(13), 1)); cellFace1.push_back(make_pair(mesh->getElement(14), 1)); cellFace1.push_back(make_pair(mesh->getElement(15), 1)); cellFace2.push_back(make_pair(mesh->getElement(11), 1)); cellFace2.push_back(make_pair(mesh->getElement(2 ), 0)); cellFace2.push_back(make_pair(mesh->getElement(5 ), 2)); cellFace2.push_back(make_pair(mesh->getElement(16), 1)); cellFace3.push_back(make_pair(mesh->getElement(9 ), 3)); cellFace3.push_back(make_pair(mesh->getElement(8 ), 3)); cellFace3.push_back(make_pair(mesh->getElement(19), 3)); cellFace3.push_back(make_pair(mesh->getElement(18), 3)); cellFace4.push_back(make_pair(mesh->getElement(9 ), 1)); cellFace4.push_back(make_pair(mesh->getElement(8 ), 1)); cellFace4.push_back(make_pair(mesh->getElement(19), 1)); cellFace4.push_back(make_pair(mesh->getElement(18), 1)); // // for loading refinement history // if (replayFile.length() > 0) { // RefinementHistory refHistory; // replayFile = replayFile; // refHistory.loadFromFile(replayFile); // refHistory.playback(mesh); // int numElems = mesh->numActiveElements(); // if (commRank==0){ // double minSideLength = meshInfo.getMinCellSideLength() ; // cout << "after replay, num elems = " << numElems << " and min side length = " << minSideLength << endl; // } // } for (int i = 0; i < uniformRefinements; i++) refinementStrategy.hRefineUniformly(mesh); double nonlinearRelativeEnergyTolerance = 1e-5; // used to determine convergence of the nonlinear solution for (int refIndex=0; refIndex<=numRefs; refIndex++) { double L2Update = 1e10; int iterCount = 0; while (L2Update > nonlinearRelativeEnergyTolerance && iterCount < maxNewtonIterations) { solution->solve(false); double u1L2Update = solution->L2NormOfSolutionGlobal(u1->ID()); double u2L2Update = solution->L2NormOfSolutionGlobal(u2->ID()); L2Update = sqrt(u1L2Update*u1L2Update + u2L2Update*u2L2Update); double energy_error = solution->energyErrorTotal(); // Check local conservation if (commRank == 0) { FunctionPtr n = Function::normal(); FunctionPtr u1_prev = Function::solution(u1hat, solution); FunctionPtr u2_prev = Function::solution(u2hat, solution); FunctionPtr flux = u1_prev*n->x() + u2_prev*n->y(); Teuchos::Tuple<double, 3> fluxImbalances = checkConservation(flux, zero, mesh); // cout << "Mass flux: Largest Local = " << fluxImbalances[0] // << ", Global = " << fluxImbalances[1] << ", Sum Abs = " << fluxImbalances[2] << endl; errOut << mesh->numGlobalDofs() << " " << energy_error << " " << fluxImbalances[0] << " " << fluxImbalances[1] << " " << fluxImbalances[2] << endl; double massFlux0 = computeFluxOverElementSides(u1_prev, mesh, cellFace0); double massFlux1 = computeFluxOverElementSides(u1_prev, mesh, cellFace1); double massFlux2 = computeFluxOverElementSides(u1_prev, mesh, cellFace2); double massFlux3 = computeFluxOverElementSides(u1_prev, mesh, cellFace3); double massFlux4 = computeFluxOverElementSides(u1_prev, mesh, cellFace4); fluxOut << massFlux0 << " " << massFlux1 << " " << massFlux2 << " " << massFlux3 << " " << massFlux4 << " " << endl; cout << "Total mass flux = " << massFlux0 << " " << massFlux1 << " " << massFlux2 << " " << massFlux3 << " " << massFlux4 << " " << endl; // if (saveFile.length() > 0) { // std::ostringstream oss; // oss << string(saveFile) << refIndex ; // cout << "on refinement " << refIndex << " saving mesh file to " << oss.str() << endl; // refHistory->saveToFile(oss.str()); // } } // line search algorithm double alpha = 1.0; // bool useLineSearch = false; // int posEnrich = 5; // amount of enriching of grid points on which to ensure positivity // if (useLineSearch){ // to enforce positivity of density rho // double lineSearchFactor = .5; double eps = .001; // arbitrary // FunctionPtr rhoTemp = Function::solution(rho,backgroundFlow) + alpha*Function::solution(rho,solution) - Function::constant(eps); // FunctionPtr eTemp = Function::solution(e,backgroundFlow) + alpha*Function::solution(e,solution) - Function::constant(eps); // bool rhoIsPositive = rhoTemp->isPositive(mesh,posEnrich); // bool eIsPositive = eTemp->isPositive(mesh,posEnrich); // int iter = 0; int maxIter = 20; // while (!(rhoIsPositive && eIsPositive) && iter < maxIter){ // alpha = alpha*lineSearchFactor; // rhoTemp = Function::solution(rho,backgroundFlow) + alpha*Function::solution(rho,solution); // eTemp = Function::solution(e,backgroundFlow) + alpha*Function::solution(e,solution); // rhoIsPositive = rhoTemp->isPositive(mesh,posEnrich); // eIsPositive = eTemp->isPositive(mesh,posEnrich); // iter++; // } // if (commRank==0 && alpha < 1.0){ // cout << "line search factor alpha = " << alpha << endl; // } // } backgroundFlow->addSolution(solution, alpha, false, true); iterCount++; // if (commRank == 0) // cout << "L2 Norm of Update = " << L2Update << endl; } if (commRank == 0) cout << endl; if (commRank == 0) { stringstream outfile; outfile << "stokeshemker" << uniformRefinements << "_" << refIndex; exporter.exportSolution(outfile.str()); } if (refIndex < numRefs) refinementStrategy.refine(commRank==0); // print to console on commRank 0 } if (commRank == 0) { errOut.close(); fluxOut.close(); } return 0; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); choice::MpiArgs args( argc, argv ); #else choice::Args args( argc, argv ); #endif int commRank = Teuchos::GlobalMPISession::getRank(); int numProcs = Teuchos::GlobalMPISession::getNProc(); // Required arguments int numRefs = args.Input<int>("--numRefs", "number of refinement steps"); int norm = args.Input<int>("--norm", "0 = graph\n 1 = robust\n 2 = coupled robust"); // Optional arguments (have defaults) bool enforceLocalConservation = args.Input<bool>("--conserve", "enforce local conservation", false); double Re = args.Input("--Re", "Reynolds number", 40); double nu = 1./Re; double lambda = Re/2.-sqrt(Re*Re/4+4*pi*pi); int maxNewtonIterations = args.Input("--maxIterations", "maximum number of Newton iterations", 20); int polyOrder = args.Input("--polyOrder", "polynomial order for field variables", 2); int deltaP = args.Input("--deltaP", "how much to enrich test space", 2); // string saveFile = args.Input<string>("--meshSaveFile", "file to which to save refinement history", ""); // string replayFile = args.Input<string>("--meshLoadFile", "file with refinement history to replay", ""); args.Process(); // if (commRank==0) // { // cout << "saveFile is " << saveFile << endl; // cout << "loadFile is " << replayFile << endl; // } //////////////////// PROBLEM DEFINITIONS /////////////////////// int H1Order = polyOrder+1; //////////////////// DECLARE VARIABLES /////////////////////// // define test variables VarFactory varFactory; // VarPtr tau11 = varFactory.testVar("tau11", HGRAD); // VarPtr tau12 = varFactory.testVar("tau12", HGRAD); // VarPtr tau22 = varFactory.testVar("tau22", HGRAD); VarPtr tau1 = varFactory.testVar("tau1", HDIV); VarPtr tau2 = varFactory.testVar("tau2", HDIV); VarPtr v1 = varFactory.testVar("v1", HGRAD); VarPtr v2 = varFactory.testVar("v2", HGRAD); VarPtr q = varFactory.testVar("q", HGRAD); // define trial variables VarPtr u1 = varFactory.fieldVar("u1"); VarPtr u2 = varFactory.fieldVar("u2"); // VarPtr sigma11 = varFactory.fieldVar("sigma11"); // VarPtr sigma12 = varFactory.fieldVar("sigma12"); // VarPtr sigma22 = varFactory.fieldVar("sigma22"); VarPtr sigma1 = varFactory.fieldVar("sigma1", VECTOR_L2); VarPtr sigma2 = varFactory.fieldVar("sigma2", VECTOR_L2); VarPtr u1hat = varFactory.traceVar("u1hat"); VarPtr u2hat = varFactory.traceVar("u2hat"); VarPtr t1hat = varFactory.fluxVar("t1hat"); VarPtr t2hat = varFactory.fluxVar("t2hat"); VarPtr p = varFactory.fieldVar("p"); //////////////////// BUILD MESH /////////////////////// BFPtr bf = Teuchos::rcp( new BF(varFactory) ); // define nodes for mesh FieldContainer<double> meshBoundary(4,2); double xmin = -0.5; double xmax = 1.0; double ymin = -0.5; double ymax = 1.5; meshBoundary(0,0) = xmin; // x1 meshBoundary(0,1) = ymin; // y1 meshBoundary(1,0) = xmax; meshBoundary(1,1) = ymin; meshBoundary(2,0) = xmax; meshBoundary(2,1) = ymax; meshBoundary(3,0) = xmin; meshBoundary(3,1) = ymax; int horizontalCells = 6, verticalCells = 8; // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = Mesh::buildQuadMesh(meshBoundary, horizontalCells, verticalCells, bf, H1Order, H1Order+deltaP); //////////////////////////////////////////////////////////////////// // INITIALIZE BACKGROUND FLOW FUNCTIONS //////////////////////////////////////////////////////////////////// BCPtr nullBC = Teuchos::rcp((BC*)NULL); RHSPtr nullRHS = Teuchos::rcp((RHS*)NULL); IPPtr nullIP = Teuchos::rcp((IP*)NULL); SolutionPtr backgroundFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); vector<double> e1(2); // (1,0) e1[0] = 1; vector<double> e2(2); // (0,1) e2[1] = 1; FunctionPtr u1_prev = Function::solution(u1, backgroundFlow); FunctionPtr u2_prev = Function::solution(u2, backgroundFlow); FunctionPtr sigma1_prev = Function::solution(sigma1, backgroundFlow); FunctionPtr sigma2_prev = Function::solution(sigma2, backgroundFlow); FunctionPtr p_prev = Function::solution(p, backgroundFlow); // FunctionPtr sigma11_prev = Function::solution(sigma11, backgroundFlow); // FunctionPtr sigma12_prev = Function::solution(sigma12, backgroundFlow); // FunctionPtr sigma22_prev = Function::solution(sigma22, backgroundFlow); FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); FunctionPtr one = Teuchos::rcp( new ConstantScalarFunction(1.0) ); FunctionPtr u1Exact = Teuchos::rcp( new ExactU1(lambda) ); FunctionPtr u2Exact = Teuchos::rcp( new ExactU2(lambda) ); // FunctionPtr beta = e1 * u1_prev + e2 * u2_prev; // ==================== SET INITIAL GUESS ========================== map<int, Teuchos::RCP<Function> > functionMap; functionMap[u1->ID()] = u1Exact; functionMap[u2->ID()] = u2Exact; // functionMap[sigma1->ID()] = Function::vectorize(zero,zero); // functionMap[sigma2->ID()] = Function::vectorize(zero,zero); // functionMap[p->ID()] = zero; backgroundFlow->projectOntoMesh(functionMap); //////////////////// DEFINE BILINEAR FORM /////////////////////// // // stress equation bf->addTerm( 1./nu*sigma1, tau1 ); bf->addTerm( 1./nu*sigma2, tau2 ); bf->addTerm( u1, tau1->div() ); bf->addTerm( u2, tau2->div() ); bf->addTerm( -u1hat, tau1->dot_normal() ); bf->addTerm( -u2hat, tau2->dot_normal() ); // bf->addTerm( 1./(2*nu)*sigma11, tau11 ); // bf->addTerm( 1./(2*nu)*sigma12, tau12 ); // bf->addTerm( 1./(2*nu)*sigma12, tau12 ); // bf->addTerm( 1./(2*nu)*sigma22, tau22 ); // bf->addTerm( u1, tau11->dx() ); // bf->addTerm( u1, tau12->dy() ); // bf->addTerm( u2, tau12->dx() ); // bf->addTerm( u2, tau22->dy() ); // bf->addTerm( -u1hat, tau11->times_normal_x() ); // bf->addTerm( -u1hat, tau12->times_normal_y() ); // bf->addTerm( -u2hat, tau12->times_normal_x() ); // bf->addTerm( -u2hat, tau22->times_normal_y() ); // momentum equation bf->addTerm( -2.*u1_prev*u1, v1->dx() ); bf->addTerm( -u2_prev*u1, v1->dy() ); bf->addTerm( -u1_prev*u2, v1->dy() ); bf->addTerm( -u2_prev*u1, v2->dx() ); bf->addTerm( -u1_prev*u2, v1->dy() ); bf->addTerm( -2.*u2_prev*u2, v2->dy() ); bf->addTerm( -p, v1->dx() ); bf->addTerm( -p, v2->dy() ); // bf->addTerm( sigma11, v1->dx() ); // bf->addTerm( sigma12, v1->dy() ); // bf->addTerm( sigma12, v2->dx() ); // bf->addTerm( sigma22, v2->dy() ); bf->addTerm( sigma1, v1->grad() ); bf->addTerm( sigma2, v2->grad() ); bf->addTerm( t1hat, v1); bf->addTerm( t2hat, v2); // continuity equation bf->addTerm( -u1, q->dx() ); bf->addTerm( -u2, q->dy() ); bf->addTerm( u1hat, q->times_normal_x() ); bf->addTerm( u2hat, q->times_normal_y() ); //////////////////// SPECIFY RHS /////////////////////// Teuchos::RCP<RHSEasy> rhs = Teuchos::rcp( new RHSEasy ); // stress equation rhs->addTerm( -u1_prev * tau1->div() ); rhs->addTerm( -u2_prev * tau2->div() ); // momentum equation rhs->addTerm( 2.*u1_prev*u1_prev * v1->dx() ); rhs->addTerm( u2_prev*u1_prev * v1->dy() ); rhs->addTerm( u1_prev*u2_prev * v1->dy() ); rhs->addTerm( u2_prev*u1_prev * v2->dx() ); rhs->addTerm( u1_prev*u2_prev * v1->dy() ); rhs->addTerm( 2.*u2_prev*u2_prev * v2->dy() ); // rhs->addTerm( p_prev * v1->dx() ); // rhs->addTerm( p_prev * v2->dy() ); // rhs->addTerm( -sigma1_prev * v1->grad() ); // rhs->addTerm( -sigma2_prev * v2->grad() ); // rhs->addTerm( -sigma11_prev * v1->dx() ); // rhs->addTerm( -sigma12_prev * v1->dy() ); // rhs->addTerm( -sigma12_prev * v2->dx() ); // rhs->addTerm( -sigma22_prev * v2->dy() ); // continuity equation rhs->addTerm( u1_prev * q->dx() ); rhs->addTerm( u2_prev * q->dy() ); //////////////////// DEFINE INNER PRODUCT(S) /////////////////////// IPPtr ip = Teuchos::rcp(new IP); if (norm == 0) { ip = bf->graphNorm(); } else if (norm == 1) { // ip = bf->l2Norm(); } //////////////////// CREATE BCs /////////////////////// Teuchos::RCP<BCEasy> bc = Teuchos::rcp( new BCEasy ); // Teuchos::RCP<PenaltyConstraints> pc = Teuchos::rcp( new PenaltyConstraints ); SpatialFilterPtr left = Teuchos::rcp( new ConstantXBoundary(-0.5) ); SpatialFilterPtr right = Teuchos::rcp( new ConstantXBoundary(1) ); SpatialFilterPtr top = Teuchos::rcp( new ConstantYBoundary(-0.5) ); SpatialFilterPtr bottom = Teuchos::rcp( new ConstantYBoundary(1.5) ); bc->addDirichlet(u1hat, left, u1Exact); bc->addDirichlet(u2hat, left, u2Exact); bc->addDirichlet(u1hat, right, u1Exact); bc->addDirichlet(u2hat, right, u2Exact); bc->addDirichlet(u1hat, top, u1Exact); bc->addDirichlet(u2hat, top, u2Exact); bc->addDirichlet(u1hat, bottom, u1Exact); bc->addDirichlet(u2hat, bottom, u2Exact); // zero mean constraint on pressure bc->addZeroMeanConstraint(p); // pc->addConstraint(u1hat*u2hat-t1hat == zero, top); // pc->addConstraint(u2hat*u2hat-t2hat == zero, top); Teuchos::RCP<Solution> solution = Teuchos::rcp( new Solution(mesh, bc, rhs, ip) ); // solution->setFilter(pc); // if (enforceLocalConservation) { // solution->lagrangeConstraints()->addConstraint(u1hat->times_normal_x() + u2hat->times_normal_y() == zero); // } // ==================== Register Solutions ========================== mesh->registerSolution(solution); mesh->registerSolution(backgroundFlow); // Teuchos::RCP< RefinementHistory > refHistory = Teuchos::rcp( new RefinementHistory ); // mesh->registerObserver(refHistory); //////////////////// SOLVE & REFINE /////////////////////// double energyThreshold = 0.2; // for mesh refinements RefinementStrategy refinementStrategy( solution, energyThreshold ); VTKExporter exporter(backgroundFlow, mesh, varFactory); stringstream outfile; outfile << "kovasznay" << "_" << 0; exporter.exportSolution(outfile.str()); double nonlinearRelativeEnergyTolerance = 1e-5; // used to determine convergence of the nonlinear solution for (int refIndex=0; refIndex<=numRefs; refIndex++) { double L2Update = 1e10; int iterCount = 0; while (L2Update > nonlinearRelativeEnergyTolerance && iterCount < maxNewtonIterations) { solution->solve(false); double u1L2Update = solution->L2NormOfSolutionGlobal(u1->ID()); double u2L2Update = solution->L2NormOfSolutionGlobal(u2->ID()); L2Update = sqrt(u1L2Update*u1L2Update + u2L2Update*u2L2Update); // Check local conservation if (commRank == 0) { cout << "L2 Norm of Update = " << L2Update << endl; // if (saveFile.length() > 0) { // std::ostringstream oss; // oss << string(saveFile) << refIndex ; // cout << "on refinement " << refIndex << " saving mesh file to " << oss.str() << endl; // refHistory->saveToFile(oss.str()); // } } // line search algorithm double alpha = 1.0; backgroundFlow->addSolution(solution, alpha); iterCount++; } if (commRank == 0) { stringstream outfile; outfile << "kovasznay" << "_" << refIndex+1; exporter.exportSolution(outfile.str()); } if (refIndex < numRefs) refinementStrategy.refine(commRank==0); // print to console on commRank 0 } return 0; }
int main(int argc, char *argv[]) { // Process command line arguments if (argc > 1) numRefs = atof(argv[1]); #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); int rank=mpiSession.getRank(); int numProcs=mpiSession.getNProc(); #else int rank = 0; int numProcs = 1; #endif FunctionPtr beta = Teuchos::rcp(new Beta()); //////////////////////////////////////////////////////////////////// // DEFINE VARIABLES //////////////////////////////////////////////////////////////////// // test variables VarFactory varFactory; VarPtr tau = varFactory.testVar("\\tau", HDIV); VarPtr v = varFactory.testVar("v", HGRAD); // trial variables VarPtr uhat = varFactory.traceVar("\\widehat{u}"); VarPtr beta_n_u_minus_sigma_n = varFactory.fluxVar("\\widehat{\\beta \\cdot n u - \\sigma_{n}}"); VarPtr u = varFactory.fieldVar("u"); VarPtr sigma1 = varFactory.fieldVar("\\sigma_1"); VarPtr sigma2 = varFactory.fieldVar("\\sigma_2"); //////////////////////////////////////////////////////////////////// // CREATE MESH //////////////////////////////////////////////////////////////////// BFPtr confusionBF = Teuchos::rcp( new BF(varFactory) ); FieldContainer<double> meshBoundary(4,2); meshBoundary(0,0) = 0.0; // x1 meshBoundary(0,1) = -2.0; // y1 meshBoundary(1,0) = 4.0; meshBoundary(1,1) = -2.0; meshBoundary(2,0) = 4.0; meshBoundary(2,1) = 2.0; meshBoundary(3,0) = 0.0; meshBoundary(3,1) = 2.0; int horizontalCells = 4, verticalCells = 4; // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = Mesh::buildQuadMesh(meshBoundary, horizontalCells, verticalCells, confusionBF, H1Order, H1Order+pToAdd, false); //////////////////////////////////////////////////////////////////// // INITIALIZE BACKGROUND FLOW FUNCTIONS //////////////////////////////////////////////////////////////////// BCPtr nullBC = Teuchos::rcp((BC*)NULL); RHSPtr nullRHS = Teuchos::rcp((RHS*)NULL); IPPtr nullIP = Teuchos::rcp((IP*)NULL); SolutionPtr prevTimeFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); SolutionPtr flowResidual = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); FunctionPtr u_prev_time = Teuchos::rcp( new PreviousSolutionFunction(prevTimeFlow, u) ); // ==================== SET INITIAL GUESS ========================== double u_free = 0.0; double sigma1_free = 0.0; double sigma2_free = 0.0; map<int, Teuchos::RCP<Function> > functionMap; functionMap[u->ID()] = Teuchos::rcp( new ConstantScalarFunction(u_free) ); functionMap[sigma1->ID()] = Teuchos::rcp( new ConstantScalarFunction(sigma1_free) ); functionMap[sigma2->ID()] = Teuchos::rcp( new ConstantScalarFunction(sigma2_free) ); prevTimeFlow->projectOntoMesh(functionMap); // ==================== END SET INITIAL GUESS ========================== //////////////////////////////////////////////////////////////////// // DEFINE BILINEAR FORM //////////////////////////////////////////////////////////////////// // tau terms: confusionBF->addTerm(sigma1 / epsilon, tau->x()); confusionBF->addTerm(sigma2 / epsilon, tau->y()); confusionBF->addTerm(u, tau->div()); confusionBF->addTerm(-uhat, tau->dot_normal()); // v terms: confusionBF->addTerm( sigma1, v->dx() ); confusionBF->addTerm( sigma2, v->dy() ); confusionBF->addTerm( beta * u, - v->grad() ); confusionBF->addTerm( beta_n_u_minus_sigma_n, v); //////////////////////////////////////////////////////////////////// // TIMESTEPPING TERMS //////////////////////////////////////////////////////////////////// Teuchos::RCP<RHSEasy> rhs = Teuchos::rcp( new RHSEasy ); double dt = 0.25; FunctionPtr invDt = Teuchos::rcp(new ScalarParamFunction(1.0/dt)); if (rank==0){ cout << "Timestep dt = " << dt << endl; } if (transient) { confusionBF->addTerm( u, invDt*v ); rhs->addTerm( u_prev_time * invDt * v ); } //////////////////////////////////////////////////////////////////// // DEFINE INNER PRODUCT //////////////////////////////////////////////////////////////////// // mathematician's norm IPPtr mathIP = Teuchos::rcp(new IP()); mathIP->addTerm(tau); mathIP->addTerm(tau->div()); mathIP->addTerm(v); mathIP->addTerm(v->grad()); // quasi-optimal norm IPPtr qoptIP = Teuchos::rcp(new IP); qoptIP->addTerm( v ); qoptIP->addTerm( tau / epsilon + v->grad() ); qoptIP->addTerm( beta * v->grad() - tau->div() ); // robust test norm IPPtr robIP = Teuchos::rcp(new IP); FunctionPtr ip_scaling = Teuchos::rcp( new EpsilonScaling(epsilon) ); if (!enforceLocalConservation) { robIP->addTerm( ip_scaling * v ); if (transient) robIP->addTerm( invDt * v ); } robIP->addTerm( sqrt(epsilon) * v->grad() ); // Weight these two terms for inflow FunctionPtr ip_weight = Teuchos::rcp( new IPWeight() ); robIP->addTerm( ip_weight * beta * v->grad() ); robIP->addTerm( ip_weight * tau->div() ); robIP->addTerm( ip_scaling/sqrt(epsilon) * tau ); if (enforceLocalConservation) robIP->addZeroMeanTerm( v ); //////////////////////////////////////////////////////////////////// // DEFINE RHS //////////////////////////////////////////////////////////////////// FunctionPtr f = Teuchos::rcp( new ConstantScalarFunction(0.0) ); rhs->addTerm( f * v ); // obviously, with f = 0 adding this term is not necessary! //////////////////////////////////////////////////////////////////// // DEFINE BC //////////////////////////////////////////////////////////////////// Teuchos::RCP<BCEasy> bc = Teuchos::rcp( new BCEasy ); // Teuchos::RCP<PenaltyConstraints> pc = Teuchos::rcp( new PenaltyConstraints ); SpatialFilterPtr lBoundary = Teuchos::rcp( new LeftBoundary ); SpatialFilterPtr tbBoundary = Teuchos::rcp( new TopBottomBoundary ); SpatialFilterPtr rBoundary = Teuchos::rcp( new RightBoundary ); FunctionPtr u0 = Teuchos::rcp( new ZeroBC ); FunctionPtr u_inlet = Teuchos::rcp( new InletBC ); // FunctionPtr n = Teuchos::rcp( new UnitNormalFunction ); bc->addDirichlet(beta_n_u_minus_sigma_n, lBoundary, u_inlet); bc->addDirichlet(beta_n_u_minus_sigma_n, tbBoundary, u0); bc->addDirichlet(uhat, rBoundary, u0); // pc->addConstraint(beta_n_u_minus_sigma_n - uhat == u0, rBoundary); //////////////////////////////////////////////////////////////////// // CREATE SOLUTION OBJECT //////////////////////////////////////////////////////////////////// Teuchos::RCP<Solution> solution = Teuchos::rcp( new Solution(mesh, bc, rhs, robIP) ); // solution->setFilter(pc); // ==================== Enforce Local Conservation ================== if (enforceLocalConservation) { if (transient) { FunctionPtr conserved_rhs = u_prev_time * invDt; LinearTermPtr conserved_quantity = invDt * u; LinearTermPtr flux_part = Teuchos::rcp(new LinearTerm(-1.0, beta_n_u_minus_sigma_n)); conserved_quantity->addTerm(flux_part, true); // conserved_quantity = conserved_quantity - beta_n_u_minus_sigma_n; solution->lagrangeConstraints()->addConstraint(conserved_quantity == conserved_rhs); } else { FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); solution->lagrangeConstraints()->addConstraint(beta_n_u_minus_sigma_n == zero); } } // ==================== Register Solutions ========================== mesh->registerSolution(solution); mesh->registerSolution(prevTimeFlow); // u_t(i-1) mesh->registerSolution(flowResidual); // u_t(i-1) double energyThreshold = 0.25; // for mesh refinements Teuchos::RCP<RefinementStrategy> refinementStrategy; refinementStrategy = Teuchos::rcp(new RefinementStrategy(solution,energyThreshold)); //////////////////////////////////////////////////////////////////// // PSEUDO-TIME SOLVE STRATEGY //////////////////////////////////////////////////////////////////// double time_tol = 1e-8; for (int refIndex=0; refIndex<=numRefs; refIndex++) { double L2_time_residual = 1e7; int timestepCount = 0; if (!transient) numTimeSteps = 1; while((L2_time_residual > time_tol) && (timestepCount < numTimeSteps)) { solution->solve(false); // subtract solutions to get residual flowResidual->setSolution(solution); // reset previous time solution to current time sol flowResidual->addSolution(prevTimeFlow, -1.0); double L2u = flowResidual->L2NormOfSolutionGlobal(u->ID()); double L2sigma1 = flowResidual->L2NormOfSolutionGlobal(sigma1->ID()); double L2sigma2 = flowResidual->L2NormOfSolutionGlobal(sigma2->ID()); L2_time_residual = sqrt(L2u*L2u + L2sigma1*L2sigma1 + L2sigma2*L2sigma2); cout << endl << "Timestep: " << timestepCount << ", dt = " << dt << ", Time residual = " << L2_time_residual << endl; if (rank == 0) { stringstream outfile; if (transient) outfile << "TransientConfusion_" << refIndex << "_" << timestepCount; else outfile << "TransientConfusion_" << refIndex; solution->writeToVTK(outfile.str(), 5); } ////////////////////////////////////////////////////////////////////////// // Check conservation by testing against one ////////////////////////////////////////////////////////////////////////// VarPtr testOne = varFactory.testVar("1", CONSTANT_SCALAR); // Create a fake bilinear form for the testing BFPtr fakeBF = Teuchos::rcp( new BF(varFactory) ); // Define our mass flux FunctionPtr flux_current_time = Teuchos::rcp( new PreviousSolutionFunction(solution, beta_n_u_minus_sigma_n) ); FunctionPtr delta_u = Teuchos::rcp( new PreviousSolutionFunction(flowResidual, u) ); LinearTermPtr surfaceFlux = -1.0 * flux_current_time * testOne; LinearTermPtr volumeChange = invDt * delta_u * testOne; LinearTermPtr massFluxTerm; if (transient) { massFluxTerm = volumeChange; // massFluxTerm->addTerm(surfaceFlux); } else { massFluxTerm = surfaceFlux; } // cout << "surface case = " << surfaceFlux->summands()[0].first->boundaryValueOnly() << " volume case = " << volumeChange->summands()[0].first->boundaryValueOnly() << endl; // FunctionPtr massFlux= Teuchos::rcp( new PreviousSolutionFunction(solution, beta_n_u_minus_sigma_n) ); // LinearTermPtr massFluxTerm = massFlux * testOne; Teuchos::RCP<shards::CellTopology> quadTopoPtr = Teuchos::rcp(new shards::CellTopology(shards::getCellTopologyData<shards::Quadrilateral<4> >() )); DofOrderingFactory dofOrderingFactory(fakeBF); int fakeTestOrder = H1Order; DofOrderingPtr testOrdering = dofOrderingFactory.testOrdering(fakeTestOrder, *quadTopoPtr); int testOneIndex = testOrdering->getDofIndex(testOne->ID(),0); vector< ElementTypePtr > elemTypes = mesh->elementTypes(); // global element types map<int, double> massFluxIntegral; // cellID -> integral double maxMassFluxIntegral = 0.0; double totalMassFlux = 0.0; double totalAbsMassFlux = 0.0; for (vector< ElementTypePtr >::iterator elemTypeIt = elemTypes.begin(); elemTypeIt != elemTypes.end(); elemTypeIt++) { ElementTypePtr elemType = *elemTypeIt; vector< ElementPtr > elems = mesh->elementsOfTypeGlobal(elemType); vector<int> cellIDs; for (int i=0; i<elems.size(); i++) { cellIDs.push_back(elems[i]->cellID()); } FieldContainer<double> physicalCellNodes = mesh->physicalCellNodesGlobal(elemType); BasisCachePtr basisCache = Teuchos::rcp( new BasisCache(elemType,mesh) ); basisCache->setPhysicalCellNodes(physicalCellNodes,cellIDs,true); // true: create side caches FieldContainer<double> cellMeasures = basisCache->getCellMeasures(); FieldContainer<double> fakeRHSIntegrals(elems.size(),testOrdering->totalDofs()); massFluxTerm->integrate(fakeRHSIntegrals,testOrdering,basisCache,true); // true: force side evaluation for (int i=0; i<elems.size(); i++) { int cellID = cellIDs[i]; // pick out the ones for testOne: massFluxIntegral[cellID] = fakeRHSIntegrals(i,testOneIndex); } // find the largest: for (int i=0; i<elems.size(); i++) { int cellID = cellIDs[i]; maxMassFluxIntegral = max(abs(massFluxIntegral[cellID]), maxMassFluxIntegral); } for (int i=0; i<elems.size(); i++) { int cellID = cellIDs[i]; maxMassFluxIntegral = max(abs(massFluxIntegral[cellID]), maxMassFluxIntegral); totalMassFlux += massFluxIntegral[cellID]; totalAbsMassFlux += abs( massFluxIntegral[cellID] ); } } // Print results from processor with rank 0 if (rank == 0) { cout << "largest mass flux: " << maxMassFluxIntegral << endl; cout << "total mass flux: " << totalMassFlux << endl; cout << "sum of mass flux absolute value: " << totalAbsMassFlux << endl; } prevTimeFlow->setSolution(solution); // reset previous time solution to current time sol timestepCount++; } if (refIndex < numRefs){ if (rank==0){ cout << "Performing refinement number " << refIndex << endl; } refinementStrategy->refine(rank==0); // RESET solution every refinement - make sure discretization error doesn't creep in // prevTimeFlow->projectOntoMesh(functionMap); } } return 0; }
bool LinearTermTests::testIntegration() { // for now, we just check the consistency: for LinearTerm a = b + c, does a->integrate // give the same values as b->integrate + c->integrate ? bool success = true; // VarPtr v1, v2, v3; // HGRAD members (test variables) // VarPtr q1, q2, q3; // HDIV members (test variables) // VarPtr u1, u2, u3; // L2 members (trial variables) // VarPtr u1_hat, u2_hat; // trace variables // VarPtr u3_hat_n; // flux variable // // FunctionPtr sine_x; if ( ! checkLTSumConsistency(1 * v1, 1 * v2, testOrder, basisCache) ) { cout << "(v1 + v2)->integrate not consistent with sum of summands integration.\n"; success = false; } if ( ! checkLTSumConsistency(sine_x * v1, 1 * v2, testOrder, basisCache) ) { cout << "(sine_x * v1 + v2)->integrate not consistent with sum of summands integration.\n"; success = false; } if ( ! checkLTSumConsistency(1 * q1->div(), 1 * q2->x(), testOrder, basisCache) ) { cout << "(q1->div() + q2->x())->integrate not consistent with sum of summands integration.\n"; success = false; } if ( ! checkLTSumConsistency(1 * u1, 1 * u2, trialOrder, basisCache) ) { cout << "(u1 + u2)->integrate not consistent with sum of summands integration.\n"; success = false; } if ( ! checkLTSumConsistency(1 * u1, sine_x * u2, trialOrder, basisCache) ) { cout << "(u1 + sine_x * u2)->integrate not consistent with sum of summands integration.\n"; success = false; } // now, same thing, but with boundary-value-only functions in the mix: // this next is a fairly complex test; may want to add a more granular one above... IPPtr ip = Teuchos::rcp(new IP); RHSPtr rhs = RHS::rhs(); BCPtr bc = BC::bc(); SolutionPtr solution = Teuchos::rcp( new Solution(mesh,bc,rhs,ip) ); // project some functions onto solution, so that something interesting is there: FunctionPtr u1_proj = sine_x; FunctionPtr u2_proj = cos_y; FunctionPtr u3_proj = u1_proj * u2_proj; map<int, FunctionPtr> solnToProject; solnToProject[u1->ID()] = u1_proj; solnToProject[u2->ID()] = u2_proj; solnToProject[u3->ID()] = u3_proj; solnToProject[u1_hat->ID()] = u1_proj; solnToProject[u2_hat->ID()] = u2_proj; // u3_hat_n isn't too much like a 'real' bilinear form, in that u3 itself is a scalar // this is just a test, so I'm not worried about it... solnToProject[u3_hat_n->ID()] = u3_proj; solution->projectOntoMesh(solnToProject); LinearTermPtr bfTestFunctional = bf->testFunctional(solution); // bf->addTerm(u1, q1->x()); // bf->addTerm(u2, q1->y()); // bf->addTerm(u3, v1); // bf->addTerm(u1_hat, q1->dot_normal()); // bf->addTerm(u3_hat_n, v1); LinearTermPtr testFunctionalNoBoundaryValues = u1_proj * q1->x() + u2_proj * q1->y() + u3_proj * v1; FunctionPtr u1_hat_prev = Teuchos::rcp( new PreviousSolutionFunction<double>(solution, u1_hat) ); FunctionPtr u2_hat_prev = Teuchos::rcp( new PreviousSolutionFunction<double>(solution, u2_hat) ); FunctionPtr u3_hat_prev = Teuchos::rcp( new PreviousSolutionFunction<double>(solution, u3_hat_n) ); LinearTermPtr testFunctionalBoundaryValues = u1_hat_prev * q1->dot_normal() + u3_hat_prev * v1; if ( ! checkLTSumConsistency(testFunctionalNoBoundaryValues, testFunctionalBoundaryValues, testOrder, basisCache) ) { cout << "bfTestFunctional->integrate not consistent with sum of summands integration.\n"; success = false; } if ( ! checkLTSumConsistency(testFunctionalBoundaryValues, bfTestFunctional - testFunctionalBoundaryValues, testOrder, basisCache) ) { cout << "bfTestFunctional->integrate not consistent with sum of summands integration.\n"; success = false; } if ( ! checkLTSumConsistency(testFunctionalNoBoundaryValues, bfTestFunctional - testFunctionalNoBoundaryValues, testOrder, basisCache) ) { cout << "bfTestFunctional->integrate not consistent with sum of summands integration.\n"; success = false; } return success; }
int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv, 0); int spaceDim = 2; int meshWidth = 2; bool conformingTraces = true; int H1Order = 2, delta_k = 3; double domainWidth = 1.0e-3; bool diagScaling = false; double h = domainWidth / meshWidth; double weight = h / 4.0; // ratio of area of square with sidelength h to its perimeter double sigma_weight = 1.0; // h / 4.0; // sigma = sigma_weight * u->grad() Space uHatSpace = conformingTraces ? HGRAD : L2; VarFactoryPtr vf = VarFactory::varFactory(); // fields VarPtr u = vf->fieldVar("u"); VarPtr sigma = vf->fieldVar("sigma", VECTOR_L2); // traces VarPtr u_hat = vf->traceVar("u_hat", uHatSpace); VarPtr sigma_n = vf->fluxVar("sigma_n"); // tests VarPtr v = vf->testVar("v", HGRAD); VarPtr tau = vf->testVar("tau", HDIV); BFPtr bf = BF::bf(vf); // standard BF: // bf->addTerm(sigma, v->grad()); // bf->addTerm(sigma_n, v); // // bf->addTerm(sigma, tau); // bf->addTerm(u, tau->div()); // bf->addTerm(-u_hat, tau->dot_normal()); // weighted BF: bf->addTerm(sigma, v->grad()); bf->addTerm(weight * sigma_n, v); bf->addTerm(sigma, tau); bf->addTerm(sigma_weight * u, tau->div()); bf->addTerm(- sigma_weight * weight * u_hat, tau->dot_normal()); IPPtr ip = IP::ip(); // standard IP: ip->addTerm(tau + v->grad()); ip->addTerm(tau->div()); ip->addTerm(v); ip->addTerm(tau); // weighted IP: // ip->addTerm(tau + v->grad()); // ip->addTerm(sigma_weight * tau->div()); // ip->addTerm(max(sigma_weight,1e-3) * v); // ip->addTerm(sigma_weight * weight * tau); BCPtr bc = BC::bc(); bc->addDirichlet(u_hat, SpatialFilter::allSpace(), Function::zero()); RHSPtr rhs = RHS::rhs(); rhs->addTerm(1.0 * sigma_weight * v); vector<double> dimensions(spaceDim,domainWidth); vector<int> elementCounts(spaceDim,meshWidth); MeshPtr mesh = MeshFactory::rectilinearMesh(bf, dimensions, elementCounts, H1Order, delta_k); SolutionPtr soln = Solution::solution(mesh, bc, rhs, ip); soln->setUseCondensedSolve(true); soln->initializeLHSVector(); soln->initializeStiffnessAndLoad(); soln->populateStiffnessAndLoad(); Teuchos::RCP<Epetra_RowMatrix> stiffness = soln->getStiffnessMatrix(); double condNumber = conditionNumberLAPACK(*stiffness, diagScaling); cout << "condest (1-norm): " << condNumber << endl; return 0; }
int main(int argc, char *argv[]) { #ifdef ENABLE_INTEL_FLOATING_POINT_EXCEPTIONS cout << "NOTE: enabling floating point exceptions for divide by zero.\n"; _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_INVALID); #endif Teuchos::GlobalMPISession mpiSession(&argc, &argv); int rank = Teuchos::GlobalMPISession::getRank(); #ifdef HAVE_MPI Epetra_MpiComm Comm(MPI_COMM_WORLD); //cout << "rank: " << rank << " of " << numProcs << endl; #else Epetra_SerialComm Comm; #endif Comm.Barrier(); // set breakpoint here to allow debugger attachment to other MPI processes than the one you automatically attached to. Teuchos::CommandLineProcessor cmdp(false,true); // false: don't throw exceptions; true: do return errors for unrecognized options double minTol = 1e-8; bool use3D = false; int refCount = 10; int k = 4; // poly order for field variables int delta_k = use3D ? 3 : 2; // test space enrichment int k_coarse = 0; bool useMumps = true; bool useGMGSolver = true; bool enforceOneIrregularity = true; bool useStaticCondensation = false; bool conformingTraces = false; bool useDiagonalScaling = false; // of the global stiffness matrix in GMGSolver bool printRefinementDetails = false; bool useWeightedGraphNorm = true; // graph norm scaled according to units, more or less int numCells = 2; int AztecOutputLevel = 1; int gmgMaxIterations = 10000; int smootherOverlap = 0; double relativeTol = 1e-6; double D = 1.0; // characteristic length scale cmdp.setOption("polyOrder",&k,"polynomial order for field variable u"); cmdp.setOption("delta_k", &delta_k, "test space polynomial order enrichment"); cmdp.setOption("k_coarse", &k_coarse, "polynomial order for field variables on coarse mesh"); cmdp.setOption("numRefs",&refCount,"number of refinements"); cmdp.setOption("D", &D, "domain dimension"); cmdp.setOption("useConformingTraces", "useNonConformingTraces", &conformingTraces); cmdp.setOption("enforceOneIrregularity", "dontEnforceOneIrregularity", &enforceOneIrregularity); cmdp.setOption("smootherOverlap", &smootherOverlap, "overlap for smoother"); cmdp.setOption("printRefinementDetails", "dontPrintRefinementDetails", &printRefinementDetails); cmdp.setOption("azOutput", &AztecOutputLevel, "Aztec output level"); cmdp.setOption("numCells", &numCells, "number of cells in the initial mesh"); cmdp.setOption("useScaledGraphNorm", "dontUseScaledGraphNorm", &useWeightedGraphNorm); // cmdp.setOption("gmgTol", &gmgTolerance, "tolerance for GMG convergence"); cmdp.setOption("relativeTol", &relativeTol, "Energy error-relative tolerance for iterative solver."); cmdp.setOption("gmgMaxIterations", &gmgMaxIterations, "tolerance for GMG convergence"); bool enhanceUField = false; cmdp.setOption("enhanceUField", "dontEnhanceUField", &enhanceUField); cmdp.setOption("useStaticCondensation", "dontUseStaticCondensation", &useStaticCondensation); if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } double width = D, height = D, depth = D; VarFactory varFactory; // fields: VarPtr u = varFactory.fieldVar("u", L2); VarPtr sigma = varFactory.fieldVar("\\sigma", VECTOR_L2); FunctionPtr n = Function::normal(); // traces: VarPtr u_hat; if (conformingTraces) { u_hat = varFactory.traceVar("\\widehat{u}", u); } else { cout << "Note: using non-conforming traces.\n"; u_hat = varFactory.traceVar("\\widehat{u}", u, L2); } VarPtr sigma_n_hat = varFactory.fluxVar("\\widehat{\\sigma}_{n}", sigma * n); // test functions: VarPtr tau = varFactory.testVar("\\tau", HDIV); VarPtr v = varFactory.testVar("v", HGRAD); BFPtr poissonBF = Teuchos::rcp( new BF(varFactory) ); FunctionPtr alpha = Function::constant(1); // viscosity // tau terms: poissonBF->addTerm(sigma / alpha, tau); poissonBF->addTerm(-u, tau->div()); // (sigma1, tau1) poissonBF->addTerm(u_hat, tau * n); // v terms: poissonBF->addTerm(- sigma, v->grad()); // (mu sigma1, grad v1) poissonBF->addTerm( sigma_n_hat, v); int horizontalCells = numCells, verticalCells = numCells, depthCells = numCells; vector<double> domainDimensions; domainDimensions.push_back(width); domainDimensions.push_back(height); vector<int> elementCounts; elementCounts.push_back(horizontalCells); elementCounts.push_back(verticalCells); if (use3D) { domainDimensions.push_back(depth); elementCounts.push_back(depthCells); } MeshPtr mesh, k0Mesh; int H1Order = k + 1; int H1Order_coarse = k_coarse + 1; if (!use3D) { Teuchos::ParameterList pl; map<int,int> trialOrderEnhancements; if (enhanceUField) { trialOrderEnhancements[u->ID()] = 1; } BFPtr poissonBilinearForm = poissonBF; pl.set("useMinRule", true); pl.set("bf",poissonBilinearForm); pl.set("H1Order", H1Order); pl.set("delta_k", delta_k); pl.set("horizontalElements", horizontalCells); pl.set("verticalElements", verticalCells); pl.set("divideIntoTriangles", false); pl.set("useConformingTraces", conformingTraces); pl.set("trialOrderEnhancements", &trialOrderEnhancements); pl.set("x0",(double)0); pl.set("y0",(double)0); pl.set("width", width); pl.set("height",height); mesh = MeshFactory::quadMesh(pl); pl.set("H1Order", H1Order_coarse); k0Mesh = MeshFactory::quadMesh(pl); } else { mesh = MeshFactory::rectilinearMesh(poissonBF, domainDimensions, elementCounts, H1Order, delta_k); k0Mesh = MeshFactory::rectilinearMesh(poissonBF, domainDimensions, elementCounts, H1Order_coarse, delta_k); } mesh->registerObserver(k0Mesh); // ensure that the k0 mesh refinements track those of the solution mesh RHSPtr rhs = RHS::rhs(); // zero FunctionPtr sin_pi_x = Teuchos::rcp( new Sin_ax(PI/D) ); FunctionPtr sin_pi_y = Teuchos::rcp( new Sin_ay(PI/D) ); FunctionPtr u_exact = sin_pi_x * sin_pi_y; FunctionPtr f = -(2.0 * PI * PI / (D * D)) * sin_pi_x * sin_pi_y; rhs->addTerm( f * v ); BCPtr bc = BC::bc(); SpatialFilterPtr boundary = SpatialFilter::allSpace(); bc->addDirichlet(u_hat, boundary, u_exact); IPPtr graphNorm; FunctionPtr h = Teuchos::rcp( new hFunction() ); if (useWeightedGraphNorm) { graphNorm = IP::ip(); graphNorm->addTerm( tau->div() ); // u graphNorm->addTerm( (h / alpha) * tau - h * v->grad() ); // sigma graphNorm->addTerm( v ); // boundary term (adjoint to u) graphNorm->addTerm( h * tau ); // // new effort, with the idea that the test norm should be considered in reference space, basically // graphNorm = IP::ip(); // graphNorm->addTerm( tau->div() ); // u // graphNorm->addTerm( tau / h - v->grad() ); // sigma // graphNorm->addTerm( v / h ); // boundary term (adjoint to u) // graphNorm->addTerm( tau / h ); } else { map<int, double> trialWeights; // on the squared terms in the trial space norm trialWeights[u->ID()] = 1.0 / (D * D); trialWeights[sigma->ID()] = 1.0; graphNorm = poissonBF->graphNorm(trialWeights, 1.0); // 1.0: weight on the L^2 terms } SolutionPtr solution = Solution::solution(mesh, bc, rhs, graphNorm); solution->setUseCondensedSolve(useStaticCondensation); mesh->registerSolution(solution); // sign up for projection of old solution onto refined cells. double energyThreshold = 0.2; RefinementStrategy refinementStrategy( solution, energyThreshold ); refinementStrategy.setReportPerCellErrors(true); refinementStrategy.setEnforceOneIrregularity(enforceOneIrregularity); Teuchos::RCP<Solver> coarseSolver, fineSolver; if (useMumps) { #ifdef HAVE_AMESOS_MUMPS coarseSolver = Teuchos::rcp( new MumpsSolver(512, true) ); #else cout << "useMumps=true, but MUMPS is not available!\n"; exit(0); #endif } else { coarseSolver = Teuchos::rcp( new KluSolver ); } GMGSolver* gmgSolver; if (useGMGSolver) { double tol = relativeTol; int maxIters = gmgMaxIterations; BCPtr zeroBCs = bc->copyImposingZero(); gmgSolver = new GMGSolver(zeroBCs, k0Mesh, graphNorm, mesh, solution->getDofInterpreter(), solution->getPartitionMap(), maxIters, tol, coarseSolver, useStaticCondensation); gmgSolver->setAztecOutput(AztecOutputLevel); gmgSolver->setUseConjugateGradient(true); gmgSolver->gmgOperator()->setSmootherType(GMGOperator::IFPACK_ADDITIVE_SCHWARZ); gmgSolver->gmgOperator()->setSmootherOverlap(smootherOverlap); fineSolver = Teuchos::rcp( gmgSolver ); } else { fineSolver = coarseSolver; } // if (rank==0) cout << "experimentally starting by solving with MUMPS on the fine mesh.\n"; // solution->solve( Teuchos::rcp( new MumpsSolver) ); solution->solve(fineSolver); #ifdef HAVE_EPETRAEXT_HDF5 ostringstream dir_name; dir_name << "poissonCavityFlow_k" << k; HDF5Exporter exporter(mesh,dir_name.str()); exporter.exportSolution(solution,varFactory,0); #endif #ifdef HAVE_AMESOS_MUMPS if (useMumps) coarseSolver = Teuchos::rcp( new MumpsSolver(512, true) ); #endif solution->reportTimings(); if (useGMGSolver) gmgSolver->gmgOperator()->reportTimings(); for (int refIndex=0; refIndex < refCount; refIndex++) { double energyError = solution->energyErrorTotal(); GlobalIndexType numFluxDofs = mesh->numFluxDofs(); if (rank==0) { cout << "Before refinement " << refIndex << ", energy error = " << energyError; cout << " (using " << numFluxDofs << " trace degrees of freedom)." << endl; } bool printToConsole = printRefinementDetails && (rank==0); refinementStrategy.refine(printToConsole); if (useStaticCondensation) { CondensedDofInterpreter* condensedDofInterpreter = dynamic_cast<CondensedDofInterpreter*>(solution->getDofInterpreter().get()); if (condensedDofInterpreter != NULL) { condensedDofInterpreter->reinitialize(); } } GlobalIndexType fineDofs = mesh->globalDofCount(); GlobalIndexType coarseDofs = k0Mesh->globalDofCount(); if (rank==0) { cout << "After refinement, coarse mesh has " << k0Mesh->numActiveElements() << " elements and " << coarseDofs << " dofs.\n"; cout << " Fine mesh has " << mesh->numActiveElements() << " elements and " << fineDofs << " dofs.\n"; } if (!use3D) { ostringstream fineMeshLocation, coarseMeshLocation; fineMeshLocation << "poissonFineMesh_k" << k << "_ref" << refIndex; GnuPlotUtil::writeComputationalMeshSkeleton(fineMeshLocation.str(), mesh, true); // true: label cells coarseMeshLocation << "poissonCoarseMesh_k" << k << "_ref" << refIndex; GnuPlotUtil::writeComputationalMeshSkeleton(coarseMeshLocation.str(), k0Mesh, true); // true: label cells } if (useGMGSolver) // create fresh fineSolver now that the meshes have changed: { #ifdef HAVE_AMESOS_MUMPS if (useMumps) coarseSolver = Teuchos::rcp( new MumpsSolver(512, true) ); #endif double tol = max(relativeTol * energyError, minTol); int maxIters = gmgMaxIterations; BCPtr zeroBCs = bc->copyImposingZero(); gmgSolver = new GMGSolver(zeroBCs, k0Mesh, graphNorm, mesh, solution->getDofInterpreter(), solution->getPartitionMap(), maxIters, tol, coarseSolver, useStaticCondensation); gmgSolver->setAztecOutput(AztecOutputLevel); gmgSolver->setUseDiagonalScaling(useDiagonalScaling); fineSolver = Teuchos::rcp( gmgSolver ); } solution->solve(fineSolver); solution->reportTimings(); if (useGMGSolver) gmgSolver->gmgOperator()->reportTimings(); #ifdef HAVE_EPETRAEXT_HDF5 exporter.exportSolution(solution,varFactory,refIndex+1); #endif } double energyErrorTotal = solution->energyErrorTotal(); GlobalIndexType numFluxDofs = mesh->numFluxDofs(); GlobalIndexType numGlobalDofs = mesh->numGlobalDofs(); if (rank==0) { cout << "Final mesh has " << mesh->numActiveElements() << " elements and " << numFluxDofs << " trace dofs ("; cout << numGlobalDofs << " total dofs, including fields).\n"; cout << "Final energy error: " << energyErrorTotal << endl; } #ifdef HAVE_EPETRAEXT_HDF5 exporter.exportSolution(solution,varFactory,0); #endif if (!use3D) { GnuPlotUtil::writeComputationalMeshSkeleton("poissonRefinedMesh", mesh, true); } coarseSolver = Teuchos::rcp((Solver*) NULL); // without this when useMumps = true and running on one rank, we see a crash on exit, which may have to do with MPI being finalized before coarseSolver is deleted. return 0; }
int main(int argc, char *argv[]) { #ifdef ENABLE_INTEL_FLOATING_POINT_EXCEPTIONS cout << "NOTE: enabling floating point exceptions for divide by zero.\n"; _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_INVALID); #endif Teuchos::GlobalMPISession mpiSession(&argc, &argv); int rank = Teuchos::GlobalMPISession::getRank(); Teuchos::CommandLineProcessor cmdp(false,true); // false: don't throw exceptions; true: do return errors for unrecognized options const static double PI = 3.141592653589793238462; bool useCondensedSolve = true; // condensed solve not yet compatible with minimum rule meshes int k = 2; // poly order for u in every direction, including temporal int numCells = 32; // in x, y int numTimeCells = 1; int numTimeSlabs = -1; int numFrames = 201; int delta_k = 3; // test space enrichment: should be 3 for 3D int maxRefinements = 0; // maximum # of refinements on each time slab bool useMumpsIfAvailable = true; bool useConstantConvection = false; double refinementTolerance = 0.1; int checkPointFrequency = 50; // output solution and mesh every 50 time slabs int previousSolutionTimeSlabNumber = -1; string previousSolutionFile = ""; string previousMeshFile = ""; cmdp.setOption("polyOrder",&k,"polynomial order for field variable u"); cmdp.setOption("delta_k", &delta_k, "test space polynomial order enrichment"); cmdp.setOption("numCells",&numCells,"number of cells in x and y directions"); cmdp.setOption("numTimeCells",&numTimeCells,"number of time axis cells"); cmdp.setOption("numTimeSlabs",&numTimeSlabs,"number of time slabs"); cmdp.setOption("numFrames",&numFrames,"number of frames for export"); cmdp.setOption("useConstantConvection", "useVariableConvection", &useConstantConvection); cmdp.setOption("useCondensedSolve", "useUncondensedSolve", &useCondensedSolve, "use static condensation to reduce the size of the global solve"); cmdp.setOption("useMumps", "useKLU", &useMumpsIfAvailable, "use MUMPS (if available)"); cmdp.setOption("refinementTolerance", &refinementTolerance, "relative error beyond which to stop refining"); cmdp.setOption("maxRefinements", &maxRefinements, "maximum # of refinements on each time slab"); cmdp.setOption("previousSlabNumber", &previousSolutionTimeSlabNumber, "time slab number of previous solution"); cmdp.setOption("previousSolution", &previousSolutionFile, "file with previous solution"); cmdp.setOption("previousMesh", &previousMeshFile, "file with previous mesh"); if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } int H1Order = k + 1; VarFactory varFactory; // traces: VarPtr qHat = varFactory.fluxVar("\\widehat{q}"); // fields: VarPtr u = varFactory.fieldVar("u", L2); // test functions: VarPtr v = varFactory.testVar("v", HGRAD); FunctionPtr x = Function::xn(1); FunctionPtr y = Function::yn(1); FunctionPtr c; if (useConstantConvection) { c = Function::vectorize(Function::constant(0.5), Function::constant(0.5), Function::constant(1.0)); } else { c = Function::vectorize(y-0.5, 0.5-x, Function::constant(1.0)); } FunctionPtr n = Function::normal(); BFPtr bf = Teuchos::rcp( new BF(varFactory) ); bf->addTerm( u, c * v->grad()); bf->addTerm(qHat, v); double width = 2.0, height = 2.0; int horizontalCells = numCells, verticalCells = numCells; int depthCells = numTimeCells; double x0 = -0.5; double y0 = -0.5; double t0 = 0; double totalTime = 2.0 * PI; vector<double> frameTimes; for (int i=0; i<numFrames; i++) { frameTimes.push_back((totalTime*i) / (numFrames-1)); } if (numTimeSlabs==-1) { // want the number of grid points in temporal direction to be about 2000. The temporal length is 2 * PI numTimeSlabs = (int) 2000 / k; } double timeLengthPerSlab = totalTime / numTimeSlabs; if (rank==0) { cout << "solving on " << numCells << " x " << numCells << " x " << numTimeCells << " mesh " << "of order " << k << ".\n"; cout << "numTimeSlabs: " << numTimeSlabs << endl; } SpatialFilterPtr inflowFilter = Teuchos::rcp( new InflowFilterForClockwisePlanarRotation (x0,x0+width,y0,y0+height,0.5,0.5)); vector<double> dimensions; dimensions.push_back(width); dimensions.push_back(height); dimensions.push_back(timeLengthPerSlab); vector<int> elementCounts(3); elementCounts[0] = horizontalCells; elementCounts[1] = verticalCells; elementCounts[2] = depthCells; vector<double> origin(3); origin[0] = x0; origin[1] = y0; origin[2] = t0; Teuchos::RCP<Solver> solver = Teuchos::rcp( new KluSolver ); #ifdef HAVE_AMESOS_MUMPS if (useMumpsIfAvailable) solver = Teuchos::rcp( new MumpsSolver ); #endif // double errorPercentage = 0.5; // for mesh refinements: ask to refine elements that account for 80% of the error in each step // Teuchos::RCP<RefinementStrategy> refinementStrategy; // refinementStrategy = Teuchos::rcp( new ErrorPercentageRefinementStrategy( soln, errorPercentage )); if (maxRefinements != 0) { cout << "Warning: maxRefinements is not 0, but the slice exporter implicitly assumes there won't be any refinements.\n"; } MeshPtr mesh; MeshPtr prevMesh; SolutionPtr prevSoln; mesh = MeshFactory::rectilinearMesh(bf, dimensions, elementCounts, H1Order, delta_k, origin); if (rank==0) cout << "Initial mesh has " << mesh->getTopology()->activeCellCount() << " active (leaf) cells " << "and " << mesh->globalDofCount() << " degrees of freedom.\n"; FunctionPtr sideParity = Function::sideParity(); int lastFrameOutputted = -1; SolutionPtr soln; IPPtr ip; ip = bf->graphNorm(); FunctionPtr u0 = Teuchos::rcp( new Cone_U0(0.0, 0.25, 0.1, 1.0, false) ); BCPtr bc = BC::bc(); bc->addDirichlet(qHat, inflowFilter, Function::zero()); // zero BCs enforced at the inflow boundary. bc->addDirichlet(qHat, SpatialFilter::matchingZ(t0), u0); MeshPtr initialMesh = mesh; int startingSlabNumber; if (previousSolutionTimeSlabNumber != -1) { startingSlabNumber = previousSolutionTimeSlabNumber + 1; if (rank==0) cout << "Loading mesh from " << previousMeshFile << endl; prevMesh = MeshFactory::loadFromHDF5(bf, previousMeshFile); prevSoln = Solution::solution(mesh, bc, RHS::rhs(), ip); // include BC and IP objects for sake of condensed dof interpreter setup... prevSoln->setUseCondensedSolve(useCondensedSolve); if (rank==0) cout << "Loading solution from " << previousSolutionFile << endl; prevSoln->loadFromHDF5(previousSolutionFile); double tn = (previousSolutionTimeSlabNumber+1) * timeLengthPerSlab; origin[2] = tn; mesh = MeshFactory::rectilinearMesh(bf, dimensions, elementCounts, H1Order, delta_k, origin); FunctionPtr q_prev = Function::solution(qHat, prevSoln); FunctionPtr q_transfer = Teuchos::rcp( new MeshTransferFunction(-q_prev, prevMesh, mesh, tn) ); // negate because the normals go in opposite directions bc = BC::bc(); bc->addDirichlet(qHat, inflowFilter, Function::zero()); // zero BCs enforced at the inflow boundary. bc->addDirichlet(qHat, SpatialFilter::matchingZ(tn), q_transfer); double t_slab_final = (previousSolutionTimeSlabNumber+1) * timeLengthPerSlab; int frameOrdinal = 0; while (frameTimes[frameOrdinal] < t_slab_final) { lastFrameOutputted = frameOrdinal++; } } else { startingSlabNumber = 0; } #ifdef HAVE_EPETRAEXT_HDF5 ostringstream dir_name; dir_name << "spacetime_slice_convectingCone_k" << k << "_startSlab" << startingSlabNumber; map<GlobalIndexType,GlobalIndexType> cellMap; MeshPtr meshSlice = MeshTools::timeSliceMesh(initialMesh, 0, cellMap, H1Order); HDF5Exporter sliceExporter(meshSlice,dir_name.str()); #endif soln = Solution::solution(mesh, bc, RHS::rhs(), ip); soln->setUseCondensedSolve(useCondensedSolve); for(int timeSlab = startingSlabNumber; timeSlab<numTimeSlabs; timeSlab++) { double energyThreshold = 0.2; // for mesh refinements: ask to refine elements that account for 80% of the error in each step Teuchos::RCP<RefinementStrategy> refinementStrategy; refinementStrategy = Teuchos::rcp( new RefinementStrategy( soln, energyThreshold )); FunctionPtr u_spacetime = Function::solution(u, soln); double relativeEnergyError; int refNumber = 0; // { // // DEBUGGING: just to try running the time slicing: // double t_slab_final = (timeStep+1) * timeLengthPerSlab; // int frameOrdinal = lastFrameOutputted + 1; // while (frameTimes[frameOrdinal] < t_slab_final) { // FunctionPtr u_spacetime = Function::solution(u, soln); // ostringstream dir_name; // dir_name << "spacetime_slice_convectingCone_k" << k; // MeshTools::timeSliceExport(dir_name.str(), mesh, u_spacetime, frameTimes[frameOrdinal], "u_slice"); // // cout << "Exported frame " << frameOrdinal << ", t=" << frameTimes[frameOrdinal] << endl; // frameOrdinal++; // } // } do { soln->solve(solver); soln->reportTimings(); #ifdef HAVE_EPETRAEXT_HDF5 ostringstream dir_name; dir_name << "spacetime_convectingCone_k" << k << "_t" << timeSlab; HDF5Exporter exporter(soln->mesh(),dir_name.str()); exporter.exportSolution(soln, varFactory); if (rank==0) cout << "Exported HDF solution for time slab to directory " << dir_name.str() << endl; // string u_name = "u_spacetime"; // exporter.exportFunction(u_spacetime, u_name); ostringstream file_name; file_name << dir_name.str(); bool saveSolutionAndMeshForThisSlab = ((timeSlab + 1) % checkPointFrequency == 0); // +1 so that first output is nth, not first if (saveSolutionAndMeshForThisSlab) { dir_name << ".soln"; soln->saveToHDF5(dir_name.str()); if (rank==0) cout << endl << "wrote " << dir_name.str() << endl; file_name << ".mesh"; soln->mesh()->saveToHDF5(file_name.str()); } #endif FunctionPtr u_soln = Function::solution(u, soln); double solnNorm = u_soln->l2norm(mesh); double energyError = soln->energyErrorTotal(); relativeEnergyError = energyError / solnNorm; if (rank==0) { cout << "Relative energy error for refinement " << refNumber++ << ": " << relativeEnergyError << endl; } if ((relativeEnergyError > refinementTolerance) && (refNumber < maxRefinements)) { refinementStrategy->refine(); if (rank==0) { cout << "After refinement, mesh has " << mesh->getTopology()->activeCellCount() << " active (leaf) cells " << "and " << mesh->globalDofCount() << " degrees of freedom.\n"; } } } while ((relativeEnergyError > refinementTolerance) && (refNumber < maxRefinements)); double t_slab_final = (timeSlab+1) * timeLengthPerSlab; int frameOrdinal = lastFrameOutputted + 1; vector<double> timesForSlab; while (frameTimes[frameOrdinal] < t_slab_final) { double t = frameTimes[frameOrdinal]; if (rank==0) cout << "exporting t=" << t << " on slab " << timeSlab << endl; FunctionPtr sliceFunction = MeshTools::timeSliceFunction(mesh, cellMap, u_spacetime, t); sliceExporter.exportFunction(sliceFunction, "u_slice", t); lastFrameOutputted = frameOrdinal++; } // set up next mesh/solution: FunctionPtr q_prev = Function::solution(qHat, soln); // cout << "Error in setup of q_prev: simple solution doesn't know about the map from the previous time slab to the current one. (TODO: fix this.)\n"; double tn = (timeSlab+1) * timeLengthPerSlab; origin[2] = tn; mesh = MeshFactory::rectilinearMesh(bf, dimensions, elementCounts, H1Order, delta_k, origin); FunctionPtr q_transfer = Teuchos::rcp( new MeshTransferFunction(-q_prev, soln->mesh(), mesh, tn) ); // negate because the normals go in opposite directions bc = BC::bc(); bc->addDirichlet(qHat, inflowFilter, Function::zero()); // zero BCs enforced at the inflow boundary. bc->addDirichlet(qHat, SpatialFilter::matchingZ(tn), q_transfer); // IMPORTANT: now that we are ready to step to next soln, nullify BC. If we do not do this, then we have an RCP chain // that extends back to the first time slab, effectively a memory leak. soln->setBC(BC::bc()); soln = Solution::solution(mesh, bc, RHS::rhs(), ip); soln->setUseCondensedSolve(useCondensedSolve); } return 0; }