Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
test_belos_gcrodr.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 // 
00005 //         Stratimikos: Thyra-based strategies for linear solvers
00006 //                Copyright (2006) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00039 // 
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include <Teuchos_ConfigDefs.hpp>
00045 #include <Teuchos_UnitTestHarness.hpp>
00046 #include <Teuchos_RCP.hpp>
00047 #include <Teuchos_ParameterList.hpp>
00048 #include "Teuchos_XMLParameterListHelpers.hpp"
00049 
00050 #include <string>
00051 #include <iostream>
00052 
00053 #ifdef HAVE_MPI
00054    #include "Epetra_MpiComm.h"
00055 #else
00056    #include "Epetra_SerialComm.h"
00057 #endif
00058 
00059 #include "Epetra_Map.h"
00060 #include "Epetra_CrsMatrix.h"
00061 #include "Epetra_Vector.h"
00062 // #include "EpetraExt_RowMatrixOut.h"
00063 
00064 #include "Thyra_LinearOpBase.hpp"
00065 #include "Thyra_VectorBase.hpp"
00066 #include "Thyra_EpetraThyraWrappers.hpp"
00067 #include "Thyra_EpetraLinearOp.hpp"
00068 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
00069 #include "Thyra_LinearOpWithSolveHelpers.hpp"
00070 #include "Thyra_DefaultZeroLinearOp.hpp"
00071 #include "Thyra_DefaultProductVector.hpp"
00072 #include "Thyra_DefaultProductVectorSpace.hpp"
00073 #include "Thyra_MultiVectorStdOps.hpp"
00074 #include "Thyra_VectorStdOps.hpp"
00075 #include "Thyra_DefaultBlockedLinearOp.hpp"
00076 
00077 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
00078 
00079 using Teuchos::RCP;
00080 using Teuchos::rcp;
00081 
00082 Teuchos::RCP<Epetra_CrsMatrix> buildMatrix(int nx, Epetra_Comm & comm)
00083 {
00084    Epetra_Map map(nx*comm.NumProc(),0,comm);
00085    Teuchos::RCP<Epetra_CrsMatrix> mat = Teuchos::rcp(new Epetra_CrsMatrix(Copy,map,3));
00086 
00087    int offsets[3] = {-1, 0, 1 };
00088    double values[3] = { -1, 2, -1};
00089    int maxGid = map.MaxAllGID();
00090    for(int lid=0;lid<nx;lid++) {
00091       int gid = mat->GRID(lid);
00092       int numEntries = 3, offset = 0;
00093       int indices[3] = { gid+offsets[0],
00094                          gid+offsets[1],
00095                          gid+offsets[2] };
00096       if(gid==0) { // left end point
00097          numEntries = 2;
00098          offset = 1;
00099       }            // right end point
00100       else if(gid==maxGid)
00101          numEntries = 2;
00102 
00103       // insert rows
00104       mat->InsertGlobalValues(gid,numEntries,values+offset,indices+offset);
00105    }
00106 
00107    mat->FillComplete();
00108    return mat;
00109 }
00110 
00111 TEUCHOS_UNIT_TEST(belos_gcrodr, multiple_solves)
00112 {
00113    // build global (or serial communicator)
00114    #ifdef HAVE_MPI
00115       Epetra_MpiComm Comm(MPI_COMM_WORLD);
00116    #else
00117       Epetra_SerialComm Comm;
00118    #endif
00119 
00120    // build and allocate linear system
00121    Teuchos::RCP<Epetra_CrsMatrix> mat = buildMatrix(100,Comm);
00122    Teuchos::RCP<Epetra_Vector> x0 = rcp(new Epetra_Vector(mat->OperatorDomainMap()));
00123    Teuchos::RCP<Epetra_Vector> x1 = rcp(new Epetra_Vector(mat->OperatorDomainMap()));
00124    Teuchos::RCP<Epetra_Vector> b = rcp(new Epetra_Vector(mat->OperatorRangeMap()));
00125 
00126    x0->Random();
00127    x1->Random();
00128    b->PutScalar(0.0);
00129 
00130    // sanity check
00131    // EpetraExt::RowMatrixToMatrixMarketFile("mat_output.mm",*mat);
00132 
00133    // build Thyra wrappers
00134    RCP<const Thyra::LinearOpBase<double> >
00135       tA = Thyra::epetraLinearOp( mat );
00136    RCP<Thyra::VectorBase<double> >
00137       tx0 = Thyra::create_Vector( x0, tA->domain() );
00138    RCP<Thyra::VectorBase<double> >
00139       tx1 = Thyra::create_Vector( x1, tA->domain() );
00140    RCP<const Thyra::VectorBase<double> >
00141       tb = Thyra::create_Vector( b, tA->range() );
00142 
00143    // now comes Stratimikos
00144    RCP<Teuchos::ParameterList> paramList = Teuchos::getParametersFromXmlFile("BelosGCRODRTest.xml");
00145    Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder;
00146    linearSolverBuilder.setParameterList(paramList);
00147  
00148    // Create a linear solver factory given information read from the
00149    // parameter list.
00150    RCP<Thyra::LinearOpWithSolveFactoryBase<double> > lowsFactory =
00151          linearSolverBuilder.createLinearSolveStrategy("");
00152 
00153    // Create a linear solver based on the forward operator A
00154    RCP<Thyra::LinearOpWithSolveBase<double> > lows =
00155          Thyra::linearOpWithSolve(*lowsFactory, tA);
00156 
00157    // Solve the linear system 
00158    Thyra::SolveStatus<double> status; 
00159    status = Thyra::solve<double>(*lows, Thyra::NOTRANS, *tb, tx0.ptr());
00160    status = Thyra::solve<double>(*lows, Thyra::NOTRANS, *tb, tx1.ptr());
00161 }
00162 
00163 TEUCHOS_UNIT_TEST(belos_gcrodr, 2x2_multiple_solves)
00164 {
00165    // build global (or serial communicator)
00166    #ifdef HAVE_MPI
00167       Epetra_MpiComm Comm(MPI_COMM_WORLD);
00168    #else
00169       Epetra_SerialComm Comm;
00170    #endif
00171 
00172    // build and allocate linear system
00173    Teuchos::RCP<Epetra_CrsMatrix> mat = buildMatrix(100,Comm);
00174    Teuchos::RCP<Epetra_Vector> b = rcp(new Epetra_Vector(mat->OperatorRangeMap()));
00175 
00176    b->PutScalar(0.0);
00177 
00178    // sanity check
00179    // EpetraExt::RowMatrixToMatrixMarketFile("mat_output.mm",*mat);
00180 
00181    // build Thyra wrappers
00182    RCP<const Thyra::LinearOpBase<double> > tA;
00183    RCP<const Thyra::VectorBase<double> > tb;
00184    {
00185       // build blocked linear Op
00186       RCP<const Thyra::LinearOpBase<double> > tA_sub 
00187             = Thyra::epetraLinearOp( mat );
00188       RCP<const Thyra::LinearOpBase<double> > zero 
00189             = Thyra::zero(tA_sub->range(),tA_sub->domain());
00190       
00191       tA = Thyra::block2x2(tA_sub,zero,zero,tA_sub);
00192 
00193       // build blocked vector
00194       RCP<const Thyra::VectorBase<double> > tb_sub 
00195             = Thyra::create_Vector( b, tA_sub->range() );
00196 
00197       RCP<Thyra::VectorBase<double> > tb_m = Thyra::createMember(tA->range());
00198       Thyra::randomize(-1.0,1.0,tb_m.ptr());
00199 
00200       tb = tb_m;
00201    }
00202    RCP<Thyra::VectorBase<double> > tx0;
00203    RCP<Thyra::VectorBase<double> > tx1;
00204    {
00205       tx0 = Thyra::createMember(tA->domain());
00206       tx1 = Thyra::createMember(tA->domain());
00207      
00208       Thyra::randomize(-1.0,1.0,tx0.ptr());
00209       Thyra::randomize(-1.0,1.0,tx1.ptr());
00210    }
00211 
00212    // now comes Stratimikos
00213    RCP<Teuchos::ParameterList> paramList = Teuchos::getParametersFromXmlFile("BelosGCRODRTest.xml");
00214    Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder;
00215    linearSolverBuilder.setParameterList(paramList);
00216  
00217    // Create a linear solver factory given information read from the
00218    // parameter list.
00219    RCP<Thyra::LinearOpWithSolveFactoryBase<double> > lowsFactory =
00220          linearSolverBuilder.createLinearSolveStrategy("");
00221 
00222    // Create a linear solver based on the forward operator A
00223    RCP<Thyra::LinearOpWithSolveBase<double> > lows =
00224          Thyra::linearOpWithSolve(*lowsFactory, tA);
00225 
00226    // Solve the linear system 
00227    Thyra::SolveStatus<double> status; 
00228    status = Thyra::solve<double>(*lows, Thyra::NOTRANS, *tb, tx0.ptr());
00229    status = Thyra::solve<double>(*lows, Thyra::NOTRANS, *tb, tx1.ptr());
00230 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines