lagsht_testsuite.cc 28.8 KB
Newer Older
1 2
#include <stdlib.h>
#include <iostream>
3
#include <iomanip>      // std::setprecision
4
#include <fstream>
5
#include <sstream>
6 7 8 9 10 11 12 13
#include <string.h>
#include <limits>       // std::numeric_limits
#include <string>
#include <typeinfo>
using namespace std;

#include "lagsht_exceptions.h"
#include "lagsht_utils.h" //getMemorySize
14
#include "walltimer.h"    //timing
15 16 17


#include "lagSphericTransform.h"
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
18 19
#include "laguerre2bessel.h"

20 21
using namespace LagSHT;

22
#define DEBUG 0
23

24 25 26

//JEC 12/1/16: alpha becomes double instead of int

27 28 29 30 31
//-------- Parameters set in the main and  used in the different test functions
struct PARAM {
  int Lmax;
  int N;
  r_8 R;
32
  int Pmax;
33
  int spin;   //JEC 18/1/16
34 35
  //  int alpha;
  double alpha;
36 37 38
  string geometry;
  int ntheta;
  int nphi;
39 40 41
  string clenshawDir; //JEC 23/11/15
  string jlnpDir;     //       "
  bool recomputeJlnp; //       "
42 43 44 45 46 47 48 49 50 51 52 53
} param ; 

//simple random generator (taken from sharp_testsuite
static double drand (double min, double max, int *state){
  *state = (((*state) * 1103515245) + 12345) & 0x7fffffff;
  return min + (max-min)*(*state)/(0x7fffffff+1.0);
}//rand


//----------------------------------------------------
void TestBasic() {

54
  // to access some general parameters... use param.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81



  cout << " ___________  BASIC TEST _____________ " << endl;
  

  cout << " sizeof(double): " << sizeof(r_8) << " bytes" 
       << " min " << std::numeric_limits<r_8>::min() << "-ln(min): " << -log(std::numeric_limits<r_8>::min()) 
       << " max " << std::numeric_limits<r_8>::max()  
       << " min exp_10 " << std::numeric_limits<r_8>::min_exponent10
       << " max exp_10 " << std::numeric_limits<r_8>::max_exponent10  
       <<  endl;
  


  cout << " sizeof(long double): " << sizeof(r_16) << " bytes" 
       << " min " << std::numeric_limits<r_16>::min() <<  "-ln(min): " << -logl(std::numeric_limits<r_16>::min()) 
       << " max " << std::numeric_limits<r_16>::max()  
       << " min exp_10 " << std::numeric_limits<r_16>::min_exponent10
       << " max exp_10 " << std::numeric_limits<r_16>::max_exponent10  
       <<  endl;
}//TestBasic

//----------------
void LaguerreQuadrature() {

  int N = param.N;
82 83
  //  int alpha = param.alpha;
  double alpha = param.alpha;
84 85 86 87 88

  cout << " ___________  LaguerreQuadrature  TEST _____________ " << endl;

  LaguerreFuncQuad lagFunc(N,alpha);
  cout << "N = " << lagFunc.GetOrder() << " alpha = " << lagFunc.GetAlpha() << endl;
89 90
  vector<r_8> nodes;
  vector<r_8> weights;
91 92
  lagFunc.QuadWeightNodes(nodes,weights);

93 94 95 96 97 98 99 100
  {
    char buff1[80], buff2[80];
    ofstream ofs1, ofs2;
    sprintf(buff1,"lagNodes-%d-Func.txt",N);
    sprintf(buff2,"lagWeights-%d-Func.txt",N);
    ofs1.open(buff1,ofstream::out);
    ofs2.open(buff2,ofstream::out);
    for (int i=0;i<N;i++){
101 102
      ofs1 << setprecision(20) << nodes[i] << endl;
      ofs2 << setprecision(20) << weights[i] << endl;
103
    }
104 105
  }

106

107 108 109 110 111 112
}//LaguerreQuadrature

//--------------------------------------
void MultiLaguerreTransformSdtAlone() {
  int N = param.N;
  r_8 R  = param.R;
113
  r_8 alpha = param.alpha; //JEC 12/1/16 to be tested with alpha =/= 2 (eg. alpha = 0)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

  cout << " ___________   MultiLaguerreTransformSdtAlone  TEST _____________ " << endl;

  //    Timer tm("Test 3 (r_16)");
      
  int mapSize=2; //this is the stride
      
  int Ntot = N*mapSize;

#if DEBUG >= 1
  cout << "Start random generation...." << endl;
#endif
  vector< complex<r_8> > fnOrig(Ntot);
  int state=1234567 + 8912 ; //random seed
  for(int i=0;i<Ntot;i++){
    r_8 rv = drand(-1,1,&state);
    r_8 iv = drand(-1,1,&state);
    fnOrig[i] = complex<r_8>(rv,iv);
  }
#if DEBUG >=2 
134
  std::copy(fnOrig.begin(), fnOrig.end(), std::ostream_iterator< complex<r_8> >(std::cout, " "));
135 136
#endif
    
137
  LaguerreTransform trans(N,R,alpha);  //JEC 12/1/16 add alpha
138 139 140 141 142 143 144 145

  vector< complex<r_8> > fi(Ntot);
#if DEBUG >= 1
  cout << "Start MultiSynthesis...." << endl;
#endif
  trans.MultiSynthesis(fnOrig,fi,mapSize);
    
#if DEBUG >=2 
146
  std::copy(fi.begin(), fi.end(), std::ostream_iterator< complex<r_8> >(std::cout, " "));
147 148 149 150 151 152 153
#endif

  vector< complex<r_8> > fn(Ntot);
  trans.MultiAnalysis(fi,fn,mapSize);

  r_8 err_abs(0.);
  r_8 err_rel(0.);
154
  int imax=-1;
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
  for(int i=0;i<Ntot;i++){
    if(i<10)cout << "("<<i<<"): " << fnOrig[i] << " <-> " << fi[i] << " <-> "  
		 << fn[i] << endl;
    complex<r_8> cdiff = (fnOrig[i] -  fn[i]) * conj(fnOrig[i] -  fn[i]);
    r_16 diff = sqrt(cdiff.real());
    if(diff>err_abs){ 
      err_abs = diff;
      imax = i;
    }
    complex<r_8> foriCAbs = fnOrig[i]*conj(fnOrig[i]);
    r_8 foriAbs = sqrt(foriCAbs.real());
    r_8 relatdiff = diff/foriAbs;
	
    if((relatdiff)>err_rel) err_rel = relatdiff;
  }
  cout << " >>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 
  cout << "r_16 Err. Max. " << err_abs << " [" << imax << "], Err. Rel. " << err_rel << endl;
  cout << " >>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 
  

}//MultiLaguerreTransformSdtAlone

//---------------------------------------
void MultiSphericalLaguerreTransform() {

180
  r_8 alpha = param.alpha;  //JEC 13/1/16
181
  int Nmax = param.N;
182
  int Lmax = param.Lmax;
183
  r_8 Rmax = param.R;
184 185 186
  string geometry = param.geometry;
  int ntheta = param.ntheta;
  int nphi = param.nphi;
187 188 189 190


  cout << " ___________  MultiSphericalLaguerreTransform   TEST _____________ " << endl;

191 192
  tstack_push("data input");

193 194
  int state = 1234567 + 8912 ; //random seed

195 196 197 198 199 200
  LaguerreSphericalTransform sphlagtrans(geometry,
					 Lmax,
					 -1,
					 Nmax,
					 Rmax,
					 ntheta,
201 202
					 nphi,
					 alpha
203
					 );
204

205 206 207

  int Nalm = sphlagtrans.GetSphericalGeometry()->Nalm();
  int Nshell = sphlagtrans.GetOrder();
208
  int Ntot = Nshell * Nalm; //total number of Coefficients of the Spherical Laguerre transform
209
  int Npix =  sphlagtrans.GetSphericalGeometry()->Npix();
210
  int NpTot=  Nshell * Npix; //totoal number of 3D-pixels
211

212 213 214
  cout << "Verif: Ntheta, Nphi, Npix, Nptot, Nalm, Nshell, Ntot: "
       << sphlagtrans.GetSphericalGeometry()->NTheta() << " "
       << sphlagtrans.GetSphericalGeometry()->NPhi() << " "
215 216 217 218 219
       <<  Npix << " "
       <<  NpTot << " "
       <<  Nalm << " "
       <<  Nshell << " "
       <<  Ntot << endl;
220

221 222 223 224 225 226 227
  unsigned int maxmemsize =  getMemorySize()/1e6;
  unsigned int estimateMem = 8*((uint_8)(NpTot+2*Ntot))/1e6;
  cout << "Estimate usage memory: " << estimateMem << " MBytes" << endl;
  if ( estimateMem > (0.9*maxmemsize) ) {
    cout << ">>>>> Warning: estimate Mem " << estimateMem <<" MB > 90\% "<< maxmemsize << " MB" << endl;
  }//test memory

228 229
  vector< complex<r_8> > flmnOrig(Ntot);
  int id=-1;
230
  for(int n=0;n<Nmax;n++){ //on each shell
231 232 233 234 235 236 237 238 239 240 241
    for(int m=0;m<Lmax;m++){ //Warning the filling of alm is adapted for libsharp memory
      for(int l=m;l<Lmax;l++){
	id++;
	r_8 rv = drand(-1,1,&state);
	r_8 iv = (m==0) ? 0.0 : drand(-1,1,&state);
	flmnOrig[id] = complex<r_8>(rv,iv);	    
      }//end l-loop
    }//end m-loop
  }//end loop on shell

#if DEBUG >=2 
242
  std::copy(flmnOrig.begin(), flmnOrig.end(), std::ostream_iterator< complex<r_8> >(std::cout, " "));
243 244
#endif
      
245 246 247 248
  
  tstack_pop("data input");
  tstack_push("processing");
  tstack_push("processing part Synthesis");
249 250 251 252 253 254

#if DEBUG >= 1
  cout << "Main:Synthesis r_8 function..." << endl;
#endif
  vector<r_8> fijk(NpTot);
  sphlagtrans.Synthesis(flmnOrig,fijk);
255
#if DEBUG >= 2
256
  for (int i=0; i<NpTot; i++){
257
    cout << "fijk("<<i<<"): " << fijk[i] << endl;
258
  }
259
#endif
260 261 262 263
  
  tstack_pop("processing part Synthesis");
  tstack_push("processing part Analysis");
  
264 265 266 267 268 269
#if DEBUG >= 1
  cout << "Main:Analysis r_8 function..." << endl;
#endif
  vector< complex<r_8> > flmn(Ntot);
  sphlagtrans.Analysis(fijk,flmn);
    
270 271 272
  tstack_pop("processing part Analysis");
  tstack_pop("processing");  

273 274 275 276 277 278


  cout << "Error analysis..." << endl;

  r_8 err_abs(0.);
  r_8 err_rel(0.);
279
  int imax = -1;
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  for(int i=0;i<Ntot;i++){
    if(i>(Ntot-9)) cout << "(" << i << ") : flnm Orig " <<  flmnOrig[i] << " <-> flmn Rec " <<  flmn[i] << endl;

    complex<r_8> cdiff = (flmnOrig[i] -  flmn[i]) * conj(flmnOrig[i] -  flmn[i]);
    r_16 diff = sqrt(cdiff.real());
    if(diff>err_abs){ 
      err_abs = diff;
      imax = i;
    }
    complex<r_8> foriCAbs = flmnOrig[i]*conj(flmnOrig[i]);
    r_8 foriAbs = sqrt(foriCAbs.real());
    r_8 relatdiff = diff/foriAbs;
	
    if((relatdiff)>err_rel) err_rel = relatdiff;

  }
  cout << " >>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 
297
  cout << "Err. Max. " << err_abs << " [" << imax << "], Err. Rel. " << err_rel << endl;
298 299 300 301 302
  cout << " >>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 


}//MultiSphericalLaguerreTransform

303 304 305
//---------------------------------------
void SpinMultiSphericalLaguerreTransform() {

306
  r_8 alpha = param.alpha;  
307 308 309 310 311 312
  int Nmax = param.N;
  int Lmax = param.Lmax;
  r_8 Rmax = param.R;
  string geometry = param.geometry;
  int ntheta = param.ntheta;
  int nphi = param.nphi;
313
  int spin = param.spin; //JEC 18/1/16
314

315 316 317
  if(spin == 0)  
    throw LagSHTError("WARNING: spin =0 : use test MultiSphericalLaguerreTransform"); 

318
  cout << " ___________  SpinMultiSphericalLaguerreTransform   TEST _____________ " << endl;
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

  tstack_push("data input");

  int state = 1234567 + 8912 ; //random seed

  LaguerreSphericalTransform sphlagtrans(geometry,
					 Lmax,
					 -1,
					 Nmax,
					 Rmax,
					 ntheta,
					 nphi,
					 alpha
					 );


  int Nalm = sphlagtrans.GetSphericalGeometry()->Nalm();
  int Nshell = sphlagtrans.GetOrder();
  int Ntot = Nshell * Nalm; //total number of Coefficients of the Spherical Laguerre transform
  int Npix =  sphlagtrans.GetSphericalGeometry()->Npix();
  int NpTot=  Nshell * Npix; //totoal number of 3D-pixels

341 342 343 344

  cout << "Verif: Ntheta, Nphi, Npix, Nptot, Nalm, Nshell, Ntot: "
       << sphlagtrans.GetSphericalGeometry()->NTheta() << " "
       << sphlagtrans.GetSphericalGeometry()->NPhi() << " "
345 346 347 348 349 350
       <<  Npix << " "
       <<  NpTot << " "
       <<  Nalm << " "
       <<  Nshell << " "
       <<  Ntot << endl;

351 352 353 354 355 356 357 358 359

  unsigned int maxmemsize =  getMemorySize()/1e6;
  unsigned int estimateMem = 2*8*((uint_8)(NpTot+2*Ntot))/1e6;
  cout << "Estimate usage memory: " << estimateMem << " MBytes" << endl;
  if ( estimateMem > (0.9*maxmemsize) ) {
    cout << ">>>>> Warning: estimate Mem " << estimateMem <<" MB > 90\% "<< maxmemsize << " MB" << endl;
  }//test memory


360 361
  vector< complex<r_8> > ElmnOrig(Ntot);
  vector< complex<r_8> > BlmnOrig(Ntot);
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
362 363
  int id
=-1;
364

365 366 367 368
  for(int n=0;n<Nmax;n++){ //on each shell
    for(int m=0;m<Lmax;m++){ //Warning the filling of alm is adapted for libsharp memory
      for(int l=m;l<Lmax;l++){
	id++;
369 370
	r_8 rv = (l<abs(spin)) ? 0.0 : drand(-1,1,&state);
	r_8 iv = (m==0 || l<abs(spin)) ? 0.0 : drand(-1,1,&state);
371
	ElmnOrig[id] = complex<r_8>(rv,iv);	    
372 373
	rv = (l<abs(spin)) ? 0.0 : drand(-1,1,&state);
	iv = (m==0 || l<abs(spin)) ? 0.0 : drand(-1,1,&state);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	BlmnOrig[id] = complex<r_8>(rv,iv);	    
      }//end l-loop
    }//end m-loop
  }//end loop on shell

#if DEBUG >=2 
  std::copy(ElmnOrig.begin(), ElmnOrig.end(), std::ostream_iterator< complex<r_8> >(std::cout, " "));
  std::copy(BlmnOrig.begin(), BlmnOrig.end(), std::ostream_iterator< complex<r_8> >(std::cout, " "));
#endif
      
  
  tstack_pop("data input");
  tstack_push("processing");
  tstack_push("processing part Synthesis");

#if DEBUG >= 1
  cout << "Main:Synthesis complex function..." << endl;
#endif
  vector<r_8> fijk_re; //NpTot
  vector<r_8> fijk_im;
394 395
//   vector< complex<r_8> > Elmk; //calcul intermediaire
//   vector< complex<r_8> > Blmk;
396

397 398
//  sphlagtrans.Synthesis(ElmnOrig, BlmnOrig, spin, fijk_re, fijk_im, Elmk, Blmk);
  sphlagtrans.Synthesis(ElmnOrig, BlmnOrig, spin, fijk_re, fijk_im);
399
#if DEBUG >= 2
400 401 402
  for (int i=0; i<NpTot; i++){
    cout << "fijk("<<i<<"): " << fijk_re[i] << ", " << fijk_im[i] << endl;
  }
403
#endif
404 405 406 407 408 409 410
  
  tstack_pop("processing part Synthesis");
  tstack_push("processing part Analysis");
  
#if DEBUG >= 1
  cout << "Main:Analysis complex function..." << endl;
#endif
411 412
  vector< complex<r_8> > Elmn(Ntot);
  vector< complex<r_8> > Blmn(Ntot);
413

414 415
//   vector< complex<r_8> > Elmk_ana;
//   vector< complex<r_8> > Blmk_ana;
416

417 418
//  sphlagtrans.Analysis(fijk_re, fijk_im, spin, Elmn, Blmn, Elmk_ana, Blmk_ana);
  sphlagtrans.Analysis(fijk_re, fijk_im, spin, Elmn, Blmn);
419 420 421 422 423 424 425 426 427 428 429
    
  tstack_pop("processing part Analysis");
  tstack_pop("processing");  


  cout << "Error analysis..." << endl;

  r_8 err_abs(0.);
  r_8 err_rel(0.);
  int imax = -1;
  for(int i=0;i<Ntot;i++){
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
430
    if(i>(Ntot-9)) {
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
      cout << "(" << i << ") : Elnm Orig " <<  ElmnOrig[i] << " <-> Elmn Rec " <<  Elmn[i] << endl;
      cout << "............  Blnm Orig " <<  BlmnOrig[i] << " <-> Blmn Rec " <<  Blmn[i] << endl;
    }

    complex<r_8> cdiff = ((ElmnOrig[i] -  Elmn[i]) * conj(ElmnOrig[i] -  Elmn[i])
			  + (BlmnOrig[i] -  Blmn[i]) * conj(BlmnOrig[i] -  Blmn[i]))/2. ;
    r_16 diff = sqrt(cdiff.real());
    if(diff>err_abs){ 
      err_abs = diff;
      imax = i;
    }
    complex<r_8> foriCAbs = (ElmnOrig[i]*conj(ElmnOrig[i]) + BlmnOrig[i]*conj(BlmnOrig[i]))/2. ;
    r_8 foriAbs = sqrt(foriCAbs.real());
    r_8 relatdiff = diff/foriAbs;
	
    if((relatdiff)>err_rel) err_rel = relatdiff;

  }
  cout << " >>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 
  cout << "Err. Max. " << err_abs << " [" << imax << "], Err. Rel. " << err_rel << endl;
  cout << " >>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 


}//SpinMultiSphericalLaguerreTransform

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
//-------------------------------------------------------------
void TestJlnpComputation() {

  int Nmax = param.N;
  int Lmax = param.Lmax;
  int Pmax = param.Pmax;
  r_8 Rmax = param.R;
  string geometry = param.geometry;
  int ntheta = param.ntheta;
  int nphi = param.nphi;
  string clenshawDir = param.clenshawDir;   //JEC 23/11/15
  string jlnpDir = param.jlnpDir;           //       "
  bool recomputeJlnp = param.recomputeJlnp; //       "

  cout << " ______________ TestJlnpComputation  START ___________ " << endl;
  LaguerreSphericalTransform sphlagtrans(geometry,
					 Lmax,
					 -1,
					 Nmax,
					 Rmax,
					 ntheta,
					 nphi
					 );

  
  BaseGeometry* sphere = sphlagtrans.GetSphericalGeometry();
  LaguerreTransform* lagTrans = sphlagtrans.GetLagTransform();


  tstack_push("Ctor lag2bess");
  Laguerre2Bessel lag2bess(sphere,lagTrans,Nmax,Pmax,Rmax,
			   clenshawDir,jlnpDir,recomputeJlnp);

  tstack_pop("Ctor lag2bess");

  cout << " ______________ TestJlnpComputation  End  ___________ " << endl;

}


Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
496 497 498 499 500 501 502
//-------------------------------------------------------------
void TestLaguerre2Bessel() {

  //Todo introduce Pmax !

  int Nmax = param.N;
  int Lmax = param.Lmax;
503
  int Pmax = param.Pmax;
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
504 505 506 507
  r_8 Rmax = param.R;
  string geometry = param.geometry;
  int ntheta = param.ntheta;
  int nphi = param.nphi;
508 509 510 511
  string clenshawDir = param.clenshawDir;   //JEC 23/11/15
  string jlnpDir = param.jlnpDir;           //       "
  bool recomputeJlnp = param.recomputeJlnp; //       "

Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
512 513 514

  cout << " ______________ TestLaguerre2Bessel TEST ___________ " << endl;
  
515 516
   cout << "tstack 1 Start" <<endl;
   tstack_push("data input");
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

  int state = 1234567 + 8912 ; //random seed

  LaguerreSphericalTransform sphlagtrans(geometry,
					 Lmax,
					 -1,
					 Nmax,
					 Rmax,
					 ntheta,
					 nphi
					 );

  
  BaseGeometry* sphere = sphlagtrans.GetSphericalGeometry();
  LaguerreTransform* lagTrans = sphlagtrans.GetLagTransform();

  int Nalm = sphere->Nalm();
  int Nshell = sphlagtrans.GetOrder();
  int Ntot = Nshell * Nalm; //total number of Coefficients of the Spherical Laguerre transform
  int Npix =  sphere->Npix();
  int NpTot=  Nshell * Npix; //totoal number of 3D-pixels

  cout << "Verif: Npix, Nptot, Nalm, Nshell, Ntot: "
       <<  Npix << " "
       <<  NpTot << " "
       <<  Nalm << " "
       <<  Nshell << " "
       <<  Ntot << endl;

  vector< complex<r_8> > flmnOrig(Ntot);
  int id=-1;
  for(int n=0;n<Nmax;n++){ //on each shell
    for(int m=0;m<Lmax;m++){ //Warning the filling of alm is adapted for libsharp memory
      for(int l=m;l<Lmax;l++){
	id++;
552 553 554 555 556 557
	
	//verif
	int idLag =  n*Nalm + l+m*Lmax-m*(m+1)/2 ;
	if(id != idLag)
	  throw LagSHTError("ERROR: FLag index error"); 

Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
558 559 560 561 562 563 564 565
	r_8 rv = drand(-1,1,&state);
	r_8 iv = (m==0) ? 0.0 : drand(-1,1,&state);
	flmnOrig[id] = complex<r_8>(rv,iv);	    
      }//end l-loop
    }//end m-loop
  }//end loop on shell

#if DEBUG >=2 
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
  cout << "F-Lag coeff orig ......... START " << endl;
  //  std::copy(flmnOrig.begin(), flmnOrig.end(), std::ostream_iterator< complex<r_8> >(std::cout, "\n"));
  
  for(int l=0; l<Lmax; l++){
    for (int m=0; m<=l; m++) {
      int almoff7 = l+m*Lmax-m*(m+1)/2;
      for (int n=0; n<Nmax; n++){
	int idLag =  n*Nalm + almoff7;
	cout << l << " " 
	     << m << " "
	     << n << " "
	     << setprecision(12) << flmnOrig[idLag] << endl;
      }
    }
  }
  
  cout << "F-Lag coeff orig ......... END " << endl;
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
583 584 585 586 587 588 589 590 591 592
#endif
        
  cout << "tstack 1 End" <<endl;
  tstack_pop("data input");


  cout << "tstack 2 Start" <<endl;
  tstack_push("processing");
  cout << "tstack 2a Start" <<endl;
  tstack_push("Init Laguerre 2 Bessel");
593 594
  Laguerre2Bessel lag2bess(sphere,lagTrans,Nmax,Pmax,Rmax,
			   clenshawDir,jlnpDir,recomputeJlnp);
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
595 596
  cout << "tstack 2a End" <<endl;
  tstack_pop("Init Laguerre 2 Bessel");
597

Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
598 599 600 601
  cout << "tstack 2b Start" <<endl;
  tstack_push("Compute Fourier-Bessel coeffs.");

  vector< complex<r_8> > FBlmp(Nalm*Pmax);
602
  lag2bess.Lag2BessCoeff(flmnOrig,FBlmp);
603 604 605 606 607
#if DEBUG >=2 
  cout << "F-Bessel coeff ......... START " << endl;
  std::copy(FBlmp.begin(), FBlmp.end(), std::ostream_iterator< complex<r_8> >(std::cout, "\n"));
  cout << "F-Bessel coeff ......... END " << endl;
#endif
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
608 609 610 611 612 613 614
  cout << "tstack 2b End" <<endl;
  tstack_pop("Compute Fourier-Bessel coeffs.");
  

  cout << "tstack 2c Start" <<endl;
  tstack_push("Compute Alm(rk) from FB coeffs.");
  vector< complex<r_8> > FBalmk;
615
  vector<r_8> fFBijk;
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
616 617 618 619 620 621
  lag2bess.Synthesis(FBlmp,FBalmk,fFBijk);
  cout << "tstack 2c End" <<endl;
  tstack_pop("Compute Alm(rk) from FB coeffs.");
  

  cout << "tstack 2d Start" <<endl;
622
  tstack_push("Compute Alm(rk) from FL coeffs.");
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
623 624 625
  vector< complex<r_8> > FLalmk;
  vector<r_8> fFLijk;
  sphlagtrans.Synthesis(flmnOrig,fFLijk,FLalmk);
626 627 628 629 630 631
#if DEBUG >=2 
  cout << "F-Lag fijk coeff ......... START " << endl;
  std::copy(fFLijk.begin(), fFLijk.end(), std::ostream_iterator< complex<r_8> >(std::cout, "\n"));
  cout << "F-Lag fijk coeff ......... END " << endl;
#endif

Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
632
  cout << "tstack 2d End" <<endl;
633
  tstack_pop("Compute Alm(rk) from FL coeffs.");
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
634
  
635
  cout << "tstack 2 End" <<endl;
636

637 638 639
  tstack_pop("processing");


640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
  //verif F-Laguerre part
  tstack_push("F-L Analysis verif");
  vector< complex<r_8> > flmn(Ntot);
  sphlagtrans.Analysis(fFLijk,flmn);
  {
    r_8 err_abs(0.);
    r_8 err_rel(0.);
    int imax = -1;
    for(int i=0;i<Ntot;i++){
      
      complex<r_8> cdiff = (flmnOrig[i] -  flmn[i]) * conj(flmnOrig[i] -  flmn[i]);
      r_16 diff = sqrt(cdiff.real());
      if(diff>err_abs){ 
	err_abs = diff;
	imax = i;
      }
      complex<r_8> foriCAbs = flmnOrig[i]*conj(flmnOrig[i]);
      r_8 foriAbs = sqrt(foriCAbs.real());
      r_8 relatdiff = diff/foriAbs;
	
      if((relatdiff)>err_rel) err_rel = relatdiff;
    }
    cout << " >>>>>>>>>>>>>>>>>>>>> Fourrier-Laguerre part <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 
    cout << "Err. Max. " << err_abs << " [" << imax << "], Err. Rel. " << err_rel << endl;
    cout << " >>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 
    
  }
  tstack_pop("F-L Analysis verif");


670 671
  
  
672
  {//Check 1
673
    cout << "Dump FL or FB reconstructed Alm(r_k)" <<endl;
674

675
#if DEBUG >=2 
676 677
    std::ofstream ofs;
    ofs.open ("Almi.txt", std::ofstream::out);
678
#endif
679 680
    
    for(int n=0;n<Nmax;n++){
681 682 683
      r_8 err_abs(0.);
      r_8 err_rel(0.);

684 685 686
      for(int l=0;l<Lmax;l++){
	for (int m=0;m<=l;m++) {
	  int id= n*Nalm + l+m*Lmax-m*(m+1)/2; // LagSHT numbering 
687 688

#if DEBUG >=2 
689 690 691 692
	  ofs <<l<<" "
	      <<m<<" "
	      <<n<< " " << setprecision(12) << FLalmk[id].real() << " " <<   FLalmk[id].imag()
	      << "  " << FBalmk[id].real() << " " << FBalmk[id].imag()  << endl;
693
#endif	  
694 695 696 697 698 699 700 701 702
	  complex<r_8> cdiff = (FLalmk[id] -  FBalmk[id])*conj(FLalmk[id] -  FBalmk[id]);
	  r_8 diff = sqrt(cdiff.real());
	  if(diff>err_abs){
	    err_abs = diff;
	  }
	  complex<r_8> cref= FLalmk[id]*conj(FLalmk[id]);
	  r_8 ref = sqrt(cref.real());
	  r_8 relatdiff = diff/ref;
	  if(relatdiff>err_rel) err_rel = relatdiff;
703 704
	}
      }
705 706 707 708 709
      
      cout << " >>>>>>>>>>>>>>>>>>>>> Shell["<<n<<"] <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 
      cout << "Err. Max. " << err_abs << ", Err. Rel. " << err_rel << endl;
      cout << " >>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 

710
    }
711
#if DEBUG >=2 
712
    ofs.close();
713
#endif
714
  }//check 1
715

716
  {//check 2
717
#if DEBUG >=2 
718 719
    std::ofstream ofs;
    ofs.open ("fijk.txt", std::ofstream::out);
720
#endif
721 722
    cout << "Dump FL or FB reconstructed fijk on each shell k" <<endl;

723
    for (int ish=0;ish<Nshell; ish++){
724 725
      r_8 err_abs(0.);
      r_8 err_rel(0.);
726 727
      for (int ip=0; ip<Npix; ip++) {
	int id = ish*Npix+ip;
728
	//	cout << "Shell("<<ish<<") Pix["<<ip<<"] FL fijk = " << fFLijk[id] << " FB fijk = " << fFBijk[id] << endl;
729 730

#if DEBUG >=2 
731
	ofs << ish << " " << ip << " " << setprecision(12) <<  fFLijk[id] << " " << fFBijk[id] << endl;
732
#endif
733 734 735 736 737 738 739 740 741 742 743 744 745 746
	r_8 diff = fabs(fFLijk[id]-fFBijk[id]);
	if(diff>err_abs){
	  err_abs = diff;
	}
	r_8 relatdiff = diff/ fFLijk[id];
	if(relatdiff>err_rel) err_rel = relatdiff;
      }//loop on px
      
            
      cout << " >>>>>>>>>>>>>>>>>>>>> Shell["<<ish<<"] <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 
      cout << "Err. Max. " << err_abs << ", Err. Rel. " << err_rel << endl;
      cout << " >>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 

    }//loop on shell
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
747 748


749
#if DEBUG >=2 
750
    ofs.close();
751
#endif
752
  }//check 2
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
753 754 755 756

  

}//TestLaguerre2Bessel
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
//-------------------------------------------------------------

void TestPixelization() {

  int Nmax = param.N;
  int Lmax = param.Lmax;
  r_8 Rmax = param.R;
  string geometry = param.geometry;
  int ntheta = param.ntheta;
  int nphi = param.nphi;


  
  LaguerreSphericalTransform lagsht(geometry,
				    Lmax,
				    -1,
				    Nmax,
				    Rmax,
				    ntheta,
				    nphi
				    );
  
  ntheta = lagsht.GetSphericalGeometry()->NTheta(); //identique a Nrings
  nphi   = lagsht.GetSphericalGeometry()->NPhi();

  int state = 1234567 + 8912 ; //random seed
  
784
  stringstream ss; ss << "pixels-" << geometry << "-L" << Lmax << ".txt";
785
  std::ofstream ofs (ss.str().c_str(), std::ofstream::out);
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810

  r_8 dTheta = M_PI/ntheta/2;
  r_8 dPhi   = 2*M_PI/nphi/2;
  
  cout << "half theta Width : " << dTheta << ", half phi Width : " << dPhi << endl; 

  
  int nloop = 100000;

  for (int i=0;i<nloop; i++) {
    r_8 theta_test = drand(0.,M_PI,&state);
    r_8 phi_test = drand(0.,2*M_PI,&state);
    int idx = lagsht.GetSphericalGeometry()->PixIndexSph(theta_test, phi_test);

    r_8 theta_out;
    r_8 phi_out;
    lagsht.GetSphericalGeometry()->PixThetaPhi(idx,theta_out, phi_out);
    
    r_8 dphi = phi_test-phi_out;
    if(dphi>M_PI)dphi=2*M_PI-dphi;

    ofs << theta_test << " " << phi_test << " " << theta_out << " " << phi_out << " " << dphi<< endl;
  }
  
}//TestPixelization
811 812 813 814 815 816 817

//----------------------------------------------
//               Main 
//----------------------------------------------

int main(int narg, char *arg[]) {

818
   unsigned int maxmemsize =  getMemorySize()/1e6;
819 820
   cout << "Max Memory size: " <<  maxmemsize << " MBytes" <<  endl;

821
  int N = 128; 
822
  r_8 R = 1.;
823
  int Lmax = 128;
824
  int Pmax = 0;
825
  int spin = 0;
826

827 828 829 830 831
  int test=1;

  string geometry = "Gauss";
  int ntheta = -1;
  int nphi = -1;
832

833 834 835 836
  //JEC 23/11/15
  char* pLAGSHTROOTDIR; pLAGSHTROOTDIR = getenv("LAGSHTROOTDIR");
  string clenshawDir = string(pLAGSHTROOTDIR)+"/data";
  string jlnpDir = string(pLAGSHTROOTDIR)+"/data";
837
  bool recomputeJlnp = true;
838 839


840 841 842
  //JEC 12/1/16 alpha as input
  double alpha = 2.0; //default

843 844 845
  int ka=1;
  while (ka<narg) {
    if (strcmp(arg[ka],"-h")==0) {
846
      cout << "usage: ./Objs/lagsht_testsuite -t <test number> [1]\n"
847 848
	   << " [-a <alpha> [2 by default] ] "
	   << " [-n <Nmax> [128]] [-l <Lmax> [128]]\n" 
849 850 851
	   << " [-g <geometry> Gauss|Fejer1|Healpix [Gauss]] \n"
	   << " [-ntheta <number of theta rings> [determined by the geometry]\n     in case of Healpix gives the nside parameter]\n"
	   << " [-nphi <number of pixel per rings> [determined by the geometry]]\n"
852
	   << " [-p <Pmax for Fourier-Bessel> [Nmax] ]\n"
853 854
	   << " [-r <Rmax> [1.]]\n"
	   << " [-spin <spin value> [0]"
855
	   << endl; 
856 857
      return 0;
    }
858 859 860 861
    else if (strcmp(arg[ka],"-a")==0) {
      alpha = atof(arg[ka+1]);
      ka+=2;      
    }    
862 863 864 865
    else if (strcmp(arg[ka],"-spin")==0) {
      spin = atoi(arg[ka+1]);
      ka+=2;      
    }    
866 867 868 869 870 871 872 873 874 875 876 877
    else if (strcmp(arg[ka],"-n")==0) {
      N=atoi(arg[ka+1]);
      ka+=2;
    }
    else if (strcmp(arg[ka],"-l")==0) {
      Lmax=atoi(arg[ka+1]);
      ka+=2;
    }
    else if (strcmp(arg[ka],"-t")==0) {
      test=atoi(arg[ka+1]);
      ka+=2;
    }
878 879 880 881 882 883 884 885 886 887 888 889
    else if (strcmp(arg[ka],"-g")==0) {
      geometry=arg[ka+1];
      ka+=2;      
    }
    else if (strcmp(arg[ka],"-ntheta")==0) {
      ntheta = atoi(arg[ka+1]);
      ka+=2;            
    }
    else if (strcmp(arg[ka],"-nphi")==0) {
      nphi = atoi(arg[ka+1]);
      ka+=2;      
    }
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
890 891 892 893
    else if (strcmp(arg[ka],"-r")==0) {
      R = atof(arg[ka+1]);
      ka+=2;      
    }    
894 895 896 897
    else if (strcmp(arg[ka],"-p")==0) {
      Pmax = atof(arg[ka+1]);
      ka+=2;      
    }    
898 899 900 901 902 903 904 905 906 907 908 909
    else if (strcmp(arg[ka],"-clenPath")==0) {
      clenshawDir = arg[ka+1];
      ka+=2;
    }
    else if (strcmp(arg[ka],"-jlnpPath")==0) {
      jlnpDir = arg[ka+1];
      ka+=2;
    }
    else if(strcmp(arg[ka],"-jlnp")==0) {
      recomputeJlnp = true;
      ka+=1;
    }
910 911 912 913 914
    else ka++;
  }//eo while

  param.Lmax = Lmax;
  param.N    = N;
915
  param.Pmax = Pmax;
916
  param.R    = R;
917
  param.spin  = spin;
918
  param.alpha = alpha;
919 920 921
  param.geometry = geometry;
  param.ntheta = ntheta;
  param.nphi = nphi;
922

923 924 925 926 927
  param.clenshawDir = clenshawDir;
  param.jlnpDir     = jlnpDir;
  param.recomputeJlnp = recomputeJlnp;


928 929
  cout << "Configuration parameters are set to: "
       << " Test number : " << test << "\n"
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
930
       << " Lmax, Nmax, Pmax, spin, Rmax , alpha: " 
931 932 933
       << param.Lmax << ", "
       << param.N << ", "
       << param.Pmax << ", "
934
       << param.spin << ", "
935 936 937
       << param.R << ", "
       << param.alpha << "\n"
       << "Geometry: " << param.geometry << " Ntheta, Nphi: " 
938
       << param.ntheta << ", " << param.nphi<<"\n";
939

940 941 942 943 944 945 946 947 948 949 950 951 952 953
  int rc=0;
  try {
    
    switch(test) {
    case 0:
      TestBasic();
      break;
    case 1:
      LaguerreQuadrature();
      break;
    case 2:
      MultiLaguerreTransformSdtAlone();
      break;
    case 3:
954 955 956
      TestPixelization();
      break;
    case 4:
957 958 959 960
      {
	if(N>5500) throw LagSHTError("WARNING: N>5500 the algorithm may fail due to weight estimates"); 
	
      
961 962 963 964 965 966 967
// 	unsigned int estimateMem = 8*N*(uint_8)((2*Lmax+1)*(2*Lmax+1))/1e6; //Npix * 8Bytes and then in MBytes
// 	if ( estimateMem > (0.9*maxmemsize) ) {
// 	  char buff[80];
// 	  cout << "estimate Mem usage " << estimateMem << " MB" << endl;
// 	  sprintf(buff,"Main FATAL: estimate Mem %u MB > (90%%) Max Mem %u MB ",estimateMem,maxmemsize);
// 	  throw  LagSHTError(buff);
// 	}//test memory
968 969 970 971 972 973
	
	tstack_push("MultiSphericalLaguerreTransform");
	MultiSphericalLaguerreTransform();
	tstack_pop("MultiSphericalLaguerreTransform");
	tstack_report("MultiSphericalLaguerreTransform");  
      }
974
      break;
975

Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
976 977
    case 5:
      {
978 979 980 981 982 983 984
	if(recomputeJlnp){
	  cout << "rebuild Jlnp: \n Clenshaw Path<"<<param.clenshawDir<<">\n and jlnp Path<"
	       << param.jlnpDir << ">"
	       << endl;
	} else {
	  cout << "Jlnp path <" << param.jlnpDir << ">" << endl;
	}
985
	if(Pmax<N){ param.Pmax = 16*N; cout << "(Warning): Pmax<Nmax => modify the value to "<<  param.Pmax << endl;}
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
986
	tstack_push("TestLaguerre2Bessel");
987 988
	//	TestLaguerre2Bessel();
	TestJlnpComputation();
989 990
 	tstack_pop("TestLaguerre2Bessel");
 	tstack_report("TestLaguerre2Bessel");
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
991 992 993
      }
      break;

994 995 996

    case 6:	
      {
997 998 999 1000 1001 1002 1003 1004 1005 1006

	unsigned int estimateMem = 8*2*N*(uint_8)((2*Lmax+1)*(2*Lmax+1))/1e6; //Npix * 8Bytes and then in MBytes
	if ( estimateMem > (0.9*maxmemsize) ) {
	  char buff[80];
	  cout << "estimate Mem usage " << estimateMem << " MB" << endl;
	  sprintf(buff,"Main FATAL: estimate Mem %u MB > (90%%) Max Mem %u MB ",estimateMem,maxmemsize);
	  throw  LagSHTError(buff);
	}//test memory


1007 1008 1009 1010 1011 1012 1013
	tstack_push("SpinMultiSphericalLaguerreTransform");
	SpinMultiSphericalLaguerreTransform();
	tstack_pop("SpinMultiSphericalLaguerreTransform");
	tstack_report("SpinMultiSphericalLaguerreTransform");  
      }
      break;
      
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
    default:
      throw LagSHTError("EROOR Test type unkwown");
    }//end of switch


    cout << "---/ Fin bloc try ---- " << endl;}
    
  catch (LagSHTError & e) {
    cerr << " lagsh_testsuite.cc: Catched Exception (LagSHTError)" << (string)typeid(e).name() 
	 << " - Msg= " << e.what() << endl;
    rc = 99;
  }
  catch (std::exception & e) {
    cerr << " lagsh_testsuite.cc: Catched std::xception "  
	 << " - what()= " << e.what() << endl;
    rc = 98;
  }
  catch (...) {
    cerr << " lagsh_testsuite.cc: some other exception (...) was caught ! " << endl;
    rc = 97;
  }
  cout << " ---- Programme lagsh_testsuite.cc -  FIN  (Rc=" << rc << ") --- " << endl;
  return rc;
}//main