void NonlinearEigen::init() { EigenExecutionerBase::init(); if (_free_iter>0) { // save the initial guess _problem.advanceState(); // free power iterations _console << std::endl << " Free power iteration starts" << std::endl; Real initial_res; inversePowerIteration(_free_iter, _free_iter, _pfactor, false, std::numeric_limits<Real>::min(), std::numeric_limits<Real>::max(), true, _output_pi, 0.0, _eigenvalue, initial_res); _problem.computeUserObjects(EXEC_TIMESTEP_END, UserObjectWarehouse::PRE_AUX); _problem.onTimestepEnd(); _problem.computeAuxiliaryKernels(EXEC_TIMESTEP_END); _problem.computeUserObjects(EXEC_TIMESTEP_END, UserObjectWarehouse::POST_AUX); if (!getParam<bool>("output_on_final")) { _problem.timeStep()++; Real t = _problem.time(); _problem.time() = _problem.timeStep(); _problem.outputStep(EXEC_TIMESTEP_END); _problem.time() = t; } } }
void InversePowerMethod::takeStep() { // save the initial guess and mark a new time step _problem.advanceState(); preSolve(); Real initial_res; inversePowerIteration(_min_iter, _max_iter, _pfactor, _cheb_on, _eig_check_tol, true, _solution_diff_name, _sol_check_tol, _eigenvalue, initial_res); postSolve(); if (lastSolveConverged()) { printEigenvalue(); _problem.onTimestepEnd(); _problem.execute(EXEC_TIMESTEP_END); } }
void NonlinearEigen::init() { if (_app.isRecovering()) { _console << "\nCannot recover NonlinearEigen solves!\nExiting...\n" << std::endl; return; } EigenExecutionerBase::init(); if (_free_iter>0) { // save the initial guess _problem.advanceState(); // free power iterations _console << " Free power iteration starts" << std::endl; Real initial_res; inversePowerIteration(_free_iter, _free_iter, _pfactor, false, std::numeric_limits<Real>::min(), true, "", std::numeric_limits<Real>::max(), _eigenvalue, initial_res); _problem.onTimestepEnd(); _problem.execute(EXEC_TIMESTEP_END); if (_output_after_pi) { // output initial guess created by free power iterations _problem.timeStep()++; Real t = _problem.time(); _problem.time() = _problem.timeStep(); _problem.outputStep(EXEC_TIMESTEP_END); _problem.time() = t; } } }
void iterativeSolvers_unitTest() { // small matrix test { Vector4f test = makeVector4f(1.0f, 3.14f, 2.0f, 2.71f); Matrix4f mat = makeRotX4f(0.32f) * makeRotY4f(-0.39f) * makeRotZ4f(0.76f); mat += mat.transpose(); mat += IDENTITY4F*5; Vector4f rhs = mat * test; Matrix4f invMat = invertMatrix(mat); Vector4f directFloat = invMat * rhs; DVectorD testD(4); for (card32 i=0; i<4; i++) testD[i] = test[i]; DVectorD rhsD(4); for (card32 i=0; i<4; i++) rhsD[i] = rhs[i]; SparseMatrixD matS(4); for (card32 r=0; r<4; r++) { for (card32 c=0; c<4; c++) { double v = mat[c][r]; if (v != 0) { matS[r].setEntry(c, v); } } } output << "Matrix:\n" << mat.toString() << "\n"; output << "true solution: " << test.toString() << "\n"; output << "right hand side: " << rhs.toString() << "\n"; output << "direct float prec. solution: " << directFloat.toString() << "\n\n"; output << "Matrix:\n" << matS.toString() << "\n"; output << "true solution: " << testD.toString() << "\n"; output << "right hand side: " << rhsD.toString() << "\n\n"; DVectorD x = nullDVector<double>(4); card32 numIterations = 1000; gaussSeidelSolve(matS, x, rhsD, numIterations, 1E-20, 1E-5, false); output << "Gauss-Seidel solution: " << x.toString() << "\n\n"; numIterations = 1000; x = nullDVector<double>(4); steepestDescentSolve(matS, x, rhsD, numIterations, 1E-5, false); output << "Steepest-Descent solution: " << x.toString() << "\n\n"; numIterations = 1000; x = nullDVector<double>(4); conjugateGradientsSolve(matS, x, rhsD, numIterations, 1E-5, true); output << "CG solution: " << x.toString() << "\n\n"; } // large matrix test { SparseMatrixD mat(MDIM*MDIM); for (int y=0; y<MDIM; y++) { for (int x=0; x<MDIM; x++) { addEntry(mat, x,y, x,y, -4, MDIM); addEntry(mat, x,y, x+1,y, 1, MDIM); addEntry(mat, x,y, x-1,y, 1, MDIM); addEntry(mat, x,y, x,y+1, 1, MDIM); addEntry(mat, x,y, x,y-1, 1, MDIM); } } Timer T; card32 vt; DVectorD x = nullDVector<double>(MDIM*MDIM); DVectorD b = scalarDVector<double>(MDIM*MDIM, 1); DVectorD gs, sd, cg; x = nullDVector<double>(MDIM*MDIM); T.getDeltaValue(); card32 numIterations = 100000; conjugateGradientsSolve(mat, x, b, numIterations, 1E-7, false); vt = T.getDeltaValue(); output << "CG time: " << vt << "\n\n"; output << "Large sytem CG solution (LAPL(f) == 1):\n"; for (int y=0; y<MDIM; y++) { for (int xp=0; xp<MDIM; xp++) { output << x[xp +y*MDIM] << "\t"; } output.cr(); } output.cr(); cg = x; x = nullDVector<double>(MDIM*MDIM); T.getDeltaValue(); numIterations = 100000; gaussSeidelSolve(mat, x, b, numIterations, 1E-20, 1E-7, false); vt = T.getDeltaValue(); output << "GS time: " << vt << "\n\n"; output << "Large sytem Gauss-Seidel solution (LAPL(f) == 1):\n"; for (int y=0; y<MDIM; y++) { for (int xp=0; xp<MDIM; xp++) { output << x[xp +y*MDIM] << "\t"; } output.cr(); } output.cr(); gs = x; x = nullDVector<double>(MDIM*MDIM); T.getDeltaValue(); numIterations = 100000; steepestDescentSolve(mat, x, b, numIterations, 1E-7, false); vt = T.getDeltaValue(); output << "SD time: " << vt << "\n\n"; output << "Large sytem steepest descent solution (LAPL(f) == 1):\n"; for (int y=0; y<MDIM; y++) { for (int xp=0; xp<MDIM; xp++) { output << (long double)x[xp +y*MDIM] << "\t"; } output.cr(); } output.cr(); sd = x; x = nullDVector<double>(MDIM*MDIM); output << "|gs-sd|_2 " << (long double)norm(gs-sd) << "\n"; output << "|gs-cg|_2 " << (long double)norm(gs-cg) << "\n"; vector< DVectorD > eigenVects; vector< double > eigenvalues; DVectorD eigenvector; double eigenvalue; for (int i=0; i<10; i++) { powerIteration(mat, eigenVects, eigenvector, eigenvalue, 1.0001, 1000, false); eigenVects.push_back(eigenvector); eigenvalues.push_back(eigenvalue); } for (int i=0; i<10; i++) { output << i << " largest of large sytem eigenvector (LAPL(f) == 1):\n"; for (int y=0; y<MDIM; y++) { for (int xp=0; xp<MDIM; xp++) { output << eigenVects[i][xp +y*MDIM] << "\t"; } output.cr(); } output.cr(); } output << "eigenvalue:\n"; for (int i=0; i<10; i++) { output << eigenvalues[i] << "\n"; } T.getDeltaValue(); eigenVects.clear(); for (int i=0; i<10; i++) { GaussSeidelSolver<double> linSolve; inversePowerIteration(mat, eigenVects, eigenvector, eigenvalue, &linSolve, 1.0001, 1000, false); eigenvalues.push_back(eigenvalue); eigenVects.push_back(eigenvector); } output << "GS-PowerIt time: " << T.getDeltaValue() << "\n\n"; eigenVects.clear(); for (int i=0; i<10; i++) { CGSolver<double> linSolve; inversePowerIteration(mat, eigenVects, eigenvector, eigenvalue, &linSolve, 1.0001, 1000, false); eigenvalues.push_back(eigenvalue); eigenVects.push_back(eigenvector); } output << "CG-PowerIt time: " << T.getDeltaValue() << "\n\n"; eigenVects.clear(); for (int i=0; i<10; i++) { SteepestDescentSolver<double> linSolve; inversePowerIteration(mat, eigenVects, eigenvector, eigenvalue, &linSolve, 1.0001, 1000, false); eigenvalues.push_back(eigenvalue); eigenVects.push_back(eigenvector); } output << "Steepest descent-PowerIt time: " << T.getDeltaValue() << "\n\n"; for (int i=0; i<10; i++) { output << i << " smallest of large sytem eigenvector (LAPL(f) == 1):\n"; for (int y=0; y<MDIM; y++) { for (int xp=0; xp<MDIM; xp++) { output << eigenVects[i][xp +y*MDIM] << "\t"; } output.cr(); } output.cr(); } output << "eigenvalues:\n"; for (int i=0; i<10; i++) { output << eigenvalues[i] << "\n"; } } { Timer T; output << "cg scaling test\n"; for (unsigned i=1; i<21; i++) { const int size = i * 10; SparseMatrixD mat(size*size); for (int y=0; y<size; y++) { for (int x=0; x<size; x++) { addEntry(mat, x,y, x,y, -4, size); addEntry(mat, x,y, x+1,y, 1, size); addEntry(mat, x,y, x-1,y, 1, size); addEntry(mat, x,y, x,y+1, 1, size); addEntry(mat, x,y, x,y-1, 1, size); } } Timer T; card32 vt; DVectorD x = nullDVector<double>(size*size); DVectorD b = scalarDVector<double>(size*size, 1); T.getDeltaValue(); card32 numIterations = 100000; conjugateGradientsSolve(mat, x, b, numIterations, 1E-7, false); vt = T.getDeltaValue(); output << i << "\t" << vt << "\n"; } } { Timer T; output << "gs scaling test\n"; for (unsigned i=1; i<21; i++) { const int size = i * 10; SparseMatrixD mat(size*size); for (int y=0; y<size; y++) { for (int x=0; x<size; x++) { addEntry(mat, x,y, x,y, -4, size); addEntry(mat, x,y, x+1,y, 1, size); addEntry(mat, x,y, x-1,y, 1, size); addEntry(mat, x,y, x,y+1, 1, size); addEntry(mat, x,y, x,y-1, 1, size); } } Timer T; card32 vt; DVectorD x = nullDVector<double>(size*size); DVectorD b = scalarDVector<double>(size*size, 1); T.getDeltaValue(); card32 numIterations = 100000; gaussSeidelSolve(mat, x, b, numIterations, 1E-20, 1E-7, false); vt = T.getDeltaValue(); output << i << "\t" << vt << "\n"; } } { Timer T; output << "sd scaling test\n"; for (unsigned i=1; i<21; i++) { const int size = i * 10; SparseMatrixD mat(size*size); for (int y=0; y<size; y++) { for (int x=0; x<size; x++) { addEntry(mat, x,y, x,y, -4, size); addEntry(mat, x,y, x+1,y, 1, size); addEntry(mat, x,y, x-1,y, 1, size); addEntry(mat, x,y, x,y+1, 1, size); addEntry(mat, x,y, x,y-1, 1, size); } } Timer T; card32 vt; DVectorD x = nullDVector<double>(size*size); DVectorD b = scalarDVector<double>(size*size, 1); T.getDeltaValue(); card32 numIterations = 100000; steepestDescentSolve(mat, x, b, numIterations, 1E-7, false); vt = T.getDeltaValue(); output << i << "\t" << vt << "\n"; } } }
void NonlinearEigen::execute() { preExecute(); // save the initial guess _problem.copyOldSolutions(); Real initial_res = 0.0; if (_free_iter>0) { // free power iterations Moose::out << std::endl << " Free power iteration starts" << std::endl; inversePowerIteration(_free_iter, _free_iter, _pfactor, false, std::numeric_limits<Real>::min(), std::numeric_limits<Real>::max(), true, _output_pi, 0.0, _eigenvalue, initial_res); } if (!getParam<bool>("output_on_final")) { _problem.timeStep() = POWERITERATION_END; Real t = _problem.time(); _problem.time() = _problem.timeStep(); _output_warehouse.outputStep(); _problem.time() = t; } Moose::out << " Nonlinear iteration starts" << std::endl; _problem.timestepSetup(); _problem.copyOldSolutions(); Real rel_tol = _rel_tol; Real abs_tol = _abs_tol; if (_free_iter>0) { Moose::out << " Initial |R| = " << initial_res << std::endl; abs_tol = _rel_tol*initial_res; if (abs_tol<_abs_tol) abs_tol = _abs_tol; rel_tol = 1e-50; } // nonlinear solve preSolve(); nonlinearSolve(rel_tol, abs_tol, _pfactor, _eigenvalue); postSolve(); _problem.computeUserObjects(EXEC_TIMESTEP, UserObjectWarehouse::PRE_AUX); _problem.onTimestepEnd(); _problem.computeAuxiliaryKernels(EXEC_TIMESTEP); _problem.computeUserObjects(EXEC_TIMESTEP, UserObjectWarehouse::POST_AUX); if (_run_custom_uo) _problem.computeUserObjects(EXEC_CUSTOM); if (!getParam<bool>("output_on_final")) { _problem.timeStep() = NONLINEAR_SOLVE_END; Real t = _problem.time(); _problem.time() = _problem.timeStep(); _output_warehouse.outputStep(); _problem.time() = t; } Real s = normalizeSolution(_norm_execflag!=EXEC_CUSTOM && _norm_execflag!=EXEC_TIMESTEP && _norm_execflag!=EXEC_RESIDUAL); Moose::out << " Solution is rescaled with factor " << s << " for normalization!" << std::endl; if (getParam<bool>("output_on_final") || std::fabs(s-1.0)>std::numeric_limits<Real>::epsilon()) { _problem.timeStep() = FINAL; Real t = _problem.time(); _problem.time() = _problem.timeStep(); _output_warehouse.outputStep(); _problem.time() = t; } postExecute(); }