Interface Documentation
Version: invalid
simple_definition.hh
1 /*~--------------------------------------------------------------------------~*
2  * Copyright (c) 2015 Los Alamos National Security, LLC
3  * All rights reserved.
4  *~--------------------------------------------------------------------------~*/
5 
6 #pragma once
7 
8 #include <fstream>
9 #include <iterator>
10 #include <sstream>
11 #include <string>
12 #include <unordered_map>
13 #include <vector>
14 
16 #include <flecsi/flog.hh>
17 
18 namespace flecsi {
19 namespace topo {
20 namespace unstructured_impl {
21 
22 class simple_definition : public definition<2>
23 {
24 public:
25  simple_definition(const char * filename) {
26  file_.open(filename, std::ifstream::in);
27 
28  if(file_.good()) {
29  std::string line;
30  std::getline(file_, line);
31  std::istringstream iss(line);
32 
33  // Read the number of vertices and cells
34  iss >> num_vertices_ >> num_cells_;
35 
36  // Get the offset to the beginning of the vertices
37  vertex_start_ = file_.tellg();
38 
39  for(size_t i(0); i < num_vertices_; ++i) {
40  std::getline(file_, line);
41  } // for
42 
43  cell_start_ = file_.tellg();
44  }
45  else {
46  flog_fatal("failed opening " << filename);
47  } // if
48 
49  // Go to the start of the cells.
50  std::string line;
51  file_.seekg(cell_start_);
52  for(size_t l(0); l < num_cells_; ++l) {
53  std::getline(file_, line);
54  std::istringstream iss(line);
55  ids_.push_back(std::vector<size_t>(
56  std::istream_iterator<size_t>(iss), std::istream_iterator<size_t>()));
57  }
58 
59  } // simple_definition
60 
61  simple_definition(const simple_definition &) = delete;
62  simple_definition & operator=(const simple_definition &) = delete;
63 
64  size_t num_entities(size_t dimension) const override {
65  flog_assert(dimension == 0 || dimension == 2, "invalid dimension");
66  return dimension == 0 ? num_vertices_ : num_cells_;
67  }
68 
72 
73  const std::vector<std::vector<size_t>> & entities(size_t from_dim,
74  size_t to_dim) const override {
75  flog_assert(from_dim == 2, "invalid dimension " << from_dim);
76  flog_assert(to_dim == 0, "invalid dimension " << to_dim);
77  return ids_;
78  }
79 
83 
84  std::vector<size_t>
85  entities(size_t from_dim, size_t to_dim, size_t entity_id) const override {
86  flog_assert(from_dim == 2, "invalid dimension " << from_dim);
87  flog_assert(to_dim == 0, "invalid dimension " << to_dim);
88 
89  std::string line;
90  std::vector<size_t> ids;
91  size_t v0, v1, v2, v3;
92 
93  // Go to the start of the cells.
94  file_.seekg(cell_start_);
95 
96  // Walk to the line with the requested id.
97  for(size_t l(0); l < entity_id; ++l) {
98  std::getline(file_, line);
99  } // for
100 
101  // Get the line with the information for the requested id.
102  std::getline(file_, line);
103  std::istringstream iss(line);
104 
105  // Read the cell definition.
106  iss >> v0 >> v1 >> v2 >> v3;
107 
108  ids.push_back(v0);
109  ids.push_back(v1);
110  ids.push_back(v2);
111  ids.push_back(v3);
112 
113  return ids;
114  } // vertices
115 
116  point_t vertex(size_t vertex_id) const {
117  std::string line;
118  point_t v;
119 
120  // Go to the start of the vertices.
121  file_.seekg(vertex_start_);
122 
123  // Walk to the line with the requested id.
124  for(size_t l(0); l < vertex_id; ++l) {
125  std::getline(file_, line);
126  } // for
127 
128  // Get the line with the information for the requested id.
129  std::getline(file_, line);
130  std::istringstream iss(line);
131 
132  // Read the vertex coordinates.
133  iss >> v[0] >> v[1];
134 
135  return v;
136  } // vertex
137 
138 private:
139  mutable std::ifstream file_;
140  std::vector<std::vector<size_t>> ids_;
141 
142  size_t num_vertices_;
143  size_t num_cells_;
144 
145  mutable std::iostream::pos_type vertex_start_;
146  mutable std::iostream::pos_type cell_start_;
147 
148 }; // class simple_definition
149 
150 } // namespace unstructured_impl
151 } // namespace topo
152 } // namespace flecsi
const std::vector< std::vector< size_t > > & entities(size_t from_dim, size_t to_dim) const override
Definition: simple_definition.hh:73
#define flog_assert(test, message)
Definition: flog.hh:411
#define flog_fatal(message)
Definition: flog.hh:358
Definition: dimensioned_array.hh:58
Definition: simple_definition.hh:22
std::vector< size_t > entities(size_t from_dim, size_t to_dim, size_t entity_id) const override
Definition: simple_definition.hh:85
size_t num_entities(size_t dimension) const override
Definition: simple_definition.hh:64
Definition: control.hh:31
static constexpr size_t dimension()
Definition: definition.hh:51