IpplParticleBase.hpp 22.6 KB
Newer Older
gsell's avatar
gsell committed
1 2 3
/***************************************************************************
 *
 * The IPPL Framework
kraus's avatar
kraus committed
4 5
 *
 * This program was prepared by PSI.
gsell's avatar
gsell committed
6 7 8 9 10 11 12 13 14
 * All rights in the program are reserved by PSI.
 * Neither PSI nor the author(s)
 * makes any warranty, express or implied, or assumes any liability or
 * responsibility for the use of this software
 *
 * Visit www.amas.web.psi for more details
 *
 ***************************************************************************/

15
#include "Particle/IpplParticleBase.h"
gsell's avatar
gsell committed
16 17 18 19 20 21 22 23
#include "Particle/ParticleLayout.h"
#include "Particle/ParticleAttrib.h"
#include "Message/Message.h"
#include "Message/Communicate.h"
#include "Utility/Inform.h"
#include "Utility/PAssert.h"
#include "Utility/IpplInfo.h"
#include "Utility/IpplStats.h"
24
#include "Utility/IpplException.h"
gsell's avatar
gsell committed
25 26 27
#include <algorithm>

/////////////////////////////////////////////////////////////////////
28
// For a IpplParticleBase that was created with the default constructor,
gsell's avatar
gsell committed
29 30 31 32
// initialize performs the same actions as are done in the non-default
// constructor.  If this object has already been initialized, it is
// an error.  For initialize, you must supply a layout instance.
template<class PLayout>
33
void IpplParticleBase<PLayout>::initialize(PLayout *layout) {
kraus's avatar
kraus committed
34

gsell's avatar
gsell committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48
  // make sure we have not already been initialized, and that we have
  // been given a good layout
  PAssert(Layout == 0);
  PAssert(layout != 0);

  // save the layout, and perform setup tasks
  Layout = layout;
  setup();
}


/////////////////////////////////////////////////////////////////////
// set up this new object:  add attributes and check in to the layout
template<class PLayout>
49
void IpplParticleBase<PLayout>::setup() {
kraus's avatar
kraus committed
50

gsell's avatar
gsell committed
51 52 53 54 55 56 57 58 59 60 61 62 63
  TotalNum = 0;
  LocalNum = 0;
  DestroyNum = 0;
  GhostNum = 0;

  // shift ID back so that first ID retrieved is myNode
  NextID = Ippl::Comm->myNode() - Ippl::Comm->getNodes();

  // Create position R and index ID attributes for the particles,
  // and add them to our list of attributes.
  addAttribute(R);
  addAttribute(ID);

64 65 66 67
  // set pointer for base class AbstractParticle
  this->R_p  = &R;
  this->ID_p = &ID;
  
68 69
  // indicate we have created a new IpplParticleBase object
  INCIPPLSTAT(incIpplParticleBases);
gsell's avatar
gsell committed
70 71 72 73 74 75
}


/////////////////////////////////////////////////////////////////////
// Return a boolean value indicating if we are on a processor which can
// be used for single-node particle creation and initialization
76

gsell's avatar
gsell committed
77
template<class PLayout>
78
bool IpplParticleBase<PLayout>::singleInitNode() const {
79
    return (Ippl::Comm->myNode() == 0);
gsell's avatar
gsell committed
80 81 82 83 84 85
}

/////////////////////////////////////////////////////////////////////
// Return a new unique ID value for use by new particles.
// The ID number = (i * numprocs) + myproc, i = 0, 1, 2, ...
template<class PLayout>
86
unsigned IpplParticleBase<PLayout>::getNextID() {
kraus's avatar
kraus committed
87 88


gsell's avatar
gsell committed
89 90 91 92 93 94 95

  return (NextID += Ippl::Comm->getNodes());
}

/////////////////////////////////////////////////////////////////////
// Reset the particle ID's to be globally consecutive, 0 thru TotalNum.
template <class PLayout>
96
void IpplParticleBase<PLayout>::resetID(void) {
kraus's avatar
kraus committed
97 98


gsell's avatar
gsell committed
99 100 101 102 103 104 105 106

  unsigned int nodes = Ippl::getNodes();
  unsigned int myNode = Ippl::myNode();
  size_t localNum = this->getLocalNum();
  size_t ip;
  int master = 0;
  if (myNode == (unsigned int) master) {
    // Node 0 can immediately set its ID's to 0 thru LocalNum-1
kraus's avatar
kraus committed
107
    for (ip=0; ip<localNum; ++ip)
gsell's avatar
gsell committed
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
      this->ID[ip] = ip;
    // if there is only one processor, we are done
    if (nodes == 1) return;
    // parallel case: must find out how many particles each processor has
    // Node 0 gathers this information into an array
    size_t *lp;
    lp = new size_t[nodes];
    lp[0] = localNum;  // enter our own number of particles
    // get next message tag and receive messages
    int tag1 = Ippl::Comm->next_tag(P_RESET_ID_TAG,P_LAYOUT_CYCLE);
    Message* msg1;
    for (ip=1; ip<nodes; ++ip) {
      int rnode = COMM_ANY_NODE;
      msg1 = Ippl::Comm->receive_block(rnode,tag1);
      PAssert(msg1);
      msg1->get(lp[rnode]);
      delete msg1;
    }
    // now we should have all the localnum values.
    // figure out starting ID for each processor and send back
    size_t current, sum = 0;
    for (ip=0; ip<nodes; ++ip) {
      current = lp[ip];
      lp[ip] = sum;
      sum += current;
    }
    // send initial ID values back out
    int tag2 = Ippl::Comm->next_tag(P_RESET_ID_TAG,P_LAYOUT_CYCLE);
    for (ip=1; ip<nodes; ++ip) {
      Message* msg2 = new Message;
      msg2->put(lp[ip]);
      bool success = Ippl::Comm->send(msg2,ip,tag2);
140 141 142 143 144
      if (success == false) {
              throw IpplException (
                      "IpplParticleBase<PLayout>::resetID()",
                      "sending initial ID values failed.");
      }
gsell's avatar
gsell committed
145 146 147 148 149 150 151 152 153 154
    }
    // we are done
    return;
  }
  else {
    // first send number of local particles to Node 0
    int tag1 = Ippl::Comm->next_tag(P_RESET_ID_TAG,P_LAYOUT_CYCLE);
    Message* msg1 = new Message;
    msg1->put(localNum);
    bool success = Ippl::Comm->send(msg1,master,tag1);
155 156 157 158 159
    if (success == false) {
            throw IpplException (
                    "IpplParticleBase<PLayout>::resetID()",
                    "sending initial ID values failed.");
    }
gsell's avatar
gsell committed
160
    // now receive back our initial ID number
gsell's avatar
gsell committed
161
    size_t initialID = 0;
gsell's avatar
gsell committed
162 163 164 165 166 167
    int tag2 = Ippl::Comm->next_tag(P_RESET_ID_TAG,P_LAYOUT_CYCLE);
    Message* msg2 = Ippl::Comm->receive_block(master,tag2);
    PAssert(msg2);
    msg2->get(initialID);
    delete msg2;
    // now reset our particle ID's using this initial value
kraus's avatar
kraus committed
168
    for (ip=0; ip<localNum; ++ip)
gsell's avatar
gsell committed
169 170 171 172 173 174 175 176 177 178 179
      this->ID[ip] = ip + initialID;
    // we are done
    return;
  }
}


/////////////////////////////////////////////////////////////////////
// put the data for M particles starting from local index I in a Message
template<class PLayout>
size_t
180
IpplParticleBase<PLayout>::putMessage(Message& msg, size_t M, size_t I) {
gsell's avatar
gsell committed
181 182 183 184 185 186 187 188 189 190 191

  // make sure we've been initialized
  PAssert(Layout != 0);

  // put into message the number of items in the message
  msg.put(M);

  // go through all the attributes and put their data in the message
  if (M > 0) {
    // this routine should only be called for local particles; call
    // ghostPutMessage to put in particles which might be ghost particles
192 193
    PAssert_LT(I, R.size());
    PAssert_LE(I + M, R.size());
gsell's avatar
gsell committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

    attrib_container_t::iterator abeg = AttribList.begin();
    attrib_container_t::iterator aend = AttribList.end();
    for ( ; abeg != aend; abeg++ )
      (*abeg)->putMessage(msg, M, I);
  }

  return M;
}


/////////////////////////////////////////////////////////////////////
// put the data for a list of particles in a Message
template<class PLayout>
size_t
209
IpplParticleBase<PLayout>::putMessage(Message& msg,
gsell's avatar
gsell committed
210 211 212 213 214 215
				  const std::vector<size_t>& putList)
{

  // make sure we've been initialized
  PAssert(Layout != 0);

kraus's avatar
kraus committed
216
  std::vector<size_t>::size_type M = putList.size();
gsell's avatar
gsell committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  msg.put(M);

  // go through all the attributes and put their data in the message
  if (M > 0) {
    attrib_container_t::iterator abeg = AttribList.begin();
    attrib_container_t::iterator aend = AttribList.end();
    for ( ; abeg != aend; ++abeg )
      (*abeg)->putMessage(msg, putList);
  }

  return M;
}

template<class PLayout>
size_t
232
IpplParticleBase<PLayout>::putMessage(Message& msg, size_t I) {
gsell's avatar
gsell committed
233 234 235 236 237

  // make sure we've been initialized
  PAssert(Layout != 0);

  // put into message the number of items in the message
kraus's avatar
kraus committed
238

gsell's avatar
gsell committed
239
  // go through all the attributes and put their data in the message
kraus's avatar
kraus committed
240

gsell's avatar
gsell committed
241 242 243 244
    attrib_container_t::iterator abeg = AttribList.begin();
    attrib_container_t::iterator aend = AttribList.end();
    for ( ; abeg != aend; abeg++ )
      (*abeg)->putMessage(msg, 1, I);
kraus's avatar
kraus committed
245

gsell's avatar
gsell committed
246 247 248 249
 return 1;
}

template<class PLayout>
250
Format* IpplParticleBase<PLayout>::getFormat()
gsell's avatar
gsell committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264
{
	//create dummy particle so we can obtain the format
    bool wasempty = false;
    if(this->getLocalNum()==0)
    {
		this->create(1);
		wasempty = true;
	}

	//obtain the format
    Message *msg = new Message;
    this->putMessage(*msg, (size_t) 0);
    Format *format = new Format(msg);
    delete msg;
kraus's avatar
kraus committed
265

gsell's avatar
gsell committed
266 267 268
	//remove the dummy particle again
    if (wasempty)
        this->destroy(1, 0, true);
kraus's avatar
kraus committed
269

gsell's avatar
gsell committed
270 271 272
    return format;
}

kraus's avatar
kraus committed
273
template<class PLayout>
gsell's avatar
gsell committed
274
size_t
275
IpplParticleBase<PLayout>::writeMsgBuffer(MsgBuffer *&msgbuf, const std::vector<size_t> &list)
gsell's avatar
gsell committed
276 277
{
    msgbuf = new MsgBuffer(this->getFormat(), list.size());
kraus's avatar
kraus committed
278

gsell's avatar
gsell committed
279 280 281 282 283 284 285 286 287
    for (unsigned int i = 0;i<list.size();++i)
    {
        Message msg;
        this->putMessage(msg, list[i]);
        msgbuf->add(&msg);
    }
    return list.size();
}

kraus's avatar
kraus committed
288
template<class PLayout>
gsell's avatar
gsell committed
289 290
template<class O>
size_t
291
IpplParticleBase<PLayout>::writeMsgBufferWithOffsets(MsgBuffer *&msgbuf, const std::vector<size_t> &list, const std::vector<O> &offset)
gsell's avatar
gsell committed
292 293 294 295 296 297 298 299 300 301
 {
     msgbuf = new MsgBuffer(this->getFormat(), list.size());
     typename PLayout::SingleParticlePos_t oldpos;
     for (unsigned int i = 0;i<list.size();++i)
         {
             oldpos = R[list[i]];
             for(int d = 0;d<Dim;++d)
                 {
                     R[list[i]][d] += offset[i][d];
                 }
kraus's avatar
kraus committed
302

gsell's avatar
gsell committed
303 304 305
             Message msg;
             this->putMessage(msg, list[i]);
             msgbuf->add(&msg);
kraus's avatar
kraus committed
306

gsell's avatar
gsell committed
307 308 309 310 311
             R[list[i]] = oldpos;
         }
     return list.size();
 }

kraus's avatar
kraus committed
312
template<class PLayout>
gsell's avatar
gsell committed
313
size_t
314
IpplParticleBase<PLayout>::readMsgBuffer(MsgBuffer *msgbuf)
gsell's avatar
gsell committed
315 316 317 318 319 320 321 322 323 324 325 326 327
{
	size_t added = 0;
	Message *msg = msgbuf->get();
    while (msg != 0)
    {
		added += this->getSingleMessage(*msg);
        delete msg;
        msg = msgbuf->get();
    }
    return added;
}


kraus's avatar
kraus committed
328
template<class PLayout>
gsell's avatar
gsell committed
329
size_t
330
IpplParticleBase<PLayout>::readGhostMsgBuffer(MsgBuffer *msgbuf, int node)
gsell's avatar
gsell committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
{
	size_t added = 0;
	Message *msg = msgbuf->get();
    while (msg != 0)
    {
		added += this->ghostGetSingleMessage(*msg, node);
        delete msg;
        msg = msgbuf->get();
    }
    return added;
}

/////////////////////////////////////////////////////////////////////
// retrieve particles from the given message and store them
template<class PLayout>
346
size_t IpplParticleBase<PLayout>::getMessage(Message& msg) {
gsell's avatar
gsell committed
347 348 349 350 351

  // make sure we've been initialized
  PAssert(Layout != 0);

  // get the number of items in the message
kraus's avatar
kraus committed
352
  size_t numitems = 0;
gsell's avatar
gsell committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  msg.get(numitems);

  // go through all the attributes and get their data from the message
  if (numitems > 0) {
    attrib_container_t::iterator abeg = AttribList.begin();
    attrib_container_t::iterator aend = AttribList.end();
    for ( ; abeg != aend; abeg++ )
      (*abeg)->getMessage(msg, numitems);
  }

  return numitems;
}

/////////////////////////////////////////////////////////////////////
// retrieve particles from the given message and store them
template<class PLayout>
369
size_t IpplParticleBase<PLayout>::getSingleMessage(Message& msg) {
gsell's avatar
gsell committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

  // make sure we've been initialized
  PAssert(Layout != 0);

  // get the number of items in the message
  size_t numitems=1;

  // go through all the attributes and get their data from the message
  if (numitems > 0) {
    attrib_container_t::iterator abeg = AttribList.begin();
    attrib_container_t::iterator aend = AttribList.end();
    for ( ; abeg != aend; abeg++ )
      (*abeg)->getMessage(msg, numitems);
  }

  return numitems;
}


/////////////////////////////////////////////////////////////////////
kraus's avatar
kraus committed
390
// retrieve particles from the given message and store them, also
gsell's avatar
gsell committed
391 392 393
// signaling we are creating the given number of particles.  Return the
// number of particles created.
template<class PLayout>
394
size_t IpplParticleBase<PLayout>::getMessageAndCreate(Message& msg) {
gsell's avatar
gsell committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

  // make sure we've been initialized
  PAssert(Layout != 0);

  // call the regular create, and add in the particles to our LocalNum
  size_t numcreate = getMessage(msg);
  LocalNum += numcreate;
  ADDIPPLSTAT(incParticlesCreated,numcreate);
  return numcreate;
}


/////////////////////////////////////////////////////////////////////
// create M new particles on this processor
template<class PLayout>
410
void IpplParticleBase<PLayout>::create(size_t M) {
kraus's avatar
kraus committed
411

gsell's avatar
gsell committed
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

  // make sure we've been initialized
  PAssert(Layout != 0);

  // go through all the attributes, and allocate space for M new particles
  attrib_container_t::iterator abeg = AttribList.begin();
  attrib_container_t::iterator aend = AttribList.end();
  for ( ; abeg != aend; abeg++ )
    (*abeg)->create(M);

  // set the unique ID value for these new particles
  size_t i1 = LocalNum;
  size_t i2 = i1 + M;
  while (i1 < i2)
    ID[i1++] = getNextID();

  // remember that we're creating these new particles
  LocalNum += M;
  ADDIPPLSTAT(incParticlesCreated,M);
}


/////////////////////////////////////////////////////////////////////
// create 1 new particle with a given ID
template<class PLayout>
437
void IpplParticleBase<PLayout>::createWithID(unsigned id) {
kraus's avatar
kraus committed
438

gsell's avatar
gsell committed
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

  // make sure we've been initialized
  PAssert(Layout != 0);

  // go through all the attributes, and allocate space for M new particles
  attrib_container_t::iterator abeg = AttribList.begin();
  attrib_container_t::iterator aend = AttribList.end();
  for ( ; abeg != aend; abeg++ )
    (*abeg)->create(1);

  const size_t M = 1;
  // set the unique ID value for these new particles
  size_t i1 = LocalNum;
  size_t i2 = i1 + M;
  while (i1 < i2)
      ID[i1++] = id;

  // remember that we're creating these new particles
  LocalNum += M;
   ADDIPPLSTAT(incParticlesCreated,1);
}


/////////////////////////////////////////////////////////////////////
// create np new particles globally, equally distributed among all processors
template<class PLayout>
465
void IpplParticleBase<PLayout>::globalCreate(size_t np) {
kraus's avatar
kraus committed
466

gsell's avatar
gsell committed
467 468 469 470 471 472 473 474 475 476 477 478 479

  // make sure we've been initialized
  PAssert(Layout != 0);

  // Compute the number of particles local to each processor:
  unsigned nPE = IpplInfo::getNodes();
  unsigned npLocal = np/nPE;

  // Handle the case where np/nPE is not an integer:
  unsigned myPE = IpplInfo::myNode();
  unsigned rem = np - npLocal * nPE;
  if (myPE < rem) ++npLocal;

480
  // Now each PE calls the local IpplParticleBase::create() function to create it's
gsell's avatar
gsell committed
481 482 483 484 485 486 487 488 489 490
  // local number of particles:
  create(npLocal);
}


/////////////////////////////////////////////////////////////////////
// delete M particles, starting with the Ith particle.  If the last argument
// is true, the destroy will be done immediately, otherwise the request
// will be cached.
template<class PLayout>
491
void IpplParticleBase<PLayout>::destroy(size_t M, size_t I, bool doNow) {
gsell's avatar
gsell committed
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 519 520 521 522

  // make sure we've been initialized
  PAssert(Layout != 0);

  if (M > 0) {
    if (doNow) {
      // find out if we are using optimized destroy method
      bool optDestroy = getUpdateFlag(PLayout::OPTDESTROY);
      // loop over attributes and carry out the destroy request
      attrib_container_t::iterator abeg, aend = AttribList.end();
      for (abeg = AttribList.begin(); abeg != aend; ++abeg)
	(*abeg)->destroy(M,I,optDestroy);
      LocalNum -= M;
    }
    else {
      // add this group of particle indices to our list of items to destroy
      std::pair<size_t,size_t> destroyEvent(I,M);
      DestroyList.push_back(destroyEvent);
      DestroyNum += M;
    }

    // remember we have this many more items to destroy (or have destroyed)
    ADDIPPLSTAT(incParticlesDestroyed,M);
  }
}


/////////////////////////////////////////////////////////////////////
// Update the particle object after a timestep.  This routine will change
// our local, total, create particle counts properly.
template<class PLayout>
523
void IpplParticleBase<PLayout>::update() {
kraus's avatar
kraus committed
524 525


gsell's avatar
gsell committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539

  // make sure we've been initialized
  PAssert(Layout != 0);

  // ask the layout manager to update our atoms, etc.
  Layout->update(*this);
  INCIPPLSTAT(incParticleUpdates);
}


/////////////////////////////////////////////////////////////////////
// Update the particle object after a timestep.  This routine will change
// our local, total, create particle counts properly.
template<class PLayout>
540
void IpplParticleBase<PLayout>::update(const ParticleAttrib<char>& canSwap) {
kraus's avatar
kraus committed
541 542


gsell's avatar
gsell committed
543 544 545 546 547 548 549 550 551 552 553 554 555 556

  // make sure we've been initialized
  PAssert(Layout != 0);

  // ask the layout manager to update our atoms, etc.
  Layout->update(*this, &canSwap);
  INCIPPLSTAT(incParticleUpdates);
}


// Actually perform the delete atoms action for all the attributes; the
// calls to destroy() only stored a list of what to do.  This actually
// does it.  This should in most cases only be called by the layout manager.
template<class PLayout>
557
void IpplParticleBase<PLayout>::performDestroy(bool updateLocalNum) {
kraus's avatar
kraus committed
558 559


gsell's avatar
gsell committed
560 561 562 563 564 565 566 567 568 569

  // make sure we've been initialized
  PAssert(Layout != 0);

  // nothing to do if destroy list is empty
  if (DestroyList.empty()) return;

  // before processing the list, we should make sure it is sorted
  bool isSorted = true;
  typedef std::vector< std::pair<size_t,size_t> > dlist_t;
570 571
  dlist_t::const_iterator curr = DestroyList.begin();
  const dlist_t::const_iterator last = DestroyList.end();
gsell's avatar
gsell committed
572 573 574 575 576 577
  dlist_t::const_iterator next = curr + 1;
  while (next != last && isSorted) {
    if (*next++ < *curr++) isSorted = false;
  }
  if (!isSorted)
    std::sort(DestroyList.begin(),DestroyList.end());
kraus's avatar
kraus committed
578

gsell's avatar
gsell committed
579 580 581 582 583 584 585 586
  // find out if we are using optimized destroy method
  bool optDestroy = getUpdateFlag(PLayout::OPTDESTROY);

  // loop over attributes and process destroy list
  attrib_container_t::iterator abeg, aend = AttribList.end();
  for (abeg = AttribList.begin(); abeg != aend; ++abeg)
    (*abeg)->destroy(DestroyList,optDestroy);

587 588 589 590 591 592
  if (updateLocalNum) {
      for (curr = DestroyList.begin(); curr != last; ++ curr) {
          LocalNum -= curr->second;
      }
  }

gsell's avatar
gsell committed
593 594 595 596 597 598 599 600 601 602
  // clear destroy list and update destroy num counter
  DestroyList.erase(DestroyList.begin(),DestroyList.end());
  DestroyNum = 0;
}


/////////////////////////////////////////////////////////////////////
// delete M ghost particles, starting with the Ith particle.
// This is done immediately.
template<class PLayout>
603
void IpplParticleBase<PLayout>::ghostDestroy(size_t M, size_t I) {
kraus's avatar
kraus committed
604 605


gsell's avatar
gsell committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

  // make sure we've been initialized
  PAssert(Layout != 0);

  if (M > 0) {
    // delete the data from the attribute containers
    size_t dnum = 0;
    attrib_container_t::iterator abeg = AttribList.begin();
    attrib_container_t::iterator aend = AttribList.end();
    for ( ; abeg != aend; ++abeg )
      dnum = (*abeg)->ghostDestroy(M, I);
    GhostNum -= dnum;
  }
}


/////////////////////////////////////////////////////////////////////
// Put the data for M particles starting from local index I in a Message.
// Return the number of particles put in the Message.  This is for building
// ghost particle interaction lists.
template<class PLayout>
size_t
628
IpplParticleBase<PLayout>::ghostPutMessage(Message &msg, size_t M, size_t I) {
gsell's avatar
gsell committed
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662

  // make sure we've been initialized
  PAssert(Layout != 0);

  // put into message the number of items in the message
  if (I >= R.size()) {
    // we're putting in ghost particles ...
    if ((I + M) > (R.size() + GhostNum))
      M = (R.size() + GhostNum) - I;
  } else {
    // we're putting in local particles ...
    if ((I + M) > R.size())
      M = R.size() - I;
  }
  msg.put(M);

  // go through all the attributes and put their data in the message
  if (M > 0) {
    attrib_container_t::iterator abeg = AttribList.begin();
    attrib_container_t::iterator aend = AttribList.end();
    for ( ; abeg != aend; abeg++ )
      (*abeg)->ghostPutMessage(msg, M, I);
  }

  return M;
}


/////////////////////////////////////////////////////////////////////
// put the data for particles on a list into a Message, given list of indices
// Return the number of particles put in the Message.  This is for building
// ghost particle interaction lists.
template<class PLayout>
size_t
663
IpplParticleBase<PLayout>::ghostPutMessage(Message &msg,
gsell's avatar
gsell committed
664 665 666 667 668
				       const std::vector<size_t>& pl) {

  // make sure we've been initialized
  PAssert(Layout != 0);

kraus's avatar
kraus committed
669
  std::vector<size_t>::size_type M = pl.size();
gsell's avatar
gsell committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
  msg.put(M);

  // go through all the attributes and put their data in the message
  if (M > 0) {
    attrib_container_t::iterator abeg = AttribList.begin();
    attrib_container_t::iterator aend = AttribList.end();
    for ( ; abeg != aend; ++abeg )
      (*abeg)->ghostPutMessage(msg, pl);
  }

  return M;
}


/////////////////////////////////////////////////////////////////////
// retrieve particles from the given message and sending node and store them
template<class PLayout>
size_t
688
IpplParticleBase<PLayout>::ghostGetMessage(Message& msg, int /*node*/) {
kraus's avatar
kraus committed
689 690


gsell's avatar
gsell committed
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

  // make sure we've been initialized
  PAssert(Layout != 0);

  // get the number of items in the message
  size_t numitems;
  msg.get(numitems);
  GhostNum += numitems;

  // go through all the attributes and get their data from the message
  if (numitems > 0) {
    attrib_container_t::iterator abeg = AttribList.begin();
    attrib_container_t::iterator aend = AttribList.end();
    for ( ; abeg != aend; abeg++ )
      (*abeg)->ghostGetMessage(msg, numitems);
  }

  return numitems;
}

template<class PLayout>
size_t
snuverink_j's avatar
snuverink_j committed
713
IpplParticleBase<PLayout>::ghostGetSingleMessage(Message& msg, int /*node*/) {
gsell's avatar
gsell committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736

  // make sure we've been initialized
  PAssert(Layout != 0);

  // get the number of items in the message
  size_t numitems=1;
  GhostNum += numitems;

  // go through all the attributes and get their data from the message
  if (numitems > 0) {
    attrib_container_t::iterator abeg = AttribList.begin();
    attrib_container_t::iterator aend = AttribList.end();
    for ( ; abeg != aend; abeg++ )
      (*abeg)->ghostGetMessage(msg, numitems);
  }

  return numitems;
}

/////////////////////////////////////////////////////////////////////
// Apply the given sort-list to all the attributes.  The sort-list
// may be temporarily modified, thus it must be passed by non-const ref.
template<class PLayout>
737
void IpplParticleBase<PLayout>::sort(SortList_t &sortlist) {
gsell's avatar
gsell committed
738 739 740 741 742 743 744 745 746 747
  attrib_container_t::iterator abeg = AttribList.begin();
  attrib_container_t::iterator aend = AttribList.end();
  for ( ; abeg != aend; ++abeg )
    (*abeg)->sort(sortlist);
}


/////////////////////////////////////////////////////////////////////
// print it out
template<class PLayout>
748
std::ostream& operator<<(std::ostream& out, const IpplParticleBase<PLayout>& P) {
kraus's avatar
kraus committed
749 750


gsell's avatar
gsell committed
751 752 753 754 755 756 757 758 759 760 761 762
  out << "Particle object contents:";
  out << "\n  Total particles: " << P.getTotalNum();
  out << "\n  Local particles: " << P.getLocalNum();
  out << "\n  Attributes (including R and ID): " << P.numAttributes();
  out << "\n  Layout = " << P.getLayout();
  return out;
}


/////////////////////////////////////////////////////////////////////
// print out debugging information
template<class PLayout>
763
void IpplParticleBase<PLayout>::printDebug(Inform& o) {
kraus's avatar
kraus committed
764

gsell's avatar
gsell committed
765 766 767 768 769 770 771 772 773 774 775
  o << "PBase: total = " << getTotalNum() << ", local = " << getLocalNum();
  o << ", attributes = " << AttribList.size() << endl;
  for (attrib_container_t::size_type i=0; i < AttribList.size(); ++i) {
    o << "    ";
    AttribList[i]->printDebug(o);
    o << endl;
  }
  o << "    ";
  Layout->printDebug(o);
}

776 777 778 779 780 781 782
// vi: set et ts=4 sw=4 sts=4:
// Local Variables:
// mode:c
// c-basic-offset: 4
// indent-tabs-mode: nil
// require-final-newline: nil
// End: