FileWriter.cc 5.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
168
169
170
171
172
173
174
175
176
177
#include "FileWriter.h"
#include "SystemVector.h"
#include "Parameters.h"
#include "TecPlotWriter.h"
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
#include "DataCollector.h"
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"

namespace AMDiS {

  FileWriter::FileWriter(const ::std::string &name_, 
			 Mesh *mesh_,
			 DOFVector<double> *vec)
    : name(name_),
      tecplotExt(".tec"),
      amdisMeshExt(".mesh"),
      amdisDataExt(".dat"),
      paraViewFileExt(".vtu"),
      periodicFileExt(".per"),
      writeTecPlotFormat(0),
      writeAMDiSFormat(0),
      writeParaViewFormat(0),
      writePeriodicFormat(0),
      appendIndex(0),
      indexLength(5),
      indexDecimals(3),
      tsModulo(1),
      mesh(mesh_)
  {
    readParameters();

    feSpace = vec->getFESpace();

    solutionVecs_.resize(1);
    solutionVecs_[0] = vec;
  }

  FileWriter::FileWriter(const ::std::string &name_, 
			 Mesh *mesh_,
			 ::std::vector< DOFVector<double>* > vecs)
    : name(name_),
      tecplotExt(".tec"),
      amdisMeshExt(".mesh"),
      amdisDataExt(".dat"),
      paraViewFileExt(".vtu"),
      periodicFileExt(".per"),
      writeTecPlotFormat(0),
      writeAMDiSFormat(0),
      writeParaViewFormat(0),
      writePeriodicFormat(0),
      appendIndex(0),
      indexLength(5),
      indexDecimals(3),
      tsModulo(1),
      mesh(mesh_)
  {
    FUNCNAME("FileWriter::FileWriter()");

    readParameters();

    for (int i = 0; i < static_cast<int>(vecs.size()); i++) {
      TEST_EXIT(vecs[0]->getFESpace() == vecs[i]->getFESpace())
	("All FESpace have to be equal!\n");
    }

    feSpace = vecs[0]->getFESpace();
    solutionVecs_ = vecs;
  }

  void FileWriter::readParameters()
  {
    FUNCNAME("FileWriter::readParamters()");

    GET_PARAMETER(0, name + "->filename", &filename);
    GET_PARAMETER(0, name + "->TecPlot format", "%d", &writeTecPlotFormat);
    GET_PARAMETER(0, name + "->TecPlot ext", &tecplotExt);
    GET_PARAMETER(0, name + "->AMDiS format", "%d", &writeAMDiSFormat);
    GET_PARAMETER(0, name + "->AMDiS mesh ext", &amdisMeshExt);
    GET_PARAMETER(0, name + "->AMDiS data ext", &amdisDataExt);
    GET_PARAMETER(0, name + "->ParaView format", "%d", &writeParaViewFormat);
    GET_PARAMETER(0, name + "->ParaView ext", &paraViewFileExt);
    GET_PARAMETER(0, name + "->Periodic format", "%d", &writePeriodicFormat);
    GET_PARAMETER(0, name + "->Periodic ext", &periodicFileExt);
    GET_PARAMETER(0, name + "->append index", "%d", &appendIndex);
    GET_PARAMETER(0, name + "->index length", "%d", &indexLength);
    GET_PARAMETER(0, name + "->index decimals", "%d", &indexDecimals);
    GET_PARAMETER(0, name + "->write every i-th timestep", "%d", &tsModulo);
  }

  void FileWriter::writeFiles(AdaptInfo *adaptInfo,
			      bool force,
			      int level,
			      Flag flag,
			      bool (*writeElem)(ElInfo*))
  {
    FUNCNAME("FileWriter::writeFiles()");

    if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force) 
      return;

    ::std::string fn = 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];

      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);

      char timeStr[20];

      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
    }

    ::std::vector< DataCollector* > dc(solutionVecs_.size());

    if (writeElem) {
      for (int i = 0; i < static_cast<int>(dc.size()); i++) {
	dc[i] = NEW DataCollector(feSpace, solutionVecs_[i], 
				  level, flag, writeElem);
      }
    } else {
      for (int i = 0; i < static_cast<int>(dc.size()); i++) {
	dc[i] = NEW DataCollector(feSpace, solutionVecs_[i], 
				  traverseLevel, flag | traverseFlag, writeElement);
      }
    }


    if (writeTecPlotFormat) {
      TecPlotWriter<DOFVector<double> >::writeValues(solutionVecs_[0], 
						     const_cast<char*>((fn + tecplotExt).c_str()), 
						     solutionVecs_[0]->getName().c_str());
      MSG("TecPlot file written to %s\n", (fn + tecplotExt).c_str());
    }

    if (writeAMDiSFormat) {
      TEST_EXIT(mesh)("no mesh\n");

      MacroWriter::writeMacro(dc[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(dc[0],
			       (fn + amdisDataExt).c_str(),
			       adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("value file written to %s\n", (fn + amdisDataExt).c_str());   
    }

    if (writePeriodicFormat) {
      MacroWriter::writePeriodicFile(dc[0], 
				     (fn + periodicFileExt).c_str());
      MSG("periodic file written to %s\n", (fn + periodicFileExt).c_str());
    }
    
    if (writeParaViewFormat) {
      VtkWriter::writeFile(&dc, const_cast<char*>( (fn + paraViewFileExt).c_str()));
      MSG("ParaView file written to %s\n", (fn + paraViewFileExt).c_str());
    }

    for (int i = 0; i < static_cast<int>(dc.size()); i++) {
      DELETE dc[i];
    }
  }
}