FileWriter.cc 7.34 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
#include "boost/lexical_cast.hpp"
23
#include "FileWriter.h"
24
#include "Initfile.h"
25
26
27
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
28
#include "VtkVectorWriter.h"
29
#include "PngWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
30
#include "PovrayWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
31
#include "DofWriter.h"
32
#include "ArhWriter.h"
33
#include "Arh2Writer.h"
34
35
36
37
38
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"
39
#include "DataCollector.h"
40

41
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
42
#include <mpi.h>
43
44
#endif

45
using namespace std;
46

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

60
  /*
61
62
63
64
65
66
67
68
69
  * 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())
  * 	("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);
Praetorius, Simon's avatar
Praetorius, Simon committed
70
71
72
      
      for (size_t i = 0; i < solutionVecs.size(); i++)
	solutionNames.push_back(solutionVecs[i]->getName());
73
74
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    template<>
    void FileWriter<double>::writeFiles(AdaptInfo *adaptInfo,
				bool force,
				int level,
				Flag flag,
				bool (*writeElem)(ElInfo*))
    {
      FUNCNAME("FileWriter<T>::writeFiles()");
      using namespace ::AMDiS::io;

      if (timeModulo > 0.0) {
	if ((lastWriteTime != 0.0 && adaptInfo->getTime() < lastWriteTime + timeModulo) && !force)
	  return;
      } else {
	if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force) 
	  return;
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
93

94
      lastWriteTime = adaptInfo->getTime();
Thomas Witkowski's avatar
Thomas Witkowski committed
95

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
      //-----------------by Siqi---------------------//
      if (writeAMDiSFormat || writePeriodicFormat || writeParaViewFormat
	|| writeParaViewVectorFormat || writeParaViewAnimation
	|| writeDofFormat || (writeArhFormat && !writeArh2Format) || writePovrayFormat)
      {
	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");
      }
	
      // Containers, which store the data to be written;
      std::vector<DataCollector<>*> dataCollectors(solutionVecs.size());

      if (writeElem) {
	for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
	  dataCollectors[i] = new DataCollector<>(feSpace, solutionVecs[i],
						level, flag, writeElem);
      } else {
	for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
	  dataCollectors[i] = new DataCollector<>(feSpace, solutionVecs[i],
						traverseLevel,
						flag | traverseFlag,
						writeElement);
      }
      
      std::string fn = filename;

  #if HAVE_PARALLEL_DOMAIN_AMDIS
      std::string paraFilename = fn;
      fn += "-p" + boost::lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank()) + "-";
      std::string postfix = "";
  #endif

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

	fn += timeStr;
  #if HAVE_PARALLEL_DOMAIN_AMDIS
	paraFilename += timeStr;
	postfix += timeStr + paraviewFileExt;
  #endif
      } else {
  #if HAVE_PARALLEL_DOMAIN_AMDIS
	postfix += paraviewFileExt;
  #endif
      }
      
      
  #if HAVE_PARALLEL_DOMAIN_AMDIS
      std::string fn_ = paraFilename;
  #else
      std::string fn_ = fn;
  #endif

      if (writeAMDiSFormat) {
	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
168
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
169

170
171
172
173
174
      if (writePeriodicFormat) {
	MacroWriter::writePeriodicFile(dataCollectors[0],
				      (fn + periodicFileExt).c_str());
	MSG("periodic file written to %s\n", (fn + periodicFileExt).c_str());
      }
175

176
      if (writeParaViewFormat) {
Praetorius, Simon's avatar
Praetorius, Simon committed
177
178
179
	VtkWriter::Aux vtkWriter(&dataCollectors, 
				 solutionNames,
				 VtkWriter::Vtuformat(paraViewMode), (paraViewPrecision == 1), writeParaViewVectorFormat);
180
181
182
183
	vtkWriter.writeFile(fn + paraviewFileExt);

  #if HAVE_PARALLEL_DOMAIN_AMDIS
	if (MPI::COMM_WORLD.Get_rank() == 0) {
Praetorius, Simon's avatar
Praetorius, Simon committed
184
185
186
// 	  vector<string> componentNames;
// 	  for (unsigned int i = 0; i < dataCollectors.size(); i++)
// 	    componentNames.push_back(dataCollectors[i]->getValues()->getName());
187
188
189
190
191

	  VtkWriter::detail::writeParallelFile(paraFilename + paraviewParallelFileExt,
				      MPI::COMM_WORLD.Get_size(),
				      filename, 
				      postfix,
Praetorius, Simon's avatar
Praetorius, Simon committed
192
				      solutionNames,
193
				      VtkWriter::Vtuformat(paraViewMode),
Praetorius, Simon's avatar
Praetorius, Simon committed
194
195
				      (paraViewPrecision == 1),
				      writeParaViewVectorFormat);
196
197
198
199
	}
  #endif

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

202
      // write vtu-vector files
Praetorius, Simon's avatar
Praetorius, Simon committed
203
      if (writeParaViewVectorFormat && !writeParaViewFormat) {
204
205
206
	VtkVectorWriter::writeFile(solutionVecs, fn_ + paraviewFileExt, true, writeAs3dVector);
	MSG("ParaView file written to %s\n", (fn_ + paraviewFileExt).c_str());
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
207

208
209
210
211
212
213
214
215
216
217
218
219
220
221
      if (writeParaViewAnimation) {
	std::string fn_2 = fn_ + paraviewFileExt;
  #if HAVE_PARALLEL_DOMAIN_AMDIS
	fn_2 = fn_ + paraviewParallelFileExt;
	if (MPI::COMM_WORLD.Get_rank() == 0)
  #endif
	{
	  VtkWriter::detail::updateAnimationFile(adaptInfo,
					fn_2,
					&paraviewAnimationFrames,
					filename + ".pvd");
	}
      }
  
Thomas Witkowski's avatar
Thomas Witkowski committed
222

223
224
225
      if (writeDofFormat) {
	DofWriter::writeFile(solutionVecs, fn + ".dof");
      }
226

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
      // write Arh files
      if (!writeArh2Format && writeArhFormat)
	ArhWriter::write(fn_ + ".arh", feSpace->getMesh(), solutionVecs);
      else if (writeArh2Format)
	Arh2Writer::writeFile(solutionVecs, fn_ + ".arh");
    
    
  #ifdef HAVE_PNG
      if (writePngFormat) {
	PngWriter pngWriter(dataCollectors[0]);
	pngWriter.writeFile(fn + ".png", pngType);

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

243
244
245
      if (writePovrayFormat) {
	PovrayWriter povrayWriter(dataCollectors[0]);
	povrayWriter.writeFile(fn + ".pov");
246

247
248
	MSG("Povray script written to %s\n", (fn + ".pov").c_str());
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
249
250


251
252
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
	delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
253
254
    }

255
256
  } // end namespace detail
} // end namespace AMDiS