ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
OSeedGrass2.cpp
Go to the documentation of this file.
1 //
2 // OSeedGrass2.cpp
3 //
4 /*
5 *******************************************************************************************************
6 Copyright (c) 2011, Christopher John Topping, Aarhus University
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/OSeedGrass2.h"
30 
31 
32 bool OSeedGrass2::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev )
33 {
34  m_farm = a_farm;
35  m_field = a_field;
36  m_ev = a_ev;
37  int d1;
38 
39  bool done = false;
40 
41  switch ( m_ev->m_todo ) {
42  case osg2_start:
43  {
44  OSG2_WATER_DATE = g_date->Date();
45  // Set up the date management stuff
46  m_last_date=g_date->DayInYear(15,8);
47  // Start and stop dates for all events after harvest
48  int noDates= 5;
49  m_field->SetMDates(0,0,g_date->DayInYear(10,7));
50  // Determined by harvest date - used to see if at all possible
51  m_field->SetMDates(1,0,g_date->DayInYear(20,7));
52  m_field->SetMDates(0,1,g_date->DayInYear(11,7));
53  m_field->SetMDates(1,1,g_date->DayInYear(25,7));
54  m_field->SetMDates(0,2,g_date->DayInYear(26,7));
55  m_field->SetMDates(1,2,g_date->DayInYear(20,7));
56  m_field->SetMDates(0,3,g_date->DayInYear(15,7));
57  m_field->SetMDates(1,3,g_date->DayInYear(15,8));
58  m_field->SetMDates(0,4,g_date->DayInYear(15,8));
59  m_field->SetMDates(1,4,g_date->DayInYear(20,8));
60  // Check the next crop for early start, unless it is a spring crop
61  // in which case we ASSUME that no checking is necessary!!!!
62  // So DO NOT implement a crop that runs over the year boundary
63 
64  //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)
65  if(!(m_farm->GetType() == tof_OptimisingFarm && g_date->GetYearNumber()>0)){
66 
67  if (m_ev->m_startday>g_date->DayInYear(1,7))
68  {
69  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
70  {
71  g_msg->Warn( WARN_BUG, "OSeedGrass2::Do(): " "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;
88  if ( ! m_ev->m_first_year ) d1+=365; // Add 365 for spring crop (not 1st yr)
89  if (today > d1)
90  {
91  // Yes too late - should not happen - raise an error
92  g_msg->Warn( WARN_BUG, "OSeedGrass2::Do(): "
93  "Crop start attempt after last possible start date", "" );
94  exit( 1 );
95  }
96  }//if
97 
98  // Reinit d1 to first possible starting date.
99  d1 = g_date->OldDays() + m_first_date+365; // Add 365 for spring crop
100  if ( g_date->Date() > d1 ) {
101  d1 = g_date->Date();
102  }
103  m_field->SetLastSownVeg( m_field->GetVegType() ); //Force last sown, needed for goose habitat classification
104  // OK, let's go.
105  SimpleEvent( d1, osg2_ferti_zero, false );
106  }
107  break;
108 
109  case osg2_ferti_zero:
110  if ( m_ev->m_lock || m_farm->DoIt( 60 )) {
111  if (!m_farm->FP_NPK( m_field, 0.0,
112  g_date->DayInYear( 15, 4 ) -
113  g_date->DayInYear())) {
114  SimpleEvent( g_date->Date() + 1, osg2_ferti_zero, true );
115  break;
116  }
117  }
118  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 15, 5 ),
119  osg2_water_zero, false );
120  break;
121 
122  case osg2_water_zero:
123  if ( m_ev->m_lock || m_farm->DoIt( 70 )) {
124  if ( !m_farm->Water( m_field, 0.0,
125  g_date->DayInYear( 30, 5 ) -
126  g_date->DayInYear())) {
127  SimpleEvent( g_date->Date() + 1, osg2_water_zero, true );
128  break;
129  }
130  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1, 6 ),
131  osg2_water_zero_b, false );
132  break;
133  }
134  // Didn't water, so skip second one too.
135  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 10, 7 ),
136  osg2_swarth, false );
137  break;
138 
139  case osg2_water_zero_b:
140  if ( m_ev->m_lock || m_farm->DoIt( 70 ))
141  {
142  if ( !m_farm->Water( m_field, 0.0, g_date->DayInYear( 30, 6 ) - g_date->DayInYear()))
143  {
144  SimpleEvent( g_date->Date() + 1, osg2_water_zero_b, true );
145  break;
146  }
147  }
148  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 10, 7 ), osg2_swarth, false );
149  break;
150 
151  case osg2_swarth:
152  if ( m_ev->m_lock || m_farm->DoIt( 5 )) {
153  if ( !m_farm->Swathing( m_field, 0.0,
154  g_date->DayInYear( 15, 7 ) -
155  g_date->DayInYear())) {
156  SimpleEvent( g_date->Date() + 1, osg2_swarth, true );
157  break;
158  }
159 
160  ChooseNextCrop (5);
161 
162  SimpleEvent( g_date->Date() + 2, osg2_harvest, false );
163  break;
164  }
165 
166  ChooseNextCrop (5);
167 
168  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 10, 7 ),
169  osg2_harvest, false );
170  break;
171 
172  case osg2_harvest:
173  if (m_field->GetMConstants(0)==0) {
174  if (!m_farm->Harvest( m_field, 0.0, -1)) { //raise an error
175  g_msg->Warn( WARN_BUG, "OSeedGrass2::Do(): failure in 'Harvest' execution", "" );
176  exit( 1 );
177  }
178  }
179  else {
180  if ( !m_farm->Harvest( m_field, 0.0, m_field->GetMDates(1,0) - g_date->DayInYear())) {
181  SimpleEvent( g_date->Date() + 1, osg2_harvest, false );
182  break;
183  }
184  }
185  SimpleEvent( g_date->Date(), osg2_strawchopping, false );
186  break;
187 
188  case osg2_strawchopping:
189  if ( m_ev->m_lock || m_farm->DoIt( 30 )) {
190  if (m_field->GetMConstants(0)==0) {
191  if (!m_farm->StrawChopping( m_field, 0.0, -1)) { //raise an error
192  g_msg->Warn( WARN_BUG, "OSeedGrass2::Do(): failure in 'StrawChopping' execution", "" );
193  exit( 1 );
194  }
195  }
196  else {
197  if ( !m_farm->StrawChopping( m_field, 0.0, m_field->GetMDates(1,0) - g_date->DayInYear())) {
198  SimpleEvent( g_date->Date() + 1, osg2_strawchopping, true );
199  break;
200  }
201  }
202  done=true;
203  break;
204  }
205  SimpleEvent( g_date->Date()+1, osg2_compress, false );
206  break;
207 
208  case osg2_compress:
209  if ( m_ev->m_lock || m_farm->DoIt( 60 )) {
210  if (m_field->GetMConstants(1)==0) {
211  if (!m_farm->HayTurning( m_field, 0.0, -1)) { //raise an error
212  g_msg->Warn( WARN_BUG, "OSeedGrass2::Do(): failure in 'HayTurning' execution", "" );
213  exit( 1 );
214  }
215  }
216  else {
217  if ( !m_farm->HayTurning( m_field, 0.0, m_field->GetMDates(1,1) - g_date->DayInYear())) {
218  SimpleEvent( g_date->Date() + 1, osg2_compress, true );
219  break;
220  }
221  }
222  }
223  {
224  d1 = g_date->Date();
225  if ( d1 < g_date->OldDays() + m_field->GetMDates(0,3))
226  d1 = g_date->OldDays() + m_field->GetMDates(0,3);
227  SimpleEvent( d1, osg2_burnstrawstubble, false );
228  }
229  break;
230 
232  if ( m_ev->m_lock || m_farm->DoIt( 70 )) {
233  if (m_field->GetMConstants(3)==0) {
234  if (!m_farm->BurnStrawStubble( m_field, 0.0, -1)) { //raise an error
235  g_msg->Warn( WARN_BUG, "OSeedGrass2::Do(): failure in 'BurnStrawStubble' execution", "" );
236  exit( 1 );
237  }
238  }
239  else {
240  if ( !m_farm->BurnStrawStubble( m_field, 0.0, m_field->GetMDates(1,3) - g_date->DayInYear())) {
241  SimpleEvent( g_date->Date() + 1, osg2_burnstrawstubble, true );
242  break;
243  }
244  }
245  done = true;
246  break;
247  }
248  SimpleEvent( g_date->Date(), osg2_stubbleharrow, false );
249  break;
250 
251  case osg2_stubbleharrow:
252  if (m_field->GetMConstants(4)==0) {
253  if (!m_farm->StubbleHarrowing( m_field, 0.0, -1)) { //raise an error
254  g_msg->Warn( WARN_BUG, "OSeedGrass2::Do(): failure in 'StubbleHarrowing' execution", "" );
255  exit( 1 );
256  }
257  }
258  else {
259  if ( !m_farm->StubbleHarrowing( m_field, 0.0, m_field->GetMDates(1,4) - g_date->DayInYear())) {
260  SimpleEvent( g_date->Date() + 1, osg2_stubbleharrow, true );
261  break;
262  }
263  }
264  done=true;
265  break;
266 
267  default:
268  g_msg->Warn( WARN_BUG, "OSeedGrass2::Do(): "
269  "Unknown event type! ", "" );
270  exit( 1 );
271  }
272 
273  return done;
274 }
275 
276 
osg2_compress
Definition: OSeedGrass2.h:43
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
FarmEvent
A struct to hold the information required to trigger a farm event.
Definition: farm.h:463
Farm::FP_NPK
virtual bool FP_NPK(LE *a_field, double a_user, int a_days)
Apply NPK fertilizer, on a_field owned by an arable farmer.
Definition: farmfuncs.cpp:629
Farm::DoIt
bool DoIt(double a_probability)
Return chance out of 0 to 100.
Definition: farm.cpp:800
osg2_swarth
Definition: OSeedGrass2.h:39
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
tof_OptimisingFarm
Definition: farm.h:273
osg2_stubbleharrow
Definition: OSeedGrass2.h:44
osg2_harvest
Definition: OSeedGrass2.h:40
osg2_start
Definition: OSeedGrass2.h:35
Farm::GetType
TTypesOfFarm GetType(void)
Definition: farm.h:901
Crop::m_first_date
int m_first_date
Definition: farm.h:540
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
osg2_burnstrawstubble
Definition: OSeedGrass2.h:42
Farm::StrawChopping
virtual bool StrawChopping(LE *a_field, double a_user, int a_days)
Carry out straw chopping on a_field.
Definition: farmfuncs.cpp:2132
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
osg2_water_zero
Definition: OSeedGrass2.h:37
osg2_ferti_zero
Definition: OSeedGrass2.h:36
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
osg2_water_zero_b
Definition: OSeedGrass2.h:38
Farm
The base class for all farm types.
Definition: farm.h:767
Farm::BurnStrawStubble
virtual bool BurnStrawStubble(LE *a_field, double a_user, int a_days)
Burn stubble on a_field.
Definition: farmfuncs.cpp:2236
Farm::Swathing
virtual bool Swathing(LE *a_field, double a_user, int a_days)
Cut the crop on a_field and leave it lying (probably rape)
Definition: farmfuncs.cpp:1744
osg2_strawchopping
Definition: OSeedGrass2.h:41
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
OSG2_WATER_DATE
#define OSG2_WATER_DATE
Definition: OSeedGrass2.h:32
Farm::StubbleHarrowing
virtual bool StubbleHarrowing(LE *a_field, double a_user, int a_days)
Carry out stubble harrowing on a_field.
Definition: farmfuncs.cpp:2209
OSeedGrass2::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: OSeedGrass2.cpp:32