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