Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
partition.cpp
Go to the documentation of this file.
1 #include "partition.h"
2 
3 
4 
5 /* partition -- data partition base class --------------------------------*/
6 
8  data_pts_set = false;
9  return;
10 }
11 
13  if(data_pts_set)
14  delete data_pts;
15 }
16 
17 std::vector<int> partition::get_layout(){
18  if(p_layout.size() == 0){
19  parse_layout();
20  }
21 
22  return p_layout;
23 }
24 
26  return;
27 }
28 
29 bool partition::parse_data(int num_dep_vars, int num_vars,
30  std::vector< std::vector<int> > rFieldMappings, index_order rIndexOrder,
31  std::vector<adj_map> conv_factor, std::vector<adj_map> norm_val){
32  return false;
33 }
34 
36  string temp = error;
37  error = "";
38 
39  return temp;
40 }
41 
43  string temp = status;
44  status = "";
45 
46  return temp;
47 }
48 
50  return data_pts->get_num_points();
51 }
52 
54  return this->data_pts;
55 }
56 
58  return data_pts->get_point(n);
59 }
60 
61 long double partition::get_adj(std::vector<adj_map> adj, int var){
62  for(int i=0; i<adj.size(); i++){
63  if(adj[i].variable == var)
64  return adj[i].factor;
65  }
66 
67  return 1.0;
68 }
69 
70 int partition::GetFieldMapping(std::vector< std::vector<int> > rFieldMappings, int rSearchField){
71 
72  for(int map_num = 0; map_num < rFieldMappings.size(); map_num++){
73 
74  if(rFieldMappings[map_num][0] == rSearchField)
75  return rFieldMappings[map_num][1];
76  }
77 
78  return rSearchField;
79 }
80 
81 
82 
83 /* tpzone -- tecplot zone base class -------------------------------------*/
84 
85 tpzone::tpzone(string *zheader, string *zdata){
86 
87  this->zheader = zheader;
88  this->zdata = zdata;
89  this->error = string("");
90 
91  return;
92 }
93 
95 
96  delete zheader;
97 
98  if(zdata != NULL) //Delete this after parsing
99  delete zdata;
100 
101  return;
102 }
103 
104 
105 
106 /* tpz_ordered -- ordered zone class -------------------------------------*/
107 
108 tpz_ordered::tpz_ordered(string *zheader, string *zdata) : tpzone(zheader, zdata){
111 
112  data_pts_set = true;
113 }
114 
116  return;
117 }
118 
120 
121  //Get I value
122  std::vector<string> i_val = readCommaExpression( *zheader, string("I") );
123 
124  if(i_val.size() == 1){
125  if( !check_non_numeric_str(string(i_val[0])) )
126  p_layout.push_back(atoi(i_val[0].c_str()));
127  }
128  else{
129  error = string("Unable to read I value from zone header");
130 
131  p_layout.erase(p_layout.begin(), p_layout.end());
132  return;
133  }
134 
135 
136  //Get J value
137  std::vector<string> j_val = readCommaExpression(*zheader, string("J"));
138 
139  if(j_val.size() == 1){
140  if( !check_non_numeric_str(string(j_val[0])) )
141  p_layout.push_back(atoi(j_val[0].c_str()));
142  }
143  else{
144  error = string("Unable to read J value from zone header");
145 
146  p_layout.erase(p_layout.begin(), p_layout.end());
147  return;
148  }
149 
150 
151  //Get K value
152  std::vector<string> k_val = readCommaExpression(*zheader, string("K"));
153 
154  if(k_val.size() == 1){
155  if( !check_non_numeric_str(string(k_val[0])) )
156  p_layout.push_back(atoi(k_val[0].c_str()));
157  }
158  else{
159  error = string("Unable to read K value from zone header");
160 
161  p_layout.erase(p_layout.begin(), p_layout.end());
162  return;
163  }
164 
165  status = "I = " + itoa(p_layout[0]);
166  status += ", J = " + itoa(p_layout[1]);
167  status += ", K = " + itoa(p_layout[2]);
168 }
169 
170 //TODO
171 //Pass file stream directly to this function to decrease memory usage/copying
172 bool tpz_ordered::parse_data(int num_dep_vars, int num_vars,
173  std::vector< std::vector<int> > rFieldMappings, index_order rIndexOrder,
174  std::vector<adj_map> conv_factor, std::vector<adj_map> norm_val){
175 
176  std::stringstream data(std::stringstream::in | std::stringstream::out);
177  data << *zdata;
178 
179  //Get the length of each dimension
180  int length = 1;
181  for(int i=0; i < 3; i++){
182  length *= p_layout[i];
183  }
184 
185  //Initialize storage
186  data_pts->set_num_points(length);
187  data_pts->set_num_vars(num_vars);
188  data_pts->set_num_indep_vars(num_vars - num_dep_vars);
189 
190  //Read Data
191  string out;
192 
193  bool swi = false;
194 
195  for(int n=0; n<(num_vars); n++){
196 
197  long double conv = get_adj(conv_factor, n);
198  long double norm = get_adj(norm_val, n);
199 
200  int var_location = GetFieldMapping(rFieldMappings, n);
201 
202  for(int z=0; z<p_layout[ 2 ]; z++){
203  for(int y=0; y<p_layout[ 1 ]; y++){
204  for(int x=0; x<p_layout[ 0 ]; x++){
205 
206  //Read the value
207  data >> out;
208 
209  //Discard data if dimension is mapped to null
210  if(var_location < 0)
211  continue;
212 
213  long double read = strtold(out.c_str(), NULL);
214 
215  //Apply conversion and normalization values
216  read = read * conv * norm;
217 
218  //Calculate the storage index
219  int index_val = get_index_value(x, y, z, rIndexOrder);
220 
221 /*
222  std::cout << index_val << " (" << var_location << ") ";
223  std::cout << ": " << "(" << x << ", " << y << ", " << z << ") > ";
224  std::cout << read << std::endl;
225 */
226 
227  //Store the point
228  data_pts_local->set_point_val(index_val, var_location, read);
229 
230  }
231  }
232  }
233  //std::cout << std::endl;
234  }
235 
236  //std::cout << "=====================================================" << std::endl << std::endl;
237 
238  //Write Status
239  string zs_out("Extracted ");
240  zs_out += itoa(length) + " datapoints";
241  status = zs_out;
242 
243  //Build point index
244  build_point_index(rFieldMappings);
245 
246  return true;
247 }
248 
249 void tpz_ordered::build_point_index(std::vector< std::vector<int> > rFieldMappings){
250 
251  int dimension_0 = GetFieldMapping(rFieldMappings, 0);
252  int dimension_1 = GetFieldMapping(rFieldMappings, 1);
253  int dimension_2 = GetFieldMapping(rFieldMappings, 2);
254 
255  std::vector<int> point_layout;
256  for(int i=0; i<p_layout.size(); i++){
257  point_layout.push_back(p_layout[i]);
258  }
259 
260  //Set layout
261  data_pts_local->set_layout(point_layout);
262 
263  //Build index
264  int n=0;
265  for(int i=0; i < p_layout[0]; i++){
266  for(int j=0; j < p_layout[1]; j++){
267  for(int k=0; k < p_layout[2]; k++){
268 
269  std::vector<int> loc;
270 
271  loc.push_back(i);
272  loc.push_back(j);
273  loc.push_back(k);
274 
275  data_pts_local->set_index(n, loc);
276  pnt curr = get_point(n);
277 
278  n++;
279  }
280  }
281  }
282 
283 }
284 
285 
286 
287 int tpz_ordered::get_index_value(int i, int j, int k, index_order rIndexOrder){
288 
289  if(rIndexOrder.order.size() <= 0 || rIndexOrder.order.size() > 3){
290 
291  int index_val = i * p_layout[ 1 ]
292  + j * p_layout[ 2 ]
293  + k;
294 
295  return index_val;
296  }
297 
298  int vars[3] = {i, j, k};
299  int layout[3] = {0, 1, 2};
300 
301  for(int dim = 0; dim < rIndexOrder.order.size(); dim++){
302  switch( rIndexOrder.order[dim] ){
303  case 0 :
304  vars[dim] = i;
305  layout[dim] = 0;
306  break;
307 
308  case 1 :
309  vars[dim] = j;
310  layout[dim] = 1;
311  break;
312 
313  case 2 :
314  vars[dim] = k;
315  layout[dim] = 2;
316  break;
317 
318  default :
319  break;
320  }
321  }
322 /*
323  std::cout << "------------------------" << std::endl << std::endl;
324 
325  for(int m=0; m<3; m++){
326  std::cout << m << ": " << layout[m] << " | " << p_layout[ layout[m] ] << std::endl;
327  }
328  std::cout << "===========" << std::endl;
329  std::cout << "i | " << i << ": " << vars[0] << std::endl;
330  std::cout << "j | " << j << ": " << vars[1] << std::endl;
331  std::cout << "k | " << k << ": " << vars[2] << std::endl;
332  std::cout << std::endl;
333 */
334  int index_val = vars[0] * p_layout[ layout[1] ]
335  + vars[1] * p_layout[ layout[2] ]
336  + vars[2];
337 
338  return index_val;
339 
340 }
341 
342 
343 
344 /* tpz_fequad -- fequad zone class ---------------------------------------*/
345 
346 tpz_fequad::tpz_fequad(string *zheader, string *zdata) : tpzone(zheader, zdata){
349 
350  data_pts_set = true;
351 }
352 
354  return;
355 }
356 
358 
359  int nodes = 0;
360  int elements = 0;
361 
362  //Parse number of nodes
363  std::vector<string> nodes_str = readCommaExpression( *zheader, string("Nodes") );
364 
365  if(nodes_str.size() == 1){
366  if( !check_non_numeric_str(string(nodes_str[0])) )
367  nodes = atoi(nodes_str[0].c_str());
368  }
369  else{
370  error = string("Unable to read Nodes value from zone header");
371 
372  return;
373  }
374 
375  //Parse number of elements
376  std::vector<string> elements_str = readCommaExpression( *zheader, string("Elements") );
377 
378  if(elements_str.size() == 1){
379  if( !check_non_numeric_str(string(elements_str[0])) )
380  elements = atoi(elements_str[0].c_str());
381  }
382  else{
383  error = string("Unable to read Elements value from zone header");
384 
385  return;
386  }
387 
388  //Set Status
389  status = string("Nodes = ") + itoa(nodes) + string(", Elements = ") + itoa(elements);
390 
391  p_layout.erase(p_layout.begin(), p_layout.end());
392  p_layout.push_back(nodes);
393 
394 }
395 
396 //TODO
397 //Pass file stream directly to this function
398 bool tpz_fequad::parse_data(int num_dep_vars, int num_vars,
399  std::vector< std::vector<int> > rFieldMappings, index_order rIndexOrder,
400  std::vector<adj_map> conv_factor, std::vector<adj_map> norm_val){
401 
402  std::stringstream data(std::stringstream::in | std::stringstream::out);
403  data << *zdata;
404 
405  int length = 1;
406  for(int i=0; i < (num_vars - num_dep_vars); i++){
407  if(i >= p_layout.size())
408  break;
409 
410  length *= p_layout[i];
411  }
412 
413  //Initialize storage
414  data_pts->set_num_points(length);
415  data_pts->set_num_vars(num_vars);
416  data_pts->set_num_indep_vars(num_vars - num_dep_vars);
417 
418  //Read Data
419  string out;
420 
421  for(int n=0; n<num_vars; n++){
422 
423  long double conv = get_adj(conv_factor, n);
424  long double norm = get_adj(norm_val, n);
425 
426  //Get variable number from mapping
427  int var_location = GetFieldMapping(rFieldMappings, n);
428 
429  for(int i=0; i<length; i++){
430 
431  data >> out;
432 
433  //Skip saving the point if the dimension is mapped to null
434  if(var_location < 0)
435  continue;
436 
437  long double read = strtold(out.c_str(), NULL);
438 
439  //Apply conversion and normalization factors
440  read = read * conv * norm;
441 
442  //Store point value
443  data_pts->set_point_val(i, var_location, read);
444  }
445  }
446 
447  //Read Connectivity Information
448  int first, sec, third, fourth;
449  first = 0;
450  sec = 0;
451  third = 0;
452  fourth = 0;
453 
454  //Read 4 values
455  data >> first >> sec >> third >> fourth;
456 
457  while(!data.eof()){
458 
459  data_pts_local->set_connectivity(first, sec);
460  data_pts_local->set_connectivity(first, fourth);
461 
462  data_pts_local->set_connectivity(sec, third);
463  data_pts_local->set_connectivity(sec, first);
464 
465  data_pts_local->set_connectivity(third, fourth);
466  data_pts_local->set_connectivity(third, sec);
467 
468  data_pts_local->set_connectivity(fourth, first);
469  data_pts_local->set_connectivity(fourth, third);
470 
471  first = 0;
472  sec = 0;
473  third = 0;
474  fourth = 0;
475  data >> first >> sec >> third >> fourth;
476  }
477 
478 
479  //Set Status
480  string zs_out("Extracted ");
481  zs_out += itoa(length) + " datapoints";
482  status = zs_out;
483 
484  return true;
485 }
486 
bool data_pts_set
Definition: partition.h:54
string get_status()
Definition: partition.cpp:42
string get_error()
Definition: partition.cpp:35
string * zdata
Definition: partition.h:75
j indices k indices k
Definition: Indexing.h:6
void int int REAL REAL * y
Definition: read.cpp:74
void set_num_indep_vars(int indep_vars)
Definition: points.cpp:113
std::vector< int > p_layout
Definition: partition.h:46
points * get_points()
Definition: partition.cpp:53
Definition: points.h:30
Used to store index order information.
Definition: datatypedef.h:22
virtual bool parse_data(int num_dep_vars, int num_vars, std::vector< std::vector< int > > rFieldMappings, index_order rIndexOrder, std::vector< adj_map > conv_factor, std::vector< adj_map > norm_val)
Definition: partition.cpp:172
void set_index(int point_num, std::vector< int > loc)
Definition: points.cpp:555
string error
Definition: partition.h:49
std::vector< int > order
Definition: datatypedef.h:23
tpz_fequad(string *zheader, string *zdata)
Definition: partition.cpp:346
T norm(const NVec< DIM, T > &v)
void set_point_val(int point_number, int var, long double val)
Definition: points.cpp:129
void build_point_index(std::vector< std::vector< int > > rFieldMappings)
Definition: partition.cpp:249
std::vector< string > readCommaExpression(string rStack, string rNeedle)
double length(Vector3D *const v, int n)
pnt get_point(int n)
Definition: points.cpp:183
Point object that represents a single point.
Definition: datatypedef.h:68
void set_num_points(int n)
Definition: points.cpp:84
virtual void parse_layout()
Definition: partition.cpp:119
string itoa(int n)
Convert an int to a string.
void int int int REAL REAL REAL * z
Definition: write.cpp:76
int GetFieldMapping(std::vector< std::vector< int > > rFieldMapping, int rSearchField)
Definition: partition.cpp:70
pnt get_point(int n)
Definition: partition.cpp:57
virtual void parse_layout()
Definition: partition.cpp:25
tpzone(string *zheader, string *zdata)
Definition: partition.cpp:85
tpz_ordered(string *zheader, string *zdata)
Definition: partition.cpp:108
blockLoc i
Definition: read.cpp:79
void int int REAL * x
Definition: read.cpp:74
connect_points * data_pts_local
Definition: partition.h:119
points * data_pts
Definition: partition.h:53
const NT & n
std::vector< int > get_layout()
Definition: partition.cpp:17
string status
Definition: partition.h:50
virtual void parse_layout()
Definition: partition.cpp:357
virtual bool parse_data(int num_dep_vars, int num_vars, std::vector< std::vector< int > > rFieldMappings, index_order rIndexOrder, std::vector< adj_map > conv_factor, std::vector< adj_map > norm_val)
Definition: partition.cpp:29
void set_connectivity(int point_num, int c_point)
Definition: points.cpp:321
int get_index_value(int i, int j, int k, index_order rIndexOrder)
Definition: partition.cpp:287
long double get_adj(std::vector< adj_map > adj, int var)
Definition: partition.cpp:61
virtual ~tpzone()
Definition: partition.cpp:94
j indices j
Definition: Indexing.h:6
int get_num_points()
Definition: partition.cpp:49
string * zheader
Definition: partition.h:74
void set_layout(std::vector< int > index_layout)
Definition: points.cpp:541
void read(std::istream &is, T &t, const io_Read_write &)
Definition: io.h:132
manual_index_pts * data_pts_local
Definition: partition.h:99
void set_num_vars(int n)
Definition: points.cpp:109
virtual ~partition()
Definition: partition.cpp:12
virtual bool parse_data(int num_dep_vars, int num_vars, std::vector< std::vector< int > > rFieldMappings, index_order rIndexOrder, std::vector< adj_map > conv_factor, std::vector< adj_map > norm_val)
Definition: partition.cpp:398
bool check_non_numeric_str(string test_str)
int get_num_points()
Definition: points.cpp:117