File: DataExtractor.cpp

package info (click to toggle)
esys-particle 2.3.5%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,132 kB
  • sloc: cpp: 81,480; python: 5,872; makefile: 1,259; sh: 313; perl: 225
file content (253 lines) | stat: -rw-r--r-- 7,599 bytes parent folder | download | duplicates (5)
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
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2017 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 "DataExtractor.h"
#include "SnapFileHelp.h"

// --- Project includes ---
#include "ntable/src/handle.h"

// --- std. includes ---
#include <cmath>

using std::atan;
using std::sqrt;

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

using std::ifstream;
using std::ofstream;

/*!
  constructor

  \param x nr. of grid points in x-direction
  \param y nr. of grid points in y-direction
  \param z nr. of grid points in z-direction
  \param range grid spacing
  \param p0_global minimal corner (origin) of the global search space
*/
DataExtractor::DataExtractor(int x,int y,int z,double range,const Vec3& p0_global):
  m_data(x,y,z,range,0.0,p0_global,0,0,0)
{
  
}

/*!
  read snapshot

  \param filename the name of the "metadata" file, usually *_0.txt
*/
void DataExtractor::read(const string& infilename)
{
  ifstream headerfile(infilename.c_str());
  int version=get_version(infilename);
  vector<string> filenames=get_filenames(infilename,version);

  // get main files
  for(vector<string>::iterator iter=filenames.begin();
      iter!=filenames.end();
      iter++){
    cout << *iter << endl;
    ifstream datafile(iter->c_str());

    // get particles
    int npart;
    Vec3 pos;
    Vec3 oldpos;
    Vec3 initpos;
    Vec3 force;
    Vec3 vel;
    Vec3 angvel;
    Vec3 circ_shift;
    double rad;
    double mass;
    double q1,q2,q3,q4;
    int id;
    int tag;
    datafile >> npart;
    if(version<2){
      for(int i=0;i<npart;i++){
	// read data
	datafile >> pos  >> rad >> id >> tag >> mass >> initpos >> oldpos >> vel >> force >> q1 >> q2 >> q3 >> q4 >> angvel;
	DataParticle dp=DataParticle(pos,initpos,rad,id);
	m_data.insert(dp);
      }
    } else {
      for(int i=0;i<npart;i++){
	// read data
	datafile >> pos  >> rad >> id >> tag >> mass >> initpos >> oldpos >> vel >> force >> circ_shift >> q1 >> q2 >> q3 >> q4 >> angvel;
	DataParticle dp=DataParticle(pos,initpos,rad,id);
	m_data.insert(dp);	
      }
    }
    datafile.close();
  }
  std::cout << "inserted " << m_data.size() << "  particles " << std::endl;
  // m_data.build();
}

/*!
  write tensor data as unstructured grid VTK-XML file

  \param filename the name of the output file
  \param dataname the name of the data in the VTK file
*/
void DataExtractor::writeTensorDataVtk(const string& filename,const string& dataname)
{

}

/*!
  write scalar data as unstructured grid VTK-XML file

  \param filename the name of the output file
  \param dataname the name of the data in the VTK file
*/
void DataExtractor::writeScalarDataVtk(const string& filename,const string& dataname)
{
  ofstream vtkfile(filename.c_str());
  // write the file
  // write header 
  vtkfile << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\">\n";
  vtkfile << "<UnstructuredGrid>\n";
  vtkfile << "<Piece NumberOfPoints=\"" << m_data.size() << "\" NumberOfCells=\"0\">\n";
  // write particle pos
  vtkfile << "<Points>\n";
  vtkfile << "<DataArray NumberOfComponents=\"3\" type=\"Float64\" format=\"ascii\">\n";
  for(NeighborTable<DataParticle>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    vtkfile << iter->getPos() << "  ";
  }
  vtkfile << "</DataArray>\n";
  vtkfile << "</Points>\n";
  
  // --- write particle data ---
  // radius
  vtkfile << "<PointData Scalars=\"radius\">\n";
  vtkfile << "<DataArray type=\"Float64\" Name=\"radius\" NumberOfComponents=\"1\" format=\"ascii\">\n";
  for(NeighborTable<DataParticle>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    vtkfile << iter->getRad() << "  ";
  }
  vtkfile << "</DataArray>\n";
  vtkfile << "</PointData>\n";
  
  // data
  vtkfile << "<PointData Scalars=\"" << dataname << "\">\n";
  vtkfile << "<DataArray type=\"Float64\" Name=\"radius\" NumberOfComponents=\"1\" format=\"ascii\">\n";
  for(NeighborTable<DataParticle>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    vtkfile << iter->getScalarData() << "  ";
  }
  vtkfile << "</DataArray>\n";
  vtkfile << "</PointData>\n";

  // write empty cell block
  vtkfile << "<Cells>\n";
  vtkfile << "<DataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"connectivity\" format=\"ascii\">\n";
  vtkfile << "</DataArray>\n";
  vtkfile << "<DataArray type=\"UInt8\" NumberOfComponents=\"1\" Name=\"types\" format=\"ascii\">\n";
  vtkfile << "</DataArray>\n";
  vtkfile << "</Cells>\n";
  
  // write footer
  vtkfile << "</Piece>\n";
  vtkfile << "</UnstructuredGrid>\n";
  vtkfile << "</VTKFile>\n";
  
  // close file
  vtkfile.close();
}



/*!
  extract best fit strain tensors from the data and write the result
  into the tensor data member of the particles

  \param rad the search radius for the neighbour particles
*/
void DataExtractor::StrainToTensorData(double rad)
{
  // for each particle
  for(NeighborTable<DataParticle>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    // get list of neighbours
    T_Handle<NeighborTable<DataParticle>::particlelist> plist=m_data.getParticlesNearPoint(iter->getPos());
    std::cout << "pos: " << iter->getPos() << "  list size : " << plist->size() << std::endl; 
    if(plist->size()>3){
      // init sums
      Matrix3 M;
      Vec3 Su,Sv,Sw;
      // for each neighbour
      for(NeighborTable<DataParticle>::particlelist::iterator n_iter=plist->begin();
	  n_iter!=plist->end();
	  n_iter++){
	// get relative position & displacement
	Vec3 disp=(*n_iter)->getDisplacement()-iter->getDisplacement();
	Vec3 rpos=(*n_iter)->getPos()-iter->getPos();
	// --- udate sums
	// M
	M(0,0)+=rpos.X()*rpos.X();
	M(0,1)+=rpos.X()*rpos.Y();
	M(0,2)+=rpos.X()*rpos.Z();
	M(1,1)+=rpos.Y()*rpos.Y();
	M(1,2)+=rpos.Y()*rpos.Z();
	M(2,2)+=rpos.Z()*rpos.Z();
	// Su,v,w
	Su+=disp.X()*rpos;
	Sv+=disp.Y()*rpos;
	Sw+=disp.Z()*rpos;
      }
      // make M symmetric
      M(1,0)=M(0,1);
      M(2,0)=M(0,2);
      M(2,1)=M(1,2);
      // solve equations to get displacement gradient tensor components
      Vec3 Du=M.solve(Su);
      Vec3 Dv=M.solve(Sv);
      Vec3 Dw=M.solve(Sw);
      // make deformation gradient tensor
      Matrix3 A;
      A(0,0)=Du.X()+1; A(0,1)=Du.Y();     A(0,2)=Du.Z();
      A(1,0)=Dv.X();      A(1,1)=Dv.Y()+1; A(1,2)=Dv.Z();
      A(2,0)=Dw.X();     A(2,1)=Dw.Y();     A(2,2)=Dw.Z()+1;
      // right Cauchy-Green deformation tensor
      Matrix3 C=A.trans()*A;
      // write to tensor data member  of the particle
      iter->setTensorData(C);
    }
  }
}

/*!
  write maximum shear strain to scalar data member. Needs strain tensor calculation done before
*/
void DataExtractor::MaxShearToScalarData()
{
    for(NeighborTable<DataParticle>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
      // get eigenvalues of tensor data
      Vec3 D1,D2,D3;
      double e1,e2,e3;
      (iter->getTensorData()).eigen(D1,D2,D3,e1,e2,e3);
      // set scalar data to max shear strain
      iter->setScalarData(atan(sqrt(e3/e1)));
    }
}