sandbox/nlemoine/halfar2D.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    
    //#include "grid/multigrid.h"
    #include "grid/quadtree.h"
    #include "run.h"
    #include "diffusion.h"
    #include "input-esri.h"
    #include "output.h"
    
    #define LEVEL         8
    #define MINLEVEL      5
    #define ETAE          20 // error on surface elevation
    #define DE            1e6 // error on diffusivity (m2/yr)
    #define sec_per_year  31557600.
    #define tfin          40000.
    
    scalar zb[],eta[];
    face vector D[];
    double Gamma;
    double R0,H0,t0; // paramaters for initial state of similarity solution
    
    double dt, tini;
    mgstats mgd;
    
    int update_diffusivity(scalar zb, scalar eta, face vector D)
    {
      //    /!\ reminder : D.x and D.y are not co-located
      foreach_face()
      {
        double hm = (eta[]+eta[-1,0]-zb[]-zb[-1,0])/2.; 			// ice thickness interpolated at face center
        hm = max(hm,0.);
        double sn = (eta[]-eta[-1,0])/Delta; 				// slope component along face normal
        double st = (eta[0,-1]+eta[-1,-1]-eta[0,1]-eta[-1,1])/4./Delta;	// slope component transverse to face normal
        D.x[] = sec_per_year*Gamma*pow(hm,5.)*(sn*sn+st*st);
      }
    
     return(0);
    }
    
    // Halfar similarity solution
    double Halfar(double r, double tt)
    {
        double tmp = 1. - pow( pow(t0/tt,1./18.) * r/R0 , 4./3.);
        tmp = H0 * pow(t0/tt,1./9.) * pow( max(0.0,tmp), 3./7.);
        return tmp  ;
    }
    
    int main (int argc, char * argv[])
    {
      N = 1 << LEVEL;
      L0 = 2.0e6;
      size (L0);
      origin (-L0/2.,-L0/2.);
    
      Gamma  = (2./5.) * (3.5e-24) * pow(9.81*910.,3.) ; // see Bueler et al (2005)
      R0 = 700.0e3;  // radius
      H0 = 3000.;	// thickness
      t0 = pow(7./4.,3.)*pow(R0,4.)/pow(H0,7.)/18./Gamma; 
      t0 = t0/sec_per_year;
    
      tini = 0.1;
    
      run();
      return(0);
    }
    
    event init (i=0)
    {
       fprintf (stdout, "%.3e %.3e %.3e %.3e,%.3e\n", H0,R0,t0,Gamma,tfin);
    
    //  (void) input_asc (zb,NULL,"topo.asc",linear=true);
    //  boundary({zb});
      foreach()
      {
        zb[] = 0.;
        double r = sqrt(x*x+y*y);
        eta[] = Halfar(r,tini);
      }
      boundary({zb,eta});
    }
    
    scalar l[];
    
    event movie (t=0.; t += 10.)
    {
      output_ppm (eta, linear = false, spread = 2, file = "eta.mp4", n = 200);
      printf ("%d %g %g %d\n", i, t, dt, mgd.i);
    
      foreach()
        l[] = level;
    
      output_ppm (l, linear = false, spread = 2, file = "level.mp4", n = 200);
    }
    
    event transect (t=0. ; t+=100.)
    {
      FILE * fp;
      char name[80];
      double xp,eta_num,eta_ana,eta_ana0;
    
      sprintf(name,"transect-%d.csv",(int)t);
      fp = fopen(name,"w");
      fprintf (fp, "xp;eta_num;eta_ana;eta_ana0\n");
    
      for(int np = 0;np<N;np++)
      {
        xp = -L0/2.+(L0/N)*((float)np+0.5);
        eta_num = interpolate (eta, xp,0.);
        eta_ana = Halfar(fabs(xp),t+tini);
        eta_ana0 = Halfar(fabs(xp),tini);
        fprintf (fp, "%g;%g;%g;%g\n", xp, eta_num, eta_ana,eta_ana0);
      }
    
      fclose(fp);
    }
    
    event stop (t = tfin);
    
    event integration (i++)
    {
    
      (void) update_diffusivity(zb,eta,D);
    
      stats s = statsf (D.x);
      dt = dtnext(10.0*L0*L0/N/N/s.max);
    
      mgd = diffusion(eta,dt,D);
    
      // preserve positivity of ice tickness
      foreach()
       eta[] = max(zb[],eta[]);
    }
    
    // Adaptivity
    
    int adapt() {
    #if TREE
    
      astats s = adapt_wavelet ({eta}, (double[]){ETAE},
    			    LEVEL, MINLEVEL);
    //  fprintf (ferr, "# refined %d cells, coarsened %d cells\n", s.nf, s.nc);
      return s.nf;
    #else // Cartesian
      return 0;
    #endif
    }
    
    // And finally we set the event that will apply our *adapt()* function at every timestep.
    
    event do_adapt (i++) adapt();