/** [Return to my homepage](http://basilisk.fr/sandbox/nlemoine/README) # Basilisk interface for computing the *Basal Envelope Surface of Talwegs* using the Analytical Element Method This is the source code for the study published in [Le Moine, 2023](https://doi.org/10.5802/crgeos.164). This code mixes Basilisk C code with a [FORTRAN library](./libaem.h) for computing [slit complex potential](./slit.f) and [areal sink potential](./areal.f), and finally [linear least-square LAPACK subroutines](./lsq_lapack.f). In order to run the code, you will need to [install Basilisk](http://basilisk.fr/src/INSTALL), get the content of the [envelope](./) directory, and then run: make envelope.tst */ #include "grid/quadtree.h" #include "libaem.h" #include "run.h" #include "nlemoine/esri-binary-grids.h" #include "envelope.h" #pragma autolink -L. -laem -lgfortran -lblas -llapack #define M_PI acos(-1.0) #define LEVEL 10 scalar Phi[]; /** ## The main initializes the grid (Evel catchment, Lambert 93 coord.) */ int main (int argc, char * argv[]) { N = 1 << LEVEL; L0 = 25600.; size (L0); X0 = 267000.-L0/2.; Y0 = 6782500.-L0/2.; origin (X0,Y0); init_grid (1 << LEVEL); run(); return(0); } event init (i=0) { double * ReZstart, * ImZstart, * Phistart, * ReZend, * ImZend, * Phiend , * PHI_topo, * Aup, * dA; int * MinUpID, * DownID; double * ReCn, * ImCn, * Q; double v0x,v0y,Phi0; double * ReZ, * ImZ, * PHI, * PSI; double * J_PHI, * J_PSI, * J_Vx, * J_Vy; double * SlitLength; int j,k; char buffer[200]; char datafile[200]; FILE * fp; int one = 1; double PHI1, PSI1, Vx1, Vy1; double Xp, Yp; int ncoef = 2; int ntheta = 4*ncoef; int nslit = 0; int nsink,nbound; ReZstart = NULL; ImZstart = NULL; Phistart = NULL; ReZend = NULL; ImZend = NULL; Phiend = NULL; MinUpID = NULL; DownID = NULL; SlitLength = NULL; Aup = NULL; dA = NULL; /** ## Read data for stream slit elements */ sprintf(datafile,"../sinks.dat"); if(!(fp = fopen (datafile, "r"))) { printf("Failed to open sink data file!\n"); return -1; } while( fgets(buffer, 200, fp)!= NULL ) // parse until end-of-file { ReZstart = (double *) realloc(ReZstart,(nslit+1)*sizeof(double)); ImZstart = (double *) realloc(ImZstart,(nslit+1)*sizeof(double)); Phistart = (double *) realloc(Phistart,(nslit+1)*sizeof(double)); ReZend = (double *) realloc(ReZend,(nslit+1)*sizeof(double)); ImZend = (double *) realloc(ImZend,(nslit+1)*sizeof(double)); Phiend = (double *) realloc(Phiend,(nslit+1)*sizeof(double)); Aup = (double *) realloc(Aup,(nslit+1)*sizeof(double)); dA = (double *) realloc(dA,(nslit+1)*sizeof(double)); MinUpID = (int *) realloc(MinUpID,(nslit+1)*sizeof(int)); DownID = (int *) realloc(DownID,(nslit+1)*sizeof(int)); sscanf(buffer,"%lf %lf %lf %lf %lf %lf %lf %lf %d %d",ReZstart+nslit,ImZstart+nslit,Phistart+nslit,ReZend+nslit,ImZend+nslit,Phiend+nslit, Aup+nslit,dA+nslit,MinUpID+nslit,DownID+nslit); nslit++; } fclose(fp); fprintf(stderr,"%d sinks read\n",nslit); fflush(stderr); /** ## Prune tree at higher support area if desired */ // Prune tree double Aup_min = 0.0e6; (void) prune_tree ( & ReZstart, & ImZstart, & Phistart, & ReZend, & ImZend, & Phiend, & Aup, & dA, & DownID, & nslit, Aup_min); fprintf(stderr,"Tree pruned down to %d sinks\n",nslit); fflush(stderr); SlitLength = (double *) malloc(nslit*sizeof(double)); for(int s=0;s0.5) { (void) slit_ ( &Xp,&Yp,&one, ReZstart,ImZstart,ReZend,ImZend,&nslit, ReCn,ImCn,&ncoef,Q,&v0x,&v0y,&Phi0, varout_grid, &PHI1,&PSI1,&Vx1,&Vy1,J_PHI,J_PSI,J_Vx,J_Vy); Phi[] = PHI1; if(gamma0!=0.) { (void) arealsink_ ( &ReZstart[nsink],&ImZstart[nsink],&nbound, &Xp,&Yp,&one,&gamma0, &PHI1,&Vx1,&Vy1,&A); Phi[] += PHI1; } } else Phi[] = -9999.; } fprintf(stderr,"Done computing Phi[] map. Writing to file...\n"); fflush(stderr); char fltfile[200]; sprintf(fltfile,"Phi.flt"); output_flt(Phi,fltfile); // Deallocate other pointers free(ReZstart); free(ImZstart); free(Phistart); free(ReZend); free(ImZend); free(Phiend); free(MinUpID); free(ReCn); free(ImCn); free(Q); free(ReZ); free(ImZ); free(PHI); free(PHI_areal1); free(Vn_areal1); free(UL);free(UC);free(UR); free(LL);free(LC);free(LR); free(PL);free(PC);free(PR); free(PHI_areal1_gw); } /** ![](https://comptes-rendus.academie-sciences.fr/geoscience/article/CRGEOS_2023__355_S1_79_0/jpg/src/tex/figures/fig14.jpg){width="800px"} */