Changeset 3320 in Sophya


Ignore:
Timestamp:
Sep 5, 2007, 9:58:05 AM (18 years ago)
Author:
cmv
Message:

intro SchechterMassDist pour accelerer la simulations cmv 05/09/2007

Location:
trunk/Cosmo/SimLSS
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cosmo/SimLSS/cmvschdist.cc

    r3319 r3320  
    66// ex: on a un pixel qui a N galaxies, quelle est la distribution
    77//     statistique de masse dans un tel pixel ?
    8 // > cmvschdist -a -v -m 1e+7,1e+14 -n 140 -r 200 -N 100000
     8// > cmvschdist -a -v -m 1e+8,1e+13 -n -100 -r 200 -N 100000
     9// compare with:
     10// > cmvschdist -a -v -m 1e+8,1e+13 -n -100 -r 200 -N 100000 -0
     11// Fabriquer un fichier
     12// > cmvschdist -a -m 1e+8,1e+13 -n -100 -r 2000 -N 5000000 -W schdist_2000.ppf
    913#include <iostream>
    1014#include <stdlib.h>
     
    2832void usage(void)
    2933{
    30  cout<<"cmvschdist -m xmin,xmax -a -n nbin -r ngmax,ngmin -N nalea"<<endl
    31      <<"  xmin,xmax : limites en masse"<<endl
    32      <<"  nbin : nombre de bins"<<endl
    33      <<"  ngmax,ngmin : distribution pour tirage de ngmin a ngmax galaxies"<<endl
    34      <<"  nalea : nombre de tirages aleatoires"<<endl
     34 cout<<"cmvschdist -m xmin,xmax -a -0 -n nbin -r ngmax,ngmin -N nalea "
     35     <<"-R readfile.ppf -W writefile.ppf"<<endl
     36     <<"  -m xmin,xmax : limites en masse"<<endl
     37     <<"  -n nbin : nombre de bins (si <0 alors nb par decade)"<<endl
     38     <<"  -r ngmax,ngmin : distribution pour tirage de ngmin a ngmax galaxies"<<endl
     39     <<"  -N nalea : nombre de tirages aleatoires"<<endl
    3540     <<"  -v : back verification of random trials"<<endl
    36      <<"  -a : init auto de l\'aleatoire"<<endl;
     41     <<"  -0 : only use ngal=1 histogram"<<endl
     42     <<"  -a : init auto de l\'aleatoire"<<endl
     43     <<"  -R readfile.ppf : read SchechterMassDist from ppf file"<<endl;
    3744}
    3845
     
    4653
    4754 double xmin=1e8, xmax=1e13;
    48  int npt = 100;
     55 int npt = -100;
    4956 int ngmax = 200, ngmin = 1;
     57 bool useonly1 = false;
    5058 unsigned long long nalea = 10000;
    5159 bool verif = false;
     60 bool readfrppf = false;
     61 string namefrppf = "";
     62 string nametoppf = "";
    5263
    5364 char c;
    54  while((c = getopt(narg,arg,"havm:N:n:r:")) != -1) {
     65 while((c = getopt(narg,arg,"h0avm:N:n:r:R:W:")) != -1) {
    5566   switch (c) {
    5667   case 'm' :
     
    6879   case 'n' :
    6980     sscanf(optarg,"%d",&npt);
    70      if(npt<=0) npt = 100;
     81     if(npt<=0) npt = -100;
    7182     break;
    7283   case 'N' :
     
    7788     verif = true;
    7889     break;
     90   case '0' :
     91     useonly1 = true;
     92     break;
     93   case 'R' :
     94     readfrppf = true;
     95     namefrppf = optarg;
     96     break;
     97   case 'W' :
     98     nametoppf = optarg;
     99     break;
    79100   case 'a' :
    80101     Auto_Ini_Ranf(5);
     
    86107 }
    87108
    88  double lnx1=log10(xmin), lnx2=log10(xmax), dlnx = (lnx2-lnx1)/npt;
    89  cout<<"xmin="<<xmin<<" ("<<lnx1<<") xmax="<<xmax<<" ("<<lnx2<<"), dlnx="<<dlnx<<endl;
     109 double lnx1=log10(xmin), lnx2=log10(xmax);
     110 cout<<"xmin="<<xmin<<" ("<<lnx1<<") xmax="<<xmax<<" ("<<lnx2<<")"<<endl;
    90111 cout<<"npt="<<npt<<",  nalea="<<nalea<<endl;
    91  cout<<"ngmin="<<ngmin<<" ngmax="<<ngmax<<endl;
     112 cout<<"useonly1="<<useonly1<<" ngmin="<<ngmin<<" ngmax="<<ngmax<<endl;
    92113 cout<<"verif="<<verif<<endl;
     114 if(readfrppf) cout<<"SchechterMassDist will be read from file "<<namefrppf<<endl;
     115 if(nametoppf.size()>0) cout<<"SchechterMassDist will be written to file "<<nametoppf<<endl;
    93116
    94117
     
    96119 cout<<"> Schechter m*dn/dm nstar="<<nstar<<" mstar="<<mstar<<" alpha="<<alpha<<endl;
    97120 Schechter sch(nstar,mstar,alpha);
    98  sch.SetOutValue(1);  // on veut m*dN/dm
    99  Histo hmdndm(lnx1,lnx2,npt); hmdndm.ReCenterBin();
    100  FuncToHisto(sch,hmdndm,true);
    101  FunRan tirhmdndm = FunRan(hmdndm,true);
    102 
    103  //------- Construct histo
    104  int nbhist = ngmax-ngmin+1;
    105  cout<<"> Creating "<<nbhist<<" histos"<<endl;
    106  vector<Histo> vhisto;  vector<string> vhname;
    107  for(int i=ngmin;i<=ngmax;i++) {
    108    Histo h(hmdndm); h.Zero();
    109    vhisto.push_back(h);
    110    char str[32]; sprintf(str,"h%d",i);
    111    vhname.push_back(string(str));
    112  }
     121
     122 //------- Construct Mass Distribution
     123 cout<<"> Creating  Mass Distribution"<<endl;
     124 SchechterMassDist schdmass(sch,xmin,xmax,npt);
     125 if(readfrppf) {
     126   cout<<"> Mass Distribution read from "<<namefrppf<<endl;
     127   schdmass.ReadPPF(namefrppf);
     128 }
     129 Histo hmdndm = schdmass.GetHmDnDm();
     130 FunRan tirhmdndm = schdmass.GetTmDnDm();
    113131
    114132 //------- Random
    115133 InitTim();
    116  cout<<"> Random: "<<nalea<<" trials"<<endl;
    117  int lpmod = nalea/25; if(lpmod<=0) lpmod=1;
    118  double bigsum = 0.; long nbigsum=0;
    119  for(unsigned long long ia=0;ia<nalea;ia++) {
    120    if(ia%lpmod==0) cout<<"... "<<ia<<endl;
    121    double sum = 0.;
    122    for(int i=1;i<=ngmax;i++) {
    123      //double l10m = tirhmdndm.Random();
    124      double l10m = tirhmdndm.RandomInterp();
    125      double m = pow(10.,l10m);
    126      sum += m;
    127      bigsum += m; nbigsum++;
    128      int ipo = i-ngmin;
    129      if(ipo<0) continue;
    130      double v = log10(sum/(double)i);
    131      vhisto[ipo].Add(v);
    132    }
    133    if(ia%lpmod==0) PrtTim(" ");
    134  }
    135  PrtTim("End Random loop");
    136  if(nbigsum>0) {
    137    bigsum /= (double)nbigsum;
    138    cout<<"Mean mass : "<<bigsum<<" ("<<log10(fabs(bigsum))<<")"<<endl;
    139  }
    140 
    141  //------- Generation des classes de tirage aleatoire et des histos de verif
    142  vector<FunRan> vtir;  vector<string> vtname;
    143  vector<Histo> vthisto;  vector<string> vthname;
     134 if(!useonly1 && !readfrppf) {
     135   cout<<"> Creating "<<ngmax-ngmin+1<<" histos and filling with  "<<nalea<<" trials"<<endl;
     136   schdmass.SetNgalLim(ngmax,ngmin,nalea);
     137   PrtTim("End of filling histos for trials");
     138 }
     139 schdmass.Print();
     140
     141 //------- Generation des histos de verif
     142 vector<Histo> vthisto; vector<string> vthname;
    144143 if(verif) {
    145    cout<<"> Creating "<<nbhist<<" FunRan and Histos for back-verif"<<endl;
     144   cout<<"> Creating "<<ngmax-ngmin+1<<" Histos for back-verif"<<endl;
    146145   for(int i=ngmin;i<=ngmax;i++) {
    147      FunRan t(vhisto[i-ngmin],true);
    148      vtir.push_back(t);
    149      char str[32]; sprintf(str,"t%d",i);
    150      vtname.push_back(string(str));
    151      Histo h(vhisto[i-ngmin]); h.Zero();
     146     Histo h(hmdndm); h.Zero();
    152147     vthisto.push_back(h);
    153      sprintf(str,"th%d",i);
     148     char str[32]; sprintf(str,"th%d",i);
    154149     vthname.push_back(string(str));
    155150   }
     151   cout<<"   "<<vthisto.size()<<" Histos created"<<endl;
     152   vthisto[20].Show();
    156153
    157154   cout<<"> Checking tirage"<<endl;
     155   int lpmod = nalea/20; if(lpmod<=0) lpmod=1;
    158156   for(unsigned long long ia=0;ia<nalea;ia++) {
    159      for(unsigned int i=0;i<vtir.size();i++) {
    160        //double v = vtir[i].Random();
    161        double v = vtir[i].RandomInterp();
    162        vthisto[i].Add(v);
     157     if(ia%lpmod==0) cout<<"...filling tirage "<<ia<<endl;
     158     for(unsigned int i=0;i<vthisto.size();i++) {
     159       int ng = ngmin+i;
     160       double v = schdmass.TirMass(ng)/(double)ng;
     161       vthisto[i].Add(log10(v));
    163162     }
    164163   }
     164   schdmass.PrintStatus();
    165165   PrtTim("End Random loop back-tirage");
    166166 }
    167167
    168  //------- Ecriture ppf
    169  cout<<"Ecriture"<<endl;
     168 //------- Ecritures ppf
     169 if(nametoppf.size()>0) {
     170   cout<<"Ecriture de l\'objet SchechterMassDist dans "<<nametoppf<<endl;
     171   schdmass.WritePPF(nametoppf);
     172 }
     173
     174 cout<<"Ecriture pour verification"<<endl;
    170175 string tag = "cmvschdist.ppf";
    171176 POutPersist pos(tag);
     177 {
     178 cout<<"  writing hmdndm tirhmdndm"<<endl;
    172179 tag = "hmdndm"; pos.PutObject(hmdndm,tag);
    173  Histo hdum2(tirhmdndm);
    174  tag = "tirhmdndm"; pos.PutObject(hdum2,tag);
    175  if(vhisto.size()>0)
    176    for(unsigned int i=0;i<vhisto.size();i++)
    177      if(vhisto[i].NEntries()>0) pos.PutObject(vhisto[i],vhname[i]);
    178  if(vthisto.size()>0)
    179    for(unsigned int i=0;i<vthisto.size();i++)
     180 Histo hdum(tirhmdndm);
     181 tag = "tirhmdndm"; pos.PutObject(hdum,tag);
     182 }
     183 if(schdmass.GetNgalLim()>0) {
     184   cout<<"  writing h t"<<endl;
     185   for(int i=0;i<schdmass.GetNgalLim();i++) {
     186     int ng = schdmass.NGalFrIndex(i);
     187     if(ng<=0) continue;
     188     char str[32];
     189     sprintf(str,"h%d",ng);
     190     Histo hdum = schdmass.GetHisto(i);
     191     tag = str; pos.PutObject(hdum,tag);
     192     sprintf(str,"t%d",ng);
     193     Histo hdum2(schdmass.GetFunRan(i));
     194     tag = str; pos.PutObject(hdum2,tag);
     195   }
     196 }
     197 if(vthisto.size()>0) {
     198  cout<<"  writing th"<<endl;
     199  for(unsigned int i=0;i<vthisto.size();i++)
    180200     if(vthisto[i].NEntries()>0) pos.PutObject(vthisto[i],vthname[i]);
     201 }
     202
     203 PrtTim("End of Job");
    181204
    182205 return 0;
     
    191214
    192215set h h$n
     216set t t$n
    193217set th th$n
     218
     219disp tirhmdndm
     220disp $t "same red"
    194221
    195222disp $h
     
    201228n/plot $h.log10(val)%x val>0. ! "connectpoints"
    202229n/plot $th.log10(val)%x val>0. ! "nsta connectpoints same red"
     230
     231n/plot $h.val%pow(10.,x) ! ! "connectpoints"
     232n/plot $th.val%pow(10.,x) ! ! "nsta connectpoints same red"
    203233
    204234c++exec \
  • trunk/Cosmo/SimLSS/cmvtstsch.cc

    r3284 r3320  
    9393 cout<<"......"<<endl;
    9494 for(double lnx=lnx1-3.;lnx<=lnx2-1.;lnx+=1.) {
    95    double num = IntegrateFuncLog(sch,lnx,lnx2,perc,dlxinc,dlxmax,glorder);
    96    cout<<"["<<lnx<<","<<lnx2<<"] integrated number = "<<num<<" Mpc^-3"<<endl;
     95   double num = sch.Integrate(pow(10.,lnx),pow(10.,lnx2),npt);
     96   double numc = IntegrateFuncLog(sch,lnx,lnx2,perc,dlxinc,dlxmax,glorder);
     97   cout<<"["<<lnx<<","<<lnx2<<"] integrated number = "<<num<<" Mpc^-3 , check: "<<numc<<endl;
    9798 }
    9899 cout<<"......"<<endl;
    99100 for(double lnx=lnx1+1.;lnx<=lnx2+3.;lnx+=1.) {
    100    double num = IntegrateFuncLog(sch,lnx1,lnx,perc,dlxinc,dlxmax,glorder);
    101    cout<<"["<<lnx1<<","<<lnx<<"] integrated number = "<<num<<" Mpc^-3"<<endl;
     101   double num = sch.Integrate(pow(10.,lnx1),pow(10.,lnx),npt);
     102   double numc = IntegrateFuncLog(sch,lnx1,lnx,perc,dlxinc,dlxmax,glorder);
     103   cout<<"["<<lnx1<<","<<lnx<<"] integrated number = "<<num<<" Mpc^-3 , check: "<<numc<<endl;
    102104 }
    103105 cout<<"......"<<endl;
    104106 for(double lnx=lnx1-3.;lnx<=lnx2+3.;lnx+=1.) {
    105    double num = IntegrateFuncLog(sch,lnx,lnx+1.,perc,dlxinc,dlxmax,glorder);
    106    cout<<"["<<lnx<<","<<lnx+1.<<"] integrated number = "<<num<<" Mpc^-3"<<endl;
     107   double num = sch.Integrate(pow(10.,lnx),pow(10.,lnx+1.),npt);
     108   double numc = IntegrateFuncLog(sch,lnx,lnx+1.,perc,dlxinc,dlxmax,glorder);
     109   cout<<"["<<lnx<<","<<lnx+1.<<"] integrated number = "<<num<<" Mpc^-3 , check: "<<numc<<endl;
    107110 }
    108111 Histo hdndm(lnx1,lnx2,npt); hdndm.ReCenterBin();
     
    115118 cout<<"......"<<endl;
    116119 for(double lnx=lnx1-3.;lnx<=lnx2-1.;lnx+=1.) {
    117    double sum = IntegrateFuncLog(sch,lnx,lnx2,perc,dlxinc,dlxmax,glorder);
    118    cout<<"["<<lnx<<","<<lnx2<<"] integrated mass = "<<sum<<" Msol.Mpc^-3"<<endl;
     120   double sum = sch.Integrate(pow(10.,lnx),pow(10.,lnx2),npt);
     121   double sumc = IntegrateFuncLog(sch,lnx,lnx2,perc,dlxinc,dlxmax,glorder);
     122   cout<<"["<<lnx<<","<<lnx2<<"] integrated mass = "<<sum<<" Msol.Mpc^-3 , check: "<<sumc<<endl;
    119123 }
    120124 cout<<"......"<<endl;
    121125 for(double lnx=lnx1+1.;lnx<=lnx2+3.;lnx+=1.) {
    122    double sum = IntegrateFuncLog(sch,lnx1,lnx,perc,dlxinc,dlxmax,glorder);
    123    cout<<"["<<lnx1<<","<<lnx<<"] integrated mass = "<<sum<<" Msol.Mpc^-3"<<endl;
     126   double sum = sch.Integrate(pow(10.,lnx1),pow(10.,lnx),npt);
     127   double sumc = IntegrateFuncLog(sch,lnx1,lnx,perc,dlxinc,dlxmax,glorder);
     128   cout<<"["<<lnx1<<","<<lnx<<"] integrated mass = "<<sum<<" Msol.Mpc^-3 , check: "<<sumc<<endl;
    124129 }
    125130 cout<<"......"<<endl;
    126131 for(double lnx=lnx1-3.;lnx<=lnx2+3.;lnx+=1.) {
    127    double sum = IntegrateFuncLog(sch,lnx,lnx+1.,perc,dlxinc,dlxmax,glorder);
    128    cout<<"["<<lnx<<","<<lnx+1.<<"] integrated mass = "<<sum<<" Msol.Mpc^-3"<<endl;
     132   double sum = sch.Integrate(pow(10.,lnx),pow(10.,lnx+1.),npt);
     133   double sumc = IntegrateFuncLog(sch,lnx,lnx+1.,perc,dlxinc,dlxmax,glorder);
     134   cout<<"["<<lnx<<","<<lnx+1.<<"] integrated mass = "<<sum<<" Msol.Mpc^-3 , check: "<<sumc<<endl;
    129135 }
    130136 Histo hmdndm(lnx1,lnx2,npt); hmdndm.ReCenterBin();
  • trunk/Cosmo/SimLSS/genefluct3d.cc

    r3289 r3320  
    940940}
    941941
     942int_8 GeneFluct3D::MinMax(double& xmin,double& xmax,double vmin,double vmax)
     943// Calcul des valeurs xmin et xmax dans le cube reel avec valeurs ]vmin,vmax[ extremites exclues
     944{
     945 bool tstval = (vmax>vmin)? true: false;
     946 if(lp_>0) {
     947   cout<<"--- MinMax";
     948   if(tstval) cout<<"  range=]"<<vmin<<","<<vmax<<"[";
     949   cout<<endl;
     950 }
     951 check_array_alloc();
     952
     953 int_8 n = 0;
     954 xmin = xmax = data_[0];
     955
     956 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
     957   int_8 ip = IndexR(i,j,l);
     958   double x = data_[ip];
     959   if(tstval && (x<=vmin || x>=vmax)) continue;
     960   if(x<xmin) xmin = x;
     961   if(x>xmax) xmax = x;
     962   n++;
     963 }
     964
     965 if(lp_>0) cout<<"  n="<<n<<" min="<<xmin<<" max="<<xmax<<endl;
     966
     967 return n;
     968}
     969
    942970int_8 GeneFluct3D::MeanSigma2(double& rm,double& rs2,double vmin,double vmax
    943971                             ,bool useout,double vout)
     
    11141142       data_[ip] += m;
    11151143     }
     1144     sum += data_[ip];
     1145   }
     1146 }
     1147 if(lp_>0) cout<<sum<<" MSol HI mass put into survey"<<endl;
     1148
     1149 return sum;
     1150}
     1151
     1152double GeneFluct3D::TurnNGal2MassQuick(SchechterMassDist& schmdist)
     1153// idem TurnNGal2Mass mais beaucoup plus rapide
     1154{
     1155 if(lp_>0) cout<<"--- TurnNGal2MassQuick ---"<<endl;
     1156 check_array_alloc();
     1157
     1158 double sum = 0.;
     1159 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
     1160   int_8 ip = IndexR(i,j,l);
     1161   double v = data_[ip];
     1162   if(v>0.) {
     1163     long ngal = long(v+0.1);
     1164     data_[ip] = schmdist.TirMass(ngal);
    11161165     sum += data_[ip];
    11171166   }
  • trunk/Cosmo/SimLSS/genefluct3d.h

    r3290 r3320  
    9393  int_8 MeanSigma2(double& rm,double& rs2,double vmin=1.,double vmax=-1.
    9494                  ,bool useout=false,double vout=0.);
     95  int_8 MinMax(double& xmin,double& xmax,double vmin=1.,double vmax=-1.);
    9596  int_8 NumberOfBad(double vmin=-1.e+150,double vmax=1.e+150);
    9697  int_8 SetToVal(double vmin, double vmax,double val0=0.);
     
    101102  double ApplyPoisson(void);
    102103  double TurnNGal2Mass(FunRan& massdist,bool axeslog=false);
     104  double TurnNGal2MassQuick(SchechterMassDist& schmdist);
    103105  double TurnMass2Flux(void);
    104106  void AddAGN(double lfjy,double lsigma,double powlaw=0.);
  • trunk/Cosmo/SimLSS/geneutils.cc

    r3199 r3320  
    366366////////////////////////////////////////////////////////////////////////////////////
    367367double IntegrateFuncLog(GenericFunc& func,double lxmin,double lxmax
    368          ,double perc,double dlxinc,double dlxmax,unsigned short  glorder)
     368         ,double perc,double dlxinc,double dlxmax,unsigned short glorder)
    369369// --- Integration adaptative ---
    370370// Idem IntegrateFunc but integrate on logarithmic (base 10) intervals:
  • trunk/Cosmo/SimLSS/schechter.cc

    r3196 r3320  
    88
    99#include "pexceptions.h"
     10#include "histos.h"
     11#include "perandom.h"
     12#include "tvector.h"
     13#include "fioarr.h"
    1014
    1115#include "constcosmo.h"
     16#include "geneutils.h"
    1217#include "schechter.h"
    1318
     
    2732Schechter::Schechter(Schechter& f)
    2833  : nstar_(f.nstar_) , mstar_(f.mstar_) , alpha_(f.alpha_) , outvalue_(f.outvalue_)
     34{
     35}
     36
     37Schechter::Schechter(void)
     38  : nstar_(0.) , mstar_(0.) , alpha_(0.) , outvalue_(0)
    2939{
    3040}
     
    4555}
    4656
     57unsigned short Schechter::GetOutValue(void)
     58{
     59  return outvalue_;
     60}
     61
     62void Schechter::SetParam(double nstar,double mstar,double alpha)
     63{
     64  nstar_ = nstar; mstar_ = mstar; alpha_ = alpha;
     65}
     66
     67void Schechter::GetParam(double& nstar,double& mstar,double& alpha)
     68{
     69  nstar = nstar_; mstar = mstar_; alpha = alpha_;
     70}
     71
    4772double Schechter::operator() (double m)
    4873// Return : "dn/dm = f(m)" or "m*dn/dm = f(m)"
     
    5277  if(outvalue_) dndm *= m;  // on veut m*dn/dm
    5378  return dndm;
     79}
     80
     81
     82double Schechter::Integrate(double massmin,double massmax,int npt)
     83// Integrate from massmin to massmax with at least npt points log10 spaced
     84{
     85 if(massmin<=0. || massmax<=0. || massmax<=massmin) return 0.;
     86 if(npt<1) npt = 100;
     87 double lmassmin = log10(massmin), lmassmax = log10(massmax);
     88 double perc=0.01, dlxinc=(lmassmax-lmassmin)/npt, dlxmax=10.*dlxinc; unsigned short glorder=4;
     89 double sum = IntegrateFuncLog(*this,lmassmin,lmassmax,perc,dlxinc,dlxmax,glorder);
     90 return sum;
    5491}
    5592
     
    63100  cout<<endl;
    64101}
     102
     103///////////////////////////////////////////////////////////
     104//******* Les facilites pour tirer sur Schechter ********//
     105///////////////////////////////////////////////////////////
     106
     107SchechterMassDist::SchechterMassDist(Schechter sch,double massmin,double massmax,int nbinmass)
     108// On veut une fonction de tirage aleatoire de la masse de N galaxies sur une Schechter
     109// Pour optimiser on cree (eventuellement) des histos de tirage
     110// ATTENTION: on donne les limites en masses pour les histos mais leurs abscisses
     111//   sont en log10(masse): histo [log10(massmin),log10(massmax)] avec nbinmass bins
     112// Si nbinmass<0 alors c'est le nombre de points par decade
     113  : sch_(sch) , sch_outvalue_(sch.GetOutValue())
     114  , massmin_(massmin) , massmax_(massmax) , nbinmass_(nbinmass)
     115  , ngalmin_(0) , ngalmax_(0) , nvalngal_(0)
     116  , ntrial_dir(0) , ntrial_tab(0)
     117  , hmdndm_(NULL) , tirhmdndm_(NULL)
     118{
     119  if(massmin_>massmax_  || massmin_<=0. || massmax_<=0.|| nbinmass_==0) {
     120    cout<<"SchechterMassDist::SchechterMassDist: error in input values"<<endl;
     121    throw ParmError("SchechterMassDist::SchechterMassDist: error in input values");
     122  }
     123
     124  // Creation du tirage aleatoire sur la Schechter
     125  sch_outvalue_ = sch.GetOutValue();  // on sauve la configuration de la schechter
     126  sch.SetOutValue(1);  // on veut m*dN/dm
     127  double lnx1 = log10(massmin_), lnx2 = log10(massmax_); // Binning en log10 de la masse
     128  if(nbinmass_<0) nbinmass_ = int((-nbinmass_)*(lnx2-lnx1+1.));
     129  hmdndm_ = new Histo(lnx1,lnx2,nbinmass_); hmdndm_->ReCenterBin();
     130  FuncToHisto(sch,*hmdndm_,true);  // true -> bin en log10(x)
     131  tirhmdndm_ = new FunRan(*hmdndm_,true);  // true -> histo est pdf
     132  sch.SetOutValue(sch_outvalue_);  // on remet a la valeur initiale
     133}
     134
     135SchechterMassDist::SchechterMassDist(void)
     136  : sch_outvalue_(0)
     137  , massmin_(0.) , massmax_(0.) , nbinmass_(0)
     138  , ngalmin_(0) , ngalmax_(0) , nvalngal_(0)
     139  , ntrial_dir(0) , ntrial_tab(0)
     140  , hmdndm_(NULL) , tirhmdndm_(NULL)
     141{
     142}
     143
     144SchechterMassDist::~SchechterMassDist(void)
     145{
     146 Delete();
     147}
     148
     149void SchechterMassDist::Delete(void)
     150{
     151 if(hmdndm_) delete hmdndm_;
     152 if(tirhmdndm_) delete tirhmdndm_;
     153 hmass_.resize(0);
     154 tmass_.resize(0);
     155 ntrial_dir = ntrial_tab = 0;
     156}
     157
     158int SchechterMassDist::GetMassLim(double& massmin,double& massmax)
     159{
     160 massmin = massmin_;
     161 massmax = massmax_;
     162 return nbinmass_;
     163}
     164
     165int SchechterMassDist::SetNgalLim(int ngalmax,int ngalmin,unsigned long nalea)
     166// Creation des Histos de tirage pour ngalmin a ngalmax galaxies
     167{
     168 int lp=2;
     169 ngalmin_=ngalmax_=nvalngal_=0;
     170 if(ngalmin<=0) ngalmin=1;
     171 if(ngalmax<ngalmin || ngalmax==1) return 0;
     172 ngalmin_ = ngalmin;
     173 ngalmax_ = ngalmax;
     174 nvalngal_ = ngalmax-ngalmin+1;
     175 
     176 if(nalea<1) nalea = 100000;
     177
     178 if(lp>0) cout<<"SchechterMassDist::SetNgalLim: ngal=["
     179              <<ngalmin_<<","<<ngalmax_<<"] n="<<nvalngal_
     180              <<" filling with "<<nalea<<" trials"<<endl;
     181
     182 //------- Construct histo
     183 double lnx1 = log10(massmin_), lnx2 = log10(massmax_);
     184 if(lp>0) cout<<"> Creating "<<nvalngal_<<" histos ["<<lnx1<<","<<lnx2<<"] n="<<nbinmass_<<endl;
     185 for(int i=ngalmin_;i<=ngalmax_;i++) {
     186   Histo h(*hmdndm_); h.Zero();
     187   hmass_.push_back(h);
     188 }
     189 if(lp>1) cout<<"...number of histos is "<<hmass_.size()<<endl;
     190
     191 //------- Remplissage random
     192 sch_.SetOutValue(1);  // on veut m*dN/dm
     193 int lpmod = nalea/20; if(lpmod<=0) lpmod=1;
     194 double s1=0., sc1=0.; unsigned long ns1=0;
     195 double sax=0., scax=0.; unsigned long nsax=0;
     196 for(unsigned long ia=0;ia<nalea;ia++) {
     197   if(lp>1 && ia%lpmod==0) cout<<"...tirage "<<ia<<endl;
     198   double sum = 0.;
     199   for(int i=1;i<=ngalmax_;i++) {
     200     //double l10m = tirhmdndm_->Random();
     201     double l10m = tirhmdndm_->RandomInterp();
     202     double m = pow(10.,l10m);
     203     sum += m;
     204     s1 += m; sc1 += m*m; ns1++;
     205     int ipo = i-ngalmin_;
     206     if(ipo<0) continue;
     207     // ATTENTION: l'histo de tirage stoque le log10(moyenne=somme_masses/ngal).
     208     //            Ca permet d'avoir le meme binning quelque soit ngal
     209     double v = log10(sum/(double)i);
     210     hmass_[ipo].Add(v);
     211     if(i==ngalmax) {sax += sum/(double)i; scax += sum/(double)i*sum/(double)i; nsax++;}
     212   }
     213 }
     214 sch_.SetOutValue(sch_outvalue_);  // on remet a la valeur initiale
     215
     216 if(ns1>1) {
     217   s1 /= ns1; sc1 = sc1/ns1 - s1*s1;
     218   cout<<"...Mean mass for ngal=1: "<<s1<<" ("<<log10(fabs(s1))<<")"
     219       <<" s="<<sqrt(fabs(sc1))<<" (ntrials "<<ns1<<")"<<endl;
     220 }
     221 if(nsax>1) {
     222   sax /= nsax; scax = scax/nsax - sax*sax;
     223   cout<<"...Mean mass for ngal="<<ngalmax_<<": "<<sax<<" ("<<log10(fabs(sax))<<")"
     224       <<" s="<<sqrt(fabs(scax))<<" (ntrials "<<nsax<<")"<<endl;
     225 }
     226
     227 //------- Generation des classes de tirage aleatoire et des histos de verif
     228 if(lp>0) cout<<"> Creating "<<nvalngal_<<" FunRan"<<endl;
     229 for(unsigned int i=0;i<hmass_.size();i++) {
     230   FunRan t(hmass_[i],true);
     231   tmass_.push_back(t);
     232 }
     233 if(lp>1) cout<<"...number of funran is "<<tmass_.size()<<endl;
     234
     235 return nvalngal_;
     236}
     237
     238int SchechterMassDist::GetNgalLim(int& ngalmax,int& ngalmin)
     239{
     240 ngalmax = ngalmax_;
     241 ngalmin = ngalmin_;
     242 return nvalngal_;
     243}
     244
     245Histo SchechterMassDist::GetHmDnDm(void) const
     246{
     247  return *hmdndm_;
     248}
     249
     250FunRan SchechterMassDist::GetTmDnDm(void) const
     251{
     252  return *tirhmdndm_;
     253}
     254
     255Histo SchechterMassDist::GetHisto(int i) const
     256{
     257  if(i<0 || i>=nvalngal_) {
     258    cout<<"SchechterMassDist::GetHisto: error in input values"<<endl;
     259    throw ParmError("SchechterMassDist::GetHisto: error in input values");
     260  }
     261  return hmass_[i];
     262}
     263
     264FunRan SchechterMassDist::GetFunRan(int i) const
     265{
     266  if(i<0 || i>=nvalngal_) {
     267    cout<<"SchechterMassDist::GetFunRan: error in input values"<<endl;
     268    throw ParmError("SchechterMassDist::GetFunRan: error in input values");
     269  }
     270  return tmass_[i];
     271}
     272
     273double SchechterMassDist::TirMass(int ngal)
     274{
     275 if(ngal<1) return 0.;
     276
     277 int ipo = IndexFrNGal(ngal);
     278 double masse_des_ngal = 0.;
     279 if(ipo<0) {  // Pas d'histos de tirage pour ce nombre de galaxies
     280   for(long i=0;i<ngal;i++) {  // On tire donc ngal fois sur la Schechter
     281     // double lm = tirhmdndm_->Random();
     282     double lm = tirhmdndm_->RandomInterp();
     283     masse_des_ngal += pow(10.,lm);  // ATTENTION abscisse en log10(masse)
     284   }
     285   ntrial_dir++;
     286 } else {
     287   // ATTENTION l'histo de tirage stoque le log10(moyenne=somme_masses/ngal)
     288   //double lmngal = tmass_[ipo].Random();
     289   double lmngal = tmass_[ipo].RandomInterp();
     290   masse_des_ngal = pow(10.,lmngal) * ngal;
     291   ntrial_tab++;
     292 }
     293
     294 return masse_des_ngal;
     295}
     296
     297void SchechterMassDist::Print(void)
     298{
     299 cout<<"SchechterMassDist::Print: mass=["<<massmin_<<","<<massmax_<<"] n="<<nbinmass_<<endl
     300     <<"                 ngal=["<<ngalmin_<<","<<ngalmax_<<"] n="<<nvalngal_<<endl;
     301 sch_.Print();
     302}
     303
     304void SchechterMassDist::PrintStatus(void)
     305{
     306 cout<<"SchechterMassDist::PrintStatus: number of trials: direct="<<ntrial_dir
     307     <<" tabulated="<<ntrial_tab<<endl;
     308}
     309
     310void SchechterMassDist::WritePPF(string ppfname)
     311{
     312 char str[64];
     313 cout<<"SchechterMassDist::WritePPF into "<<ppfname<<endl;
     314 POutPersist pos(ppfname.c_str());
     315
     316 double nstar,mstar,alpha;
     317 sch_.GetParam(nstar,mstar,alpha);
     318 TVector<r_8> tdum(20); tdum = 0.;
     319 tdum(0) = nstar;
     320 tdum(1) = mstar;
     321 tdum(2) = alpha;
     322 tdum(3) = sch_outvalue_;
     323 tdum(4) = massmin_;
     324 tdum(5) = massmax_;
     325 tdum(6) = nbinmass_;
     326 tdum(7) = ngalmin_;
     327 tdum(8) = ngalmax_;
     328 tdum(9) = nvalngal_;
     329 tdum(10) = hmass_.size();
     330 tdum(11) = tmass_.size();
     331 pos << PPFNameTag("SMDparam") << tdum;
     332
     333 pos << PPFNameTag("SMDhmdndm") << *hmdndm_;
     334 pos << PPFNameTag("SMDtirhmdndm") << *tirhmdndm_;
     335
     336 if(hmass_.size()>0) {
     337   for(unsigned int i=0;i<hmass_.size();i++) {
     338     sprintf(str,"SMDh%d",NGalFrIndex(i));
     339     pos << PPFNameTag(str) << hmass_[i];
     340   }
     341 }
     342
     343 if(tmass_.size()>0) {
     344   for(unsigned int i=0;i<tmass_.size();i++) {
     345     sprintf(str,"SMDt%d",NGalFrIndex(i));
     346     Histo hdum(tmass_[i]);
     347     pos << PPFNameTag(str) << hdum;
     348   }
     349 }
     350
     351}
     352
     353void SchechterMassDist::ReadPPF(string ppfname)
     354{
     355 Delete(); // On des-alloue si deja remplit!
     356
     357 char str[64];
     358 cout<<"SchechterMassDist::ReadPPF from "<<ppfname<<endl;
     359 PInPersist pis(ppfname.c_str());
     360
     361 TVector<r_8> tdum;
     362 pis >> PPFNameTag("SMDparam") >> tdum;
     363 sch_.SetParam(tdum(0),tdum(1),tdum(2));
     364 sch_.SetOutValue((unsigned short)(tdum(3)+0.1));
     365 massmin_ = tdum(4);
     366 massmax_ = tdum(5);
     367 nbinmass_ = int(tdum(6)+0.1);
     368 ngalmin_ = int(tdum(7)+0.1);
     369 ngalmax_ = int(tdum(8)+0.1);
     370 nvalngal_ = int(tdum(9)+0.1);
     371 unsigned int nhmass = (unsigned int)(tdum(10)+0.1);
     372 unsigned int ntmass = (unsigned int)(tdum(11)+0.1);
     373
     374 {
     375 Histo hdum;
     376 pis >> PPFNameTag("SMDhmdndm") >> hdum;
     377 hmdndm_ = new Histo(hdum);
     378 pis >> PPFNameTag("SMDtirhmdndm") >> hdum;
     379 tirhmdndm_ = new FunRan(hdum,false);
     380 }
     381
     382 if(nhmass>0) {
     383   for(unsigned int i=0;i<nhmass;i++) {
     384     sprintf(str,"SMDh%d",NGalFrIndex(i));
     385     Histo hdum;
     386     pis >> PPFNameTag(str) >> hdum;
     387     hmass_.push_back(hdum);
     388   }
     389 }
     390
     391 if(ntmass>0) {
     392   for(unsigned int i=0;i<ntmass;i++) {
     393     sprintf(str,"SMDt%d",NGalFrIndex(i));
     394     Histo hdum;
     395     pis >> PPFNameTag(str) >> hdum;
     396     FunRan fdum(hdum,false);
     397     tmass_.push_back(fdum);
     398   }
     399 }
     400
     401}
     402
     403
     404///////////////////////////////////////////////////////////
     405//***************** Fonctions de Check ******************//
     406///////////////////////////////////////////////////////////
     407
     408bool IsCompatible(Schechter& sch1,Schechter& sch2,double eps)
     409// on compare les differences a eps pres
     410{
     411  if(eps<=0.) eps=1.e-4;
     412  double nstar1,mstar1,alpha1;
     413  sch1.GetParam(nstar1,mstar1,alpha1);
     414  double nstar2,mstar2,alpha2;
     415  sch2.GetParam(nstar2,mstar2,alpha2);
     416
     417  if(fabs(nstar1-nstar2)>fabs(nstar1+nstar2)/2.*eps) return false;
     418  if(fabs(mstar1-mstar2)>fabs(mstar1+mstar2)/2.*eps) return false;
     419  if(fabs(alpha1-alpha2)>fabs(alpha1+alpha2)/2.*eps) return false;
     420  return true;
     421}
     422
    65423
    66424///////////////////////////////////////////////////////////
     
    98456  return f *d*d / 2.0e-28;
    99457}
     458
  • trunk/Cosmo/SimLSS/schechter.h

    r3196 r3320  
    55#include "genericfunc.h"
    66
     7namespace SOPHYA {
    78
    8 namespace SOPHYA {
     9class Histo;
     10class FunRan;
    911
    1012//-----------------------------------------------------------------------------------
     
    1315  Schechter(double nstar,double mstar,double alpha);
    1416  Schechter(Schechter& f);
     17  Schechter(void);
    1518  virtual ~Schechter(void);
     19
    1620  void SetOutValue(unsigned short outvalue=0);
     21  unsigned short GetOutValue(void);
     22  void SetParam(double nstar,double mstar,double alpha);
     23  void GetParam(double& nstar,double& mstar,double& alpha);
     24
    1725  virtual double operator() (double m);
     26
     27  double Integrate(double massmin,double massmax,int npt=100);
     28
    1829  virtual void Print(void);
     30
    1931protected:
    2032  double nstar_,mstar_,alpha_;
     
    2234};
    2335
     36//-----------------------------------------------------------------------------------
     37class SchechterMassDist : public AnyDataObj {
     38  friend class ObjFileIO<SchechterMassDist>;
     39public:
     40  SchechterMassDist(Schechter sch,double massmin,double massmax,int nbinmass);
     41  SchechterMassDist(void);
     42  virtual ~SchechterMassDist(void);
     43
     44  int GetMassLim(double& massmin,double& massmax);
     45  int SetNgalLim(int ngalmax,int ngalmin=1,unsigned long nalea=10000);
     46  int GetNgalLim(int& ngalmax,int& ngalmin);
     47  int GetNgalLim(void) {return nvalngal_;}
     48  Schechter GetSchechter(void) {return sch_;}
     49
     50  inline int IndexFrNGal(int ngal) {
     51    int i = ngal-ngalmin_;
     52    if(nvalngal_<1 || i<0) return -1;
     53    if(i>=nvalngal_) return -2; else return i;
     54  }
     55  inline int NGalFrIndex(int i) {
     56    if(nvalngal_<1 || i<0 || i>=nvalngal_) return -1;
     57    return ngalmin_+i;
     58  }
     59
     60  Histo GetHmDnDm(void) const;
     61  FunRan GetTmDnDm(void) const;
     62
     63  Histo GetHisto(int i) const;
     64  FunRan GetFunRan(int i) const;
     65
     66  double TirMass(int ngal);
     67
     68  void Print(void);
     69  void PrintStatus(void);
     70
     71  void WritePPF(string ppfname);
     72  void ReadPPF(string ppfname);
     73
     74protected:
     75 void Delete(void);
     76
     77 Schechter sch_;
     78 unsigned short sch_outvalue_;
     79
     80 double massmin_,massmax_;   int nbinmass_;
     81 int ngalmin_,ngalmax_,nvalngal_;
     82 unsigned long ntrial_dir, ntrial_tab;
     83
     84 Histo* hmdndm_;
     85 FunRan* tirhmdndm_;
     86
     87 vector<Histo> hmass_;
     88 vector<FunRan> tmass_;
     89};
     90
    2491} // Fin du namespace
     92
     93//-----------------------------------------------------------------------------------
     94bool IsCompatible(Schechter& sch1,Schechter& sch2,double eps=1.e-4);
    2595
    2696//-----------------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.