FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fei_utils.cpp
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2005 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8 #include "fei_fstream.hpp"
9 
10 #include "fei_utils.hpp"
11 #include "snl_fei_Utils.hpp"
12 #include "fei_MatrixReducer.hpp"
13 #include "fei_Matrix_Impl.hpp"
14 #include "fei_LinearSystemCore.hpp"
15 #include "fei_ParameterSet.hpp"
16 
17 #include "fei_version.h"
18 
19 #ifdef FEI_HAVE_TIME_H
20 #include <time.h>
21 #endif
22 
23 //----------------------------------------------------------------------------
24 const char* fei_VERSION::version()
25 {
26  static int times_called = 0;
27 
28  static std::string static_fei_version_string;
29 
30  if (times_called == 0) {
31  FEI_OSTRINGSTREAM osstr;
32 
33  osstr << FEI_MAJOR_VERSION << "."
34  << FEI_MINOR_VERSION << "."
35  << FEI_PATCH_VERSION;
36 
37  static_fei_version_string = osstr.str();
38 
39  times_called = 1;
40  }
41 
42  return( static_fei_version_string.c_str() );
43 }
44 
45 //----------------------------------------------------------------------------
46 double fei::utils::cpu_time()
47 {
48  double cpu_seconds = 0.0;
49 
50 #ifdef FEI_HAVE_TIME_H
51  cpu_seconds = (1.0*clock())/CLOCKS_PER_SEC;
52 #endif
53 
54  return(cpu_seconds);
55 }
56 
57 //----------------------------------------------------------------------------
59 {
60  if (str == "STATS" || str == "fei::STATS") {
61  return fei::STATS;
62  }
63  else if (str == "BRIEF_LOGS" || str == "fei::BRIEF_LOGS") {
64  return fei::BRIEF_LOGS;
65  }
66  else if (str == "MATRIX_FILES" || str == "fei::MATRIX_FILES") {
67  return fei::MATRIX_FILES;
68  }
69  else if (str == "FULL_LOGS" || str == "fei::FULL_LOGS") {
70  return fei::FULL_LOGS;
71  }
72  else if (str == "ALL" || str == "fei::ALL") {
73  return fei::ALL;
74  }
75 
76  return fei::NONE;
77 }
78 
79 //----------------------------------------------------------------------------
82 {
83  fei::Matrix* matptr = matrix;
84  fei::MatrixReducer* matred = dynamic_cast<fei::MatrixReducer*>(matptr);
85  if (matred != NULL) matptr = matred->getTargetMatrix().get();
86 
88  dynamic_cast<fei::Matrix_Impl<LinearSystemCore>*>(matptr);
89 
90  if (mat_lsc != NULL) {
91  return mat_lsc->getMatrix().get();
92  }
93 
94  return(NULL);
95 }
96 
97 //----------------------------------------------------------------------------
99  std::vector<int>& nodes,
100  std::vector<int>& elem_offsets)
101 {
102  //first, iterate over the connectivity-blocks and count how many nodes
103  //and elements there are.
104  int num_elems = 0;
105  int num_nodes = 0;
106 
107  std::map<int,fei::ConnectivityBlock*>& cBlocks =
108  matrixGraph.getConnectivityBlocks();
109  std::map<int,fei::ConnectivityBlock*>::iterator
110  iter = cBlocks.begin(),
111  iter_end = cBlocks.end();
112 
113  for(; iter != iter_end; ++iter) {
114  fei::ConnectivityBlock* cblk = iter->second;
115 
116  //if not symmetric, then assume it's not an element-block.
117  if (!cblk->isSymmetric()) continue;
118 
119  num_elems += cblk->getConnectivityIDs().size();
120 
121  fei::Pattern* pattern = cblk->getRowPattern();
122  num_nodes += num_elems*pattern->getNumIDs();
123  }
124 
125  nodes.resize(num_nodes);
126  elem_offsets.resize(num_elems+1);
127 
128  iter = cBlocks.begin(),
129  iter_end = cBlocks.end();
130 
131  int node_offset = 0;
132  int elem_offset = 0;
133  int elem_counter = 0;
134 
135  int nodeType = 0;
136  snl_fei::RecordCollection* nodeRecords = NULL;
137  matrixGraph.getRowSpace()->getRecordCollection(nodeType, nodeRecords);
138 
139  for(; iter != iter_end; ++iter) {
140  fei::ConnectivityBlock* cblk = iter->second;
141 
142  //if not symmetric, then assume it's not an element-block.
143  if (!cblk->isSymmetric()) continue;
144 
145  fei::Pattern* pattern = cblk->getRowPattern();
146 
147  int ne = cblk->getConnectivityIDs().size();
148  int nn = pattern->getNumIDs();
149  std::vector<int>& cblk_nodes = cblk->getRowConnectivities();
150  for(unsigned i=0; i<cblk_nodes.size(); ++i) {
151  nodes[node_offset++] = nodeRecords->getRecordWithLocalID(cblk_nodes[i])->getID();
152  }
153 
154  for(int i=0; i<ne; ++i) {
155  elem_offsets[elem_counter++] = elem_offset;
156  elem_offset += nn;
157  }
158  }
159 
160  elem_offsets[elem_counter] = elem_offset;
161 }
162 
163 //----------------------------------------------------------------------------
164 void fei::utils::char_ptrs_to_strings(int numStrings,
165  const char*const* charstrings,
166  std::vector<std::string>& stdstrings)
167 {
168  stdstrings.resize(0);
169  for(int i=0; i<numStrings; ++i) {
170  if (charstrings[i] != NULL) {
171  std::string tempstr(charstrings[i]);
172  stdstrings.push_back(tempstr);
173  }
174  }
175 }
176 
177 //----------------------------------------------------------------------------
178 void fei::utils::strings_to_char_ptrs(std::vector<std::string>& stdstrings,
179  int& numStrings,
180  const char**& charPtrs)
181 {
182  numStrings = stdstrings.size();
183  charPtrs = numStrings > 0 ? new const char*[numStrings] : NULL;
184 
185  for(int i=0; i<numStrings; ++i) {
186  charPtrs[i] = stdstrings[i].c_str();
187  }
188 }
189 
190 //----------------------------------------------------------------------------
191 void fei::utils::parse_strings(std::vector<std::string>& stdstrings,
192  const char* separator_string,
193  fei::ParameterSet& paramset)
194 {
195  std::vector<std::string>::iterator
196  s_iter = stdstrings.begin(),
197  s_end = stdstrings.end();
198 
199  int intval = 0;
200  double doubleval = 0.0;
201  const char* charstring_key = NULL;
202  const char* charstring_val = NULL;
203  int key_len = 0;
204  int val_len = 0;
205 
206  for(; s_iter != s_end; ++s_iter) {
207  snl_fei::separate_string((*s_iter).c_str(), separator_string,
208  charstring_key, key_len, charstring_val, val_len);
209  if (key_len == 0) {
210  continue;
211  }
212 
213  std::string keystr(charstring_key, key_len);
214 
215  if (val_len == 0) {
216  fei::Param vparam(keystr.c_str(), true);
217  paramset.add(vparam, false);
218  continue;
219  }
220 
221  std::string valstr(charstring_val);
222 
223  if (valstr == "true" || valstr == "True" || valstr == "TRUE") {
224  fei::Param bparam(keystr.c_str(), true);
225  paramset.add(bparam, false);
226  continue;
227  }
228 
229  if (valstr == "false" || valstr == "False" || valstr == "FALSE") {
230  fei::Param bparam(keystr.c_str(), false);
231  paramset.add(bparam, false);
232  continue;
233  }
234 
235  FEI_ISTRINGSTREAM isstr(valstr);
236 
237  //Does charstring_val contain a floating-point value?
238  //If so, we'll store it as a double.
239  std::string::size_type i = valstr.find(".");
240  std::string::size_type valstrsize = valstr.size();
241 
242  if (i < valstrsize) {
243  isstr >> doubleval;
244  if (!isstr.fail()) {
245  fei::Param dparam(keystr.c_str(), doubleval);
246  paramset.add(dparam, false);
247  continue;
248  }
249  isstr.clear();
250  }
251 
252  //Does charstring_val contain an int?
253  isstr >> intval;
254  if (!isstr.fail()) {
255  fei::Param iparam(keystr.c_str(), intval);
256  paramset.add(iparam, false);
257  continue;
258  }
259  isstr.clear();
260 
261  //If charstring_val doesn't contain an int or a real, we'll just store
262  //it as a string.
263  fei::Param sparam(keystr.c_str(), charstring_val);
264  paramset.add(sparam, false);
265  }
266 }
267 
268 //----------------------------------------------------------------------------
269 void
271  std::vector<std::string>& paramStrings)
272 {
273  paramStrings.resize(0);
274 
276  iter = paramset->begin(),
277  iter_end = paramset->end();
278 
279  for(; iter != iter_end; ++iter) {
280  const fei::Param& param = *iter;
281  fei::Param::ParamType ptype = param.getType();
282 
283  FEI_OSTRINGSTREAM osstr;
284  osstr << param.getName();
285 
286  switch(ptype) {
287  case fei::Param::STRING:
288  osstr << " " << param.getStringValue();
289  break;
290  case fei::Param::DOUBLE:
291  osstr << " " << param.getDoubleValue();
292  break;
293  case fei::Param::INT:
294  osstr << " " << param.getIntValue();
295  break;
296  case fei::Param::VOID:
297  break;
298  case fei::Param::BOOL:
299  if (param.getBoolValue()) osstr << " true";
300  else osstr << " false";
301  break;
302  default:
303  break;
304  }
305 
306  paramStrings.push_back(osstr.str());
307  }
308 }
309 
void strings_to_char_ptrs(std::vector< std::string > &stdstrings, int &numStrings, const char **&charPtrs)
Definition: fei_utils.cpp:178
void char_ptrs_to_strings(int numStrings, const char *const *charstrings, std::vector< std::string > &stdstrings)
Definition: fei_utils.cpp:164
ParamType getType() const
Definition: fei_Param.hpp:98
const std::map< int, int > & getConnectivityIDs() const
void convert_ParameterSet_to_strings(const fei::ParameterSet *paramset, std::vector< std::string > &paramStrings)
Definition: fei_utils.cpp:270
fei::OutputLevel string_to_output_level(const std::string &str)
Definition: fei_utils.cpp:58
bool getBoolValue() const
Definition: fei_Param.hpp:122
virtual std::map< int, fei::ConnectivityBlock * > & getConnectivityBlocks()=0
int getIntValue() const
Definition: fei_Param.hpp:116
double getDoubleValue() const
Definition: fei_Param.hpp:110
std::vector< int > & getRowConnectivities()
virtual fei::SharedPtr< fei::VectorSpace > getRowSpace()=0
bool isSymmetric() const
void add(const Param &param, bool maintain_unique_keys=true)
LinearSystemCore * get_LinearSystemCore(fei::Matrix *matrix)
Definition: fei_utils.cpp:81
OutputLevel
T * get() const
fei::SharedPtr< T > getMatrix()
void parse_strings(std::vector< std::string > &stdstrings, const char *separator_string, fei::ParameterSet &paramset)
Definition: fei_utils.cpp:191
int getNumIDs() const
const std::string & getStringValue() const
Definition: fei_Param.hpp:104
double cpu_time()
Definition: fei_utils.cpp:46
const std::string & getName() const
Definition: fei_Param.hpp:92
const_iterator end() const
void getConnectivityArrays(fei::MatrixGraph &matrixGraph, std::vector< int > &nodes, std::vector< int > &elem_offsets)
Definition: fei_utils.cpp:98
int getRecordCollection(int idType, snl_fei::RecordCollection *&records)
const_iterator begin() const
const fei::Pattern * getRowPattern() const