Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_BasisInteractionGraphUnitTest.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Stokhos Package
6 // Copyright (2009) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 #include <Teuchos_ConfigDefs.hpp>
46 #include <Teuchos_TimeMonitor.hpp>
47 #include <Teuchos_RCP.hpp>
48 
50 
51 // Stokhos Stochastic Galerkin
54 #include "Stokhos_ParallelData.hpp"
55 
56 #ifdef HAVE_MPI
57  #include "Epetra_MpiComm.h"
58  #include "mpi.h"
59 #else
60  #include "Epetra_SerialComm.h"
61 #endif
62 
63 TEUCHOS_UNIT_TEST(basis_interaction_graph, test_square)
64 {
65  #ifdef HAVE_MPI
67  #else
69  #endif
70 
71  int numProc = comm->NumProc();
72 
73  int num_KL = 3;
74  int porder = 3;
75 
77  Teuchos::RCP<Stokhos::Sparse3Tensor<int,double> > Cijk = basis->computeTripleProductTensor();
78  Teuchos::RCP<Stokhos::ParallelData> sg_parallel_data;
79 
80  {
81  Teuchos::ParameterList parallelParams;
82  parallelParams.set("Number of Spatial Processors", numProc);
83  sg_parallel_data = Teuchos::rcp(new Stokhos::ParallelData(basis, Cijk, comm,
84  parallelParams));
85  }
86  Teuchos::RCP<const EpetraExt::MultiComm> sg_comm = sg_parallel_data->getMultiComm();
88  Teuchos::rcp(new Stokhos::EpetraSparse3Tensor(basis,Cijk,sg_comm));
89 
90  Stokhos::BasisInteractionGraph big(*basis);
91 
92  // for grins print out the graph
93  out << "Size = " << big.rowCount() << " x " << big.colCount() << std::endl;
94 
95  for(std::size_t i=0;i<big.rowCount();i++) {
96  for(std::size_t j=0;j<big.colCount();j++)
97  out << " " << (big(i,j) ? "*" : " " ) << " ";
98  out << std::endl;
99  }
100  out << std::endl;
101 
102  // check the graph for correctness
104  TEST_EQUALITY(graph->NumMyRows(),graph->NumGlobalRows());
105  TEST_EQUALITY(int(big.rowCount()),graph->NumMyRows());
106  TEST_EQUALITY(int(big.colCount()),graph->NumMyCols());
107 
108  for(int i=0;i<graph->NumGlobalRows();i++) {
109  bool rowPassed = true;
110  int count = 0;
111  std::vector<bool> row(graph->NumGlobalRows(),false);
112  std::vector<int> activeRow(graph->NumGlobalRows());
113  graph->ExtractGlobalRowCopy(i,graph->NumGlobalRows(),count,&activeRow[0]);
114 
115  // get truth graph
116  for(int j=0;j<count;j++)
117  row[activeRow[j]] = true;
118 
119  // check active row
120  for(std::size_t j=0;j<row.size();j++)
121  rowPassed = rowPassed && (row[j]==big(i,j));
122 
123  TEST_ASSERT(rowPassed);
124  }
125 }
126 
127 TEUCHOS_UNIT_TEST(basis_interaction_graph, test_isotropic_rect)
128 {
129  #ifdef HAVE_MPI
130  Teuchos::RCP<const Epetra_Comm> comm = Teuchos::rcp(new Epetra_MpiComm(MPI_COMM_WORLD));
131  #else
133  #endif
134 
135  int num_KL = 2;
136  int porder = 3;
137 
141 
142  out << "Master Array Basis = \n";
143  for(int i=0;i<masterBasis->size();i++) {
144  Stokhos::MultiIndex<int> masterArray = masterBasis->term(i);
145  for(int i=0;i<num_KL;i++) {
146  out << masterArray[i] << " ";
147  }
148  out << std::endl;
149  }
150 
151  out << "Row Array Basis = \n";
152  for(int i=0;i<rowBasis->size();i++) {
153  Stokhos::MultiIndex<int> rowArray = rowBasis->term(i);
154  for(int i=0;i<num_KL;i++) {
155  out << rowArray[i] << " ";
156  }
157  out << std::endl;
158  }
159 
160  Stokhos::BasisInteractionGraph masterBig(*masterBasis);
161  Stokhos::BasisInteractionGraph rectBig(*masterBasis,*rowBasis,*colBasis);
162 
163  out << "rowBasis.size = " << rowBasis->size() << std::endl;
164  out << "colBasis.size = " << colBasis->size() << std::endl;
165 
166  // for grins print out the graph
167  out << "Size = " << rectBig.rowCount() << " x " << rectBig.colCount() << std::endl;
168  for(std::size_t i=0;i<rectBig.rowCount();i++) {
169  for(std::size_t j=0;j<rectBig.colCount();j++)
170  out << " " << (rectBig(i,j) ? "*" : " " ) << " ";
171  out << std::endl;
172  }
173  out << std::endl;
174 
175  out << "Size = " << masterBig.rowCount() << " x " << masterBig.colCount() << std::endl;
176  for(std::size_t i=0;i<masterBig.rowCount();i++) {
177  for(std::size_t j=0;j<masterBig.colCount();j++)
178  out << " " << (masterBig(i,j) ? "*" : " " ) << " ";
179  out << std::endl;
180  }
181  out << std::endl;
182 
183  // loop over rectangle graph making sure it matches with the master graph
184  bool graphs_match = true;
185  for(std::size_t i=0;i<rectBig.rowCount();i++) {
186  std::size_t masterI = masterBasis->index(rowBasis->term(i));
187  for(std::size_t j=0;j<rectBig.colCount();j++) {
188  std::size_t masterJ = masterBasis->index(colBasis->term(j));
189  graphs_match &= (rectBig(i,j)==masterBig(masterI,masterJ));
190  }
191  }
192 
193  TEST_ASSERT(graphs_match);
194 }
int NumGlobalRows() const
#define TEST_ASSERT(v1)
Teuchos::RCP< const EpetraExt::MultiComm > getMultiComm() const
Get global comm.
std::size_t colCount() const
What is the number of columns.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Teuchos::RCP< const Epetra_CrsGraph > getStochasticGraph() const
Get stochastic graph.
int NumMyRows() const
int NumMyCols() const
A multidimensional index.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< int, double > > buildBasis(int num_KL, int porder)
std::size_t rowCount() const
What is the number of rows.
virtual int NumProc() const =0
int ExtractGlobalRowCopy(int_type Row, int LenOfIndices, int &NumIndices, int_type *Indices) const
#define TEST_EQUALITY(v1, v2)
TEUCHOS_UNIT_TEST(tAdaptivityManager, test_interface)