GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/io/vtksequencewriter.hh
Date: 2024-05-04 19:09:25
Exec Total Coverage
Lines: 31 34 91.2%
Functions: 4 4 100.0%
Branches: 52 136 38.2%

Line Branch Exec Source
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 //
4 // SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 /*!
8 * \file
9 * \ingroup InputOutput
10 * \brief Base class to write pvd-files which contains a list of all collected vtk-files.
11 * This is a modified version of DUNE's pvd writer which takes a VTKWriter as template
12 * argument making it more general.
13 */
14 #ifndef DUMUX_VTKSEQUENCEWRITER_HH
15 #define DUMUX_VTKSEQUENCEWRITER_HH
16
17 #include <vector>
18 #include <iostream>
19 #include <sstream>
20 #include <fstream>
21 #include <iomanip>
22 #include <memory>
23
24 #include <dune/grid/io/file/vtk/common.hh>
25 #include <dune/common/path.hh>
26
27
28 namespace Dumux {
29
30 /*!
31 * \ingroup InputOutput
32 * \brief Base class to write pvd-files which contains a list of all collected vtk-files.
33 * This is a modified version of DUNE's pvd writer which takes a VTKWriter as template
34 * argument making it more general.
35 *
36 * Derive from this class to write pvd-file suitable for easy visualization with
37 * <a href="http://www.vtk.org/">The Visualization Toolkit (VTK)</a>.
38 *
39 * \tparam VTKWriter The VTKWriter class
40 *
41 */
42 template<class VTKWriter>
43 class VTKSequenceWriter
44 {
45 std::shared_ptr<VTKWriter > vtkWriter_;
46 std::vector<double> timesteps_;
47 std::string name_,path_,extendpath_;
48 int rank_;
49 int size_;
50 public:
51 /*! \brief Set up the VTKSequenceWriter class
52 *
53 * \param vtkWriter Writer object used to write the individual time step data files
54 * \param name Base name of the output files. This should not
55 * contain any directory part and not filename
56 * extensions. It will be used both for each processes
57 * piece as well as the parallel collection file.
58 * \param path Directory where to put the parallel collection
59 * (.pvtu/.pvtp) file. If it is relative, it is taken
60 * relative to the current directory
61 * \param extendpath Directory where to put the piece file (.vtu/.vtp) of
62 * this process. If it is relative, it is taken
63 * relative to the directory denoted by path
64 * \param rank Process number in a multi-process setting
65 * \param size Total number of processes
66 */
67 75 explicit VTKSequenceWriter( std::shared_ptr<VTKWriter > vtkWriter,
68 const std::string& name,
69 const std::string& path,
70 const std::string& extendpath,
71 int rank,
72 int size)
73 : vtkWriter_(vtkWriter),
74 name_(name), path_(path),
75 extendpath_(extendpath),
76 rank_(rank),
77
4/16
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 75 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 75 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 75 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
75 size_(size)
78 75 {}
79
80
7/10
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 75 times.
✓ Branch 4 taken 60 times.
✓ Branch 5 taken 15 times.
✓ Branch 6 taken 26 times.
✓ Branch 7 taken 49 times.
✓ Branch 8 taken 75 times.
✗ Branch 9 not taken.
161 ~VTKSequenceWriter() {}
81
82
83 /*!
84 * \brief Writes VTK data for the given time,
85 * \param time The time(step) for the data to be written.
86 * \param type VTK output type.
87 */
88 87 void write (double time, Dune::VTK::OutputType type = Dune::VTK::ascii)
89 {
90 /* remember current time step */
91 87 unsigned int count = timesteps_.size();
92 87 timesteps_.push_back(time);
93
94 /* write VTK file */
95
1/2
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
87 if(size_==1)
96
4/12
✓ Branch 3 taken 87 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 87 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 87 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 87 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
261 vtkWriter_->write(Dune::concatPaths(path_,seqName(count)),type);
97 else
98 vtkWriter_->pwrite(seqName(count), path_,extendpath_,type);
99
100 /* write pvd file ... only on rank 0 */
101
1/2
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
87 if (rank_==0) {
102 87 std::ofstream pvdFile;
103
1/2
✓ Branch 1 taken 87 times.
✗ Branch 2 not taken.
87 pvdFile.exceptions(std::ios_base::badbit | std::ios_base::failbit |
104 std::ios_base::eofbit);
105
3/4
✓ Branch 1 taken 87 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 84 times.
✓ Branch 4 taken 3 times.
258 std::string pvdname = name_ + ".pvd";
106
2/4
✓ Branch 1 taken 87 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 87 times.
✗ Branch 5 not taken.
174 pvdFile.open(pvdname.c_str());
107 pvdFile << "<?xml version=\"1.0\"?> \n"
108 << "<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"" << Dune::VTK::getEndiannessString() << "\"> \n"
109
4/12
✓ Branch 3 taken 87 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 87 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 87 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 87 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
348 << "<Collection> \n";
110
2/2
✓ Branch 0 taken 1060 times.
✓ Branch 1 taken 87 times.
1147 for (unsigned int i=0; i<=count; i++)
111 {
112 // filename
113
1/4
✓ Branch 0 taken 1060 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2120 std::string piecepath;
114
3/6
✓ Branch 0 taken 1060 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1060 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1060 times.
3180 std::string fullname;
115
1/2
✓ Branch 0 taken 1060 times.
✗ Branch 1 not taken.
1060 if(size_==1) {
116
1/2
✓ Branch 1 taken 1060 times.
✗ Branch 2 not taken.
1060 piecepath = path_;
117
5/12
✓ Branch 1 taken 1060 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1060 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1060 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1060 times.
✓ Branch 12 taken 1060 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
3180 fullname = vtkWriter_->getSerialPieceName(seqName(i), piecepath);
118 }
119 else {
120 piecepath = Dune::concatPaths(path_, extendpath_);
121 fullname = vtkWriter_->getParallelHeaderName(seqName(i), piecepath, size_);
122 }
123
2/4
✓ Branch 2 taken 1060 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1060 times.
✗ Branch 6 not taken.
3180 pvdFile << "<DataSet timestep=\"" << timesteps_[i]
124 << "\" group=\"\" part=\"0\" name=\"\" file=\""
125
2/4
✓ Branch 2 taken 1060 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1060 times.
✗ Branch 6 not taken.
2120 << fullname << "\"/> \n";
126 }
127 pvdFile << "</Collection> \n"
128
1/2
✓ Branch 3 taken 87 times.
✗ Branch 4 not taken.
261 << "</VTKFile> \n" << std::flush;
129
1/2
✓ Branch 1 taken 87 times.
✗ Branch 2 not taken.
87 pvdFile.close();
130 }
131 87 }
132 private:
133
134 // create sequence name
135 1147 std::string seqName(unsigned int count) const
136 {
137 1147 std::stringstream n;
138
1/2
✓ Branch 1 taken 1147 times.
✗ Branch 2 not taken.
1147 n.fill('0');
139
4/8
✓ Branch 1 taken 1147 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1147 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1147 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1147 times.
✗ Branch 12 not taken.
4588 n << name_ << "-" << std::setw(5) << count;
140
1/2
✓ Branch 1 taken 1147 times.
✗ Branch 2 not taken.
1147 return n.str();
141 }
142 };
143
144 } // end namespace Dumux
145
146 #endif
147