00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "xsmmath.h"
00044 #include "xsmtypes.h"
00045 #include "glbvars.h"
00046 #include "stdio.h"
00047
00048 #include "bench.h"
00049 #include "regress.h"
00050
00051 #include "mathilbl.h"
00052
00053 #include <math.h>
00054
00055
00056
00057
00058
00059 double d_sa=0.;
00060 double d_pr=0.;
00061 double d_lbn=5.;
00062 int inter=1;
00063 gchar *filename=NULL;
00064 gchar *simname=NULL;
00065 int save=0;
00066
00067 long proper(long cl_link[], long label){
00068 if (cl_link[label]==label)
00069 return label;
00070 else
00071 return proper(cl_link, cl_link[label]);
00072 }
00073
00074 int SF(int line, int n_l, int sa){
00075 if (sa <=0) return 0;
00076 if (sa >0){
00077 int TW= n_l / sa;
00078 for (int s=0; s<sa; s++){
00079 if ( line==(s+1)*TW ) return -1;
00080 if ( line==s*TW+1 ) return 1;
00081 }
00082 }
00083 return 0;
00084 }
00085
00086
00087 void labeling(ZData *ZData_Ptr, int n_c, int n_l, long Nmax, long cl_link[], int sa, int step_cb){
00088
00089 long n_cluster=0 , site, siteL, siteT, siteLT, siteRT, min1, min2;
00090
00091 for (int line=1; line < n_l+1; line++) {
00092 ZData_Ptr->SetPtrT(1,line);
00093 for (int col = 1; col < n_c+1; col++){
00094 site = (long)ZData_Ptr->GetThis();
00095 siteL = (long)ZData_Ptr->GetThisL();
00096 siteT = (long)ZData_Ptr->GetThisT();
00097 siteRT = (long)ZData_Ptr->GetThisRT();
00098 if ( (SF(line, n_l, sa)==1) && (step_cb==0) ){
00099 siteT=0;
00100 siteRT=0;
00101 }
00102 if (site < 0)
00103 if ( (siteL==0) && (siteT==0) && (siteRT==0) ) {
00104 n_cluster++;
00105 ZData_Ptr->SetThis(n_cluster);
00106 cl_link[n_cluster]=n_cluster;
00107 }
00108 else {
00109 if (siteT==0) siteT=Nmax; else siteT=proper(cl_link, siteT);
00110 if (siteL==0) siteL=Nmax; else siteL=proper(cl_link, siteL);
00111 if (siteRT==0) siteRT=Nmax; else siteRT=proper(cl_link, siteRT);
00112 min1=MIN(siteT, siteL);
00113 min2=MIN(siteL, siteRT);
00114 ZData_Ptr->SetThis(MIN(min1, min2));
00115 site = (long)ZData_Ptr->GetThis();
00116 if ((siteT < Nmax) && (siteT > site)) cl_link[(long) ZData_Ptr->GetThisT()]=site;
00117 if ((siteL<Nmax) && (siteL > site)) cl_link[(long) ZData_Ptr->GetThisL()]=site;
00118 if ((siteRT<Nmax) && (siteRT> site)) cl_link[(long) ZData_Ptr->GetThisRT()]=site;
00119 }
00120 ZData_Ptr->IncPtrT();
00121 }
00122 }
00123 }
00124
00125 void proper_labeling(ZData *ZData_Ptr, int n_c, int n_l, long cl_link[]){
00126
00127 for (int line=1; line < n_l +1; line++) {
00128 ZData_Ptr->SetPtr(1,line);
00129 for (int col = 1; col < n_c +1; col++)
00130 ZData_Ptr->SetNext( proper(cl_link, (long)ZData_Ptr->GetThis()) );
00131 }
00132 }
00133
00134
00135 double deltaz(int sa, int n_l, int line){
00136
00137 int TW;
00138 if (sa <= 0) return 0;
00139 TW=(int) (n_l/sa);
00140 for (int s=0; s<sa; s++)
00141 if ((line > s*TW) && (line <= (s+1)*TW))
00142 return (double) (s*256);
00143 }
00144
00145 void sub_stepfunc(ZData *ZData_Ptr, int n_c, int n_l, int sa){
00146
00147 double dz;
00148 for (int line=1; line <= n_l; line++){
00149 dz=deltaz(sa, n_l, line);
00150 ZData_Ptr->SetPtr(1,line);
00151 for (int col=1; col <= n_c; col++)
00152 ZData_Ptr->SetNext(ZData_Ptr->GetThis()-dz);
00153 }
00154 }
00155
00156 void rescale(ZData *ZData_Ptr, double low, double high, int n_c, int n_l){
00157
00158 double skl;
00159
00160
00161 skl=256.;
00162 for (int line=1; line <= n_l; line++){
00163 ZData_Ptr->SetPtr(1,line);
00164 for (int col=1; col <= n_c; col++)
00165 ZData_Ptr->SetNext( (ZData_Ptr->GetThis()-low)/skl*(-1) );
00166 }
00167 }
00168
00169 void cluster_link_col(ZData *ZData_Ptr, int n_c, int n_l, long cl_link[]){
00170
00171 double zl, zr;
00172 for (int line=1; line <= n_l; line++){
00173 ZData_Ptr->SetPtr(1,line);
00174 zl=ZData_Ptr->GetThis();
00175 ZData_Ptr->SetPtr(n_c+1,line);
00176 zr=ZData_Ptr->GetThis();
00177 if (zl < zr) cl_link[(long)zr]=(long)zl;
00178 if (zl > zr) cl_link[(long)zl]=(long)zr;
00179 }
00180 }
00181
00182 void cluster_link_line(ZData *ZData_Ptr, int n_c, int n_l, long cl_link[]){
00183
00184 double zo, zu;
00185 for (int col=1; col <= n_c; col++){
00186 ZData_Ptr->SetPtr(col,1);
00187 zo=ZData_Ptr->GetThis();
00188 ZData_Ptr->SetPtr(col, n_l+1);
00189 zu=ZData_Ptr->GetThis();
00190 if (zo < zu) cl_link[(long)zu]=(long)zo;
00191 if (zo > zu) cl_link[(long)zo]=(long)zu;
00192 }
00193 }
00194
00195 void LoHi(ZData *ZData_Ptr, int n_c, int n_l, double *p_low, double *p_high){
00196
00197 ZData_Ptr->SetPtr(1,1);
00198 *p_low=ZData_Ptr->GetThis();
00199 *p_high=ZData_Ptr->GetThis();
00200 for (int line=1; line <= n_l; line++){
00201 ZData_Ptr->SetPtr(1,line);
00202 for (int col=1; col <= n_c; col++){
00203 if (ZData_Ptr->GetThis() < *p_low) *p_low=ZData_Ptr->GetThis();
00204 if (ZData_Ptr->GetThis() > *p_high) *p_high=ZData_Ptr->GetThis();
00205 ++(*ZData_Ptr);
00206 }
00207 }
00208 }
00209
00210
00211 gboolean IslandLabl(MATHOPPARAMS){
00212
00213 double high, low, skl, z, site;
00214 int n_lines=Src->mem2d->GetNy(), n_cols=Src->mem2d->GetNx();
00215 long Nmax=n_lines*n_cols/3;
00216 int sa=0, pr, sf, sz[Nmax], lbn, sim_nr;
00217 long size[Nmax], n_stable=0 , n_monomer=0, amount=0;
00218 long cl_link[Nmax];
00219 ZData *SrcZ, *HIdx, *DstZ;
00220 FILE *filePtr;
00221 gchar *simfile;
00222
00223 Mem2d HIndex(n_cols+3, n_lines+3, ZD_SHORT);
00224
00225
00226
00227
00228 for (long j=0; j < Nmax+1; j++){
00229 cl_link[j]=0; sz[j]=0; size[j]=0;
00230 }
00231 SrcZ = Src->mem2d->data;
00232 DstZ = Dest->mem2d->data;
00233 HIdx = HIndex.data;
00234
00235 Dest->data.s.nx=Dest->data.s.nx +3;
00236 Dest->data.s.ny=Dest->data.s.ny +3;
00237 Dest->mem2d->Resize(Dest->data.s.nx, Dest->data.s.ny);
00238
00239
00240
00241 PI_DEBUG (DBG_L2, "F2D_IslandLabl");
00242
00243 if (inter==1){
00244
00245 gchar *txt1 = g_strdup_printf("Periodische RB: ohne(-1), x+y(0), x(1)");
00246 gapp->ValueRequest("Enter Value", "RB", txt1, gapp->xsm->Unity, -1., 1., "g", &d_pr);
00247 g_free(txt1);
00248
00249 gchar *txt2 = g_strdup_printf("Stufenanzahl des Orginal-Substrates");
00250 gapp->ValueRequest("Enter Value", "sa", txt2, gapp->xsm->Unity, 0.,512., "g", &d_sa);
00251 g_free(txt2);
00252 }
00253 sa= (int) d_sa;
00254 pr= (int) d_pr;
00255
00256
00257
00258 for (int line=0; line < Src->mem2d->GetNy(); line++) {
00259 SrcZ->SetPtr(0,line);
00260 HIdx->SetPtr(1,line+1);
00261 for (int col = 0; col < Src->mem2d->GetNx(); col++)
00262 HIdx->SetNext( SrcZ->GetNext() );
00263 }
00264
00265
00266
00267 LoHi(HIdx, n_cols, n_lines, &low, &high);
00268 skl = high-low;
00269 PI_DEBUG (DBG_L2, "F2D_IslandLabl" << std::endl << high << " " << low);
00270 rescale(HIdx, low, high, n_cols, n_lines);
00271
00272
00273
00274 for (int line=1; line <= n_lines; line++){
00275 HIdx->SetPtr(1,line);
00276 z=HIdx->GetThis();
00277 HIdx->SetPtr(n_cols+1,line);
00278 HIdx->SetThis(z);
00279 }
00280
00281 for (int col=1; col <= n_cols; col++){
00282 HIdx->SetPtr(col, 1);
00283 z=HIdx->GetThis();
00284 HIdx->SetPtr(col, n_lines+1);
00285 HIdx->SetThis(z);
00286 }
00287
00288
00289 labeling(HIdx, n_cols+1, n_lines+1, Nmax, cl_link, sa, 0);
00290
00291
00292
00293 if (pr > -1){
00294 cluster_link_col(HIdx, n_cols, n_lines, cl_link);
00295 if (pr == 0)
00296 cluster_link_line(HIdx, n_cols, n_lines, cl_link);
00297 }
00298
00299
00300 proper_labeling(HIdx, n_cols+1, n_lines+1, cl_link);
00301
00302
00303
00304
00305
00306
00307
00308 for (int line=1; line <= n_lines; line++){
00309 HIdx->SetPtr(1,line);
00310 for (int col=1; col <= n_cols; col++){
00311 if (HIdx->GetThis()>0) size[(int)HIdx->GetThis()]++;
00312 ++(*HIdx);
00313 }
00314 }
00315
00316
00317 for (int j=1; j<=Nmax; j++){
00318 if (size[j]>1) n_stable++;
00319 if (size[j]==1) n_monomer++;
00320 }
00321
00322
00323 for (int j=1; j<=Nmax; j++)
00324 amount=amount+size[j];
00325
00326 double d_n_stable= (double) n_stable;
00327 double d_n_monomer= (double) n_monomer;
00328 double d_amount= (double) amount;
00329 double d_n_stable_tr=(double) n_stable / ( (double) (n_lines * n_cols) ) *1000000;
00330
00331 std::cout << "\n\n************************Statistik************************";
00332 std::cout << "\n# stabile Inseln: " << n_stable << " rel. Fehler: " << 1/sqrt(n_stable) <<"\n";
00333 printf("\t entspricht (%.2f" , (double) n_stable / ( (double) (n_lines * n_cols) ) *1000000);
00334 printf(" +- %.2f) / 10^6 sites" , (double) n_stable / ( (double) (n_lines * n_cols) )*1000000 * 1/sqrt(n_stable));
00335 std::cout << "\n# Monomere: " << n_monomer << " rel. Fehler: " << 1/sqrt(n_monomer);
00336 std::cout << "\n Bedeckung: " << amount << " rel. Fehler: " << 1/sqrt(amount) << "\n";
00337 std::cout << "*********************************************************\n\n";
00338 std::cout << "Save: " << save << "\n";
00339
00340
00341
00342 if (save==1){
00343 lbn= (int) d_lbn;
00344 simfile = g_strdup(strrchr(Src->data.ui.name,'/')+1);
00345 std::cout << "\n" << simfile << "\n";
00346 std::cout << "\n" << g_strndup(simfile+lbn, strlen(simfile)-lbn-4) << "\n";
00347 sim_nr=atoi( (const char *) g_strndup(simfile+lbn, strlen(simfile)-lbn-4));
00348 if ( (filePtr=fopen( (const char *) filename, "a+")) == NULL)
00349 printf("File could not be opened\n");
00350 else {
00351 fprintf(filePtr, "%.0f\t%.3f\t", d_n_stable, 1/sqrt(d_n_stable) );
00352 fprintf(filePtr, "%.1f\t", d_n_stable_tr);
00353 fprintf(filePtr, "%.1f\t", d_n_stable_tr * 1/sqrt(d_n_stable) );
00354 fprintf(filePtr, "%.0f\t", d_n_monomer);
00355 fprintf(filePtr, "%.3f\t", 1/sqrt(d_n_monomer) );
00356 fprintf(filePtr, "%.0f\t", d_amount);
00357 fprintf(filePtr, "%.5f\t", 1/sqrt(d_amount) );
00358 fprintf(filePtr, "%d\n", sim_nr );
00359 printf("saving data\n");
00360 fclose(filePtr);
00361 printf("closing file\n");
00362 }
00363 }
00364
00365
00366
00367
00368
00369 Dest->mem2d->ConvertFrom(&HIndex, 0, 0, 0, 0, Dest->mem2d->GetNx(),Dest->mem2d->GetNy());
00370
00371 return MATH_OK;
00372 }
00373
00374
00375
00376 gboolean StepFlaten(MATHOPPARAMS){
00377
00378 int n_lines=Src->mem2d->GetNy(), n_cols=Src->mem2d->GetNx(), sa;
00379 ZData *SrcZ, *HIdx, *DstZ;
00380
00381 Mem2d HIndex(n_cols+1, n_lines+1, ZD_SHORT);
00382
00383
00384 SrcZ = Src->mem2d->data;
00385 DstZ = Dest->mem2d->data;
00386 HIdx = HIndex.data;
00387
00388
00389
00390 for (int line=0; line < Src->mem2d->GetNy(); line++) {
00391 SrcZ->SetPtr(0,line);
00392 HIdx->SetPtr(1,line+1);
00393 for (int col = 0; col < Src->mem2d->GetNx(); col++)
00394 HIdx->SetNext( SrcZ->GetNext() );
00395 }
00396
00397 if (inter==1){
00398
00399 gchar *txt = g_strdup_printf("Stepamount: ");
00400 gapp->ValueRequest("Enter Value", "Stepamount", txt, gapp->xsm->Unity, 1., 512., "g", &d_sa);
00401 g_free(txt);
00402 }
00403 sa= (int) d_sa;
00404
00405
00406 if (sa > 0) sub_stepfunc(HIdx, n_cols, n_lines, sa);
00407
00408
00409 Dest->mem2d->ConvertFrom(&HIndex, 1, 1, 0, 0, Dest->mem2d->GetNx(),Dest->mem2d->GetNy());
00410
00411 return MATH_OK;
00412 }
00413
00414 gboolean KillStepIslands(MATHOPPARAMS){
00415
00416 double high, low, skl, z, site;
00417 int n_lines=Src->mem2d->GetNy(), n_cols=Src->mem2d->GetNx();
00418 long Nmax=n_lines*n_cols/3;
00419 int pr=1, sa, sf, sz[Nmax], amount=0;
00420 long cl_link[Nmax];
00421 ZData *SrcZ, *HIdx, *DstZ, *HIdxTemp;
00422 FILE *filePtr;
00423
00424 Mem2d HIndex(n_cols+1, n_lines+1, ZD_SHORT);
00425 Mem2d HIndexTemp(n_cols+3, n_lines+3, ZD_SHORT);
00426
00427
00428 for (long j=0; j < Nmax+1; j++){
00429 cl_link[j]=0; sz[j]=0;
00430 }
00431 SrcZ = Src->mem2d->data;
00432 DstZ = Dest->mem2d->data;
00433 HIdx = HIndex.data;
00434 HIdxTemp = HIndexTemp.data;
00435
00436
00437
00438 for (int line=0; line < Src->mem2d->GetNy(); line++) {
00439 SrcZ->SetPtr(0,line);
00440 HIdxTemp->SetPtr(1,line+1);
00441 for (int col = 0; col < Src->mem2d->GetNx(); col++)
00442 HIdxTemp->SetNext( SrcZ->GetNext() );
00443 }
00444
00445
00446 for (int line=0; line < Src->mem2d->GetNy(); line++) {
00447 SrcZ->SetPtr(0,line);
00448 HIdx->SetPtr(1,line+1);
00449 for (int col = 0; col < Src->mem2d->GetNx(); col++)
00450 HIdx->SetNext( SrcZ->GetNext() );
00451 }
00452
00453 if (inter==1){
00454
00455 gchar *txt1 = g_strdup_printf("Periodische RB: ohne(-1), x+y(0), x(1)");
00456 gapp->ValueRequest("Enter Value", "RB", txt1, gapp->xsm->Unity, -1., 1., "g", &d_pr);
00457 g_free(txt1);
00458
00459 gchar *txt2 = g_strdup_printf("Stepamount: ");
00460 gapp->ValueRequest("Enter Value", "Stepamount", txt2, gapp->xsm->Unity, 1., 256., "g", &d_sa);
00461 g_free(txt2);
00462 }
00463 sa= (int) d_sa;
00464 pr= (int) d_pr;
00465
00466
00467 LoHi(HIdxTemp, n_cols, n_lines, &low, &high);
00468 skl = high-low;
00469 std::cout << "F2D_IslandLabl" << std::endl << high << " " << low << std::endl;
00470 rescale(HIdxTemp, low, high, n_cols, n_lines);
00471
00472
00473
00474 for (int line=1; line <= n_lines; line++){
00475 HIdxTemp->SetPtr(1,line);
00476 z=HIdxTemp->GetThis();
00477 HIdxTemp->SetPtr(n_cols+1,line);
00478 HIdxTemp->SetThis(z);
00479 }
00480
00481 for (int col=1; col <= n_cols; col++){
00482 HIdxTemp->SetPtr(col, 1);
00483 z=HIdxTemp->GetThis();
00484 HIdxTemp->SetPtr(col, n_lines+1);
00485 HIdxTemp->SetThis(z);
00486 }
00487
00488
00489 labeling(HIdxTemp, n_cols+1, n_lines+1, Nmax, cl_link, sa, 0);
00490
00491
00492
00493 if (pr > -1){
00494 cluster_link_col(HIdxTemp, n_cols, n_lines, cl_link);
00495 if (pr == 0)
00496 cluster_link_line(HIdxTemp, n_cols, n_lines, cl_link);
00497 }
00498
00499
00500 proper_labeling(HIdxTemp, n_cols+1, n_lines+1, cl_link);
00501
00502
00503 if (sa>0){
00504 for (int line=1; line <= n_lines; line++){
00505 HIdxTemp->SetPtr(1,line);
00506 sf=SF(line, n_lines, sa);
00507 for (int col=1; col <= n_cols; col++){
00508 if (sf==-1) sz[(int)HIdxTemp->GetThis()]=1;
00509 HIdxTemp->IncPtrT();
00510 }
00511 }
00512 sz[0]=0;
00513 }
00514
00515
00516
00517 for (int line=1; line <= n_lines; line++){
00518 HIdxTemp->SetPtr(1,line);
00519 HIdx->SetPtr(1,line);
00520 for (int col=1; col <= n_cols; col++){
00521 z=HIdx->GetThis();
00522 if (sz[(int)HIdxTemp->GetThis()]==1){
00523 amount++;
00524 HIdx->SetThis(z-256.);
00525 }
00526 ++(*HIdxTemp);
00527 ++(*HIdx);
00528 }
00529 }
00530
00531
00532 double d_amount= (double) amount;
00533 std::cout << "\n\n************************Statistik************************";
00534 std::cout << "\nBedeckung an Stufen: " << amount << " rel. Fehler: " << 1/sqrt(amount) <<"\n";
00535 std::cout << "*********************************************************\n\n";
00536 std::cout << "Save: " << save << "\n";
00537
00538
00539
00540 if (save==1){
00541 if ( (filePtr=fopen( (const char *) filename, "a+")) == NULL)
00542 printf("File could not be opened\n");
00543 else {
00544 fprintf(filePtr, "%.0f\t", d_amount);
00545 fprintf(filePtr, "%.5f\t", 1/sqrt(d_amount) );
00546 printf("saving data\n");
00547 fclose(filePtr);
00548 printf("closing file\n");
00549 }
00550 }
00551
00552 Dest->mem2d->ConvertFrom(&HIndex, 1, 1, 0, 0, Dest->mem2d->GetNx(),Dest->mem2d->GetNy());
00553
00554 return MATH_OK;
00555 }