FileWriter.cc 5.95 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"
Praetorius, Simon's avatar
Praetorius, Simon committed
15
#include "FileWriter.hh"
16
#include "Initfile.h"
17
18
19
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
20
21
#include "VtkVectorWriter.h"
#include "VtkVectorWriter.hh"
22
#include "PngWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
23
#include "PovrayWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
24
#include "DofWriter.h"
25
#include "ArhWriter.h"
26
27
28
29
30
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"
31
#include "DataCollector.hh"
32

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

37
38
namespace AMDiS {

39
40
  using boost::lexical_cast;

41

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

    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);
  }
62

63

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

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

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

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

91
    std::string fn = filename;
92

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

99
100
101
102
    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
103

104
105
106
      char formatStr[9];
      char timeStr[20];

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

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

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

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

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

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

#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0)
	vtkWriter.writeParallelFile(paraFilename + paraviewParallelFileExt,
				    MPI::COMM_WORLD.Get_size(),
148
149
				    filename, postfix,
				    dataCollectors.size());
Thomas Witkowski's avatar
Thomas Witkowski committed
150
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
151

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

155
    if (writeParaViewVectorFormat) {
Praetorius, Simon's avatar
Praetorius, Simon committed
156
      VtkVectorWriter::writeFile(solutionVecs, fn + paraviewFileExt, true, writeAs3dVector);
Praetorius, Simon's avatar
Praetorius, Simon committed
157

Praetorius, Simon's avatar
Praetorius, Simon committed
158
159
160
161
//       VtkVectorWriter::Impl<double> vtkVectorWriter(&dataCollectors, writeAs3dVector);
//       vtkVectorWriter.setCompression(compression);
//       vtkVectorWriter.setWriteAs3dVector(writeAs3dVector);
//       vtkVectorWriter.writeFile(fn + paraviewFileExt);
Praetorius, Simon's avatar
Praetorius, Simon committed
162

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

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

      }
#else
176
177
      VtkWriter::updateAnimationFile(adaptInfo,
				    fn + paraviewFileExt,
Thomas Witkowski's avatar
Thomas Witkowski committed
178
				    &paraviewAnimationFrames,
179
				    filename + ".pvd");
Thomas Witkowski's avatar
Thomas Witkowski committed
180
#endif
181
    }
182

Thomas Witkowski's avatar
Thomas Witkowski committed
183
184
185
186
187
188

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

189
190
191
    if (writeArhFormat)
      ArhWriter::write(fn + ".arh", feSpace->getMesh(), solutionVecs);

192
#ifdef HAVE_PNG
193
194
    if (writePngFormat) {
      PngWriter pngWriter(dataCollectors[0]);
195
      pngWriter.writeFile(fn + ".png", pngType);
196

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

    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
208

Thomas Witkowski's avatar
Thomas Witkowski committed
209
    for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
210
      delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
211
212
  }

213
}