//---------------------------------------------------------------------- //---------------------------------------------------------------------- // // AMR Driver for the 2-D Wave Application // //---------------------------------------------------------------------- // ---------------------------------------------------------------------- // waveamr2d.C // Program to evolve 2D wave equation using DAGH structures // Mijan Huq and Manish Parashar // University of Texas at Austin //---------------------------------------------------------------------- // Program uses wave equation in first order form. That is, there are a // set of three equations and three grid functions. // A = D_t phi , B = D_x phi , C = D_y phi // Wave equation D_t^2 phi = D_x^2 phi + D_y^2 phi is rewritten as // D_t A = D_x B + D_y C // D_t B = D_x A // D_t C = D_x A // and solved using a Leapfrog scheme. //---------------------------------------------------------------------- #include "DAGH.h" #include "DAGHCluster.h" #include "DAGHIO.h" #include "waveamr2d.h" #include "wavefortran2d.h" #include "bbhutil.h" #include "math.h" #define VizServer 0 #define WAVEXGRAPH "/u5/parashar/xgraph/xgraph" #define WAVEDISPLAY "zeus.ticam.utexas.edu:0.0" char space[41]; INTEGER MaxLev; DOUBLE smoothval = 0.5; // AMR parameters DOUBLE thresh; // Threshold for regridding // Clustering routine prototypes //BBoxList Cluster2(GridData(2) &flag, // double Efficiency, // int MinWidth, // int BufferWidth); static char *indent(int const l) { assert(l>=0); for (int i=0;i B("b", t_sten, s_sten, GH, COMM, HAS_SHADOW); GridFunction(DIM) C("c", t_sten, s_sten, GH, COMM, HAS_SHADOW); GridFunction(DIM) phi("phi", t_sten, s_sten, GH, COMM, HAS_SHADOW); GridFunction(DIM) temp("temp", t_sten, s_sten, GH, COMM, HAS_SHADOW); // Additional variables needed for evolution DOUBLE anorm, bnorm, cnorm, phinorm; INTEGER me = MY_PROC(GH); INTEGER num = NUM_PROC(GH); // Initialize Level = 0 (Base Grid) INTEGER Level = 0; // Tell DAGH which prolongation and restriction to use forallGF(GH,GF,DIM,GFTYPE) SetProlongFunction(GF, (void *) &f_prolong_amr); SetRestrictFunction(GF, (void *) &f_restrict_amr); end_forallGF INTEGER t = CurrentTime(GH,Level); INTEGER idt = TimeStep(GH,Level); INTEGER l = Level; // Set up initial data if(me == 0) cout << "****Setting up Initial data on MAIN ****" << endl << flush; // grid spacings at current level on MAIN DOUBLE dagh_h = DeltaX(GH,X_AXIS,l,MAIN); DOUBLE dagh_dt = dagh_h * cfl; // Load in initial data forall(phi, t, l, c) f_initial_data(FBA(phi(t,l,c,MAIN)), FDA(phi(t,l,c,MAIN)), FDA(A(t,l,c,MAIN)), FDA(B(t,l,c,MAIN)), FDA(C(t,l,c,MAIN)), &dagh_h, &dagh_dt, &litude, &width, &vx, &vy, &xmin, &ymin); end_forall // Initialize the first level on the MAIN hierarchy if(me == 0) cout << "****Initializing first step on MAIN****" << endl << flush; dagh_h = DeltaX(GH,X_AXIS,l,MAIN); dagh_dt = dagh_h * cfl; // Set up the n-1 level by iteration initial_step (MAIN, GH, A, B, C, phi, dagh_h, dagh_dt, stp, maxsteps,l); if(me == 0) cout << "****Setting up Initial data on SHADOW ****" << endl << flush; // grid spacings at current level on SHADOW dagh_h = DeltaX(GH,X_AXIS,l,SHADOW); dagh_dt = dagh_h * cfl; // Load in initial data on SHADOW forall(phi, t, l, c) f_initial_data(FBA(phi(t,l,c,SHADOW)), FDA(phi(t,l,c,SHADOW)), FDA(A(t,l,c,SHADOW)), FDA(B(t,l,c,SHADOW)), FDA(C(t,l,c,SHADOW)), &dagh_h, &dagh_dt, &litude, &width, &vx, &vy, &xmin, &ymin); end_forall // Initialize the first level on the SHADOW hierarchy if(me == 0) cout << "****Initializing first step on SHADOW****" << endl << flush; dagh_h = DeltaX(GH,X_AXIS,l,SHADOW); dagh_dt = dagh_h * cfl; // Set up the n-1 level by iteration on SHADOW initial_step (SHADOW, GH, A, B, C, phi, dagh_h, dagh_dt, stp, maxsteps,l); // *************************Begin Evolution************************** INTEGER currentiter = 0; // Dump out the initial data char fname[40]; idt = TimeStep(GH,l); sprintf(fname,"SlicePhi%d.dat",currentiter); writeslice(MAIN, fname, GH, phi, t, l, VizServer); idt = TimeStep(GH,l); sprintf(fname,"SliceShadowPhi%d.dat",currentiter); writeslice(SHADOW, fname, GH, phi, t, l, VizServer); if(me == 0)cout << "Starting main loop" << endl << flush; for (currentiter=1;currentiter<=niters;currentiter++) { // Carry out evolution steps on all grids on all levels // At this stage only timelevels n and n-1 are defined. t = CurrentTime(GH,Level); idt = TimeStep(GH,Level); bno_RecursiveIntegrate(GH, Level, A, B, C, phi, temp, dt, h, cfl); /******* MAIN *********/ // Calculate norms of A, B, C, phi on MAIN hierarchy anorm = Norm2(A, t, l, MAIN); bnorm = Norm2(B, t, l, MAIN); cnorm = Norm2(C, t, l, MAIN); phinorm = Norm2(phi,t, l, MAIN); // Loop through existing levels and dump out data (1 processor only) for(INTEGER curlev = 0; curlev <= FineLevel(GH); curlev++){ if(me == 0)cout << "Dumping Level" << curlev << endl << flush; INTEGER myidt = TimeStep(GH,curlev); INTEGER myt = CurrentTime(GH,curlev); sprintf(fname,"SliceLevel%dPhi%d.dat",curlev,currentiter); writeslice(MAIN, fname, GH, phi, myt,curlev, VizServer); } t = CurrentTime(GH,Level); idt = TimeStep(GH,Level); if(me == 0){ cout << "MAIN norms " << anorm << " " << bnorm << " " << cnorm << " " << phinorm << endl << flush; } /******* SHADOW *********/ if(StepsTaken(GH,Level) % RefineFactor(GH) == 0){ // Calculate norms of A, B, C, phi on SHADOW hierarchy anorm = Norm2(A, t, l, SHADOW); bnorm = Norm2(B, t, l, SHADOW); cnorm = Norm2(C, t, l, SHADOW); phinorm = Norm2(phi,t, l, SHADOW); // Loop through existing levels and dump out data (1 processor only) for(INTEGER curlev = 0; curlev <= FineLevel(GH); curlev++){ if(me == 0)cout << "Dumping Level" << curlev << endl << flush; INTEGER myidt = TimeStep(GH,curlev); INTEGER myt = CurrentTime(GH,curlev); sprintf(fname,"SliceSHLevel%dPhi%d.dat",curlev,currentiter); writeslice(SHADOW, fname, GH, phi, myt,curlev, VizServer); } if(me == 0){ cout << "SHADOW norms " << anorm << " " << bnorm << " " << cnorm << " " << phinorm << endl << flush; } } // End if statement for SHADOW } // End loop over number of iterations DAGHMPI_Finalize(); } // End main //---------------------------------------------------------------------- // // Recursive Integrate assumes: // if Level == 0 then timelevels n and n-1 are defined and n+1 is not. // if Level > 0 then timelevels n+1, n and n-1 are defined on level Level-1. // This allows for time interpolations for boundary // updates in the UpdateExternalBoundary call. // void bno_RecursiveIntegrate( GridHierarchy &GH, INTEGER const Level, GridFunction(DIM) &A, GridFunction(DIM) &B, GridFunction(DIM) &C, GridFunction(DIM) &phi, GridFunction(DIM) &temp, DOUBLE &dt, DOUBLE &h, DOUBLE const &cfl) { INTEGER me = MY_PROC(GH); INTEGER num = NUM_PROC(GH); INTEGER l = Level; INTEGER t = CurrentTime(GH,Level); INTEGER idt = TimeStep(GH,Level); INTEGER NoIterations = 0; // Select number of iterations to run based on current grid level if (Level==0) NoIterations = 1; else NoIterations = RefineFactor(GH); for (INTEGER i=0;i& u, INTEGER const IDENT, GridHierarchy &GH, INTEGER const Level) { INTEGER t = CurrentTime(GH,Level); Restrict(u, t, Level+1, Level, (GFTYPE *)0,0,IDENT); Sync(u, t, Level, IDENT); } void bno_RegridSystemAbove(GridHierarchy &GH, GridFunction(DIM)& u, INTEGER const Level, GFTYPE const thresh) { BBoxList bblist, tmpbblist; INTEGER flev = FineLevel(GH); INTEGER t = CurrentTime(GH,Level); INTEGER idt = TimeStep(GH,Level); INTEGER l = 0; DOUBLE newthresh; if (flev == 0) l = 0; else if (flev == MaxLev-1) l = flev-1; else l = flev; for (;l>=Level;l--) { t = CurrentTime(GH,l); idt = TimeStep(GH,l); int factor = 1 << (l+1); // newthresh = thresh / (1.0*factor) ; // newthresh = thresh / pow(2.0,(double)(factor)); // if(MY_PROC(GH) == 0)cout << "Level " << l << "Threshold " << newthresh << endl << flush; Cluster2d(u, t, l, BLOCK_WIDTH, BUFFER_WIDTH, MIN_EFFICIENCY, thresh, bblist, bblist, MAIN); comm_service::log() << indent(Level) << "********* From Clustering Routine *********" << endl << flush; comm_service::log() << indent(Level) << bblist << flush; if (1) { static int cnt = 0; char fname[40]; sprintf(fname,"GridSlice%d.dat",cnt); PlotBBox(MAIN, u, t, l, bblist, fname, VizServer); cnt++; } Refine(GH,bblist,l); } // Ends for loop over levels /* static mycnt2 = 0; if (1 && FineLevel(GH) != CoarseLevel(GH)) { char fname[40]; INTEGER idt = TimeStep(GH,FineLevel(GH)); sprintf(fname,"SliceShadowFinePhiBefore%d.dat",mycnt2++); writeslice(SHADOW, fname, GH, u, t, FineLevel(GH), VizServer); sprintf(fname,"SliceFinePhiBefore%d.dat",mycnt2++); writeslice(MAIN, fname, GH, u, t, FineLevel(GH), VizServer); } */ GH.DAGH_RecomposeHierarchy(); /* static mycnt = 0; if (1 && FineLevel(GH) != CoarseLevel(GH)) { char fname[40]; INTEGER idt = TimeStep(GH,FineLevel(GH)); sprintf(fname,"SliceShadowFinePhiAfter%d.dat",mycnt++); writeslice(SHADOW, fname, GH, u, t, FineLevel(GH), VizServer); sprintf(fname,"SliceFinePhiAfter%d.dat",mycnt++); writeslice(MAIN, fname, GH, u, t, FineLevel(GH), VizServer); } */ } // bno_RegridSystemAbove void writeslice(int const IDENT, char const *fname, GridHierarchy &GH, GridFunction(DIM) &gf, INTEGER const t, INTEGER const l, INTEGER const viz_server) { FILE *dfile; INTEGER me = MY_PROC(GH); BBox bb; Coords s; forall(gf,t,l,c) bb += bbox(gf(t,l,c,IDENT)); s = stepsize(gf(t,l,c,IDENT)); end_forall bb.stepsize() = s; GridData(DIM) gd; gf.GF_gather_one(t,l,bb,bb,gd,viz_server,IDENT); if (me == viz_server) { if (!(dfile = fopen(fname, "w"))) exit(-1); for(INTEGER i=bb.lower(0);i<=bb.upper(0);i+=bb.stepsize(0)) { for(INTEGER j=bb.lower(1);j<=bb.upper(1);j+=bb.stepsize(1)) { fprintf(dfile,"%d\t%d\t%le\n",i,j,gd(i,j)); } //end for over j fprintf(dfile,"\n"); } //end for over i fclose(dfile); } } void writeslicehdf(int const IDENT, char const *fname, GridHierarchy &GH, GridFunction(DIM) &gf, INTEGER const t, INTEGER const l, INTEGER const viz_server) { FILE *dfile; INTEGER me = MY_PROC(GH); INTEGER rank = DIM; INTEGER gft_rc; char *cnames[3]; // Label coordinates with numbers 1 through DIM cnames[0] = (char *)malloc(sizeof(char)*3); cnames[1] = (char *)malloc(sizeof(char)*3); cnames[2] = (char *)malloc(sizeof(char)*3); sprintf(cnames[0],"1"); sprintf(cnames[1],"2"); sprintf(cnames[2],"3"); BBox bb; Coords s; forall(gf,t,l,c) bb += bbox(gf(t,l,c,IDENT)); s = stepsize(gf(t,l,c,IDENT)); end_forall INTEGER *shape = bb.extents() ; DOUBLE *xy; xy = (DOUBLE *)malloc(sizeof(DOUBLE)*(shape[0]+shape[1])); bb.stepsize() = s; GridData(DIM) gd; gf.GF_gather_one(t,l,bb,bb,gd,viz_server,IDENT); if (me == viz_server) { if (!(dfile = fopen(fname, "w"))) exit(-1); // Set up coordinates for(INTEGER i=bb.lower(0);i<=bb.upper(0);i+=bb.stepsize(0)) { xy[i] = (DOUBLE)i; } // end for over i for(INTEGER j=bb.lower(1);j<=bb.upper(1);j+=bb.stepsize(1)) { xy[i + bb.upper(0)] = (DOUBLE)j; } //end for over j /****** DUMP HDF Data ******/ gft_rc = gft_write_full(fname, t, bb.extents(),cnames,DIM, xy, gd.data()); if(gft_rc == 0){ cout << "Error in gft_write_full: Did not write to file " << fname << endl << flush; } } free(cnames); free(xy); } void SyncNext(INTEGER const IDENT, GridFunction(DIM) &gf, GridFunction(DIM) &temp, INTEGER const t, INTEGER const l, INTEGER const idt) { Sync(gf,t+idt,l,IDENT); } void SyncPrev(INTEGER const IDENT, GridFunction(DIM) &gf, GridFunction(DIM) &temp, INTEGER const t, INTEGER const l, INTEGER const idt) { Sync(gf,t-idt,l,IDENT); } void MoveStencilUp(INTEGER const IDENT, GridFunction(DIM) &gf, INTEGER const t, INTEGER const l, INTEGER const idt) { forall(gf, t, l, c) gf(t-idt,l,c,IDENT) = gf(t,l,c,IDENT); // copy n to n-1 gf(t,l,c,IDENT) = gf(t+idt,l,c,IDENT); // copy n+1 to n end_forall } void PlotBBox(INTEGER const IDENT, GridFunction(DIM) &gf, INTEGER const t, INTEGER const l, BBoxList &bbl, char const *fname, INTEGER const viz_server) { BBox bb; Coords s; forall(gf,t,l,c) bb += bbox(gf(t,l,c,IDENT)); s = stepsize(gf(t,l,c,IDENT)); end_forall bb.stepsize() = s; Array(2) flag_array(bb); flag_array = 0; BBox *_b = bbl.first(); for (;_b;_b=bbl.next()) { flag_array.equals(1,*_b); } { FILE *dfile; INTEGER me = MY_PROC(GH); BBox viewbb = flag_array.bbox(); if (me == viz_server) { if (!(dfile = fopen(fname, "w"))) exit(-1); for(INTEGER i=viewbb.lower(0);i<=viewbb.upper(0);i+=viewbb.stepsize(0)) { for(INTEGER j=viewbb.lower(1);j<=viewbb.upper(1);j+=viewbb.stepsize(1)) { fprintf(dfile,"%d\t%d\t%d\n",i,j,flag_array(i,j)); } // End for over j fprintf(dfile,"\n"); } // End for over i fclose(dfile); } } } //End PlotBBox