00001
00006
00007
00008
00009 #include "dyn.h"
00010
00011 #ifndef TOOLBOX
00012 #else
00013
00014 local real get_primary_mass(dyn *b, bool S_flag) {
00015
00016 real m = -1;
00017 if(b->is_leaf()) {
00018 m = b->get_mass();
00019 }
00020 else if(!S_flag) {
00021 for_all_leaves(dyn, b, bi) {
00022 m = max(m, bi->get_mass());
00023 }
00024 }
00025
00026 return m;
00027 }
00028
00029 local dyn* cut_randomsample(dyn * b, int nd, real mmin, bool S_flag) {
00030
00031 int nb=0;
00032 for_all_daughters(dyn, b, bi) {
00033 if(get_primary_mass(bi, S_flag)>mmin)
00034 nb++;
00035 }
00036 real paccept = 1;
00037 if(nd>=0)
00038 paccept = real(nd)/real(nb);
00039 PRC(nb);PRC(nd);PRL(paccept);
00040
00041 real mtot=0;
00042 dyn *d = new dyn();
00043 dyn *bj=NULL;
00044 int ndone = 0;
00045 for_all_daughters(dyn, b, bi) {
00046 ndone++;
00047
00048 real r = randinter(0, 1);
00049
00050 if(get_primary_mass(bi, S_flag)>mmin && paccept>=r) {
00051 bi->pretty_print_node(cerr);
00052
00053 if(bi != NULL && bi->get_younger_sister()!= NULL) {
00054 bj = bi->get_younger_sister();
00055 detach_node_from_general_tree(*bi);
00056
00057
00058
00059
00060 add_node(*bi, *d);
00061 mtot += bi->get_mass();
00062 if(bj->get_elder_sister()!=NULL) {
00063 bi = bj->get_elder_sister();
00064 }
00065 else {
00066 cerr << "no elder sister from younger sister in cutrandomsample"
00067 <<endl;
00068 bi = bj;
00069
00070 }
00071 }
00072 }
00073 }
00074 cerr << "\n# done: " << ndone << endl;
00075
00076
00077
00078
00079 PRC(mtot);
00080 PRC(b->get_mass());
00081
00082
00083
00084
00085 PRL(mtot);
00086 d->set_mass(mtot);
00087
00088 d->pretty_print_node(cerr);
00089 cerr << endl;
00090
00091 return d;
00092 }
00093
00094 void main(int argc, char ** argv)
00095 {
00096 bool S_flag = false;
00097 bool s_flag = false;
00098 bool leave_unscaled = true;
00099 bool s = false;
00100 real mmin = 0;
00101 int nd = -1;
00102
00103 check_help();
00104
00105 int input_seed, actual_seed;
00106 extern char *poptarg;
00107 int c;
00108 char* param_string = "m:n:Ss:u";
00109
00110 while ((c = pgetopt(argc, argv, param_string)) != -1)
00111 switch(c) {
00112
00113 case 'm': mmin = atof(poptarg);
00114 break;
00115 case 'n': nd = atoi(poptarg);
00116 break;
00117 case 'u': leave_unscaled = !leave_unscaled;
00118 break;
00119 case 'S': S_flag = !S_flag;
00120 break;
00121 case 's': s_flag = !s_flag;
00122 input_seed = atoi(poptarg);
00123 break;
00124 case '?': params_to_usage(cerr, argv[0], param_string);
00125 get_help();
00126 exit(1);
00127 }
00128
00129 dyn *b = get_dyn(cin);
00130
00131 b->log_history(argc, argv);
00132
00133 if (s_flag == false) input_seed = 0;
00134 actual_seed = srandinter(input_seed);
00135
00136 char seedlog[64];
00137 sprintf(seedlog, " random number generator seed = %d",actual_seed);
00138 b->log_comment(seedlog);
00139
00140 if(!leave_unscaled) {
00141 mmin = b->get_starbase()->conv_m_star_to_dyn(mmin);
00142 PRL(mmin);
00143 }
00144
00145 dyn *d = cut_randomsample(b, nd, mmin, S_flag);
00146 putrq(b->get_log_story(), "initial_mass", b->get_mass());
00147 if(rmq(b->get_log_story(), "initial_mass")) {
00148 cerr << "initial_mass" << endl;
00149 }
00150 d->set_log_story(b->get_log_story());
00151
00152 real mtot = d->get_mass();
00153 PRL(mtot);
00154 d->get_starbase()->print_stellar_evolution_scaling(cerr);
00155 real old_r_vir= b->get_starbase()->conv_r_star_to_dyn(1);
00156 real old_t_vir= b->get_starbase()->conv_t_star_to_dyn(1);
00157 d->get_starbase()->set_stellar_evolution_scaling(mtot,
00158 old_r_vir,
00159 old_t_vir);
00160 d->get_starbase()->print_stellar_evolution_scaling(cerr);
00161
00162
00163 put_dyn(cout, *d);
00164 }
00165
00166 #endif
00167
00168
00169
00170
00171
00172 #if 0
00173
00174 real mtot = 0;
00175 real dmtot = 0;
00176 for_all_daughters(dyn, d, di) {
00177 mtot += di->get_mass();
00178 dmtot += di->get_starbase()->conv_m_star_to_dyn(di->get_mass());
00179 }
00180 PRC(mtot);PRL(dmtot);
00181
00182 real eps = 0;
00183 bool c_flag, e_flag, e, m_flag, q_flag, r_flag;
00184 c_flag = e_flag = m_flag = q_flag = r_flag = true;
00185 real m = 1;
00186 real q = 0.5;
00187 real r = 1;
00188
00189
00190 d->get_starbase()->print_stellar_evolution_scaling(cerr);
00191
00192 real old_r_vir= b->get_starbase()->conv_r_star_to_dyn(1);
00193 real old_t_vir= b->get_starbase()->conv_t_star_to_dyn(1);
00194 d->get_starbase()->set_stellar_evolution_scaling(mtot/dmtot,
00195 old_r_vir,
00196 old_t_vir);
00197 d->get_starbase()->print_stellar_evolution_scaling(cerr);
00198
00199 #endif