ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
PermanentGrassLowYield.cpp
Go to the documentation of this file.
1 //
2 // PermanentGrassLowYield.cpp
3 //
4 /*
5 *******************************************************************************************************
6 Copyright (c) 2011, Christopher John Topping, University of Aarhus
7 All rights reserved.
8 
9 Redistribution and use in source and binary forms, with or without modification, are permitted provided
10 that the following conditions are met:
11 
12 Redistributions of source code must retain the above copyright notice, this list of conditions and the
13 following disclaimer.
14 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
15 the following disclaimer in the documentation and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
20 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 ********************************************************************************************************
26 */
27 
28 #include "../../Landscape/ls.h"
29 #include "../../Landscape/cropprogs/PermanentGrassLowYield.h"
30 
31 
32 extern CfgFloat cfg_ins_app_prop1;
33 extern CfgFloat cfg_herbi_app_prop;
34 extern CfgFloat cfg_fungi_app_prop1;
35 extern CfgFloat cfg_greg_app_prop;
36 
37 bool PermanentGrassLowYield::Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
38 {
39  m_farm = a_farm;
40  m_field = a_field;
41  m_ev = a_ev;
42 
43  int d1;
44 
45  bool done = false;
46 
47  switch (m_ev->m_todo) {
48  case pgly_start:
49  {
50  PGLY_CUT_DATE = 0;
51  // Set up the date management stuff
52  m_last_date = g_date->DayInYear(1, 10);
53  // Start and stop dates for all events after harvest
54  int noDates = 1;
55  // Start and stop dates for all events after harvest
56  m_field->SetMDates(0, 0, g_date->DayInYear(15, 9));
57  // 0,0 determined by harvest date - used to see if at all possible
58  m_field->SetMDates(1, 0, g_date->DayInYear(1, 10));
59  // Check the next crop for early start, unless it is a spring crop
60  // in which case we ASSUME that no checking is necessary!!!!
61  // So DO NOT implement a crop that runs over the year boundary
62 
63  //new if: do the check only for non-optimising farms and if year>0. (030713 - m_rotation used only in the hidden year, so I modified the condition from >7 to >0)
64  if (!(m_farm->GetType() == tof_OptimisingFarm && g_date->GetYearNumber() > 0)){
65 
66  if (m_ev->m_startday > g_date->DayInYear(1, 7))
67  {
68  if (m_field->GetMDates(0, 0) >= m_ev->m_startday)
69  {
70  g_msg->Warn(WARN_BUG, "PermanentGrassLowYield::Do(): "
71  "Harvest too late for the next crop to start!!!", "");
72  exit(1);
73  }
74  // Now fix any late finishing problems
75  for (int i = 0; i < noDates; i++) {
76  if (m_field->GetMDates(0, i) >= m_ev->m_startday) {
77  m_field->SetMDates(0, i, m_ev->m_startday - 1); //move the starting date
78  }
79  if (m_field->GetMDates(1, i) >= m_ev->m_startday){
80  m_field->SetMConstants(i, 0);
81  m_field->SetMDates(1, i, m_ev->m_startday - 1); //move the finishing date
82  }
83  }
84  }
85  // Now no operations can be timed after the start of the next crop.
86  int today = g_date->Date();
87  d1 = g_date->OldDays() + m_first_date + 365; // Add 365 for spring crop
88  if (today > d1)
89  {
90  // Yes too late - should not happen - raise an error
91  g_msg->Warn(WARN_BUG, " PermanentGrassLowYield::Do(): "
92  "Crop start attempt after last possible start date", "");
93  exit(1);
94  }
95  }
96  // Reinit d1 to first possible starting date.
97  d1 = g_date->OldDays() + m_first_date;
98  if (!m_ev->m_first_year) d1 += 365; // Add 365 for spring crop (not 1st yr)
99  if (g_date->Date() > d1) {
100  d1 = g_date->Date();
101  }
102  // OK, let's go.
103  m_field->SetLastSownVeg( m_field->GetVegType() ); //Force last sown, needed for goose habitat classification
104 
105  SimpleEvent(d1, pgly_cut_to_hay, false);
106  }
107  break;
108 
109  case pgly_cut_to_hay:
110  if (m_ev->m_lock || m_farm->DoIt(20))
111  {
112  if (!m_farm->CutToHay(m_field, 0.0, g_date->DayInYear(15, 6) - g_date->DayInYear())) {
113  SimpleEvent(g_date->Date() + 1, pgly_cut_to_hay, true);
114  break;
115  }
116  // did cut to hay so try turning
117  else
118  {
119  SimpleEvent(g_date->OldDays() + g_date->DayInYear(15, 6),
120  pgly_raking1, false);
121  PGLY_CUT_DATE = g_date->Date();
122  }
123  break;
124  }
125  SimpleEvent(g_date->OldDays() + m_first_date, pgly_cattle_out1, false); // non-cutters graze
126 
127  break;
128 
129  case pgly_raking1:
130  if (PGLY_CUT_DATE + 20 >= g_date->Date()) m_ev->m_lock = true;
131  if (m_ev->m_lock || m_farm->DoIt(50))
132  {
133  if (!m_farm->HayTurning(m_field, 0.0,
134  g_date->DayInYear(25, 6) - g_date->DayInYear()))
135  {
136  SimpleEvent(g_date->Date() + 1, pgly_raking1, true);
137  break;
138  }
139  SimpleEvent(g_date->Date() + 1, pgly_raking2, false);
140  break;
141  }
142  ChooseNextCrop(1);
143  SimpleEvent(PGLY_CUT_DATE + 21, pgly_cattle_out1, false); // 100% of these graze
144  break;
145 
146  case pgly_raking2:
147  if (m_ev->m_lock || m_farm->DoIt(25))
148  {
149  if (!m_farm->HayTurning(m_field, 0.0,
150  g_date->DayInYear(26, 6) - g_date->DayInYear())) {
151  SimpleEvent(g_date->Date() + 1, pgly_raking2, true);
152  break;
153  }
154  }
155  SimpleEvent(g_date->Date() + 1, pgly_compress_straw, false);
156  break;
157 
158  case pgly_compress_straw:
159  if (!m_farm->HayBailing(m_field, 0.0,
160  g_date->DayInYear(1, 7) - g_date->DayInYear()))
161  {
162  SimpleEvent(g_date->Date() + 1, pgly_compress_straw, true);
163  break;
164  }
165  // Did compress straw
166 
167  ChooseNextCrop(1);
168 
169  SimpleEvent(PGLY_CUT_DATE + 21, pgly_cattle_out1, false); // 100% of these graze
170  break;
171 
172  case pgly_cattle_out1:
173  ChooseNextCrop(1);
174 
175  if (m_field->GetMConstants(0) == 0) {
176  if (!m_farm->CattleOut(m_field, 0.0, -1)) {
177  //raise an error
178  g_msg->Warn(WARN_BUG, "PermanentGrassLowYield::Do(): failure in 'CattleOut' execution", "");
179  exit(1);
180  }
181  }
182  else {
183  if (!m_farm->CattleOut(m_field, 0.0, m_field->GetMDates(1, 0) - g_date->DayInYear())) {
184  SimpleEvent(g_date->Date() + 1, pgly_cattle_out1, true);
185  break;
186  }
187  }
188  SimpleEvent(g_date->Date() + m_field->GetMConstants(0), pgly_cattle_is_out, false);
189  break;
190 
191  case pgly_cattle_out2:
192  if ((m_ev->m_lock || m_farm->DoIt(33)))
193  {
194  if (m_field->GetMConstants(0) == 0) {
195  if (!m_farm->CattleOut(m_field, 0.0, -1)) { //raise an error
196  g_msg->Warn(WARN_BUG, "PermanentGrassLowYield::Do(): failure in 'CattleOut' execution", "");
197  exit(1);
198  }
199  }
200  else {
201  if (!m_farm->CattleOut(m_field, 0.0, m_field->GetMDates(1, 0) - g_date->DayInYear())) {
202  SimpleEvent(g_date->Date() + 1, pgly_cattle_out2, true);
203  break;
204  }
205  }
206  // Success
207  SimpleEvent(g_date->Date() + m_field->GetMConstants(0), pgly_cattle_is_out, false);
208  break;
209  }
210  //Don't graze - but don't end too early;
211  SimpleEvent(g_date->OldDays() + g_date->DayInYear(2, 7), pgly_wait, false);
212  break;
213 
214  case pgly_wait:
215  done = true;
216  break;
217 
218  case pgly_cattle_is_out:
219  if (m_field->GetMConstants(0) == 0) {
220  if (!m_farm->CattleIsOut(m_field, 0.0, -1, m_field->GetMDates(1, 0))) { //raise an error
221  //added 28.08 - issue a warning only if we fail on the last day that this can be done, i.e. MDate
222  if (g_date->Date() == m_field->GetMDates(1, 0)){
223  g_msg->Warn(WARN_BUG, "PermanentGrassLowYield::Do(): failure in 'CattleIsOut' execution", "");
224  exit(1);
225  }
226  }
227  }
228  else {
229  if (!m_farm->CattleIsOut(m_field, 0.0, m_field->GetMDates(1, 0) - g_date->DayInYear(), m_field->GetMDates(1, 0))) {
230  SimpleEvent(g_date->Date() + 1, pgly_cattle_is_out, true);
231  break;
232  }
233  }
234  // if they come in then send them out if too early
235  if (g_date->DayInYear() < g_date->DayInYear(10, 9))
236  {
237  SimpleEvent(g_date->Date() + 1, pgly_cattle_out2, true);
238  break;
239  }
240  SimpleEvent(g_date->OldDays() + g_date->DayInYear(15, 6), pgly_herbicide, false);
241  break;
242 
243  case pgly_herbicide:
244  if ((m_ev->m_lock || m_farm->DoIt((int)(5 * cfg_herbi_app_prop.value() * m_farm->Prob_multiplier()))) && //modified probability
245  // --FN--
246  (g_date->DayInYear() < g_date->DayInYear(15, 8)))
247  {
248  //new - for decision making
249  TTypesOfVegetation tov = m_field->GetVegType();
250  if (!m_ev->m_lock && !m_farm->Spraying_herbicides(tov)){
251  Field * pf = dynamic_cast<Field*>(m_field);
252  pf->Add_missed_herb_app();
253  } //end of the part for dec. making
254  else{
255  if (!m_farm->HerbicideTreat(m_field, 0.0, g_date->DayInYear(15, 8) - g_date->DayInYear())) {
256  // --FN--
257  SimpleEvent(g_date->Date() + 1, pgly_herbicide, true);
258  break;
259  }
260  }
261  }
262  SimpleEvent(g_date->OldDays() + g_date->DayInYear(15, 8),
263  pgly_cut_weeds, false);
264  break;
265 
266  case pgly_cut_weeds:
267  if ((m_ev->m_lock || m_farm->DoIt(25)) &&
268  //--FN--
269  (g_date->DayInYear() < g_date->DayInYear(15, 9)))
270  {
271  if (!m_farm->CutWeeds(m_field, 0.0,
272  g_date->DayInYear(15, 9) - g_date->DayInYear())) {
273  // --FN--
274  SimpleEvent(g_date->Date() + 1, pgly_cut_weeds, true);
275  break;
276  }
277  }
278  // END MAIN THREAD
279  done = true;
280  break;
281 
282  default:
283  g_msg->Warn(WARN_BUG, "PermanantGrassLowYield::Do(): "
284  "Unknown event type! ", "");
285  exit(1);
286  }
287  return done;
288 }
289 
290 
Farm::HerbicideTreat
virtual bool HerbicideTreat(LE *a_field, double a_user, int a_days)
Apply herbicide to a_field.
Definition: farmfuncs.cpp:1156
pgly_herbicide
Definition: PermanentGrassLowYield.h:41
FarmEvent::m_lock
bool m_lock
Definition: farm.h:465
FarmEvent
A struct to hold the information required to trigger a farm event.
Definition: farm.h:463
Farm::CattleIsOut
virtual bool CattleIsOut(LE *a_field, double a_user, int a_days, int a_max)
Generate a 'cattle_out' event for every day the cattle are on a_field.
Definition: farmfuncs.cpp:1974
PermanentGrassLowYield::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: PermanentGrassLowYield.cpp:37
Farm::DoIt
bool DoIt(double a_probability)
Return chance out of 0 to 100.
Definition: farm.cpp:800
pgly_cattle_out1
Definition: PermanentGrassLowYield.h:37
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
tof_OptimisingFarm
Definition: farm.h:273
Farm::GetType
TTypesOfFarm GetType(void)
Definition: farm.h:901
Farm::CutToHay
virtual bool CutToHay(LE *a_field, double a_user, int a_days)
Carry out hay cutting on a_field.
Definition: farmfuncs.cpp:2269
Crop::m_first_date
int m_first_date
Definition: farm.h:540
cfg_herbi_app_prop
CfgFloat cfg_herbi_app_prop
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
Crop::SimpleEvent
void SimpleEvent(long a_date, int a_todo, bool a_lock)
Adds an event to this crop management.
Definition: farm.cpp:307
PGLY_CUT_DATE
#define PGLY_CUT_DATE
Definition: PermanentGrassLowYield.h:32
Farm::Prob_multiplier
virtual double Prob_multiplier()
Definition: farm.h:786
TTypesOfVegetation
TTypesOfVegetation
Definition: tov_declaration.h:30
pgly_compress_straw
Definition: PermanentGrassLowYield.h:44
Farm::Spraying_herbicides
virtual bool Spraying_herbicides(TTypesOfVegetation)
Definition: farm.h:784
Farm::HayBailing
virtual bool HayBailing(LE *a_field, double a_user, int a_days)
Carry out hay bailing on a_field.
Definition: farmfuncs.cpp:2184
pgly_cut_to_hay
Definition: PermanentGrassLowYield.h:36
pgly_cattle_is_out
Definition: PermanentGrassLowYield.h:39
pgly_wait
Definition: PermanentGrassLowYield.h:45
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
cfg_ins_app_prop1
CfgFloat cfg_ins_app_prop1
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
cfg_fungi_app_prop1
CfgFloat cfg_fungi_app_prop1
cfg_greg_app_prop
CfgFloat cfg_greg_app_prop
Crop::m_last_date
int m_last_date
Definition: farm.h:542
pgly_raking2
Definition: PermanentGrassLowYield.h:43
Farm
The base class for all farm types.
Definition: farm.h:767
pgly_start
Definition: PermanentGrassLowYield.h:35
Crop::ChooseNextCrop
void ChooseNextCrop(int a_no_dates)
Chooses the next crop to grow in a field.
Definition: farm.cpp:318
Farm::HayTurning
virtual bool HayTurning(LE *a_field, double a_user, int a_days)
Carry out hay turning on a_field.
Definition: farmfuncs.cpp:2159
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539
pgly_cut_weeds
Definition: PermanentGrassLowYield.h:40
Farm::CattleOut
virtual bool CattleOut(LE *a_field, double a_user, int a_days)
Start a grazing event on a_field today.
Definition: farmfuncs.cpp:1910
pgly_raking1
Definition: PermanentGrassLowYield.h:42
pgly_cattle_out2
Definition: PermanentGrassLowYield.h:38
Farm::CutWeeds
virtual bool CutWeeds(LE *a_field, double a_user, int a_days)
Carry out weed topping on a_field.
Definition: farmfuncs.cpp:2303