File: frame_geo.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 (421 lines) | stat: -rw-r--r-- 12,600 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/////////////////////////////////////////////////////////////
//                                                         //
// 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          //
//                                                         //
/////////////////////////////////////////////////////////////

/*
frame_geo.cpp:
  Written by Feng Chen, Vince Boros and Michele Griffa between
  November 2010 and January 2011, based on ../dump2vtk/frame_vtk.cpp
  (https://answers.launchpad.net/esys-particle/+question/132497).
*/

#include "frame_geo.h"

// --- project includes ---
#include "vec3.h"


// --- STL includes ---
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <map>
#include <utility>
#include <sstream>

using std::vector;
using std::string;
using std::ifstream;
using std::istream_iterator;
using std::back_inserter;
using std::map;
using std::pair;
using std::make_pair;
using std::ostringstream;

string dimension;

struct nr_part{
  Vec3 pos;
  Vec3 vel;
  Vec3 force;
  double rad;
  double mass;
  int tag;
  int id;
};

struct r_part{
  Vec3 pos;
  Vec3 init_pos;
  Vec3 vel;
  Vec3 force;
  Vec3 circ_shift;
  double rad;
  double mass;
  int tag;
  int id;
  double q1,q2,q3,q4;
  Vec3 angvel;
  int proc_id;
};

struct bond
{
  int id1;
  int id2;
  int tag;

  bond(int,int,int);
};

bond::bond(int i1,int i2,int t)
{
  id1=i1;
  id2=i2;
  tag=t;
}

int get_version(const string& infilename)
{
  string dummystring;
  int version;
  ifstream headerfile(infilename.c_str()); 
  // read token  
  headerfile >> dummystring;

  if(dummystring=="V"){ // if V -> new version 
    headerfile >> version ;
    cout << "version : " << version << endl;
  } else {
    cout << "pre- V.1 version" << endl;
    version=0;
  }
  headerfile.close();

  return version;
}

vector<string> get_filenames(const string& infilename, int version, string& dimension, float* geo_pbdry=NULL, double* bdbx=NULL)
{
  cout << "infilename : " << infilename << endl;
  ifstream headerfile(infilename.c_str()); 
  float dummy,xmax,ymax,zmax,xmin,ymin,zmin,geo_version;
  vector<string> filenames;
  string dummystring;

  if(version==0){
    headerfile >> dummy >> dummy >> dummy;
    headerfile >> dummystring >> dummy;
  } else if ((version==1)||(version==2) || (version==3)) {
    headerfile >> dummystring >> dummy;
    headerfile >> dummy >> dummy >> dummy;
    headerfile >> dummystring >> geo_version;
  } else {
    cerr << "unknown checkpoint version " << version << endl;
  }
  // get bounding box
  headerfile >> dummystring;
  headerfile >> xmin >> ymin >> zmin >> xmax >> ymax >> zmax ;
  
  //double boundingBox[6]={xmin,ymin,zmin,xmax,ymax,zmax};
  if (bdbx!=NULL)
  {
     bdbx[0]=xmin,bdbx[1]=ymin,bdbx[2]=zmin;
     bdbx[3]=xmax,bdbx[4]=ymax,bdbx[5]=zmax;
  }
  //std::cerr << bdbx[0] << " " << bdbx[1]<< " " << bdbx[2]<< " " << bdbx[3]<< " " << bdbx[4]<< " " << bdbx[5] << endl;

  //geo_pbdry[0] = geo_version;
  geo_pbdry[0] = 1.2;

  // periodic bdry
  headerfile >> dummystring >> geo_pbdry[1] >> geo_pbdry[2] >> geo_pbdry[3];

  // v. 1.1 geometry files didn't have dimension info
  if(geo_version>1.15){
    headerfile >> dummystring >> dimension;
  }

  // get file names
  copy(istream_iterator<string>(headerfile),istream_iterator<string>(),back_inserter(filenames));
  
  headerfile.close();
  
  //==================================
  
  //==================================

  cout << "nr. of filenames: " << filenames.size() << endl;
  return filenames;
}

void do_single_frame_geo(const string& infilename,const string& outfilename,int iframe,const string& listfilename)
{
  int version=get_version(infilename);
  string dimension;
  float geo_pbdry[4];
  double bdbx[6];
  vector<string> filenames=get_filenames(infilename,version,dimension,geo_pbdry,bdbx);
  
  std::cerr << bdbx[0] << " " << bdbx[1]<< " " << bdbx[2]<< " " << bdbx[3]<< " " << bdbx[4]<< " " << bdbx[5] << endl;
  
  map<int,nr_part> datamap;
  vector<bond> bonds;
  map<int,int> id2idx;
  map<int,int> idx2id;

  for(vector<string>::iterator iter=filenames.begin();
      iter!=filenames.end();
      iter++){
    cout << *iter << endl;
    ifstream datafile(iter->c_str());
    vector<double> pdata;
    // get particles
    Vec3 circ_shift;
    int npart;
    nr_part data;
    int id;
    Vec3 pos;
    Vec3 oldpos;
    Vec3 initpos;
    datafile >> npart;
    if(version<2){
      for(int i=0;i<npart;i++){
        datafile >> data.pos  >> data.rad >> id >> data.tag >> data.mass >> initpos >> oldpos >> data.vel >> data.force;
        data.id = id;
        datamap[id]=data;
      }
    } else {
      for(int i=0;i<npart;i++){
        datafile >> data.pos  >> data.rad >> id >> data.tag >> data.mass >> initpos >> oldpos >> data.vel >> data.force >> circ_shift;
        data.id = id;
        datamap[id]=data;
      }
    }

    // get bonds
    int ngrp;
    int nbond;
    string type;

    datafile >> ngrp;
    for(int ni=0;ni<ngrp;ni++){
      if(version>0){
        datafile >> type;
        if(type=="Bonded"){
          datafile >> nbond;
          for(int i=0;i<nbond;i++){
            int b1,b2,tag;
            datafile >> b1 >> b2 >> tag;
            bonds.push_back(bond(b1,b2,tag));
          }
        }
      } else { // pre - V1 snapshot -> assume bondend pair IG
        datafile >> nbond;
        for(int i=0;i<nbond;i++){
          int b1,b2,tag;
          datafile >> b1 >> b2 >> tag;
          bonds.push_back(bond(b1,b2,tag));
        }
      }
    }
    datafile.close();
  }
  // generate reverse mapping between particle id and point idx
  int count=0;
  for(map<int,nr_part>::iterator iter=datamap.begin();
      iter!=datamap.end();
      iter++){
    id2idx.insert(make_pair(iter->first,count));
    idx2id.insert(make_pair(count, iter->first));
    count++;
  }
  // open output file
  ostringstream geofilename;
  geofilename << outfilename << iframe << ".geo";
  ofstream geofile(geofilename.str().c_str());
  geofile << "LSMGeometry " << geo_pbdry[0] << endl;
  geofile << "BoundingBox " << bdbx[0] << " " << bdbx[1] << " " << bdbx[2] << " " << bdbx[3] << " " << bdbx[4] << " " << bdbx[5] << endl;
  geofile << "PeriodicBoundaries " << geo_pbdry[1] << " " << geo_pbdry[2] << " " << geo_pbdry[3] << endl;
  geofile << "Dimension " << dimension << endl;
  geofile << "BeginParticles" << endl;
  geofile << "Simple" << endl;
  geofile << datamap.size() << endl;
  for(map<int,nr_part>::iterator iter=datamap.begin(); iter!=datamap.end(); iter++){
    //geofile << (iter->second).pos << " "<< (iter->second).rad << " "<< geopid++ << " " << (iter->second).tag << endl;
    geofile << (iter->second).pos << " "<< (iter->second).rad << " "<< (iter->second).id << " " << (iter->second).tag << endl;
//    geopid++;
  }
  geofile << "EndParticles" << endl;
  //write bond info
  geofile << "BeginConnect" << endl;
  geofile << bonds.size() << endl;
  for(vector<bond>::iterator iter=bonds.begin(); iter!=bonds.end(); iter++){
    geofile << iter->id1 << " " << iter->id2 << " " << iter->tag << endl;
  }                                 
  geofile << "EndConnect" << endl;
  geofile.close();
}

void do_single_frame_geo_r(const string& infilename,const string& outfilename,int iframe,bool with_list,const string& listfilename,bool remove_xbonds,double bond_remove_dist,bool with_brklist,const string& brklistname)
{  
  int version=get_version(infilename);
  string dimension;
  float geo_pbdry[4];
  double bdbx[6];
  vector<string> filenames=get_filenames(infilename,version,dimension,geo_pbdry,bdbx);
  
  std::cerr << bdbx[0] << " " << bdbx[1]<< " " << bdbx[2]<< " " << bdbx[3]<< " " << bdbx[4]<< " " << bdbx[5] << endl;
  
  map<int,r_part> datamap;
  map<int,int> ng_id_map;
  map<int,float> ng_mass_map;
  vector<bond> bonds;
  map<int,int> id2idx;
  map<int,int> id_brk_map;

  int proc_cnt=0;
  for(vector<string>::iterator iter=filenames.begin();
      iter!=filenames.end();
      iter++){
    proc_cnt++;
    cout << *iter << " , " << proc_cnt << endl;
    ifstream datafile(iter->c_str());
    vector<double> pdata;
    // get particles
    int npart;
    r_part data;
    int id;
    Vec3 pos;
    Vec3 oldpos;
    datafile >> npart;
    if(version < 2){
      for(int i=0;i<npart;i++){
        datafile >> data.pos  >> data.rad >> data.id >> data.tag >> data.mass >> data.init_pos >> oldpos >> data.vel >> data.force
          >> data.q1 >> data.q2 >> data.q3 >> data.q4 >> data.angvel;
        data.proc_id=proc_cnt;
        id = data.id;
        datamap[id]=data;
      } 
    } else {
     for(int i=0;i<npart;i++){
       datafile >> data.pos  >> data.rad >> data.id >> data.tag >> data.mass >> data.init_pos >> oldpos >> data.vel >> data.force >> data.circ_shift
        >> data.q1 >> data.q2 >> data.q3 >> data.q4 >> data.angvel;
       data.proc_id=proc_cnt;
       id = data.id;
       datamap[id]=data;
      } 
    }  

    // get bonds
    int ngrp;
    int nbond;
    string type;

    
    datafile >> ngrp;
    for(int ni=0;ni<ngrp;ni++){
      if(version>0){
        datafile >> type;
        if((type=="Bonded") || (type=="RotBonded")){
          datafile >> nbond;
          if(remove_xbonds){
            for(int i=0;i<nbond;i++){
              int b1,b2,dummy;
              datafile >> b1 >> b2 >> dummy;
              double dist=(datamap[b1].pos-datamap[b2].pos).norm();
              if(dist < bond_remove_dist){
                bonds.push_back(bond(b1,b2,ni));
              }
            }
          } else {
            for(int i=0;i<nbond;i++){
              int b1,b2,dummy;
              datafile >> b1 >> b2 >> dummy;
              bonds.push_back(bond(b1,b2,ni));
            }
          }
        }
      } else { // pre - V1 snapshot -> assume bondend pair IG
        datafile >> nbond;
        for(int i=0;i<nbond;i++){
          int b1,b2,tag;
          datafile >> b1 >> b2 >> tag;
          bonds.push_back(bond(b1,b2,tag));
        }
      }
    }
    datafile.close();
  }

  // generate reverse mapping between particle id and point idx
  int count=0;
  for(map<int,r_part>::iterator iter=datamap.begin();
      iter!=datamap.end();
      iter++){
    id2idx.insert(make_pair(iter->first,count));
    count++;
  }

  // if option set, read list file into ng_id_map;
  if(with_list){
    ifstream listfile(listfilename.c_str());
    int id, nid;
    float gmass;
    while(!listfile.eof()){
      listfile >> id >> nid >> gmass;
      ng_id_map[id]=nid;
      ng_mass_map[id]=gmass;
    }
    listfile.close();
  }
  // if option set, read fracture list file int id_brk_map;
  if(with_brklist){
    std::cerr << "opening " << brklistname << std::endl;
    ifstream brklistfile(brklistname.c_str());
    int id, nbrk;
    while(!brklistfile.eof()){
      brklistfile >> id >> nbrk;
      id_brk_map[id]=nbrk;
    }
    brklistfile.close();
  }
  
  //std::cerr << "I am being called!!!!!!!!!!!!!!!!!!" << std::endl;
  
  // open output file
  ostringstream geofilename;
  geofilename << outfilename << iframe << ".geo";
  ofstream geofile(geofilename.str().c_str());
  geofile << "LSMGeometry " << geo_pbdry[0] << endl;
  geofile << "BoundingBox " << bdbx[0] << " " << bdbx[1] << " " << bdbx[2] << " " << bdbx[3] << " " << bdbx[4] << " " << bdbx[5] << endl;
  geofile << "PeriodicBoundaries " << geo_pbdry[1] << " " << geo_pbdry[2] << " " << geo_pbdry[3] << endl;
  geofile << "Dimension " << dimension << endl;
  geofile << "BeginParticles" << endl;
  geofile << "Simple" << endl;
  geofile << datamap.size() << endl;
  for(map<int,r_part>::iterator iter=datamap.begin(); iter!=datamap.end(); iter++){
    //geofile << (iter->second).pos << " "<< (iter->second).rad << " "<< geopid++ << " " << (iter->second).tag << endl;
    geofile << (iter->second).pos << " "<< (iter->second).rad << " "<< (iter->second).id << " " << (iter->second).tag << endl;
  }
  geofile << "EndParticles" << endl;
  //write bond info
  geofile << "BeginConnect" << endl;
  geofile << bonds.size() << endl;
  for(vector<bond>::iterator iter=bonds.begin(); iter!=bonds.end(); iter++){
    geofile << iter->id1 << " " << iter->id2 << " " << iter->tag << endl;
  }
  geofile << "EndConnect" << endl;
  geofile.close();
}