forked from perabrahamsen/daisy-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adsorption_freundlich.C
179 lines (164 loc) · 5.23 KB
/
adsorption_freundlich.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
// adsorption_freundlich.C
//
// Copyright 1996-2001 Per Abrahamsen and Søren Hansen
// Copyright 2000-2001 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 "adsorption.h"
#include "block_model.h"
#include "soil.h"
#include "check.h"
#include "mathlib.h"
#include "librarian.h"
#include "treelog.h"
#include "frame.h"
#include "units.h"
#include "metalib.h"
#include <sstream>
static const double c_fraction_in_humus = 0.587;
class AdsorptionFreundlich : public Adsorption
{
// Parameters.
const double K_clay;
const double K_OC;
const double C_ref;
const double m;
// Simulation.
public:
double C_to_M (const Soil&, double Theta, int, double C, double sf) const;
double M_to_C (const Soil&, double Theta, int, double M, double sf) const;
// Create.
public:
AdsorptionFreundlich (const BlockModel& al)
: Adsorption (al),
K_clay (al.number ("K_clay", 0.0)),
K_OC (al.number ("K_OC", K_clay)),
C_ref (al.number ("C_ref")),
m (al.number ("m"))
{ }
};
double
AdsorptionFreundlich::C_to_M (const Soil& soil,
double Theta, int i, double C, double sf) const
{
daisy_assert (std::isfinite (C));
if (C <= 0.0)
{
if (C < 0.0)
{
std::ostringstream tmp;
tmp << "C = " << C << "; should be >= 0.0";
Assertion::warning (tmp.str ());
}
return 0.0;
}
daisy_assert (Theta >= 0.0);
const double K = soil.clay (i) * K_clay
+ soil.humus (i) * c_fraction_in_humus * K_OC;
const double rho = soil.dry_bulk_density (i);
return sf * rho * K * C_ref * pow (C / C_ref, m) + Theta * C;
}
double
AdsorptionFreundlich::M_to_C (const Soil& soil,
double Theta, int i, double M, double sf) const
{
// Check for zero.
if (iszero (M))
return 0.0;
daisy_assert (M > 0.0);
// Guess start boundary.
double min_C = 0.0;
double min_M = C_to_M (soil, Theta, i, min_C, sf);
double max_C = 1.0;
double max_M = C_to_M (soil, Theta, i, max_C, sf);
// Find upper boundary by doubling repeatedly.
while (max_M < M)
{
max_C *= 2;
daisy_assert (max_C > 0.0); // Overlow detection.
max_M = C_to_M (soil, Theta, i, max_C, sf);
}
// Guess by middling the C value.
while (!approximate (min_M, max_M))
{
const double new_C = (min_C + max_C) / 2.0;
daisy_assert (new_C >= 0.0);
const double new_M = C_to_M (soil, Theta, i, new_C, sf);
if (new_M < M)
{
daisy_assert (min_C < new_C);
min_C = new_C;
min_M = new_M;
}
else
{
daisy_assert (max_C > new_C);
max_C = new_C;
max_M = new_M;
}
}
return (min_C + max_C) / 2.0;
}
static struct AdsorptionFreundlichSyntax : DeclareModel
{
Model* make (const BlockModel& al) const
{ return new AdsorptionFreundlich (al); }
static bool check_alist (const Metalib&, const Frame& al, Treelog& err)
{
bool ok = true;
const bool has_K_clay = al.check ("K_clay");
const bool has_K_OC = al.check ("K_OC");
if (!has_K_clay && !has_K_OC)
{
err.entry ("You must specify either 'K_clay' or 'K_OC'");
ok = false;
}
return ok;
}
AdsorptionFreundlichSyntax ()
: DeclareModel (Adsorption::component, "Freundlich", "\
M = rho K C_ref (C / C_ref)^m + Theta C.\n\
Formula from FOCUS page 93.")
{ }
void load_frame (Frame& frame) const
{
frame.add_check (check_alist);
frame.set_strings ("cite", "freundlich1939adsorption", "focus2000");
frame.declare ("K_clay", "cm^3/g", Check::non_negative (),
Attribute::OptionalConst,
"Clay dependent distribution parameter.\n\
It is multiplied with the soil clay fraction to get the clay part of\n \
the 'K' factor. If 'K_OC' is specified, 'K_clay' defaults to 0.");
frame.declare ("K_OC", "cm^3/g", Check::non_negative (),
Attribute::OptionalConst,
"Humus dependent distribution parameter.\n\
It is multiplied with the soil organic carbon fraction to get the\n\
carbon part of the 'K' factor. By default, 'K_OC' is equal to 'K_clay'.");
frame.declare_number_cited ("m", Attribute::None (), Check::positive (),
Attribute::Const, Attribute::Singleton, "\
Freundlich parameter (1/n).\n\
If not known, FOCUS recommend a value of 0.9 (page 93).",
"focus2000");
frame.declare_number_cited ("C_ref", "g/cm^3", Check::positive (),
Attribute::Const, Attribute::Singleton, "\
Reference concentration for determining the other parameters.\n\
Accrding to FOCUS, the usual value is 1 mg/L (page 93).",
"focus2000");
}
} AdsorptionFreundlich_syntax;
// adsorption_freundlich.C ends here.