File: InteractionFieldMaster.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 (518 lines) | stat: -rw-r--r-- 14,943 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/////////////////////////////////////////////////////////////
//                                                         //
// 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 "InteractionFieldMaster.h"
#include "field_const.h"
#include "console.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
*/ 
ScalarInteractionFieldMaster::ScalarInteractionFieldMaster(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);
}

/*!
  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 tag the tag of the particles to be saved
  \param mask the mask to be applied to the tag
  \param checked choose between "full" and "checked" fields
*/
ScalarInteractionFieldMaster::ScalarInteractionFieldMaster(TML_Comm* comm,const string& fieldname,const string& igtype,const string& igname,const string& filename,const string& savetype,int t0,int tend,int dt,int tag,int mask,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=1;
  m_comm->broadcast(is_tagged);
  m_comm->broadcast(tag);
  m_comm->broadcast(mask);
  int is_checked=checked ? 1 : 0;
  m_comm->broadcast(is_checked);
}

void ScalarInteractionFieldMaster::collect()
{
  // send field id to slave
  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();
  }

  // if(m_write_type==WRITE_TYPE_SUM){
//     collectSum(); 
//   } else if (m_write_type==5){
//     collectFull2();
//   } else if (m_write_
//   } else {
//     collectFull();
//   }
}

/*!
  collect full data set
*/
void ScalarInteractionFieldMaster::collectFull()
{
  multimap<int,pair<Vec3,double> > temp_mm;
  
  // send type of collect to slaves
  int coll_type=COLL_TYPE_FULL;
  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,double> >::iterator iter=temp_mm.begin();
      iter!=temp_mm.end();
      iter++){
    m_data.push_back(iter->second);
  }
}
/*!
  collect full data set, both particle positions
*/
void ScalarInteractionFieldMaster::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 data and <pid1,pid2,pos> info
*/
void ScalarInteractionFieldMaster::collectFullWithID()
{
  // debug output 
  console.XDebug() << "ScalarInteractionFieldMaster::collectFullWithID()\n";

  multimap<int,DataWithID> temp_mm;

  // send type of collect to slaves
  int coll_type=COLL_TYPE_FULL_WITH_ID;
  m_comm->broadcast(coll_type);
  console.XDebug() << "after bcast coll_type\n";
  // get data from slaves
  m_comm->gather(temp_mm);
  console.XDebug() << "after gather temp_mm \n";
  // 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 ScalarInteractionFieldMaster::collectFullWithPosID()
{
  multimap<int,DataWithPosID> temp_mm;

  // debug output 
  console.XDebug() << "ScalarInteractionFieldMaster::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";
}

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

  // send type of collect to slaves
  int coll_type=COLL_TYPE_SUM;
  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,double>::iterator iter=temp_mm.begin();
      iter!=temp_mm.end();
      iter++){
    m_sum_vec.push_back(iter->second);
  } 
}

/*!
  write data out as OpenDX compatible file

  \todo desciption
*/
void ScalarInteractionFieldMaster::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, scalar" << 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,double> >::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());
}

/*!
  write data as pos1,pos2,value groups 
*/
void ScalarInteractionFieldMaster::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 ScalarInteractionFieldMaster::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 ScalarInteractionFieldMaster::writeAsRawWithPosID()
{
  //generate filename
  string fn=makeFilename();

  // debug output
  console.XDebug() << "ScalarInteractionFieldMaster::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();
}

/*!
  write data as pos1,value pairs 
*/
void ScalarInteractionFieldMaster::writeAsRAW()
{  
  // make filename
  string fn=makeFilename();
  // open file
  ofstream out_file(fn.c_str());
  
  // write data
  for(vector<pair<Vec3,double> >::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    out_file << iter->first  << " " << iter->second << endl;
  }
  out_file.close();

  // clean up
  m_data.erase(m_data.begin(),m_data.end());
}

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

  // set output precision to 10 significant digits, scientific format 
  out_file.setf(ios_base::scientific, ios_base::floatfield);
  out_file.precision(10);

  // 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());

}

/*!
  get the maximum of the data and write it out into a single continuous file
*/
void ScalarInteractionFieldMaster::writeAsMAX()
{
  // sum data
  double max_data=*(m_sum_vec.begin());
  for(vector<double>::iterator iter=m_sum_vec.begin();
      iter!=m_sum_vec.end();
      iter++){
    max_data=(*iter > max_data) ? *iter : max_data;
  }
  // open file for appending
  ofstream out_file(m_file_name.c_str(),ios::app);
  // write data
  out_file << max_data << endl;

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

}

/*!
  write data as a raw series of values, one row of values per time step,
  all timesteps into the same file
*/
void ScalarInteractionFieldMaster::writeAsRAW_SERIES()
{
  cerr << "ScalarInteractionFieldMaster::writeAsRAW_SERIES not implemented" << endl;
}