Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
GraphModel2ndAdjsFromAdjs.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 //
11 // Basic testing of Zoltan2::PamgenMeshAdapter
12 
13 #include <Zoltan2_GraphModel.hpp>
14 #include <Zoltan2_ModelHelpers.hpp>
16 
17 // Teuchos includes
18 #include "Teuchos_XMLParameterListHelpers.hpp"
19 
20 // Pamgen includes
21 #include "create_inline_mesh.h"
22 
23 using Teuchos::RCP;
24 
25 /*********************************************************/
26 /* Typedefs */
27 /*********************************************************/
29 
30 
31 
32 /*****************************************************************************/
33 /******************************** MAIN ***************************************/
34 /*****************************************************************************/
35 
36 int main(int narg, char *arg[]) {
37 
38  Tpetra::ScopeGuard tscope(&narg, &arg);
39  Teuchos::RCP<const Teuchos::Comm<int> > CommT = Tpetra::getDefaultComm();
40 
41  int me = CommT->getRank();
42  int numProcs = CommT->getSize();
43 
44  /***************************************************************************/
45  /*************************** GET XML INPUTS ********************************/
46  /***************************************************************************/
47 
48  // default values for command-line arguments
49  std::string xmlMeshInFileName("Poisson.xml");
50 
51  // Read run-time options.
52  Teuchos::CommandLineProcessor cmdp (false, false);
53  cmdp.setOption("xmlfile", &xmlMeshInFileName,
54  "XML file with PamGen specifications");
55  cmdp.parse(narg, arg);
56 
57  // Read xml file into parameter list
58  Teuchos::ParameterList inputMeshList;
59 
60  if(xmlMeshInFileName.length()) {
61  if (me == 0) {
62  std::cout << "\nReading parameter list from the XML file \""
63  <<xmlMeshInFileName<<"\" ...\n\n";
64  }
65  Teuchos::updateParametersFromXmlFile(xmlMeshInFileName,
66  Teuchos::inoutArg(inputMeshList));
67  if (me == 0) {
68  inputMeshList.print(std::cout,2,true,true);
69  std::cout << "\n";
70  }
71  }
72  else {
73  std::cout << "Cannot read input file: " << xmlMeshInFileName << "\n";
74  return 5;
75  }
76 
77  // Get pamgen mesh definition
78  std::string meshInput = Teuchos::getParameter<std::string>(inputMeshList,
79  "meshInput");
80 
81  /***************************************************************************/
82  /********************** GET CELL TOPOLOGY **********************************/
83  /***************************************************************************/
84 
85  // Get dimensions
86  int dim = 3;
87 
88  /***************************************************************************/
89  /***************************** GENERATE MESH *******************************/
90  /***************************************************************************/
91 
92  if (me == 0) std::cout << "Generating mesh ... \n\n";
93 
94  // Generate mesh with Pamgen
95  long long maxInt = std::numeric_limits<long long>::max();
96  Create_Pamgen_Mesh(meshInput.c_str(), dim, me, numProcs, maxInt);
97 
98  // Creating mesh adapter
99  if (me == 0) std::cout << "Creating mesh adapter ... \n\n";
100 
101  typedef Zoltan2::PamgenMeshAdapter<basic_user_t> inputAdapter_t;
103 
104  inputAdapter_t ia(*CommT, "region");
105  inputAdapter_t ia2(*CommT, "vertex");
106  inputAdapter_t::gno_t const *adjacencyIds=NULL;
107  inputAdapter_t::offset_t const *offsets=NULL;
108  Teuchos::ArrayRCP<inputAdapter_t::offset_t> moffsets;
109  Teuchos::ArrayRCP<inputAdapter_t::gno_t> madjacencyIds;
110  ia.print(me);
111 
112  if (me == 0) std::cout << "REGION-BASED TEST" << std::endl;
113  Zoltan2::MeshEntityType primaryEType = ia.getPrimaryEntityType();
114  Zoltan2::MeshEntityType adjEType = ia.getAdjacencyEntityType();
115  Zoltan2::MeshEntityType secondAdjEType = ia.getSecondAdjacencyEntityType();
116  RCP<const base_adapter_t> baseInputAdapter;
117  RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment(CommT));
118  std::bitset<Zoltan2::NUM_MODEL_FLAGS> modelFlags;
119 
120  if (ia.availAdjs(primaryEType, adjEType)) {
121  // Create a GraphModel based on this input data.
122 
123  if (me == 0) std::cout << " Creating GraphModel" << std::endl;
124 
125  baseInputAdapter = (rcp(dynamic_cast<const base_adapter_t *>(&ia), false));
126 
127  Zoltan2::GraphModel<base_adapter_t> graphModel(baseInputAdapter, env,
128  CommT, modelFlags);
129 
130  if (me == 0)
131  std::cout << " Calling get2ndAdjsViewFromAdjs" << std::endl;
132  Zoltan2::get2ndAdjsViewFromAdjs(baseInputAdapter, graphModel.getComm(),
133  primaryEType,
134  secondAdjEType, moffsets, madjacencyIds);
135 
136  if (me == 0) std::cout << " Checking results" << std::endl;
137  if (ia.avail2ndAdjs(primaryEType, secondAdjEType)) {
138  ia.get2ndAdjsView(primaryEType, secondAdjEType, offsets, adjacencyIds);
139  }
140  else{
141  std::cout << "2nd adjacencies not available; cannot check results"
142  << std::endl;
143  return 2;
144  }
145 
146  for (size_t telct = 0; telct < ia.getLocalNumOf(primaryEType); telct++) {
147  if (offsets[telct+1]-offsets[telct]!=moffsets[telct+1]-moffsets[telct]) {
148  std::cout << "Number of adjacencies do not match" << std::endl;
149  return 3;
150  }
151 
152  for (inputAdapter_t::offset_t j=moffsets[telct]; j<moffsets[telct+1]; j++) {
153  ssize_t in_list = -1;
154 
155  for (inputAdapter_t::offset_t k=offsets[telct]; k<offsets[telct+1]; k++) {
156  if (adjacencyIds[k] == madjacencyIds[j]) {
157  in_list = k;
158  break;
159  }
160  }
161 
162  if (in_list < 0) {
163  std::cout << "Adjacency missing" << std::endl;
164  return 4;
165  }
166  }
167  }
168  }
169  else{
170  std::cout << "Adjacencies not available" << std::endl;
171  return 1;
172  }
173 
174  if (me == 0) std::cout << "VERTEX-BASED TEST" << std::endl;
175  primaryEType = ia2.getPrimaryEntityType();
176  adjEType = ia2.getAdjacencyEntityType();
177  secondAdjEType = ia2.getSecondAdjacencyEntityType();
178 
179  if (ia2.availAdjs(primaryEType, adjEType)) {
180  if (ia2.avail2ndAdjs(primaryEType, secondAdjEType)) {
181  ia2.get2ndAdjsView(primaryEType, secondAdjEType, offsets, adjacencyIds);
182  }
183  else{
184  std::cout << "2nd adjacencies not available" << std::endl;
185  return 2;
186  }
187 
188  // Create a GraphModel based on this input data.
189 
190  if (me == 0) std::cout << " Creating GraphModel" << std::endl;
191 
192  baseInputAdapter = (rcp(dynamic_cast<const base_adapter_t *>(&ia2),false));
193 
194  Zoltan2::GraphModel<base_adapter_t> graphModel2(baseInputAdapter, env,
195  CommT, modelFlags);
196 
197  if (me == 0)
198  std::cout << " Calling get2ndAdjsViewFromAdjs" << std::endl;
199  Zoltan2::get2ndAdjsViewFromAdjs(baseInputAdapter, graphModel2.getComm(),
200  primaryEType,
201  secondAdjEType, moffsets, madjacencyIds);
202 
203  if (me == 0) std::cout << " Checking results" << std::endl;
204 
205  for (size_t tnoct = 0; tnoct < ia2.getLocalNumOf(primaryEType); tnoct++) {
206  if (offsets[tnoct+1]-offsets[tnoct]!=moffsets[tnoct+1]-moffsets[tnoct]) {
207  std::cout << "Number of adjacencies do not match" << std::endl;
208  return 3;
209  }
210 
211  for (inputAdapter_t::offset_t j=moffsets[tnoct]; j<moffsets[tnoct+1]; j++) {
212  ssize_t in_list = -1;
213 
214  for (inputAdapter_t::offset_t k=offsets[tnoct]; k<offsets[tnoct+1]; k++) {
215  if (adjacencyIds[k] == madjacencyIds[j]) {
216  in_list = k;
217  break;
218  }
219  }
220 
221  if (in_list < 0) {
222  std::cout << "Adjacency missing" << std::endl;
223  return 4;
224  }
225  }
226  }
227  }
228  else{
229  std::cout << "Adjacencies not available" << std::endl;
230  return 1;
231  }
232 
233  // delete mesh
234  if (me == 0) std::cout << "Deleting the mesh ... \n\n";
235 
236  Delete_Pamgen_Mesh();
237 
238  if (me == 0)
239  std::cout << "PASS" << std::endl;
240 
241  return 0;
242 }
243 /*****************************************************************************/
244 /********************************* END MAIN **********************************/
245 /*****************************************************************************/
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
void get2ndAdjsViewFromAdjs(const Teuchos::RCP< const MeshAdapter< User > > &ia, const RCP< const Comm< int > > comm, Zoltan2::MeshEntityType sourcetarget, Zoltan2::MeshEntityType through, Teuchos::ArrayRCP< typename MeshAdapter< User >::offset_t > &offsets, Teuchos::ArrayRCP< typename MeshAdapter< User >::gno_t > &adjacencyIds)
Zoltan2::BasicUserTypes basic_user_t
map_t::global_ordinal_type gno_t
Definition: mapRemotes.cpp:27
A simple class that can be the User template argument for an InputAdapter.
int main(int narg, char **arg)
Definition: coloring1.cpp:164
Defines the PamgenMeshAdapter class.
Defines helper functions for use in the models.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
GraphModel defines the interface required for graph models.
MeshEntityType
Enumerate entity types for meshes: Regions, Faces, Edges, or Vertices.
Defines the GraphModel interface.
const RCP< const Comm< int > > getComm()
Return the communicator used by the model.
This class represents a mesh.