File: VectorInteractionFieldMaster.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 (414 lines) | stat: -rw-r--r-- 11,704 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
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
/////////////////////////////////////////////////////////////
//                                                         //
// 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              //
//                                                         //
/////////////////////////////////////////////////////////////

// --- TML includes ---
#include "tml/comm/comm.h"

// --- Project includes ---
#include "VectorInteractionFieldMaster.h"
#include "console.h"
#include "field_const.h"

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

//--- STL inculdes ---
#include <string>
#include <map>

using std::cout;
using std::endl;
using std::ofstream;
using std::string;
using std::multimap;


/*!
  Constructor. Setup master and broadcast parameters to slaves

  \param comm the communicator
  \param fieldname the name of the field to be saved
  \param igtype the type of interaction group for which the field is saved
  \param igname the name of the interaction group for which the field is saved
  \param filename the name of the file to be saved into or the base for the generation of the filenames if the saving format requires multiple files
  \param savetype the way to save data, currently supported are DX,SUM
  \param t0 the first timestep to be saved
  \param tend the last timestep to be saved
  \param dt the interval between timesteps to be saving
  \param checked choose between "full" and "checked" fields
*/ 
VectorInteractionFieldMaster::VectorInteractionFieldMaster(TML_Comm* comm,const string& fieldname,const string& igtype,const string& igname,const string& filename,const string& savetype,int t0,int tend,int dt,bool checked)
  :AFieldMaster(comm,fieldname,filename,savetype,t0,tend,dt)
{
  m_comm->broadcast_cont(fieldname);
  m_comm->broadcast(m_id);
  m_comm->broadcast_cont(igname);
  m_comm->broadcast_cont(igtype);
  int is_tagged=0;
  m_comm->broadcast(is_tagged);
  int is_checked=checked ? 1 : 0;
  m_comm->broadcast(is_checked);
}

void VectorInteractionFieldMaster::collect()
{
   m_comm->broadcast(m_id);

   switch(m_write_type){
   case WRITE_TYPE_SUM: collectSum(); break;
   case WRITE_TYPE_RAW2: collectFull2();break;
   case WRITE_TYPE_RAW_WITH_ID: collectFullWithID(); break;
   case WRITE_TYPE_RAW_WITH_POS_ID: collectFullWithPosID(); break;
   default: collectFull();
   }
}

/*!
  collect data and <pid1,pid2,pos> info
*/
void VectorInteractionFieldMaster::collectFullWithID()
{
  multimap<int,DataWithID> temp_mm;
  console.XDebug() << "VectorInteractionFieldMaster::collectFullWithID()\n";
  
  // send type of collect to slaves
  int coll_type=COLL_TYPE_FULL_WITH_ID;
  m_comm->broadcast(coll_type);
  // get data from slaves
  m_comm->gather(temp_mm);
  // collate receive data from multimap into single map
  console.XDebug() << temp_mm.size() << " data sets collected\n";
  int count=0;
  for(multimap<int,DataWithID>::iterator iter=temp_mm.begin();
      iter!=temp_mm.end();
      iter++){
    m_data_with_id.push_back(iter->second);
    count++;
    // write debug message every 10k writes
    if((count % 10000)==0){
      console.XDebug() << count << " data pushed into m_data_with_id\n";
    }  
  }
  console.XDebug() << "finished inserting " << count << " data into m_data_with_id\n";
}

/*!
  collect data and <pid1,pid2,pos1,pos2,ipos> info
*/
void VectorInteractionFieldMaster::collectFullWithPosID()
{
  multimap<int,DataWithPosID> temp_mm;

  // debug output 
  console.XDebug() << "VectorInteractionFieldMaster::collectFullWithPosID()\n";
  
  // send type of collect to slaves
  int coll_type=COLL_TYPE_FULL_WITH_POS_ID;
  m_comm->broadcast(coll_type);
  // get data from slaves
  m_comm->gather(temp_mm);
  // collate receive data from multimap into single map
  console.XDebug() << temp_mm.size() << " data sets collected\n";
  int count=0;
  for(multimap<int,DataWithPosID>::iterator iter=temp_mm.begin();
      iter!=temp_mm.end();
      iter++){
    m_data_with_pos_id.push_back(iter->second);
    count++;
    // write debug message every 10k writes
    if((count % 10000)==0){
      console.XDebug() << count << " data pushed into m_data_with_id\n";
    }  
  }
  console.XDebug() << "finished inserting " << count << " data into m_data_with_id\n";
}

/*!
  write data out as OpenDX compatible file

  \todo desciption
*/
void VectorInteractionFieldMaster::writeAsDX()
{
  //generate filename
  string fn=makeFilename();

  // write header 
  ofstream out_file(fn.c_str());
  out_file << "points = " << m_data.size() << endl;
  out_file << "format = ascii" << endl;
  out_file << "dependency = positions, positions" << endl;
  out_file << "interleaving = field" << endl;
  out_file << "field = locations, " << m_field_name << endl;
  out_file << "structure = 3-vector, 3-vector" << endl;
  out_file << "type = float, float  " << endl;
  out_file << "header =  marker \"Start\\n\"" << endl;
  out_file << endl << "end" << endl;
  out_file << "Start" << endl;
  
  // write data
  for(vector<pair<Vec3,Vec3> >::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    out_file << iter->first  << " " << iter->second << endl;
  }
  out_file.close(); 
  m_data.erase(m_data.begin(),m_data.end());
}

/*!
  sum data and write them out into a single continuous file
*/
void VectorInteractionFieldMaster::writeAsSUM()
{
  // sum data
  Vec3 sum_data=Vec3(0.0,0.0,0.0);
  for(vector<Vec3>::iterator iter=m_sum_vec.begin();
      iter!=m_sum_vec.end();
      iter++){
    sum_data=sum_data+(*iter);
  }
  // open file for appending
  ofstream out_file(m_file_name.c_str(),ios::app);
  // write data
  out_file << sum_data << endl;

  // close file
  out_file.close();
  //clean up
  m_sum_vec.erase(m_sum_vec.begin(),m_sum_vec.end());

}

/*!
  write data as pos1,pos2,value groups 
*/
void VectorInteractionFieldMaster::writeAsRAW2()
{
  //generate filename
  string fn=makeFilename();
  
  // write data
  ofstream out_file(fn.c_str());
  // check if file is sucessfully opened
  if(!out_file){
    console.Error() << "can not open output file " << fn << "\n";
  } else {
    int count=0;
    console.XDebug() << m_data2.size() << " vectors to be written\n";
    for(vector<IVecData2>::iterator iter=m_data2.begin();
        iter!=m_data2.end();
        iter++) {
#if 0
      out_file
        << (iter->first).template get<0>()
        << " "
        << (iter->first).template get<1>()
        << " "
        << (iter->first).template get<2>()
        << " "
        << (iter->first).template get<3>()
        << " "
        << (iter->first).template get<4>()
        << " "
        << iter->second
        << endl;
#else
      out_file
        << (iter->first).get<0>()
        << " "
        << (iter->first).get<1>()
        << " "
        << (iter->first).get<2>()
        << " "
        << (iter->first).get<3>()
        << " "
        << (iter->first).get<4>()
        << " "
        << iter->second
        << endl;
#endif
      count++;
      // write debug message every 10k writes
      if((count % 10000)==0){
        console.XDebug() << count << " vectors written\n";
      }
    }
    console.XDebug() << "finished writing " << count << " vectors \n";
    // close file
    out_file.close();
  }
  //clean up
  m_data2.clear();
}

/*!
  write data as pid1,pid2,ipos,value groups 
*/
void VectorInteractionFieldMaster::writeAsRawWithID()
{
  //generate filename
  string fn=makeFilename();
  
  // write data
  ofstream out_file(fn.c_str());
  // check if file is sucessfully opened
  if(!out_file){
    console.Error() << "can not open output file " << fn << "\n";
  } else {
    int count=0;
    console.XDebug() << m_data_with_id.size() << " vectors to be written\n";
    for(vector<DataWithID>::iterator iter=m_data_with_id.begin();
        iter!=m_data_with_id.end();
        iter++) {
      out_file
        << (iter->first).get<0>()
        << " "
        << (iter->first).get<1>()
        << " "
        << (iter->first).get<2>()
	<< " "
        << iter->second
        << endl;
      count++;
      // write debug message every 10k writes
      if((count % 10000)==0){
        console.XDebug() << count << " vectors written\n";
      }
    }
    console.XDebug() << "finished writing " << count << " vectors \n";
    // close file
    out_file.close();
  }
  //clean up
  m_data_with_id.clear();
}

/*!
  write data as pid1,pid2,ipos,value groups 
*/
void VectorInteractionFieldMaster::writeAsRawWithPosID()
{
  //generate filename
  string fn=makeFilename();
  
  // debug output
  console.XDebug() << "VectorInteractionFieldMaster::writeAsRawWithPosID() " << fn << "\n"; 

  // write data
  ofstream out_file(fn.c_str());
  // check if file is sucessfully opened
  if(!out_file){
    console.Error() << "can not open output file " << fn << "\n";
  } else {
    int count=0;
    console.XDebug() << m_data_with_id.size() << " vectors to be written\n";
    for(vector<DataWithPosID>::iterator iter=m_data_with_pos_id.begin();
        iter!=m_data_with_pos_id.end();
        iter++) {
      out_file
        << (iter->first).get<0>()
        << " "
        << (iter->first).get<1>()
        << " "
        << (iter->first).get<2>()
	<< " "
	<< (iter->first).get<3>()
        << " "
        << (iter->first).get<4>()
	<< " "
        << iter->second
        << endl;
      count++;
      // write debug message every 10k writes
      if((count % 10000)==0){
        console.XDebug() << count << " vectors written\n";
      }
    }
    console.XDebug() << "finished writing " << count << " vectors \n";
    // close file
    out_file.close();
  }
  //clean up
  m_data_with_pos_id.clear();
}

/*!
  collect full data set
*/
void VectorInteractionFieldMaster::collectFull()
{
  multimap<int,pair<Vec3,Vec3> > temp_mm;
  
  // send type of collect to slaves
  int coll_type=1;
  m_comm->broadcast(coll_type);

  // get data from slaves
  m_comm->gather(temp_mm);
  // collate receive data from multimap into single map  
  for(multimap<int,pair<Vec3,Vec3> >::iterator iter=temp_mm.begin();
      iter!=temp_mm.end();
      iter++){
    m_data.push_back(iter->second);
  }
}

/*!
  collect full data set, both particle positions
*/
void VectorInteractionFieldMaster::collectFull2()
{
  multimap<int,IVecData2> temp_mm;

  // send type of collect to slaves
  int coll_type=5;
  m_comm->broadcast(coll_type);
  // get data from slaves
  m_comm->gather(temp_mm);
  // collate receive data from multimap into single map
  console.XDebug() << temp_mm.size() << " data sets collected\n";
  int count=0;
  for(multimap<int,IVecData2>::iterator iter=temp_mm.begin();
      iter!=temp_mm.end();
      iter++){
    m_data2.push_back(iter->second);
    count++;
    // write debug message every 10k writes
    if((count % 10000)==0){
      console.XDebug() << count << " data pushed into m_data2\n";
    }  
  }
  console.XDebug() << "finished inserting " << count << " data into m_data2\n";
}

/*!
  collect sum of values only 
*/
void VectorInteractionFieldMaster::collectSum()
{
  multimap<int,Vec3> temp_mm;

  // send type of collect to slaves
  m_comm->broadcast(m_write_type);

  // get data from slaves
  m_comm->gather(temp_mm);

  // collate receive data from multimap into single map  
  for(multimap<int,Vec3>::iterator iter=temp_mm.begin();
      iter!=temp_mm.end();
      iter++){
    m_sum_vec.push_back(iter->second);
  } 
}