void spiral(struct node *root,int level,bool flag) { if(root == NULL) return ; if(level == 1) { printf("%d\t",root->data); return; } else if(flag == true && level >1) { spiral(root->left,level-1,flag); spiral(root->right,level-1,flag); } else if(flag == false && level >1) { spiral(root->right,level-1,flag); spiral(root->left,level-1,flag); } }
void drawSpiral() { int b, sx, sy, ex, ey, px, py; int c=cur_color; float r; setdrawmode(XOR_MODE); getmousepos(&b,&sx,&sy); set_color(15-c); ex = px = sx;ey = py = sy; if(b==1) hidemouseptr(); while(b == 1) { if(px != ex || py != ey) { set_color(cur_color); spiral((px+sx)/2, (py+sy)/2, r); px = ex; py = ey; r = (sx<px)?(px-sx)/2:(sx-px)/2; spiral((px+sx)/2, (py+sy)/2, r); } getmousepos(&b,&ex,&ey); } set_color(c); setdrawmode(COPY_MODE); r = (sx<px)?(px-sx)/2:(sx-px)/2; spiral((px+sx)/2, (py+sy)/2, r); showmouseptr(); }
void spiral(int side, int offset) { /* * Base case: empty square */ if (side == 0) return; /* * Base case: unit square */ if (side == 1) { printf("(%d, %d)\n", offset, offset); return; } /* * Outter square */ int i, j = 0; for (i = 0; i < side - 1; i++) printf("(%d, %d)\n", offset + i, offset + j); for (j = 0; j < side - 1; j++) printf("(%d, %d)\n", offset + i, offset + j); for (; i > 0; i--) printf("(%d, %d)\n", offset + i, offset + j); for (; j > 0; j--) printf("(%d, %d)\n", offset + i, offset + j); /* * Recurse */ spiral(side - 2, offset + 1); }
vector<int> spiralOrder(vector<vector<int> > &matrix) { if (matrix.size() == 0) return vector<int>(); vector<int>result; int m = matrix.size() - 1; int n = matrix[0].size() - 1; //注意m,n的范围 spiral(matrix, result, 0, 0, m, n); return result; }
int main(void) { int arr[20][20],r,c,i,j; scanf("%d %d",&r,&c); for(i=0;i<r;i++) for(j=0;j<c;j++) scanf("%d",&arr[i][j]); spiral(r,c,arr); return 0; }
void main() { setlocale(LC_CTYPE,"rus"); massive_size(); massive_initialisation(); spiral(); massive_output(); getch(); massive_delete(); }
void FilePrinter::print(int** array, int length) { /* std::string h; std::cout << "Specify filename to print into" << std::endl; std::cin >> h; std::streambuf *psbuf, *backup; std::ofstream filestr; filestr.open(h.c_str()); backup = std::cout.rdbuf(); // back up cout's streambuf psbuf = filestr.rdbuf(); // get file's streambuf std::cout.rdbuf(psbuf); // assign streambuf to cout */ spiral(array, length); // std::cout.rdbuf(backup); // restore cout's original streambuf // filestr.close(); }
void printLevelOrder(struct node * root) { int height = getHeight(root); printf("H=%d\t",height); bool flag = false; for(int i = 1; i<= height;i++) { if(i%2 != 0) flag = true; else flag = false; spiral(root,i,flag); } }
int main() { srand(time(NULL)); int count =25; for(int i=0;i<5;i++){ for(int j=4;j!=-1;j--){ array[i][j]=count--; } } printt(); selectionsort(); printf("after selection sort\n"); printt(); spiral(); printf("after spiral\n"); printtt(); return 0; }
void display (BST * root, Type t) { switch (t) { case INORDER: printf ("\ndisplay :: INORDER \n"); inOrder (root); break; case POSTORDER: printf ("\ndisplay :: POSTORDER \n"); postOrder (root); break; case PREORDER: printf ("\ndisplay :: PREORDDER \n"); preOrder (root); break; case BFS: printf ("\ndisplay ::BFS \n"); bfs (root); break; case SPIRAL: printf ("\ndisplay ::EBFS \n"); spiral (root); break; case INORDERITER: printf ("\n display :: INORDERITER\n"); inOrderIter (root); break; case PREORDERITER: printf ("\n display :: INORDERITER\n"); preOrderIter (root); break; case POSTORDERITER: printf ("\n display :: INORDERITER\n"); postOrderIter(root); break; default: printf ("\ndisplay :: default \n"); break; } }
int main(int argc, char* argv[]){ /* N is the array width and legnth. * initial_value is the number that will be * at the center of the array. */ int N, initial_value; int *array; printf("Input the array size please: "); scanf("%d",&N); printf("Input the initial value please: "); scanf("%d",&initial_value); array=(int*)malloc(N*N*sizeof(int)); spiral(N, initial_value, array); print(array, N); free(array); return 0; }
void spiral(vector<vector<int>>&matrix, vector<int>&result,int a, int b, int m, int n) { if (a > m || b > n) return; if (a == m){ for (int i = b; i <= n; i++) result.push_back(matrix[a][i]); return; } if (b == n){ for (int j = a; j <= m; j++) result.push_back(matrix[j][n]); return; } for (int i = b; i <= n; i++) result.push_back(matrix[a][i]); for (int j = a + 1; j <= m; j++) result.push_back(matrix[j][n]); for (int p = n - 1; p >= b; p--) result.push_back(matrix[m][p]); for (int q = m - 1; q >= a + 1; q--) result.push_back(matrix[q][b]); spiral(matrix, result, a + 1, b + 1, m - 1, n - 1); }
/* Launches one of those fancy effects. */ void launch_effect(int effect) { switch (effect) { case 0: /* Lights all the layers one by one. */ load_bar(1000); break; case 1: /* A pixel bouncing randomly around. */ boing_boing(150, 500, 0x03, 0x01); break; case 2: /* Randomly fill and empty the cube. */ fill(0x00); random_filler(100, 1, 500, 1); random_filler(100, 1, 500, 0); break; case 3: /* Send voxels randomly back and forth the z-axis. */ send_voxels_rand_z(150, 500, 2000); break; case 4: /* Spinning spiral */ spiral(1, 75, 1000); break; case 5: /* A coordinate bounces randomly around the cube. For every position * the status of that voxel is toggled. */ boing_boing(150, 500, 0x03, 0x02); break; case 6: /* Random raindrops */ rain(40, 1000, 500, 500); break; case 7: /* Snake: a snake randomly bounce around the cube. */ boing_boing(150, 500, 0x03, 0x03); break; case 8: /* Spinning plane */ spinning_plane(1, 50, 1000); break; case 9: /* Set x number of random voxels, delay, unset them. x increases * from 1 to 32 and back to 1. */ random_2(48); break; case 10: /* Set then unset all 64 voxels in a random order. */ random_filler2(200, 1); delay_ms(2000); random_filler2(200, 0); delay_ms(1000); break; case 11: /* Bounce a plane up and down all the directions. */ fly_plane('z', 1, 1000); delay_ms(2000); fly_plane('y', 1, 1000); delay_ms(2000); fly_plane('x', 1, 1000); delay_ms(2000); fly_plane('z', 0, 1000); delay_ms(2000); fly_plane('y', 0, 1000); delay_ms(2000); fly_plane('x', 0, 1000); delay_ms(2000); break; case 12: /* Fade in and out at low framerate. */ blinky2(); break; case 13: /* Random walk */ random_walk(4, 250, 1000); break; case 14: /* Spinning square */ spinning_square(1, 50, 1000); break; } }
int main() { int i,j,m,n; int array[100][100]; printf("Enter the value of m \n"); scanf("%d",&m); printf("Enter the value of n \n"); scanf("%d",&n); printf("Accepting the elements of the matrix\n"); for(i=0; i<m; i++) { for(j=0; j<n; j++) { array[i][j]=rand()%(m*n); //scanf("%d",&array[i][j]); } } printf(" The matrix you have entered is shown below \n"); for(i=0; i<m; i++) { for(j=0; j<n; j++) { printf("%5d",array[i][j]); } printf("\n"); } if (m==1 && n==1) { printf("The Spiral order the matrix is shown below :\n"); printf(" %d \n",array[0][0]); printf("The matrix contains only only one element\n"); return 0; } if(m==1 && n !=1) { j=0; printf("The Spiral order the matrix is shown below :\n"); while(j<n) { printf("%d ",array[0][j]); j++; } printf("\nThis is a Row Matrix and number of elements are %d \n",(1*n)); return 0; } if (m!=1 && n==1) { i=0; printf("The Spiral order the matrix is shown below :\n"); while(i<m) { printf("%d \n",array[i][0]); i++; } printf("This is a Column Matrix and number of elements are %d \n",(m*1)); return 0; } else spiral(array,m,n); return 0; }
int main(){ int arr[4][4]= {{1,2,3,10},{4,5,6,11},{7,8,9,12},{13,14,15,16}}; spiral(arr,4); }
void display_loop(){ // mcuf_serial_mode(); mode = setjmp(newmode_jmpbuf); #ifdef JOYSTICK_SUPPORT // in case we get here via mode jump, we (re)enable joystick queries waitForFire = 1; #endif oldOldmode = oldMode; #ifdef JOYSTICK_SUPPORT waitForFire = 1; #endif for(;;){ #ifndef MENU_SUPPORT clear_screen(0); #endif oldMode = mode; switch(mode++) { #ifdef ANIMATION_SCROLLTEXT case 1: scrolltext(scrolltext_text); #ifdef RANDOM_SUPPORT { char a[28]; sprintf(a,"</# counter == %lu ", (unsigned long)percnt_get(&g_reset_counter, &g_reset_counter_idx)); scrolltext(a); } #endif #endif #ifdef ANIMATION_TIME #ifndef ANIMATION_SCROLLTEXT case 1: #endif time_anim(); break; #else #ifdef ANIMATION_SCROLLTEXT break; #endif #endif #ifdef ANIMATION_SPIRAL # ifndef SPIRAL_DELAY # define SPIRAL_DELAY 5 # endif case 2: spiral(SPIRAL_DELAY); break; #endif #ifdef ANIMATION_JOERN1 case 3: joern1(); break; #endif #ifdef ANIMATION_SNAKE case 4: snake_animation(); break; #endif #ifdef ANIMATION_CHECKERBOARD case 5: checkerboard(20); break; #endif #ifdef ANIMATION_FIRE case 6: fire(); break; #endif #ifdef ANIMATION_TIME case 7: time_anim(); break; #endif #ifdef ANIMATION_MATRIX case 8: matrix(); break; #endif #ifdef ANIMATION_RANDOM_BRIGHT case 9: random_bright(30); break; #endif #ifdef ANIMATION_STONEFLY case 10: stonefly(); break; #endif #ifdef ANIMATION_GAMEOFLIFE case 11: gameoflife(); break; #endif #ifdef ANIMATION_FLYINGDOTS case 12: flyingdots(); break; #endif #ifdef ANIMATION_BREAKOUT case 13: breakout_demo(); break; #endif #ifdef ANIMATION_MHERWEG case 14: mherweg(); break; #endif #ifdef ANIMATION_MOIRE case 15: moire(); break; #endif #ifdef ANIMATION_TIME case 16: time_anim(); break; #endif #ifdef ANIMATION_LTN_ANT case 17: ltn_ant(); break; #endif #ifdef ANIMATION_LABORLOGO case 18: laborlogo(); break; #endif #ifdef ANIMATION_AMPHIBIAN case 19: amphibian(); break; #endif #ifdef ANIMATION_LOGO_OOS case 20: logo_OutOfSpec(); break; #endif #ifdef ANIMATION_FAIRYDUST case 21: fairydust(); break; #endif #ifdef ANIMATION_PLASMA case 22: plasma(); break; #endif #ifdef ANIMATION_PSYCHEDELIC case 23: psychedelic(); break; #endif #ifdef ANIMATION_BLACKHOLE case 24: blackhole(); break; #endif #ifdef ANIMATION_SQUARES case 25: squares(); break; #endif #ifdef ANIMATION_DNA case 26: dna(); break; #endif #ifdef ANIMATION_TESTS case 31: test_level(1, false); break; case 32: test_level(2, false); break; case 33: test_level(3, false); break; case 35: test_palette(false); test_palette2(false); break; #endif #ifdef SMALLANIMATION_ROWWALK case 36: rowwalk(SMALLANIMATION_ROWWALK_COUNT,SMALLANIMATION_ROWWALK_SPEED); break; #endif #ifdef SMALLANIMATION_COLWALK case 37: colwalk(SMALLANIMATION_COLWALK_COUNT,SMALLANIMATION_COLWALK_SPEED); break; #endif #ifdef SMALLANIMATION_COLBOUNCE case 38: colbounce(SMALLANIMATION_COLBOUNCE_COUNT,SMALLANIMATION_COLBOUNCE_SPEED); break; #endif #ifdef SMALLANIMATION_ROWBOUNCE case 39: rowbounce(SMALLANIMATION_ROWBOUNCE_COUNT,SMALLANIMATION_ROWBOUNCE_SPEED); break; #endif #ifdef MENU_SUPPORT case 0xFDu: mode = 1; break; case 0xFEu: menu(); mode = oldOldmode; break; #else case 0xFDu: #ifdef JOYSTICK_SUPPORT if (JOYISFIRE) mode = 0xFEu; else #endif mode = 1; break; case 0xFEu: #ifdef JOYSTICK_SUPPORT waitForFire = 0; // avoid circular jumps while (JOYISFIRE); // wait until user released the fire button #endif wait(25); // wait for button to settle # ifdef GAME_TETRIS tetris(); # endif # ifdef GAME_BASTET tetris_bastet(); # endif # ifdef GAME_TETRIS_FP tetris_fp(); # endif # ifdef GAME_SPACE_INVADERS borg_invaders(); # endif # ifdef GAME_SNAKE snake_game(); # endif # ifdef GAME_BREAKOUT borg_breakout(0); # endif #ifdef JOYSTICK_SUPPORT while (JOYISFIRE); // avoid an unwanted restart of the game loop #endif wait(25); // wait for button to settle mode = oldOldmode; // restore old animation mode #ifdef JOYSTICK_SUPPORT waitForFire = 1; // reenable joystick query of the wait() function #endif break; #endif #ifdef ANIMATION_OFF case 0xFFu: off(); break; #endif default: if (reverseMode) { if (reverseMode-- == (mode - 1)) { mode -= 2; } else { reverseMode = 0; } } break; } } }
int main (int argv, char *args[]) { int n = atoi(args[1]); spiral(n, 0); return 0; }
int main(int argc, char *argv[]) { double ne0, ne, ne1, ne2, ne3, ne4, ne5, ne6, ne7, ne8, ne9, ne10; double gl, gb, dordm, dist, xx, yy, zz, r, sl, cl, sb, cb, ll, hh; double nstep, dstep, dmstep; static double dd, dtest, dmpsr, rr; double dmm=0; double dm=0; double DM_MC=0; double DM_Gal=0; double DM_Host=0; double DDM; double tau_sc=0; double tau_Gal=0; double tau_MC=0; double tau_MC_sc=0; double R_g=0; double gd=0; //The localtion of Sun relative to GP and Warp double z_warp, zz_w, R_warp, theta_warp, theta_max; R_warp=8400;//pc theta_max=0.0; //In +x direction int WGN=0; int WLB=0; int WLI=0; int WFB=0; int np, ndir,vbs, nk, uu, nn; char str[5]; char dirname[64]="NULL",text[64]=""; char *p; static int i, ncount; int w_lmc=0; int w_smc=0; int umc=1; char *s; struct Warp_Sun t0; struct Thick t1; struct Thin t2; struct Spiral t3; struct GC t4; struct Gum t5; struct LB t6; struct LI t7; struct FB t8; struct LMC t9; struct Dora t10; struct SMC t11; vbs=0; argc--; argv++; if(argc < 5)usage(1); while(argc > 5){ /* Get command line inputs */ if((*argv)[0] == '-'){ s=argv[0]+1; argc--; argv++; switch(*s){ case 'h': case '?': usage(0); case 't': if(sscanf(*argv,"%s",text) != 1)usage(1); argc--; argv++; break; case 'd': if(sscanf(*argv,"%s",dirname) != 1)usage(1); argc--; argv++; break; case 'v': vbs=1; break; case 'V': vbs=2; break; default: usage(1); } } else{ if(argc>6){ printf("Extra parameters exist in input\n"); usage(1); } else break; } } if(argc==5){ if(sscanf(*argv,"%s",str) != 1){ printf("Incorrect arguments\n"); usage(1); } argc--; argv++; if(sscanf(*argv,"%lf",&gl) != 1){ printf("Incorrect arguments\n"); usage(1); } argc--; argv++; if(sscanf(*argv,"%lf",&gb) != 1){ printf("Incorrect arguments\n"); usage(1); } argc--; argv++; if(sscanf(*argv,"%lf",&dordm) != 1){ printf("Incorrect arguments\n"); usage(1); } argc--; argv++; if(sscanf(*argv,"%d",&ndir) != 1){ printf("Incorrect arguments\n"); usage(1); } DM_Host=100;//default } if(argc==6){ if(sscanf(*argv,"%s",str) != 1){ printf("Incorrect arguments\n"); usage(1); } argc--; argv++; if(sscanf(*argv,"%lf",&gl) != 1){ printf("Incorrect arguments\n"); usage(1); } argc--; argv++; if(sscanf(*argv,"%lf",&gb) != 1){ printf("Incorrect arguments\n"); usage(1); } argc--; argv++; if(sscanf(*argv,"%lf",&dordm) != 1){ printf("Incorrect arguments\n"); usage(1); } argc--; argv++; if(sscanf(*argv,"%lf",&DM_Host) != 1){ printf("Incorrect arguments\n"); usage(1); } argc--; argv++; if(sscanf(*argv,"%d",&ndir) != 1){ printf("Incorrect arguments\n"); usage(1); } } //convert to upper case p=strupr(str); if(strcmp(p,"IGM") == 0) np=-1; // IGM else if(strcmp(p,"MC") == 0) np=0; // Mag Clouds else if(strcmp(p,"GAL") == 0){ // Galaxy np=1; p="Gal"; } else{ printf("please input correct model\n"); usage(1); exit(1); } if(ndir!=1&&ndir!=2){ printf("please input correct ndir\n"); usage(1); } if(!strcmp(dirname,"NULL")){ if(getenv("YMW16_DIR")==NULL){ printf("Warning: YMW16_DIR set to local directory\n"); strcpy(dirname,"./"); }else{ strcpy(dirname,getenv("YMW16_DIR")); } } if(vbs>=1)printf("File directory: %s\n",dirname); ymw16par(&t0, &t1, &t2, &t3, &t4, &t5, &t6, &t7, &t8, &t9, &t10, &t11, dirname); if(ndir==1)printf("%s: gl=%8.3f gb=%8.3f DM=%8.2f", p, gl, gb, dordm); else printf("%s: gl=%8.3f gb=%8.3f Dist=%9.1f", p, gl, gb, dordm); ll=gl; gl=gl/RAD; gb=gb/RAD; sl=sin(gl); sb=sin(gb); cl=cos(gl); cb=cos(gb); dstep=5.0; if(np==-1){ // FRBs if(ndir==1)uu=0;//dm---dist else uu=1;//dist---dm ndir=2; DDM=dordm; dordm=100000; nk=20000; } if(np==0){ // Magellanic Cloud nk=20000; } if(np==1){ //Galactic pulsars nk=5000; } if(ndir==1){ dm=dordm; if(np==1)tau_sc=tsc(dm); dtest=dm/N0; nstep=dtest/dstep; if(nstep<200) dstep=dtest/200; if(vbs>=1){ printf("\ndtest=%lf, nstep=%lf, dstep=%lf\n", dtest, nstep, dstep); } } if(ndir==2){ dist=dordm; dtest=dist; nstep=dtest/dstep; if(nstep<200) dstep=dtest/200; if(vbs>=1){ printf("\ndtest=%lf, nstep=%lf, dstep=%lf\n", dtest, nstep, dstep); } } dd=-0.5*dstep; ncount=0; for(i=1;i<=nk;i++){ ncount++; if(vbs>=2){ printf("ncount=%d, dstep=%lf\n", ncount,dstep); } dd+=dstep; r=dd*cb; /* r is different from rr */ xx=r*sl; yy=R0*1000-r*cl; zz=dd*sb+t0.z_Sun; rr=sqrt(xx*xx+yy*yy); /* Definition of warp */ if(rr<R_warp){ zz_w=zz; }else{ theta_warp=atan2(yy,xx); z_warp=t0.Gamma_w*(rr-R_warp)*cos(theta_warp-theta_max); zz_w=zz-z_warp; } if(vbs>=2) { printf("dd=%lf, xx=%lf, yy=%lf, zz=%lf, rr=%lf\n", dd, xx, yy, zz, rr); printf("theta_warp=%lf, z_warp=%lf, zz_w=%lf\n",theta_warp,z_warp,zz_w); } R_g=sqrt(xx*xx+yy*yy+zz_w*zz_w); /* DM to Distance */ if(ndir==1){ if(dmm<=dm){ if(R_g<=35000){ thick(xx, yy, zz_w, &gd, &ne1, rr, t1); thin(xx, yy, zz_w, gd, &ne2, rr, t2); spiral(xx, yy, zz_w, gd, &ne3, rr, t3, dirname); galcen(xx, yy, zz, &ne4, t4); gum(xx, yy, zz, &ll, &ne5, t5); localbubble(xx, yy, zz, &ll, &ne6, &hh, t6); nps(xx, yy, zz, &ne7, &WLI, t7); fermibubble(xx, yy, zz, &WFB); }else{ if(np==1){ dstep=5; }else{ dstep=200; if(w_lmc>=1||w_smc>=1) dstep=5; lmc(gl,gb,dd,&w_lmc,&ne8,t9); dora(gl,gb,dd,&ne9,t10); smc(xx, yy, zz,&w_smc, &ne10, t11); } } if(WFB==1){ ne1=t8.J_FB*ne1; } ne0=ne1+max(ne2,ne3); if(hh>110){ /* Outside LB */ if(ne6>ne0 && ne6>ne5){ WLB=1; }else{ WLB=0; } }else{ /* Inside LB */ if(ne6>ne0){ WLB=1; }else{ ne1=t6.J_LB*ne1; ne0=ne1+max(ne2,ne3); WLB=0; } } if(ne7>ne0){ /* Loop I */ WLI=1; }else{ WLI=0; } if(ne5>ne0){ /* Gum Nebula */ WGN=1; }else{ WGN=0; } /* Galactic ne */ ne=(1-WLB)*((1-WGN)*((1-WLI)*(ne0+ne4+ne8+ne9+ne10)+WLI*ne7)+WGN*ne5)+WLB*ne6; if(vbs>=2){ printf("ne=%lf, ne1=%lf, ne2=%lf, ne3=%lf, ne4=%lf, ne5=%lf, ne6=%lf, ne7=%lf, ne8=%lf, ne9=%lf, ne10=%lf\n", ne, ne1, ne2, ne3, ne4, ne5, ne6, ne7, ne8, ne9, ne10); } dmstep=ne*dstep; if(dmstep<=0.000001)dmstep=0; if(vbs>=2){ printf("dmstep=%lf, dstep=%lf\n", dmstep, dstep); } dmm+=dmstep; dist=dd; if(np==0&&umc==1){ if(R_g>35000){ DM_Gal=dmm; tau_Gal=0.5*tsc(dmm); printf(" DM_Gal:%8.2f",DM_Gal); umc++; } } if(i==nk){ dist+=0.5*dstep; if(dist>100000)dist=100000; if(np==0){ DM_MC=dmm-DM_Gal; tau_MC=0.5*tsc(DM_MC); tau_MC_sc=max(tau_Gal, tau_MC); printf(" DM_MC:%8.2f",DM_MC); } if(np==0)printf(" Dist:%9.1f log(tau_sc):%7.3f %s\n",dist, log10(tau_MC_sc),text); if(np==1)printf(" DM_Gal:%8.2f Dist:%9.1f log(tau_sc):%7.3f %s\n", dmm, dist,log10(tau_sc),text); } if(vbs>=2){ printf("dmm=%lf\n", dmm); } } else{ dist=dd-0.5*dstep-(dstep*(dmm-dm))/dmstep; if(np==0){ DM_MC=dm-DM_Gal; if(DM_Gal==0){ DM_MC=0; DM_Gal=dm; tau_MC_sc=tsc(dm); printf(" DM_Gal:%8.2f ", DM_Gal); } else{ tau_MC=0.5*tsc(DM_MC); tau_MC_sc=max(tau_MC, tau_Gal); } printf(" DM_MC:%8.2f", DM_MC); } if(np==0)printf(" Dist:%9.1f log(tau_sc):%7.3f %s\n",dist,log10(tau_MC_sc),text); if(np==1)printf(" DM_Gal:%8.2f Dist:%9.1f log(tau_sc):%7.3f %s\n", dm, dist,log10(tau_sc),text); break; } } /* Distance to DM */ if(ndir==2){ if(dd<=dtest){ if(R_g<=35000){ thick(xx, yy, zz_w, &gd, &ne1, rr, t1); thin(xx, yy, zz_w, gd, &ne2, rr, t2); spiral(xx, yy, zz_w, gd, &ne3, rr, t3, dirname); galcen(xx, yy, zz, &ne4, t4); gum(xx, yy, zz, &ll, &ne5, t5); localbubble(xx, yy, zz, &ll, &ne6, &hh, t6); nps(xx, yy, zz, &ne7, &WLI, t7); fermibubble(xx, yy, zz, &WFB); }else{ if(np==1)dstep=5; else{ dstep=200; if(np==-1)dstep=5; if(w_lmc>=1||w_smc>=1) dstep=5; lmc(gl,gb,dd,&w_lmc,&ne8,t9); dora(gl,gb,dd,&ne9,t10); smc(xx, yy, zz,&w_smc, &ne10, t11); } } if(WFB==1){ ne1=t8.J_FB*ne1; } ne0=ne1+max(ne2,ne3); if(hh>110){ /* Outside LB */ if(ne6>ne0 && ne6>ne5){ WLB=1; }else{ WLB=0; } }else{ /* Inside LB */ if(ne6>ne0){ WLB=1; }else{ ne1=t6.J_LB*ne1; ne0=ne1+max(ne2,ne3); WLB=0; } } if(ne7>ne0){ /* Loop I */ WLI=1; }else{ WLI=0; } if(ne5>ne0){ /* Gum Nebula */ WGN=1; }else{ WGN=0; } /* Galactic ne */ ne=(1-WLB)*((1-WGN)*((1-WLI)*(ne0+ne4+ne8+ne9+ne10)+WLI*ne7)+WGN*ne5)+WLB*ne6; if(vbs>=2){ printf("ne=%lf, ne1=%lf, ne2=%lf, ne3=%lf, ne4=%lf, ne5=%lf, ne6=%lf, ne7=%lf, ne8=%lf, ne9=%lf, ne10=%lf\n", ne, ne1, ne2, ne3, ne4, ne5, ne6, ne7, ne8, ne9, ne10); } dmstep=ne*dstep; if(dmstep<=0.000001)dmstep=0; dm+=dmstep; if(np!=1&&umc==1){ if(R_g>35000){ DM_Gal=dm; tau_Gal=0.5*tsc(dm); printf(" DM_Gal:%8.2f",dm); umc++; } } if(i==nk&&np!=-1){ dmpsr=dm; if(np==0){ DM_MC=dm-DM_Gal; tau_MC=0.5*tsc(DM_MC); printf(" DM_MC:%8.2f", DM_MC); } tau_sc=tsc(dmpsr); tau_MC_sc=max(tau_Gal, tau_MC); if(np==0)printf(" DM:%8.2f log(tau_sc):%7.3f %s\n", dmpsr,log10(tau_MC_sc),text); if(np==1)printf(" DM:%8.2f log(tau_sc):%7.3f %s\n", dmpsr, log10(tau_sc),text); } if(i==nk&&np==-1){ if(dordm==100000){ DM_MC=dm-DM_Gal; printf(" DM_MC:%8.2f",DM_MC); } frb_d(DDM, DM_Gal, DM_MC, DM_Host, uu, vbs, text); break; } } else{ dmpsr=dm+(dmstep*(dtest-(dd-0.5*dstep)))/dstep; if(np==0){ DM_MC=dmpsr-DM_Gal; if(DM_Gal==0){ DM_MC=0; DM_Gal=dmpsr; tau_MC_sc=tsc(dmpsr); printf(" DM_Gal:%8.2f", DM_Gal); } else{ tau_MC=0.5*tsc(DM_MC); tau_MC_sc=max(tau_Gal, tau_MC); } printf(" DM_MC:%8.2f", DM_MC); } tau_sc=tsc(dmpsr); if(np==0)printf(" DM:%8.2f log(tau_sc):%7.3f %s\n", dmpsr,log10(tau_MC_sc),text); if(np==1)printf(" DM:%8.2f log(tau_sc):%7.3f %s\n", dmpsr, log10(tau_sc),text); break; } } } }
int main (int argc, char *argv[]) { struct parms parms; struct timeval start; int i, rank, size, ncy, cyc; int freq[5]={PHI,P,V,C}; double *data, *phi0, *kr, *r, *dr, *phit, *dphi; double telp, time, tcyc, havg, vavg, delh; char *wkdir=0; char input[64]={"init/input.dat"}; char phichek[64]={"init/phi.chk"}; //char phii[Nimob][64]; //for (i = 0; i < Nimob; i++){ // sprintf(phii[i],"data/phi%d.",(i+1)); //} char phii[64] = {"data/phi."}; char p[64] = {"data/p."}; char vx[64] = {"data/vx."}; char vy[64] = {"data/vy."}; char vz[64] = {"data/vz."}; char c[64] = {"data/c."}; char avname[64]={"data/avg."}; char fname[64]; FILE *fileptr; if (argc > 1) cyc = atoi(argv[1]); if (argc > 2) wkdir = argv[2]; winit(&start); // MPI Initialization MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); parms.rank = rank; parms.size = size; parms.V0 = 0; parms.nsten = (2*Dim)+1; parms.Nvar = Dim + 3; nameinput(fname, input, wkdir); fileptr = fopen(fname, "r"); // Read input data fscanf(fileptr, "%*s %*s %*s %*s %*s %*s %*s"); fscanf(fileptr, "%d %lf %lf %lf %lf %lf %lf", &ncy, &parms.Re, &parms.Pe, &parms.Da, &parms.pow, &parms.dx, &parms.dt); fclose(fileptr); // Reading the initial porosity profile #ifdef SEED char phseed[64] = {"init/phi.dat-seed"}; filename(fname, phseed, wkdir, parms); #endif #ifndef SEED char phinit[64] = {"init/phi.dat"}; filename(fname, phinit, wkdir, parms); #endif fileptr = fopen(fname, "r"); fscanf(fileptr, "%5d %5d %5d %5d %5d %5d %lf", &parms.Nx, &parms.Ny, &parms.nx, &parms.ny, &parms.Nz, &parms.cyc, &parms.t); fclose(fileptr); // 2D Domain decomposition parms = mapping(parms); parms.N = (parms.nx*parms.ny*parms.Nz); // Initialize pointers phi0 = (double *) calloc(parms.N, sizeof(double)); data = (double *) calloc(parms.N*parms.Nvar, sizeof(double)); dphi = (double *) calloc(parms.N, sizeof(double)); phit = (double *) calloc(parms.N, sizeof(double)); kr = (double *) calloc(parms.N, sizeof(double)); r = (double *) calloc(parms.N, sizeof(double)); dr = (double *) calloc(parms.N, sizeof(double)); // Create Spiral indexing int *sp = spiral(parms); readf(fname, phi0, parms); // Read from the input porosity profile writedata(phi0, data, 0, 0, parms); // Write the input porosity to pointer "data" @ 0 for(i = 0; i < parms.N; i++){ data[(i*parms.Nvar)+Dim+2] = 1.0; // Initialize c = 1.0 kr[i] = 1.0; } erode(data, kr, r, dr, sp, parms); // Intial fields parms.V0 = avgf(data, 2, parms); if (cyc > 0){ parms.cyc = cyc; filename(fname, phichek, wkdir, parms); readb(fname, phi0, parms); } writedata(phi0, data, 0, 0, parms); // Write the input porosity to pointer "data" @ 0 telp = wtime(&start); time = telp; if (parms.rank == 0){ fprintf(stdout, "Initialization; elapsed time: % .3e\n\n", telp); } while (parms.cyc <= ncy){ erode(data, kr, r, dr, sp, parms); havg = avgf(data, 0, parms); vavg = avgf(data, 2, parms); vavg = vavg*parms.Ny; delh = havg - parms.havg; parms.havg = havg; if (parms.rank == 0){ filename(fname, avname, wkdir, parms); fileptr = fopen(fname, "w"); if (parms.cyc == 0) fprintf(fileptr, " cycle time havg qavg delh\n"); fprintf(fileptr, "%5d % .3e % .3e % .3e % .3e\n", parms.cyc, parms.t, havg, vavg, delh); fclose(fileptr); } //for (i = 0; i < Nimob; i++){ // filename(fname, phii[i], wkdir, parms); // Write data files // writef(fname, data, i, parms, freq[0]); //} filename(fname, phii, wkdir, parms); // Write data files writef(fname, data, 0, parms, freq[0]); filename(fname, p, wkdir, parms); writef(fname, data, 1, parms, freq[1]); filename(fname, vx, wkdir, parms); writef(fname, data, 2, parms, freq[2]); filename(fname, vy, wkdir, parms); writef(fname, data, 3, parms, freq[2]); filename(fname, vz, wkdir, parms); writef(fname, data, 4, parms, freq[2]); filename(fname, c, wkdir, parms); writef(fname, data, Dim+2, parms, freq[3]); // Calculate erosion during dt double dt = parms.dt/parms.Da; #ifdef EULER for (i = 0; i < parms.N; i++) dphi[i] = r[i]*dt; #endif #ifdef MIDPT for (i = 0; i < parms.N; i++){ phit[i] = phi0[i] + 0.5*r[i]*dt; } writedata(phit, data, 0, 0, parms); erode (data, kr, r, dr, sp, parms); for (i = 0; i < parms.N; i++) dphi[i] = r[i]*dt; #endif #ifdef RK4 for (i = 0; i < parms.N; i++){ phit[i] = phi0[i] + 0.5*r[i]*dt; dphi[i] = r[i]*dt/6.0; } writedata(phit, data, 0, 0, parms); erode (data, kr, r, dr, sp, parms); for (i = 0; i < parms.N; i++){ phit[i] = phi0[i] + 0.5*r[i]*dt; dphi[i]+= r[i]*dt/3.0; } writedata(phit, data, 0, 0, parms); erode (data, kr, r, dr, sp, parms); for (i = 0; i < parms.N; i++){ phit[i] = phi0[i] + r[i]*dt; dphi[i]+= r[i]*dt/3.0; } writedata(phit, data, 0, 0, parms); erode (data, kr, r, dr, sp, parms); for (i = 0; i < parms.N; i++) dphi[i]+= r[i]*dt/6.0; #endif // Update height profile for (i = 0; i < parms.N; i++){ phi0[i] += dphi[i]; #ifdef APLIM if (phi0[i] >= AMAX){ phi0[i] = AMAX; kr[i] = 0.0; } #endif } writedata(phi0, data, 0, 0, parms); telp = wtime(&start); tcyc = time; time += telp; tcyc = time - tcyc; if (parms.rank == 0){ fprintf(stdout, "Cycle %6d; cycle time: % .3e total time: % .3e\n\n", parms.cyc, tcyc, time); } parms.cyc++; parms.t += parms.dt; #if CHEK > 0 if ((parms.cyc-1)%CHEK == 0){ filename(fname, phichek, wkdir, parms); writeb(fname, data, 0, parms); /* Checkpoint aperture */ } #endif } // Free memory free(phit); free(r); free(dr); free(kr); free (dphi); free(phi0); free(data); free(sp); // Finalize MPI MPI_Finalize(); return (0); }