Nalu
Nalu: a generalized unstructured massively parallel low Mach flow code designed to support a variety of energy applications of interest (most notably Wind ECP) built on the Sierra Toolkit and Trilinos solver Tpetra/Epetra stack. The open source BSD, clause 3 license model has been chosen for the code base. See LICENSE for more information. http://NaluCFD.org
NaluParsingHelper.h
Go to the documentation of this file.
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2014 Sandia Corporation. */
3 /* This software is released under the license detailed */
4 /* in the file, LICENSE, which is located in the top-level Nalu */
5 /* directory structure */
6 /*------------------------------------------------------------------------*/
7 
8 
9 #ifndef NaluParsingHelper_h
10 #define NaluParsingHelper_h
11 
12 // yaml for parsing..
13 #include <yaml-cpp/yaml.h>
14 
15 #include <iostream>
16 #include <fstream>
17 #include <string>
18 #include <vector>
19 
20 namespace sierra {
21 namespace nalu {
22 
24 
25 public:
26  static void emit(YAML::Emitter& emout, const YAML::Node & node) {
27  // recursive depth first
28  YAML::NodeType::value type = node.Type();
29  std::string out;
30  switch (type)
31  {
33  out = node.as<std::string>();
34  emout << out;
35  break;
36  case YAML::NodeType::Sequence:
37  emout << YAML::BeginSeq;
38  for (unsigned int i = 0; i < node.size(); ++i) {
39  const YAML::Node subnode = node[i] ;
40  emit(emout, subnode);
41  }
42  emout << YAML::EndSeq;
43  break;
44  case YAML::NodeType::Map:
45  emout << YAML::BeginMap ;
46  for (YAML::const_iterator i = node.begin(); i != node.end(); ++i) {
47  const YAML::Node key = i->first;
48  const YAML::Node value = i->second;
49  out = key.as<std::string>() ;
50  emout << YAML::Key << out;
51  emout << YAML::Value;
52  emit(emout, value);
53  }
54  emout << YAML::EndMap ;
55  break;
56  case YAML::NodeType::Null:
57  emout << " (empty) ";
58  break;
59  default:
60  std::cerr << "Warning: emit: unknown/unsupported node type" << std::endl;
61  break;
62  }
63  }
64 
66  static void emit(std::ostream& sout, const YAML::Node & node) {
67  YAML::Emitter out;
68  emit(out,node);
69  sout << out.c_str() << std::endl;
70  }
71 
72  static std::string line_info(const YAML::Node & node) {
73  std::ostringstream sout;
74  sout << "(pos,line,column) = ("
75  << node.Mark().pos << ", "
76  << node.Mark().line << ", "
77  << node.Mark().column << ")" ;
78  // << "Unknown for now" ;
79  return sout.str();
80  }
81 
82  static std::string info(const YAML::Node & node) {
83  std::ostringstream sout;
84  sout << "Node at " << line_info(node) << " => \n" ;
85  emit(sout, node);
86  return sout.str();
87  }
88 
90  static void traverse(std::ostream& sout, const YAML::Node & node, unsigned int depth = 0) {
91  using namespace std;
92 
93  // recursive depth first
94  YAML::NodeType::value type = node.Type();
95  string indent((size_t)depth*2, ' ');
96  string out;
97  switch (type)
98  {
100  out = node.as<std::string>();
101  sout << indent << "Scalar: " << out << endl;
102  break;
103  case YAML::NodeType::Sequence:
104  sout << indent << "Sequence:" << endl;
105  for (unsigned int i = 0; i < node.size(); ++i) {
106  const YAML::Node & subnode = node[i];
107  sout << indent << "[" << i << "]:" << endl;
108  traverse(sout, subnode, depth + 1);
109  }
110  break;
111  case YAML::NodeType::Map:
112  sout << indent << "Map:" << endl;
113  for (YAML::const_iterator i = node.begin(); i != node.end(); ++i) {
114  const YAML::Node key = i->first;
115  const YAML::Node value = i->second;
116  out = key.as<std::string>();
117  sout << indent << "Key: " << out << endl;
118  sout << indent << "Value:" << endl;
119  traverse(sout, value, depth + 1);
120  }
121  break;
122  case YAML::NodeType::Null:
123  sout << indent << "(empty)" << endl;
124  break;
125  default:
126  cerr << "Warning: traverse: unknown/unsupported node type" << endl;
127  break;
128  }
129  }
130 
132  static void find_nodes_given_key(const std::string& key, const YAML::Node &node, std::vector<const YAML::Node *> & result)
133  {
134  // recursive depth first
135  YAML::NodeType::value type = node.Type();
136  if (type != YAML::NodeType::Scalar && type != YAML::NodeType::Null) {
137  if (node[key])
138  result.push_back(&node);
139  }
140 
141  switch (type) {
143  break;
144  case YAML::NodeType::Sequence:
145  for (unsigned int i = 0; i < node.size(); ++i) {
146  const YAML::Node & subnode = node[i];
147  find_nodes_given_key(key, subnode, result);
148  }
149  break;
150  case YAML::NodeType::Map:
151  for (YAML::const_iterator i = node.begin(); i != node.end(); ++i) {
152  //const YAML::Node & key1 = i.first();
153  const YAML::Node value = i->second;
154  find_nodes_given_key(key, value, result);
155  }
156  break;
157  case YAML::NodeType::Null:
158  break;
159  default:
160  break;
161  }
162  }
163 };
164 
165 }
166 }
167 #endif
Definition: ABLForcingAlgorithm.C:26
double Scalar
Definition: LinearSolver.h:28
static std::string info(const YAML::Node &node)
Definition: NaluParsingHelper.h:82
STL namespace.
static void find_nodes_given_key(const std::string &key, const YAML::Node &node, std::vector< const YAML::Node * > &result)
returns a vector of nodes that match the given key (depth first traversal)
Definition: NaluParsingHelper.h:132
static void emit(YAML::Emitter &emout, const YAML::Node &node)
Definition: NaluParsingHelper.h:26
static void emit(std::ostream &sout, const YAML::Node &node)
uses Emitter to print node to stream
Definition: NaluParsingHelper.h:66
Definition: NaluParsingHelper.h:23
static std::string line_info(const YAML::Node &node)
Definition: NaluParsingHelper.h:72
static void traverse(std::ostream &sout, const YAML::Node &node, unsigned int depth=0)
just prints nodes depth-first
Definition: NaluParsingHelper.h:90
Tpetra::Map< LocalOrdinal, GlobalOrdinal >::node_type Node
Definition: LinearSolver.h:32