-
Notifications
You must be signed in to change notification settings - Fork 0
/
soil.C
826 lines (724 loc) · 24.4 KB
/
soil.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
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
662
663
664
665
666
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
// soil.C
//
// Copyright 1996-2004 Per Abrahamsen and Søren Hansen
// Copyright 2000-2004 KVL.
//
// This file is part of Daisy.
//
// Daisy is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// Daisy is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser Public License for more details.
//
// You should have received a copy of the GNU Lesser Public License
// along with Daisy; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#define BUILD_DLL
#include "soil.h"
#include "horizon.h"
#include "geometry.h"
#include "hydraulic.h"
#include "tortuosity.h"
#include "groundwater.h"
#include "metalib.h"
#include "library.h"
#include "frame_submodel.h"
#include "mathlib.h"
#include "librarian.h"
#include "submodeler.h"
#include "log.h"
#include "check.h"
#include "vcheck.h"
#include "plf.h"
#include "treelog.h"
#include "memutils.h"
#include "mathlib.h"
#include "secondary.h"
#include "zone.h"
#include "water.h"
#include "soil_water.h"
#include "organic.h"
#include <sstream>
struct Soil::Implementation
{
// Layers.
struct Layer
{
// Content.
const double end;
std::auto_ptr<Horizon> horizon;
// Simulation.
void output (Log& log) const
{ output_derived (horizon, "horizon", log); }
// Create and Destroy.
static void load_syntax (Frame& frame)
{
frame.declare ("end", "cm", Check::negative (), Attribute::Const,
"End point of this layer (a negative number).");
frame.declare_object ("horizon", Horizon::component,
"Soil properties of this layer.");
frame.order ("end", "horizon");
}
Layer (const Block& al)
: end (al.number ("end")),
horizon (Librarian::build_item<Horizon> (al, "horizon"))
{ }
~Layer ()
{ }
};
auto_vector<const Layer*> layers;
// Regions.
struct Region
{
// Content.
std::auto_ptr<Zone> volume;
std::auto_ptr<Horizon> horizon;
// Simulation.
void output (Log& log) const
{ output_derived (horizon, "horizon", log); }
// Create and Destroy.
static void load_syntax (Frame& frame)
{
frame.declare_object ("volume", Zone::component,
"Volume covered by this zone.");
frame.declare_object ("horizon", Horizon::component,
"Soil properties of this zone.");
frame.order ("volume", "horizon");
}
Region (const Block& al)
: volume (Librarian::build_item<Zone> (al, "volume")),
horizon (Librarian::build_item<Horizon> (al, "horizon"))
{ }
~Region ()
{ }
};
const auto_vector<const Region*> zones;
// Access.
std::vector<Horizon*> horizon_;
// Volume
std::map<const Horizon*, double> volume;
// Parameters
/* const */ double MaxRootingDepth;
const double dispersivity;
const double dispersivity_transversal;
const std::vector<double> border;
const double frozen_water_K_factor; // []
// Cache.
std::vector<double> anisotropy_edge;
bool has_attribute (const symbol name, Treelog& msg) const
{
bool missing = false;
for (size_t i = 0; i < layers.size (); i++)
if (!layers[i]->horizon->has_attribute (name))
{
msg.error ("Required attribute '"
+ name + "' is missing from the soil horizon '"
+ layers[i]->horizon->objid + "'");
missing = true;
}
for (size_t i = 0; i < zones.size (); i++)
if (!zones[i]->horizon->has_attribute (name))
{
msg.error ("Required attribute '"
+ name + "' is missing from the soil zone '"
+ zones[i]->horizon->objid + "'");
missing = true;
}
return !missing;
}
bool has_attribute (const symbol name) const
{
bool missing = false;
for (size_t i = 0; i < layers.size (); i++)
if (!layers[i]->horizon->has_attribute (name))
missing = true;
for (size_t i = 0; i < zones.size (); i++)
if (!zones[i]->horizon->has_attribute (name))
missing = true;
return !missing;
}
void tillage (const Geometry& geo, const double from, const double to,
const double surface_loose, const double RR0,
const SoilWater& soil_water,
const OrganicMatter& organic_matter)
{
// Water content.
std::map<const Horizon*, double> water;
for (std::size_t c = 0; c < geo.cell_size (); c++)
if (geo.fraction_in_z_interval (c, from, to) > 0.01)
water[horizon_[c]] += soil_water.Theta (c) * geo.cell_volume (c);
static const double c_fraction_in_humus = 0.587;
static const double aom_from = 0.0; // [cm]
static const double aom_to = -15.0; // [cm]
const double AOM_C = organic_matter.AOM_C (geo, aom_from, aom_to)
/ geo.surface_area (); // [g C/cm^2]
const double AOM15 = 10.0 * AOM_C / c_fraction_in_humus; // [kg/m^2]
for (std::map<const Horizon*, double>::const_iterator i = water.begin ();
i != water.end ();
i++)
{
const Horizon *const hor = (*i).first;
const double total_water = (*i).second;
const double total_volume = volume[hor];
daisy_assert (total_water < total_volume);
const double Theta = total_water / total_volume;
hor->hydraulic->tillage (surface_loose, RR0, Theta, AOM15);
}
}
void tick (const double dt, const double rain, const Geometry& geo,
const SoilWater& soil_water, Treelog& msg)
{
// Ice content.
std::map<const Horizon*, double> ice;
for (std::size_t c = 0; c < geo.cell_size (); c++)
ice[horizon_[c]] += soil_water.X_ice (c) * geo.cell_volume (c);
for (std::map<const Horizon*, double>::const_iterator i = ice.begin ();
i != ice.end ();
i++)
{
const Horizon *const hor = (*i).first;
const double total_ice = (*i).second;
const double total_volume = volume[hor];
const double Ice = total_ice / total_volume;
hor->hydraulic->tick (dt, rain, Ice, msg);
}
}
// Create and Destroy.
Implementation (const Block& al)
: layers (map_submodel_const<Layer> (al, "horizons")),
zones (map_submodel_const<Region> (al, "zones")),
MaxRootingDepth (al.number ("MaxRootingDepth")),
dispersivity (al.number ("dispersivity")),
dispersivity_transversal (al.number ("dispersivity_transversal",
dispersivity * 0.1)),
border (al.number_sequence ("border")),
frozen_water_K_factor (al.number ("frozen_water_K_factor"))
{ }
~Implementation ()
{ }
};
static DeclareSubmodel
soil_layer_submodel (Soil::Implementation::Layer::load_syntax, "SoilLayer", "\
A location and content of a soil layer.\n\
The layers apply to the soil section not covered by the 'zones' parameter.");
static DeclareSubmodel
soil_zone_submodel (Soil::Implementation::Region::load_syntax, "SoilRegion", "\
A location and content of a soil zone.\n\
If several zones cover the same soil, the first one listed is used.\n\
If no zones cover the soil, the 'horizons' parameter is used.\n\
\n\
With regard to the numeric discretization, the whole cell is assumed to\n\
be of the soil found in the cell center.");
size_t
Soil::size () const
{ return impl->horizon_.size (); }
const Horizon&
Soil::horizon (size_t i) const
{ return *impl->horizon_[i]; }
double
Soil::K (size_t i, double h, double h_ice, double T) const
{
static struct ViscosityFactor : public PLF
{
ViscosityFactor ()
{
const double v20 = Water::viscosity (20.0);
add ( 0.0, v20 / Water::viscosity (0.0));
add ( 5.0, v20 / Water::viscosity (5.0));
add (10.0, v20 / Water::viscosity (10.0));
add (15.0, v20 / Water::viscosity (15.0));
add (20.0, v20 / Water::viscosity (20.0));
add (25.0, v20 / Water::viscosity (25.0));
add (30.0, v20 / Water::viscosity (30.0));
add (35.0, v20 / Water::viscosity (35.0));
add (40.0, v20 / Water::viscosity (40.0));
}
} viscosity_factor;
const double T_factor = (T < 0.0)
? impl->frozen_water_K_factor
: viscosity_factor (T);
const double h_water = std::min (h, h_ice);
return horizon (i).K (h_water) * T_factor;
}
double
Soil::Cw1 (size_t i, double h, double h_ice) const
{ return Theta (i, h, h_ice) - Cw2 (i, h) * h; }
double
Soil::Cw2 (size_t i, double h) const
{
const double answer = horizon (i).hydraulic->Cw2 (h);
if (answer > 0.0)
return answer;
// We divide with this.
return 1.0e-8;
}
double Soil::Theta (size_t i, double h, double h_ice) const
{
if (h < h_ice)
return horizon (i).hydraulic->Theta (h);
else
return horizon (i).hydraulic->Theta (h_ice);
}
double
Soil::Theta_res (size_t i) const
{ return horizon (i).hydraulic->Theta_res; }
double
Soil::Theta_sat (size_t i) const
{ return horizon (i).hydraulic->Theta_sat; }
double
Soil::h (size_t i, double Theta) const
{ return horizon (i).hydraulic->h (Theta); }
double
Soil::M (size_t i, double h) const
{ return horizon (i).hydraulic->M (h); }
double
Soil::primary_sorption_fraction (size_t c) const
{ return horizon (c).primary_sorption_fraction (); }
double
Soil::dispersivity (size_t) const
{ return impl->dispersivity; }
double
Soil::dispersivity_transversal (size_t c) const
{ return impl->dispersivity_transversal; }
void
Soil::tillage (const Geometry& geo, const double from, const double to,
const double surface_loose, const double RR0,
const SoilWater& soil_water,
const OrganicMatter& organic_matter)
{ impl->tillage (geo, from, to, surface_loose, RR0,
soil_water, organic_matter); }
void
Soil::tick (const double dt, const double rain, const Geometry& geo,
const SoilWater& soil_water, Treelog& msg)
{ impl->tick (dt, rain, geo, soil_water, msg); }
void
Soil::set_porosity (size_t i, double Theta)
{ horizon (i).hydraulic->set_porosity (Theta); }
double // Activation pressure for secondary domain. [cm]
Soil::h_secondary (size_t i) const
{ return horizon (i).secondary_domain ().h_lim (); }
double // Exchange rate between primary and secondary water. [h^-1]
Soil::alpha (size_t i) const
{ return horizon (i).secondary_domain ().alpha (); }
double
Soil::tortuosity_factor (size_t i, double Theta) const
{ return horizon (i).tortuosity->factor (*horizon (i).hydraulic, Theta); }
double
Soil::anisotropy_cell (size_t c) const
{ return horizon (c).anisotropy (); }
double
Soil::anisotropy_edge (size_t e) const
{
daisy_assert (impl->anisotropy_edge.size () > e);
return impl->anisotropy_edge[e];
}
double
Soil::dry_bulk_density (size_t i) const
{ return horizon (i).dry_bulk_density (); }
double
Soil::clay (size_t i) const
{ return horizon (i).clay (); }
double
Soil::texture_below (size_t i, double size) const
{ return horizon (i).texture_below (size); }
double
Soil::humus (size_t i) const
{ return horizon (i).humus (); }
double
Soil::humus_C (size_t i) const
{ return horizon (i).humus_C (); }
const std::vector<double>&
Soil::SOM_fractions (size_t i) const
{ return horizon (i).SOM_fractions (); }
const std::vector<double>&
Soil::SOM_C_per_N (size_t i) const
{ return horizon (i).SOM_C_per_N (); }
double
Soil::C_per_N (size_t i) const
{ return horizon (i).C_per_N (); }
double
Soil::turnover_factor (size_t i) const
{ return horizon (i).turnover_factor (); }
double
Soil::heat_conductivity (size_t i, double Theta, double Ice) const
{ return horizon (i).heat_conductivity (Theta, Ice); }
double
Soil::heat_capacity (size_t i, double Theta, double Ice) const
{ return horizon (i).heat_capacity (Theta, Ice); }
bool
Soil::has_attribute (const symbol name, Treelog& msg) const
{ return impl->has_attribute (name, msg); }
bool
Soil::has_attribute (const symbol name) const
{ return impl->has_attribute (name); }
bool
Soil::has_attribute (size_t i, const symbol name) const
{ return horizon (i).has_attribute (name); }
double
Soil::get_attribute (size_t i, const symbol name) const
{ return horizon (i).get_attribute (name); }
symbol
Soil::get_dimension (size_t i, const symbol name) const
{ return horizon (i).get_dimension (name); }
void
Soil::append_attributes (size_t i, std::set<symbol>& all) const
{ horizon (i).append_attributes (all); }
void
Soil::output (Log& log) const
{
static const symbol horizons_symbol ("horizons");
if (log.check_interior (horizons_symbol))
{
Log::Open open (log, horizons_symbol);
for (size_t i = 0; i < impl->layers.size (); i++)
{
Log::Unnamed unnamed (log);
impl->layers[i]->output (log);
}
}
static const symbol zones_symbol ("zones");
if (log.check_interior (zones_symbol))
{
Log::Open open (log, zones_symbol);
for (size_t i = 0; i < impl->zones.size (); i++)
{
Log::Unnamed unnamed (log);
impl->zones[i]->output (log);
}
}
}
void
Soil::nitrification (const size_t i,
const double M, const double C,
const double h, const double T,
double& NH4, double& N2O, double& NO3) const
{ horizon (i).nitrification (M, C, h, T, NH4, N2O, NO3); }
double
Soil::MaxRootingHeight () const
{
return -impl->MaxRootingDepth;
}
double
Soil::end_of_first_horizon () const
{
daisy_assert (impl->layers.size () > 0);
return impl->layers[0]->end;
}
bool
Soil::check (const int som_size, Geometry& geo, Treelog& err) const
{
bool ok = true;
if (som_size >= 0)
{
{
Treelog::Open nest (err, "horizons");
for (size_t i = 0; i < impl->layers.size (); i++)
{
const Horizon& horizon = *impl->layers[i]->horizon;
Treelog::Open nest (err, horizon.objid);
const size_t f_size = horizon.SOM_fractions ().size ();
if (f_size > 0 && f_size != som_size)
{
Treelog::Open nest (err, "SOM_fractions");
std::ostringstream tmp;
tmp << "Need " << som_size << " fractions, got " << f_size;
err.error (tmp.str ());
ok = false;
}
const size_t n_size = horizon.SOM_C_per_N ().size ();
if (n_size != som_size)
{
Treelog::Open nest (err, "SOM_C_per_N");
std::ostringstream tmp;
tmp << "Need " << som_size << " C/N numbers, got " << n_size;
err.error (tmp.str ());
ok = false;
}
}
}
{
Treelog::Open nest (err, "zones");
for (size_t i = 0; i < impl->zones.size (); i++)
{
const Horizon& horizon = *impl->zones[i]->horizon;
Treelog::Open nest (err, horizon.objid);
const size_t f_size = horizon.SOM_fractions ().size ();
if (f_size > 0 && f_size != som_size)
{
Treelog::Open nest (err, "SOM_fractions");
std::ostringstream tmp;
tmp << "Need " << som_size << " fractions, got " << f_size;
err.error (tmp.str ());
ok = false;
}
const size_t n_size = horizon.SOM_C_per_N ().size ();
if (n_size != som_size)
{
Treelog::Open nest (err, "SOM_C_per_N");
std::ostringstream tmp;
tmp << "Need " << som_size << " C/N numbers, got " << n_size;
err.error (tmp.str ());
ok = false;
}
}
}
}
bool geo_ok = true;
for (size_t i = 0; i < geo.cell_size (); i++)
if (impl->horizon_[i] == NULL)
geo_ok = false;
if (!geo_ok)
{
Treelog::Open nest (err, "horizons");
err.error ("Some cells have no associated horizon");
ok = false;
}
return ok;
}
bool
Soil::check_z_border (const double value, Treelog& err) const
{
bool ok = false;
for (size_t i = 0; i < impl->border.size (); i++)
if (approximate (value, impl->border[i]))
ok = true;
if (!ok)
{
std::ostringstream tmp;
tmp << "No soil border near " << value
<< " [cm], log results may be inexact";
err.warning (tmp.str ());
}
return ok;
}
bool
Soil::check_x_border (const double, Treelog&) const
{ return true; }
bool
Soil::check_y_border (const double, Treelog&) const
{ return true; }
static bool
check_alist (const Metalib&, const Frame& al, Treelog& err)
{
bool ok = true;
const std::vector<boost::shared_ptr<const FrameSubmodel>/**/>& layers
= al.submodel_sequence ("horizons");
if (layers.size () < 1U)
{
err.entry ("You need at least one horizon");
ok = false;
}
double last = 0.0;
for (size_t i = 0; i < layers.size (); i++)
{
double end = layers[i]->number ("end");
if (end >= last)
{
err.entry ("Horizon endpoints must be monotonically decreasing");
ok = false;
break;
}
last = end;
}
return ok;
}
void
Soil::load_syntax (Frame& frame)
{
frame.add_check (check_alist);
frame.declare_submodule_sequence ("horizons", Attribute::State, "\
Layered description of the soil properties.\n\
The horizons can be overlapped by the 'zones' parameter.\n\
Some groundwater models, specifically 'pipe', may cause an extra horizon to\n\
be added below the one specified here if you do not also specify an explicit\n\
geometry.",
Implementation::Layer::load_syntax);
frame.declare_submodule_sequence ("zones", Attribute::State, "\
Regions with special soil properties.\n\
This overrules the 'horizons' paramter.",
Implementation::Region::load_syntax);
frame.set_empty ("zones");
frame.declare ("MaxRootingDepth", "cm", Check::positive (), Attribute::Const,
"Depth at the end of the root zone (a positive number).");
frame.declare ("dispersivity", "cm", Check::positive (),
Attribute::Const, "Dispersion length.");
frame.set ("dispersivity", 5.0);
frame.declare ("dispersivity_transversal", "cm", Check::non_negative (),
Attribute::OptionalConst, "Transversal dispersion length.\n\
By default, this is 0.1 times the dispersivity.");
frame.declare ("border", "cm", Check::negative (),
Attribute::Const, Attribute::Variable, "\
List of flux depths where a mass balance should be possible when logging.\n\
This attribute is ignored if the geometry is specified explicitly.");
frame.set_check ("border", VCheck::decreasing ());
std::vector<double> default_borders;
default_borders.push_back (-100.0);
frame.set ("border", default_borders);
frame.declare ("frozen_water_K_factor", Attribute::None (),
Check::positive (),
Attribute::OptionalConst, "\
Hydraulic conductivity for water below 0 dg C compared to 20 dg C.\n\
The default value, 0.561, corresponds to 0 dg C water viscosity.");
frame.set ("frozen_water_K_factor", 0.561);
}
Soil::Soil (const Block& al)
: impl (new Implementation (al))
{ }
void
Soil::initialize (const Time& time, Geometry& geo,
Groundwater& groundwater,
const int som_size, Treelog& msg)
{
Treelog::Open nest (msg, "Soil");
const bool volatile_bottom =
groundwater.bottom_type () == Groundwater::lysimeter;
const std::vector<const Implementation::Layer*>::const_iterator begin
= impl->layers.begin ();
const std::vector<const Implementation::Layer*>::const_iterator end
= impl->layers.end ();
daisy_assert (begin != end);
std::vector<const Implementation::Layer*>::const_iterator layer;
// Initialize zone horizons.
for (int i = 0; i < impl->zones.size (); i++)
{
const Zone& zone = *impl->zones[i]->volume;
const double center_z = zone.center_z ();
const bool top_soil = center_z > -20; // Center of zone within plow layer
impl->zones[i]->horizon->initialize (top_soil, som_size, center_z, msg);
}
// Initialize geometry and layer horizons.
std::vector<double> fixed;
{
Treelog::Open nest (msg, "Horizons");
double last = 0.0;
size_t next_border = 0;
for (layer = begin; layer != end; layer++)
{
const double current = (*layer)->end;
daisy_assert (current < last);
const bool top_soil = (layer == begin);
const double center_z = 0.5 * (current + last); // center of layer.
(*layer)->horizon->initialize (top_soil, som_size, center_z, msg);
while (next_border < impl->border.size ()
&& current < impl->border[next_border])
{
if (last > impl->border[next_border])
fixed.push_back (impl->border[next_border]);
next_border++;
}
last = current;
fixed.push_back (last);
}
if (-last < impl->MaxRootingDepth)
impl->MaxRootingDepth = -last;
}
geo.initialize_zplus (volatile_bottom, fixed, -impl->MaxRootingDepth,
2 * impl->dispersivity, msg);
const size_t cell_size = geo.cell_size ();
// Initialize horizons.
impl->horizon_.insert (impl->horizon_.end (), cell_size, NULL);
daisy_assert (impl->horizon_.size () == cell_size);
// Check zones first.
for (size_t c = 0; c < cell_size; c++)
{
for (size_t i = 0; i < impl->zones.size (); i++)
if (impl->zones[i]->volume->contain_point (geo.cell_z (c),
geo.cell_x (c), geo.cell_y (c)))
{
daisy_assert (impl->horizon_[c] == NULL);
impl->horizon_[c] = impl->zones[i]->horizon.get ();
break;
}
}
// Fill in missing stuff by layers.
double last = 0.0;
for (layer = begin; layer != end; layer++)
{
Horizon *const h = (*layer)->horizon.get ();
const double next = (*layer)->end;
for (size_t i = 0; i < cell_size; i++)
{
if (impl->horizon_[i] != NULL)
// Already defined by a zone.
continue;
const double z = geo.cell_z (i);
if (last > z && z >= next)
{
daisy_assert (impl->horizon_[i] == NULL);
impl->horizon_[i] = h;
}
}
last = next;
}
for (size_t i = 0; i < cell_size; i++)
{
// Sanity check.
std::ostringstream tmp;
tmp << "cell[" << i << "] of " << cell_size
<< " z = " << geo.cell_z (i) << ", last = " << last;
Treelog::Open nest (msg, tmp.str ());
daisy_assert (impl->horizon_[i] != NULL);
// Count volume for all horizons.
impl->volume[impl->horizon_[i]] +=geo.cell_volume (i);
}
for (int i = 0; i < impl->zones.size (); i++)
{
const Horizon* h = impl->zones[i]->horizon.get ();
if (impl->volume[h] > 0.0)
continue;
std::ostringstream tmp;
tmp << "zone '" << h->objid << "' has volume of " << impl->volume[h];
msg.warning (tmp.str ());
}
for (layer = begin; layer != end; layer++)
{
const Horizon* h = (*layer)->horizon.get ();
if (impl->volume[h] > 0.0)
continue;
std::ostringstream tmp;
tmp << "horizon '" << h->objid << "' has volume of " << impl->volume[h];
msg.warning (tmp.str ());
}
// anisotropy_edge.
const size_t edge_size = geo.edge_size ();
for (size_t e = 0; e < edge_size; e++)
{
const int from = geo.edge_from (e);
const int to = geo.edge_to (e);
// External edges.
if (!geo.cell_is_internal (from))
{
daisy_assert (geo.cell_is_internal (to));
impl->anisotropy_edge.push_back (anisotropy_cell (to));
continue;
}
if (!geo.cell_is_internal (to))
{
daisy_assert (geo.cell_is_internal (from));
impl->anisotropy_edge.push_back (anisotropy_cell (from));
continue;
}
// Internal edges.
const double sin_angle = geo.edge_sin_angle (e);
const double cos_angle = geo.edge_cos_angle (e);
const double a_from = anisotropy_cell (from);
const double a_to = anisotropy_cell (to);
// Arithmetic average. Because we don't know.
const double factor = (a_from + a_to) / 2.0;
// Geometric average. Because it is a geometric problem.
const double aniso = sqrt (sqr (sin_angle) + sqr (factor * cos_angle));
impl->anisotropy_edge.push_back (aniso);
}
daisy_assert (impl->anisotropy_edge.size () == edge_size);
}
Soil::~Soil ()
{ }
static DeclareSubmodel
soil_submodel (Soil::load_syntax, "Soil", "\
The soil submodel provides the numeric and physical properties of the soil.");
// soil.C ends here.