Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_InterlacedOpUnitTest.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include <Teuchos_ConfigDefs.hpp>
12 #include <Teuchos_TimeMonitor.hpp>
13 #include <Teuchos_RCP.hpp>
14 
16 
17 // Stokhos Stochastic Galerkin
18 #include "Stokhos_Epetra.hpp"
21 
22 #ifdef HAVE_MPI
23 #include "Epetra_MpiComm.h"
24 #else
25 #include "Epetra_SerialComm.h"
26 #endif
27 #include "Epetra_CrsGraph.h"
28 #include "Epetra_Map.h"
29 #include "EpetraExt_BlockUtility.h"
30 #include "EpetraExt_RowMatrixOut.h"
31 
32 TEUCHOS_UNIT_TEST(interlaced_op, test)
33 {
34 #ifdef HAVE_MPI
36 #else
38 #endif
39 
40  //int rank = comm->MyPID();
41  int numProc = comm->NumProc();
42 
43  int num_KL = 1;
44  int porder = 5;
45  bool full_expansion = false;
46 
49  Teuchos::RCP<Stokhos::ParallelData> sg_parallel_data;
51  {
52  if(full_expansion)
53  Cijk = basis->computeTripleProductTensor();
54  else
55  Cijk = basis->computeLinearTripleProductTensor();
56 
57  Teuchos::ParameterList parallelParams;
58  parallelParams.set("Number of Spatial Processors", numProc);
59  sg_parallel_data = Teuchos::rcp(new Stokhos::ParallelData(basis, Cijk, comm,
60  parallelParams));
61 
63  Cijk));
64  }
65  Teuchos::RCP<const EpetraExt::MultiComm> sg_comm = sg_parallel_data->getMultiComm();
66 
67  // determinstic PDE graph
68  Teuchos::RCP<Epetra_Map> determRowMap = Teuchos::rcp(new Epetra_Map(-1,10,0,*comm));
69  Teuchos::RCP<Epetra_CrsGraph> determGraph = Teuchos::rcp(new Epetra_CrsGraph(Copy,*determRowMap,1));
70  for(int row=0;row<determRowMap->NumMyElements();row++) {
71  int gid = determRowMap->GID(row);
72  determGraph->InsertGlobalIndices(gid,1,&gid);
73  }
74  for(int row=1;row<determRowMap->NumMyElements()-1;row++) {
75  int gid = determRowMap->GID(row);
76  int indices[2] = {gid-1,gid+1};
77  determGraph->InsertGlobalIndices(gid,2,indices);
78  }
79  determGraph->FillComplete();
80 
82  params->set("Scale Operator by Inverse Basis Norms", false);
83  params->set("Include Mean", true);
84  params->set("Only Use Linear Terms", false);
85 
87  Teuchos::rcp(new Stokhos::EpetraSparse3Tensor(basis,Cijk,sg_comm));
89  Teuchos::rcp(new Stokhos::EpetraOperatorOrthogPoly(basis, epetraCijk->getStochasticRowMap(), determRowMap, determRowMap, sg_comm));
90  for(int i=0; i<W_sg_blocks->size(); i++) {
92  crsMat->PutScalar(1.0 + i);
93  W_sg_blocks->setCoeffPtr(i,crsMat); // allocate a bunch of matrices
94  }
95 
97  Teuchos::rcp(EpetraExt::BlockUtility::GenerateBlockMap(
98  *determRowMap, *(epetraCijk->getStochasticRowMap()),
99  *(epetraCijk->getMultiComm())));
100 
101  // build an interlaced operator (object under test) and a benchmark
102  // fully assembled operator
104 
105  Stokhos::InterlacedOperator op(sg_comm,basis,epetraCijk,determGraph,params);
106  op.PutScalar(0.0);
107  op.setupOperator(W_sg_blocks);
108 
109  Stokhos::FullyAssembledOperator full_op(sg_comm,basis,epetraCijk,determGraph,sg_map,sg_map,params);
110  full_op.PutScalar(0.0);
111  full_op.setupOperator(W_sg_blocks);
112 
113  // here we test interlaced operator against the fully assembled operator
115  bool result = true;
116  for(int i=0;i<100;i++) {
117  // build vector for fully assembled operator (blockwise)
119  Teuchos::rcp(new Stokhos::EpetraVectorOrthogPoly(basis,epetraCijk->getStochasticRowMap(),determRowMap,epetraCijk->getMultiComm()));
121  Teuchos::rcp(new Stokhos::EpetraVectorOrthogPoly(basis,epetraCijk->getStochasticRowMap(),determRowMap,epetraCijk->getMultiComm()));
122  Teuchos::RCP<Epetra_Vector> x_vec_blocked = x_vec_blocks->getBlockVector();
123  Teuchos::RCP<Epetra_Vector> f_vec_blocked = f_vec_blocks->getBlockVector();
124  x_vec_blocked->Random(); // build an initial vector
125  f_vec_blocked->PutScalar(0.0);
126 
127  // build interlaced vectors
128  Teuchos::RCP<Epetra_Vector> x_vec_inter = Teuchos::rcp(new Epetra_Vector(op.OperatorDomainMap()));
129  Teuchos::RCP<Epetra_Vector> f_vec_inter = Teuchos::rcp(new Epetra_Vector(op.OperatorRangeMap()));
130  Teuchos::RCP<Epetra_Vector> f_vec_blk_inter = Teuchos::rcp(new Epetra_Vector(op.OperatorRangeMap()));
131  Stokhos::SGModelEvaluator_Interlaced::copyToInterlacedVector(*x_vec_blocks,*x_vec_inter); // copy random x to
132  f_vec_inter->PutScalar(0.0);
133 
134  full_op.Apply(*x_vec_blocked,*f_vec_blocked);
135  op.Apply(*x_vec_inter,*f_vec_inter);
136 
137  // copy blocked action to interlaced for comparison
138  Stokhos::SGModelEvaluator_Interlaced::copyToInterlacedVector(*f_vec_blocks,*f_vec_blk_inter);
139 
140  // compute norm
141  double error = 0.0;
142  double true_norm = 0.0;
143  f_vec_blk_inter->NormInf(&true_norm);
144  f_vec_blk_inter->Update(-1.0,*f_vec_inter,1.0);
145  f_vec_blk_inter->NormInf(&error);
146 
147  out << "rel error = " << error/true_norm << " ( " << true_norm << " ), ";
148  result &= (error/true_norm < 1e-14);
149  }
150  out << std::endl;
151 
152  TEST_ASSERT(result);
153 }
#define TEST_ASSERT(v1)
void setCoeffPtr(ordinal_type i, const Teuchos::RCP< coeff_type > &c)
Set coefficient i to c.
A container class storing an orthogonal polynomial whose coefficients are vectors, operators, or in general any type that would have an expensive copy constructor.
Teuchos::RCP< const EpetraExt::MultiComm > getMultiComm() const
Get global comm.
virtual void setupOperator(const Teuchos::RCP< Stokhos::EpetraOperatorOrthogPoly > &poly)
Setup operator.
Teuchos::RCP< EpetraExt::BlockVector > getBlockVector()
Get block vector.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
int InsertGlobalIndices(int_type GlobalRow, int NumIndices, int_type *Indices)
int PutScalar(double ScalarConstant)
int NumMyElements() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const Epetra_BlockMap > getStochasticRowMap() const
Get stochastic row map.
A container class storing an orthogonal polynomial whose coefficients are vectors, operators, or in general any type that would have an expensive copy constructor.
int GID(int LID) const
virtual int Apply(const Epetra_MultiVector &Input, Epetra_MultiVector &Result) const
Wrap Apply() to add a timer.
Teuchos::RCP< const EpetraExt::MultiComm > getMultiComm() const
Get global comm.
std::string error
An Epetra operator representing the block stochastic Galerkin operator generated by fully assembling ...
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< int, double > > buildBasis(int num_KL, int porder)
virtual int NumProc() const =0
TEUCHOS_UNIT_TEST(tAdaptivityManager, test_interface)
An Epetra operator representing the block stochastic Galerkin operator generated by fully assembling ...
ordinal_type size() const
Return size.
static void copyToInterlacedVector(const Stokhos::EpetraVectorOrthogPoly &x_sg, Epetra_Vector &x)