ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
OBarleyPeaCloverGrass.cpp
Go to the documentation of this file.
1 //
2 // OBarleyPeaCloverGrass.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/OBarleyPeaCloverGrass.h"
30 
31 extern CfgBool cfg_organic_extensive;
32 
33 bool OBarleyPeaCloverGrass::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev )
34 {
35  m_farm = a_farm;
36  m_field = a_field;
37  m_ev = a_ev;
38  int d1=0;
39 
40  bool done = false;
41 
42  switch ( m_ev->m_todo )
43  {
44  case obpcg_start:
45  {
46  // Set up the date management stuff
47  m_last_date=g_date->DayInYear(10,10);
48  // Start and stop dates for all events after harvest
49  int noDates= 3;
50  m_field->SetMDates(0,0,g_date->DayInYear(25,7));
51  // Determined by harvest date - used to see if at all possible
52  m_field->SetMDates(1,0,g_date->DayInYear(25,8));
53  m_field->SetMDates(0,1,g_date->DayInYear(10,10));
54  m_field->SetMDates(1,1,g_date->DayInYear(10,10));
55  m_field->SetMDates(0,2,g_date->DayInYear(25,7));
56  m_field->SetMDates(1,2,g_date->DayInYear(1,8));
57  // Check the next crop for early start, unless it is a spring crop
58  // in which case we ASSUME that no checking is necessary!!!!
59  // So DO NOT implement a crop that runs over the year boundary
60  if (m_ev->m_startday>g_date->DayInYear(1,7))
61  {
62  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
63  {
64  g_msg->Warn( WARN_BUG, "OBarleyPeaCloverGrass::Do(): "
65  "Harvest too late for the next crop to start!!!", "" );
66  exit( 1 );
67  }
68  // Now fix any late finishing problems
69  for (int i=0; i<noDates; i++)
70  {
71  if (m_field->GetMDates(0,i)>=m_ev->m_startday)
72  m_field->SetMDates(0,i,m_ev->m_startday-1);
73  if (m_field->GetMDates(1,i)>=m_ev->m_startday)
74  m_field->SetMDates(1,i,m_ev->m_startday-1);
75  }
76  }
77  // Now no operations can be timed after the start of the next crop.
78 
79  // Now no operations can be timed after the start of the next crop.
80 
81  int today=g_date->Date();
82  d1 = g_date->OldDays() + m_first_date;
83  if ( ! m_ev->m_first_year ) d1+=365; // Add 365 for spring crop (not 1st yr)
84  if (today > d1)
85  {
86  // Yes too late - should not happen - raise an error
87  g_msg->Warn( WARN_BUG, "OBarleyPeaCloverGrass::Do(): "
88  "Crop start attempt after last possible start date", "" );
89  exit( 1 );
90  }
91  // OK, let's go.
92  SimpleEvent( d1, obpcg_ferti_zero, false );
93  }
94  break;
95 
96  case obpcg_ferti_zero:
97  if ( m_ev->m_lock || m_farm->DoIt( 20 ))
98  {
99  if (!m_farm->FA_Manure( m_field, 0.0,
100  g_date->DayInYear( 10, 4 ) - g_date->DayInYear()))
101  {
102  SimpleEvent( g_date->Date() + 1, obpcg_ferti_zero, true );
103  break;
104  }
105  }
106  SimpleEvent( g_date->Date(),obpcg_ferti_one, false );
107  break;
108 
109  case obpcg_ferti_one:
110  if ( m_ev->m_lock || m_farm->DoIt( 100 ))
111  {
112  if (!m_farm->FA_Slurry( m_field, 0.0,
113  g_date->DayInYear( 10, 4 ) - g_date->DayInYear())) {
114  SimpleEvent( g_date->Date() + 1, obpcg_ferti_one, true );
115  break;
116  }
117  }
118  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 15, 3 ),
119  obpcg_spring_plough, false );
120  break;
121 
122  case obpcg_spring_plough:
123  if (!m_farm->SpringPlough( m_field, 0.0,
124  g_date->DayInYear( 10, 4 ) - g_date->DayInYear())) {
125  SimpleEvent( g_date->Date() + 1, obpcg_spring_plough, true );
126  break;
127  }
128  SimpleEvent( g_date->Date(), obpcg_spring_harrow, false );
129  break;
130 
131  case obpcg_spring_harrow:
132  if (!m_farm->SpringHarrow( m_field, 0.0,
133  g_date->DayInYear( 10, 4 ) - g_date->DayInYear())) {
134  SimpleEvent( g_date->Date() + 1, obpcg_spring_harrow, true );
135  break;
136  }
137  SimpleEvent( g_date->Date(), obpcg_spring_roll, false );
138  break;
139 
140  case obpcg_spring_roll:
141  if (!m_farm->SpringRoll( m_field, 0.0,
142  g_date->DayInYear( 10, 4 ) - g_date->DayInYear())) {
143  SimpleEvent( g_date->Date() + 1, obpcg_spring_roll, true );
144  break;
145  }
146  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20, 3 ),
147  obpcg_spring_sow1, false );
148  break;
149 
150  case obpcg_spring_sow1:
151  if (!m_farm->SpringSow( m_field, 0.0,
152  g_date->DayInYear( 12, 4 ) - g_date->DayInYear())) {
153  SimpleEvent( g_date->Date() + 1, obpcg_spring_sow1, true );
154  break;
155  }
156  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20,3 ),
157  obpcg_spring_sow2, false );
158  break;
159 
160  case obpcg_spring_sow2:
161  if (!m_farm->SpringSow( m_field, 0.0,
162  g_date->DayInYear( 12, 4 ) - g_date->DayInYear())) {
163  SimpleEvent( g_date->Date() + 1, obpcg_spring_sow2, true );
164  break;
165  }
166  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 5,6 ),
167  obpcg_water1, false );
168  break;
169 
170  case obpcg_water1:
171  if ( m_ev->m_lock || m_farm->DoIt( 10 ))
172  {
173  if (!m_farm->Water( m_field, 0.0,
174  g_date->DayInYear( 15,6 ) - g_date->DayInYear())) {
175  SimpleEvent( g_date->Date() + 1, obpcg_water1, true );
176  break;
177  }
178  // Must have watered so do it again
179  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 16,6 ),
180  obpcg_water2, false );
181  break;
182  }
183  // Must have failed to water so go straight to cut to silage
184  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 15,7 ),
185  obpcg_cut_to_silage, false );
186  break;
187 
188  case obpcg_water2:
189  if (!m_farm->Water( m_field, 0.0,
190  g_date->DayInYear( 30,6 ) - g_date->DayInYear())) {
191  SimpleEvent( g_date->Date() + 1, obpcg_water2, true );
192  break;
193  }
194  SimpleEvent( g_date->OldDays() + m_field->GetMDates(0,2),
195  obpcg_cut_to_silage, false );
196  break;
197 
198  case obpcg_cut_to_silage:
199  if ( m_ev->m_lock || m_farm->DoIt( 95 ))
200  {
201  if (!m_farm->CutToSilage( m_field, 0.0,
202  m_field->GetMDates(1,2) - g_date->DayInYear())) {
203  SimpleEvent( g_date->Date() + 1, obpcg_cut_to_silage, true );
204  break;
205  }
206  m_field->SetGrowthPhase( harvest1 );
207  // cjt 210503 need some special checking here to make sure we don't go
208  // over the last date possible
209  if ((g_date->DayInYear( )+21)>= m_field->GetMDates(0,1))
210  d1=m_field->GetMDates(0,1);
211  else d1=g_date->DayInYear( )+21;
212  SimpleEvent( d1,obpcg_cattle_out, false );
213  break;
214  }
215  // Did not cut for silage to do harvest instead
216  SimpleEvent( g_date->OldDays() + m_field->GetMDates( 0,0 ),
217  obpcg_harvest, false );
218  break;
219 
220  case obpcg_harvest:
221  if (!m_farm->Harvest( m_field, 0.0,
222  m_field->GetMDates(1,0) - g_date->DayInYear())) {
223  SimpleEvent( g_date->Date() + 1, obpcg_harvest, true );
224  break;
225  }
226  // cjt 210503 need some special checking here to make sure we don't go
227  // over the last date possible
228  if ((g_date->DayInYear( )+21)>= m_field->GetMDates(0,1))
229  d1=m_field->GetMDates(0,1);
230  else d1=g_date->DayInYear( )+21;
231  SimpleEvent( g_date->OldDays() + d1,obpcg_cattle_out, false );
232  break;
233 
234  case obpcg_cattle_out:
235  if ( m_ev->m_lock || m_farm->DoIt( 90 ))
236  {
237  if (cfg_organic_extensive.value()){
238  if (!m_farm->CattleOutLowGrazing( m_field, 0.0,
239  m_field->GetMDates(0,1) - g_date->DayInYear())) {
240  SimpleEvent( g_date->Date() + 1, obpcg_cattle_out, true );
241  break;
242  }
243  }
244  else {
245  if (!m_farm->CattleOut( m_field, 0.0,
246  m_field->GetMDates(0,1) - g_date->DayInYear())) {
247  SimpleEvent( g_date->Date() + 1, obpcg_cattle_out, true );
248  break;
249  }
250  }
251 
252  SimpleEvent( g_date->Date() + 1, obpcg_cattle_is_out, false );
253  break;
254  }
255  done=true;
256  // END OF MAIN THREAD
257  break;
258 
259  case obpcg_cattle_is_out:
260 
261  if (cfg_organic_extensive.value()){
262  if (!m_farm->CattleIsOutLow( m_field, 0.0,
263  m_field->GetMDates(1,1) - g_date->DayInYear(),m_field->GetMDates(1,1)))
264  {
265  SimpleEvent( g_date->Date() + 1, obpcg_cattle_is_out, false );
266  break;
267  }
268  }
269  else {
270  if (!m_farm->CattleIsOut( m_field, 0.0,
271  m_field->GetMDates(1,1) - g_date->DayInYear(),m_field->GetMDates(1,1)))
272  {
273  SimpleEvent( g_date->Date() + 1, obpcg_cattle_is_out, false );
274  break;
275  }
276  }
277  // END OF MAIN THREAD
278  done = true;
279  break;
280 
281  default:
282  g_msg->Warn( WARN_BUG, "OBarleyPeaCloverGrass::Do(): "
283  "Unknown event type! ", "" );
284  exit( 1 );
285  }
286 
287  return done;
288 }
289 
290 
Farm::SpringRoll
virtual bool SpringRoll(LE *a_field, double a_user, int a_days)
Carry out a roll event in the spring on a_field.
Definition: farmfuncs.cpp:525
Farm::FA_Slurry
virtual bool FA_Slurry(LE *a_field, double a_user, int a_days)
Spready slurry on a_field owned by an stock farmer.
Definition: farmfuncs.cpp:965
Farm::SpringPlough
virtual bool SpringPlough(LE *a_field, double a_user, int a_days)
Carry out a ploughing event in the spring on a_field.
Definition: farmfuncs.cpp:444
obpcg_harvest
Definition: OBarleyPeaCloverGrass.h:38
Farm::Harvest
virtual bool Harvest(LE *a_field, double a_user, int a_days)
Carry out a harvest on a_field.
Definition: farmfuncs.cpp:1769
FarmEvent::m_lock
bool m_lock
Definition: farm.h:465
obpcg_water2
Definition: OBarleyPeaCloverGrass.h:37
obpcg_ferti_one
Definition: OBarleyPeaCloverGrass.h:39
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
Farm::CutToSilage
virtual bool CutToSilage(LE *a_field, double a_user, int a_days)
Cut vegetation for silage on a_field.
Definition: farmfuncs.cpp:2329
Farm::DoIt
bool DoIt(double a_probability)
Return chance out of 0 to 100.
Definition: farm.cpp:800
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
Farm::FA_Manure
virtual bool FA_Manure(LE *a_field, double a_user, int a_days)
Spread manure on a_field owned by an stock farmer.
Definition: farmfuncs.cpp:1036
cfg_organic_extensive
CfgBool cfg_organic_extensive
obpcg_cattle_out
Definition: OBarleyPeaCloverGrass.h:47
Crop::m_first_date
int m_first_date
Definition: farm.h:540
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
obpcg_spring_sow1
Definition: OBarleyPeaCloverGrass.h:44
Farm::CattleIsOutLow
virtual bool CattleIsOutLow(LE *a_field, double a_user, int a_days, int a_max)
Generate a 'cattle_out_low' event for every day the cattle are on a_field.
Definition: farmfuncs.cpp:2018
Crop::SimpleEvent
void SimpleEvent(long a_date, int a_todo, bool a_lock)
Adds an event to this crop management.
Definition: farm.cpp:307
Farm::CattleOutLowGrazing
virtual bool CattleOutLowGrazing(LE *a_field, double a_user, int a_days)
Start a extensive grazing event on a_field today.
Definition: farmfuncs.cpp:1944
obpcg_spring_harrow
Definition: OBarleyPeaCloverGrass.h:41
obpcg_start
Definition: OBarleyPeaCloverGrass.h:34
obpcg_spring_sow2
Definition: OBarleyPeaCloverGrass.h:45
OBarleyPeaCloverGrass::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: OBarleyPeaCloverGrass.cpp:33
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
obpcg_spring_plough
Definition: OBarleyPeaCloverGrass.h:40
obpcg_spring_roll
Definition: OBarleyPeaCloverGrass.h:42
Farm::Water
virtual bool Water(LE *a_field, double a_user, int a_days)
Carry out a watering on a_field.
Definition: farmfuncs.cpp:1717
Crop::m_last_date
int m_last_date
Definition: farm.h:542
Farm::SpringSow
virtual bool SpringSow(LE *a_field, double a_user, int a_days)
Carry out a sowing event in the spring on a_field.
Definition: farmfuncs.cpp:546
obpcg_cattle_is_out
Definition: OBarleyPeaCloverGrass.h:48
Farm
The base class for all farm types.
Definition: farm.h:767
obpcg_water1
Definition: OBarleyPeaCloverGrass.h:36
obpcg_cut_to_silage
Definition: OBarleyPeaCloverGrass.h:46
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539
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
obpcg_ferti_zero
Definition: OBarleyPeaCloverGrass.h:35
Farm::SpringHarrow
virtual bool SpringHarrow(LE *a_field, double a_user, int a_days)
Carry out a harrow event in the spring on a_field.
Definition: farmfuncs.cpp:471