This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
action_harvest.C
343 lines (312 loc) · 10.2 KB
/
action_harvest.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
// action_harvest.C
//
// Copyright 1996-2001 Per Abrahamsen and Søren Hansen
// Copyright 2000-2001 KVL.
// Copyright 2006 Per Abrahamsen and 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 "action.h"
#include "daisy.h"
#include "field.h"
#include "harvest.h"
#include "librarian.h"
#include "vegetation.h"
#include "treelog.h"
#include "frame.h"
#include "crop.h"
#include "mathlib.h"
#include <sstream>
// The 'emerge' action model.
struct ActionEmerge : public Action
{
const symbol crop;
void doIt (Daisy& daisy, const Scope&, Treelog& out)
{
if (crop != Vegetation::all_crops ())
{
if (daisy.field ().crop_ds (crop) < -1.0)
{
out.warning ("Attempted forced emerge of "
+ crop + " which is not on the field");
return;
}
if (daisy.field ().crop_ds (crop) >= 0.0)
{
out.warning ("Forced emerge of " + crop
+ " which is already emerged");
return;
}
}
out.message ("Forcing emergence of " + crop);
daisy.field ().emerge (crop, out);
}
void tick (const Daisy&, const Scope&, Treelog&)
{ }
void initialize (const Daisy&, const Scope&, Treelog&)
{ }
bool check (const Daisy&, const Scope&, Treelog& err) const
{ return true; }
ActionEmerge (const BlockModel& al)
: Action (al),
crop (al.name ("crop"))
{ }
};
static struct ActionEmergeSyntax : DeclareModel
{
Model* make (const BlockModel& al) const
{ return new ActionEmerge (al); }
ActionEmergeSyntax ()
: DeclareModel (Action::component, "emerge", "Force a crop to emerge.")
{ }
void load_frame (Frame& frame) const
{
frame.declare_string ("crop", Attribute::Const,
"Name of the crop to emerge.\n\
If you specify 'all', all crops will emerge.\n\
If there are no crop on the field with the specified name,\n\
nothing will happen.");
frame.set ("crop", Vegetation::all_crops ());
frame.set_check ("crop", Crop::check_all ());
frame.order ("crop");
}
} ActionEmerge_syntax;
// The 'harvest_base' base model.
static struct ActionHarvestBaseSyntax : DeclareBase
{
ActionHarvestBaseSyntax ()
: DeclareBase (Action::component, "harvest_base", "\
Common parameters for harvest operations.")
{ }
void load_frame (Frame& frame) const
{
frame.declare_string ("crop", Attribute::Const,
"Name of the crop to harvest or cut.\n\
If you specify 'all', all crops will be harvested.\n\
If there are no crop on the field with the specified name,\n\
nothing will happen.");
frame.set ("crop", Vegetation::all_crops ());
frame.set_check ("crop", Crop::check_all ());
frame.declare ("stub", "cm", Attribute::Const, "\
Leave stem and leafs below this height on the field.");
frame.set ("stub", 0.0);
frame.declare_fraction ("stem", Attribute::Const, "\
Fraction of stem (above stub) to harvest.");
frame.set ("stem", 1.0);
frame.declare_fraction ("leaf", Attribute::Const, "\
Fraction of leafs (above stub) to harvest.");
frame.set ("leaf", 1.0);
frame.declare_fraction ("sorg", Attribute::Const, "\
Fraction of storage organ to harvest.");
frame.set ("sorg", 1.0);
frame.declare_boolean ("combine", Attribute::Const, "\
Set this to 'true' in order to combine all crop parts into stem\n\
in the harvest log files.\n\
This is mostly useful for silage.");
frame.set ("combine", false);
frame.order ("crop");
}
} ActionHarvestBase_syntax;
// The 'harvest' action model.
struct ActionHarvest : public Action
{
const symbol crop;
const double stub;
const double stem;
const double leaf;
const double sorg;
const bool combine;
void doIt (Daisy& daisy, const Scope&, Treelog& msg)
{
if (crop != Vegetation::all_crops () && daisy.field ().crop_ds (crop) < 0.0)
{
msg.warning ("Attempting to harvest " + crop
+ " which has not emerged on the field");
return;
}
double old_DM = 0.0;
double old_SOrg = 0.0;
for (size_t i = 0; i < daisy.harvest ().size (); i++)
{
old_DM += daisy.harvest ()[i]->total_DM ();
old_SOrg += daisy.harvest ()[i]->sorg_DM;
}
daisy.field ().harvest (daisy.time (),
crop, stub, stem, leaf, sorg, combine,
daisy.harvest (), msg);
double new_DM = 0.0;
double new_SOrg = 0.0;
for (size_t i = 0; i < daisy.harvest ().size (); i++)
{
new_DM += daisy.harvest ()[i]->total_DM ();
new_SOrg += daisy.harvest ()[i]->sorg_DM;
}
std::ostringstream tmp;
const bool killed = daisy.field ().crop_ds (crop) < 0.0;
if (killed)
tmp << "Harvesting ";
else
tmp << "Cutting ";
tmp << crop;
const double total = (new_DM - old_DM) * 0.01;
const double sorg = (new_SOrg - old_SOrg) * 0.01;
if (total < 1e-5)
tmp << ", with no yield :-(";
else if (approximate (total, sorg))
tmp << ", removing " << total << " Mg DM/ha";
else
tmp << ", removing " << sorg << " + " << (total - sorg) << " Mg DM/ha";
msg.message (tmp.str ());
was_killed (killed, msg);
}
virtual void was_killed (const bool killed, Treelog& msg)
{
if (!killed)
msg.warning ("The crop survived harvest.\n\
If this was intended, you should use the 'cut' action instead to avoid this message");
}
void tick (const Daisy&, const Scope&, Treelog&)
{ }
void initialize (const Daisy&, const Scope&, Treelog&)
{ }
bool check (const Daisy&, const Scope&, Treelog& err) const
{ return true; }
ActionHarvest (const BlockModel& al)
: Action (al),
crop (al.name ("crop")),
stub (al.number ("stub")),
stem (al.number ("stem")),
leaf (al.number ("leaf")),
sorg (al.number ("sorg")),
combine (al.flag ("combine"))
{ }
};
static struct ActionHarvestSyntax : DeclareModel
{
Model* make (const BlockModel& al) const
{ return new ActionHarvest (al); }
ActionHarvestSyntax ()
: DeclareModel (Action::component, "harvest", "harvest_base", "\
Harvest a crop.")
{ }
void load_frame (Frame& frame) const
{ }
} ActionHarvest_syntax;
// The 'cut' action model.
struct ActionCut : public ActionHarvest
{
void was_killed (const bool killed, Treelog& msg)
{
if (killed)
msg.warning ("The crop did not survive the cut.\n\
If this was intended, you should use the 'harvest' action instead to avoid this message");
}
ActionCut (const BlockModel& al)
: ActionHarvest (al)
{ }
};
static struct ActionCutSyntax : DeclareModel
{
Model* make (const BlockModel& al) const
{ return new ActionCut (al); }
ActionCutSyntax ()
: DeclareModel (Action::component, "cut", "harvest_base", "Cut a crop.")
{ }
void load_frame (Frame& frame) const
{ }
} ActionCut_syntax;
// The 'pluck' action model.
struct ActionPluck : public Action
{
const symbol crop;
const double stem;
const double leaf;
const double sorg;
void doIt (Daisy& daisy, const Scope&, Treelog& msg)
{
if (crop != Vegetation::all_crops () && daisy.field ().crop_ds (crop) < 0.0)
{
msg.warning ("Attempting to pluck " + crop
+ " which has not emerged on the field");
return;
}
double old_DM = 0.0;
for (size_t i = 0; i < daisy.harvest ().size (); i++)
old_DM += daisy.harvest ()[i]->total_DM ();
daisy.field ().pluck (daisy.time (), crop, stem, leaf, sorg,
daisy.harvest (), msg);
double new_DM = 0.0;
for (size_t i = 0; i < daisy.harvest ().size (); i++)
new_DM += daisy.harvest ()[i]->total_DM ();
std::ostringstream tmp;
const bool killed = daisy.field ().crop_ds (crop) < 0.0;
if (killed)
tmp << "Harvesting ";
else
tmp << "Plucking ";
tmp << crop << ", removing " << (new_DM - old_DM) * 0.01 << " Mg DM/ha";
msg.message (tmp.str ());
if (killed)
msg.warning ("The crop did not survive the plucking.\n\
If this was intended, you should use the 'harvest' action instead to avoid this message");
}
void tick (const Daisy&, const Scope&, Treelog&)
{ }
void initialize (const Daisy&, const Scope&, Treelog&)
{ }
bool check (const Daisy&, const Scope&, Treelog& err) const
{ return true; }
ActionPluck (const BlockModel& al)
: Action (al),
crop (al.name ("crop")),
stem (al.number ("stem")),
leaf (al.number ("leaf")),
sorg (al.number ("sorg"))
{ }
};
static struct ActionPluckSyntax : DeclareModel
{
Model* make (const BlockModel& al) const
{ return new ActionPluck (al); }
ActionPluckSyntax ()
: DeclareModel (Action::component, "pluck", "Pluck a crop.\n\
Unlike the 'harvest' operation, this allows you to pluck selected parts of\n\
the above ground dry matter without killing the crop.\n\
It is intended for crops like tomatoes, that are harvested multiple times.")
{ }
void load_frame (Frame& frame) const
{
frame.declare_string ("crop", Attribute::Const,
"Name of the crop to pluck.\n\
If you specify 'all', all crops will be plucked.\n\
If there are no crop on the field with the specified name,\n\
nothing will happen.");
frame.set ("crop", Vegetation::all_crops ());
frame.set_check ("crop", Crop::check_all ());
frame.declare_fraction ("stem", Attribute::Const, "\
Fraction of stem to pluck.");
frame.set ("stem", 0.0);
frame.declare_fraction ("leaf", Attribute::Const, "\
Fraction of leaves to pluck.");
frame.set ("leaf", 0.0);
frame.declare_fraction ("sorg", Attribute::Const, "\
Fraction of storage organ to pluck.");
frame.set ("sorg", 1.0);
frame.order ("crop");
}
} ActionPluck_syntax;
// action_harvest.C ends here.