lagsht_testsuite.cc 26.1 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 215 216 217

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

  vector< complex<r_8> > flmnOrig(Ntot);
  int id=-1;
221
  for(int n=0;n<Nmax;n++){ //on each shell
222 223 224 225 226 227 228 229 230 231 232
    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 
233
  std::copy(flmnOrig.begin(), flmnOrig.end(), std::ostream_iterator< complex<r_8> >(std::cout, " "));
234 235
#endif
      
236 237 238 239
  
  tstack_pop("data input");
  tstack_push("processing");
  tstack_push("processing part Synthesis");
240 241 242 243 244 245

#if DEBUG >= 1
  cout << "Main:Synthesis r_8 function..." << endl;
#endif
  vector<r_8> fijk(NpTot);
  sphlagtrans.Synthesis(flmnOrig,fijk);
246
#if DEBUG >= 2
247
  for (int i=0; i<NpTot; i++){
248
    cout << "fijk("<<i<<"): " << fijk[i] << endl;
249
  }
250
#endif
251 252 253 254
  
  tstack_pop("processing part Synthesis");
  tstack_push("processing part Analysis");
  
255 256 257 258 259 260
#if DEBUG >= 1
  cout << "Main:Analysis r_8 function..." << endl;
#endif
  vector< complex<r_8> > flmn(Ntot);
  sphlagtrans.Analysis(fijk,flmn);
    
261 262 263
  tstack_pop("processing part Analysis");
  tstack_pop("processing");  

264 265 266 267 268 269


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

  r_8 err_abs(0.);
  r_8 err_rel(0.);
270
  int imax = -1;
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
  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; 
288
  cout << "Err. Max. " << err_abs << " [" << imax << "], Err. Rel. " << err_rel << endl;
289 290 291 292 293
  cout << " >>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 


}//MultiSphericalLaguerreTransform

294 295 296
//---------------------------------------
void SpinMultiSphericalLaguerreTransform() {

297
  r_8 alpha = param.alpha;  
298 299 300 301 302 303
  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;
304
  int spin = param.spin; //JEC 18/1/16
305

306
  cout << " ___________  SpinMultiSphericalLaguerreTransform   TEST _____________ " << endl;
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

  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

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

  vector< complex<r_8> > ElmnOrig(Ntot);
  vector< complex<r_8> > BlmnOrig(Ntot);
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
338 339
  int id
=-1;
340

341 342 343 344
  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++;
345 346
	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);
347
	ElmnOrig[id] = complex<r_8>(rv,iv);	    
348 349
	rv = (l<abs(spin)) ? 0.0 : drand(-1,1,&state);
	iv = (m==0 || l<abs(spin)) ? 0.0 : drand(-1,1,&state);
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
	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;
  vector< complex<r_8> > Elmk; //calcul intermediaire
  vector< complex<r_8> > Blmk;

  sphlagtrans.Synthesis(ElmnOrig, BlmnOrig, spin, fijk_re, fijk_im, Elmk, Blmk);
374
#if DEBUG >= 2
375 376 377
  for (int i=0; i<NpTot; i++){
    cout << "fijk("<<i<<"): " << fijk_re[i] << ", " << fijk_im[i] << endl;
  }
378
#endif
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  
  tstack_pop("processing part Synthesis");
  tstack_push("processing part Analysis");
  
#if DEBUG >= 1
  cout << "Main:Analysis complex function..." << endl;
#endif
  vector< complex<r_8> > Elmn;
  vector< complex<r_8> > Blmn;

  vector< complex<r_8> > Elmk_ana;
  vector< complex<r_8> > Blmk_ana;

  sphlagtrans.Analysis(fijk_re, fijk_im, spin, Elmn, Blmn, Elmk_ana, Blmk_ana);
    
  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
404
    if(i>(Ntot-9)) {
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
      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

Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
430 431 432 433 434 435 436
//-------------------------------------------------------------
void TestLaguerre2Bessel() {

  //Todo introduce Pmax !

  int Nmax = param.N;
  int Lmax = param.Lmax;
437
  int Pmax = param.Pmax;
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
438 439 440 441
  r_8 Rmax = param.R;
  string geometry = param.geometry;
  int ntheta = param.ntheta;
  int nphi = param.nphi;
442 443 444 445
  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
446 447 448

  cout << " ______________ TestLaguerre2Bessel TEST ___________ " << endl;
  
449 450
   cout << "tstack 1 Start" <<endl;
   tstack_push("data input");
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
451 452 453 454 455 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

  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++;
486 487 488 489 490 491
	
	//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
492 493 494 495 496 497 498 499
	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 
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
  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
517 518 519 520 521 522 523 524 525 526
#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");
527 528
  Laguerre2Bessel lag2bess(sphere,lagTrans,Nmax,Pmax,Rmax,
			   clenshawDir,jlnpDir,recomputeJlnp);
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
529 530
  cout << "tstack 2a End" <<endl;
  tstack_pop("Init Laguerre 2 Bessel");
531

Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
532 533 534 535
  cout << "tstack 2b Start" <<endl;
  tstack_push("Compute Fourier-Bessel coeffs.");

  vector< complex<r_8> > FBlmp(Nalm*Pmax);
536
  lag2bess.Lag2BessCoeff(flmnOrig,FBlmp);
537 538 539 540 541
#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
542 543 544 545 546 547 548
  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;
549
  vector<r_8> fFBijk;
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
550 551 552 553 554 555
  lag2bess.Synthesis(FBlmp,FBalmk,fFBijk);
  cout << "tstack 2c End" <<endl;
  tstack_pop("Compute Alm(rk) from FB coeffs.");
  

  cout << "tstack 2d Start" <<endl;
556
  tstack_push("Compute Alm(rk) from FL coeffs.");
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
557 558 559
  vector< complex<r_8> > FLalmk;
  vector<r_8> fFLijk;
  sphlagtrans.Synthesis(flmnOrig,fFLijk,FLalmk);
560 561 562 563 564 565
#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
566
  cout << "tstack 2d End" <<endl;
567
  tstack_pop("Compute Alm(rk) from FL coeffs.");
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
568
  
569
  cout << "tstack 2 End" <<endl;
570

571 572 573
  tstack_pop("processing");


574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
  //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");


604 605
  
  
606
  {//Check 1
607
    cout << "Dump FL or FB reconstructed Alm(r_k)" <<endl;
608

609
#if DEBUG >=2 
610 611
    std::ofstream ofs;
    ofs.open ("Almi.txt", std::ofstream::out);
612
#endif
613 614
    
    for(int n=0;n<Nmax;n++){
615 616 617
      r_8 err_abs(0.);
      r_8 err_rel(0.);

618 619 620
      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 
621 622

#if DEBUG >=2 
623 624 625 626
	  ofs <<l<<" "
	      <<m<<" "
	      <<n<< " " << setprecision(12) << FLalmk[id].real() << " " <<   FLalmk[id].imag()
	      << "  " << FBalmk[id].real() << " " << FBalmk[id].imag()  << endl;
627
#endif	  
628 629 630 631 632 633 634 635 636
	  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;
637 638
	}
      }
639 640 641 642 643
      
      cout << " >>>>>>>>>>>>>>>>>>>>> Shell["<<n<<"] <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 
      cout << "Err. Max. " << err_abs << ", Err. Rel. " << err_rel << endl;
      cout << " >>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<" << endl; 

644
    }
645
#if DEBUG >=2 
646
    ofs.close();
647
#endif
648
  }//check 1
649

650
  {//check 2
651
#if DEBUG >=2 
652 653
    std::ofstream ofs;
    ofs.open ("fijk.txt", std::ofstream::out);
654
#endif
655 656
    cout << "Dump FL or FB reconstructed fijk on each shell k" <<endl;

657
    for (int ish=0;ish<Nshell; ish++){
658 659
      r_8 err_abs(0.);
      r_8 err_rel(0.);
660 661
      for (int ip=0; ip<Npix; ip++) {
	int id = ish*Npix+ip;
662
	//	cout << "Shell("<<ish<<") Pix["<<ip<<"] FL fijk = " << fFLijk[id] << " FB fijk = " << fFBijk[id] << endl;
663 664

#if DEBUG >=2 
665
	ofs << ish << " " << ip << " " << setprecision(12) <<  fFLijk[id] << " " << fFBijk[id] << endl;
666
#endif
667 668 669 670 671 672 673 674 675 676 677 678 679 680
	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
681 682


683
#if DEBUG >=2 
684
    ofs.close();
685
#endif
686
  }//check 2
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
687 688 689 690

  

}//TestLaguerre2Bessel
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
//-------------------------------------------------------------

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
  
718
  stringstream ss; ss << "pixels-" << geometry << "-L" << Lmax << ".txt";
719
  std::ofstream ofs (ss.str().c_str(), std::ofstream::out);
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

  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
745 746 747 748 749 750 751

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

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

752
   unsigned int maxmemsize =  getMemorySize()/1e6;
753 754
   cout << "Max Memory size: " <<  maxmemsize << " MBytes" <<  endl;

755
  int N = 128; 
756
  r_8 R = 1.;
757
  int Lmax = 128;
758
  int Pmax = 0;
759
  int spin = 0;
760

761 762 763 764 765
  int test=1;

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

767 768 769 770 771 772 773
  //JEC 23/11/15
  char* pLAGSHTROOTDIR; pLAGSHTROOTDIR = getenv("LAGSHTROOTDIR");
  string clenshawDir = string(pLAGSHTROOTDIR)+"/data";
  string jlnpDir = string(pLAGSHTROOTDIR)+"/data";
  bool recomputeJlnp = false;


774 775 776
  //JEC 12/1/16 alpha as input
  double alpha = 2.0; //default

777 778 779
  int ka=1;
  while (ka<narg) {
    if (strcmp(arg[ka],"-h")==0) {
780
      cout << "usage: ./Objs/lagsht_testsuite -t <test number> [1]\n"
781 782
	   << " [-a <alpha> [2 by default] ] "
	   << " [-n <Nmax> [128]] [-l <Lmax> [128]]\n" 
783 784 785
	   << " [-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"
786
	   << " [-p <Pmax for Fourier-Bessel> [Nmax] ]\n"
787 788
	   << " [-r <Rmax> [1.]]\n"
	   << " [-spin <spin value> [0]"
789
	   << endl; 
790 791
      return 0;
    }
792 793 794 795
    else if (strcmp(arg[ka],"-a")==0) {
      alpha = atof(arg[ka+1]);
      ka+=2;      
    }    
796 797 798 799
    else if (strcmp(arg[ka],"-spin")==0) {
      spin = atoi(arg[ka+1]);
      ka+=2;      
    }    
800 801 802 803 804 805 806 807 808 809 810 811
    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;
    }
812 813 814 815 816 817 818 819 820 821 822 823
    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
824 825 826 827
    else if (strcmp(arg[ka],"-r")==0) {
      R = atof(arg[ka+1]);
      ka+=2;      
    }    
828 829 830 831
    else if (strcmp(arg[ka],"-p")==0) {
      Pmax = atof(arg[ka+1]);
      ka+=2;      
    }    
832 833 834 835 836 837 838 839 840 841 842 843
    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;
    }
844 845 846 847 848
    else ka++;
  }//eo while

  param.Lmax = Lmax;
  param.N    = N;
849
  param.Pmax = Pmax;
850
  param.R    = R;
851
  param.spin  = spin;
852
  param.alpha = alpha;
853 854 855
  param.geometry = geometry;
  param.ntheta = ntheta;
  param.nphi = nphi;
856

857 858 859 860 861
  param.clenshawDir = clenshawDir;
  param.jlnpDir     = jlnpDir;
  param.recomputeJlnp = recomputeJlnp;


862 863
  cout << "Configuration parameters are set to: "
       << " Test number : " << test << "\n"
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
864
       << " Lmax, Nmax, Pmax, spin, Rmax , alpha: " 
865 866 867
       << param.Lmax << ", "
       << param.N << ", "
       << param.Pmax << ", "
868
       << param.spin << ", "
869 870 871
       << param.R << ", "
       << param.alpha << "\n"
       << "Geometry: " << param.geometry << " Ntheta, Nphi: " 
872
       << param.ntheta << ", " << param.nphi<<"\n";
873

874 875 876 877 878 879 880 881 882 883 884 885 886 887
  int rc=0;
  try {
    
    switch(test) {
    case 0:
      TestBasic();
      break;
    case 1:
      LaguerreQuadrature();
      break;
    case 2:
      MultiLaguerreTransformSdtAlone();
      break;
    case 3:
888 889 890
      TestPixelization();
      break;
    case 4:
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
      {
	if(N>5500) throw LagSHTError("WARNING: N>5500 the algorithm may fail due to weight estimates"); 
	
      
	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
	
	tstack_push("MultiSphericalLaguerreTransform");
	MultiSphericalLaguerreTransform();
	tstack_pop("MultiSphericalLaguerreTransform");
	tstack_report("MultiSphericalLaguerreTransform");  
      }
908
      break;
909

Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
910 911
    case 5:
      {
912 913 914 915 916 917 918
	if(recomputeJlnp){
	  cout << "rebuild Jlnp: \n Clenshaw Path<"<<param.clenshawDir<<">\n and jlnp Path<"
	       << param.jlnpDir << ">"
	       << endl;
	} else {
	  cout << "Jlnp path <" << param.jlnpDir << ">" << endl;
	}
919
	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
920 921
	tstack_push("TestLaguerre2Bessel");
	TestLaguerre2Bessel();
922 923
 	tstack_pop("TestLaguerre2Bessel");
 	tstack_report("TestLaguerre2Bessel");
Jean-Eric Campagne's avatar
Jean-Eric Campagne committed
924 925 926
      }
      break;

927 928 929 930 931 932 933 934 935 936

    case 6:	
      {
	tstack_push("SpinMultiSphericalLaguerreTransform");
	SpinMultiSphericalLaguerreTransform();
	tstack_pop("SpinMultiSphericalLaguerreTransform");
	tstack_report("SpinMultiSphericalLaguerreTransform");  
      }
      break;
      
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
    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