double AztecOO_StatusTestResNorm::ComputeNorm(const Epetra_Vector & vec, NormType typeofnorm) { double result = 0.0; if (typeofnorm==TwoNorm) vec.Norm2(&result); else if (typeofnorm==OneNorm) vec.Norm1(&result); else vec.NormInf(&result); return(result); }
// // Calculate and print the 1-norm of the solution vector x along with the parameter value. // Used to generate the solution graph // void HeqProblem::printSolution(const Epetra_Vector &x, const double conParam) { double n1; // temporary variable to hold the value of the norm x.Norm1(&n1); // calculate the 1-norm of x if (outputFilePtr) { // print out the values of c and ||x||_1 if (Comm->MyPID()==0) { (*outputFilePtr) << conParam << " " << n1 << endl; } } else cout << "No output file!" << endl; }
/****************************************************************** Compute weight balance ******************************************************************/ int compute_balance(const Epetra_Vector &wgts, double myGoalWeight, double &min, double &max, double &avg) { if ((myGoalWeight < 0) || (myGoalWeight > 1.0)){ std::cerr << "compute_balance: Goal weight should be in the range [0, 1]" << std::endl; return -1; } double weightTotal; wgts.Norm1(&weightTotal); double weightLocal = 0.0; for (int i=0; i < wgts.MyLength(); i++){ weightLocal += wgts[i]; } /* My degree of imbalance. * If myGoalWeight is zero, I'm in perfect balance since I got what I wanted. */ double goalWeight = myGoalWeight * weightTotal; double imbalance = 1.0; if (myGoalWeight > 0.0){ if (weightLocal >= goalWeight) imbalance += (weightLocal - goalWeight) / goalWeight; else imbalance += (goalWeight - weightLocal) / goalWeight; } const Epetra_Comm &comm = wgts.Comm(); comm.MaxAll(&imbalance, &max, 1); comm.MinAll(&imbalance, &min, 1); comm.SumAll(&imbalance, &avg, 1); avg /= comm.NumProc(); return 0; }
int TestOneMatrix( std::string HBname, std::string MMname, std::string TRIname, Epetra_Comm &Comm, bool verbose ) { if ( Comm.MyPID() != 0 ) verbose = false ; Epetra_Map * readMap = 0; Epetra_CrsMatrix * HbA = 0; Epetra_Vector * Hbx = 0; Epetra_Vector * Hbb = 0; Epetra_Vector * Hbxexact = 0; Epetra_CrsMatrix * TriplesA = 0; Epetra_Vector * Triplesx = 0; Epetra_Vector * Triplesb = 0; Epetra_Vector * Triplesxexact = 0; Epetra_CrsMatrix * MatrixMarketA = 0; Epetra_Vector * MatrixMarketx = 0; Epetra_Vector * MatrixMarketb = 0; Epetra_Vector * MatrixMarketxexact = 0; int TRI_Size = TRIname.size() ; std::string LastFiveBytes = TRIname.substr( EPETRA_MAX(0,TRI_Size-5), TRI_Size ); if ( LastFiveBytes == ".TimD" ) { // Call routine to read in a file with a Tim Davis header and zero-based indexing EPETRA_CHK_ERR( Trilinos_Util_ReadTriples2Epetra64( &TRIname[0], false, Comm, readMap, TriplesA, Triplesx, Triplesb, Triplesxexact, false, true, true ) ); delete readMap; } else { if ( LastFiveBytes == ".triU" ) { // Call routine to read in unsymmetric Triplet matrix EPETRA_CHK_ERR( Trilinos_Util_ReadTriples2Epetra64( &TRIname[0], false, Comm, readMap, TriplesA, Triplesx, Triplesb, Triplesxexact, false, false ) ); delete readMap; } else { if ( LastFiveBytes == ".triS" ) { // Call routine to read in symmetric Triplet matrix EPETRA_CHK_ERR( Trilinos_Util_ReadTriples2Epetra64( &TRIname[0], true, Comm, readMap, TriplesA, Triplesx, Triplesb, Triplesxexact, false, false ) ); delete readMap; } else { assert( false ) ; } } } EPETRA_CHK_ERR( Trilinos_Util_ReadMatrixMarket2Epetra64( &MMname[0], Comm, readMap, MatrixMarketA, MatrixMarketx, MatrixMarketb, MatrixMarketxexact) ); delete readMap; // Call routine to read in HB problem Trilinos_Util_ReadHb2Epetra64( &HBname[0], Comm, readMap, HbA, Hbx, Hbb, Hbxexact) ; #if 0 std::cout << " HbA " ; HbA->Print( std::cout ) ; std::cout << std::endl ; std::cout << " MatrixMarketA " ; MatrixMarketA->Print( std::cout ) ; std::cout << std::endl ; std::cout << " TriplesA " ; TriplesA->Print( std::cout ) ; std::cout << std::endl ; #endif int TripleErr = 0 ; int MMerr = 0 ; for ( int i = 0 ; i < 10 ; i++ ) { double resid_Hb_Triples; double resid_Hb_Matrix_Market; double norm_A ; Hbx->Random(); // // Set the output vectors to different values: // Triplesb->PutScalar(1.1); Hbb->PutScalar(1.2); MatrixMarketb->PutScalar(1.3); HbA->Multiply( false, *Hbx, *Hbb ); norm_A = HbA->NormOne( ) ; TriplesA->Multiply( false, *Hbx, *Triplesb ); Triplesb->Update( 1.0, *Hbb, -1.0 ) ; MatrixMarketA->Multiply( false, *Hbx, *MatrixMarketb ); MatrixMarketb->Update( 1.0, *Hbb, -1.0 ) ; Triplesb->Norm1( &resid_Hb_Triples ) ; MatrixMarketb->Norm1( &resid_Hb_Matrix_Market ) ; TripleErr += ( resid_Hb_Triples > 1e-11 * norm_A ) ; MMerr += ( resid_Hb_Matrix_Market > 1e-11 * norm_A ) ; if ( verbose && resid_Hb_Triples > 1e-11 * norm_A ) std::cout << " resid_Hb_Triples = " << resid_Hb_Triples << " norm_A = " << norm_A << std::endl ; if ( verbose && resid_Hb_Matrix_Market > 1e-11 * norm_A ) std::cout << " resid_Hb_Matrix_Market = " << resid_Hb_Matrix_Market << " norm_A = " << norm_A << std::endl ; } if ( verbose ) { if ( TripleErr ) std::cout << " Error in reading " << HBname << " or " << TRIname << std::endl ; if ( MMerr ) std::cout << " Error in reading " << HBname << " or " << MMname << std::endl ; } delete HbA; delete Hbx; delete Hbb; delete Hbxexact; delete TriplesA; delete Triplesx; delete Triplesb; delete Triplesxexact; delete MatrixMarketA; delete MatrixMarketx; delete MatrixMarketb; delete MatrixMarketxexact; delete readMap; return TripleErr+MMerr ; }
/*----------------------------------------------------------------------* | Constructor (public) m.gee 01/05| | IMPORTANT: | | No matter on which level we are here, the vector xfine is ALWAYS | | a fine grid vector here! | | this is the constructor for the ismatrixfree==true case *----------------------------------------------------------------------*/ ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel( int level, int nlevel, int printlevel, ML* ml, ML_Aggregate* ag,Epetra_CrsMatrix** P, ML_NOX::Ml_Nox_Fineinterface& interface, const Epetra_Comm& comm, const Epetra_Vector& xfine, bool ismatrixfree, bool isnlnCG, int nitersCG, bool broyden, string fsmoothertype, string smoothertype, string coarsesolvetype, int nsmooth_fine, int nsmooth, int nsmooth_coarse, double conv_normF, double conv_nupdate, int conv_maxiter, int numPDE, int nullspdim, Epetra_CrsMatrix* Mat, ML_NOX::Nox_CoarseProblem_Interface* coarseinterface) : fineinterface_(interface), comm_(comm) { level_ = level; // this level nlevel_ = nlevel; // number of total levels ml_printlevel_ = printlevel; // printlevel ml_ = ml; // the global ML object ag_ = ag; // the global ML_Aggregate object thislevel_prec_ = 0; // this level's linear preconditioner thislevel_ml_ = 0; // this level's local ML object thislevel_ag_ = 0; // this level's local ML_Aggregate object coarseinterface_ = coarseinterface; // this level's coarse interface coarseprepost_ = 0; xthis_ = 0; // this level's current solution matching this level's map!!!! thislevel_A_ = 0; // this level's NOX Matrixfree operator SmootherA_ = 0; // this level's Epetra_CrsMatrix for thislevel_prec_ ismatrixfree_ = ismatrixfree; // matrixfree flag conv_normF_ = conv_normF; // NOX convergence test stuff conv_nupdate_ = conv_nupdate; conv_maxiter_ = conv_maxiter; absresid_ = 0; nupdate_ = 0; fv_ = 0; maxiters_ = 0; combo1_ = 0; combo2_ = 0; thislevel_linSys_ = 0; // this level's NOX linear system nlParams_ = 0; // NOX parameters initialGuess_ = 0; // NOX initial guess group_ = 0; // NOX group solver_ = 0; // NOX solver SmootherA_ = Mat; isnlnCG_ = isnlnCG; azlinSys_ = 0; clone_ = 0; nitersCG_ = nitersCG; broyden_ = broyden; Broyd_ = 0; #if 0 if (isnlnCG_==false && (fsmoothertype == "Jacobi" || smoothertype == "Jacobi" || coarsesolvetype == "Jacobi" )) { cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" << "**ERR**: Modified Newton's method not supported for \n" << "**ERR**: ismatrixfree_==true && smoothertype == Jacobi-Smoother\n" << "**ERR**: because no full Jacobian exists!\n" << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; } #endif if (ismatrixfree_==false) { cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" << "**ERR**: ismatrixfree_==false on level " << level_ << "\n" << "**ERR**: in constructor for ismatrixfree_==true - case\n" << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; } if (!coarseinterface_) { cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" << "**ERR**: ptr to coarseinterface=NULL on level " << level_ << "\n" << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; } if (!Mat) { cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" << "**ERR**: ptr to Matrix Mat=NULL on level " << level_ << "\n" << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; } // ------------------------------------------------------------------------ Mat->OptimizeStorage(); // ------------------------------------------------------------------------ // get the current solution to this level xthis_ = coarseinterface_->restrict_fine_to_this(xfine); // ------------------------------------------------------------------------ // create this level's preconditioner // We use a 1-level ML-hierarchy for that ML_Aggregate_Create(&thislevel_ag_); ML_Create(&thislevel_ml_,1); // ------------------------------------------------------------------------ // set the Jacobian on level 0 of the local ml EpetraMatrix2MLMatrix(thislevel_ml_,0, (dynamic_cast<Epetra_RowMatrix*>(Mat))); // ------------------------------------------------------------------------ // construct a 1-level ML-hierarchy on this level as a smoother // ------------------------------------------------------------------------ ML_Set_PrintLevel(ml_printlevel_); ML_Aggregate_Set_CoarsenScheme_Uncoupled(thislevel_ag_); ML_Aggregate_Set_DampingFactor(thislevel_ag_, 0.0); ML_Aggregate_Set_Threshold(thislevel_ag_, 0.0); ML_Aggregate_Set_MaxCoarseSize(thislevel_ag_,1); ML_Aggregate_Set_NullSpace(thislevel_ag_,numPDE,nullspdim,NULL,Mat->NumMyRows()); int thislevel_nlevel = ML_Gen_MGHierarchy_UsingAggregation(thislevel_ml_,0, ML_INCREASING,thislevel_ag_); if (thislevel_nlevel != 1) { cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" << "**ERR**: ML generated a local hierarchy of " << thislevel_nlevel << " on level " << level_ << "\n" << "**ERR**: this is supposed to be 1 Level only!\n" << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; } // set the smoother if (level_==0) Set_Smoother(ml,ag,level_,nlevel,thislevel_ml_,thislevel_ag_,fsmoothertype,nsmooth_fine); else if (level_ != nlevel_-1) // set the smoother from the input Set_Smoother(ml,ag,level_,nlevel,thislevel_ml_,thislevel_ag_,smoothertype,nsmooth); else // set the coarse solver from the input Set_Smoother(ml,ag,level_,nlevel,thislevel_ml_,thislevel_ag_,coarsesolvetype,nsmooth_coarse); // create this level's preconditioner class ML_Epetra::MultiLevelOperator* ml_tmp = new ML_Epetra::MultiLevelOperator( thislevel_ml_,comm_, Mat->OperatorDomainMap(), Mat->OperatorRangeMap()); thislevel_prec_ = new ML_NOX::ML_Nox_ConstrainedMultiLevelOperator(ml_tmp,*coarseinterface_); if (!thislevel_prec_) { cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" << "**ERR**: thislevel_prec_==NULL on level " << level_ << "\n" << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; } // intensive test of this level's ML-smoother #if 0 { cout << "Test of smoother on level " << level_ << endl; Epetra_Vector *out = new Epetra_Vector(Copy,*xthis_,0); out->PutScalar(0.0); cout << "Input\n"; xthis_->PutScalar(1.0); Mat->Multiply(false,*xthis_,*out); xthis_->PutScalar(3.0); cout << "rhs\n"; cout << *out; double norm = 0.0; out->Norm1(&norm); cout << "Norm of rhs = " << norm << endl; thislevel_prec_->ApplyInverse(*out,*xthis_); cout << "result after smoother\n"; cout << *xthis_; delete out; out = 0; } if (level_==2) exit(0); #endif // ------------------------------------------------------------------------ // generate this level's coarse prepostoperator if (level_==0) coarseprepost_ = new ML_NOX::Ml_Nox_CoarsePrePostOperator(*coarseinterface_, fineinterface_); // ------------------------------------------------------------------------ // set up NOX on this level // ------------------------------------------------------------------------ nlParams_ = new Teuchos::ParameterList(); Teuchos::ParameterList& printParams = nlParams_->sublist("Printing"); printParams.setParameter("MyPID", comm_.MyPID()); printParams.setParameter("Output Precision", 9); printParams.setParameter("Output Processor", 0); if (ml_printlevel_>9) printParams.setParameter("Output Information", NOX::Utils::OuterIteration + //NOX::Utils::OuterIterationStatusTest + //NOX::Utils::InnerIteration + //NOX::Utils::Parameters + //NOX::Utils::Details + NOX::Utils::Warning); else if (ml_printlevel_>8) printParams.setParameter("Output Information", NOX::Utils::Warning); else printParams.setParameter("Output Information",0); if (level_==0) nlParams_->sublist("Solver Options").setParameter("User Defined Pre/Post Operator", *coarseprepost_); nlParams_->setParameter("Nonlinear Solver", "Line Search Based"); Teuchos::ParameterList& searchParams = nlParams_->sublist("Line Search"); Teuchos::ParameterList* lsParamsptr = 0; if (isnlnCG_) { searchParams.setParameter("Method", "NonlinearCG"); Teuchos::ParameterList& dirParams = nlParams_->sublist("Direction"); dirParams.setParameter("Method", "NonlinearCG"); Teuchos::ParameterList& nlcgParams = dirParams.sublist("Nonlinear CG"); nlcgParams.setParameter("Restart Frequency", 10); nlcgParams.setParameter("Precondition", "On"); nlcgParams.setParameter("Orthogonalize", "Polak-Ribiere"); //nlcgParams.setParameter("Orthogonalize", "Fletcher-Reeves"); Teuchos::ParameterList& lsParams = nlcgParams.sublist("Linear Solver"); lsParams.setParameter("Aztec Solver", "CG"); lsParams.setParameter("Max Iterations", 1); lsParams.setParameter("Tolerance", 1e-11); lsParams.setParameter("Output Frequency", 0); lsParams.setParameter("Preconditioning", "User Supplied Preconditioner"); lsParams.setParameter("Preconditioner","User Defined"); } else // Newton's method using ML-preconditioned Aztec as linear solver { searchParams.setParameter("Method", "Full Step"); // Sublist for direction Teuchos::ParameterList& dirParams = nlParams_->sublist("Direction"); dirParams.setParameter("Method", "Newton"); Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton"); newtonParams.setParameter("Forcing Term Method", "Constant"); //newtonParams.setParameter("Forcing Term Method", "Type 1"); //newtonParams.setParameter("Forcing Term Method", "Type 2"); newtonParams.setParameter("Forcing Term Minimum Tolerance", 1.0e-6); newtonParams.setParameter("Forcing Term Maximum Tolerance", 0.1); Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver"); lsParamsptr = &lsParams; lsParams.setParameter("Aztec Solver", "CG"); lsParams.setParameter("Max Iterations", nitersCG_); lsParams.setParameter("Tolerance", conv_normF_); // FIXME? is this correct? if (ml_printlevel_>8) lsParams.setParameter("Output Frequency", 50); else lsParams.setParameter("Output Frequency", 0); lsParams.setParameter("Preconditioning", "User Supplied Preconditioner"); lsParams.setParameter("Preconditioner","User Defined"); } // create the initial guess initialGuess_ = new NOX::Epetra::Vector(*xthis_, NOX::DeepCopy, true); // NOTE: do not delete xthis_, it's used and destroyed by initialGuess_ // create the necessary interfaces NOX::EpetraNew::Interface::Preconditioner* iPrec = 0; NOX::EpetraNew::Interface::Required* iReq = 0; NOX::EpetraNew::Interface::Jacobian* iJac = 0; if (isnlnCG_) { // create the matrixfree operator used in the nlnCG thislevel_A_ = new NOX::EpetraNew::MatrixFree(*coarseinterface_,*xthis_,false); // create the necessary interfaces iPrec = 0; iReq = coarseinterface_; iJac = thislevel_A_; // create the linear system thislevel_linSys_ = new ML_NOX::Ml_Nox_LinearSystem( *iJac,*thislevel_A_,*iPrec, coarseinterface_,*thislevel_prec_, *xthis_,ismatrixfree_,level_,ml_printlevel_); // create the group group_ = new NOX::EpetraNew::Group(printParams,*iReq,*initialGuess_,*thislevel_linSys_); } else // Modified Newton's method { if (!broyden_) { // create the necessary interfaces iPrec = this; iReq = coarseinterface_; iJac = this; // create the initial guess vector clone_ = new Epetra_Vector(*xthis_); // create the linear system azlinSys_ = new NOX::EpetraNew::LinearSystemAztecOO( printParams,*lsParamsptr, *iJac,*SmootherA_,*iPrec, *thislevel_prec_,*clone_); } else { // create the initial guess vector clone_ = new Epetra_Vector(*xthis_); // create the necessary interfaces iPrec = this; iReq = coarseinterface_; Broyd_ = new NOX::EpetraNew::BroydenOperator(*nlParams_,*clone_, *SmootherA_,false); // create the linear system azlinSys_ = new NOX::EpetraNew::LinearSystemAztecOO( printParams,*lsParamsptr, *Broyd_,*SmootherA_,*iPrec, *thislevel_prec_,*clone_); } // create the group group_ = new NOX::EpetraNew::Group(printParams,*iReq,*initialGuess_,*azlinSys_); } // create convergence test create_Nox_Convergencetest(conv_normF_,conv_nupdate_,conv_maxiter_); // create the solver solver_ = new NOX::Solver::Manager(*group_,*combo2_,*nlParams_); return; }