Alpine3D

Alpine3D Svn Source Tree

Root/trunk/alpine3d/SnowpackInterface.cc

1/***********************************************************************************/
2/* Copyright 2009-2015 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
3/***********************************************************************************/
4/* This file is part of Alpine3D.
5 Alpine3D is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 Alpine3D is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with Alpine3D. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include <alpine3d/SnowpackInterface.h>
19#include <alpine3d/AlpineMain.h>
20#include <alpine3d/MPIControl.h>
21#include <alpine3d/OMPControl.h>
22
23#include <errno.h>
24#include <algorithm>
25
26using namespace std;
27using namespace mio;
28
29//sort the by increasing y and increasing x as a second key
30inline bool pair_comparator(const std::pair<double, double>& l, const std::pair<double, double>& r)
31{
32if (l.second == r.second)
33return l.first < r.first;
34
35return l.second < r.second;
36}
37
38//convert the POI to grid index representation and warn of duplicates
39std::vector< std::pair<size_t,size_t> > prepare_pts(const std::vector<Coords>& vec_pts)
40{
41std::vector< std::pair<size_t,size_t> > pts;
42std::vector<size_t> vec_idx;
43for (size_t ii=0; ii<vec_pts.size(); ii++) {
44const size_t ix = vec_pts[ii].getGridI();
45const size_t iy = vec_pts[ii].getGridJ();
46std::pair<size_t,size_t> tmp(ix,iy);
47
48const std::vector< std::pair<size_t,size_t> >::const_iterator it = std::find(pts.begin(), pts.end(), tmp);
49if (it != pts.end()) {
50if (MPIControl::instance().master()) {
51const size_t orig_idx = vec_idx[ it - pts.begin() ];
52std::cout << "[W] POI #" << ii << " " << vec_pts[ii].toString(Coords::CARTESIAN);
53std::cout << " is a duplicate of POI #" << orig_idx << " " << vec_pts[orig_idx].toString(Coords::CARTESIAN) << std::endl; //flush for openmp
54}
55} else {
56pts.push_back( tmp );
57vec_idx.push_back( ii ); //in order to be able to find out where a duplicate is originating from
58}
59}
60sort(pts.begin(), pts.end(), pair_comparator);
61return pts;
62}
63
64/**
65 * @brief Constructs and initialise Snowpack Interface Master. He creates the Worker and
66 * Distributes the Data from the other modules to the Worker. Is the acces interface A3D side
67 * @param io_cfg is used to init Runoff and to create IOManager, which is used to write the standart output
68 * @param nbworkers gives the new values for the wind speed
69 * @param dem_in gives the demographic Data. Also tetermines size and position of the geographical modeling scope
70 * @param landuse_in gives the landuse Data. Also tetermines size and position of the landuse for modeling scope
71 * @param vec_pts gives the spezial points. For this points more output is done then for the others. Calcualtion is the same.
72 * @param startTime is the time and date the first simulation step is done
73 * @param grids_requirements list of grids that must be prepared for other modules (similar to the Output::GRIDS_PARAMETERS configuration key)
74 * @param is_restart_in is used to know which files the worker needs to read to init the pixel
75 */
76SnowpackInterface::SnowpackInterface(const mio::Config& io_cfg, const size_t& nbworkers,
77 const mio::DEMObject& dem_in,
78 const mio::Grid2DObject& landuse_in,
79 const std::vector<mio::Coords>& vec_pts,
80 const mio::Date &startTime,
81 const std::string& grids_requirements,
82 const bool is_restart_in)
83 : run_info(), io(io_cfg), pts(prepare_pts(vec_pts)),dem(dem_in),
84 is_restart(is_restart_in), useCanopy(false), enable_lateral_flow(false), a3d_view(false),
85 do_io_locally(true), station_name(),glacier_katabatic_flow(false), snow_production(false), snow_grooming(false),
86 Tsoil_idx(), grids_start(0), grids_days_between(0), ts_start(0.), ts_days_between(0.), prof_start(0.), prof_days_between(0.),
87 grids_write(true), ts_write(false), prof_write(false), snow_write(false), snow_poi_written(false), glacier_from_grid(false),
88 meteo_outpath(), outpath(), mask_glaciers(false), mask_dynamic(false), maskGlacier(), tz_out(0.),
89 sn_cfg(readAndTweakConfig(io_cfg, !pts.empty())), snowpackIO(sn_cfg), dimx(dem_in.getNx()), dimy(dem_in.getNy()), mpi_offset(0), mpi_nx(dimx),
90 landuse(landuse_in), mns(dem_in, IOUtils::nodata), shortwave(dem_in, IOUtils::nodata), longwave(dem_in, IOUtils::nodata), diffuse(dem_in, IOUtils::nodata),
91 psum(dem_in, IOUtils::nodata), psum_ph(dem_in, IOUtils::nodata), psum_tech(dem_in, IOUtils::nodata), grooming(dem_in, IOUtils::nodata),
92 vw(dem_in, IOUtils::nodata), dw(dem_in, IOUtils::nodata), rh(dem_in, IOUtils::nodata), ta(dem_in, IOUtils::nodata), init_glaciers_height(dem_in, IOUtils::nodata),
93 solarElevation(0.), output_grids(), workers(nbworkers), worker_startx(nbworkers), worker_deltax(nbworkers), worker_stations_coord(nbworkers),
94 timer(), nextStepTimestamp(startTime), timeStep(dt_main/86400.), dataMeteo2D(false), dataDa(false), dataSnowDrift(false), dataRadiation(false),
95 drift(NULL), eb(NULL), da(NULL), runoff(NULL), glaciers(NULL), techSnow(NULL)
96{
97MPIControl& mpicontrol = MPIControl::instance();
98
99std::vector<SnowStation*> snow_stations;
100std::vector<std::pair<size_t,size_t> > snow_stations_coord;
101
102// Check if glacier height should be obtained from a grid.
103// This option overwrite landuse information.
104// To use it the following set of keys should be provided:
105// GLACIER_FROM_GRID = TRUE
106// GLACIER = ARC (for now no other plugin is supported)
107// GLACIERFILE = path_to_glacier_file
108// The glacier file (grid simmilar to the DEM) should contain glacier height in meters,
109// and 0 or nodata for glacier free pixels.
110sn_cfg.getValue("GLACIER_FROM_GRID", "input", glacier_from_grid,IOUtils::nothrow);
111if(glacier_from_grid)
112{
113setInitGlacierHeight();
114}
115
116readInitalSnowCover(snow_stations,snow_stations_coord);
117
118if (mpicontrol.master()) {
119std::cout << "[i] SnowpackInterface initializing a total of " << mpicontrol.size();
120if (mpicontrol.size()>1) std::cout << " processes with " << nbworkers;
121else std::cout << " process with " << nbworkers;
122if (nbworkers>1) std::cout << " workers";
123else std::cout << " worker";
124std::cout << " each using Snowpack " << snowpack::getLibVersion() << "\n";
125}
126
127//create and prepare the vector of output grids
128if (grids_write) {
129sn_cfg.getValue("GRIDS_PARAMETERS", "output", output_grids);
130std::vector<double> soil_temp_depths;
131sn_cfg.getValue("SOIL_TEMPERATURE_DEPTHS", "Output", soil_temp_depths, IOUtils::nothrow);
132const unsigned short max_Tsoil( SnGrids::lastparam - SnGrids::TSOIL1 + 1 );
133if (soil_temp_depths.size()>max_Tsoil)
134throw InvalidArgumentException("Too many soil temperatures requested", AT);
135for (size_t ii=0; ii<soil_temp_depths.size(); ii++) {
136std::stringstream ss;
137ss << (ii+1);
138const std::string ii_str(ss.str());
139Tsoil_idx.push_back( ii_str );
140output_grids.push_back( "TSOIL"+ii_str );
141}
142SnowpackInterfaceWorker::uniqueOutputGrids(output_grids);
143}
144//add the grids that are necessary for the other modules
145const std::string all_grids = sn_cfg.get("GRIDS_PARAMETERS", "output", "");
146sn_cfg.addKey("GRIDS_PARAMETERS", "output", all_grids + " " + grids_requirements + " " + getGridsRequirements()); //also consider own requirements
147
148//check if lateral flow is enabled
149sn_cfg.getValue("LATERAL_FLOW", "Alpine3D", enable_lateral_flow, IOUtils::nothrow);
150
151//check if A3D view should be used for grids
152sn_cfg.getValue("A3D_VIEW", "Output", a3d_view, IOUtils::nothrow);
153
154//If MPI is active, every node gets a slice of the DEM to work on
155mpicontrol.getArraySliceParamsOptim(dimx, mpi_offset, mpi_nx,dem,landuse);
156std::cout << "[i] MPI instance "<< mpicontrol.rank() <<" for solving snowpack : grid range = ["
157<< mpi_offset << " to " << mpi_offset+mpi_nx-1 << "] " << mpi_nx << " columns\n";
158//Cut DEM and landuse in MPI domain, MPI domain are computed
159//by trying to havve the same number of cell to compute per domain
160const DEMObject mpi_sub_dem(dem_in, mpi_offset, 0, mpi_nx, dimy, false);
161const Grid2DObject mpi_sub_landuse(landuse_in, mpi_offset, 0, mpi_nx, dimy);
162std::vector<std::vector<size_t> > omp_snow_stations_ind;
163//The OMP slicing for snowpack computation is not based on rectangular domain.
164//It return a table of pixel to compute and coordiantes to have the same
165//number of pixel per slice.
166OMPControl::getArraySliceParamsOptim(nbworkers,snow_stations,mpi_sub_dem, mpi_sub_landuse,omp_snow_stations_ind);
167// construct slices and workers
168#pragma omp parallel for schedule(static)
169for (size_t ii=0; ii<nbworkers; ii++) {
170// Could be optimised, but not really big gain.
171// Each worker will check again the point to be sure they belong
172// to it, so no need to double check here
173std::vector< std::pair<size_t,size_t> > sub_pts;
174const size_t n_pts = pts.size();
175for (size_t kk=0; kk<n_pts; kk++) { // could be optimised... but not really big gain
176if (pts[kk].first >= mpi_offset && pts[kk].first < mpi_offset + mpi_nx) {
177sub_pts.push_back( pts[kk] );
178sub_pts.back().first -= mpi_offset;
179}
180}
181// In this implementation, all OMP workers see the whole MPI grid
182// This could be change but with this when passing the meteo grids
183// Only one slicing and copy is necessary per MPI, insetead of one per OMP
184const DEMObject sub_dem(mpi_sub_dem);
185const Grid2DObject sub_landuse(mpi_sub_landuse);
186
187// Generate workers
188std::vector<SnowStation*> thread_stations;
189std::vector<std::pair<size_t,size_t> > thread_stations_coord;
190for (std::vector<size_t>::iterator it = omp_snow_stations_ind.at(ii).begin(); it != omp_snow_stations_ind.at(ii).end(); ++it){
191thread_stations.push_back (snow_stations.at(*it));
192thread_stations_coord.push_back(snow_stations_coord.at(*it));
193}
194
195// The OMP slicing into rectangle is only used for post computation
196// Over the grid (i.e. lateral flow and snow preparation)
197size_t omp_offset, omp_nx;
198OMPControl::getArraySliceParams(mpi_nx, nbworkers, ii, omp_offset, omp_nx);
199const size_t offset = mpi_offset + omp_offset;
200
201workers[ii] = new SnowpackInterfaceWorker(sn_cfg, sub_dem, sub_landuse, sub_pts, thread_stations, thread_stations_coord, offset);
202
203worker_startx[ii] = offset;
204worker_deltax[ii] = omp_nx;
205#pragma omp critical(snowpackWorkers_status)
206{
207const std::pair<size_t,size_t> coord_start(snow_stations_coord.at(omp_snow_stations_ind.at(ii).front()));
208const std::pair<size_t,size_t> coord_end(snow_stations_coord.at(omp_snow_stations_ind.at(ii).back()));
209
210std::cout << "[i] SnowpackInterface worker for solving snowpack " << ii
211<< " on process " << mpicontrol.rank() << ": coord. x-y range = [" << coord_start.first + mpi_offset << "-"
212<< coord_start.second << " to " << coord_end.first + mpi_offset << "-"
213<< coord_end.second<<"] " << omp_snow_stations_ind.at(ii).size() << " cells\n";
214std::cout << "[i] SnowpackInterface worker for grid computation " << ii
215<< " on process " << mpicontrol.rank() << ": grid range = [" << offset
216<< " to " << omp_nx+offset-1 << "] " << omp_nx << " columns\n";
217}
218}
219
220// init glacier map (after creating and init workers) for output
221if (mask_glaciers || glacier_katabatic_flow) {
222maskGlacier = getGrid(SnGrids::GLACIER);
223if (glacier_katabatic_flow) {
224glaciers = new Glaciers(io_cfg, dem);
225glaciers->setGlacierMap(maskGlacier);
226}
227}
228
229//init snow preparation
230sn_cfg.getValue("SNOW_GROOMING", "TechSnow", snow_grooming, IOUtils::nothrow);
231sn_cfg.getValue("SNOW_PRODUCTION", "TechSnow", snow_production, IOUtils::nothrow);
232if (snow_production || snow_grooming) {
233techSnow = new TechSnowA3D(io_cfg, dem);
234}
235}
236
237SnowpackInterface& SnowpackInterface::operator=(const SnowpackInterface& source) {
238if (this != &source) {
239run_info = source.run_info;
240snowpackIO = source.snowpackIO;
241dimx = source.dimx;
242dimy = source.dimy;
243landuse = source.landuse;
244mns = source.mns;
245shortwave = source.shortwave;
246longwave = source.longwave;
247diffuse = source.diffuse;
248psum = source.psum;
249psum_ph = source.psum_ph;
250psum_tech = source.psum_tech;
251grooming = source.grooming;
252vw = source.vw;
253dw = source.dw;
254rh = source.rh;
255ta = source.ta;
256solarElevation = source.solarElevation;
257output_grids = source.output_grids;
258workers = source.workers;
259worker_startx = source.worker_startx;
260worker_deltax = source.worker_deltax;
261timer = source.timer;
262nextStepTimestamp = source.nextStepTimestamp;
263timeStep = source.timeStep;
264
265drift = source.drift;
266eb = source.eb;
267da = source.da;
268runoff = source.runoff;
269dataMeteo2D = source.dataMeteo2D;
270dataDa = source.dataDa;
271dataSnowDrift = source.dataSnowDrift;
272dataRadiation = source.dataRadiation;
273
274//io = source.io;
275
276outpath = source.outpath;
277mask_glaciers = source.mask_glaciers;
278mask_dynamic = source.mask_dynamic;
279maskGlacier = source.maskGlacier;
280
281glacier_katabatic_flow = source.glacier_katabatic_flow;
282snow_production = source.snow_production;
283glaciers = source.glaciers;
284techSnow = source.techSnow;
285enable_lateral_flow = source.enable_lateral_flow;
286a3d_view = source.a3d_view;
287
288sn_cfg = source.sn_cfg;
289//dem = source.dem;
290is_restart = source.is_restart;
291useCanopy = source.useCanopy;
292do_io_locally = source.do_io_locally;
293station_name = source.station_name;
294
295Tsoil_idx = source.Tsoil_idx;
296grids_start = source.grids_start;
297grids_days_between = source.grids_days_between;
298ts_start = source.ts_start;
299ts_days_between = source.ts_days_between;
300prof_start = source.prof_start;
301prof_days_between = source.prof_days_between;
302grids_write = source.grids_write;
303ts_write = source.ts_write;
304prof_write = source.prof_write;
305snow_write = source.snow_write;
306snow_poi_written = source.snow_poi_written;
307meteo_outpath = source.meteo_outpath;
308tz_out = source.tz_out;
309pts = source.pts;
310}
311return *this;
312}
313
314std::string SnowpackInterface::getGridsRequirements() const
315{
316if (glacier_katabatic_flow) {
317return "GLACIER TSS HS";
318}
319return "";
320}
321
322mio::Config SnowpackInterface::readAndTweakConfig(const mio::Config& io_cfg, const bool have_pts)
323{
324SnowpackConfig tmp_cfg(io_cfg);
325//force some keys
326double calculation_step_length;
327tmp_cfg.getValue("CALCULATION_STEP_LENGTH", "Snowpack", calculation_step_length);
328std::stringstream ss;
329ss << calculation_step_length;
330tmp_cfg.addKey("METEO_STEP_LENGTH", "Snowpack", ss.str());
331tmp_cfg.addKey("ALPINE3D", "SnowpackAdvanced", "true");
332tmp_cfg.addKey("ALPINE3D_PTS", "SnowpackAdvanced",have_pts?"true":"false");
333tmp_cfg.addKey("PERP_TO_SLOPE", "SnowpackAdvanced", "true");
334
335tmp_cfg.getValue("LOCAL_IO", "General", do_io_locally, IOUtils::nothrow);
336tmp_cfg.getValue("GRID2DPATH", "Output", outpath);
337tmp_cfg.getValue("MASK_GLACIERS", "Output", mask_glaciers, IOUtils::nothrow);
338tmp_cfg.getValue("MASK_DYNAMIC", "Output", mask_dynamic, IOUtils::nothrow);
339tmp_cfg.getValue("GLACIER_KATABATIC_FLOW", "Alpine3D", glacier_katabatic_flow, IOUtils::nothrow);
340tmp_cfg.getValue("SNOW_PRODUCTION", "TechSnow", snow_production, IOUtils::nothrow);
341tmp_cfg.getValue("SNOW_GROOMING", "TechSnow", snow_grooming, IOUtils::nothrow);
342tmp_cfg.getValue("GRIDS_WRITE", "Output", grids_write);
343tmp_cfg.getValue("GRIDS_START", "Output", grids_start);
344tmp_cfg.getValue("GRIDS_DAYS_BETWEEN", "Output", grids_days_between);
345tmp_cfg.getValue("TS_WRITE", "Output", ts_write);
346tmp_cfg.getValue("TS_START", "Output", ts_start);
347tmp_cfg.getValue("TS_DAYS_BETWEEN", "Output", ts_days_between);
348tmp_cfg.getValue("PROF_WRITE", "Output", prof_write);
349tmp_cfg.getValue("PROF_START", "Output", prof_start);
350tmp_cfg.getValue("PROF_DAYS_BETWEEN", "Output", prof_days_between);
351
352tmp_cfg.getValue("METEOPATH", "Output", meteo_outpath);
353tmp_cfg.getValue("TIME_ZONE", "Output", tz_out, IOUtils::nothrow);
354tmp_cfg.getValue("EXPERIMENT", "Output", station_name, IOUtils::dothrow);
355
356tmp_cfg.getValue("SNOW_WRITE", "Output", snow_write);
357tmp_cfg.getValue("CANOPY", "Snowpack", useCanopy);
358
359return tmp_cfg;
360}
361
362/**
363 * @brief Destructor of SnowpackInterface Master. Handels special cases with POP-C++ and
364 * also free correctly runoff and workers.
365 */
366SnowpackInterface::~SnowpackInterface()
367{
368if (glacier_katabatic_flow) delete glaciers;
369//if (runoff) delete runoff;
370while (!workers.empty()) delete workers.back(), workers.pop_back();
371}
372
373
374/**
375 * @brief get time who was used to exchange Data with Workers and run on each Pixel
376 * the Snowpack Model throught workers.
377 */
378double SnowpackInterface::getTiming() const
379{
380return timer.getElapsed();
381}
382
383
384/**
385 * @brief Internal in Snowpack Interface Master used method to write standard
386 * results in output files.
387 * Attantion: to have old format files output, set in ini file following key:
388 * **A3D_VIEW= true**
389 *
390 * @param date is the date for which the output is done
391 */
392void SnowpackInterface::writeOutput(const mio::Date& date)
393{
394MPIControl& mpicontrol = MPIControl::instance();
395const bool isMaster = mpicontrol.master();
396
397if (do_grid_output(date)) {
398//no OpenMP pragma here, otherwise multiple threads might call an MPI allreduce_sum()
399for (size_t ii=0; ii<output_grids.size(); ii++) {
400const size_t SnGrids_idx = SnGrids::getParameterIndex( output_grids[ii] );
401mio::Grid2DObject grid( getGrid( static_cast<SnGrids::Parameters>(SnGrids_idx)) );
402
403if (isMaster) {
404if (mask_glaciers) grid *= maskGlacier;
405const size_t meteoGrids_idx = MeteoGrids::getParameterIndex( output_grids[ii] );
406if (meteoGrids_idx!=IOUtils::npos) { //for this, the grid plugins must be thread-safe!
407io.write2DGrid(grid, static_cast<MeteoGrids::Parameters>(meteoGrids_idx), date);
408} else {
409std::string grid_type;
410sn_cfg.getValue("GRID2D", "output",grid_type);
411if (grid_type == "ARC") {
412std::string file_name;
413if (a3d_view) {
414std::string dateStr( date.toString(Date::NUM) );
415dateStr.erase( dateStr.size()-2, string::npos); //remove the seconds
416file_name = dateStr + "_" + output_grids[ii] + ".asc" ;
417} else {
418std::string date_str(date.toString(mio::Date::ISO));
419std::replace( date_str.begin(), date_str.end(), ':', '.');
420file_name = date_str + "_" + output_grids[ii] + ".asc" ;
421}
422io.write2DGrid(grid, file_name);
423} else if (grid_type=="NETCDF") {
424std::string file;
425sn_cfg.getValue("GRID2DFILE", "output", file);
426io.write2DGrid(grid, output_grids[ii]+"@"+date.toString(mio::Date::ISO));
427} else {
428throw InvalidFormatException("[E] Only ARC and NetCDF allow writing out non-standard grids such as "+output_grids[ii], AT);
429}
430}
431}
432}
433// Output Runoff
434if (runoff) runoff->output(date, psum, ta, io);
435}
436
437
438}
439
440/**
441 * @brief Method tells if on given date, gridded output should be done (read this out of snowpack ini)
442 * @param date is the date object which is controlled, if output needs to be done
443 */
444bool SnowpackInterface::do_grid_output(const mio::Date &date) const
445{
446return (grids_write && booleanTime(date.getJulian(), grids_days_between, grids_start, dt_main/60.));
447}
448
449/**
450 * @brief commands worker to write .sno files. Is triggered by Alpine Control
451 *
452 * Hack --> Find better software architecture to do this then SnowpackInterface also
453 * does output for other modules of A3D here..
454 * @param date is the date witch the output is done
455 */
456void SnowpackInterface::writeOutputSNO(const mio::Date& date)
457{
458MPIControl& mpicontrol = MPIControl::instance();
459
460vector<SnowStation*> snow_station;
461
462for (size_t ii=0; ii<workers.size(); ii++)
463workers[ii]->getOutputSNO(snow_station);
464
465if (mpicontrol.master()) {
466std::cout << "[i] Writing SNO output for process " << mpicontrol.master_rank() << "\n";
467writeSnowCover(date, snow_station); //local data
468
469//Now gather all elements on the master node
470for (size_t ii=0; ii<mpicontrol.size(); ii++) {
471if (ii == mpicontrol.master_rank() || do_io_locally) continue;
472std::cout << "[i] Writing SNO output for process " << ii << "\n";
473vector<SnowStation*> snow_station_tmp;
474
475mpicontrol.receive(snow_station_tmp, ii);
476writeSnowCover(date, snow_station_tmp);
477for (size_t jj=0; jj<snow_station_tmp.size(); jj++) delete snow_station_tmp[jj];
478}
479} else {
480if (do_io_locally) {
481std::cout << "[i] Writing SNO output for process " << mpicontrol.rank() << "\n";
482writeSnowCover(date, snow_station); //local data
483} else {
484mpicontrol.send(snow_station, mpicontrol.master_rank());
485}
486}
487}
488
489void SnowpackInterface::writeSnowCover(const mio::Date& date, const std::vector<SnowStation*>& snow_station)
490{
491for (size_t jj=0; jj<snow_station.size(); jj++)
492snowpackIO.writeSnowCover(date, *(snow_station[jj]), ZwischenData());
493}
494
495/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496 Methods to set references to other methodes
497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
498/**
499 * @brief Set reference to SnowDrift module, to comunicate with it.
500 * @param mydrift is the reference to the SnowDrift module
501 */
502void SnowpackInterface::setSnowDrift(SnowDriftA3D& mydrift)
503{
504drift = &mydrift;
505
506if (drift) {
507for (size_t i = 0; i < workers.size(); i++) workers[i]->setUseDrift(true);
508
509// Provide initial snow parameters to SnowDrift
510const Grid2DObject cH( getGrid(SnGrids::HS) );
511const Grid2DObject sp( getGrid(SnGrids::SP) );
512const Grid2DObject rg( getGrid(SnGrids::RG) );
513const Grid2DObject N3( getGrid(SnGrids::N3) );
514const Grid2DObject rb( getGrid(SnGrids::RB) );
515drift->setSnowSurfaceData(cH, sp, rg, N3, rb);
516}
517}
518
519/**
520 * @brief Set reference to EnergyBalance module, to comunicate with it.
521 * @param myeb is the reference to the EnergyBalance module
522 */
523void SnowpackInterface::setEnergyBalance(EnergyBalance& myeb)
524{
525
526eb = &myeb;
527if (eb) {
528for (size_t i = 0; i < workers.size(); i++){
529workers[i]->setUseEBalance(true);
530}
531// Provide initial albedo to EnergyBalance
532const Grid2DObject alb( getGrid(SnGrids::TOP_ALB) );
533eb->setAlbedo(alb);
534}
535}
536
537/**
538 * @brief Set reference to DataAssimilation module, to comunicate with it.
539 * @param init_da is the reference to the DataAssimilation module
540 */
541void SnowpackInterface::setDataAssimilation(DataAssimilation& init_da)
542{
543da = &init_da;
544}
545
546void SnowpackInterface::setRunoff(Runoff& init_runoff)
547{
548runoff = &init_runoff;
549}
550
551/**
552 * @brief Interface that DataAssimilation can push the data to the SnowpackInterface
553 * This is currently never used...
554 * @param daData are the data from the DataAssimilation module
555 * @param timestamp for controlle if data from DataAssimilation are form the correct simulation step
556 */
557void SnowpackInterface::assimilate(const Grid2DObject& /*daData*/, const mio::Date& timestamp)
558{
559
560if (nextStepTimestamp != timestamp) {
561throw InvalidArgumentException("Assimilation and snowpack time steps don't match", AT);
562}
563
564cout << "Updating state variables...\n";
565/*for (size_t iy = 0; iy < dimy; iy++) {
566for (size_t ix = 0; ix < dimx; ix++) {
567const size_t i = dimy*iy + ix;
568if ( daData.grid2D(ix,iy) == 1.0 ) {
569//if DA-data DOES NOT have snow
570if (sn_Xdata[i].cH-sn_Xdata[i].Ground > 0.) {
571//and snowpack DOES have snow
572//clear the pixel
573store(ix,iy) = 0.;
574sn_Xdata[i].cH = sn_Xdata[i].Ground;
575sn_Xdata[i].mH = sn_Xdata[i].Ground;
576sn_Xdata[i].nElems = sn_Xdata[i].SoilNode;
577sn_Xdata[i].nNodes = sn_Xdata[i].nElems + 1;
578}
579} else {
580if ( daData.grid2D(ix,iy) == 4 ) {
581//if DA-data DOES have snow
582if (sn_Xdata[i].cH-sn_Xdata[i].Ground < 1e-15) {
583//if snowpack DOES NOT have snow -> add some snow
584store(ix,iy) += 40*M_TO_H(calculation_step_length);
585}
586}
587}
588}
589}*/
590
591dataDa = true;
592calcNextStep();
593}
594
595/**
596 * @brief Interface that SnowDrift can push the data to the SnowpackInterface
597 * @param new_mns are the data about the new Snow masses from the DataAssimilation module
598 * @param timestamp for controlle if data from DataAssimilation are form the correct simulation step
599 */
600void SnowpackInterface::setSnowMassChange(const mio::Grid2DObject& new_mns, const mio::Date& timestamp)
601{
602if (nextStepTimestamp != timestamp) {
603if (MPIControl::instance().master()) {
604std::cerr << "Providing drift snow mass at " << timestamp.toString(Date::ISO);
605std::cerr << " for Snowpack timestamp " << nextStepTimestamp.toString(Date::ISO) << "\n";
606}
607throw InvalidArgumentException("Snowdrift and snowpack steps don't match", AT);
608}
609
610if (!new_mns.isSameGeolocalization(dem)) {
611std::ostringstream ss;
612ss << "Trying to set snow mass changes from a (" << new_mns.getNx() << "," << new_mns.getNy() << ") grid ";
613ss << "when the dem is (" << dem.getNx() << "," << dem.getNy() << ")";
614throw IndexOutOfBoundsException(ss.str(), AT);
615}
616
617mns = new_mns;
618dataSnowDrift = true;
619calcNextStep();
620}
621
622/**
623 * @brief get Meteo changes from AlpineControl or SnowDrift module
624 * @param new_psum gives the new values for new Water High
625 * @param new_psum_ph gives the new values for the precipitation phase
626 * @param new_vw gives the new values for the wind speed
627 * @param new_dw gives the new values for the wind direction
628 * @param new_rh gives the new values for the realtiv Humidity
629 * @param new_ta gives the new values for the Aire temperature
630 * @param timestamp is the time of the calculation step from which this new values are comming
631 */
632void SnowpackInterface::setMeteo(const Grid2DObject& new_psum, const Grid2DObject& new_psum_ph, const Grid2DObject& new_vw, const Grid2DObject& new_dw, const Grid2DObject& new_rh, const Grid2DObject& new_ta, const mio::Date& timestamp)
633{
634if (nextStepTimestamp != timestamp) {
635if (MPIControl::instance().master()) {
636std::cerr << "Providing meteo fields at " << timestamp.toString(Date::ISO);
637std::cerr << " for Snowpack timestamp " << nextStepTimestamp.toString(Date::ISO) << "\n";
638}
639throw InvalidArgumentException("Meteo and snowpack time steps don't match", AT);
640}
641
642psum = new_psum;
643psum_ph = new_psum_ph;
644vw = new_vw;
645dw = new_dw;
646rh = new_rh;
647if (mask_dynamic) maskGlacier = getGrid(SnGrids::GLACIER); //so the updated glacier map is available for all
648
649if (!glacier_katabatic_flow) {
650ta = new_ta;
651} else {
652if (mask_dynamic) glaciers->setGlacierMap(maskGlacier);
653const Grid2DObject TSS( getGrid(SnGrids::TSS) );
654const Grid2DObject cH( getGrid(SnGrids::HS) );
655ta = glaciers->correctTemperatures(cH, TSS, new_ta);
656}
657
658if (snow_production || snow_grooming) {
659const Grid2DObject cH( getGrid(SnGrids::HS) );
660techSnow->setMeteo(new_ta, new_rh, cH, timestamp);
661psum_tech = techSnow->getGrid(SnGrids::PSUM_TECH);
662grooming = techSnow->getGrid(SnGrids::GROOMING);
663}
664
665dataMeteo2D = true;
666calcNextStep();
667}
668
669/**
670 * @brief get values from Energy Balance
671 * @param shortwave_in is the 2D double Array map of ISWR [W m-2]
672 * @param longwave_in is the 2D double Array map of ILWR [W m-2]
673 * @param diff_in is the 2D double Array map of Diffuse radiation from the sky [W m-2]
674 * @param solarElevation_in double of Solar elevation to be used for Canopy (in dec)
675 * @param timestamp is the time of the calculation step from which this new values are comming
676 */
677void SnowpackInterface::setRadiationComponents(const mio::Array2D<double>& shortwave_in, const mio::Array2D<double>& longwave_in, const mio::Array2D<double>& diff_in, const double& solarElevation_in, const mio::Date& timestamp)
678{
679if (nextStepTimestamp != timestamp) {
680if (MPIControl::instance().master()) {
681std::cerr << "Providing radiation fields at " << timestamp.toString(Date::ISO);
682std::cerr << " for Snowpack timestamp " << nextStepTimestamp.toString(Date::ISO) << "\n";
683}
684throw InvalidArgumentException("Radiation and snowpack time steps don't match", AT);
685}
686
687shortwave.grid2D = shortwave_in;
688longwave.grid2D = longwave_in;
689diffuse.grid2D = diff_in;
690solarElevation = solarElevation_in;
691
692dataRadiation = true;
693calcNextStep();
694}
695
696/**
697 * @brief Request specific grid by parameter type
698 * @param param parameter
699 * @return 2D output grid (empty if the requested parameter was not available)
700 */
701mio::Grid2DObject SnowpackInterface::getGrid(const SnGrids::Parameters& param) const
702{
703//special case for the meteo forcing grids
704switch (param) {
705case SnGrids::TA:
706return ta;
707case SnGrids::RH:
708return rh;
709case SnGrids::VW:
710return vw;
711case SnGrids::DW:
712return dw;
713case SnGrids::PSUM:
714return psum;
715case SnGrids::PSUM_PH:
716return psum_ph;
717case SnGrids::PSUM_TECH:
718return psum_tech;
719case SnGrids::MNS:
720return mns;
721case SnGrids::ISWR:
722return shortwave;
723case SnGrids::ILWR:
724return longwave;
725default: ; //so compilers do not complain about missing conditions
726}
727
728mio::Grid2DObject o_grid2D(dem, 0.); //so the allreduce_sum works
729size_t errCount = 0;
730mio::Grid2DObject tmp_grid2D_(dem,mpi_offset, 0, mpi_nx, dimy);
731mio::Grid2DObject tmp_grid2D(tmp_grid2D_,mio::IOUtils::nodata);
732#pragma omp parallel for schedule(dynamic) reduction(+: errCount)
733for (size_t ii = 0; ii < workers.size(); ii++) {
734const mio::Grid2DObject tmp( workers[ii]->getGrid(param) );
735if (!tmp.empty()) {
736for (size_t i=0; i<tmp_grid2D.getNx(); ++i){
737for (size_t j=0; j<tmp_grid2D.getNy(); ++j){
738if (tmp(i,j)!=mio::IOUtils::nodata)
739{
740tmp_grid2D(i,j)=tmp(i,j);
741}
742}
743}
744} else {
745errCount++;
746}
747}
748o_grid2D.grid2D.fill(tmp_grid2D.grid2D, mpi_offset, 0, mpi_nx, dimy);
749
750MPIControl::instance().allreduce_sum(o_grid2D);
751//with some MPI implementations, when transfering large amounts of data, the buffers might get full and lead to a crash
752
753if (errCount>0) {
754std::cerr << "[W] Requested " << SnGrids::getParameterName( param ) << " but this was not available in the workers\n";
755o_grid2D.clear(); //the requested parameter was not available
756}
757return o_grid2D;
758}
759
760/**
761 * @brief Get data from other modules and run one simulation step.
762 * Once the simulation step has been performed, the data are pushed to ther other modules.
763 */
764void SnowpackInterface::calcNextStep()
765{
766//Control if all data are present
767if (!dataMeteo2D) {
768return;
769} else {
770if (drift != NULL && !dataSnowDrift) return;
771if (da != NULL && !dataDa) return;
772if (eb != NULL && !dataRadiation) return;
773}
774// control if the necessary data are available
775if (!dataRadiation) {
776throw NoDataException("Radiation data not available", AT);
777}
778
779dataDa = dataMeteo2D = dataSnowDrift = dataRadiation = false; //the external modules will turn them back to true when pushing their data
780
781// timing
782timer.restart();
783
784size_t errCount = 0;
785const mio::Grid2DObject tmp_psum(psum, mpi_offset, 0, mpi_nx, dimy);
786const mio::Grid2DObject tmp_psum_ph(psum_ph, mpi_offset, 0, mpi_nx, dimy);
787const mio::Grid2DObject tmp_psum_tech(psum_tech, mpi_offset, 0, mpi_nx, dimy);
788const mio::Grid2DObject tmp_rh(rh, mpi_offset, 0, mpi_nx, dimy);
789const mio::Grid2DObject tmp_ta(ta, mpi_offset, 0, mpi_nx, dimy);
790const mio::Grid2DObject tmp_vw(vw, mpi_offset, 0, mpi_nx, dimy);
791const mio::Grid2DObject tmp_dw(dw, mpi_offset, 0, mpi_nx, dimy);
792const mio::Grid2DObject tmp_mns(mns, mpi_offset, 0, mpi_nx, dimy);
793const mio::Grid2DObject tmp_shortwave(shortwave, mpi_offset, 0, mpi_nx, dimy);
794const mio::Grid2DObject tmp_diffuse(diffuse, mpi_offset, 0, mpi_nx, dimy);
795const mio::Grid2DObject tmp_longwave(longwave, mpi_offset, 0, mpi_nx, dimy);
796#pragma omp parallel for schedule(dynamic, 1) reduction(+: errCount)
797for (size_t ii = 0; ii < workers.size(); ii++) { // make slices
798// run model, process exceptions in a way that is compatible with openmp
799try {
800workers[ii]->runModel(nextStepTimestamp, tmp_psum, tmp_psum_ph, tmp_psum_tech, tmp_rh, tmp_ta, tmp_vw, tmp_dw, tmp_mns, tmp_shortwave, tmp_diffuse, tmp_longwave, solarElevation);
801if (snow_grooming) {
802const mio::Grid2DObject tmp_grooming(grooming, mpi_offset, 0, mpi_nx, dimy);
803workers[ii]->grooming( nextStepTimestamp, tmp_grooming );
804}
805} catch(const std::exception& e) {
806++errCount;
807cout << e.what() << std::endl;
808}
809}
810
811//Lateral flow
812if (enable_lateral_flow) {
813calcLateralFlow();
814}
815
816//Retrieve special points data and write files
817if (!pts.empty()) write_special_points();
818
819if (errCount>0) {
820//something wrong took place, quitting. At least we tried writing the special points out
821std::abort(); //force core dump
822}
823
824// Gather data if needed and make exchange for SnowDrift
825if (drift) {
826const Grid2DObject cH( getGrid(SnGrids::HS) );
827const Grid2DObject sp( getGrid(SnGrids::SP) );
828const Grid2DObject rg( getGrid(SnGrids::RG) );
829const Grid2DObject N3( getGrid(SnGrids::N3) );
830const Grid2DObject rb( getGrid(SnGrids::RB) );
831drift->setSnowSurfaceData(cH, sp, rg, N3, rb);
832}
833
834// Gather data if needed and make exchange for EnergyBalance
835if (eb) {
836const Grid2DObject alb( getGrid(SnGrids::TOP_ALB) );
837eb->setAlbedo(alb);
838}
839
840//make output
841writeOutput(nextStepTimestamp);
842
843timer.stop();
844if (MPIControl::instance().master())
845cout << "[i] Snowpack simulations done for " << nextStepTimestamp.toString(Date::ISO) << "\n";
846nextStepTimestamp = nextStepTimestamp + timeStep;
847MPIControl::instance().barrier();
848
849}
850
851void SnowpackInterface::write_special_points()
852{
853MPIControl& mpicontrol = MPIControl::instance();
854
855std::vector<SnowStation*> snow_pixel;
856std::vector<CurrentMeteo*> meteo_pixel;
857std::vector<SurfaceFluxes*> surface_flux;
858
859// note: do not parallelize this with OpenMP
860for (size_t ii=0; ii<workers.size(); ii++)
861workers[ii]->getOutputSpecialPoints(snow_pixel, meteo_pixel, surface_flux);
862
863if (do_io_locally) {
864writeOutputSpecialPoints(nextStepTimestamp, snow_pixel, meteo_pixel, surface_flux);
865if (!snow_write && !snow_poi_written) {
866writeSnowCover(nextStepTimestamp, snow_pixel); //also write the .sno of the special points
867snow_poi_written = true;
868}
869} else { // data has to be sent to the master process
870if (mpicontrol.master()) {
871// Write out local data first and then gather data from all processes
872writeOutputSpecialPoints(nextStepTimestamp, snow_pixel, meteo_pixel, surface_flux);
873if (!snow_write && !snow_poi_written) {
874writeSnowCover(nextStepTimestamp, snow_pixel); //also write the .sno of the special points
875}
876
877for (size_t ii=0; ii<mpicontrol.size(); ii++) {
878if (ii == mpicontrol.master_rank()) continue;
879snow_pixel.clear(); meteo_pixel.clear(); surface_flux.clear();
880
881mpicontrol.receive(snow_pixel, ii);
882mpicontrol.receive(meteo_pixel, ii);
883mpicontrol.receive(surface_flux, ii);
884
885writeOutputSpecialPoints(nextStepTimestamp, snow_pixel, meteo_pixel, surface_flux);
886if (!snow_write && !snow_poi_written) {
887writeSnowCover(nextStepTimestamp, snow_pixel); //also write the .sno of the special points
888}
889}
890snow_poi_written = true;
891} else {
892mpicontrol.send(snow_pixel, mpicontrol.master_rank());
893mpicontrol.send(meteo_pixel, mpicontrol.master_rank());
894mpicontrol.send(surface_flux, mpicontrol.master_rank());
895}
896}
897
898#pragma omp parallel for schedule(static)
899for (size_t ii=0; ii<workers.size(); ii++) workers[ii]->clearSpecialPointsData();
900}
901
902/**
903 * @brief Write the output which is asked to have more for the special points
904 * @param date Output date
905 * @param snow_pixel The SnowStation data for all the special points
906 * @param meteoPixel The CurrentMeteo data for all the special points
907 * @param surface_flux The SurfaceFlux data for all the special points
908 */
909void SnowpackInterface::writeOutputSpecialPoints(const mio::Date& date, const std::vector<SnowStation*>& snow_pixel, const std::vector<CurrentMeteo*>& meteo_pixel,
910 const std::vector<SurfaceFluxes*>& surface_flux)
911{
912const bool TS = (ts_write && booleanTime(date.getJulian(), ts_days_between, ts_start, dt_main/60.));
913const bool PR = (prof_write && booleanTime(date.getJulian(), prof_days_between, prof_days_between, dt_main/60));
914
915const ProcessDat Hdata; // empty ProcessDat, get it from where ??
916for (size_t ii=0; ii<snow_pixel.size(); ii++) {
917write_SMET(*meteo_pixel[ii], snow_pixel[ii]->meta, *surface_flux[ii]);
918if (TS) snowpackIO.writeTimeSeries(*snow_pixel[ii], *surface_flux[ii], *meteo_pixel[ii], Hdata, 0.);
919if (PR) snowpackIO.writeProfile(date, *snow_pixel[ii]);
920}
921}
922
923/**
924 * @brief Write header of SMET file for specific point
925 * @param meta StationData for the SMET header initialization
926 */
927void SnowpackInterface::write_SMET_header(const mio::StationData& meta, const double& landuse_code) const
928{
929const std::string filename( meteo_outpath + "/" + meta.stationName + "_meteo.smet" );
930if (!FileUtils::validFileAndPath(filename)) throw InvalidNameException(filename,AT);
931errno = 0;
932
933std::ofstream smet_out; //Output file streams
934smet_out.open(filename.c_str());
935if (smet_out.fail()) throw AccessException(filename.c_str(), AT);
936
937smet_out << "SMET 1.1 ASCII\n";
938smet_out << "[HEADER]\n";
939smet_out << "station_name = " << meta.stationName << "\n";
940smet_out << "station_id = " << meta.stationID << "\n";
941smet_out << std::right;
942smet_out << std::fixed;
943
944smet_out << "altitude = " << std::setw(11) << std::setprecision(1) << meta.position.getAltitude() << "\n";
945smet_out << "latitude = " << std::setw(11) << std::setprecision(8) << meta.position.getLat() << "\n";
946smet_out << "longitude = " << std::setw(11) << std::setprecision(8) << meta.position.getLon() << "\n";
947smet_out << "easting = " << std::setw(11) << std::setprecision(1) << meta.position.getEasting() << "\n";
948smet_out << "northing = " << std::setw(11) << std::setprecision(1) << meta.position.getNorthing() << "\n";
949smet_out << "epsg = " << std::setw(11) << std::setprecision(0) << meta.position.getEPSG() << "\n";
950smet_out << "slope = " << std::setw(11) << std::setprecision(1) << meta.getSlopeAngle() << "\n";
951smet_out << "azimuth = " << std::setw(11) << std::setprecision(1) << meta.getAzimuth() << "\n";
952smet_out << "landuse = " << std::setw(11) << std::setprecision(0) << SnowpackInterfaceWorker::round_landuse( landuse_code ) << "\n";
953smet_out << "nodata = " << std::setw(11) << std::setprecision(0) << mio::IOUtils::nodata << "\n";
954smet_out << "tz = " << std::setw(11) << std::setprecision(0) << tz_out << "\n";
955smet_out << "source = " << "Alpine3D version " << A3D_VERSION << " run by " << run_info.user << "\n";
956smet_out << "creation = " << run_info.computation_date.toString(Date::ISO) << "\n";
957if (useCanopy) smet_out << "comment = " << "ISWR/RSWR are above the canopy, ISWR_can/RSWR_can and PSUM/PSUM_PH are below the canopy\n";
958
959smet_out << "fields = timestamp TA TSS TSG VW DW VW_MAX ISWR OSWR ILWR PSUM PSUM_PH HS RH";
960for (size_t ii=0; ii<Tsoil_idx.size(); ii++)
961smet_out << " TSOIL" << Tsoil_idx[ii];
962
963if (useCanopy) smet_out << " ISWR_can RSWR_can";
964if (snow_production) smet_out << " PSUM_TECH";
965smet_out << "\n[DATA]\n";
966
967smet_out.close();
968}
969
970/**
971 * @brief Write the meteorogical data for the current step into the SMET file for the respective point
972 * @param met The CurrentMeteo data for one special point
973 * @param ix is the x-coordiante of the special point
974 * @param iy is the y-coordiante of the special point
975 */
976void SnowpackInterface::write_SMET(const CurrentMeteo& met, const mio::StationData& meta, const SurfaceFluxes& surf) const
977{
978const std::string filename( meteo_outpath + "/" + meta.stationName + "_meteo.smet" );
979if (!FileUtils::validFileAndPath(filename)) throw InvalidNameException(filename,AT);
980
981std::ofstream smet_out; //Output file streams
982smet_out.open(filename.c_str(), std::ios::out | std::ios::app );
983if (smet_out.fail()) throw AccessException(filename.c_str(), AT);
984
985// write line
986smet_out.fill(' ');
987smet_out << std::right;
988smet_out << std::fixed;
989smet_out << met.date.toString(mio::Date::ISO) << " ";
990smet_out << std::setw(8) << std::setprecision(2) << met.ta << " ";
991smet_out << std::setw(8) << std::setprecision(2) << met.tss << " ";
992smet_out << std::setw(8) << std::setprecision(2) << met.ts0 << " ";
993smet_out << std::setw(6) << std::setprecision(1) << met.vw << " ";
994smet_out << std::setw(5) << std::setprecision(0) << met.dw << " ";
995smet_out << std::setw(6) << std::setprecision(1) << met.vw_max << " ";
996smet_out << std::setw(6) << std::setprecision(0) << met.iswr << " ";
997smet_out << std::setw(6) << std::setprecision(0) << met.rswr << " ";
998smet_out << std::setw(6) << std::setprecision(3) << mio::Atmosphere::blkBody_Radiation(met.ea, met.ta) << " ";
999smet_out << std::setw(6) << std::setprecision(3) << met.psum << " ";
1000smet_out << std::setw(6) << std::setprecision(3) << met.psum_ph << " ";
1001smet_out << std::setw(8) << std::setprecision(3) << met.hs / cos(meta.getSlopeAngle()*Cst::to_rad) << " ";
1002smet_out << std::setw(7) << std::setprecision(3) << met.rh << " ";
1003if (!met.ts.empty())
1004smet_out << std::setw(8) << std::setprecision(2) << met.ts[0] << " ";
1005if (useCanopy) {
1006smet_out << std::setw(6) << std::setprecision(0) << surf.sw_in << " ";
1007smet_out << std::setw(6) << std::setprecision(0) << surf.sw_out << " ";
1008}
1009if (snow_production) {
1010smet_out << std::setw(6) << std::setprecision(3) << met.psum_tech << " ";
1011}
1012smet_out << "\n";
1013
1014smet_out.close();
1015}
1016
1017/**
1018 * @brief Read glacier height map and store values in init_glaciers_height
1019 * @author Adrien Michel
1020 */
1021void SnowpackInterface::setInitGlacierHeight()
1022{
1023io.readGlacier(init_glaciers_height);
1024if (!init_glaciers_height.isSameGeolocalization(dem))
1025throw IOException("The glacier initial height map and the provided DEM don't have the same geolocalization!", AT);
1026}
1027
1028
1029
1030/**
1031 * @brief Return a SN_SNOWSOIL_DATA pixel initialized witht the provided glacier height
1032 * @param glacier_height The hight of ice to initialze the glacier with
1033 * @param glacier_height GRID_sno of the corresponding pixel
1034 * @param seaIce Is this pixel sea ice
1035 * @author Adrien Michel
1036 */
1037SN_SNOWSOIL_DATA SnowpackInterface::getIcePixel(const double glacier_height, const std::stringstream& GRID_sno, const bool seaIce)
1038{
1039
1040SN_SNOWSOIL_DATA snow_soil_tmp;
1041std::stringstream LUS_sno_tmp;
1042LUS_sno_tmp << station_name << "_" << "11400";
1043ZwischenData zwischenData_tmp; //not used by Alpine3D but necessary for Snowpack
1044// Load glacier sno file and extract one ice layer
1045readSnowCover(GRID_sno.str(), LUS_sno_tmp.str(), false, snow_soil_tmp, zwischenData_tmp, seaIce);
1046LayerData ldata = LayerData(snow_soil_tmp.Ldata.back()); //MUST be an ice layer
1047
1048// Count soil layers
1049size_t i=0;
1050for (;i<snow_soil_tmp.Ldata.size();++i){
1051if(snow_soil_tmp.Ldata[i].phiSoil==0){break;}
1052}
1053// Remove layers above soil
1054snow_soil_tmp.Ldata.erase(snow_soil_tmp.Ldata.begin()+i,snow_soil_tmp.Ldata.end());
1055
1056// Create ice layers
1057double layer_height=2;
1058double total_height=0;
1059while (total_height < glacier_height-0.1){
1060ldata.hl=layer_height;
1061snow_soil_tmp.Ldata.push_back(ldata);
1062total_height += layer_height;
1063if(glacier_height-total_height<0.5){layer_height=0.1;}
1064else if(glacier_height-total_height<2){layer_height=0.2;}
1065else if(glacier_height-total_height<4){layer_height=1.;}
1066}
1067
1068// Add last layer
1069const double remaining = glacier_height-total_height;
1070if(remaining>0.02){
1071ldata.hl=remaining;
1072snow_soil_tmp.Ldata.push_back(ldata);
1073total_height += remaining;
1074}
1075
1076// Set remaining parameters of SN_SNOWSOIL_DATA which need to be modified
1077snow_soil_tmp.nLayers=snow_soil_tmp.Ldata.size();
1078snow_soil_tmp.nN = 1;
1079snow_soil_tmp.Height = 0.;
1080for (size_t ll = 0; ll < snow_soil_tmp.nLayers; ll++) {
1081snow_soil_tmp.nN += snow_soil_tmp.Ldata[ll].ne;
1082snow_soil_tmp.Height += snow_soil_tmp.Ldata[ll].hl;
1083}
1084return snow_soil_tmp;
1085}
1086
1087/**
1088 * @page reading_snow_files Reading initial snow cover
1089 * The initial snow cover consist of an instantaneous snow/soil profile from which the time evolution will be computed.
1090 * When this is for a normal "cold" start, the file names are built based on the landuse code. For restarts, the
1091 * file names are built based on the cell (ii,jj) indices, for example:
1092 * + {station_name}_{landuse_code}.{ext} for a "cold" start;
1093 * + {ii}_{jj}_{station_name}.{ext} for a restart;
1094 *
1095 * The station name is given in the [Output] section as "EXPERIMENT" key. The other keys controlling the process (including
1096 * the file extension) are:
1097 * + in the [Snowpack] section:
1098 * + CANOPY: should the pixels enable the canopy module?
1099 * + SNP_SOIL: should the pixels use soil layers?
1100 * + in the [Input] section:
1101 * + SNOW: file format of the "sno" files, either SMET or SNOOLD (default: SMET);
1102 * + COORDSYS, COORDPARAM: in order to convert (ii,jj) coordinates to geographic coordinates so each pixel's metadata
1103 * can be reused (for example in order to rerun a \ref poi_outputs "Point Of Interest" offline in the SNOWPACK standalone model).
1104 */
1105 void SnowpackInterface::readInitalSnowCover(std::vector<SnowStation*>& snow_stations,
1106 std::vector<std::pair<size_t,size_t> >& snow_stations_coord){
1107 //HACK: with nextStepTimestamp, check that the snow cover is older than the start timestep!
1108
1109if (MPIControl::instance().master() || do_io_locally) {
1110const bool useSoil = sn_cfg.get("SNP_SOIL", "Snowpack");
1111const std::string coordsys = sn_cfg.get("COORDSYS", "Input");
1112const std::string coordparam = sn_cfg.get("COORDPARAM", "Input", "");
1113Coords llcorner_out( dem.llcorner );
1114llcorner_out.setProj(coordsys, coordparam);
1115const double refX = llcorner_out.getEasting();
1116const double refY = llcorner_out.getNorthing();
1117const double cellsize = dem.cellsize;
1118
1119const size_t nrWorkers = MPIControl::instance().size();
1120for (size_t ii=0; ii<nrWorkers; ii++) {
1121if (do_io_locally && (ii != MPIControl::instance().rank())) continue; // only read/write points managed by this process
1122
1123SN_SNOWSOIL_DATA snow_soil;
1124size_t startx, deltax;
1125MPIControl::instance().getArraySliceParamsOptim(dimx, ii, startx, deltax,dem,landuse);
1126vector<SnowStation*> snow_stations_tmp;
1127vector<pair<size_t,size_t> > snow_stations_coord_tmp;
1128snow_stations_tmp.reserve( dimy*deltax );
1129
1130// read snow cover for all points that are dealt with on this process
1131for (size_t iy = 0; iy < dimy; iy++) {
1132for (size_t ix = startx; ix < (startx+deltax); ix++) {
1133snow_stations_coord_tmp.push_back(std::pair<size_t,size_t>(ix - startx,iy));
1134if (SnowpackInterfaceWorker::skipThisCell(landuse(ix,iy), dem(ix,iy))) { //skip nodata cells as well as water bodies, etc
1135snow_stations_tmp.push_back( NULL );
1136continue;
1137}
1138snow_stations_tmp.push_back( new SnowStation(useCanopy, useSoil) );
1139
1140SnowStation& snowPixel = *(snow_stations_tmp.back());
1141const bool is_special_point = SnowpackInterfaceWorker::is_special(pts, ix, iy);
1142
1143// get potential filenames for initial snow pixel values
1144std::stringstream LUS_sno, GRID_sno;
1145LUS_sno << station_name << "_" << SnowpackInterfaceWorker::round_landuse(landuse.grid2D(ix,iy));
1146GRID_sno << ix << "_" << iy << "_" << station_name;
1147
1148// read standard values of pixel
1149try {
1150ZwischenData zwischenData; //not used by Alpine3D but necessary for Snowpack
1151readSnowCover(GRID_sno.str(), LUS_sno.str(), is_special_point, snow_soil, zwischenData, (snowPixel.Seaice!=NULL));
1152} catch (exception& e) {
1153cout << e.what()<<"\n";
1154throw IOException("Can not read snow files", AT);
1155}
1156
1157// Change pixel value if galciers are forced from grids
1158if(glacier_from_grid) {
1159if(init_glaciers_height(ix,iy)>0) {
1160snow_soil=getIcePixel(init_glaciers_height(ix,iy),GRID_sno,(snowPixel.Seaice!=NULL));
1161if(SnowpackInterfaceWorker::round_landuse(landuse.grid2D(ix,iy))!=11400)
1162{
1163std::cerr << "[W] Pixel [" << ix << "," << iy << "] was declared as glacier glacier height map but not in the landuse file. Pixel changed to glacier.\n";
1164landuse.grid2D(ix,iy)=11400;
1165}
1166}
1167// If was glaccier in LUS but not in glacier height --> set pixel to rock
1168else if(SnowpackInterfaceWorker::round_landuse(landuse.grid2D(ix,iy))==11400)
1169{
1170ZwischenData zwischenData; //not used by Alpine3D but necessary for Snowpack
1171std::stringstream LUS_sno_tmp;
1172LUS_sno_tmp << station_name << "_" << "11500";
1173readSnowCover(GRID_sno.str(), LUS_sno_tmp.str(), is_special_point, snow_soil, zwischenData, (snowPixel.Seaice!=NULL));
1174landuse.grid2D(ix,iy)=11500;
1175std::cerr << "[W] Pixel [" << ix << "," << iy << "] was declared as glacier in the landuse file but not in glacier height map. Pixel changed to rock.\n";
1176}
1177}
1178
1179// Copy standard values to specific pixel (station) data and init it
1180try {
1181snowPixel.initialize(snow_soil, 0); //force sector 0
1182snowPixel.mH = Constants::undefined;
1183} catch (exception&) {
1184cout << "[E] Could not intialize cell (" << ix << "," << iy << ")!\n";
1185throw IOException("Can not initialize snow pixel", AT);
1186}
1187
1188// Set proper pixel metadata
1189snowPixel.meta.position.setProj(coordsys, coordparam);
1190snowPixel.meta.position.setXY(refX+double(ix)*cellsize, refY+double(iy)*cellsize, dem.grid2D(ix,iy));
1191snowPixel.meta.position.setGridIndex((int)ix, (int)iy, 0, true);
1192snowPixel.meta.setSlope(dem.slope(ix,iy), dem.azi(ix,iy));
1193snowPixel.cos_sl = cos( snowPixel.meta.getSlopeAngle()*mio::Cst::to_rad );
1194
1195// Initialize the station name for the pixel
1196stringstream station_idx;
1197station_idx << ix << "_" << iy;
1198snowPixel.meta.stationName = station_idx.str() + "_" + station_name;
1199snowPixel.meta.stationID = station_idx.str();
1200if (is_special_point) { //create SMET files for special points
1201write_SMET_header(snowPixel.meta, landuse(ix, iy));
1202}
1203}
1204}
1205if (ii == MPIControl::instance().master_rank() || do_io_locally) {
1206snow_stations = snow_stations_tmp; //simply copy the pointers
1207snow_stations_coord = snow_stations_coord_tmp;
1208} else {
1209MPIControl::instance().send(snow_stations_tmp, ii);
1210MPIControl::instance().send(snow_stations_coord_tmp, ii);
1211while (!snow_stations_tmp.empty()) delete snow_stations_tmp.back(), snow_stations_tmp.pop_back();
1212}
1213}
1214std::cout << "[i] Read initial snow cover for process " << MPIControl::instance().rank() << "\n";
1215} else {
1216MPIControl::instance().receive(snow_stations, MPIControl::instance().master_rank());
1217MPIControl::instance().receive(snow_stations_coord, MPIControl::instance().master_rank());
1218}
1219}
1220
1221void SnowpackInterface::readSnowCover(const std::string& GRID_sno, const std::string& LUS_sno, const bool& is_special_point,
1222SN_SNOWSOIL_DATA &sno, ZwischenData &zwischenData, const bool& read_seaice)
1223{
1224// read standard values of pixel
1225if (is_special_point && !is_restart) {
1226//special points can come either from LUS snow files or GRID snow files
1227if (snowpackIO.snowCoverExists(GRID_sno, station_name)) {
1228snowpackIO.readSnowCover(GRID_sno, station_name, sno, zwischenData, read_seaice);
1229} else {
1230snowpackIO.readSnowCover(LUS_sno, station_name, sno, zwischenData, read_seaice);
1231}
1232} else {
1233if (is_restart) {
1234snowpackIO.readSnowCover(GRID_sno, station_name, sno, zwischenData, read_seaice);
1235} else {
1236snowpackIO.readSnowCover(LUS_sno, station_name, sno, zwischenData, read_seaice);
1237}
1238}
1239
1240//check that the layers are older than the start date
1241if (sno.nLayers>0 && sno.Ldata.front().depositionDate>nextStepTimestamp) {
1242ostringstream ss;
1243ss << "A layer can not be younger than the start date!";
1244if (snowpackIO.snowCoverExists(GRID_sno, station_name))
1245ss << " Please check profile '" << GRID_sno << "'";
1246else
1247ss << " Please check profile '" << LUS_sno << "'";
1248throw IOException(ss.str(), AT);
1249}
1250}
1251
1252/**
1253 * @brief Calculates lateral flow
1254 * @author Nander Wever
1255 */
1256void SnowpackInterface::calcLateralFlow()
1257{
1258std::vector<SnowStation*> snow_pixel;
1259// Retrieve snow stations
1260for (size_t ii = 0; ii < workers.size(); ii++) {
1261workers[ii]->getLateralFlow(snow_pixel);
1262}
1263// Translate lateral flow in source/sink term
1264size_t ix=0;// The source cell x coordinate
1265int ixd=-1, iyd=-1;// The destination cell x and y coordinates
1266size_t errCount = 0;
1267#pragma omp parallel for schedule(dynamic, 1) reduction(+: errCount)
1268for (size_t ii = 0; ii < workers.size(); ii++) {// Cycle over all workers
1269try {
1270for (size_t jj = 0; jj < worker_deltax[ii]; jj++) {// Cycle over x range per worker
1271for (size_t iy = 0; iy < dimy; iy++) {// Cycle over y
1272ix = worker_startx[ii] + jj;
1273const size_t index_SnowStation_src = ix * dimy + iy;// Index of source cell of water
1274double tmp_dist = -1;// Cell distance
1275if (snow_pixel[index_SnowStation_src] != NULL) {// Make sure it is not a NULL pointer (in case of skipped cells)
1276// Now determine destination cell for the water, based on azimuth
1277if ((snow_pixel[index_SnowStation_src]->meta.getAzimuth() > 337.5 && snow_pixel[index_SnowStation_src]->meta.getAzimuth() <= 360.) || (snow_pixel[index_SnowStation_src]->meta.getAzimuth() >=0. && snow_pixel[index_SnowStation_src]->meta.getAzimuth() <= 22.5)) {
1278ixd = ix;
1279iyd = iy-1;
1280tmp_dist = dem.cellsize;
1281} else if (snow_pixel[index_SnowStation_src]->meta.getAzimuth() > 22.5 || snow_pixel[index_SnowStation_src]->meta.getAzimuth() < 67.5) {
1282ixd = ix+1;
1283iyd = iy-1;
1284tmp_dist = mio::Cst::Sqrt2 * dem.cellsize;
1285} else if (snow_pixel[index_SnowStation_src]->meta.getAzimuth() > 67.5 || snow_pixel[index_SnowStation_src]->meta.getAzimuth() < 112.5) {
1286ixd = ix+1;
1287iyd = iy;
1288tmp_dist = dem.cellsize;
1289} else if (snow_pixel[index_SnowStation_src]->meta.getAzimuth() > 112.5 || snow_pixel[index_SnowStation_src]->meta.getAzimuth() < 157.5) {
1290ixd = ix+1;
1291iyd = iy+1;
1292tmp_dist = mio::Cst::Sqrt2 * dem.cellsize;
1293} else if (snow_pixel[index_SnowStation_src]->meta.getAzimuth() > 157.5 || snow_pixel[index_SnowStation_src]->meta.getAzimuth() < 202.5) {
1294ixd = ix;
1295iyd = iy+1;
1296tmp_dist = dem.cellsize;
1297} else if (snow_pixel[index_SnowStation_src]->meta.getAzimuth() > 202.5 || snow_pixel[index_SnowStation_src]->meta.getAzimuth() < 247.5) {
1298ixd = ix-1;
1299iyd = iy+1;
1300tmp_dist = mio::Cst::Sqrt2 * dem.cellsize;
1301} else if (snow_pixel[index_SnowStation_src]->meta.getAzimuth() > 247.5 || snow_pixel[index_SnowStation_src]->meta.getAzimuth() < 292.5) {
1302ixd = ix-1;
1303iyd = iy;
1304tmp_dist = dem.cellsize;
1305} else if (snow_pixel[index_SnowStation_src]->meta.getAzimuth() > 292.5 || snow_pixel[index_SnowStation_src]->meta.getAzimuth() < 337.5) {
1306ixd = ix-1;
1307iyd = iy-1;
1308tmp_dist = mio::Cst::Sqrt2 * dem.cellsize;
1309} else {
1310// Undefined aspect, don't route the lateral flow by setting destination cell outside domain
1311ixd=-1;
1312iyd=-1;
1313}
1314if (ixd >= 0 && iyd >= 0 && ixd < int(dimx) && iyd < int(dimy)) {// Check if destination cell is inside domain
1315const size_t index_SnowStation_dst = dimy * ixd + iyd;// Destination cell of water
1316if (snow_pixel[index_SnowStation_dst] != NULL) {// Make sure destination cell is not a NULL pointer (in case of skipped cells)
1317for (size_t n=0; n < snow_pixel[index_SnowStation_src]->getNumberOfElements(); n++) {// Loop over all layers in source cell
1318for (size_t nn=0; nn < snow_pixel[index_SnowStation_dst]->getNumberOfElements(); nn++) {// Loop over all layers in destination cell
1319// Now check if deposition date is equal or newer (i.e., never put lateral water in an older layer, except when we cycled over all layers and we are at the top layer)
1320if (snow_pixel[index_SnowStation_dst]->Edata[nn].depositionDate >= snow_pixel[index_SnowStation_src]->Edata[n].depositionDate
1321|| nn == snow_pixel[index_SnowStation_dst]->getNumberOfElements()-1) {
1322// The flux into the pixel is a source term for the destination cell
1323snow_pixel[index_SnowStation_dst]->Edata[nn].lwc_source += snow_pixel[index_SnowStation_src]->Edata[n].SlopeParFlux / tmp_dist * (snow_pixel[index_SnowStation_dst]->Edata[nn].L / snow_pixel[index_SnowStation_src]->Edata[n].L);
1324// The flux out of the pixel is a sink term for the source cell
1325snow_pixel[index_SnowStation_src]->Edata[n].lwc_source -= snow_pixel[index_SnowStation_src]->Edata[n].SlopeParFlux / tmp_dist * (snow_pixel[index_SnowStation_dst]->Edata[nn].L / snow_pixel[index_SnowStation_src]->Edata[n].L);
1326// Set the SlopeParFlux to zero, now that we have redistributed it.
1327snow_pixel[index_SnowStation_src]->Edata[n].SlopeParFlux = 0.;
1328break;
1329}
1330}
1331}
1332}
1333}
1334}
1335}
1336ix++;
1337}
1338} catch(const std::exception& e) {
1339++errCount;
1340cout << e.what() << std::endl;
1341}
1342}
1343
1344if (errCount>0) {
1345//something wrong took place, quitting. At least we tried writing the special points out
1346std::abort(); //force core dump
1347}
1348
1349// Send back SnowStations to the workers
1350#pragma omp parallel for schedule(dynamic, 1) reduction(+: errCount)
1351for (size_t ii = 0; ii < workers.size(); ii++) {
1352std::vector<SnowStation*> snow_pixel_out;// Construct vector to send snow stations to the associated worker
1353for (size_t jj = 0; jj < worker_deltax[ii]; jj++) {// Cycle over x range per worker
1354for (size_t iy = 0; iy < dimy; iy++) {// Cycle over y
1355ix = worker_startx[ii] + jj;
1356const size_t idx = ix * dimy + iy;// Index of snow pixel
1357snow_pixel_out.push_back( snow_pixel[idx] );
1358}
1359}
1360workers[ii]->setLateralFlow(snow_pixel_out);
1361}
1362return;
1363}

Archive Download this file

Revision: HEAD