FileWriter.cc 6.07 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
#include "boost/lexical_cast.hpp"
14
#include "FileWriter.h"
15
#include "Initfile.h"
16
17
18
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
19
#include "VtkVectorWriter.h"
20
#include "PngWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
21
#include "PovrayWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
22
#include "DofWriter.h"
23
#include "ArhWriter.h"
24
25
26
27
28
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"
29
#include "DataCollector.h"
30

31
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
32
#include <mpi.h>
33
34
#endif

35
36
namespace AMDiS {

37
38
  using boost::lexical_cast;

39

Praetorius, Simon's avatar
Praetorius, Simon committed
40
41
  template<>
  FileWriterTemplated<double>::FileWriterTemplated(std::string name_,
42
43
44
45
46
			 Mesh *mesh_,
			 SystemVector *vecs)
    : name(name_),
      mesh(mesh_)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
47
    FUNCNAME("FileWriterTemplated<T>::FileWriterTemplated()");
48
49
50
51
52
53
54
55
56
57
58
59

    initialize();

    for (int i = 0; i < static_cast<int>(vecs->getSize()); i++)
      TEST_EXIT(vecs->getDOFVector(0)->getFeSpace() == vecs->getDOFVector(i)->getFeSpace())
	("All FeSpace have to be equal!\n");

    feSpace = vecs->getDOFVector(0)->getFeSpace();
    solutionVecs.resize(vecs->getSize());
    for (int i = 0; i < static_cast<int>(vecs->getSize()); i++)
      solutionVecs[i] = vecs->getDOFVector(i);
  }
60

61

Praetorius, Simon's avatar
Praetorius, Simon committed
62
63
  template<>
  void FileWriterTemplated<double>::writeFiles(AdaptInfo *adaptInfo,
64
65
66
67
68
			      bool force,
			      int level,
			      Flag flag,
			      bool (*writeElem)(ElInfo*))
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
69
    FUNCNAME("FileWriterTemplated<T>::writeFiles()");
70

Praetorius, Simon's avatar
Praetorius, Simon committed
71
    if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force)
72
73
      return;

74
    // Containers, which store the data to be written;
75
    std::vector<DataCollector<>*> dataCollectors(solutionVecs.size());
76

Thomas Witkowski's avatar
Thomas Witkowski committed
77
    if (writeElem) {
78
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Praetorius, Simon's avatar
Praetorius, Simon committed
79
	dataCollectors[i] = new DataCollector<>(feSpace, solutionVecs[i],
80
					      level, flag, writeElem);
Thomas Witkowski's avatar
Thomas Witkowski committed
81
    } else {
82
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Praetorius, Simon's avatar
Praetorius, Simon committed
83
84
85
	dataCollectors[i] = new DataCollector<>(feSpace, solutionVecs[i],
					      traverseLevel,
					      flag | traverseFlag,
86
					      writeElement);
Thomas Witkowski's avatar
Thomas Witkowski committed
87
88
    }

89
    std::string fn = filename;
90

91
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
92
    std::string paraFilename = fn;
93
    fn += "-p" + lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank()) + "-";
Thomas Witkowski's avatar
Thomas Witkowski committed
94
    std::string postfix = "";
95
96
#endif

97
98
99
100
    if (appendIndex) {
      TEST_EXIT(indexLength <= 99)("index lenght > 99\n");
      TEST_EXIT(indexDecimals <= 97)("index decimals > 97\n");
      TEST_EXIT(indexDecimals < indexLength)("index length <= index decimals\n");
Praetorius, Simon's avatar
Praetorius, Simon committed
101

102
103
104
      char formatStr[9];
      char timeStr[20];

Thomas Witkowski's avatar
Thomas Witkowski committed
105
      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
106
107
108
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
Thomas Witkowski's avatar
Thomas Witkowski committed
109
110
#if HAVE_PARALLEL_DOMAIN_AMDIS
      paraFilename += timeStr;
111
      postfix += timeStr + paraviewFileExt;
112
113
114
115
#endif
    } else {
#if HAVE_PARALLEL_DOMAIN_AMDIS
      postfix += paraviewFileExt;
Thomas Witkowski's avatar
Thomas Witkowski committed
116
#endif
117
118
119
    }

    if (writeAMDiSFormat) {
Praetorius, Simon's avatar
Praetorius, Simon committed
120
121
      MacroWriter::writeMacro(dataCollectors[0],
			      const_cast<char*>((fn +  amdisMeshExt).c_str()),
122
			      adaptInfo ? adaptInfo->getTime() : 0.0);
123
      MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str());
Praetorius, Simon's avatar
Praetorius, Simon committed
124

125
      ValueWriter::writeValues(dataCollectors[0],
126
127
			       (fn + amdisDataExt).c_str(),
			       adaptInfo ? adaptInfo->getTime() : 0.0);
Praetorius, Simon's avatar
Praetorius, Simon committed
128
      MSG("value file written to %s\n", (fn + amdisDataExt).c_str());
129
130
131
    }

    if (writePeriodicFormat) {
Praetorius, Simon's avatar
Praetorius, Simon committed
132
      MacroWriter::writePeriodicFile(dataCollectors[0],
133
134
135
				     (fn + periodicFileExt).c_str());
      MSG("periodic file written to %s\n", (fn + periodicFileExt).c_str());
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
136

137
    if (writeParaViewFormat) {
Praetorius, Simon's avatar
Praetorius, Simon committed
138
      VtkWriter vtkWriter(&dataCollectors);
139
      vtkWriter.setCompression(compression);
Praetorius, Simon's avatar
Praetorius, Simon committed
140
      vtkWriter.writeFile(fn + paraviewFileExt);
Thomas Witkowski's avatar
Thomas Witkowski committed
141
142

#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
143
144
145
146
147
      if (MPI::COMM_WORLD.Get_rank() == 0) {
	vector<string> componentNames;
	for (unsigned int i = 0; i < dataCollectors.size(); i++)
	  componentNames.push_back(dataCollectors[i]->getValues()->getName());

Thomas Witkowski's avatar
Thomas Witkowski committed
148
149
	vtkWriter.writeParallelFile(paraFilename + paraviewParallelFileExt,
				    MPI::COMM_WORLD.Get_size(),
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
150
151
152
153
				    filename, 
				    postfix,
				    componentNames);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
154
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
155

Thomas Witkowski's avatar
Thomas Witkowski committed
156
      MSG("ParaView file written to %s\n", (fn + paraviewFileExt).c_str());
157
158
    }

159
    if (writeParaViewVectorFormat) {
Praetorius, Simon's avatar
Praetorius, Simon committed
160
      VtkVectorWriter::writeFile(solutionVecs, fn + paraviewFileExt, true, writeAs3dVector);
Praetorius, Simon's avatar
Praetorius, Simon committed
161

Praetorius, Simon's avatar
Praetorius, Simon committed
162
163
164
165
//       VtkVectorWriter::Impl<double> vtkVectorWriter(&dataCollectors, writeAs3dVector);
//       vtkVectorWriter.setCompression(compression);
//       vtkVectorWriter.setWriteAs3dVector(writeAs3dVector);
//       vtkVectorWriter.writeFile(fn + paraviewFileExt);
Praetorius, Simon's avatar
Praetorius, Simon committed
166

167
168
      MSG("ParaView file written to %s\n", (fn + paraviewFileExt).c_str());
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
169

170
    if (writeParaViewAnimation) {
Thomas Witkowski's avatar
Thomas Witkowski committed
171
172
#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0) {
173
174
	VtkWriter::updateAnimationFile(adaptInfo,
				      paraFilename + paraviewParallelFileExt,
Thomas Witkowski's avatar
Thomas Witkowski committed
175
176
177
178
179
				      &paraviewAnimationFrames,
				      filename + ".pvd");

      }
#else
180
181
      VtkWriter::updateAnimationFile(adaptInfo,
				    fn + paraviewFileExt,
Thomas Witkowski's avatar
Thomas Witkowski committed
182
				    &paraviewAnimationFrames,
183
				    filename + ".pvd");
Thomas Witkowski's avatar
Thomas Witkowski committed
184
#endif
185
    }
186

Thomas Witkowski's avatar
Thomas Witkowski committed
187
188
189
190
191
192

    if (writeDofFormat) {
      DofWriter dofWriter(feSpace);
      dofWriter.writeFile(fn + ".dof", solutionVecs);
    }

193
    if (writeArhFormat)
194
      ArhWriter::write(paraFilename + ".arh", feSpace->getMesh(), solutionVecs);
195

196
#ifdef HAVE_PNG
197
198
    if (writePngFormat) {
      PngWriter pngWriter(dataCollectors[0]);
199
      pngWriter.writeFile(fn + ".png", pngType);
200

201
      MSG("PNG image file written to %s\n", (fn + ".png").c_str());
202
    }
203
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
204
205
206
207
208
209
210
211

    if (writePovrayFormat) {
      PovrayWriter povrayWriter(this, dataCollectors[0]);
      povrayWriter.writeFile(fn + ".pov");

      MSG("Povray script written to %s\n", (fn + ".pov").c_str());
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
212

Thomas Witkowski's avatar
Thomas Witkowski committed
213
    for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
214
      delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
215
216
  }

217
}