CanopyHydrology_kern1_multiple.cpp 12.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <array>
#include <sstream>
#include <iterator>
#include <exception>
#include <string>
#include <stdlib.h>
#include <cstring>
#include <vector>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <algorithm>
#include <Kokkos_Core.hpp>
#include "utils.hh"
#include "readers.hh"
16
17
#include "landunit_varcon.h"
#include "column_varcon.h" 
18
19


20
namespace ELM {
Pillai, Himanshu's avatar
Pillai, Himanshu committed
21
KOKKOS_INLINE_FUNCTION void CanopyHydrology_Interception(double dtime,
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        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;
               


               
               h2ocan = fmax(0.0, h2ocan + dtime*qflx_prec_intr);

               
               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)
        }

    }
  }

}

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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;

using MatrixState = MatrixStatic<n_grid_cells, n_pfts>;
using MatrixForc = MatrixStatic<n_max_times,n_grid_cells>;


} // namespace
} // namespace


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;
  
  // 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;
  double dtime = 1800.0;

Pillai, Himanshu's avatar
Pillai, Himanshu committed
198
199
  
  Kokkos::initialize( );//argc, argv );
200
201
202
  {

  typedef Kokkos::View<double**>  ViewMatrixType;
Pillai, Himanshu's avatar
Pillai, Himanshu committed
203
204
205
  // typedef Kokkos::Cuda ExecSpace;
  // typedef Kokkos::Cuda MemSpace;
  // typedef Kokkos::RangePolicy<ExecSpace> range_policy;
206
 
207
208
  ViewMatrixType elai( "elai", n_grid_cells, n_pfts );
  ViewMatrixType esai( "esai", n_grid_cells, n_pfts );
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  ViewMatrixType::HostMirror h_elai = Kokkos::create_mirror_view( elai );
  ViewMatrixType::HostMirror h_esai = Kokkos::create_mirror_view( esai );

  // phenology state
  // ELM::Utils::MatrixState elai;
  // ELM::Utils::MatrixState 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 state
  // ELM::Utils::MatrixForc forc_rain;
  // ELM::Utils::MatrixForc forc_snow;
  // ELM::Utils::MatrixForc forc_air_temp;

  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);
230
231
232
  ViewMatrixType forc_irrig( "forc_irrig", n_max_times,n_grid_cells );
  ViewMatrixType::HostMirror h_forc_irrig = Kokkos::create_mirror_view( forc_irrig );
  //ELM::Utils::MatrixForc forc_irrig; forc_irrig = 0.;
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
  
  // output state by the grid cell
  // auto qflx_prec_intr = std::array<double,n_grid_cells>();
  // auto qflx_irrig = std::array<double,n_grid_cells>();
  // auto qflx_prec_grnd = std::array<double,n_grid_cells>();
  // auto qflx_snwcp_liq = std::array<double,n_grid_cells>();
  // auto qflx_snwcp_ice = std::array<double,n_grid_cells>();
  // auto qflx_snow_grnd_patch = std::array<double,n_grid_cells>();
  // auto qflx_rain_grnd = std::array<double,n_grid_cells>();
  // auto qflx_prec_intr = ELM::Utils::MatrixState();
  // auto qflx_irrig = ELM::Utils::MatrixState();
  // auto qflx_prec_grnd = ELM::Utils::MatrixState();
  // auto qflx_snwcp_liq = ELM::Utils::MatrixState();
  // auto qflx_snwcp_ice = ELM::Utils::MatrixState();
  // auto qflx_snow_grnd_patch = ELM::Utils::MatrixState();
  // auto qflx_rain_grnd = ELM::Utils::MatrixState();
  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  );

  // output state by the pft
  ViewMatrixType h2o_can( "h2o_can", n_grid_cells, n_pfts );
  //auto h2o_can = ELM::Utils::MatrixState(); 
  ViewMatrixType::HostMirror h_h2o_can = Kokkos::create_mirror_view( h2o_can );
  //h_h2o_can = 0.;
269
  //auto h2o_can1 = ELM::Utils::MatrixState(); 
270
  // Array<int64_t, 2> a = h_h2o_can;
271
  //const size_t n0 = h2o_can.extent_0 ();
272
273
274
275
276
277
278

  //   const int64_t begin0 = h_h2o_can.begin();
  //   const int64_t end0= h_h2o_can.end();
  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);
279
  Kokkos::deep_copy( forc_irrig, h_forc_irrig);
280
281
282
283
284
285
286
287
288
289
  Kokkos::deep_copy( forc_air_temp, h_forc_air_temp);
  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);
  Kokkos::deep_copy( h2o_can, h_h2o_can);

290
  double* end = &h_h2o_can(n_grid_cells-1, n_pfts-1) ;
291
292

  std::cout << "Time\t Total Canopy Water\t Min Water\t Max Water" << std::endl;
293
  auto min_max = std::minmax_element(&h_h2o_can(0,0), end+1);//h2o_can1.begin(), h2o_can1.end());
294
  std::cout << std::setprecision(16)
295
            << 0 << "\t" << std::accumulate(&h_h2o_can(0,0), end+1, 0.) //h2o_can1.begin(), h2o_can1.end(), 0.)
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
            << "\t" << *min_max.first
            << "\t" << *min_max.second << std::endl;

  



  
  // 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
    // Kokkos::parallel_for(range_policy(0,n_grid_cells), KOKKOS_LAMBDA ( size_t g ) {
    // //for (size_t g = 0; g != n_grid_cells; ++g) {
    //   //Kokkos::parallel_for(range_policy(0,n_pfts), KOKKOS_LAMBDA ( size_t p ) { 
    //     for (size_t p = 0; p != n_pfts; ++p) {
    //     // NOTE: this currently punts on what to do with the qflx variables!
    //     // Surely they should be either accumulated or stored on PFTs as well.
    //     // --etc

    // Kokkos::parallel_for("CanopyHydrology_Interception", Kokkos::MDRangePolicy<Kokkos::Rank<2,Kokkos::Iterate::Left>>({0,0},{n_grid_cells,n_pfts}),
    //    KOKKOS_LAMBDA (size_t g, size_t p) {
319
    Kokkos::parallel_for("n_grid_cells", n_grid_cells, KOKKOS_LAMBDA (const size_t& g) {
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
      for (size_t p = 0; p != n_pfts; ++p) {
        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,
                h2o_can(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));

                // qflx_prec_intr[g], qflx_irrig[g], qflx_prec_grnd[g],
                // qflx_snwcp_liq[g], qflx_snwcp_ice[g],
                // qflx_snow_grnd_patch[g], qflx_rain_grnd[g]);
        //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), h2o_can(g,p), qflx_prec_intr[g]);
      //}//)
      }
    });

338
    auto min_max = std::minmax_element(&h_h2o_can(0,0), end+1);//h2o_can1.begin(), h2o_can1.end());
339
    std::cout << std::setprecision(16)
340
              << t+1 << "\t" << std::accumulate(&h_h2o_can(0,0), end+1, 0.)//h2o_can1.begin(), h2o_can1.end(), 0.)
341
342
343
344
345
              << "\t" << *min_max.first
              << "\t" << *min_max.second << std::endl;

  }
  }
346
  //Kokkos::finalize();
Pillai, Himanshu's avatar
Pillai, Himanshu committed
347
  Kokkos::finalize();
348
349
  return 0;
}