CanopyHydrology_module.cpp 32.7 KB
Newer Older
1
2
3
4
5
6
7
#include <array>
#include <sstream>
#include <iterator>
#include <exception>
#include <string>
#include <stdlib.h>
#include <cstring>
8
#include <cmath>
9
10
11
12
13
#include <vector>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <Kokkos_Core.hpp>
14
15
16
17
#include "landunit_varcon.h"    
#include "column_varcon.h" 
#include "clm_varpar.h"         
#include "clm_varctl.h"   
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "utils.hh"
#include "readers.hh"

namespace ELM {
namespace Utils {

static const int n_months = 12;
static const int n_pfts = 17;
static const int n_max_times = 31 * 24 * 2; // max days per month times hours per
                                            // day * half hour timestep
static const int n_grid_cells = 24;
static const int n_levels_snow = 5;

using MatrixStatePFT = MatrixStatic<n_grid_cells, n_pfts>;
using MatrixStateSoilColumn = MatrixStatic<n_grid_cells, n_levels_snow>;
using MatrixForc = MatrixStatic<n_max_times,n_grid_cells>;
using VectorColumn = VectorStatic<n_grid_cells>;
using VectorColumnInt = VectorStatic<n_grid_cells,int>;

} // namespace
} // namespace

40
namespace ELM {
41
KOKKOS_INLINE_FUNCTION void CanopyHydrology_Interception(double dtime,
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
        const double& forc_rain,
        const double& forc_snow,
        const double& irrig_rate,
        const int& ltype, const int& ctype,
        const bool& urbpoi, const bool& do_capsnow,
        const double& elai, const double& esai,
        const double& dewmx, const int& frac_veg_nosno,
        double& h2ocan,
        int n_irrig_steps_left, //fix it
        double& qflx_prec_intr,
        double& qflx_irrig,
        double& qflx_prec_grnd,
        double& qflx_snwcp_liq,
        double& qflx_snwcp_ice,
        double& qflx_snow_grnd_patch,
        double& qflx_rain_grnd)

 {  

  
      double  fpi, xrun, h2ocanmx   ;
      double  qflx_candrip, qflx_through_snow, qflx_through_rain ;
      double  qflx_prec_grnd_snow;
      double  qflx_prec_grnd_rain ;
      double  fracsnow ;
      double  fracrain , forc_irrig;


      if (ltype==istsoil || ltype==istwet || urbpoi || ltype==istcrop) {

         qflx_candrip = 0.0      ;
         qflx_through_snow = 0.0 ;
         qflx_through_rain = 0.0 ;
         qflx_prec_intr = 0.0    ;
         fracsnow = 0.0          ;
         fracrain = 0.0          ;
         forc_irrig = 0.0;


         if (ctype != icol_sunwall && ctype != icol_shadewall) {
            if (frac_veg_nosno == 1 && (forc_rain + forc_snow) > 0.0) {

              
               fracsnow = forc_snow/(forc_snow + forc_rain);
               fracrain = forc_rain/(forc_snow + forc_rain);

               
               h2ocanmx = dewmx * (elai + esai);

               
               fpi = 0.250*(1.0 - exp(-0.50*(elai + esai)));

              
               qflx_through_snow = forc_snow * (1.0-fpi);
               qflx_through_rain = forc_rain * (1.0-fpi);

               
               qflx_prec_intr = (forc_snow + forc_rain) * fpi;
               


               
104
               h2ocan = max(0.0, h2ocan + dtime*qflx_prec_intr);
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

               
               qflx_candrip = 0.0;

               
               xrun = (h2ocan - h2ocanmx)/dtime;

               
               if (xrun > 0.0) {
                  qflx_candrip = xrun;
                  h2ocan = h2ocanmx;
               }

            }
         }

      else if (ltype==istice || ltype==istice_mec) {
         
         h2ocan            = 0.0;
         qflx_candrip      = 0.0;
         qflx_through_snow = 0.0;
         qflx_through_rain = 0.0;
         qflx_prec_intr    = 0.0;
         fracsnow          = 0.0;
         fracrain          = 0.0;

      }

      

      if (ctype != icol_sunwall && ctype != icol_shadewall) {
         if (frac_veg_nosno == 0) {
            qflx_prec_grnd_snow = forc_snow;
            qflx_prec_grnd_rain = forc_rain;  }
         else{
            qflx_prec_grnd_snow = qflx_through_snow + (qflx_candrip * fracsnow);
            qflx_prec_grnd_rain = qflx_through_rain + (qflx_candrip * fracrain);
          }
      }   
      else{
         qflx_prec_grnd_snow = 0.;
         qflx_prec_grnd_rain = 0.;
        }

      
      if (n_irrig_steps_left > 0) {
         qflx_irrig         = forc_irrig;
         n_irrig_steps_left = n_irrig_steps_left - 1; }
      else{
         qflx_irrig = 0.0;
        }

      
      qflx_prec_grnd_rain = qflx_prec_grnd_rain + qflx_irrig;

      

      qflx_prec_grnd = qflx_prec_grnd_snow + qflx_prec_grnd_rain;

      if (do_capsnow) {
         qflx_snwcp_liq = qflx_prec_grnd_rain;
         qflx_snwcp_ice = qflx_prec_grnd_snow;

         qflx_snow_grnd_patch = 0.0;
         qflx_rain_grnd = 0.0;  }
      else{

         qflx_snwcp_liq = 0.0;
         qflx_snwcp_ice = 0.0;
         qflx_snow_grnd_patch = qflx_prec_grnd_snow   ;      //ice onto ground (mm/s)
         qflx_rain_grnd     = qflx_prec_grnd_rain      ;   //liquid water onto ground (mm/s)
        }

    }
  }

}

namespace ELM {

185
KOKKOS_INLINE_FUNCTION void CanopyHydrology_FracWet(const int& frac_veg_nosno,
186
187
188
189
190
191
192
193
194
195
196
197
198
199
        const double& h2ocan,
        const double& elai, 
        const double& esai,
        const double& dewmx,
        double& fwet,
        double& fdry)
{  

  double vegt, dewmxi ;    

  if (frac_veg_nosno == 1) {
    if (h2ocan > 0.0) {
        vegt    = frac_veg_nosno*(elai + esai);
        dewmxi  = 1.00/dewmx;
200
201
        fwet = pow(((dewmxi/vegt)*h2ocan), 2.0/3);
        fwet = min(fwet,1.00);   
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
        fdry = (1.0-fwet)*elai/(elai+esai);
      }
    else{
        fwet = 0.0;
        fdry = 0.0 ;
      } }
  else{
     fwet = 0.0;
     fdry = 0.0;
  }

}

}


namespace ELM {

template<typename Array_d>
221
KOKKOS_INLINE_FUNCTION void CanopyHydrology_SnowWater(const double& dtime,
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
        const double& qflx_floodg,
        const int& ltype,
        const int& ctype,
        const bool& urbpoi,
        const bool& do_capsnow,                            
        const int& oldfflag,
        const double& forc_air_temp,
        const double& t_grnd,
        const double& qflx_snow_grnd_col,
        const double& qflx_snow_melt,
        const double& n_melt,
        const double& frac_h2osfc,
        double& snow_depth,
        double& h2osno,
        double& integrated_snow,
        Array_d swe_old,
        Array_d h2osoi_liq,
        Array_d h2osoi_ice,
        Array_d t_soisno,
        Array_d frac_iceold,
        int& snow_level,
        Array_d dz,
        Array_d z,
        Array_d zi,
        int& newnode,
        double& qflx_floodc,
        double& qflx_snow_h2osfc,
        double& frac_sno_eff,
        double& frac_sno)
{       
  
  
//parameters
  double rpi=4.0e0*atan(1.0e0)  ;
  double tfrz=273.15;
  double zlnd = 0.010;

  
  // real(r8), intent(inout), dimension(-nlevsno+1:0)  :: swe_old 
  // real(r8), intent(inout), dimension(-nlevsno+1:0) :: h2osoi_liq, h2osoi_ice
  // real(r8), intent(inout), dimension(-nlevsno+1:0)  :: t_soisno, frac_iceold
  // real(r8), intent(inout), dimension(-nlevsno+1:0)  :: dz, z, zi
  
//local variables 
  double  temp_intsnow, temp_snow_depth, z_avg, fmelt, dz_snowf, snowmelt ;
  double  newsnow, bifall, accum_factor, fsno_new, smr ;
  int j ;

//apply gridcell flood water flux to non-lake columns
  if (ctype != icol_sunwall && ctype != icol_shadewall) {      
     qflx_floodc = qflx_floodg; }
  else{
     qflx_floodc = 0.0;
  }

//Determine snow height and snow water

//Use Alta relationship, Anderson(1976); LaChapelle(1961),
//U.S.Department of Agriculture Forest Service, Project F,
//Progress Rep. 1, Alta Avalanche Study Center:Snow Layer Densification.

  qflx_snow_h2osfc = 0.0;
//set temporary variables prior to updating
  temp_snow_depth=snow_depth;
//save initial snow content
287
288
289
290
291
292
293
294
295
296
297
298
  //for(j = -nlevsno+1; j < snow_level; j++) {
  // for(j = 0; j <= snow_level+nlevsno-1; j++) {
  //    swe_old[j] = 0.00;
  // }
  // //for(j = snow_level+1; j < 0; j++) {
  // for(j = snow_level+nlevsno; j <= 0+nlevsno-1; j++) {
  //    swe_old[j]=h2osoi_liq[j]+h2osoi_ice[j];
  // }
  for(j = 0; j < nlevsno; j++) {
       if(j < nlevsno+snow_level )  swe_old[j]=0.00;
       else  swe_old[j] = h2osoi_liq[j]+h2osoi_ice[j];
     }
299
300
301
302
303
304
305
306
307

  if (do_capsnow) {
     dz_snowf = 0.;
     newsnow = (1. - frac_h2osfc) * qflx_snow_grnd_col * dtime;
     frac_sno=1.;
     integrated_snow = 5.e2; 
  } else {

     if (forc_air_temp > tfrz + 2.) {
308
        bifall=50. + 1.7*pow((17.0),1.5);
309
     } else if (forc_air_temp > tfrz - 15.) {
310
        bifall=50. + 1.7*pow((forc_air_temp - tfrz + 15.),1.5);
311
312
313
314
315
316
317
318
     } else {
        bifall=50.;
     }

     // newsnow is all snow that doesn't fall on h2osfc
     newsnow = (1. - frac_h2osfc) * qflx_snow_grnd_col * dtime;

     // update integrated_snow
319
     integrated_snow = max(integrated_snow,h2osno) ; //h2osno could be larger due to frost
320
321
322
323
324
325
326
327
328
329
330
331
332
333

     // snowmelt from previous time step * dtime
     snowmelt = qflx_snow_melt * dtime;

     // set shape factor for accumulation of snow
     accum_factor=0.1;

     if (h2osno > 0.0) {

        //======================  FSCA PARAMETERIZATIONS  ======================
        // fsca parameterization based on *changes* in swe
        // first compute change from melt during previous time step
        if(snowmelt > 0.) {

334
           smr=min(1.,(h2osno)/(integrated_snow));
335

336
           frac_sno = 1. - pow((acos(fmin(1.,(2.*smr - 1.)))/rpi),(n_melt)) ;
337
338
339
340
341
342
343
344
345

        }

        // update fsca by new snow event, add to previous fsca
        if (newsnow > 0.) {
           fsno_new = 1. - (1. - tanh(accum_factor*newsnow))*(1. - frac_sno);
           frac_sno = fsno_new;

           // reset integrated_snow after accumulation events
346
347
           temp_intsnow= (h2osno + newsnow) / (0.5*(cos(rpi*pow((1.0-max(frac_sno,1e-6)),(1.0/n_melt))+1.0))) ;
           integrated_snow = min(1.e8,temp_intsnow) ;
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
        }

        //====================================================================

        // for subgrid fluxes
        if (subgridflag ==1 && ! urbpoi) {
           if (frac_sno > 0.){
              snow_depth=snow_depth + newsnow/(bifall * frac_sno);
           } else {
              snow_depth=0.;
           }
        } else {
           // for uniform snow cover
           snow_depth=snow_depth+newsnow/bifall;
        }

        // use original fsca formulation (n&y 07)
        if (oldfflag == 1) { 
           // snow cover fraction in Niu et al. 2007
           if(snow_depth > 0.0)  {
368
              frac_sno = tanh(snow_depth/(2.5*zlnd*pow((min(800.0,(h2osno+ newsnow)/snow_depth)/100.0),1.0)) ) ;
369
370
           }
           if(h2osno < 1.0)  {
371
              frac_sno=min(frac_sno,h2osno);
372
373
374
375
376
377
378
379
380
381
382
383
           }
        }

     } else { //h2osno == 0
        // initialize frac_sno and snow_depth when no snow present initially
        if (newsnow > 0.) { 
           z_avg = newsnow/bifall;
           fmelt=newsnow;
           frac_sno = tanh(accum_factor*newsnow);

           // make integrated_snow consistent w/ new fsno, h2osno
           integrated_snow = 0. ;//reset prior to adding newsnow below
384
385
           temp_intsnow= (h2osno + newsnow) / (0.5*(cos(rpi*pow((1.0-max(frac_sno,1e-6)),(1.0/n_melt)))+1.0));
           integrated_snow = min(1.e8,temp_intsnow);
386
387
388
389
390
391
392
393
394
395
396

           // update snow_depth and h2osno to be consistent with frac_sno, z_avg
           if (subgridflag ==1 && !urbpoi) {
              snow_depth=z_avg/frac_sno;
           } else {
              snow_depth=newsnow/bifall;
           }
           // use n&y07 formulation
           if (oldfflag == 1) { 
              // snow cover fraction in Niu et al. 2007
              if(snow_depth > 0.0)  {
397
                 frac_sno = tanh(snow_depth/(2.5*zlnd*pow((min(800.0,newsnow/snow_depth)/100.0),1.0)) );
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
              }
           }
        } else {
           z_avg = 0.;
           snow_depth = 0.;
           frac_sno = 0.;
        }
     } // end of h2osno > 0

     // snow directly falling on surface water melts, increases h2osfc
     qflx_snow_h2osfc = frac_h2osfc*qflx_snow_grnd_col;

     // update h2osno for new snow
     h2osno = h2osno + newsnow ;
     integrated_snow = integrated_snow + newsnow;

     // update change in snow depth
     dz_snowf = (snow_depth - temp_snow_depth) / dtime;

  } //end of do_capsnow construct

  // set frac_sno_eff variable
  if (ltype == istsoil || ltype == istcrop) {
     if (subgridflag ==1) { 
        frac_sno_eff = frac_sno;
     } else {
        frac_sno_eff = 1.;
     }
  } else {
     frac_sno_eff = 1.;
  }

  if (ltype==istwet && t_grnd>tfrz) {
     h2osno=0.;
     snow_depth=0.;
  }

//When the snow accumulation exceeds 10 mm, initialize snow layer
//Currently, the water temperature for the precipitation is simply set
//as the surface air temperature
  newnode = 0 ; //flag for when snow node will be initialized
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//         if (snow_level == 0 && qflx_snow_grnd_col > 0.00 && frac_sno*snow_depth >= 0.010) {
//            newnode = 1;
//            snow_level = -1;
//            dz[0] = snow_depth ;                    //meter
//            z[0] = -0.50*dz[0];
//            zi[-1] = -dz[0];
//            t_soisno[0] = fmin(tfrz, forc_air_temp) ;   //K
//            h2osoi_ice[0] = h2osno ;            //kg/m2
//            h2osoi_liq[0] = 0.0  ;               //kg/m2
//            frac_iceold[0] = 1.0;
//         }

// //The change of ice partial density of surface node due to precipitation.
// //Only ice part of snowfall is added here, the liquid part will be added
// //later.
//         if (snow_level < 0 && newnode == 0) {
//         h2osoi_ice[snow_level+1] = h2osoi_ice[snow_level+1]+newsnow;
//         dz[snow_level+1] = dz[snow_level+1]+dz_snowf*dtime;
      if (snow_level == 0 && qflx_snow_grnd_col > 0.00 && frac_sno*snow_depth >= 0.010) {
   newnode = 1;
   snow_level = -1;
   dz[nlevsno-1] = snow_depth ;                    //meter
   z[nlevsno-1] = -0.50*dz[nlevsno-1];
   zi[nlevsno-2] = -dz[nlevsno-1];
   t_soisno[nlevsno-1] = min(tfrz, forc_air_temp) ;   //K
   h2osoi_ice[nlevsno-1] = h2osno ;            //kg/m2
   h2osoi_liq[nlevsno-1] = 0.0  ;               //kg/m2
   frac_iceold[nlevsno-1] = 1.0;
 }
468

469
470
471
472
473
474
  //The change of ice partial density of surface node due to precipitation.
  //Only ice part of snowfall is added here, the liquid part will be added
  //later.
 if (snow_level < 0 && newnode == 0) {
  h2osoi_ice[nlevsno-1+snow_level+1] = h2osoi_ice[nlevsno-1+snow_level+1]+newsnow;
  dz[nlevsno-1+snow_level+1] = dz[nlevsno-1+snow_level+1]+dz_snowf*dtime;
475
476
477
478
479
480
        }
  }
 }

namespace ELM {

481
KOKKOS_INLINE_FUNCTION void CanopyHydrology_FracH2OSfc(const double& dtime,
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
        const double& min_h2osfc,
        const int& ltype,
        const double& micro_sigma,
        double& h2osno,
        double& h2osfc,
        double& h2osoi_liq,
        double& frac_sno,
        double& frac_sno_eff,
        double& qflx_h2osfc2topsoi,
        double& frac_h2osfc)
  {
    bool no_update = false;
    double shr_const_pi=4.0e0*atan(1.0e0) ;
    bool no_update_l ;

    
    double d,fd,dfdd,sigma   ;

    if (!no_update) { 
      no_update_l = false; }
    else { no_update_l = no_update; }
    
    qflx_h2osfc2topsoi = 0.0 ;
    
    if ( ltype  == istsoil || ltype == istcrop) {

       

       if (h2osfc > min_h2osfc) {
          
          d=0.0 ;

          sigma=1.0e3 * micro_sigma ;
          for(int l = 0 ; l < 10; l++) {
516
             fd = 0.5*d*(1.00+erf(d/(sigma*sqrt(2.0)))) + sigma/sqrt(2.0*shr_const_pi)*exp(-pow(d,2)/(2.0*pow(sigma,2))) -h2osfc;
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
             dfdd = 0.5*(1.00+erf(d/(sigma*sqrt(2.0))));

             d = d - fd/dfdd;
          }
          
          frac_h2osfc = 0.5*(1.00+erf(d/(sigma*sqrt(2.0)))) ;  }

       else {
          frac_h2osfc = 0.0 ;
          h2osoi_liq = h2osoi_liq + h2osfc ;
          qflx_h2osfc2topsoi = h2osfc/dtime ;
          h2osfc=0.0 ;
        }

       if (!no_update_l) {

          
          if (frac_sno > (1.0 - frac_h2osfc) && h2osno > 0) {

             if (frac_h2osfc > 0.010) {
537
                frac_h2osfc = max(1.00 - frac_sno,0.010) ;
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
                frac_sno = 1.00 - frac_h2osfc; }
             else {
                frac_sno = 1.00 - frac_h2osfc;
              }
             
             frac_sno_eff=frac_sno;

          }

       } 
    }  
    else {

       frac_h2osfc = 0.0;

    }

  }

}



561
562
563
564
565
566
567

int main(int argc, char ** argv)
{
  using ELM::Utils::n_months;
  using ELM::Utils::n_pfts;
  using ELM::Utils::n_grid_cells;
  using ELM::Utils::n_max_times;
568
  using ELM::Utils::n_levels_snow;
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
  
  // fixed magic parameters for now
  const int ctype = 1;
  const int ltype = 1;
  const bool urbpoi = false;
  const bool do_capsnow = false;
  const int frac_veg_nosno = 1;
  int n_irrig_steps_left = 0;

  const double dewmx = 0.1;
  const double dtime = 1800.0;

  // fixed magic parameters for SnowWater
  const double qflx_snow_melt = 0.;

  // fixed magic parameters for fracH2Osfc  
  const int oldfflag = 0;
  const double micro_sigma = 0.1;
  const double min_h2osfc = 1.0e-8;
  const double n_melt = 0.7;
589
  double qflx_floodg = 0.0;
590
591
592
593
594
595
  
  Kokkos::initialize( argc, argv );
  {                             
  // phenology input
  typedef Kokkos::View<double*>   ViewVectorType;
  typedef Kokkos::View<double**>  ViewMatrixType;
596
597
  typedef Kokkos::View<int**>  ViewMatrixType1;
  typedef Kokkos::View<int*>   ViewVectorType1;
598
599
  // ELM::Utils::MatrixState elai;
  // ELM::Utils::MatrixState esai;
600
601
  ViewMatrixType elai( "elai", n_grid_cells, n_pfts );
  ViewMatrixType esai( "esai", n_grid_cells, n_pfts );
602
603
604
605
606
607
608
609
610
611
612
613
614
  ViewMatrixType::HostMirror h_elai = Kokkos::create_mirror_view( elai );
  ViewMatrixType::HostMirror h_esai = Kokkos::create_mirror_view( esai );
  ELM::Utils::read_phenology("../links/surfacedataWBW.nc", n_months, n_pfts, 0, h_elai, h_esai);
  ELM::Utils::read_phenology("../links/surfacedataBRW.nc", n_months, n_pfts, n_months, h_elai, h_esai);

  // forcing input
  ViewMatrixType forc_rain( "forc_rain", n_max_times,n_grid_cells );
  ViewMatrixType forc_snow( "forc_snow", n_max_times,n_grid_cells );
  ViewMatrixType forc_air_temp( "forc_air_temp", n_max_times,n_grid_cells );
  ViewMatrixType::HostMirror h_forc_rain = Kokkos::create_mirror_view( forc_rain );
  ViewMatrixType::HostMirror h_forc_snow = Kokkos::create_mirror_view( forc_snow );
  ViewMatrixType::HostMirror h_forc_air_temp = Kokkos::create_mirror_view( forc_air_temp );
  const int n_times = ELM::Utils::read_forcing("../links/forcing", n_max_times, 0, n_grid_cells, h_forc_rain, h_forc_snow, h_forc_air_temp);
615
616
617
618
  // ELM::Utils::MatrixForc forc_irrig; forc_irrig = 0.;
  ViewMatrixType forc_irrig( "forc_irrig", n_max_times,n_grid_cells );
  ViewMatrixType::HostMirror h_forc_irrig = Kokkos::create_mirror_view( forc_irrig );
  
619
620
621
622
623

  
  // mesh input (though can also change as snow layers evolve)
  //
  // NOTE: in a real case, these would be populated, but we don't actually
624
  // // need them to be for these kernels. --etc
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
  // auto z = ELM::Utils::MatrixStateSoilColumn(0.);
  // auto zi = ELM::Utils::MatrixStateSoilColumn(0.);
  // auto dz = ELM::Utils::MatrixStateSoilColumn(0.);
  ViewMatrixType z( "z", n_grid_cells, n_levels_snow );
  ViewMatrixType zi( "zi", n_grid_cells, n_levels_snow );
  ViewMatrixType dz( "dz", n_grid_cells, n_levels_snow );
  ViewMatrixType::HostMirror h_z = Kokkos::create_mirror_view( z );
  ViewMatrixType::HostMirror h_zi = Kokkos::create_mirror_view( zi );
  ViewMatrixType::HostMirror h_dz = Kokkos::create_mirror_view( dz );

  // state variables that require ICs and evolve (in/out)
  // auto h2ocan = ELM::Utils::MatrixStatePFT(0.);
  // auto swe_old = ELM::Utils::MatrixStateSoilColumn(0.);
  // auto h2osoi_liq = ELM::Utils::MatrixStateSoilColumn(0.);
  // auto h2osoi_ice = ELM::Utils::MatrixStateSoilColumn(0.);
  // auto t_soisno = ELM::Utils::MatrixStateSoilColumn(0.);
  // auto frac_iceold = ELM::Utils::MatrixStateSoilColumn(0.);
  ViewMatrixType h2ocan( "h2ocan", n_grid_cells, n_pfts );
  ViewMatrixType swe_old( "swe_old", n_grid_cells, n_levels_snow );
  ViewMatrixType h2osoi_liq( "h2osoi_liq", n_grid_cells, n_levels_snow );
  ViewMatrixType h2osoi_ice( "h2osoi_ice", n_grid_cells, n_levels_snow );
  ViewMatrixType t_soisno( "t_soisno", n_grid_cells, n_levels_snow );
  ViewMatrixType frac_iceold( "frac_iceold", n_grid_cells, n_levels_snow );
  ViewMatrixType::HostMirror h_h2ocan = Kokkos::create_mirror_view( h2ocan );
  ViewMatrixType::HostMirror h_swe_old = Kokkos::create_mirror_view( swe_old );
  ViewMatrixType::HostMirror h_h2osoi_liq = Kokkos::create_mirror_view( h2osoi_liq );
  ViewMatrixType::HostMirror h_h2osoi_ice = Kokkos::create_mirror_view( h2osoi_ice );
  ViewMatrixType::HostMirror h_t_soisno = Kokkos::create_mirror_view( t_soisno );
  ViewMatrixType::HostMirror h_frac_iceold = Kokkos::create_mirror_view( frac_iceold );

  // auto t_grnd = ELM::Utils::VectorColumn(0.);
  // auto h2osno = ELM::Utils::VectorColumn(0.);
  // auto snow_depth = ELM::Utils::VectorColumn(0.);
  // auto snl = ELM::Utils::VectorColumnInt(0.); // note this tracks the snow_depth
  ViewVectorType t_grnd( "t_grnd", n_grid_cells );
  ViewVectorType h2osno( "h2osno", n_grid_cells );
  ViewVectorType snow_depth( "snow_depth", n_grid_cells );
662
  ViewVectorType1 snow_level( "snow_level", n_grid_cells );
663
664
665
  ViewVectorType::HostMirror h_t_grnd = Kokkos::create_mirror_view(  t_grnd);
  ViewVectorType::HostMirror h_h2osno = Kokkos::create_mirror_view( h2osno);
  ViewVectorType::HostMirror h_snow_depth = Kokkos::create_mirror_view(  snow_depth);
666
  ViewVectorType1::HostMirror h_snow_level = Kokkos::create_mirror_view( snow_level);
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700

  // auto h2osfc = ELM::Utils::VectorColumn(0.);
  // auto frac_h2osfc = ELM::Utils::VectorColumn(0.);
  ViewVectorType h2osfc( "h2osfc", n_grid_cells );
  ViewVectorType frac_h2osfc( "frac_h2osfc", n_grid_cells );
  ViewVectorType::HostMirror h_h2osfc = Kokkos::create_mirror_view(  h2osfc);
  ViewVectorType::HostMirror h_frac_h2osfc = Kokkos::create_mirror_view( frac_h2osfc);

  
  // output fluxes by pft
  // auto qflx_prec_intr = ELM::Utils::MatrixStatePFT();
  // auto qflx_irrig = ELM::Utils::MatrixStatePFT();
  // auto qflx_prec_grnd = ELM::Utils::MatrixStatePFT();
  // auto qflx_snwcp_liq = ELM::Utils::MatrixStatePFT();
  // auto qflx_snwcp_ice = ELM::Utils::MatrixStatePFT();
  // auto qflx_snow_grnd_patch = ELM::Utils::MatrixStatePFT();
  // auto qflx_rain_grnd = ELM::Utils::MatrixStatePFT();
  ViewMatrixType qflx_prec_intr( "qflx_prec_intr", n_grid_cells, n_pfts );
  ViewMatrixType qflx_irrig( "qflx_irrig", n_grid_cells, n_pfts  );
  ViewMatrixType qflx_prec_grnd( "qflx_prec_grnd", n_grid_cells, n_pfts  );
  ViewMatrixType qflx_snwcp_liq( "qflx_snwcp_liq", n_grid_cells, n_pfts );
  ViewMatrixType qflx_snwcp_ice ( "qflx_snwcp_ice ", n_grid_cells, n_pfts  );
  ViewMatrixType qflx_snow_grnd_patch( "qflx_snow_grnd_patch", n_grid_cells, n_pfts  );
  ViewMatrixType qflx_rain_grnd( "qflx_rain_grnd", n_grid_cells, n_pfts  );
  ViewMatrixType::HostMirror h_qflx_prec_intr = Kokkos::create_mirror_view( qflx_prec_intr );
  ViewMatrixType::HostMirror h_qflx_irrig = Kokkos::create_mirror_view( qflx_irrig);
  ViewMatrixType::HostMirror h_qflx_prec_grnd = Kokkos::create_mirror_view( qflx_prec_grnd);
  ViewMatrixType::HostMirror h_qflx_snwcp_liq = Kokkos::create_mirror_view(  qflx_snwcp_liq);
  ViewMatrixType::HostMirror h_qflx_snwcp_ice = Kokkos::create_mirror_view( qflx_snwcp_ice   );
  ViewMatrixType::HostMirror h_qflx_snow_grnd_patch = Kokkos::create_mirror_view( qflx_snow_grnd_patch );
  ViewMatrixType::HostMirror h_qflx_rain_grnd = Kokkos::create_mirror_view(  qflx_rain_grnd  );

  // FIXME: I have no clue what this is... it is inout on WaterSnow.  For now I
  // am guessing the data structure. Ask Scott.  --etc
701
702
703
  //auto integrated_snow = ELM::Utils::VectorColumn(0.);
  ViewVectorType integrated_snow( "integrated_snow", n_grid_cells );
  ViewVectorType::HostMirror h_integrated_snow = Kokkos::create_mirror_view(  integrated_snow);
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
  
  // output fluxes, state by the column
  // auto qflx_snow_grnd_col = ELM::Utils::VectorColumn();
  // auto qflx_snow_h2osfc = ELM::Utils::VectorColumn();
  // auto qflx_h2osfc2topsoi = ELM::Utils::VectorColumn();
  // auto qflx_floodc = ELM::Utils::VectorColumn();
  ViewVectorType qflx_snow_grnd_col( "qflx_snow_grnd_col", n_grid_cells );
  ViewVectorType qflx_snow_h2osfc( "qflx_snow_h2osfc", n_grid_cells );
  ViewVectorType qflx_h2osfc2topsoi( "qflx_h2osfc2topsoi", n_grid_cells );
  ViewVectorType qflx_floodc( "qflx_floodc", n_grid_cells );
  ViewVectorType::HostMirror h_qflx_snow_grnd_col = Kokkos::create_mirror_view(  qflx_snow_grnd_col);
  ViewVectorType::HostMirror h_qflx_snow_h2osfc = Kokkos::create_mirror_view( qflx_snow_h2osfc);
  ViewVectorType::HostMirror h_qflx_h2osfc2topsoi = Kokkos::create_mirror_view(  qflx_h2osfc2topsoi);
  ViewVectorType::HostMirror h_qflx_floodc = Kokkos::create_mirror_view( qflx_floodc);

  // auto frac_sno_eff = ELM::Utils::VectorColumn();
  // auto frac_sno = ELM::Utils::VectorColumn();
  ViewVectorType frac_sno_eff( "frac_sno_eff", n_grid_cells );
  ViewVectorType frac_sno( "frac_sno", n_grid_cells );
  ViewVectorType::HostMirror h_frac_sno_eff = Kokkos::create_mirror_view(  frac_sno_eff);
  ViewVectorType::HostMirror h_frac_sno = Kokkos::create_mirror_view( frac_sno);
  

  // std::cout << "Time\t Total Canopy Water\t Min Water\t Max Water" << std::endl;
728
  // auto min_max = std::minmax_element(&h_h2ocan(0,0), end1+1);
729
  // std::cout << std::minmax_element(16)
730
  //           << 0 << "\t" << std::accumulate(&h_h2ocan(0,0), end1+1, 0.)
731
732
  //           << "\t" << *min_max.first
  //           << "\t" << *min_max.second << std::endl;
733
734

    
735
736
737
738
739
  
  Kokkos::deep_copy( elai, h_elai);
  Kokkos::deep_copy( esai, h_esai);
  Kokkos::deep_copy( forc_rain, h_forc_rain);
  Kokkos::deep_copy( forc_snow, h_forc_snow);
740
  Kokkos::deep_copy( forc_irrig, h_forc_irrig);
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
  Kokkos::deep_copy( forc_air_temp, h_forc_air_temp);
  Kokkos::deep_copy( z, h_z);
  Kokkos::deep_copy( zi, h_zi);
  Kokkos::deep_copy( dz, h_dz);
  Kokkos::deep_copy( h2ocan, h_h2ocan);
  Kokkos::deep_copy( swe_old, h_swe_old);
  Kokkos::deep_copy( h2osoi_liq, h_h2osoi_liq);
  Kokkos::deep_copy( h2osoi_ice, h_h2osoi_ice);
  Kokkos::deep_copy( t_soisno, h_t_soisno);
  Kokkos::deep_copy( frac_iceold, h_frac_iceold);
  Kokkos::deep_copy( t_grnd, h_t_grnd);
  Kokkos::deep_copy( h2osno, h_h2osno);
  Kokkos::deep_copy( snow_depth, h_snow_depth);
  Kokkos::deep_copy( snow_level, h_snow_level);
  Kokkos::deep_copy( h2osfc, h_h2osfc);
  Kokkos::deep_copy( frac_h2osfc, h_frac_h2osfc);
  Kokkos::deep_copy( qflx_prec_intr,h_qflx_prec_intr);
  Kokkos::deep_copy( qflx_irrig,h_qflx_irrig);
  Kokkos::deep_copy( qflx_prec_grnd,h_qflx_prec_grnd);
  Kokkos::deep_copy( qflx_snwcp_liq,h_qflx_snwcp_liq);
  Kokkos::deep_copy( qflx_snwcp_ice,h_qflx_snwcp_ice);
  Kokkos::deep_copy( qflx_snow_grnd_patch,h_qflx_snow_grnd_patch);
  Kokkos::deep_copy( qflx_rain_grnd,h_qflx_rain_grnd);
764
  Kokkos::deep_copy( integrated_snow,h_integrated_snow);
765
766
767
768
769
770
771
  Kokkos::deep_copy( qflx_snow_grnd_col, h_qflx_snow_grnd_col);
  Kokkos::deep_copy( qflx_snow_h2osfc, h_qflx_snow_h2osfc);
  Kokkos::deep_copy( qflx_h2osfc2topsoi, h_qflx_h2osfc2topsoi);
  Kokkos::deep_copy( qflx_floodc, h_qflx_floodc);
  Kokkos::deep_copy( frac_sno_eff, h_frac_sno_eff);
  Kokkos::deep_copy( frac_sno, h_frac_sno);

772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
  double* end1 = &h_h2ocan(n_grid_cells-1, n_pfts-1) ;
  double* end2 = &h_h2osno(n_grid_cells-1) ;
  double* end3 = &h_frac_h2osfc(n_grid_cells-1) ;
  std::cout << "Time\t Total Canopy Water\t Min Water\t Max Water\t Total Snow\t Min Snow\t Max Snow\t Avg Frac Sfc\t Min Frac Sfc\t Max Frac Sfc" << std::endl;
  auto min_max_water = std::minmax_element(&h_h2ocan(0,0), end1+1);
  auto sum_water = std::accumulate(&h_h2ocan(0,0), end1+1, 0.);

  auto min_max_snow = std::minmax_element(&h_h2osno(0), end2+1);
  auto sum_snow = std::accumulate(&h_h2osno(0), end2+1, 0.);

  auto min_max_frac_sfc = std::minmax_element(&h_frac_h2osfc(0), end3+1);
  auto avg_frac_sfc = std::accumulate(&h_frac_h2osfc(0), end3+1, 0.) / (end3+1 - &h_frac_h2osfc(0));

  std::cout << std::setprecision(16)
            << 0 << "\t" << sum_water << "\t" << *min_max_water.first << "\t" << *min_max_water.second
            << "\t" << sum_snow << "\t" << *min_max_snow.first << "\t" << *min_max_snow.second
            << "\t" << avg_frac_sfc << "\t" << *min_max_frac_sfc.first << "\t" << *min_max_frac_sfc.second << std::endl;

790
791
792
793
794
795
796
797
798
799

  // main loop
  // -- the timestep loop cannot/should not be parallelized
  for (size_t t = 0; t != n_times; ++t) {

    // grid cell and/or pft loop can be parallelized
    //for (size_t g = 0; g != n_grid_cells; ++g) {

      // PFT level operations
      //for (size_t p = 0; p != n_pfts; ++p) {
800
    Kokkos::parallel_for("n_grid_cells", n_grid_cells, KOKKOS_LAMBDA (const size_t& g) {
801
      for (size_t p = 0; p != n_pfts; ++p) {
802
803
804
805
        //
        // Calculate interception
        //
        // NOTE: this currently punts on what to do with the qflx variables!
806
        // Surely they should be either std::accumulated or stored on PFTs as well.
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
        // --etc
        ELM::CanopyHydrology_Interception(dtime,
                forc_rain(t,g), forc_snow(t,g), forc_irrig(t,g),
                ltype, ctype, urbpoi, do_capsnow,
                elai(g,p), esai(g,p), dewmx, frac_veg_nosno,
                h2ocan(g,p), n_irrig_steps_left,
                qflx_prec_intr(g,p), qflx_irrig(g,p), qflx_prec_grnd(g,p),
                qflx_snwcp_liq(g,p), qflx_snwcp_ice(g,p),
                qflx_snow_grnd_patch(g,p), qflx_rain_grnd(g,p));
        //printf("%i %i %16.8g %16.8g %16.8g %16.8g %16.8g %16.8g\n", g, p, forc_rain(t,g), forc_snow(t,g), elai(g,p), esai(g,p), h2ocan(g,p), qflx_prec_intr(g));

        //
        // Calculate fraction of LAI that is wet vs dry.
        //
        // FIXME: this currently punts on what to do with the fwet/fdry variables.
        // Surely they should be something, as such this is dead code.
        // By the PFT?
        // --etc
        double fwet = 0., fdry = 0.;
        ELM::CanopyHydrology_FracWet(frac_veg_nosno, h2ocan(g,p), elai(g,p), esai(g,p), dewmx, fwet, fdry);
827
      } // end PFT loop
828
829

      // Column level operations
830
      
831
      double* qpatch = &qflx_snow_grnd_patch(n_grid_cells-1, n_pfts-1);
832
      // NOTE: this is effectively an accumulation kernel/task! --etc
833
834
835
836
837
838
839
840
841
      //qflx_snow_grnd_col(g) = std::accumulate(&qflx_snow_grnd_patch(0,0), qpatch+1, 0.);
      // for (int x = 0; x <n_grid_cells; x++) {
      double sum = 0 ;    
      for (size_t p = 0; p != n_pfts; ++p) {
      sum += qflx_snow_grnd_patch(g,p);
      }
      qflx_snow_grnd_col(g) = sum ; 
      
      
842
843
844
845
846
847

      // Calculate ?water balance? on the snow column, adding throughfall,
      // removing melt, etc.
      //
      // local outputs
      int newnode;
848

849
850
851
852
      ELM::CanopyHydrology_SnowWater(dtime, qflx_floodg,
              ltype, ctype, urbpoi, do_capsnow, oldfflag,
              forc_air_temp(t,g), t_grnd(g),
              qflx_snow_grnd_col(g), qflx_snow_melt, n_melt, frac_h2osfc(g),
853
              snow_depth(g), h2osno(g), integrated_snow(g), Kokkos::subview(swe_old, g , Kokkos::ALL),
854
855
              Kokkos::subview(h2osoi_liq, g , Kokkos::ALL), Kokkos::subview(h2osoi_ice, g , Kokkos::ALL), Kokkos::subview(t_soisno, g , Kokkos::ALL), Kokkos::subview(frac_iceold, g , Kokkos::ALL),
              snow_level(g), Kokkos::subview(dz, g , Kokkos::ALL), Kokkos::subview(z, g , Kokkos::ALL), Kokkos::subview(zi, g , Kokkos::ALL), newnode,
856
857
858
859
860
861
862
863
864
865
866
867
868
              qflx_floodc(g), qflx_snow_h2osfc(g), frac_sno_eff(g), frac_sno(g));

      // Calculate Fraction of Water to the Surface?
      //
      // FIXME: Fortran black magic... h2osoi_liq is a vector, but the
      // interface specifies a single double.  For now passing the 0th
      // entry. --etc
      ELM::CanopyHydrology_FracH2OSfc(dtime, min_h2osfc, ltype, micro_sigma,
              h2osno(g), h2osfc(g), h2osoi_liq(g,0), frac_sno(g), frac_sno_eff(g),
              qflx_h2osfc2topsoi(g), frac_h2osfc(g));
      
    }); // end grid cell loop

869
870
871
872
873
874
875
876
877
878
879
880
    // Kokkos::parallel_reduce( n_pfts-1, KOKKOS_LAMBDA ( int j ) {
    //   double sum = 0;

    //   for ( int i = 0; i < n_grid_cells-1; ++i ) {
    //     sum += qflx_snow_grnd_patch( j, i );
    //   }

    //   qflx_snow_grnd_col(j) = sum ;
    //   });



881
    // auto min_max = std::minmax_element(&h_h2ocan(0,0), end1+1);
882
    // std::cout << std::minmax_element(16)
883
    //           << t+1 << "\t" << std::accumulate(&h_h2ocan(0,0), end1+1, 0.)
884
885
886
    //           << "\t" << *min_max.first
    //           << "\t" << *min_max.second << std::endl;

887
888
889
890
891
892
893
894
895
896
    auto min_max_water = std::minmax_element(&h_h2ocan(0,0), end1+1);
    auto sum_water = std::accumulate(&h_h2ocan(0,0), end1+1, 0.);

    auto min_max_snow = std::minmax_element(&h_h2osno(0), end2+1);
    auto sum_snow = std::accumulate(&h_h2osno(0), end2+1, 0.);

    auto min_max_frac_sfc = std::minmax_element(&h_frac_h2osfc(0), end3+1);
    auto avg_frac_sfc = std::accumulate(&h_frac_h2osfc(0), end3+1, 0.) / (end3+1 - &h_frac_h2osfc(0));
                  
    std::cout << std::setprecision(16)
897
              << t+1 << "\t" << sum_water << "\t" << *min_max_water.first << "\t" << *min_max_water.second
898
899
900
              << "\t" << sum_snow << "\t" << *min_max_snow.first << "\t" << *min_max_snow.second
              << "\t" << avg_frac_sfc << "\t" << *min_max_frac_sfc.first << "\t" << *min_max_frac_sfc.second << std::endl;

901
902
903
904
905
  } // end timestep loop
  }
  Kokkos::finalize();
  return 0;
}