This source file includes following definitions.
- write
- write
- read
- read
- no_of_nodes
- create_simplices
- create_stringSimplices
- get_precision
- calculate_dof
- create_vertices
- create_stringVertices
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <fstream>
18 #include <sstream>
19 #include <vector>
20 #include "io_dgf.h"
21 #include "grid_GridAndTime_impl.h"
22
23 using namespace std;
24
25 using Esfem::Io::Dgf::Handler;
26 using Simplex = Handler::Simplex;
27 using Simplices = Handler::Simplices;
28 using Vertex = Handler::Vertex;
29 using Vertices = Handler::Vertices;
30 using Lines = Handler::Lines;
31 using Vec_FEfun = Esfem::Grid::Vec_FEfun;
32 using Scal_FEfun = Esfem::Grid::Scal_FEfun;
33
34 static constexpr int dim = 3;
35
36
37
38
39 struct Handler::Data{
40 int digit_precision {8};
41 const size_t scal_dim_dof;
42 const size_t vec_dim_dof;
43 const Simplices simplices;
44 Data(const string&);
45
46
47
48
49
50 };
51
52
53
54
55 Handler::Data::Data(const string& fname)
56 : digit_precision {static_cast<int>(get_precision(fname))},
57 scal_dim_dof {calculate_dof(fname)},
58 vec_dim_dof {dim * scal_dim_dof},
59 simplices {create_simplices(fname)}
60 {}
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 Handler::Handler(const std::string& dgf_filename)
78 try : d_ptr {make_unique<Data>(dgf_filename)}
79 {}
80 catch(const exception&){
81 throw_with_nested
82 (runtime_error {"Error in constructor of Esfem::Io::Handler."});
83 }
84 catch(...){
85 throw runtime_error {"Unknown error in constructor of Esfem::Io::Handler."};
86 }
87
88 Handler::~Handler() = default;
89
90 void Handler::write(const std::string& out_filename, const Scal_FEfun& fef) const try{
91 if(fef.size() != no_of_nodes()) throw logic_error
92 {"Finit element function has a bad degree of freedom dimension."};
93
94 ofstream dgf_file {out_filename};
95 if(!dgf_file) throw runtime_error {"Could not open file " + out_filename};
96
97 dgf_file << "DGF" << std::endl;
98 const auto vertices = Esfem::Io::Dgf::to_vertices(fef, 1);
99 dgf_file << "VERTEX" << std::endl;
100 to_dgfFile(vertices, dgf_file, d_ptr -> digit_precision);
101 dgf_file << "#" << '\n'
102 << "SIMPLEX" << std::endl;
103 Esfem::Io::Dgf::to_dgfFile(d_ptr -> simplices, dgf_file, d_ptr -> digit_precision);
104 dgf_file << "#" << std::endl;
105 dgf_file << "GRIDPARAMETER\n"
106 << "tolerance " << pow(10,(-1) * d_ptr -> digit_precision)
107 << "\n#\n#" << std::endl;
108 }
109 catch(...){
110 throw_with_nested (runtime_error {"Error in Handler::write()."});
111 }
112
113 void Handler::write(const std::string& out_filename, const Vec_FEfun& vfef) const try{
114 if(vfef.size() != d_ptr -> vec_dim_dof) throw logic_error
115 {"Finit element function has a bad degree of freedom dimension."};
116
117 ofstream dgf_file {out_filename};
118 if(!dgf_file) throw runtime_error {"Could not open file " + out_filename};
119
120 dgf_file << "DGF" << std::endl;
121 const auto vertices = Esfem::Io::Dgf::to_vertices(vfef);
122 dgf_file << "VERTEX" << std::endl;
123 to_dgfFile(vertices, dgf_file, d_ptr -> digit_precision);
124 dgf_file << "#" << '\n'
125 << "SIMPLEX" << std::endl;
126 to_dgfFile(d_ptr -> simplices, dgf_file, d_ptr -> digit_precision);
127 dgf_file << "#" << std::endl;
128 dgf_file << "GRIDPARAMETER\n"
129 << "tolerance " << pow(10, (-1) * d_ptr -> digit_precision)
130 << "\n#\n#" << std::endl;
131 }
132 catch(...){
133 throw_with_nested (runtime_error {"Error in Handler::write()."});
134 }
135
136 void Handler::read(const string& in_filename, Vec_FEfun& vfef) const try{
137 const auto vertices = create_vertices(in_filename);
138
139
140 const auto dim_of_vertices = vertices.size() * vertices.front().size();
141 const auto dim_of_fef = vfef.size();
142
143
144
145
146 vfef << vertices;
147 }
148 catch(const exception&){
149 throw_with_nested
150 (runtime_error {"Error in Handler::read()."});
151 }
152 catch(...){
153 throw runtime_error {"Unknown error in Handler::write()."};
154 }
155
156 void Handler::read(const string& in_filename, Scal_FEfun& fef) const try{
157 const auto vertices = create_vertices(in_filename);
158
159
160 const auto dim_of_vertices = vertices.size() * vertices.front().size();
161 const auto dim_of_fef = fef.size();
162
163
164
165
166 if(dim_of_fef != dim_of_vertices) throw logic_error
167 {"Input file is incompatible with finite element function."};
168
169 fef << vertices;
170 }
171 catch(const exception&){
172 throw_with_nested
173 (runtime_error {"Error in Handler::read()."});
174 }
175 catch(...){
176 throw runtime_error {"Unknown error in Handler::write()."};
177 }
178
179
180 size_t Handler::no_of_nodes() const noexcept{
181 return d_ptr -> scal_dim_dof;
182 }
183
184
185
186
187 Simplices Esfem::Io::Dgf::create_simplices(const std::string& filename) try{
188 Simplices rv {};
189 const auto simplices_list = create_stringSimplices(filename);
190 rv.reserve(simplices_list.size());
191 for(const auto& line : simplices_list)
192 rv.emplace_back(to<Simplex>(line));
193 return rv;
194 }
195 catch(const exception&){
196 throw_with_nested
197 (runtime_error {"Error in create_simplices()."});
198 }
199 Lines Esfem::Io::Dgf::create_stringSimplices(const string& filename) try{
200 const auto lines = Esfem::Impl::dgfFile_to_vec(filename);
201 auto first = Esfem::Io::Dgf::find(cbegin(lines), cend(lines), "SIMPLEX");
202 auto last = Esfem::Io::Dgf::close_list(first, cend(lines), "#");
203 return {first, last};
204 }
205 catch(const exception&){
206 throw_with_nested
207 (runtime_error {"Error in create_stringSimplices()."});
208 }
209 size_t Esfem::Io::Dgf::get_precision(const string& dgf_file){
210 const auto vertices_list = create_stringVertices(dgf_file);
211 return Esfem::Impl::get_relevant_precision(vertices_list);
212 }
213 size_t Esfem::Io::Dgf::calculate_dof(const string& dgf_file){
214 const auto lines = Esfem::Impl::dgfFile_to_vec(dgf_file);
215 auto first = Esfem::Io::Dgf::find(cbegin(lines), cend(lines), "VERTEX");
216 auto last = Esfem::Io::Dgf::close_list(first, cend(lines), "#");
217 return distance(first, last);
218 }
219 Vertices Esfem::Io::Dgf::create_vertices(const string& filename) try{
220 Vertices rv {};
221 const auto vertices_list = create_stringVertices(filename);
222 rv.reserve(vertices_list.size());
223 for(const auto& line : vertices_list)
224 rv.emplace_back(to<Vertex>(line));
225 return rv;
226 }
227 catch(const exception&){
228 throw_with_nested
229 (runtime_error {"Error in Esfem::Io::Dgf::create_vertices()."});
230 }
231 catch(...){
232 throw runtime_error {"Unknown error in Esfem::Io::Dgf::create_vertices()."};
233 }
234 Lines Esfem::Io::Dgf::create_stringVertices(const string& filename){
235 const auto lines = Esfem::Impl::dgfFile_to_vec(filename);
236 auto first = Esfem::Io::Dgf::find(cbegin(lines), cend(lines), "VERTEX");
237 auto last = Esfem::Io::Dgf::close_list(first, cend(lines), "#");
238 return {first, last};
239 }
240
241
242