FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
ext_vtk_writer.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
9#include <kernel/geometry/export_vtk.hpp>
10#include <kernel/geometry/reference_cell_factory.hpp>
11#include <kernel/space/eval_data.hpp>
12#include <kernel/util/math.hpp>
13
14// includes, system
15#include <iostream>
16#include <fstream>
17
18namespace FEAT
19{
20 namespace Space
21 {
23 namespace Intern
24 {
25 template<bool _enable>
26 struct ExtVtkValueWriter
27 {
28 template<typename ExtVtkWriter_>
29 static void write(const ExtVtkWriter_&, std::ostream&) {}
30 };
31
32 template<bool _enable>
33 struct ExtVtkGradWriter
34 {
35 template<typename ExtVtkWriter_>
36 static void write(const ExtVtkWriter_&, std::ostream&) {}
37 };
38
39 template<bool _enable>
40 struct ExtVtkHessWriter
41 {
42 template<typename ExtVtkWriter_>
43 static void write(const ExtVtkWriter_&, std::ostream&) {}
44 };
45
46 template<>
47 struct ExtVtkValueWriter<true>
48 {
49 template<typename ExtVtkWriter_>
50 static void write(const ExtVtkWriter_& writer, std::ostream& os)
51 {
52 writer.write_values(os);
53 }
54 };
55
56 template<>
57 struct ExtVtkGradWriter<true>
58 {
59 template<typename ExtVtkWriter_>
60 static void write(const ExtVtkWriter_& writer, std::ostream& os)
61 {
62 writer.write_gradients(os);
63 }
64 };
65
66 template<>
67 struct ExtVtkHessWriter<true>
68 {
69 template<typename ExtVtkWriter_>
70 static void write(const ExtVtkWriter_& writer, std::ostream& os)
71 {
72 writer.write_hessians(os);
73 }
74 };
75 } // namespace Intern
77
86 template<typename Trafo_>
88 {
89 friend struct Intern::ExtVtkValueWriter<true>;
90 friend struct Intern::ExtVtkGradWriter<true>;
91 friend struct Intern::ExtVtkHessWriter<true>;
92
93 public:
94 typedef Trafo_ TrafoType;
95 typedef typename TrafoType::MeshType MeshType;
96 typedef typename MeshType::ShapeType ShapeType;
98
99 protected:
100 typedef typename TrafoType::template Evaluator<>::Type TrafoEval;
101
102 protected:
103 const TrafoType& _trafo;
104 std::ofstream _ofs;
105 RefMeshType* _ref_mesh;
106
107 public:
108 explicit ExtVtkWriter(const TrafoType& trafo, Index num_refines = 1) :
109 _trafo(trafo),
110 _ref_mesh(nullptr)
111 {
112 // create refined reference cell mesh
113 typedef Geometry::ReferenceCellFactory<ShapeType> RefCellFactory;
114 typedef Geometry::StandardRefinery<RefMeshType> RefCellRefinery;
115 RefCellFactory ref_factory;
116 RefMeshType* ref_mesh = new RefMeshType(ref_factory);
117 for(Index i(0); i < num_refines; ++i)
118 {
119 RefMeshType* old_mesh = ref_mesh;
120 {
121 RefCellRefinery refinery(*old_mesh);
122 ref_mesh = new RefMeshType(refinery);
123 }
124 delete old_mesh;
125 }
126 _ref_mesh = ref_mesh;
127 }
128
129 virtual ~ExtVtkWriter()
130 {
131 if(_ref_mesh != nullptr)
132 {
133 delete _ref_mesh;
134 }
135 }
136
140 bool open(String filename)
141 {
142 _ofs.open(filename.c_str());
143 if(!_ofs.is_open() || !_ofs.good())
144 return false;
145
146 // write VTK header
147 _ofs << "# vtk DataFile Version 2.0\n";
148 _ofs << "Generated by FEAT v" << version_major << "." << version_minor << "." << version_patch << "\n";
149 _ofs << "ASCII\n";
150
151 // write mesh type
152 _ofs << "DATASET UNSTRUCTURED_GRID\n";
153
154 // write vertices
155 write_vertices();
156
157 // write indices
158 write_indices();
159
160 // write point data header
161 _ofs << "POINT_DATA " << (_trafo.get_mesh().get_num_entities(ShapeType::dimension)
162 * _ref_mesh->get_num_entities(0)) << "\n";
163
164 // okay
165 return true;
166 }
167
171 void close()
172 {
173 _ofs.close();
174 }
175
188 template<typename Space_, typename VectorType_>
189 void write_values_blocked(String name, const Space_& space, const VectorType_& v)
190 {
191 typedef Space_ SpaceType;
192 typedef typename SpaceType::template Evaluator<TrafoEval>::Type SpaceEval;
193 //static_assert(SpaceEval::can_value != 0, "space cannot evaluate basis function values!");
194 typedef typename SpaceType::DofMappingType DofMappingType;
195 typedef typename SpaceEval::template ConfigTraits<SpaceTags::value> SpaceConfigTraits;
196 typedef typename SpaceConfigTraits::EvalDataType SpaceData;
197 static constexpr TrafoTags trafo_value_config = SpaceConfigTraits::trafo_config;
198 typedef typename TrafoEval::template ConfigTraits<trafo_value_config>::EvalDataType TrafoData;
199 typedef typename VectorType_::ValueType ValueType;
201
202 const Memory::TypedView<ValueType> vv(v.elements_view_r());
203
204 DofMappingType dof_map(space);
205 TrafoEval trafo_eval(_trafo);
206 SpaceEval space_eval(space);
207 TrafoData trafo_data;
208 SpaceData space_data;
209
210 // write basic info
211 _ofs << "VECTORS " << name << " double\n";
212
213 // loop over all cells
214 for(Index cell(trafo_eval.begin()); cell != trafo_eval.end(); ++cell)
215 {
216 // gather local vector
217 dof_map.prepare(cell);
218 for(int i(0); i < dof_map.get_num_local_dofs(); ++i)
219 {
220 loc_vec[i] = vv(dof_map.get_index(i));
221 }
222 dof_map.finish();
223
224 // prepare evaluators
225 trafo_eval.prepare(cell);
226 space_eval.prepare(trafo_eval);
227
228 // loop over all points
229 for(Index pt(0); pt < _ref_mesh->get_num_entities(0); ++pt)
230 {
231 // get domain point
232 typename TrafoEval::DomainPointType dom_point;
233 get_dom_point(dom_point, pt);
234
235 // evaluate trafo and space
236 trafo_eval(trafo_data, dom_point);
237 space_eval(space_data, trafo_data);
238
239 // compute value
240 ValueType value(0);
241 for(int i(0); i < space_eval.get_num_local_dofs(); ++i)
242 value += space_data.phi[i].value * loc_vec[i];
243
244 // write value
245 for(int d(0); d < ValueType::n; ++d)
246 _ofs << " " << value(d);
247
248 for(int d(ValueType::n); d < 3; ++d)
249 _ofs << " 0.0";
250 _ofs << "\n";
251 }
252
253 // finish evaluators
254 space_eval.finish();
255 trafo_eval.finish();
256 }
257 }
258
271 template<typename Space_, typename T_>
272 void write_values(String name, const Space_& space, const T_* data)
273 {
274 XASSERT(data != nullptr);
275 typedef Space_ SpaceType;
276 typedef typename SpaceType::template Evaluator<TrafoEval>::Type SpaceEval;
277 static_assert(*(SpaceEval::eval_caps & SpaceTags::value), "space cannot evaluate basis function values!");
278 typedef typename SpaceType::DofMappingType DofMappingType;
279 typedef typename SpaceEval::template ConfigTraits<SpaceTags::value> SpaceConfigTraits;
280 typedef typename SpaceConfigTraits::EvalDataType SpaceData;
281 static constexpr TrafoTags trafo_value_config = SpaceConfigTraits::trafo_config;
282 typedef typename TrafoEval::template ConfigTraits<trafo_value_config>::EvalDataType TrafoData;
283 typedef typename SpaceEval::DataType DataType;
285
286 DofMappingType dof_map(space);
287 TrafoEval trafo_eval(_trafo);
288 SpaceEval space_eval(space);
289 TrafoData trafo_data;
290 SpaceData space_data;
291
292 // write basic info
293 _ofs << "SCALARS " << name << " double 1\n";
294 _ofs << "LOOKUP_TABLE default\n";
295
296 // loop over all cells
297 for(Index cell(trafo_eval.begin()); cell != trafo_eval.end(); ++cell)
298 {
299 // gather local vector
300 dof_map.prepare(cell);
301 for(int i(0); i < dof_map.get_num_local_dofs(); ++i)
302 {
303 loc_vec[i] = DataType(data[dof_map.get_index(i)]);
304 }
305 dof_map.finish();
306
307 // prepare evaluators
308 trafo_eval.prepare(cell);
309 space_eval.prepare(trafo_eval);
310
311 // loop over all points
312 for(Index pt(0); pt < _ref_mesh->get_num_entities(0); ++pt)
313 {
314 // get domain point
315 typename TrafoEval::DomainPointType dom_point;
316 get_dom_point(dom_point, pt);
317
318 // evaluate trafo and space
319 trafo_eval(trafo_data, dom_point);
320 space_eval(space_data, trafo_data);
321
322 // compute value
323 DataType value(0);
324 for(int i(0); i < space_eval.get_num_local_dofs(); ++i)
325 value += loc_vec[i] * space_data.phi[i].value;
326
327 // write value
328 _ofs << value << "\n";
329 }
330
331 // finish evaluators
332 space_eval.finish();
333 trafo_eval.finish();
334 }
335 }
336
349 template<typename Space_, typename T_>
350 void write_gradients(String name, const Space_& space, const T_* data)
351 {
352 XASSERT(data != nullptr);
353 typedef Space_ SpaceType;
354 typedef typename SpaceType::template Evaluator<TrafoEval>::Type SpaceEval;
355 static_assert(*(SpaceEval::eval_caps & SpaceTags::grad), "space cannot evaluate basis function gradients!");
356 typedef typename SpaceType::DofMappingType DofMappingType;
357 typedef typename SpaceEval::template ConfigTraits<SpaceTags::grad> SpaceConfigTraits;
358 typedef typename SpaceConfigTraits::EvalDataType SpaceData;
359 static constexpr TrafoTags trafo_grad_config = SpaceConfigTraits::trafo_config;
360 typedef typename TrafoEval::template ConfigTraits<trafo_grad_config>::EvalDataType TrafoData;
361 typedef typename SpaceEval::DataType DataType;
362 typedef typename SpaceEval::SpaceEvalTraits SpaceEvalTraits;
363 typedef typename SpaceEvalTraits::BasisGradientType BasisGradientType;
365
366 DofMappingType dof_map(space);
367 TrafoEval trafo_eval(_trafo);
368 SpaceEval space_eval(space);
369 TrafoData trafo_data;
370 SpaceData space_data;
371
372 // write basic info
373 _ofs << "VECTORS " << name << " double\n";
374
375 // loop over all cells
376 for(Index cell(trafo_eval.begin()); cell != trafo_eval.end(); ++cell)
377 {
378 // gather local vector
379 dof_map.prepare(cell);
380 for(int i(0); i < dof_map.get_num_local_dofs(); ++i)
381 {
382 loc_vec[i] = DataType(data[dof_map.get_index(i)]);
383 }
384 dof_map.finish();
385
386 // prepare evaluators
387 trafo_eval.prepare(cell);
388 space_eval.prepare(trafo_eval);
389
390 // loop over all points
391 for(Index pt(0); pt < _ref_mesh->get_num_entities(0); ++pt)
392 {
393 // get domain point
394 typename TrafoEval::DomainPointType dom_point;
395 get_dom_point(dom_point, pt);
396
397 // evaluate trafo and space
398 trafo_eval(trafo_data, dom_point);
399 space_eval(space_data, trafo_data);
400
401 // compute value
402 BasisGradientType grad;
403 grad.format();
404 for(int i(0); i < space_eval.get_num_local_dofs(); ++i)
405 grad += loc_vec[i] * space_data.phi[i].grad;
406
407 // write value
408 _ofs << grad[0];
409 for(int i(1); i < ShapeType::dimension; ++i)
410 _ofs << " " << grad[i];
411 for(int i(ShapeType::dimension); i < 3; ++i)
412 _ofs << " 0.0";
413 _ofs << "\n";
414 }
415
416 // finish evaluators
417 space_eval.finish();
418 trafo_eval.finish();
419 }
420
421 }
422
435 template<typename Space_, typename T_>
436 void write_hessians(String name, const Space_& space, const T_* data)
437 {
438 XASSERT(data != nullptr);
439 typedef Space_ SpaceType;
440 typedef typename SpaceType::template Evaluator<TrafoEval>::Type SpaceEval;
441 static_assert(*(SpaceEval::eval_caps & SpaceTags::hess), "space cannot evaluate basis function hessians!");
442 typedef typename SpaceType::DofMappingType DofMappingType;
443 typedef typename SpaceEval::template ConfigTraits<SpaceTags::hess> SpaceConfigTraits;
444 typedef typename SpaceConfigTraits::EvalDataType SpaceData;
445 static constexpr TrafoTags trafo_hess_config = SpaceConfigTraits::trafo_config;
446 typedef typename TrafoEval::template ConfigTraits<trafo_hess_config>::EvalDataType TrafoData;
447 typedef typename SpaceEval::DataType DataType;
448 typedef typename SpaceEval::SpaceEvalTraits SpaceEvalTraits;
449 typedef typename SpaceEvalTraits::BasisHessianType BasisHessianType;
451
452 DofMappingType dof_map(space);
453 TrafoEval trafo_eval(_trafo);
454 SpaceEval space_eval(space);
455 TrafoData trafo_data;
456 SpaceData space_data;
457
458 // write basic info
459 _ofs << "TENSORS " << name << " double\n";
460
461 // loop over all cells
462 for(Index cell(trafo_eval.begin()); cell != trafo_eval.end(); ++cell)
463 {
464 // gather local vector
465 dof_map.prepare(cell);
466 for(int i(0); i < dof_map.get_num_local_dofs(); ++i)
467 {
468 loc_vec[i] = DataType(data[dof_map.get_index(i)]);
469 }
470 dof_map.finish();
471
472 // prepare evaluators
473 trafo_eval.prepare(cell);
474 space_eval.prepare(trafo_eval);
475
476 // loop over all points
477 for(Index pt(0); pt < _ref_mesh->get_num_entities(0); ++pt)
478 {
479 // get domain point
480 typename TrafoEval::DomainPointType dom_point;
481 get_dom_point(dom_point, pt);
482
483 // evaluate trafo and space
484 trafo_eval(trafo_data, dom_point);
485 space_eval(space_data, trafo_data);
486
487 // compute value
488 BasisHessianType hess;
489 hess.format();
490 for(int i(0); i < space_eval.get_num_local_dofs(); ++i)
491 hess += loc_vec[i] * space_data.phi[i].hess;
492
493 // write value
494 for(int i(0); i < ShapeType::dimension; ++i)
495 {
496 _ofs << hess(i,0);
497 for(int j(1); j < ShapeType::dimension; ++j)
498 _ofs << " " << hess(i,j);
499 for(int j(ShapeType::dimension); j < 3; ++j)
500 _ofs << " 0.0";
501 _ofs << "\n";
502 }
503 for(int i(ShapeType::dimension); i < 3; ++i)
504 {
505 _ofs << "0.0";
506 for(int j(1); j < 3; ++j)
507 _ofs << " 0.0";
508 _ofs << "\n";
509 }
510 }
511
512 // finish evaluators
513 space_eval.finish();
514 trafo_eval.finish();
515 }
516 }
517
518 protected:
519 void write_vertices()
520 {
521 // fetch index set and vertex set
522 const TrafoType& trafo = _trafo;
523 const MeshType& mesh = trafo.get_mesh();
524
525 // write vertex set
526 const Index num_cells = mesh.get_num_entities(ShapeType::dimension);
527 const Index num_ref_verts = num_cells * _ref_mesh->get_num_entities(0);
528 _ofs << "POINTS " << num_ref_verts << " double\n";
529
530 // create trafo evaluator and data
531 typedef typename TrafoEval::DomainPointType DomainPointType;
532 typedef typename TrafoEval::template ConfigTraits<TrafoTags::img_point>::EvalDataType TrafoData;
533 DomainPointType dom_point;
534 TrafoEval trafo_eval(trafo);
535 TrafoData trafo_data;
536
537 // loop over all cells
538 for(Index cell(trafo_eval.begin()); cell != trafo_eval.end(); ++cell)
539 {
540 // prepare trafo
541 trafo_eval.prepare(cell);
542
543 // loop over all reference vertices
544 for(Index i(0); i < _ref_mesh->get_num_entities(0); ++i)
545 {
546 // get reference vertex
547 get_dom_point(dom_point, i);
548
549 // apply trafo
550 trafo_eval(trafo_data, dom_point);
551
552 // write image coords
553 _ofs << trafo_data.img_point[0];
554 for(int j(1); j < ShapeType::dimension; ++j)
555 _ofs << " " << trafo_data.img_point[j];
556 for(int j(ShapeType::dimension); j < 3; ++j)
557 _ofs << " 0.0";
558 _ofs << "\n";
559 }
560
561 // finish trafo
562 trafo_eval.finish();
563 }
564 }
565
566 void write_indices()
567 {
568 // fetch index set and vertex set
569 const TrafoType& trafo = _trafo;
570 const MeshType& mesh = trafo.get_mesh();
571
572 // write index set
573 const Index num_cells = mesh.get_num_entities(ShapeType::dimension);
574 const Index num_ref_verts = _ref_mesh->get_num_entities(0);
575 const Index num_ref_cells = _ref_mesh->get_num_entities(ShapeType::dimension);
576 const typename RefMeshType::template IndexSet<ShapeType::dimension,0>::Type& idx =
577 _ref_mesh->template get_index_set<ShapeType::dimension, 0>();
578 int num_idx = idx.get_num_indices();
579
580 typedef Geometry::Intern::VTKShape<ShapeType> VTKHelperType;
581
582 _ofs << "CELLS " << (num_cells*num_ref_cells) << " " << (Index(num_idx+1)*num_cells*num_ref_cells) << "\n";
583
584 // loop over all mesh cells
585 for(Index cell(0); cell < num_cells; ++cell)
586 {
587 // compute vertex offset
588 Index voff = num_ref_verts * cell;
589 for(Index i(0); i < num_ref_cells; ++i)
590 {
591 _ofs << num_idx;
592 for(int j(0); j < num_idx; ++j)
593 {
594 _ofs << " " << voff + idx(i,VTKHelperType::map(j));
595 }
596 _ofs << "\n";
597 }
598 }
599
600 // write cell types
601 _ofs << "CELL_TYPES " << (num_cells*num_ref_cells) << "\n";
602
603 // loop over all mesh cells
604 for(Index cell(0); cell < num_cells*num_ref_cells; ++cell)
605 {
606 _ofs << VTKHelperType::type << "\n";
607 }
608 }
609
610 void get_dom_point(typename TrafoEval::DomainPointType& dom_point, Index i) const
611 {
612 // get reference mesh vertex set
613 const typename RefMeshType::VertexSetType& ref_vtx = _ref_mesh->get_vertex_set();
614
615 // set domain point
616 for(int j(0); j < ShapeType::dimension; ++j)
617 dom_point[j] = ref_vtx[i][j];
618 }
619 };
620 } // namespace Space
621} // namespace FEAT
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
Conformal mesh class template.
Index get_num_entities(int dim) const
Returns the number of entities.
VertexSet< num_coords_, Coord_ > VertexSetType
Vertex set type.
VertexSetType & get_vertex_set()
Returns a reference to the vertex set of the mesh.
Standard Refinery class template.
Definition: factory.hpp:58
Typed memory view class.
Extended VTK writer class template.
void close()
Closes the VTK file.
void write_gradients(String name, const Space_ &space, const T_ *data)
Writes a finite element function gradient field to the VTK file.
void write_values_blocked(String name, const Space_ &space, const VectorType_ &v)
Writes a finite element function to the VTK file.
bool open(String filename)
Opens a VTK files and writes basic information.
void write_hessians(String name, const Space_ &space, const T_ *data)
Writes a finite element function Hessian field to the VTK file.
void write_values(String name, const Space_ &space, const T_ *data)
Writes a finite element function to the VTK file.
String class implementation.
Definition: string.hpp:47
Tiny Vector class template.
@ write
write-only access (see attention note above)
FEAT namespace.
Definition: adjactor.hpp:12
static constexpr int version_major
FEAT major version number.
static constexpr int version_patch
FEAT patch version number (month)
static constexpr int version_minor
FEAT minor version number (year)
@ value
specifies whether the space should supply basis function values
@ hess
specifies whether the space should supply basis function hessians
@ grad
specifies whether the space should supply basis function gradients
std::uint64_t Index
Index data type.
TrafoTags
Trafo configuration tags enum.
Definition: eval_tags.hpp:22
@ dom_point
specifies whether the trafo should supply domain point coordinates