Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
read_parameter_file.C
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 #include <iostream>
24 #include <fstream>
25 #include <iomanip>
26 #include <cstdlib>
27 
28 
29 #include "roccom_c++.h"
30 #include "Rocin.h"
31 
32 using namespace std;
33 using namespace COM;
34 
35 void eat_whitespace_until(std::string& str, const char& c, int start =0);
36 
37 void get_parameter_list(std::vector<std::pair<std::string,std::string> >& param_list,
38  const char* file_name);
39 
40 void Rocin::read_parameter_file( const char* file_name,
41  const char* window_name,
42  const MPI_Comm *param_comm){
43 
44  // vector of (option,value) pairs
45  std::vector<std::pair<std::string,std::string> > param_list;
46  get_parameter_list(param_list, file_name);
47 
48  // Check to see if the window exists
49  // Roccom_base * rcom= COM_get_roccom();
50  COM::Roccom_base * rbase = COM_get_roccom();
51  int whandle = COM_get_window_handle(window_name);
52  COM::Window* param_window = NULL;
53  if(whandle >0)
54  param_window = rbase->get_window_object(whandle);
55  COM::Attribute* param_att = NULL;
56 
57  // if the parameter window already exists, then only read
58  // in attributes which exist in the window
59  if(param_window){
60  for(uint i =0, ni = param_list.size(); i<ni; ++i){
61 
62  param_att = param_window->attribute(param_list[i].first);
63 
64  if (param_att) {
65  COM_assertion_msg( param_att->location()=='w' &&
66  param_att->size_of_components()==1,
67  "The attribute must be a windowed scalar attribute.");
68  // Obtain address for the current attribute.
69  void *addr = param_att->pointer();
70  COM_assertion_msg( addr, "Unexpected null pointer");
71 
72  // Map value based on its type.
73  switch ( param_att->data_type()) {
74  case COM_INTEGER:
75  case COM_UNSIGNED:
76  case COM_INT: {
77  *(int*)addr = std::atoi( param_list[i].second.c_str());
78  break;
79  }
81  case COM_DOUBLE: {
82  // replace 'd' or 'D' by 'e'
83  char *p = const_cast< char* >( strpbrk(param_list[i].second.c_str(), "dD") );
84  if (p) *p = 'e';
85  *(double*)addr = std::atof( param_list[i].second.c_str());
86  break;
87  }
88  case COM_FLOAT:
89  case COM_REAL: {
90  char *p = const_cast< char* >( strpbrk(param_list[i].second.c_str(), "dD") );
91  if (p) *p = 'e';
92  *(float*)addr = std::atof( param_list[i].second.c_str());
93  break;
94  }
95  case COM_CHAR: {
96  int param_size = param_list[i].second.size();
97  COM_assertion_msg( param_size<param_att->size_of_items(),
98  "Attribute does not have enough space");
99 
100  strcpy( (char*)addr, param_list[i].second.c_str());
101  break;
102  }
103  case COM_BOOL: {
104  int param_size = param_list[i].second.size();
105  const char *str = param_list[i].second.c_str();
106  char val = str[0];
107  if (val == 'T') val = 1;
108  if (val == 'F') val = 0;
109  if (val == 'Y') val = 1;
110  if (val == 'N') val = 0;
111  if (val == '1') val = 1;
112  if (val == '0') val = 0;
113  COM_assertion_msg((val == 1 || val==0),
114  "Invalid boolean value (T or F)");
115 
116  *(char *)addr = val;
117  break;
118  }
119  default:
120  COM_assertion_msg(false, "Unsupported data type.");
121  }
122  }
123  }
124  }
125  // if the parameter window doesn't exist, then create it
126  // and populate it will all of the option value pairs
127  else{
128  if(param_comm)
129  COM_new_window(window_name, *param_comm);
130  else
131  COM_new_window(window_name);
132  std::string wname(window_name);
133  for(uint i =0, ni = param_list.size(); i<ni; ++i){
134  COM_new_attribute( (wname+"."+param_list[i].first).c_str(),
135  'w', COM_CHAR, 1, "");
136  int param_size = param_list[i].second.size();
137  COM_set_size( (wname+"."+param_list[i].first).c_str(),
138  0, param_size+1,0);
139  void *addr;
140  COM_resize_array( (wname+"."+param_list[i].first).c_str(), 0, &addr);
141  strcpy( (char*)addr, param_list[i].second.c_str());
142  }
143  // Create an empty pane so that the window will be written out by Rocout
144  COM_set_size((wname+".nc").c_str(),1,0);
145  COM_window_init_done(window_name);
146  }
147 }
148 
149 void eat_whitespace_until(std::string& str, const char& c, int start){
150  for(int i=start, ni=str.size(); i<ni&&str[i]!=c; ++i){
151  if(str[i] == ' ' || str[i] == '\t'){
152  str.erase(i,1);
153  --i;
154  }
155  }
156 }
157 
158 void get_parameter_list(std::vector<std::pair<std::string,std::string> >& param_list,
159  const char* file_name){
160 
161  //open parameter file for input
162  std::ifstream param_file(file_name);
163  COM_assertion_msg(param_file,
164  "Specified parameter file not found.");
165 
166  string cur_line, option, value;
167  string::size_type cur_pos;
168  // get one line of the file at a time
169  while (getline(param_file,cur_line)){
170 
171  cur_pos = cur_line.find('#');
172  if(cur_pos != string::npos)
173  cur_line.erase(cur_pos, cur_line.size()-cur_pos);
174 
175  // remove whitespace up to first '='
176  eat_whitespace_until(cur_line, (char)61);
177 
178  // If the line only contained whitespace and comments,
179  // go to the next line.
180  if(cur_line.size()==0)
181  continue;
182 
183  // Now find the first '='
184  cur_pos = cur_line.find_first_of("=");
185  COM_assertion_msg(cur_pos != string::npos && cur_pos !=0,
186  (std::string("Parameter file '")
187  + file_name + "' has an invalid line.").c_str());
188 
189  option.assign(cur_line,0,cur_pos);
190  value.assign(cur_line,cur_pos+1,string::npos);
191 
192  //eat all whitspace in the value up to the first '"'
193  eat_whitespace_until(value, '"');
194 
195  //if the value has quotes, then make sure they are only at
196  //beginning and ending of the string
197  cur_pos = value.find('"');
198  if(cur_pos != string::npos){
199  COM_assertion_msg(cur_pos == 0,
200  "Parameter file has invalid value format");
201  value.erase(0,1);
202  cur_pos = value.rfind('"');
203  // This should get rid of any whitespace after the last quotation mark
204  eat_whitespace_until(value, '"', (int)cur_pos+1);
205  COM_assertion_msg(cur_pos == value.size()-1,
206  "Parameter file has invalid value format");
207  value.erase(cur_pos,1);
208  COM_assertion_msg(value.find('"') == string::npos,
209  "Parameter file has invalid value format");
210  }
211 
212  // quotations are never allowed in the option
213  cur_pos = option.find_first_of("\"");
214  COM_assertion_msg(cur_pos == string::npos,
215  "Unexpected quotation mark found in an option.");
216 
217  param_list.push_back(std::make_pair(option,value));
218  }
219 
220 }
221 
222 
Contains the wrapper routines for C++ binding of Roccom API.
const char * option(const char *const name, const int argc, const char *const *const argv, const char *defaut, const char *const usage=0)
Definition: CImg.h:5604
#define COM_assertion_msg(EX, msg)
void COM_set_size(const char *wa_str, int pane_id, int size, int ng=0)
Set sizes of for a specific attribute.
Definition: roccom_c++.h:136
void get_parameter_list(std::vector< std::pair< std::string, std::string > > &param_list, const char *file_name)
C/C++ Data types.
Definition: roccom_basic.h:129
float atof(const char *const str)
Read a float number from a C-string.
Definition: CImg.h:4905
void read_parameter_file(const char *file_name, const char *window_name, const MPI_Comm *comm=NULL)
Read in parameters from a given file into the given window.
void eat_whitespace_until(std::string &str, const char &c, int start=0)
blockLoc i
Definition: read.cpp:79
void COM_window_init_done(const char *w_str, int pane_changed=true)
Definition: roccom_c++.h:102
void COM_new_window(const char *wname, MPI_Comm c=MPI_COMM_NULL)
Definition: roccom_c++.h:86
Rocin creates a series of Roccom windows by reading in a list of files.
int COM_get_window_handle(const char *wname)
Definition: roccom_c++.h:404
void int int REAL REAL REAL *z blockDim dim * ni
Definition: read.cpp:77
void COM_new_attribute(const char *wa_str, const char loc, const int type, int ncomp, const char *unit)
Registering an attribute type.
Definition: roccom_c++.h:118
COM_END_NAME_SPACE COM::Roccom_base * COM_get_roccom()
Definition: Roccom_base.h:537
void COM_resize_array(const char *wa_str, int pane_id=0, void **addr=NULL, int strd=-1, int cap=0)
Resize an attribute on a specific pane and return the address by setting addr.
Definition: roccom_c++.h:200