Zoltan2  Version of the Day
GeometricGenerator.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 #include <Teuchos_Comm.hpp>
46 #include <Teuchos_ParameterList.hpp>
47 #include <Teuchos_FilteredIterator.hpp>
48 #include <Teuchos_ParameterEntry.hpp>
49 #include <iostream>
50 #include <ctime>
51 #include <limits>
52 #include <climits>
53 #include <string>
54 #include <cstdlib>
55 #include <sstream>
56 #include <fstream>
57 #include <Tpetra_MultiVector_decl.hpp>
60 #include <Teuchos_ArrayViewDecl.hpp>
61 
63 const std::string shapes[] = {"SQUARE", "RECTANGLE", "CIRCLE", "CUBE", "RECTANGULAR_PRISM", "SPHERE"};
64 #define SHAPE_COUNT 6
65 
67 const std::string distribution[] = {"distribution", "uniform"};
68 #define DISTRIBUTION_COUNT 2
69 
70 #define HOLE_ALLOC_STEP 10
71 #define MAX_WEIGHT_DIM 10
72 #define INVALID(STR) "Invalid argument at " + STR
73 #define INVALIDSHAPE(STR, DIM) "Invalid shape name " + STR + " for " + DIM + ".\nValid shapes are \"SQUARE\", \"RECTANGLE\", \"CIRCLE\" for 2D, and \"CUBE\", \"RECTANGULAR_PRISM\", \"SPHERE\" for 3D"
74 
75 #define INVALID_SHAPE_ARG(SHAPE, REQUIRED) "Invalid argument count for shape " + SHAPE + ". Requires " + REQUIRED + " argument(s)."
76 #define MAX_ITER_ALLOWED 500
77 
78 // KDD const std::string weight_distribution = "WeightDistribution";
79 const std::string weight_distribution_string = "WeightDistribution-";
80 template <typename T>
82  T x;
83  T y;
84  T z;
85  /*
86  bool isInArea(int dim, T *minCoords, T *maxCoords){
87  dim = min(3, dim);
88  for (int i = 0; i < dim; ++i){
89  bool maybe = true;
90  switch(i){
91  case 0:
92  maybe = x < maxCoords[i] && x > maxCoords[i];
93  break;
94  case 1:
95  maybe = y < maxCoords[i] && y > maxCoords[i];
96  break;
97  case 2:
98  maybe = z < maxCoords[i] && z > maxCoords[i];
99  break;
100  }
101  if (!maybe){
102  return false;
103  }
104  }
105  return true;
106  }
107  */
109  x = 0;y=0;z=0;
110  }
111 };
112 
113 template <typename T>
114 class Hole{
115 public:
117  T edge1, edge2, edge3;
118  Hole(CoordinatePoint<T> center_, T edge1_, T edge2_, T edge3_){
119  this->center.x = center_.x;
120  this->center.y = center_.y;
121  this->center.z = center_.z;
122  this->edge1 = edge1_;
123  this->edge2 = edge2_;
124  this->edge3 = edge3_;
125  }
126  virtual bool isInArea(CoordinatePoint<T> dot) = 0;
127  virtual ~Hole(){}
128 };
129 
130 template <typename T>
131 class SquareHole:public Hole<T>{
132 public:
133  SquareHole(CoordinatePoint<T> center_ , T edge_): Hole<T>(center_, edge_, 0 , 0){}
134  virtual ~SquareHole(){}
135  virtual bool isInArea(CoordinatePoint<T> dot){
136  return fabs(dot.x - this->center.x) < this->edge1 / 2 && fabs(dot.y - this->center.y) < this->edge1 / 2;
137  }
138 };
139 
140 template <typename T>
141 class RectangleHole:public Hole<T>{
142 public:
143  RectangleHole(CoordinatePoint<T> center_ , T edge_x_, T edge_y_): Hole<T>(center_, edge_x_, edge_y_, 0){}
144  virtual bool isInArea(CoordinatePoint<T> dot){
145  return fabs(dot.x - this->center.x) < this->edge1 / 2 && fabs(dot.y - this->center.y) < this->edge2 / 2;
146  }
147  virtual ~RectangleHole(){}
148 };
149 
150 template <typename T>
151 class CircleHole:public Hole<T>{
152 public:
153  CircleHole(CoordinatePoint<T> center_ , T edge_): Hole<T>(center_, edge_, 0 , 0){}
154  virtual ~CircleHole(){}
155  virtual bool isInArea(CoordinatePoint<T> dot){
156  return (dot.x - this->center.x)*(dot.x - this->center.x) + (dot.y - this->center.y) * (dot.y - this->center.y) < this->edge1 * this->edge1;
157  }
158 };
159 
160 template <typename T>
161 class CubeHole:public Hole<T>{
162 public:
163  CubeHole(CoordinatePoint<T> center_ , T edge_): Hole<T>(center_, edge_, 0 , 0){}
164  virtual ~CubeHole(){}
165  virtual bool isInArea(CoordinatePoint<T> dot){
166  return fabs(dot.x - this->center.x) < this->edge1 / 2 && fabs(dot.y - this->center.y) < this->edge1 / 2 && fabs(dot.z - this->center.z) < this->edge1 / 2;
167  }
168 };
169 
170 template <typename T>
171 class RectangularPrismHole:public Hole<T>{
172 public:
173  RectangularPrismHole(CoordinatePoint<T> center_ , T edge_x_, T edge_y_, T edge_z_): Hole<T>(center_, edge_x_, edge_y_, edge_z_){}
175  virtual bool isInArea(CoordinatePoint<T> dot){
176  return fabs(dot.x - this->center.x) < this->edge1 / 2 && fabs(dot.y - this->center.y) < this->edge2 / 2 && fabs(dot.z - this->center.z) < this->edge3 / 2;
177  }
178 };
179 
180 template <typename T>
181 class SphereHole:public Hole<T>{
182 public:
183  SphereHole(CoordinatePoint<T> center_ , T edge_): Hole<T>(center_, edge_, 0 , 0){}
184  virtual ~SphereHole(){}
185  virtual bool isInArea(CoordinatePoint<T> dot){
186  return fabs((dot.x - this->center.x) * (dot.x - this->center.x) * (dot.x - this->center.x)) +
187  fabs((dot.y - this->center.y) * (dot.y - this->center.y) * (dot.y - this->center.y)) +
188  fabs((dot.z - this->center.z) * (dot.z - this->center.z) * (dot.z - this->center.z))
189  <
190  this->edge1 * this->edge1 * this->edge1;
191  }
192 };
193 
194 template <typename T, typename weighttype>
196 public:
197  virtual weighttype getWeight(CoordinatePoint<T> P)=0;
198  virtual weighttype get1DWeight(T x)=0;
199  virtual weighttype get2DWeight(T x, T y)=0;
200  virtual weighttype get3DWeight(T x, T y, T z)=0;
202  virtual ~WeightDistribution(){};
203 };
204 
223 template <typename T, typename weighttype>
224 class SteppedEquation:public WeightDistribution<T, weighttype>{
225  T a1,a2,a3;
226  T b1,b2,b3;
227  T c;
228  T x1,y1,z1;
229 
230  T *steps;
231  weighttype *values;
232  int stepCount;
233 public:
234  SteppedEquation(T a1_, T a2_, T a3_, T b1_, T b2_, T b3_, T c_, T x1_, T y1_, T z1_, T *steps_, T *values_, int stepCount_):WeightDistribution<T,weighttype>(){
235  this->a1 = a1_;
236  this->a2 = a2_;
237  this->a3 = a3_;
238  this->b1 = b1_;
239  this->b2 = b2_;
240  this->b3 = b3_;
241  this->c = c_;
242  this->x1 = x1_;
243  this->y1 = y1_;
244  this->z1 = z1_;
245 
246 
247  this->stepCount = stepCount_;
248  if(this->stepCount > 0){
249  this->steps = new T[this->stepCount];
250  this->values = new T[this->stepCount + 1];
251 
252  for (int i = 0; i < this->stepCount; ++i){
253  this->steps[i] = steps_[i];
254  this->values[i] = values_[i];
255  }
256  this->values[this->stepCount] = values_[this->stepCount];
257  }
258 
259  }
260 
261  virtual ~SteppedEquation(){
262  if(this->stepCount > 0){
263  delete [] this->steps;
264  delete [] this->values;
265  }
266  }
267 
268 
269  virtual weighttype get1DWeight(T x){
270  T expressionRes = this->a1 * pow( (x - this->x1), b1) + c;
271  if(this->stepCount > 0){
272  for (int i = 0; i < this->stepCount; ++i){
273  if (expressionRes < this->steps[i]) return this->values[i];
274  }
275  return this->values[this->stepCount];
276  }
277  else {
278  return weighttype(expressionRes);
279  }
280  }
281 
282  virtual weighttype get2DWeight(T x, T y){
283  T expressionRes = this->a1 * pow( (x - this->x1), b1) + this->a2 * pow( (y - this->y1), b2) + c;
284  if(this->stepCount > 0){
285  for (int i = 0; i < this->stepCount; ++i){
286  if (expressionRes < this->steps[i]) return this->values[i];
287  }
288  return this->values[this->stepCount];
289  }
290  else {
291  return weighttype(expressionRes);
292  }
293  }
294 
295  void print (T x, T y, T z){
296  cout << this->a1 << "*" << "math.pow( (" << x << "- " << this->x1 << " ), " << b1 <<")" << "+" << this->a2<< "*"<< "math.pow( (" << y << "-" << this->y1 << "), " <<
297  b2 << " ) + " << this->a3 << " * math.pow( (" << z << "-" << this->z1 << "), " << b3 << ")+ " << c << " == " <<
298  this->a1 * pow( (x - this->x1), b1) + this->a2 * pow( (y - this->y1), b2) + this->a3 * pow( (z - this->z1), b3) + c << endl;
299 
300  }
301 
302  virtual weighttype get3DWeight(T x, T y, T z){
303  T expressionRes = this->a1 * pow( (x - this->x1), b1) + this->a2 * pow( (y - this->y1), b2) + this->a3 * pow( (z - this->z1), b3) + this->c;
304 
305  //this->print(x,y,z);
306  if(this->stepCount > 0){
307  for (int i = 0; i < this->stepCount; ++i){
308  if (expressionRes < this->steps[i]) {
309  //cout << "0exp:" << expressionRes << " step:" << steps[i] << " value:" << values[i] << endl;
310  return this->values[i];
311  }
312  }
313 
314  //cout << "1exp:" << expressionRes << " step:" << steps[stepCount] << " value:" << values[stepCount] << endl;
315  return this->values[this->stepCount];
316  }
317  else {
318  return weighttype(expressionRes);
319  }
320  }
321 
322  virtual weighttype getWeight(CoordinatePoint<T> p){
323  T expressionRes = this->a1 * pow( (p.x - this->x1), b1) + this->a2 * pow( (p.y - this->y1), b2) + this->a3 * pow( (p.z - this->z1), b3);
324  if(this->stepCount > 0){
325  for (int i = 0; i < this->stepCount; ++i){
326  if (expressionRes < this->steps[i]) return this->values[i];
327  }
328  return this->values[this->stepCount];
329  }
330  else {
331  return weighttype(expressionRes);
332  }
333  }
334 };
335 
336 template <typename T, typename lno_t, typename gno_t>
338 public:
345 
346  CoordinateDistribution(gno_t np_, int dim, int wSize) :
347  numPoints(np_), dimension(dim), requested(0), assignedPrevious(0),
348  worldSize(wSize){}
349 
350  virtual CoordinatePoint<T> getPoint(gno_t point_index, unsigned int &state) = 0;
351  virtual T getXCenter() = 0;
352  virtual T getXRadius() =0;
353 
354  void GetPoints(lno_t requestedPointcount, CoordinatePoint<T> *points /*preallocated sized numPoints*/,
355  Hole<T> **holes, lno_t holeCount,
356  float *sharedRatios_, int myRank){
357 
358  for (int i = 0; i < myRank; ++i){
359  //cout << "me:" << myRank << " i:" << i << " s:" << sharedRatios_[i]<< endl;
360  this->assignedPrevious += lno_t(sharedRatios_[i] * this->numPoints);
361  if (i < this->numPoints % this->worldSize){
362  this->assignedPrevious += 1;
363  }
364  }
365 
366  this->requested = requestedPointcount;
367 
368  unsigned int slice = UINT_MAX/(this->worldSize);
369  unsigned int stateBegin = myRank * slice;
370 
371 //#ifdef HAVE_ZOLTAN2_OMP
372 //#pragma omp parallel for
373 //#endif
374 #ifdef HAVE_ZOLTAN2_OMP
375 #pragma omp parallel
376 #endif
377  {
378  int me = 0;
379  int tsize = 1;
380 #ifdef HAVE_ZOLTAN2_OMP
381  me = omp_get_thread_num();
382  tsize = omp_get_num_threads();
383 #endif
384  unsigned int state = stateBegin + me * slice/(tsize);
385 
386 #ifdef HAVE_ZOLTAN2_OMP
387 #pragma omp for
388 #endif
389  for(lno_t cnt = 0; cnt < requestedPointcount; ++cnt){
390  lno_t iteration = 0;
391  while(1){
392  if(++iteration > MAX_ITER_ALLOWED) {
393  throw "Max number of Iteration is reached for point creation. Check the area criteria or hole coordinates.";
394  }
395  CoordinatePoint <T> p = this->getPoint( this->assignedPrevious + cnt, &state);
396 
397  bool isInHole = false;
398  for(lno_t i = 0; i < holeCount; ++i){
399  if(holes[i][0].isInArea(p)){
400  isInHole = true;
401  break;
402  }
403  }
404  if(isInHole) continue;
405  points[cnt].x = p.x;
406 
407  points[cnt].y = p.y;
408  points[cnt].z = p.z;
409  break;
410  }
411  }
412  }
413 //#pragma omp parallel
414  /*
415  {
416 
417  lno_t cnt = 0;
418  lno_t iteration = 0;
419  while (cnt < requestedPointcount){
420  if(++iteration > MAX_ITER_ALLOWED) {
421  throw "Max number of Iteration is reached for point creation. Check the area criteria or hole coordinates.";
422  }
423  CoordinatePoint <T> p = this->getPoint();
424 
425  bool isInHole = false;
426  for(lno_t i = 0; i < holeCount; ++i){
427  if(holes[i][0].isInArea(p)){
428  isInHole = true;
429  break;
430  }
431  }
432  if(isInHole) continue;
433  iteration = 0;
434  points[cnt].x = p.x;
435  points[cnt].y = p.y;
436  points[cnt].z = p.z;
437  ++cnt;
438  }
439  }
440  */
441  }
442 
443  void GetPoints(lno_t requestedPointcount, T **coords/*preallocated sized numPoints*/, lno_t tindex,
444  Hole<T> **holes, lno_t holeCount,
445  float *sharedRatios_, int myRank){
446 
447  for (int i = 0; i < myRank; ++i){
448  //cout << "me:" << myRank << " i:" << i << " s:" << sharedRatios_[i]<< endl;
449  this->assignedPrevious += lno_t(sharedRatios_[i] * this->numPoints);
450  if (i < this->numPoints % this->worldSize){
451  this->assignedPrevious += 1;
452  }
453  }
454 
455  this->requested = requestedPointcount;
456 
457  unsigned int slice = UINT_MAX/(this->worldSize);
458  unsigned int stateBegin = myRank * slice;
459 #ifdef HAVE_ZOLTAN2_OMP
460 #pragma omp parallel
461 #endif
462  {
463  int me = 0;
464  int tsize = 1;
465 #ifdef HAVE_ZOLTAN2_OMP
466  me = omp_get_thread_num();
467  tsize = omp_get_num_threads();
468 #endif
469  unsigned int state = stateBegin + me * (slice/(tsize));
470  /*
471 #pragma omp critical
472  {
473 
474  cout << "myRank:" << me << " stateBeg:" << stateBegin << " tsize:" << tsize << " state:" << state << " slice: " << slice / tsize << endl;
475  }
476  */
477 #ifdef HAVE_ZOLTAN2_OMP
478 #pragma omp for
479 #endif
480  for(lno_t cnt = 0; cnt < requestedPointcount; ++cnt){
481  lno_t iteration = 0;
482  while(1){
483  if(++iteration > MAX_ITER_ALLOWED) {
484  throw "Max number of Iteration is reached for point creation. Check the area criteria or hole coordinates.";
485  }
486  CoordinatePoint <T> p = this->getPoint( this->assignedPrevious + cnt, state);
487 
488  bool isInHole = false;
489  for(lno_t i = 0; i < holeCount; ++i){
490  if(holes[i][0].isInArea(p)){
491  isInHole = true;
492  break;
493  }
494  }
495  if(isInHole) continue;
496  coords[0][cnt + tindex] = p.x;
497  if(this->dimension > 1){
498  coords[1][cnt + tindex] = p.y;
499  if(this->dimension > 2){
500  coords[2][cnt + tindex] = p.z;
501  }
502  }
503  break;
504  }
505  }
506  }
507  }
508 };
509 
510 template <typename T, typename lno_t, typename gno_t>
512 public:
517 
518 
519  virtual T getXCenter(){
520  return center.x;
521  }
522  virtual T getXRadius(){
523  return standartDevx;
524  }
525 
527  T sd_x, T sd_y, T sd_z, int wSize) :
528  CoordinateDistribution<T,lno_t,gno_t>(np_,dim,wSize),
529  standartDevx(sd_x), standartDevy(sd_y), standartDevz(sd_z)
530  {
531  this->center.x = center_.x;
532  this->center.y = center_.y;
533  this->center.z = center_.z;
534  }
535 
536  virtual CoordinatePoint<T> getPoint(gno_t pindex, unsigned int &state){
537 
538  pindex = 0; // not used in normal distribution.
540 
541  for(int i = 0; i < this->dimension; ++i){
542  switch(i){
543  case 0:
544  p.x = normalDist(this->center.x, this->standartDevx, state);
545  break;
546  case 1:
547  p.y = normalDist(this->center.y, this->standartDevy, state);
548  break;
549  case 2:
550  p.z = normalDist(this->center.z, this->standartDevz, state);
551  break;
552  default:
553  throw "unsupported dimension";
554  }
555  }
556  return p;
557  }
558 
560 private:
561  T normalDist(T center_, T sd, unsigned int &state) {
562  static bool derived=false;
563  static T storedDerivation;
564  T polarsqrt, normalsquared, normal1, normal2;
565  if (!derived) {
566  do {
567  normal1=2.0*( T(rand_r(&state))/T(RAND_MAX) ) - 1.0;
568  normal2=2.0*( T(rand_r(&state))/T(RAND_MAX) ) - 1.0;
569  normalsquared=normal1*normal1+normal2*normal2;
570  } while ( normalsquared>=1.0 || normalsquared == 0.0);
571 
572  polarsqrt=sqrt(-2.0*log(normalsquared)/normalsquared);
573  storedDerivation=normal1*polarsqrt;
574  derived=true;
575  return normal2*polarsqrt*sd + center_;
576  }
577  else {
578  derived=false;
579  return storedDerivation*sd + center_;
580  }
581  }
582 };
583 
584 template <typename T, typename lno_t, typename gno_t>
586 public:
593 
594 
595  virtual T getXCenter(){
596  return (rightMostx - leftMostx)/2 + leftMostx;
597  }
598  virtual T getXRadius(){
599  return (rightMostx - leftMostx)/2;
600  }
601 
602 
603  CoordinateUniformDistribution(gno_t np_, int dim, T l_x, T r_x, T l_y, T r_y,
604  T l_z, T r_z, int wSize ) :
605  CoordinateDistribution<T,lno_t,gno_t>(np_,dim,wSize),
606  leftMostx(l_x), rightMostx(r_x), leftMosty(l_y), rightMosty(r_y),
607  leftMostz(l_z), rightMostz(r_z){}
608 
610  virtual CoordinatePoint<T> getPoint(gno_t pindex, unsigned int &state){
611 
612 
613  pindex = 0; //not used in uniform dist.
615  for(int i = 0; i < this->dimension; ++i){
616  switch(i){
617  case 0:
618  p.x = uniformDist(this->leftMostx, this->rightMostx, state);
619  break;
620  case 1:
621  p.y = uniformDist(this->leftMosty, this->rightMosty, state);
622  break;
623  case 2:
624  p.z = uniformDist(this->leftMostz, this->rightMostz, state);
625  break;
626  default:
627  throw "unsupported dimension";
628  }
629  }
630  return p;
631  }
632 
633 private:
634 
635  T uniformDist(T a, T b, unsigned int &state)
636  {
637  return (b-a)*(rand_r(&state) / double(RAND_MAX)) + a;
638  }
639 };
640 
641 template <typename T, typename lno_t, typename gno_t>
643 public:
650  gno_t along_X, along_Y, along_Z;
651  //T currentX, currentY, currentZ;
653  int myRank;
654  T xstep, ystep, zstep;
655  gno_t xshift, yshift, zshift;
656 
657  virtual T getXCenter(){
658  return (rightMostx - leftMostx)/2 + leftMostx;
659  }
660  virtual T getXRadius(){
661  return (rightMostx - leftMostx)/2;
662  }
663 
664 
665  CoordinateGridDistribution(gno_t alongX, gno_t alongY, gno_t alongZ, int dim,
666  T l_x, T r_x, T l_y, T r_y, T l_z, T r_z ,
667  int myRank_, int wSize) :
668  CoordinateDistribution<T,lno_t,gno_t>(alongX * alongY * alongZ,dim,wSize),
669  leftMostx(l_x), rightMostx(r_x), leftMosty(l_y), rightMosty(r_y), leftMostz(l_z), rightMostz(r_z), myRank(myRank_){
670  //currentX = leftMostx, currentY = leftMosty, currentZ = leftMostz;
671  this->processCnt = 0;
672  this->along_X = alongX; this->along_Y = alongY; this->along_Z = alongZ;
673 
674  if(this->along_X > 1)
675  this->xstep = (rightMostx - leftMostx) / (alongX - 1);
676  else
677  this->xstep = 1;
678  if(this->along_Y > 1)
679  this->ystep = (rightMosty - leftMosty) / (alongY - 1);
680  else
681  this->ystep = 1;
682  if(this->along_Z > 1)
683  this->zstep = (rightMostz - leftMostz) / (alongZ - 1);
684  else
685  this->zstep = 1;
686  xshift = 0; yshift = 0; zshift = 0;
687  }
688 
690  virtual CoordinatePoint<T> getPoint(gno_t pindex, unsigned int &state){
691  //lno_t before = processCnt + this->assignedPrevious;
692  //cout << "before:" << processCnt << " " << this->assignedPrevious << endl;
693  //lno_t xshift = 0, yshift = 0, zshift = 0;
694 
695  //lno_t tmp = before % (this->along_X * this->along_Y);
696  //xshift = tmp % this->along_X;
697  //yshift = tmp / this->along_X;
698  //zshift = before / (this->along_X * this->along_Y);
699 
700  state = 0; //not used here
701  this->zshift = pindex / (along_X * along_Y);
702  this->yshift = (pindex % (along_X * along_Y)) / along_X;
703  this->xshift = (pindex % (along_X * along_Y)) % along_X;
704 
705  //this->xshift = pindex / (along_Z * along_Y);
706  // this->zshift = (pindex % (along_Z * along_Y)) / along_Y;
707  //this->yshift = (pindex % (along_Z * along_Y)) % along_Y;
708 
710  p.x = xshift * this->xstep + leftMostx;
711  p.y = yshift * this->ystep + leftMosty;
712  p.z = zshift * this->zstep + leftMostz;
713 /*
714  ++xshift;
715  if(xshift == this->along_X){
716  ++yshift;
717  xshift = 0;
718  if(yshift == this->along_Y){
719  ++zshift;
720  yshift = 0;
721  }
722  }
723 */
724  /*
725  if(this->processCnt == 0){
726  this->xshift = this->assignedPrevious / (along_Z * along_Y);
727  //this->yshift = (this->assignedPrevious % (along_X * along_Y)) / along_X;
728  this->zshift = (this->assignedPrevious % (along_Z * along_Y)) / along_Y;
729  //this->xshift = (this->assignedPrevious % (along_X * along_Y)) % along_X;
730  this->yshift = (this->assignedPrevious % (along_Z * along_Y)) % along_Y;
731  ++this->processCnt;
732  }
733 
734  CoordinatePoint <T> p;
735  p.x = xshift * this->xstep + leftMostx;
736  p.y = yshift * this->ystep + leftMosty;
737  p.z = zshift * this->zstep + leftMostz;
738 
739  ++yshift;
740  if(yshift == this->along_Y){
741  ++zshift;
742  yshift = 0;
743  if(zshift == this->along_Z){
744  ++xshift;
745  zshift = 0;
746  }
747 
748  }
749  */
750  /*
751  if(this->requested - 1 > this->processCnt){
752  this->processCnt++;
753  } else {
754  cout << "req:" << this->requested << " pr:" <<this->processCnt << endl;
755  cout << "p:" << p.x << " " << p.y << " " << p.z << endl ;
756  cout << "s:" << xshift << " " << yshift << " " << zshift << endl ;
757  cout << "st:" << this->xstep << " " << this->ystep << " " << this->zstep << endl ;
758  }
759  */
760  return p;
761  }
762 
763 private:
764 
765 };
766 
767 template <typename T, typename lno_t, typename gno_t, typename node_t>
769 private:
770  Hole<T> **holes; //to represent if there is any hole in the input
771  int holeCount;
772  int coordinate_dimension; //dimension of the geometry
773  gno_t numGlobalCoords; //global number of coordinates requested to be created.
774  lno_t numLocalCoords;
775  float *loadDistributions; //sized as the number of processors, the load of each processor.
776  bool loadDistSet;
777  bool distinctCoordSet;
778  CoordinateDistribution<T, lno_t,gno_t> **coordinateDistributions;
779  int distributionCount;
780  //CoordinatePoint<T> *points;
781  T **coords;
782  T **wghts;
784  int weight_dimension; //dimension of the geometry
785 
786  //RCP< Tpetra::MultiVector<T, lno_t, gno_t, node_t> >tmVector;
787  //RCP< Tpetra::MultiVector<T, lno_t, gno_t, node_t> >tmwVector;
788  int worldSize;
789  int myRank;
790  T minx;
791  T maxx;
792  T miny;
793  T maxy;
794  T minz;
795  T maxz;
796  std::string outfile;
797 
798 
799 
800  template <typename tt>
801  tt getParamVal( const Teuchos::ParameterEntry& pe, const std::string &paramname){
802  tt returnVal;
803  try {
804  returnVal = Teuchos::getValue<tt>(pe);
805  }
806  catch (...){
807  throw INVALID(paramname);
808  }
809  return returnVal;
810  }
811 
812  int countChar (std::string inStr, char cntChar){
813  int cnt = 0;
814  for (unsigned int i = 0; i < inStr.size(); ++i){
815  if (inStr[i] == cntChar) {
816  cnt++;
817  }
818  }
819  return cnt;
820  }
821 
822  template <typename tt>
823  std::string toString(tt obj){
824  std::stringstream ss (std::stringstream::in |std::stringstream::out);
825  ss << obj;
826  std::string tmp = "";
827  ss >> tmp;
828  return tmp;
829  }
830 
831  template <typename tt>
832  tt fromString(std::string obj){
833  std::stringstream ss (std::stringstream::in | std::stringstream::out);
834  ss << obj;
835  tt tmp;
836  ss >> tmp;
837  if (ss.fail()){
838  throw "Cannot convert string " + obj;
839  }
840  return tmp;
841  }
842 
843 
844  void splitString(std::string inStr, char splitChar, std::string *outSplittedStr){
845  std::stringstream ss (std::stringstream::in | std::stringstream::out);
846  ss << inStr;
847  int cnt = 0;
848  while (!ss.eof()){
849  std::string tmp = "";
850  std::getline(ss, tmp ,splitChar);
851  outSplittedStr[cnt++] = tmp;
852  }
853  }
854 
855  /*
856  void getDistinctCoordinateDescription(std::string distinctDescription){
857 
858  this->distinctCoordinates = new bool[this->dimension];
859  if(distinctDescription != ""){
860  int argCnt = this->countChar(distinctDescription, ',') + 1;
861  if(argCnt != this->dimension) {
862  throw "Invalid parameter count for distinct_coordinates. Size should be equal to dimension.";
863  }
864 
865  std::string *splittedStr = new std::string[argCnt];
866  splitString(holeDescription, ',', splittedStr);
867 
868  for(int i = 0; i < argCnt; ++i){
869  if(splittedStr[i] == "T"){
870  distinctCoordinates[i] = true;
871  } else if(splittedStr[i] == "F"){
872  distinctCoordinates[i] = false;
873  } else {
874  throw "Invalid parameter " + splittedStr[i] + " for distinct_coordinates.";
875  }
876  }
877  delete []splittedStr;
878  }
879  else {
880  for(int i = 0; i < this->dimension; ++i){
881  distinctCoordinates[i] = false;
882  }
883  }
884  }
885  */
886 
887 
888  void getCoordinateDistributions(std::string coordinate_distributions){
889  if(coordinate_distributions == ""){
890  throw "At least one distribution function must be provided to coordinate generator.";
891  }
892 
893  int argCnt = this->countChar(coordinate_distributions, ',') + 1;
894  std::string *splittedStr = new std::string[argCnt];
895  splitString(coordinate_distributions, ',', splittedStr);
896  coordinateDistributions = (CoordinateDistribution<T, lno_t,gno_t> **) malloc(sizeof (CoordinateDistribution<T, lno_t,gno_t> *) * 1);
897  for(int i = 0; i < argCnt; ){
898  coordinateDistributions = (CoordinateDistribution<T, lno_t,gno_t> **)realloc((void *)coordinateDistributions, (this->distributionCount + 1)* sizeof(CoordinateDistribution<T, lno_t,gno_t> *));
899 
900  std::string distName = splittedStr[i++];
901  gno_t np_ = 0;
902  if(distName == "NORMAL"){
903  int reqArg = 5;
904  if (this->coordinate_dimension == 3){
905  reqArg = 7;
906  }
907  if(i + reqArg > argCnt) {
908  std::string tmp = toString<int>(reqArg);
909  throw INVALID_SHAPE_ARG(distName, tmp);
910  }
911  np_ = fromString<gno_t>(splittedStr[i++]);
913 
914  pp.x = fromString<T>(splittedStr[i++]);
915  pp.y = fromString<T>(splittedStr[i++]);
916  pp.z = 0;
917  if(this->coordinate_dimension == 3){
918  pp.z = fromString<T>(splittedStr[i++]);
919  }
920 
921  T sd_x = fromString<T>(splittedStr[i++]);
922  T sd_y = fromString<T>(splittedStr[i++]);
923  T sd_z = 0;
924  if(this->coordinate_dimension == 3){
925  sd_z = fromString<T>(splittedStr[i++]);
926  }
927  this->coordinateDistributions[this->distributionCount++] = new CoordinateNormalDistribution<T, lno_t,gno_t>(np_, this->coordinate_dimension, pp , sd_x, sd_y, sd_z, this->worldSize );
928 
929  } else if(distName == "UNIFORM" ){
930  int reqArg = 5;
931  if (this->coordinate_dimension == 3){
932  reqArg = 7;
933  }
934  if(i + reqArg > argCnt) {
935  std::string tmp = toString<int>(reqArg);
936  throw INVALID_SHAPE_ARG(distName, tmp);
937  }
938  np_ = fromString<gno_t>(splittedStr[i++]);
939  T l_x = fromString<T>(splittedStr[i++]);
940  T r_x = fromString<T>(splittedStr[i++]);
941  T l_y = fromString<T>(splittedStr[i++]);
942  T r_y = fromString<T>(splittedStr[i++]);
943 
944  T l_z = 0, r_z = 0;
945 
946  if(this->coordinate_dimension == 3){
947  l_z = fromString<T>(splittedStr[i++]);
948  r_z = fromString<T>(splittedStr[i++]);
949  }
950 
951  this->coordinateDistributions[this->distributionCount++] = new CoordinateUniformDistribution<T, lno_t,gno_t>( np_, this->coordinate_dimension, l_x, r_x, l_y, r_y, l_z, r_z, this->worldSize );
952  } else if (distName == "GRID"){
953  int reqArg = 6;
954  if(this->coordinate_dimension == 3){
955  reqArg = 9;
956  }
957  if(i + reqArg > argCnt) {
958  std::string tmp = toString<int>(reqArg);
959  throw INVALID_SHAPE_ARG(distName, tmp);
960  }
961 
962  gno_t np_x = fromString<gno_t>(splittedStr[i++]);
963  gno_t np_y = fromString<gno_t>(splittedStr[i++]);
964  gno_t np_z = 1;
965 
966 
967  if(this->coordinate_dimension == 3){
968  np_z = fromString<gno_t>(splittedStr[i++]);
969  }
970 
971  np_ = np_x * np_y * np_z;
972  T l_x = fromString<T>(splittedStr[i++]);
973  T r_x = fromString<T>(splittedStr[i++]);
974  T l_y = fromString<T>(splittedStr[i++]);
975  T r_y = fromString<T>(splittedStr[i++]);
976 
977  T l_z = 0, r_z = 0;
978 
979  if(this->coordinate_dimension == 3){
980  l_z = fromString<T>(splittedStr[i++]);
981  r_z = fromString<T>(splittedStr[i++]);
982  }
983 
984  if(np_x < 1 || np_z < 1 || np_y < 1 ){
985  throw "Provide at least 1 point along each dimension for grid test.";
986  }
987  //cout << "ly:" << l_y << " ry:" << r_y << endl;
988  this->coordinateDistributions[this->distributionCount++] = new CoordinateGridDistribution<T, lno_t,gno_t>
989  (np_x, np_y,np_z, this->coordinate_dimension, l_x, r_x,l_y, r_y, l_z, r_z , this->myRank, this->worldSize);
990 
991  }
992  else {
993  std::string tmp = toString<int>(this->coordinate_dimension);
994  throw INVALIDSHAPE(distName, tmp);
995  }
996  this->numGlobalCoords += (gno_t) np_;
997  }
998  delete []splittedStr;
999  }
1000 
1001  void getProcLoadDistributions(std::string proc_load_distributions){
1002 
1003  this->loadDistributions = new float[this->worldSize];
1004  if(proc_load_distributions == ""){
1005  float r = 1.0 / this->worldSize;
1006  for(int i = 0; i < this->worldSize; ++i){
1007  this->loadDistributions[i] = r;
1008  }
1009  } else{
1010 
1011 
1012  int argCnt = this->countChar(proc_load_distributions, ',') + 1;
1013  if(argCnt != this->worldSize) {
1014  throw "Invalid parameter count load distributions. Given " + toString<int>(argCnt) + " processor size is " + toString<int>(this->worldSize);
1015  }
1016  std::string *splittedStr = new std::string[argCnt];
1017  splitString(proc_load_distributions, ',', splittedStr);
1018  for(int i = 0; i < argCnt; ++i){
1019  this->loadDistributions[i] = fromString<float>(splittedStr[i]);
1020  }
1021  delete []splittedStr;
1022 
1023 
1024  float sum = 0;
1025  for(int i = 0; i < this->worldSize; ++i){
1026  sum += this->loadDistributions[i];
1027  }
1028  if (fabs(sum - 1.0) > 10*std::numeric_limits<float>::epsilon()){
1029  throw "Processor load ratios do not sum to 1.0.";
1030  }
1031  }
1032 
1033  }
1034 
1035  void getHoles(std::string holeDescription){
1036  if(holeDescription == ""){
1037  return;
1038  }
1039  this->holes = (Hole<T> **) malloc(sizeof (Hole <T>*));
1040  int argCnt = this->countChar(holeDescription, ',') + 1;
1041  std::string *splittedStr = new std::string[argCnt];
1042  splitString(holeDescription, ',', splittedStr);
1043 
1044  for(int i = 0; i < argCnt; ){
1045  holes = (Hole<T> **)realloc((void *)holes, (this->holeCount + 1)* sizeof(Hole<T> *));
1046 
1047  std::string shapeName = splittedStr[i++];
1048  if(shapeName == "SQUARE" && this->coordinate_dimension == 2){
1049  if(i + 3 > argCnt) {
1050  throw INVALID_SHAPE_ARG(shapeName, "3");
1051  }
1052  CoordinatePoint<T> pp;
1053  pp.x = fromString<T>(splittedStr[i++]);
1054  pp.y = fromString<T>(splittedStr[i++]);
1055  T edge = fromString<T>(splittedStr[i++]);
1056  this->holes[this->holeCount++] = new SquareHole<T>(pp, edge);
1057  } else if(shapeName == "RECTANGLE" && this->coordinate_dimension == 2){
1058  if(i + 4 > argCnt) {
1059  throw INVALID_SHAPE_ARG(shapeName, "4");
1060  }
1061  CoordinatePoint<T> pp;
1062  pp.x = fromString<T>(splittedStr[i++]);
1063  pp.y = fromString<T>(splittedStr[i++]);
1064  T edgex = fromString<T>(splittedStr[i++]);
1065  T edgey = fromString<T>(splittedStr[i++]);
1066 
1067  this->holes[this->holeCount++] = new RectangleHole<T>(pp, edgex,edgey);
1068  } else if(shapeName == "CIRCLE" && this->coordinate_dimension == 2){
1069  if(i + 3 > argCnt) {
1070  throw INVALID_SHAPE_ARG(shapeName, "3");
1071  }
1072  CoordinatePoint<T> pp;
1073  pp.x = fromString<T>(splittedStr[i++]);
1074  pp.y = fromString<T>(splittedStr[i++]);
1075  T r = fromString<T>(splittedStr[i++]);
1076  this->holes[this->holeCount++] = new CircleHole<T>(pp, r);
1077  } else if(shapeName == "CUBE" && this->coordinate_dimension == 3){
1078  if(i + 4 > argCnt) {
1079  throw INVALID_SHAPE_ARG(shapeName, "4");
1080  }
1081  CoordinatePoint<T> pp;
1082  pp.x = fromString<T>(splittedStr[i++]);
1083  pp.y = fromString<T>(splittedStr[i++]);
1084  pp.z = fromString<T>(splittedStr[i++]);
1085  T edge = fromString<T>(splittedStr[i++]);
1086  this->holes[this->holeCount++] = new CubeHole<T>(pp, edge);
1087  } else if(shapeName == "RECTANGULAR_PRISM" && this->coordinate_dimension == 3){
1088  if(i + 6 > argCnt) {
1089  throw INVALID_SHAPE_ARG(shapeName, "6");
1090  }
1091  CoordinatePoint<T> pp;
1092  pp.x = fromString<T>(splittedStr[i++]);
1093  pp.y = fromString<T>(splittedStr[i++]);
1094  pp.z = fromString<T>(splittedStr[i++]);
1095  T edgex = fromString<T>(splittedStr[i++]);
1096  T edgey = fromString<T>(splittedStr[i++]);
1097  T edgez = fromString<T>(splittedStr[i++]);
1098  this->holes[this->holeCount++] = new RectangularPrismHole<T>(pp, edgex, edgey, edgez);
1099 
1100  } else if(shapeName == "SPHERE" && this->coordinate_dimension == 3){
1101  if(i + 4 > argCnt) {
1102  throw INVALID_SHAPE_ARG(shapeName, "4");
1103  }
1104  CoordinatePoint<T> pp;
1105  pp.x = fromString<T>(splittedStr[i++]);
1106  pp.y = fromString<T>(splittedStr[i++]);
1107  pp.z = fromString<T>(splittedStr[i++]);
1108  T r = fromString<T>(splittedStr[i++]);
1109  this->holes[this->holeCount++] = new SphereHole<T>(pp, r);
1110  } else {
1111  std::string tmp = toString<int>(this->coordinate_dimension);
1112  throw INVALIDSHAPE(shapeName, tmp);
1113  }
1114  }
1115  delete [] splittedStr;
1116  }
1117 
1118  void getWeightDistribution(std::string *weight_distribution_arr, int wdimension){
1119  int wcount = 0;
1120 
1121  this->wd = new WeightDistribution<T,T> *[wdimension];
1122  for(int ii = 0; ii < MAX_WEIGHT_DIM; ++ii){
1123  std::string weight_distribution = weight_distribution_arr[ii];
1124  if(weight_distribution == "") continue;
1125  if(wcount == wdimension) {
1126  throw "Weight Dimension is provided as " + toString<int>(wdimension) + ". More weight distribution is provided.";
1127  }
1128 
1129  int count = this->countChar(weight_distribution, ' ');
1130  std::string *splittedStr = new string[count + 1];
1131  this->splitString(weight_distribution, ' ', splittedStr);
1132  //cout << count << endl;
1133  T c=1;
1134  T a1=0,a2=0,a3=0;
1135  T x1=0,y1=0,z1=0;
1136  T b1=1,b2=1,b3=1;
1137  T *steps = NULL;
1138  T *values= NULL;
1139  int stepCount = 0;
1140  int valueCount = 1;
1141 
1142  for (int i = 1; i < count + 1; ++i){
1143  int assignmentCount = this->countChar(splittedStr[i], '=');
1144  if (assignmentCount != 1){
1145  throw "Error at the argument " + splittedStr[i];
1146  }
1147  std::string parameter_vs_val[2];
1148  this->splitString(splittedStr[i], '=', parameter_vs_val);
1149  std::string parameter = parameter_vs_val[0];
1150  std::string value = parameter_vs_val[1];
1151  //cout << "parameter:" << parameter << " value:" << value << endl;
1152 
1153  if (parameter == "a1"){
1154  a1 = this->fromString<T>(value);
1155  }
1156  else if (parameter == "a2"){
1157  if(this->coordinate_dimension > 1){
1158  a2 = this->fromString<T>(value);
1159  }
1160  else {
1161  throw parameter+ " argument is not valid when dimension is " + toString<int>(this->coordinate_dimension);
1162  }
1163 
1164  }
1165  else if (parameter == "a3"){
1166  if(this->coordinate_dimension > 2){
1167  a3 = this->fromString<T>(value);
1168  }
1169  else {
1170  throw parameter+ " argument is not valid when dimension is " + toString<int>(this->coordinate_dimension);
1171  }
1172  }
1173  else if (parameter == "b1"){
1174  b1 = this->fromString<T>(value);
1175  }
1176  else if (parameter == "b2"){
1177  if(this->coordinate_dimension > 1){
1178  b2 = this->fromString<T>(value);
1179  }
1180  else {
1181  throw parameter+ " argument is not valid when dimension is " + toString<int>(this->coordinate_dimension);
1182  }
1183  }
1184  else if (parameter == "b3"){
1185 
1186  if(this->coordinate_dimension > 2){
1187  b3 = this->fromString<T>(value);
1188  }
1189  else {
1190  throw parameter+ " argument is not valid when dimension is " + toString<int>(this->coordinate_dimension);
1191  }
1192  }
1193  else if (parameter == "c"){
1194  c = this->fromString<T>(value);
1195  }
1196  else if (parameter == "x1"){
1197  x1 = this->fromString<T>(value);
1198  }
1199  else if (parameter == "y1"){
1200  if(this->coordinate_dimension > 1){
1201  y1 = this->fromString<T>(value);
1202  }
1203  else {
1204  throw parameter+ " argument is not valid when dimension is " + toString<int>(this->coordinate_dimension);
1205  }
1206  }
1207  else if (parameter == "z1"){
1208  if(this->coordinate_dimension > 2){
1209  z1 = this->fromString<T>(value);
1210  }
1211  else {
1212  throw parameter+ " argument is not valid when dimension is " + toString<int>(this->coordinate_dimension);
1213  }
1214  }
1215  else if (parameter == "steps"){
1216  stepCount = this->countChar(value, ',') + 1;
1217  std::string *stepstr = new std::string[stepCount];
1218  this->splitString(value, ',', stepstr);
1219  steps = new T[stepCount];
1220  for (int j = 0; j < stepCount; ++j){
1221  steps[j] = this->fromString<T>(stepstr[j]);
1222  }
1223  delete [] stepstr;
1224  }
1225  else if (parameter == "values"){
1226  valueCount = this->countChar(value, ',') + 1;
1227  std::string *stepstr = new std::string[valueCount];
1228  this->splitString(value, ',', stepstr);
1229  values = new T[valueCount];
1230  for (int j = 0; j < valueCount; ++j){
1231  values[j] = this->fromString<T>(stepstr[j]);
1232  }
1233  delete [] stepstr;
1234  }
1235  else {
1236  throw "Invalid parameter name at " + splittedStr[i];
1237  }
1238  }
1239 
1240  delete []splittedStr;
1241  if(stepCount + 1!= valueCount){
1242  throw "Step count: " + this->toString<int>(stepCount) + " must be 1 less than value count: " + this->toString<int>(valueCount);
1243  }
1244 
1245 
1246  this->wd[wcount++] = new SteppedEquation<T,T>(a1, a2, a3, b1, b2, b3, c, x1, y1, z1, steps, values, stepCount);
1247 
1248  if(stepCount > 0){
1249  delete [] steps;
1250  delete [] values;
1251 
1252  }
1253  }
1254  if(wcount != this->weight_dimension){
1255  throw "Weight Dimension is provided as " + toString<int>(wdimension) + ". But " + toString<int>(wcount)+" weight distributions are provided.";
1256  }
1257  }
1258 
1259  void parseParams(Teuchos::ParameterList params){
1260  try {
1261  std::string holeDescription = "";
1262  std::string proc_load_distributions = "";
1263  std::string distinctDescription = "";
1264  std::string coordinate_distributions = "";
1265  std::string weight_dimension_parameters[MAX_WEIGHT_DIM];
1266  for (int i = 0; i < MAX_WEIGHT_DIM; ++i){
1267  weight_dimension_parameters[i] = "";
1268  }
1269 
1270 
1271  for (Teuchos::ParameterList::ConstIterator pit = params.begin(); pit != params.end(); ++pit ){
1272  const std::string &paramName = params.name(pit);
1273 
1274  const Teuchos::ParameterEntry &pe = params.entry(pit);
1275 
1276  if(paramName.find("hole-") == 0){
1277  if(holeDescription == ""){
1278  holeDescription = getParamVal<std::string>(pe, paramName);
1279  }
1280  else {
1281  holeDescription +=","+getParamVal<std::string>(pe, paramName);
1282  }
1283  }
1284  else if(paramName.find("distribution-") == 0){
1285  if(coordinate_distributions == "")
1286  coordinate_distributions = getParamVal<std::string>(pe, paramName);
1287  else
1288  coordinate_distributions += ","+getParamVal<std::string>(pe, paramName);
1289  //cout << coordinate_distributions << endl;
1290  //TODO coordinate distribution description
1291  }
1292 
1293  else if (paramName.find(weight_distribution_string) == 0){
1294  std::string weight_dist_param = paramName.substr(weight_distribution_string.size());
1295  int dash_pos = weight_dist_param.find("-");
1296  std::string distribution_index_string = weight_dist_param.substr(0, dash_pos);
1297  int distribution_index = fromString<int>(distribution_index_string);
1298 
1299  if(distribution_index >= MAX_WEIGHT_DIM){
1300  throw "Given distribution index:" + distribution_index_string + " larger than maximum allowed weight dimension:" + toString<int>(MAX_WEIGHT_DIM);
1301  }
1302  weight_dimension_parameters[distribution_index] += " " + weight_dist_param.substr(dash_pos + 1)+ "="+ getParamVal<std::string>(pe, paramName);
1303  }
1304  else if(paramName == "dim"){
1305  int dim = fromString<int>(getParamVal<std::string>(pe, paramName));
1306  if(dim < 2 && dim > 3){
1307  throw INVALID(paramName);
1308  } else {
1309  this->coordinate_dimension = dim;
1310  }
1311  }
1312  else if(paramName == "wdim"){
1313  int dim = fromString<int>(getParamVal<std::string>(pe, paramName));
1314  if(dim < 1 && dim > MAX_WEIGHT_DIM){
1315  throw INVALID(paramName);
1316  } else {
1317  this->weight_dimension = dim;
1318  }
1319  }
1320 
1321  else if(paramName == "proc_load_distributions"){
1322  proc_load_distributions = getParamVal<std::string>(pe, paramName);
1323  this->loadDistSet = true;
1324  }
1325 
1326  else if(paramName == "distinct_coordinates"){
1327  distinctDescription = getParamVal<std::string>(pe, paramName);
1328  if(distinctDescription == "T"){
1329  this->distinctCoordSet = true;
1330  } else if(distinctDescription == "F"){
1331  this->distinctCoordSet = true;
1332  } else {
1333  throw "Invalid parameter for distinct_coordinates: " + distinctDescription + ". Candiates: T or F." ;
1334  }
1335  }
1336 
1337  else if(paramName == "out_file"){
1338  this->outfile = getParamVal<std::string>(pe, paramName);
1339  }
1340 
1341  else {
1342  throw INVALID(paramName);
1343  }
1344  }
1345 
1346 
1347  if(this->coordinate_dimension == 0){
1348  throw "Dimension must be provided to coordinate generator.";
1349  }
1350  /*
1351  if(this->globalNumCoords == 0){
1352  throw "Number of coordinates must be provided to coordinate generator.";
1353  }
1354  */
1355  /*
1356  if(maxx <= minx ){
1357  throw "Error: maxx= "+ toString<T>(maxx)+ " and minx=" + toString<T>(minx);
1358  }
1359  if(maxy <= miny ){
1360  throw "Error: maxy= "+ toString<T>(maxy)+ " and miny=" + toString<T>(miny);
1361 
1362  }
1363  if(this->dimension == 3 && maxz <= minz ){
1364  throw "Error: maxz= "+ toString<T>(maxz)+ " and minz=" + toString<T>(minz);
1365  }
1366  */
1367  if (this->loadDistSet && this->distinctCoordSet){
1368  throw "distinct_coordinates and proc_load_distributions parameters cannot be satisfied together.";
1369  }
1370  this->getHoles(holeDescription);
1371  //this->getDistinctCoordinateDescription(distinctDescription);
1372  this->getProcLoadDistributions(proc_load_distributions);
1373  this->getCoordinateDistributions(coordinate_distributions);
1374  this->getWeightDistribution(weight_dimension_parameters, this->weight_dimension);
1375  /*
1376  if(this->numGlobalCoords <= 0){
1377  throw "Must have at least 1 point";
1378  }
1379  */
1380  }
1381  catch(std::string s){
1382  throw s;
1383  }
1384 
1385  catch(char * s){
1386  throw s;
1387  }
1388 
1389  catch(char const* s){
1390  throw s;
1391  }
1392 
1393  }
1394 public:
1395 
1397  if(this->holes){
1398  for (int i = 0; i < this->holeCount; ++i){
1399  delete this->holes[i];
1400  }
1401  free (this->holes);
1402  }
1403 
1404 
1405  delete []loadDistributions; //sized as the number of processors, the load of each processor.
1406  //delete []distinctCoordinates; // if processors have different or same range for coordinates to be created.
1407  if(coordinateDistributions){
1408 
1409  for (int i = 0; i < this->distributionCount; ++i){
1410  delete this->coordinateDistributions[i];
1411  }
1412  free (this->coordinateDistributions);
1413  }
1414  if (this->wd){
1415  for (int i = 0; i < this->weight_dimension; ++i){
1416  delete this->wd[i];
1417  }
1418  delete []this->wd;
1419  }
1420 
1421  if(this->weight_dimension){
1422  for(int i = 0; i < this->weight_dimension; ++i)
1423  delete [] this->wghts[i];
1424  delete []this->wghts;
1425  }
1426  if(this->coordinate_dimension){
1427  for(int i = 0; i < this->coordinate_dimension; ++i)
1428  delete [] this->coords[i];
1429  delete [] this->coords;
1430  }
1431  //delete []this->points;
1432  }
1433 
1435  cout <<"\nGeometric Generator Parameter File Format:" << endl;
1436  cout <<"- dim=Coordinate Dimension: 2 or 3" << endl;
1437  cout <<"- Available distributions:" << endl;
1438  cout <<"\tUNIFORM: -> distribution-1=UNIFORM,NUMCOORDINATES,XMIN,XMAX,YMIN,YMAX{,ZMIN,ZMAX}" << endl;
1439  cout <<"\tGRID: -> distribution-2=GRID,XLENGTH,YLENGTH{,ZLENGTH},XMIN,XMAX,YMIN,YMAX{,ZMIN,ZMAX}" << endl;
1440  cout <<"\tNORMAL: -> distribution-3=NORMAL,XCENTER,YCENTER{,ZCENTER},XSD,YSD,{,ZSD}" << endl;
1441  cout <<"- wdim=weight_dimension: weight dimension >= 0. There should be as many weight function as weight dimension." << endl;
1442  cout <<"- Weight Equation: w = (a1 * (x - x1)^b1) + (a2 * (y - y1)^b2) + (a3 * (z - z1)^b3) + c" << endl;
1443  cout << "Parameter settings:" << endl;
1444  cout << "\tWeightDistribution-1-a1=a1 " << endl;
1445  cout << "\tWeightDistribution-1-a2=a2 " << endl;
1446  cout << "\tWeightDistribution-1-a3=a3 " << endl;
1447  cout << "\tWeightDistribution-1-b1=b1 " << endl;
1448  cout << "\tWeightDistribution-1-b2=b2 " << endl;
1449  cout << "\tWeightDistribution-1-b3=b3 " << endl;
1450  cout << "\tWeightDistribution-1-x1=x1 " << endl;
1451  cout << "\tWeightDistribution-1-y1=y1 " << endl;
1452  cout << "\tWeightDistribution-1-z1=z1 " << endl;
1453  cout << "\tWeightDistribution-1-c=c " << endl;
1454  cout << "\tIt is possible to set step function to the result of weight equation." << endl;
1455  cout << "\tWeightDistribution-1-steps=step1,step2,step3:increasing order" << endl;
1456  cout << "\tWeightDistribution-1-values=val1,val2,val3,val4." << endl;
1457  cout << "\t\tIf w < step1 -> w = val1" << endl;
1458  cout << "\t\tElse if w < step2 -> w = val2" << endl;
1459  cout << "\t\tElse if w < step3 -> w = val3" << endl;
1460  cout << "\t\tElse -> w = val4" << endl;
1461  cout <<"- Holes:" << endl;
1462  cout << "\thole-1:SPHERE,XCENTER,YCENTER,ZCENTER,RADIUS (only for dim=3)" << endl;
1463  cout << "\thole-2:CUBE,XCENTER,YCENTER,ZCENTER,EDGE (only for dim=3)" << endl;
1464  cout << "\thole-3:RECTANGULAR_PRISM,XCENTER,YCENTER,ZCENTER,XEDGE,YEDGE,ZEDGE (only for dim=3)" << endl;
1465  cout << "\thole-4:SQUARE,XCENTER,YCENTER,EDGE (only for dim=2)" << endl;
1466  cout << "\thole-5:RECTANGLE,XCENTER,YCENTER,XEDGE,YEDGE (only for dim=2)" << endl;
1467  cout << "\thole-6:CIRCLE,XCENTER,YCENTER,RADIUS (only for dim=2)" << endl;
1468  cout << "- out_file:out_file_path : if provided output will be written to files." << endl;
1469  cout << "- proc_load_distributions:ratio_0, ratio_1, ratio_2....ratio_n. Loads of each processor, should be as many as MPI ranks and should sum up to 1." << endl;
1470 
1471  }
1472 
1473  GeometricGenerator(Teuchos::ParameterList &params, const RCP<const Teuchos::Comm<int> > & comm_){
1474  this->wd = NULL;
1475  this->holes = NULL; //to represent if there is any hole in the input
1476  this->coordinate_dimension = 0; //dimension of the geometry
1477  this->weight_dimension = 0; //dimension of the geometry
1478  this->worldSize = comm_->getSize(); //comminication world object.
1479  this->numGlobalCoords = 0; //global number of coordinates requested to be created.
1480  this->loadDistributions = NULL; //sized as the number of processors, the load of each processor.
1481  //this->distinctCoordinates = NULL; // if processors have different or same range for coordinates to be created.
1482  this->coordinateDistributions = NULL;
1483  this->holeCount = 0;
1484  this->distributionCount = 0;
1485  this->outfile = "";
1486  //this->points = NULL;
1487 
1488  /*
1489  this->minx = 0;
1490  this->maxx = 0;
1491  this->miny = 0;
1492  this->maxy = 0;
1493  this->minz = 0;
1494  this->maxz = 0;
1495  */
1496  this->loadDistSet = false;
1497  this->distinctCoordSet = false;
1498  this->myRank = comm_->getRank();
1499 
1500  try {
1501  this->parseParams(params);
1502  }
1503  catch(std::string s){
1504  if(myRank == 0){
1505  print_description();
1506  }
1507  throw s;
1508  }
1509 
1510 
1511 
1512 
1513  lno_t myPointCount = 0;
1514  this->numGlobalCoords = 0;
1515 
1516  gno_t prefixSum = 0;
1517  for(int i = 0; i < this->distributionCount; ++i){
1518  for(int ii = 0; ii < this->worldSize; ++ii){
1519  lno_t increment = lno_t (this->coordinateDistributions[i]->numPoints * this->loadDistributions[ii]);
1520  if (ii < this->coordinateDistributions[i]->numPoints % this->worldSize){
1521  increment += 1;
1522  }
1523  this->numGlobalCoords += increment;
1524  if(ii < myRank){
1525  prefixSum += increment;
1526  }
1527  }
1528  myPointCount += lno_t(this->coordinateDistributions[i]->numPoints * this->loadDistributions[myRank]);
1529  if (myRank < this->coordinateDistributions[i]->numPoints % this->worldSize){
1530  myPointCount += 1;
1531  }
1532  }
1533 
1534  this->coords = new T *[this->coordinate_dimension];
1535  for(int i = 0; i < this->coordinate_dimension; ++i){
1536  this->coords[i] = new T[myPointCount];
1537  }
1538 
1539  for (int ii = 0; ii < this->coordinate_dimension; ++ii){
1540 #ifdef HAVE_ZOLTAN2_OMP
1541 #pragma omp parallel for
1542 #endif
1543  for(int i = 0; i < myPointCount; ++i){
1544  this->coords[ii][i] = 0;
1545  }
1546  }
1547 
1548  this->numLocalCoords = 0;
1549  srand (myRank + 1);
1550  for (int i = 0; i < distributionCount; ++i){
1551 
1552  lno_t requestedPointCount = lno_t(this->coordinateDistributions[i]->numPoints * this->loadDistributions[myRank]);
1553  if (myRank < this->coordinateDistributions[i]->numPoints % this->worldSize){
1554  requestedPointCount += 1;
1555  }
1556  //cout << "req:" << requestedPointCount << endl;
1557  //this->coordinateDistributions[i]->GetPoints(requestedPointCount,this->points + this->numLocalCoords, this->holes, this->holeCount, this->loadDistributions, myRank);
1558  this->coordinateDistributions[i]->GetPoints(requestedPointCount,this->coords, this->numLocalCoords, this->holes, this->holeCount, this->loadDistributions, myRank);
1559  this->numLocalCoords += requestedPointCount;
1560  }
1561 
1562 
1563 
1564  if (this->distinctCoordSet){
1565  //TODO: Partition and migration.
1566  }
1567 
1568  if(this->outfile != ""){
1569 
1570  std::ofstream myfile;
1571  myfile.open ((this->outfile + toString<int>(myRank)).c_str());
1572  for(lno_t i = 0; i < this->numLocalCoords; ++i){
1573 
1574  myfile << this->coords[0][i];
1575  if(this->coordinate_dimension > 1){
1576  myfile << " " << this->coords[1][i];
1577  }
1578  if(this->coordinate_dimension > 2){
1579  myfile << " " << this->coords[2][i];
1580  }
1581  myfile << std::endl;
1582  }
1583  myfile.close();
1584  }
1585 
1586 
1587 
1588  /*
1589  Zoltan2::XpetraMultiVectorInput < Tpetra::MultiVector<T, lno_t, gno_t, node_t> > xmv (RCP < Tpetra::MultiVector<T, lno_t, gno_t, node_t> > (tmVector));
1590 
1591  RCP< Tpetra::MultiVector<T, lno_t, gno_t, node_t> >tmVector2;
1592  Zoltan2::PartitioningSolution< Tpetra::MultiVector<T, lno_t, gno_t, node_t> > solution;
1593  xmv.applyPartitioningSolution<Tpetra::MultiVector<T, lno_t, gno_t, node_t> >(this->tmVector, &tmVector2, solution);
1594  */
1595  if (this->weight_dimension > 0){
1596  this->wghts = new T *[this->weight_dimension];
1597  for(int i = 0; i < this->weight_dimension; ++i){
1598  this->wghts[i] = new T[this->numLocalCoords];
1599  }
1600  }
1601 
1602  for(int ii = 0; ii < this->weight_dimension; ++ii){
1603  switch(this->coordinate_dimension){
1604  case 1:
1605 #ifdef HAVE_ZOLTAN2_OMP
1606 #pragma omp parallel for
1607 #endif
1608  for (lno_t i = 0; i < this->numLocalCoords; ++i){
1609  this->wghts[ii][i] = this->wd[ii]->get1DWeight(this->coords[0][i]);
1610  }
1611  break;
1612  case 2:
1613 #ifdef HAVE_ZOLTAN2_OMP
1614 #pragma omp parallel for
1615 #endif
1616  for (lno_t i = 0; i < this->numLocalCoords; ++i){
1617  this->wghts[ii][i] = this->wd[ii]->get2DWeight(this->coords[0][i], this->coords[1][i]);
1618  }
1619  break;
1620  case 3:
1621 #ifdef HAVE_ZOLTAN2_OMP
1622 #pragma omp parallel for
1623 #endif
1624  for (lno_t i = 0; i < this->numLocalCoords; ++i){
1625  this->wghts[ii][i] = this->wd[ii]->get3DWeight(this->coords[0][i], this->coords[1][i], this->coords[2][i]);
1626  }
1627  break;
1628  }
1629  }
1630  }
1631 
1633  return this->weight_dimension;
1634  }
1636  return this->coordinate_dimension;
1637  }
1639  return this->numLocalCoords;
1640  }
1642  return this->numGlobalCoords;
1643  }
1644 
1646  return this->coords;
1647  }
1648 
1650  return this->wghts;
1651  }
1652 
1654  for(int ii = 0; ii < this->coordinate_dimension; ++ii){
1655 #ifdef HAVE_ZOLTAN2_OMP
1656 #pragma omp parallel for
1657 #endif
1658  for (lno_t i = 0; i < this->numLocalCoords; ++i){
1659  c[ii][i] = this->coords[ii][i];
1660  }
1661  }
1662  }
1663 
1664  void getLocalWeightsCopy(T **w){
1665  for(int ii = 0; ii < this->weight_dimension; ++ii){
1666 #ifdef HAVE_ZOLTAN2_OMP
1667 #pragma omp parallel for
1668 #endif
1669  for (lno_t i = 0; i < this->numLocalCoords; ++i){
1670  w[ii][i] = this->wghts[ii][i];
1671  }
1672  }
1673  }
1674 };
#define INVALID(STR)
virtual bool isInArea(CoordinatePoint< T > dot)
virtual bool isInArea(CoordinatePoint< T > dot)
virtual ~SphereHole()
virtual weighttype get1DWeight(T x)
paramName
Definition: xml2dox.py:207
SphereHole(CoordinatePoint< T > center_, T edge_)
virtual CoordinatePoint< T > getPoint(gno_t pindex, unsigned int &state)
CoordinateGridDistribution(gno_t alongX, gno_t alongY, gno_t alongZ, int dim, T l_x, T r_x, T l_y, T r_y, T l_z, T r_z, int myRank_, int wSize)
RectangularPrismHole(CoordinatePoint< T > center_, T edge_x_, T edge_y_, T edge_z_)
virtual ~SquareHole()
virtual ~CubeHole()
virtual weighttype get3DWeight(T x, T y, T z)=0
void print(T x, T y, T z)
Defines the PartitioningSolution class.
virtual bool isInArea(CoordinatePoint< T > dot)
void GetPoints(lno_t requestedPointcount, CoordinatePoint< T > *points, Hole< T > **holes, lno_t holeCount, float *sharedRatios_, int myRank)
Defines the XpetraMultiVectorInput adapter.
Expression is a generic following method.
CoordinateDistribution(gno_t np_, int dim, int wSize)
virtual weighttype get3DWeight(T x, T y, T z)
virtual CoordinatePoint< T > getPoint(gno_t pindex, unsigned int &state)
virtual ~CircleHole()
GeometricGenerator(Teuchos::ParameterList &params, const RCP< const Teuchos::Comm< int > > &comm_)
const std::string shapes[]
void GetPoints(lno_t requestedPointcount, T **coords, lno_t tindex, Hole< T > **holes, lno_t holeCount, float *sharedRatios_, int myRank)
SteppedEquation(T a1_, T a2_, T a3_, T b1_, T b2_, T b3_, T c_, T x1_, T y1_, T z1_, T *steps_, T *values_, int stepCount_)
outfile
Definition: xml2dox.py:11
virtual CoordinatePoint< T > getPoint(gno_t pindex, unsigned int &state)
std::string toString(tt obj)
Hole(CoordinatePoint< T > center_, T edge1_, T edge2_, T edge3_)
CoordinateUniformDistribution(gno_t np_, int dim, T l_x, T r_x, T l_y, T r_y, T l_z, T r_z, int wSize)
#define MAX_ITER_ALLOWED
const std::string weight_distribution_string
virtual weighttype get2DWeight(T x, T y)=0
#define INVALID_SHAPE_ARG(SHAPE, REQUIRED)
RectangleHole(CoordinatePoint< T > center_, T edge_x_, T edge_y_)
virtual weighttype get2DWeight(T x, T y)
CircleHole(CoordinatePoint< T > center_, T edge_)
void getLocalCoordinatesCopy(T **c)
virtual bool isInArea(CoordinatePoint< T > dot)
#define INVALIDSHAPE(STR, DIM)
SquareHole(CoordinatePoint< T > center_, T edge_)
CoordinatePoint< T > center
virtual bool isInArea(CoordinatePoint< T > dot)
virtual weighttype get1DWeight(T x)=0
virtual ~Hole()
CubeHole(CoordinatePoint< T > center_, T edge_)
virtual bool isInArea(CoordinatePoint< T > dot)
virtual weighttype getWeight(CoordinatePoint< T > p)
CoordinateNormalDistribution(gno_t np_, int dim, CoordinatePoint< T > center_, T sd_x, T sd_y, T sd_z, int wSize)
#define MAX_WEIGHT_DIM