Zoltan2
PamgenMeshInput.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
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 Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 //
46 // Basic testing of Zoltan2::PamgenMeshAdapter
47 
50 
51 // Teuchos includes
52 #include "Teuchos_XMLParameterListHelpers.hpp"
53 
54 // Pamgen includes
55 #include "create_inline_mesh.h"
56 
57 using namespace std;
58 using Teuchos::RCP;
59 
60 /*********************************************************/
61 /* Typedefs */
62 /*********************************************************/
63 //Tpetra typedefs
64 typedef Tpetra::DefaultPlatform::DefaultPlatformType Platform;
65 typedef Tpetra::MultiVector<double, int, int> tMVector_t;
66 
67 
68 
69 /*****************************************************************************/
70 /******************************** MAIN ***************************************/
71 /*****************************************************************************/
72 
73 int main(int narg, char *arg[]) {
74 
75  Teuchos::GlobalMPISession mpiSession(&narg, &arg,0);
76  Platform &platform = Tpetra::DefaultPlatform::getDefaultPlatform();
77  RCP<const Teuchos::Comm<int> > CommT = platform.getComm();
78 
79  int me = CommT->getRank();
80  int numProcs = CommT->getSize();
81 
82  /***************************************************************************/
83  /*************************** GET XML INPUTS ********************************/
84  /***************************************************************************/
85 
86  // default values for command-line arguments
87  std::string xmlMeshInFileName("Poisson.xml");
88 
89  // Read run-time options.
90  Teuchos::CommandLineProcessor cmdp (false, false);
91  cmdp.setOption("xmlfile", &xmlMeshInFileName,
92  "XML file with PamGen specifications");
93  cmdp.parse(narg, arg);
94 
95  // Read xml file into parameter list
96  Teuchos::ParameterList inputMeshList;
97 
98  if(xmlMeshInFileName.length()) {
99  if (me == 0) {
100  cout << "\nReading parameter list from the XML file \""
101  <<xmlMeshInFileName<<"\" ...\n\n";
102  }
103  Teuchos::updateParametersFromXmlFile(xmlMeshInFileName,
104  Teuchos::inoutArg(inputMeshList));
105  if (me == 0) {
106  inputMeshList.print(cout,2,true,true);
107  cout << "\n";
108  }
109  }
110  else {
111  cout << "Cannot read input file: " << xmlMeshInFileName << "\n";
112  return 5;
113  }
114 
115  // Get pamgen mesh definition
116  std::string meshInput = Teuchos::getParameter<std::string>(inputMeshList,
117  "meshInput");
118 
119  /***************************************************************************/
120  /********************** GET CELL TOPOLOGY **********************************/
121  /***************************************************************************/
122 
123  // Get dimensions
124  int dim = 3;
125 
126  /***************************************************************************/
127  /***************************** GENERATE MESH *******************************/
128  /***************************************************************************/
129 
130  if (me == 0) cout << "Generating mesh ... \n\n";
131 
132  // Generate mesh with Pamgen
133  long long maxInt = 9223372036854775807LL;
134  Create_Pamgen_Mesh(meshInput.c_str(), dim, me, numProcs, maxInt);
135 
136  // Creating mesh adapter
137  if (me == 0) cout << "Creating mesh adapter ... \n\n";
138 
139  typedef Zoltan2::PamgenMeshAdapter<tMVector_t> inputAdapter_t;
140 
141  inputAdapter_t ia(*CommT, "region");
142  inputAdapter_t ia2(*CommT, "vertex");
143  inputAdapter_t::gno_t const *adjacencyIds=NULL;
144  inputAdapter_t::lno_t const *offsets=NULL;
145  ia.print(me);
146 
147  // Exercise the componentMetrics on the input; make sure the adapter works
148  {
150  std::cout << me << " Region-based: Number of components on processor = "
151  << cc.getNumComponents() << std::endl;
152  std::cout << me << " Region-based: Max component size on processor = "
153  << cc.getMaxComponentSize() << std::endl;
154  std::cout << me << " Region-based: Min component size on processor = "
155  << cc.getMinComponentSize() << std::endl;
156  std::cout << me << " Region-based: Avg component size on processor = "
157  << cc.getAvgComponentSize() << std::endl;
158  }
159 
160  Zoltan2::MeshEntityType primaryEType = ia.getPrimaryEntityType();
161  Zoltan2::MeshEntityType adjEType = ia.getAdjacencyEntityType();
162 
163  int dimension, num_nodes, num_elem;
164  int error = 0;
165  char title[100];
166  int exoid = 0;
167  int num_elem_blk, num_node_sets, num_side_sets;
168  error += im_ex_get_init(exoid, title, &dimension, &num_nodes, &num_elem,
169  &num_elem_blk, &num_node_sets, &num_side_sets);
170 
171  int *element_num_map = new int [num_elem];
172  error += im_ex_get_elem_num_map(exoid, element_num_map);
173 
174  inputAdapter_t::gno_t *node_num_map = new int [num_nodes];
175  error += im_ex_get_node_num_map(exoid, node_num_map);
176 
177  int *elem_blk_ids = new int [num_elem_blk];
178  error += im_ex_get_elem_blk_ids(exoid, elem_blk_ids);
179 
180  int *num_nodes_per_elem = new int [num_elem_blk];
181  int *num_attr = new int [num_elem_blk];
182  int *num_elem_this_blk = new int [num_elem_blk];
183  char **elem_type = new char * [num_elem_blk];
184  int **connect = new int * [num_elem_blk];
185 
186  for(int i = 0; i < num_elem_blk; i++){
187  elem_type[i] = new char [MAX_STR_LENGTH + 1];
188  error += im_ex_get_elem_block(exoid, elem_blk_ids[i], elem_type[i],
189  (int*)&(num_elem_this_blk[i]),
190  (int*)&(num_nodes_per_elem[i]),
191  (int*)&(num_attr[i]));
192  delete[] elem_type[i];
193  }
194 
195  delete[] elem_type;
196  elem_type = NULL;
197  delete[] num_attr;
198  num_attr = NULL;
199 
200  for(int b = 0; b < num_elem_blk; b++) {
201  connect[b] = new int [num_nodes_per_elem[b]*num_elem_this_blk[b]];
202  error += im_ex_get_elem_conn(exoid, elem_blk_ids[b], connect[b]);
203  }
204 
205  delete[] elem_blk_ids;
206  elem_blk_ids = NULL;
207 
208  int telct = 0;
209 
210  if (ia.availAdjs(primaryEType, adjEType)) {
211  ia.getAdjsView(primaryEType, adjEType, offsets, adjacencyIds);
212 
213  if ((int)ia.getLocalNumOf(primaryEType) != num_elem) {
214  cout << "Number of elements do not match\n";
215  return 2;
216  }
217 
218  for (int b = 0; b < num_elem_blk; b++) {
219  for (int i = 0; i < num_elem_this_blk[b]; i++) {
220  if (offsets[telct + 1] - offsets[telct] != num_nodes_per_elem[b]) {
221  std::cout << "Number of adjacencies do not match" << std::endl;
222  return 3;
223  }
224 
225  for (int j = 0; j < num_nodes_per_elem[b]; j++) {
226  ssize_t in_list = -1;
227 
228  for(inputAdapter_t::lno_t k=offsets[telct];k<offsets[telct+1];k++) {
229  if(adjacencyIds[k] ==
230  node_num_map[connect[b][i*num_nodes_per_elem[b]+j]-1]) {
231  in_list = k;
232  break;
233  }
234  }
235 
236  if (in_list < 0) {
237  std::cout << "Adjacency missing" << std::endl;
238  return 4;
239  }
240  }
241 
242  ++telct;
243  }
244  }
245 
246  if (telct != num_elem) {
247  cout << "Number of elements do not match\n";
248  return 2;
249  }
250  }
251  else{
252  std::cout << "Adjacencies not available" << std::endl;
253  return 1;
254  }
255 
256  ia2.print(me);
257  {
259  std::cout << me << " Vertex-based: Number of components on processor = "
260  << cc.getNumComponents() << std::endl;
261  std::cout << me << " Vertex-based: Max component size on processor = "
262  << cc.getMaxComponentSize() << std::endl;
263  std::cout << me << " Vertex-based: Min component size on processor = "
264  << cc.getMinComponentSize() << std::endl;
265  std::cout << me << " Vertex-based: Avg component size on processor = "
266  << cc.getAvgComponentSize() << std::endl;
267  }
268 
269  primaryEType = ia2.getPrimaryEntityType();
270  adjEType = ia2.getAdjacencyEntityType();
271 
272  if (ia2.availAdjs(primaryEType, adjEType)) {
273  ia2.getAdjsView(primaryEType, adjEType, offsets, adjacencyIds);
274 
275  if ((int)ia2.getLocalNumOf(primaryEType) != num_nodes) {
276  cout << "Number of nodes do not match\n";
277  return 2;
278  }
279 
280  telct = 0;
281  int *num_adj = new int[num_nodes];
282 
283  for (int i = 0; i < num_nodes; i++) {
284  num_adj[i] = 0;
285  }
286 
287  for (int b = 0; b < num_elem_blk; b++) {
288  for (int i = 0; i < num_elem_this_blk[b]; i++) {
289  for (int j = 0; j < num_nodes_per_elem[b]; j++) {
290  ssize_t in_list = -1;
291  ++num_adj[connect[b][i * num_nodes_per_elem[b] + j] - 1];
292 
293  for(inputAdapter_t::lno_t k =
294  offsets[connect[b][i * num_nodes_per_elem[b] + j] - 1];
295  k < offsets[connect[b][i * num_nodes_per_elem[b] + j]]; k++) {
296  if(adjacencyIds[k] == element_num_map[telct]) {
297  in_list = k;
298  break;
299  }
300  }
301 
302  if (in_list < 0) {
303  std::cout << "Adjacency missing" << std::endl;
304  return 4;
305  }
306  }
307 
308  ++telct;
309  }
310  }
311 
312  for (int i = 0; i < num_nodes; i++) {
313  if (offsets[i + 1] - offsets[i] != num_adj[i]) {
314  std::cout << "Number of adjacencies do not match" << std::endl;
315  return 3;
316  }
317  }
318 
319  delete[] num_adj;
320  num_adj = NULL;
321  }
322  else{
323  std::cout << "Adjacencies not available" << std::endl;
324  return 1;
325  }
326 
327  // delete mesh
328  if (me == 0) cout << "Deleting the mesh ... \n\n";
329 
330  Delete_Pamgen_Mesh();
331  // clean up - reduce the result codes
332 
333  // make sure another process doesn't mangle the PASS output or the test will read as a fail when it should pass
334  std::cout << std::flush;
335  CommT->barrier();
336  if (me == 0)
337  std::cout << "PASS" << std::endl;
338 
339  return 0;
340 }
341 /*****************************************************************************/
342 /********************************* END MAIN **********************************/
343 /*****************************************************************************/
Defines the PamgenMeshAdapter class.
Tpetra::DefaultPlatform::DefaultPlatformType Platform
Tpetra::DefaultPlatform::DefaultPlatformType Platform
int main(int narg, char *arg[])
MeshEntityType
Enumerate entity types for meshes: Regions, Faces, Edges, or Vertices.
Tpetra::MultiVector< double, int, int > tMVector_t
Identify and compute the number of connected components in a processor&#39;s input Note that this routine...
This class represents a mesh.