Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
file.cpp
Go to the documentation of this file.
1 #include "file.h"
2 
3 /* datafile implementation -----------------------------------------------*/
4 
5 datafile::datafile(ifstream &infile, ofstream &outfile, bool loud, string filename, int dim,
6  std::vector< std::vector<int> > rFieldMappings, std::vector<index_order> rIndexOrder,
7  std::vector<adj_map> conv_factor, std::vector<adj_map> norm_val)
8  : infile(infile), outfile(outfile) {
9 
10  this->loud = loud;
11  this->filename = filename;
12 
13  this->conv_factor = conv_factor;
14  this->norm_val = norm_val;
15 
16  this->mFieldMappings = rFieldMappings;
17 
18  this->num_partitions = -1;
19  this->dep_vars = 0;
20 
21  this->dimensions = dim;
22 
23  this->mIndexOrder = rIndexOrder;
24 
25  //Seek to beginning of file
26  infile.clear();
27  infile.seekg(0, std::ios::beg);
28 }
29 
31 
32  if(num_partitions > 0){
33  for(int i=0; i<num_partitions; i++)
34  delete data_part[i];
35  delete[] data_part;
36  }
37 
38 }
39 
41  : infile(d.infile), outfile(d.outfile) {
42 
43  filename = d.filename;
44  loud = d.loud;
46  title = d.title;
47  dep_vars = d.dep_vars;
48 
49  var_names = d.var_names;
50 
51 }
52 
54  return string("Datafile");
55 }
56 
58 
59  //Print status
61 
62  return;
63 }
64 
65 void datafile::error_out(string err_ps){
66  outfile << "ERROR | File \"" << this->filename << "\": " << err_ps << std::endl;
67  cout << "ERROR | File \"" << this->filename << "\": " << err_ps << std::endl;
68 }
69 
70 void datafile::status_out(string st_ps){
71  if(loud){
72  outfile << "status | File \"" << this->filename << "\": " << st_ps << std::endl;
73  }
74 }
75 
76 /*
77  * Log field mapping data
78  */
80  for(int field=0; field<mFieldMappings.size(); field++){
81  string st_out = string("Mapping VAR ");
82  st_out += itoa(mFieldMappings[field][0]);
83  st_out += " -> VAR ";
84  st_out += itoa(mFieldMappings[field][1]);
85 
86  status_out(st_out);
87  }
88 }
89 
91  if(data_part[partition] != NULL)
92  return data_part[partition]->get_layout()[n];
93  else
94  return 0;
95 }
96 
97 /*
98  * Get the points of the specified partition
99  */
101 
102  if(partition >= get_num_partitions())
103  return NULL;
104 
105  return data_part[partition]->get_points();
106 }
107 
108 /*
109  * Get specified point
110  */
112 
113  //Calculate the correct partition
114  int i;
115  for(i=0; i<num_partitions; i++){
116  if(data_part[i]->get_num_points() > n)
117  break;
118 
119  n = n - data_part[i]->get_num_points();
120  }
121 
122  //Retrieve point from partition
123  return data_part[i]->get_point(n);
124 }
125 
127  return data_part[partition]->get_point(n);
128 }
129 
131 
132  int ret = 0;
133 
134  //Get number of points of each partition
135  for(int i=0; i<num_partitions; i++){
136  if(data_part[i] != NULL)
137  ret += data_part[i]->get_num_points();
138  }
139 
140  return ret;
141 }
142 
144 
145  if(data_part[partition] != NULL)
146  return data_part[partition]->get_num_points();
147  else
148  return 0;
149 }
150 
152  return num_partitions;
153 }
154 
156  return title;
157 }
158 
160  return var_names.size();
161 }
162 
164  return var_names.size() - dimensions;
165 }
166 
168  for(int record_num = 0; record_num < mIndexOrder.size(); record_num++){
169  if(mIndexOrder[record_num].partition_number == partition){
170  return mIndexOrder[record_num];
171  }
172  }
173 
175  return empty;
176 }
177 
178 
179 
180 /* tecplot_data - tecplot file parent class --------------------------*/
181 
183  return;
184 }
185 
186 tecplot_data::tecplot_data(ifstream &infile, ofstream &outfile, bool loud, string filename, int dim,
187  std::vector< std::vector<int> > rFieldMappings, std::vector<index_order> rIndexOrder,
188  std::vector<adj_map> conv_factor, std::vector<adj_map> norm_val)
189  : datafile(infile, outfile, loud, filename, dim, rFieldMappings, rIndexOrder, conv_factor, norm_val){
190 
191  return;
192 }
193 
195  return string("Tecplot File");
196 }
197 
199 
200  string tmp("");
201 
202  while(!infile.eof() && tmp.compare(string("ZONE")) != 0)
203  infile >> tmp;
204 
205 }
206 
208 
209  //Seek to beginning
210  infile.clear();
211  infile.seekg(0, std::ios::beg);
212 
213  string temp;
214  string header;
215 
216  infile >> temp;
217 
218  //Read until a Zone header is reached
219  while(temp.compare(string("ZONE")) != 0){
220  header += temp;
221  header += " ";
222  infile >> temp;
223  }
224 
225  //Parse the file title
226  std::vector<string> title_parse = readQuoteExpression(header, string("TITLE"));
227  if(title_parse.size() == 1){
228  this->title = title_parse[0];
229  }
230 
231  //Parse variable names
232  this->var_names = readQuoteExpression(header, string("VARIABLES"));
233 
234  status_out(itoa(this->var_names.size()) + " Variable(s) Detected");
235 
236  //Log Field Mapping
238 
239  //Seek to beginning
240  infile.clear();
241  infile.seekg(0, std::ios::beg);
242 
243 }
244 
245 void tecplot_data::zone_status_out(int zone, string zs_out){
246  string st_ps("Zone ");
247  st_ps = st_ps + itoa(zone) + " | " + zs_out;
248 
249  status_out(st_ps);
250 }
251 
252 void tecplot_data::zone_error_out(int zone, string zs_err){
253  string err_ps("Zone ");
254  err_ps = err_ps + itoa(zone) + " | " + zs_err;
255 
256  error_out(err_ps);
257 }
258 
259 /*
260  * Count number of zones in the file from the current location
261  */
263 
264  int zones = 0;
265 
266  //Seek to the next zone until
267  //end of file is reached
268  seek_to_zone();
269  while(!infile.eof()){
270  zones++;
271  seek_to_zone();
272  }
273 
274  infile.clear();
275  infile.seekg(0, std::ios::beg);
276 
277  return zones;
278 
279 }
280 
282 
283  //Get header information
284  // - Sets num_vars
285  // - Sets title
286  read_header();
287 
288  int zone_count = count_zones();
289 
290  if(zone_count == 0){
291  string err_ps("No Zones Found");
292  error_out(err_ps);
293  return;
294  }
295 
296  status_out(itoa(zone_count) + " Zone(s) Detected");
297 
298  //Seek to beginning
299  infile.clear();
300  infile.seekg(0, std::ios::beg);
301 
302  this->num_partitions = zone_count;
303  this->data_part = new partition*[this->num_partitions];
304 
305  for(int i=0; i<zone_count; i++){
306 
307  //Detect zone
308  //Create proper zone object
309  data_part[i] = zone_detect(i);
310 
311  //Parse zone header
312  if(data_part[i] != NULL){
314  if( data_part[i]->get_error().compare("") != 0)
315  zone_error_out(i, data_part[i]->get_error());
316  else
317  zone_status_out(i, data_part[i]->get_status());
318  }
319  else{
320  string err_ps("Invalid Zone");
321  zone_error_out(i, err_ps);
322  }
323  }
324 
325  //Parse zone data
326  for(int i=0; i<zone_count; i++){
327 
328  if(data_part[i] != NULL){
329  if(data_part[i]->parse_data(get_num_dep_vars(), get_num_vars(), mFieldMappings,
331  zone_status_out(i, data_part[i]->get_status());
332  else
333  zone_error_out(i, data_part[i]->get_error());
334  }
335  }
336 
337  return;
338 }
339 
341 
342  //Retrieve zone header and read zone type
343  string *zheader = get_zone_header(zone);
344  std::vector<string> ztype = readCommaExpression(*zheader, string("ZONETYPE"));
345 
346  tpzone *retzone = NULL;
347 
348  if(ztype.size() == 1){
349 
350  //Get zone data
351  string *zdata = get_zone_nodes(zone);
352 
353  //If the zone is an ordered zone, create a new ordered zone object
354  if(ztype[0].compare(string("Ordered")) == 0){
355  retzone = new tpz_ordered( zheader, zdata );
356 
357  string zone_st("Ordered Zone Detected");
358  zone_status_out(zone, zone_st);
359  }
360  //If the zone is FEQuad, create a new FEQuad object
361  else if(ztype[0].compare(string("FEQuadrilateral")) == 0){
362  retzone = new tpz_fequad( zheader, zdata );
363 
364  string zone_st("FEQuadrilateral Zone Detected");
365  zone_status_out(zone, zone_st);
366  }
367 
368  }
369 
370  return retzone;
371 }
372 
374 
375  //Seek to beginning
376  infile.clear();
377  infile.seekg(0, std::ios::beg);
378 
379  //Seek to specified zone
380  for(int i=0; i<=zone; i++){
381  seek_to_zone();
382  }
383 
384  string temp;
385  infile >> temp;
386 
387  string *header = new string;
388 
389  //TODO
390  //Make finding the end of the header
391  //more robust
392  while( (temp.find("DT=") == string::npos || temp.find("\"") != string::npos) && !infile.eof()){
393 
394  *header += temp;
395  *header += " ";
396 
397  infile >> temp;
398  }
399 
400  *header += temp;
401 
402  //Get the last line
403  char c;
404  infile.get(c);
405  while(c != '\n'){
406  *header += c;
407  infile.get(c);
408  }
409 
410  return header;
411 
412 }
413 
415 
416  string *ret = new string;
417 
418  //Seek to beginning
419  infile.clear();
420  infile.seekg(0, std::ios::beg);
421 
422  //Check valid zone number
423  if(zone+1 > num_partitions)
424  return NULL;
425 
426  //Seek to specified zone
427  for(int i=0; i<=zone; i++){
428  seek_to_zone();
429  }
430 
431  char line[BUFFER_SIZE];
432  string curr_line;
433 
434  infile.getline(line, BUFFER_SIZE-1);
435  curr_line = line;
436 
437  //Skip through the zone header
438  while( (curr_line.find("DT=") == string::npos || curr_line.find("\"") != string::npos)
439  && !infile.eof() ){
440 
441  infile.getline(line, 511);
442  curr_line = line;
443  }
444 
445  //Read zone point data
446  infile.getline(line, 511);
447  curr_line = line;
448 
449  //Stop when the next zone is reached, or eof
450  while( (curr_line.find("ZONE") == string::npos) && !infile.eof() ){
451 
452  *ret += curr_line;
453 
454  infile.getline(line, 511);
455  curr_line = line;
456  }
457 
458  if(infile.eof()){
459  infile.clear();
460  infile.seekg(0, std::ios::beg);
461  }
462 
463  return ret;
464 
465 }
virtual string get_mod_name()
Get the name of the class (tecplot_data)
Definition: file.cpp:194
datafile(ifstream &infile, ofstream &outfile, bool loud, string filename, int dim, std::vector< std::vector< int > > rFieldMappings, std::vector< index_order > rIndexOrder, std::vector< adj_map > conv_factor, std::vector< adj_map > norm_val)
Construct data file object using specified information.
Definition: file.cpp:5
std::vector< string > var_names
Definition: file.h:168
pnt get_point(int n)
Get the nth point of the file.
Definition: file.cpp:111
string * get_zone_header(int zone)
Read the zone header.
Definition: file.cpp:373
const NT & d
int get_num_points()
Get the number of points in the file.
Definition: file.cpp:130
string * get_zone_nodes(int zone)
Retrieve zone point data from the zone body.
Definition: file.cpp:414
Base class for file parsing.
Definition: file.h:32
virtual void parse()
Parse the data file.
Definition: file.cpp:57
void zone_error_out(int zone, string zs_err)
Log an error message with appended zone information so the error can be located.
Definition: file.cpp:252
static const int BUFFER_SIZE
Definition: file.h:293
boolean empty(T_VertexSet s)
points * get_points()
Definition: partition.cpp:53
Definition: points.h:30
int get_num_dep_vars()
Get number of dependent variables.
Definition: file.cpp:163
int dimensions
Definition: file.h:179
Used to store index order information.
Definition: datatypedef.h:22
void status_out(string st_ps)
Log the string specified as a status event.
Definition: file.cpp:70
string get_title()
Get file title if specified in file header.
Definition: file.cpp:155
partition ** data_part
Definition: file.h:164
ofstream & outfile
Definition: file.h:142
std::vector< string > readCommaExpression(string rStack, string rNeedle)
int get_num_vars()
Get the number of variables in the file.
Definition: file.cpp:159
Point object that represents a single point.
Definition: datatypedef.h:68
bool loud
Definition: file.h:143
virtual void parse()
Parse the tecplot file.
Definition: file.cpp:281
string itoa(int n)
Convert an int to a string.
int count_zones()
Count the number of zones in the tecplot file, starting from the current location of the file stream...
Definition: file.cpp:262
void read_header()
Read the tecplot header from the file and retrieve the title, variable names, and number of variables...
Definition: file.cpp:207
std::vector< std::vector< int > > mFieldMappings
Definition: file.h:186
points * get_points(int partition)
Get a partitions&#39; points.
Definition: file.cpp:100
pnt get_point(int n)
Definition: partition.cpp:57
int num_partitions
Definition: file.h:167
virtual void parse_layout()
Definition: partition.cpp:25
blockLoc i
Definition: read.cpp:79
int get_num_partitions()
Get number of partition in the file.
Definition: file.cpp:151
ifstream & infile
Definition: file.h:141
std::vector< adj_map > norm_val
Definition: file.h:183
const NT & n
std::vector< int > get_layout()
Definition: partition.cpp:17
tpzone * zone_detect(int zone)
Read the header information for the specified zone, then create a new zone object based on the type o...
Definition: file.cpp:340
std::vector< index_order > mIndexOrder
Definition: file.h:197
virtual ~datafile()
Free used data, delete the object.
Definition: file.cpp:30
tecplot_data(ifstream &infile, ofstream &outfile, bool loud, string filename, int dim, std::vector< std::vector< int > > rFieldMappings, std::vector< index_order > rIndexOrder, std::vector< adj_map > conv_factor, std::vector< adj_map > norm_val)
Constructor that initializes values.
Definition: file.cpp:186
virtual string get_mod_name()
Get class name.
Definition: file.cpp:53
int dep_vars
Definition: file.h:191
void zone_status_out(int zone, string zs_out)
Log a status message with appended zone information.
Definition: file.cpp:245
int get_num_points()
Definition: partition.cpp:49
void seek_to_zone()
Seek to the next zone in the file.
Definition: file.cpp:198
std::vector< string > readQuoteExpression(string rStack, string rNeedle)
Read expressions of the form x = &quot;test&quot; &quot;100&quot; &quot;another test&quot;.
string title
Definition: file.h:169
void LogFieldMappings()
Log the field mapping data from the command line and stored in mFieldMappings.
Definition: file.cpp:79
index_order get_index_order(int partition)
Definition: file.cpp:167
CGAL_KERNEL_INLINE Comparison_result compare(const NT &n1, const NT &n2)
Definition: number_utils.h:143
int partition_layout(int partition, int n)
Get the size of n dimension of the specified partition.
Definition: file.cpp:90
virtual ~tecplot_data()
Destructor.
Definition: file.cpp:182
string filename
Definition: file.h:138
std::vector< adj_map > conv_factor
Definition: file.h:182
void error_out(string err_ps)
Log the string specified as an error event.
Definition: file.cpp:65