source: Sophya/trunk/Cosmo/SimLSS/cmvschdist.cc@ 3319

Last change on this file since 3319 was 3319, checked in by cmv, 18 years ago

commit pour memoire avant intro class SchechterMassDist cmv 03/09/2007

File size: 5.9 KB
Line 
1/* Distributions de masse pour n=1,2,3,... tirages */
2#include "sopnamsp.h"
3#include "machdefs.h"
4// Pour faire les histogrammes des distributions de masses
5// pour ntirages:
6// ex: on a un pixel qui a N galaxies, quelle est la distribution
7// statistique de masse dans un tel pixel ?
8// > cmvschdist -a -v -m 1e+7,1e+14 -n 140 -r 200 -N 100000
9#include <iostream>
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
13#include <math.h>
14#include <unistd.h>
15#include "timing.h"
16#include "histos.h"
17#include "ntuple.h"
18#include "srandgen.h"
19#include "perandom.h"
20
21#include "geneutils.h"
22#include "cosmocalc.h"
23#include "schechter.h"
24
25#include <vector>
26
27void usage(void);
28void usage(void)
29{
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
35 <<" -v : back verification of random trials"<<endl
36 <<" -a : init auto de l\'aleatoire"<<endl;
37}
38
39int main(int narg,char *arg[])
40{
41 double h75 = 0.71 / 0.75;
42 double nstar = 0.006*pow(h75,3.); //
43 double mstar = pow(10.,9.8/(h75*h75)); // MSol
44 double alpha = -1.37;
45 cout<<"h75= "<<h75<<" nstar= "<<nstar<<" mstar="<<mstar<<" alpha="<<alpha<<endl;
46
47 double xmin=1e8, xmax=1e13;
48 int npt = 100;
49 int ngmax = 200, ngmin = 1;
50 unsigned long long nalea = 10000;
51 bool verif = false;
52
53 char c;
54 while((c = getopt(narg,arg,"havm:N:n:r:")) != -1) {
55 switch (c) {
56 case 'm' :
57 sscanf(optarg,"%lf,%lf",&xmin,&xmax);
58 if(xmin<=0.) xmin=1e6;
59 if(xmax<=0.) xmax=xmin;
60 if(xmin>xmax) xmax=10.*xmin;
61 break;
62 case 'r' :
63 sscanf(optarg,"%d,%d",&ngmax,&ngmin);
64 if(ngmin<=0) ngmin=1;
65 if(ngmax<=0) ngmax=ngmin;
66 if(ngmin>ngmax) ngmin=ngmax;
67 break;
68 case 'n' :
69 sscanf(optarg,"%d",&npt);
70 if(npt<=0) npt = 100;
71 break;
72 case 'N' :
73 sscanf(optarg,"%llu",&nalea);
74 if(nalea<=0) nalea=10000;
75 break;
76 case 'v' :
77 verif = true;
78 break;
79 case 'a' :
80 Auto_Ini_Ranf(5);
81 break;
82 case 'h' :
83 default :
84 usage(); return -1;
85 }
86 }
87
88 double lnx1=log10(xmin), lnx2=log10(xmax), dlnx = (lnx2-lnx1)/npt;
89 cout<<"xmin="<<xmin<<" ("<<lnx1<<") xmax="<<xmax<<" ("<<lnx2<<"), dlnx="<<dlnx<<endl;
90 cout<<"npt="<<npt<<", nalea="<<nalea<<endl;
91 cout<<"ngmin="<<ngmin<<" ngmax="<<ngmax<<endl;
92 cout<<"verif="<<verif<<endl;
93
94
95 //-------m*dn/dm
96 cout<<"> Schechter m*dn/dm nstar="<<nstar<<" mstar="<<mstar<<" alpha="<<alpha<<endl;
97 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 }
113
114 //------- Random
115 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;
144 if(verif) {
145 cout<<"> Creating "<<nbhist<<" FunRan and Histos for back-verif"<<endl;
146 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();
152 vthisto.push_back(h);
153 sprintf(str,"th%d",i);
154 vthname.push_back(string(str));
155 }
156
157 cout<<"> Checking tirage"<<endl;
158 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);
163 }
164 }
165 PrtTim("End Random loop back-tirage");
166 }
167
168 //------- Ecriture ppf
169 cout<<"Ecriture"<<endl;
170 string tag = "cmvschdist.ppf";
171 POutPersist pos(tag);
172 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 if(vthisto[i].NEntries()>0) pos.PutObject(vthisto[i],vthname[i]);
181
182 return 0;
183}
184
185/*
186delobjs *
187openppf cmvschdist.ppf
188
189set n 70
190echo ${h70.vmax}
191
192set h h$n
193set th th$n
194
195disp $h
196disp $th "same red"
197
198n/plot $h.val%x ! ! "connectpoints"
199n/plot $th.val%x ! ! "nsta connectpoints same red"
200
201n/plot $h.log10(val)%x val>0. ! "connectpoints"
202n/plot $th.log10(val)%x val>0. ! "nsta connectpoints same red"
203
204c++exec \
205for(int i=0;i<$h.NBins();i++) \
206 if($h(i)>0.) { \
207 cout<<i<<" m="<<$h.BinCenter(i)<<" ("<<pow(10.,$h.BinCenter(i))<<") h="<<$h(i)<<endl; \
208 break; \
209} \
210for(int i=$h.NBins()-1;i>=0;i--) \
211 if($h(i)>0.) { \
212 cout<<i<<" m="<<$h.BinCenter(i)<<" ("<<pow(10.,$h.BinCenter(i))<<") h="<<$h(i)<<endl; \
213 break; \
214}
215
216# Compare evolution
217disp h200
218disp h100 "same red"
219disp h50 "same blue"
220disp h10 "same green"
221disp h1 "same yellow"
222*/
Note: See TracBrowser for help on using the repository browser.