Repository navigation
Expand file tree
/
Copy pathWriter.cpp
More file actions
174 lines (152 loc) · 3.33 KB
/
Copy pathWriter.cpp
File metadata and controls
174 lines (152 loc) · 3.33 KB
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
#include<stdio.h>
#include<cstdlib>
#include<string>
#include<iostream>
#include<iomanip>
#include<sstream>
#include<fstream>
#include<cmath>
#include "Writer.hpp"
// Empty constructor
Writer::Writer()
:M_littleEndianMachine(true),
M_is_binary(false),
_meshSetted(false),
_folderCreated(false),
_mesh(NULL),
M_count(0),
M_writing_count(0),
M_verbose(1)
{
M_littleEndianMachine=isLittleEndian();
M_dir="./";
M_prefix="heart";
_variablesContainer.clear();
}
// Constructor with mesh assignement
Writer::Writer(Mesh * theMesh)
:M_littleEndianMachine(true),
M_is_binary(false),
_meshSetted(false),
_folderCreated(false),
_mesh(NULL),
M_count(0),
M_writing_count(0),
M_verbose(1)
{
M_littleEndianMachine=isLittleEndian();
M_dir="./";
M_prefix="heart";
setMesh(theMesh);
_variablesContainer.clear();
}
// Destructor
Writer::~Writer()
{
_mesh=NULL;
_meshSetted=false;
_variablesContainer.clear();
}
// Function to set the mesh
void Writer::setMesh(Mesh * theMesh)
{
_meshSetted=true;
_mesh=theMesh;
};
// Function to set prefix and directory names
void Writer::setOutput(std::string theDir,std::string thePrefix)
{
setOutputDir(theDir);
setPrefixName(thePrefix);
}
// Adds a post-process variable within the container
void Writer::addVariable( unkVecType * theValues, std::string theName)
{
Variable theVariable;
theVariable.values = theValues;
theVariable.nameVar = theName;
_variablesContainer.push_back(theVariable);
}
// Generic postprocess function
void Writer::postProcess(const double & time)
{
//Create the folder, if not already done
if(!_folderCreated)
{
std::string command="mkdir -p " + M_dir;
int ierr=system(command.c_str());
if(ierr)
{
std::cerr<<"directory "<< M_dir<<" not created"<<std::endl;
_folderCreated = false;
}
else
{
_folderCreated = true;
}
}
}
//used to evaluate the post-fix of the file; returns false if no data has to be written
bool Writer::M_getPostfix()
{
std::ostringstream index;
bool dump=true;
if( std::fmod( float( M_count ), float( M_verbose ) ) == 0.0 )
{
++M_writing_count ;
dump=true;
index << ( M_count / M_verbose );
switch ( index.str().size() )
{
case 1:
M_postfix = "0000" + index.str();
break;
case 2:
M_postfix = "000" + index.str();
break;
case 3:
M_postfix = "00" + index.str();
break;
case 4:
M_postfix = "0" + index.str();
break;
case 5:
M_postfix = index.str();
break;
}
}
else
{
M_postfix = "*****";
dump=false;
}
++M_count;
return(dump);
}
//used to convert to big endian
void Writer::SwapBytes(void *pv, size_t n)
{
char *p = static_cast<char*>(pv);
size_t lo, hi;
for(lo=0, hi=n-1; hi>lo; lo++, hi--)
{
char tmp=p[lo];
p[lo] = p[hi];
p[hi] = tmp;
}
}
//used to evaluate endianess
bool Writer::isLittleEndian()
{
bool littleEndianMachine=true;
int x = 1;
if(*(char *)&x == 1)
{
littleEndianMachine=true;
}
else
{
littleEndianMachine=false;
}
return(littleEndianMachine);
}