PlasCom2  1.0
XPACC Multi-physics simluation application
PCPPFieldData.C
Go to the documentation of this file.
1 #include "PCPPFieldData.H"
2 
3 namespace pcpp {
4 
5  namespace field {
6 
7  std::ostream &operator<<(std::ostream &Ostr,const databuffer &buf)
8  {
9  if(!buf._data_ptr)
10  return(Ostr);
11  std::ostringstream OStr;
12  OStr << buf._number_of_items << " " << buf._item_size << std::endl;
13  bool known_data_type = false;
14  if(buf._item_size == 1){
15  buf.OutputToStream<char>(OStr);
16  }
17  else if(buf._item_size == 2){
18  buf.OutputToStream<short>(OStr);
19  }
20  else if(buf._item_size == 4){
21  buf.OutputToStream<int>(OStr);
22  }
23  else if(buf._item_size == 8){
24  buf.OutputToStream<double>(OStr);
25  }
26  else
27  assert(known_data_type);
28  Ostr << OStr.str();
29  return(Ostr);
30  };
31 
32  std::istream &operator>>(std::istream &Istr, databuffer &buf)
33  {
34  int in_nitems = 0;
35  int in_size = 0;
36  std::string line;
37  while(line.empty())
38  std::getline(Istr,line);
39  std::istringstream IStr(line);
40  IStr >> in_nitems;
41  IStr >> in_size;
42  int read_size = in_nitems * in_size;
43  int expected_size = buf._number_of_items * buf._item_size;
44  if(expected_size <= 0) {
45  expected_size = read_size;
46  buf._number_of_items = in_nitems;
47  buf._item_size = in_size;
48  }
49  bool known_data_type = false;
50  if(buf._data_ptr != NULL){
51  if(expected_size != read_size){
52  std::cout << "PC2::ERROR line = " << line << std::endl
53  << "PC2::ERROR in_nitems = " << in_nitems << std::endl
54  << "PC2::ERROR in_size = " << in_size << std::endl
55  << "PC2::ERROR read_size = " << read_size << std::endl
56  << "PC2::ERROR expected = " << expected_size << std::endl;
57  std::getline(Istr,line);
58  std::cout << "PC2::ERROR next_line = " << line << std::endl;
59  }
60  assert(expected_size == read_size);
61  }
62  else
63  buf.Allocate(in_nitems,in_size);
64  if(buf._item_size == 1)
65  buf.ReadFromStream<char>(Istr);
66  else if(buf._item_size == 2)
67  buf.ReadFromStream<short>(Istr);
68  else if(buf._item_size == 4)
69  buf.ReadFromStream<int>(Istr);
70  else if(buf._item_size == 8)
71  buf.ReadFromStream<double>(Istr);
72  else
73  assert(known_data_type);
74  return(Istr);
75  };
76 
77  std::istream &operator>>(std::istream &IStr, metadata &md)
78  {
79  IStr >> md.name >> md.loc >> md.ncomp >> md.dsize >> md.unit;
80  if(md.unit == "[NULL]")
81  md.unit.erase();
82  return(IStr);
83  };
84 
85  std::istream &operator>>(std::istream &IStr, metadataset &mdSet)
86  {
87  mdSet.resize(0);
88  int numData = 0;
89  IStr >> numData;
90  for(int iData = 0;iData < numData;iData++){
91  metadata md;
92  IStr >> md;
93  if(md.ncomp > 0 && md.dsize > 0 && !md.name.empty())
94  mdSet.push_back(md);
95  }
96  return(IStr);
97  };
98 
99  std::ostream &operator<<(std::ostream &OStr,const metadata &md)
100  {
101  OStr << md.name << " " << md.loc << " " << md.ncomp << " " << md.dsize << " "
102  << (md.unit.empty() ? "[NULL]" : md.unit);
103  return(OStr);
104  };
105 
106  std::ostream &operator<<(std::ostream &OStr,const metadataset &mdSet)
107  {
108  metadataset::const_iterator mdIt = mdSet.begin();
109  OStr << mdSet.size() << std::endl;
110  while(mdIt != mdSet.end()){
111  const metadata &md(*mdIt++);
112  OStr << md << std::endl;
113  }
114  return(OStr);
115  };
116 
117 
118 
119  // ///
120  // /// @brief Configures field data
121  // ///
122  // /// This routine reads the field data configuration out of the plascomConfig
123  // /// configuration object and populates the plascomSimulationInfo.metaData
124  // /// which stores the metadata for all the field data.
125  // int plascom2::ConfigureFieldData()
126  // {
127  // FunctionEntry("ConfigFieldData");
128  // int error = 0;
129  // std::ostringstream Ostr;
130  // std::string solutionKey("Solution:");
131  // std::vector<std::string> fieldNames(plascomConfig.GetValueVector<std::string>("Solution:Fields"));
132  // if(fieldNames.empty()){
133  // FunctionExit("ConfigFieldData");
134  // return(0);
135  // }
136  // std::vector<std::string>::iterator fieldIt = fieldNames.begin();
137  // while(fieldIt != fieldNames.end()){
138  // std::string fieldName(*fieldIt++);
139  // std::string fieldKey(solutionKey+fieldName+std::string(":"));
140  // std::string fieldUnits = plascomConfig.GetValue<std::string>(fieldKey+"Units");
141  // int numComponents = plascomConfig.GetValue<int>(fieldKey+"Components");
142  // int dataSize = plascomConfig.GetValue<int>(fieldKey+"DataSize");
143  // std::string fieldLocation = plascomConfig.GetValue<std::string>(fieldKey+"Location");
144  // if(dataSize <= 0 || dataSize > 8){
145  // std::ostringstream errOut;
146  // errOut << "ConfigureSolution:Warning: invalid data size for "
147  // << fieldName << ", defaulting to double(8)." << std::endl;
148  // ErrOut(errOut.str());
149  // dataSize = 8; // default to double data
150  // }
151  // if(numComponents <= 0)
152  // numComponents = 1; // default to scalar field
153  // if(fieldLocation.empty())
154  // fieldLocation = "n";
155  // if(fieldLocation.size() != 1)
156  // fieldLocation = fieldLocation.substr(0,1);
157  // fieldLocation[0] = std::tolower(fieldLocation[0]);
158  // if(fieldLocation != "n" && fieldLocation != "c" &&
159  // fieldLocation != "m" && fieldLocation != "s"){
160  // std::ostringstream errOut;
161  // errOut << "ConfigureSolution:Warning: invalid solution location for "
162  // << fieldName << ", defaulting to nodes." << std::endl;
163  // ErrOut(errOut.str());
164  // fieldLocation = "n";
165  // }
166  // plascomSimulationInfo.metaData.AddField(fieldName,fieldLocation[0],numComponents,dataSize,fieldUnits);
167  // }
168  // StdOut(ReportSimulationMetaData(plascomSimulationInfo.metaData),2);
169  // FunctionExit("ConfigFieldData");
170  // return(error);
171  // }
172 
173  // ///
174  // /// @brief Create main data & solution storage buffers
175  // ///
176  // /// This function looks at the configured field data,
177  // /// separates the configured fields into simulation-wide
178  // /// or grid-bound data, and then allocates data buffers
179  // /// to store the data. Each field buffer is contiguous,
180  // /// and is independent of storage policy.
181  // int plascom2::CreateFieldDataBuffers()
182  // {
183 
184  // FunctionEntry("CreateFieldBuffers");
185  // if(computeProcess){
186  // pcpp::simulation::solutionmetadata &solnMeta(plascomSimulationInfo.metaData);
187  // pcpp::geometry::domaininfo domainInfo(plascomDomain.Info());
188  // std::vector<std::string>::iterator geomNameIt = domainInfo.geometryNames.begin();
189  // pcpp::geometry::domain::iterator domIt = plascomDomain.begin();
190 
191  // // separate into simulation-wide data vs. grid-bound data
192  // pcpp::simulation::solutionmetadata simulationDataMeta;
193  // pcpp::simulation::solutionmetadata gridDataMeta;
194  // pcpp::simulation::solutionmetadata::iterator metaIt = solnMeta.begin();
195  // while(metaIt != solnMeta.end()){
196  // pcpp::simulation::fieldmetadata &fieldMeta(*metaIt++);
197  // if(fieldMeta.loc == 's')
198  // simulationDataMeta.push_back(fieldMeta);
199  // else
200  // gridDataMeta.push_back(fieldMeta);
201  // }
202 
203  // // Create simulation-wide data
204  // simulationData.Meta().Copy(simulationDataMeta);
205  // simulationData.Create();
206 
207  // // Iterate through the grids and create grid-bound data
208  // while(domIt != plascomDomain.end()){
209  // int gridIndex = domIt - plascomDomain.begin();
210  // std::string geomName(*geomNameIt++);
211  // pcpp::geometry::grid &currentGrid(*domIt++);
212  // pcpp::geometry::gridinfo &gridInfo(currentGrid.Info());
213  // pcpp::simulation::topologyinfo &gridTopo(gridTopology[gridIndex]);
214 
215  // std::vector<int> &gridSize(gridInfo.globalSize);
216  // std::vector<int> &localSize(gridInfo.localSize);
217  // std::vector<int> &localStart(gridInfo.localStart);
218  // int gridDimension = gridInfo.numDimensions;
219 
220  // bool gridOK = true;
221  // if(gridDimension == 0)
222  // gridOK = false;
223  // std::vector<int>::iterator gsIt = gridSize.begin();
224  // while(gsIt != gridSize.end())
225  // if(*gsIt++ == 0)
226  // gridOK = false;
227  // if(!gridOK){
228  // continue;
229  // }
230  // size_t numNodes = 1;
231  // size_t numCells = 1;
232  // std::vector<int>::iterator sizeIt = localSize.begin();
233  // while(sizeIt != localSize.end()){
234  // if(*sizeIt != 1)
235  // numCells *= (*sizeIt - 1);
236  // numNodes *= *sizeIt++;
237  // }
238 
239  // std::ostringstream reportOut;
240  // reportOut << "Creating grid-bound data for "
241  // << currentGrid.Name() << " with "
242  // << numNodes << " grid vertices, and "
243  // << numCells << " grid cells." << std::endl;
244  // StdOut(reportOut.str(),3);
245  // pcpp::simulation::solutiondata &currentSoln(currentGrid.Solution());
246  // pcpp::simulation::solutionmetadata &currentMeta(currentSoln.Meta());
247  // currentMeta.Copy(gridDataMeta);
248  // currentSoln.Create(numNodes,numCells);
249 
250  // // Create buffers for the halo regions too
251  // std::vector<pcpp::IndexIntervalType> &haloExtents(gridTopo.haloExtents);
252  // std::vector<pcpp::simulation::solutiondata> &haloData(currentGrid.HaloData());
253  // haloData.resize(haloExtents.size());
254  // std::vector<pcpp::IndexIntervalType>::iterator haloIt = haloExtents.begin();
255  // while(haloIt != haloExtents.end()){
256  // int iDir = haloIt - haloExtents.begin();
257  // pcpp::IndexIntervalType haloExtent(*haloIt++);
258  // if(!haloExtent.empty()){
259  // std::ostringstream haloOut;
260  // haloOut << "Creating direction " << iDir
261  // << " halo data for region: ";
262  // haloExtent.PrettyPrint(haloOut);
263  // haloOut << std::endl;
264  // StdOut(haloOut.str(),3);
265  // int numHaloNodes = haloExtent.NNodes();
266  // haloData[iDir].Meta().Copy(gridDataMeta);
267  // haloData[iDir].Create(numHaloNodes);
268  // }
269  // }
270  // }
271  // }
272  // FunctionExit("CreateFieldBuffers");
273  // return(0);
274  // }
275 
276 
277  // void plascom2::CreateSendData(const pcpp::simulation::solutionmetadata &solnMeta,
278  // const pcpp::IndexIntervalType &gridExtent,
279  // const std::vector<pcpp::RemoteCollisionType> &inCollision,
280  // std::vector<pcpp::simulation::solutiondata> &bufferVec,
281  // std::vector<std::vector<size_t> > &indexVec)
282  // {
283  // if(inCollision.empty())
284  // return;
285  // int numCollisions = inCollision.size();
286  // bufferVec.resize(numCollisions);
287  // indexVec.resize(numCollisions);
288  // std::vector<pcpp::RemoteCollisionType>::const_iterator loopIt = inCollision.begin();
289  // while(loopIt != inCollision.end()){
290  // int dataIndex = loopIt - inCollision.begin();
291  // int remoteRank = loopIt->first;
292  // const pcpp::IndexIntervalType &commExtent(loopIt++->second);
293  // pcpp::simulation::solutiondata &commData(bufferVec[dataIndex]);
294  // std::vector<size_t> &commIndices(indexVec[dataIndex]);
295 
296  // // Create the buffer to handle the communication
297  // commData.Meta().Copy(solnMeta);
298  // int nNodes = commExtent.NNodes();
299  // commData.Create(nNodes);
300  // // Figure out the indices required to feed the comm buffer from
301  // // local grid-bound data
302  // gridExtent.GetFlatIndices(commExtent,commIndices);
303  // assert(commIndices.size() == nNodes);
304 
305  // // Report to the user
306  // std::ostringstream Ostr;
307  // Ostr << "Creating buffer for send(" << remoteRank << "): ";
308  // commExtent.PrettyPrint(Ostr);
309  // Ostr << " " << nNodes << " nodes." << std::endl;
310  // StdOut(Ostr.str(),3);
311  // }
312  // };
313 
314  // void plascom2::CreateRecvData(const pcpp::simulation::solutionmetadata &solnMeta,
315  // const std::vector<pcpp::IndexIntervalType> &haloExtents,
316  // const std::vector<pcpp::RemoteCollisionType> &inCollision,
317  // std::vector<pcpp::simulation::solutiondata> &bufferVec,
318  // std::vector<pcpp::CollisionIndicesType> &indexVec)
319  // {
320  // if(inCollision.empty())
321  // return;
322  // int numCollisions = inCollision.size();
323  // bufferVec.resize(numCollisions);
324  // indexVec.resize(numCollisions);
325  // std::vector<pcpp::RemoteCollisionType>::const_iterator loopIt = inCollision.begin();
326  // while(loopIt != inCollision.end()){
327  // int dataIndex = loopIt - inCollision.begin();
328  // int remoteRank = loopIt->first;
329  // const pcpp::IndexIntervalType &commExtent(loopIt++->second);
330  // pcpp::simulation::solutiondata &commData(bufferVec[dataIndex]);
331  // pcpp::CollisionIndicesType &commIndices(indexVec[dataIndex]);
332  // pcpp::SizeVec &recvIndices(commIndices.second);
333  // int &haloIndex(commIndices.first);
334 
335  // // Create the buffer to handle the communication
336  // commData.Meta().Copy(solnMeta);
337  // int nNodes = commExtent.NNodes();
338  // commData.Create(nNodes);
339  // // Figure out *which halo buffer* and the indices required to feed
340  // // that halo buffer from the communication buffer.
341  // //
342  // // Since there is an array of collisions coming in, and we don't
343  // // know which collision lives with which halo, we have to loop
344  // // through the halos and collide the communication extent with
345  // // the halo extent to find out which halo each communication
346  // // buffer should feed. This information along with an array of
347  // // flat indices are recorded in a persistent data structure that
348  // // is used to avoid having to do these collisions during
349  // // time stepping.
350  // int numHalos = 0;
351  // std::vector<pcpp::IndexIntervalType>::const_iterator extentIt = haloExtents.begin();
352  // while(extentIt != haloExtents.end()){
353  // int hIndex = extentIt - haloExtents.begin();
354  // const pcpp::IndexIntervalType &haloExtent(*extentIt++);
355  // pcpp::IndexIntervalType haloCollision(haloExtent.Overlap(commExtent));
356  // if(!haloCollision.empty()){
357  // numHalos++;
358  // haloIndex = hIndex;
359  // haloExtent.GetFlatIndices(commExtent,recvIndices);
360  // assert(recvIndices.size() == nNodes);
361  // }
362  // }
363  // // each communication extent should collide
364  // // with one and only one halo
365  // assert(numHalos == 1);
366 
367  // // Report to the user
368  // std::ostringstream Ostr;
369  // Ostr << "Creating buffer for recv(" << remoteRank << "): ";
370  // commExtent.PrettyPrint(Ostr);
371  // Ostr << " " << nNodes << " nodes in the "
372  // << haloIndex << " grid direction." << std::endl;
373  // StdOut(Ostr.str(),3);
374  // }
375 
376  // };
377 
378  // ///
379  // /// @brief Create communication buffers for solution transfers
380  // ///
381  // /// This function looks at the configured field data,
382  // /// separates the configured fields into simulation-wide
383  // /// or grid-bound data, and then allocates data buffers
384  // /// to store the data. Each field buffer is contiguous,
385  // /// and is independent of storage policy.
386  // int plascom2::CreateCommunicationBuffers()
387  // {
388 
389  // FunctionEntry("CreateCommBuffers");
390  // if(computeProcess){
391 
392  // pcpp::geometry::domaininfo domainInfo(plascomDomain.Info());
393  // std::vector<std::string>::iterator geomNameIt = domainInfo.geometryNames.begin();
394  // pcpp::geometry::domain::iterator domIt = plascomDomain.begin();
395  // while(domIt != plascomDomain.end()){
396 
397  // int gridIndex = domIt - plascomDomain.begin();
398  // std::string geomName(*geomNameIt++);
399  // pcpp::geometry::grid &currentGrid(*domIt++);
400  // pcpp::geometry::gridinfo &gridInfo(currentGrid.Info());
401  // if(gridInfo.numDimensions <= 0)
402  // continue;
403  // pcpp::simulation::topologyinfo &gridTopo(gridTopology[gridIndex]);
404 
405  // std::vector<int> &gridSize(gridInfo.globalSize);
406  // std::vector<int> &localSize(gridInfo.localSize);
407  // std::vector<int> &localStart(gridInfo.localStart);
408  // int gridDimension = gridSize.size();
409  // pcpp::IndexIntervalType gridExtent(localStart,localSize);
410  // std::vector<pcpp::IndexIntervalType> &haloExtents(gridTopo.haloExtents);
411 
412  // std::ostringstream Ostr;
413  // // Ostr << "Grid Extent: ";
414  // // gridExtent.PrettyPrint(Ostr);
415  // // Ostr << std::endl
416  // // << "Halo Extents:" << std::endl;
417  // // std::vector<pcpp::IndexIntervalType>::iterator haloIt = haloExtents.begin();
418  // // while(haloIt != haloExtents.end()){
419  // // haloIt++->PrettyPrint(Ostr);
420  // // Ostr << std::endl;
421  // // }
422  // // StdOut(Ostr.str(),3);
423  // // pcpp::io::RenewStream(Ostr);
424 
425  // bool gridOK = true;
426  // if(gridDimension == 0)
427  // gridOK = false;
428  // std::vector<int>::iterator gsIt = gridSize.begin();
429  // while(gsIt != gridSize.end())
430  // if(*gsIt++ == 0)
431  // gridOK = false;
432  // if(!gridOK){
433  // continue;
434  // }
435 
436  // int collisionExtentSize = gridDimension*2;
437  // int collisionCommSize = collisionExtentSize+1;
438 
439  // // computeCommunicator.Barrier();
440  // // std::cout << computeCommunicator.Rank() << " here grid "
441  // // << gridIndex << std::endl;
442  // // computeCommunicator.Barrier();
443 
444  // std::vector<pcpp::RemoteCollisionType> &interComputeRecv(gridTopo.interComputeRecv[0]);
445  // std::vector<pcpp::RemoteCollisionType> &interComputeSend(gridTopo.interComputeSend[0]);
446  // std::vector<pcpp::RemoteCollisionType> &intraComputeRecv(gridTopo.intraComputeRecv[0]);
447  // std::vector<pcpp::RemoteCollisionType> &intraComputeSend(gridTopo.intraComputeSend[0]);
448 
449  // pcpp::simulation::solutiondata &currentSoln(currentGrid.Solution());
450  // pcpp::simulation::solutionmetadata &currentMeta(currentSoln.Meta());
451 
452  // // computeCommunicator.Barrier();
453  // // std::cout << computeCommunicator.Rank() << " preparing buffers."
454  // // << std::endl;
455  // // computeCommunicator.Barrier();
456 
457  // Ostr << "InterComputeSend Size: " << interComputeSend.size() << std::endl
458  // << "IntraComputeSend Size: " << intraComputeSend.size() << std::endl;
459  // StdOut(Ostr.str(),3);
460  // pcpp::io::RenewStream(Ostr);
461 
462  // // computeCommunicator.Barrier();
463  // // std::cout << computeCommunicator.Rank() << " preparing buffers for real yo."
464  // // << std::endl;
465  // // computeCommunicator.Barrier();
466 
467 
468  // // computeCommunicator.Barrier();
469  // CreateSendData(currentMeta,gridExtent,intraComputeSend,
470  // currentGrid.IntraSendData(),currentGrid.IntraSendIndices());
471  // CreateSendData(currentMeta,gridExtent,interComputeSend,
472  // currentGrid.InterSendData(),currentGrid.InterSendIndices());
473 
474  // Ostr << "InterComputeRecv Size: " << interComputeRecv.size() << std::endl
475  // << "IntraComputeRecv Size: " << intraComputeRecv.size() << std::endl;
476  // StdOut(Ostr.str(),3);
477  // pcpp::io::RenewStream(Ostr);
478  // CreateRecvData(currentMeta,haloExtents,interComputeRecv,currentGrid.InterRecvData(),
479  // currentGrid.InterRecvIndices());
480  // CreateRecvData(currentMeta,haloExtents,intraComputeRecv,currentGrid.IntraRecvData(),
481  // currentGrid.IntraRecvIndices());
482 
483  // }
484  // }
485  // FunctionExit("CreateCommBuffers");
486  // return(0);
487  // }
488 
489 
490  }
491 }
std::ostream & operator<<(std::ostream &Ostr, const databuffer &buf)
Definition: PCPPFieldData.C:7
std::ostream & OutputToStream(std::ostream &Ostr) const
Definition: FieldData.C:3
std::istream & ReadFromStream(std::istream &Istr)
std::istream & operator>>(std::istream &Istr, databuffer &buf)
Definition: PCPPFieldData.C:32