rheolef  6.3
space_constitution_get.cc
Go to the documentation of this file.
1 // idiststream& operator >> (idiststream&, space_constitution&);
2 // * lecture du fichier sur proc=0 -> arbre sur proc=0
3 // * lecture du stringstream sur ts les proc -> arbre
4 
5 #include <sstream> // flex include it, but in namespace rheolef
6 #include <cstring>
7 #include "rheolef/space.h"
8 
9 namespace rheolef {
10 
11 
12 typedef std::pair<std::string,std::string> base_geo_pair;
13 
14 struct tree_type {
15  bool is_hier;
17  std::list<tree_type> hier;
18  std::string spec;
19  tree_type(const std::string& base, const std::string& geo)
20  : is_hier(false), base_geo(base,geo), hier(), spec("undef") {}
21  tree_type() : is_hier(true), base_geo(), hier(), spec("undef") {}
22  tree_type(const std::string& s, const std::list<tree_type>& h) : is_hier(true), base_geo(), hier(h), spec(s) {}
23 };
24 typedef std::list<tree_type> list_type;
25 
27 
28 } // namespace rheolef
29 
30 /* AIX requires this to be the first thing in the file. */
31 #ifndef __GNUC__
32 # if _RHEOLEF_HAVE_ALLOCA_H
33 # include <alloca.h>
34 # else
35 # ifdef _AIX
36 #pragma alloca
37 # else
38 # ifndef alloca /* predefined by HP cc +Olibcalls */
39 char *alloca ();
40 # endif
41 # endif
42 # endif
43 #endif
44 
45 namespace rheolef {
46 
47 using namespace std;
48 
49 typedef size_t size_type;
50 
53 
54 extern int space_constitution_lex();
55 void space_constitution_error (const char* msg) {
56  std::string near;
57  error_macro("space constitution input:" << space_constitution_line_no << ": " << msg);
59 }
60 int space_constitution_wrap () { return 1; }
61 
62 static std::vector<std::string> symbol_table;
63 static const std::string& symbol (size_t i) { return symbol_table[i]; }
64 static size_t insert (const std::string& str) {
65  size_t i = symbol_table.size();
66  symbol_table.push_back (str);
67  return i;
68 }
69 #define YYMALLOC ::malloc
70 #define YYFREE ::free
71 
74 
76 
78 
79 void
80 tree_dump (const tree_type& x, std::ostream& out, size_t level = 0)
81 {
82  if (!x.is_hier) {
83  out << x.base_geo.first << "(" << x.base_geo.second << ")";
84  return;
85  }
86  if (x.spec != "mixed") { out << x.spec; }
87  if (x.spec != "mixed" || level != 0) { out << "("; }
88  std::list<tree_type>::const_iterator iter = x.hier.begin(), last = x.hier.end();
89  while (iter != last) {
90  tree_dump (*iter, out, level+1);
91  ++iter;
92  if (iter != last) { out << "*"; }
93  }
94  if (x.spec != "mixed" || level != 0) { out << ")"; }
95 }
96 static
97 void
99 {
101  std::istream& is = ids.is();
102  communicator comm = ids.comm();
103  size_type io_proc = odiststream::io_proc();
104  size_type my_proc = comm.rank();
105  std::string str;
106  if (my_proc == io_proc) {
107  std::getline (ids.is(), str);
108  }
109 #ifdef _RHEOLEF_HAVE_MPI
110  mpi::broadcast (mpi::communicator(), str, io_proc);
111 #endif // _RHEOLEF_HAVE_MPI
112  std::istringstream istrstr;
113  istrstr.str (str);
115  symbol_table.clear();
118  if (space_constitution_parse() != 0 || space_constitution_n_error != 0) {
119  is.setstate (std::ios::badbit);
121  error_macro ("internal error in space_constitution io (pass 2)");
122  }
123  symbol_table.clear();
124 }
125 template<class T, class M>
126 static
129  if (! x.is_hier) {
130  geo_basic<T,M> omega (x.base_geo.second); // TODO: geo hash_table
131  return space_constitution<T,M>(omega, x.base_geo.first);
132  }
133  if (x.spec == "vector" || x.spec == "tensor") {
134  assert_macro (x.hier.size() == 1, "unexpected tree");
135  const tree_type& node = *(x.hier.begin());
136  assert_macro (! node.is_hier, "unexpected tree");
137  geo_basic<T,M> omega (node.base_geo.second); // TODO: geo hash_table
138  space_constitution<T,M> base (omega, node.base_geo.first);
139  space_constitution<T,M> constit;
140  constit.set_hierarchy(true);
141  size_t d = omega.dimension();
143  size_type n_comp = space_constant::n_component (valued_tag, d, omega.coordinate_system());
144  constit.get_hierarchy().resize (n_comp);
145  for (size_t i_comp = 0; i_comp < n_comp; i_comp++) {
146  constit.get_hierarchy().operator[] (i_comp) = base;
147  }
148  constit.set_valued_tag (valued_tag);
149  constit.data().initialize();
150  return constit;
151  }
152  space_constitution<T,M> constit;
153  constit.set_hierarchy(true);
154  size_t n_comp = x.hier.size();
155  constit.get_hierarchy().resize(n_comp);
156  std::list<tree_type>::const_iterator iter = x.hier.begin();
157  for (size_t i_comp = 0; i_comp < n_comp; i_comp++, ++iter) {
158  const tree_type& xi = *iter;
159  constit.get_hierarchy().operator[] (i_comp) = build_from_tree<T,M> (xi); // recursive call
160  }
161  constit.data().initialize();
162  return constit;
163 }
164 template<class T, class M>
165 idiststream&
166 operator>> (idiststream& ids, space_constitution<T,M>& constit)
167 {
169  const tree_type* ptr = result_ptr;
170  constit = build_from_tree<T,M> (*ptr);
172  return ids;
173 }
174 template idiststream& operator>> (idiststream&, space_constitution<Float,sequential>&);
176 #ifdef _RHEOLEF_HAVE_MPI
177 template idiststream& operator>> (idiststream&, space_constitution<Float,distributed>&);
178 #endif // _RHEOLEF_HAVE_MPI
179 
180 } // namespace rheolef