Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
ReadRTT.hpp
Go to the documentation of this file.
1 /**
2  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
3  * storing and accessing finite element mesh data.
4  *
5  * Copyright 2004 Sandia Corporation. Under the terms of Contract
6  * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
7  * retains certain rights in this software.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  */
15 
16 //-------------------------------------------------------------------------
17 // Filename : ReadRTT.hpp
18 //
19 // Purpose : RTT file reader
20 //
21 // Creator : Andrew Davis
22 //
23 // Date : 02/2014
24 //
25 //-------------------------------------------------------------------------
26 
27 /**
28  * The RTT file format is used by the Attila deterministic radiation
29  * transport code. The specific mesh format can be found in Chapter 9
30  * of the Attila manual. The format is defined by xml like, block/end block
31  * type syntax. The implementation at the time of writing supports a subset
32  * of the whole format, and even Attila does not support the entireity of
33  * its own mesh format.
34  *
35  * The mesh contains several features, that as a whole allow the conversion
36  * from the RTT format, to a DAGMC geometry and a Tet Mesh for tallying.
37  *
38  * Sides - Defines the 6 boundary condtions for top, bottom, front, back
39  * left and right, as well as internal and external.
40  *---------------------------------------------------------------------
41  * Faces - Logically equivalent to surfaces in DAGMC, containers for triangles, includes
42  * the definition of the sense of the faces with respect to the Cells (volumes)
43  * which bound it.
44  *
45  * The face syntax looks like
46  *
47  * 1 (+)Pyrex@14
48  *
49  * This means Face (surface) 1 is used to define the insde of the Pyrex cell only
50  *
51  * 75 (+)Pyrex/(-)Fuel30@25
52  *
53  * This means Face (surface) 75 is used by both Cell Pyrex and Cell Fuel 30,
54  * the + and - signs refer to the sense, i.e. the inside sense defines the Pyrex and
55  * the outside sense defines the Fuel.
56  *---------------------------------------------------------------------
57  * Cells - Entityset like coillections of tetrahedra which define contiguous material properties
58  *
59  * cell_flags
60  * 1 REGIONS
61  * 1 Pyrex
62  * end_cell_flags
63  *
64  * Defines that there is 1 region called Pyrex
65  *---------------------------------------------------------------------
66  * Nodes - Defines the vertices for facets and tets, the syntax of which is shown below
67  *
68  * 100 1.8900000000E+03 0.0000000000E+00 5.0000000000E+03 100
69  *
70  * Defines that this is node 100, and has the coordinates 1890.0, 0.0 5000.0 cm
71  **---------------------------------------------------------------------
72  * Side (element) - Triangles
73  *
74  * 1 3 874 132 154 3 6365
75  *
76  * Defines that this is side element 1, it has 3 nodes, 874, 132 and 154,
77  * side ID 3 and surface number 6365
78  *---------------------------------------------------------------------
79  * Cells (element) - Tetrahedra
80  *
81  * 691 4 599 556 1218 1216 2
82  *
83  * Defines that this is tet 691, it has 4 connections to nodes 599, 556,
84  * 1218, 1216 and belongs to cell number 2.
85  *
86  */
87 
88 #ifndef READRTT_HPP
89 #define READRTT_HPP
90 
91 #ifndef IS_BUILDING_MB
92 #error "ReadRTT.hpp isn't supposed to be included into an application"
93 #endif
94 
95 #include <iostream>
96 #include <fstream>
97 #include <sstream>
98 #include <map>
99 #include <vector>
100 
101 #include "moab/Interface.hpp"
102 #include "moab/ReaderIface.hpp"
103 #include "FileTokenizer.hpp"
104 #include "moab/RangeMap.hpp"
105 
106 namespace moab
107 {
108 
109 class ReadUtilIface;
110 class GeomTopoTool;
111 
112 class ReadRTT : public ReaderIface
113 {
114 
115  public:
116  // factory method
117  static ReaderIface* factory( Interface* );
118 
119  // generic overloaded core -> load_file
120  ErrorCode load_file( const char* file_name,
121  const EntityHandle* file_set,
122  const FileOptions& opts,
123  const SubsetList* subset_list = 0,
124  const Tag* file_id_tag = 0 );
125  // constructor
126  ReadRTT( Interface* impl = NULL );
127 
128  // destructor
129  virtual ~ReadRTT();
130 
131  // implementation empty
132  ErrorCode read_tag_values( const char* file_name,
133  const char* tag_name,
134  const FileOptions& opts,
135  std::vector< int >& tag_values_out,
136  const SubsetList* subset_list = 0 );
137 
138  protected:
139  // private functions
140  private:
141  // structure to hold the header data
142  struct headerData
143  {
144  std::string version;
145  std::string title;
146  std::string date;
147  std::string contiguity;
148  };
149 
150  struct dimData
151  {
152  std::string coor_units;
153  std::string prob_time_units;
157 
158  int ndim;
159  int nnodes;
161  std::vector< int > nnode_flags;
163 
164  int nsides;
168  std::vector< int > nside_flags;
170 
171  int ncells;
175  std::vector< int > ncell_flags;
177 
178  void print()
179  {
180  std::cout << "dimData: " << std::endl;
181  std::cout << "coor_units: " << coor_units << std::endl;
182  std::cout << "prob_time_units: " << prob_time_units << std::endl;
183  std::cout << "ncell_defs: " << ncell_defs << std::endl;
184  std::cout << std::endl;
185  std::cout << "Node information: " << std::endl;
186  std::cout << "nnodes_max: " << nnodes_max << std::endl;
187  std::cout << "nsides_max: " << nsides_max << std::endl;
188  std::cout << "ndim: " << ndim << std::endl;
189  std::cout << "nnodes: " << nnodes << std::endl;
190  std::cout << "nnode_flag_types: " << nnode_flag_types << std::endl;
191  std::cout << "nnode_flags: ";
192  for( size_t i = 0; i < nnode_flags.size(); i++ )
193  {
194  std::cout << nnode_flags[i] << " ";
195  }
196  std::cout << std::endl;
197  std::cout << "nnode_data: " << nnode_data << std::endl;
198 
199  std::cout << std::endl;
200  std::cout << "Side information: " << std::endl;
201  std::cout << "nsides: " << nsides << std::endl;
202  std::cout << "nside_types: " << nside_types << std::endl;
203  std::cout << "side_types: " << side_types << std::endl;
204  std::cout << "nside_flag_types: " << nside_flag_types << std::endl;
205  std::cout << "nside_flags: ";
206  for( size_t i = 0; i < nside_flags.size(); i++ )
207  {
208  std::cout << nside_flags[i] << " ";
209  }
210  std::cout << std::endl;
211  std::cout << "nside_data: " << nside_data << std::endl;
212 
213  std::cout << std::endl;
214  std::cout << "Cell information: " << std::endl;
215  std::cout << "ncells: " << ncells << std::endl;
216  std::cout << "ncell_types: " << ncell_types << std::endl;
217  std::cout << "cell_types: " << cell_types << std::endl;
218  std::cout << "ncell_flag_types: " << ncell_flag_types << std::endl;
219  std::cout << "ncell_flags: ";
220  for( size_t i = 0; i < ncell_flags.size(); i++ )
221  {
222  std::cout << ncell_flags[i] << " ";
223  }
224  std::cout << std::endl;
225  std::cout << "ncell_data: " << ncell_data << std::endl;
226  }
227 
228  void validate()
229  {
230  if( nnode_flag_types > 0 && nnode_flag_types != (int)nnode_flags.size() )
231  {
232  std::cerr << "Warning: nnode_flag_types does not match nnode_flags.size()" << std::endl;
233  }
234 
235  if( nside_flag_types > 0 && nside_flag_types != (int)nside_flags.size() )
236  {
237  std::cerr << "Warning: nside_flag_types does not match nside_flags.size()" << std::endl;
238  }
239 
240  if( ncell_flag_types > 0 && ncell_flag_types != (int)ncell_flags.size() )
241  {
242  std::cerr << "Warning: ncell_flag_types does not match ncell_flags.size()" << std::endl;
243  }
244 
245  if( ncell_flag_types > 1 )
246  {
247  std::cerr << "Warning: Additional flag types will not be read" << std::endl;
248  }
249  }
250  };
251 
252  struct cell_def
253  {
254  int id;
255  std::string name;
256  int nnodes;
257  int nsides;
258 
259  std::vector< int > side_type;
260  std::vector< std::vector< int > > sides_nodes;
261  };
262 
263  // structure to hold sense & vol data
264  struct boundary
265  {
266  int sense;
267  std::string name;
268  };
269 
270  // structure to hold side data
271  struct side
272  {
273  int id;
274  int senses[2];
275  std::string names[2];
276  side() : id( 0 )
277  {
278  senses[0] = senses[1] = 0;
279  names[0] = names[1] = "";
280  }
281  };
282 
283  // structure to hold cell data
284  struct cell
285  {
286  int id;
287  std::string name;
288  cell() : id( 0 ), name( "" ) {}
289  };
290 
291  // structure to hold node data
292  struct node
293  {
294  int id;
295  double x, y, z;
296  node() : id( 0 ), x( 0. ), y( 0. ), z( 0. ) {}
297  };
298 
299  // structure to hold facet data
300  struct facet
301  {
302  int id;
303  int connectivity[3];
304  int side_id;
306  facet() : id( 0 ), side_id( 0 ), surface_number( 0 )
307  {
308  for( int k = 0; k < 3; k++ )
309  connectivity[k] = 0;
310  }
311  };
312 
313  // structure to hold tet data
314  struct tet
315  {
316  int id;
317  int type_id;
318  int connectivity[4];
319  std::vector< int > flag_values;
320  // with c++11 we could use tet(): id(0), connectivity({0}), material_number(0) {}
321  tet() : id( 0 )
322  {
323  for( int k = 0; k < 4; k++ )
324  connectivity[k] = 0;
325  }
326  };
327 
328  // structure to hold a subsection of the RTT input
329  typedef std::map< std::string, std::vector< std::string > > rtt_flags;
330  typedef std::map< std::string, std::vector< cell > > rtt_flags_data;
331 
332  /**
333  * generates the topology of the problem from the already read input data, loops over the 2 and
334  * 3 dimension macrodata that exist from the rtt file, sides = dagmc surfaces, cells = dagmc
335  * cells, creates a meshset for each surface and tags with the id number, and similarly makes a
336  * meshset for dagmc cells and tags with the id number. The surfaces are added to the s surface
337  * map, where the key is the surface ID number (1->N) and (cells and surfaces are added to an
338  * dimesional entity map stored in the class
339  *
340  * @param side_data, vector of side data
341  * @param cell_data, vector of vector of cell data
342  * @param tet_data, vector of tet data
343  * @param surface_map, reference to the surface map of data
344  * @param volume_map, reference to the volume map of data
345  *
346  */
347  ErrorCode generate_topology( std::vector< side > side_data,
348  std::vector< cell > cell_data,
349  std::vector< tet > tet_data,
350  std::map< int, EntityHandle >& surface_map,
351  std::map< int, EntityHandle >& volume_map );
352  /**
353  * Generate parent child links to create DAGMC like structure of surface meshsets being children
354  * of parent cell meshsets. By looping over the surfaces (1->N), look in the description of the
355  * cells that are shared by that surface, and then make the surface the child of the parent
356  * volume. The appropriate sense data will be set later
357  *
358  * @param num_ents[4], array containing the number of surfaces, cells, groups etc
359  * @param entity_map[4], vector of maps containing data by dimension
360  * @param side_data, vector of all the side data in the problem
361  * @param cell_data, vector of the cell data in the problem
362  *
363  */
364  void generate_parent_child_links( int num_ents[4],
365  std::vector< EntityHandle > entity_map[4],
366  std::vector< side > side_data,
367  std::vector< cell > cell_data );
368  /**
369  * Sets the appropriate surface senses for each surface in the problem. By looping through all
370  * the surfaces, we determine from the side_data vector, the volume id's that are shared, then
371  * using 1 to mean +ve sense and -1 to mean -ve sense wrt the volume.
372  *
373  * @param num_ents[4], array containing the number of surfaces, cells, groups etc
374  * @param entity_map[4], vector of maps containing data by dimension
375  * @param side_data, vector of all the side data in the problem
376  * @param cell_data, vector of the cell data in the problem
377  *
378  */
379  void set_surface_senses( int num_ents[4],
380  std::vector< EntityHandle > entity_map[4],
381  std::vector< side > side_data,
382  std::vector< cell > cell_data );
383 
384  /**
385  * creates the group data requried for dagmc, reflecting planes, material assignments etc
386  * @param entity_map, vector of vector of entitiy handles for each dimension
387  * @param tet_data, vector of tet data
388  *
389  * @returns moab::ErrorCode
390  */
391  ErrorCode setup_group_data( std::vector< EntityHandle > entity_map[4],
392  std::vector< tet > tet_data,
393  std::map< int, EntityHandle >& volume_map );
394 
395  /**
396  * create a group of a given name, mustkeep track of id
397  * @param group_name, name of the group
398  * @param id, integer id number
399  *
400  * returns the entity handle of the group
401  */
402  EntityHandle create_group( std::string group_name, int id );
403 
404  /** parse the dimensions of the problem from the file header
405  * @param input_file, an open filestream
406  */
407  ErrorCode parse_dims( std::ifstream& input_file );
408 
409  /** parse the cell definition car
410  * @param input_file, an open filestream
411  */
412  ErrorCode read_cell_defs( std::ifstream& input_file );
413 
414  /**
415  * Builds the full MOAB representation of the data, making vertices from coordinates, triangles
416  * from vertices and tets from the same vertices. Tags appropriate to each dataset collection
417  * are applied, triangles are tagged with the surface id and side id they belong to, as well as
418  * tagging the surface with the same data. Tets are similarly tagged only with the Material
419  * number
420  *
421  * @param node_data the node data
422  * @param facet_data, the triangles in the problem
423  * @param tet_data, the tets in the problem
424  * @param surface_map, the map of surface meshset and id numbers
425  *
426  * @return moab::ErrorCode
427  */
428  ErrorCode build_moab( std::vector< node > node_data,
429  std::vector< facet > facet_data,
430  std::vector< tet > tet_data,
431  std::map< int, EntityHandle > surface_map,
432  std::map< int, EntityHandle > volume_map );
433 
434  /**
435  * Add Metadata to the meshset, this includes the version number and contiguity value
436  * @returns moab::ErrorCode
437  */
438  ErrorCode add_metadata( EntityHandle file_set );
439 
440  /**
441  * reads the full set of header data
442  *
443  * @param filename, the file to read the data from
444  *
445  * @return moab::Error code
446  */
447  ErrorCode read_header( const char* filename );
448 
449  /**
450  * Reads the full set of data from the file
451  *
452  * @param filename, the file to read all the data from
453  * @param n_flags, a vector containing the number of flags
454  * @param flag_id, the flag id to read
455  * @param flags, a map for all the flags from the XX_flags section
456  * @param flag_idx, a map for the index of the flags
457  *
458  * @return moab::ErrorCode
459  */
460  ErrorCode read_all_flags( const char* filename,
461  std::vector< int > n_flags,
462  std::string flag_id,
463  rtt_flags& flags,
464  std::map< std::string, int >& flag_idx );
465 
466  /**
467  * Reads the full set of side data from the file
468  *
469  * @param filename, the file to read all the side data from
470  *
471  * @return moab::ErrorCode
472  */
473  ErrorCode read_side_flags( const char* filename );
474 
475  /**
476  * Process the FACES flag from the side_flags section
477  *
478  * @param side_flags, a vector containing all the read side data
479  * @param side_data, a vector containing all the read side data
480  *
481  * @return moab::ErrorCode
482  */
483  ErrorCode side_process_faces( rtt_flags side_flags, std::vector< side >& side_data );
484 
485  /**
486  * Reads the full set of cell data from the file
487  *
488  * @param filename, the file to read all the side data from
489  *
490  * @return moab::ErrorCode
491  */
492  ErrorCode read_cell_flags( const char* filename );
493 
494  /**
495  * Process the standard flag from the cell_flags section
496  *
497  * @param cell_flags, a vector containing all the read side_flag section
498  * @param key, the key to read
499  * @param cell_data, a vector containing all the read cell data
500  *
501  * @return moab::ErrorCode
502  */
503  ErrorCode cell_process_flag( rtt_flags cell_flags, std::string key );
504 
505  /**
506  * Reads the full set of node data from the file
507  *
508  * @param filename, the file to read all the side data from
509  * @param node data, a vector containing all the read node data
510  *
511  * @return moab::ErrorCode
512  */
513  ErrorCode read_nodes( const char* filename, std::vector< node >& node_data );
514 
515  /**
516  * Reads the full set of facet data from the file
517  *
518  * @param filename, the file to read all the side data from
519  * @param facet data, a vector containing all the read facet data
520  *
521  * @return moab::ErrorCode
522  */
523  ErrorCode read_facets( const char* filename, std::vector< facet >& facet_data );
524 
525  /**
526  * Reads the full set of tet data from the file
527  *
528  * @param filename, the file to read all the side data from
529  * @param tet data, a vector containing all the read tet data
530  *
531  * @return moab::ErrorCode
532  */
533  ErrorCode read_tets( const char* filename, std::vector< tet >& tet_data );
534 
535  /**
536  * Reads the header data into a class member structure
537  *
538  * @param input_file, an open filestream
539  *
540  * @return void
541  */
542  ErrorCode get_header_data( std::ifstream& input_file );
543 
544  /**
545  * Reads a single atomic cell data string and populates a cell struct
546  *
547  * @param celldata, a string of read data and
548  *
549  * @return cell, the propulated cell struct
550  */
551  cell get_cell_data( std::string celldata );
552 
553  /**
554  * Reads a single atomic side data string and populates a side struct
555  *
556  * @param sidedata, a string of read data and
557  *
558  * @return side, the propulated side struct
559  */
560  side get_side_data( std::string sidedata );
561 
562  /**
563  * Reads a single atomic node data string and populates a node struct
564  *
565  * @param sidedata, a string of read data and
566  *
567  * @return node, the propulated node struct
568  */
569  node get_node_data( std::string nodedata );
570 
571  /**
572  * Reads a single atomic facet data string and populates a facet struct
573  *
574  * @param facetdata, a string of facet data and
575  *
576  * @return facet, the propulated facet struct
577  */
578  facet get_facet_data( std::string facetdata );
579 
580  /**
581  * Reads a single atomic tet data string and populates a tet struct
582  *
583  * @param tetdata, a string of tet data and
584  *
585  * @return tet, the propulated tet struct
586  */
587  tet get_tet_data( std::string tetdata );
588 
589  /**
590  * @brief Get the material ref flag object
591  *
592  * @return std::string
593  */
594  std::string get_material_ref_flag();
595 
596  /**
597  * @brief Get the volume ref flag object
598  *
599  * @return std::string
600  */
601  std::string get_volume_ref_flag();
602 
603  /**
604  * @brief Get the max name size object
605  *
606  * @param cell_data, vector of cell data
607  * @return int, the max name size
608  */
609  int get_max_name_size( std::vector< cell > cell_data );
610 
611  /**
612  * Splits a string into a vector of substrings delimited by split_char
613  *
614  * @param string_to_split, the string that needs splitting into chunks
615  * @param split_char, the character to split the string with
616  *
617  * @return a vector of strings that are delimited by split_char
618  */
619  std::vector< std::string > split_string( std::string string_to_split, char split_char );
620 
621  /**
622  * Splits an Attila cellname and populates a boundary structure
623  *
624  * @param attila_cellname, string containing the boundary information
625  *
626  * @return a boundary object
627  */
628  boundary split_name( std::string atilla_cellname );
629 
630  /**
631  * Count the number of unique surface numbers in the dataset, also get list of surface numbers
632  * @param side_data, collection of all the side data in the mesh
633  * @param surface_numbers, collection of surface numbers
634  *
635  * returns the number of surface numbers
636  */
637  int count_sides( std::vector< side > side_data, std::vector< int >& surface_numbers );
638 
639  ErrorCode create_facets( const std::vector< facet >& facet_data,
640  const std::map< int, EntityHandle >& surface_map,
641  Range& mb_coords,
642  EntityHandle file_set );
643 
644  ErrorCode create_material_group( const std::string& material_name, int material_id, EntityHandle& handle );
645 
646  // Class Member variables
647  private:
650 
651  // Cell Datas read from the cell_flags section
652  rtt_flags_data cell_flag_datas; //vector of cell for each cell sub-flag
653  std::map< std::string, std::map< int, int > >
654  cell_flag_indexes; // map of indexes for each element of the cell_flag_datas
655  std::map< std::string, int > cell_flag_idx; // map the order of each sub-cell flag
656 
657  // Side Datas read from the side_flags section
659  std::map< std::string, std::map< int, int > > side_flag_indexes;
660  std::map< std::string, int > side_flag_idx;
661 
662  // Data from the cell_def section
663  std::map< int, cell_def > cell_def_data; // definition of the types of cells
664  std::vector< cell > cell_data;
665  std::map< int, int > cell_data_idx;
666 
667  std::vector< side > side_data;
668  // read mesh interface
670  // Moab Interface
672  // geom tool instance
674  // tags used in the problem
676 };
677 
678 } // namespace moab
679 
680 #endif