File: WallFieldMaster.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 (181 lines) | stat: -rw-r--r-- 5,433 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
/////////////////////////////////////////////////////////////
//                                                         //
// 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 "Fields/WallFieldMaster.h"

// --- STL includes ---
#include <map>

using std::multimap;

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

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

/*!
  write the data out as series of raw data, i.e.

  x_0,0 y_0,0 z_0,0 x_1,0 y_1,0 z_1,0...
  x_0,1 y_0,1 z_0,1 x_1,1 y_1,1 z_1,1...
  ...
  where x_i,j is the x-value from wall i at time j
*/
void VectorWallFieldMaster::writeAsRAW_SERIES()
{
  console.XDebug() << "VectorWallFieldMaster::writeAsRAW_SERIES()\n";

  // open file
  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
  for(map<int,Vec3>::iterator iter=m_data.begin();
      iter!=m_data.end();
      iter++){
    out_file << iter->second << "  ";
  }
  out_file << endl; 
  out_file.close();
  m_data.clear();
}

/*!
  write data out as SILO file (if supported)
*/
void VectorWallFieldMaster::writeAsSILO()
{
#if HAVE_SILO
  bool append;
  DBfile* dbfile = openSiloFile(append);

  if (!dbfile) {
    console.Error() << "writeAsSILO not supported";
    return;
  }

  // prepare data structures
  std::vector<float> x, y, z;
  x.clear(); y.clear(); z.clear();

  for (map<int,Vec3>::iterator iter=m_data.begin();
       iter != m_data.end();
       iter++) {
    const Vec3& v = iter->second;
    x.push_back(v.X());
    y.push_back(v.Y());
    z.push_back(v.Z());
  }

  float* coords[] = { &x[0], &y[0], &z[0] };

  // this is not very nice - the mesh is defined by the Position field
  // which _must_ be written before any other fields
  if (m_field_name == "Position")
    DBPutPointmesh(dbfile, "Position", 3, coords, m_data.size(), DB_FLOAT, NULL);
  else {
    if (!append || DBInqVarExists(dbfile, "Position") == 0)
      console.Error() << "writeAsSILO: You have to write out the Position first\n";
    else
      DBPutPointvar(dbfile, m_field_name.c_str(), "Position", 3, coords,
                     m_data.size(), DB_FLOAT, NULL);
  }
  // close file 
  DBClose(dbfile);
#else
  console.Error() << "writeAsSILO not supported\n";
#endif
  m_data.clear();
}


/*!
  Constructor. Set up the Master and broadcast parameters to the slaves.

  \param comm the communicator
  \param fieldname the name of the field to be saved
  \param filename the name of the file to be saved
  \param walls the names of the walls
  \param savetype the way in which the data are to be saved - only RAW_SERIES supported at the moment
  \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
*/
VectorWallFieldMaster::VectorWallFieldMaster(TML_Comm* comm,
					     const string& fieldname,
					     const string& filename,
					     vector<string> walls,
					     const string& savetype,
					     int t0,
					     int tend,
					     int dt)
  : AFieldMaster(comm,fieldname,filename,savetype,t0,tend,dt)
{
  console.XDebug() << "VectorWallFieldMaster::VectorWallFieldMaster()\n";
  m_comm->broadcast_cont(fieldname);
  console.XDebug() << "bcast fieldname\n";
  m_comm->broadcast(int(walls.size())); 
  for(vector<string>::iterator iter=walls.begin();
      iter!=walls.end();
      iter++){
    m_comm->broadcast_cont(*iter);
  }
  m_comm->broadcast(m_id);
  m_comm->receive(m_sum_flag,1);
  console.XDebug() << "m_sum_flag = " << m_sum_flag << "\n";
  m_comm->barrier();
  console.XDebug() << "end VectorWallFieldMaster::VectorWallFieldMaster()\n";
}

void VectorWallFieldMaster::collect()
{
  console.XDebug() << "VectorWallFieldMaster::collect()\n";
  multimap<int,pair<int,Vec3> > recv_data;

  // send field id to slave
  m_comm->broadcast(m_id);

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

  if(m_sum_flag==0){ // don't sum -> take data from node 1
    multimap<int,pair<int,Vec3> >::iterator it_lower=recv_data.lower_bound(1);
    multimap<int,pair<int,Vec3> >::iterator it_upper=recv_data.upper_bound(1);
    for(multimap<int,pair<int,Vec3> >::iterator iter=it_lower;
	iter!=it_upper;
	iter++){
      m_data.insert(iter->second);
    }
  } else if(m_sum_flag==1){
    int nslaves=m_comm->size();
    for(int i=1;i<nslaves;i++){
      multimap<int,pair<int,Vec3> >::iterator it_lower=recv_data.lower_bound(i);
      multimap<int,pair<int,Vec3> >::iterator it_upper=recv_data.upper_bound(i);
      for(multimap<int,pair<int,Vec3> >::iterator iter=it_lower;
	  iter!=it_upper;
	  iter++){
	m_data[(iter->second).first]+=(iter->second).second;
      }
    }
  }

  console.XDebug() << "end VectorWallFieldMaster::collect()\n";
}