TestParticleDebug.cpp 12.2 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4
// -*- C++ -*-
/***************************************************************************
 *
 * The IPPL Framework
5 6
 *
 * This program was prepared by PSI.
gsell's avatar
gsell committed
7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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 http://www.acl.lanl.gov/POOMS for more details
 *
 ***************************************************************************/

// -*- C++ -*-
/***************************************************************************
 *
 * The IPPL Framework
20
 *
gsell's avatar
gsell committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 *
 * Visit http://people.web.psi.ch/adelmann/ for more details
 *
 ***************************************************************************/

// TestParticleDebug.cpp , Tim Williams 8/11/1998
// This tests the functions [e,s]pap() and the function setFormat() from
// Utility/FieldDebug.[h,cpp] . These are meant to be called from the debugger,
// but this function tests whether they work (for a couple of possible calls)
// within a program. It also includes specialized function definitions like the
// user of ParticleDebug must have in his own source code in order to be able
// to access callable functions from the debugger, as an example for users.
// This function also tests the setInform() function, to specify the Inform
// object used internally by ParticleDebug functions. Constructing an Inform
// object that writes into a file makes it easy to do the comparson with
// correct output.

// include files
#include "Utility/IpplInfo.h"
#include "Utility/Inform.h"
#include "Index/NDIndex.h"
#include "FieldLayout/FieldLayout.h"
#include "Particle/ParticleSpatialLayout.h"
44
#include "Particle/IpplParticleBase.h"
gsell's avatar
gsell committed
45 46 47 48 49 50 51 52 53 54
#include "Particle/PAssign.h"
#include "Utility/RNGLattice.h"
#include "Utility/ParticleDebug.h"
#include "Utility/FieldDebug.h"

#include <iostream>
#include <fstream>

//-----------------------------------------------------------------------------
// Simple user Particles class definition
55
class Particles: public IpplParticleBase< ParticleSpatialLayout<double, 3> > {
gsell's avatar
gsell committed
56 57 58
public:
  //tjwdebug: add a scalar attribute:
  ParticleAttrib<double> sa;
59 60
  // Constructor:
  Particles(ParticleSpatialLayout<double,3>* psl) :
61
    IpplParticleBase<ParticleSpatialLayout<double, 3> >(psl) {
gsell's avatar
gsell committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      //tjwdebug: add a scalar attribute:
      addAttribute(sa);
  }
  // Destructor.
  virtual ~Particles() {}
  // Overload the = operator; does the same thing as the copy constructor.
  Particles& operator=(const Particles& p) {
    R = p.R;
    update();
    return(*this);
  }
};
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// User-inserted prototypes to get debugger access (examples for user ref.):
// Scalar (double):
void  dpap(ParticleAttrib<double>& pattr, bool docomm) {
  pap(pattr, docomm);
}
void depap(ParticleAttrib<double>& pattr, int i, bool docomm) {
  epap(pattr, i, docomm);
}
85
void dspap(ParticleAttrib<double>& pattr, int base, int bnd, int stride,
gsell's avatar
gsell committed
86 87 88 89 90 91 92 93 94 95
	   bool docomm) {
  spap(pattr, base, bnd, stride, docomm);
}
// 3D Vektor (double):
void  dv3pap(ParticleAttrib<Vektor<double,3> >& pattr, bool docomm) {
  pap(pattr, docomm);
}
void dv3epap(ParticleAttrib<Vektor<double,3> >& pattr, int i, bool docomm) {
  epap(pattr, i, docomm);
}
96
void dv3spap(ParticleAttrib<Vektor<double,3> >& pattr, int base, int bnd,
gsell's avatar
gsell committed
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
	     int stride, bool docomm) {
  spap(pattr, base, bnd, stride, docomm);
}
//-----------------------------------------------------------------------------

// Forward declarations:
void hardCodedOutput(char* filename); // Prototype of function defined below.
bool thediff(char* filename1, char* filename2);


int main(int argc, char *argv[])
{
  Ippl ippl(argc,argv);
  Inform testmsg(argv[0]);
  bool passed = true; // Pass/fail test

  // FieldLayout used for ParticleSpatialLayout below:
  int nx = 4, ny = 4, nz = 4;
  Index I(nx); Index J(ny); Index K(nz);
  // Specify multipple vnodes (8) to make sure this works right:
  FieldLayout<3> layout(I,J,K,PARALLEL,PARALLEL,PARALLEL,8);

  // Create Particles object
  ParticleSpatialLayout<double,3>* pslayout =
    new ParticleSpatialLayout<double,3>(layout);
  Particles parts(pslayout);
  int np = 16;
  parts.globalCreate(np);

  double deltaPX = 4.0/np;
  for (int p = 0; p < parts.getLocalNum(); p++) {
    double positionComponent = 0.0 + deltaPX*parts.ID[p];;
    parts.R[p](0) = positionComponent;
    parts.R[p](1) = positionComponent;
    parts.R[p](2) = positionComponent;
  }
  parts.update();

  // Inform output objects for test output:
136
  Inform* fdip =
gsell's avatar
gsell committed
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
    new Inform(NULL,"text.test.TestParticleDebug",Inform::OVERWRITE,0);
  Inform& fdi = *fdip;

  // --------------------------------------------------------------------------
  // WITH COMMUNICATION
  // --------------------------------------------------------------------------
  setPtclDbgInform(fdi);

  // Scalar ParticleAttribute -------------------------------------------------
  setFormat(16,1);

  fdi << endl << "--------pap(parts.ID, true)-------" << endl;
  pap(parts.ID, true);

  fdi << endl << "--------epap(parts.ID, np/4-1, true)-------" << endl;
  epap(parts.ID, np/4-1, true);

  fdi << endl << "--------spap(parts.ID, 0, np/4-1, 2, true)-------" << endl;
  spap(parts.ID, 0, np/4-1, 2, true);

  // 3D Vector ParticleAttribute ----------------------------------------------
  setFormat(1,8);

  fdi << endl << "--------pap(parts.R, true)-------" << endl;
  pap(parts.R, true);

  fdi << endl << "--------epap(parts.R, np/4-1, true)-------" << endl;
  epap(parts.R, np/4-1, true);

  fdi << endl << "--------spap(parts.R, 0, np/4-1, 2, true)-------" << endl;
  spap(parts.R, 0, np/4-1, 2, true);

  // --------------------------------------------------------------------------
  // NOW TURN OFF COMMUNICATION
  // --------------------------------------------------------------------------
  fdi.setPrintNode(INFORM_ALL_NODES);

  // Scalar ParticleAttribute -------------------------------------------------
  setFormat(16,1);

  fdi << endl << "--------pap(parts.ID, false)-------" << endl;
  pap(parts.ID, false);

  fdi << endl << "--------epap(parts.ID, np/4-1, false)-------" << endl;
  epap(parts.ID, np/4-1, false);

  fdi << endl << "--------spap(parts.ID, 0, np/4-1, 2, false)-------" << endl;
  spap(parts.ID, 0, np/4-1, 2, false);

  // 3D Vector ParticleAttribute ----------------------------------------------
  setFormat(1,8);

  fdi << endl << "--------pap(parts.R, false)-------" << endl;
  pap(parts.R, false);

  fdi << endl << "--------epap(parts.R, np/4-1, false)-------" << endl;
  epap(parts.R, np/4-1, false);

  fdi << endl << "--------spap(parts.R, 0, np/4-1, 2, false)-------" << endl;
  spap(parts.R, 0, np/4-1, 2, false);

  // Write out "by hand" into another file what the previous field-printing
  // functions should have produced; this will be compared with what they
  // actually did produce:
  hardCodedOutput("text.correct.TestParticleDebug");

  // Compare the two files by mocking up the Unix "diff" command:
  delete fdip;
205
  passed =
gsell's avatar
gsell committed
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
    thediff("text.test.TestParticleDebug","text.correct.TestParticleDebug");

  testmsg << ( (passed) ? "PASSED" : "FAILED" ) << endl;
  return 0;
}

//-----------------------------------------------------------------------------
// Mock up the Unix "diff" utility to compare two files:
//-----------------------------------------------------------------------------
bool thediff(char* filename1, char* filename2)
{
  bool same = true;
  char ch1, ch2;
  ifstream file1(filename1);
  ifstream file2(filename2);
  if (!file1) {
    cout << "thediff(): Failed to open file " << filename1 << endl;
    return(false);
  }
  if (!file2) {
    cout << "thediff(): Failed to open file " << filename2 << endl;
    return(false);
  }
  while (file1.get(ch1)) {          // Read file 1 char-by-char until eof
    if (file2.get(ch2)) {           // Read equivalent char from file 2
      if (ch1 != ch2) same = false; // If they're different,files are different
    }
    else {
      same = false;                 // If file 2 ends before file 1, different
    }
  }
  return same;
}

//-----------------------------------------------------------------------------
void hardCodedOutput(char* filename)
{
  ofstream of(filename);
244
  of << endl
gsell's avatar
gsell committed
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
     << "--------pap(parts.ID, true)-------" << endl
     << "....PE = 0 GLOBAL ptcle index subrange (0 : 15 : 1)...." << endl
     << "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 " << endl
     << "" << endl
     << "" << endl
     << "--------epap(parts.ID, np/4-1, true)-------" << endl
     << "....PE = 0 GLOBAL ptcle index subrange (3 : 3 : 1)...." << endl
     << "3 " << endl
     << "" << endl
     << "" << endl
     << "--------spap(parts.ID, 0, np/4-1, 2, true)-------" << endl
     << "....PE = 0 GLOBAL ptcle index subrange (0 : 2 : 2)...." << endl
     << "0 2 " << endl
     << "" << endl
     << "" << endl
     << "--------pap(parts.R, true)-------" << endl
     << "....PE = 0 GLOBAL ptcle index subrange (0 : 15 : 1)...." << endl
     << "( 0 , 0 , 0 ) " << endl
     << "( 0.25 , 0.25 , 0.25 ) " << endl
     << "( 0.5 , 0.5 , 0.5 ) " << endl
     << "( 0.75 , 0.75 , 0.75 ) " << endl
     << "( 1 , 1 , 1 ) " << endl
     << "( 1.25 , 1.25 , 1.25 ) " << endl
     << "( 1.5 , 1.5 , 1.5 ) " << endl
     << "( 1.75 , 1.75 , 1.75 ) " << endl
     << "( 2 , 2 , 2 ) " << endl
     << "( 2.25 , 2.25 , 2.25 ) " << endl
     << "( 2.5 , 2.5 , 2.5 ) " << endl
     << "( 2.75 , 2.75 , 2.75 ) " << endl
     << "( 3 , 3 , 3 ) " << endl
     << "( 3.25 , 3.25 , 3.25 ) " << endl
     << "( 3.5 , 3.5 , 3.5 ) " << endl
     << "( 3.75 , 3.75 , 3.75 ) " << endl
     << "" << endl
     << "" << endl
     << "--------epap(parts.R, np/4-1, true)-------" << endl
     << "....PE = 0 GLOBAL ptcle index subrange (3 : 3 : 1)...." << endl
     << "( 0.75 , 0.75 , 0.75 ) " << endl
     << "" << endl
     << "" << endl
     << "--------spap(parts.R, 0, np/4-1, 2, true)-------" << endl
     << "....PE = 0 GLOBAL ptcle index subrange (0 : 2 : 2)...." << endl
     << "( 0 , 0 , 0 ) " << endl
     << "( 0.5 , 0.5 , 0.5 ) " << endl
     << "" << endl
     << "" << endl
     << "--------pap(parts.ID, false)-------" << endl
     << "....PE = 0 LOCAL ptcle index range (0 : 15 : 1)...." << endl
     << "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 " << endl
     << "" << endl
     << "" << endl
     << "--------epap(parts.ID, np/4-1, false)-------" << endl
     << "....PE = 0 LOCAL ptcle index range (3 : 3 : 1)...." << endl
     << "3 " << endl
     << "" << endl
     << "" << endl
     << "--------spap(parts.ID, 0, np/4-1, 2, false)-------" << endl
     << "....PE = 0 LOCAL ptcle index range (0 : 3 : 2)...." << endl
     << "0 2 " << endl
     << "" << endl
     << "" << endl
     << "--------pap(parts.R, false)-------" << endl
     << "....PE = 0 LOCAL ptcle index range (0 : 15 : 1)...." << endl
     << "( 0 , 0 , 0 ) " << endl
     << "( 0.25 , 0.25 , 0.25 ) " << endl
     << "( 0.5 , 0.5 , 0.5 ) " << endl
     << "( 0.75 , 0.75 , 0.75 ) " << endl
     << "( 1 , 1 , 1 ) " << endl
     << "( 1.25 , 1.25 , 1.25 ) " << endl
     << "( 1.5 , 1.5 , 1.5 ) " << endl
     << "( 1.75 , 1.75 , 1.75 ) " << endl
     << "( 2 , 2 , 2 ) " << endl
     << "( 2.25 , 2.25 , 2.25 ) " << endl
     << "( 2.5 , 2.5 , 2.5 ) " << endl
     << "( 2.75 , 2.75 , 2.75 ) " << endl
     << "( 3 , 3 , 3 ) " << endl
     << "( 3.25 , 3.25 , 3.25 ) " << endl
     << "( 3.5 , 3.5 , 3.5 ) " << endl
     << "( 3.75 , 3.75 , 3.75 ) " << endl
     << "" << endl
     << "" << endl
     << "--------epap(parts.R, np/4-1, false)-------" << endl
     << "....PE = 0 LOCAL ptcle index range (3 : 3 : 1)...." << endl
     << "( 0.75 , 0.75 , 0.75 ) " << endl
     << "" << endl
     << "" << endl
     << "--------spap(parts.R, 0, np/4-1, 2, false)-------" << endl
     << "....PE = 0 LOCAL ptcle index range (0 : 3 : 2)...." << endl
     << "( 0 , 0 , 0 ) " << endl
     << "( 0.5 , 0.5 , 0.5 ) " << endl
     << "" << endl;
     of.close();
     return;
}

/***************************************************************************
 * $RCSfile: TestParticleDebug.cpp,v $   $Author: adelmann $
 * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:39 $
343
 * IPPL_VERSION_ID: $Id: TestParticleDebug.cpp,v 1.1.1.1 2003/01/23 07:40:39 adelmann Exp $
344
 ***************************************************************************/