FileWriter.cc 6.35 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

71
72
73
74
75
76
77
78
79
    if (timeModulo > 0.0) {
      if ((adaptInfo->getTime() < lastWriteTime + timeModulo) && !force)
	return;
    } else {
      if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force) 
	return;
    }

    lastWriteTime = adaptInfo->getTime();
80

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

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

96
    std::string fn = filename;
97

98
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
99
    std::string paraFilename = fn;
100
    fn += "-p" + lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank()) + "-";
Thomas Witkowski's avatar
Thomas Witkowski committed
101
    std::string postfix = "";
102
103
#endif

104
105
106
107
    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
108

109
110
111
      char formatStr[9];
      char timeStr[20];

Thomas Witkowski's avatar
Thomas Witkowski committed
112
      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
113
114
115
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
Thomas Witkowski's avatar
Thomas Witkowski committed
116
117
#if HAVE_PARALLEL_DOMAIN_AMDIS
      paraFilename += timeStr;
118
      postfix += timeStr + paraviewFileExt;
119
120
121
122
#endif
    } else {
#if HAVE_PARALLEL_DOMAIN_AMDIS
      postfix += paraviewFileExt;
Thomas Witkowski's avatar
Thomas Witkowski committed
123
#endif
124
125
126
    }

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

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

    if (writePeriodicFormat) {
Praetorius, Simon's avatar
Praetorius, Simon committed
139
      MacroWriter::writePeriodicFile(dataCollectors[0],
140
141
142
				     (fn + periodicFileExt).c_str());
      MSG("periodic file written to %s\n", (fn + periodicFileExt).c_str());
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
143

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

#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
150
151
152
153
154
      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
155
156
	vtkWriter.writeParallelFile(paraFilename + paraviewParallelFileExt,
				    MPI::COMM_WORLD.Get_size(),
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
157
158
159
160
				    filename, 
				    postfix,
				    componentNames);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
161
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
162

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

166
    if (writeParaViewVectorFormat) {
167
168
#if HAVE_PARALLEL_DOMAIN_AMDIS
      VtkVectorWriter::writeFile(solutionVecs, paraFilename + paraviewFileExt, true, writeAs3dVector);
169
      MSG("ParaView file written to %s\n", (paraFilename + paraviewFileExt).c_str());
170
#else
Praetorius, Simon's avatar
Praetorius, Simon committed
171
      VtkVectorWriter::writeFile(solutionVecs, fn + paraviewFileExt, true, writeAs3dVector);
172
      MSG("ParaView file written to %s\n", (fn + paraviewFileExt).c_str());
173
#endif
Praetorius, Simon's avatar
Praetorius, Simon committed
174

175
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
176

177
    if (writeParaViewAnimation) {
Thomas Witkowski's avatar
Thomas Witkowski committed
178
179
#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0) {
180
181
	VtkWriter::updateAnimationFile(adaptInfo,
				      paraFilename + paraviewParallelFileExt,
Thomas Witkowski's avatar
Thomas Witkowski committed
182
183
184
185
186
				      &paraviewAnimationFrames,
				      filename + ".pvd");

      }
#else
187
188
      VtkWriter::updateAnimationFile(adaptInfo,
				    fn + paraviewFileExt,
Thomas Witkowski's avatar
Thomas Witkowski committed
189
				    &paraviewAnimationFrames,
190
				    filename + ".pvd");
Thomas Witkowski's avatar
Thomas Witkowski committed
191
#endif
192
    }
193

Thomas Witkowski's avatar
Thomas Witkowski committed
194
195
196
197
198
199

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

200
#if HAVE_PARALLEL_DOMAIN_AMDIS
201
    if (writeArhFormat)
202
      ArhWriter::write(paraFilename + ".arh", feSpace->getMesh(), solutionVecs);
203
204
#else    
    if (writeArhFormat)
205
      ArhWriter::write(fn + ".arh", feSpace->getMesh(), solutionVecs);
206
207
#endif

208

209
#ifdef HAVE_PNG
210
211
    if (writePngFormat) {
      PngWriter pngWriter(dataCollectors[0]);
212
      pngWriter.writeFile(fn + ".png", pngType);
213

214
      MSG("PNG image file written to %s\n", (fn + ".png").c_str());
215
    }
216
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
217
218
219
220
221
222
223
224

    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
225

Thomas Witkowski's avatar
Thomas Witkowski committed
226
    for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
227
      delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
228
229
  }

230
}