FileWriter.cc 8.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20
21


22
23
24
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>

25
#include "FileWriter.h"
26
#include "Initfile.h"
27
28
29
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
30
#include "VtkVectorWriter.h"
31
#include "PngWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
32
#include "PovrayWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
33
#include "DofWriter.h"
34
#include "ArhWriter.h"
35
#include "Arh2Writer.h"
Siqi Ling's avatar
Siqi Ling committed
36
#include "Arh3Writer.h"
37
38
39
40
41
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"
42
#include "DataCollector.h"
43

44
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
45
#include <mpi.h>
46
47
#endif

48
using namespace std;
49

50
51
52
namespace AMDiS
{
  namespace detail
53
  {
54
55
    template<>
    FileWriter<double>::FileWriter(std::string name_,
56
57
			  Mesh *mesh_,
			  SystemVector *vecs)
58
      : name(name_),
59
	mesh(mesh_)
60
61
62
    {
      initialize();

63
  /*
64
65
66
  * Removed by Siqi. not sure.
  * for (int i = 0; i < static_cast<int>(vecs->getSize()); i++)
  * TEST_EXIT(vecs->getDOFVector(0)->getFeSpace() == vecs->getDOFVector(i)->getFeSpace())
67
  * 	("All FeSpace have to be equal!\n");
68
69
70
71
  */ 
      feSpace = vecs->getDOFVector(0)->getFeSpace();
      solutionVecs.resize(vecs->getSize());
      for (int i = 0; i < static_cast<int>(vecs->getSize()); i++)
72
	solutionVecs[i] = vecs->getDOFVector(i);
Praetorius, Simon's avatar
Praetorius, Simon committed
73
74
      
      for (size_t i = 0; i < solutionVecs.size(); i++)
75
	solutionNames.push_back(solutionVecs[i]->getName());
76
77
78
79
80
81
82
      
      std::set<Mesh*> meshset;
      for (size_t i = 0; i < solutionVecs.size(); i++)
	if (solutionVecs[i])
	  meshset.insert(solutionVecs[i]->getFeSpace()->getMesh());

      multiMesh = meshset.size() > 1;
83
84
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
85

86
87
    template<>
    void FileWriter<double>::writeFiles(AdaptInfo *adaptInfo,
88
89
90
91
				bool force,
				int level,
				Flag flag,
				bool (*writeElem)(ElInfo*))
92
93
94
95
    {
      FUNCNAME("FileWriter<T>::writeFiles()");
      using namespace ::AMDiS::io;

96
97
      if (!super::doWriteTimestep(adaptInfo, force))
	return;
Thomas Witkowski's avatar
Thomas Witkowski committed
98

99
100
      //-----------------by Siqi---------------------//
      if (writeAMDiSFormat || writePeriodicFormat || writeParaViewFormat
101
	|| writeParaViewVectorFormat || writeParaViewAnimation
Siqi Ling's avatar
Siqi Ling committed
102
	|| writeDofFormat || writeArh1 || writePovrayFormat)
103
      {
104
105
106
	for (int i = 0; i < static_cast<int>(solutionVecs.size()); i++)
	  TEST_EXIT(solutionVecs[0]->getFeSpace() == solutionVecs[i]->getFeSpace())
	  ("All FeSpaces have to be equal!\n");
107
      }
108
	
109
110
111
112
      // Containers, which store the data to be written;
      std::vector<DataCollector<>*> dataCollectors(solutionVecs.size());

      if (writeElem) {
113
114
115
	for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
	  dataCollectors[i] = new DataCollector<>(feSpace, solutionVecs[i],
						level, flag, writeElem);
116
      } else {
117
118
119
120
121
	for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
	  dataCollectors[i] = new DataCollector<>(feSpace, solutionVecs[i],
						traverseLevel,
						flag | traverseFlag,
						writeElement);
122
123
      }
      
124
     std::string fn, fn2, fn_;
125
126
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      std::string paraFilename, postfix;
127
      super::getFilename(adaptInfo, fn, fn2, paraFilename, postfix);
128
129
130
131
      postfix += paraviewFileExt;
      fn_ = paraFilename;
#else
      super::getFilename(adaptInfo, fn);
132
      fn_ = fn2 = fn;
133
134
#endif
    
135
136

      if (writeAMDiSFormat) {
137
138
139
140
141
142
143
144
145
	MacroWriter::writeMacro(dataCollectors[0],
				const_cast<char*>((fn +  amdisMeshExt).c_str()),
				adaptInfo ? adaptInfo->getTime() : 0.0);
	MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str());

	ValueWriter::writeValues(dataCollectors[0],
				(fn + amdisDataExt).c_str(),
				adaptInfo ? adaptInfo->getTime() : 0.0);
	MSG("value file written to %s\n", (fn + amdisDataExt).c_str());
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
146
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
147

148
      if (writePeriodicFormat) {
149
150
151
	MacroWriter::writePeriodicFile(dataCollectors[0],
				      (fn + periodicFileExt).c_str());
	MSG("periodic file written to %s\n", (fn + periodicFileExt).c_str());
152
      }
153

154
      if (writeParaViewFormat) {
155
156
157
158
159
	std::string vtu_file = fn + paraviewFileExt;
	VtkWriter::Aux vtkWriter(&dataCollectors, 
				 solutionNames,
				 VtkWriter::Vtuformat(paraViewMode), (paraViewPrecision == 1), writeParaViewVectorFormat);
	vtkWriter.writeFile(vtu_file);
160
161

  #if HAVE_PARALLEL_DOMAIN_AMDIS
162
163
164
165
166
167
168
169
170
171
172
	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());
	  VtkWriter::detail::writeParallelFile(paraFilename + paraviewParallelFileExt,
				      MPI::COMM_WORLD.Get_size(),
				      filename, 
				      postfix,
				      solutionNames,
				      VtkWriter::Vtuformat(paraViewMode),
				      (paraViewPrecision == 1),
173
174
				      writeParaViewVectorFormat,
				      createSubDir > 0);
175
	}
176
177
  #endif

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

181
      // write vtu-vector files
Praetorius, Simon's avatar
Praetorius, Simon committed
182
      if (writeParaViewVectorFormat && !writeParaViewFormat) {
183
184
	VtkVectorWriter::writeFile(solutionVecs, fn_ + paraviewFileExt, true, writeAs3dVector);
	MSG("ParaView file written to %s\n", (fn_ + paraviewFileExt).c_str());
185
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
186

187
      if (writeParaViewAnimation) {
188
	std::string pvd_file = fn_ + paraviewFileExt;
189
  #if HAVE_PARALLEL_DOMAIN_AMDIS
190
191
	pvd_file = fn_ + paraviewParallelFileExt;
	if (MPI::COMM_WORLD.Get_rank() == 0)
192
  #endif
193
194
195
196
197
198
	{
	  VtkWriter::detail::updateAnimationFile(adaptInfo,
					pvd_file,
					&paraviewAnimationFrames,
					filename + ".pvd");
	}
199
      }
Siqi Ling's avatar
Siqi Ling committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
      
      if (writeArhAnimation) {
#if HAVE_PARALLEL_DOMAIN_AMDIS
	if (MPI::COMM_WORLD.Get_rank() == 0)
#endif	
	{
	  Arh3Writer::detail::updateAnimationFile(adaptInfo, 
						  &arhAnimationFrames,
						  createSubDir > 0,
						  indexLength,
						  indexDecimals,
						  filename + ".tarh");
	}
      }
214
  
Thomas Witkowski's avatar
Thomas Witkowski committed
215

216
      if (writeDofFormat) {
217
	DofWriter::writeFile(solutionVecs, fn + ".dof");
218
      }
219

220
      // write Arh files
Siqi Ling's avatar
Siqi Ling committed
221
      if (writeArh2)
222
	Arh2Writer::writeFile(solutionVecs, fn_ + ".arh");
Siqi Ling's avatar
Siqi Ling committed
223
224
      else if (writeArh1)
	ArhWriter::write(fn_ + ".arh", feSpace->getMesh(), solutionVecs);
225
226
227
228
      else if (writeArh3 || writeArhFormat) {
#if HAVE_PARALLEL_DOMAIN_AMDIS
	if (MPI::COMM_WORLD.Get_rank() == 0) {
	  TEST_EXIT(!multiMesh) ("Multi mesh arh writer is not supported in parallel.\n");
229
	  Arh3Writer::detail::writeParallelFile(paraFilename + arhParallelFileExt,
230
231
232
233
						feSpace->getMesh(), 
						createSubDir > 0);
	}
#endif
234
	Arh3Writer::writeFile(solutionVecs, fn2 + ".arh", false);
235
      }
236
237
238
239
    
    
  #ifdef HAVE_PNG
      if (writePngFormat) {
240
241
	PngWriter pngWriter(dataCollectors[0]);
	pngWriter.writeFile(fn + ".png", pngType);
242

243
	MSG("PNG image file written to %s\n", (fn + ".png").c_str());
244
245
      }
  #endif
246

247
      if (writePovrayFormat) {
248
249
	PovrayWriter povrayWriter(dataCollectors[0]);
	povrayWriter.writeFile(fn + ".pov");
250

251
	MSG("Povray script written to %s\n", (fn + ".pov").c_str());
252
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
253
254


255
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
256
	delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
257
258
    }

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
    template<> 
    string FileWriter<double>::getParaViewFilename(AdaptInfo* adaptInfo) const
    {
      string ret(filename);
      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");

        char formatStr[9];
        char timeStr[20];

        sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
        sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

        ret += timeStr;
      }
      return ret;
    }
278
279
  } // end namespace detail
} // end namespace AMDiS