File: fracwriter.cpp

package info (click to toggle)
esys-particle 2.3.4%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,036 kB
  • ctags: 10,805
  • sloc: cpp: 80,009; python: 5,872; makefile: 1,243; sh: 313; perl: 225
file content (303 lines) | stat: -rw-r--r-- 8,302 bytes parent folder | download
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2014 by The University of Queensland //
// Centre for Geoscience Computing                         //
// http://earth.uq.edu.au/centre-geoscience-computing      //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.apache.org/licenses/LICENSE-2.0          //
//                                                         //
/////////////////////////////////////////////////////////////

#include "fracwriter.h"

// --- IO includes ---
#include <fstream>
#include <iostream>
#include <sstream>

using std::ostringstream;
using std::ofstream;
using std::endl;

fwdata::fwdata(const FracFrame::fdata& fd,int t)
{
  pos=fd.pos;
  normal=fd.normal;
  size=fd.size;
  id1=fd.id1;
  id2=fd.id2;
  time=t;
  tag=fd.tag;
  dist=fd.dist;
}

/*!
  constructor
*/
FracWriter::FracWriter()
{
  with_plane=false;
}

/*!
  add point data to the writer
*/
void FracWriter::addData(const vector<FracFrame::fdata>& newdata,int t)
{
  for(vector<FracFrame::fdata>::const_iterator iter=newdata.begin();
      iter!=newdata.end();
      iter++){
    m_data.push_back(fwdata(*iter,t));
    m_nbrk_map[iter->id1]++;
    m_nbrk_map[iter->id2]++;
  }
  std::cerr << "added " << newdata.size() << " fractures" << std::endl;
}

/*!
  add a plane to the data writer
*/
void FracWriter::addPlane(const Plane3D& P)
{
  with_plane=true;
  Vec3 center=P.getPos();
  double maxdist=0.0;
  for(vector<fwdata>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    double dist=(center-(iter->pos)).norm();
    maxdist=(dist > maxdist) ? dist : maxdist;
  }
  Vec3 v1=maxdist*P.GetV();
  Vec3 v2=maxdist*P.GetU();
  m_c1=center-v1-v2;
  m_c2=center+v1-v2;
  m_c3=center+v1+v2;
  m_c4=center-v1+v2;
}
 
/*!
  write data int VTK-XML file

  \param filename the filename
*/
void FracWriter::write(const string& filename)
{
  // open file
  ofstream outfile(filename.c_str());

  // write VTK-XML header
  outfile << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\">\n";
  outfile << "<UnstructuredGrid>\n";
  outfile << "<Piece NumberOfPoints=\"" << m_data.size()<< "\" NumberOfCells=\"0\">\n";

  // write positions
  outfile << "<Points>\n";
  outfile << "<DataArray NumberOfComponents=\"3\" type=\"Float64\" format=\"ascii\">\n";
  for(vector<fwdata>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    outfile << iter->pos << endl;
  }
  outfile << "</DataArray>\n";
  outfile << "</Points>\n";
  
  // write times
  outfile << "<PointData Scalars=\"time\">\n";
  outfile << "<DataArray type=\"Int32\" Name=\"time\" NumberOfComponents=\"1\" format=\"ascii\">\n";
  for(vector<fwdata>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    outfile << iter->time << endl;
  }
  outfile << "</DataArray>\n";
  // write radii
  outfile << "<DataArray type=\"Float32\" Name=\"radius\" NumberOfComponents=\"1\" format=\"ascii\">\n";
  for(vector<fwdata>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    outfile << iter->size << endl;
  }
  outfile << "</DataArray>\n";
 // write particle distances
  outfile << "<DataArray type=\"Float32\" Name=\"distance\" NumberOfComponents=\"1\" format=\"ascii\">\n";
  for(vector<fwdata>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    outfile << iter->dist << endl;
  }
  outfile << "</DataArray>\n";
  // write normals
  outfile << "<DataArray type=\"Float32\" Name=\"normal\" NumberOfComponents=\"3\" format=\"ascii\">\n";
  for(vector<fwdata>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    outfile << iter->normal.X() << " " << iter->normal.Y() << " " << iter->normal.Z()  << endl;
  }
  outfile << "</DataArray>\n";
  
  // write (ex-)bond tags
  outfile << "<DataArray type=\"Int32\" Name=\"tag\" NumberOfComponents=\"1\" format=\"ascii\">\n";
  for(vector<fwdata>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    outfile << iter->tag  << endl;
  }
  outfile << "</DataArray>\n";
  outfile << "</PointData>\n";
  
  // write empty cell block
  outfile << "<Cells>\n";
  outfile << "<DataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"connectivity\" format=\"ascii\">\n";
  outfile << "</DataArray>";
  outfile << "<DataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"offsets\" format=\"ascii\">\n";
  outfile << "</DataArray>\n";
  outfile << "<DataArray type=\"UInt8\" NumberOfComponents=\"1\" Name=\"types\" format=\"ascii\">\n";
  outfile << "</DataArray>\n";  
  outfile << "</Cells>\n";

  // write footer
  outfile << "</Piece>\n";
  outfile << "</UnstructuredGrid>\n";
  outfile << "</VTKFile>\n";


  // close file
  outfile.close();

  // if we have a plane, setup filename and write it
  if(with_plane){
    ostringstream planefilename;

    planefilename << "plane_" << filename;
    writePlane(planefilename.str());
  }
}


/*!
  write data as text file

  \param filename the filename
*/
void FracWriter::writeText(const string& filename)
{
  // open file
  ofstream outfile(filename.c_str());

  // write positions & times
  for(vector<fwdata>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    outfile << iter->pos << " " << iter->size << " " << iter->time << " " << iter->normal << " " << iter->id1 << " " << iter->id2 << endl;
  }

  // close file
  outfile.close();
}

/*!
  write data as text file

  \param filename the filename
*/
void FracWriter::writeParticleList(const string& filename)
{
  // open file
  ofstream outfile(filename.c_str());

  // write positions & times
  for(map<int,int>::iterator iter=m_nbrk_map.begin();
      iter!=m_nbrk_map.end();
      iter++){
    outfile << iter->first << " " << iter->second << endl;
  }

  // close file
  outfile.close();
}

/*!
  write fracture distribution profile (y-direction)

  \param ymin minimum y-coordinate
  \param ymax maximum y-coordinate
  \nbin number of bins in the distribution
  \param filename the filename
*/								
void FracWriter::writeProfile(double ymin,double ymax,int nbin,const string& filename)
{
  // open file
  ofstream outfile(filename.c_str());
  
  vector<double> prof=vector<double>(nbin,0.0);

  // calculate profile
  for(vector<fwdata>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    int bnr=int(floor(nbin*((iter->pos.Y()-ymin)/(ymax-ymin))));
    if((bnr>=0) && (bnr<nbin)){
      prof[bnr]+=(iter->size)*(iter->size);
    }
  }

  // write profile
  for(int i=0;i<nbin;i++){
    outfile << ymin+(double(i)+0.5)*((ymax-ymin)/double(nbin)) << " " << prof[i] << std::endl;
  }
  // close file
  outfile.close();
}

/*!
  write plane data int VTK-XML file
*/
void FracWriter::writePlane(const string& filename)
{
  // open file
  ofstream outfile(filename.c_str());

  // write VTK-XML header
  outfile << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\">\n";
  outfile << "<UnstructuredGrid>\n";
  outfile << "<Piece NumberOfPoints=\"4\" NumberOfCells=\"1\">\n";

  // write positions
  outfile << "<Points>\n";
  outfile << "<DataArray NumberOfComponents=\"3\" type=\"Float64\" format=\"ascii\">\n";
  outfile << m_c1 << endl;
  outfile << m_c2 << endl;
  outfile << m_c3 << endl;
  outfile << m_c4 << endl;
  outfile << "</DataArray>\n";
  outfile << "</Points>\n";
  
  // write cells
  outfile << "<Cells>\n";
  outfile << "<DataArray type=\"Int32\" Name=\"connectivity\">\n";
  outfile << "0 1 2 3\n";
  outfile << "</DataArray>\n";
  outfile << "<DataArray type=\"Int32\" Name=\"offsets\">\n";
  outfile << "4\n";
  outfile << "</DataArray>\n";
  outfile << "<DataArray type=\"UInt8\" Name=\"types\">\n";
  outfile << "9\n";
  outfile << "</DataArray>\n";
  outfile << "</Cells>\n";

  // write dummy cell data
  outfile << "<CellData>\n";
  outfile << "<DataArray NumberOfComponents=\"1\" type=\"Float32\" Name=\"dummy\">\n";
  outfile << "1.0\n";
  outfile << "</DataArray>\n";
  outfile << "</CellData>\n";
  outfile << "</Piece>\n";
  outfile << "</UnstructuredGrid>\n";
  outfile << "</VTKFile>\n";
  // close file
  outfile.close();
 
}